OSDN Git Service

Add res popup.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 11 Sep 2006 05:20:59 +0000 (14:20 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 15 Sep 2006 01:16:51 +0000 (10:16 +0900)
src/FukuiNoNamari/thread_window.py

index 16de1b7..eb4ce60 100644 (file)
@@ -30,6 +30,7 @@ import gobject
 import threading
 import gconf
 import traceback
+import itertools
 import os
 
 import misc
@@ -107,6 +108,8 @@ class WinWrap(winwrapbase.WinWrapBase):
 
         self.initialize_buffer()
 
+        self.hint = HintWrap()
+
         sigdic = {"on_refresh_activate": self.update,
                   "on_compose_activate": self.on_compose_clicked,
                   "on_toolbar_activate": self.on_toolbar_activate,
@@ -219,6 +222,7 @@ class WinWrap(winwrapbase.WinWrapBase):
 
     def set_cursor_if_appropriate(self, widget, x, y):
         hovering = False
+        href = ""
 
         buffer = widget.get_buffer()
         iter = widget.get_iter_at_location(x, y)
@@ -235,9 +239,44 @@ class WinWrap(winwrapbase.WinWrapBase):
         if self.hovering_over_link:
             widget.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(
                 self.hand_cursor)
+            if href:
+                if not href.startswith("http://"):
+                    href = urlparse.urljoin(self.bbs_type.get_uri_base(), href)
+
+                strict_uri = self.bbs_type.get_thread_uri()
+                if href != strict_uri and href.startswith(strict_uri):
+                    resnum = href[len(strict_uri):]
+                    match = re.match("\d+", resnum)
+                    if match:
+                        resnum = int(match.group())
+                        if not self.hint.visible(resnum):
+                            mark = self.textbuffer.get_mark(str(resnum))
+                            n_mark = self.textbuffer.get_mark(str(resnum+1))
+                            if mark and n_mark:
+                                iter = self.textbuffer.get_iter_at_mark(mark)
+                                n_iter = self.textbuffer.get_iter_at_mark(
+                                    n_mark)
+                                text = self.textbuffer.get_text(
+                                    iter, n_iter, False)
+                                if text:
+                                    iter = self.textview.get_iter_at_location(
+                                        x, y)
+                                    rect = self.textview.get_iter_location(
+                                        iter)
+                                    x, y = \
+                                       self.textview.buffer_to_window_coords(
+                                        gtk.TEXT_WINDOW_WIDGET, rect.x, rect.y)
+                                    x, y = self.textview.translate_coordinates(
+                                        self.window, x, y)
+                                    wx, wy = self.window.get_position()
+                                    x += wx
+                                    y += wy
+                                    y += rect.height
+                                    self.hint.show(x, y, text, resnum)
         else:
             widget.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(
                 self.regular_cursor)
+            self.hint.destroy()
 
     def on_close_activate(self, widget):
         self.destroy()
@@ -659,3 +698,50 @@ class WinWrap(winwrapbase.WinWrapBase):
                                  priority=gobject.PRIORITY_HIGH)
         except:
             traceback.print_exc()
+
+
+class HintWrap:
+
+    def __init__(self):
+        self.window = None
+        self.textview = None
+        self.nums = None
+
+    def __del__(self):
+        print "destruct"
+        self.destroy()
+
+    def destroy(self):
+        if self.window:
+            self.window.destroy()
+        self.window = None
+        self.textview = None
+        self.nums = None
+
+    def show(self, x, y, text, *nums):
+        self.destroy()
+
+        self.window = gtk.Window(gtk.WINDOW_POPUP)
+        self.window.set_default_size(400, 10)
+        self.window.move(x, y)
+
+        self.textview = gtk.TextView()
+        self.window.add(self.textview)
+
+        self.textview.set_wrap_mode(gtk.WRAP_CHAR)
+        self.textview.set_editable(False)
+
+        buffer = self.textview.get_buffer()
+        buffer.set_text(text.rstrip())
+        self.nums = nums
+
+        self.window.show_all()
+
+    def visible(self, *nums):
+        if not self.nums or len(self.nums) != len(nums):
+            return False
+
+        for num, mun in itertools.izip(self.nums, nums):
+            if num != mun:
+                return False
+        return True