OSDN Git Service

Support link. See HyperText of pygtk-demo.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 25 Aug 2006 19:49:44 +0000 (04:49 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 25 Aug 2006 19:49:44 +0000 (04:49 +0900)
src/Hage1/thread_window.py

index 7d3a786..9f233a2 100644 (file)
@@ -52,6 +52,10 @@ def open_thread(uri):
 
 
 class WinWrap:
+    hovering_over_link = False
+    hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
+    regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
+
 
     def __init__(self, uri):
         from BbsType import bbs_type_judge_uri
@@ -87,8 +91,86 @@ class WinWrap:
                   "on_thread_window_destroy": self.on_thread_window_destroy}
         self.widget_tree.signal_autoconnect(sigdic)
 
+        self.textview.connect("event-after", self.on_event_after)
+        self.textview.connect("motion-notify-event",
+                              self.on_motion_notify_event)
+        self.textview.connect("visibility-notify-event",
+                              self.on_visibility_notify_event)
+
         self.load_dat()
 
+    def on_event_after(self, widget, event):
+        if event.type != gtk.gdk.BUTTON_RELEASE:
+            return False
+        if event.button != 1:
+            return False
+        buffer = widget.get_buffer()
+
+        try:
+            start, end = buffer.get_selection_bounds()
+        except ValueError:
+            pass
+        else:
+            if start.get_offset() != end.get_offset():
+                return False
+
+        x, y = widget.window_to_buffer_coords(
+            gtk.TEXT_WINDOW_WIDGET, int (event.x), int(event.y))
+        iter = widget.get_iter_at_location(x, y)
+        if not iter.has_tag(self.leftmargintag) or x > 20:
+            tags = iter.get_tags()
+            for tag in tags:
+                href = tag.get_data("href")
+                if href:
+                    self.on_link_clicked(widget, href)
+        return False
+
+    def on_link_clicked(self, widget, href):
+        if href.startswith("http://"):
+            from BbsType import bbs_type_exception
+            import uri_opener
+            try:
+                uri_opener.open_uri(href)
+            except bbs_type_exception.BbsTypeError:
+                import gnome
+                gnome.url_show(href)
+
+    def on_motion_notify_event(self, widget, event):
+        x, y = widget.window_to_buffer_coords(
+            gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
+        self.set_cursor_if_appropriate(widget, x, y)
+        widget.window.get_pointer()
+        return False
+
+    def on_visibility_notify_event(self, widget, event):
+        wx, wy, mod = widget.window.get_pointer()
+        bx, by = widget.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
+
+        self.set_cursor_if_appropriate(widget, bx, by)
+        return False
+
+    def set_cursor_if_appropriate(self, widget, x, y):
+        hovering = False
+
+        buffer = widget.get_buffer()
+        iter = widget.get_iter_at_location(x, y)
+        if not iter.has_tag(self.leftmargintag) or x > 20:
+            tags = iter.get_tags()
+            for tag in tags:
+                href = tag.get_data("href")
+                if href:
+                    hovering = True
+
+        if hovering != self.hovering_over_link:
+            self.hovering_over_link = hovering
+
+        if self.hovering_over_link:
+            widget.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(
+                self.hand_cursor)
+        else:
+            widget.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(
+                self.regular_cursor)
+
     def on_close_activate(self, widget):
         self.window.destroy()
 
@@ -245,16 +327,39 @@ class WinWrap:
         p.feed("<br><br>")
         p.close()
 
+    def href_tag(self, href):
+        tag = self.textbuffer.create_tag(underline=pango.UNDERLINE_SINGLE)
+        tag.set_data("href", href)
+        return tag
+
     def untiedata_to_buffer(self, data, bold, href):
         if bold:
-            self.textbuffer.insert_with_tags(self.enditer, data, self.boldtag)
+            if href:
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data, self.boldtag, self.href_tag(href))
+            else:                
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data, self.boldtag)
         else:
-            self.textbuffer.insert(self.enditer, data)
+            if href:
+                self.textbuffer.insert(self.enditer, data, self.href(href))
+            else:
+                self.textbuffer.insert(self.enditer, data)
 
     def untiedata_to_buffer_with_leftmargin(self, data, bold, href):
         if bold:
-            self.textbuffer.insert_with_tags(self.enditer, data,
-                                             self.boldtag, self.leftmargintag)
+            if href:
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data, self.boldtag,
+                    self.leftmargintag,self.href_tag(href))
+            else:
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data, self.boldtag, self.leftmargintag)
         else:
-            self.textbuffer.insert_with_tags(self.enditer, data,
-                                             self.leftmargintag)
+            if href:
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data,
+                    self.leftmargintag, self.href_tag(href))
+            else:
+                self.textbuffer.insert_with_tags(
+                    self.enditer, data, self.leftmargintag)