1 /* This file is part of EdiTeX, an editor of mathematical
2 * expressions based on TeX syntax.
4 * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5 * 2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * For more information, please visit the project's home page
22 * http://helm.cs.unibo.it/editex/
23 * or send an email to <lpadovan@cs.unibo.it>
29 #include <gdk/gdkkeysyms.h>
31 #include <gtkmathview.h>
34 #define XLINK_NS_URI "http://www.w3.org/1999/xlink"
36 static GtkWidget* window;
37 static GtkWidget* main_area;
38 static GtkWidget* scrolled_area;
39 static GtkMenuItem* anti_aliasing_item;
40 static GtkMenuItem* transparency_item;
42 static gpointer context = NULL;
43 static gchar* doc_name = NULL;
44 static GdomeElement* first_selected = NULL;
45 static GdomeElement* root_selected = NULL;
47 static void create_widget_set(gpointer);
48 static GtkWidget* get_main_menu(void);
49 static void file_open(GtkWidget*, gpointer);
50 static void file_re_open(GtkWidget*, gpointer);
51 static void file_close(GtkWidget*, gpointer);
52 static void file_output_tex(GtkWidget*, gpointer);
53 static void options_set_font_size(GtkWidget*, gpointer);
54 static void options_change_font_size(GtkWidget*, gboolean);
55 static void options_verbosity(GtkWidget*, guint);
56 static void options_anti_aliasing(GtkWidget*, gpointer);
57 static void options_transparency(GtkWidget*, gpointer);
58 static void edit_delete_selection(GtkWidget*, gpointer);
59 static void edit_select_parent(GtkWidget*, gpointer);
60 static void edit_reset_selection(GtkWidget*, gpointer);
61 static void edit_reset(GtkWidget*, gpointer);
62 static void edit_insert(GtkWidget*, gpointer);
63 static void help_about(GtkWidget*, gpointer);
65 static GtkItemFactoryEntry menu_items[] = {
66 { "/_File", NULL, NULL, 0, "<Branch>" },
67 { "/File/_Open...", "<control>O", file_open, 0, NULL },
68 { "/File/_Reopen", NULL, file_re_open, 0, NULL },
69 { "/File/_Close", "<control>W", file_close, 0, NULL },
70 { "/File/Output _TeX", NULL, file_output_tex, 0, NULL },
71 { "/File/sep1", NULL, NULL, 0, "<Separator>" },
72 { "/File/_Quit", "<control>Q", gtk_main_quit, 0, NULL },
74 { "/_Edit", NULL, NULL, 0, "<Branch>" },
75 { "/Edit/Reset Selection", NULL, edit_reset_selection, 0, NULL },
76 { "/Edit/Delete Selection", NULL, edit_delete_selection, 0, NULL },
77 { "/Edit/Select Parent", NULL, edit_select_parent, 0, NULL },
78 { "/Edit/sep1", NULL, NULL, 0, "<Separator>" },
79 { "/Edit/_Reset", NULL, edit_reset, 0, NULL },
80 { "/Edit/Insert...", "<control>I", edit_insert, 0, NULL },
82 { "/_Options", NULL, NULL, 0, "<Branch>" },
83 { "/Options/Default _Font Size", NULL, NULL, 0, "<Branch>" },
84 { "/Options/Default Font Size/Set...", NULL, options_set_font_size, 0, NULL },
85 { "/Options/Default Font Size/sep1", NULL, NULL, 0, "<Separator>" },
86 { "/Options/Default Font Size/Larger", NULL, options_change_font_size, TRUE, NULL },
87 { "/Options/Default Font Size/Smaller", NULL, options_change_font_size, FALSE, NULL },
88 { "/Options/Verbosity", NULL, NULL, 0, "<Branch>" },
89 { "/Options/Verbosity/_Errors", NULL, options_verbosity, 0, "<RadioItem>" },
90 { "/Options/Verbosity/_Warnings", NULL, options_verbosity, 1, "/Options/Verbosity/Errors" },
91 { "/Options/Verbosity/_Info", NULL, options_verbosity, 2, "/Options/Verbosity/Errors" },
92 { "/Options/Verbosity/_Debug", NULL, options_verbosity, 3, "/Options/Verbosity/Errors" },
93 { "/Options/sep1", NULL, NULL, 0, "<Separator>" },
94 { "/Options/_Anti Aliasing", NULL, options_anti_aliasing, 0, "<ToggleItem>" },
95 { "/Options/_Transparency", NULL, options_transparency, 0, "<ToggleItem>" },
97 { "/_Help" , NULL, NULL, 0, "<LastBranch>" },
98 { "/Help/About...", NULL, help_about, 0, NULL }
102 quick_message(const char* msg)
106 GtkWidget* okay_button;
108 /* Create the widgets */
110 dialog = gtk_dialog_new();
111 label = gtk_label_new (msg);
112 okay_button = gtk_button_new_with_label("OK");
114 gtk_widget_set_usize(dialog, 300, 100);
116 /* Ensure that the dialog box is destroyed when the user clicks ok. */
118 gtk_signal_connect_object (GTK_OBJECT (okay_button), "clicked",
119 GTK_SIGNAL_FUNC (gtk_widget_destroy), dialog);
120 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
123 /* Add the label, and show everything we've added to the dialog. */
125 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
126 gtk_widget_show_all (dialog);
130 load_error_msg(const char* name)
132 char* msg = g_strdup_printf("Could not load\n`%s'", name);
137 static guint edit_timeout_id;
138 extern void edit_timeout(gpointer);
141 GUI_init(int* argc, char*** argv, char* title, guint width, guint height, gpointer c)
143 gtk_init(argc, argv);
145 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
146 gtk_window_set_title(GTK_WINDOW(window), title);
147 gtk_window_set_default_size(GTK_WINDOW(window), width, height);
148 gtk_signal_connect(GTK_OBJECT(window), "delete_event", (GtkSignalFunc) gtk_main_quit, NULL);
149 create_widget_set(context);
151 gtk_widget_show(window);
154 /*edit_timeout_id = gtk_timeout_add(50, edit_timeout, context);*/
160 GdomeException exc = 0;
162 if (first_selected != NULL)
164 gdome_el_unref(first_selected, &exc);
166 first_selected = NULL;
169 if (root_selected != NULL)
171 gdome_el_unref(root_selected, &exc);
173 root_selected = NULL;
180 GUI_load_document(GdomeDocument* doc)
182 GtkMathView* math_view;
184 g_return_val_if_fail(doc != NULL, -1);
185 g_return_val_if_fail(main_area != NULL, -1);
186 g_return_val_if_fail(GTK_IS_MATH_VIEW(main_area), -1);
188 math_view = GTK_MATH_VIEW(main_area);
190 if (!gtk_math_view_load_doc(math_view, doc)) return -1;
198 gtk_math_view_freeze(GTK_MATH_VIEW(main_area));
204 gtk_math_view_thaw(GTK_MATH_VIEW(main_area));
208 GUI_unload_document()
210 GtkMathView* math_view;
212 g_return_if_fail(main_area != NULL);
213 g_return_if_fail(GTK_IS_MATH_VIEW(main_area));
215 math_view = GTK_MATH_VIEW(main_area);
217 gtk_math_view_unload(math_view);
219 if (doc_name != NULL) g_free(doc_name);
231 GUI_set_font_manager(FontManagerId id)
234 GtkMathView* math_view;
236 g_return_if_fail(id != FONT_MANAGER_UNKNOWN);
237 g_return_if_fail(main_area != NULL);
238 g_return_if_fail(GTK_IS_MATH_VIEW(main_area));
240 t1 = id == FONT_MANAGER_T1;
242 math_view = GTK_MATH_VIEW(main_area);
244 gtk_math_view_freeze(math_view);
246 if (id != gtk_math_view_get_font_manager_type(math_view))
247 gtk_math_view_set_font_manager_type(math_view, id);
249 gtk_widget_set_sensitive(anti_aliasing_item, t1);
250 gtk_widget_set_sensitive(transparency_item, t1);
254 gtk_math_view_set_anti_aliasing(math_view, GTK_CHECK_MENU_ITEM(anti_aliasing_item)->active);
255 gtk_math_view_set_transparency(math_view, GTK_CHECK_MENU_ITEM(transparency_item)->active);
258 gtk_math_view_thaw(math_view);
263 store_filename(GtkFileSelection* selector, GtkWidget* user_data)
265 gchar* selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION(user_data));
266 if (selected_filename != NULL)
267 GUI_load_document(selected_filename);
271 file_close(GtkWidget* widget, gpointer data)
273 GUI_unload_document();
277 file_re_open(GtkWidget* widget, gpointer data)
279 if (doc_name != NULL) {
280 GUI_load_document(doc_name);
285 file_open(GtkWidget* widget, gpointer data)
287 GtkWidget* fs = gtk_file_selection_new("Open File");
289 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
290 "clicked", GTK_SIGNAL_FUNC (store_filename), (gpointer) fs);
292 /* Ensure that the dialog box is destroyed when the user clicks a button. */
294 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
295 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
298 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
299 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
302 /* Display that dialog */
304 gtk_widget_show (fs);
308 file_output_tex(GtkWidget* widget, gpointer data)
310 g_assert(context != NULL);
311 edit_output_tex(context);
316 options_font_manager(GtkWidget* widget, FontManagerId id)
318 g_return_if_fail(id != FONT_MANAGER_UNKNOWN);
319 GUI_set_font_manager(id);
324 options_anti_aliasing(GtkWidget* widget, gpointer data)
326 gboolean aa = gtk_math_view_get_anti_aliasing(GTK_MATH_VIEW(main_area));
327 gtk_math_view_set_anti_aliasing(GTK_MATH_VIEW(main_area), !aa);
331 options_transparency(GtkWidget* widget, gpointer data)
333 gboolean t = gtk_math_view_get_transparency(GTK_MATH_VIEW(main_area));
334 gtk_math_view_set_transparency(GTK_MATH_VIEW(main_area), !t);
338 options_verbosity(GtkWidget* widget, guint level)
340 gtk_math_view_set_log_verbosity(GTK_MATH_VIEW(main_area), level);
344 edit_delete_selection(GtkWidget* widget, gpointer data)
346 if (root_selected != NULL)
349 gtk_math_view_freeze(GTK_MATH_VIEW(main_area));
350 printf("about to remove element %p\n", root_selected);
351 delete_element(root_selected);
352 gdome_el_unref(root_selected, &exc);
354 root_selected = NULL;
355 gtk_math_view_thaw(GTK_MATH_VIEW(main_area));
360 edit_select_parent(GtkWidget* widget, gpointer data)
362 if (root_selected != NULL)
364 GdomeException exc = 0;
365 GdomeElement* parent = gdome_n_parentNode(root_selected, &exc);
367 gdome_el_unref(root_selected, &exc);
369 root_selected = parent;
370 /* gtk_math_view_set_selection(GTK_MATH_VIEW(main_area), root_selected); */
375 edit_reset_selection(GtkWidget* widget, gpointer data)
377 if (root_selected != NULL)
379 GdomeException exc = 0;
380 /* gtk_math_view_reset_selection(GTK_MATH_VIEW(main_area), root_selected); */
381 gdome_el_unref(root_selected, &exc);
383 root_selected = NULL;
388 edit_reset(GtkWidget* widget, gpointer data)
390 g_assert(context != NULL);
391 edit_reset_tex(context);
395 insert_tex(GtkWidget* widget, GtkEntry* entry)
398 g_return_if_fail(entry != NULL);
400 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
401 edit_push_string(context, text);
406 edit_insert(GtkWidget* widget, gpointer data)
413 dialog = gtk_dialog_new();
414 entry = gtk_entry_new();
415 ok = gtk_button_new_with_label("OK");
416 cancel = gtk_button_new_with_label("Cancel");
418 gtk_signal_connect (GTK_OBJECT (ok), "clicked",
419 GTK_SIGNAL_FUNC (insert_tex), (gpointer) entry);
421 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
422 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
424 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
425 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
427 gtk_signal_connect_object (GTK_OBJECT (cancel), "clicked",
428 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
430 gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), 5);
432 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), entry);
433 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), ok);
434 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), cancel);
436 gtk_widget_show_all (dialog);
440 help_about(GtkWidget* widget, gpointer data)
446 dialog = gtk_dialog_new();
447 label = gtk_label_new("\n MathML Editor \n Copyright (C) 2003 Luca Padovani \n");
448 ok = gtk_button_new_with_label("Close");
450 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
451 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
452 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
455 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
457 gtk_widget_show_all (dialog);
461 change_default_font_size(GtkWidget* widget, GtkSpinButton* spin)
463 g_return_if_fail(spin != NULL);
464 gtk_math_view_set_font_size( GTK_MATH_VIEW(main_area), gtk_spin_button_get_value_as_int(spin));
468 options_change_font_size(GtkWidget* widget, gboolean larger)
470 gfloat size = gtk_math_view_get_font_size (GTK_MATH_VIEW(main_area));
471 if (larger) size = size / 0.71;
472 else size = size * 0.71;
473 if (size < 1) size = 1;
474 gtk_math_view_set_font_size (GTK_MATH_VIEW(main_area), (gint) size + 0.5);
478 options_set_font_size(GtkWidget* widget, gpointer data)
487 dialog = gtk_dialog_new();
488 label = gtk_label_new("Default font size:");
489 ok = gtk_button_new_with_label("OK");
490 cancel = gtk_button_new_with_label("Cancel");
492 adj = gtk_adjustment_new (gtk_math_view_get_font_size (GTK_MATH_VIEW(main_area)), 1, 200, 1, 1, 1);
493 spin = gtk_spin_button_new (GTK_ADJUSTMENT(adj), 1, 0);
494 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
496 gtk_signal_connect (GTK_OBJECT (ok), "clicked",
497 GTK_SIGNAL_FUNC (change_default_font_size), (gpointer) spin);
499 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
500 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
502 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
503 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
505 gtk_signal_connect_object (GTK_OBJECT (cancel), "clicked",
506 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
508 gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), 5);
510 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), ok);
511 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), cancel);
512 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
513 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), spin);
515 gtk_widget_show_all (dialog);
519 select_begin(GtkMathView* math_view, GdomeElement* first, gint state)
521 GdomeException exc = 0;
523 g_return_if_fail(math_view != NULL);
524 g_return_if_fail(GTK_IS_MATH_VIEW(math_view));
525 g_return_if_fail(first != NULL);
527 gtk_math_view_freeze(math_view);
529 if (root_selected != NULL)
531 gtk_math_view_unselect(math_view, root_selected);
532 gdome_el_unref(root_selected, &exc);
536 root_selected = first_selected = find_element_with_ref(first);
538 if (root_selected != NULL)
540 gtk_math_view_select(math_view, root_selected);
541 gdome_el_ref(root_selected, &exc);
545 gtk_math_view_thaw(math_view);
549 select_over(GtkMathView* math_view, GdomeElement* elem, gint state)
551 GdomeElement* new_selected = NULL;
552 GdomeException exc = 0;
554 g_return_if_fail(math_view != NULL);
555 g_return_if_fail(GTK_IS_MATH_VIEW(math_view));
556 g_return_if_fail(elem != NULL);
558 if (first_selected == NULL || elem == NULL)
561 new_selected = find_common_ancestor_with_ref(first_selected, elem);
563 if (new_selected != root_selected)
565 gtk_math_view_freeze(math_view);
566 if (root_selected != NULL)
568 gtk_math_view_unselect(math_view, root_selected);
569 gdome_el_unref(root_selected, &exc);
572 root_selected = new_selected;
573 if (root_selected != NULL)
574 gtk_math_view_select(math_view, root_selected);
575 gtk_math_view_thaw(math_view);
577 else if (new_selected != NULL)
579 gdome_el_unref(new_selected, &exc);
586 key_press_event(gpointer c,
590 g_return_val_if_fail(widget != NULL, FALSE);
591 g_return_val_if_fail(event != NULL, FALSE);
592 g_return_val_if_fail(context != NULL, FALSE);
594 if (event->type != GDK_KEY_PRESS) return FALSE;
596 switch (event->keyval)
599 edit_drop(context, event->state & GDK_MOD1_MASK, event->state & GDK_CONTROL_MASK);
602 edit_complete(context);
605 if ((event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) == 0 && event->keyval < 0x80)
606 edit_push_char(context, event->keyval);
614 create_widget_set(gpointer context)
616 GtkWidget* main_vbox;
619 main_vbox = gtk_vbox_new(FALSE, 1);
620 gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
621 gtk_container_add(GTK_CONTAINER(window), main_vbox);
622 gtk_widget_show(main_vbox);
624 menu_bar = get_main_menu();
625 gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar, FALSE, TRUE, 0);
626 gtk_widget_show(menu_bar);
628 main_area = gtk_math_view_new(NULL, NULL);
629 gtk_widget_show(main_area);
631 //gtk_math_view_set_log_verbosity(GTK_MATH_VIEW(main_area), 3);
633 gtk_signal_connect_object (GTK_OBJECT (main_area),
634 "select_begin", GTK_SIGNAL_FUNC (select_begin),
635 (gpointer) main_area);
637 gtk_signal_connect_object (GTK_OBJECT (main_area),
638 "select_over", GTK_SIGNAL_FUNC (select_over),
639 (gpointer) main_area);
641 gtk_signal_connect_object (GTK_OBJECT(window),
642 "key_press_event", GTK_SIGNAL_FUNC(key_press_event),
645 gtk_widget_add_events(GTK_WIDGET(main_area), GDK_KEY_PRESS_MASK);
647 scrolled_area = gtk_scrolled_window_new(NULL, NULL);
648 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_area),
649 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
651 gtk_widget_show(scrolled_area);
652 gtk_container_add(GTK_CONTAINER(scrolled_area), main_area);
653 gtk_box_pack_start(GTK_BOX(main_vbox), scrolled_area, TRUE, TRUE, 0);
655 gtk_widget_show(main_vbox);
657 if (gtk_math_view_get_anti_aliasing(GTK_MATH_VIEW(main_area)))
658 gtk_menu_item_activate(anti_aliasing_item);
664 GtkItemFactory* item_factory;
665 GtkAccelGroup* accel_group;
666 GtkWidget* menu_item;
668 gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
670 accel_group = gtk_accel_group_new();
672 item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);
674 gtk_item_factory_create_items(item_factory, nmenu_items, menu_items, NULL);
676 gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
678 menu_item = gtk_item_factory_get_widget(item_factory, "/Options/Anti Aliasing");
679 anti_aliasing_item = GTK_MENU_ITEM(menu_item);
681 menu_item = gtk_item_factory_get_widget(item_factory, "/Options/Transparency");
682 transparency_item = GTK_MENU_ITEM(menu_item);
684 return gtk_item_factory_get_widget(item_factory, "<main>");