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
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.
27 #include "diagram_tree_menu_callbacks.h"
28 #include "diagram_tree_menu.h"
29 #include "diagram_tree_window.h"
30 #include "preferences.h"
33 struct _DiagramTreeMenus
{
34 GtkItemFactory
*factories
[2]; /* menu factories */
35 GtkWidget
*menus
[2]; /* popup menus */
36 GtkWidget
*show_menus
[2]; /* show object type menus */
37 DiagramTree
*tree
; /* associated diagram tree */
40 #define SHOW_TYPE_PATH "/Show object type"
42 static const gchar
*ROOT_NAMES_
[] = {"<DiagramTreeDia>", "<DiagramTreeObj>"};
44 static GtkItemFactoryEntry common_items_
[] = {
45 { "/sep0", NULL
, NULL
, 0, "<Separator>" },
46 { N_("/_Sort objects"), NULL
, NULL
, 0, "<Branch>" },
47 { N_("/Sort objects/by _name"), NULL
, on_sort_objects_activate
,
48 DIA_TREE_SORT_NAME
, "" },
49 { N_("/Sort objects/by _type"), NULL
, on_sort_objects_activate
,
50 DIA_TREE_SORT_TYPE
, "" },
51 { N_("/Sort objects/as _inserted"), NULL
, on_sort_objects_activate
,
52 DIA_TREE_SORT_INSERT
, "" },
53 { "/Sort objects/sep0", NULL
, NULL
, 0, "<Separator>" },
54 { N_("/Sort objects/All by name"), NULL
, on_sort_all_objects_activate
,
55 DIA_TREE_SORT_NAME
, "" },
56 { N_("/Sort objects/All by type"), NULL
, on_sort_all_objects_activate
,
57 DIA_TREE_SORT_TYPE
, "" },
58 { N_("/Sort objects/All as inserted"), NULL
, on_sort_all_objects_activate
,
59 DIA_TREE_SORT_INSERT
, "" },
60 { N_("/Sort objects/_Default"), NULL
, NULL
, 0, "<Branch>"},
61 { N_("/Sort objects/Default/by _name"), NULL
, on_sort_def_activate
,
62 DIA_TREE_SORT_NAME
, "<RadioItem>" },
63 { N_("/Sort objects/Default/by _type"), NULL
, on_sort_def_activate
,
64 DIA_TREE_SORT_TYPE
, "/Sort objects/Default/by name" },
65 { N_("/Sort objects/Default/as _inserted"), NULL
, on_sort_def_activate
,
66 DIA_TREE_SORT_INSERT
, "/Sort objects/Default/by name" },
67 { N_("/Sort _diagrams"), NULL
, NULL
, 0, "<Branch>" },
68 { N_("/Sort _diagrams/by _name"), NULL
, on_sort_diagrams_activate
,
69 DIA_TREE_SORT_NAME
, "" },
70 { N_("/Sort _diagrams/as _inserted"), NULL
, on_sort_diagrams_activate
,
71 DIA_TREE_SORT_INSERT
, "" },
72 { N_("/Sort diagrams/_Default"), NULL
, NULL
, 0, "<Branch>"},
73 { N_("/Sort diagrams/Default/by _name"), NULL
, on_sort_dia_def_activate
,
74 DIA_TREE_SORT_NAME
, "<RadioItem>" },
75 { N_("/Sort diagrams/Default/as _inserted"), NULL
, on_sort_dia_def_activate
,
76 DIA_TREE_SORT_INSERT
, "/Sort diagrams/Default/by name" },
79 static const gint COMMON_ITEMS_SIZE_
=
80 sizeof (common_items_
) / sizeof (common_items_
[0]);
82 static GtkItemFactoryEntry object_items_
[] = {
83 { N_("/_Locate"), NULL
, on_locate_object_activate
, 0, "" },
84 { N_("/_Properties"), NULL
, on_properties_activate
, 0, "" },
85 { N_("/_Hide this type"), NULL
, on_hide_object_activate
, 0, "" },
86 { N_(SHOW_TYPE_PATH
), NULL
, NULL
, 0, "<Branch>" },
87 /* { "/sep1", NULL, NULL, 0, "<Separator>" },
88 { N_("/_Delete"), NULL, on_delete_object_activate, 0, "" }, */
91 #define OBJ_ITEMS_SIZE_ (sizeof (object_items_) / sizeof (object_items_[0]))
93 static GtkItemFactoryEntry dia_items_
[] = {
94 { N_("/_Locate"), NULL
, on_locate_dia_activate
, 0, "" },
95 { N_(SHOW_TYPE_PATH
), NULL
, NULL
, 0, "<Branch>" },
98 #define DIA_ITEMS_SIZE_ (sizeof (dia_items_) / sizeof (dia_items_[0]))
100 static GtkItemFactoryEntry
*items_
[] = { dia_items_
, object_items_
};
101 static gint items_size_
[] = { DIA_ITEMS_SIZE_
, OBJ_ITEMS_SIZE_
};
103 static GtkItemFactory
*
104 create_factory(DiagramTree
*tree
, GtkWindow
*window
, gint no
,
105 GtkItemFactoryEntry entries
[], const gchar
*path
)
107 GtkItemFactory
*factory
;
108 GtkAccelGroup
*accel
= gtk_accel_group_new();
109 gchar
*item_path
= NULL
;
112 factory
= gtk_item_factory_new(GTK_TYPE_MENU
, path
, accel
);
113 gtk_item_factory_create_items(factory
, no
, entries
,tree
);
114 gtk_item_factory_create_items(factory
, COMMON_ITEMS_SIZE_
, common_items_
,
116 gtk_window_add_accel_group(window
, accel
);
118 switch (diagram_tree_object_sort_type(tree
)) {
119 case DIA_TREE_SORT_NAME
:
120 item_path
= g_strconcat(path
, "/Sort objects/Default/by name", NULL
);
122 case DIA_TREE_SORT_TYPE
:
123 item_path
= g_strconcat(path
, "/Sort objects/Default/by type", NULL
);
126 case DIA_TREE_SORT_INSERT
:
127 item_path
= g_strconcat(path
, "/Sort objects/Default/as inserted", NULL
);
131 menu
= gtk_item_factory_get_widget(factory
, item_path
);
132 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu
), TRUE
);
135 switch (diagram_tree_diagram_sort_type(tree
)) {
136 case DIA_TREE_SORT_NAME
:
137 item_path
= g_strconcat(path
, "/Sort diagrams/Default/by name", NULL
);
140 case DIA_TREE_SORT_INSERT
:
141 item_path
= g_strconcat(path
, "/Sort diagrams/Default/as inserted", NULL
);
145 menu
= gtk_item_factory_get_widget(factory
, item_path
);
146 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu
), TRUE
);
153 diagram_tree_menus_new(DiagramTree
*tree
, GtkWindow
*window
)
155 DiagramTreeMenus
*result
;
158 g_return_val_if_fail(tree
, NULL
);
159 g_return_val_if_fail(window
, NULL
);
161 result
= g_new(DiagramTreeMenus
, 1);
163 for (k
= 0; k
< 2; ++k
) {
165 static gchar BUFF
[LEN
];
166 result
->factories
[k
] = create_factory(tree
, window
, items_size_
[k
],
167 items_
[k
], ROOT_NAMES_
[k
]);
169 gtk_item_factory_get_widget(result
->factories
[k
], ROOT_NAMES_
[k
]);
170 g_snprintf(BUFF
, LEN
, "%s%s", ROOT_NAMES_
[k
], SHOW_TYPE_PATH
);
171 result
->show_menus
[k
] =
172 gtk_item_factory_get_widget(result
->factories
[k
], BUFF
);
178 diagram_tree_menus_get_menu(const DiagramTreeMenus
*menus
,
179 DiagramTreeMenuType type
)
181 g_return_val_if_fail(menus
, NULL
);
182 g_return_val_if_fail(type
<= DIA_MENU_OBJECT
, NULL
);
183 return menus
->menus
[type
];
187 diagram_tree_menus_popup_menu(const DiagramTreeMenus
*menus
,
188 DiagramTreeMenuType type
, gint time
)
190 g_return_if_fail(menus
);
191 g_return_if_fail(type
<= DIA_MENU_OBJECT
);
192 gtk_menu_popup(GTK_MENU(menus
->menus
[type
]), NULL
, NULL
, NULL
, NULL
, 3, time
);
196 typedef struct _ShowTypeData
198 DiagramTreeMenus
*menus
;
204 on_show_object_type(GtkMenuItem
*item
, ShowTypeData
*data
)
207 diagram_tree_unhide_type(data
->menus
->tree
, data
->type
);
208 for (k
= 0; k
< 2; ++k
) {
209 gtk_container_remove(GTK_CONTAINER(data
->menus
->show_menus
[k
]),
217 diagram_tree_menus_add_hidden_type(DiagramTreeMenus
*menus
,
222 g_return_if_fail(menus
);
223 g_return_if_fail(type
);
224 data
= g_new(ShowTypeData
, 1);
225 data
->type
= g_strdup(type
);
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 g_signal_connect(GTK_OBJECT(item
),
234 G_CALLBACK(on_show_object_type
),
236 gtk_widget_show(item
);