]> git.sur5r.net Git - glabels/blob - glabels2/src/ui.c
Renamed menus module to ui.
[glabels] / glabels2 / src / ui.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  ui.c:  GLabels ui module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 /*
24  * This file is based on gedit-menus.c from gedit2:
25  *
26  * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
27  * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi 
28  *
29  */
30 #include <config.h>
31
32 #include "ui.h"
33 #include "commands.h"
34 #include "tools.h"
35 #include "glabels.h"
36
37 BonoboUIVerb gl_ui_verbs [] = {
38         BONOBO_UI_VERB ("FileNew", gl_cmd_file_new),
39         BONOBO_UI_VERB ("FileOpen", gl_cmd_file_open),
40         BONOBO_UI_VERB ("FileSave", gl_cmd_file_save),
41         BONOBO_UI_VERB ("FileSaveAs", gl_cmd_file_save_as),
42         BONOBO_UI_VERB ("FilePrint", gl_cmd_file_print),
43         BONOBO_UI_VERB ("FileClose", gl_cmd_file_close),
44         BONOBO_UI_VERB ("FileCloseAll", gl_cmd_file_close_all),
45         BONOBO_UI_VERB ("FileExit", gl_cmd_file_exit),
46         BONOBO_UI_VERB ("EditCut", gl_cmd_edit_cut),
47         BONOBO_UI_VERB ("EditCopy", gl_cmd_edit_copy),
48         BONOBO_UI_VERB ("EditPaste", gl_cmd_edit_paste),
49         BONOBO_UI_VERB ("EditDelete", gl_cmd_edit_delete),
50         BONOBO_UI_VERB ("EditSelectAll", gl_cmd_edit_select_all),
51         BONOBO_UI_VERB ("EditUnSelectAll", gl_cmd_edit_unselect_all),
52         BONOBO_UI_VERB ("ToolsArrow", gl_tools_arrow),
53         BONOBO_UI_VERB ("ToolsText", gl_tools_text),
54         BONOBO_UI_VERB ("ToolsBox", gl_tools_box),
55         BONOBO_UI_VERB ("ToolsLine", gl_tools_line),
56         BONOBO_UI_VERB ("ToolsEllipse", gl_tools_ellipse),
57         BONOBO_UI_VERB ("ToolsImage", gl_tools_image),
58         BONOBO_UI_VERB ("ToolsBarcode", gl_tools_barcode),
59         BONOBO_UI_VERB ("ToolsZoomIn", gl_tools_zoomin),
60         BONOBO_UI_VERB ("ToolsZoomOut", gl_tools_zoomout),
61         BONOBO_UI_VERB ("ToolsZoom1to1", gl_tools_zoom1to1),
62         BONOBO_UI_VERB ("ToolsMergeProperties", gl_tools_merge_properties),
63         BONOBO_UI_VERB ("ToolsObjectProperties", gl_tools_object_properties),
64         BONOBO_UI_VERB ("ToolsRaiseObjects", gl_tools_raise_objects),
65         BONOBO_UI_VERB ("ToolsLowerObjects", gl_tools_lower_objects),
66         BONOBO_UI_VERB ("SettingsPreferences", gl_cmd_settings_preferences),
67         BONOBO_UI_VERB ("HelpContents", gl_cmd_help_contents),
68         BONOBO_UI_VERB ("About", gl_cmd_help_about),
69
70         BONOBO_UI_VERB_END
71 };
72
73 gchar* gl_ui_no_docs_sensible_verbs [] = {
74         "/commands/FileSave",
75         "/commands/FileSaveAs",
76         "/commands/FilePrint",
77         "/commands/FilePrintPreview",
78         "/commands/FileClose",
79         "/commands/FileCloseAll",
80         "/commands/EditUndo",
81         "/commands/EditRedo",
82         "/commands/EditCut",
83         "/commands/EditCopy",
84         "/commands/EditPaste",
85         "/commands/EditDelete",
86         "/commands/EditSelectAll",
87         "/commands/EditUnSelectAll",
88         "/commands/ToolsArrow",
89         "/commands/ToolsText",
90         "/commands/ToolsLine",
91         "/commands/ToolsBox",
92         "/commands/ToolsEllipse",
93         "/commands/ToolsImage",
94         "/commands/ToolsBarcode",
95         "/commands/ToolsZoomIn",
96         "/commands/ToolsZoomOut",
97         "/commands/ToolsZoom1to1",
98         "/commands/ToolsMergeProperties",
99         "/commands/ToolsObjectProperties",
100         "/commands/ToolsRaiseObjects",
101         "/commands/ToolsLowerObjects",
102
103         NULL
104 };
105
106 gchar* gl_ui_not_modified_doc_sensible_verbs [] = {
107         "/commands/FileSave",
108
109         NULL
110 };
111
112 gchar* gl_ui_selection_sensible_verbs [] = {
113         "/commands/EditCut",
114         "/commands/EditCopy",
115         "/commands/EditDelete",
116         "/commands/EditUnSelectAll",
117         "/commands/ToolsRaiseObjects",
118         "/commands/ToolsLowerObjects",
119
120         NULL
121 };
122
123 gchar* gl_ui_atomic_selection_sensible_verbs [] = {
124         "/commands/ToolsObjectProperties",
125
126         NULL
127 };
128
129
130 void
131 gl_ui_set_verb_sensitive (BonoboUIComponent  *ui_component,
132                           gchar              *cname,
133                           gboolean            sensitive)
134 {
135         g_return_if_fail (cname != NULL);
136         g_return_if_fail (BONOBO_IS_UI_COMPONENT (ui_component));
137
138         bonobo_ui_component_set_prop (ui_component,
139                                       cname,
140                                       "sensitive",
141                                       sensitive ? "1" : "0",
142                                       NULL);
143 }
144
145 void
146 gl_ui_set_verb_list_sensitive (BonoboUIComponent   *ui_component,
147                                gchar              **vlist,
148                                gboolean             sensitive)
149 {
150         g_return_if_fail (vlist != NULL);
151         g_return_if_fail (BONOBO_IS_UI_COMPONENT (ui_component));
152
153         for ( ; *vlist; ++vlist)
154         {
155                 bonobo_ui_component_set_prop (ui_component,
156                                               *vlist,
157                                               "sensitive",
158                                               sensitive ? "1" : "0",
159                                               NULL);
160         }
161 }
162
163 void
164 gl_ui_set_verb_state (BonoboUIComponent   *ui_component,
165                       gchar               *cname,
166                       gboolean             state)
167 {
168         g_return_if_fail (cname != NULL);
169         g_return_if_fail (BONOBO_IS_UI_COMPONENT (ui_component));
170
171         bonobo_ui_component_set_prop (ui_component,
172                                       cname,
173                                       "state",
174                                       state ? "1" : "0",
175                                       NULL);
176 }
177
178 void
179 gl_ui_add_menu_item (BonoboWindow   *window,
180                      const gchar    *path,
181                      const gchar    *name,
182                      const gchar    *label,
183                      const gchar    *tooltip,
184                      const gchar    *stock_pixmap,
185                      BonoboUIVerbFn  cb)
186 {
187         BonoboUIComponent *ui_component;
188         gchar *item_path;
189         gchar *cmd;
190
191         g_return_if_fail (window != NULL);
192         g_return_if_fail (path != NULL);
193         g_return_if_fail (label != NULL);
194         g_return_if_fail (cb != NULL);
195         
196         item_path = g_strconcat (path, name, NULL);
197         ui_component =
198                 bonobo_mdi_get_ui_component_from_window (BONOBO_WINDOW (window));
199         if (!bonobo_ui_component_path_exists (ui_component, item_path, NULL)) {
200                 gchar *xml;
201
202                 xml = g_strdup_printf ("<menuitem name=\"%s\" verb=\"\""
203                                        " _label=\"%s\""
204                                        " _tip=\"%s\" hident=\"0\" />", name,
205                                        label, tooltip);
206
207
208                 if (stock_pixmap != NULL) {
209                         cmd = g_strdup_printf ("<cmd name=\"%s\""
210                                 " pixtype=\"stock\" pixname=\"%s\" />",
211                                 name, stock_pixmap);
212                 }
213                 else {
214                         cmd = g_strdup_printf ("<cmd name=\"%s\" />", name);
215                 }
216
217
218                 bonobo_ui_component_set_translate (ui_component,
219                                                    path,
220                                                    xml,
221                                                    NULL);
222
223                 bonobo_ui_component_set_translate (ui_component,
224                                                    "/commands/",
225                                                    cmd,
226                                                    NULL);
227                                                    
228                 bonobo_ui_component_add_verb (ui_component, name, cb, NULL);
229
230                 g_free (xml);
231                 g_free (cmd);
232         }
233
234         g_free (item_path);
235 }
236
237 void
238 gl_ui_remove_menu_item (BonoboWindow *window,
239                         const gchar  *path,
240                         const gchar  *name)
241 {
242         BonoboUIComponent *ui_component;
243         gchar *item_path;
244
245         g_return_if_fail (window != NULL);
246         g_return_if_fail (path != NULL);
247         g_return_if_fail (name != NULL);
248
249         item_path = g_strconcat (path, name, NULL);
250         ui_component =
251                 bonobo_mdi_get_ui_component_from_window (BONOBO_WINDOW (window));
252
253         if (bonobo_ui_component_path_exists (ui_component, item_path, NULL)) {
254                 gchar *cmd;
255
256                 cmd = g_strdup_printf ("/commands/%s", name);
257                 
258                 bonobo_ui_component_rm (ui_component, item_path, NULL);
259                 bonobo_ui_component_rm (ui_component, cmd, NULL);
260                 
261                 g_free (cmd);
262         }
263
264         g_free (item_path);
265 }
266
267 void
268 gl_ui_add_menu_item_all (const gchar    *path,
269                          const gchar    *name,
270                          const gchar    *label,
271                          const gchar    *tooltip,
272                          const gchar    *stock_pixmap,
273                          BonoboUIVerbFn  cb)
274 {
275         GList* top_windows;
276         
277         top_windows = glabels_get_top_windows ();
278         g_return_if_fail (top_windows != NULL);
279        
280         while (top_windows)
281         {
282                 BonoboWindow* window = BONOBO_WINDOW (top_windows->data);
283
284
285                 gl_ui_add_menu_item (window,
286                                      path,
287                                      name,
288                                      label,
289                                      tooltip,
290                                      stock_pixmap,
291                                      cb);
292                 
293                 top_windows = g_list_next (top_windows);
294         }
295 }
296
297 void
298 gl_ui_remove_menu_item_all (const gchar *path,
299                             const gchar *name)
300 {
301         GList* top_windows;
302         
303         top_windows = glabels_get_top_windows ();
304         g_return_if_fail (top_windows != NULL);
305        
306         while (top_windows)
307         {
308                 BonoboWindow* window = BONOBO_WINDOW (top_windows->data);
309
310
311                 gl_ui_remove_menu_item (window, path, name);
312
313                 
314                 top_windows = g_list_next (top_windows);
315         }
316 }