New cisco icons, and a fix in element.h
[dia.git] / app / sheets.c
blobc337fe5887f4a252b2c266397f0c06e3dfe93845
1 /* Dia -- a diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * sheets.c : a sheets and objects dialog
5 * Copyright (C) 2002 M.C. Nelson
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 #include <string.h>
25 #ifdef GNOME
26 #include <gnome.h>
27 #else
28 #include <gtk/gtk.h>
29 #endif
31 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include <gmodule.h>
34 #include "../lib/plug-ins.h"
35 #include "../lib/sheet.h"
36 #include "../lib/message.h"
37 #include "../app/pixmaps/missing.xpm"
39 #include "interface.h"
40 #include "sheets.h"
41 #include "sheets_dialog.h"
42 #include "gtkhwrapbox.h"
44 GtkWidget *sheets_dialog = NULL;
45 GSList *sheets_mods_list = NULL;
46 GtkTooltips *sheets_dialog_tooltips = NULL;
47 static gpointer custom_type_symbol;
49 /* Given a SheetObject and a SheetMod, create a new SheetObjectMod
50 and hook it into the 'objects' list in the SheetMod->sheet
52 NOTE: that the objects structure points to SheetObjectMod's and
53 *not* SheetObject's */
55 SheetObjectMod *
56 sheets_append_sheet_object_mod(SheetObject *so, SheetMod *sm)
58 SheetObjectMod *sheet_object_mod;
59 DiaObjectType *ot;
61 sheet_object_mod = g_new(SheetObjectMod, 1);
62 sheet_object_mod->sheet_object = *so;
63 sheet_object_mod->mod = SHEET_OBJECT_MOD_NONE;
65 ot = object_get_type(so->object_type);
66 g_assert(ot);
67 if (ot->ops == ((DiaObjectType *)(custom_type_symbol))->ops)
68 sheet_object_mod->type = OBJECT_TYPE_SVG;
69 else
70 sheet_object_mod->type = OBJECT_TYPE_PROGRAMMED;
72 sm->sheet.objects = g_slist_append(sm->sheet.objects, sheet_object_mod);
74 return sheet_object_mod;
77 /* Given a Sheet, create a SheetMod wrapper for a list of SheetObjectMod's */
79 SheetMod *
80 sheets_append_sheet_mods(Sheet *sheet)
82 SheetMod *sheet_mod;
83 GSList *sheet_objects_list;
85 sheet_mod = g_new(SheetMod, 1);
86 sheet_mod->sheet = *sheet;
87 sheet_mod->type = SHEETMOD_TYPE_NORMAL;
88 sheet_mod->mod = SHEETMOD_MOD_NONE;
89 sheet_mod->sheet.objects = NULL;
91 for (sheet_objects_list = sheet->objects; sheet_objects_list;
92 sheet_objects_list = g_slist_next(sheet_objects_list))
93 sheets_append_sheet_object_mod(sheet_objects_list->data, sheet_mod);
95 sheets_mods_list = g_slist_append(sheets_mods_list, sheet_mod);
97 return sheet_mod;
100 static gint
101 menu_item_compare_labels(gconstpointer a, gconstpointer b)
103 GList *a_list;
104 gchar *label;
106 a_list = gtk_container_children(GTK_CONTAINER(GTK_MENU_ITEM(a)));
107 g_assert(g_list_length(a_list) == 1);
109 gtk_label_get(GTK_LABEL(a_list->data), &label);
110 g_list_free(a_list);
112 if (!strcmp(label, (gchar *)b))
113 return 0;
114 else
115 return 1;
118 void
119 sheets_optionmenu_create(GtkWidget *option_menu, GtkWidget *wrapbox,
120 gchar *sheet_name)
122 GtkWidget *optionmenu_menu;
123 GSList *sheets_list;
124 GList *menu_item_list;
126 /* Delete the contents, if any, of this optionemnu first */
128 optionmenu_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu));
129 gtk_container_foreach(GTK_CONTAINER(optionmenu_menu),
130 (GtkCallback)gtk_widget_destroy, NULL);
132 if (!sheets_dialog_tooltips)
133 sheets_dialog_tooltips = gtk_tooltips_new();
135 for (sheets_list = sheets_mods_list; sheets_list;
136 sheets_list = g_slist_next(sheets_list))
138 SheetMod *sheet_mod;
139 GtkWidget *menu_item;
140 gchar *tip;
142 sheet_mod = sheets_list->data;
144 /* We don't display sheets which have been deleted prior to Apply */
146 if (sheet_mod->mod == SHEETMOD_MOD_DELETED)
147 continue;
150 menu_item = gtk_menu_item_new_with_label(sheet_mod->sheet.name);
152 gtk_menu_append(GTK_MENU(optionmenu_menu), menu_item);
154 if (sheet_mod->sheet.scope == SHEET_SCOPE_SYSTEM)
155 tip = g_strdup_printf(_("%s\nSystem sheet"), sheet_mod->sheet.description);
156 else
157 tip = g_strdup_printf(_("%s\nUser sheet"), sheet_mod->sheet.description);
159 gtk_tooltips_set_tip(GTK_TOOLTIPS(sheets_dialog_tooltips), menu_item, tip,
160 NULL);
161 g_free(tip);
164 gtk_widget_show(menu_item);
166 gtk_object_set_data(GTK_OBJECT(menu_item), "wrapbox", wrapbox);
168 g_signal_connect(GTK_OBJECT(menu_item), "activate",
169 G_CALLBACK(on_sheets_dialog_optionmenu_activate),
170 (gpointer)sheet_mod);
173 menu_item_list = gtk_container_children(GTK_CONTAINER(optionmenu_menu));
175 /* If we were passed a sheet_name, then make the optionmenu point to that
176 name after creation */
178 if (sheet_name)
180 gint index = 0;
181 GList *list;
183 list = g_list_find_custom(menu_item_list, sheet_name,
184 menu_item_compare_labels);
185 if (list)
186 index = g_list_position(menu_item_list, list);
187 gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu), index);
188 gtk_menu_item_activate(GTK_MENU_ITEM(g_list_nth_data(menu_item_list,
189 index)));
191 else
193 gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu), 0);
194 gtk_menu_item_activate(GTK_MENU_ITEM(menu_item_list->data));
197 g_list_free(menu_item_list);
200 gboolean
201 sheets_dialog_create(void)
203 GList *plugin_list;
204 GSList *sheets_list;
205 GtkWidget *option_menu;
206 GtkWidget *sw;
207 GtkWidget *wrapbox;
208 gchar *sheet_left;
209 gchar *sheet_right;
211 if (sheets_mods_list)
213 /* FIXME: not sure if I understood the data structure
214 but simply leaking isn't acceptable ... --hb
216 g_slist_foreach(sheets_mods_list, (GFunc)g_free, NULL);
217 g_slist_free(sheets_mods_list);
219 sheets_mods_list = NULL;
221 if (sheets_dialog == NULL)
223 sheets_dialog = create_sheets_main_dialog();
224 /* Make sure to null our pointer when destroyed */
225 g_signal_connect (GTK_OBJECT (sheets_dialog), "destroy",
226 G_CALLBACK (gtk_widget_destroyed),
227 &sheets_dialog);
228 g_signal_connect (GTK_OBJECT (sheets_dialog), "destroy",
229 G_CALLBACK (gtk_widget_destroyed),
230 &sheets_dialog_tooltips);
232 sheet_left = NULL;
233 sheet_right = NULL;
235 else
237 option_menu = lookup_widget(sheets_dialog, "optionmenu_left");
238 sheet_left = gtk_object_get_data(GTK_OBJECT(option_menu),
239 "active_sheet_name");
241 option_menu = lookup_widget(sheets_dialog, "optionmenu_right");
242 sheet_right = gtk_object_get_data(GTK_OBJECT(option_menu),
243 "active_sheet_name");
245 wrapbox = lookup_widget(sheets_dialog, "wrapbox_left");
246 gtk_widget_destroy(wrapbox);
248 wrapbox = lookup_widget(sheets_dialog, "wrapbox_right");
249 gtk_widget_destroy(wrapbox);
252 if (custom_type_symbol == NULL)
254 /* This little bit identifies a custom object symbol so we can tell the
255 difference later between a SVG shape and a Programmed shape */
257 custom_type_symbol = NULL;
258 for (plugin_list = dia_list_plugins(); plugin_list != NULL;
259 plugin_list = g_list_next(plugin_list))
261 PluginInfo *info = plugin_list->data;
263 custom_type_symbol = (gpointer)dia_plugin_get_symbol (info,
264 "custom_type");
265 if (custom_type_symbol)
266 break;
270 if (!custom_type_symbol)
272 message_warning (_("Can't get symbol 'custom_type' from any module.\n"
273 "Editing shapes is disabled."));
274 return FALSE;
277 for (sheets_list = get_sheets_list(); sheets_list;
278 sheets_list = g_slist_next(sheets_list))
279 sheets_append_sheet_mods(sheets_list->data);
281 sw = lookup_widget(sheets_dialog, "scrolledwindow_right");
282 wrapbox = gtk_hwrap_box_new(FALSE);
283 gtk_widget_ref(wrapbox);
284 gtk_object_set_data(GTK_OBJECT(sheets_dialog), "wrapbox_right", wrapbox);
285 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), wrapbox);
286 gtk_wrap_box_set_justify(GTK_WRAP_BOX(wrapbox), GTK_JUSTIFY_TOP);
287 gtk_wrap_box_set_line_justify(GTK_WRAP_BOX(wrapbox), GTK_JUSTIFY_LEFT);
288 gtk_widget_show(wrapbox);
289 gtk_object_set_data(GTK_OBJECT(wrapbox), "is_left", FALSE);
290 option_menu = lookup_widget(sheets_dialog, "optionmenu_right");
291 sheets_optionmenu_create(option_menu, wrapbox, sheet_right);
293 sw = lookup_widget(sheets_dialog, "scrolledwindow_left");
294 wrapbox = gtk_hwrap_box_new(FALSE);
295 gtk_widget_ref(wrapbox);
296 gtk_object_set_data(GTK_OBJECT(sheets_dialog), "wrapbox_left", wrapbox);
297 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), wrapbox);
298 gtk_wrap_box_set_justify(GTK_WRAP_BOX(wrapbox), GTK_JUSTIFY_TOP);
299 gtk_wrap_box_set_line_justify(GTK_WRAP_BOX(wrapbox), GTK_JUSTIFY_LEFT);
300 gtk_widget_show(wrapbox);
301 gtk_object_set_data(GTK_OBJECT(wrapbox), "is_left", (gpointer)TRUE);
302 option_menu = lookup_widget(sheets_dialog, "optionmenu_left");
303 sheets_optionmenu_create(option_menu, wrapbox, sheet_left);
305 return TRUE;
308 void
309 create_object_pixmap(SheetObject *so, GtkWidget *parent,
310 GdkPixmap **pixmap, GdkBitmap **mask)
312 GtkStyle *style;
314 g_assert(so);
315 g_assert(pixmap);
316 g_assert(mask);
318 style = gtk_widget_get_style(parent);
320 if (so->pixmap != NULL)
322 *pixmap =
323 gdk_pixmap_colormap_create_from_xpm_d(NULL,
324 gtk_widget_get_colormap(parent),
325 mask,
326 &style->bg[GTK_STATE_NORMAL],
327 so->pixmap);
329 else
331 if (so->pixmap_file != NULL)
333 GdkPixbuf *pixbuf;
334 GError *error = NULL;
336 pixbuf = gdk_pixbuf_new_from_file(so->pixmap_file, &error);
337 if (pixbuf != NULL)
339 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 1.0);
340 gdk_pixbuf_unref(pixbuf);
341 } else {
342 message_warning (error->message);
343 g_error_free (error);
344 *pixmap = gdk_pixmap_colormap_create_from_xpm_d
345 (NULL,
346 gtk_widget_get_colormap(parent),
347 mask,
348 &style->bg[GTK_STATE_NORMAL],
349 missing);
352 else
354 *pixmap = NULL;
355 *mask = NULL;
360 GtkWidget*
361 lookup_widget (GtkWidget *widget,
362 const gchar *widget_name)
364 GtkWidget *parent, *found_widget;
366 for (;;)
368 if (GTK_IS_MENU (widget))
369 parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
370 else
371 parent = widget->parent;
372 if (parent == NULL)
373 break;
374 widget = parent;
377 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
378 widget_name);
379 if (!found_widget)
380 g_warning (_("Widget not found: %s"), widget_name);
381 return found_widget;
384 #include "pixmaps/n_a.xpm"
385 #include "pixmaps/line_break.xpm"
387 /* This is only called by the code written by glade and at the moment
388 it is only used to create the static pixmap of the current sheet object. */
390 GtkWidget *
391 create_pixmap(GtkWidget *dialog, gchar *filename, gboolean arg3)
393 GdkPixmap *pixmap, *mask;
394 GtkWidget *button;
395 GtkWidget *wrapbox;
396 GList *button_list;
397 SheetObjectMod *som;
399 button = sheets_dialog_get_active_button(&wrapbox, &button_list);
400 som = gtk_object_get_data(GTK_OBJECT(button), "sheet_object_mod");
402 if (som)
403 create_object_pixmap(&som->sheet_object, wrapbox, &pixmap, &mask);
404 else
406 GtkStyle *style;
407 gchar **icon;
409 style = gtk_widget_get_style(wrapbox);
411 if ((gboolean)gtk_object_get_data(GTK_OBJECT(button), "is_hidden_button")
412 == TRUE)
413 icon = n_a_xpm;
414 else
415 icon = line_break_xpm;
417 pixmap =
418 gdk_pixmap_colormap_create_from_xpm_d(NULL,
419 gtk_widget_get_colormap(wrapbox),
420 &mask,
421 &style->bg[GTK_STATE_NORMAL],
422 icon);
425 return gtk_pixmap_new(pixmap, mask);
428 /* The menu calls us here, after we've been instantiated */
430 void
431 sheets_dialog_show_callback(gpointer data, guint action, GtkWidget *widget)
433 GtkWidget *wrapbox;
434 GtkWidget *option_menu;
436 if (!sheets_dialog)
437 sheets_dialog_create();
438 if (!sheets_dialog)
439 return;
441 wrapbox = gtk_object_get_data(GTK_OBJECT(sheets_dialog), "wrapbox_left");
442 option_menu = lookup_widget(sheets_dialog, "optionmenu_left");
443 sheets_optionmenu_create(option_menu, wrapbox, interface_current_sheet_name);
445 g_assert(GTK_IS_WIDGET(sheets_dialog));
446 gtk_widget_show(sheets_dialog);
449 gchar *
450 sheet_object_mod_get_type_string(SheetObjectMod *som)
452 switch (som->type)
454 case OBJECT_TYPE_SVG:
455 return _("SVG Shape");
456 case OBJECT_TYPE_PROGRAMMED:
457 return _("Programmed DiaObject");
458 default:
459 g_assert_not_reached();
460 return "";