graphics/GraphicCommons.py
changeset 1169 53e4a2b775a7
parent 1166 2ed9675be08d
child 1170 074e46cdedbc
--- a/graphics/GraphicCommons.py	Fri May 17 20:58:34 2013 +0200
+++ b/graphics/GraphicCommons.py	Thu May 23 18:47:44 2013 +0200
@@ -29,6 +29,8 @@
 import datetime
 from threading import Lock,Timer
 
+from controls.CustomToolTip import CustomToolTip, TOOLTIP_WAIT_PERIOD
+
 #-------------------------------------------------------------------------------
 #                               Common constants
 #-------------------------------------------------------------------------------
@@ -102,9 +104,6 @@
 # Define highlight refresh inhibition period in second
 REFRESH_HIGHLIGHT_PERIOD = 0.1
 
-# Define tooltip wait for displaying period in second
-TOOLTIP_WAIT_PERIOD = 0.5
-
 HANDLE_CURSORS = {
     (1, 1) : 2,
     (3, 3) : 2,
@@ -555,119 +554,6 @@
         dc.SetUserScale(scalex, scaley)
 
 #-------------------------------------------------------------------------------
-#                               Viewer ToolTip
-#-------------------------------------------------------------------------------
-
-"""
-Class that implements a custom tool tip
-"""
-
-if wx.Platform == '__WXMSW__':
-    faces = { 'times': 'Times New Roman',
-              'mono' : 'Courier New',
-              'helv' : 'Arial',
-              'other': 'Comic Sans MS',
-              'size' : 10,
-             }
-else:
-    faces = { 'times': 'Times',
-              'mono' : 'Courier',
-              'helv' : 'Helvetica',
-              'other': 'new century schoolbook',
-              'size' : 12,
-             }
-
-TOOLTIP_MAX_CHARACTERS = 30
-TOOLTIP_MAX_LINE = 5
-
-class ToolTip(wx.PopupWindow):
-    
-    def __init__(self, parent, tip, restricted=True):
-        wx.PopupWindow.__init__(self, parent)
-        
-        self.CurrentPosition = wx.Point(0, 0)
-        self.Restricted = restricted
-        
-        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
-        self.SetTip(tip)
-        
-        self.Font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
-        
-        self.Bind(wx.EVT_PAINT, self.OnPaint)
-    
-    def SetFont(self, font):
-        self.Font = font
-        self.RefreshTip()
-    
-    def SetTip(self, tip):
-        lines = []
-        for line in tip.splitlines():
-            if self.Restricted and line != "":
-                words = line.split()
-                new_line = words[0]
-                for word in words[1:]:
-                    if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS:
-                        new_line += " " + word
-                    else:
-                        lines.append(new_line)
-                        new_line = word
-                lines.append(new_line)
-            else:
-                lines.append(line)
-        if self.Restricted and len(lines) > TOOLTIP_MAX_LINE:
-            self.Tip = lines[:TOOLTIP_MAX_LINE]
-            if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3:
-                self.Tip[-1] += "..."
-            else:
-                self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..."
-        else:
-            self.Tip = lines
-        wx.CallAfter(self.RefreshTip)
-    
-    def MoveToolTip(self, pos):
-        screen_size = wx.GetDisplaySize()
-        w, h = self.GetTipExtent()
-        self.CurrentPosition = wx.Point(
-            max(0, min(pos.x, screen_size[0] - w - 4)),
-            max(0, min(pos.y, screen_size[1] - h - 4))) 
-        self.SetPosition(pos)
-    
-    def GetTipExtent(self):
-        max_width = 0
-        max_height = 0
-        for line in self.Tip:
-            dc = wx.MemoryDC()
-            dc.SetFont(self.Font)
-            w, h = dc.GetTextExtent(line)
-            max_width = max(max_width, w)
-            max_height += h
-        return max_width, max_height
-    
-    def RefreshTip(self):
-        if self:
-            w, h = self.GetTipExtent()
-            self.SetSize(wx.Size(w + 4, h + 4))
-            self.SetPosition(self.CurrentPosition)
-            self.Refresh()
-        
-    def OnPaint(self, event):
-        dc = wx.AutoBufferedPaintDC(self)
-        dc.Clear()
-        dc.SetPen(MiterPen(wx.BLACK))
-        dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170)))
-        dc.SetFont(self.Font)
-        dc.BeginDrawing()
-        w, h = self.GetTipExtent()
-        dc.DrawRectangle(0, 0, w + 4, h + 4)
-        offset = 0
-        for line in self.Tip:
-            dc.DrawText(line, 2, offset + 2)
-            w, h = dc.GetTextExtent(line)
-            offset += h
-        dc.EndDrawing()
-        event.Skip()
-
-#-------------------------------------------------------------------------------
 #                    Helpers for highlighting text
 #-------------------------------------------------------------------------------
 
@@ -1107,7 +993,7 @@
     def OnToolTipTimer(self, event):
         value = self.GetToolTipValue()
         if value is not None and self.ToolTipPos is not None:
-            self.ToolTip = ToolTip(self.Parent, value)
+            self.ToolTip = CustomToolTip(self.Parent, value)
             self.ToolTip.MoveToolTip(self.ToolTipPos)
             self.ToolTip.Show()