Package list xapp / 386ca8f
Add a base preferences dialog (#31) This gives us a basic reusable dialog window we can use in our apps. It supplies a simple setup with a GtkStack, StackSidebar, and ActionBar to hold buttons at the bottom and allows closing with the ESC key. JosephMcc authored 3 years ago Clement Lefebvre committed 3 years ago
4 changed file(s) with 226 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1818 <xi:include href="xml/xapp-kbd-layout-controller.xml"/>
1919 <xi:include href="xml/xapp-monitor-blanker.xml"/>
2020 <xi:include href="xml/xapp-gtk-window.xml"/>
21 <xi:include href="xml/xapp-preferences-window.xml"/>
2122
2223 </chapter>
2324 <chapter id="object-tree">
1717 xapp-monitor-blanker.c \
1818 xapp-kbd-layout-controller.c \
1919 xapp-gtk-window.c \
20 xapp-preferences-window.c \
2021 xapp-glade-catalog.c
2122
2223 libxapp_la_SOURCES = \
4344 libxapp_HEADERS = \
4445 xapp-monitor-blanker.h \
4546 xapp-kbd-layout-controller.h \
46 xapp-gtk-window.h
47 xapp-gtk-window.h \
48 xapp-preferences-window.h
4749
4850 -include $(INTROSPECTION_MAKEFILE)
4951 INTROSPECTION_GIRS =
0 #include <gdk/gdk.h>
1 #include "xapp-preferences-window.h"
2
3 /**
4 * SECTION:xapp-preferences-window
5 * @Short_description: A base preferences window
6 * @Title: XAppPreferencesWindow
7 *
8 * The XAppPreferencesWindow sets up a simple dialog
9 * window with a GtkStack, GtkSidebarSwitcher, and
10 * GtkActionBar. The stack switcher and action bar only
11 * show when needed.
12 */
13
14 typedef struct
15 {
16 GtkWidget *stack;
17 GtkWidget *side_switcher;
18 GtkWidget *button_area;
19 GtkSizeGroup *button_size_group;
20
21 gint num_pages;
22 } XAppPreferencesWindowPrivate;
23
24 enum
25 {
26 CLOSE,
27 LAST_SIGNAL
28 };
29
30 static guint signals[LAST_SIGNAL] = {0, };
31
32 G_DEFINE_TYPE_WITH_PRIVATE (XAppPreferencesWindow, xapp_preferences_window, GTK_TYPE_WINDOW)
33
34 static void
35 xapp_preferences_window_init (XAppPreferencesWindow *window)
36 {
37 XAppPreferencesWindowPrivate *priv = xapp_preferences_window_get_instance_private (window);
38 GtkWidget *main_box;
39 GtkWidget *secondary_box;
40 GtkWidget *separator;
41
42 gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
43 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
44 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DIALOG);
45
46 main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
47 gtk_container_add (GTK_CONTAINER (window), main_box);
48
49 secondary_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
50 gtk_box_pack_start (GTK_BOX (main_box), secondary_box, TRUE, TRUE, 0);
51
52 priv->side_switcher = gtk_stack_sidebar_new ();
53 gtk_widget_set_size_request (priv->side_switcher, 100, -1);
54 gtk_box_pack_start (GTK_BOX (secondary_box), priv->side_switcher, FALSE, FALSE, 0);
55 gtk_widget_set_no_show_all (priv->side_switcher, TRUE);
56
57 separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
58 gtk_box_pack_start (GTK_BOX (secondary_box), separator, FALSE, FALSE, 0);
59
60 priv->stack = gtk_stack_new ();
61 gtk_stack_set_transition_type (GTK_STACK (priv->stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
62 gtk_box_pack_start (GTK_BOX (secondary_box), priv->stack, TRUE, TRUE, 0);
63 gtk_stack_sidebar_set_stack (GTK_STACK_SIDEBAR (priv->side_switcher), GTK_STACK (priv->stack));
64
65 priv->button_area = gtk_action_bar_new ();
66 gtk_box_pack_start (GTK_BOX (main_box), priv->button_area, FALSE, FALSE, 0);
67 gtk_widget_set_no_show_all (priv->button_area, TRUE);
68
69 priv->button_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
70
71 /* Keep track of the number of pages so we can hide the stack switcher with < 2 */
72 priv->num_pages = 0;
73 }
74
75 static void
76 xapp_preferences_window_close (XAppPreferencesWindow *window)
77 {
78 gtk_window_close (GTK_WINDOW (window));
79 }
80
81 static void
82 xapp_preferences_window_class_init (XAppPreferencesWindowClass *klass)
83 {
84 GtkBindingSet *binding_set;
85
86 klass->close = xapp_preferences_window_close;
87
88 signals[CLOSE] =
89 g_signal_new ("close",
90 G_TYPE_FROM_CLASS (klass),
91 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
92 G_STRUCT_OFFSET (XAppPreferencesWindowClass, close),
93 NULL, NULL, NULL,
94 G_TYPE_NONE, 0);
95
96 binding_set = gtk_binding_set_by_class (klass);
97 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
98 }
99
100 /**
101 * xapp_preferences_window_new:
102 *
103 * Creates a new #XAppPreferencesWindow.
104 *
105 * Returns: a newly created #XAppPreferencesWindow
106 */
107 XAppPreferencesWindow *
108 xapp_preferences_window_new (void)
109 {
110 return g_object_new (XAPP_TYPE_PREFERENCES_WINDOW, NULL);
111 }
112
113
114 /**
115 * xapp_preferences_window_add_page:
116 * @window: a #XAppPreferencesWindow
117 * @widget: a #GtkWidget to add
118 * @name: the name for the page
119 * @title: a human-readable title for the page
120 *
121 * Adds a page to the window. The page is identified by name. The
122 * title will be used in the sidebar so should be short. The sidebar
123 * will show automatically once at least two pages are added.
124 */
125 void
126 xapp_preferences_window_add_page (XAppPreferencesWindow *window,
127 GtkWidget *widget,
128 const gchar *name,
129 const gchar *title)
130 {
131 XAppPreferencesWindowPrivate *priv = xapp_preferences_window_get_instance_private (window);
132
133 g_return_if_fail (XAPP_IS_PREFERENCES_WINDOW (window));
134
135 gtk_stack_add_titled (GTK_STACK (priv->stack), widget, name, title);
136
137 priv->num_pages++;
138
139 if (priv->num_pages > 1)
140 {
141 gtk_widget_set_no_show_all (priv->side_switcher, FALSE);
142 }
143 }
144
145 /**
146 * xapp_preferences_window_add_button:
147 * @window: a #XAppPreferencesWindow
148 * @button: a #GtkWidget to add
149 * @pack_type: a #GtkPackType to use
150 *
151 * Adds a button to the bottom action bar of the window. Where
152 * the button is place will be determined by the #GtkPackType. The
153 * action bar will show automatically once at least one button is
154 * added.
155 */
156 void
157 xapp_preferences_window_add_button (XAppPreferencesWindow *window,
158 GtkWidget *button,
159 GtkPackType pack_type)
160 {
161 XAppPreferencesWindowPrivate *priv = xapp_preferences_window_get_instance_private (window);
162 GtkStyleContext *style_context;
163
164 g_return_if_fail (XAPP_IS_PREFERENCES_WINDOW (window));
165 g_return_if_fail (GTK_IS_WIDGET (button));
166
167 if (pack_type == GTK_PACK_START)
168 {
169 gtk_action_bar_pack_start (GTK_ACTION_BAR (priv->button_area), button);
170 gtk_widget_set_margin_start (button, 6);
171 }
172 else if (pack_type == GTK_PACK_END)
173 {
174 gtk_action_bar_pack_end (GTK_ACTION_BAR (priv->button_area), button);
175 gtk_widget_set_margin_end (button, 6);
176 }
177 else
178 {
179 return;
180 }
181
182 style_context = gtk_widget_get_style_context (button);
183 gtk_style_context_add_class (style_context, "text-button");
184
185 gtk_size_group_add_widget (priv->button_size_group, button);
186 gtk_widget_set_no_show_all (priv->button_area, FALSE);
187 }
0 #ifndef _XAPP_PREFERENCES_WINDOW_H_
1 #define _XAPP_PREFERENCES_WINDOW_H_
2
3 #include <glib-object.h>
4 #include <gtk/gtk.h>
5
6 G_BEGIN_DECLS
7
8 #define XAPP_TYPE_PREFERENCES_WINDOW (xapp_preferences_window_get_type ())
9
10 G_DECLARE_DERIVABLE_TYPE (XAppPreferencesWindow, xapp_preferences_window, XAPP, PREFERENCES_WINDOW, GtkWindow)
11
12 struct _XAppPreferencesWindowClass
13 {
14 GtkWindowClass parent_class;
15
16 /* Keybinding signals */
17 void (* close) (XAppPreferencesWindow *window);
18 };
19
20 XAppPreferencesWindow *xapp_preferences_window_new (void);
21
22 void xapp_preferences_window_add_page (XAppPreferencesWindow *window,
23 GtkWidget *widget,
24 const gchar *name,
25 const gchar *title);
26
27 void xapp_preferences_window_add_button (XAppPreferencesWindow *window,
28 GtkWidget *button,
29 GtkPackType pack_type);
30
31 G_END_DECLS
32
33 #endif /* _XAPP_PREFERENCES_WINDOW_H_ */