0

I am coding a GUI (using GTK3) for a simple program that performs a simulation and plots the requested graph (using gnuplot).

When I'm using only gnuplot, I have no problem. The graph looks exactly like I want. However, when I try using this same code from GTK, it doesn't work anymore. It truncates all my value. Why ? What should I do to fix this?

This is the expected result (gnuplot without GTK): expected

And this is the actual result (gnuplot with GTK) : actual

#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show_all (window);

  FILE *gp = popen("gnuplot -persist", "w");
  fprintf(gp, "plot '-' using 1:2 with lines\n");

  int i;
  for(i = 0; i < 13; i++){
    fprintf(gp, "%d %f\n", i, 0.1*i);
  }

  pclose(gp);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}
Ethan
  • 13,715
  • 2
  • 12
  • 21
  • This is way too much code to read. Can you modify `on_button_clicked` to dump the gnuplot script to a file rather than piping to gnuplot? Inspection of that file should narrow down whether the problem is with your code or with gnuplot. Either way, what we need is a [mcve], emphasis here on *minimal*. – Nate Eldredge Mar 24 '19 at 18:26
  • One note is that `%lf` traditionally should just be `%f`; unlike `scanf`, the `printf` family of functions by default expects `double` for floating-point conversions. With a C99 library `%lf` should also work, but it's possibly one thing to check. See https://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f – Nate Eldredge Mar 24 '19 at 18:28
  • 1
    @NateEldredge I reduced the code. I also dumped the function to a file and it seems like it uses a comma instead of a point as a decimal separator. So I think that's the problem. Thank you. However, how can I change it? – user3410806 Mar 24 '19 at 18:55
  • Aha. The decimal point character follows your locale, so perhaps that is getting set differently when gtk is used. See https://stackoverflow.com/questions/7684014/c-printf-with-f-but-localized-for-the-users-country. I guess what you want is to use the "C" locale for this `fprintf` call only. I don't offhand know an easy way to do that, but you could ask a new question. – Nate Eldredge Mar 24 '19 at 19:05
  • You may also want to edit this question's title, since it seems clear now that this isn't gnuplot's fault :-) – Nate Eldredge Mar 24 '19 at 19:09
  • @NateEldredge Changing only the C locale didn't work. However, with some googling I found this function : void gtk_disable_setlocale (void); And now it works !! Thank you. Also, for the title, what do you suggest, I'm quite bad at finding a title. – user3410806 Mar 24 '19 at 19:16
  • Maybe "fprintf uses comma as decimal point but I need a dot" – Nate Eldredge Mar 24 '19 at 19:46
  • For what it's worth, you could accommodate this on the gnuplot end with the gnuplot command `set decimalsign locale`. – Ethan Mar 25 '19 at 00:56

0 Answers0