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