551 if self.currentBox: |
550 if self.currentBox: |
552 # Draw current box |
551 # Draw current box |
553 dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, |
552 dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, |
554 self.currentBox.width * scalex, self.currentBox.height * scaley) |
553 self.currentBox.width * scalex, self.currentBox.height * scaley) |
555 dc.SetUserScale(scalex, scaley) |
554 dc.SetUserScale(scalex, scaley) |
556 |
|
557 #------------------------------------------------------------------------------- |
|
558 # Viewer ToolTip |
|
559 #------------------------------------------------------------------------------- |
|
560 |
|
561 """ |
|
562 Class that implements a custom tool tip |
|
563 """ |
|
564 |
|
565 if wx.Platform == '__WXMSW__': |
|
566 faces = { 'times': 'Times New Roman', |
|
567 'mono' : 'Courier New', |
|
568 'helv' : 'Arial', |
|
569 'other': 'Comic Sans MS', |
|
570 'size' : 10, |
|
571 } |
|
572 else: |
|
573 faces = { 'times': 'Times', |
|
574 'mono' : 'Courier', |
|
575 'helv' : 'Helvetica', |
|
576 'other': 'new century schoolbook', |
|
577 'size' : 12, |
|
578 } |
|
579 |
|
580 TOOLTIP_MAX_CHARACTERS = 30 |
|
581 TOOLTIP_MAX_LINE = 5 |
|
582 |
|
583 class ToolTip(wx.PopupWindow): |
|
584 |
|
585 def __init__(self, parent, tip, restricted=True): |
|
586 wx.PopupWindow.__init__(self, parent) |
|
587 |
|
588 self.CurrentPosition = wx.Point(0, 0) |
|
589 self.Restricted = restricted |
|
590 |
|
591 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) |
|
592 self.SetTip(tip) |
|
593 |
|
594 self.Font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"]) |
|
595 |
|
596 self.Bind(wx.EVT_PAINT, self.OnPaint) |
|
597 |
|
598 def SetFont(self, font): |
|
599 self.Font = font |
|
600 self.RefreshTip() |
|
601 |
|
602 def SetTip(self, tip): |
|
603 lines = [] |
|
604 for line in tip.splitlines(): |
|
605 if self.Restricted and line != "": |
|
606 words = line.split() |
|
607 new_line = words[0] |
|
608 for word in words[1:]: |
|
609 if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS: |
|
610 new_line += " " + word |
|
611 else: |
|
612 lines.append(new_line) |
|
613 new_line = word |
|
614 lines.append(new_line) |
|
615 else: |
|
616 lines.append(line) |
|
617 if self.Restricted and len(lines) > TOOLTIP_MAX_LINE: |
|
618 self.Tip = lines[:TOOLTIP_MAX_LINE] |
|
619 if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3: |
|
620 self.Tip[-1] += "..." |
|
621 else: |
|
622 self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..." |
|
623 else: |
|
624 self.Tip = lines |
|
625 wx.CallAfter(self.RefreshTip) |
|
626 |
|
627 def MoveToolTip(self, pos): |
|
628 screen_size = wx.GetDisplaySize() |
|
629 w, h = self.GetTipExtent() |
|
630 self.CurrentPosition = wx.Point( |
|
631 max(0, min(pos.x, screen_size[0] - w - 4)), |
|
632 max(0, min(pos.y, screen_size[1] - h - 4))) |
|
633 self.SetPosition(pos) |
|
634 |
|
635 def GetTipExtent(self): |
|
636 max_width = 0 |
|
637 max_height = 0 |
|
638 for line in self.Tip: |
|
639 dc = wx.MemoryDC() |
|
640 dc.SetFont(self.Font) |
|
641 w, h = dc.GetTextExtent(line) |
|
642 max_width = max(max_width, w) |
|
643 max_height += h |
|
644 return max_width, max_height |
|
645 |
|
646 def RefreshTip(self): |
|
647 if self: |
|
648 w, h = self.GetTipExtent() |
|
649 self.SetSize(wx.Size(w + 4, h + 4)) |
|
650 self.SetPosition(self.CurrentPosition) |
|
651 self.Refresh() |
|
652 |
|
653 def OnPaint(self, event): |
|
654 dc = wx.AutoBufferedPaintDC(self) |
|
655 dc.Clear() |
|
656 dc.SetPen(MiterPen(wx.BLACK)) |
|
657 dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170))) |
|
658 dc.SetFont(self.Font) |
|
659 dc.BeginDrawing() |
|
660 w, h = self.GetTipExtent() |
|
661 dc.DrawRectangle(0, 0, w + 4, h + 4) |
|
662 offset = 0 |
|
663 for line in self.Tip: |
|
664 dc.DrawText(line, 2, offset + 2) |
|
665 w, h = dc.GetTextExtent(line) |
|
666 offset += h |
|
667 dc.EndDrawing() |
|
668 event.Skip() |
|
669 |
555 |
670 #------------------------------------------------------------------------------- |
556 #------------------------------------------------------------------------------- |
671 # Helpers for highlighting text |
557 # Helpers for highlighting text |
672 #------------------------------------------------------------------------------- |
558 #------------------------------------------------------------------------------- |
673 |
559 |