graphics/GraphicCommons.py
changeset 993 7fbde4a19ec3
parent 945 c1159acb0886
child 1047 efcc2283dd77
equal deleted inserted replaced
992:72ee7f3e3cf3 993:7fbde4a19ec3
   576 TOOLTIP_MAX_CHARACTERS = 30
   576 TOOLTIP_MAX_CHARACTERS = 30
   577 TOOLTIP_MAX_LINE = 5
   577 TOOLTIP_MAX_LINE = 5
   578 
   578 
   579 class ToolTip(wx.PopupWindow):
   579 class ToolTip(wx.PopupWindow):
   580     
   580     
   581     def __init__(self, parent, tip):
   581     def __init__(self, parent, tip, restricted=True):
   582         wx.PopupWindow.__init__(self, parent)
   582         wx.PopupWindow.__init__(self, parent)
   583         
   583         
   584         self.CurrentPosition = wx.Point(0, 0)
   584         self.CurrentPosition = wx.Point(0, 0)
       
   585         self.Restricted = restricted
   585         
   586         
   586         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
   587         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
   587         self.SetTip(tip)
   588         self.SetTip(tip)
   588         
   589         
       
   590         self.Font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
       
   591         
   589         self.Bind(wx.EVT_PAINT, self.OnPaint)
   592         self.Bind(wx.EVT_PAINT, self.OnPaint)
   590         
   593     
       
   594     def SetFont(self, font):
       
   595         self.Font = font
       
   596         self.RefreshTip()
       
   597     
   591     def SetTip(self, tip):
   598     def SetTip(self, tip):
   592         lines = []
   599         lines = []
   593         for line in tip.splitlines():
   600         for line in tip.splitlines():
   594             if line != "":
   601             if self.Restricted and line != "":
   595                 words = line.split()
   602                 words = line.split()
   596                 new_line = words[0]
   603                 new_line = words[0]
   597                 for word in words[1:]:
   604                 for word in words[1:]:
   598                     if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS:
   605                     if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS:
   599                         new_line += " " + word
   606                         new_line += " " + word
   601                         lines.append(new_line)
   608                         lines.append(new_line)
   602                         new_line = word
   609                         new_line = word
   603                 lines.append(new_line)
   610                 lines.append(new_line)
   604             else:
   611             else:
   605                 lines.append(line)
   612                 lines.append(line)
   606         if len(lines) > TOOLTIP_MAX_LINE:
   613         if self.Restricted and len(lines) > TOOLTIP_MAX_LINE:
   607             self.Tip = lines[:TOOLTIP_MAX_LINE]
   614             self.Tip = lines[:TOOLTIP_MAX_LINE]
   608             if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3:
   615             if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3:
   609                 self.Tip[-1] += "..."
   616                 self.Tip[-1] += "..."
   610             else:
   617             else:
   611                 self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..."
   618                 self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..."
   612         else:
   619         else:
   613             self.Tip = lines
   620             self.Tip = lines
   614         wx.CallAfter(self.RefreshTip)
   621         wx.CallAfter(self.RefreshTip)
   615     
   622     
   616     def MoveToolTip(self, pos):
   623     def MoveToolTip(self, pos):
   617         self.CurrentPosition = pos
   624         screen_size = wx.GetDisplaySize()
       
   625         w, h = self.GetTipExtent()
       
   626         self.CurrentPosition = wx.Point(
       
   627             max(0, min(pos.x, screen_size[0] - w - 4)),
       
   628             max(0, min(pos.y, screen_size[1] - h - 4))) 
   618         self.SetPosition(pos)
   629         self.SetPosition(pos)
   619     
   630     
   620     def GetTipExtent(self):
   631     def GetTipExtent(self):
   621         max_width = 0
   632         max_width = 0
   622         max_height = 0
   633         max_height = 0
   623         for line in self.Tip:
   634         for line in self.Tip:
   624             w, h = self.GetTextExtent(line)
   635             dc = wx.MemoryDC()
       
   636             dc.SetFont(self.Font)
       
   637             w, h = dc.GetTextExtent(line)
   625             max_width = max(max_width, w)
   638             max_width = max(max_width, w)
   626             max_height += h
   639             max_height += h
   627         return max_width, max_height
   640         return max_width, max_height
   628     
   641     
   629     def RefreshTip(self):
   642     def RefreshTip(self):
   636     def OnPaint(self, event):
   649     def OnPaint(self, event):
   637         dc = wx.AutoBufferedPaintDC(self)
   650         dc = wx.AutoBufferedPaintDC(self)
   638         dc.Clear()
   651         dc.Clear()
   639         dc.SetPen(MiterPen(wx.BLACK))
   652         dc.SetPen(MiterPen(wx.BLACK))
   640         dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170)))
   653         dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170)))
   641         dc.SetFont(wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"]))
   654         dc.SetFont(self.Font)
   642         dc.BeginDrawing()
   655         dc.BeginDrawing()
   643         w, h = self.GetTipExtent()
   656         w, h = self.GetTipExtent()
   644         dc.DrawRectangle(0, 0, w + 4, h + 4)
   657         dc.DrawRectangle(0, 0, w + 4, h + 4)
   645         offset = 0
   658         offset = 0
   646         for line in self.Tip:
   659         for line in self.Tip: