]> matita.cs.unibo.it Git - helm.git/blob - helm/gtkmathview-bonobo/test/main.c
...
[helm.git] / helm / gtkmathview-bonobo / test / main.c
1 #include <config.h>
2 #include <gnome.h>
3 #include <bonobo.h>
4 #include <glib.h>
5
6 #include "control-factory.h"
7 #include "GtkMathView.h"
8
9 static GtkWidget *control;
10
11 typedef enum{LOAD_STREAM,LOAD_FILE,NONE}FileSelectionOp;
12
13 struct FileSelectionInfo {
14   BonoboWidget* control;
15   GtkWidget* widget;
16
17   FileSelectionOp Operations;
18 };
19
20
21 static struct FileSelectionInfo file_selection_info = { NULL, NULL,NONE};
22
23 static void
24 file_selection_destroy_cb (GtkWidget *widget,gpointer data)
25 {
26         file_selection_info.widget = NULL;
27 }
28
29
30 static void
31 load_through_persist_file (const gchar *filename,
32                                            Bonobo_PersistFile pfile)
33 {
34         CORBA_Environment ev;
35         CORBA_exception_init (&ev);
36         Bonobo_PersistFile_load (pfile, filename, &ev);
37         if (ev._major != CORBA_NO_EXCEPTION)
38                    g_warning ("Cannot load.");
39         CORBA_exception_free (&ev);
40 }
41
42 static void 
43 load_through_persist_stream(const gchar *filename,
44                             Bonobo_PersistStream pstream)
45 {
46     BonoboObject *stream = NULL;
47     CORBA_Environment ev;
48     CORBA_exception_init (&ev);
49
50 #if 0
51     stream = bonobo_stream_open ("fs", filename,
52                                  Bonobo_Storage_READ, 0);
53 #endif
54
55     /*if (stream == NULL) 
56     {
57         g_warning ("Couldn't load `%s'\n", filename);
58     } else*/ {
59         Bonobo_Stream corba_stream;
60         corba_stream = bonobo_object_corba_objref (stream);
61         Bonobo_Stream_truncate (corba_stream, 0, &ev);
62         Bonobo_PersistStream_load (pstream, corba_stream, "application/mathml+xml", &ev);
63     }
64     Bonobo_Unknown_unref (pstream, &ev);
65     CORBA_Object_release (pstream, &ev);
66     CORBA_exception_free (&ev);
67 }
68
69 static void
70 file_selection_ok_cb (GtkWidget *widget,
71                       gpointer data)
72 {
73   CORBA_Object interface;
74   const gchar *interface_name;
75   CORBA_Environment ev;
76
77   if (file_selection_info.Operations == LOAD_FILE)
78             interface_name = "IDL:Bonobo/PersistFile:1.0";
79   else if(file_selection_info.Operations == LOAD_STREAM)
80       interface_name = "IDL:Bonobo/PersistStream:1.0";
81   else printf("failed to get operation type\n");
82
83   CORBA_exception_init (&ev);
84   interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (file_selection_info.control),
85                                              interface_name, &ev);
86   CORBA_exception_free (&ev);
87
88   if (interface == CORBA_OBJECT_NIL)
89     {
90       g_warning ("The Control does not seem to support `%s'.", interface_name);
91     } 
92   else  
93     {
94        const gchar *fname;
95        fname = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selection_info.widget));
96        switch (file_selection_info.Operations) {
97        case LOAD_STREAM:
98          load_through_persist_stream (fname, interface);
99          break;
100        case LOAD_FILE:
101          load_through_persist_file (fname, interface);
102          break;
103        }
104     }
105   
106   gtk_widget_destroy (file_selection_info.widget);
107 }
108
109 static void
110 open_dialog (BonoboWindow *app,
111              FileSelectionOp operation)
112 {
113   GtkWidget    *widget;
114   BonoboWidget *control;
115
116   control = BONOBO_WIDGET (bonobo_window_get_contents (app));
117
118   if (file_selection_info.widget != NULL) {
119     gdk_window_show (GTK_WIDGET (file_selection_info.widget)->window);
120     return;
121   }
122
123   if(operation == LOAD_STREAM)
124       widget = gtk_file_selection_new(_("Open Stream...."));
125   if(operation == LOAD_FILE)
126       widget = gtk_file_selection_new (_("Open file..."));
127
128   gtk_window_set_transient_for (GTK_WINDOW (widget),
129                                 GTK_WINDOW (app));
130
131   file_selection_info.widget = widget;
132   file_selection_info.control = control;
133   file_selection_info.Operations = operation;
134
135   g_signal_connect_object (GTK_FILE_SELECTION (widget)->cancel_button,
136                            "clicked", G_CALLBACK (gtk_widget_destroy), widget, G_CONNECT_AFTER);
137   
138   g_signal_connect (GTK_FILE_SELECTION (widget)->ok_button, "clicked", G_CALLBACK (file_selection_ok_cb), NULL);
139   
140   g_signal_connect (file_selection_info.widget, "destroy", G_CALLBACK (file_selection_destroy_cb), NULL);
141
142   gtk_widget_show (file_selection_info.widget);
143 }
144
145 /* "Open through persist file" dialog.  */
146 static void
147 open_through_persist_file_cb (GtkWidget *widget,
148                               gpointer data)
149 {
150   open_dialog (BONOBO_WINDOW (data), LOAD_FILE);
151 }
152
153 static void 
154 open_through_persist_stream_cb(GtkWidget *widget,
155                                gpointer data)
156 {
157   open_dialog(BONOBO_WINDOW(data), LOAD_STREAM);
158 }
159
160 static void
161 test_view(GtkWidget* widget, gpointer data)
162 {
163   CORBA_Object interface;
164   CORBA_Environment ev;
165
166   CORBA_exception_init (&ev);
167   interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (BONOBO_WIDGET (bonobo_window_get_contents (BONOBO_WINDOW(data)))),
168                                              "IDL:GNOME/GtkMathView/View:1.0", &ev);
169   CORBA_exception_free (&ev);
170
171   if (interface == CORBA_OBJECT_NIL)
172     {
173       g_warning ("The Control does not seem to support `View'.");
174     }
175
176   CORBA_exception_init (&ev);
177   GNOME_GtkMathView_View_freeze(interface, &ev);
178   CORBA_exception_free (&ev);
179
180   CORBA_exception_init (&ev);
181   GNOME_GtkMathView_View_thaw(interface, &ev);
182   CORBA_exception_free (&ev);
183
184   CORBA_exception_init (&ev);
185   Bonobo_Unknown_unref (interface, &ev);
186   CORBA_Object_release (interface, &ev);
187   CORBA_exception_free (&ev);
188 }
189
190 static void
191 exit_cb (GtkWidget *widget,
192          gpointer data)
193 {
194   gtk_widget_destroy (GTK_WIDGET (data));
195   bonobo_main_quit ();
196 }
197
198 static void 
199 get_size(GtkWidget *widget,
200                 gpointer data)
201 {
202     gint width,height;
203     BonoboWidget* control;
204     BonoboControlFrame* control_frame;
205
206         Bonobo_PropertyBag prop_bag;
207
208     control = BONOBO_WIDGET (bonobo_window_get_contents (BONOBO_WINDOW (data)));
209     control_frame = bonobo_widget_get_control_frame(BONOBO_WIDGET(control));
210     prop_bag = bonobo_control_frame_get_control_property_bag(control_frame, NULL);
211
212         width = bonobo_pbclient_get_long(prop_bag,"width",NULL);
213     height = bonobo_pbclient_get_long(prop_bag,"height",NULL);
214     
215     printf("Width: %d Height: %d\n",width,height);
216     
217     bonobo_object_release_unref (prop_bag,NULL);
218
219 };
220
221 static void
222 get_top(GtkWidget *widget,
223         gpointer data)
224 {
225     gint top_x,top_y;
226     BonoboWidget* control;
227     BonoboControlFrame* control_frame;
228     Bonobo_PropertyBag prop_bag;
229
230     control = BONOBO_WIDGET(bonobo_window_get_contents(BONOBO_WINDOW(data)));
231     control_frame = bonobo_widget_get_control_frame(BONOBO_WIDGET(control));
232     prop_bag = bonobo_control_frame_get_control_property_bag(control_frame,NULL);
233
234     top_x = bonobo_pbclient_get_long(prop_bag,"top-x",NULL);
235     top_y = bonobo_pbclient_get_long(prop_bag,"top-y",NULL);
236
237     printf("Top X: %d ,Top Y: %d\n",top_x,top_y);
238
239     bonobo_object_release_unref(prop_bag,NULL);
240 }
241
242 static void 
243 get_font_size(GtkWidget *widget,
244               gpointer data)
245 {
246     gint font_size;
247     BonoboWidget* control;
248     BonoboControlFrame* control_frame;
249     Bonobo_PropertyBag prop_bag;
250
251     control = BONOBO_WIDGET(bonobo_window_get_contents(BONOBO_WINDOW(data)));
252     control_frame = bonobo_widget_get_control_frame(BONOBO_WIDGET(control));
253     prop_bag = bonobo_control_frame_get_control_property_bag(control_frame,NULL);
254
255     font_size = bonobo_pbclient_get_long(prop_bag,"font-size",NULL);
256
257     printf("FontSize : %d\n",font_size);
258
259     bonobo_object_release_unref(prop_bag,NULL);
260 }
261
262 static void 
263 get_verbosity(GtkWidget *widget,
264               gpointer data)
265 {
266     gint verbosity;
267     BonoboWidget* control;
268     BonoboControlFrame* control_frame;
269     Bonobo_PropertyBag prop_bag;
270
271     control = BONOBO_WIDGET(bonobo_window_get_contents(BONOBO_WINDOW(data)));
272     control_frame = bonobo_widget_get_control_frame(BONOBO_WIDGET(control));
273     prop_bag = bonobo_control_frame_get_control_property_bag(control_frame,NULL);
274
275     verbosity = bonobo_pbclient_get_long(prop_bag,"verbosity",NULL);
276
277     printf("Verbosity: %d\n",verbosity);
278
279     bonobo_object_release_unref(prop_bag,NULL);
280 }
281
282 static BonoboUIVerb verbs [] = {
283   BONOBO_UI_UNSAFE_VERB ("OpenFile", open_through_persist_file_cb),
284   BONOBO_UI_UNSAFE_VERB ("OpenStream", open_through_persist_stream_cb),
285   BONOBO_UI_UNSAFE_VERB ("TestView", test_view),
286   BONOBO_UI_UNSAFE_VERB ("Size", get_size),
287   BONOBO_UI_UNSAFE_VERB ("Top", get_top),
288   BONOBO_UI_UNSAFE_VERB ("Font Size", get_font_size),
289   BONOBO_UI_UNSAFE_VERB ("Verbosity", get_verbosity),
290   BONOBO_UI_UNSAFE_VERB ("FileExit", exit_cb),
291   BONOBO_UI_VERB_END
292 };
293
294 /* A dirty, non-translatable hack */
295 static char ui [] = 
296 "<Root>"
297 "       <commands>"
298 "               <cmd name=\"FileExit\" _label=\"Exit\" _tip=\"Exit the program\""
299 "                pixtype=\"stock\" pixname=\"Exit\" accel=\"*Control*q\"/>"
300 "               <cmd name=\"FormatHTML\" _label=\"HTML mode\" type=\"toggle\" _tip=\"HTML Format switch\"/>"
301 "       </commands>"
302 "       <menu>"
303 "               <submenu name=\"File\" _label=\"_File\">"
304 "                       <menuitem name=\"OpenFile\" verb=\"\" _label=\"Open (PersistFile)\" _tip=\"Open using the PersistFile interface\""
305 "                       pixtype=\"stock\" pixname=\"Open\"/>"
306 "                       <menuitem name=\"OpenStream\" verb=\"\" _label=\"_Open Stream (PersistStream)\" _tip=\"Open using the PersistStream interface\""
307 "                       pixtype=\"stock\" pixname=\"Open Stream\"/>"
308 "                       <separator/>"
309 "                       <menuitem name=\"TestView\" verb=\"\" _label=\"Test View\" _tip=\"Test the View interface\"/>"
310 "                       <separator/>"
311 "                       <menuitem name=\"FileExit\" verb=\"\" _label=\"E_xit\"/>"
312 "               </submenu>"
313 "               <placeholder name=\"Component\"/>"
314 "               <submenu name=\"PropertyBags\" _label=\"Property_Bag\">"
315 "                       <menuitem name=\"Size\" verb=\"\" _label=\"Size(PropertyBag)\" _tip=\"Get size\""
316 "                       pixtype=\"stock\" pixname=\"Get Size\"/>"
317
318 "                       <menuitem name=\"Top\" verb=\"\" _label=\"Top(PropertyBag)\" _tip=\"Get Top\""
319 "                       pixtype=\"stock\" pixname=\"Get Top\"/>"
320
321 "                       <menuitem name=\"Font Size\" verb=\"\" _label=\"Font Size(PropertyBag)\" _tip=\"Get FontSize\""
322 "                       pixtype=\"stock\" pixname=\"Get FontSize\"/>"
323
324 "                       <menuitem name=\"Verbosity\" verb=\"\" _label=\"Verbosity(PropertyBag)\" _tip=\"Get Verbosity\""
325 "                       pixtype=\"stock\" pixname=\"Get Verbosity\"/>"
326 "               </submenu>"
327 "       </menu>"
328 "       <dockitem name=\"Toolbar\" behavior=\"exclusive\">"
329 "       </dockitem>"
330 "</Root>";
331
332 static int
333 app_delete_cb (GtkWidget *widget, GdkEvent *event, gpointer dummy)
334 {
335   gtk_widget_destroy (GTK_WIDGET (widget));
336   bonobo_main_quit ();
337
338   return FALSE;
339 }
340
341 static guint
342 container_create (void)
343 {
344   GtkWidget *win;
345   GtkWindow *window;
346   BonoboUIComponent *component;
347   BonoboUIContainer *container;
348   CORBA_Environment ev;
349   
350
351   win = bonobo_window_new ("test-editor",
352                            "GtkMathView Control Test");
353   window = GTK_WINDOW (win);
354
355   container = bonobo_window_get_ui_container (BONOBO_WINDOW (win));
356
357   g_signal_connect (window, "delete_event", G_CALLBACK (app_delete_cb), NULL);
358
359   gtk_window_set_default_size (window, 600, 440);
360   gtk_window_set_resizable (window, TRUE);
361
362   component = bonobo_ui_component_new ("test");
363   bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (component));
364
365   bonobo_ui_component_set_container (component, BONOBO_OBJREF (container), NULL);
366   bonobo_ui_component_add_verb_list_with_data (component, verbs, win);
367   bonobo_ui_component_set_translate (component, "/", ui, NULL);
368   
369   control = bonobo_widget_new_control (CONTROL_ID, BONOBO_OBJREF (container));
370
371   if (control == NULL)
372     g_error ("Cannot get `%s'.", CONTROL_ID);
373
374   bonobo_window_set_contents (BONOBO_WINDOW (win), control);
375
376   gtk_widget_show_all (GTK_WIDGET (window));
377
378   CORBA_exception_init (&ev);
379
380   return FALSE;
381 }
382
383 static gint
384 load_file (const gchar *fname)
385 {
386   CORBA_Object interface;
387   CORBA_Environment ev;
388
389   printf ("loading: %s\n", fname);
390   CORBA_exception_init (&ev);
391   interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (BONOBO_WIDGET (control)),
392                                              "IDL:Bonobo/PersistFile:1.0", &ev);
393   CORBA_exception_free (&ev);
394   load_through_persist_file (fname, interface);
395
396   return FALSE;
397 }
398
399 static gint 
400 load_file_stream(const gchar *fname)
401 {
402     CORBA_Object interface;
403     CORBA_Environment ev;
404
405     printf("loading with stream %s\n",fname);
406     CORBA_exception_init(&ev);
407     interface = Bonobo_Unknown_queryInterface(bonobo_widget_get_objref(BONOBO_WIDGET(control)),
408                                               "IDL:Bonobo/PersistStream:1.0",&ev);
409
410     CORBA_exception_free(&ev);
411     load_through_persist_stream(fname,interface);
412
413     return FALSE;
414 }
415
416 /*
417 static void
418 install_property_bag_listener (BonoboWidget *control, BonoboWindow *bonobo_win)
419 {
420     Bonobo_Listener corba_listener;
421     BonoboListener *listener;
422     BonoboControlFrame *control_frame;
423     CORBA_Environment ev;
424     Bonobo_PropertyBag prop_bag;
425
426     CORBA_exception_init(&ev);
427
428     control_frame = bonobo_widget_get_control_frame(BONOBO_WIDGET(control));
429     if(!control_frame)
430         g_error("can't find control frame\n");
431
432     prop_bag = bonobo_control_frame_get_control_property_bag(control_frame, NULL);
433     if(prop_bag == CORBA_OBJECT_NIL)
434         g_error("can't connect to property bag\n");
435
436     //connect a listener to the property bag
437     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn) width_changed,
438                                             "Bonobo/Property:change:width",NULL,bonobo_win);
439
440     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)height_changed,
441                                             "Bonobo/Property:change:height",NULL,bonobo_win);
442     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)top_x_changed,
443                                             "Bonobo/Property:change:top-x",NULL,bonobo_win);
444     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)top_y_changed,
445                                             "Bonobo/Property:change:top-y",NULL,bonobo_win);
446     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)font_size_changed,
447                                             "Bonobo/Property:change:font-size",NULL,bonobo_win);
448     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)anti_aliasing_changed,
449                                             "Bonobo/Property:change:verbosity",NULL,bonobo_win);
450     bonobo_event_source_client_add_listener(prop_bag, (BonoboListenerCallbackFn)font_manager_changed,
451                                             "Bonobo/Property:change:font_manager",NULL,bonobo_win);
452     CORBA_exception_free(&ev);
453 }*/
454
455
456 int
457 main (int argc, char **argv)
458 {
459   gnome_program_init("test-editor", VERSION, LIBGNOMEUI_MODULE, argc, argv, 
460                      GNOME_PROGRAM_STANDARD_PROPERTIES,
461                      GNOME_PARAM_HUMAN_READABLE_NAME, _("GtkMathView Test Container"),                     
462                      NULL);
463
464   bonobo_activate ();
465
466   /* We can't make any CORBA calls unless we're in the main loop.  So we
467      delay creating the container here. */
468   gtk_idle_add ((GtkFunction) container_create, NULL);
469   if (argc > 1 && *argv [argc - 1] != '-')
470     gtk_idle_add ((GtkFunction) load_file, argv [argc - 1]);
471
472   bonobo_activate ();
473   bonobo_main ();
474
475   return bonobo_ui_debug_shutdown ();
476 }