Package fife :: Package extensions :: Package pychan :: Package widgets' :: Module buttons
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pychan.widgets'.buttons

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2011 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  from common import * 
 25  from basictextwidget import BasicTextWidget 
 26  from fife.extensions.pychan.properties import ImageProperty 
 27   
28 -class Button(BasicTextWidget):
29 """ 30 A basic push button. 31 """
32 - def __init__(self, 33 parent = None, 34 name = None, 35 size = None, 36 min_size = None, 37 max_size = None, 38 helptext = None, 39 position = None, 40 style = None, 41 hexpand = None, 42 vexpand = None, 43 font = None, 44 base_color = None, 45 background_color = None, 46 foreground_color = None, 47 selection_color = None, 48 border_size = None, 49 position_technique = None, 50 is_focusable = None, 51 comment = None, 52 margins = None, 53 text = None):
54 55 self.real_widget = fife.Button("") 56 super(Button,self).__init__(parent=parent, 57 name=name, 58 size=size, 59 min_size=min_size, 60 max_size=max_size, 61 helptext=helptext, 62 position=position, 63 style=style, 64 hexpand=hexpand, 65 vexpand=vexpand, 66 font=font, 67 base_color=base_color, 68 background_color=background_color, 69 foreground_color=foreground_color, 70 selection_color=selection_color, 71 border_size=border_size, 72 position_technique=position_technique, 73 is_focusable=is_focusable, 74 comment=comment, 75 margins=margins, 76 text=text)
77
78 - def clone(self, prefix):
79 btnClone = Button(None, 80 self._createNameWithPrefix(prefix), 81 self.size, 82 self.min_size, 83 self.max_size, 84 self.helptext, 85 self.position, 86 self.style, 87 self.hexpand, 88 self.vexpand, 89 self.font, 90 self.base_color, 91 self.background_color, 92 self.foreground_color, 93 self.selection_color, 94 self.border_size, 95 self.position_technique, 96 self.is_focusable, 97 self.comment, 98 self.margins, 99 self.text) 100 101 return btnClone;
102 103
104 -class ImageButton(BasicTextWidget):
105 """ 106 A basic push button with three different images for the up, down and hover state. 107 108 B{Work in progress.} 109 110 New Attributes 111 ============== 112 113 - up_image: String: The source location of the Image for the B{unpressed} state. 114 - down_image: String: The source location of the Image for the B{pressed} state. 115 - hover_image: String: The source location of the Image for the B{unpressed hovered} state. 116 """ 117 118 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [ Attr('up_image'), 119 Attr('down_image'), 120 PointAttr('offset'), 121 Attr('hover_image') 122 ] 123 124 DEFAULT_UPIMAGE = "" 125 DEFAULT_DOWNIMAGE = "" 126 DEFAULT_HOVERIMAGE = "" 127 DEFAULT_OFFSET = 0,0
128 - def __init__(self, 129 parent = None, 130 name = None, 131 size = None, 132 min_size = None, 133 max_size = None, 134 helptext = None, 135 position = None, 136 style = None, 137 hexpand = None, 138 vexpand = None, 139 font = None, 140 base_color = None, 141 background_color = None, 142 foreground_color = None, 143 selection_color = None, 144 border_size = None, 145 position_technique = None, 146 is_focusable = None, 147 comment = None, 148 margins = None, 149 text = None, 150 up_image=None, 151 down_image=None, 152 hover_image=None, 153 offset=None, 154 real_widget=None):
155 156 if real_widget is None: 157 self.real_widget = fife.TwoButton() 158 else: 159 self.real_widget = real_widget 160 161 # set the defaulst 162 offset = self.DEFAULT_OFFSET 163 164 super(ImageButton,self).__init__(parent=parent, 165 name=name, 166 size=size, 167 min_size=min_size, 168 max_size=max_size, 169 helptext=helptext, 170 position=position, 171 style=style, 172 hexpand=hexpand, 173 vexpand=vexpand, 174 font=font, 175 base_color=base_color, 176 background_color=background_color, 177 foreground_color=foreground_color, 178 selection_color=selection_color, 179 border_size=border_size, 180 position_technique=position_technique, 181 is_focusable=is_focusable, 182 comment=comment, 183 margins=margins, 184 text=text) 185 186 if up_image is not None: 187 self.up_image = up_image 188 else: 189 self.up_image = self.DEFAULT_UPIMAGE 190 191 if down_image is not None: 192 self.down_image = down_image 193 else: 194 self.down_image = self.DEFAULT_DOWNIMAGE 195 196 if hover_image is not None: 197 self.hover_image = hover_image 198 else: 199 self.hover_image = self.DEFAULT_HOVERIMAGE 200 201 # Override anything set when stylize was called 202 if offset is not None: self.offset = offset
203 204
205 - def clone(self, prefix):
206 207 imgButtonClone = ImageButton(None, 208 self._createNameWithPrefix(prefix), 209 self.size, 210 self.min_size, 211 self.max_size, 212 self.helptext, 213 self.position, 214 self.style, 215 self.hexpand, 216 self.vexpand, 217 self.font, 218 self.base_color, 219 self.background_color, 220 self.foreground_color, 221 self.selection_color, 222 self.border_size, 223 self.position_technique, 224 self.is_focusable, 225 self.comment, 226 self.margins, 227 self.text, 228 self.up_image, 229 self.down_image, 230 self.hover_image, 231 self.offset) 232 return imgButtonClone
233 234 235 up_image = ImageProperty("UpImage") 236 down_image = ImageProperty("DownImage") 237 hover_image = ImageProperty("HoverImage") 238
239 - def _setOffset(self, offset):
240 self.real_widget.setDownOffset(offset[0], offset[1])
241 - def _getOffset(self):
242 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset())
243 offset = property(_getOffset,_setOffset) 244
245 - def resizeToContent(self, recurse=True):
246 th, tw = 0, 0 247 if self.text: 248 th = self.real_font.getHeight()#+self.real_font.getSpacing() 249 tw = self.real_font.getWidth(text2gui(self.text))#+self.real_font.getSpacing() 250 self.height = max( 251 self._prop_upimage["image"].getHeight(), 252 self._prop_downimage["image"].getHeight(), 253 self._prop_hoverimage["image"].getHeight(), 254 th) + self.margins[1]*2 255 self.width = max( 256 self._prop_upimage["image"].getWidth(), 257 self._prop_downimage["image"].getWidth(), 258 self._prop_hoverimage["image"].getWidth(), 259 tw) + self.margins[0]*2
260
261 -class ToggleButton(ImageButton):
262 """ 263 A basic push button that can be toggled. 264 265 Unfortunately a bit of code duplication from ImageButton. 266 267 New Attributes 268 ============== 269 270 - group: String: The group the button belongs to. Only one button in each group will be toggled at one time. 271 - toggled: Boolean: Whether the button is toggled or not. 272 """ 273 274 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [ Attr('up_image'), 275 Attr('down_image'), 276 Attr('hover_image'), 277 PointAttr('offset'), 278 Attr('group') 279 ] 280 DEFAULT_GROUP = "" 281
282 - def __init__(self, 283 parent = None, 284 name = None, 285 size = None, 286 min_size = None, 287 max_size = None, 288 helptext = None, 289 position = None, 290 style = None, 291 hexpand = None, 292 vexpand = None, 293 font = None, 294 base_color = None, 295 background_color = None, 296 foreground_color = None, 297 selection_color = None, 298 border_size = None, 299 position_technique = None, 300 is_focusable = None, 301 comment = None, 302 margins = None, 303 text = None, 304 up_image = None, 305 down_image = None, 306 hover_image = None, 307 offset = None, 308 group = None):
309 310 group = self.DEFAULT_GROUP 311 312 super(ToggleButton,self).__init__(parent=parent, 313 name=name, 314 size=size, 315 min_size=min_size, 316 max_size=max_size, 317 helptext=helptext, 318 position=position, 319 style=style, 320 hexpand=hexpand, 321 vexpand=vexpand, 322 font=font, 323 base_color=base_color, 324 background_color=background_color, 325 foreground_color=foreground_color, 326 selection_color=selection_color, 327 border_size=border_size, 328 position_technique=position_technique, 329 is_focusable=is_focusable, 330 comment=comment, 331 margins=margins, 332 text=text, 333 up_image=up_image, 334 down_image=down_image, 335 hover_image=hover_image, 336 offset=offset, 337 real_widget=fife.ToggleButton()) 338 339 if group is not None: self.group = group
340
341 - def clone(self, prefix):
342 toggleButtonClone = ToggleButton(None, 343 self._createNameWithPrefix(prefix), 344 self.size, 345 self.min_size, 346 self.max_size, 347 self.helptext, 348 self.position, 349 self.style, 350 self.hexpand, 351 self.vexpand, 352 self.font, 353 self.base_color, 354 self.background_color, 355 self.foreground_color, 356 self.selection_color, 357 self.border_size, 358 self.position_technique, 359 self.is_focusable, 360 self.comment, 361 self.margins, 362 self.text, 363 self.up_image, 364 self.down_image, 365 self.hover_image, 366 self.offset, 367 self.group) 368 return toggleButtonClone
369
370 - def _setGroup(self,group):
371 if group is not None and group != "": 372 self.real_widget.setGroup( group )
373
374 - def _getGroup(self):
375 return self.real_widget.getGroup()
376 group = property(_getGroup,_setGroup) 377
378 - def _setToggled(self, toggled):
379 self.real_widget.setToggled( toggled )
380
381 - def _isToggled(self):
382 return self.real_widget.isToggled()
383 toggled = property(_isToggled, _setToggled) 384
385 - def resizeToContent(self, recurse=True):
386 # NOTE: Figure out how the spacing comes into play 387 tw, th = 0, 0 388 if self.text: 389 th = self.real_font.getHeight() + self.real_widget.getSpacing() 390 tw = self.real_font.getWidth(text2gui(self.text)) + self.real_widget.getSpacing() 391 self.height = max( 392 self._prop_upimage["image"].getHeight(), 393 self._prop_downimage["image"].getHeight(), 394 self._prop_hoverimage["image"].getHeight(), 395 th) + self.margins[1]*2 396 self.width = max( 397 self._prop_upimage["image"].getWidth(), 398 self._prop_downimage["image"].getWidth(), 399 self._prop_hoverimage["image"].getWidth(), 400 tw) + self.margins[0]*2
401