File: test-traditional-ui.c

package info (click to toggle)
libgedit-amtk 5.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 692 kB
  • sloc: ansic: 2,996; xml: 288; makefile: 7
file content (221 lines) | stat: -rw-r--r-- 5,636 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* SPDX-FileCopyrightText: 2017, 2018 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <amtk/amtk.h>

/* This should be stored instead in the Private struct of the app GtkApplication
 * subclass. But here to not make the code too complicated there is no
 * GtkApplication subclass.
 */
static AmtkActionInfoStore *action_info_store = NULL;

static AmtkActionInfoEntry file_action_info_entries[] =
{
	{ "win.open", "document-open", "_Open", "<Control>o",
	  "Open a file" },

	{ "app.quit", "application-exit", "_Quit", "<Control>q",
	  "Quit the application" },

	{ NULL }
};

static void
add_action_info_entries (void)
{
	const AmtkActionInfoEntry entries[] =
	{
		/* action, icon, label, accel, tooltip */

		{ "app.about", "help-about", "_About", NULL,
		  "About this application" },

		{ "win.show-side-panel", NULL, "_Side Panel", "F9",
		  "Toggle side panel visibility" },
	};

	g_assert (action_info_store == NULL);
	action_info_store = amtk_action_info_store_new ();

	amtk_action_info_store_add_entries (action_info_store,
					    entries,
					    G_N_ELEMENTS (entries),
					    NULL);

	amtk_action_info_store_add_entries (action_info_store,
					    file_action_info_entries, -1,
					    NULL);
}

static void
quit_activate_cb (GSimpleAction *action,
		  GVariant      *parameter,
		  gpointer       user_data)
{
	g_application_quit (G_APPLICATION (user_data));
}

static void
about_activate_cb (GSimpleAction *action,
		   GVariant      *parameter,
		   gpointer       user_data)
{
	g_print ("About\n");
}

static void
add_app_action_entries (GApplication *app)
{
	const GActionEntry entries[] =
	{
		{ "quit", quit_activate_cb },
		{ "about", about_activate_cb },
	};

	amtk_action_map_add_action_entries_check_dups (G_ACTION_MAP (app),
						       entries,
						       G_N_ELEMENTS (entries),
						       app);
}

static void
startup_cb (GApplication *g_app,
	    gpointer      user_data)
{
	add_action_info_entries ();
	add_app_action_entries (g_app);
}

static GtkWidget *
create_view_submenu (void)
{
	GtkMenuShell *view_submenu;
	AmtkFactory *factory;

	view_submenu = GTK_MENU_SHELL (gtk_menu_new ());

	factory = amtk_factory_new_with_default_application ();
	gtk_menu_shell_append (view_submenu, amtk_factory_create_check_menu_item (factory, "win.show-side-panel"));
	g_object_unref (factory);

	return GTK_WIDGET (view_submenu);
}

static GtkWidget *
create_help_submenu (void)
{
	GtkMenuShell *help_submenu;
	AmtkFactory *factory;

	help_submenu = GTK_MENU_SHELL (gtk_menu_new ());

	factory = amtk_factory_new_with_default_application ();
	gtk_menu_shell_append (help_submenu, amtk_factory_create_menu_item (factory, "app.about"));
	g_object_unref (factory);

	return GTK_WIDGET (help_submenu);
}

static GtkMenuBar *
create_menu_bar (void)
{
	AmtkFactory *factory;
	GtkWidget *file_menu_item;
	GtkWidget *view_menu_item;
	GtkWidget *help_menu_item;
	GtkMenuBar *menu_bar;

	factory = amtk_factory_new_with_default_application ();

	file_menu_item = gtk_menu_item_new_with_mnemonic ("_File");
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (file_menu_item),
				   amtk_factory_create_simple_menu (factory, file_action_info_entries, -1));

	view_menu_item = gtk_menu_item_new_with_mnemonic ("_View");
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (view_menu_item),
				   create_view_submenu ());

	help_menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (help_menu_item),
				   create_help_submenu ());

	menu_bar = GTK_MENU_BAR (gtk_menu_bar_new ());
	gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), file_menu_item);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), view_menu_item);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), help_menu_item);

	amtk_action_info_store_check_all_used (action_info_store);

	g_object_unref (factory);
	return menu_bar;
}

static void
add_win_actions (GtkApplicationWindow *window,
		 GtkWidget            *side_panel)
{
	GPropertyAction *side_panel_action;

	side_panel_action = g_property_action_new ("show-side-panel", side_panel, "visible");
	g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (side_panel_action));
	g_object_unref (side_panel_action);
}

static void
activate_cb (GApplication *g_app,
	     gpointer      user_data)
{
	GtkWidget *window;
	GtkWidget *vgrid;
	GtkWidget *hgrid;
	GtkWidget *side_panel;

	window = gtk_application_window_new (GTK_APPLICATION (g_app));
	gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);

	vgrid = gtk_grid_new ();
	gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid), GTK_ORIENTATION_VERTICAL);

	gtk_container_add (GTK_CONTAINER (vgrid), GTK_WIDGET (create_menu_bar ()));

	hgrid = gtk_grid_new ();
	side_panel = gtk_label_new ("Side panel");
	gtk_container_add (GTK_CONTAINER (hgrid), side_panel);
	gtk_container_add (GTK_CONTAINER (hgrid), gtk_label_new ("Text view"));
	gtk_container_add (GTK_CONTAINER (vgrid), hgrid);

	add_win_actions (GTK_APPLICATION_WINDOW (window), side_panel);

	gtk_container_add (GTK_CONTAINER (window), vgrid);
	gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
	GtkApplication *app;
	int status;

	amtk_init ();

	app = gtk_application_new ("org.gnome.amtk.test-traditional-ui", G_APPLICATION_DEFAULT_FLAGS);

	g_signal_connect (app,
			  "startup",
			  G_CALLBACK (startup_cb),
			  NULL);

	g_signal_connect (app,
			  "activate",
			  G_CALLBACK (activate_cb),
			  NULL);

	status = g_application_run (G_APPLICATION (app), argc, argv);

	amtk_finalize ();
	g_object_unref (app);
	g_clear_object (&action_info_store);
	return status;
}