diff options
author | Marco Pesenti Gritti <marco@it.gnome.org> | 2003-01-21 02:57:20 +0800 |
---|---|---|
committer | Marco Pesenti Gritti <mpeseng@src.gnome.org> | 2003-01-21 02:57:20 +0800 |
commit | c2beffd03405e6db4295afd6c2982388b808cb2e (patch) | |
tree | 819e50881626c5109ccdba5abb158585a3ca148b /lib/egg | |
parent | 822ca51797e05edbe42b80f1a33a70a7327d732e (diff) | |
download | gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar.gz gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar.bz2 gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar.lz gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar.xz gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.tar.zst gsoc2013-epiphany-c2beffd03405e6db4295afd6c2982388b808cb2e.zip |
Merge eog-menu-api branch
2003-01-20 Marco Pesenti Gritti <marco@it.gnome.org>
* Merge eog-menu-api branch
Diffstat (limited to 'lib/egg')
32 files changed, 8498 insertions, 0 deletions
diff --git a/lib/egg/Makefile.am b/lib/egg/Makefile.am new file mode 100644 index 000000000..93abfd0d3 --- /dev/null +++ b/lib/egg/Makefile.am @@ -0,0 +1,59 @@ +INCLUDES = \ + $(EPIPHANY_DEPENDENCY_CFLAGS) \ + -DGTK_DISABLE_DEPRECATED \ + -DGDK_DISABLE_DEPRECATED \ + -DG_DISABLE_DEPRECATED + +noinst_LTLIBRARIES = libegg.la + +libegg_la_SOURCES = \ + egg-action.c \ + egg-action-group.c \ + egg-toggle-action.c \ + egg-radio-action.c \ + egg-markup.c \ + egg-menu-merge.c \ + egg-accel-dialog.c \ + eggradiotoolbutton.c \ + eggtoggletoolbutton.c \ + eggtoolitem.c \ + eggseparatortoolitem.c \ + eggtoolbar.c \ + eggtoolbutton.c \ + eggmarshalers.c + +noinst_HEADERS = \ + egg-menu.h \ + egg-action.h \ + egg-action-group.h \ + egg-toggle-action.h \ + egg-radio-action.h \ + egg-markup.h \ + egg-menu-merge.h \ + egg-accel-dialog.h \ + eggradiotoolbutton.h \ + eggtoggletoolbutton.h \ + eggtoolitem.h \ + eggseparatortoolitem.h \ + eggtoolbar.h \ + eggtoolbutton.h + +eggmarshalers.h: + cd $(srcdir) \ + && $(GLIB_GENMARSHAL) --prefix=_egg_marshal eggmarshalers.list --header > xgen-emh \ + && cp xgen-emh eggmarshalers.h \ + && rm -f xgen-emh xgen-emh~ + +eggmarshalers.c: + cd $(srcdir) \ + && $(GLIB_GENMARSHAL) --prefix=_egg_marshal eggmarshalers.list --body > xgen-emc \ + && cp xgen-emc eggmarshalers.c \ + && rm -f xgen-emc xgen-emc~ + +egg-marshal.c: eggmarshalers.h eggmarshalers.c + +noinst_HEADERS = \ + eggmarshalers.h + +EXTRA_DIST= \ + eggmarshalers.list diff --git a/lib/egg/egg-accel-dialog.c b/lib/egg/egg-accel-dialog.c new file mode 100644 index 000000000..4173cbf02 --- /dev/null +++ b/lib/egg/egg-accel-dialog.c @@ -0,0 +1,332 @@ +#include "egg-accel-dialog.h" + +static void egg_accel_dialog_init (EggAccelDialog *self); +static void egg_accel_dialog_class_init (EggAccelDialogClass *class); + +GType +egg_accel_dialog_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggAccelDialogClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_accel_dialog_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggAccelDialog), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_accel_dialog_init, + }; + + type = g_type_register_static (GTK_TYPE_DIALOG, + "EggAccelDialog", + &type_info, 0); + } + return type; +} + +static void +egg_accel_dialog_class_init (EggAccelDialogClass *class) +{ +} + +static void accel_path_selection_changed (GtkTreeSelection *selection, + EggAccelDialog *self); +static void accel_path_set (GtkWidget *button, EggAccelDialog *self); +static void accel_path_reset (GtkWidget *button, EggAccelDialog *self); + +static void +egg_accel_dialog_init (EggAccelDialog *self) +{ + GtkCellRenderer *renderer; + GtkWidget *swin; + GtkWidget *table; + + /* set up the list store for all the accelerators */ + self->accel_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self->accel_store), + 0, GTK_SORT_ASCENDING); + egg_accel_dialog_rescan_accels (self); + + + swin = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), + GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_box_pack_start (GTK_BOX (GTK_DIALOG (self)->vbox), swin, + TRUE, TRUE, 0); + gtk_widget_show (swin); + + /* set up a two column view of the model in browse selection mode */ + self->accel_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(self->accel_store)); + gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (self->accel_view), TRUE); + gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (self->accel_view)), + GTK_SELECTION_BROWSE); + g_signal_connect_object (gtk_tree_view_get_selection (GTK_TREE_VIEW (self->accel_view)), + "changed", G_CALLBACK (accel_path_selection_changed), + G_OBJECT (self), 0); + + renderer = gtk_cell_renderer_text_new (); + g_object_set (G_OBJECT (renderer), "xalign", 0.0, NULL); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (self->accel_view), + -1, "Path", renderer, + "text", 0, + NULL); + gtk_tree_view_column_set_sort_column_id (gtk_tree_view_get_column (GTK_TREE_VIEW (self->accel_view), 0), 0); + + renderer = gtk_cell_renderer_text_new (); + g_object_set (G_OBJECT (renderer), "xalign", 0.0, NULL); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (self->accel_view), + -1, "Accel", renderer, + "text", 1, + NULL); + gtk_tree_view_column_set_sort_column_id (gtk_tree_view_get_column (GTK_TREE_VIEW (self->accel_view), 1), 1); + + gtk_container_add (GTK_CONTAINER (swin), self->accel_view); + gtk_widget_show (self->accel_view); + + table = gtk_table_new (2, 4, FALSE); + gtk_container_set_border_width (GTK_CONTAINER (table), 2); + gtk_table_set_row_spacings (GTK_TABLE (table), 2); + gtk_table_set_col_spacings (GTK_TABLE (table), 2); + gtk_box_pack_start (GTK_BOX (GTK_DIALOG (self)->vbox), table, FALSE,TRUE, 0); + gtk_widget_show (table); + + /* widgets for editing accels */ + self->shift_toggle = gtk_check_button_new_with_mnemonic ("S_hift"); + gtk_table_attach (GTK_TABLE (table), self->shift_toggle, + 0, 1, 0, 1, + GTK_FILL, GTK_FILL, 0, 0); + gtk_widget_show (self->shift_toggle); + self->ctrl_toggle = gtk_check_button_new_with_mnemonic ("_Ctrl"); + gtk_table_attach (GTK_TABLE (table), self->ctrl_toggle, + 1, 2, 0, 1, + GTK_FILL, GTK_FILL, 0, 0); + gtk_widget_show (self->ctrl_toggle); + self->alt_toggle = gtk_check_button_new_with_mnemonic ("_Alt"); + gtk_table_attach (GTK_TABLE (table), self->alt_toggle, + 2, 3, 0, 1, + GTK_FILL, GTK_FILL, 0, 0); + gtk_widget_show (self->alt_toggle); + self->key_entry = gtk_entry_new (); + gtk_table_attach (GTK_TABLE (table), self->key_entry, + 0, 3, 1, 2, + GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0); + gtk_widget_show (self->key_entry); + + /* buttons for changing path */ + self->set_button = gtk_button_new_with_mnemonic ("_Set"); + gtk_table_attach (GTK_TABLE (table), self->set_button, + 3, 4, 0, 1, + GTK_FILL, GTK_FILL, 0, 0); + gtk_widget_show (self->set_button); + self->reset_button = gtk_button_new_with_mnemonic ("_Reset"); + gtk_table_attach (GTK_TABLE (table), self->reset_button, + 3, 4, 1, 2, + GTK_FILL, GTK_FILL, 0, 0); + /*gtk_widget_show (self->reset_button);*/ + + g_signal_connect_object (self->set_button, "clicked", + G_CALLBACK (accel_path_set), G_OBJECT (self), 0); + g_signal_connect_object (self->reset_button, "clicked", + G_CALLBACK (accel_path_reset), G_OBJECT (self), 0); + + + self->ok_button = gtk_dialog_add_button (GTK_DIALOG (self), + GTK_STOCK_OK, + GTK_RESPONSE_OK); + gtk_widget_grab_default (self->ok_button); +} + +static void +accel_map_foreach (gpointer data, + const gchar *accel_path, + guint accel_key, + guint accel_mods, + gboolean changed) +{ + EggAccelDialog *self = data; + GtkTreeIter iter; + gchar *accel_name; + + gtk_list_store_append (self->accel_store, &iter); + if (accel_key != 0) + accel_name = gtk_accelerator_name (accel_key, accel_mods); + else + accel_name = ""; + + gtk_list_store_set (self->accel_store, &iter, + 0, accel_path, + 1, accel_name, + -1); + if (accel_key != 0) + g_free(accel_name); +} + +void +egg_accel_dialog_rescan_accels (EggAccelDialog *self) +{ + g_return_if_fail (EGG_IS_ACCEL_DIALOG (self)); + + gtk_list_store_clear (self->accel_store); + gtk_accel_map_foreach (self, accel_map_foreach); +} + +/* make sure the currently selected accel is up to date */ +static void +refresh_selected_row (EggAccelDialog *self) +{ + GtkTreeSelection *selection; + GtkTreeIter iter; + + g_return_if_fail (EGG_IS_ACCEL_DIALOG (self)); + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->accel_view)); + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) { + char *accel_path; + + /* get the accel path for the selected row */ + gtk_tree_model_get (GTK_TREE_MODEL (self->accel_store), &iter, + 0, &accel_path, -1); + if (accel_path) { + GtkAccelKey key; + + if (gtk_accel_map_lookup_entry (accel_path, &key)) { + char *accel_name; + + accel_name = gtk_accelerator_name (key.accel_key, key.accel_mods); + gtk_list_store_set (self->accel_store, &iter, 1, accel_name, -1); + g_free (accel_name); + } + g_free (accel_path); + } + } +} + +static void +accel_path_selection_changed (GtkTreeSelection *selection, + EggAccelDialog *self) +{ + GtkTreeIter iter; + + /* just make sure the selected row is up to date */ + refresh_selected_row (self); + + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) { + char *accel_path; + + /* get the accel path for the selected row */ + gtk_tree_model_get (GTK_TREE_MODEL (self->accel_store), &iter, + 0, &accel_path, -1); + if (accel_path) { + GtkAccelKey key; + + if (gtk_accel_map_lookup_entry (accel_path, &key)) { + gchar *keyname; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->shift_toggle), + (key.accel_mods & GDK_SHIFT_MASK)!=0); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->ctrl_toggle), + (key.accel_mods & GDK_CONTROL_MASK)!=0); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->alt_toggle), + (key.accel_mods & GDK_MOD1_MASK)!=0); + keyname = gdk_keyval_name (key.accel_key); + if (keyname) + gtk_entry_set_text (GTK_ENTRY (self->key_entry), keyname); + else + gtk_entry_set_text (GTK_ENTRY (self->key_entry), ""); + } + } + g_free (accel_path); + } +} + +static void +accel_path_set (GtkWidget *button, EggAccelDialog *self) +{ + GtkTreeSelection *selection; + GtkTreeIter iter; + gboolean changed = FALSE; + + g_return_if_fail (EGG_IS_ACCEL_DIALOG (self)); + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->accel_view)); + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) { + char *accel_path; + + /* get the accel path for the selected row */ + gtk_tree_model_get (GTK_TREE_MODEL (self->accel_store), &iter, + 0, &accel_path, -1); + if (accel_path) { + GdkModifierType accel_mods = 0; + const gchar *key_name; + guint accel_key = 0; + + /* get modifiers */ + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(self->shift_toggle))) + accel_mods |= GDK_SHIFT_MASK; + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(self->ctrl_toggle))) + accel_mods |= GDK_CONTROL_MASK; + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(self->alt_toggle))) + accel_mods |= GDK_MOD1_MASK; + + key_name = gtk_entry_get_text (GTK_ENTRY (self->key_entry)); + /* check to see if entyr is empty -- if so, unset accel */ + if (key_name[0] != '\0') { + accel_key = gdk_keyval_from_name (key_name); + + if (accel_key) { + changed = gtk_accel_map_change_entry (accel_path, + accel_key, accel_mods, + TRUE); + } + } else + changed = gtk_accel_map_change_entry (accel_path, 0, 0, TRUE); + + g_free (accel_path); + } + } + if (!changed) + gdk_beep (); + accel_path_selection_changed (selection, self); +} + +static void +accel_path_reset (GtkWidget *button, EggAccelDialog *self) +{ + GtkTreeSelection *selection; + GtkTreeIter iter; + gboolean changed = FALSE; + + g_return_if_fail (EGG_IS_ACCEL_DIALOG (self)); + + g_message ("don't know how to reset to defaults :("); + return; + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->accel_view)); + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) { + char *accel_path; + + /* get the accel path for the selected row */ + gtk_tree_model_get (GTK_TREE_MODEL (self->accel_store), &iter, + 0, &accel_path, -1); + if (accel_path) { + changed = gtk_accel_map_change_entry (accel_path, 0, 0, TRUE); + g_free (accel_path); + } + } + if (!changed) + gdk_beep (); + accel_path_selection_changed (selection, self); +} + +GtkWidget * +egg_accel_dialog_new (void) +{ + return g_object_new(EGG_TYPE_ACCEL_DIALOG, NULL); +} diff --git a/lib/egg/egg-accel-dialog.h b/lib/egg/egg-accel-dialog.h new file mode 100644 index 000000000..8aa2fc435 --- /dev/null +++ b/lib/egg/egg-accel-dialog.h @@ -0,0 +1,43 @@ +#ifndef EGG_ACCEL_DIALOG_H +#define EGG_ACCEL_DIALOG_H + +#include <gtk/gtk.h> + +#define EGG_TYPE_ACCEL_DIALOG (egg_accel_dialog_get_type ()) +#define EGG_ACCEL_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_ACCEL_DIALOG, EggAccelDialog)) +#define EGG_ACCEL_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_ACCEL_DIALOG, EggAccelDialogClass)) +#define EGG_IS_ACCEL_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_ACCEL_DIALOG)) +#define EGG_IS_ACCEL_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_ACCEL_DIALOG)) +#define EGG_ACCEL_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_ACCEL_DIALOG, EggAccelDialogClass)) + +typedef struct _EggAccelDialog EggAccelDialog; +typedef struct _EggAccelDialogClass EggAccelDialogClass; + +struct _EggAccelDialog { + GtkDialog parent; + + GtkListStore *accel_store; + + GtkWidget *accel_view; + + GtkWidget *shift_toggle; + GtkWidget *ctrl_toggle; + GtkWidget *alt_toggle; + GtkWidget *key_entry; + + GtkWidget *set_button; + GtkWidget *reset_button; + + GtkWidget *ok_button; +}; + +struct _EggAccelDialogClass { + GtkDialogClass parent_class; +}; + +GType egg_accel_dialog_get_type (void); +GtkWidget *egg_accel_dialog_new (void); + +void egg_accel_dialog_rescan_accels (EggAccelDialog *accel_dialog); + +#endif diff --git a/lib/egg/egg-action-group.c b/lib/egg/egg-action-group.c new file mode 100644 index 000000000..b1aba64a5 --- /dev/null +++ b/lib/egg/egg-action-group.c @@ -0,0 +1,297 @@ +#include "egg-action-group.h" +#include "egg-toggle-action.h" +#include "egg-radio-action.h" + +#ifndef _ +# define _(s) (s) +#endif + +static void egg_action_group_init (EggActionGroup *self); +static void egg_action_group_class_init (EggActionGroupClass *class); + +GType +egg_action_group_get_type (void) +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggActionGroupClass), + (GBaseInitFunc) egg_action_group_init, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_action_group_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggActionGroup), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_action_group_init, + }; + + type = g_type_register_static (G_TYPE_OBJECT, "EggActionGroup", + &type_info, 0); + } + + return type; +} + +static GObjectClass *parent_class = NULL; +static void egg_action_group_finalize (GObject *object); +static EggAction *egg_action_group_real_get_action (EggActionGroup *self, + const gchar *name); + +static void +egg_action_group_class_init (EggActionGroupClass *class) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (class); + parent_class = g_type_class_peek_parent (class); + + object_class->finalize = egg_action_group_finalize; + class->get_action = egg_action_group_real_get_action; +} + +static void +egg_action_group_init (EggActionGroup *self) +{ + self->name = NULL; + self->actions = g_hash_table_new_full (g_str_hash, g_str_equal, + (GDestroyNotify) g_free, + (GDestroyNotify) g_object_unref); +} + +/** + * egg_action_group_new: + * @name: the name of the action group + * + * Creates a new EggActionGroup object. + * + * Returns: the new EggActionGroup + */ +EggActionGroup * +egg_action_group_new(const gchar *name) +{ + EggActionGroup *self; + + self = g_object_new (EGG_TYPE_ACTION_GROUP, NULL); + self->name = g_strdup (name); + + return self; +} + +static void +egg_action_group_finalize (GObject *object) +{ + EggActionGroup *self; + + self = EGG_ACTION_GROUP (object); + + g_free (self->name); + self->name = NULL; + + g_hash_table_destroy (self->actions); + self->actions = NULL; + + if (parent_class->finalize) + (* parent_class->finalize) (object); +} + +static EggAction * +egg_action_group_real_get_action (EggActionGroup *self, + const gchar *action_name) +{ + return g_hash_table_lookup (self->actions, action_name); +} + +/** + * egg_action_group_get_name: + * @action_group: the action group + * + * Returns: the name of the EggActionGroup + */ +const gchar * +egg_action_group_get_name (EggActionGroup *action_group) +{ + g_return_val_if_fail (EGG_IS_ACTION_GROUP (action_group), NULL); + + return action_group->name; +} + +/** + * egg_action_group_get_action: + * @action_group: the action group + * @action_name: the name of the action + * + * This function looks up an action in the action group by name. + * + * Returns: the action, or NULL if no action by that name exists + */ +EggAction * +egg_action_group_get_action (EggActionGroup *action_group, + const gchar *action_name) +{ + g_return_val_if_fail (EGG_IS_ACTION_GROUP (action_group), NULL); + g_return_val_if_fail (EGG_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL); + + return (* EGG_ACTION_GROUP_GET_CLASS (action_group)->get_action) + (action_group, action_name); +} + +/** + * egg_action_group_add_action: + * @action_group: the action group + * @action: an action + * + * This function adds an action object to the action group. + */ +void +egg_action_group_add_action (EggActionGroup *action_group, + EggAction *action) +{ + g_return_if_fail (EGG_IS_ACTION_GROUP (action_group)); + g_return_if_fail (EGG_IS_ACTION (action)); + g_return_if_fail (action->name != NULL); + + g_hash_table_insert (action_group->actions, g_strdup (action->name), + g_object_ref (action)); +} + +/** + * egg_action_group_removes_action: + * @action_group: the action group + * @action: an action + * + * This function removes an action object to the action group. + */ +void +egg_action_group_remove_action (EggActionGroup *action_group, + EggAction *action) +{ + g_return_if_fail (EGG_IS_ACTION_GROUP (action_group)); + g_return_if_fail (EGG_IS_ACTION (action)); + g_return_if_fail (action->name != NULL); + + /* extra protection to make sure action->name is valid */ + g_object_ref (action); + g_hash_table_remove (action_group->actions, action->name); + g_object_unref (action); +} + +static void +add_single_action (gpointer key, gpointer value, gpointer user_data) +{ + GList **list = user_data; + + *list = g_list_prepend (*list, value); +} + +/** + * egg_action_group_list_actions: + * @action_group: the action group + * + * Lists the actions in the action group. + * + * Returns: an allocated list of the action objects in the action group + */ +GList * +egg_action_group_list_actions (EggActionGroup *action_group) +{ + GList *actions = NULL; + + g_hash_table_foreach (action_group->actions, add_single_action, &actions); + + return g_list_reverse (actions); +} + + +/** + * egg_action_group_add_actions: + * @action_group: the action group + * @entries: an array of action descriptions + * @n_entries: the number of entries + * + * This is a convenience routine to create a number of actions and add + * them to the action group. Each member of the array describes an + * action to create. + */ +void +egg_action_group_add_actions (EggActionGroup *action_group, + EggActionGroupEntry *entries, + guint n_entries) +{ + guint i; + + for (i = 0; i < n_entries; i++) + { + EggAction *action; + GType action_type; + gchar *accel_path; + + switch (entries[i].entry_type) { + case NORMAL_ACTION: + action_type = EGG_TYPE_ACTION; + break; + case TOGGLE_ACTION: + action_type = EGG_TYPE_TOGGLE_ACTION; + break; + case RADIO_ACTION: + action_type = EGG_TYPE_RADIO_ACTION; + break; + default: + g_warning ("unsupported action type"); + action_type = EGG_TYPE_ACTION; + } + + action = g_object_new (action_type, + "name", entries[i].name, + "label", _(entries[i].label), + "tooltip", _(entries[i].tooltip), + "stock_id", entries[i].stock_id, + NULL); + + if (entries[i].entry_type == RADIO_ACTION && + entries[i].extra_data != NULL) + { + EggAction *radio_action; + GSList *group; + + radio_action = + egg_action_group_get_action (EGG_ACTION_GROUP (action_group), + entries[i].extra_data); + if (radio_action) + { + group = egg_radio_action_get_group (EGG_RADIO_ACTION (radio_action)); + egg_radio_action_set_group (EGG_RADIO_ACTION (action), group); + } + else + g_warning (G_STRLOC " could not look up `%s'", entries[i].extra_data); + } + + if (entries[i].callback) + g_signal_connect (action, "activate", + entries[i].callback, entries[i].user_data); + + /* set the accel path for the menu item */ + accel_path = g_strconcat ("<Actions>/", action_group->name, "/", + entries[i].name, NULL); + if (entries[i].accelerator) + { + guint accel_key = 0; + GdkModifierType accel_mods; + + gtk_accelerator_parse (entries[i].accelerator, &accel_key, + &accel_mods); + if (accel_key) + gtk_accel_map_add_entry (accel_path, accel_key, accel_mods); + } + + egg_action_set_accel_path (action, accel_path); + g_free(accel_path); + + egg_action_group_add_action (action_group, action); + g_object_unref (action); + } +} diff --git a/lib/egg/egg-action-group.h b/lib/egg/egg-action-group.h new file mode 100644 index 000000000..2ee05dcec --- /dev/null +++ b/lib/egg/egg-action-group.h @@ -0,0 +1,71 @@ +#ifndef EGG_ACTION_GROUP_H +#define EGG_ACTION_GROUP_H + +#include <gtk/gtk.h> +#include <egg-action.h> + +#define EGG_TYPE_ACTION_GROUP (egg_action_group_get_type ()) +#define EGG_ACTION_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_ACTION_GROUP, EggActionGroup)) +#define EGG_ACTION_GROUP_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), EGG_TYPE_ACTION_GROUP, EggActionGroupClass)) +#define EGG_IS_ACTION_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_ACTION_GROUP)) +#define EGG_IS_ACTION_GROUP_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), EGG_TYPE_ACTION_GROUP)) +#define EGG_ACTION_GROUP_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), EGG_TYPE_ACTION_GROUP, EggActionGroupClass)) + +typedef struct _EggActionGroup EggActionGroup; +typedef struct _EggActionGroupClass EggActionGroupClass; +typedef struct _EggActionGroupEntry EggActionGroupEntry; + +struct _EggActionGroup +{ + GObject parent; + + gchar *name; + GHashTable *actions; +}; + +struct _EggActionGroupClass +{ + GObjectClass parent_class; + + EggAction *(* get_action) (EggActionGroup *action_group, + const gchar *action_name); +}; + +typedef enum { + NORMAL_ACTION, + TOGGLE_ACTION, + RADIO_ACTION +} EggActionGroupEntryType; + +struct _EggActionGroupEntry { + gchar *name; + gchar *label; + gchar *stock_id; + gchar *accelerator; + gchar *tooltip; + + GCallback callback; + gpointer user_data; + + EggActionGroupEntryType entry_type; + gchar *extra_data; +}; + +GType egg_action_group_get_type (void); + +EggActionGroup *egg_action_group_new (const gchar *name); + +const gchar *egg_action_group_get_name (EggActionGroup *action_group); +EggAction *egg_action_group_get_action (EggActionGroup *action_group, + const gchar *action_name); +GList *egg_action_group_list_actions (EggActionGroup *action_group); +void egg_action_group_add_action (EggActionGroup *action_group, + EggAction *action); +void egg_action_group_remove_action (EggActionGroup *action_group, + EggAction *action); + +void egg_action_group_add_actions (EggActionGroup *action_group, + EggActionGroupEntry *entries, + guint n_entries); + +#endif diff --git a/lib/egg/egg-action.c b/lib/egg/egg-action.c new file mode 100644 index 000000000..360ba358e --- /dev/null +++ b/lib/egg/egg-action.c @@ -0,0 +1,961 @@ +#include "egg-action.h" +#include "eggtoolbutton.h" + +#ifndef _ +# define _(s) (s) +#endif + +/* some code for making arbitrary GtkButtons that act like toolbar + * buttons */ +static GtkWidget *tool_button_new (GType button_type, + const gchar *text, + GtkWidget *icon); +static GtkWidget *tool_button_get_label (GtkWidget *button); +static GtkWidget *tool_button_get_icon (GtkWidget *button); + +enum { + ACTIVATE, + LAST_SIGNAL +}; + +enum { + PROP_0, + PROP_NAME, + PROP_LABEL, + PROP_SHORT_LABEL, + PROP_TOOLTIP, + PROP_STOCK_ID, + PROP_SENSITIVE, + PROP_VISIBLE, +}; + +static void egg_action_init (EggAction *action); +static void egg_action_class_init (EggActionClass *class); + +static GQuark accel_path_id = 0; +static const gchar *accel_path_key = "EggAction::accel_path"; + +GType +egg_action_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggActionClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_action_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggAction), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_action_init, + }; + + type = g_type_register_static (G_TYPE_OBJECT, + "EggAction", + &type_info, 0); + } + return type; +} + +static void egg_action_finalize (GObject *object); +static void egg_action_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void egg_action_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +static GtkWidget *create_menu_item (EggAction *action); +static GtkWidget *create_tool_item (EggAction *action); +static void connect_proxy (EggAction *action, + GtkWidget *proxy); +static void disconnect_proxy (EggAction *action, + GtkWidget *proxy); + +static GObjectClass *parent_class = NULL; +static guint action_signals[LAST_SIGNAL] = { 0 }; + + +static void +egg_action_class_init (EggActionClass *class) +{ + GObjectClass *object_class; + + accel_path_id = g_quark_from_static_string(accel_path_key); + + parent_class = g_type_class_peek_parent (class); + object_class = G_OBJECT_CLASS(class); + + object_class->finalize = egg_action_finalize; + object_class->set_property = egg_action_set_property; + object_class->get_property = egg_action_get_property; + + class->activate = NULL; + + class->create_menu_item = create_menu_item; + class->create_tool_item = create_tool_item; + class->connect_proxy = connect_proxy; + class->disconnect_proxy = disconnect_proxy; + + class->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM; + class->toolbar_item_type = EGG_TYPE_TOOL_BUTTON; + + g_object_class_install_property (object_class, + PROP_NAME, + g_param_spec_string ("name", + _("Name"), + _("A unique name for the action."), + NULL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (object_class, + PROP_LABEL, + g_param_spec_string ("label", + _("Label"), + _("The label used for menu items and buttons that activate this action."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_SHORT_LABEL, + g_param_spec_string ("short_label", + _("Short label"), + _("A shorter label that may be used on toolbar buttons."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_TOOLTIP, + g_param_spec_string ("tooltip", + _("Tooltip"), + _("A tooltip for this action."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_STOCK_ID, + g_param_spec_string ("stock_id", + _("Stock Icon"), + _("The stock icon displayed in widgets representing this action."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_SENSITIVE, + g_param_spec_boolean ("sensitive", + _("Sensitive"), + _("Whether the action is enabled."), + TRUE, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_VISIBLE, + g_param_spec_boolean ("visible", + _("Visible"), + _("Whether the action is visible."), + TRUE, + G_PARAM_READWRITE)); + + action_signals[ACTIVATE] = + g_signal_new ("activate", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, + G_STRUCT_OFFSET (EggActionClass, activate), NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + + +static void +egg_action_init (EggAction *action) +{ + action->name = NULL; + action->label = NULL; + action->short_label = NULL; + action->tooltip = NULL; + action->stock_id = NULL; + + action->sensitive = TRUE; + action->visible = TRUE; + + action->label_set = FALSE; + action->short_label_set = FALSE; + + action->accel_quark = 0; + + action->proxies = NULL; +} + +static void +egg_action_finalize (GObject *object) +{ + EggAction *action; + + action = EGG_ACTION (object); + + g_free (action->name); + g_free (action->label); + g_free (action->short_label); + g_free (action->tooltip); + g_free (action->stock_id); +} + +static void +egg_action_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EggAction *action; + + action = EGG_ACTION (object); + + switch (prop_id) + { + case PROP_NAME: + g_free (action->name); + action->name = g_value_dup_string (value); + break; + case PROP_LABEL: + g_free (action->label); + action->label = g_value_dup_string (value); + action->label_set = (action->label != NULL); + /* if label is unset, then use the label from the stock item */ + if (!action->label_set && action->stock_id) + { + GtkStockItem stock_item; + + if (gtk_stock_lookup(action->stock_id, &stock_item)) + action->label = g_strdup(stock_item.label); + } + /* if short_label is unset, set short_label=label */ + if (!action->short_label_set) + { + g_free(action->short_label); + action->short_label = g_strdup(action->label); + g_object_notify(object, "short_label"); + } + break; + case PROP_SHORT_LABEL: + g_free (action->short_label); + action->short_label = g_value_dup_string (value); + action->short_label_set = (action->short_label != NULL); + /* if short_label is unset, then use the value of label */ + if (!action->short_label_set) + { + action->short_label = g_strdup(action->label); + } + break; + case PROP_TOOLTIP: + g_free (action->tooltip); + action->tooltip = g_value_dup_string (value); + break; + case PROP_STOCK_ID: + g_free (action->stock_id); + action->stock_id = g_value_dup_string (value); + /* update label and short_label if appropriate */ + if (!action->label_set) + { + GtkStockItem stock_item; + + g_free(action->label); + if (gtk_stock_lookup(action->stock_id, &stock_item)) + action->label = g_strdup(stock_item.label); + else + action->label = NULL; + g_object_notify(object, "label"); + } + if (!action->short_label_set) + { + g_free(action->short_label); + action->short_label = g_strdup(action->label); + g_object_notify(object, "short_label"); + } + break; + case PROP_SENSITIVE: + action->sensitive = g_value_get_boolean (value); + break; + case PROP_VISIBLE: + action->visible = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +egg_action_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EggAction *action; + + action = EGG_ACTION (object); + + switch (prop_id) + { + case PROP_NAME: + g_value_set_string (value, action->name); + break; + case PROP_LABEL: + g_value_set_string (value, action->label); + break; + case PROP_SHORT_LABEL: + g_value_set_string (value, action->short_label); + break; + case PROP_TOOLTIP: + g_value_set_string (value, action->tooltip); + break; + case PROP_STOCK_ID: + g_value_set_string (value, action->stock_id); + break; + case PROP_SENSITIVE: + g_value_set_boolean (value, action->sensitive); + break; + case PROP_VISIBLE: + g_value_set_boolean (value, action->visible); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GtkWidget * +create_menu_item (EggAction *action) +{ + GType menu_item_type; + + menu_item_type = EGG_ACTION_GET_CLASS (action)->menu_item_type; + + return g_object_new (menu_item_type, NULL); +} + +static GtkWidget * +create_tool_item (EggAction *action) +{ + GType toolbar_item_type; + + toolbar_item_type = EGG_ACTION_GET_CLASS (action)->toolbar_item_type; + + return g_object_new (toolbar_item_type, NULL); +} + +static void +egg_action_remove_proxy (GtkWidget *widget, EggAction *action) +{ + action->proxies = g_slist_remove (action->proxies, widget); +} + +static void +egg_action_sync_property (EggAction *action, GParamSpec *pspec, + GtkWidget *proxy) +{ + const gchar *property; + GValue value = { 0, }; + + property = g_param_spec_get_name (pspec); + + g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); + g_object_get_property (G_OBJECT (action), property, &value); + + g_object_set_property (G_OBJECT (proxy), property, &value); + g_value_unset (&value); +} + +static void +egg_action_sync_label (EggAction *action, GParamSpec *pspec, GtkWidget *proxy) +{ + GtkWidget *label = NULL; + + g_return_if_fail (GTK_IS_MENU_ITEM (proxy)); + label = GTK_BIN(proxy)->child; + + if (GTK_IS_LABEL (label)) + gtk_label_set_label (GTK_LABEL (label), action->label); +} + +static void +egg_action_sync_short_label (EggAction *action, GParamSpec *pspec, + GtkWidget *proxy) +{ + GValue value = { 0, }; + + g_value_init(&value, G_TYPE_STRING); + g_object_get_property (G_OBJECT (action), "short_label", &value); + + g_object_set_property (G_OBJECT (proxy), "label", &value); + g_value_unset (&value); +} + +static void +egg_action_sync_stock_id (EggAction *action, GParamSpec *pspec, + GtkWidget *proxy) +{ + GtkWidget *image = NULL; + + if (GTK_IS_IMAGE_MENU_ITEM (proxy)) + { + image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy)); + + if (GTK_IS_IMAGE (image)) + gtk_image_set_from_stock (GTK_IMAGE (image), + action->stock_id, GTK_ICON_SIZE_MENU); + } +} + +static GtkWidget * +egg_action_create_menu_proxy (EggToolItem *tool_item, EggAction *action) +{ + return egg_action_create_menu_item (action); +} + +static void +connect_proxy (EggAction *action, GtkWidget *proxy) +{ + g_object_ref (action); + g_object_set_data_full (G_OBJECT (proxy), "egg-action", action, + g_object_unref); + + /* add this widget to the list of proxies */ + action->proxies = g_slist_prepend (action->proxies, proxy); + g_signal_connect (proxy, "destroy", + G_CALLBACK (egg_action_remove_proxy), action); + + g_signal_connect_object (action, "notify::sensitive", + G_CALLBACK (egg_action_sync_property), proxy, 0); + gtk_widget_set_sensitive (proxy, action->sensitive); + + g_signal_connect_object (action, "notify::visible", + G_CALLBACK (egg_action_sync_property), proxy, 0); + if (action->visible) + gtk_widget_show (proxy); + else + gtk_widget_hide (proxy); + + if (GTK_IS_MENU_ITEM (proxy)) + { + GtkWidget *label; + /* menu item specific synchronisers ... */ + + label = GTK_BIN (proxy)->child; + + /* make sure label is a label */ + if (label && !GTK_IS_LABEL (label)) + { + gtk_container_remove (GTK_CONTAINER(proxy), label); + label = NULL; + } + if (!label) + { + label = g_object_new (GTK_TYPE_ACCEL_LABEL, + "use_underline", TRUE, + "xalign", 0.0, + "visible", TRUE, + "parent", proxy, + "accel_widget", proxy, + NULL); + } + gtk_label_set_label (GTK_LABEL (label), action->label); + g_signal_connect_object (action, "notify::label", + G_CALLBACK (egg_action_sync_label), proxy, 0); + + if (GTK_IS_IMAGE_MENU_ITEM (proxy)) + { + GtkWidget *image; + + image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy)); + if (image && !GTK_IS_IMAGE(image)) + { + gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),NULL); + image = NULL; + } + if (!image) + { + image = gtk_image_new_from_stock (NULL, + GTK_ICON_SIZE_MENU); + gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), + image); + gtk_widget_show (image); + } + gtk_image_set_from_stock (GTK_IMAGE (image), + action->stock_id, GTK_ICON_SIZE_MENU); + g_signal_connect_object (action, "notify::stock_id", + G_CALLBACK (egg_action_sync_stock_id), + proxy, 0); + } + + if (action->accel_quark) + { + gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy), + g_quark_to_string (action->accel_quark)); + } + + g_signal_connect_object (proxy, "activate", + G_CALLBACK (egg_action_activate), action, + G_CONNECT_SWAPPED); + } + else if (EGG_IS_TOOL_BUTTON (proxy)) + { + GtkWidget *label; + GtkWidget *icon; + /* toolbar button specific synchronisers ... */ + + /* synchronise the label */ + g_object_set (G_OBJECT (proxy), + "label", action->short_label, + "use_underline", TRUE, + NULL); + g_signal_connect_object (action, "notify::short_label", + G_CALLBACK (egg_action_sync_short_label), + proxy, 0); + + g_object_set (G_OBJECT (proxy), "stock_id", action->stock_id, NULL); + g_signal_connect_object (action, "notify::stock_id", + G_CALLBACK (egg_action_sync_property), proxy, 0); + + g_signal_connect_object (proxy, "create_menu_proxy", + G_CALLBACK (egg_action_create_menu_proxy), + action, 0); + + g_signal_connect_object (proxy, "clicked", + G_CALLBACK (egg_action_activate), action, + G_CONNECT_SWAPPED); + } +} + +static void +disconnect_proxy (EggAction *action, GtkWidget *proxy) +{ + static guint notify_id = 0; + + if (!notify_id) + notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT); + + g_object_set_data (G_OBJECT (proxy), "egg-action", NULL); + + /* remove proxy from list of proxies */ + g_signal_handlers_disconnect_by_func (proxy, + G_CALLBACK (egg_action_remove_proxy), + action); + egg_action_remove_proxy (proxy, action); + + /* disconnect the activate handler */ + g_signal_handlers_disconnect_by_func (proxy, + G_CALLBACK (egg_action_activate), + action); + + /* disconnect handlers for notify::* signals */ + g_signal_handlers_disconnect_by_func (proxy, + G_CALLBACK (egg_action_sync_property), + action); + + g_signal_handlers_disconnect_by_func (action, + G_CALLBACK (egg_action_sync_stock_id), proxy); + + /* menu item specific synchronisers ... */ + g_signal_handlers_disconnect_by_func (action, + G_CALLBACK (egg_action_sync_label), + proxy); + + gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy), NULL); + + /* toolbar button specific synchronisers ... */ + g_signal_handlers_disconnect_by_func (action, + G_CALLBACK (egg_action_sync_short_label), + proxy); + g_signal_handlers_disconnect_by_func (proxy, + G_CALLBACK (egg_action_create_menu_proxy), + action); +} + +/** + * egg_action_activate: + * @action: the action object + * + * Calling this function will emit the "activate" signal on the + * specified action. It gets called by the proxy widgets when they + * get activated. + * + * It can also be used to manually activate an action. + */ +void +egg_action_activate (EggAction *action) +{ + g_signal_emit (action, action_signals[ACTIVATE], 0); +} + +/** + * egg_action_create_icon: + * @action: the action object + * @icon_size: the size of the icon that should be created. + * + * This function is intended for use by action implementations to + * create icons displayed in the proxy widgets. + * + * Returns: a widget that displays the icon for this action. + */ +GtkWidget * +egg_action_create_icon (EggAction *action, GtkIconSize icon_size) +{ + if (action->stock_id) + return gtk_image_new_from_stock (action->stock_id, icon_size); + else + return NULL; +} + +/** + * egg_action_create_menu_item: + * @action: the action object + * + * This function creates a menu item widget that proxies for the given + * action. + * + * Returns: a menu item connected to the action. + */ +GtkWidget * +egg_action_create_menu_item (EggAction *action) +{ + GtkWidget *menu_item; + + menu_item = (* EGG_ACTION_GET_CLASS (action)->create_menu_item) (action); + + (* EGG_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item); + + return menu_item; +} + +/** + * egg_action_create_tool_item: + * @action: the action object + * + * This function creates a toolbar item widget that proxies for the + * given action. + * + * Returns: a toolbar item connected to the action. + */ +GtkWidget * +egg_action_create_tool_item (EggAction *action) +{ + GtkWidget *button; + + button = (* EGG_ACTION_GET_CLASS (action)->create_tool_item) (action); + + (* EGG_ACTION_GET_CLASS (action)->connect_proxy) (action, button); + + return button; +} + +/** + * egg_action_connect_proxy: + * @action: the action object + * @proxy: the proxy widget + * + * This function connects a widget to an action object as a proxy. It + * will synchronise various properties of the action with the widget + * (such as label text, icon, tooltip, etc), and attaches a callback + * so that the action gets activated when the proxy widget does. + * + * If the widget is already connected to an action, it is disconnected + * first. + */ +void +egg_action_connect_proxy (EggAction *action, + GtkWidget *proxy) +{ + EggAction *prev_action; + + g_return_if_fail (EGG_IS_ACTION (action)); + g_return_if_fail (GTK_IS_WIDGET (proxy)); + + prev_action = g_object_get_data (G_OBJECT (proxy), "egg-action"); + + if (prev_action) + { + (* EGG_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy); + } + + (* EGG_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy); +} + +/** + * egg_action_disconnect_proxy: + * @action: the action object + * @proxy: the proxy widget + * + * This disconnects a proxy widget from an action. It does not + * destroy the widget, however. + */ +void +egg_action_disconnect_proxy (EggAction *action, + GtkWidget *proxy) +{ + EggAction *prev_action; + + g_return_if_fail (EGG_IS_ACTION (action)); + g_return_if_fail (GTK_IS_WIDGET (proxy)); + + g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "egg-action") != action); + + (* EGG_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy); +} + +/** + * egg_action_block_activate_from: + * @action: the action object + * @proxy: a proxy widget + * + * Calling this function disables calls to the egg_action_activate() + * function by signals on the given proxy widget. This is used to + * break notification loops for things like check or radio actions. + * + * This function is intended for use by action implementations. + */ +void +egg_action_block_activate_from (EggAction *action, GtkWidget *proxy) +{ + g_return_if_fail (EGG_IS_ACTION (action)); + + g_signal_handlers_block_by_func (proxy, G_CALLBACK (egg_action_activate), + action); +} + +/** + * egg_action_unblock_activate_from: + * @action: the action object + * @proxy: a proxy widget + * + * Calling this function re-enables calls to the egg_action_activate() + * function by signals on the given proxy widget. This undoes the + * blocking done by egg_action_block_activate_from(). + * + * This function is intended for use by action implementations. + */ +void +egg_action_unblock_activate_from (EggAction *action, GtkWidget *proxy) +{ + g_return_if_fail (EGG_IS_ACTION (action)); + + g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (egg_action_activate), + action); +} + +/** + * egg_action_set_accel_path: + * @action: the action object + * @accel_path: the accelerator path + * + * Sets the accel path for this action. All proxy widgets associated + * with the action will have this accel path, so that their + * accelerators are consistent. + */ +void +egg_action_set_accel_path (EggAction *action, const gchar *accel_path) +{ + action->accel_quark = g_quark_from_string(accel_path); +} + +/* ---- code to create sort-of-toolbar-buttons ---- */ + +static GtkWidget * +tool_button_get_label (GtkWidget *button) +{ + g_return_val_if_fail (GTK_IS_BUTTON (button), NULL); + + return g_object_get_data (G_OBJECT (button), "tool-button-label"); +} + +static GtkWidget * +tool_button_get_icon (GtkWidget *button) +{ + g_return_val_if_fail (GTK_IS_BUTTON (button), NULL); + + return g_object_get_data (G_OBJECT (button), "tool-button-icon"); +} + +static void +tool_button_parent_set (GtkWidget *button, GtkWidget *old_parent) +{ + GtkWidget *box; + GtkWidget *label; + GtkWidget *icon; + + box = g_object_get_data (G_OBJECT (button), "tool-button-box"); + label = g_object_get_data (G_OBJECT (button), "tool-button-label"); + icon = g_object_get_data (G_OBJECT (button), "tool-button-icon"); + + if (button->parent && GTK_IS_TOOLBAR (button->parent)) + { + GtkReliefStyle relief = GTK_RELIEF_NORMAL; + GList *tmp; + + /* set button relief to match toolbar */ + gtk_widget_style_get (GTK_WIDGET (button->parent), + "button_relief", &relief, NULL); + gtk_button_set_relief (GTK_BUTTON (button), relief); + + /* set the button style */ + switch (gtk_toolbar_get_style (GTK_TOOLBAR (button->parent))) + { + case GTK_TOOLBAR_ICONS: + if (icon && !GTK_WIDGET_VISIBLE (icon)) + gtk_widget_show (icon); + if (label && GTK_WIDGET_VISIBLE (label)) + gtk_widget_hide (label); + break; + + case GTK_TOOLBAR_TEXT: + if (icon && GTK_WIDGET_VISIBLE (icon)) + gtk_widget_hide (icon); + if (label && !GTK_WIDGET_VISIBLE (label)) + gtk_widget_show (label); + break; + + case GTK_TOOLBAR_BOTH: + if (icon && !GTK_WIDGET_VISIBLE (icon)) + gtk_widget_show (icon); + if (label && !GTK_WIDGET_VISIBLE (label)) + gtk_widget_show (label); + + if (GTK_IS_HBOX (box)) + { + if (icon) + { + g_object_ref (icon); + gtk_container_remove (GTK_CONTAINER (box), icon); + } + if (label) + { + g_object_ref (label); + gtk_container_remove (GTK_CONTAINER (box), label); + } + gtk_container_remove (GTK_CONTAINER (button), box); + box = gtk_vbox_new (FALSE, 0); + + gtk_widget_show (box); + + if (label) + { + gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0); + g_object_unref (label); + } + if (icon) + { + gtk_box_pack_end (GTK_BOX (box), icon, FALSE, FALSE, 0); + g_object_unref (label); + } + gtk_container_add (GTK_CONTAINER (button), box); + g_object_set_data (G_OBJECT (button), "tool-button-box", box); + } + break; + + case GTK_TOOLBAR_BOTH_HORIZ: + if (icon && !GTK_WIDGET_VISIBLE (icon)) + gtk_widget_show (icon); + if (label && !GTK_WIDGET_VISIBLE (label)) + gtk_widget_show (label); + + if (GTK_IS_VBOX (box)) + { + if (icon) + { + g_object_ref (icon); + gtk_container_remove (GTK_CONTAINER (box), icon); + } + if (label) + { + g_object_ref (label); + gtk_container_remove (GTK_CONTAINER (box), label); + } + gtk_container_remove (GTK_CONTAINER (button), box); + box = gtk_hbox_new (FALSE, 0); + + gtk_widget_show (box); + + if (label) + { + gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0); + g_object_unref (label); + } + if (icon) + { + gtk_box_pack_end (GTK_BOX (box), icon, FALSE, FALSE, 0); + g_object_unref (label); + } + gtk_container_add (GTK_CONTAINER (button), box); + g_object_set_data (G_OBJECT (button), "tool-button-box", box); + } + break; + } + + /* set the icon size */ + icon = tool_button_get_icon (button); + if (GTK_IS_IMAGE (icon) && + gtk_image_get_storage_type (GTK_IMAGE (icon)) == GTK_IMAGE_STOCK) + { + gchar *stock_id; + + gtk_image_get_stock (GTK_IMAGE (icon), &stock_id, NULL); + stock_id = g_strdup (stock_id); + gtk_image_set_from_stock (GTK_IMAGE (icon), stock_id, + GTK_TOOLBAR (button->parent)->icon_size); + g_free (stock_id); + } + + /* gross hack!!! */ + for (tmp = GTK_TOOLBAR (button->parent)->children; tmp; tmp = tmp->next) + { + GtkToolbarChild *tool_child = tmp->data; + + if (tool_child->widget == button) + { + tool_child->type = GTK_TOOLBAR_CHILD_BUTTON; + tool_child->icon = icon; + tool_child->label = label; + break; + } + } + } +} + +static GtkWidget * +tool_button_new (GType button_type, const gchar *text, GtkWidget *icon) +{ + GtkWidget *button; + GtkWidget *vbox; + GtkWidget *label; + + g_return_val_if_fail (g_type_is_a (button_type, GTK_TYPE_BUTTON), NULL); + + button = g_object_new (button_type, NULL); + + vbox = gtk_vbox_new (FALSE, 0); + gtk_container_add (GTK_CONTAINER (button), vbox); + gtk_widget_show (vbox); + + label = gtk_label_new (text); + gtk_label_set_use_underline (GTK_LABEL (label), TRUE); + gtk_label_set_mnemonic_widget (GTK_LABEL (label), button); + gtk_box_pack_end (GTK_BOX (vbox), label, FALSE, FALSE, 0); + + if (!icon) + icon = gtk_image_new(); + gtk_box_pack_end (GTK_BOX (vbox), icon, FALSE, FALSE, 0); + + g_object_set_data (G_OBJECT (button), "tool-button-box", vbox); + g_object_set_data (G_OBJECT (button), "tool-button-label", label); + g_object_set_data (G_OBJECT (button), "tool-button-icon", icon); + + g_signal_connect (button, "parent_set", + G_CALLBACK (tool_button_parent_set), NULL); + + GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS); + + return button; +} diff --git a/lib/egg/egg-action.h b/lib/egg/egg-action.h new file mode 100644 index 000000000..7acf59210 --- /dev/null +++ b/lib/egg/egg-action.h @@ -0,0 +1,81 @@ +#ifndef EGG_ACTION_H +#define EGG_ACTION_H + +#include <gtk/gtk.h> + +#define EGG_TYPE_ACTION (egg_action_get_type ()) +#define EGG_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_ACTION, EggAction)) +#define EGG_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_ACTION, EggActionClass)) +#define EGG_IS_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_ACTION)) +#define EGG_IS_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_ACTION)) +#define EGG_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_ACTION, EggActionClass)) + +typedef struct _EggAction EggAction; +typedef struct _EggActionClass EggActionClass; + +struct _EggAction +{ + GObject object; + + gchar *name; + gchar *label; + gchar *short_label; + gchar *tooltip; + gchar *stock_id; /* icon */ + + guint sensitive : 1; + guint visible : 1; + guint label_set : 1; /* these two used so we can set label */ + guint short_label_set : 1; /* based on stock id */ + + /* accelerator */ + GQuark accel_quark; + + /* list of proxy widgets */ + GSList *proxies; +}; + +struct _EggActionClass +{ + GObjectClass parent_class; + + /* activation signal */ + void (* activate) (EggAction *action); + + GType menu_item_type; + GType toolbar_item_type; + + /* widget creation routines (not signals) */ + GtkWidget *(* create_menu_item) (EggAction *action); + GtkWidget *(* create_tool_item) (EggAction *action); + void (* connect_proxy) (EggAction *action, + GtkWidget *proxy); + void (* disconnect_proxy) (EggAction *action, + GtkWidget *proxy); +}; + +GType egg_action_get_type (void); + +void egg_action_activate (EggAction *action); + +GtkWidget *egg_action_create_icon (EggAction *action, + GtkIconSize icon_size); +GtkWidget *egg_action_create_menu_item (EggAction *action); +GtkWidget *egg_action_create_tool_item (EggAction *action); +void egg_action_connect_proxy (EggAction *action, + GtkWidget *proxy); +void egg_action_disconnect_proxy (EggAction *action, + GtkWidget *proxy); + +/* protected ... for use by child actions */ +void egg_action_block_activate_from (EggAction *action, + GtkWidget *proxy); +void egg_action_unblock_activate_from (EggAction *action, + GtkWidget *proxy); + +/* protected ... for use by action groups */ +void egg_action_set_accel_path (EggAction *action, + const gchar *accel_path); + + +#endif diff --git a/lib/egg/egg-markup.c b/lib/egg/egg-markup.c new file mode 100644 index 000000000..feae85a00 --- /dev/null +++ b/lib/egg/egg-markup.c @@ -0,0 +1,438 @@ +#include <string.h> +#include "egg-markup.h" +#include "eggtoolbar.h" + +#ifndef _ +# define _(String) (String) +# define N_(String) (String) +#endif + +typedef enum { + STATE_START, + STATE_ROOT, + STATE_MENU, + STATE_TOOLBAR, + STATE_POPUPS, + STATE_ITEM, + STATE_END +} ParseState; + +typedef struct _ParseContext ParseContext; +struct _ParseContext +{ + /* parser state information */ + ParseState state; + ParseState prev_state; + + /* function to call when we finish off a toplevel widget */ + EggWidgetFunc widget_func; + gpointer user_data; + + /* GdkAccelGroup to use for menus */ + GtkAccelGroup *accel_group; + + /* info about the widget we are constructing at the moment */ + GtkWidget *top; + gchar *type; + gchar *name; + + /* the current container we are working on */ + GtkWidget *current; + + /* the ActionGroup used to create menu items */ + EggActionGroup *action_group; +}; + +static void +start_element_handler (GMarkupParseContext *context, + const gchar *element_name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error) +{ + ParseContext *ctx = user_data; + gboolean raise_error = TRUE; + gchar *error_attr = NULL; + + switch (element_name[0]) + { + case 'R': + if (ctx->state == STATE_START && !strcmp(element_name, "Root")) + { + ctx->state = STATE_ROOT; + raise_error = FALSE; + } + break; + case 'm': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "menu")) + { + ctx->state = STATE_MENU; + + ctx->top = ctx->current = gtk_menu_bar_new(); + ctx->type = "menu"; + ctx->name = NULL; + + raise_error = FALSE; + } + else if (ctx->state == STATE_MENU && !strcmp(element_name, "menuitem")) + { + gint i; + const gchar *action_name = NULL; + EggAction *action = NULL; + + ctx->state = STATE_ITEM; + + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "verb")) + { + action_name = attribute_values[i]; + action = egg_action_group_get_action(ctx->action_group, + action_name); + } + } + + if (action) + { + GtkWidget *widget = egg_action_create_menu_item(action); + + gtk_container_add(GTK_CONTAINER(ctx->current), widget); + gtk_widget_show(widget); + } + else + { + g_warning("could not find action '%s'", + action_name ? action_name : "(null)"); + } + + raise_error = FALSE; + } + break; + case 'd': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "dockitem")) + { + gint i; + + ctx->state = STATE_TOOLBAR; + + ctx->top = ctx->current = egg_toolbar_new(); + ctx->type = "toolbar"; + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "name")) + ctx->name = g_strdup(attribute_values[i]); + } + + raise_error = FALSE; + } + break; + case 'p': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "popups")) + { + ctx->state = STATE_POPUPS; + raise_error = FALSE; + } + else if (ctx->state == STATE_POPUPS &&!strcmp(element_name, "popup")) + { + gint i; + + ctx->state = STATE_MENU; + + ctx->top = ctx->current = gtk_menu_new(); + gtk_menu_set_accel_group(GTK_MENU(ctx->current), ctx->accel_group); + ctx->type = "popup"; + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "name")) + { + ctx->name = g_strdup(attribute_values[i]); + gtk_menu_set_title(GTK_MENU(ctx->current), ctx->name); + } + else if (!strcmp(attribute_names[i], "tearoff")) + { + GtkWidget *tearoff = gtk_tearoff_menu_item_new(); + + gtk_container_add(GTK_CONTAINER(ctx->current), tearoff); + gtk_widget_show(tearoff); + } + } + + raise_error = FALSE; + } + break; + case 's': + if (ctx->state == STATE_MENU && !strcmp(element_name, "submenu")) + { + gint i; + const gchar *label = NULL; + gboolean tearoff = FALSE; + GtkWidget *widget; + + ctx->state = STATE_MENU; + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "label")) + label = g_strdup(attribute_values[i]); + else if (!strcmp(attribute_names[i], "tearoff")) + tearoff = TRUE; + } + widget = gtk_menu_item_new_with_label(label); + gtk_label_set_use_underline(GTK_LABEL(GTK_BIN(widget)->child), TRUE); + gtk_container_add(GTK_CONTAINER(ctx->current), widget); + gtk_widget_show(widget); + + ctx->current = gtk_menu_new(); + gtk_menu_set_accel_group(GTK_MENU(ctx->current), ctx->accel_group); + gtk_menu_set_title(GTK_MENU(ctx->current), label); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(widget), ctx->current); + + if (tearoff) + { + GtkWidget *tearoff = gtk_tearoff_menu_item_new(); + + gtk_container_add(GTK_CONTAINER(ctx->current), tearoff); + gtk_widget_show(tearoff); + } + + raise_error = FALSE; + } + else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) && + !strcmp(element_name, "separator")) + { + ctx->state = STATE_ITEM; + + if (GTK_IS_MENU_SHELL(ctx->current)) + { + GtkWidget *widget = gtk_separator_menu_item_new(); + gtk_container_add(GTK_CONTAINER(ctx->current), widget); + gtk_widget_show(widget); + } + else /* toolbar */ + { + EggToolItem *item = egg_tool_item_new (); + egg_toolbar_insert_tool_item (EGG_TOOLBAR(ctx->current), item, -1); + gtk_widget_show (GTK_WIDGET (item)); + } + + raise_error = FALSE; + } + break; + case 't': + if (ctx->state == STATE_TOOLBAR && !strcmp(element_name, "toolitem")) + { + gint i; + const gchar *action_name = NULL; + EggAction *action = NULL; + + ctx->state = STATE_ITEM; + + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "verb")) + { + action_name = attribute_values[i]; + action = egg_action_group_get_action(ctx->action_group, + action_name); + } + } + + if (action) + { + GtkWidget *widget = egg_action_create_tool_item (action); + + gtk_container_add (GTK_CONTAINER (ctx->current), widget); + } + else + { + g_warning("could not find action '%s'", + action_name ? action_name : "(null)"); + } + + raise_error = FALSE; + } + break; + }; + + if (raise_error) + { + gint line_number, char_number; + + g_markup_parse_context_get_position (context, + &line_number, &char_number); + + if (error_attr) + g_set_error (error, + G_MARKUP_ERROR, + G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, + _("Unknown attribute '%s' on line %d char %d"), + error_attr, + line_number, char_number); + else + g_set_error (error, + G_MARKUP_ERROR, + G_MARKUP_ERROR_UNKNOWN_ELEMENT, + _("Unknown tag '%s' on line %d char %d"), + element_name, + line_number, char_number); + } +} + +static void +end_element_handler (GMarkupParseContext *context, + const gchar *element_name, + gpointer user_data, + GError **error) +{ + ParseContext *ctx = user_data; + GtkWidget *widget; + + switch (ctx->state) + { + case STATE_START: + g_warning("shouldn't get any end tags at this point"); + /* should do a GError here */ + break; + case STATE_ROOT: + ctx->state = STATE_END; + break; + case STATE_MENU: + widget = GTK_IS_MENU(ctx->current) ? + gtk_menu_get_attach_widget(GTK_MENU(ctx->current)) : NULL; + if (widget) /* not back to the toplevel ... */ + { + ctx->current = widget->parent; + ctx->state = STATE_MENU; + } + else + { + if (GTK_IS_MENU(ctx->current)) /* must be a popup */ + ctx->state = STATE_POPUPS; + else + ctx->state = STATE_ROOT; + + /* notify */ + (* ctx->widget_func)(ctx->top, ctx->type, ctx->name, ctx->user_data); + ctx->top = NULL; + ctx->type = NULL; + g_free(ctx->name); + ctx->name = NULL; + ctx->current = NULL; + } + break; + case STATE_TOOLBAR: + ctx->state = STATE_ROOT; + + /* notify */ + (* ctx->widget_func)(ctx->top, ctx->type, ctx->name, ctx->user_data); + ctx->top = NULL; + ctx->type = NULL; + g_free(ctx->name); + ctx->name = NULL; + ctx->current = NULL; + break; + case STATE_POPUPS: + ctx->state = STATE_ROOT; + break; + case STATE_ITEM: + if (GTK_IS_MENU_SHELL(ctx->current)) + ctx->state = STATE_MENU; + else + ctx->state = STATE_TOOLBAR; + break; + case STATE_END: + g_warning("shouldn't get any end tags at this point"); + /* should do a GError here */ + break; + } +} + +static void +cleanup (GMarkupParseContext *context, + GError *error, + gpointer user_data) +{ + ParseContext *ctx = user_data; + + gtk_widget_destroy(ctx->top); + ctx->top = NULL; + ctx->type = NULL; + g_free(ctx->name); + ctx->name = NULL; + ctx->current = NULL; +} + + +static GMarkupParser ui_parser = { + start_element_handler, + end_element_handler, + NULL, + NULL, + cleanup +}; + + +gboolean +egg_create_from_string (EggActionGroup *action_group, + EggWidgetFunc widget_func, gpointer user_data, + GtkAccelGroup *accel_group, + const gchar *buffer, guint length, + GError **error) +{ + ParseContext ctx = { 0 }; + GMarkupParseContext *context; + gboolean res = TRUE; + + g_return_val_if_fail(EGG_IS_ACTION_GROUP(action_group), FALSE); + g_return_val_if_fail(widget_func != NULL, FALSE); + g_return_val_if_fail(GTK_IS_ACCEL_GROUP(accel_group), FALSE); + g_return_val_if_fail(buffer != NULL, FALSE); + + ctx.state = STATE_START; + ctx.widget_func = widget_func; + ctx.user_data = user_data; + ctx.accel_group = accel_group; + ctx.top = NULL; + ctx.type = NULL; + ctx.name = NULL; + ctx.current = NULL; + ctx.action_group = action_group; + + context = g_markup_parse_context_new(&ui_parser, 0, &ctx, NULL); + if (length < 0) + length = strlen(buffer); + + if (g_markup_parse_context_parse(context, buffer, length, error)) + { + if (!g_markup_parse_context_end_parse(context, error)) + res = FALSE; + } + else + res = FALSE; + + g_markup_parse_context_free (context); + + return res; +} + +gboolean +egg_create_from_file (EggActionGroup *action_group, + EggWidgetFunc widget_func, + gpointer user_data, + GtkAccelGroup *accel_group, + const gchar *filename, + GError **error) +{ + gchar *buffer; + gint length; + gboolean res; + + if (!g_file_get_contents (filename, &buffer, &length, error)) + return FALSE; + + res = egg_create_from_string(action_group, widget_func, user_data, + accel_group, buffer, length, error); + g_free(buffer); + + return res; +} diff --git a/lib/egg/egg-markup.h b/lib/egg/egg-markup.h new file mode 100644 index 000000000..15524189f --- /dev/null +++ b/lib/egg/egg-markup.h @@ -0,0 +1,30 @@ +#ifndef EGG_MARKUP_H +#define EGG_MARKUP_H + +#include <gtk/gtk.h> +#include <egg-action.h> +#include <egg-action-group.h> + + +/* this stuff can go away once I am finished with the merge code */ + +typedef void (*EggWidgetFunc) (GtkWidget *widget, + const gchar *type, + const gchar *name, + gpointer user_data); + +gboolean egg_create_from_string (EggActionGroup *action_group, + EggWidgetFunc widget_func, + gpointer user_data, + GtkAccelGroup *accel_group, + const gchar *buffer, guint length, + GError **error); + +gboolean egg_create_from_file (EggActionGroup *action_group, + EggWidgetFunc widget_func, + gpointer user_data, + GtkAccelGroup *accel_group, + const gchar *filename, + GError **error); + +#endif diff --git a/lib/egg/egg-menu-merge.c b/lib/egg/egg-menu-merge.c new file mode 100644 index 000000000..17895ca99 --- /dev/null +++ b/lib/egg/egg-menu-merge.c @@ -0,0 +1,1316 @@ +#include <string.h> +#include "egg-menu-merge.h" +#include "eggtoolbar.h" +#include "eggseparatortoolitem.h" + +#ifndef _ +# define _(string) (string) +#endif + +#define NODE_INFO(node) ((EggMenuMergeNode *)node->data) + +typedef struct { + guint merge_id; + GQuark action_quark; +} NodeUIReference; + +static void egg_menu_merge_class_init (EggMenuMergeClass *class); +static void egg_menu_merge_init (EggMenuMerge *merge); + +static void egg_menu_merge_queue_update (EggMenuMerge *self); +static void egg_menu_merge_dirty_all (EggMenuMerge *self); + +static GNode *get_child_node (EggMenuMerge *self, GNode *parent, + const gchar *childname, + gint childname_length, + EggMenuMergeNodeType node_type, + gboolean create, gboolean top); +static GNode *egg_menu_merge_get_node (EggMenuMerge *self, + const gchar *path, + EggMenuMergeNodeType node_type, + gboolean create); +static guint egg_menu_merge_next_merge_id (EggMenuMerge *self); + +static void egg_menu_merge_node_prepend_ui_reference (EggMenuMergeNode *node, + guint merge_id, + GQuark action_quark); +static void egg_menu_merge_node_remove_ui_reference (EggMenuMergeNode *node, + guint merge_id); + +enum { + ADD_WIDGET, + REMOVE_WIDGET, + LAST_SIGNAL +}; + +static guint merge_signals[LAST_SIGNAL] = { 0 }; + +static GMemChunk *merge_node_chunk = NULL; + +GType +egg_menu_merge_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggMenuMergeClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_menu_merge_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggMenuMerge), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_menu_merge_init, + }; + + type = g_type_register_static (G_TYPE_OBJECT, + "EggMenuMerge", + &type_info, 0); + } + return type; +} + +static void +egg_menu_merge_class_init (EggMenuMergeClass *class) +{ + if (!merge_node_chunk) + merge_node_chunk = g_mem_chunk_create(EggMenuMergeNode, 64, + G_ALLOC_AND_FREE); + + merge_signals[ADD_WIDGET] = + g_signal_new ("add_widget", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, + G_STRUCT_OFFSET (EggMenuMergeClass, add_widget), NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + GTK_TYPE_WIDGET); + merge_signals[REMOVE_WIDGET] = + g_signal_new ("remove_widget", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, + G_STRUCT_OFFSET (EggMenuMergeClass, remove_widget), NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + GTK_TYPE_WIDGET); + +} + + +static void +egg_menu_merge_init (EggMenuMerge *self) +{ + guint merge_id; + GNode *node; + + self->accel_group = gtk_accel_group_new(); + + self->root_node = NULL; + self->action_groups = NULL; + + self->last_merge_id = 0; + + + merge_id = egg_menu_merge_next_merge_id(self); + node = get_child_node(self, NULL, "Root", 4, + EGG_MENU_MERGE_ROOT, TRUE, FALSE); + egg_menu_merge_node_prepend_ui_reference(NODE_INFO(node), merge_id, 0); + node = get_child_node(self, self->root_node, "popups", 6, + EGG_MENU_MERGE_POPUPS, TRUE, FALSE); + egg_menu_merge_node_prepend_ui_reference(NODE_INFO(node), merge_id, 0); +} + +EggMenuMerge * +egg_menu_merge_new (void) +{ + return g_object_new(EGG_TYPE_MENU_MERGE, NULL); +} + +void +egg_menu_merge_insert_action_group (EggMenuMerge *self, + EggActionGroup *action_group, gint pos) +{ + g_return_if_fail (EGG_IS_MENU_MERGE(self)); + g_return_if_fail (EGG_IS_ACTION_GROUP(action_group)); + g_return_if_fail (g_list_find(self->action_groups, action_group) == NULL); + + g_object_ref(action_group); + self->action_groups = g_list_insert(self->action_groups, action_group, pos); + + /* dirty all nodes, as action bindings may change */ + egg_menu_merge_dirty_all(self); +} + +void +egg_menu_merge_remove_action_group (EggMenuMerge *self, + EggActionGroup *action_group) +{ + g_return_if_fail (EGG_IS_MENU_MERGE(self)); + g_return_if_fail (EGG_IS_ACTION_GROUP(action_group)); + g_return_if_fail (g_list_find(self->action_groups, action_group) != NULL); + + self->action_groups = g_list_remove(self->action_groups, action_group); + g_object_unref(action_group); + + /* dirty all nodes, as action bindings may change */ + egg_menu_merge_dirty_all(self); +} + +GtkWidget * +egg_menu_merge_get_widget (EggMenuMerge *self, const gchar *path) +{ + GNode *node; + + /* ensure that there are no pending updates before we get the + * widget */ + egg_menu_merge_ensure_update(self); + + node = egg_menu_merge_get_node(self, path, EGG_MENU_MERGE_UNDECIDED, FALSE); + return NODE_INFO(node)->proxy; +} + +static GNode * +get_child_node(EggMenuMerge *self, GNode *parent, + const gchar *childname, gint childname_length, + EggMenuMergeNodeType node_type, + gboolean create, gboolean top) +{ + GNode *child = NULL; + + g_return_val_if_fail(parent == NULL || + (NODE_INFO(parent)->type != EGG_MENU_MERGE_MENUITEM && + NODE_INFO(parent)->type != EGG_MENU_MERGE_TOOLITEM), NULL); + + if (parent) + { + if (childname) + { + for (child = parent->children; child != NULL; child = child->next) + { + if (strlen(NODE_INFO(child)->name) == childname_length && + !strncmp(NODE_INFO(child)->name, childname, childname_length)) + { + /* if undecided about node type, set it */ + if (NODE_INFO(child)->type == EGG_MENU_MERGE_UNDECIDED) + NODE_INFO(child)->type = node_type; + + /* warn about type mismatch */ + if (NODE_INFO(child)->type != EGG_MENU_MERGE_UNDECIDED && + NODE_INFO(child)->type != node_type) + g_warning("node type doesn't match %d (%s is type %d)", + node_type, NODE_INFO(child)->name, + NODE_INFO(child)->type); + + return child; + } + } + } + if (!child && create) + { + EggMenuMergeNode *mnode; + + mnode = g_chunk_new0(EggMenuMergeNode, merge_node_chunk); + mnode->type = node_type; + mnode->name = g_strndup(childname, childname_length); + mnode->dirty = TRUE; + + if (top) + child = g_node_prepend_data(parent, mnode); + else + child = g_node_append_data(parent, mnode); + } + } + else + { + /* handle root node */ + if (self->root_node) + { + child = self->root_node; + if (strncmp(NODE_INFO(child)->name, childname, childname_length) !=0) + g_warning("root node name '%s' doesn't match '%s'", + childname, NODE_INFO(child)->name); + if (NODE_INFO(child)->type != EGG_MENU_MERGE_ROOT) + g_warning("base element must be of type ROOT"); + } + else if (create) + { + EggMenuMergeNode *mnode; + + mnode = g_chunk_new0(EggMenuMergeNode, merge_node_chunk); + mnode->type = node_type; + mnode->name = g_strndup(childname, childname_length); + mnode->dirty = TRUE; + + child = self->root_node = g_node_new(mnode); + } + } + + return child; +} + +static GNode * +egg_menu_merge_get_node(EggMenuMerge *self, const gchar *path, + EggMenuMergeNodeType node_type, gboolean create) +{ + const gchar *pos, *end; + GNode *parent, *node; + + end = path + strlen(path); + pos = path; + parent = node = NULL; + while (pos < end) + { + const gchar *slash; + gsize length; + + slash = strchr(pos, '/'); + if (slash) + length = slash - pos; + else + length = strlen(pos); + + node = get_child_node(self, parent, pos, length, EGG_MENU_MERGE_UNDECIDED, + create, FALSE); + if (!node) + return NULL; + + pos += length + 1; /* move past the node name and the slash too */ + parent = node; + } + + if (NODE_INFO(node)->type == EGG_MENU_MERGE_UNDECIDED) + NODE_INFO(node)->type = node_type; + return node; +} + +static guint +egg_menu_merge_next_merge_id (EggMenuMerge *self) +{ + self->last_merge_id++; + + return self->last_merge_id; +} + +static void +egg_menu_merge_node_prepend_ui_reference (EggMenuMergeNode *node, + guint merge_id, GQuark action_quark) +{ + NodeUIReference *reference; + + reference = g_new (NodeUIReference, 1); + reference->action_quark = action_quark; + reference->merge_id = merge_id; + + /* Prepend the reference */ + node->uifiles = g_list_prepend (node->uifiles, reference); + + node->dirty = TRUE; +} + +static void +egg_menu_merge_node_remove_ui_reference (EggMenuMergeNode *node, + guint merge_id) +{ + GList *p; + + for (p = node->uifiles; p != NULL; p = p->next) + { + NodeUIReference *reference = p->data; + + if (reference->merge_id == merge_id) + { + node->uifiles = g_list_remove_link (node->uifiles, p); + node->dirty = TRUE; + g_free (reference); + + break; + } + } +} + +/* -------------------- The UI file parser -------------------- */ + +typedef enum { + STATE_START, + STATE_ROOT, + STATE_MENU, + STATE_TOOLBAR, + STATE_POPUPS, + STATE_MENUITEM, + STATE_TOOLITEM, + STATE_END +} ParseState; + +typedef struct _ParseContext ParseContext; +struct _ParseContext +{ + ParseState state; + ParseState prev_state; + + EggMenuMerge *self; + + GNode *current; + + guint merge_id; +}; + +static void +start_element_handler (GMarkupParseContext *context, + const gchar *element_name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error) +{ + ParseContext *ctx = user_data; + EggMenuMerge *self = ctx->self; + + gint i; + const gchar *node_name; + GQuark verb_quark; + gboolean top; + + gboolean raise_error = TRUE; + gchar *error_attr = NULL; + + //g_message("starting element %s", element_name); + + /* work out a name for this node. Either the name attribute, or + * element name */ + node_name = element_name; + verb_quark = 0; + top = FALSE; + for (i = 0; attribute_names[i] != NULL; i++) + { + if (!strcmp(attribute_names[i], "name")) + { + node_name = attribute_values[i]; + } + else if (!strcmp(attribute_names[i], "verb")) + { + verb_quark = g_quark_from_string(attribute_values[i]); + } + else if (!strcmp(attribute_names[i], "pos")) + { + top = !strcmp(attribute_values[i], "top"); + } + } + /* if no verb, then set it to the node's name */ + if (verb_quark == 0) + verb_quark = g_quark_from_string(node_name); + + switch (element_name[0]) + { + case 'R': + if (ctx->state == STATE_START && !strcmp(element_name, "Root")) + { + ctx->state = STATE_ROOT; + ctx->current = self->root_node; + raise_error = FALSE; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + } + break; + case 'm': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "menu")) + { + ctx->state = STATE_MENU; + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_MENUBAR, + TRUE, FALSE); + if (NODE_INFO(ctx->current)->action_name == 0) + NODE_INFO(ctx->current)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + else if (ctx->state == STATE_MENU && !strcmp(element_name, "menuitem")) + { + GNode *node; + + ctx->state = STATE_MENUITEM; + node = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_MENUITEM, + TRUE, top); + if (NODE_INFO(node)->action_name == 0) + NODE_INFO(node)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (node), + ctx->merge_id, verb_quark); + NODE_INFO(node)->dirty = TRUE; + + raise_error = FALSE; + } + break; + case 'd': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "dockitem")) + { + ctx->state = STATE_TOOLBAR; + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_TOOLBAR, + TRUE, FALSE); + if (NODE_INFO(ctx->current)->action_name == 0) + NODE_INFO(ctx->current)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + break; + case 'p': + if (ctx->state == STATE_ROOT && !strcmp(element_name, "popups")) + { + ctx->state = STATE_POPUPS; + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_POPUPS, + TRUE, FALSE); + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + else if (ctx->state == STATE_POPUPS && !strcmp(element_name, "popup")) + { + ctx->state = STATE_MENU; + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_MENU, + TRUE, FALSE); + if (NODE_INFO(ctx->current)->action_name == 0) + NODE_INFO(ctx->current)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) && + !strcmp(element_name, "placeholder")) + { + if (ctx->state == STATE_MENU) + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_MENU_PLACEHOLDER, + TRUE, top); + else + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER, + TRUE, top); + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + break; + case 's': + if (ctx->state == STATE_MENU && !strcmp(element_name, "submenu")) + { + ctx->state = STATE_MENU; + ctx->current = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_MENU, + TRUE, top); + if (NODE_INFO(ctx->current)->action_name == 0) + NODE_INFO(ctx->current)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (ctx->current), + ctx->merge_id, verb_quark); + NODE_INFO(ctx->current)->dirty = TRUE; + + raise_error = FALSE; + } + else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) && + !strcmp(element_name, "separator")) + { + GNode *node; + + if (ctx->state == STATE_MENU) + ctx->state = STATE_MENUITEM; + else + ctx->state = STATE_TOOLITEM; + node = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_SEPARATOR, + TRUE, top); + if (NODE_INFO(node)->action_name == 0) + NODE_INFO(node)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (node), + ctx->merge_id, verb_quark); + NODE_INFO(node)->dirty = TRUE; + + raise_error = FALSE; + } + break; + case 't': + if (ctx->state == STATE_TOOLBAR && !strcmp(element_name, "toolitem")) + { + GNode *node; + + ctx->state = STATE_TOOLITEM; + node = get_child_node(self, ctx->current, + node_name, strlen(node_name), + EGG_MENU_MERGE_TOOLITEM, + TRUE, top); + if (NODE_INFO(node)->action_name == 0) + NODE_INFO(node)->action_name = verb_quark; + + egg_menu_merge_node_prepend_ui_reference (NODE_INFO (node), + ctx->merge_id, verb_quark); + NODE_INFO(node)->dirty = TRUE; + + raise_error = FALSE; + } + break; + default: + break; + } + if (raise_error) + { + gint line_number, char_number; + + g_markup_parse_context_get_position (context, + &line_number, &char_number); + if (error_attr) + g_set_error (error, + G_MARKUP_ERROR, + G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, + _("Unknown attribute '%s' on line %d char %d"), + error_attr, + line_number, char_number); + else + g_set_error (error, + G_MARKUP_ERROR, + G_MARKUP_ERROR_UNKNOWN_ELEMENT, + _("Unknown tag '%s' on line %d char %d"), + element_name, + line_number, char_number); + } +} + +static void +end_element_handler (GMarkupParseContext *context, + const gchar *element_name, + gpointer user_data, + GError **error) +{ + ParseContext *ctx = user_data; + EggMenuMerge *self = ctx->self; + + //g_message("ending element %s (state=%d)", element_name, ctx->state); + + switch (ctx->state) + { + case STATE_START: + g_warning("shouldn't get any end tags in start state"); + /* should we GError here? */ + break; + case STATE_ROOT: + if (ctx->current != self->root_node) + g_warning("we are in STATE_ROOT, but the current node isn't the root"); + ctx->current = NULL; + ctx->state = STATE_END; + break; + case STATE_MENU: + ctx->current = ctx->current->parent; + if (NODE_INFO(ctx->current)->type == EGG_MENU_MERGE_ROOT) /* menubar */ + ctx->state = STATE_ROOT; + else if (NODE_INFO(ctx->current)->type == EGG_MENU_MERGE_POPUPS) /* popup */ + ctx->state = STATE_POPUPS; + /* else, stay in STATE_MENU state */ + break; + case STATE_TOOLBAR: + ctx->current = ctx->current->parent; + /* we conditionalise this test, in case we are closing off a + * placeholder */ + if (NODE_INFO(ctx->current)->type == EGG_MENU_MERGE_ROOT) + ctx->state = STATE_ROOT; + /* else, stay in STATE_TOOLBAR state */ + break; + case STATE_POPUPS: + ctx->current = ctx->current->parent; + ctx->state = STATE_ROOT; + break; + case STATE_MENUITEM: + ctx->state = STATE_MENU; + break; + case STATE_TOOLITEM: + ctx->state = STATE_TOOLBAR; + break; + case STATE_END: + g_warning("shouldn't get any end tags at this point"); + /* should do an error here */ + break; + } +} + +static void +cleanup (GMarkupParseContext *context, + GError *error, + gpointer user_data) +{ + ParseContext *ctx = user_data; + EggMenuMerge *self = ctx->self; + + ctx->current = NULL; + /* should also walk through the tree and get rid of nodes related to + * this UI file's tag */ + + egg_menu_merge_remove_ui (self, ctx->merge_id); +} + +static GMarkupParser ui_parser = { + start_element_handler, + end_element_handler, + NULL, + NULL, + cleanup +}; + +guint +egg_menu_merge_add_ui_from_string (EggMenuMerge *self, + const gchar *buffer, guint length, + GError **error) +{ + ParseContext ctx = { 0 }; + GMarkupParseContext *context; + gboolean res = TRUE; + + g_return_val_if_fail(EGG_IS_MENU_MERGE(self), FALSE); + g_return_val_if_fail(buffer != NULL, FALSE); + + ctx.state = STATE_START; + ctx.self = self; + ctx.current = NULL; + ctx.merge_id = egg_menu_merge_next_merge_id (self); + + context = g_markup_parse_context_new(&ui_parser, 0, &ctx, NULL); + if (length < 0) + length = strlen(buffer); + + if (g_markup_parse_context_parse(context, buffer, length, error)) + { + if (!g_markup_parse_context_end_parse(context, error)) + res = FALSE; + } + else + res = FALSE; + + g_markup_parse_context_free (context); + + egg_menu_merge_queue_update(self); + + if (res) + return ctx.merge_id; + return 0; +} + +guint +egg_menu_merge_add_ui_from_file (EggMenuMerge *self, + const gchar *filename, + GError **error) +{ + gchar *buffer; + gint length; + guint res; + + if (!g_file_get_contents (filename, &buffer, &length, error)) + return 0; + + res = egg_menu_merge_add_ui_from_string(self, buffer, length, error); + g_free(buffer); + + return res; +} + +static gboolean +remove_ui (GNode *node, gpointer user_data) +{ + guint merge_id = GPOINTER_TO_UINT (user_data); + + egg_menu_merge_node_remove_ui_reference (NODE_INFO (node), merge_id); + + return FALSE; /* continue */ +} + +void +egg_menu_merge_remove_ui (EggMenuMerge *self, guint merge_id) +{ + g_node_traverse(self->root_node, G_POST_ORDER, G_TRAVERSE_ALL, -1, + remove_ui, GUINT_TO_POINTER (merge_id)); + + egg_menu_merge_queue_update(self); +} + +/* -------------------- Updates -------------------- */ + + +static EggAction * +get_action_by_name (EggMenuMerge *merge, const char *action_name) +{ + GList *tmp; + + if (!action_name) + return NULL; + + /* lookup name */ + for (tmp = merge->action_groups; tmp != NULL; tmp = tmp->next) + { + EggActionGroup *action_group = tmp->data; + EggAction *action; + + action = egg_action_group_get_action (action_group, action_name); + + if (action) + return action; + } + + return NULL; +} + +static gboolean +find_menu_position (GNode *node, GtkWidget **menushell_p, gint *pos_p) +{ + GtkWidget *menushell; + gint pos; + + g_return_val_if_fail(node != NULL, FALSE); + g_return_val_if_fail(NODE_INFO(node)->type == EGG_MENU_MERGE_MENU || + NODE_INFO(node)->type == EGG_MENU_MERGE_MENU_PLACEHOLDER || + NODE_INFO(node)->type == EGG_MENU_MERGE_MENUITEM || + NODE_INFO(node)->type == EGG_MENU_MERGE_SEPARATOR, + FALSE); + + /* first sibling -- look at parent */ + if (node->prev == NULL) + { + GNode *parent; + + parent = node->parent; + switch (NODE_INFO(parent)->type) + { + case EGG_MENU_MERGE_MENUBAR: + menushell = NODE_INFO(parent)->proxy; + pos = 0; + break; + case EGG_MENU_MERGE_MENU: + menushell = NODE_INFO(parent)->proxy; + if (GTK_IS_MENU_ITEM(menushell)) + menushell = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menushell)); + pos = 0; + break; + case EGG_MENU_MERGE_MENU_PLACEHOLDER: + menushell = gtk_widget_get_parent(NODE_INFO(parent)->proxy); + g_return_val_if_fail(GTK_IS_MENU_SHELL(menushell), FALSE); + pos = g_list_index(GTK_MENU_SHELL(menushell)->children, + NODE_INFO(parent)->proxy) + 1; + break; + default: + g_warning("%s: bad parent node type %d", G_STRLOC, + NODE_INFO(parent)->type); + return FALSE; + } + } + else + { + GtkWidget *prev_child; + GNode *sibling; + + sibling = node->prev; + if (NODE_INFO(sibling)->type == EGG_MENU_MERGE_MENU_PLACEHOLDER) + prev_child = NODE_INFO(sibling)->extra; /* second Separator */ + else + prev_child = NODE_INFO(sibling)->proxy; + + g_return_val_if_fail(GTK_IS_WIDGET(prev_child), FALSE); + menushell = gtk_widget_get_parent(prev_child); + g_return_val_if_fail(GTK_IS_MENU_SHELL(menushell), FALSE); + + pos = g_list_index(GTK_MENU_SHELL(menushell)->children, prev_child) + 1; + } + + if (menushell_p) + *menushell_p = menushell; + if (pos_p) + *pos_p = pos; + + return TRUE; +} + +static gboolean +find_toolbar_position (GNode *node, GtkWidget **toolbar_p, gint *pos_p) +{ + GtkWidget *toolbar; + gint pos; + + g_return_val_if_fail(node != NULL, FALSE); + g_return_val_if_fail(NODE_INFO(node)->type == EGG_MENU_MERGE_TOOLBAR || + NODE_INFO(node)->type == EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER || + NODE_INFO(node)->type == EGG_MENU_MERGE_TOOLITEM || + NODE_INFO(node)->type == EGG_MENU_MERGE_SEPARATOR, + FALSE); + + /* first sibling -- look at parent */ + if (node->prev == NULL) + { + GNode *parent; + + parent = node->parent; + switch (NODE_INFO(parent)->type) + { + case EGG_MENU_MERGE_TOOLBAR: + toolbar = NODE_INFO(parent)->proxy; + pos = 0; + break; + case EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER: + toolbar = gtk_widget_get_parent(NODE_INFO(parent)->proxy); + g_return_val_if_fail(EGG_IS_TOOLBAR(toolbar), FALSE); + pos = g_list_index(egg_toolbar_get_tool_items(EGG_TOOLBAR(toolbar)), + NODE_INFO(parent)->proxy) + 1; + break; + default: + g_warning("%s: bad parent node type %d", G_STRLOC, + NODE_INFO(parent)->type); + return FALSE; + } + } + else + { + GtkWidget *prev_child; + GNode *sibling; + + sibling = node->prev; + if (NODE_INFO(sibling)->type == EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER) + prev_child = NODE_INFO(sibling)->extra; /* second Separator */ + else + prev_child = NODE_INFO(sibling)->proxy; + + g_return_val_if_fail(GTK_IS_WIDGET(prev_child), FALSE); + toolbar = gtk_widget_get_parent(prev_child); + g_return_val_if_fail(EGG_IS_TOOLBAR(toolbar), FALSE); + + pos = g_list_index(egg_toolbar_get_tool_items(EGG_TOOLBAR(toolbar)), + prev_child) + 1; + } + + if (toolbar_p) + *toolbar_p = toolbar; + if (pos_p) + *pos_p = pos; + + return TRUE; +} + +static void +update_node (EggMenuMerge *self, GNode *node) +{ + EggMenuMergeNode *info; + GNode *child; + EggAction *action; + GList *tmp; + + g_return_if_fail (node != NULL); + g_return_if_fail (NODE_INFO(node) != NULL); + + info = NODE_INFO(node); + + if (NODE_INFO(node)->dirty) + { + const gchar *action_name; + NodeUIReference *ref; + + if (info->uifiles == NULL) { + /* We may need to remove this node. + * This must be done in post order + */ + goto recurse_children; + } + + ref = info->uifiles->data; + action_name = g_quark_to_string (ref->action_quark); + action = get_action_by_name (self, action_name); + + NODE_INFO(node)->dirty = FALSE; + + /* Check if the node doesn't have an action and must have an action */ + if (action == NULL && + info->type != EGG_MENU_MERGE_MENUBAR && + info->type != EGG_MENU_MERGE_TOOLBAR && + info->type != EGG_MENU_MERGE_SEPARATOR && + info->type != EGG_MENU_MERGE_MENU_PLACEHOLDER && + info->type != EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER) + { + /* FIXME: Should we warn here? */ + goto recurse_children; + } + + /* If the widget already has a proxy and the action hasn't changed, then + * we don't have to do anything. + */ + if (info->proxy != NULL && + action == info->action) + { + goto recurse_children; + } + + if (info->action) + g_object_unref (info->action); + info->action = action; + if (info->action) + g_object_ref (info->action); + + switch (info->type) + { + case EGG_MENU_MERGE_MENUBAR: + if (info->proxy == NULL) + { + info->proxy = gtk_menu_bar_new (); + gtk_widget_show (info->proxy); + g_signal_emit (self, merge_signals[ADD_WIDGET], 0, info->proxy); + } + break; + case EGG_MENU_MERGE_MENU: + { + GtkWidget *prev_submenu = NULL; + /* remove the proxy if it is of the wrong type ... */ + if (info->proxy && G_OBJECT_TYPE(info->proxy) != + EGG_ACTION_GET_CLASS(info->action)->menu_item_type) + { + prev_submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(info->proxy)); + if (prev_submenu) + { + g_object_ref (prev_submenu); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(info->proxy),NULL); + } + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + info->proxy = NULL; + } + /* create proxy if needed ... */ + if (info->proxy == NULL) + { + GtkWidget *menushell; + gint pos; + GNode *parent; + + parent = node->parent; + + if (parent && NODE_INFO (parent)->type == EGG_MENU_MERGE_POPUPS) + { + info->proxy = gtk_menu_new(); + } + else if (find_menu_position(node, &menushell, &pos)) + { + GtkWidget *menu; + info->proxy = egg_action_create_menu_item (info->action); + menu = gtk_menu_new(); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(info->proxy), menu); + gtk_menu_set_accel_group (GTK_MENU (menu), self->accel_group); + gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), + info->proxy, pos); + } + } + else + { + egg_action_connect_proxy (info->action, info->proxy); + } + if (prev_submenu) + { + gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), + prev_submenu); + g_object_unref (prev_submenu); + } + } + break; + case EGG_MENU_MERGE_UNDECIDED: + g_warning("found 'undecided node!"); + break; + case EGG_MENU_MERGE_ROOT: + break; + case EGG_MENU_MERGE_TOOLBAR: + if (info->proxy == NULL) + { + info->proxy = egg_toolbar_new (); + gtk_widget_show (info->proxy); + g_signal_emit (self, merge_signals[ADD_WIDGET], 0, info->proxy); + } + break; + case EGG_MENU_MERGE_MENU_PLACEHOLDER: + /* create menu items for placeholders if necessary ... */ + if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) || + !GTK_IS_SEPARATOR_MENU_ITEM (info->extra)) + { + if (info->proxy) + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + if (info->extra) + gtk_container_remove(GTK_CONTAINER(info->extra->parent), + info->extra); + info->proxy = NULL; + info->extra = NULL; + } + if (info->proxy == NULL) + { + GtkWidget *menushell; + gint pos; + + if (find_menu_position(node, &menushell, &pos)) + { + NODE_INFO(node)->proxy = gtk_separator_menu_item_new(); + gtk_menu_shell_insert(GTK_MENU_SHELL(menushell), + NODE_INFO(node)->proxy, pos); + //gtk_widget_show(NODE_INFO(node)->proxy); + + NODE_INFO(node)->extra = gtk_separator_menu_item_new(); + gtk_menu_shell_insert(GTK_MENU_SHELL(menushell), + NODE_INFO(node)->extra, pos+1); + //gtk_widget_show(NODE_INFO(node)->extra); + } + } + break; + case EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER: + /* create toolbar items for placeholders if necessary ... */ + if (!EGG_IS_SEPARATOR_TOOL_ITEM (info->proxy) || + !EGG_IS_SEPARATOR_TOOL_ITEM (info->extra)) + { + if (info->proxy) + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + if (info->extra) + gtk_container_remove(GTK_CONTAINER(info->extra->parent), + info->extra); + info->proxy = NULL; + info->extra = NULL; + } + if (info->proxy == NULL) + { + GtkWidget *toolbar; + gint pos; + + if (find_toolbar_position(node, &toolbar, &pos)) + { + EggToolItem *item; + + item = egg_separator_tool_item_new(); + egg_toolbar_insert_tool_item(EGG_TOOLBAR(toolbar), item, pos); + NODE_INFO(node)->proxy = GTK_WIDGET (item); + //gtk_widget_show(NODE_INFO(node)->proxy); + + item = egg_separator_tool_item_new(); + egg_toolbar_insert_tool_item(EGG_TOOLBAR(toolbar), item, pos+1); + NODE_INFO(node)->extra = GTK_WIDGET (item); + //gtk_widget_show(NODE_INFO(node)->extra); + } + } + break; + case EGG_MENU_MERGE_POPUPS: + break; + case EGG_MENU_MERGE_MENUITEM: + /* remove the proxy if it is of the wrong type ... */ + if (info->proxy && G_OBJECT_TYPE(info->proxy) != + EGG_ACTION_GET_CLASS(info->action)->menu_item_type) + { + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + info->proxy = NULL; + } + /* create proxy if needed ... */ + if (info->proxy == NULL) + { + GtkWidget *menushell; + gint pos; + + if (find_menu_position(node, &menushell, &pos)) + { + info->proxy = egg_action_create_menu_item (info->action); + + gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), + info->proxy, pos); + } + } + else + { + gtk_menu_item_set_submenu(GTK_MENU_ITEM(info->proxy), NULL); + egg_action_connect_proxy (info->action, info->proxy); + } + break; + case EGG_MENU_MERGE_TOOLITEM: + /* remove the proxy if it is of the wrong type ... */ + if (info->proxy && G_OBJECT_TYPE(info->proxy) != + EGG_ACTION_GET_CLASS(info->action)->toolbar_item_type) + { + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + info->proxy = NULL; + } + /* create proxy if needed ... */ + if (info->proxy == NULL) + { + GtkWidget *toolbar; + gint pos; + + if (find_toolbar_position(node, &toolbar, &pos)) + { + info->proxy = egg_action_create_tool_item (info->action); + + egg_toolbar_insert_tool_item (EGG_TOOLBAR (toolbar), + EGG_TOOL_ITEM (info->proxy), pos); + } + } + else + { + egg_action_connect_proxy (info->action, info->proxy); + } + break; + case EGG_MENU_MERGE_SEPARATOR: + if (NODE_INFO (node->parent)->type == EGG_MENU_MERGE_TOOLBAR || + NODE_INFO (node->parent)->type == EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER) + { + GtkWidget *toolbar; + gint pos; + + if (EGG_IS_SEPARATOR_TOOL_ITEM(info->proxy)) + { + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + info->proxy = NULL; + } + + if (find_toolbar_position(node, &toolbar, &pos)) + { + EggToolItem *item = egg_separator_tool_item_new(); + egg_toolbar_insert_tool_item (EGG_TOOLBAR (toolbar), item, pos); + info->proxy = GTK_WIDGET (item); + gtk_widget_show(info->proxy); + } + } + else + { + GtkWidget *menushell; + gint pos; + + if (GTK_IS_SEPARATOR_MENU_ITEM(info->proxy)) + { + gtk_container_remove(GTK_CONTAINER(info->proxy->parent), + info->proxy); + info->proxy = NULL; + } + + if (find_menu_position(node, &menushell, &pos)) + { + info->proxy = gtk_separator_menu_item_new(); + gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), + info->proxy, pos); + gtk_widget_show(info->proxy); + } + } + break; + } + + /* if this node has a widget, but it is the wrong type, remove it */ + } + + recurse_children: + /* process children */ + child = node->children; + while (child) + { + GNode *current; + + current = child; + child = current->next; + update_node (self, current); + } + + /* handle cleanup of dead nodes */ + if (node->children == NULL && NODE_INFO(node)->uifiles == NULL) + { + if (NODE_INFO(node)->proxy) + gtk_widget_destroy(NODE_INFO(node)->proxy); + if ((NODE_INFO(node)->type == EGG_MENU_MERGE_MENU_PLACEHOLDER || + NODE_INFO(node)->type == EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER) && + NODE_INFO(node)->extra) + gtk_widget_destroy(NODE_INFO(node)->extra); + g_chunk_free(NODE_INFO(node), merge_node_chunk); + g_node_destroy(node); + } +} + +static gboolean +do_updates(EggMenuMerge *self) +{ + /* this function needs to check through the tree for dirty nodes. + * For such nodes, it needs to do the following: + * + * 1) check if they are referenced by any loaded UI files anymore. + * In which case, the proxy widget should be destroyed, unless + * there are any subnodes. + * + * 2) lookup the action for this node again. If it is different to + * the current one (or if no previous action has been looked up), + * the proxy is reconnected to the new action (or a new proxy widget + * is created and added to the parent container). + */ + + g_message("do_updates"); + + update_node (self, self->root_node); + + self->update_tag = 0; + return FALSE; +} + +static void +egg_menu_merge_queue_update (EggMenuMerge *self) +{ + if (self->update_tag != 0) + return; + + self->update_tag = g_idle_add((GSourceFunc)do_updates, self); +} + +void +egg_menu_merge_ensure_update (EggMenuMerge *self) +{ + if (self->update_tag != 0) + { + g_source_remove(self->update_tag); + do_updates(self); + } +} + +static gboolean +dirty_traverse_func (GNode *node, gpointer data) +{ + NODE_INFO(node)->dirty = TRUE; + return FALSE; +} + +static void +egg_menu_merge_dirty_all (EggMenuMerge *self) +{ + g_node_traverse(self->root_node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, + dirty_traverse_func, NULL); + egg_menu_merge_queue_update(self); +} diff --git a/lib/egg/egg-menu-merge.h b/lib/egg/egg-menu-merge.h new file mode 100644 index 000000000..f4c440706 --- /dev/null +++ b/lib/egg/egg-menu-merge.h @@ -0,0 +1,100 @@ +#ifndef EGG_MENU_MERGE_H +#define EGG_MENU_MERGE_H + +#include <glib.h> +#include <glib-object.h> + +#include <egg-action.h> +#include <egg-action-group.h> + +#define EGG_TYPE_MENU_MERGE (egg_menu_merge_get_type ()) +#define EGG_MENU_MERGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_MENU_MERGE, EggMenuMerge)) +#define EGG_MENU_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_MENU_MERGE, EggMenuMergeClass)) +#define EGG_IS_MENU_MERGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_MENU_MERGE)) +#define EGG_IS_MENU_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_MENU_MERGE)) +#define EGG_MENU_MERGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_MENU_MERGE, EggMenuMergeClass)) + +typedef struct _EggMenuMerge EggMenuMerge; +typedef struct _EggMenuMergeClass EggMenuMergeClass; +typedef struct _EggMenuMergeNode EggMenuMergeNode; + +typedef enum { + EGG_MENU_MERGE_UNDECIDED, + EGG_MENU_MERGE_ROOT, + EGG_MENU_MERGE_MENUBAR, + EGG_MENU_MERGE_MENU, + EGG_MENU_MERGE_TOOLBAR, + EGG_MENU_MERGE_MENU_PLACEHOLDER, + EGG_MENU_MERGE_TOOLBAR_PLACEHOLDER, + EGG_MENU_MERGE_POPUPS, + EGG_MENU_MERGE_MENUITEM, + EGG_MENU_MERGE_TOOLITEM, + EGG_MENU_MERGE_SEPARATOR, +} EggMenuMergeNodeType; + +struct _EggMenuMerge { + GObject parent; + + GtkAccelGroup *accel_group; + + GNode *root_node; + GList *action_groups; + + guint last_merge_id; + + guint update_tag; +}; + +struct _EggMenuMergeClass { + GObjectClass parent_class; + + void (* add_widget) (EggMenuMerge *merge, GtkWidget *widget); + void (* remove_widget) (EggMenuMerge *merge, GtkWidget *widget); +}; + +struct _EggMenuMergeNode { + EggMenuMergeNodeType type; + + const gchar *name; + + GQuark action_name; + EggAction *action; + GtkWidget *proxy; + GtkWidget *extra; /*GtkMenu for submenus, second separator for placeholders*/ + + GList *uifiles; + + guint dirty : 1; +}; + +GType egg_menu_merge_get_type (void); +EggMenuMerge *egg_menu_merge_new (void); + +/* these two functions will dirty all merge nodes, as they may need to + * be connected up to different actions */ +void egg_menu_merge_insert_action_group (EggMenuMerge *self, + EggActionGroup *action_group, + gint pos); +void egg_menu_merge_remove_action_group (EggMenuMerge *self, + EggActionGroup*action_group); + + +GtkWidget *egg_menu_merge_get_widget (EggMenuMerge *self, + const gchar *path); + +/* these two functions are for adding UI elements to the merged user + * interface */ +guint egg_menu_merge_add_ui_from_string (EggMenuMerge *self, + const gchar *buffer, + guint length, + GError **error); +guint egg_menu_merge_add_ui_from_file (EggMenuMerge *self, + const gchar *filename, + GError **error); +void egg_menu_merge_remove_ui (EggMenuMerge *self, + guint merge_id); + +void egg_menu_merge_ensure_update (EggMenuMerge *self); + + +#endif /* EGG_MENU_MERGE_H */ diff --git a/lib/egg/egg-menu.h b/lib/egg/egg-menu.h new file mode 100644 index 000000000..eb71ef66d --- /dev/null +++ b/lib/egg/egg-menu.h @@ -0,0 +1,13 @@ +#ifndef EGG_MENU_H +#define EGG_MENU_H + +#include <libegg/menu/egg-action.h> +#include <libegg/menu/egg-toggle-action.h> +#include <libegg/menu/egg-radio-action.h> +#include <libegg/menu/egg-action-group.h> + +#include <libegg/menu/egg-menu-merge.h> + +#include <libegg/menu/egg-accel-dialog.h> + +#endif diff --git a/lib/egg/egg-radio-action.c b/lib/egg/egg-radio-action.c new file mode 100644 index 000000000..888448c50 --- /dev/null +++ b/lib/egg/egg-radio-action.c @@ -0,0 +1,194 @@ +#include "egg-radio-action.h" + +static void egg_radio_action_init (EggRadioAction *action); +static void egg_radio_action_class_init (EggRadioActionClass *class); + +GType +egg_radio_action_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggRadioActionClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_radio_action_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggRadioAction), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_radio_action_init, + }; + + type = g_type_register_static (EGG_TYPE_TOGGLE_ACTION, + "EggRadioAction", + &type_info, 0); + } + return type; +} + +static void egg_radio_action_finalize (GObject *object); +static void egg_radio_action_activate (EggAction *action); + +static GObjectClass *parent_class = NULL; + +static void +egg_radio_action_class_init (EggRadioActionClass *class) +{ + GObjectClass *object_class; + EggActionClass *action_class; + + parent_class = g_type_class_peek_parent (class); + object_class = G_OBJECT_CLASS (class); + action_class = EGG_ACTION_CLASS (class); + + object_class->finalize = egg_radio_action_finalize; + + action_class->activate = egg_radio_action_activate; +} + +static void +egg_radio_action_init (EggRadioAction *action) +{ + action->group = g_slist_prepend (NULL, action); +} + +static void +egg_radio_action_finalize (GObject *object) +{ + EggRadioAction *action; + GSList *tmp_list; + + g_return_if_fail (EGG_IS_RADIO_ACTION (object)); + + action = EGG_RADIO_ACTION (object); + + action->group = g_slist_remove (action->group, action); + + tmp_list = action->group; + + while (tmp_list) + { + EggRadioAction *tmp_action = tmp_list->data; + + tmp_list = tmp_list->next; + tmp_action->group = action->group; + } + + if (parent_class->finalize) + (* parent_class->finalize) (object); +} + +static void +egg_radio_action_activate (EggAction *action) +{ + EggRadioAction *radio_action; + EggToggleAction *toggle_action; + EggToggleAction *tmp_action; + GSList *tmp_list; + + g_return_if_fail (EGG_IS_RADIO_ACTION (action)); + + radio_action = EGG_RADIO_ACTION (action); + toggle_action = EGG_TOGGLE_ACTION (action); + + if (toggle_action->active) + { + tmp_list = radio_action->group; + + while (tmp_list) + { + tmp_action = tmp_list->data; + tmp_list = tmp_list->next; + + if (tmp_action->active && (tmp_action != toggle_action)) + { + toggle_action->active = !toggle_action->active; + break; + } + } + } + else + { + toggle_action->active = !toggle_action->active; + + tmp_list = radio_action->group; + while (tmp_list) + { + tmp_action = tmp_list->data; + tmp_list = tmp_list->next; + + if (tmp_action->active && (tmp_action != toggle_action)) + { + egg_action_activate (EGG_ACTION (tmp_action)); + break; + } + } + } + + egg_toggle_action_toggled (toggle_action); +} + +/** + * egg_radio_action_get_group: + * @action: the action object + * + * Returns: the list representing the radio group for this object + */ +GSList * +egg_radio_action_get_group (EggRadioAction *action) +{ + g_return_val_if_fail (EGG_IS_RADIO_ACTION (action), NULL); + + return action->group; +} + +/** + * egg_radio_action_set_group: + * @action: the action object + * @group: a list representing a radio group + * + * Sets the radio group for the radio action object. + */ +void +egg_radio_action_set_group (EggRadioAction *action, GSList *group) +{ + g_return_if_fail (EGG_IS_RADIO_ACTION (action)); + g_return_if_fail (!g_slist_find (group, action)); + + if (action->group) + { + GSList *slist; + + action->group = g_slist_remove (action->group, action); + + for (slist = action->group; slist; slist = slist->next) + { + EggRadioAction *tmp_action = slist->data; + + tmp_action->group = action->group; + } + } + + action->group = g_slist_prepend (group, action); + + if (group) + { + GSList *slist; + + for (slist = action->group; slist; slist = slist->next) + { + EggRadioAction *tmp_action = slist->data; + + tmp_action->group = action->group; + } + } + else + { + EGG_TOGGLE_ACTION (action)->active = TRUE; + } +} diff --git a/lib/egg/egg-radio-action.h b/lib/egg/egg-radio-action.h new file mode 100644 index 000000000..6fe097be5 --- /dev/null +++ b/lib/egg/egg-radio-action.h @@ -0,0 +1,34 @@ +#ifndef EGG_RADIO_ACTION_H +#define EGG_RADIO_ACTION_H + +#include <gtk/gtk.h> +#include <egg-action.h> +#include <egg-toggle-action.h> + +#define EGG_TYPE_RADIO_ACTION (egg_radio_action_get_type ()) +#define EGG_RADIO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_RADIO_ACTION, EggRadioAction)) +#define EGG_RADIO_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_RADIO_ACTION, EggRadioActionClass)) +#define EGG_IS_RADIO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_RADIO_ACTION)) +#define EGG_IS_RADIO_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_RADIO_ACTION)) +#define EGG_RADIO_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_RADIO_ACTION, EggRadioActionClass)) + +typedef struct _EggRadioAction EggRadioAction; +typedef struct _EggRadioActionClass EggRadioActionClass; + +struct _EggRadioAction { + EggToggleAction parent; + + GSList *group; +}; + +struct _EggRadioActionClass { + EggToggleActionClass parent_class; +}; + +GType egg_radio_action_get_type (void); + +GSList *egg_radio_action_get_group (EggRadioAction *action); +void egg_radio_action_set_group (EggRadioAction *action, + GSList *group); + +#endif diff --git a/lib/egg/egg-toggle-action.c b/lib/egg/egg-toggle-action.c new file mode 100644 index 000000000..6c0c0b442 --- /dev/null +++ b/lib/egg/egg-toggle-action.c @@ -0,0 +1,197 @@ +#include "egg-toggle-action.h" +#include "eggtoggletoolbutton.h" + +enum { + TOGGLED, + LAST_SIGNAL +}; + +static void egg_toggle_action_init (EggToggleAction *action); +static void egg_toggle_action_class_init (EggToggleActionClass *class); + +GType +egg_toggle_action_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggToggleActionClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_toggle_action_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggToggleAction), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_toggle_action_init, + }; + + type = g_type_register_static (EGG_TYPE_ACTION, + "EggToggleAction", + &type_info, 0); + } + return type; +} + +static void egg_toggle_action_activate (EggAction *action); +static void egg_toggle_action_real_toggled (EggToggleAction *action); +static void connect_proxy (EggAction *action, + GtkWidget *proxy); +static void disconnect_proxy (EggAction *action, + GtkWidget *proxy); + +static GObjectClass *parent_class = NULL; +static guint action_signals[LAST_SIGNAL] = { 0 }; + +static void +egg_toggle_action_class_init (EggToggleActionClass *class) +{ + EggActionClass *action_class; + + parent_class = g_type_class_peek_parent (class); + action_class = EGG_ACTION_CLASS (class); + + action_class->activate = egg_toggle_action_activate; + action_class->connect_proxy = connect_proxy; + action_class->disconnect_proxy = disconnect_proxy; + + action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM; + action_class->toolbar_item_type = EGG_TYPE_TOGGLE_TOOL_BUTTON; + + class->toggled = egg_toggle_action_real_toggled; + + action_signals[TOGGLED] = + g_signal_new ("toggled", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EggToggleActionClass, toggled), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void +egg_toggle_action_init (EggToggleAction *action) +{ + action->active = FALSE; +} + +static void +egg_toggle_action_activate (EggAction *action) +{ + EggToggleAction *toggle_action; + + g_return_if_fail (EGG_IS_TOGGLE_ACTION (action)); + + toggle_action = EGG_TOGGLE_ACTION (action); + + toggle_action->active = !toggle_action->active; + + egg_toggle_action_toggled (toggle_action); +} + +static void +egg_toggle_action_real_toggled (EggToggleAction *action) +{ + GSList *slist; + + g_return_if_fail (EGG_IS_TOGGLE_ACTION (action)); + + for (slist = EGG_ACTION (action)->proxies; slist; slist = slist->next) + { + GtkWidget *proxy = slist->data; + + egg_action_block_activate_from (EGG_ACTION (action), proxy); + if (GTK_IS_CHECK_MENU_ITEM (proxy)) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy), + action->active); + else if (EGG_IS_TOGGLE_TOOL_BUTTON (proxy)) + egg_toggle_tool_button_set_active (EGG_TOGGLE_TOOL_BUTTON (proxy), + action->active); + else { + g_warning ("Don't know how to toggle `%s' widgets", + G_OBJECT_TYPE_NAME (proxy)); + } + egg_action_unblock_activate_from (EGG_ACTION (action), proxy); + } +} + +static void +connect_proxy (EggAction *action, GtkWidget *proxy) +{ + EggToggleAction *toggle_action; + + toggle_action = EGG_TOGGLE_ACTION (action); + + /* do this before hand, so that we don't call the "activate" handler */ + if (GTK_IS_MENU_ITEM (proxy)) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy), + toggle_action->active); + else if (EGG_IS_TOGGLE_TOOL_BUTTON (proxy)) + egg_toggle_tool_button_set_active (EGG_TOGGLE_TOOL_BUTTON (proxy), + toggle_action->active); + + (* EGG_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy); +} + +static void +disconnect_proxy (EggAction *action, GtkWidget *proxy) +{ + EggToggleAction *toggle_action; + + toggle_action = EGG_TOGGLE_ACTION (action); + + (* EGG_ACTION_CLASS (parent_class)->disconnect_proxy) (action, proxy); +} + +/** + * egg_toggle_action_toggled: + * @action: the action object + * + * Emits the "toggled" signal on the toggle action. + */ +void +egg_toggle_action_toggled (EggToggleAction *action) +{ + g_return_if_fail (EGG_IS_TOGGLE_ACTION (action)); + + g_signal_emit (action, action_signals[TOGGLED], 0); +} + +/** + * egg_toggle_action_set_active: + * @action: the action object + * @is_active: whether the action should be checked or not + * + * Sets the checked state on the toggle action. + */ +void +egg_toggle_action_set_active (EggToggleAction *action, gboolean is_active) +{ + g_return_if_fail (EGG_IS_TOGGLE_ACTION (action)); + + is_active = is_active != 0; + + if (action->active != is_active) + { + egg_action_activate (EGG_ACTION (action)); + } +} + +/** + * egg_toggle_action_get_active: + * @action: the action object + * + * Returns: the checked state of the toggle action + */ +gboolean +egg_toggle_action_get_active (EggToggleAction *action) +{ + g_return_val_if_fail (EGG_IS_TOGGLE_ACTION (action), FALSE); + + return action->active; +} diff --git a/lib/egg/egg-toggle-action.h b/lib/egg/egg-toggle-action.h new file mode 100644 index 000000000..4a5293bfe --- /dev/null +++ b/lib/egg/egg-toggle-action.h @@ -0,0 +1,38 @@ +#ifndef EGG_TOGGLE_ACTION_H +#define EGG_TOGGLE_ACTION_H + +#include <gtk/gtk.h> +#include <egg-action.h> + +#define EGG_TYPE_TOGGLE_ACTION (egg_toggle_action_get_type ()) +#define EGG_TOGGLE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_TOGGLE_ACTION, EggToggleAction)) +#define EGG_TOGGLE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_TOGGLE_ACTION, EggToggleActionClass)) +#define EGG_IS_TOGGLE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_TOGGLE_ACTION)) +#define EGG_IS_TOGGLE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_TOGGLE_ACTION)) +#define EGG_TOGGLE_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_TOGGLE_ACTION, EggToggleActionClass)) + +typedef struct _EggToggleAction EggToggleAction; +typedef struct _EggToggleActionClass EggToggleActionClass; + +struct _EggToggleAction +{ + EggAction parent; + + guint active : 1; +}; + +struct _EggToggleActionClass +{ + EggActionClass parent_class; + + void (* toggled) (EggToggleAction *action); +}; + +GType egg_toggle_action_get_type (void); + +void egg_toggle_action_toggled (EggToggleAction *action); +void egg_toggle_action_set_active (EggToggleAction *action, + gboolean is_active); +gboolean egg_toggle_action_get_active (EggToggleAction *action); + +#endif diff --git a/lib/egg/eggmarshalers.c b/lib/egg/eggmarshalers.c new file mode 100644 index 000000000..3cc9b963e --- /dev/null +++ b/lib/egg/eggmarshalers.c @@ -0,0 +1,277 @@ + +#include <glib-object.h> + + +#ifdef G_ENABLE_DEBUG +#define g_marshal_value_peek_boolean(v) g_value_get_boolean (v) +#define g_marshal_value_peek_char(v) g_value_get_char (v) +#define g_marshal_value_peek_uchar(v) g_value_get_uchar (v) +#define g_marshal_value_peek_int(v) g_value_get_int (v) +#define g_marshal_value_peek_uint(v) g_value_get_uint (v) +#define g_marshal_value_peek_long(v) g_value_get_long (v) +#define g_marshal_value_peek_ulong(v) g_value_get_ulong (v) +#define g_marshal_value_peek_int64(v) g_value_get_int64 (v) +#define g_marshal_value_peek_uint64(v) g_value_get_uint64 (v) +#define g_marshal_value_peek_enum(v) g_value_get_enum (v) +#define g_marshal_value_peek_flags(v) g_value_get_flags (v) +#define g_marshal_value_peek_float(v) g_value_get_float (v) +#define g_marshal_value_peek_double(v) g_value_get_double (v) +#define g_marshal_value_peek_string(v) (char*) g_value_get_string (v) +#define g_marshal_value_peek_param(v) g_value_get_param (v) +#define g_marshal_value_peek_boxed(v) g_value_get_boxed (v) +#define g_marshal_value_peek_pointer(v) g_value_get_pointer (v) +#define g_marshal_value_peek_object(v) g_value_get_object (v) +#else /* !G_ENABLE_DEBUG */ +/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API. + * Do not access GValues directly in your code. Instead, use the + * g_value_get_*() functions + */ +#define g_marshal_value_peek_boolean(v) (v)->data[0].v_int +#define g_marshal_value_peek_char(v) (v)->data[0].v_int +#define g_marshal_value_peek_uchar(v) (v)->data[0].v_uint +#define g_marshal_value_peek_int(v) (v)->data[0].v_int +#define g_marshal_value_peek_uint(v) (v)->data[0].v_uint +#define g_marshal_value_peek_long(v) (v)->data[0].v_long +#define g_marshal_value_peek_ulong(v) (v)->data[0].v_ulong +#define g_marshal_value_peek_int64(v) (v)->data[0].v_int64 +#define g_marshal_value_peek_uint64(v) (v)->data[0].v_uint64 +#define g_marshal_value_peek_enum(v) (v)->data[0].v_int +#define g_marshal_value_peek_flags(v) (v)->data[0].v_uint +#define g_marshal_value_peek_float(v) (v)->data[0].v_float +#define g_marshal_value_peek_double(v) (v)->data[0].v_double +#define g_marshal_value_peek_string(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_param(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_boxed(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_pointer(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_object(v) (v)->data[0].v_pointer +#endif /* !G_ENABLE_DEBUG */ + + +/* VOID:OBJECT,OBJECT (eggmarshalers.list:1) */ +void +_egg_marshal_VOID__OBJECT_OBJECT (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer data2); + register GMarshalFunc_VOID__OBJECT_OBJECT callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 3); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_object (param_values + 1), + g_marshal_value_peek_object (param_values + 2), + data2); +} + +/* VOID:OBJECT,STRING,LONG,LONG (eggmarshalers.list:2) */ +void +_egg_marshal_VOID__OBJECT_STRING_LONG_LONG (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__OBJECT_STRING_LONG_LONG) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + glong arg_3, + glong arg_4, + gpointer data2); + register GMarshalFunc_VOID__OBJECT_STRING_LONG_LONG callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 5); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__OBJECT_STRING_LONG_LONG) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_object (param_values + 1), + g_marshal_value_peek_string (param_values + 2), + g_marshal_value_peek_long (param_values + 3), + g_marshal_value_peek_long (param_values + 4), + data2); +} + +/* VOID:OBJECT,LONG (eggmarshalers.list:3) */ +void +_egg_marshal_VOID__OBJECT_LONG (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__OBJECT_LONG) (gpointer data1, + gpointer arg_1, + glong arg_2, + gpointer data2); + register GMarshalFunc_VOID__OBJECT_LONG callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 3); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__OBJECT_LONG) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_object (param_values + 1), + g_marshal_value_peek_long (param_values + 2), + data2); +} + +/* VOID:OBJECT,STRING,STRING (eggmarshalers.list:4) */ +void +_egg_marshal_VOID__OBJECT_STRING_STRING (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__OBJECT_STRING_STRING) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer arg_3, + gpointer data2); + register GMarshalFunc_VOID__OBJECT_STRING_STRING callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 4); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__OBJECT_STRING_STRING) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_object (param_values + 1), + g_marshal_value_peek_string (param_values + 2), + g_marshal_value_peek_string (param_values + 3), + data2); +} + +/* BOOLEAN:VOID (eggmarshalers.list:5) */ +void +_egg_marshal_BOOLEAN__VOID (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef gboolean (*GMarshalFunc_BOOLEAN__VOID) (gpointer data1, + gpointer data2); + register GMarshalFunc_BOOLEAN__VOID callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + gboolean v_return; + + g_return_if_fail (return_value != NULL); + g_return_if_fail (n_param_values == 1); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_BOOLEAN__VOID) (marshal_data ? marshal_data : cc->callback); + + v_return = callback (data1, + data2); + + g_value_set_boolean (return_value, v_return); +} + +/* OBJECT:VOID (eggmarshalers.list:6) */ +void +_egg_marshal_OBJECT__VOID (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef GObject* (*GMarshalFunc_OBJECT__VOID) (gpointer data1, + gpointer data2); + register GMarshalFunc_OBJECT__VOID callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + GObject* v_return; + + g_return_if_fail (return_value != NULL); + g_return_if_fail (n_param_values == 1); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_OBJECT__VOID) (marshal_data ? marshal_data : cc->callback); + + v_return = callback (data1, + data2); + + g_value_set_object_take_ownership (return_value, v_return); +} + diff --git a/lib/egg/eggmarshalers.h b/lib/egg/eggmarshalers.h new file mode 100644 index 000000000..dac29f1d1 --- /dev/null +++ b/lib/egg/eggmarshalers.h @@ -0,0 +1,60 @@ + +#ifndef ___egg_marshal_MARSHAL_H__ +#define ___egg_marshal_MARSHAL_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +/* VOID:OBJECT,OBJECT (eggmarshalers.list:1) */ +extern void _egg_marshal_VOID__OBJECT_OBJECT (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +/* VOID:OBJECT,STRING,LONG,LONG (eggmarshalers.list:2) */ +extern void _egg_marshal_VOID__OBJECT_STRING_LONG_LONG (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +/* VOID:OBJECT,LONG (eggmarshalers.list:3) */ +extern void _egg_marshal_VOID__OBJECT_LONG (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +/* VOID:OBJECT,STRING,STRING (eggmarshalers.list:4) */ +extern void _egg_marshal_VOID__OBJECT_STRING_STRING (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +/* BOOLEAN:VOID (eggmarshalers.list:5) */ +extern void _egg_marshal_BOOLEAN__VOID (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +/* OBJECT:VOID (eggmarshalers.list:6) */ +extern void _egg_marshal_OBJECT__VOID (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); + +G_END_DECLS + +#endif /* ___egg_marshal_MARSHAL_H__ */ + diff --git a/lib/egg/eggmarshalers.list b/lib/egg/eggmarshalers.list new file mode 100644 index 000000000..03890af58 --- /dev/null +++ b/lib/egg/eggmarshalers.list @@ -0,0 +1,6 @@ +VOID:OBJECT,OBJECT +VOID:OBJECT,STRING,LONG,LONG +VOID:OBJECT,LONG +VOID:OBJECT,STRING,STRING +BOOLEAN:VOID +OBJECT:VOID diff --git a/lib/egg/eggradiotoolbutton.c b/lib/egg/eggradiotoolbutton.c new file mode 100644 index 000000000..ec14990b8 --- /dev/null +++ b/lib/egg/eggradiotoolbutton.c @@ -0,0 +1,121 @@ +/* eggradiotoolbutton.c + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "eggradiotoolbutton.h" +#include <gtk/gtkradiobutton.h> + +#ifndef _ +# define _(s) (s) +#endif + +static void egg_radio_tool_button_init (EggRadioToolButton *button); +static void egg_radio_tool_button_class_init (EggRadioToolButtonClass *klass); + +GType +egg_radio_tool_button_get_type (void) +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggRadioToolButtonClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_radio_tool_button_class_init, + (GClassFinalizeFunc) NULL, + NULL, + sizeof (EggRadioToolButton), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_radio_tool_button_init + }; + + type = g_type_register_static (EGG_TYPE_TOGGLE_TOOL_BUTTON, + "EggRadioToolButton", &type_info, 0); + } + return type; +} + + +static void +egg_radio_tool_button_class_init (EggRadioToolButtonClass *klass) +{ + EggToolButtonClass *toolbutton_class; + + toolbutton_class = (EggToolButtonClass *)klass; + + toolbutton_class->button_type = GTK_TYPE_RADIO_BUTTON; +} + +static void +egg_radio_tool_button_init (EggRadioToolButton *button) +{ + gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (EGG_TOOL_BUTTON (button)->button), FALSE); +} + +EggToolItem * +egg_radio_tool_button_new (GSList *group) +{ + EggRadioToolButton *button; + + button = g_object_new (EGG_TYPE_RADIO_TOOL_BUTTON, + NULL); + + egg_radio_tool_button_set_group (button, group); + + return EGG_TOOL_ITEM (button); +} + +EggToolItem * +egg_radio_tool_button_new_from_stock (GSList *group, + const gchar *stock_id) +{ + EggRadioToolButton *button; + + button = g_object_new (EGG_TYPE_RADIO_TOOL_BUTTON, + "stock_id", stock_id, + "use_underline", TRUE, + NULL); + + + egg_radio_tool_button_set_group (button, group); + + return EGG_TOOL_ITEM (button); +} + +GSList * +egg_radio_tool_button_get_group (EggRadioToolButton *button) +{ + g_return_val_if_fail (EGG_IS_RADIO_TOOL_BUTTON (button), NULL); + + return gtk_radio_button_get_group (GTK_RADIO_BUTTON (EGG_TOOL_BUTTON (button)->button)); +} + +void +egg_radio_tool_button_set_group (EggRadioToolButton *button, + GSList *group) +{ + g_return_if_fail (EGG_IS_RADIO_TOOL_BUTTON (button)); + + gtk_radio_button_set_group (GTK_RADIO_BUTTON (EGG_TOOL_BUTTON (button)->button), group); +} + diff --git a/lib/egg/eggradiotoolbutton.h b/lib/egg/eggradiotoolbutton.h new file mode 100644 index 000000000..44f6a4c1f --- /dev/null +++ b/lib/egg/eggradiotoolbutton.h @@ -0,0 +1,57 @@ +/* eggradiotoolbutton.h + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_RADIO_TOOL_BUTTON_H__ +#define __EGG_RADIO_TOOL_BUTTON_H__ + +#include "eggtoggletoolbutton.h" + +#define EGG_TYPE_RADIO_TOOL_BUTTON (egg_radio_tool_button_get_type ()) +#define EGG_RADIO_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_RADIO_TOOL_BUTTON, EggRadioToolButton)) +#define EGG_RADIO_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_RADIO_TOOL_BUTTON, EggRadioToolButtonClass)) +#define EGG_IS_RADIO_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_RADIO_TOOL_BUTTON)) +#define EGG_IS_RADIO_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_RADIO_TOOL_BUTTON)) +#define EGG_RADIO_TOOL_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_RADIO_TOOL_BUTTON, EggRadioToolButtonClass)) + +typedef struct _EggRadioToolButton EggRadioToolButton; +typedef struct _EggRadioToolButtonClass EggRadioToolButtonClass; + +struct _EggRadioToolButton +{ + EggToggleToolButton parent; +}; + +struct _EggRadioToolButtonClass +{ + EggToggleToolButtonClass parent_class; +}; + +GType egg_radio_tool_button_get_type (void) G_GNUC_CONST; +EggToolItem *egg_radio_tool_button_new (GSList *group); +EggToolItem *egg_radio_tool_button_new_from_stock (GSList *group, + const gchar *stock_id); + +GSList *egg_radio_tool_button_get_group (EggRadioToolButton *button); +void egg_radio_tool_button_set_group (EggRadioToolButton *button, + GSList *group); + + +#endif /* __EGG_RADIO_TOOL_BUTTON_H__ */ diff --git a/lib/egg/eggseparatortoolitem.c b/lib/egg/eggseparatortoolitem.c new file mode 100644 index 000000000..e3fe098fc --- /dev/null +++ b/lib/egg/eggseparatortoolitem.c @@ -0,0 +1,86 @@ +#include <gtk/gtkseparatormenuitem.h> +#include "eggseparatortoolitem.h" + +#ifndef _ +# define _(s) (s) +#endif + +static void egg_separator_tool_item_init (EggSeparatorToolItem *self); +static void egg_separator_tool_item_class_init (EggSeparatorToolItemClass*class); + +static void egg_separator_tool_item_add (GtkContainer *container, + GtkWidget *child); +static GtkWidget *egg_separator_tool_item_create_menu_proxy (EggToolItem *self); + +static GObjectClass *parent_class = NULL; + + +GType +egg_separator_tool_item_get_type (void) +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggSeparatorToolItemClass), + (GBaseInitFunc) 0, + (GBaseFinalizeFunc) 0, + (GClassInitFunc) egg_separator_tool_item_class_init, + (GClassFinalizeFunc) 0, + NULL, + sizeof (EggSeparatorToolItem), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_separator_tool_item_init + }; + + type = g_type_register_static (EGG_TYPE_TOOL_ITEM, + "EggSeparatorToolItem", &type_info, 0); + } + return type; +} + + +static void +egg_separator_tool_item_class_init (EggSeparatorToolItemClass *class) +{ + GtkContainerClass *container_class; + EggToolItemClass *toolitem_class; + + parent_class = g_type_class_peek_parent (class); + container_class = (GtkContainerClass *)class; + toolitem_class = (EggToolItemClass *)class; + + container_class->add = egg_separator_tool_item_add; + toolitem_class->create_menu_proxy = egg_separator_tool_item_create_menu_proxy; +} + +static void +egg_separator_tool_item_init (EggSeparatorToolItem *self) +{ +} + +static void +egg_separator_tool_item_add (GtkContainer *container, GtkWidget *child) +{ + g_warning("attempt to add a child to an EggSeparatorToolItem"); +} + +static GtkWidget * +egg_separator_tool_item_create_menu_proxy (EggToolItem *item) +{ + return gtk_separator_menu_item_new (); +} + + +EggToolItem * +egg_separator_tool_item_new (void) +{ + EggToolItem *self; + + self = g_object_new (EGG_TYPE_SEPARATOR_TOOL_ITEM, + NULL); + + return self; +} diff --git a/lib/egg/eggseparatortoolitem.h b/lib/egg/eggseparatortoolitem.h new file mode 100644 index 000000000..2947b981b --- /dev/null +++ b/lib/egg/eggseparatortoolitem.h @@ -0,0 +1,50 @@ +/* eggtoggletoolbutton.h + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_SEPARATOR_TOOL_ITEM_H__ +#define __EGG_SEPARATOR_TOOL_ITEM_H__ + +#include "eggtoolitem.h" + +#define EGG_TYPE_SEPARATOR_TOOL_ITEM (egg_separator_tool_item_get_type ()) +#define EGG_SEPARATOR_TOOL_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_SEPARATOR_TOOL_ITEM, EggSeparatorToolItem)) +#define EGG_SEPARATOR_TOOL_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_SEPARATOR_TOOL_ITEM, EggSeparatorToolItemClass)) +#define EGG_IS_SEPARATOR_TOOL_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_SEPARATOR_TOOL_ITEM)) +#define EGG_IS_SEPARATOR_TOOL_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_SEPARATOR_TOOL_ITEM)) +#define EGG_SEPARATOR_TOOL_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_SEPARATOR_TOOL_ITEM, EggSeparatorToolItemClass)) + +typedef struct _EggSeparatorToolItem EggSeparatorToolItem; +typedef struct _EggSeparatorToolItemClass EggSeparatorToolItemClass; + +struct _EggSeparatorToolItem +{ + EggToolItem parent; +}; + +struct _EggSeparatorToolItemClass +{ + EggToolItemClass parent_class; +}; + +GType egg_separator_tool_item_get_type (void) G_GNUC_CONST; +EggToolItem *egg_separator_tool_item_new (void); + +#endif /* __EGG_SEPARATOR_TOOL_ITEM_H__ */ diff --git a/lib/egg/eggtoggletoolbutton.c b/lib/egg/eggtoggletoolbutton.c new file mode 100644 index 000000000..c694eb9ad --- /dev/null +++ b/lib/egg/eggtoggletoolbutton.c @@ -0,0 +1,192 @@ +/* eggtoggletoolbutton.c + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "eggtoggletoolbutton.h" +#include <gtk/gtkcheckmenuitem.h> +#include <gtk/gtklabel.h> +#include <gtk/gtktogglebutton.h> + +#ifndef _ +# define _(s) (s) +#endif + +enum { + TOGGLED, + LAST_SIGNAL +}; + +static void egg_toggle_tool_button_init (EggToggleToolButton *button); +static void egg_toggle_tool_button_class_init (EggToggleToolButtonClass *klass); + +static GtkWidget *egg_toggle_tool_button_create_menu_proxy (EggToolItem *button); + + +static void button_toggled (GtkWidget *widget, + EggToggleToolButton *button); + + + +static GObjectClass *parent_class = NULL; +static guint toggle_signals[LAST_SIGNAL] = { 0 }; + +GType +egg_toggle_tool_button_get_type (void) +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggToggleToolButtonClass), + (GBaseInitFunc) 0, + (GBaseFinalizeFunc) 0, + (GClassInitFunc) egg_toggle_tool_button_class_init, + (GClassFinalizeFunc) 0, + NULL, + sizeof (EggToggleToolButton), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_toggle_tool_button_init + }; + + type = g_type_register_static (EGG_TYPE_TOOL_BUTTON, + "EggToggleToolButton", &type_info, 0); + } + return type; +} + + +static void +egg_toggle_tool_button_class_init (EggToggleToolButtonClass *klass) +{ + EggToolItemClass *toolitem_class; + EggToolButtonClass *toolbutton_class; + + parent_class = g_type_class_peek_parent (klass); + + toolitem_class = (EggToolItemClass *)klass; + toolbutton_class = (EggToolButtonClass *)klass; + + toolitem_class->create_menu_proxy = egg_toggle_tool_button_create_menu_proxy; + toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON; + + toggle_signals[TOGGLED] = + g_signal_new ("toggled", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EggToggleToolButtonClass, toggled), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void +egg_toggle_tool_button_init (EggToggleToolButton *button) +{ + g_signal_connect_object (EGG_TOOL_BUTTON (button)->button, "toggled", + G_CALLBACK (button_toggled), button, 0); +} + +static GtkWidget * +egg_toggle_tool_button_create_menu_proxy (EggToolItem *item) +{ + EggToggleToolButton *button = EGG_TOGGLE_TOOL_BUTTON (item); + GtkWidget *menu_item; + const char *label; + + label = gtk_label_get_text (GTK_LABEL (EGG_TOOL_BUTTON (button)->label)); + + menu_item = gtk_check_menu_item_new_with_mnemonic (label); + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item), + button->active); + g_signal_connect_object (menu_item, "activate", + G_CALLBACK (gtk_button_clicked), + EGG_TOOL_BUTTON (button)->button, + G_CONNECT_SWAPPED); + + return menu_item; +} + +static void +button_toggled (GtkWidget *widget, + EggToggleToolButton *button) +{ + gboolean toggle_active; + + toggle_active = GTK_TOGGLE_BUTTON (widget)->active; + if (toggle_active != button->active) + { + button->active = toggle_active; + g_signal_emit (G_OBJECT (button), toggle_signals[TOGGLED], 0); + } +} + +EggToolItem * +egg_toggle_tool_button_new (void) +{ + EggToolButton *button; + + button = g_object_new (EGG_TYPE_TOGGLE_TOOL_BUTTON, + NULL); + + return EGG_TOOL_ITEM (button); +} + +EggToolItem * +egg_toggle_tool_button_new_from_stock (const gchar *stock_id) +{ + EggToolButton *button; + + button = g_object_new (EGG_TYPE_TOGGLE_TOOL_BUTTON, + "stock_id", stock_id, + "use_underline", TRUE, + NULL); + + return EGG_TOOL_ITEM (button); +} + +void +egg_toggle_tool_button_toggled (EggToggleToolButton *button) +{ + g_return_if_fail (EGG_IS_TOGGLE_TOOL_BUTTON (button)); + + gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(EGG_TOOL_BUTTON(button)->button)); +} + +void +egg_toggle_tool_button_set_active (EggToggleToolButton *button, + gboolean is_active) +{ + g_return_if_fail (EGG_IS_TOGGLE_TOOL_BUTTON (button)); + + is_active = is_active != FALSE; + + if (button->active != is_active) + gtk_button_clicked (GTK_BUTTON (EGG_TOOL_BUTTON (button)->button)); +} + +gboolean +egg_toggle_tool_button_get_active (EggToggleToolButton *button) +{ + g_return_val_if_fail (EGG_IS_TOGGLE_TOOL_BUTTON (button), FALSE); + + return button->active; +} diff --git a/lib/egg/eggtoggletoolbutton.h b/lib/egg/eggtoggletoolbutton.h new file mode 100644 index 000000000..587d42257 --- /dev/null +++ b/lib/egg/eggtoggletoolbutton.h @@ -0,0 +1,64 @@ +/* eggtoggletoolbutton.h + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_TOGGLE_TOOL_BUTTON_H__ +#define __EGG_TOGGLE_TOOL_BUTTON_H__ + +#include "eggtoolbutton.h" + +G_BEGIN_DECLS + +#define EGG_TYPE_TOGGLE_TOOL_BUTTON (egg_toggle_tool_button_get_type ()) +#define EGG_TOGGLE_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_TOGGLE_TOOL_BUTTON, EggToggleToolButton)) +#define EGG_TOGGLE_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_TOGGLE_TOOL_BUTTON, EggToggleToolButtonClass)) +#define EGG_IS_TOGGLE_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_TOGGLE_TOOL_BUTTON)) +#define EGG_IS_TOGGLE_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_TOGGLE_TOOL_BUTTON)) +#define EGG_TOGGLE_TOOL_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_TOGGLE_TOOL_BUTTON, EggToggleToolButtonClass)) + +typedef struct _EggToggleToolButton EggToggleToolButton; +typedef struct _EggToggleToolButtonClass EggToggleToolButtonClass; + +struct _EggToggleToolButton +{ + EggToolButton parent; + + guint active : 1; +}; + +struct _EggToggleToolButtonClass +{ + EggToolButtonClass parent_class; + + void (* toggled) (EggToggleToolButton *button); +}; + +GType egg_toggle_tool_button_get_type (void) G_GNUC_CONST; +EggToolItem *egg_toggle_tool_button_new (void); +EggToolItem *egg_toggle_tool_button_new_from_stock (const gchar *stock_id); + +void egg_toggle_tool_button_toggled (EggToggleToolButton *button); +void egg_toggle_tool_button_set_active (EggToggleToolButton *button, + gboolean is_active); +gboolean egg_toggle_tool_button_get_active (EggToggleToolButton *button); + +G_END_DECLS + +#endif /* __EGG_TOGGLE_TOOL_BUTTON_H__ */ diff --git a/lib/egg/eggtoolbar.c b/lib/egg/eggtoolbar.c new file mode 100644 index 000000000..3f90fed6a --- /dev/null +++ b/lib/egg/eggtoolbar.c @@ -0,0 +1,1850 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * GtkToolbar copyright (C) Federico Mena + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GTK+ Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + */ + +#include <gtk/gtkarrow.h> +#include "eggtoolbar.h" +#include "eggradiotoolbutton.h" +#include "eggseparatortoolitem.h" +#include <gtk/gtkmenu.h> +#include <gtk/gtkradiobutton.h> +#include <gtk/gtktoolbar.h> + +#define DEFAULT_IPADDING 0 +#define DEFAULT_SPACE_SIZE 5 +#define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE + +#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR +#define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH + +#define SPACE_LINE_DIVISION 10 +#define SPACE_LINE_START 3 +#define SPACE_LINE_END 7 + +#define TOOLBAR_ITEM_VISIBLE(item) \ +(GTK_WIDGET_VISIBLE (item) && \ +((toolbar->orientation == GTK_ORIENTATION_HORIZONTAL && item->visible_horizontal) || \ + (toolbar->orientation == GTK_ORIENTATION_VERTICAL && item->visible_vertical))) + +#ifndef _ +# define _(s) (s) +#endif + +enum { + PROP_0, + PROP_ORIENTATION, + PROP_TOOLBAR_STYLE, + PROP_SHOW_ARROW +}; + +enum { + ORIENTATION_CHANGED, + STYLE_CHANGED, + LAST_SIGNAL +}; + +static void egg_toolbar_init (EggToolbar *toolbar); +static void egg_toolbar_class_init (EggToolbarClass *klass); + +static void egg_toolbar_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void egg_toolbar_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +static gint egg_toolbar_expose (GtkWidget *widget, + GdkEventExpose *event); +static void egg_toolbar_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void egg_toolbar_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); +static void egg_toolbar_style_set (GtkWidget *widget, + GtkStyle *prev_style); +static gboolean egg_toolbar_focus (GtkWidget *widget, + GtkDirectionType dir); +static void egg_toolbar_screen_changed (GtkWidget *widget, + GdkScreen *previous_screen); + +static void egg_toolbar_add (GtkContainer *container, + GtkWidget *widget); +static void egg_toolbar_remove (GtkContainer *container, + GtkWidget *widget); +static void egg_toolbar_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data); +static GType egg_toolbar_child_type (GtkContainer *container); + + +static void egg_toolbar_real_orientation_changed (EggToolbar *toolbar, + GtkOrientation orientation); +static void egg_toolbar_real_style_changed (EggToolbar *toolbar, + GtkToolbarStyle style); + +static void egg_toolbar_button_press (GtkWidget *button, + GdkEventButton *event, + EggToolbar *toolbar); +static void egg_toolbar_update_button_relief (EggToolbar *toolbar); +static GtkReliefStyle get_button_relief (EggToolbar *toolbar); +static gint get_space_size (EggToolbar *toolbar); +static GtkToolbarSpaceStyle get_space_style (EggToolbar *toolbar); + +static GtkWidget *egg_toolbar_internal_insert_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position, + gboolean use_stock); + + +#define PRIVATE_KEY "egg-toolbar-private" + +#define EGG_TOOLBAR_GET_PRIVATE(toolbar) (g_object_get_data (G_OBJECT (toolbar), PRIVATE_KEY)) + +typedef struct +{ + GList *items; + GList *first_non_fitting_item; + + gint total_button_maxw; + gint total_button_maxh; + + GtkWidget *button; + GtkWidget *arrow; + + gboolean show_arrow; +} EggToolbarPrivate; + +static GtkContainerClass *parent_class = NULL; +static guint toolbar_signals [LAST_SIGNAL] = { 0 }; + +GType +egg_toolbar_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggToolbarClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_toolbar_class_init, + (GClassFinalizeFunc) NULL, + NULL, + sizeof (EggToolbar), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_toolbar_init, + }; + + type = g_type_register_static (GTK_TYPE_CONTAINER, + "EggToolbar", + &type_info, 0); + } + + return type; +} + +static void +egg_toolbar_class_init (EggToolbarClass *klass) +{ + GObjectClass *gobject_class; + GtkWidgetClass *widget_class; + GtkContainerClass *container_class; + + gobject_class = (GObjectClass *)klass; + widget_class = (GtkWidgetClass *)klass; + container_class = (GtkContainerClass *)klass; + + gobject_class->set_property = egg_toolbar_set_property; + gobject_class->get_property = egg_toolbar_get_property; + + widget_class->expose_event = egg_toolbar_expose; + widget_class->size_request = egg_toolbar_size_request; + widget_class->size_allocate = egg_toolbar_size_allocate; + widget_class->style_set = egg_toolbar_style_set; + widget_class->focus = egg_toolbar_focus; + widget_class->screen_changed = egg_toolbar_screen_changed; + + container_class->add = egg_toolbar_add; + container_class->remove = egg_toolbar_remove; + container_class->forall = egg_toolbar_forall; + container_class->child_type = egg_toolbar_child_type; + + klass->orientation_changed = egg_toolbar_real_orientation_changed; + klass->style_changed = egg_toolbar_real_style_changed; + + toolbar_signals[ORIENTATION_CHANGED] = + g_signal_new ("orientation_changed", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EggToolbarClass, orientation_changed), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_ORIENTATION); + toolbar_signals[STYLE_CHANGED] = + g_signal_new ("style_changed", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EggToolbarClass, style_changed), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_TOOLBAR_STYLE); + + g_object_class_install_property (gobject_class, + PROP_ORIENTATION, + g_param_spec_enum ("orientation", + _("Orientation"), + _("The orientation of the toolbar"), + GTK_TYPE_ORIENTATION, + GTK_ORIENTATION_HORIZONTAL, + G_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, + PROP_TOOLBAR_STYLE, + g_param_spec_enum ("toolbar_style", + _("Toolbar Style"), + _("How to draw the toolbar"), + GTK_TYPE_TOOLBAR_STYLE, + GTK_TOOLBAR_ICONS, + G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, + PROP_SHOW_ARROW, + g_param_spec_boolean ("show_arrow", + _("Show Arrow"), + _("If an arrow should be shown if the toolbar doesn't fit"), + FALSE, + G_PARAM_READWRITE)); + + gtk_widget_class_install_style_property (widget_class, + g_param_spec_int ("space_size", + _("Spacer size"), + _("Size of spacers"), + 0, + G_MAXINT, + DEFAULT_SPACE_SIZE, + G_PARAM_READABLE)); + + gtk_widget_class_install_style_property (widget_class, + g_param_spec_int ("internal_padding", + _("Internal padding"), + _("Amount of border space between the toolbar shadow and the buttons"), + 0, + G_MAXINT, + DEFAULT_IPADDING, + G_PARAM_READABLE)); + + gtk_widget_class_install_style_property (widget_class, + g_param_spec_enum ("space_style", + _("Space style"), + _("Whether spacers are vertical lines or just blank"), + GTK_TYPE_TOOLBAR_SPACE_STYLE, + DEFAULT_SPACE_STYLE, + G_PARAM_READABLE)); + + gtk_widget_class_install_style_property (widget_class, + g_param_spec_enum ("button_relief", + _("Button relief"), + _("Type of bevel around toolbar buttons"), + GTK_TYPE_RELIEF_STYLE, + GTK_RELIEF_NONE, + G_PARAM_READABLE)); + gtk_widget_class_install_style_property (widget_class, + g_param_spec_enum ("shadow_type", + _("Shadow type"), + _("Style of bevel around the toolbar"), + GTK_TYPE_SHADOW_TYPE, + GTK_SHADOW_OUT, + G_PARAM_READABLE)); + + gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style", + _("Toolbar style"), + _("Whether default toolbars have text only, text and icons, icons only, etc."), + GTK_TYPE_TOOLBAR_STYLE, + DEFAULT_TOOLBAR_STYLE, + G_PARAM_READWRITE)); + + gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size", + _("Toolbar icon size"), + _("Size of icons in default toolbars"), + GTK_TYPE_ICON_SIZE, + DEFAULT_ICON_SIZE, + G_PARAM_READWRITE)); +} + +static void +egg_toolbar_init (EggToolbar *toolbar) +{ + EggToolbarPrivate *priv; + + GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW); + GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS); + + priv = g_new0 (EggToolbarPrivate, 1); + g_object_set_data (G_OBJECT (toolbar), PRIVATE_KEY, priv); + + toolbar->orientation = GTK_ORIENTATION_HORIZONTAL; + toolbar->style = GTK_TOOLBAR_ICONS; + toolbar->tooltips = gtk_tooltips_new (); + g_object_ref (toolbar->tooltips); + gtk_object_sink (GTK_OBJECT (toolbar->tooltips)); + + priv->button = gtk_toggle_button_new (); + g_signal_connect (priv->button, "button_press_event", + G_CALLBACK (egg_toolbar_button_press), toolbar); + gtk_button_set_relief (GTK_BUTTON (priv->button), + get_button_relief (toolbar)); + GTK_WIDGET_UNSET_FLAGS (priv->button, GTK_CAN_FOCUS); + + priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE); + gtk_widget_show (priv->arrow); + gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow); + + gtk_widget_set_parent (priv->button, GTK_WIDGET (toolbar)); +} + +static void +egg_toolbar_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EggToolbar *toolbar = EGG_TOOLBAR (object); + + switch (prop_id) + { + case PROP_ORIENTATION: + egg_toolbar_set_orientation (toolbar, g_value_get_enum (value)); + break; + case PROP_TOOLBAR_STYLE: + egg_toolbar_set_style (toolbar, g_value_get_enum (value)); + break; + case PROP_SHOW_ARROW: + egg_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +egg_toolbar_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EggToolbar *toolbar = EGG_TOOLBAR (object); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + switch (prop_id) + { + case PROP_ORIENTATION: + g_value_set_enum (value, toolbar->orientation); + break; + case PROP_TOOLBAR_STYLE: + g_value_set_enum (value, toolbar->style); + break; + case PROP_SHOW_ARROW: + g_value_set_boolean (value, priv->show_arrow); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +egg_toolbar_paint_space_line (GtkWidget *widget, + GdkRectangle *area, + EggToolItem *item) +{ + EggToolbar *toolbar; + GtkAllocation *allocation; + gint space_size; + + g_return_if_fail (GTK_BIN (item)->child == NULL); + + toolbar = EGG_TOOLBAR (widget); + + allocation = >K_WIDGET (item)->allocation; + space_size = get_space_size (toolbar); + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + gtk_paint_vline (widget->style, widget->window, + GTK_WIDGET_STATE (widget), area, widget, + "toolbar", + allocation->y + allocation->height * + SPACE_LINE_START / SPACE_LINE_DIVISION, + allocation->y + allocation->height * + SPACE_LINE_END / SPACE_LINE_DIVISION, + allocation->x + (space_size-widget->style->xthickness)/2); + else if (toolbar->orientation == GTK_ORIENTATION_VERTICAL) + gtk_paint_hline (widget->style, widget->window, + GTK_WIDGET_STATE (widget), area, widget, + "toolbar", + allocation->x + allocation->width * + SPACE_LINE_START / SPACE_LINE_DIVISION, + allocation->x + allocation->width * + SPACE_LINE_END / SPACE_LINE_DIVISION, + allocation->y + (space_size-widget->style->ythickness)/2); +} + +static gint +egg_toolbar_expose (GtkWidget *widget, + GdkEventExpose *event) +{ + EggToolbar *toolbar = EGG_TOOLBAR (widget); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + GList *items; + gint border_width; + + border_width = GTK_CONTAINER (widget)->border_width; + + if (GTK_WIDGET_DRAWABLE (widget)) + { + GtkShadowType shadow_type; + + gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL); + + gtk_paint_box (widget->style, + widget->window, + GTK_WIDGET_STATE (widget), + shadow_type, + &event->area, widget, "toolbar", + widget->allocation.x + border_width, + widget->allocation.y + border_width, + widget->allocation.width - border_width, + widget->allocation.height - border_width); + + } + + items = priv->items; + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (GTK_BIN (item)->child) + gtk_container_propagate_expose (GTK_CONTAINER (widget), + GTK_WIDGET (item), + event); + else if (GTK_WIDGET_MAPPED (item) && get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE) + egg_toolbar_paint_space_line (widget, &event->area, item); + + items = items->next; + } + + gtk_container_propagate_expose (GTK_CONTAINER (widget), + priv->button, + event); + + return FALSE; +} + +static void +egg_toolbar_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + EggToolbar *toolbar = EGG_TOOLBAR (widget); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GList *items; + gint nbuttons, ipadding; + gint button_maxw, button_maxh; + gint total_button_maxw, total_button_maxh; + gint space_size; + GtkRequisition child_requisition; + + requisition->width = GTK_CONTAINER (toolbar)->border_width * 2; + requisition->height = GTK_CONTAINER (toolbar)->border_width * 2; + nbuttons = 0; + button_maxw = 0; + button_maxh = 0; + total_button_maxw = 0; + total_button_maxh = 0; + items = priv->items; + space_size = get_space_size (toolbar); + + if (priv->show_arrow) + { + /* When we enable the arrow we only want to be the + * size of the arrows plus the size of any items that + * are pack-end. + */ + + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (TOOLBAR_ITEM_VISIBLE (item)) + { + gtk_widget_size_request (GTK_WIDGET (item), &child_requisition); + + total_button_maxw = MAX (total_button_maxw, child_requisition.width); + total_button_maxh = MAX (total_button_maxh, child_requisition.height); + + if (item->homogeneous) + { + if (item->pack_end) + nbuttons++; + button_maxw = MAX (button_maxw, child_requisition.width); + button_maxh = MAX (button_maxh, child_requisition.height); + } + else if (item->pack_end) + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + requisition->width += child_requisition.width; + else + requisition->height += child_requisition.height; + } + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + requisition->height = MAX (requisition->height, child_requisition.height); + else + requisition->width = MAX (requisition->width, child_requisition.width); + } + + items = items->next; + } + + /* Add the arrow */ + gtk_widget_size_request (priv->button, &child_requisition); + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + requisition->width += child_requisition.width; + requisition->height = MAX (requisition->height, child_requisition.height); + } + else + { + requisition->height += child_requisition.height; + requisition->width = MAX (requisition->width, child_requisition.width); + } + } + else + { + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (!TOOLBAR_ITEM_VISIBLE (item)) + { + items = items->next; + continue; + } + + if (!GTK_BIN (item)->child) + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + requisition->width += space_size; + else + requisition->height += space_size; + } + else + { + gtk_widget_size_request (GTK_WIDGET (item), &child_requisition); + + total_button_maxw = MAX (total_button_maxw, child_requisition.width); + total_button_maxh = MAX (total_button_maxh, child_requisition.height); + + if (item->homogeneous) + { + nbuttons++; + button_maxw = MAX (button_maxw, child_requisition.width); + button_maxh = MAX (button_maxh, child_requisition.height); + } + else + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + requisition->width += child_requisition.width; + requisition->height = MAX (requisition->height, child_requisition.height); + } + else + { + requisition->height += child_requisition.height; + requisition->width = MAX (requisition->width, child_requisition.width); + } + } + } + + items = items->next; + } + } + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + requisition->width += nbuttons * button_maxw; + requisition->height = MAX (requisition->height, button_maxh); + } + else + { + requisition->width = MAX (requisition->width, button_maxw); + requisition->height += nbuttons * button_maxh; + } + + /* Extra spacing */ + gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL); + + requisition->width += 2 * ipadding; + requisition->height += 2 * ipadding; + + priv->total_button_maxw = total_button_maxw; + priv->total_button_maxh = total_button_maxh; + + toolbar->button_maxw = button_maxw; + toolbar->button_maxh = button_maxh; +} + +static void +egg_toolbar_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + EggToolbar *toolbar = EGG_TOOLBAR (widget); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GList *items; + GtkAllocation child_allocation; + gint ipadding, space_size; + gint border_width, edge_position; + gint available_width, available_height; + gint available_size, total_size; + GtkRequisition child_requisition; + gint remaining_size; + gint number_expandable, expandable_size; + gboolean first_expandable; + + widget->allocation = *allocation; + border_width = GTK_CONTAINER (widget)->border_width; + total_size = 0; + number_expandable = 0; + space_size = get_space_size (toolbar); + + gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL); + border_width += ipadding; + + available_width = allocation->width - 2 * border_width; + available_height = allocation->height - 2 * border_width; + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + edge_position = allocation->x + allocation->width - border_width; + available_size = available_width; + } + else + { + edge_position = allocation->y + allocation->height - border_width; + available_size = available_height; + } + + items = g_list_last (priv->items); + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (!item->pack_end || !TOOLBAR_ITEM_VISIBLE (item)) + { + items = items->prev; + continue; + } + + if (!GTK_BIN (item)->child) + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + child_allocation.width = space_size; + child_allocation.height = available_height; + child_allocation.x = edge_position - child_allocation.width; + child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + + edge_position -= child_allocation.width; + available_size -= child_allocation.width; + } + else + { + child_allocation.width = available_width; + child_allocation.height = space_size; + child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2; + child_allocation.y = edge_position - child_allocation.height; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + + edge_position -= child_allocation.height; + available_size -= child_allocation.height; + } + } + else + { + gtk_widget_get_child_requisition (GTK_WIDGET (item), &child_requisition); + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + if (item->homogeneous) + child_allocation.width = toolbar->button_maxw; + else + child_allocation.width = child_requisition.width; + child_allocation.height = available_height; + child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2; + child_allocation.x = edge_position - child_allocation.width; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + + edge_position -= child_allocation.width; + available_size -= child_allocation.width; + } + else + { + if (item->homogeneous) + child_allocation.height = toolbar->button_maxh; + else + child_allocation.height = child_requisition.height; + + child_allocation.width = available_width; + child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2; + child_allocation.y = edge_position - child_allocation.height; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + + edge_position -= child_allocation.height; + available_size -= child_allocation.height; + } + } + + items = items->prev; + } + + /* Now go through the items and see if they fit */ + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (item->pack_end || !TOOLBAR_ITEM_VISIBLE (item)) + { + items = items->next; + continue; + } + + if (item->expandable) + number_expandable += 1; + + if (!GTK_BIN (item)->child) + { + total_size += space_size; + } + else + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + gtk_widget_get_child_requisition (GTK_WIDGET (item), &child_requisition); + + if (item->homogeneous) + total_size += toolbar->button_maxw; + else + total_size += child_requisition.width; + } + else + { + gtk_widget_get_child_requisition (GTK_WIDGET (item), &child_requisition); + + if (item->homogeneous) + total_size += toolbar->button_maxh; + else + total_size += child_requisition.height; + } + } + items = items->next; + } + + /* Check if we need to allocate and show the arrow */ + if (available_size < total_size) + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + gtk_widget_get_child_requisition (priv->button, &child_requisition); + available_size -= child_requisition.width; + + child_allocation.width = child_requisition.width; + child_allocation.height = priv->total_button_maxh; + child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2; + child_allocation.x = edge_position - child_allocation.width; + } + else + { + gtk_widget_get_child_requisition (priv->button, &child_requisition); + available_size -= child_requisition.width; + + child_allocation.height = child_requisition.height; + child_allocation.width = priv->total_button_maxw; + child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2; + child_allocation.y = edge_position - child_allocation.height; + } + + gtk_widget_size_allocate (priv->button, &child_allocation); + gtk_widget_show (priv->button); + } + else + gtk_widget_hide (priv->button); + + /* Finally allocate the remaining items */ + items = priv->items; + child_allocation.x = allocation->x + border_width; + child_allocation.y = allocation->y + border_width; + remaining_size = MAX (0, available_size - total_size); + total_size = 0; + first_expandable = TRUE; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (item->pack_end || !TOOLBAR_ITEM_VISIBLE (item)) + { + items = items->next; + continue; + } + + if (!GTK_BIN (item)->child) + { + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + child_allocation.width = space_size; + child_allocation.height = available_height; + child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2; + total_size += child_allocation.width; + + if (total_size > available_size) + break; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + gtk_widget_map (GTK_WIDGET (item)); + + child_allocation.x += child_allocation.width; + } + else + { + child_allocation.width = available_width; + child_allocation.height = space_size; + child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2; + total_size += child_allocation.height; + + if (total_size > available_size) + break; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + gtk_widget_map (GTK_WIDGET (item)); + + child_allocation.y += child_allocation.height; + } + } + else + { + gtk_widget_get_child_requisition (GTK_WIDGET (item), &child_requisition); + + if (item->expandable) + { + expandable_size = remaining_size / number_expandable; + + if (first_expandable) + { + expandable_size += remaining_size % number_expandable; + first_expandable = FALSE; + } + } + else + expandable_size = 0; + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + if (item->homogeneous) + child_allocation.width = toolbar->button_maxw; + else + child_allocation.width = child_requisition.width; + + child_allocation.height = available_height; + child_allocation.width += expandable_size; + child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2; + total_size += child_allocation.width; + } + else + { + if (item->homogeneous) + child_allocation.height = toolbar->button_maxh; + else + child_allocation.height = child_requisition.height; + + child_allocation.width = available_width; + child_allocation.height += expandable_size; + child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2; + total_size += child_allocation.height; + } + + if (total_size > available_size) + break; + + gtk_widget_size_allocate (GTK_WIDGET (item), &child_allocation); + gtk_widget_map (GTK_WIDGET (item)); + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + child_allocation.x += child_allocation.width; + else + child_allocation.y += child_allocation.height; + + } + + items = items->next; + } + + /* Unmap the remaining items */ + priv->first_non_fitting_item = items; + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + gtk_widget_unmap (GTK_WIDGET (item)); + items = items->next; + } +} + +static void +egg_toolbar_style_set (GtkWidget *widget, + GtkStyle *prev_style) +{ + if (prev_style) + egg_toolbar_update_button_relief (EGG_TOOLBAR (widget)); +} + +static gboolean +egg_toolbar_focus (GtkWidget *widget, + GtkDirectionType dir) +{ + /* Focus can't go in toolbars */ + + return FALSE; +} + +static void +style_change_notify (EggToolbar *toolbar) +{ + if (!toolbar->style_set) + { + /* pretend it was set, then unset, thus reverting to new default */ + toolbar->style_set = TRUE; + egg_toolbar_unset_style (toolbar); + } +} + +static void +icon_size_change_notify (EggToolbar *toolbar) +{ + if (!toolbar->icon_size_set) + { + /* pretend it was set, then unset, thus reverting to new default */ + toolbar->icon_size_set = TRUE; + egg_toolbar_unset_icon_size (toolbar); + } +} + +static GtkSettings * +toolbar_get_settings (EggToolbar *toolbar) +{ + return g_object_get_data (G_OBJECT (toolbar), "egg-toolbar-settings"); +} + +static void +egg_toolbar_screen_changed (GtkWidget *widget, + GdkScreen *previous_screen) +{ + EggToolbar *toolbar = EGG_TOOLBAR (widget); + GtkSettings *old_settings = toolbar_get_settings (toolbar); + GtkSettings *settings; + + if (gtk_widget_has_screen (GTK_WIDGET (toolbar))) + settings = gtk_widget_get_settings (GTK_WIDGET (toolbar)); + else + settings = NULL; + + if (settings == old_settings) + return; + + if (old_settings) + { + g_signal_handler_disconnect (old_settings, toolbar->style_set_connection); + g_signal_handler_disconnect (old_settings, toolbar->icon_size_connection); + + g_object_unref (old_settings); + } + + if (settings) + { + toolbar->style_set_connection = + g_signal_connect_swapped (settings, + "notify::gtk-toolbar-style", + G_CALLBACK (style_change_notify), + toolbar); + toolbar->icon_size_connection = + g_signal_connect_swapped (settings, + "notify::gtk-toolbar-icon-size", + G_CALLBACK (icon_size_change_notify), + toolbar); + + g_object_ref (settings); + g_object_set_data (G_OBJECT (toolbar), "egg-toolbar-settings", settings); + } + else + g_object_set_data (G_OBJECT (toolbar), "egg-toolbar-settings", NULL); + + style_change_notify (toolbar); + icon_size_change_notify (toolbar); +} + + +static void +egg_toolbar_add (GtkContainer *container, + GtkWidget *widget) +{ + g_return_if_fail (EGG_IS_TOOLBAR (container)); + g_return_if_fail (EGG_IS_TOOL_ITEM (widget)); + + egg_toolbar_append_tool_item (EGG_TOOLBAR (container), + EGG_TOOL_ITEM (widget)); +} + +static void +egg_toolbar_remove (GtkContainer *container, + GtkWidget *widget) +{ + g_return_if_fail (EGG_IS_TOOLBAR (container)); + g_return_if_fail (EGG_IS_TOOL_ITEM (widget)); + + egg_toolbar_remove_tool_item (EGG_TOOLBAR (container), + EGG_TOOL_ITEM (widget)); +} + +static void +egg_toolbar_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data) +{ + EggToolbar *toolbar = EGG_TOOLBAR (container); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GList *items; + + g_return_if_fail (callback != NULL); + + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + items = items->next; + (*callback) (GTK_WIDGET (item), callback_data); + } + + if (include_internals) + (* callback) (priv->button, callback_data); +} + +static GType +egg_toolbar_child_type (GtkContainer *container) +{ + return EGG_TYPE_TOOL_ITEM; +} + +static void +egg_toolbar_real_orientation_changed (EggToolbar *toolbar, + GtkOrientation orientation) +{ + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + GList *items; + + if (toolbar->orientation != orientation) + { + toolbar->orientation = orientation; + + items = priv->items; + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + egg_tool_item_set_orientation (item, orientation); + + items = items->next; + } + + if (orientation == GTK_ORIENTATION_HORIZONTAL) + gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE); + else + gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE); + + gtk_widget_queue_resize (GTK_WIDGET (toolbar)); + g_object_notify (G_OBJECT (toolbar), "orientation"); + } +} + +static void +egg_toolbar_real_style_changed (EggToolbar *toolbar, + GtkToolbarStyle style) +{ + EggToolbarPrivate *priv; + GList *items; + + priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + if (toolbar->style != style) + { + toolbar->style = style; + + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + egg_tool_item_set_toolbar_style (item, style); + + items = items->next; + } + + gtk_widget_queue_resize (GTK_WIDGET (toolbar)); + g_object_notify (G_OBJECT (toolbar), "toolbar_style"); + } +} + +static void +menu_position_func (GtkMenu *menu, + gint *x, + gint *y, + gboolean *push_in, + gpointer user_data) +{ + EggToolbar *toolbar = EGG_TOOLBAR (user_data); + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GtkRequisition req; + + gdk_window_get_origin (GTK_BUTTON (priv->button)->event_window, x, y); + gtk_widget_size_request (priv->button, &req); + + if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) + { + *y += priv->button->allocation.height; + *x += priv->button->allocation.width - req.width; + } + else + { + *x += priv->button->allocation.width; + *y += priv->button->allocation.height - req.height; + } + + *push_in = TRUE; +} + +static void +menu_deactivated (GtkWidget *menu, GtkWidget *button) +{ + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE); +} + +static void +egg_toolbar_button_press (GtkWidget *button, + GdkEventButton *event, + EggToolbar *toolbar) +{ + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GtkWidget *menu; + GtkWidget *menu_item; + GList *items; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); + + menu = gtk_menu_new (); + g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivated), button); + + items = priv->first_non_fitting_item; + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + if (TOOLBAR_ITEM_VISIBLE (item) && !item->pack_end) + { + menu_item = NULL; + g_signal_emit_by_name (item, "create_menu_proxy", &menu_item); + + if (menu_item) + { + gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item); + } + } + items = items->next; + } + + gtk_widget_show_all (menu); + + gtk_menu_popup (GTK_MENU (menu), NULL, NULL, + menu_position_func, toolbar, + event->button, event->time); +} + +static void +egg_toolbar_update_button_relief (EggToolbar *toolbar) +{ + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + GtkReliefStyle relief; + GList *items; + + relief = get_button_relief (toolbar); + + items = priv->items; + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + egg_tool_item_set_relief_style (item, relief); + + items = items->next; + } + + gtk_button_set_relief (GTK_BUTTON (priv->button), relief); +} + +static GtkReliefStyle +get_button_relief (EggToolbar *toolbar) +{ + GtkReliefStyle button_relief = GTK_RELIEF_NORMAL; + + gtk_widget_ensure_style (GTK_WIDGET (toolbar)); + + gtk_widget_style_get (GTK_WIDGET (toolbar), + "button_relief", &button_relief, + NULL); + + return button_relief; +} + +static gint +get_space_size (EggToolbar *toolbar) +{ + gint space_size = DEFAULT_SPACE_SIZE; + + gtk_widget_style_get (GTK_WIDGET (toolbar), + "space_size", &space_size, + NULL); + + return space_size; +} + +static GtkToolbarSpaceStyle +get_space_style (EggToolbar *toolbar) +{ + GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE; + + gtk_widget_style_get (GTK_WIDGET (toolbar), + "space_style", &space_style, + NULL); + + + return space_style; +} + +GtkWidget * +egg_toolbar_new (void) +{ + EggToolbar *toolbar; + + toolbar = g_object_new (EGG_TYPE_TOOLBAR, NULL); + + return GTK_WIDGET (toolbar); +} + +void +egg_toolbar_append_tool_item (EggToolbar *toolbar, + EggToolItem *item) +{ + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + g_return_if_fail (EGG_IS_TOOL_ITEM (item)); + + egg_toolbar_insert_tool_item (toolbar, item, toolbar->num_children); +} + +void +egg_toolbar_prepend_tool_item (EggToolbar *toolbar, + EggToolItem *item) +{ + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + g_return_if_fail (EGG_IS_TOOL_ITEM (item)); + + egg_toolbar_insert_tool_item (toolbar, item, 0); +} + +void +egg_toolbar_remove_tool_item (EggToolbar *toolbar, + EggToolItem *item) +{ + EggToolbarPrivate *priv; + GList *tmp; + + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + g_return_if_fail (EGG_IS_TOOL_ITEM (item)); + + priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + for (tmp = priv->items; tmp != NULL; tmp = tmp->next) + { + GtkWidget *child = tmp->data; + + if (child == GTK_WIDGET (item)) + { + gboolean was_visible; + + was_visible = GTK_WIDGET_VISIBLE (item); + gtk_widget_unparent (GTK_WIDGET (item)); + + priv->items = g_list_remove_link (priv->items, tmp); + toolbar->num_children--; + + if (was_visible && GTK_WIDGET_VISIBLE (toolbar)) + gtk_widget_queue_resize (GTK_WIDGET (toolbar)); + + break; + } + } +} + +void +egg_toolbar_insert_tool_item (EggToolbar *toolbar, + EggToolItem *item, + gint pos) +{ + EggToolbarPrivate *priv; + + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + g_return_if_fail (EGG_IS_TOOL_ITEM (item)); + + priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + priv->items = g_list_insert (priv->items, item, pos); + toolbar->num_children++; + + egg_tool_item_set_orientation (item, toolbar->orientation); + egg_tool_item_set_toolbar_style (item, toolbar->style); + egg_tool_item_set_relief_style (item, get_button_relief (toolbar)); + + gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar)); + GTK_WIDGET_UNSET_FLAGS (item, GTK_CAN_FOCUS); +} + +void +egg_toolbar_set_orientation (EggToolbar *toolbar, + GtkOrientation orientation) +{ + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0, orientation); +} + +GtkOrientation +egg_toolbar_get_orientation (EggToolbar *toolbar) +{ + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL); + + return toolbar->orientation; +} + +void +egg_toolbar_set_style (EggToolbar *toolbar, + GtkToolbarStyle style) +{ + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + toolbar->style_set = TRUE; + g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style); +} + +GtkToolbarStyle +egg_toolbar_get_style (EggToolbar *toolbar) +{ + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE); + + return toolbar->style; +} + +void +egg_toolbar_unset_style (EggToolbar *toolbar) +{ + GtkToolbarStyle style; + + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + if (toolbar->style_set) + { + GtkSettings *settings = toolbar_get_settings (toolbar); + + if (settings) + g_object_get (settings, + "gtk-toolbar-style", &style, + NULL); + else + style = DEFAULT_TOOLBAR_STYLE; + + if (style != toolbar->style) + g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style); + + toolbar->style_set = FALSE; + } +} + +void +egg_toolbar_set_tooltips (EggToolbar *toolbar, + gboolean enable) +{ + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + if (enable) + gtk_tooltips_enable (toolbar->tooltips); + else + gtk_tooltips_disable (toolbar->tooltips); +} + +gboolean +egg_toolbar_get_tooltips (EggToolbar *toolbar) +{ + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), FALSE); + + return toolbar->tooltips->enabled; +} + +GList* +egg_toolbar_get_tool_items (EggToolbar *toolbar) +{ + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), FALSE); + + return priv->items; +} + +void +egg_toolbar_set_icon_size (EggToolbar *toolbar, + GtkIconSize icon_size) +{ + GList *items; + EggToolbarPrivate *priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + toolbar->icon_size_set = TRUE; + + if (toolbar->icon_size == icon_size) + return; + + toolbar->icon_size = icon_size; + + items = priv->items; + + while (items) + { + EggToolItem *item = EGG_TOOL_ITEM (items->data); + + egg_tool_item_set_icon_size (item, icon_size); + + items = items->next; + } + + gtk_widget_queue_resize (GTK_WIDGET (toolbar)); +} + +GtkIconSize +egg_toolbar_get_icon_size (EggToolbar *toolbar) +{ + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE); + + return toolbar->icon_size; +} + +void +egg_toolbar_unset_icon_size (EggToolbar *toolbar) +{ + GtkIconSize size; + + if (toolbar->icon_size_set) + { + GtkSettings *settings = toolbar_get_settings (toolbar); + + if (settings) + g_object_get (settings, + "gtk-toolbar-icon-size", &size, + NULL); + else + size = DEFAULT_ICON_SIZE; + + if (size != toolbar->icon_size) + egg_toolbar_set_icon_size (toolbar, size); + + toolbar->icon_size_set = FALSE; + } +} + + +void +egg_toolbar_set_show_arrow (EggToolbar *toolbar, + gboolean show_arrow) +{ + EggToolbarPrivate *priv; + + g_return_if_fail (EGG_IS_TOOLBAR (toolbar)); + + priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + show_arrow = show_arrow != FALSE; + + if (priv->show_arrow != show_arrow) + { + priv->show_arrow = show_arrow; + + if (!priv->show_arrow) + gtk_widget_hide (priv->button); + + gtk_widget_queue_resize (GTK_WIDGET (toolbar)); + g_object_notify (G_OBJECT (toolbar), "show_arrow"); + } +} + +gboolean +egg_toolbar_get_show_arrow (EggToolbar *toolbar) +{ + EggToolbarPrivate *priv; + + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), FALSE); + + priv = EGG_TOOLBAR_GET_PRIVATE (toolbar); + + return priv->show_arrow; +} + +GtkWidget * +egg_toolbar_append_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data) +{ + return egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_BUTTON, + NULL, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, + toolbar->num_children); +} + +GtkWidget * +egg_toolbar_prepend_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data) +{ + return egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_BUTTON, + NULL, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, + 0); +} + +GtkWidget * +egg_toolbar_insert_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position) +{ + return egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_BUTTON, + NULL, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, + position); +} + +GtkWidget* +egg_toolbar_insert_stock (EggToolbar *toolbar, + const gchar *stock_id, + const char *tooltip_text, + const char *tooltip_private_text, + GtkSignalFunc callback, + gpointer user_data, + gint position) +{ + return egg_toolbar_internal_insert_element (toolbar, EGG_TOOLBAR_CHILD_BUTTON, + NULL, stock_id, + tooltip_text, tooltip_private_text, + NULL, callback, user_data, + position, TRUE); +} + +void +egg_toolbar_append_space (EggToolbar *toolbar) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_SPACE, + NULL, NULL, + NULL, NULL, + NULL, NULL, NULL, + toolbar->num_children); +} + +void +egg_toolbar_prepend_space (EggToolbar *toolbar) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_SPACE, + NULL, NULL, + NULL, NULL, + NULL, NULL, NULL, + 0); +} + +void +egg_toolbar_insert_space (EggToolbar *toolbar, + gint position) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_SPACE, + NULL, NULL, + NULL, NULL, + NULL, NULL, NULL, + position); +} + +void +egg_toolbar_append_widget (EggToolbar *toolbar, + GtkWidget *widget, + const gchar *tooltip_text, + const gchar *tooltip_private_text) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_WIDGET, + widget, NULL, + tooltip_text, tooltip_private_text, + NULL, NULL, NULL, + toolbar->num_children); +} + +void +egg_toolbar_prepend_widget (EggToolbar *toolbar, + GtkWidget *widget, + const gchar *tooltip_text, + const gchar *tooltip_private_text) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_WIDGET, + widget, NULL, + tooltip_text, tooltip_private_text, + NULL, NULL, NULL, + 0); +} + +void +egg_toolbar_insert_widget (EggToolbar *toolbar, + GtkWidget *widget, + const char *tooltip_text, + const char *tooltip_private_text, + gint position) +{ + egg_toolbar_insert_element (toolbar, EGG_TOOLBAR_CHILD_WIDGET, + widget, NULL, + tooltip_text, tooltip_private_text, + NULL, NULL, NULL, + position); +} + +GtkWidget* +egg_toolbar_append_element (EggToolbar *toolbar, + GtkToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data) +{ + return egg_toolbar_insert_element (toolbar, type, widget, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, + toolbar->num_children); +} + +GtkWidget * +egg_toolbar_prepend_element (EggToolbar *toolbar, + GtkToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data) +{ + return egg_toolbar_insert_element (toolbar, type, widget, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, 0); +} + +GtkWidget * +egg_toolbar_insert_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position) +{ + return egg_toolbar_internal_insert_element (toolbar, type, widget, text, + tooltip_text, tooltip_private_text, + icon, callback, user_data, position, FALSE); +} + +static GtkWidget * +egg_toolbar_internal_insert_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position, + gboolean use_stock) +{ + EggToolbarChild *child; + EggToolItem *item = NULL; + + g_return_val_if_fail (EGG_IS_TOOLBAR (toolbar), NULL); + + if (type == EGG_TOOLBAR_CHILD_WIDGET) + g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL); + else if (type != EGG_TOOLBAR_CHILD_RADIOBUTTON) + g_return_val_if_fail (widget == NULL, NULL); + + child = g_new (EggToolbarChild, 1); + + child->type = type; + child->icon = NULL; + child->label = NULL; + + switch (type) + { + case EGG_TOOLBAR_CHILD_SPACE: + item = egg_separator_tool_item_new (); + child->widget = NULL; + break; + + case EGG_TOOLBAR_CHILD_WIDGET: + item = egg_tool_item_new (); + child->widget = widget; + gtk_container_add (GTK_CONTAINER (item), child->widget); + + break; + + case EGG_TOOLBAR_CHILD_BUTTON: + item = egg_tool_button_new (); + child->widget = EGG_TOOL_BUTTON (item)->button; + break; + + case EGG_TOOLBAR_CHILD_TOGGLEBUTTON: + item = egg_toggle_tool_button_new (); + child->widget = EGG_TOOL_BUTTON (item)->button; + break; + + case EGG_TOOLBAR_CHILD_RADIOBUTTON: + item = egg_radio_tool_button_new (widget + ? gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget)) + : NULL); + child->widget = EGG_TOOL_BUTTON (item)->button; + break; + } + + /* + * We need to connect to the button's clicked callback because some + * programs may rely on that the widget in the callback is a GtkButton + */ + if (callback) + g_signal_connect (child->widget, "clicked", + callback, user_data); + + if (type == EGG_TOOLBAR_CHILD_BUTTON || + type == EGG_TOOLBAR_CHILD_RADIOBUTTON || + type == EGG_TOOLBAR_CHILD_TOGGLEBUTTON) + { + if (text) + { + child->label = EGG_TOOL_BUTTON (item)->label; + + if (use_stock) + g_object_set (G_OBJECT (item), "stock_id", text, NULL); + else + egg_tool_button_set_label (EGG_TOOL_BUTTON (item), text); + } + + if (icon) + { + child->icon = icon; + egg_tool_button_set_icon_widget (EGG_TOOL_BUTTON (item), icon); + } + + } + + if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text) + egg_tool_item_set_tooltip (item, toolbar->tooltips, + tooltip_text, tooltip_private_text); + + toolbar->children = g_list_insert (toolbar->children, child, position); + egg_toolbar_insert_tool_item (toolbar, item, position); + + return child->widget; +} diff --git a/lib/egg/eggtoolbar.h b/lib/egg/eggtoolbar.h new file mode 100644 index 000000000..5420b9dfa --- /dev/null +++ b/lib/egg/eggtoolbar.h @@ -0,0 +1,242 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * GtkToolbar copyright (C) Federico Mena + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GTK+ Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + */ + +#ifndef __EGG_TOOLBAR_H__ +#define __EGG_TOOLBAR_H__ + +#include <gdk/gdk.h> +#include <gtk/gtkcontainer.h> +#include <gtk/gtkenums.h> +#include <gtk/gtktooltips.h> + +#include "eggtoolitem.h" + +/* Not needed, retained for compatibility -Yosh */ +#include <gtk/gtkpixmap.h> +#include <gtk/gtksignal.h> + +G_BEGIN_DECLS + +#define EGG_TYPE_TOOLBAR (egg_toolbar_get_type ()) +#define EGG_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_TOOLBAR, EggToolbar)) +#define EGG_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_TOOLBAR, EggToolbarClass)) +#define EGG_IS_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_TOOLBAR)) +#define EGG_IS_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TYPE_TOOLBAR)) +#define EGG_TOOLBAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TYPE_TOOLBAR, EggToolbarClass)) + +#ifndef EGG_DISABLE_DEPRECATED +typedef enum +{ + EGG_TOOLBAR_CHILD_SPACE, + EGG_TOOLBAR_CHILD_BUTTON, + EGG_TOOLBAR_CHILD_TOGGLEBUTTON, + EGG_TOOLBAR_CHILD_RADIOBUTTON, + EGG_TOOLBAR_CHILD_WIDGET +} EggToolbarChildType; + +typedef struct _EggToolbarChild EggToolbarChild; + +struct _EggToolbarChild +{ + EggToolbarChildType type; + GtkWidget *widget; + GtkWidget *icon; + GtkWidget *label; +}; + +#endif /* EGG_DISABLE_DEPRECATED */ +typedef struct _EggToolbar EggToolbar; +typedef struct _EggToolbarClass EggToolbarClass; + +struct _EggToolbar +{ + GtkContainer container; + + gint num_children; + GList *children; + GtkOrientation orientation; + GtkToolbarStyle style; + GtkIconSize icon_size; + + GtkTooltips *tooltips; + + gint button_maxw; + gint button_maxh; + + guint style_set_connection; + guint icon_size_connection; + + guint style_set : 1; + guint icon_size_set : 1; +}; + +struct _EggToolbarClass +{ + GtkContainerClass parent_class; + + void (* orientation_changed) (EggToolbar *toolbar, + GtkOrientation orientation); + void (* style_changed) (EggToolbar *toolbar, + GtkToolbarStyle style); + + /* Padding for future expansion */ + void (*_gtk_reserved1) (void); + void (*_gtk_reserved2) (void); + void (*_gtk_reserved3) (void); + void (*_gtk_reserved4) (void); +}; + +GType egg_toolbar_get_type (void) G_GNUC_CONST; +GtkWidget* egg_toolbar_new (void); + +void egg_toolbar_append_tool_item (EggToolbar *toolbar, + EggToolItem *item); +void egg_toolbar_prepend_tool_item (EggToolbar *toolbar, + EggToolItem *item); +void egg_toolbar_insert_tool_item (EggToolbar *toolbar, + EggToolItem *item, + gint pos); +void egg_toolbar_remove_tool_item (EggToolbar *toolbar, + EggToolItem *item); + +/* Style functions */ +void egg_toolbar_set_show_arrow (EggToolbar *toolbar, + gboolean show_arrow); +void egg_toolbar_set_orientation (EggToolbar *toolbar, + GtkOrientation orientation); +void egg_toolbar_set_style (EggToolbar *toolbar, + GtkToolbarStyle style); +void egg_toolbar_set_icon_size (EggToolbar *toolbar, + GtkIconSize icon_size); +void egg_toolbar_set_tooltips (EggToolbar *toolbar, + gboolean enable); +void egg_toolbar_unset_style (EggToolbar *toolbar); +void egg_toolbar_unset_icon_size (EggToolbar *toolbar); +gboolean egg_toolbar_get_show_arrow (EggToolbar *toolbar); +GtkOrientation egg_toolbar_get_orientation (EggToolbar *toolbar); +GtkToolbarStyle egg_toolbar_get_style (EggToolbar *toolbar); +GtkIconSize egg_toolbar_get_icon_size (EggToolbar *toolbar); +gboolean egg_toolbar_get_tooltips (EggToolbar *toolbar); +GList* egg_toolbar_get_tool_items (EggToolbar *toolbar); + + + +#ifndef EGG_DISABLE_DEPRECATED +/* Simple button items */ +GtkWidget* egg_toolbar_append_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data); +GtkWidget* egg_toolbar_prepend_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data); +GtkWidget* egg_toolbar_insert_item (EggToolbar *toolbar, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position); + +/* Stock Items */ +GtkWidget* egg_toolbar_insert_stock (EggToolbar *toolbar, + const gchar *stock_id, + const char *tooltip_text, + const char *tooltip_private_text, + GtkSignalFunc callback, + gpointer user_data, + gint position); + +/* Space Items */ +void egg_toolbar_append_space (EggToolbar *toolbar); +void egg_toolbar_prepend_space (EggToolbar *toolbar); +void egg_toolbar_insert_space (EggToolbar *toolbar, + gint position); +/* FIXME: Write this function */ +void egg_toolbar_remove_space (EggToolbar *toolbar, + gint position); +/* Any element type */ +GtkWidget* egg_toolbar_append_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data); + +GtkWidget* egg_toolbar_prepend_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data); + +GtkWidget* egg_toolbar_insert_element (EggToolbar *toolbar, + EggToolbarChildType type, + GtkWidget *widget, + const char *text, + const char *tooltip_text, + const char *tooltip_private_text, + GtkWidget *icon, + GtkSignalFunc callback, + gpointer user_data, + gint position); + +/* Generic Widgets */ +void egg_toolbar_append_widget (EggToolbar *toolbar, + GtkWidget *widget, + const char *tooltip_text, + const char *tooltip_private_text); +void egg_toolbar_prepend_widget (EggToolbar *toolbar, + GtkWidget *widget, + const char *tooltip_text, + const char *tooltip_private_text); +void egg_toolbar_insert_widget (EggToolbar *toolbar, + GtkWidget *widget, + const char *tooltip_text, + const char *tooltip_private_text, + gint position); + +#endif /* EGG_DISABLE_DEPRECATED */ + + +G_END_DECLS + +#endif /* __EGG_TOOLBAR_H__ */ diff --git a/lib/egg/eggtoolbutton.c b/lib/egg/eggtoolbutton.c new file mode 100644 index 000000000..7c6e8e865 --- /dev/null +++ b/lib/egg/eggtoolbutton.c @@ -0,0 +1,670 @@ +/* eggtoolbutton.c + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "eggtoolbutton.h" +#include <gtk/gtkbutton.h> +#include <gtk/gtkhbox.h> +#include <gtk/gtkiconfactory.h> +#include <gtk/gtkimage.h> +#include <gtk/gtkimagemenuitem.h> +#include <gtk/gtklabel.h> +#include <gtk/gtkstock.h> +#include <gtk/gtkvbox.h> + +#include <string.h> + +#ifndef _ +# define _(s) (s) +#endif + +enum { + PROP_0, + PROP_LABEL, + PROP_USE_UNDERLINE, + PROP_STOCK_ID, + PROP_ICON_SET, + PROP_ICON_WIDGET, +}; + +static void egg_tool_button_init (EggToolButton *button, + EggToolButtonClass *klass); +static void egg_tool_button_class_init (EggToolButtonClass *klass); + + +static void egg_tool_button_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void egg_tool_button_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); +static void egg_tool_button_finalize (GObject *object); + +static void egg_tool_button_show_all (GtkWidget *widget); + +static GtkWidget *egg_tool_button_create_menu_proxy (EggToolItem *item); +static void egg_tool_button_set_orientation (EggToolItem *tool_item, + GtkOrientation orientation); +static void egg_tool_button_set_icon_size (EggToolItem *tool_item, + GtkIconSize icon_size); +static void egg_tool_button_set_toolbar_style (EggToolItem *tool_item, + GtkToolbarStyle style); +static void egg_tool_button_set_relief_style (EggToolItem *tool_item, + GtkReliefStyle style); +static void egg_tool_button_set_tooltip (EggToolItem *tool_item, + GtkTooltips *tooltips, + const gchar *tip_text, + const gchar *tip_private); +static void button_clicked (GtkWidget *widget, + EggToolButton *button); + +static GObjectClass *parent_class = NULL; + +GType +egg_tool_button_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggToolButtonClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_tool_button_class_init, + (GClassFinalizeFunc) NULL, + NULL, + sizeof (EggToolButton), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_tool_button_init, + }; + + type = g_type_register_static (EGG_TYPE_TOOL_ITEM, + "EggToolButton", + &type_info, 0); + } + return type; +} + +static void +egg_tool_button_class_init (EggToolButtonClass *klass) +{ + GObjectClass *object_class; + GtkWidgetClass *widget_class; + EggToolItemClass *tool_item_class; + + parent_class = g_type_class_peek_parent (klass); + + object_class = (GObjectClass *)klass; + widget_class = (GtkWidgetClass *)klass; + tool_item_class = (EggToolItemClass *)klass; + + object_class->set_property = egg_tool_button_set_property; + object_class->get_property = egg_tool_button_get_property; + object_class->finalize = egg_tool_button_finalize; + + widget_class->show_all = egg_tool_button_show_all; + + tool_item_class->create_menu_proxy = egg_tool_button_create_menu_proxy; + tool_item_class->set_orientation = egg_tool_button_set_orientation; + tool_item_class->set_icon_size = egg_tool_button_set_icon_size; + tool_item_class->set_toolbar_style = egg_tool_button_set_toolbar_style; + tool_item_class->set_relief_style = egg_tool_button_set_relief_style; + tool_item_class->set_tooltip = egg_tool_button_set_tooltip; + + klass->button_type = GTK_TYPE_BUTTON; + + g_object_class_install_property (object_class, + PROP_LABEL, + g_param_spec_string ("label", + _("Label"), + _("Text to show in the item."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_USE_UNDERLINE, + g_param_spec_boolean ("use_underline", + _("Use underline"), + _("Interpret underlines in the item label."), + FALSE, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_STOCK_ID, + g_param_spec_string ("stock_id", + _("Stock Id"), + _("The stock icon displayed on the item."), + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_ICON_SET, + g_param_spec_boxed ("icon_set", + _("Icon set"), + _("Icon set to use to draw the item's icon."), + GTK_TYPE_ICON_SET, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_ICON_WIDGET, + g_param_spec_object ("icon_widget", + _("Icon widget"), + _("Icon widget to display in the item."), + GTK_TYPE_WIDGET, + G_PARAM_READWRITE)); +} + +static void +egg_tool_button_init (EggToolButton *button, EggToolButtonClass *klass) +{ + EggToolItem *toolitem = EGG_TOOL_ITEM (button); + + toolitem->homogeneous = TRUE; + + /* create button */ + button->button = g_object_new (klass->button_type, NULL); + GTK_WIDGET_UNSET_FLAGS (button->button, GTK_CAN_FOCUS); + g_signal_connect_object (button->button, "clicked", + G_CALLBACK (button_clicked), button, 0); + + button->box = gtk_vbox_new (FALSE, 0); + gtk_container_add (GTK_CONTAINER (button->button), button->box); + gtk_widget_show (button->box); + +#if 0 + button->icon = gtk_image_new(); + gtk_box_pack_start (GTK_BOX (button->box), button->icon, TRUE, TRUE, 0); + gtk_widget_show (button->icon); +#endif + + button->label = gtk_label_new (NULL); + gtk_label_set_use_underline (GTK_LABEL (button->label), TRUE); + gtk_box_pack_start (GTK_BOX (button->box), button->label, FALSE, TRUE, 0); + gtk_widget_show (button->label); + + gtk_container_add (GTK_CONTAINER (button), button->button); + gtk_widget_show (button->button); +} + +static gchar * +elide_underscores (const gchar *original) +{ + gchar *q, *result; + const gchar *p; + gboolean last_underscore; + + q = result = g_malloc (strlen (original) + 1); + last_underscore = FALSE; + + for (p = original; *p; p++) + { + if (!last_underscore && *p == '_') + last_underscore = TRUE; + else + { + last_underscore = FALSE; + *q++ = *p; + } + } + + *q = '\0'; + + return result; +} + +static void +egg_tool_button_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EggToolButton *button = EGG_TOOL_BUTTON (object); + GtkStockItem stock_item; + GtkIconSet *icon_set; + gchar *label_no_mnemonic = NULL; + + switch (prop_id) + { + case PROP_LABEL: + egg_tool_button_set_label (button, g_value_get_string (value)); + break; + case PROP_USE_UNDERLINE: + gtk_label_set_use_underline (GTK_LABEL (button->label), + g_value_get_boolean (value)); + break; + case PROP_STOCK_ID: + g_free (button->stock_id); + button->stock_id = g_value_dup_string (value); + if (!button->label_set) + { + if (gtk_stock_lookup (button->stock_id, &stock_item)) + { + label_no_mnemonic = elide_underscores (stock_item.label); + gtk_label_set_label (GTK_LABEL (button->label), label_no_mnemonic); + g_free (label_no_mnemonic); + } + } + if (!button->icon_set) + { + if (button->icon && !GTK_IS_IMAGE (button->icon)) + { + gtk_container_remove (GTK_CONTAINER (button->box), button->icon); + button->icon = NULL; + } + if (!button->icon) + { + button->icon = gtk_image_new (); + gtk_box_pack_start (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0); + gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0); + } + gtk_image_set_from_stock (GTK_IMAGE (button->icon), button->stock_id, + EGG_TOOL_ITEM (button)->icon_size); + } + break; + case PROP_ICON_SET: + if (button->icon && !GTK_IS_IMAGE (button->icon)) + { + gtk_container_remove (GTK_CONTAINER (button->box), button->icon); + button->icon = NULL; + } + if (!button->icon) + { + button->icon = gtk_image_new (); + gtk_box_pack_start (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0); + gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0); + } + icon_set = g_value_get_boxed (value); + button->icon_set = (icon_set != NULL); + if (!button->icon_set && button->stock_id) + gtk_image_set_from_stock (GTK_IMAGE (button->icon), button->stock_id, + EGG_TOOL_ITEM (button)->icon_size); + else + gtk_image_set_from_icon_set (GTK_IMAGE (button->icon), icon_set, + EGG_TOOL_ITEM (button)->icon_size); + break; + case PROP_ICON_WIDGET: + egg_tool_button_set_icon_widget (button, g_value_get_object (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +egg_tool_button_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EggToolButton *button = EGG_TOOL_BUTTON (object); + + switch (prop_id) + { + case PROP_LABEL: + if (button->label_set) + g_value_set_string (value, + gtk_label_get_label (GTK_LABEL (button->label))); + break; + case PROP_USE_UNDERLINE: + g_value_set_boolean (value, + gtk_label_get_use_underline (GTK_LABEL (button->label))); + break; + case PROP_STOCK_ID: + g_value_set_string (value, button->stock_id); + break; + case PROP_ICON_SET: + if (GTK_IS_IMAGE (button->icon) && + GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_ICON_SET) + { + GtkIconSet *icon_set; + gtk_image_get_icon_set (GTK_IMAGE (button->icon), &icon_set, NULL); + g_value_set_boxed (value, icon_set); + } + else + g_value_set_boxed (value, NULL); + break; + case PROP_ICON_WIDGET: + g_value_set_object (value, button->icon); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +egg_tool_button_finalize (GObject *object) +{ + EggToolButton *button = EGG_TOOL_BUTTON (object); + + g_free (button->stock_id); + button->stock_id = NULL; + + parent_class->finalize (object); +} + +static void +egg_tool_button_show_all (GtkWidget *widget) +{ + EggToolButton *button = EGG_TOOL_BUTTON (widget); + + switch (EGG_TOOL_ITEM (widget)->style) + { + case GTK_TOOLBAR_ICONS: + if (button->icon) gtk_widget_show_all (button->icon); + gtk_widget_hide (button->label); + gtk_widget_show (button->box); + gtk_widget_show (button->button); + break; + case GTK_TOOLBAR_TEXT: + if (button->icon) gtk_widget_hide (button->icon); + gtk_widget_show_all (button->label); + gtk_widget_show (button->box); + gtk_widget_show (button->button); + break; + case GTK_TOOLBAR_BOTH: + case GTK_TOOLBAR_BOTH_HORIZ: + gtk_widget_show_all (button->button); + } + + gtk_widget_show (GTK_WIDGET (button)); +} + +static GtkWidget * +egg_tool_button_create_menu_proxy (EggToolItem *item) +{ + EggToolButton *button = EGG_TOOL_BUTTON (item); + GtkWidget *menu_item; + GtkWidget *image; + const char *label; + + label = gtk_label_get_text (GTK_LABEL (button->label)); + + menu_item = gtk_image_menu_item_new_with_label (label); + + if (GTK_IS_IMAGE (button->icon)) + { + image = gtk_image_new (); + + if (GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_STOCK) + { + gchar *stock_id; + + gtk_image_get_stock (GTK_IMAGE (button->icon), + &stock_id, NULL); + gtk_image_set_from_stock (GTK_IMAGE (image), stock_id, + GTK_ICON_SIZE_MENU); + } + else if (GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_ICON_SET) + { + GtkIconSet *icon_set; + + gtk_image_get_icon_set (GTK_IMAGE (button->icon), &icon_set, NULL); + gtk_image_set_from_icon_set (GTK_IMAGE (image), icon_set, + GTK_ICON_SIZE_MENU); + } + else + { + g_warning ("FIXME: Add more cases here"); + } + gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image); + } + + g_signal_connect_object (menu_item, "activate", + G_CALLBACK (gtk_button_clicked), + EGG_TOOL_BUTTON (button)->button, + G_CONNECT_SWAPPED); + + return menu_item; +} + +static void +egg_tool_button_set_orientation (EggToolItem *tool_item, + GtkOrientation orientation) +{ + if (tool_item->orientation != orientation) + { + tool_item->orientation = orientation; + } +} + +static void +egg_tool_button_set_icon_size (EggToolItem *tool_item, + GtkIconSize icon_size) +{ + if (tool_item->icon_size != icon_size) + { + EggToolButton *button = EGG_TOOL_BUTTON (tool_item); + char *stock_id; + + if (button->icon && GTK_IS_IMAGE (button->icon) && + gtk_image_get_storage_type (GTK_IMAGE (button->icon)) == GTK_IMAGE_STOCK) + { + gtk_image_get_stock (GTK_IMAGE (button->icon), &stock_id, NULL); + stock_id = g_strdup (stock_id); + gtk_image_set_from_stock (GTK_IMAGE (button->icon), + stock_id, + icon_size); + g_free (stock_id); + } + tool_item->icon_size = icon_size; + } +} + +static void +egg_tool_button_set_toolbar_style (EggToolItem *tool_item, + GtkToolbarStyle style) +{ + EggToolButton *button = EGG_TOOL_BUTTON (tool_item); + + if (tool_item->style != style) + { + tool_item->style = style; + + switch (tool_item->style) + { + case GTK_TOOLBAR_ICONS: + gtk_widget_hide (button->label); + if (button->icon) { + gtk_box_set_child_packing (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->icon); + } + break; + case GTK_TOOLBAR_TEXT: + gtk_box_set_child_packing (GTK_BOX (button->box), button->label, + TRUE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->label); + if (button->icon) + gtk_widget_hide (button->icon); + break; + case GTK_TOOLBAR_BOTH: + if (GTK_IS_HBOX (button->box)) + { + GtkWidget *vbox; + + vbox = gtk_vbox_new (FALSE, 0); + gtk_widget_show (vbox); + + if (button->icon) + { + g_object_ref (button->icon); + gtk_container_remove (GTK_CONTAINER (button->box), button->icon); + gtk_box_pack_start (GTK_BOX (vbox), button->icon, + TRUE, TRUE, 0); + g_object_unref (button->icon); + } + + g_object_ref (button->label); + gtk_container_remove (GTK_CONTAINER (button->box), button->label); + gtk_box_pack_start (GTK_BOX (vbox), button->label, FALSE, TRUE, 0); + g_object_unref (button->label); + + gtk_container_remove (GTK_CONTAINER (button->button), button->box); + button->box = vbox; + gtk_container_add (GTK_CONTAINER (button->button), button->box); + } + + gtk_box_set_child_packing (GTK_BOX (button->box), button->label, + FALSE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->label); + if (button->icon) { + gtk_box_set_child_packing (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->icon); + } + break; + case GTK_TOOLBAR_BOTH_HORIZ: + if (GTK_IS_VBOX (button->box)) + { + GtkWidget *hbox; + + hbox = gtk_hbox_new (FALSE, 0); + gtk_widget_show (hbox); + + if (button->icon) + { + g_object_ref (button->icon); + gtk_container_remove (GTK_CONTAINER (button->box), button->icon); + gtk_box_pack_start (GTK_BOX (hbox), button->icon, + TRUE, TRUE, 0); + g_object_unref (button->icon); + } + + g_object_ref (button->label); + gtk_container_remove (GTK_CONTAINER (button->box), button->label); + gtk_box_pack_start (GTK_BOX (hbox), button->label, FALSE, TRUE, 0); + g_object_unref (button->label); + + gtk_container_remove (GTK_CONTAINER (button->button), button->box); + button->box = hbox; + gtk_container_add (GTK_CONTAINER (button->button), button->box); + } + + gtk_box_set_child_packing (GTK_BOX (button->box), button->label, + TRUE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->label); + if (button->icon) { + gtk_box_set_child_packing (GTK_BOX (button->box), button->icon, + FALSE, TRUE, 0, GTK_PACK_START); + gtk_widget_show (button->icon); + } + break; + } + } +} + +static void +egg_tool_button_set_relief_style (EggToolItem *tool_item, + GtkReliefStyle style) +{ + gtk_button_set_relief (GTK_BUTTON (EGG_TOOL_BUTTON (tool_item)->button), style); +} + +static void +egg_tool_button_set_tooltip (EggToolItem *tool_item, + GtkTooltips *tooltips, + const gchar *tip_text, + const gchar *tip_private) +{ + gtk_tooltips_set_tip (tooltips, EGG_TOOL_BUTTON (tool_item)->button, + tip_text, tip_private); +} + +static void +button_clicked (GtkWidget *widget, EggToolButton *button) +{ + g_signal_emit_by_name (button, "clicked"); +} + +EggToolItem * +egg_tool_button_new_from_stock (const gchar *stock_id) +{ + EggToolButton *button; + + button = g_object_new (EGG_TYPE_TOOL_BUTTON, + "stock_id", stock_id, + "use_underline", TRUE, + NULL); + + return EGG_TOOL_ITEM (button); +} + +EggToolItem * +egg_tool_button_new (void) +{ + EggToolButton *button; + + button = g_object_new (EGG_TYPE_TOOL_BUTTON, + NULL); + + return EGG_TOOL_ITEM (button); +} + +void +egg_tool_button_set_icon_widget (EggToolButton *button, + GtkWidget *icon) +{ + g_return_if_fail (EGG_IS_TOOL_BUTTON (button)); + g_return_if_fail (GTK_IS_WIDGET (icon)); + + if (button->icon) + gtk_container_remove (GTK_CONTAINER (button->box), button->icon); + button->icon = NULL; + + button->icon_set = (icon != NULL); + if (icon) + { + button->icon = icon; + gtk_box_pack_start (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0); + gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0); + } + else if (button->stock_id) + { + button->icon = gtk_image_new_from_stock (button->stock_id, + EGG_TOOL_ITEM (button)->icon_size); + gtk_box_pack_start (GTK_BOX (button->box), button->icon, + TRUE, TRUE, 0); + } +} + +void +egg_tool_button_set_label (EggToolButton *button, + const gchar *label) +{ + gchar *label_no_mnemonic = NULL; + GtkStockItem stock_item; + + g_return_if_fail (EGG_IS_TOOL_BUTTON (button)); + g_return_if_fail (label != NULL); + + button->label_set = (label != NULL); + + if (label) + label_no_mnemonic = elide_underscores (label); + else if (button->stock_id && gtk_stock_lookup (button->stock_id, &stock_item)) + label_no_mnemonic = elide_underscores (stock_item.label); + + gtk_label_set_label (GTK_LABEL (button->label), label_no_mnemonic); + g_free (label_no_mnemonic); +} + diff --git a/lib/egg/eggtoolbutton.h b/lib/egg/eggtoolbutton.h new file mode 100644 index 000000000..cb0c0b15e --- /dev/null +++ b/lib/egg/eggtoolbutton.h @@ -0,0 +1,74 @@ +/* eggtoolbutton.h + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_TOOL_BUTTON_H__ +#define __EGG_TOOL_BUTTON_H__ + +#include "eggtoolitem.h" + +G_BEGIN_DECLS + +#define EGG_TYPE_TOOL_BUTTON (egg_tool_button_get_type ()) +#define EGG_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_TOOL_BUTTON, EggToolButton)) +#define EGG_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_TOOL_BUTTON, EggToolButtonClass)) +#define EGG_IS_TOOL_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_TOOL_BUTTON)) +#define EGG_IS_TOOL_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_TOOL_BUTTON)) +#define EGG_TOOL_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_TOOL_BUTTON, EggToolButtonClass)) + +typedef struct _EggToolButton EggToolButton; +typedef struct _EggToolButtonClass EggToolButtonClass; + +struct _EggToolButton +{ + EggToolItem parent; + + /*< private >*/ + GtkWidget *button; + GtkWidget *box; + GtkWidget *label; + GtkWidget *icon; + + gchar *stock_id; + guint label_set : 1; + guint icon_set : 1; +}; + +struct _EggToolButtonClass +{ + EggToolItemClass parent_class; + + GType button_type; +}; + +GType egg_tool_button_get_type (void); +EggToolItem *egg_tool_button_new (void); +EggToolItem *egg_tool_button_new_from_stock (const gchar *stock_id); + + +void egg_tool_button_set_icon_widget (EggToolButton *button, + GtkWidget *icon); +void egg_tool_button_set_label (EggToolButton *button, + const gchar *label); + + +G_END_DECLS + +#endif /* __EGG_TOOL_BUTTON_H__ */ diff --git a/lib/egg/eggtoolitem.c b/lib/egg/eggtoolitem.c new file mode 100644 index 000000000..0bb234286 --- /dev/null +++ b/lib/egg/eggtoolitem.c @@ -0,0 +1,416 @@ +/* eggtoolitem.c + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "eggtoolitem.h" +#include "eggmarshalers.h" + +#ifndef _ +# define _(s) (s) +#endif + +enum { + CLICKED, + CREATE_MENU_PROXY, + SET_ORIENTATION, + SET_ICON_SIZE, + SET_TOOLBAR_STYLE, + SET_RELIEF_STYLE, + SET_TOOLTIP, + LAST_SIGNAL +}; + +enum { + PROP_0, + PROP_VISIBLE_HORIZONTAL, + PROP_VISIBLE_VERTICAL, + PROP_HOMOGENEOUS, + PROP_ORIENTATION +}; + +static void egg_tool_item_init (EggToolItem *toolitem); +static void egg_tool_item_class_init (EggToolItemClass *class); + +static void egg_tool_item_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void egg_tool_item_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +static void egg_tool_item_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void egg_tool_item_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); + +static GtkWidget *egg_tool_item_create_menu_proxy (EggToolItem *item); + + +static GObjectClass *parent_class = NULL; +static guint toolitem_signals[LAST_SIGNAL] = { 0 }; + +GType +egg_tool_item_get_type (void) +{ + static GtkType type = 0; + + if (!type) + { + static const GTypeInfo type_info = + { + sizeof (EggToolItemClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) egg_tool_item_class_init, + (GClassFinalizeFunc) NULL, + NULL, + + sizeof (EggToolItem), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_tool_item_init, + }; + + type = g_type_register_static (GTK_TYPE_BIN, + "EggToolItem", + &type_info, 0); + } + return type; +} + +static gboolean +create_proxy_accumulator (GSignalInvocationHint *hint, + GValue *return_accumulator, + const GValue *handler_return, + gpointer user_data) +{ + GObject *proxy; + gboolean continue_emission; + + proxy = g_value_get_object(handler_return); + g_value_set_object(return_accumulator, proxy); + continue_emission = (proxy == NULL); + + return continue_emission; +} + +static void +egg_tool_item_class_init (EggToolItemClass *klass) +{ + GObjectClass *object_class; + GtkWidgetClass *widget_class; + + parent_class = g_type_class_peek_parent (klass); + object_class = (GObjectClass *)klass; + widget_class = (GtkWidgetClass *)klass; + + object_class->set_property = egg_tool_item_set_property; + object_class->get_property = egg_tool_item_get_property; + + widget_class->size_request = egg_tool_item_size_request; + widget_class->size_allocate = egg_tool_item_size_allocate; + + klass->create_menu_proxy = egg_tool_item_create_menu_proxy; + + g_object_class_install_property (object_class, + PROP_VISIBLE_HORIZONTAL, + g_param_spec_boolean ("visible_horizontal", + _("Visible when horizontal"), + _("Whether the toolbar item is visible when the toolbar is in a horizontal orientation."), + TRUE, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_VISIBLE_VERTICAL, + g_param_spec_boolean ("visible_vertical", + _("Visible when vertical"), + _("Whether the toolbar item is visible when the toolbar is in a vertical orientation."), + TRUE, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_HOMOGENEOUS, + g_param_spec_boolean ("homogeneous", + _("Homogeneous size"), + _("Whether the toolbar item should be the same size as other homogeneous items."), + FALSE, + G_PARAM_READWRITE)); + + toolitem_signals[CLICKED] = + g_signal_new ("clicked", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EggToolItemClass, clicked), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + toolitem_signals[CREATE_MENU_PROXY] = + g_signal_new ("create_menu_proxy", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, create_menu_proxy), + create_proxy_accumulator, NULL, + _egg_marshal_OBJECT__VOID, + GTK_TYPE_WIDGET, 0); + toolitem_signals[SET_ORIENTATION] = + g_signal_new ("set_orientation", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, set_orientation), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_ORIENTATION); + toolitem_signals[SET_ICON_SIZE] = + g_signal_new ("set_icon_size", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, set_icon_size), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_ICON_SIZE); + toolitem_signals[SET_TOOLBAR_STYLE] = + g_signal_new ("set_toolbar_style", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, set_toolbar_style), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_TOOLBAR_STYLE); + toolitem_signals[SET_RELIEF_STYLE] = + g_signal_new ("set_relief_style", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, set_relief_style), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_RELIEF_STYLE); + toolitem_signals[SET_TOOLTIP] = + g_signal_new ("set_tooltip", + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolItemClass, set_tooltip), + NULL, NULL, + _egg_marshal_VOID__OBJECT_STRING_STRING, + G_TYPE_NONE, 3, + GTK_TYPE_TOOLTIPS, + G_TYPE_STRING, + G_TYPE_STRING); + +} + +static void +egg_tool_item_init (EggToolItem *toolitem) +{ + toolitem->visible_horizontal = TRUE; + toolitem->visible_vertical = TRUE; + toolitem->homogeneous = FALSE; + + toolitem->orientation = GTK_ORIENTATION_HORIZONTAL; + toolitem->icon_size = GTK_ICON_SIZE_LARGE_TOOLBAR; + toolitem->style = GTK_TOOLBAR_BOTH; +} + +static void +egg_tool_item_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EggToolItem *toolitem = EGG_TOOL_ITEM (object); + + switch (prop_id) + { + case PROP_VISIBLE_HORIZONTAL: + toolitem->visible_horizontal = g_value_get_boolean (value); + break; + case PROP_VISIBLE_VERTICAL: + toolitem->visible_vertical = g_value_get_boolean (value); + break; + case PROP_HOMOGENEOUS: + toolitem->homogeneous = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +egg_tool_item_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EggToolItem *toolitem = EGG_TOOL_ITEM (object); + + switch (prop_id) + { + case PROP_VISIBLE_HORIZONTAL: + g_value_set_boolean (value, toolitem->visible_horizontal); + break; + case PROP_VISIBLE_VERTICAL: + g_value_set_boolean (value, toolitem->visible_vertical); + break; + case PROP_HOMOGENEOUS: + g_value_set_boolean (value, toolitem->homogeneous); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +egg_tool_item_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + GtkBin *bin = GTK_BIN (widget); + + if (bin->child) + gtk_widget_size_request (bin->child, requisition); + + requisition->width += GTK_CONTAINER (widget)->border_width * 2; + requisition->height += GTK_CONTAINER (widget)->border_width * 2; +} + +static void +egg_tool_item_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkBin *bin = GTK_BIN (widget); + GtkAllocation child_allocation; + + widget->allocation = *allocation; + + if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) + { + child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width; + child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width; + child_allocation.width = allocation->width - GTK_CONTAINER (widget)->border_width * 2; + child_allocation.height = allocation->height - GTK_CONTAINER (widget)->border_width * 2; + + gtk_widget_size_allocate (bin->child, &child_allocation); + } +} + +static GtkWidget * +egg_tool_item_create_menu_proxy (EggToolItem *item) +{ + if (GTK_BIN (item)->child) + return NULL; + else + return gtk_separator_menu_item_new (); +} + +EggToolItem * +egg_tool_item_new (void) +{ + EggToolItem *item; + + item = g_object_new (EGG_TYPE_TOOL_ITEM, NULL); + + return item; +} + +void +egg_tool_item_set_orientation (EggToolItem *tool_item, + GtkOrientation orientation) +{ + g_return_if_fail (EGG_IS_TOOL_ITEM (tool_item)); + + g_signal_emit (tool_item, toolitem_signals[SET_ORIENTATION], 0, orientation); +} + +void +egg_tool_item_set_icon_size (EggToolItem *tool_item, + GtkIconSize icon_size) +{ + g_return_if_fail (EGG_IS_TOOL_ITEM (tool_item)); + + g_signal_emit (tool_item, toolitem_signals[SET_ICON_SIZE], 0, icon_size); +} + +void +egg_tool_item_set_toolbar_style (EggToolItem *tool_item, + GtkToolbarStyle style) +{ + g_return_if_fail (EGG_IS_TOOL_ITEM (tool_item)); + + g_signal_emit (tool_item, toolitem_signals[SET_TOOLBAR_STYLE], 0, style); +} + +void +egg_tool_item_set_relief_style (EggToolItem *tool_item, + GtkReliefStyle style) +{ + g_return_if_fail (EGG_IS_TOOL_ITEM (tool_item)); + + g_signal_emit (tool_item, toolitem_signals[SET_RELIEF_STYLE], 0, style); +} + +void +egg_tool_item_set_expandable (EggToolItem *tool_item, + gboolean expandable) +{ + if ((expandable && tool_item->expandable) || + (!expandable && !tool_item->expandable)) + return; + + tool_item->expandable = expandable; + gtk_widget_queue_resize (GTK_WIDGET (tool_item)); +} + +void +egg_tool_item_set_pack_end (EggToolItem *tool_item, + gboolean pack_end) +{ + if ((pack_end && tool_item->pack_end) || + (!pack_end && !tool_item->pack_end)) + return; + + tool_item->pack_end = pack_end; + gtk_widget_queue_resize (GTK_WIDGET (tool_item)); +} + +void +egg_tool_item_set_homogeneous (EggToolItem *tool_item, + gboolean homogeneous) +{ + if ((homogeneous && tool_item->homogeneous) || + (!homogeneous && !tool_item->homogeneous)) + return; + + tool_item->homogeneous = homogeneous; + gtk_widget_queue_resize (GTK_WIDGET (tool_item)); +} + +void +egg_tool_item_set_tooltip (EggToolItem *tool_item, + GtkTooltips *tooltips, + const gchar *tip_text, + const gchar *tip_private) +{ + g_return_if_fail (EGG_IS_TOOL_ITEM (tool_item)); + + g_signal_emit (tool_item, toolitem_signals[SET_TOOLTIP], 0, + tooltips, tip_text, tip_private); +} diff --git a/lib/egg/eggtoolitem.h b/lib/egg/eggtoolitem.h new file mode 100644 index 000000000..222e095f9 --- /dev/null +++ b/lib/egg/eggtoolitem.h @@ -0,0 +1,98 @@ +/* eggtoolitem.c + * + * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se> + * Copyright (C) 2002 James Henstridge <james@daa.com.au> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_TOOL_ITEM_H__ +#define __EGG_TOOL_ITEM_H__ + +#include <gtk/gtkbin.h> +#include <gtk/gtktooltips.h> + +#define EGG_TYPE_TOOL_ITEM (egg_tool_item_get_type ()) +#define EGG_TOOL_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_TOOL_ITEM, EggToolItem)) +#define EGG_TOOL_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_TOOL_ITEM, EggToolItemClass)) +#define EGG_IS_TOOL_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_TOOL_ITEM)) +#define EGG_IS_TOOL_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EGG_TYPE_TOOL_ITEM)) +#define EGG_TOOL_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EGG_TYPE_TOOL_ITEM, EggToolItemClass)) + +typedef struct _EggToolItem EggToolItem; +typedef struct _EggToolItemClass EggToolItemClass; + +struct _EggToolItem +{ + GtkBin parent; + + GtkOrientation orientation; + GtkIconSize icon_size; + GtkToolbarStyle style; + + gchar *tip_text; + gchar *tip_private; + + guint visible_horizontal : 1; + guint visible_vertical : 1; + guint homogeneous : 1; + guint expandable : 1; + guint pack_end : 1; +}; + +struct _EggToolItemClass +{ + GtkBinClass parent_class; + + void (* clicked) (EggToolItem *tool_item); + GtkWidget *(* create_menu_proxy) (EggToolItem *tool_item); + void (* set_orientation) (EggToolItem *tool_item, + GtkOrientation orientation); + void (* set_icon_size) (EggToolItem *tool_item, + GtkIconSize icon_size); + void (* set_toolbar_style) (EggToolItem *tool_item, + GtkToolbarStyle style); + void (* set_relief_style) (EggToolItem *tool_item, + GtkReliefStyle relief_style); + void (* set_tooltip) (EggToolItem *tool_item, + GtkTooltips *tooltips, + const gchar *tip_text, + const gchar *tip_private); +}; + +GType egg_tool_item_get_type (void); +EggToolItem *egg_tool_item_new (void); + +void egg_tool_item_set_orientation (EggToolItem *tool_item, + GtkOrientation orientation); +void egg_tool_item_set_icon_size (EggToolItem *tool_item, + GtkIconSize icon_size); +void egg_tool_item_set_toolbar_style (EggToolItem *tool_item, + GtkToolbarStyle style); +void egg_tool_item_set_relief_style (EggToolItem *tool_item, + GtkReliefStyle style); +void egg_tool_item_set_homogeneous (EggToolItem *tool_item, + gboolean homogeneous); +void egg_tool_item_set_expandable (EggToolItem *tool_item, + gboolean expandable); +void egg_tool_item_set_pack_end (EggToolItem *tool_item, + gboolean pack_end); +void egg_tool_item_set_tooltip (EggToolItem *tool_item, + GtkTooltips *tooltips, + const gchar *tip_text, + const gchar *tip_private); + +#endif /* __EGG_TOOL_ITEM_H__ */ diff --git a/lib/egg/prop-editor.h b/lib/egg/prop-editor.h new file mode 100644 index 000000000..1f86d9be1 --- /dev/null +++ b/lib/egg/prop-editor.h @@ -0,0 +1,31 @@ +/* prop-editor.h + * Copyright (C) 2000 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#include <gtk/gtkwidget.h> + +#ifndef __PROP_EDITOR_H__ +#define __PROP_EDITOR_H__ + +G_BEGIN_DECLS + +GtkWidget *create_prop_editor (GObject *object, + GType type); + +G_END_DECLS + +#endif /* __PROP_EDITOR_H__ */ |