2 * Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
4 * This file is part of HelmPot, a minimal browser for HELM.
6 * HelmPot is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * HelmPot is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with HelmPot; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * For details, see the HelmPot World-Wide-Web page,
21 * http://cs.unibo.it/helm/helmview, or send a mail to
22 * <luca.padovani@cs.unibo.it>
32 # include <sys/types.h>
33 # include <sys/stat.h>
38 #include <gdk/gdkkeysyms.h>
40 #include "gtkmathview.h"
43 #define XLINK_NS_URI "http://www.w3.org/1999/xlink"
47 PRIVATE unsigned char pot_bits[] = {
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x07,
49 0x00, 0x00, 0xe6, 0x2f, 0x34, 0x5f, 0xdc, 0x5f, 0xf8, 0x3f, 0xf8, 0x3f,
50 0xf0, 0x1f, 0xe0, 0x0f, 0x00, 0x00, 0x00, 0x00};
52 #define pot_mask_width 16
53 #define pot_mask_height 16
54 static unsigned char pot_mask_bits[] = {
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x03, 0xe0, 0x0f, 0xe0, 0x0f,
56 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfc, 0x7f,
57 0xfc, 0x7f, 0xf8, 0x3f, 0xf0, 0x1f, 0x00, 0x00};
59 PRIVATE GtkWidget* window;
60 PRIVATE GtkWidget* main_area;
61 PRIVATE GtkWidget* scrolled_area;
62 PRIVATE GtkWidget* status_bar;
63 PRIVATE GtkMenuItem* kerning_item;
64 PRIVATE GtkMenuItem* anti_aliasing_item;
65 PRIVATE GtkMenuItem* font_size_item;
66 PRIVATE gchar* file_name = NULL;
68 PRIVATE GdkCursor* pot;
70 PRIVATE guint statusbar_context;
72 PRIVATE void create_widget_set(void);
73 PRIVATE GtkWidget* get_main_menu(void);
74 PRIVATE void options_font_size(GtkWidget*, guint);
75 PRIVATE void options_font_manager(GtkWidget*, guint);
76 PRIVATE void options_verbosity(GtkWidget*, guint);
77 PRIVATE void options_kerning(GtkWidget*, gpointer);
78 PRIVATE void options_anti_aliasing(GtkWidget*, gpointer);
79 PRIVATE void help_about(GtkWidget*, gpointer);
80 PRIVATE void save_as(GtkWidget*);
81 PRIVATE void export_to_ps(GtkWidget*);
83 PRIVATE GtkItemFactoryEntry menu_items[] = {
84 { "/_File", NULL, NULL, 0, "<Branch>" },
85 { "/File/Save _As...", NULL, save_as, 0, NULL },
86 { "/File/_Export to PostScript...", NULL, export_to_ps, 0, NULL },
87 { "/File/sep1", NULL, NULL, 0, "<Separator>" },
88 { "/File/_Quit", "<control>Q", gtk_main_quit, 0, NULL },
90 { "/_Options", NULL, NULL, 0, "<Branch>" },
91 { "/Options/Default _Font Size", NULL, NULL, 0, "<Branch>" },
92 { "/Options/Default Font Size/8pt", NULL, options_font_size, 8, "<RadioItem>" },
93 { "/Options/Default Font Size/10pt", NULL, options_font_size, 10, "/Options/Default Font Size/8pt" },
94 { "/Options/Default Font Size/12pt", NULL, options_font_size, 12, "/Options/Default Font Size/8pt" },
95 { "/Options/Default Font Size/14pt", NULL, options_font_size, 14, "/Options/Default Font Size/8pt" },
96 { "/Options/Default Font Size/18pt", NULL, options_font_size, 18, "/Options/Default Font Size/8pt" },
97 { "/Options/Default Font Size/24pt", NULL, options_font_size, 24, "/Options/Default Font Size/8pt" },
98 { "/Options/Font Manager", NULL, NULL, 0, "<Branch>" },
99 { "/Options/Font Manager/_GTK", NULL, options_font_manager, 0, "<RadioItem>" },
100 { "/Options/Font Manager/_Type 1", NULL, options_font_manager, 1, "/Options/Font Manager/GTK" },
101 { "/Options/Verbosity", NULL, NULL, 0, "<Branch>" },
102 { "/Options/Verbosity/_Errors", NULL, options_verbosity, 0, "<RadioItem>" },
103 { "/Options/Verbosity/_Warnings", NULL, options_verbosity, 1, "/Options/Verbosity/Errors" },
104 { "/Options/Verbosity/_Info", NULL, options_verbosity, 2, "/Options/Verbosity/Errors" },
105 { "/Options/Verbosity/_Debug", NULL, options_verbosity, 3, "/Options/Verbosity/Errors" },
106 { "/Options/sep1", NULL, NULL, 0, "<Separator>" },
107 { "/Options/_Kerning", NULL, options_kerning, 0, "<ToggleItem>" },
108 { "/Options/_Anti Aliasing", NULL, options_anti_aliasing, 0, "<ToggleItem>" },
110 { "/_Help" , NULL, NULL, 0, "<LastBranch>" },
111 { "/Help/About...", NULL, help_about, 0, NULL }
115 quick_message(const gchar* msg)
119 GtkWidget* okay_button;
121 /* Create the widgets */
123 dialog = gtk_dialog_new();
124 label = gtk_label_new (msg);
125 okay_button = gtk_button_new_with_label("OK");
127 gtk_widget_set_usize(dialog, 300, 100);
129 /* Ensure that the dialog box is destroyed when the user clicks ok. */
131 gtk_signal_connect_object (GTK_OBJECT (okay_button), "clicked",
132 GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT(dialog));
133 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
136 /* Add the label, and show everything we've added to the dialog. */
138 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
139 gtk_widget_show_all (dialog);
143 load_error_msg(const gchar* name)
145 gchar* msg = g_strdup_printf("Could not load\n`%s'", name);
151 save_error_msg(const gchar* name)
153 gchar* msg = g_strdup_printf("Could not save\n`%s'", name);
159 GUI_init(gint* argc, gchar*** argv, gchar* title, guint width, guint height, GtkFunction f, guint32 timeout)
164 GdkColor fg = { 0, 65535, 65535, 65535 };
165 GdkColor bg = { 0, 0, 0, 0 };
167 gtk_init(argc, argv);
169 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
170 gtk_window_set_title(GTK_WINDOW(window), title);
171 gtk_window_set_default_size(GTK_WINDOW(window), width, height);
172 gtk_signal_connect(GTK_OBJECT(window), "delete_event", (GtkSignalFunc) gtk_main_quit, NULL);
175 gtk_widget_show(window);
177 gtk_timeout_add(timeout, f, NULL);
179 source = gdk_bitmap_create_from_data (NULL, pot_bits,
180 pot_width, pot_height);
181 mask = gdk_bitmap_create_from_data (NULL, pot_mask_bits,
182 pot_mask_width, pot_mask_height);
183 pot = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
184 gdk_pixmap_unref (source);
185 gdk_pixmap_unref (mask);
194 GUI_load_document(const gchar* name)
196 GtkMathView* math_view;
197 GtkMathViewClass* klass;
199 g_return_val_if_fail(name != NULL, -1);
200 g_return_val_if_fail(main_area != NULL, -1);
201 g_return_val_if_fail(GTK_IS_MATH_VIEW(main_area), -1);
203 math_view = GTK_MATH_VIEW(main_area);
204 g_return_val_if_fail(math_view != NULL, -1);
206 klass = (GtkMathViewClass*) gtk_type_class(gtk_math_view_get_type());
207 g_return_val_if_fail(klass != NULL, -1);
209 gdk_window_set_cursor (main_area->window, pot);
211 if (!gtk_math_view_load(math_view, name)) {
212 load_error_msg(name);
216 gtk_statusbar_pop(GTK_STATUSBAR(status_bar), statusbar_context);
217 if (strlen(name) > 40) name += strlen(name) - 40;
218 gtk_statusbar_push(GTK_STATUSBAR(status_bar), statusbar_context, name);
220 /*gdk_window_set_cursor(gtk_widget_get_parent_window(main_area), klass->normal_cursor);*/
222 if (file_name != NULL) g_free(file_name);
223 file_name = g_strdup(name);
229 GUI_unload_document()
231 GtkMathView* math_view;
233 g_return_if_fail(main_area != NULL);
234 g_return_if_fail(GTK_IS_MATH_VIEW(main_area));
236 math_view = GTK_MATH_VIEW(main_area);
238 gtk_math_view_unload(math_view);
248 options_font_size(GtkWidget* widget, guint size)
250 GtkMathView* math_view;
252 g_return_if_fail(main_area != NULL);
253 g_return_if_fail(GTK_IS_MATH_VIEW(main_area));
255 math_view = GTK_MATH_VIEW(main_area);
257 gtk_math_view_set_font_size(math_view, size);
261 options_font_manager(GtkWidget* widget, guint id)
263 GtkMathView* math_view;
265 g_return_if_fail(main_area != NULL);
266 g_return_if_fail(GTK_IS_MATH_VIEW(main_area));
268 math_view = GTK_MATH_VIEW(main_area);
270 gtk_math_view_set_font_manager_type(math_view, id);
274 options_anti_aliasing(GtkWidget* widget, gpointer data)
276 gboolean aa = gtk_math_view_get_anti_aliasing(GTK_MATH_VIEW(main_area));
277 gtk_math_view_set_anti_aliasing(GTK_MATH_VIEW(main_area), !aa);
281 options_kerning(GtkWidget* widget, gpointer data)
283 gboolean k = gtk_math_view_get_kerning(GTK_MATH_VIEW(main_area));
284 gtk_math_view_set_kerning(GTK_MATH_VIEW(main_area), !k);
288 options_verbosity(GtkWidget* widget, guint level)
290 gtk_math_view_set_log_verbosity(GTK_MATH_VIEW(main_area), level);
294 help_about(GtkWidget* widget, gpointer data)
300 dialog = gtk_dialog_new();
301 label = gtk_label_new("\n HELM PoT \n Copyright (C) 2001 Luca Padovani \n");
302 ok = gtk_button_new_with_label("Close");
304 gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
305 GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) dialog);
306 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
309 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
311 gtk_widget_show_all (dialog);
315 export_filename(GtkFileSelection* selector, gpointer user_data)
318 GtkMathView* math_view;
319 gchar* selected_filename;
321 selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION(user_data));
323 math_view = GTK_MATH_VIEW(main_area);
325 f = fopen(selected_filename, "wt");
327 save_error_msg(selected_filename);
328 /*g_free(selected_filename);*/
332 gtk_math_view_export_to_postscript(math_view,
333 (21 * SCALED_POINTS_PER_CM) / SCALED_POINTS_PER_PX,
334 (29 * SCALED_POINTS_PER_CM) / SCALED_POINTS_PER_PX,
335 SCALED_POINTS_PER_IN / SCALED_POINTS_PER_PX,
336 SCALED_POINTS_PER_IN / SCALED_POINTS_PER_PX,
341 /*g_free(selected_filename);*/
345 save_filename(GtkFileSelection* selector, gpointer user_data)
350 gchar* selected_filename;
352 if (file_name == NULL) return;
354 source = fopen(file_name, "rt");
355 if (source == NULL) {
356 load_error_msg(file_name);
360 selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION(user_data));
361 if (selected_filename == NULL) return;
363 dest = fopen(selected_filename, "wt");
365 save_error_msg(selected_filename);
366 /*g_free(selected_filename);*/
370 /*g_free(selected_filename);*/
372 buffer = g_new(gchar, 2048);
373 while (!feof(source)) {
374 size_t n = fread(buffer, sizeof(gchar), 2048, source);
375 fwrite(buffer, sizeof(gchar), n, dest);
384 file_dialog(const gchar* title, GtkSignalFunc f)
388 g_return_if_fail(title != NULL);
389 g_return_if_fail(f != NULL);
391 fs = gtk_file_selection_new(title);
393 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
396 /* Ensure that the dialog box is destroyed when the user clicks a button. */
398 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
399 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
402 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
403 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
406 /* Display that dialog */
408 gtk_widget_show (fs);
412 export_to_ps(GtkWidget* widget)
414 file_dialog("Export to PostScript", export_filename);
416 static GList* items = NULL;
422 items = g_list_append(items, "A4");
423 items = g_list_append(items, "A5");
426 dialog = gtk_dialog_new();
427 tmp = gtk_label_new("Paper size");
428 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), tmp);
429 tmp = gtk_combo_new();
430 gtk_combo_set_popdown_strings(GTK_COMBO(tmp), items);
431 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), tmp);
433 tmp = gtk_check_button_new_with_label("Disable Colors");
434 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), tmp);
437 tmp = gtk_button_new_with_label("OK");
438 gtk_signal_connect_object(GTK_OBJECT(tmp), "clicked", GTK_SIGNAL_FUNC(export_to_ps_get_file_name), dialog);
439 gtk_signal_connect_object(GTK_OBJECT(tmp), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), dialog);
440 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), tmp);
441 tmp = gtk_button_new_with_label("Cancel");
442 gtk_signal_connect_object(GTK_OBJECT(tmp), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), dialog);
443 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), tmp);
445 gtk_widget_show_all(dialog);
450 save_as(GtkWidget* widget)
452 file_dialog("Save As...", save_filename);
456 selection_changed(GtkMathView* math_view, mDOMNodeRef node)
458 g_return_if_fail(math_view != NULL);
459 g_return_if_fail(GTK_IS_MATH_VIEW(math_view));
461 while (node != NULL && !mdom_node_has_attribute(node, DOM_CONST_STRING("xref")))
462 node = mdom_node_get_parent(node);
464 gtk_math_view_set_selection(math_view, node);
468 jump(GtkMathView* math_view, mDOMNodeRef node)
472 g_return_if_fail(node != NULL);
473 href = mdom_node_get_attribute_ns(node, DOM_CONST_STRING("href"), XLINK_NS_URI);
478 g_assert(main_area != NULL);
479 gdk_window_set_cursor (main_area->window, pot);
482 if (pid == -1) exit(-1);
484 gchar* open_url = g_strdup_printf("openURL(%s,cic)", href);
491 fd = open("/dev/null", O_RDWR);
495 execlp("netscape", "netscape", "-noraise", "-remote", open_url, NULL);
496 perror("exec failed:");
499 mdom_string_free(href);
504 clicked(GtkMathView* math_view, gpointer user_data)
506 if (gtk_math_view_get_action(math_view) != NULL)
507 gtk_math_view_action_toggle(math_view);
513 GtkWidget* main_vbox;
516 main_vbox = gtk_vbox_new(FALSE, 1);
517 gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
518 gtk_container_add(GTK_CONTAINER(window), main_vbox);
519 gtk_widget_show(main_vbox);
521 menu_bar = get_main_menu();
522 gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar, FALSE, TRUE, 0);
523 gtk_widget_show(menu_bar);
525 main_area = gtk_math_view_new(NULL, NULL);
526 gtk_widget_show(main_area);
528 gtk_signal_connect_object (GTK_OBJECT (main_area),
529 "selection_changed", GTK_SIGNAL_FUNC (selection_changed),
530 (gpointer) main_area);
532 gtk_signal_connect_object (GTK_OBJECT (main_area),
533 "jump", GTK_SIGNAL_FUNC(jump),
534 (gpointer) main_area);
536 gtk_signal_connect_object (GTK_OBJECT (main_area),
537 "clicked", GTK_SIGNAL_FUNC(clicked),
538 (gpointer) main_area);
540 scrolled_area = gtk_scrolled_window_new(NULL, NULL);
541 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_area),
542 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
543 gtk_widget_show(scrolled_area);
544 gtk_container_add(GTK_CONTAINER(scrolled_area), main_area);
545 gtk_box_pack_start(GTK_BOX(main_vbox), scrolled_area, TRUE, TRUE, 0);
547 status_bar = gtk_statusbar_new();
548 gtk_widget_show(status_bar);
549 gtk_box_pack_start(GTK_BOX(main_vbox), status_bar, FALSE, TRUE, 0);
550 statusbar_context = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar), "filename");
552 gtk_widget_show(main_vbox);
554 if (gtk_math_view_get_anti_aliasing(GTK_MATH_VIEW(main_area)))
555 gtk_menu_item_activate(anti_aliasing_item);
557 if (gtk_math_view_get_kerning(GTK_MATH_VIEW(main_area)))
558 gtk_menu_item_activate(kerning_item);
560 gtk_math_view_set_font_size(GTK_MATH_VIEW(main_area), DEFAULT_FONT_SIZE);
561 gtk_menu_item_activate(font_size_item);
567 GtkItemFactory* item_factory;
568 GtkAccelGroup* accel_group;
569 GtkWidget* menu_item;
571 gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
573 accel_group = gtk_accel_group_new();
575 item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);
577 gtk_item_factory_create_items(item_factory, nmenu_items, menu_items, NULL);
579 gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
581 menu_item = gtk_item_factory_get_widget(item_factory, "/Options/Kerning");
582 kerning_item = GTK_MENU_ITEM(menu_item);
584 menu_item = gtk_item_factory_get_widget(item_factory, "/Options/Anti Aliasing");
585 anti_aliasing_item = GTK_MENU_ITEM(menu_item);
587 /* !!!BEWARE!!! the default font size must be kept aligned with the definition
590 menu_item = gtk_item_factory_get_widget(item_factory, "/Options/Default Font Size/14pt");
591 font_size_item = GTK_MENU_ITEM(menu_item);
593 return gtk_item_factory_get_widget(item_factory, "<main>");