using dia-manual.chm instead of dia.chm
[dia.git] / app / diagram_tree_menu.c
blob6553ec681801a97136b0eeac6bbb3daf87c2331a
1 /* Dia -- a diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * diagram_tree_menu.c : menus for the diagram tree.
5 * Copyright (C) 2001 Jose A Ortega Ruiz
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include "diagram_tree_menu_callbacks.h"
28 #include "diagram_tree_menu.h"
29 #include "diagram_tree_window.h"
30 #include "preferences.h"
32 struct _DiagramTreeMenus {
33 GtkItemFactory *factories[2]; /* menu factories */
34 GtkWidget *menus[2]; /* popup menus */
35 GtkWidget *show_menus[2]; /* show object type menus */
36 DiagramTree *tree; /* associated diagram tree */
39 #define SHOW_TYPE_PATH "/Show object type"
41 static const gchar *ROOT_NAMES_[] = {"<DiagramTreeDia>", "<DiagramTreeObj>"};
43 static GtkItemFactoryEntry common_items_[] = {
44 { "/sep0", NULL, NULL, 0, "<Separator>" },
45 { N_("/_Sort objects"), NULL, NULL, 0, "<Branch>" },
46 { N_("/Sort objects/by _name"), NULL, on_sort_objects_activate,
47 DIA_TREE_SORT_NAME, "" },
48 { N_("/Sort objects/by _type"), NULL, on_sort_objects_activate,
49 DIA_TREE_SORT_TYPE, "" },
50 { N_("/Sort objects/as _inserted"), NULL, on_sort_objects_activate,
51 DIA_TREE_SORT_INSERT, "" },
52 { "/Sort objects/sep0", NULL, NULL, 0, "<Separator>" },
53 { N_("/Sort objects/All by name"), NULL, on_sort_all_objects_activate,
54 DIA_TREE_SORT_NAME, "" },
55 { N_("/Sort objects/All by type"), NULL, on_sort_all_objects_activate,
56 DIA_TREE_SORT_TYPE, "" },
57 { N_("/Sort objects/All as inserted"), NULL, on_sort_all_objects_activate,
58 DIA_TREE_SORT_INSERT, "" },
59 { N_("/Sort objects/_Default"), NULL, NULL, 0, "<Branch>"},
60 { N_("/Sort objects/Default/by _name"), NULL, on_sort_def_activate,
61 DIA_TREE_SORT_NAME, "<RadioItem>" },
62 { N_("/Sort objects/Default/by _type"), NULL, on_sort_def_activate,
63 DIA_TREE_SORT_TYPE, "/Sort objects/Default/by name" },
64 { N_("/Sort objects/Default/as _inserted"), NULL, on_sort_def_activate,
65 DIA_TREE_SORT_INSERT, "/Sort objects/Default/by name" },
66 { N_("/Sort _diagrams"), NULL, NULL, 0, "<Branch>" },
67 { N_("/Sort _diagrams/by _name"), NULL, on_sort_diagrams_activate,
68 DIA_TREE_SORT_NAME, "" },
69 { N_("/Sort _diagrams/as _inserted"), NULL, on_sort_diagrams_activate,
70 DIA_TREE_SORT_INSERT, "" },
71 { N_("/Sort diagrams/_Default"), NULL, NULL, 0, "<Branch>"},
72 { N_("/Sort diagrams/Default/by _name"), NULL, on_sort_dia_def_activate,
73 DIA_TREE_SORT_NAME, "<RadioItem>" },
74 { N_("/Sort diagrams/Default/as _inserted"), NULL, on_sort_dia_def_activate,
75 DIA_TREE_SORT_INSERT, "/Sort diagrams/Default/by name" },
78 static const gint COMMON_ITEMS_SIZE_ =
79 sizeof (common_items_) / sizeof (common_items_[0]);
81 static GtkItemFactoryEntry object_items_[] = {
82 { N_("/_Locate"), NULL, on_locate_object_activate, 0, "" },
83 { N_("/_Properties"), NULL, on_properties_activate, 0, "" },
84 { N_("/_Hide this type"), NULL, on_hide_object_activate, 0, "" },
85 { N_(SHOW_TYPE_PATH), NULL, NULL, 0, "<Branch>" },
86 /* { "/sep1", NULL, NULL, 0, "<Separator>" },
87 { N_("/_Delete"), NULL, on_delete_object_activate, 0, "" }, */
90 #define OBJ_ITEMS_SIZE_ (sizeof (object_items_) / sizeof (object_items_[0]))
92 static GtkItemFactoryEntry dia_items_[] = {
93 { N_("/_Locate"), NULL, on_locate_dia_activate, 0, "" },
94 { N_(SHOW_TYPE_PATH), NULL, NULL, 0, "<Branch>" },
97 #define DIA_ITEMS_SIZE_ (sizeof (dia_items_) / sizeof (dia_items_[0]))
99 static GtkItemFactoryEntry *items_[] = { dia_items_, object_items_ };
100 static gint items_size_[] = { DIA_ITEMS_SIZE_, OBJ_ITEMS_SIZE_ };
102 static GtkItemFactory*
103 create_factory(DiagramTree *tree, GtkWindow *window, gint no,
104 GtkItemFactoryEntry entries[], const gchar *path)
106 GtkItemFactory *factory;
107 GtkAccelGroup *accel = gtk_accel_group_new();
108 gchar *item_path = NULL;
109 GtkWidget *menu;
111 factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel);
112 gtk_item_factory_create_items(factory, no, entries,tree);
113 gtk_item_factory_create_items(factory, COMMON_ITEMS_SIZE_, common_items_,
114 tree);
115 gtk_window_add_accel_group(window, accel);
117 switch (diagram_tree_object_sort_type(tree)) {
118 case DIA_TREE_SORT_NAME:
119 item_path = g_strconcat(path, "/Sort objects/Default/by name", NULL);
120 break;
121 case DIA_TREE_SORT_TYPE:
122 item_path = g_strconcat(path, "/Sort objects/Default/by type", NULL);
123 break;
124 default:
125 case DIA_TREE_SORT_INSERT:
126 item_path = g_strconcat(path, "/Sort objects/Default/as inserted", NULL);
127 break;
130 menu = gtk_item_factory_get_widget(factory, item_path);
131 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
132 g_free(item_path);
134 switch (diagram_tree_diagram_sort_type(tree)) {
135 case DIA_TREE_SORT_NAME:
136 item_path = g_strconcat(path, "/Sort diagrams/Default/by name", NULL);
137 break;
138 default:
139 case DIA_TREE_SORT_INSERT:
140 item_path = g_strconcat(path, "/Sort diagrams/Default/as inserted", NULL);
141 break;
144 menu = gtk_item_factory_get_widget(factory, item_path);
145 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
146 g_free(item_path);
148 return factory;
151 DiagramTreeMenus *
152 diagram_tree_menus_new(DiagramTree *tree, GtkWindow *window)
154 DiagramTreeMenus *result;
155 int k;
157 g_return_val_if_fail(tree, NULL);
158 g_return_val_if_fail(window, NULL);
160 result = g_new(DiagramTreeMenus, 1);
161 result->tree = tree;
162 for (k = 0; k < 2; ++k) {
163 enum {LEN = 128};
164 static gchar BUFF[LEN];
165 result->factories[k] = create_factory(tree, window, items_size_[k],
166 items_[k], ROOT_NAMES_[k]);
167 result->menus[k] =
168 gtk_item_factory_get_widget(result->factories[k], ROOT_NAMES_[k]);
169 g_snprintf(BUFF, LEN, "%s%s", ROOT_NAMES_[k], SHOW_TYPE_PATH);
170 result->show_menus[k] =
171 gtk_item_factory_get_widget(result->factories[k], BUFF);
173 return result;
176 GtkWidget *
177 diagram_tree_menus_get_menu(const DiagramTreeMenus *menus,
178 DiagramTreeMenuType type)
180 g_return_val_if_fail(menus, NULL);
181 g_return_val_if_fail(type <= DIA_MENU_OBJECT, NULL);
182 return menus->menus[type];
185 void
186 diagram_tree_menus_popup_menu(const DiagramTreeMenus *menus,
187 DiagramTreeMenuType type, gint time)
189 g_return_if_fail(menus);
190 g_return_if_fail(type <= DIA_MENU_OBJECT);
191 gtk_menu_popup(GTK_MENU(menus->menus[type]), NULL, NULL, NULL, NULL, 3, time);
195 typedef struct _ShowTypeData
197 DiagramTreeMenus *menus;
198 GtkWidget *items[2];
199 gchar *type;
200 } ShowTypeData;
202 static void
203 on_show_object_type(GtkMenuItem *item, ShowTypeData *data)
205 int k;
206 diagram_tree_unhide_type(data->menus->tree, data->type);
207 for (k = 0; k < 2; ++k) {
208 gtk_container_remove(GTK_CONTAINER(data->menus->show_menus[k]),
209 data->items[k]);
211 diagram_tree_config_remove_hidden_type(&prefs.dia_tree, data->type);
212 g_free(data->type);
213 g_free(data);
216 void
217 diagram_tree_menus_add_hidden_type(DiagramTreeMenus *menus,
218 const gchar *type)
220 int k;
221 ShowTypeData *data;
222 g_return_if_fail(menus);
223 g_return_if_fail(type);
224 data = g_new(ShowTypeData, 1);
225 data->type = g_strdup(type);
226 data->menus = menus;
227 for (k = 0; k < 2; ++k) {
228 GtkMenuShell *shell = GTK_MENU_SHELL(menus->show_menus[k]);
229 GtkWidget *item = gtk_menu_item_new_with_label(type);
230 data->items[k] = item;
231 gtk_menu_shell_append(shell, item);
232 gtk_signal_connect(GTK_OBJECT(item),
233 "activate",
234 GTK_SIGNAL_FUNC(on_show_object_type),
235 (gpointer)data);
236 gtk_widget_show(item);