Fixed wrong bounding box calculation for hints.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 8 Dec 2012 15:40:41 +0000 (16:40 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 8 Dec 2012 15:40:41 +0000 (16:40 +0100)
Added shorter type for the webkit dom types to cleanup the code.

src/dom.c
src/dom.h
src/hints.c

index 94442b3..986d708 100644 (file)
--- a/src/dom.c
+++ b/src/dom.c
  * along with this program. If not, see http://www.gnu.org/licenses/.
  */
 
-#include "dom.h"
 #include "main.h"
+#include "dom.h"
 
-static gboolean dom_auto_insert(WebKitDOMElement* element);
-static gboolean dom_editable_focus_cb(WebKitDOMElement* element, WebKitDOMEvent* event);
-static gboolean dom_is_editable(WebKitDOMElement* element);
-static WebKitDOMElement* dom_get_active_element(WebKitDOMDocument* doc);
+static gboolean dom_auto_insert(Element* element);
+static gboolean dom_editable_focus_cb(Element* element, Event* event);
+static gboolean dom_is_editable(Element* element);
+static WebKitDOMElement* dom_get_active_element(Document* doc);
 
 
 void dom_check_auto_insert(void)
 {
-    WebKitDOMDocument* doc   = webkit_web_view_get_dom_document(vp.gui.webview);
-    WebKitDOMElement* active = dom_get_active_element(doc);
+    Document* doc   = webkit_web_view_get_dom_document(vp.gui.webview);
+    Element* active = dom_get_active_element(doc);
     if (!dom_auto_insert(active)) {
-        WebKitDOMHTMLElement *element = webkit_dom_document_get_body(doc);
+        HtmlElement* element = webkit_dom_document_get_body(doc);
         if (!element) {
             element = WEBKIT_DOM_HTML_ELEMENT(webkit_dom_document_get_document_element(doc));
         }
@@ -41,51 +41,59 @@ void dom_check_auto_insert(void)
     }
 }
 
-void dom_element_set_style(WebKitDOMElement* element, const gchar* style)
+void dom_element_set_style(Element* element, const gchar* format, ...)
 {
-    WebKitDOMCSSStyleDeclaration* css = webkit_dom_element_get_style(element);
+    va_list args;
+    va_start(args, format);
+    gchar* value = g_strdup_vprintf(format, args);
+    CssDeclaration* css = webkit_dom_element_get_style(element);
     if (css) {
-        webkit_dom_css_style_declaration_set_css_text(css, style, NULL);
+        webkit_dom_css_style_declaration_set_css_text(css, value, NULL);
     }
+    g_free(value);
 }
 
-void dom_element_style_set_property(WebKitDOMElement* element, const gchar* property, const gchar* style)
+void dom_element_style_set_property(Element* element, const gchar* property, const gchar* style)
 {
-    WebKitDOMCSSStyleDeclaration* css = webkit_dom_element_get_style(element);
+    CssDeclaration* css = webkit_dom_element_get_style(element);
     if (css) {
         webkit_dom_css_style_declaration_set_property(css, property, style, "", NULL);
     }
 }
 
-gboolean dom_element_is_visible(WebKitDOMDOMWindow* win, WebKitDOMElement* element)
+gboolean dom_element_is_visible(Window* win, Element* element)
 {
-    gchar* value = NULL;
+    if (webkit_dom_html_element_get_hidden(WEBKIT_DOM_HTML_ELEMENT(element))) {
+        return FALSE;
+    }
 
-    WebKitDOMCSSStyleDeclaration* style = webkit_dom_dom_window_get_computed_style(win, element, "");
-    value = webkit_dom_css_style_declaration_get_property_value(style, "visibility");
-    if (value && g_ascii_strcasecmp(value, "hidden") == 0) {
+    CssDeclaration* style = webkit_dom_dom_window_get_computed_style(win, element, "");
+    if (style_compare_property(style, "display", "none")) {
         return FALSE;
     }
-    value = webkit_dom_css_style_declaration_get_property_value(style, "display");
-    if (value && g_ascii_strcasecmp(value, "none") == 0) {
+    if (style_compare_property(style, "visibility", "hidde")) {
         return FALSE;
     }
 
     return TRUE;
 }
 
-DomBoundingRect dom_elemen_get_bounding_rect(WebKitDOMElement* element)
+DomBoundingRect dom_elemen_get_bounding_rect(Element* element)
 {
     DomBoundingRect rect;
-    rect.left   = webkit_dom_element_get_offset_left(element);
-    rect.top    = webkit_dom_element_get_offset_top(element);
+    rect.left = 0;
+    rect.top  = 0;
+    for (Element* e = element; e; e = webkit_dom_element_get_offset_parent(e)) {
+        rect.left += webkit_dom_element_get_offset_left(e);
+        rect.top  += webkit_dom_element_get_offset_top(e);
+    }
     rect.right  = rect.left + webkit_dom_element_get_offset_width(element);
     rect.bottom = rect.top + webkit_dom_element_get_offset_height(element);
 
     return rect;
 }
 
-static gboolean dom_auto_insert(WebKitDOMElement* element)
+static gboolean dom_auto_insert(Element* element)
 {
     if (dom_is_editable(element)) {
         vp_set_mode(VP_MODE_INSERT, FALSE);
@@ -94,13 +102,13 @@ static gboolean dom_auto_insert(WebKitDOMElement* element)
     return FALSE;
 }
 
-static gboolean dom_editable_focus_cb(WebKitDOMElement* element, WebKitDOMEvent* event)
+static gboolean dom_editable_focus_cb(Element* element, Event* event)
 {
     webkit_dom_event_target_remove_event_listener(
         WEBKIT_DOM_EVENT_TARGET(element), "focus", G_CALLBACK(dom_editable_focus_cb), true
     );
     if (GET_CLEAN_MODE() != VP_MODE_INSERT) {
-        WebKitDOMEventTarget* target = webkit_dom_event_get_target(event);
+        EventTarget* target = webkit_dom_event_get_target(event);
         dom_auto_insert((void*)target);
     }
     return FALSE;
@@ -129,18 +137,18 @@ static gboolean dom_is_editable(WebKitDOMElement* element)
     return FALSE;
 }
 
-static WebKitDOMElement* dom_get_active_element(WebKitDOMDocument* doc)
+static Element* dom_get_active_element(Document* doc)
 {
-    WebKitDOMDocument* d     = NULL;
-    WebKitDOMElement* active = webkit_dom_html_document_get_active_element((void*)doc);
-    gchar* tagname           = webkit_dom_element_get_tag_name(active);
+    Document* d     = NULL;
+    Element* active = webkit_dom_html_document_get_active_element((void*)doc);
+    gchar* tagname  = webkit_dom_element_get_tag_name(active);
 
     if (!g_strcmp0(tagname, "FRAME")) {
-        d   = webkit_dom_html_frame_element_get_content_document(WEBKIT_DOM_HTML_FRAME_ELEMENT(active));
+        d = webkit_dom_html_frame_element_get_content_document(WEBKIT_DOM_HTML_FRAME_ELEMENT(active));
         return dom_get_active_element(d);
     }
     if (!g_strcmp0(tagname, "IFRAME")) {
-        d   = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(active));
+        d = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(active));
         return dom_get_active_element(d);
     }
     return active;
index aa697b6..f3ca21a 100644 (file)
--- a/src/dom.h
+++ b/src/dom.h
 
 #include <webkit/webkit.h>
 
+// Types
+#define Document       WebKitDOMDocument
+#define Body           WebKitDOMHTMLBodyElement
+#define Head           WebKitDOMHTMLHeadElement
+#define Window         WebKitDOMDOMWindow
+#define NodeList       WebKitDOMNodeList
+#define Node           WebKitDOMNode
+#define Style          WebKitDOMCSSStyleDeclaration
+#define HtmlElement    WebKitDOMHTMLElement
+#define Element        WebKitDOMElement
+#define CssDeclaration WebKitDOMCSSStyleDeclaration
+#define Event          WebKitDOMEvent
+#define EventTarget    WebKitDOMEventTarget
+
+// style 
+#define style_compare_property(style, name, value)    (!strcmp(webkit_dom_css_style_declaration_get_property_value(style, name), value))
+
 typedef struct {
     gulong left;
     gulong right;
@@ -30,9 +47,9 @@ typedef struct {
 } DomBoundingRect;
 
 void dom_check_auto_insert(void);
-void dom_element_set_style(WebKitDOMElement* element, const gchar* style);
-void dom_element_style_set_property(WebKitDOMElement* element, const gchar* property, const gchar* style);
-gboolean dom_element_is_visible(WebKitDOMDOMWindow* win, WebKitDOMElement* element);
-DomBoundingRect dom_elemen_get_bounding_rect(WebKitDOMElement* element);
+void dom_element_set_style(Element* element, const gchar* format, ...);
+void dom_element_style_set_property(Element* element, const gchar* property, const gchar* style);
+gboolean dom_element_is_visible(Window* win, Element* element);
+DomBoundingRect dom_elemen_get_bounding_rect(Element* element);
 
 #endif /* end of include guard: DOM_H */
index 0d3dde6..e1475ae 100644 (file)
 
 typedef struct {
     gulong num;
-    WebKitDOMElement* elem;                 /* hinted element */
-    gchar*            elemColor;            /* element color */
-    gchar*            elemBackgroundColor;  /* element background color */
-    WebKitDOMElement* hint;                 /* numbered hint element */
+    Element* elem;                 /* hinted element */
+    gchar*   elemColor;            /* element color */
+    gchar*   elemBackgroundColor;  /* element background color */
+    Element* hint;                 /* numbered hint element */
 } Hint;
 
 static void hints_create_for_window(
-    const gchar* input, WebKitDOMDOMWindow* win, gulong top_width,
+    const gchar* input, Window* win, gulong top_width,
     gulong top_height, gulong offsetX, gulong offsetY);
 static void hints_focus(const gulong num);
 static Hint* hints_get_hint_by_number(const gulong num);
@@ -62,7 +62,7 @@ static GList* hints = NULL;
 static gulong currentFocusNum = 0;
 static gulong hintCount = 0;
 static HintMode currentMode = HINTS_MODE_LINK;
-static WebKitDOMElement* hintContainer = NULL;
+static Element* hintContainer = NULL;
 
 void hints_clear(void)
 {
@@ -83,7 +83,7 @@ void hints_clear(void)
     }
     /* remove the hint container */
     if (hintContainer) {
-        WebKitDOMNode* parent = webkit_dom_node_get_parent_node(WEBKIT_DOM_NODE(hintContainer));
+        Node* parent = webkit_dom_node_get_parent_node(WEBKIT_DOM_NODE(hintContainer));
         webkit_dom_node_remove_child(parent, WEBKIT_DOM_NODE(hintContainer), NULL);
         hintContainer = NULL;
     }
@@ -91,8 +91,8 @@ void hints_clear(void)
 
 void hints_create(const gchar* input, HintMode mode)
 {
-    WebKitDOMDocument* doc;
-    WebKitDOMDOMWindow* win;
+    Document* doc;
+    Window* win;
     gulong top_width, top_height, offsetX, offsetY;
 
     hints_clear();
@@ -154,18 +154,18 @@ void hints_focus_next(const gboolean back)
 
 static void hints_create_for_window(
     const gchar* input,
-    WebKitDOMDOMWindow* win,
+    Window* win,
     gulong top_width,
     gulong top_height,
     gulong offsetX,
     gulong offsetY)
 {
-    WebKitDOMDocument* doc = webkit_dom_dom_window_get_document(win);
-    WebKitDOMNodeList* list = webkit_dom_document_get_elements_by_tag_name(doc, "body");
+    Document* doc = webkit_dom_dom_window_get_document(win);
+    NodeList* list = webkit_dom_document_get_elements_by_tag_name(doc, "body");
     if (!list) {
         return;
     }
-    WebKitDOMNode* body = webkit_dom_node_list_item(list, 0);
+    Node* body = webkit_dom_node_list_item(list, 0);
 
     WebKitDOMXPathNSResolver* ns_resolver = webkit_dom_document_create_ns_resolver(doc, body);
     if (!ns_resolver) {
@@ -200,7 +200,7 @@ static void hints_create_for_window(
     gulong snapshot_length = webkit_dom_xpath_result_get_snapshot_length(result, NULL);
 
     for (gulong i = 0; i < snapshot_length && hintCount < MAX_HINTS; i++) {
-        WebKitDOMNode* node = webkit_dom_xpath_result_snapshot_item(result, i, NULL);
+        Node* node = webkit_dom_xpath_result_snapshot_item(result, i, NULL);
         if (!dom_element_is_visible(win, WEBKIT_DOM_ELEMENT(node))) {
             continue;
         }
@@ -212,8 +212,8 @@ static void hints_create_for_window(
         hintCount++;
 
         /* create the hint element */
-        WebKitDOMElement* hint = webkit_dom_document_create_element(doc, "span", NULL);
-        WebKitDOMCSSStyleDeclaration* css_style = webkit_dom_element_get_style(WEBKIT_DOM_ELEMENT(node));
+        Element* hint = webkit_dom_document_create_element(doc, "span", NULL);
+        CssDeclaration* css_style = webkit_dom_element_get_style(WEBKIT_DOM_ELEMENT(node));
 
         Hint* newHint = g_new0(Hint, 1);
         newHint->num                 = hintCount;
@@ -225,9 +225,7 @@ static void hints_create_for_window(
 
         gulong left = rect.left - 3;
         gulong top  = rect.top - 3;
-        gchar* hint_style = g_strdup_printf(HINT_STYLE, left, top);
-        dom_element_set_style(hint, hint_style);
-        g_free(hint_style);
+        dom_element_set_style(hint, HINT_STYLE, left, top);
 
         gchar* num = g_strdup_printf("%li", newHint->num);
         webkit_dom_html_element_set_inner_text(WEBKIT_DOM_HTML_ELEMENT(hint), num, NULL);