]> Pileus Git - ~andy/gtk/blob - examples/colorsel/colorsel.c
Use gtk_scale_new() instead gtk_[v|h]scale_new()
[~andy/gtk] / examples / colorsel / colorsel.c
1
2 #include <glib.h>
3 #include <gdk/gdk.h>
4 #include <gtk/gtk.h>
5
6 GtkWidget *colorseldlg = NULL;
7 GtkWidget *drawingarea = NULL;
8 GdkColor color;
9
10 /* Color changed handler */
11
12 static void color_changed_cb( GtkWidget         *widget,
13                               GtkColorSelection *colorsel )
14 {
15   GdkColor ncolor;
16
17   gtk_color_selection_get_current_color (colorsel, &ncolor);
18   gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &ncolor);       
19 }
20
21 /* Drawingarea event handler */
22
23 static gboolean area_event( GtkWidget *widget,
24                             GdkEvent  *event,
25                             gpointer   client_data )
26 {
27   gint handled = FALSE;
28   gint response;
29   GtkColorSelection *colorsel;
30   GtkColorSelectionDialog *selection_dialog;
31
32   /* Check if we've received a button pressed event */
33
34   if (event->type == GDK_BUTTON_PRESS)
35     {
36       handled = TRUE;
37
38        /* Create color selection dialog */
39       if (colorseldlg == NULL)
40         colorseldlg = gtk_color_selection_dialog_new ("Select background color");
41
42       /* Get the ColorSelection widget */
43       selection_dialog = GTK_COLOR_SELECTION_DIALOG (colorseldlg);
44       colorsel = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_colorsel (selection_dialog));
45
46       gtk_color_selection_set_previous_color (colorsel, &color);
47       gtk_color_selection_set_current_color (colorsel, &color);
48       gtk_color_selection_set_has_palette (colorsel, TRUE);
49
50       /* Connect to the "color-changed" signal, set the client-data
51        * to the colorsel widget */
52       g_signal_connect (colorsel, "color-changed",
53                         G_CALLBACK (color_changed_cb),
54                         colorsel);
55
56       /* Show the dialog */
57       response = gtk_dialog_run (GTK_DIALOG (colorseldlg));
58
59       if (response == GTK_RESPONSE_OK)
60         gtk_color_selection_get_current_color (colorsel, &color);
61       else 
62         gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &color);
63
64       gtk_widget_hide (colorseldlg);
65     }
66
67   return handled;
68 }
69
70 /* Close down and exit handler */
71
72 static gboolean destroy_window( GtkWidget *widget,
73                                 GdkEvent  *event,
74                                 gpointer   client_data )
75 {
76   gtk_main_quit ();
77   return TRUE;
78 }
79
80 /* Main */
81
82 gint main( gint   argc,
83            gchar *argv[] )
84 {
85   GtkWidget *window;
86
87   /* Initialize the toolkit, remove gtk-related commandline stuff */
88
89   gtk_init (&argc, &argv);
90
91   /* Create toplevel window, set title and policies */
92
93   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
94   gtk_window_set_title (GTK_WINDOW (window), "Color selection test");
95   gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
96
97   /* Attach to the "delete" and "destroy" events so we can exit */
98
99   g_signal_connect (window, "delete_event",
100                     G_CALLBACK (destroy_window), (gpointer) window);
101   
102   /* Create drawingarea, set size and catch button events */
103
104   drawingarea = gtk_drawing_area_new ();
105
106   color.red = 0;
107   color.blue = 65535;
108   color.green = 0;
109   gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &color);       
110
111   gtk_widget_set_size_request (GTK_WIDGET (drawingarea), 200, 200);
112
113   gtk_widget_set_events (drawingarea, GDK_BUTTON_PRESS_MASK);
114
115   g_signal_connect (drawingarea, "event",
116                     G_CALLBACK (area_event), (gpointer) drawingarea);
117   
118   /* Add drawingarea to window, then show them both */
119
120   gtk_container_add (GTK_CONTAINER (window), drawingarea);
121
122   gtk_widget_show (drawingarea);
123   gtk_widget_show (window);
124   
125   /* Enter the gtk main loop (this never returns) */
126
127   gtk_main ();
128
129   /* Satisfy grumpy compilers */
130
131   return 0;
132 }