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.
25 #include <liboaf/liboaf.h>
29 #include "disp_callbacks.h"
30 #include "app_procs.h"
31 #include "interface.h"
32 #include "load_save.h"
35 # include "render_gnomeprint.h"
38 typedef struct _EmbeddedDia EmbeddedDia
;
39 typedef struct _EmbeddedView EmbeddedView
;
42 BonoboEmbeddable
*embeddable
;
47 struct _EmbeddedView
{
49 EmbeddedDia
*embedded_dia
;
53 static BonoboGenericFactory
*factory
= NULL
;
56 view_show_hide (EmbeddedView
*view_data
,
59 DDisplay
*ddisp
= view_data
->display
;
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
);
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
);
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
);
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
));
100 dia_view_destroy (BonoboView
*view
, EmbeddedView
*view_data
)
102 if (view_data
->display
)
103 ddisplay_destroy(view_data
->display
->shell
, view_data
->display
);
108 dia_embeddable_destroy(BonoboEmbeddable
*embeddable
,
109 EmbeddedDia
*embedded_dia
)
111 Diagram
*dia
= embedded_dia
->diagram
;
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
);
125 dia_view_system_exception(BonoboView
*view
, CORBA_Object corba_object
,
126 CORBA_Environment
*ev
, gpointer data
)
128 bonobo_object_unref(BONOBO_OBJECT(view
));
132 dia_embeddable_system_exception(BonoboEmbeddable
*embeddable
, CORBA_Object corba_object
,
133 CORBA_Environment
*ev
, gpointer data
)
135 bonobo_object_unref(BONOBO_OBJECT(embeddable
));
139 view_factory (BonoboEmbeddable
*embeddable
,
140 const Bonobo_ViewFrame view_frame
,
141 EmbeddedDia
*embedded_dia
)
143 EmbeddedView
*view_data
;
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
),
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
);
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
);
193 save_fn (BonoboPersistFile
*pf
,
194 const CORBA_char
*filename
,
195 CORBA_Environment
*ev
,
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
);
207 refresh_view (DDisplay
*ddisp
)
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
);
221 load_fn (BonoboPersistFile
*pf
,
222 const CORBA_char
*filename
,
223 CORBA_Environment
*ev
,
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
,
235 diagram_update_extents(dia
->diagram
);
236 for (l
=dia
->diagram
->displays
;l
;l
=l
->next
)
237 refresh_view (l
->data
);
244 object_print (GnomePrintContext
*ctx
,
247 const Bonobo_PrintScissor
*scissor
,
250 RendererGPrint
*rend
;
251 EmbeddedDia
*edia
= user_data
;
252 Diagram
*dia
= edia
->diagram
;
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
);
280 static BonoboObject
*
281 embeddable_factory (BonoboGenericFactory
*this,
284 BonoboPersistFile
*pfile
;
285 BonoboEmbeddable
*embeddable
;
287 EmbeddedDia
*embedded_dia
;
289 embedded_dia
= g_new0 (EmbeddedDia
, 1);
290 if (embedded_dia
== NULL
)
293 embedded_dia
->diagram
= new_diagram("foobar_embedd");
295 embeddable
= bonobo_embeddable_new (BONOBO_VIEW_FACTORY (view_factory
),
298 if (embeddable
== NULL
) {
299 diagram_destroy (embedded_dia
->diagram
);
303 embedded_dia
->embeddable
= embeddable
;
305 g_signal_connect(GTK_OBJECT(embeddable
), "system_exception",
306 G_CALLBACK (dia_embeddable_system_exception
),
309 g_signal_connect(GTK_OBJECT(embeddable
), "destroy",
310 G_CALLBACK (dia_embeddable_destroy
),
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
));
320 /* Register the Bonobo::Print interface */
321 print
= bonobo_print_new (object_print
, embedded_dia
);
323 bonobo_object_unref (BONOBO_OBJECT (embeddable
));
327 bonobo_object_add_interface (BONOBO_OBJECT (embeddable
),
328 BONOBO_OBJECT (print
));
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
);
342 init_server_factory (int argc
, char **argv
)
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!"));
357 app_is_embedded(void)
363 last_unref_cb (BonoboObject
*bonobo_object
,
366 bonobo_object_unref (BONOBO_OBJECT (factory
));
371 main (int argc
, char **argv
)
375 init_server_factory (argc
, argv
);
376 factory
= init_dia_factory ();
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
);
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
);
400 g_signal_connect (GTK_OBJECT (bonobo_context_running_get ()),
402 G_CALLBACK (last_unref_cb
),