diff options
author | BST 1998 Tony Gale <gale@gtk.org> | 1998-08-13 13:11:14 +0000 |
---|---|---|
committer | Tony Gale <gale@src.gnome.org> | 1998-08-13 13:11:14 +0000 |
commit | e8e7692534cb51972a905fa6304e3adfe29a80db (patch) | |
tree | 93873d3349e6f03956bff01f3b3a41b22146c66b /examples | |
parent | 7a235648ea4282a784669004fe60d859725a322d (diff) | |
download | gdk-pixbuf-e8e7692534cb51972a905fa6304e3adfe29a80db.tar.gz |
- Tidy up of the menufactory example from Andy Kahn <kahn@zk3.dec.com> -
Thu Aug 13 09:11:11 BST 1998 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml:
- Tidy up of the menufactory example from
Andy Kahn <kahn@zk3.dec.com>
- New section on Range Widgets from
David Huggins-Daines <bn711@freenet.carleton.ca>
- Started a new section on 'Advanced Event and Signal
Handling' - used an email from Owen.
- New appendix on Gdk Event Types
- Added the tictactoe full example code to the
'Code Examples' appendix
Diffstat (limited to 'examples')
-rw-r--r-- | examples/menu/menufactory.c | 145 | ||||
-rw-r--r-- | examples/menu/menufactory.h | 9 | ||||
-rw-r--r-- | examples/menu/mfmain.c | 12 | ||||
-rw-r--r-- | examples/menu/mfmain.h | 6 | ||||
-rw-r--r-- | examples/rangewidgets/Makefile | 8 | ||||
-rw-r--r-- | examples/rangewidgets/rangewidgets.c | 287 |
6 files changed, 330 insertions, 137 deletions
diff --git a/examples/menu/menufactory.c b/examples/menu/menufactory.c index 9f72e1e92..a7f781959 100644 --- a/examples/menu/menufactory.c +++ b/examples/menu/menufactory.c @@ -1,17 +1,11 @@ -/* This file extracted from the GTK tutorial. */ - -/* menufactory.c */ +/* example-start menu menufactory.c */ #include <gtk/gtk.h> #include <strings.h> #include "mfmain.h" - -static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path); -static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path); -void menus_init(void); -void menus_create(GtkMenuEntry * entries, int nmenu_entries); +static void print_hello(GtkWidget *widget, gpointer data); /* this is the GtkMenuEntry structure used to create new menus. The @@ -24,128 +18,37 @@ void menus_create(GtkMenuEntry * entries, int nmenu_entries); static GtkMenuEntry menu_items[] = { - {"<Main>/File/New", "<control>N", NULL, NULL}, - {"<Main>/File/Open", "<control>O", NULL, NULL}, - {"<Main>/File/Save", "<control>S", NULL, NULL}, - {"<Main>/File/Save as", NULL, NULL, NULL}, - {"<Main>/File/<separator>", NULL, NULL, NULL}, - {"<Main>/File/Quit", "<control>Q", file_quit_cmd_callback, "OK, I'll quit"}, - {"<Main>/Options/Test", NULL, NULL, NULL} + {"<Main>/File/New", "<control>N", print_hello, NULL}, + {"<Main>/File/Open", "<control>O", print_hello, NULL}, + {"<Main>/File/Save", "<control>S", print_hello, NULL}, + {"<Main>/File/Save as", NULL, NULL, NULL}, + {"<Main>/File/<separator>", NULL, NULL, NULL}, + {"<Main>/File/Quit", "<control>Q", file_quit_cmd_callback, "OK, I'll quit"}, + {"<Main>/Options/Test", NULL, NULL, NULL} }; -/* calculate the number of menu_item's */ -static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]); -static int initialize = TRUE; -static GtkMenuFactory *factory = NULL; -static GtkMenuFactory *subfactory[1]; -static GHashTable *entry_ht = NULL; - -void get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table) +static void +print_hello(GtkWidget *widget, gpointer data) { - if (initialize) - menus_init(); - - if (menubar) - *menubar = subfactory[0]->widget; - if (table) - *table = subfactory[0]->table; + printf("hello!\n"); } -void menus_init(void) +void get_main_menu(GtkWidget *window, GtkWidget ** menubar) { - if (initialize) { - initialize = FALSE; - - factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); - subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); - - gtk_menu_factory_add_subfactory(factory, subfactory[0], "<Main>"); - menus_create(menu_items, nmenu_items); - } -} + int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]); + GtkMenuFactory *factory; + GtkMenuFactory *subfactory; -void menus_create(GtkMenuEntry * entries, int nmenu_entries) -{ - char *accelerator; - int i; - - if (initialize) - menus_init(); - - if (entry_ht) - for (i = 0; i < nmenu_entries; i++) { - accelerator = g_hash_table_lookup(entry_ht, entries[i].path); - if (accelerator) { - if (accelerator[0] == '\0') - entries[i].accelerator = NULL; - else - entries[i].accelerator = accelerator; - } - } - gtk_menu_factory_add_entries(factory, entries, nmenu_entries); - - for (i = 0; i < nmenu_entries; i++) - if (entries[i].widget) { - gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator", - (GtkSignalFunc) menus_install_accel, - entries[i].path); - gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator", - (GtkSignalFunc) menus_remove_accel, - entries[i].path); - } -} + factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); + subfactory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); -static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path) -{ - char accel[64]; - char *t1, t2[2]; - - accel[0] = '\0'; - if (modifiers & GDK_CONTROL_MASK) - strcat(accel, "<control>"); - if (modifiers & GDK_SHIFT_MASK) - strcat(accel, "<shift>"); - if (modifiers & GDK_MOD1_MASK) - strcat(accel, "<alt>"); + gtk_menu_factory_add_subfactory(factory, subfactory, "<Main>"); + gtk_menu_factory_add_entries(factory, menu_items, nmenu_items); + gtk_window_add_accelerator_table(GTK_WINDOW(window), subfactory->table); - t2[0] = key; - t2[1] = '\0'; - strcat(accel, t2); - - if (entry_ht) { - t1 = g_hash_table_lookup(entry_ht, path); - g_free(t1); - } else - entry_ht = g_hash_table_new(g_str_hash, g_str_equal); - - g_hash_table_insert(entry_ht, path, g_strdup(accel)); - - return TRUE; -} - -static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path) -{ - char *t; - - if (entry_ht) { - t = g_hash_table_lookup(entry_ht, path); - g_free(t); - - g_hash_table_insert(entry_ht, path, g_strdup("")); - } + if (menubar) + *menubar = subfactory->widget; } -void menus_set_sensitive(char *path, int sensitive) -{ - GtkMenuPath *menu_path; - - if (initialize) - menus_init(); - - menu_path = gtk_menu_factory_find(factory, path); - if (menu_path) - gtk_widget_set_sensitive(menu_path->widget, sensitive); - else - g_warning("Unable to set sensitivity for menu which doesn't exist: %s", path); -} +/* example-end */ diff --git a/examples/menu/menufactory.h b/examples/menu/menufactory.h index e1569dae4..acd04684e 100644 --- a/examples/menu/menufactory.h +++ b/examples/menu/menufactory.h @@ -1,6 +1,4 @@ -/* This file extracted from the GTK tutorial. */ - -/* menufactory.h */ +/* example-start menu menufactory.h */ #ifndef __MENUFACTORY_H__ #define __MENUFACTORY_H__ @@ -9,11 +7,12 @@ extern "C" { #endif /* __cplusplus */ -void get_main_menu (GtkWidget **menubar, GtkAcceleratorTable **table); -void menus_create(GtkMenuEntry *entries, int nmenu_entries); +void get_main_menu (GtkWidget *, GtkWidget **menubar); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* __MENUFACTORY_H__ */ + +/* example-end */ diff --git a/examples/menu/mfmain.c b/examples/menu/mfmain.c index cbe0c5840..5777632ee 100644 --- a/examples/menu/mfmain.c +++ b/examples/menu/mfmain.c @@ -1,21 +1,16 @@ -/* This file extracted from the GTK tutorial. */ - -/* mfmain.c */ +/* example-start menu mfmain.c */ #include <gtk/gtk.h> #include "mfmain.h" #include "menufactory.h" - int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *main_vbox; GtkWidget *menubar; - GtkAcceleratorTable *accel; - gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); @@ -30,8 +25,7 @@ int main(int argc, char *argv[]) gtk_container_add(GTK_CONTAINER(window), main_vbox); gtk_widget_show(main_vbox); - get_main_menu(&menubar, &accel); - gtk_window_add_accelerator_table(GTK_WINDOW(window), accel); + get_main_menu(window, &menubar); gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0); gtk_widget_show(menubar); @@ -50,3 +44,5 @@ void file_quit_cmd_callback (GtkWidget *widget, gpointer data) g_print ("%s\n", (char *) data); gtk_exit(0); } + +/* example-end */ diff --git a/examples/menu/mfmain.h b/examples/menu/mfmain.h index fe481b0c1..83fc0e3a4 100644 --- a/examples/menu/mfmain.h +++ b/examples/menu/mfmain.h @@ -1,6 +1,4 @@ -/* This file extracted from the GTK tutorial. */ - -/* mfmain.h */ +/* example-start menu mfmain.h */ #ifndef __MFMAIN_H__ #define __MFMAIN_H__ @@ -17,3 +15,5 @@ void file_quit_cmd_callback(GtkWidget *widget, gpointer data); #endif /* __cplusplus */ #endif /* __MFMAIN_H__ */ + +/* example-end */ diff --git a/examples/rangewidgets/Makefile b/examples/rangewidgets/Makefile new file mode 100644 index 000000000..1259faef4 --- /dev/null +++ b/examples/rangewidgets/Makefile @@ -0,0 +1,8 @@ + +CC = gcc + +rangewidgets: rangewidgets.c + $(CC) `gtk-config --cflags` `gtk-config --libs` rangewidgets.c -o rangewidgets + +clean: + rm -f *.o rangewidgets diff --git a/examples/rangewidgets/rangewidgets.c b/examples/rangewidgets/rangewidgets.c new file mode 100644 index 000000000..d59c8b273 --- /dev/null +++ b/examples/rangewidgets/rangewidgets.c @@ -0,0 +1,287 @@ +/* example-start rangewidgets rangewidgets.c */ + +#include <gtk/gtk.h> + +GtkWidget *hscale, *vscale; + +void cb_pos_menu_select (GtkWidget *item, GtkPositionType pos) +{ + /* set the value position on both scale widgets */ + gtk_scale_set_value_pos (GTK_SCALE (hscale), pos); + gtk_scale_set_value_pos (GTK_SCALE (vscale), pos); +} + +void cb_update_menu_select (GtkWidget *item, GtkUpdateType policy) +{ + /* set the update policy for both scale widgets */ + gtk_range_set_update_policy (GTK_RANGE (hscale), policy); + gtk_range_set_update_policy (GTK_RANGE (vscale), policy); +} + +void cb_digits_scale (GtkAdjustment *adj) +{ + /* set the number of decimal places to which adj->vaule is rounded + */ + gtk_scale_set_digits (GTK_SCALE (hscale), (gint) adj->value); + gtk_scale_set_digits (GTK_SCALE (vscale), (gint) adj->value); +} + +void cb_page_size (GtkAdjustment *get, GtkAdjustment *set) +{ + /* set the page size and page increment size of the sample + adjustment to the value specified by the "Page Size" scale */ + set->page_size = get->value; + set->page_increment = get->value; + /* now emit the "changed" signal to reconfigure all the widgets that + are attached to this adjustment */ + gtk_signal_emit_by_name (GTK_OBJECT (set), "changed"); +} + +void cb_draw_value (GtkToggleButton *button) +{ + /* turn the value display on the scale widgets off or on depending + on the state of the checkbutton */ + gtk_scale_set_draw_value (GTK_SCALE (hscale), button->active); + gtk_scale_set_draw_value (GTK_SCALE (vscale), button->active); +} + +/* convenience functions */ + +GtkWidget *make_menu_item (gchar *name, GtkSignalFunc callback, + gpointer data) +{ + GtkWidget *item; + + item = gtk_menu_item_new_with_label (name); + gtk_signal_connect (GTK_OBJECT (item), "activate", + callback, data); + gtk_widget_show (item); + + return item; +} + +void scale_set_default_values (GtkScale *scale) +{ + gtk_range_set_update_policy (GTK_RANGE (scale), + GTK_UPDATE_CONTINUOUS); + gtk_scale_set_digits (scale, 1); + gtk_scale_set_value_pos (scale, GTK_POS_TOP); + gtk_scale_set_draw_value (scale, TRUE); +} + +/* makes the sample window */ + +void create_range_controls (void) +{ + GtkWidget *window; + GtkWidget *box1, *box2, *box3; + GtkWidget *button; + GtkWidget *scrollbar; + GtkWidget *separator; + GtkWidget *opt, *menu, *item; + GtkWidget *label; + GtkWidget *scale; + GtkObject *adj1, *adj2; + + /* standard window-creating stuff */ + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_signal_connect (GTK_OBJECT (window), "destroy", + GTK_SIGNAL_FUNC(gtk_main_quit), + NULL); + gtk_window_set_title (GTK_WINDOW (window), "range controls"); + + box1 = gtk_vbox_new (FALSE, 0); + gtk_container_add (GTK_CONTAINER (window), box1); + gtk_widget_show (box1); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + /* value, lower, upper, step_increment, page_increment, page_size */ + /* note that the page_size value only makes a difference for + scrollbar widgets, and the highest value you'll get is actually + (upper - page_size). */ + adj1 = gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0); + + vscale = gtk_vscale_new (GTK_ADJUSTMENT (adj1)); + scale_set_default_values (GTK_SCALE (vscale)); + gtk_box_pack_start (GTK_BOX (box2), vscale, TRUE, TRUE, 0); + gtk_widget_show (vscale); + + box3 = gtk_vbox_new (FALSE, 10); + gtk_box_pack_start (GTK_BOX (box2), box3, TRUE, TRUE, 0); + gtk_widget_show (box3); + + /* reuse the same adjustment */ + hscale = gtk_hscale_new (GTK_ADJUSTMENT (adj1)); + gtk_widget_set_usize (GTK_WIDGET (hscale), 200, 30); + scale_set_default_values (GTK_SCALE (hscale)); + gtk_box_pack_start (GTK_BOX (box3), hscale, TRUE, TRUE, 0); + gtk_widget_show (hscale); + + /* reuse the same adjustment again */ + scrollbar = gtk_hscrollbar_new (GTK_ADJUSTMENT (adj1)); + /* notice how this causes the scales to always be updated + continuously when the scrollbar is moved */ + gtk_range_set_update_policy (GTK_RANGE (scrollbar), + GTK_UPDATE_CONTINUOUS); + gtk_box_pack_start (GTK_BOX (box3), scrollbar, TRUE, TRUE, 0); + gtk_widget_show (scrollbar); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + /* a checkbutton to control whether the value is displayed or not */ + button = gtk_check_button_new_with_label + ("Display value on scale widgets"); + gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (button), TRUE); + gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC + (cb_draw_value), NULL); + gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); + gtk_widget_show (button); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + + /* an option menu to change the position of the value */ + label = gtk_label_new ("Scale Value Position:"); + gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + opt = gtk_option_menu_new(); + menu = gtk_menu_new(); + + item = make_menu_item ("Top", GTK_SIGNAL_FUNC (cb_pos_menu_select), + GINT_TO_POINTER (GTK_POS_TOP)); + gtk_menu_append (GTK_MENU (menu), item); + + item = make_menu_item ("Bottom", GTK_SIGNAL_FUNC (cb_pos_menu_select), + GINT_TO_POINTER (GTK_POS_BOTTOM)); + gtk_menu_append (GTK_MENU (menu), item); + + item = make_menu_item ("Left", GTK_SIGNAL_FUNC (cb_pos_menu_select), + GINT_TO_POINTER (GTK_POS_LEFT)); + gtk_menu_append (GTK_MENU (menu), item); + + item = make_menu_item ("Right", GTK_SIGNAL_FUNC (cb_pos_menu_select), + GINT_TO_POINTER (GTK_POS_RIGHT)); + gtk_menu_append (GTK_MENU (menu), item); + + gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu); + gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0); + gtk_widget_show (opt); + + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + + /* yet another option menu, this time for the update policy of the + scale widgets */ + label = gtk_label_new ("Scale Update Policy:"); + gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + opt = gtk_option_menu_new(); + menu = gtk_menu_new(); + + item = make_menu_item ("Continuous", + GTK_SIGNAL_FUNC (cb_update_menu_select), + GINT_TO_POINTER (GTK_UPDATE_CONTINUOUS)); + gtk_menu_append (GTK_MENU (menu), item); + + item = make_menu_item ("Discontinuous", + GTK_SIGNAL_FUNC (cb_update_menu_select), + GINT_TO_POINTER (GTK_UPDATE_DISCONTINUOUS)); + gtk_menu_append (GTK_MENU (menu), item); + + item = make_menu_item ("Delayed", + GTK_SIGNAL_FUNC (cb_update_menu_select), + GINT_TO_POINTER (GTK_UPDATE_DELAYED)); + gtk_menu_append (GTK_MENU (menu), item); + + gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu); + gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0); + gtk_widget_show (opt); + + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + + /* a GtkHScale widget for adjusting the number of digits on the + sample scales. */ + label = gtk_label_new ("Scale Digits:"); + gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + adj2 = gtk_adjustment_new (1.0, 0.0, 5.0, 1.0, 1.0, 0.0); + gtk_signal_connect (GTK_OBJECT (adj2), "value_changed", + GTK_SIGNAL_FUNC (cb_digits_scale), NULL); + scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2)); + gtk_scale_set_digits (GTK_SCALE (scale), 0); + gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0); + gtk_widget_show (scale); + + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + box2 = gtk_hbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + + /* And, one last GtkHScale widget for adjusting the page size of the + scrollbar. */ + label = gtk_label_new ("Scrollbar Page Size:"); + gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + adj2 = gtk_adjustment_new (1.0, 1.0, 101.0, 1.0, 1.0, 0.0); + gtk_signal_connect (GTK_OBJECT (adj2), "value_changed", + GTK_SIGNAL_FUNC (cb_page_size), adj1); + scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2)); + gtk_scale_set_digits (GTK_SCALE (scale), 0); + gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0); + gtk_widget_show (scale); + + gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); + gtk_widget_show (box2); + + separator = gtk_hseparator_new (); + gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0); + gtk_widget_show (separator); + + box2 = gtk_vbox_new (FALSE, 10); + gtk_container_border_width (GTK_CONTAINER (box2), 10); + gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0); + gtk_widget_show (box2); + + button = gtk_button_new_with_label ("Quit"); + gtk_signal_connect_object (GTK_OBJECT (button), "clicked", + GTK_SIGNAL_FUNC(gtk_main_quit), + NULL); + gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); + GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); + gtk_widget_grab_default (button); + gtk_widget_show (button); + + gtk_widget_show (window); +} + +int main (int argc, char *argv[]) +{ + gtk_init(&argc, &argv); + + create_range_controls(); + + gtk_main(); + + return 0; +} + +/* example-end */ |