Package library :: Package pyjamas :: Package ui :: Module Tooltip
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.ui.Tooltip

  1  # Tooltip component for Pyjamas 
  2  # Ported by Willie Gollino from Tooltip component for GWT - Originally by Alexei Sokolov http://gwt.components.googlepages.com/ 
  3  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  4   
  5  from pyjamas.ui.PopupPanel import PopupPanel 
  6  from pyjamas import Factory 
  7  from pyjamas.ui.HTML import HTML 
  8  from pyjamas.ui.RootPanel import RootPanel 
  9  from pyjamas.Timer import Timer 
 10  from pyjamas import Window 
 11  from pyjamas import DOM 
 12   
 13  tooltip_hide_timer = None 
 14   
15 -class Tooltip(PopupPanel):
16 - def __init__(self, sender, offsetX, offsetY, contents, 17 show_delay, hide_delay, styleName, **kwargs):
18 """ contents may be a text string or it may be a widget 19 """ 20 PopupPanel.__init__(self, True, **kwargs) 21 self.show_delay = show_delay 22 self.hide_delay = hide_delay 23 24 if isinstance(contents, basestring): 25 contents = HTML(contents) 26 self.add(contents) 27 28 left = sender.getAbsoluteLeft() + offsetX 29 top = sender.getAbsoluteTop() + offsetY 30 31 self.setPopupPosition(left, top) 32 self.setStyleName(styleName) 33 34 if tooltip_hide_timer: 35 self.tooltip_show_timer = Timer(1, self) 36 else: 37 self.tooltip_show_timer = Timer(self.show_delay, self)
38
39 - def onShowImpl(self, popup):
40 width = self.getOffsetWidth() 41 heigth = self.getOffsetHeight() 42 w_width = Window.getClientWidth() 43 w_heigth = Window.getClientHeight() 44 if w_width > width and w_heigth > heigth: 45 offset_x = self.getAbsoluteLeft() 46 offset_y = self.getAbsoluteTop() 47 element = self.getElement() 48 if (offset_x + width) > w_width: 49 offset_x = w_width - width 50 DOM.setStyleAttribute(element, "left", "%dpx" % offset_x) 51 if (offset_y + heigth) > w_heigth: 52 offset_y = w_heigth - heigth 53 DOM.setStyleAttribute(element, "top", "%dpx" % offset_y)
54
55 - def show(self):
56 global tooltip_hide_timer 57 58 # activate fast tooltips 59 tooltip_hide_timer = Timer(self.hide_delay, self) 60 PopupPanel.show(self)
61
62 - def hide(self, autoClosed=False):
63 self.tooltip_show_timer.cancel() 64 PopupPanel.hide(self, autoClosed)
65
66 - def onTimer(self, timer):
67 global tooltip_hide_timer 68 69 # deactivate fast tooltips on last timer 70 if timer is tooltip_hide_timer: 71 tooltip_hide_timer = None 72 73 if timer is self.tooltip_show_timer: 74 self.show() 75 else: 76 self.hide()
77 78
79 -class TooltipListener:
80 DEFAULT_TOOLTIP_STYLE = "TooltipPopup" 81 DEFAULT_OFFSET_X = 10 82 DEFAULT_OFFSET_Y = 35 83
84 - def __init__(self, text, show_delay = 1000, hide_delay = 5000, styleName = ""):
85 if not styleName: 86 styleName = self.DEFAULT_TOOLTIP_STYLE 87 88 self.tooltip = None 89 self.text = text 90 self.styleName = styleName 91 self.show_delay = show_delay 92 self.hide_delay = hide_delay 93 self.offsetX = self.DEFAULT_OFFSET_X 94 self.offsetY = self.DEFAULT_OFFSET_Y
95
96 - def onMouseEnter(self, sender):
97 self.hide() 98 self.tooltip = Tooltip(sender, self.offsetX, self.offsetY, self.text, self.show_delay, self.hide_delay, self.styleName)
99
100 - def onMouseLeave(self, sender):
101 self.hide()
102
103 - def onMouseMove(self, sender, x, y):
104 pass
105
106 - def onMouseDown(self, sender, x, y):
107 pass
108
109 - def onMouseUp(self, sender, x, y):
110 pass
111
112 - def getStyleName(self):
113 return self.styleName
114
115 - def setStyleName(self, styleName):
116 self.styleName = styleName
117
118 - def getOffsetX(self):
119 return self.offsetX
120
121 - def setOffsetX(self, offsetX):
122 self.offsetX = offsetX
123
124 - def getOffsetY(self):
125 return self.offsetY
126
127 - def setOffsetY(self, offsetY):
128 self.offsetY = offsetY
129
130 - def hide(self):
131 if self.tooltip is not None: 132 self.tooltip.hide()
133 134 Factory.registerClass('pyjamas.ui.Tooltip', 'Tooltip', Tooltip) 135