summaryrefslogtreecommitdiff
path: root/gtk
diff options
context:
space:
mode:
authorMatthias Clasen <maclas@gmx.de>2003-09-15 19:51:55 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2003-09-15 19:51:55 +0000
commitcaf380c793789e868458e32fbb64a33b5ef1d852 (patch)
treeee65930936942b62c28d6ae7a5de24905059e906 /gtk
parentb14b12f9d2b5ba0b554b7d8ec2a3c90a8cd78d4f (diff)
downloadgdk-pixbuf-caf380c793789e868458e32fbb64a33b5ef1d852.tar.gz
Add value parameter to allow setting the currently selected group member
2003-09-15 Matthias Clasen <maclas@gmx.de> * gtk/gtkactiongroup.[hc]: (gtk_action_group_add_radio_actions): (gtk_action_group_add_radio_actions_full): Add value parameter to allow setting the currently selected group member before connecting signals. (GtkToggleActionEntry): Separate struct for constructing toggle actions, including a boolean to initialize the action state before connecting signals. (gtk_action_group_add_toggle_actions): (gtk_action_group_add_toggle_actions_full): New functions to construct toggle actions from an array of GtkToggleActionEntries. * demos/gtk-demo/appwindow.c: * tests/testactions.c: * tests/testmerge.c: Adjust to new action group API. * gtk/gtk-sections.txt: Add gtk_action_group_add_toggle_actions[_full].
Diffstat (limited to 'gtk')
-rw-r--r--gtk/gtkactiongroup.c161
-rw-r--r--gtk/gtkactiongroup.h117
2 files changed, 206 insertions, 72 deletions
diff --git a/gtk/gtkactiongroup.c b/gtk/gtkactiongroup.c
index 8e35839ec..f09e2e4ae 100644
--- a/gtk/gtkactiongroup.c
+++ b/gtk/gtkactiongroup.c
@@ -48,8 +48,12 @@ struct _GtkActionGroupPrivate
GtkDestroyNotify translate_notify;
};
-static void gtk_action_group_init (GtkActionGroup *self);
-static void gtk_action_group_class_init (GtkActionGroupClass *class);
+static void gtk_action_group_init (GtkActionGroup *self);
+static void gtk_action_group_class_init (GtkActionGroupClass *class);
+static void gtk_action_group_finalize (GObject *object);
+static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
+ const gchar *name);
+
GType
gtk_action_group_get_type (void)
@@ -79,9 +83,6 @@ gtk_action_group_get_type (void)
}
static GObjectClass *parent_class = NULL;
-static void gtk_action_group_finalize (GObject *object);
-static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
- const gchar *name);
static void
gtk_action_group_class_init (GtkActionGroupClass *klass)
@@ -284,8 +285,8 @@ gtk_action_group_list_actions (GtkActionGroup *action_group)
* @n_entries: the number of entries
* @user_data: data to pass to the action callbacks
*
- * This is a convenience function to create a number of actions and add them to the
- * action group.
+ * This is a convenience function to create a number of actions and add them
+ * to the action group.
*
* The "activate" signals of the actions are connected to the callbacks and
* their accel paths are set to
@@ -337,15 +338,118 @@ gtk_action_group_add_actions_full (GtkActionGroup *action_group,
for (i = 0; i < n_entries; i++)
{
GtkAction *action;
- GType action_type;
gchar *accel_path;
gchar *label;
gchar *tooltip;
- if (entries[i].is_toggle)
- action_type = GTK_TYPE_TOGGLE_ACTION;
+ if (translate_func)
+ {
+ label = translate_func (entries[i].label, translate_data);
+ tooltip = translate_func (entries[i].tooltip, translate_data);
+ }
else
- action_type = GTK_TYPE_ACTION;
+ {
+ label = entries[i].label;
+ tooltip = entries[i].tooltip;
+ }
+
+ action = g_object_new (GTK_TYPE_ACTION,
+ "name", entries[i].name,
+ "label", label,
+ "tooltip", tooltip,
+ "stock_id", entries[i].stock_id,
+ NULL);
+
+ if (entries[i].callback)
+ g_signal_connect_data (action, "activate",
+ entries[i].callback,
+ user_data, (GClosureNotify)destroy, 0);
+
+ /* set the accel path for the menu item */
+ accel_path = g_strconcat ("<Actions>/", action_group->private_data->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);
+ }
+
+ gtk_action_set_accel_path (action, accel_path);
+ g_free (accel_path);
+
+ gtk_action_group_add_action (action_group, action);
+ g_object_unref (action);
+ }
+}
+
+/**
+ * gtk_action_group_add_toggle_actions:
+ * @action_group: the action group
+ * @entries: an array of toggle action descriptions
+ * @n_entries: the number of entries
+ * @user_data: data to pass to the action callbacks
+ *
+ * This is a convenience function to create a number of toggle actions and add them
+ * to the action group.
+ *
+ * The "activate" signals of the actions are connected to the callbacks and
+ * their accel paths are set to
+ * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
+ *
+ * Since: 2.4
+ */
+void
+gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
+ GtkToggleActionEntry *entries,
+ guint n_entries,
+ gpointer user_data)
+{
+ gtk_action_group_add_toggle_actions_full (action_group,
+ entries, n_entries,
+ user_data, NULL);
+}
+
+
+/**
+ * gtk_action_group_add_toggle_actions_full:
+ * @action_group: the action group
+ * @entries: an array of toggle action descriptions
+ * @n_entries: the number of entries
+ * @user_data: data to pass to the action callbacks
+ * @destroy: destroy notification callback for @user_data
+ *
+ * This variant of gtk_action_group_add_toggle_actions() adds a
+ * #GDestroyNotify callback for @user_data.
+ *
+ * Since: 2.4
+ */
+void
+gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
+ GtkToggleActionEntry *entries,
+ guint n_entries,
+ gpointer user_data,
+ GDestroyNotify destroy)
+{
+ guint i;
+ GtkTranslateFunc translate_func;
+ gpointer translate_data;
+
+ g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
+
+ translate_func = action_group->private_data->translate_func;
+ translate_data = action_group->private_data->translate_data;
+
+ for (i = 0; i < n_entries; i++)
+ {
+ GtkAction *action;
+ gchar *accel_path;
+ gchar *label;
+ gchar *tooltip;
if (translate_func)
{
@@ -358,13 +462,16 @@ gtk_action_group_add_actions_full (GtkActionGroup *action_group,
tooltip = entries[i].tooltip;
}
- action = g_object_new (action_type,
+ action = g_object_new (GTK_TYPE_TOGGLE_ACTION,
"name", entries[i].name,
"label", label,
"tooltip", tooltip,
"stock_id", entries[i].stock_id,
NULL);
+ gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
+ entries[i].is_active);
+
if (entries[i].callback)
g_signal_connect_data (action, "activate",
entries[i].callback,
@@ -397,14 +504,16 @@ gtk_action_group_add_actions_full (GtkActionGroup *action_group,
* @action_group: the action group
* @entries: an array of radio action descriptions
* @n_entries: the number of entries
+ * @value: the value of the action to activate initially, or -1 if
+ * no action should be activated
* @on_change: the callback to connect to the changed signal
* @user_data: data to pass to the action callbacks
*
* This is a convenience routine to create a group of radio actions and
* add them to the action group.
*
- * The"changed" signal of the first radio action is connected to the @on_change
- * callback and the accel paths of the actions are set to
+ * The "changed" signal of the first radio action is connected to the
+ * @on_change callback and the accel paths of the actions are set to
* <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
*
* Since: 2.4
@@ -413,11 +522,13 @@ void
gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
GtkRadioActionEntry *entries,
guint n_entries,
+ gint value,
GCallback on_change,
gpointer user_data)
{
gtk_action_group_add_radio_actions_full (action_group,
entries, n_entries,
+ value,
on_change, user_data, NULL);
}
@@ -426,6 +537,8 @@ gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
* @action_group: the action group
* @entries: an array of radio action descriptions
* @n_entries: the number of entries
+ * @value: the value of the action to activate initially, or -1 if
+ * no action should be activated
* @on_change: the callback to connect to the changed signal
* @user_data: data to pass to the action callbacks
* @destroy: destroy notification callback for @user_data
@@ -439,6 +552,7 @@ void
gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
GtkRadioActionEntry *entries,
guint n_entries,
+ gint value,
GCallback on_change,
gpointer user_data,
GDestroyNotify destroy)
@@ -447,6 +561,7 @@ gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
GtkTranslateFunc translate_func;
gpointer translate_data;
GSList *group = NULL;
+ GtkAction *first_action;
g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
@@ -478,14 +593,13 @@ gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
"stock_id", entries[i].stock_id,
"value", entries[i].value,
NULL);
-
+
if (i == 0)
- {
- if (on_change)
- g_signal_connect_data (action, "changed",
- on_change, user_data,
- (GClosureNotify)destroy, 0);
- }
+ first_action = action;
+
+ if (value == entries[i].value)
+ gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
+
gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
group = gtk_radio_action_get_group (GTK_RADIO_ACTION (action));
@@ -509,6 +623,11 @@ gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
gtk_action_group_add_action (action_group, action);
g_object_unref (action);
}
+
+ if (on_change)
+ g_signal_connect_data (first_action, "changed",
+ on_change, user_data,
+ (GClosureNotify)destroy, 0);
}
/**
diff --git a/gtk/gtkactiongroup.h b/gtk/gtkactiongroup.h
index f3a01b537..22d99f48d 100644
--- a/gtk/gtkactiongroup.h
+++ b/gtk/gtkactiongroup.h
@@ -44,6 +44,7 @@ typedef struct _GtkActionGroup GtkActionGroup;
typedef struct _GtkActionGroupPrivate GtkActionGroupPrivate;
typedef struct _GtkActionGroupClass GtkActionGroupClass;
typedef struct _GtkActionEntry GtkActionEntry;
+typedef struct _GtkToggleActionEntry GtkToggleActionEntry;
typedef struct _GtkRadioActionEntry GtkRadioActionEntry;
struct _GtkActionGroup
@@ -71,15 +72,23 @@ struct _GtkActionGroupClass
struct _GtkActionEntry
{
- gchar *name;
- gchar *stock_id;
- gchar *label;
- gchar *accelerator;
- gchar *tooltip;
-
- GCallback callback;
+ gchar *name;
+ gchar *stock_id;
+ gchar *label;
+ gchar *accelerator;
+ gchar *tooltip;
+ GCallback callback;
+};
- gboolean is_toggle;
+struct _GtkToggleActionEntry
+{
+ gchar *name;
+ gchar *stock_id;
+ gchar *label;
+ gchar *accelerator;
+ gchar *tooltip;
+ GCallback callback;
+ gboolean is_active;
};
struct _GtkRadioActionEntry
@@ -89,50 +98,56 @@ struct _GtkRadioActionEntry
gchar *label;
gchar *accelerator;
gchar *tooltip;
-
- gint value;
+ gint value;
};
-GType gtk_action_group_get_type (void);
-
-GtkActionGroup *gtk_action_group_new (const gchar *name);
-
-const gchar *gtk_action_group_get_name (GtkActionGroup *action_group);
-GtkAction *gtk_action_group_get_action (GtkActionGroup *action_group,
- const gchar *action_name);
-GList *gtk_action_group_list_actions (GtkActionGroup *action_group);
-void gtk_action_group_add_action (GtkActionGroup *action_group,
- GtkAction *action);
-void gtk_action_group_remove_action (GtkActionGroup *action_group,
- GtkAction *action);
-
-void gtk_action_group_add_actions (GtkActionGroup *action_group,
- GtkActionEntry *entries,
- guint n_entries,
- gpointer user_data);
-void gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
- GtkRadioActionEntry *entries,
- guint n_entries,
- GCallback on_change,
- gpointer user_data);
-void gtk_action_group_add_actions_full (GtkActionGroup *action_group,
- GtkActionEntry *entries,
- guint n_entries,
- gpointer user_data,
- GDestroyNotify destroy);
-void gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
- GtkRadioActionEntry *entries,
- guint n_entries,
- GCallback on_change,
- gpointer user_data,
- GDestroyNotify destroy);
-
-void gtk_action_group_set_translate_func (GtkActionGroup *action_group,
- GtkTranslateFunc func,
- gpointer data,
- GtkDestroyNotify notify);
-
-void gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
- const gchar *domain);
+GType gtk_action_group_get_type (void);
+GtkActionGroup *gtk_action_group_new (const gchar *name);
+const gchar *gtk_action_group_get_name (GtkActionGroup *action_group);
+GtkAction *gtk_action_group_get_action (GtkActionGroup *action_group,
+ const gchar *action_name);
+GList *gtk_action_group_list_actions (GtkActionGroup *action_group);
+void gtk_action_group_add_action (GtkActionGroup *action_group,
+ GtkAction *action);
+void gtk_action_group_remove_action (GtkActionGroup *action_group,
+ GtkAction *action);
+void gtk_action_group_add_actions (GtkActionGroup *action_group,
+ GtkActionEntry *entries,
+ guint n_entries,
+ gpointer user_data);
+void gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
+ GtkToggleActionEntry *entries,
+ guint n_entries,
+ gpointer user_data);
+void gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
+ GtkRadioActionEntry *entries,
+ guint n_entries,
+ gint value,
+ GCallback on_change,
+ gpointer user_data);
+void gtk_action_group_add_actions_full (GtkActionGroup *action_group,
+ GtkActionEntry *entries,
+ guint n_entries,
+ gpointer user_data,
+ GDestroyNotify destroy);
+void gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
+ GtkToggleActionEntry *entries,
+ guint n_entries,
+ gpointer user_data,
+ GDestroyNotify destroy);
+void gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
+ GtkRadioActionEntry *entries,
+ guint n_entries,
+ gint value,
+ GCallback on_change,
+ gpointer user_data,
+ GDestroyNotify destroy);
+void gtk_action_group_set_translate_func (GtkActionGroup *action_group,
+ GtkTranslateFunc func,
+ gpointer data,
+ GtkDestroyNotify notify);
+void gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
+ const gchar *domain);
+
#endif /* __GTK_ACTION_GROUP_H__ */