]> Pileus Git - ~andy/gtk/blob - examples/buttons/buttons.c
Removed g_object_pointer_hash, which was just g_direct_hash.
[~andy/gtk] / examples / buttons / buttons.c
1 /* This file extracted from the GTK tutorial. */
2
3 /* buttons.c */
4
5 #include <gtk/gtk.h>
6
7 /* create a new hbox with an image and a label packed into it
8  * and return the box.. */
9
10 GtkWidget *xpm_label_box (GtkWidget *parent, gchar *xpm_filename, gchar *label_text)
11 {
12     GtkWidget *box1;
13     GtkWidget *label;
14     GtkWidget *pixmapwid;
15     GdkPixmap *pixmap;
16     GdkBitmap *mask;
17     GtkStyle *style;
18
19     /* create box for xpm and label */
20     box1 = gtk_hbox_new (FALSE, 0);
21     gtk_container_border_width (GTK_CONTAINER (box1), 2);
22
23     /* get style of button.. I assume it's to get the background color.
24      * if someone knows the real reason, please enlighten me. */
25     style = gtk_widget_get_style(parent);
26
27     /* now on to the xpm stuff.. load xpm */
28     pixmap = gdk_pixmap_create_from_xpm (parent->window, &mask,
29                                          &style->bg[GTK_STATE_NORMAL],
30                                          xpm_filename);
31     pixmapwid = gtk_pixmap_new (pixmap, mask);
32
33     /* create label for button */
34     label = gtk_label_new (label_text);
35
36     /* pack the pixmap and label into the box */
37     gtk_box_pack_start (GTK_BOX (box1),
38                         pixmapwid, FALSE, FALSE, 3);
39
40     gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);
41
42     gtk_widget_show(pixmapwid);
43     gtk_widget_show(label);
44
45     return (box1);
46 }
47
48 /* our usual callback function */
49 void callback (GtkWidget *widget, gpointer data)
50 {
51     g_print ("Hello again - %s was pressed\n", (char *) data);
52 }
53
54
55 int main (int argc, char *argv[])
56 {
57     /* GtkWidget is the storage type for widgets */
58     GtkWidget *window;
59     GtkWidget *button;
60     GtkWidget *box1;
61
62     gtk_init (&argc, &argv);
63
64     /* create a new window */
65     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
66
67     gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
68
69     /* It's a good idea to do this for all windows. */
70     gtk_signal_connect (GTK_OBJECT (window), "destroy",
71                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
72
73     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
74                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
75
76
77     /* sets the border width of the window. */
78     gtk_container_border_width (GTK_CONTAINER (window), 10);
79     gtk_widget_realize(window);
80
81     /* create a new button */
82     button = gtk_button_new ();
83
84     /* You should be getting used to seeing most of these functions by now */
85     gtk_signal_connect (GTK_OBJECT (button), "clicked",
86                         GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");
87
88     /* this calls our box creating function */
89     box1 = xpm_label_box(window, "info.xpm", "cool button");
90
91     /* pack and show all our widgets */
92     gtk_widget_show(box1);
93
94     gtk_container_add (GTK_CONTAINER (button), box1);
95
96     gtk_widget_show(button);
97
98     gtk_container_add (GTK_CONTAINER (window), button);
99
100     gtk_widget_show (window);
101
102     /* rest in gtk_main and wait for the fun to begin! */
103     gtk_main ();
104
105     return 0;
106 }