1 /*
2  * mape - C4 Landscape.txt editor
3  *
4  * Copyright (c) 2005-2009, Armin Burgmeier
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include <stdlib.h>
17 
18 #include "mape/header.h"
19 
20 static const GtkActionEntry mape_header_file_entries[] = {
21 	{
22 		"File",
23 		NULL,
24 		"_File",
25 		NULL,
26 		NULL,
27 		NULL
28 	}, {
29 		"FileNew",
30 		GTK_STOCK_NEW,
31 		"_New",
32 		"<control>N",
33 		"Opens a new document",
34 		NULL
35 	}, {
36 		"FileOpen",
37 		GTK_STOCK_OPEN,
38 		"_Open",
39 		"<control>O",
40 		"Opens an already existing document from disk",
41 		NULL
42 	}, {
43 		"FileSave",
44 		GTK_STOCK_SAVE,
45 		"_Save",
46 		"<control>S",
47 		"Saves the current document to disk",
48 		NULL
49 	}, {
50 		"FileSaveAs",
51 		GTK_STOCK_SAVE_AS,
52 		"Save as",
53 		"<control><shift>S",
54 		"Save the current map to another path on disk",
55 		NULL
56 	}, {
57 		"FileQuit",
58 		GTK_STOCK_QUIT,
59 		"_Quit",
60 		"<control>Q",
61 		"Exit the program",
62 		NULL
63 	}
64 };
65 
66 static const GtkActionEntry mape_header_edit_entries[] = {
67 	{
68 		"Edit",
69 		NULL,
70 		"_Edit",
71 		NULL,
72 		NULL,
73 		NULL
74 	}, {
75 		"EditUndo",
76 		GTK_STOCK_UNDO,
77 		"_Undo",
78 		"<control>Z",
79 		"Undo the last action",
80 		NULL
81 	}, {
82 		"EditRedo",
83 		GTK_STOCK_REDO,
84 		"_Redo",
85 		"<control>Y",
86 		"Redo the last action",
87 		NULL
88 	}, {
89 		"EditPreferences",
90 		GTK_STOCK_PREFERENCES,
91 		"Pr_eferences",
92 		NULL,
93 		"Configure the application",
94 		NULL
95 	}
96 };
97 
98 static const GtkActionEntry mape_header_help_entries[] = {
99 	{
100 		"Help",
101 		NULL,
102 		"_Help",
103 		NULL,
104 		NULL,
105 		NULL
106 	}, {
107 		"HelpAbout",
108 		GTK_STOCK_ABOUT,
109 		"_About",
110 		NULL,
111 		"Shows authors and copyright information",
112 		NULL
113 	}
114 };
115 
116 static const gchar* mape_header_ui_desc =
117 	"<ui>"
118 	" <menubar name='MenuBar'>"
119 	"  <menu action='File'>"
120 	"   <menuitem action='FileNew' />"
121 	"   <menuitem action='FileOpen' />"
122 	"   <menuitem action='FileSave' />"
123 	"   <menuitem action='FileSaveAs' />"
124 	"   <separator />"
125 	"   <menuitem action='FileQuit' />"
126 	"  </menu>"
127 	"  <menu action='Edit'>"
128 	"   <menuitem action='EditUndo' />"
129 	"   <menuitem action='EditRedo' />"
130 	"   <separator />"
131 	"   <menuitem action='EditPreferences' />"
132 	"  </menu>"
133 	"  <menu action='Help'>"
134 	"   <menuitem action='HelpAbout' />"
135 	"  </menu>"
136 	" </menubar>"
137 	" <toolbar name='ToolBar'>"
138 	"  <toolitem action='FileNew' />"
139 	"  <toolitem action='FileOpen' />"
140 	"  <toolitem action='FileSave' />"
141 	"  <toolitem action='FileSaveAs' />"
142 	"  <separator />"
143 	"  <toolitem action='EditUndo' />"
144 	"  <toolitem action='EditRedo' />"
145 	" </toolbar>"
146 	"</ui>";
147 
mape_header_new(void)148 MapeHeader* mape_header_new(void)
149 {
150 	MapeHeader* header;
151 	gint result;
152 
153 	header = malloc(sizeof(MapeHeader) );
154 
155 	header->group_file = gtk_action_group_new("FileActions");
156 	gtk_action_group_add_actions(
157 		header->group_file,
158 		mape_header_file_entries,
159 		G_N_ELEMENTS(mape_header_file_entries),
160 		header
161 	);
162 
163 	header->group_edit = gtk_action_group_new("EditActions");
164 	gtk_action_group_add_actions(
165 		header->group_edit,
166 		mape_header_edit_entries,
167 		G_N_ELEMENTS(mape_header_edit_entries),
168 		header
169 	);
170 
171 	header->group_help = gtk_action_group_new("HelpActions");
172 	gtk_action_group_add_actions(
173 		header->group_help,
174 		mape_header_help_entries,
175 		G_N_ELEMENTS(mape_header_help_entries),
176 		header
177 	);
178 
179 	header->ui_manager = gtk_ui_manager_new();
180 	gtk_ui_manager_insert_action_group(
181 		header->ui_manager,
182 		header->group_file,
183 		0
184 	);
185 
186 	gtk_ui_manager_insert_action_group(
187 		header->ui_manager,
188 		header->group_edit,
189 		0
190 	);
191 
192 	gtk_ui_manager_insert_action_group(
193 		header->ui_manager,
194 		header->group_help,
195 		0
196 	);
197 
198 	result = gtk_ui_manager_add_ui_from_string(
199 		header->ui_manager,
200 		mape_header_ui_desc,
201 		-1,
202 		NULL
203 	);
204 
205 	g_assert(result != 0);
206 
207 	header->menubar = gtk_ui_manager_get_widget(
208 		header->ui_manager,
209 		"/MenuBar"
210 	);
211 
212 	header->toolbar = gtk_ui_manager_get_widget(
213 		header->ui_manager,
214 		"/ToolBar"
215 	);
216 
217 	g_assert(header->menubar != NULL);
218 	g_assert(header->toolbar != NULL);
219 
220 	header->accel_group = gtk_ui_manager_get_accel_group(
221 		header->ui_manager
222 	);
223 
224 	g_assert(header->accel_group != NULL);
225 
226 	header->file_new = gtk_action_group_get_action(
227 		header->group_file,
228 		"FileNew"
229 	);
230 
231 	header->file_open = gtk_action_group_get_action(
232 		header->group_file,
233 		"FileOpen"
234 	);
235 
236 	header->file_save = gtk_action_group_get_action(
237 		header->group_file,
238 		"FileSave"
239 	);
240 
241 	header->file_save_as = gtk_action_group_get_action(
242 		header->group_file,
243 		"FileSaveAs"
244 	);
245 
246 	header->file_quit = gtk_action_group_get_action(
247 		header->group_file,
248 		"FileQuit"
249 	);
250 
251 	header->edit_undo = gtk_action_group_get_action(
252 		header->group_edit,
253 		"EditUndo"
254 	);
255 
256 	header->edit_redo = gtk_action_group_get_action(
257 		header->group_edit,
258 		"EditRedo"
259 	);
260 
261 	header->edit_preferences = gtk_action_group_get_action(
262 		header->group_edit,
263 		"EditPreferences"
264 	);
265 
266 	header->help_about = gtk_action_group_get_action(
267 		header->group_help,
268 		"HelpAbout"
269 	);
270 
271 	return header;
272 }
273 
mape_header_destroy(MapeHeader * header)274 void mape_header_destroy(MapeHeader* header)
275 {
276 	g_object_unref(G_OBJECT(header->ui_manager) );
277 	free(header);
278 }
279