]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/helmpot/guiGTK.c
ocaml 3.09 transition
[helm.git] / helm / helmpot / guiGTK.c
index ede4aa03f5d006b99329c9c07b43683451530ed0..906daf2b60f55253333fbc75cf3d75e092210aef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
+ * Copyright (C) 2000-2002, Luca Padovani <luca.padovani@cs.unibo.it>.
  * 
  * This file is part of HelmPot, a minimal browser for HELM.
  * 
@@ -65,8 +65,11 @@ PRIVATE GtkMenuItem* anti_aliasing_item;
 PRIVATE GtkMenuItem* font_size_item;
 PRIVATE gchar* file_name = NULL;
 
-PRIVATE GdkCursor* pot;
+PRIVATE GdkCursor* normal_cursor;
+PRIVATE GdkCursor* link_cursor;  
+PRIVATE GdkCursor* pot_cursor;
 
+PRIVATE gboolean loading = FALSE;
 PRIVATE guint statusbar_context;
 
 PRIVATE void create_widget_set(void);
@@ -176,11 +179,14 @@ GUI_init(gint* argc, gchar*** argv, gchar* title, guint width, guint height, Gtk
 
   gtk_timeout_add(timeout, f, NULL);
 
+  normal_cursor = gdk_cursor_new(GDK_TOP_LEFT_ARROW);
+  link_cursor = gdk_cursor_new(GDK_HAND2);
+  
   source = gdk_bitmap_create_from_data (NULL, pot_bits,
                                        pot_width, pot_height);
   mask = gdk_bitmap_create_from_data (NULL, pot_mask_bits,
                                      pot_mask_width, pot_mask_height);
-  pot = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
+  pot_cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
   gdk_pixmap_unref (source);
   gdk_pixmap_unref (mask);  
 }
@@ -195,6 +201,7 @@ GUI_load_document(const gchar* name)
 {
   GtkMathView* math_view;
   GtkMathViewClass* klass;
+  gboolean res;
 
   g_return_val_if_fail(name != NULL, -1);
   g_return_val_if_fail(main_area != NULL, -1);
@@ -206,9 +213,11 @@ GUI_load_document(const gchar* name)
   klass = (GtkMathViewClass*) gtk_type_class(gtk_math_view_get_type());
   g_return_val_if_fail(klass != NULL, -1);
 
-  gdk_window_set_cursor (main_area->window, pot);  
+  res = gtk_math_view_load(math_view, name);
+  gdk_window_set_cursor(main_area->window, normal_cursor);
+  loading = FALSE;
 
-  if (!gtk_math_view_load(math_view, name)) {
+  if (!res) {
     load_error_msg(name);
     return -1;
   }
@@ -217,8 +226,6 @@ GUI_load_document(const gchar* name)
   if (strlen(name) > 40) name += strlen(name) - 40;
   gtk_statusbar_push(GTK_STATUSBAR(status_bar), statusbar_context, name);
 
-  gdk_window_set_cursor(main_area->window, klass->normal_cursor);
-
   if (file_name != NULL) g_free(file_name);
   file_name = g_strdup(name);
 
@@ -298,7 +305,7 @@ help_about(GtkWidget* widget, gpointer data)
   GtkWidget* ok;
 
   dialog = gtk_dialog_new();
-  label = gtk_label_new("\n    HELM PoT    \n    Copyright (C) 2001 Luca Padovani    \n");
+  label = gtk_label_new("\n    HELM PoT    \n    Copyright (C) 2001-2002 Luca Padovani    \n");
   ok = gtk_button_new_with_label("Close");
 
   gtk_signal_connect_object (GTK_OBJECT (ok), "clicked",
@@ -453,47 +460,140 @@ save_as(GtkWidget* widget)
 }
 
 PRIVATE void
-selection_changed(GtkMathView* math_view, mDOMNodeRef node)
+element_changed(GtkMathView* math_view, GdomeElement* elem)
 {
   g_return_if_fail(math_view != NULL);
+  g_return_if_fail(main_area != NULL);
   g_return_if_fail(GTK_IS_MATH_VIEW(math_view));
-  gtk_math_view_set_selection(math_view, node);
+  /* fprintf(stderr, "node changed: %p %s\n", node, (node != NULL) ? mdom_node_get_name(node) : "-"); */
+
+  if (!loading) {
+    GdomeException exc = 0;
+    GdomeDOMString* namespaceURI = gdome_str_mkref(XLINK_NS_URI);
+    GdomeDOMString* localName = gdome_str_mkref("href");
+    
+    if (elem != NULL) {
+      gdome_el_ref(elem, &exc);
+      g_assert(exc == 0);
+    }
+
+    while (elem != NULL && !gdome_el_hasAttributeNS(elem, namespaceURI, localName, &exc)) {
+      GdomeElement* parent = gdome_cast_el(gdome_el_parentNode(elem, &exc));
+      g_assert(exc == 0);
+      gdome_el_unref(elem, &exc);
+      g_assert(exc == 0);
+      elem = parent;
+    }
+    g_assert(exc == 0);
+
+    gdome_str_unref(namespaceURI);
+    gdome_str_unref(localName);
+
+    if (elem != NULL) {
+      gdome_el_unref(elem, &exc);
+      g_assert(exc == 0);
+      gdk_window_set_cursor(main_area->window, link_cursor);
+    } else
+      gdk_window_set_cursor(main_area->window, normal_cursor);
+  }
 }
 
 PRIVATE void
-jump(GtkMathView* math_view, mDOMNodeRef node)
+selection_changed(GtkMathView* math_view, GdomeElement* elem)
 {
-  mDOMStringRef href;
+  GdomeException exc = 0;
+  GdomeDOMString* localName = gdome_str_mkref("xref");
 
-  g_return_if_fail(node != NULL);
-  href = mdom_node_get_attribute_ns(node, DOM_CONST_STRING("href"), XLINK_NS_URI);
+  g_return_if_fail(math_view != NULL);
+  g_return_if_fail(GTK_IS_MATH_VIEW(math_view));
 
-  if (href != NULL) {
-    pid_t pid;
+  if (elem != NULL) {
+    gdome_el_ref(elem, &exc);
+    g_assert(exc == 0);
+  }
 
-    g_assert(main_area != NULL);
-    gdk_window_set_cursor (main_area->window, pot);  
+  while (elem != NULL && !gdome_el_hasAttribute(elem, localName, &exc)) {
+    GdomeElement* parent = gdome_cast_el(gdome_el_parentNode(elem, &exc));
+    g_assert(exc == 0);
+    gdome_el_unref(elem, &exc);
+    g_assert(exc == 0);
+    elem = parent;
+  }
 
-    pid = fork();
-    if (pid == -1) exit(-1);
-    if (pid == 0) {
-      gchar* open_url = g_strdup_printf("openURL(%s,cic)", href);
-      gint fd;
+  gdome_str_unref(localName);
 
-      close(0);
-      close(1);
-      close(2);
+  gtk_math_view_set_selection(math_view, elem);
+  if (elem != NULL) {
+    gdome_el_unref(elem, &exc);
+    g_assert(exc == 0);
+  }
+}
 
-      fd = open("/dev/null", O_RDWR);
-      dup(fd);
-      dup(fd);
+PRIVATE void
+jump(GdomeDOMString* href)
+{
+  pid_t pid;
+  g_return_if_fail(href != NULL);
+
+  gdk_window_set_cursor(main_area->window, pot_cursor);  
+  loading = TRUE;
+  pid = fork();
+  if (pid == -1) exit(-1);
+  if (pid == 0) {
+    gchar* open_url = g_strdup_printf("openURL(%s,cic)", href->str);
+    gint fd;
+    
+    close(0);
+    close(1);
+    close(2);
+    
+    fd = open("/dev/null", O_RDWR);
+    dup(fd);
+    dup(fd);
+    
+    execlp("netscape", "netscape", "-noraise", "-remote", open_url, NULL);
+    perror("exec failed:");
+    exit(-1);
+  }
+}
 
-      execlp("netscape", "netscape", "-noraise", "-remote", open_url, NULL);
-      perror("exec failed:");
-      exit(-1);
+PRIVATE void
+clicked(GtkMathView* math_view, gpointer user_data)
+{
+  GdomeException exc = 0;
+  GdomeDOMString* namespaceURI = gdome_str_mkref(XLINK_NS_URI);
+  GdomeDOMString* localName = gdome_str_mkref("href");
+
+  GdomeElement* p = gtk_math_view_get_element(math_view);
+  while (p != NULL && !gdome_el_hasAttributeNS(p, namespaceURI, localName, &exc)) {
+    GdomeElement* parent = gdome_cast_el(gdome_el_parentNode(p, &exc));
+    g_assert(exc == 0);
+    gdome_el_unref(p, &exc);
+    g_assert(exc == 0);
+    p = parent;
+  }
+  g_assert(exc == 0);
+
+  if (p != NULL) {
+    GdomeDOMString* href = gdome_el_getAttributeNS(p, namespaceURI, localName, &exc);
+    g_assert(exc == 0);
+    g_assert(href != NULL);
+
+    jump(href);
+    gdome_str_unref(href);
+    gdome_el_unref(p, &exc);
+    g_assert(exc == 0);
+  } else {
+    p = gtk_math_view_get_action(math_view);
+    if (p != NULL) {
+      gtk_math_view_action_toggle(math_view);
+      gdome_el_unref(p, &exc);
+      g_assert(exc == 0);
     }
-    mdom_string_free(href);
   }
+
+  gdome_str_unref(namespaceURI);
+  gdome_str_unref(localName);
 }
 
 PRIVATE void
@@ -514,11 +614,17 @@ create_widget_set()
   main_area = gtk_math_view_new(NULL, NULL);
   gtk_widget_show(main_area);
 
+  gtk_signal_connect_object (GTK_OBJECT (main_area),
+                            "element_changed", GTK_SIGNAL_FUNC (element_changed),
+                            (gpointer) main_area);
+
   gtk_signal_connect_object (GTK_OBJECT (main_area),
                             "selection_changed", GTK_SIGNAL_FUNC (selection_changed),
                             (gpointer) main_area);
 
-  gtk_signal_connect_object (GTK_OBJECT (main_area), "jump", GTK_SIGNAL_FUNC(jump), NULL);
+  gtk_signal_connect_object (GTK_OBJECT (main_area), 
+                            "clicked", GTK_SIGNAL_FUNC(clicked),
+                            (gpointer) main_area);
                             
   scrolled_area = gtk_scrolled_window_new(NULL, NULL);
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_area),
@@ -575,3 +681,4 @@ get_main_menu()
 
   return gtk_item_factory_get_widget(item_factory, "<main>");
 }
+