1 #include <gtk/gtk.h>
2 
3 
4 /*Callback function in which reacts to the "response" signal. Be sure to place
5 it before the function it is called in*/
6 static void
on_response(GtkDialog * dialog,gint response_id,gpointer user_data)7 on_response (GtkDialog *dialog,
8              gint       response_id,
9              gpointer   user_data)
10 {
11   /*For demonstration purposes, this will show the int value
12   of the response type*/
13   g_print ("response is %d\n", response_id);
14 
15   /*This will cause the dialog to be destroyed*/
16   gtk_widget_destroy (GTK_WIDGET (dialog));
17 }
18 
19 
20 
21 /*Callback function in which reacts to the "clicked" signal*/
22 static void
show_dialog(GtkButton * button,gpointer user_data)23 show_dialog (GtkButton *button,
24              gpointer   user_data)
25 {
26   GtkWindow *window = user_data;
27   GtkWidget *dialog;
28   GtkWidget *content_area;
29   GtkWidget *label;
30 
31   gint response_id;
32 
33   /*Create the dialog window. Modal windows prevent interaction with other
34   windows in the same application*/
35   dialog = gtk_dialog_new_with_buttons ("A Gtk+ Dialog",
36                                         window,
37                                         GTK_DIALOG_MODAL,
38                                         GTK_STOCK_OK,
39                                         GTK_RESPONSE_OK,
40                                         NULL);
41 
42   /*Create a label and attach it to the content area of the dialog*/
43   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
44   label = gtk_label_new ("This demonstrates a dialog with a label");
45   gtk_container_add (GTK_CONTAINER (content_area), label);
46 
47   /*The main purpose of this is to show dialog's child widget, label*/
48   gtk_widget_show_all (dialog);
49 
50   /*Connecting the "response" signal from the user to the associated
51   callback function*/
52   g_signal_connect (GTK_DIALOG (dialog),
53                     "response",
54                     G_CALLBACK (on_response),
55                     NULL);
56 
57 }
58 
59 
60 
61 static void
activate(GtkApplication * app,gpointer user_data)62 activate (GtkApplication *app,
63           gpointer        user_data)
64 {
65   GtkWidget *window;
66   GtkWidget *button;
67 
68   /*Create a window with a title and a default size*/
69   window = gtk_application_window_new (app);
70   gtk_window_set_title (GTK_WINDOW (window), "GNOME Button");
71   gtk_window_set_default_size (GTK_WINDOW (window), 250, 50);
72 
73   /*Create a button with a label, and add it to the window*/
74   button = gtk_button_new_with_label ("Click Me");
75   gtk_container_add (GTK_CONTAINER (window), button);
76 
77   /*Connecting the clicked signal to the callback*/
78   g_signal_connect (GTK_BUTTON (button),
79                     "clicked",
80                     G_CALLBACK (show_dialog),
81                     GTK_WINDOW (window));
82 
83   gtk_widget_show_all (window);
84 }
85 
86 
87 
88 int
main(int argc,char ** argv)89 main (int argc, char **argv)
90 {
91   GtkApplication *app;
92   int status;
93 
94   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
95   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
96   status = g_application_run (G_APPLICATION (app), argc, argv);
97   g_object_unref (app);
98 
99   return status;
100 }
101