Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * COMPILE ME WITH:
- * gcc -o eventloop eventloop.c `pkg-config --cflags --libs gtk+-2.0`
- */
- #include <gtk/gtk.h>
- typedef struct {
- GtkWidget *window, *pushbutton, *stopbutton, *indicator, *randnumentry,
- *indicatorlabel, *randnumlabel, *windowlabel, *vbox1, *hbox, *vbox2;
- } Widgets;
- /* Forward declarations */
- static void on_push(GtkButton *pushbutton, Widgets *w);
- static void on_stop(GtkButton *stopbutton, Widgets *w);
- static gboolean on_quit(GtkWidget *window, GdkEvent *event, Widgets *w);
- static gboolean on_timer(Widgets *w);
- static gboolean on_user_event(Widgets *w);
- int
- main(int argc, char **argv)
- {
- gtk_init(&argc, &argv);
- /* Construct the "controls" of our "front panel" */
- Widgets *w = g_new0(Widgets, 1);
- w->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- w->pushbutton = gtk_button_new_with_label("Push!");
- w->stopbutton = gtk_button_new_with_label("STOP");
- w->indicator = gtk_image_new_from_stock(GTK_STOCK_NO, GTK_ICON_SIZE_BUTTON);
- w->randnumentry = gtk_entry_new();
- w->indicatorlabel = gtk_label_new("x > y?");
- w->randnumlabel = gtk_label_new("Randnum");
- w->windowlabel = gtk_label_new("Try to close this front panel\n"
- "while the VI is running.");
- /* Change the properties of some of the controls */
- gtk_container_set_border_width(GTK_CONTAINER(w->window), 12);
- gtk_entry_set_width_chars(GTK_ENTRY(w->randnumentry), 8);
- gtk_editable_set_editable(GTK_EDITABLE(w->randnumentry), FALSE);
- gtk_label_set_justify(GTK_LABEL(w->windowlabel), GTK_JUSTIFY_CENTER);
- /* Put the controls together */
- w->vbox1 = gtk_vbox_new(FALSE, 6);
- w->hbox = gtk_hbox_new(FALSE, 6);
- w->vbox2 = gtk_vbox_new(FALSE, 6);
- gtk_box_pack_start(GTK_BOX(w->vbox2), w->indicatorlabel, FALSE, FALSE, 0);
- gtk_box_pack_start(GTK_BOX(w->vbox2), w->indicator, FALSE, FALSE, 0);
- gtk_box_pack_start(GTK_BOX(w->vbox2), w->randnumlabel, FALSE, FALSE, 0);
- gtk_box_pack_start(GTK_BOX(w->vbox2), w->randnumentry, FALSE, FALSE, 0);
- gtk_box_pack_start(GTK_BOX(w->hbox), w->pushbutton, TRUE, TRUE, 0);
- gtk_box_pack_start(GTK_BOX(w->hbox), w->stopbutton, TRUE, TRUE, 0);
- gtk_box_pack_start(GTK_BOX(w->hbox), w->vbox2, TRUE, TRUE, 0);
- gtk_box_pack_start(GTK_BOX(w->vbox1), w->hbox, TRUE, TRUE, 0);
- gtk_box_pack_start(GTK_BOX(w->vbox1), w->windowlabel, TRUE, TRUE, 0);
- gtk_container_add(GTK_CONTAINER(w->window), w->vbox1);
- gtk_widget_show_all(w->window);
- /* Connect the signals. Here we define what functions get called when
- certain events happen. */
- g_signal_connect(w->pushbutton, "clicked", G_CALLBACK(on_push), w);
- g_signal_connect(w->stopbutton, "clicked", G_CALLBACK(on_stop), w);
- g_signal_connect(w->window, "delete-event", G_CALLBACK(on_quit), w);
- /* The function on_timer() takes the place of the LabVIEW code's while loop
- containing the 3000 ms wait. The main loop will call on_timer() every
- 3000 milliseconds. */
- g_timeout_add(3000, (GSourceFunc)on_timer, w);
- /* This transfers control to GTK's main loop. From here on, the only parts
- of this code that are executed are those that respond to GUI events or the
- 3000 millisecond timer. */
- gtk_main();
- g_free(w);
- return 0;
- }
- static void
- on_push(GtkButton *pushbutton, Widgets *w)
- {
- /* Show a dialog */
- GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
- GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "You pressed Push!");
- gtk_dialog_run(GTK_DIALOG(dialog));
- gtk_widget_destroy(dialog);
- }
- static void
- on_stop(GtkButton *stopbutton, Widgets *w)
- {
- /* Tell the main loop to quit. The timeout function will be stopped
- automatically, so we don't need to do that here. */
- gtk_main_quit();
- }
- static gboolean
- on_quit(GtkWidget *window, GdkEvent *event, Widgets *w)
- {
- /* Show a dialog */
- GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
- GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "A FilterEvent is not going to let "
- "you close this window!");
- gtk_dialog_run(GTK_DIALOG(dialog));
- gtk_widget_destroy(dialog);
- /* By returning TRUE from this callback, we filter this event, stopping it
- from closing our window. */
- return TRUE;
- }
- static gboolean
- on_timer(Widgets *w)
- {
- gdouble randnum = g_random_double();
- /* Put our number into the "randnum" indicator */
- gchar *text = g_strdup_printf("%0.6f", randnum);
- gtk_entry_set_text(GTK_ENTRY(w->randnumentry), text);
- g_free(text);
- if(randnum > 0.85)
- {
- /* This is our user-defined event. It's not implemented as a signal like
- the other events, but as an idle function, since only objects can have
- signals in GTK. */
- g_idle_add((GSourceFunc)on_user_event, w);
- }
- /* By returning TRUE from this event, we keep it repeating every 3 sec. */
- return TRUE;
- }
- static gboolean
- on_user_event(Widgets *w)
- {
- /* Change the "x > y?" indicator to light up */
- gtk_image_set_from_stock(GTK_IMAGE(w->indicator), GTK_STOCK_YES,
- GTK_ICON_SIZE_BUTTON);
- /* Show a dialog */
- GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(w->window), 0,
- GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Randnum > 0.85");
- gtk_dialog_run(GTK_DIALOG(dialog));
- gtk_widget_destroy(dialog);
- /* By returning FALSE from this event, we indicate that we only want it
- executed once. */
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement