comparison engine/extensions/pychan/widgets.py @ 154:d29593182f40

- modified clicklabel to provide a mouse listener for hover events - modified clicklabel.i to make new methods available for python - added LabelListener to pychan wrapper, as well as two new methods to Label class to set enter / exit callbacks NOTE:
author chewie@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 11 Oct 2008 14:13:56 +0000
parents fe7ff4808529
children bb9902910067
comparison
equal deleted inserted replaced
153:d8e32b4adc5c 154:d29593182f40
1026 if self._source is not None: 1026 if self._source is not None:
1027 return self._source 1027 return self._source
1028 return self._image 1028 return self._image
1029 image = property(_getImage,_setImage) 1029 image = property(_getImage,_setImage)
1030 1030
1031 class LabelListener(fife.ClickLabelListener):
1032 """ the listener class for label onMouse events
1033
1034 @type btn: object
1035 @param btn: the label widget
1036 """
1037 def __init__(self, lbl):
1038 fife.ClickLabelListener.__init__(self)
1039 self.lbl = lbl
1040 self.entercb = None
1041 self.exitcb = None
1042
1043 def mouseEntered(self, lbl):
1044 if self.entercb:
1045 self.entercb(self.lbl)
1046
1047 def mouseExited(self, lbl):
1048 if self.exitcb:
1049 self.exitcb(self.lbl)
1050
1031 class Label(BasicTextWidget): 1051 class Label(BasicTextWidget):
1032 """ 1052 """
1033 A basic label - displaying a string. 1053 A basic label - displaying a string.
1034 1054
1035 Also allows text wrapping. 1055 Also allows text wrapping and onMouse hover callbacks.
1036 1056
1037 New Attributes 1057 New Attributes
1038 ============== 1058 ==============
1039 1059
1040 - wrap_text: Boolean: Enable/Disable automatic text wrapping. Disabled by default. 1060 - wrap_text: Boolean: Enable/Disable automatic text wrapping. Disabled by default.
1041 Currently to actually see text wrapping you have to explicitly set a max_size with 1061 Currently to actually see text wrapping you have to explicitly set a max_size with
1042 the desired width of the text, as the layout engine is not capable of deriving 1062 the desired width of the text, as the layout engine is not capable of deriving
1043 the maximum width from a parent container. 1063 the maximum width from a parent container.
1064
1044 """ 1065 """
1045 1066
1046 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [BoolAttr('wrap_text')] 1067 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [BoolAttr('wrap_text')]
1047 1068
1048 def __init__(self,wrap_text=False,**kwargs): 1069 def __init__(self,wrap_text=False,**kwargs):
1049 self.real_widget = fife.Label("") 1070 self.real_widget = fife.Label("")
1050 self.wrap_text = wrap_text 1071 self.wrap_text = wrap_text
1051 super(Label,self).__init__(**kwargs) 1072 super(Label,self).__init__(**kwargs)
1052 1073 self.listener = LabelListener(self)
1074 self.real_widget.setListener(self.listener)
1075
1053 def resizeToContent(self): 1076 def resizeToContent(self):
1054 self.real_widget.setWidth( self.max_size[0] ) 1077 self.real_widget.setWidth( self.max_size[0] )
1055 self.real_widget.adjustSize() 1078 self.real_widget.adjustSize()
1056 self.height = self.real_widget.getHeight() + self.margins[1]*2 1079 self.height = self.real_widget.getHeight() + self.margins[1]*2
1057 self.width = self.real_widget.getWidth() + self.margins[0]*2 1080 self.width = self.real_widget.getWidth() + self.margins[0]*2
1059 1082
1060 def _setTextWrapping(self,wrapping): self.real_widget.setTextWrapping(wrapping) 1083 def _setTextWrapping(self,wrapping): self.real_widget.setTextWrapping(wrapping)
1061 def _getTextWrapping(self): self.real_widget.isTextWrapping() 1084 def _getTextWrapping(self): self.real_widget.isTextWrapping()
1062 wrap_text = property(_getTextWrapping,_setTextWrapping) 1085 wrap_text = property(_getTextWrapping,_setTextWrapping)
1063 1086
1087 def setEnterCallback(self, cb):
1088 """
1089 Callback is called when mouse enters the area of Label
1090 callback should have form of function(button)
1091 """
1092 self.listener.entercb = cb
1093
1094 def setExitCallback(self, cb):
1095 """
1096 Callback is called when mouse enters the area of Label
1097 callback should have form of function(button)
1098 """
1099 self.listener.exitcb = cb
1100
1064 class ClickLabel(Label): 1101 class ClickLabel(Label):
1065 """ 1102 """
1066 Deprecated - use L{Label} instead. 1103 Deprecated - use L{Label} instead.
1067 """ 1104 """
1068 __init__ = tools.this_is_deprecated(Label.__init__,message = "ClickLabel - Use Label instead") 1105 __init__ = tools.this_is_deprecated(Label.__init__,message = "ClickLabel - Use Label instead")
1163 def resizeToContent(self): 1200 def resizeToContent(self):
1164 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight()) + self.margins[1]*2 1201 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight()) + self.margins[1]*2
1165 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth()) + self.margins[1]*2 1202 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth()) + self.margins[1]*2
1166 1203
1167 def setEnterCallback(self, cb): 1204 def setEnterCallback(self, cb):
1168 ''' 1205 """
1169 Callback is called when mouse enters the area of ImageButton 1206 Callback is called when mouse enters the area of ImageButton
1170 callback should have form of function(button) 1207 callback should have form of function(button)
1171 ''' 1208 """
1172 self.listener.entercb = cb 1209 self.listener.entercb = cb
1173 1210
1174 def setExitCallback(self, cb): 1211 def setExitCallback(self, cb):
1175 ''' 1212 """
1176 Callback is called when mouse enters the area of ImageButton 1213 Callback is called when mouse enters the area of ImageButton
1177 callback should have form of function(button) 1214 callback should have form of function(button)
1178 ''' 1215 """
1179 self.listener.exitcb = cb 1216 self.listener.exitcb = cb
1180 1217
1181 1218
1182 1219
1183 class CheckBox(BasicTextWidget): 1220 class CheckBox(BasicTextWidget):