]> matita.cs.unibo.it Git - helm.git/blob - helm/gtkmathview-bonobo/test/main.c
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / gtkmathview-bonobo / test / main.c
1
2 #include <config.h>
3
4 #include <gnome.h>
5 #include <bonobo.h>
6  
7 #include <glib.h>
8
9 #include "control-factory.h"
10
11 static GtkWidget *control;
12
13 struct FileSelectionInfo {
14   BonoboWidget* control;
15   GtkWidget* widget;
16 };
17
18 static struct FileSelectionInfo file_selection_info = { NULL, NULL };
19
20 static void
21 load_through_persist_file (const gchar *filename,
22                                            Bonobo_PersistFile pfile)
23 {
24         CORBA_Environment ev;
25         CORBA_exception_init (&ev);
26         Bonobo_PersistFile_load (pfile, filename, &ev);
27         if (ev._major != CORBA_NO_EXCEPTION)
28                    g_warning ("Cannot load.");
29         CORBA_exception_free (&ev);
30 }
31
32 static void
33 file_selection_ok_cb (GtkWidget *widget,
34                       gpointer data)
35 {
36   CORBA_Object interface;
37   const gchar *interface_name;
38   CORBA_Environment ev;
39
40   interface_name = "IDL:Bonobo/PersistFile:1.0";
41
42   CORBA_exception_init (&ev);
43   interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (file_selection_info.control),
44                                              interface_name, &ev);
45   CORBA_exception_free (&ev);
46
47   if (interface == CORBA_OBJECT_NIL)
48     {
49       g_warning ("The Control does not seem to support `%s'.", interface_name);
50     } 
51   else  
52     {
53       const gchar *fname = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selection_info.widget));
54       load_through_persist_file (fname, interface);
55     }
56   
57   gtk_widget_destroy (file_selection_info.widget);
58 }
59
60 static void
61 file_selection_destroy_cb (GtkWidget *widget,
62                            gpointer data)
63 {
64   file_selection_info.widget = NULL;
65 }
66
67 static void
68 open_dialog (BonoboWindow *app)
69 {
70   GtkWidget    *widget;
71   BonoboWidget *control;
72
73   control = BONOBO_WIDGET (bonobo_window_get_contents (app));
74
75   if (file_selection_info.widget != NULL) {
76     gdk_window_show (GTK_WIDGET (file_selection_info.widget)->window);
77     return;
78   }
79
80   widget = gtk_file_selection_new (_("Open file..."));
81
82   gtk_window_set_transient_for (GTK_WINDOW (widget),
83                                 GTK_WINDOW (app));
84
85   file_selection_info.widget = widget;
86   file_selection_info.control = control;
87
88   g_signal_connect_object (GTK_FILE_SELECTION (widget)->cancel_button,
89                            "clicked", G_CALLBACK (gtk_widget_destroy), widget, G_CONNECT_AFTER);
90   g_signal_connect (GTK_FILE_SELECTION (widget)->ok_button, "clicked", G_CALLBACK (file_selection_ok_cb), NULL);
91   g_signal_connect (file_selection_info.widget, "destroy", G_CALLBACK (file_selection_destroy_cb), NULL);
92
93   gtk_widget_show (file_selection_info.widget);
94 }
95
96 /* "Open through persist file" dialog.  */
97 static void
98 open_through_persist_file_cb (GtkWidget *widget,
99                               gpointer data)
100 {
101   open_dialog (BONOBO_WINDOW (data));
102 }
103
104 static void
105 exit_cb (GtkWidget *widget,
106          gpointer data)
107 {
108   gtk_widget_destroy (GTK_WIDGET (data));
109   bonobo_main_quit ();
110 }
111
112 static BonoboUIVerb verbs [] = {
113   BONOBO_UI_UNSAFE_VERB ("OpenFile", open_through_persist_file_cb),
114   BONOBO_UI_UNSAFE_VERB ("FileExit", exit_cb),
115
116   BONOBO_UI_VERB_END
117 };
118
119 /* A dirty, non-translatable hack */
120 static char ui [] = 
121 "<Root>"
122 "       <commands>"
123 "               <cmd name=\"FileExit\" _label=\"Exit\" _tip=\"Exit the program\""
124 "                pixtype=\"stock\" pixname=\"Exit\" accel=\"*Control*q\"/>"
125 "               <cmd name=\"FormatHTML\" _label=\"HTML mode\" type=\"toggle\" _tip=\"HTML Format switch\"/>"
126 "       </commands>"
127 "       <menu>"
128 "               <submenu name=\"File\" _label=\"_File\">"
129 "                       <menuitem name=\"OpenFile\" verb=\"\" _label=\"Open (PersistFile)\" _tip=\"Open using the PersistFile interface\""
130 "                       pixtype=\"stock\" pixname=\"Open\"/>"
131 "                       <menuitem name=\"SaveFile\" verb=\"\" _label=\"Save (PersistFile)\" _tip=\"Save using the PersistFile interface\""
132 "                       pixtype=\"stock\" pixname=\"Save\"/>"
133 "                       <separator/>"
134 "                       <menuitem name=\"OpenStream\" verb=\"\" _label=\"_Open (PersistStream)\" _tip=\"Open using the PersistStream interface\""
135 "                       pixtype=\"stock\" pixname=\"Open\"/>"
136 "                       <menuitem name=\"SaveStream\" verb=\"\" _label=\"_Save (PersistStream)\" _tip=\"Save using the PersistStream interface\""
137 "                       pixtype=\"stock\" pixname=\"Save\"/>"
138 "                       <menuitem name=\"SavePlainStream\" verb=\"\" _label=\"Save _plain(PersistStream)\" _tip=\"Save using the PersistStream interface\""
139 "                       pixtype=\"stock\" pixname=\"Save\"/>"
140 "                       <separator/>"
141 "                       <menuitem name=\"ViewHTMLSource\" verb=\"\" _label=\"View HTML Source\" _tip=\"View the html source of the current document\"/>"
142 "                       <menuitem name=\"ViewHTMLSourceHTML\" verb=\"\" _label=\"View HTML Output\" _tip=\"View the html source of the current document as html\"/>"
143 "                       <menuitem name=\"ViewPlainSource\" verb=\"\" _label=\"View PLAIN Source\" _tip=\"View the plain text source of the current document\"/>"
144 "                       <separator/>"
145 "                       <menuitem name=\"FileExit\" verb=\"\" _label=\"E_xit\"/>"
146 "               </submenu>"
147 "               <placeholder name=\"Component\"/>"
148 "               <submenu name=\"Format\" _label=\"For_mat\">"
149 "                       <menuitem name=\"FormatHTML\" verb=\"\"/>"
150 "                       <separator/>"
151 "                       <placeholder name=\"FormatParagraph\"/>"
152 "               </submenu>"
153 "       </menu>"
154 "       <dockitem name=\"Toolbar\" behavior=\"exclusive\">"
155 "       </dockitem>"
156 "</Root>";
157
158 static int
159 app_delete_cb (GtkWidget *widget, GdkEvent *event, gpointer dummy)
160 {
161   gtk_widget_destroy (GTK_WIDGET (widget));
162   bonobo_main_quit ();
163
164   return FALSE;
165 }
166
167 static guint
168 container_create (void)
169 {
170   GtkWidget *win;
171   GtkWindow *window;
172   BonoboUIComponent *component;
173   BonoboUIContainer *container;
174   CORBA_Environment ev;
175
176   win = bonobo_window_new ("test-editor",
177                            "GtkMathView Control Test");
178   window = GTK_WINDOW (win);
179
180   container = bonobo_window_get_ui_container (BONOBO_WINDOW (win));
181
182   g_signal_connect (window, "delete_event", G_CALLBACK (app_delete_cb), NULL);
183
184   gtk_window_set_default_size (window, 600, 440);
185   gtk_window_set_resizable (window, TRUE);
186
187   component = bonobo_ui_component_new ("test");
188   bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (component));
189
190   bonobo_ui_component_set_container (component, BONOBO_OBJREF (container), NULL);
191   bonobo_ui_component_add_verb_list_with_data (component, verbs, win);
192   bonobo_ui_component_set_translate (component, "/", ui, NULL);
193
194   control = bonobo_widget_new_control (CONTROL_ID, BONOBO_OBJREF (container));
195
196   if (control == NULL)
197     g_error ("Cannot get `%s'.", CONTROL_ID);
198
199   bonobo_window_set_contents (BONOBO_WINDOW (win), control);
200
201   gtk_widget_show_all (GTK_WIDGET (window));
202
203   CORBA_exception_init (&ev);
204
205   return FALSE;
206 }
207
208 static gint
209 load_file (const gchar *fname)
210 {
211   CORBA_Object interface;
212   CORBA_Environment ev;
213
214   printf ("loading: %s\n", fname);
215   CORBA_exception_init (&ev);
216   interface = Bonobo_Unknown_queryInterface (bonobo_widget_get_objref (BONOBO_WIDGET (control)),
217                                              "IDL:Bonobo/PersistFile:1.0", &ev);
218   CORBA_exception_free (&ev);
219   load_through_persist_file (fname, interface);
220
221   return FALSE;
222 }
223
224 int
225 main (int argc, char **argv)
226 {
227   gnome_program_init("test-editor", VERSION, LIBGNOMEUI_MODULE, argc, argv, 
228                      GNOME_PROGRAM_STANDARD_PROPERTIES,
229                      GNOME_PARAM_HUMAN_READABLE_NAME, _("GtkMathView Test Container"),                     
230                      NULL);
231
232   bonobo_activate ();
233
234   /* We can't make any CORBA calls unless we're in the main loop.  So we
235      delay creating the container here. */
236   gtk_idle_add ((GtkFunction) container_create, NULL);
237   if (argc > 1 && *argv [argc - 1] != '-')
238     gtk_idle_add ((GtkFunction) load_file, argv [argc - 1]);
239
240   bonobo_activate ();
241   bonobo_main ();
242
243   return bonobo_ui_debug_shutdown ();
244 }