From: pmasoudi Date: Mon, 8 Sep 2003 15:04:22 +0000 (+0000) Subject: select and click signal added X-Git-Tag: v0_0_1~15 X-Git-Url: http://matita.cs.unibo.it/gitweb/?p=helm.git;a=commitdiff_plain;h=a7201afb196a65767622051aca79b84f59c6db38 select and click signal added --- diff --git a/helm/gtkmathview-bonobo/src/Makefile.am b/helm/gtkmathview-bonobo/src/Makefile.am index 9ae3d2cff..e78683aaf 100644 --- a/helm/gtkmathview-bonobo/src/Makefile.am +++ b/helm/gtkmathview-bonobo/src/Makefile.am @@ -31,12 +31,15 @@ libgtkmathview_bonobo_la_SOURCES = \ control-factory.c \ persist-file.c \ persist-stream.c \ + aux.cc \ + handlers.c \ view.c pkginclude_HEADERS = \ control-factory.h \ persist-file.h \ - persist-stream.h \ + persist-stream.h \ + handlers.h \ view.h server_DATA = $(server_in_files:.server.in.in=.server) diff --git a/helm/gtkmathview-bonobo/src/aux.cc b/helm/gtkmathview-bonobo/src/aux.cc new file mode 100644 index 000000000..4eaef1074 --- /dev/null +++ b/helm/gtkmathview-bonobo/src/aux.cc @@ -0,0 +1,279 @@ +// Copyright (C) 2000-2002, Luca Padovani . +// +// This file is part of GtkMathView, a Gtk widget for MathML. +// +// GtkMathView is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// GtkMathView is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with GtkMathView; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// For details, see the GtkMathView World-Wide-Web page, +// http://www.cs.unibo.it/helm/mml-widget, or send a mail to +// + +#include +#include + +#include + +#include +#include + +#include + +#define MATHML_NS_URI "http://www.w3.org/1998/Math/MathML" + +namespace DOM = GdomeSmartDOM; + +static unsigned +getDepth(const DOM::Element& elem) +{ + unsigned length = 0; + DOM::Element p = elem; + + while (p) + { + p = p.get_parentNode(); + length++; + } + + return length; +} + +static DOM::Element +findCommonAncestor(const DOM::Element& first, const DOM::Element& last) +{ + if (!first || !last) return DOM::Element(0); + + DOM::Element p(first); + DOM::Element q(last); + + if (p != q) + { + unsigned pDepth = getDepth(p); + unsigned qDepth = getDepth(q); + + while (p && pDepth > qDepth) + { + p = p.get_parentNode(); + pDepth--; + } + + while (q && qDepth > pDepth) + { + q = q.get_parentNode(); + qDepth--; + } + + assert(pDepth == qDepth); + + while (p && q && p != q) + { + p = p.get_parentNode(); + q = q.get_parentNode(); + } + } + + return p; +} + +static void +findCommonSiblings(const DOM::Element& first, const DOM::Element& last, + DOM::Element& firstS, DOM::Element& lastS) +{ + DOM::Element p(first); + DOM::Element q(last); + + if (p != q) + { + unsigned pDepth = getDepth(p); + unsigned qDepth = getDepth(q); + + while (p && pDepth > qDepth) + { + p = p.get_parentNode(); + pDepth--; + } + + while (q && qDepth > pDepth) + { + q = q.get_parentNode(); + qDepth--; + } + + assert(pDepth == qDepth); + + while (p && q && p.get_parentNode() != q.get_parentNode()) + { + p = p.get_parentNode(); + q = q.get_parentNode(); + } + } + + firstS = p; + lastS = q; +} + +static DOM::Node +leftmostChild(const DOM::Node& node) +{ + if (!node) return node; + + DOM::Node firstChild = node.get_firstChild(); + if (!firstChild) return node; + + return leftmostChild(firstChild); +} + +static DOM::Node +rightmostChild(const DOM::Node& node) +{ + if (!node) return node; + + DOM::Node lastChild = node.get_lastChild(); + if (!lastChild) return node; + + return rightmostChild(lastChild); +} + +static DOM::Node +leftSibling(const DOM::Node& node) +{ + DOM::Node p = node; + + if (!p) return p; + + while (p.get_parentNode() && p.get_parentNode().get_firstChild() == p) + p = p.get_parentNode(); + + if (!p.get_parentNode()) return DOM::Node(0); + + DOM::Node prevSibling = p.get_previousSibling(); + assert(prevSibling); + + return rightmostChild(prevSibling); +} + +static DOM::Node +rightSibling(const DOM::Node& node) +{ + DOM::Node p = node; + + if (!p) return p; + + DOM::Node firstChild = p.get_firstChild(); + if (firstChild) return firstChild; + + while (p.get_parentNode() && p.get_parentNode().get_lastChild() == p) + p = p.get_parentNode(); + + if (!p.get_parentNode()) return DOM::Node(0); + + DOM::Node nextSibling = p.get_nextSibling(); + assert(nextSibling); + + return leftmostChild(nextSibling); +} + +extern "C" GdomeElement* +find_common_ancestor(GdomeElement* first, GdomeElement* last) +{ + DOM::Element p(first); + DOM::Element q(last); + return gdome_cast_el(findCommonAncestor(p, q).gdome_object()); +} + +extern "C" GdomeElement* +find_self_or_ancestor(GdomeElement* elem, const char* uri, const char* name) +{ + DOM::Element el(elem); + + while (el && (el.get_namespaceURI() != uri || el.get_localName() != name)) + el = el.get_parentNode(); + + return gdome_cast_el(el.gdome_object()); +} + +extern "C" void +action_toggle(GdomeElement* elem) +{ + DOM::Element el(elem); + if (el.get_namespaceURI() != MATHML_NS_URI || el.get_localName() != "maction") return; + + guint idx; + if (el.hasAttribute("selection")) + idx = atoi(std::string(el.getAttribute("selection")).c_str()); + else idx = 1; + + idx++; + + std::ostringstream os; + os << idx; + el.setAttribute("selection", os.str()); +} + +extern "C" void +find_common_siblings(GdomeElement* first, GdomeElement* last, + GdomeElement** firstS, GdomeElement** lastS) +{ + DOM::Element fs(0); + DOM::Element ls(0); + + findCommonSiblings(DOM::Element(first), DOM::Element(last), fs, ls); + + if (firstS != NULL) *firstS = gdome_cast_el(fs.gdome_object()); + if (lastS != NULL) *lastS = gdome_cast_el(ls.gdome_object()); +} + +static DOM::Element +findElementWithAttribute(const DOM::Element& elem, const std::string& name) +{ + DOM::Element el(elem); + while (el && !el.hasAttribute(name)) el = el.get_parentNode(); + return el; +} + +static DOM::Element +findElementWithAttributeNS(const DOM::Element& elem, const std::string& ns_uri, const std::string& name) +{ + DOM::Element el(elem); + while (el && !el.hasAttributeNS(ns_uri, name)) el = el.get_parentNode(); + return el; +} + +extern "C" GdomeElement* +find_xref_element(GdomeElement* elem) +{ + DOM::Element el = findElementWithAttribute(DOM::Element(elem), "xref"); + return gdome_cast_el(el.gdome_object()); +} + +extern "C" GdomeDOMString* +find_hyperlink(GdomeElement* elem) +{ + DOM::Element el = findElementWithAttribute(DOM::Element(elem),"href"); + if (el) return el.getAttribute("href").gdome_str(); + else return NULL; +} + + +extern "C" void +delete_element(GdomeElement* elem) +{ + DOM::Element p(elem); + + DOM::Element parent = p.get_parentNode(); + assert(parent); + + parent.removeChild(p); +} + diff --git a/helm/gtkmathview-bonobo/src/control-data.h b/helm/gtkmathview-bonobo/src/control-data.h new file mode 100644 index 000000000..0f636c2f4 --- /dev/null +++ b/helm/gtkmathview-bonobo/src/control-data.h @@ -0,0 +1,12 @@ +#ifndef __CONTROL_DATA__ +#define __CONTROL_DATA__ + +typedef struct _GtkMathViewControlData +{ + gboolean semantic_selection; + GdomeElement* first_selected; + GdomeElement* root_selected; + GtkMathView *math_view; +} GtkMathViewControlData; + +#endif diff --git a/helm/gtkmathview-bonobo/src/control-factory.c b/helm/gtkmathview-bonobo/src/control-factory.c index 8d4e9a12a..3f6b65e3e 100644 --- a/helm/gtkmathview-bonobo/src/control-factory.c +++ b/helm/gtkmathview-bonobo/src/control-factory.c @@ -3,13 +3,10 @@ #include #include #include "control-factory.h" +#include "handlers.h" +#include "control-data.h" #include "view.h" -typedef struct _GtkMathViewControlData -{ - GtkMathView *math_view; -} GtkMathViewControlData; - enum { MATH_VIEW_WIDTH, MATH_VIEW_HEIGHT, @@ -28,6 +25,10 @@ gtk_math_view_control_data_new(GtkMathView *math_view) { GtkMathViewControlData *cd = g_new(GtkMathViewControlData,1); cd->math_view = math_view; + cd->semantic_selection = FALSE; + cd->doc_name = NULL; + cd->first_selected = NULL; + cd->root_selected = NULL; return cd; } @@ -214,18 +215,6 @@ control_destroy(BonoboObject *object,GtkMathViewControlData *cd) gtk_math_view_control_data_destroy(cd); } -static void -set_frame(BonoboControl *control,gpointer data) -{ - Bonobo_UIContainer remote_ui_container; - BonoboUIComponent *ui_component; - GtkMathViewControlData *control_data; - GtkWidget *scrolled_window; - Bonobo_ControlFrame frame; - - control_data = (GtkMathViewControlData *) data; -} - static void gtk_math_view_control_init(BonoboControl *control,GtkWidget *scrolled_window) { @@ -243,6 +232,7 @@ gtk_math_view_control_init(BonoboControl *control,GtkWidget *scrolled_window) gtk_widget_show(math_view); control_data = gtk_math_view_control_data_new((GtkMathView*)math_view); + g_signal_connect (control, "destroy", G_CALLBACK (control_destroy), control_data); gtk_container_add(GTK_CONTAINER (scrolled_window), GTK_WIDGET (control_data->math_view)); @@ -281,6 +271,11 @@ gtk_math_view_control_init(BonoboControl *control,GtkWidget *scrolled_window) bonobo_object_unref(BONOBO_OBJECT(prop_bag)); g_signal_connect(control,"set_frame",G_CALLBACK(set_frame),control_data); + g_signal_connect(control_data->math_view, "click", G_CALLBACK (click_cb),control_data); + g_signal_connect(control_data->math_view, "select_begin",G_CALLBACK(select_begin_cb),control_data); + g_signal_connect(control_data->math_view, "select_over",G_CALLBACK(select_over_cb),control_data); + g_signal_connect(control_data->math_view, "select_end",G_CALLBACK(select_end_cb),control_data); + g_signal_connect(control_data->math_view, "select_abort",G_CALLBACK(select_abort_cb),control_data); } static BonoboObject* diff --git a/helm/gtkmathview-bonobo/src/handlers.c b/helm/gtkmathview-bonobo/src/handlers.c new file mode 100644 index 000000000..c3bda4737 --- /dev/null +++ b/helm/gtkmathview-bonobo/src/handlers.c @@ -0,0 +1,193 @@ +#include "handlers.h" + +void +set_frame(BonoboControl *control,gpointer data) +{ + /*Bonobo_UIContainer remote_ui_container; + BonoboUIComponent *ui_component; + GtkMathViewControlData *control_data; + GtkWidget *scrolled_window; + Bonobo_ControlFrame frame; + control_data = (GtkMathViewControlData *) data;*/ +} + +void +click_cb(GtkMathView* math_view, GdomeElement* elem, gint state, GtkMathViewControlData* control_data) +{ + GdomeException exc; + GdomeDOMString* name; + GdomeDOMString* ns_uri; + //GdomeElement* p; + + g_return_if_fail(math_view != NULL); + + printf("*** click signal: %p %x %p\n", elem, state, control_data); + + if (elem != NULL) + { + GdomeElement* action; + GdomeDOMString* href = find_hyperlink(elem); + if (href != NULL) + { + /* printf("hyperlink %s\n", href->str); */ + gtk_math_view_load_uri(math_view,href->str); + gdome_str_unref(href); + } + else + { + action = find_self_or_ancestor(elem, MATHML_NS_URI, "maction"); + /* printf("action? %p\n", action); */ + if (action != NULL) + { + gtk_math_view_freeze(math_view); + action_toggle(action); + gtk_math_view_thaw(math_view); + gdome_el_unref(action, &exc); + g_assert(exc == 0); + return; + } + } + if (control_data->root_selected != NULL) + { + gtk_math_view_freeze(math_view); + gtk_math_view_unselect(math_view, control_data->root_selected); + gtk_math_view_thaw(math_view); + gdome_el_unref(control_data->root_selected, &exc); + g_assert(exc == 0); + control_data->root_selected = NULL; + } + } +} + +void +select_begin_cb(GtkMathView* math_view, GdomeElement* elem, gint state,GtkMathViewControlData* control_data) +{ + g_return_if_fail(math_view != NULL); + g_return_if_fail(GTK_IS_MATH_VIEW(math_view)); + printf("*** select_begin signal: %p %x\n", elem, state); + if (elem != NULL) + { + GdomeException exc = 0; + gtk_math_view_freeze(math_view); + if (control_data->root_selected != NULL) + { + gtk_math_view_unselect(math_view,control_data->root_selected); + gdome_el_unref(control_data->root_selected, &exc); + g_assert(exc == 0); + control_data->root_selected = NULL; + } + + if (control_data->semantic_selection) + { + GdomeElement* new_elem = find_xref_element(elem); + if (new_elem != NULL) + { + gdome_el_ref(new_elem, &exc); + g_assert(exc == 0); + } + control_data->first_selected = control_data->root_selected = new_elem; + } + else + { + gdome_el_ref(elem, &exc); + g_assert(exc == 0); + gdome_el_ref(elem, &exc); + g_assert(exc == 0); + control_data->first_selected = control_data->root_selected = elem; + } + + if (control_data->root_selected != NULL) + gtk_math_view_select(math_view, control_data->root_selected); + + gtk_math_view_thaw(math_view); + } +} + +void +select_over_cb(GtkMathView* math_view, GdomeElement* elem, gint state,GtkMathViewControlData* control_data) +{ + g_return_if_fail(math_view != NULL); + g_return_if_fail(GTK_IS_MATH_VIEW(math_view)); + + printf("*** select_over signal: %p %x\n", elem, state); + + if (control_data->first_selected != NULL && elem != NULL) + { + GdomeException exc = 0; + + gtk_math_view_freeze(math_view); + + if (control_data->root_selected != NULL) + { + gtk_math_view_unselect(math_view, control_data->root_selected); + gdome_el_unref(control_data->root_selected, &exc); + g_assert(exc == 0); + control_data->root_selected = NULL; + } + + if (control_data->semantic_selection) + { + GdomeElement* new_root = find_common_ancestor(control_data->first_selected, elem); + if (new_root != NULL) + { + control_data->root_selected = find_xref_element(new_root); + gdome_el_unref(new_root, &exc); + g_assert(exc == 0); + } + else + control_data->root_selected = NULL; + } + else + control_data->root_selected = find_common_ancestor(control_data->first_selected, elem); + + if (control_data->root_selected != NULL) + gtk_math_view_select(math_view, control_data->root_selected); + + gtk_math_view_thaw(math_view); + } +} + +void +select_end_cb(GtkMathView* math_view, GdomeElement* elem, gint state,GtkMathViewControlData* control_data) +{ + g_return_if_fail(math_view != NULL); + g_return_if_fail(GTK_IS_MATH_VIEW(math_view)); + + printf("*** select_end signal: %p %x\n", elem, state); + + if (control_data->first_selected != NULL) + { + GdomeException exc = 0; + gdome_el_unref(control_data->first_selected, &exc); + g_assert(exc == 0); + control_data->first_selected = NULL; + } +} + +void +select_abort_cb(GtkMathView* math_view,GtkMathViewControlData* control_data) +{ + GdomeException exc = 0; + + g_return_if_fail(math_view != NULL); + g_return_if_fail(GTK_IS_MATH_VIEW(math_view)); + + printf("*** select_abort signal\n"); + + if (control_data->first_selected != NULL) + { + gdome_el_unref(control_data->first_selected, &exc); + g_assert(exc == 0); + control_data->first_selected = NULL; + } + if (control_data->root_selected != NULL) + { + gtk_math_view_freeze(math_view); + gtk_math_view_unselect(math_view, control_data->root_selected); + gtk_math_view_thaw(math_view); + gdome_el_unref(control_data->root_selected, &exc); + g_assert(exc == 0); + control_data->root_selected = NULL; + } +} + diff --git a/helm/gtkmathview-bonobo/src/handlers.h b/helm/gtkmathview-bonobo/src/handlers.h new file mode 100644 index 000000000..84f6bc943 --- /dev/null +++ b/helm/gtkmathview-bonobo/src/handlers.h @@ -0,0 +1,27 @@ +#ifndef __handlers___ +#define __handlers___ + +#include +#include +#include +#include "control-data.h" + +void +set_frame(BonoboControl*,gpointer); + +void +click_cb(GtkMathView*, GdomeElement*, gint,GtkMathViewControlData*); + +void +select_begin_cb(GtkMathView*,GdomeElement*, gint,GtkMathViewControlData*); + +void +select_over_cb(GtkMathView*,GdomeElement*,gint,GtkMathViewControlData*); + +void +select_end_cb(GtkMathView*,GdomeElement*,gint,GtkMathViewControlData*); + +void +select_abort_cb(GtkMathView*,GtkMathViewControlData*); + +#endif