1 /* This file is part of GtkMathView-Bonobo, a Bonobo wrapper for GtkMathView.
2 * Copyright (C) 2003 Luca Padovani <lpadovan@cs.unibo.it>
3 * Pouria Masoudi <pmasoudi@cs.unibo.it>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * For more information, please visit the project's home page
20 * http://helm.cs.unibo.it/gtkmathview-bonobo
21 * or send an email to <lpadovan@cs.unibo.it>
27 #include <bonobo/bonobo-shlib-factory.h>
28 #include <gtkmathview.h>
30 #include "control-factory.h"
31 #include "control-data.h"
32 #include "persist-file.h"
33 #include "persist-stream.h"
50 activate_maction(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
54 g_return_if_fail(control_data != NULL);
56 elem = gtk_math_view_get_element_at(control_data->math_view, control_data->x, control_data->y);
59 GdomeException exc = 0;
60 GdomeElement* action = find_self_or_ancestor(elem, MATHML_NS_URI, "maction");
63 gtk_math_view_freeze(control_data->math_view);
64 action_toggle(action);
65 gtk_math_view_thaw(control_data->math_view);
66 gdome_el_unref(action, &exc);
69 gdome_el_unref(elem, &exc);
75 copy_link_address(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
80 zoom_in(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
82 g_return_if_fail(control_data != NULL);
83 guint old_font_size = gtk_math_view_get_font_size(control_data->math_view);
84 gtk_math_view_set_font_size(control_data->math_view, old_font_size + 1);
88 zoom_out(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
90 g_return_if_fail(control_data != NULL);
91 guint old_font_size = gtk_math_view_get_font_size(control_data->math_view);
92 gtk_math_view_set_font_size(control_data->math_view, old_font_size - 1);
96 switch_semantic_selection(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
98 g_return_if_fail(control_data != NULL);
99 control_data->semantic_selection = !control_data->semantic_selection;
100 if (control_data->semantic_selection)
102 GdomeElement* elem = find_element_with_id(control_data->root_selected,
103 control_data->id_ns_uri,
104 control_data->id_name);
110 copy_selected_markup(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
112 gchar* res = copy_markup(control_data->root_selected);
122 copy_selected_id(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
127 select_parent(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
129 g_return_if_fail(control_data != NULL);
130 if (control_data->root_selected != NULL)
132 GdomeException exc = 0;
133 GdomeNode* parent_node = gdome_el_parentNode(control_data->root_selected, &exc);
134 g_assert(parent_node != NULL);
136 GdomeElement* parent = gdome_cast_el(parent_node);
137 gtk_math_view_control_data_set_root_selected(control_data, parent);
138 gdome_n_unref(parent_node, &exc);
144 deselect(GtkMathViewControlData* control_data, guint action, GtkWidget* widget)
146 g_return_if_fail(control_data != NULL);
148 if (control_data->root_selected != NULL)
150 gtk_math_view_unselect(control_data->math_view, control_data->root_selected);
151 control_data->root_selected = NULL;
155 /* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item */
156 static GtkItemFactoryEntry menu_items[] = {
157 { "/Activate <maction>", NULL, activate_maction, 0, "<StockItem>", GTK_STOCK_EXECUTE },
158 { "/Copy Link Address", NULL, copy_link_address, 0, "<StockItem>", GTK_STOCK_COPY },
159 /* { "/Jump To", NULL, NULL, 0, "<StockItem>", GTK_STOCK_JUMP_TO }, */
160 { "/sep1", NULL, NULL, 0, "<Separator>" },
161 /* { "/Semantic Selection", NULL, switch_semantic_selection, 0, "<CheckItem>" }, */
162 /* { "/Copy Selected Markup", NULL, copy_selected_markup, 0, "<Item>" }, */
163 /* { "/Copy Id", NULL, copy_selected_id, 0, "<Item>" }, */
164 /* { "/Show Selected", NULL, NULL, 0, "<Item>" }, */
165 { "/Select Parent", NULL, select_parent, 0, "<Item>" },
166 { "/De-Select", NULL, deselect, 0, "<StockItem>", GTK_STOCK_CLEAR },
167 { "/sep2", NULL, NULL, 0, "<Separator>" },
168 { "/Smaller", NULL, zoom_out, 0, "<StockItem>", GTK_STOCK_ZOOM_OUT },
169 { "/Bigger", NULL, zoom_in, 0, "<StockItem>", GTK_STOCK_ZOOM_IN },
170 /* { "/Properties...", NULL, NULL, 0, "<StockItem>", GTK_STOCK_PROPERTIES }, */
173 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
176 button_pressed_cb(GtkMathView* math_view, GdkEventButton* event, GtkMathViewControlData* control_data)
178 g_return_if_fail(math_view != NULL);
179 g_return_if_fail(event != NULL);
180 g_return_if_fail(control_data != NULL);
182 if (event->button == 3)
185 gtk_menu_popup (GTK_MENU(control_data->popup_menu), NULL, NULL,
186 NULL, event, event->button, event->time);
188 control_data->x = (gint) event->x;
189 control_data->y = (gint) event->y;
190 printf("data %d %d\n", control_data->x, control_data->y);
192 gtk_item_factory_popup_with_data(control_data->item_factory,
193 NULL, NULL, event->x_root, event->y_root,
194 event->button, gtk_get_current_event_time());
200 get_prop(BonoboPropertyBag* bag,
203 CORBA_Environment *ev,
206 GtkMathViewControlData* control_data = user_data;
207 g_assert(control_data != NULL);
211 case MATH_VIEW_WIDTH:
212 BONOBO_ARG_SET_INT(arg, gtk_math_view_get_width(control_data->math_view));
214 case MATH_VIEW_HEIGHT:
215 BONOBO_ARG_SET_INT(arg, gtk_math_view_get_height(control_data->math_view));
217 case MATH_VIEW_TOP_X:
220 gtk_math_view_get_top(control_data->math_view, &top_x, NULL);
221 BONOBO_ARG_SET_INT(arg, top_x);
224 case MATH_VIEW_TOP_Y:
227 gtk_math_view_get_top(control_data->math_view, NULL, &top_y);
228 BONOBO_ARG_SET_INT(arg, top_y);
231 case MATH_VIEW_FONT_SIZE:
232 BONOBO_ARG_SET_INT(arg, gtk_math_view_get_font_size(control_data->math_view));
234 case MATH_VIEW_VERBOSITY:
235 BONOBO_ARG_SET_INT(arg, gtk_math_view_get_log_verbosity(control_data->math_view));
237 case MATH_VIEW_ID_NS_URI:
239 gchar* id_ns_uri = gtk_math_view_control_data_get_id_ns_uri(control_data);
240 BONOBO_ARG_SET_STRING(arg, id_ns_uri);
244 case MATH_VIEW_ID_NAME:
246 gchar* id_name = gtk_math_view_control_data_get_id_name(control_data);
247 BONOBO_ARG_SET_STRING(arg, id_name);
252 bonobo_exception_set (ev, ex_Bonobo_PropertyBag_NotFound);
258 set_prop(BonoboPropertyBag* bag,
259 const BonoboArg* arg,
261 CORBA_Environment* ev,
264 GtkMathViewControlData *control_data = user_data;
265 g_assert(control_data != NULL);
269 case MATH_VIEW_TOP_X:
272 gtk_math_view_get_top(control_data->math_view, NULL, &old_top_y);
273 gtk_math_view_set_top(control_data->math_view, BONOBO_ARG_GET_INT(arg), old_top_y);
276 case MATH_VIEW_TOP_Y:
279 gtk_math_view_get_top(control_data->math_view, &old_top_x, NULL);
280 gtk_math_view_set_top(control_data->math_view, BONOBO_ARG_GET_INT(arg), old_top_x);
283 case MATH_VIEW_FONT_SIZE:
284 gtk_math_view_set_font_size(control_data->math_view, BONOBO_ARG_GET_INT(arg));
286 case MATH_VIEW_VERBOSITY:
287 gtk_math_view_set_log_verbosity(control_data->math_view, BONOBO_ARG_GET_INT(arg));
289 case MATH_VIEW_ID_NS_URI:
290 gtk_math_view_control_data_set_id_ns_uri(control_data, BONOBO_ARG_GET_STRING(arg));
292 case MATH_VIEW_ID_NAME:
293 gtk_math_view_control_data_set_id_name(control_data, BONOBO_ARG_GET_STRING(arg));
296 bonobo_exception_set (ev, ex_Bonobo_PropertyBag_NotFound);
302 control_destroy(BonoboObject *object, GtkMathViewControlData *cd)
304 gtk_math_view_control_data_destroy(cd);
308 gtk_math_view_control_init(BonoboControl *control, GtkWidget *scrolled_window)
310 GtkMathViewControlData *control_data;
311 GtkWidget *math_view;
312 GtkItemFactory *item_factory;
314 Bonobo_UIContainer remote_ui_container;
315 BonoboUIComponent *ui_component;
317 BonoboPropertyBag *prop_bag;
318 BonoboObject *persist_file;
319 BonoboObject *persist_stream;
320 BonoboEventSource *evs;
323 math_view = gtk_math_view_new(NULL,NULL);
324 gtk_widget_show(math_view);
326 control_data = gtk_math_view_control_data_new(control, (GtkMathView*)math_view);
328 g_signal_connect (control, "destroy", G_CALLBACK (control_destroy), control_data);
330 /* Same as before but don't bother with the accelerators */
331 control_data->item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>", NULL);
332 gtk_item_factory_create_items (control_data->item_factory, nmenu_items, menu_items, control_data);
333 /* control_data->popup_menu = gtk_item_factory_get_widget (control_data->item_factory, "<main>"); */
334 /* gtk_widget_ref(control_data->popup_menu); */
336 evs = bonobo_event_source_new();
337 bonobo_object_add_interface(BONOBO_OBJECT(control), BONOBO_OBJECT(evs));
339 gtk_container_add(GTK_CONTAINER (scrolled_window), GTK_WIDGET (control_data->math_view));
341 view = view_new(control_data);
342 bonobo_object_add_interface(BONOBO_OBJECT(control), BONOBO_OBJECT(view));
344 persist_file = gtk_math_view_persist_file_new(GTK_MATH_VIEW(math_view));
345 bonobo_object_add_interface(BONOBO_OBJECT(control), persist_file);
347 persist_stream = gtk_math_view_persist_stream_new(GTK_MATH_VIEW(math_view));
348 bonobo_object_add_interface(BONOBO_OBJECT(control), persist_stream);
350 prop_bag = bonobo_property_bag_new(get_prop, set_prop, control_data);
351 bonobo_control_set_properties(control, BONOBO_OBJREF(prop_bag), NULL);
353 bonobo_property_bag_add(prop_bag, "width",
354 MATH_VIEW_WIDTH, BONOBO_ARG_INT,
356 "Width of the view", 0);
357 bonobo_property_bag_add(prop_bag, "height",
358 MATH_VIEW_HEIGHT, BONOBO_ARG_INT,
360 "Height of the view", 0);
361 bonobo_property_bag_add(prop_bag, "top-x",
362 MATH_VIEW_TOP_X, BONOBO_ARG_INT,
364 "X coordinate of the top-left corner", 0);
365 bonobo_property_bag_add(prop_bag, "top-y",
366 MATH_VIEW_TOP_Y, BONOBO_ARG_INT,
368 "Y coordinate of the top-left corner", 0);
369 bonobo_property_bag_add(prop_bag, "font-size",
370 MATH_VIEW_FONT_SIZE, BONOBO_ARG_INT,
372 "Default font size", 0);
373 bonobo_property_bag_add(prop_bag,"verbosity",
374 MATH_VIEW_VERBOSITY, BONOBO_ARG_INT,
376 "Verbosity level", 0);
377 bonobo_property_bag_add(prop_bag, "id-ns-uri",
378 MATH_VIEW_ID_NS_URI, BONOBO_ARG_STRING,
380 "Namespace URI of ID attribute", 0);
381 bonobo_property_bag_add(prop_bag, "id-name",
382 MATH_VIEW_ID_NAME, BONOBO_ARG_STRING,
384 "Name of ID attribute", 0);
386 bonobo_object_unref(BONOBO_OBJECT(prop_bag));
388 g_signal_connect(control,"set_frame", G_CALLBACK(set_frame), control_data);
389 g_signal_connect(control_data->math_view, "button_press_event", G_CALLBACK (button_pressed_cb),
391 g_signal_connect(control_data->math_view, "click", G_CALLBACK (click_cb),
393 g_signal_connect(control_data->math_view, "select_begin", G_CALLBACK(select_begin_cb),
395 g_signal_connect(control_data->math_view, "select_over", G_CALLBACK(select_over_cb),
397 g_signal_connect(control_data->math_view, "select_end", G_CALLBACK(select_end_cb),
399 g_signal_connect(control_data->math_view, "select_abort", G_CALLBACK(select_abort_cb),
404 gtk_math_view_control_factory(BonoboGenericFactory* factory, const gchar* component_id,
407 BonoboControl *control;
408 GtkWidget *scrolled_window;
410 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
411 /* putting SHADOW_NONE screws the plugin window, how's that??? */
412 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
413 gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 0);
414 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window),
415 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
416 gtk_widget_show_all (scrolled_window);
418 control = bonobo_control_new(scrolled_window);
422 gtk_math_view_control_init(control,scrolled_window);
423 return BONOBO_OBJECT(control);
429 BONOBO_ACTIVATION_SHLIB_FACTORY (CONTROL_FACTORY_ID, "GtkMathView Factory",
430 gtk_math_view_control_factory, NULL);