2011-10-30 16:35:12 +00:00
|
|
|
/* testgmenu.c
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
* Written by Matthias Clasen
|
|
|
|
*
|
|
|
|
* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-30 16:35:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2022-10-08 00:59:10 +00:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
|
2011-10-31 12:28:05 +00:00
|
|
|
/* TODO
|
|
|
|
*
|
2011-11-06 05:14:33 +00:00
|
|
|
* - Labeled sections
|
2011-10-31 12:28:05 +00:00
|
|
|
*
|
2011-11-06 06:17:15 +00:00
|
|
|
* - Focus changes. Verify that stopping subscriptions works.
|
2011-10-31 12:28:05 +00:00
|
|
|
*
|
|
|
|
* - Other attributes. What about icons ?
|
|
|
|
*/
|
|
|
|
|
2011-10-30 16:35:12 +00:00
|
|
|
/* The example menu {{{1 */
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
static const char menu_markup[] =
|
2012-01-26 01:56:06 +00:00
|
|
|
"<interface>\n"
|
2011-10-30 16:35:12 +00:00
|
|
|
"<menu id='edit-menu'>\n"
|
|
|
|
" <section>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <item>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.undo</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <attribute name='label' translatable='yes' context='Stock label'>_Undo</attribute>\n"
|
|
|
|
" </item>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Redo</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.redo</attribute>\n"
|
2011-11-08 21:28:22 +00:00
|
|
|
" </item>\n"
|
2011-10-30 16:35:12 +00:00
|
|
|
" </section>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <section/>\n"
|
|
|
|
" <section>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Copy & Paste</attribute>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Cut</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.cut</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" </item>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Copy</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.copy</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" </item>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Paste</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.paste</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" </item>\n"
|
2011-10-30 16:35:12 +00:00
|
|
|
" </section>\n"
|
|
|
|
" <section>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Bold</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.bold</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" </item>\n"
|
2016-06-01 13:58:22 +00:00
|
|
|
" <section id=\"size-placeholder\">\n"
|
|
|
|
" <attribute name=\"label\">Size</attribute>"
|
|
|
|
" </section>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <submenu>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Language</attribute>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Latin</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.lang</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <attribute name='target'>latin</attribute>\n"
|
|
|
|
" </item>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Greek</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.lang</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <attribute name='target'>greek</attribute>\n"
|
|
|
|
" </item>\n"
|
|
|
|
" <item>\n"
|
|
|
|
" <attribute name='label' translatable='yes'>Urdu</attribute>\n"
|
2015-07-06 13:40:48 +00:00
|
|
|
" <attribute name='action'>actions.lang</attribute>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
" <attribute name='target'>urdu</attribute>\n"
|
|
|
|
" </item>\n"
|
2011-10-30 16:35:12 +00:00
|
|
|
" </submenu>\n"
|
|
|
|
" </section>\n"
|
2012-01-26 01:56:06 +00:00
|
|
|
"</menu>\n"
|
|
|
|
"</interface>\n";
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
static GMenuModel *
|
|
|
|
get_model (void)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2012-01-26 01:56:06 +00:00
|
|
|
GtkBuilder *builder;
|
2016-06-01 13:58:22 +00:00
|
|
|
GMenuModel *menu, *section;
|
|
|
|
float i;
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2012-01-26 01:56:06 +00:00
|
|
|
builder = gtk_builder_new ();
|
|
|
|
gtk_builder_add_from_string (builder, menu_markup, -1, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
2017-12-09 12:14:00 +00:00
|
|
|
menu = G_MENU_MODEL (g_object_ref (gtk_builder_get_object (builder, "edit-menu")));
|
2016-06-01 13:58:22 +00:00
|
|
|
|
2017-12-09 12:14:00 +00:00
|
|
|
section = G_MENU_MODEL (g_object_ref (gtk_builder_get_object (builder, "size-placeholder")));
|
2012-01-26 01:56:06 +00:00
|
|
|
g_object_unref (builder);
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2016-06-01 13:58:22 +00:00
|
|
|
for (i = 0.5; i <= 2.0; i += 0.5)
|
|
|
|
{
|
|
|
|
GMenuItem *item;
|
|
|
|
char *target;
|
|
|
|
char *label;
|
|
|
|
|
|
|
|
target = g_strdup_printf ("actions.size::%.1f", i);
|
|
|
|
label = g_strdup_printf ("x %.1f", i);
|
|
|
|
item = g_menu_item_new (label, target);
|
|
|
|
g_menu_append_item (G_MENU (section), item);
|
|
|
|
g_free (label);
|
|
|
|
g_free (target);
|
|
|
|
}
|
|
|
|
|
2012-01-26 01:56:06 +00:00
|
|
|
return menu;
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:56:06 +00:00
|
|
|
/* The example actions {{{1 */
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
activate_action (GSimpleAction *action, GVariant *parameter, gpointer user_data)
|
|
|
|
{
|
|
|
|
g_print ("Action %s activated\n", g_action_get_name (G_ACTION (action)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-26 16:56:42 +00:00
|
|
|
activate_toggle (GSimpleAction *action, GVariant *parameter, gpointer user_data)
|
2011-10-30 16:35:12 +00:00
|
|
|
{
|
2011-11-26 16:56:42 +00:00
|
|
|
GVariant *old_state, *new_state;
|
|
|
|
|
|
|
|
old_state = g_action_get_state (G_ACTION (action));
|
|
|
|
new_state = g_variant_new_boolean (!g_variant_get_boolean (old_state));
|
|
|
|
|
|
|
|
g_print ("Toggle action %s activated, state changes from %d to %d\n",
|
2011-10-30 16:35:12 +00:00
|
|
|
g_action_get_name (G_ACTION (action)),
|
2011-11-26 16:56:42 +00:00
|
|
|
g_variant_get_boolean (old_state),
|
|
|
|
g_variant_get_boolean (new_state));
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2011-11-26 16:56:42 +00:00
|
|
|
g_simple_action_set_state (action, new_state);
|
|
|
|
g_variant_unref (old_state);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-26 17:39:05 +00:00
|
|
|
activate_radio (GSimpleAction *action, GVariant *parameter, gpointer user_data)
|
2011-10-30 16:35:12 +00:00
|
|
|
{
|
2011-11-26 17:39:05 +00:00
|
|
|
GVariant *old_state, *new_state;
|
|
|
|
|
|
|
|
old_state = g_action_get_state (G_ACTION (action));
|
|
|
|
new_state = g_variant_new_string (g_variant_get_string (parameter, NULL));
|
|
|
|
|
|
|
|
g_print ("Radio action %s activated, state changes from %s to %s\n",
|
2011-10-30 16:35:12 +00:00
|
|
|
g_action_get_name (G_ACTION (action)),
|
2011-11-26 17:39:05 +00:00
|
|
|
g_variant_get_string (old_state, NULL),
|
|
|
|
g_variant_get_string (new_state, NULL));
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2011-11-26 17:39:05 +00:00
|
|
|
g_simple_action_set_state (action, new_state);
|
|
|
|
g_variant_unref (old_state);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GActionEntry actions[] = {
|
|
|
|
{ "undo", activate_action, NULL, NULL, NULL },
|
|
|
|
{ "redo", activate_action, NULL, NULL, NULL },
|
|
|
|
{ "cut", activate_action, NULL, NULL, NULL },
|
|
|
|
{ "copy", activate_action, NULL, NULL, NULL },
|
|
|
|
{ "paste", activate_action, NULL, NULL, NULL },
|
2011-11-26 16:56:42 +00:00
|
|
|
{ "bold", activate_toggle, NULL, "true", NULL },
|
2011-11-26 17:39:05 +00:00
|
|
|
{ "lang", activate_radio, "s", "'latin'", NULL },
|
2011-10-30 16:35:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GActionGroup *
|
|
|
|
get_group (void)
|
|
|
|
{
|
|
|
|
GSimpleActionGroup *group;
|
|
|
|
|
|
|
|
group = g_simple_action_group_new ();
|
|
|
|
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_add_action_entries (G_ACTION_MAP (group), actions, G_N_ELEMENTS (actions), NULL);
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
return G_ACTION_GROUP (group);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The action treeview {{{1 */
|
|
|
|
|
|
|
|
static void
|
|
|
|
enabled_cell_func (GtkTreeViewColumn *column,
|
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GActionGroup *group = data;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2011-10-30 16:35:12 +00:00
|
|
|
gboolean enabled;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, 0, &name, -1);
|
|
|
|
enabled = g_action_group_get_action_enabled (group, name);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell), enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_cell_func (GtkTreeViewColumn *column,
|
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GActionGroup *group = data;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2011-10-30 16:35:12 +00:00
|
|
|
GVariant *state;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, 0, &name, -1);
|
|
|
|
state = g_action_group_get_action_state (group, name);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
gtk_cell_renderer_set_visible (cell, FALSE);
|
|
|
|
g_object_set (cell, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
|
|
|
|
|
2011-11-26 17:39:05 +00:00
|
|
|
if (state == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN) &&
|
2011-10-30 16:35:12 +00:00
|
|
|
GTK_IS_CELL_RENDERER_TOGGLE (cell))
|
|
|
|
{
|
|
|
|
gtk_cell_renderer_set_visible (cell, TRUE);
|
|
|
|
g_object_set (cell, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
|
|
|
|
gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell),
|
|
|
|
g_variant_get_boolean (state));
|
|
|
|
}
|
2011-11-26 17:39:05 +00:00
|
|
|
else if (g_variant_is_of_type (state, G_VARIANT_TYPE_STRING) &&
|
2011-10-30 16:35:12 +00:00
|
|
|
GTK_IS_CELL_RENDERER_COMBO (cell))
|
|
|
|
{
|
|
|
|
gtk_cell_renderer_set_visible (cell, TRUE);
|
|
|
|
g_object_set (cell, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL);
|
|
|
|
g_object_set (cell, "text", g_variant_get_string (state, NULL), NULL);
|
|
|
|
}
|
|
|
|
|
2011-11-26 17:39:05 +00:00
|
|
|
g_variant_unref (state);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enabled_cell_toggled (GtkCellRendererToggle *cell,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path_str,
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreeModel *model)
|
|
|
|
{
|
|
|
|
GActionGroup *group;
|
|
|
|
GAction *action;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreePath *path;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean enabled;
|
|
|
|
|
|
|
|
group = g_object_get_data (G_OBJECT (model), "group");
|
|
|
|
path = gtk_tree_path_new_from_string (path_str);
|
|
|
|
gtk_tree_model_get_iter (model, &iter, path);
|
|
|
|
gtk_tree_model_get (model, &iter, 0, &name, -1);
|
|
|
|
|
|
|
|
enabled = g_action_group_get_action_enabled (group, name);
|
2013-09-05 22:50:23 +00:00
|
|
|
action = g_action_map_lookup_action (G_ACTION_MAP (group), name);
|
2011-10-30 16:35:12 +00:00
|
|
|
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), !enabled);
|
|
|
|
|
|
|
|
gtk_tree_model_row_changed (model, path, &iter);
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_cell_toggled (GtkCellRendererToggle *cell,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path_str,
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreeModel *model)
|
|
|
|
{
|
|
|
|
GActionGroup *group;
|
|
|
|
GAction *action;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreePath *path;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GVariant *state;
|
|
|
|
|
|
|
|
group = g_object_get_data (G_OBJECT (model), "group");
|
|
|
|
path = gtk_tree_path_new_from_string (path_str);
|
|
|
|
gtk_tree_model_get_iter (model, &iter, path);
|
|
|
|
gtk_tree_model_get (model, &iter, 0, &name, -1);
|
|
|
|
|
|
|
|
state = g_action_group_get_action_state (group, name);
|
2013-09-05 22:50:23 +00:00
|
|
|
action = g_action_map_lookup_action (G_ACTION_MAP (group), name);
|
2011-10-30 16:35:12 +00:00
|
|
|
if (state && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
|
|
|
|
{
|
|
|
|
gboolean b;
|
|
|
|
|
|
|
|
b = g_variant_get_boolean (state);
|
|
|
|
g_simple_action_set_state (G_SIMPLE_ACTION (action), g_variant_new_boolean (!b));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_model_row_changed (model, path, &iter);
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
if (state)
|
|
|
|
g_variant_unref (state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_cell_edited (GtkCellRendererCombo *cell,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path_str,
|
|
|
|
const char *new_text,
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreeModel *model)
|
|
|
|
{
|
|
|
|
GActionGroup *group;
|
|
|
|
GAction *action;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2011-10-30 16:35:12 +00:00
|
|
|
GtkTreePath *path;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
group = g_object_get_data (G_OBJECT (model), "group");
|
|
|
|
path = gtk_tree_path_new_from_string (path_str);
|
|
|
|
gtk_tree_model_get_iter (model, &iter, path);
|
|
|
|
gtk_tree_model_get (model, &iter, 0, &name, -1);
|
2013-09-05 22:50:23 +00:00
|
|
|
action = g_action_map_lookup_action (G_ACTION_MAP (group), name);
|
2011-10-30 16:35:12 +00:00
|
|
|
g_simple_action_set_state (G_SIMPLE_ACTION (action), g_variant_new_string (new_text));
|
|
|
|
|
|
|
|
gtk_tree_model_row_changed (model, path, &iter);
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
create_action_treeview (GActionGroup *group)
|
|
|
|
{
|
|
|
|
GtkWidget *tv;
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkListStore *values;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeViewColumn *column;
|
|
|
|
GtkCellRenderer *cell;
|
2020-07-24 18:40:36 +00:00
|
|
|
char **group_actions;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
|
2020-03-06 17:05:43 +00:00
|
|
|
group_actions = g_action_group_list_actions (group);
|
|
|
|
for (i = 0; group_actions[i]; i++)
|
2011-10-30 16:35:12 +00:00
|
|
|
{
|
|
|
|
gtk_list_store_append (store, &iter);
|
2020-03-06 17:05:43 +00:00
|
|
|
gtk_list_store_set (store, &iter, 0, group_actions[i], -1);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
2020-03-06 17:05:43 +00:00
|
|
|
g_strfreev (group_actions);
|
2011-10-30 16:35:12 +00:00
|
|
|
g_object_set_data (G_OBJECT (store), "group", group);
|
|
|
|
|
|
|
|
tv = gtk_tree_view_new ();
|
|
|
|
|
|
|
|
g_signal_connect_swapped (group, "action-enabled-changed",
|
|
|
|
G_CALLBACK (gtk_widget_queue_draw), tv);
|
|
|
|
g_signal_connect_swapped (group, "action-state-changed",
|
|
|
|
G_CALLBACK (gtk_widget_queue_draw), tv);
|
|
|
|
|
|
|
|
gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
|
|
|
|
|
|
|
|
cell = gtk_cell_renderer_text_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Action", cell,
|
|
|
|
"text", 0,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);
|
|
|
|
|
|
|
|
column = gtk_tree_view_column_new ();
|
|
|
|
gtk_tree_view_column_set_title (column, "Enabled");
|
|
|
|
cell = gtk_cell_renderer_toggle_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, cell, FALSE);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (column, cell, enabled_cell_func, group, NULL);
|
|
|
|
g_signal_connect (cell, "toggled", G_CALLBACK (enabled_cell_toggled), store);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);
|
|
|
|
|
|
|
|
column = gtk_tree_view_column_new ();
|
|
|
|
gtk_tree_view_column_set_title (column, "State");
|
|
|
|
cell = gtk_cell_renderer_toggle_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, cell, FALSE);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (column, cell, state_cell_func, group, NULL);
|
|
|
|
g_signal_connect (cell, "toggled", G_CALLBACK (state_cell_toggled), store);
|
|
|
|
cell = gtk_cell_renderer_combo_new ();
|
|
|
|
values = gtk_list_store_new (1, G_TYPE_STRING);
|
|
|
|
gtk_list_store_append (values, &iter);
|
|
|
|
gtk_list_store_set (values, &iter, 0, "latin", -1);
|
|
|
|
gtk_list_store_append (values, &iter);
|
|
|
|
gtk_list_store_set (values, &iter, 0, "greek", -1);
|
|
|
|
gtk_list_store_append (values, &iter);
|
|
|
|
gtk_list_store_set (values, &iter, 0, "urdu", -1);
|
2011-11-01 04:30:31 +00:00
|
|
|
gtk_list_store_append (values, &iter);
|
|
|
|
gtk_list_store_set (values, &iter, 0, "sumerian", -1);
|
2011-10-30 16:35:12 +00:00
|
|
|
g_object_set (cell,
|
|
|
|
"has-entry", FALSE,
|
|
|
|
"model", values,
|
|
|
|
"text-column", 0,
|
|
|
|
"editable", TRUE,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_column_pack_start (column, cell, FALSE);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (column, cell, state_cell_func, group, NULL);
|
|
|
|
g_signal_connect (cell, "edited", G_CALLBACK (state_cell_edited), store);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);
|
|
|
|
|
|
|
|
return tv;
|
|
|
|
}
|
|
|
|
|
2011-11-01 04:30:31 +00:00
|
|
|
/* Dynamic menu changes {{{1 */
|
|
|
|
|
|
|
|
static void
|
2021-06-10 11:49:08 +00:00
|
|
|
toggle_sumerian (GtkCheckButton *button, gpointer data)
|
2011-11-01 04:30:31 +00:00
|
|
|
{
|
|
|
|
GMenuModel *model;
|
|
|
|
gboolean adding;
|
|
|
|
GMenuModel *m;
|
|
|
|
|
|
|
|
model = g_object_get_data (G_OBJECT (button), "model");
|
|
|
|
|
2021-06-10 11:49:08 +00:00
|
|
|
adding = gtk_check_button_get_active (button);
|
2011-11-01 04:30:31 +00:00
|
|
|
|
|
|
|
m = g_menu_model_get_item_link (model, g_menu_model_get_n_items (model) - 1, G_MENU_LINK_SECTION);
|
|
|
|
m = g_menu_model_get_item_link (m, g_menu_model_get_n_items (m) - 1, G_MENU_LINK_SUBMENU);
|
|
|
|
if (adding)
|
|
|
|
g_menu_append (G_MENU (m), "Sumerian", "lang::sumerian");
|
|
|
|
else
|
|
|
|
g_menu_remove (G_MENU (m), g_menu_model_get_n_items (m) - 1);
|
|
|
|
}
|
|
|
|
|
2011-11-06 06:17:15 +00:00
|
|
|
static void
|
|
|
|
action_list_add (GtkTreeModel *store,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *action)
|
2011-11-06 06:17:15 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
gtk_list_store_append (GTK_LIST_STORE (store), &iter);
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, action, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
action_list_remove (GtkTreeModel *store,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *action)
|
2011-11-06 06:17:15 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2011-11-06 06:17:15 +00:00
|
|
|
|
|
|
|
gtk_tree_model_get_iter_first (store, &iter);
|
|
|
|
do {
|
|
|
|
gtk_tree_model_get (store, &iter, 0, &text, -1);
|
|
|
|
if (g_strcmp0 (action, text) == 0)
|
|
|
|
{
|
|
|
|
g_free (text);
|
|
|
|
gtk_list_store_remove (GTK_LIST_STORE (store), &iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_free (text);
|
|
|
|
} while (gtk_tree_model_iter_next (store, &iter));
|
|
|
|
}
|
|
|
|
|
2011-11-01 04:30:31 +00:00
|
|
|
static void
|
2021-06-10 11:49:08 +00:00
|
|
|
toggle_italic (GtkCheckButton *button, gpointer data)
|
2011-11-01 04:30:31 +00:00
|
|
|
{
|
|
|
|
GMenuModel *model;
|
|
|
|
GActionGroup *group;
|
|
|
|
GSimpleAction *action;
|
|
|
|
gboolean adding;
|
|
|
|
GMenuModel *m;
|
|
|
|
GtkTreeView *tv = data;
|
|
|
|
GtkTreeModel *store;
|
|
|
|
|
|
|
|
model = g_object_get_data (G_OBJECT (button), "model");
|
|
|
|
group = g_object_get_data (G_OBJECT (button), "group");
|
|
|
|
|
|
|
|
store = gtk_tree_view_get_model (tv);
|
|
|
|
|
2021-06-10 11:49:08 +00:00
|
|
|
adding = gtk_check_button_get_active (button);
|
2011-11-01 04:30:31 +00:00
|
|
|
|
|
|
|
m = g_menu_model_get_item_link (model, g_menu_model_get_n_items (model) - 1, G_MENU_LINK_SECTION);
|
|
|
|
if (adding)
|
|
|
|
{
|
|
|
|
action = g_simple_action_new_stateful ("italic", NULL, g_variant_new_boolean (FALSE));
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action));
|
2011-11-26 16:56:42 +00:00
|
|
|
g_signal_connect (action, "activate", G_CALLBACK (activate_toggle), NULL);
|
2011-11-01 04:30:31 +00:00
|
|
|
g_object_unref (action);
|
2011-11-06 06:17:15 +00:00
|
|
|
action_list_add (store, "italic");
|
2011-11-01 04:30:31 +00:00
|
|
|
g_menu_insert (G_MENU (m), 1, "Italic", "italic");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_remove_action (G_ACTION_MAP (group), "italic");
|
2011-11-06 06:17:15 +00:00
|
|
|
action_list_remove (store, "italic");
|
2011-11-01 04:30:31 +00:00
|
|
|
g_menu_remove (G_MENU (m), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 06:17:15 +00:00
|
|
|
static void
|
2021-06-10 11:49:08 +00:00
|
|
|
toggle_speed (GtkCheckButton *button, gpointer data)
|
2011-11-06 06:17:15 +00:00
|
|
|
{
|
|
|
|
GMenuModel *model;
|
|
|
|
GActionGroup *group;
|
|
|
|
GSimpleAction *action;
|
|
|
|
gboolean adding;
|
|
|
|
GMenuModel *m;
|
|
|
|
GMenu *submenu;
|
|
|
|
GtkTreeView *tv = data;
|
|
|
|
GtkTreeModel *store;
|
|
|
|
|
|
|
|
model = g_object_get_data (G_OBJECT (button), "model");
|
|
|
|
group = g_object_get_data (G_OBJECT (button), "group");
|
|
|
|
|
|
|
|
store = gtk_tree_view_get_model (tv);
|
|
|
|
|
2021-06-10 11:49:08 +00:00
|
|
|
adding = gtk_check_button_get_active (button);
|
2011-11-06 06:17:15 +00:00
|
|
|
|
|
|
|
m = g_menu_model_get_item_link (model, 1, G_MENU_LINK_SECTION);
|
|
|
|
if (adding)
|
|
|
|
{
|
|
|
|
action = g_simple_action_new ("faster", NULL);
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action));
|
2011-11-06 06:17:15 +00:00
|
|
|
g_signal_connect (action, "activate", G_CALLBACK (activate_action), NULL);
|
|
|
|
g_object_unref (action);
|
|
|
|
|
|
|
|
action = g_simple_action_new ("slower", NULL);
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action));
|
2011-11-06 06:17:15 +00:00
|
|
|
g_signal_connect (action, "activate", G_CALLBACK (activate_action), NULL);
|
|
|
|
g_object_unref (action);
|
|
|
|
|
|
|
|
action_list_add (store, "faster");
|
|
|
|
action_list_add (store, "slower");
|
|
|
|
|
|
|
|
submenu = g_menu_new ();
|
|
|
|
g_menu_append (submenu, "Faster", "faster");
|
|
|
|
g_menu_append (submenu, "Slower", "slower");
|
|
|
|
g_menu_append_submenu (G_MENU (m), "Speed", G_MENU_MODEL (submenu));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-05 22:50:23 +00:00
|
|
|
g_action_map_remove_action (G_ACTION_MAP (group), "faster");
|
|
|
|
g_action_map_remove_action (G_ACTION_MAP (group), "slower");
|
2011-11-06 06:17:15 +00:00
|
|
|
|
|
|
|
action_list_remove (store, "faster");
|
|
|
|
action_list_remove (store, "slower");
|
|
|
|
|
|
|
|
g_menu_remove (G_MENU (m), g_menu_model_get_n_items (m) - 1);
|
|
|
|
}
|
|
|
|
}
|
2011-11-01 04:30:31 +00:00
|
|
|
static GtkWidget *
|
|
|
|
create_add_remove_buttons (GActionGroup *group,
|
|
|
|
GMenuModel *model,
|
|
|
|
GtkWidget *treeview)
|
|
|
|
{
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
|
|
|
|
|
|
|
|
button = gtk_check_button_new_with_label ("Add Italic");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), button);
|
2011-11-01 04:30:31 +00:00
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (button), "group", group);
|
|
|
|
g_object_set_data (G_OBJECT (button), "model", model);
|
|
|
|
|
|
|
|
g_signal_connect (button, "toggled",
|
|
|
|
G_CALLBACK (toggle_italic), treeview);
|
|
|
|
|
|
|
|
button = gtk_check_button_new_with_label ("Add Sumerian");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), button);
|
2011-11-01 04:30:31 +00:00
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (button), "group", group);
|
|
|
|
g_object_set_data (G_OBJECT (button), "model", model);
|
|
|
|
|
|
|
|
g_signal_connect (button, "toggled",
|
|
|
|
G_CALLBACK (toggle_sumerian), NULL);
|
|
|
|
|
2011-11-06 06:17:15 +00:00
|
|
|
button = gtk_check_button_new_with_label ("Add Speed");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), button);
|
2011-11-06 06:17:15 +00:00
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (button), "group", group);
|
|
|
|
g_object_set_data (G_OBJECT (button), "model", model);
|
|
|
|
|
|
|
|
g_signal_connect (button, "toggled",
|
|
|
|
G_CALLBACK (toggle_speed), treeview);
|
2011-11-01 04:30:31 +00:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2011-11-06 05:14:33 +00:00
|
|
|
/* main {{{1 */
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2011-11-18 02:58:20 +00:00
|
|
|
#define BUS_NAME "org.gtk.TestMenus"
|
|
|
|
#define OBJ_PATH "/org/gtk/TestMenus"
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2011-10-30 16:35:12 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *tv;
|
2011-11-01 04:30:31 +00:00
|
|
|
GtkWidget *buttons;
|
2011-10-30 16:35:12 +00:00
|
|
|
GMenuModel *model;
|
|
|
|
GActionGroup *group;
|
|
|
|
GDBusConnection *bus;
|
|
|
|
GError *error = NULL;
|
|
|
|
gboolean do_export = FALSE;
|
|
|
|
gboolean do_import = FALSE;
|
|
|
|
GOptionEntry entries[] = {
|
|
|
|
{ "export", 0, 0, G_OPTION_ARG_NONE, &do_export, "Export actions and menus over D-Bus", NULL },
|
|
|
|
{ "import", 0, 0, G_OPTION_ARG_NONE, &do_import, "Use exported actions and menus", NULL },
|
|
|
|
{ NULL, }
|
|
|
|
};
|
2016-12-28 13:53:22 +00:00
|
|
|
GOptionContext *context;
|
2020-02-10 03:24:47 +00:00
|
|
|
gboolean done = FALSE;
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
context = g_option_context_new ("");
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
|
|
|
g_option_context_parse (context, &argc, &argv, NULL);
|
|
|
|
gtk_init ();
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
if (do_export && do_import)
|
|
|
|
{
|
|
|
|
g_error ("can't have it both ways\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
|
2011-10-30 16:35:12 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
|
|
|
|
|
|
|
|
if (do_import)
|
|
|
|
{
|
|
|
|
g_print ("Getting menus from the bus...\n");
|
2011-12-09 16:34:39 +00:00
|
|
|
model = (GMenuModel*)g_dbus_menu_model_get (bus, BUS_NAME, OBJ_PATH);
|
2011-10-30 16:35:12 +00:00
|
|
|
g_print ("Getting actions from the bus...\n");
|
2011-12-09 16:34:39 +00:00
|
|
|
group = (GActionGroup*)g_dbus_action_group_get (bus, BUS_NAME, OBJ_PATH);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
group = get_group ();
|
|
|
|
model = get_model ();
|
|
|
|
|
|
|
|
tv = create_action_treeview (group);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), tv);
|
2011-11-01 04:30:31 +00:00
|
|
|
buttons = create_add_remove_buttons (group, model, tv);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), buttons);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_export)
|
|
|
|
{
|
|
|
|
g_print ("Exporting menus on the bus...\n");
|
2011-12-02 21:10:31 +00:00
|
|
|
if (!g_dbus_connection_export_menu_model (bus, OBJ_PATH, model, &error))
|
2011-10-30 16:35:12 +00:00
|
|
|
{
|
|
|
|
g_warning ("Menu export failed: %s", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
g_print ("Exporting actions on the bus...\n");
|
2011-12-02 21:10:31 +00:00
|
|
|
if (!g_dbus_connection_export_action_group (bus, OBJ_PATH, group, &error))
|
2011-10-30 16:35:12 +00:00
|
|
|
{
|
|
|
|
g_warning ("Action export failed: %s", error->message);
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-11-18 02:58:20 +00:00
|
|
|
g_bus_own_name_on_connection (bus, BUS_NAME, 0, NULL, NULL, NULL, NULL);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-06 13:40:48 +00:00
|
|
|
button = gtk_menu_button_new ();
|
2020-05-10 23:58:36 +00:00
|
|
|
gtk_menu_button_set_label (GTK_MENU_BUTTON (button), "Click here");
|
2015-07-06 13:40:48 +00:00
|
|
|
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), model);
|
|
|
|
gtk_widget_insert_action_group (button, "actions", group);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), button);
|
2011-10-30 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2011-10-30 16:35:12 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2011-10-30 16:35:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Epilogue {{{1 */
|
|
|
|
/* vim:set foldmethod=marker: */
|