Advertisement
Guest User

ptomato

a guest
Mar 11th, 2010
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. /*
  2.  * COMPILE ME WITH:
  3.  * gcc -o eventloop eventloop.c `pkg-config --cflags --libs gtk+-2.0`
  4.  */
  5.  
  6. #include <gtk/gtk.h>
  7.  
  8. typedef struct {
  9.     GtkWidget *window, *pushbutton, *stopbutton, *indicator, *randnumentry,
  10.         *indicatorlabel, *randnumlabel, *windowlabel, *vbox1, *hbox, *vbox2;
  11. } Widgets;
  12.  
  13. /* Forward declarations */
  14. static void on_push(GtkButton *pushbutton, Widgets *w);
  15. static void on_stop(GtkButton *stopbutton, Widgets *w);
  16. static gboolean on_quit(GtkWidget *window, GdkEvent *event, Widgets *w);
  17. static gboolean on_timer(Widgets *w);
  18. static gboolean on_user_event(Widgets *w);
  19.  
  20. int
  21. main(int argc, char **argv)
  22. {
  23.     gtk_init(&argc, &argv);
  24.    
  25.     /* Construct the "controls" of our "front panel" */
  26.     Widgets *w = g_new0(Widgets, 1);
  27.     w->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  28.     w->pushbutton = gtk_button_new_with_label("Push!");
  29.     w->stopbutton = gtk_button_new_with_label("STOP");
  30.     w->indicator = gtk_image_new_from_stock(GTK_STOCK_NO, GTK_ICON_SIZE_BUTTON);
  31.     w->randnumentry = gtk_entry_new();
  32.     w->indicatorlabel = gtk_label_new("x > y?");
  33.     w->randnumlabel = gtk_label_new("Randnum");
  34.     w->windowlabel = gtk_label_new("Try to close this front panel\n"
  35.         "while the VI is running.");
  36.    
  37.     /* Change the properties of some of the controls */
  38.     gtk_container_set_border_width(GTK_CONTAINER(w->window), 12);
  39.     gtk_entry_set_width_chars(GTK_ENTRY(w->randnumentry), 8);
  40.     gtk_editable_set_editable(GTK_EDITABLE(w->randnumentry), FALSE);
  41.     gtk_label_set_justify(GTK_LABEL(w->windowlabel), GTK_JUSTIFY_CENTER);
  42.    
  43.     /* Put the controls together */
  44.     w->vbox1 = gtk_vbox_new(FALSE, 6);
  45.     w->hbox = gtk_hbox_new(FALSE, 6);
  46.     w->vbox2 = gtk_vbox_new(FALSE, 6);
  47.     gtk_box_pack_start(GTK_BOX(w->vbox2), w->indicatorlabel, FALSE, FALSE, 0);
  48.     gtk_box_pack_start(GTK_BOX(w->vbox2), w->indicator, FALSE, FALSE, 0);
  49.     gtk_box_pack_start(GTK_BOX(w->vbox2), w->randnumlabel, FALSE, FALSE, 0);
  50.     gtk_box_pack_start(GTK_BOX(w->vbox2), w->randnumentry, FALSE, FALSE, 0);
  51.     gtk_box_pack_start(GTK_BOX(w->hbox), w->pushbutton, TRUE, TRUE, 0);
  52.     gtk_box_pack_start(GTK_BOX(w->hbox), w->stopbutton, TRUE, TRUE, 0);
  53.     gtk_box_pack_start(GTK_BOX(w->hbox), w->vbox2, TRUE, TRUE, 0);
  54.     gtk_box_pack_start(GTK_BOX(w->vbox1), w->hbox, TRUE, TRUE, 0);
  55.     gtk_box_pack_start(GTK_BOX(w->vbox1), w->windowlabel, TRUE, TRUE, 0);
  56.     gtk_container_add(GTK_CONTAINER(w->window), w->vbox1);
  57.     gtk_widget_show_all(w->window);
  58.    
  59.     /* Connect the signals. Here we define what functions get called when
  60.     certain events happen. */
  61.     g_signal_connect(w->pushbutton, "clicked", G_CALLBACK(on_push), w);
  62.     g_signal_connect(w->stopbutton, "clicked", G_CALLBACK(on_stop), w);
  63.     g_signal_connect(w->window, "delete-event", G_CALLBACK(on_quit), w);
  64.     /* The function on_timer() takes the place of the LabVIEW code's while loop
  65.     containing the 3000 ms wait. The main loop will call on_timer() every
  66.     3000 milliseconds. */
  67.     g_timeout_add(3000, (GSourceFunc)on_timer, w);
  68.    
  69.     /* This transfers control to GTK's main loop. From here on, the only parts
  70.     of this code that are executed are those that respond to GUI events or the
  71.     3000 millisecond timer. */
  72.     gtk_main();
  73.    
  74.     g_free(w);
  75.     return 0;
  76. }
  77.  
  78. static void
  79. on_push(GtkButton *pushbutton, Widgets *w)
  80. {
  81.     /* Show a dialog */
  82.     GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
  83.         GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "You pressed Push!");
  84.     gtk_dialog_run(GTK_DIALOG(dialog));
  85.     gtk_widget_destroy(dialog);
  86. }
  87.  
  88. static void
  89. on_stop(GtkButton *stopbutton, Widgets *w)
  90. {
  91.     /* Tell the main loop to quit. The timeout function will be stopped
  92.     automatically, so we don't need to do that here. */
  93.     gtk_main_quit();
  94. }
  95.  
  96. static gboolean
  97. on_quit(GtkWidget *window, GdkEvent *event, Widgets *w)
  98. {
  99.     /* Show a dialog */
  100.     GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
  101.         GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "A FilterEvent is not going to let "
  102.         "you close this window!");
  103.     gtk_dialog_run(GTK_DIALOG(dialog));
  104.     gtk_widget_destroy(dialog);
  105.    
  106.     /* By returning TRUE from this callback, we filter this event, stopping it
  107.     from closing our window. */
  108.     return TRUE;
  109. }
  110.  
  111. static gboolean
  112. on_timer(Widgets *w)
  113. {
  114.     gdouble randnum = g_random_double();
  115.    
  116.     /* Put our number into the "randnum" indicator */
  117.     gchar *text = g_strdup_printf("%0.6f", randnum);
  118.     gtk_entry_set_text(GTK_ENTRY(w->randnumentry), text);
  119.     g_free(text);
  120.    
  121.     if(randnum > 0.85)
  122.     {
  123.         /* This is our user-defined event. It's not implemented as a signal like
  124.         the other events, but as an idle function, since only objects can have
  125.         signals in GTK. */
  126.         g_idle_add((GSourceFunc)on_user_event, w);
  127.     }
  128.  
  129.     /* By returning TRUE from this event, we keep it repeating every 3 sec. */
  130.     return TRUE;
  131. }
  132.  
  133. static gboolean
  134. on_user_event(Widgets *w)
  135. {
  136.     /* Change the "x > y?" indicator to light up */
  137.     gtk_image_set_from_stock(GTK_IMAGE(w->indicator), GTK_STOCK_YES,
  138.         GTK_ICON_SIZE_BUTTON);
  139.    
  140.     /* Show a dialog */
  141.     GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
  142.         GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Randnum > 0.85");
  143.     gtk_dialog_run(GTK_DIALOG(dialog));
  144.     gtk_widget_destroy(dialog);
  145.    
  146.     /* By returning FALSE from this event, we indicate that we only want it
  147.     executed once. */
  148.     return FALSE;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement