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
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.
31 #include <gdk-pixbuf/gdk-pixbuf.h>
34 #include "../lib/plug-ins.h"
35 #include "../lib/sheet.h"
36 #include "../lib/message.h"
38 #include "interface.h"
40 #include "sheets_dialog.h"
41 #include "gtkhwrapbox.h"
43 GtkWidget
*sheets_dialog
= NULL
;
44 GSList
*sheets_mods_list
= NULL
;
45 GtkTooltips
*sheets_dialog_tooltips
= NULL
;
46 static gpointer custom_type_symbol
;
48 /* Given a SheetObject and a SheetMod, create a new SheetObjectMod
49 and hook it into the 'objects' list in the SheetMod->sheet
51 NOTE: that the objects structure points to SheetObjectMod's and
52 *not* SheetObject's */
55 sheets_append_sheet_object_mod(SheetObject
*so
, SheetMod
*sm
)
57 SheetObjectMod
*sheet_object_mod
;
60 sheet_object_mod
= g_new(SheetObjectMod
, 1);
61 sheet_object_mod
->sheet_object
= *so
;
62 sheet_object_mod
->mod
= SHEET_OBJECT_MOD_NONE
;
64 ot
= object_get_type(so
->object_type
);
66 if (ot
->ops
== ((ObjectType
*)(custom_type_symbol
))->ops
)
67 sheet_object_mod
->type
= OBJECT_TYPE_SVG
;
69 sheet_object_mod
->type
= OBJECT_TYPE_PROGRAMMED
;
71 sm
->sheet
.objects
= g_slist_append(sm
->sheet
.objects
, sheet_object_mod
);
73 return sheet_object_mod
;
76 /* Given a Sheet, create a SheetMod wrapper for a list of SheetObjectMod's */
79 sheets_append_sheet_mods(Sheet
*sheet
)
82 GSList
*sheet_objects_list
;
84 sheet_mod
= g_new(SheetMod
, 1);
85 sheet_mod
->sheet
= *sheet
;
86 sheet_mod
->type
= SHEETMOD_TYPE_NORMAL
;
87 sheet_mod
->mod
= SHEETMOD_MOD_NONE
;
88 sheet_mod
->sheet
.objects
= NULL
;
90 for (sheet_objects_list
= sheet
->objects
; sheet_objects_list
;
91 sheet_objects_list
= g_slist_next(sheet_objects_list
))
92 sheets_append_sheet_object_mod(sheet_objects_list
->data
, sheet_mod
);
94 sheets_mods_list
= g_slist_append(sheets_mods_list
, sheet_mod
);
100 menu_item_compare_labels(gconstpointer a
, gconstpointer b
)
105 a_list
= gtk_container_children(GTK_CONTAINER(GTK_MENU_ITEM(a
)));
106 g_assert(g_list_length(a_list
) == 1);
108 gtk_label_get(GTK_LABEL(a_list
->data
), &label
);
111 if (!strcmp(label
, (gchar
*)b
))
118 sheets_optionmenu_create(GtkWidget
*option_menu
, GtkWidget
*wrapbox
,
121 GtkWidget
*optionmenu_menu
;
123 GList
*menu_item_list
;
125 /* Delete the contents, if any, of this optionemnu first */
127 optionmenu_menu
= gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu
));
128 gtk_container_foreach(GTK_CONTAINER(optionmenu_menu
),
129 (GtkCallback
)gtk_widget_destroy
, NULL
);
131 if (!sheets_dialog_tooltips
)
132 sheets_dialog_tooltips
= gtk_tooltips_new();
134 for (sheets_list
= sheets_mods_list
; sheets_list
;
135 sheets_list
= g_slist_next(sheets_list
))
138 GtkWidget
*menu_item
;
141 sheet_mod
= sheets_list
->data
;
143 /* We don't display sheets which have been deleted prior to Apply */
145 if (sheet_mod
->mod
== SHEETMOD_MOD_DELETED
)
149 menu_item
= gtk_menu_item_new_with_label(sheet_mod
->sheet
.name
);
151 gtk_menu_append(GTK_MENU(optionmenu_menu
), menu_item
);
153 if (sheet_mod
->sheet
.scope
== SHEET_SCOPE_SYSTEM
)
154 tip
= g_strdup_printf(_("%s\nSystem sheet"), sheet_mod
->sheet
.description
);
156 tip
= g_strdup_printf(_("%s\nUser sheet"), sheet_mod
->sheet
.description
);
158 gtk_tooltips_set_tip(GTK_TOOLTIPS(sheets_dialog_tooltips
), menu_item
, tip
,
163 gtk_widget_show(menu_item
);
165 gtk_object_set_data(GTK_OBJECT(menu_item
), "wrapbox", wrapbox
);
167 g_signal_connect(GTK_OBJECT(menu_item
), "activate",
168 G_CALLBACK(on_sheets_dialog_optionmenu_activate
),
169 (gpointer
)sheet_mod
);
172 menu_item_list
= gtk_container_children(GTK_CONTAINER(optionmenu_menu
));
174 /* If we were passed a sheet_name, then make the optionmenu point to that
175 name after creation */
182 list
= g_list_find_custom(menu_item_list
, sheet_name
,
183 menu_item_compare_labels
);
185 index
= g_list_position(menu_item_list
, list
);
186 gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu
), index
);
187 gtk_menu_item_activate(GTK_MENU_ITEM(g_list_nth_data(menu_item_list
,
192 gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu
), 0);
193 gtk_menu_item_activate(GTK_MENU_ITEM(menu_item_list
->data
));
196 g_list_free(menu_item_list
);
200 sheets_dialog_create(void)
204 GtkWidget
*option_menu
;
210 if (sheets_mods_list
)
212 /* FIXME: not sure if I understood the data structure
213 but simply leaking isn't acceptable ... --hb
215 g_slist_foreach(sheets_mods_list
, (GFunc
)g_free
, NULL
);
216 g_slist_free(sheets_mods_list
);
218 sheets_mods_list
= NULL
;
220 if (sheets_dialog
== NULL
)
222 sheets_dialog
= create_sheets_main_dialog();
223 /* Make sure to null our pointer when destroyed */
224 g_signal_connect (GTK_OBJECT (sheets_dialog
), "destroy",
225 G_CALLBACK (gtk_widget_destroyed
),
227 g_signal_connect (GTK_OBJECT (sheets_dialog
), "destroy",
228 G_CALLBACK (gtk_widget_destroyed
),
229 &sheets_dialog_tooltips
);
236 option_menu
= lookup_widget(sheets_dialog
, "optionmenu_left");
237 sheet_left
= gtk_object_get_data(GTK_OBJECT(option_menu
),
238 "active_sheet_name");
240 option_menu
= lookup_widget(sheets_dialog
, "optionmenu_right");
241 sheet_right
= gtk_object_get_data(GTK_OBJECT(option_menu
),
242 "active_sheet_name");
244 wrapbox
= lookup_widget(sheets_dialog
, "wrapbox_left");
245 gtk_widget_destroy(wrapbox
);
247 wrapbox
= lookup_widget(sheets_dialog
, "wrapbox_right");
248 gtk_widget_destroy(wrapbox
);
251 if (custom_type_symbol
== NULL
)
253 /* This little bit identifies a custom object symbol so we can tell the
254 difference later between a SVG shape and a Programmed shape */
256 custom_type_symbol
= NULL
;
257 for (plugin_list
= dia_list_plugins(); plugin_list
!= NULL
;
258 plugin_list
= g_list_next(plugin_list
))
260 PluginInfo
*info
= plugin_list
->data
;
262 custom_type_symbol
= (gpointer
)dia_plugin_get_symbol (info
,
264 if (custom_type_symbol
)
269 if (!custom_type_symbol
)
271 message_warning (_("Can't get symbol 'custom_type' from any module.\n"
272 "Editing shapes is disabled."));
276 for (sheets_list
= get_sheets_list(); sheets_list
;
277 sheets_list
= g_slist_next(sheets_list
))
278 sheets_append_sheet_mods(sheets_list
->data
);
280 sw
= lookup_widget(sheets_dialog
, "scrolledwindow_right");
281 wrapbox
= gtk_hwrap_box_new(FALSE
);
282 gtk_widget_ref(wrapbox
);
283 gtk_object_set_data(GTK_OBJECT(sheets_dialog
), "wrapbox_right", wrapbox
);
284 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw
), wrapbox
);
285 gtk_wrap_box_set_justify(GTK_WRAP_BOX(wrapbox
), GTK_JUSTIFY_TOP
);
286 gtk_wrap_box_set_line_justify(GTK_WRAP_BOX(wrapbox
), GTK_JUSTIFY_LEFT
);
287 gtk_widget_show(wrapbox
);
288 gtk_object_set_data(GTK_OBJECT(wrapbox
), "is_left", FALSE
);
289 option_menu
= lookup_widget(sheets_dialog
, "optionmenu_right");
290 sheets_optionmenu_create(option_menu
, wrapbox
, sheet_right
);
292 sw
= lookup_widget(sheets_dialog
, "scrolledwindow_left");
293 wrapbox
= gtk_hwrap_box_new(FALSE
);
294 gtk_widget_ref(wrapbox
);
295 gtk_object_set_data(GTK_OBJECT(sheets_dialog
), "wrapbox_left", wrapbox
);
296 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw
), wrapbox
);
297 gtk_wrap_box_set_justify(GTK_WRAP_BOX(wrapbox
), GTK_JUSTIFY_TOP
);
298 gtk_wrap_box_set_line_justify(GTK_WRAP_BOX(wrapbox
), GTK_JUSTIFY_LEFT
);
299 gtk_widget_show(wrapbox
);
300 gtk_object_set_data(GTK_OBJECT(wrapbox
), "is_left", (gpointer
)TRUE
);
301 option_menu
= lookup_widget(sheets_dialog
, "optionmenu_left");
302 sheets_optionmenu_create(option_menu
, wrapbox
, sheet_left
);
308 create_object_pixmap(SheetObject
*so
, GtkWidget
*parent
,
309 GdkPixmap
**pixmap
, GdkBitmap
**mask
)
317 style
= gtk_widget_get_style(parent
);
319 if (so
->pixmap
!= NULL
)
322 gdk_pixmap_colormap_create_from_xpm_d(NULL
,
323 gtk_widget_get_colormap(parent
),
325 &style
->bg
[GTK_STATE_NORMAL
],
330 if (so
->pixmap_file
!= NULL
)
333 GError
*error
= NULL
;
335 pixbuf
= gdk_pixbuf_new_from_file(so
->pixmap_file
, &error
);
338 gdk_pixbuf_render_pixmap_and_mask(pixbuf
, pixmap
, mask
, 1.0);
339 gdk_pixbuf_unref(pixbuf
);
341 g_warning (error
->message
);
342 g_error_free (error
);
354 lookup_widget (GtkWidget
*widget
,
355 const gchar
*widget_name
)
357 GtkWidget
*parent
, *found_widget
;
361 if (GTK_IS_MENU (widget
))
362 parent
= gtk_menu_get_attach_widget (GTK_MENU (widget
));
364 parent
= widget
->parent
;
370 found_widget
= (GtkWidget
*) gtk_object_get_data (GTK_OBJECT (widget
),
373 g_warning (_("Widget not found: %s"), widget_name
);
377 #include "pixmaps/n_a.xpm"
378 #include "pixmaps/line_break.xpm"
380 /* This is only called by the code written by glade and at the moment
381 it is only used to create the static pixmap of the current sheet object. */
384 create_pixmap(GtkWidget
*dialog
, gchar
*filename
, gboolean arg3
)
386 GdkPixmap
*pixmap
, *mask
;
392 button
= sheets_dialog_get_active_button(&wrapbox
, &button_list
);
393 som
= gtk_object_get_data(GTK_OBJECT(button
), "sheet_object_mod");
396 create_object_pixmap(&som
->sheet_object
, wrapbox
, &pixmap
, &mask
);
402 style
= gtk_widget_get_style(wrapbox
);
404 if ((gboolean
)gtk_object_get_data(GTK_OBJECT(button
), "is_hidden_button")
408 icon
= line_break_xpm
;
411 gdk_pixmap_colormap_create_from_xpm_d(NULL
,
412 gtk_widget_get_colormap(wrapbox
),
414 &style
->bg
[GTK_STATE_NORMAL
],
418 return gtk_pixmap_new(pixmap
, mask
);
421 /* The menu calls us here, after we've been instantiated */
424 sheets_dialog_show_callback(gpointer data
, guint action
, GtkWidget
*widget
)
427 GtkWidget
*option_menu
;
430 sheets_dialog_create();
434 wrapbox
= gtk_object_get_data(GTK_OBJECT(sheets_dialog
), "wrapbox_left");
435 option_menu
= lookup_widget(sheets_dialog
, "optionmenu_left");
436 sheets_optionmenu_create(option_menu
, wrapbox
, interface_current_sheet_name
);
438 g_assert(GTK_IS_WIDGET(sheets_dialog
));
439 gtk_widget_show(sheets_dialog
);
443 sheet_object_mod_get_type_string(SheetObjectMod
*som
)
447 case OBJECT_TYPE_SVG
:
448 return _("SVG Shape");
449 case OBJECT_TYPE_PROGRAMMED
:
450 return _("Programmed Object");
452 g_assert_not_reached();
453 return FALSE
; /* shut gcc up */