1/*
2 * Copyright (c) 2020 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19#include <glib/gi18n-lib.h>
20
21#include "shortcuts.h"
22#include "gtklabel.h"
23#include "gtksizegroup.h"
24#include "gtkstack.h"
25#include "gtkshortcut.h"
26#include "gtkshortcuttrigger.h"
27#include "gtkshortcutcontroller.h"
28#include "gtksignallistitemfactory.h"
29#include "gtklistitem.h"
30#include "gtkcolumnview.h"
31#include "gtkcolumnviewcolumn.h"
32#include "gtkscrolledwindow.h"
33#include "gtknoselection.h"
34#include "gtkbinlayout.h"
35
36
37struct _GtkInspectorShortcuts
38{
39 GtkWidget parent;
40
41 GtkWidget *view;
42};
43
44G_DEFINE_TYPE (GtkInspectorShortcuts, gtk_inspector_shortcuts, GTK_TYPE_WIDGET)
45
46static void
47setup_row (GtkSignalListItemFactory *factory,
48 GtkListItem *list_item,
49 gpointer data)
50{
51 GtkWidget *label;
52
53 label = gtk_label_new (str: "");
54 gtk_label_set_xalign (GTK_LABEL (label), xalign: 0);
55 gtk_list_item_set_child (self: list_item, child: label);
56}
57
58static void
59bind_trigger (GtkSignalListItemFactory *factory,
60 GtkListItem *list_item,
61 gpointer data)
62{
63 GtkWidget *label;
64 GtkShortcut *shortcut;
65 GtkShortcutTrigger *trigger;
66 char *str;
67
68 label = gtk_list_item_get_child (self: list_item);
69 shortcut = gtk_list_item_get_item (self: list_item);
70 trigger = gtk_shortcut_get_trigger (self: shortcut);
71 str = gtk_shortcut_trigger_to_label (self: trigger, display: gtk_widget_get_display (widget: label));
72 gtk_label_set_label (GTK_LABEL (label), str);
73 g_free (mem: str);
74}
75
76static void
77bind_action (GtkSignalListItemFactory *factory,
78 GtkListItem *list_item,
79 gpointer data)
80{
81 GtkWidget *label;
82 GtkShortcut *shortcut;
83 GtkShortcutAction *action;
84 char *str;
85
86 label = gtk_list_item_get_child (self: list_item);
87 shortcut = gtk_list_item_get_item (self: list_item);
88 action = gtk_shortcut_get_action (self: shortcut);
89 str = gtk_shortcut_action_to_string (self: action);
90 gtk_label_set_label (GTK_LABEL (label), str);
91 g_free (mem: str);
92}
93
94static void
95gtk_inspector_shortcuts_init (GtkInspectorShortcuts *self)
96{
97 GtkWidget *sw;
98 GtkListItemFactory *factory;
99 GtkColumnViewColumn *column;
100
101 sw = gtk_scrolled_window_new ();
102
103 self->view = gtk_column_view_new (NULL);
104
105 factory = gtk_signal_list_item_factory_new ();
106 g_signal_connect (factory, "setup", G_CALLBACK (setup_row), NULL);
107 g_signal_connect (factory, "bind", G_CALLBACK (bind_trigger), NULL);
108
109 column = gtk_column_view_column_new (title: "Trigger", factory);
110 gtk_column_view_append_column (GTK_COLUMN_VIEW (self->view), column);
111 g_object_unref (object: column);
112
113 factory = gtk_signal_list_item_factory_new ();
114 g_signal_connect (factory, "setup", G_CALLBACK (setup_row), NULL);
115 g_signal_connect (factory, "bind", G_CALLBACK (bind_action), NULL);
116
117 column = gtk_column_view_column_new (title: "Action", factory);
118 gtk_column_view_append_column (GTK_COLUMN_VIEW (self->view), column);
119 g_object_unref (object: column);
120
121 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: self->view);
122
123 gtk_widget_set_parent (widget: sw, GTK_WIDGET (self));
124}
125
126void
127gtk_inspector_shortcuts_set_object (GtkInspectorShortcuts *self,
128 GObject *object)
129{
130 GtkWidget *stack;
131 GtkStackPage *page;
132 GtkNoSelection *no_selection;
133
134 stack = gtk_widget_get_parent (GTK_WIDGET (self));
135 page = gtk_stack_get_page (GTK_STACK (stack), GTK_WIDGET (self));
136
137 if (!GTK_IS_SHORTCUT_CONTROLLER (object))
138 {
139 gtk_column_view_set_model (GTK_COLUMN_VIEW (self->view), NULL);
140 g_object_set (object: page, first_property_name: "visible", FALSE, NULL);
141 return;
142 }
143
144 g_object_set (object: page, first_property_name: "visible", TRUE, NULL);
145
146 no_selection = gtk_no_selection_new (g_object_ref (G_LIST_MODEL (object)));
147 gtk_column_view_set_model (GTK_COLUMN_VIEW (self->view), model: GTK_SELECTION_MODEL (ptr: no_selection));
148 g_object_unref (object: no_selection);
149}
150
151static void
152dispose (GObject *object)
153{
154 GtkInspectorShortcuts *self = GTK_INSPECTOR_SHORTCUTS (ptr: object);
155
156 gtk_widget_unparent (widget: gtk_widget_get_first_child (GTK_WIDGET (self)));
157
158 G_OBJECT_CLASS (gtk_inspector_shortcuts_parent_class)->dispose (object);
159}
160
161static void
162gtk_inspector_shortcuts_class_init (GtkInspectorShortcutsClass *klass)
163{
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
166
167 object_class->dispose = dispose;
168
169 gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
170}
171

source code of gtk/gtk/inspector/shortcuts.c