New cisco icons, and a fix in element.h
[dia.git] / app / dia_embedd.c
blob7f10b302ddd879e5cbc9ac027a8eb8cdc51078d9
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 2000 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <gnome.h>
24 #include <bonobo.h>
25 #include <liboaf/liboaf.h>
27 #include "display.h"
28 #include "menus.h"
29 #include "disp_callbacks.h"
30 #include "app_procs.h"
31 #include "interface.h"
32 #include "load_save.h"
34 #ifdef GNOME_PRINT
35 # include "render_gnomeprint.h"
36 #endif
38 typedef struct _EmbeddedDia EmbeddedDia;
39 typedef struct _EmbeddedView EmbeddedView;
41 struct _EmbeddedDia {
42 BonoboEmbeddable *embeddable;
44 Diagram *diagram;
47 struct _EmbeddedView {
48 BonoboView *view;
49 EmbeddedDia *embedded_dia;
50 DDisplay *display;
53 static BonoboGenericFactory *factory = NULL;
55 static void
56 view_show_hide (EmbeddedView *view_data,
57 gboolean activate)
59 DDisplay *ddisp = view_data->display;
61 if (activate) {
62 toolbox_show();
64 gtk_widget_show(ddisp->origin);
65 gtk_widget_show(ddisp->hrule);
66 gtk_widget_show(ddisp->vrule);
67 gtk_widget_show(ddisp->hsb);
68 gtk_widget_show(ddisp->vsb);
69 gtk_widget_show(ddisp->zoom_status->parent);
70 gtk_widget_show(ddisp->modified_status);
71 } else {
72 toolbox_hide();
74 gtk_widget_hide(ddisp->origin);
75 gtk_widget_hide(ddisp->hrule);
76 gtk_widget_hide(ddisp->vrule);
77 gtk_widget_hide(ddisp->hsb);
78 gtk_widget_hide(ddisp->vsb);
79 gtk_widget_hide(ddisp->zoom_status->parent);
80 gtk_widget_hide(ddisp->modified_status);
84 static void
85 dia_view_activate(BonoboView *view, gboolean activate,
86 EmbeddedView *view_data)
88 bonobo_view_activate_notify(view, activate);
89 view_show_hide(view_data,activate);
92 static void
93 dia_view_display_destroy (GtkWidget *ddisp_shell, EmbeddedView *view_data)
95 view_data->display = NULL;
96 bonobo_object_unref(BONOBO_OBJECT(view_data->view));
99 static void
100 dia_view_destroy (BonoboView *view, EmbeddedView *view_data)
102 if (view_data->display)
103 ddisplay_destroy(view_data->display->shell, view_data->display);
104 g_free(view_data);
107 static void
108 dia_embeddable_destroy(BonoboEmbeddable *embeddable,
109 EmbeddedDia *embedded_dia)
111 Diagram *dia = embedded_dia->diagram;
112 DDisplay *ddisp;
114 /* Destroy all views: */
115 while (dia->displays) {
116 ddisp = (DDisplay *)dia->displays->data;
117 gtk_widget_destroy(ddisp->shell);
120 diagram_destroy(embedded_dia->diagram);
121 g_free(embedded_dia);
124 static void
125 dia_view_system_exception(BonoboView *view, CORBA_Object corba_object,
126 CORBA_Environment *ev, gpointer data)
128 bonobo_object_unref(BONOBO_OBJECT(view));
131 static void
132 dia_embeddable_system_exception(BonoboEmbeddable *embeddable, CORBA_Object corba_object,
133 CORBA_Environment *ev, gpointer data)
135 bonobo_object_unref(BONOBO_OBJECT(embeddable));
138 static BonoboView *
139 view_factory (BonoboEmbeddable *embeddable,
140 const Bonobo_ViewFrame view_frame,
141 EmbeddedDia *embedded_dia)
143 EmbeddedView *view_data;
144 BonoboView *view;
147 * Create the private view data.
149 view_data = g_new0 (EmbeddedView, 1);
150 view_data->embedded_dia = embedded_dia;
152 view_data->display = new_display(embedded_dia->diagram);
154 view_show_hide (view_data, FALSE);
156 g_signal_connect (GTK_OBJECT (view_data->display->shell), "destroy",
157 G_CALLBACK (dia_view_display_destroy),
158 view_data);
160 view = bonobo_view_new (view_data->display->shell);
161 view_data->view = view;
162 gtk_object_set_data (GTK_OBJECT (view), "view_data", view_data);
164 g_signal_connect(GTK_OBJECT (view), "activate",
165 G_CALLBACK (dia_view_activate), view_data);
167 g_signal_connect(GTK_OBJECT (view), "system_exception",
168 G_CALLBACK (dia_view_system_exception), view_data);
170 g_signal_connect (GTK_OBJECT (view), "destroy",
171 G_CALLBACK (dia_view_destroy), view_data);
174 #if 0
175 menuitem = menus_get_item_from_path("<Display>/File/New diagram");
176 gtk_widget_hide(menuitem);
177 menuitem = menus_get_item_from_path("<Display>/File/Open...");
178 gtk_widget_hide(menuitem);
179 menuitem = menus_get_item_from_path("<Display>/File/Save As...");
180 gtk_widget_hide(menuitem);
181 menuitem = menus_get_item_from_path("<Display>/File/Close");
182 gtk_widget_hide(menuitem);
183 menuitem = menus_get_item_from_path("<Display>/File/Exit");
184 gtk_widget_hide(menuitem);
185 menuitem = menus_get_item_from_path("<Display>/View/New View");
186 gtk_widget_hide(menuitem);
187 #endif
189 return view;
192 static int
193 save_fn (BonoboPersistFile *pf,
194 const CORBA_char *filename,
195 CORBA_Environment *ev,
196 void *closure)
198 EmbeddedDia *dia = closure;
200 if (!diagram_save (dia->diagram, filename))
201 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
202 ex_Bonobo_Storage_IOError, NULL);
203 return 0;
206 static void
207 refresh_view (DDisplay *ddisp)
209 Point middle;
210 Rectangle *visible;
212 visible = &ddisp->visible;
213 middle.x = visible->left*0.5 + visible->right*0.5;
214 middle.y = visible->top*0.5 + visible->bottom*0.5;
216 ddisplay_zoom(ddisp, &middle, 1.0);
217 gtk_widget_queue_draw(ddisp->shell);
220 static int
221 load_fn (BonoboPersistFile *pf,
222 const CORBA_char *filename,
223 CORBA_Environment *ev,
224 void *closure)
226 EmbeddedDia *dia = closure;
228 if (!diagram_load_into (dia->diagram, filename, NULL)) {
229 g_warning ("Failed to load '%s'", filename);
230 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
231 ex_Bonobo_Persist_WrongDataType,
232 NULL);
233 } else {
234 GSList *l;
235 diagram_update_extents(dia->diagram);
236 for (l=dia->diagram->displays;l;l=l->next)
237 refresh_view (l->data);
239 return 0;
242 #ifdef GNOME_PRINT
243 static void
244 object_print (GnomePrintContext *ctx,
245 double width,
246 double height,
247 const Bonobo_PrintScissor *scissor,
248 gpointer user_data)
250 RendererGPrint *rend;
251 EmbeddedDia *edia = user_data;
252 Diagram *dia = edia->diagram;
253 Rectangle *extents;
254 double scalex, scaley;
256 rend = new_gnomeprint_renderer(dia, ctx);
259 * FIXME: we need to de-complicate all this - this should
260 * probably be done by scaling the view to the extent or
261 * alternatively keeping the visible bounds on the embeddable
262 * not the view; or possibly none of these.
264 extents = &dia->data->extents;
266 scalex = width / (extents->right - extents->left);
267 scaley = -height / (extents->bottom - extents->top);
269 gnome_print_translate (ctx, - scalex* extents->left,
270 height - scaley * extents->top);
271 gnome_print_scale (ctx, scalex, scaley);
274 data_render(dia->data, (Renderer *)rend, extents, NULL, NULL);
276 g_free (rend);
278 #endif
280 static BonoboObject *
281 embeddable_factory (BonoboGenericFactory *this,
282 void *data)
284 BonoboPersistFile *pfile;
285 BonoboEmbeddable *embeddable;
286 BonoboPrint *print;
287 EmbeddedDia *embedded_dia;
289 embedded_dia = g_new0 (EmbeddedDia, 1);
290 if (embedded_dia == NULL)
291 return NULL;
293 embedded_dia->diagram = new_diagram("foobar_embedd");
295 embeddable = bonobo_embeddable_new (BONOBO_VIEW_FACTORY (view_factory),
296 embedded_dia);
298 if (embeddable == NULL) {
299 diagram_destroy (embedded_dia->diagram);
300 return NULL;
303 embedded_dia->embeddable = embeddable;
305 g_signal_connect(GTK_OBJECT(embeddable), "system_exception",
306 G_CALLBACK (dia_embeddable_system_exception),
307 embedded_dia);
309 g_signal_connect(GTK_OBJECT(embeddable), "destroy",
310 G_CALLBACK (dia_embeddable_destroy),
311 embedded_dia);
313 /* Register the Bonobo::PersistFile interface. */
314 pfile = bonobo_persist_file_new (load_fn, save_fn, embedded_dia);
315 bonobo_object_add_interface (
316 BONOBO_OBJECT (embeddable),
317 BONOBO_OBJECT (pfile));
319 #ifdef GNOME_PRINT
320 /* Register the Bonobo::Print interface */
321 print = bonobo_print_new (object_print, embedded_dia);
322 if (!print) {
323 bonobo_object_unref (BONOBO_OBJECT (embeddable));
324 return NULL;
327 bonobo_object_add_interface (BONOBO_OBJECT (embeddable),
328 BONOBO_OBJECT (print));
329 #endif
331 return BONOBO_OBJECT (embeddable);
334 static BonoboGenericFactory *
335 init_dia_factory (void)
337 return bonobo_generic_factory_new ("OAFIID:GNOME_Dia_DiagramFactory",
338 embeddable_factory, NULL);
341 static void
342 init_server_factory (int argc, char **argv)
344 CORBA_ORB orb;
346 gnome_init_with_popt_table ("dia-embedd", VERSION,
347 argc, argv, oaf_popt_options, 0, NULL);
349 orb = oaf_init (argc, argv);
351 if (bonobo_init (orb, NULL, NULL) == FALSE)
352 g_error (_("Could not initialize Bonobo!"));
353 bonobo_activate ();
357 app_is_embedded(void)
359 return 1;
362 static void
363 last_unref_cb (BonoboObject *bonobo_object,
364 gpointer dummy)
366 bonobo_object_unref (BONOBO_OBJECT (factory));
367 gtk_main_quit ();
371 main (int argc, char **argv)
373 GtkWidget *menuitem;
375 init_server_factory (argc, argv);
376 factory = init_dia_factory ();
378 app_init(0, NULL);
379 app_splash_done();
381 #ifdef GNOME
382 menuitem = menus_get_item_from_path("<Toolbox>/File/New diagram", NULL);
383 gtk_widget_hide(menuitem);
384 menuitem = menus_get_item_from_path("<Toolbox>/File/Open...", NULL);
385 gtk_widget_hide(menuitem);
386 menuitem = menus_get_item_from_path("<Toolbox>/File/Exit", NULL);
387 gtk_widget_hide(menuitem);
388 #else
389 menuitem = menus_get_item_from_path("<Toolbox>/File/New", NULL);
390 gtk_widget_hide(menuitem);
391 menuitem = menus_get_item_from_path("<Toolbox>/File/Open...", NULL);
392 gtk_widget_hide(menuitem);
393 menuitem = menus_get_item_from_path("<Toolbox>/File/sep1", NULL);
394 gtk_widget_hide(menuitem);
395 menuitem = menus_get_item_from_path("<Toolbox>/File/Quit", NULL);
396 gtk_widget_hide(menuitem);
397 #endif
400 g_signal_connect (GTK_OBJECT (bonobo_context_running_get ()),
401 "last_unref",
402 G_CALLBACK (last_unref_cb),
403 NULL);
405 * Start processing.
407 bonobo_main ();
409 return 0;