1 #---------------------------------------------------------------------- 
   2 # Name:        wxPython.lib.buttons 
   3 # Purpose:     Various kinds of generic buttons, (not native controls but 
  10 # Copyright:   (c) 1999 by Total Control Software 
  11 # Licence:     wxWindows license 
  12 #---------------------------------------------------------------------- 
  15 This module implements various forms of generic buttons, meaning that 
  16 they are not built on native controls but are self-drawn. 
  18 The wxGenButton is the base.  It acts like a normal button but you 
  19 are able to better control how it looks, bevel width, colours, etc. 
  21 wxGenBitmapButton is a button with one or more bitmaps that show 
  22 the various states the button can be in. 
  24 wxGenToggleButton stays depressed when clicked, until clicked again. 
  26 wxGenBitmapToggleButton the same but with bitmaps. 
  30 from wxPython
.wx 
import * 
  32 #---------------------------------------------------------------------- 
  34 class wxGenButtonEvent(wxPyCommandEvent
): 
  35     def __init__(self
, eventType
, ID
): 
  36         wxPyCommandEvent
.__init
__(self
, eventType
, ID
) 
  39     def SetIsDown(self
, isDown
): 
  46 #---------------------------------------------------------------------- 
  48 class wxGenButton(wxWindow
): 
  49     def __init__(self
, parent
, ID
, label
, 
  50                  pos 
= wxDefaultPosition
, size 
= wxDefaultSize
, 
  51                  style 
= 0, validator 
= wxDefaultValidator
, 
  53         wxWindow
.__init
__(self
, parent
, ID
, pos
, size
, style
, name
) 
  54         self
.SetValidator(validator
) 
  59         self
.useFocusInd 
= true
 
  63         if type(size
) == type(()): 
  64             size 
= wxSize(size
[0], size
[1]) 
  67         dsize 
= wxSize(75,23)  ### wxButton_GetDefaultSize() 
  68         if self
.bezelWidth 
> 2: 
  69             dsize
.width 
= dsize
.width 
+ self
.bezelWidth 
- 2 
  70             dsize
.height 
= dsize
.height 
+ self
.bezelWidth 
- 2 
  71         if w 
== -1: w 
= dsize
.width
 
  72         if h 
== -1: h 
= dsize
.height
 
  73         self
.SetSize(wxSize(w
,h
)) 
  74         font 
= parent
.GetFont() 
  76             font 
= wxSystemSettings_GetSystemFont(wxSYS_DEFAULT_GUI_FONT
) 
  80         EVT_LEFT_DOWN(self
,  self
.OnLeftDown
) 
  81         EVT_LEFT_UP(self
,    self
.OnLeftUp
) 
  82         EVT_MOTION(self
,     self
.OnMotion
) 
  83         EVT_SET_FOCUS(self
,  self
.OnGainFocus
) 
  84         EVT_KILL_FOCUS(self
, self
.OnLoseFocus
) 
  85         EVT_KEY_DOWN(self
,   self
.OnKeyDown
) 
  86         EVT_KEY_UP(self
,     self
.OnKeyUp
) 
  91     def SetBezelWidth(self
, width
): 
  92         """Set the width of the 3D effect""" 
  93         self
.bezelWidth 
= width
 
  95     def GetBezelWidth(self
): 
  96         """Return the width of the 3D effect""" 
  97         return self
.bezelWidth
 
  99     def SetUseFocusIndicator(self
, flag
): 
 100         """Specifiy if a focus indicator (dotted line) should be used""" 
 101         self
.useFocusInd 
= flag
 
 103     def GetUseFocusIndicator(self
): 
 104         """Return focus indicator flag""" 
 105         return self
.useFocusInd
 
 108     def InitColours(self
): 
 109         faceClr      
= wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNFACE
) 
 110         textClr      
= wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNTEXT
) 
 111         self
.SetBackgroundColour(faceClr
) 
 112         self
.SetForegroundColour(textClr
) 
 114         shadowClr    
= wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNSHADOW
) 
 115         highlightClr 
= wxSystemSettings_GetSystemColour(wxSYS_COLOUR_BTNHIGHLIGHT
) 
 116         self
.shadowPen    
= wxPen(shadowClr
, 1, wxSOLID
) 
 117         self
.highlightPen 
= wxPen(highlightClr
, 1, wxSOLID
) 
 119         self
.focusIndPen  
= wxPen(textClr
, 1, wxUSER_DASH
) 
 123         evt 
= wxGenButtonEvent(wxEVT_COMMAND_BUTTON_CLICKED
, self
.GetId()) 
 124         evt
.SetIsDown(not self
.up
) 
 125         self
.GetEventHandler().ProcessEvent(evt
) 
 128     def DrawBezel(self
, dc
, x1
, y1
, x2
, y2
): 
 129         # draw the upper left sides 
 131             dc
.SetPen(self
.highlightPen
) 
 133             dc
.SetPen(self
.shadowPen
) 
 134         for i 
in range(self
.bezelWidth
): 
 135             dc
.DrawLine(x1
+i
, y1
, x1
+i
, y2
-i
) 
 136             dc
.DrawLine(x1
, y1
+i
, x2
-i
, y1
+i
) 
 138         # draw the lower right sides 
 140             dc
.SetPen(self
.shadowPen
) 
 142             dc
.SetPen(self
.highlightPen
) 
 143         for i 
in range(self
.bezelWidth
): 
 144             dc
.DrawLine(x1
+i
, y2
-i
, x2
+1, y2
-i
) 
 145             dc
.DrawLine(x2
-i
, y1
+i
, x2
-i
, y2
) 
 148     def DrawLabel(self
, dc
, width
, height
, dw
=0, dy
=0): 
 149         dc
.SetFont(self
.GetFont()) 
 151             dc
.SetTextForeground(self
.GetForegroundColour()) 
 153             dc
.SetTextForeground(wxSystemSettings_GetSystemColour(wxSYS_COLOUR_GRAYTEXT
)) 
 154         label 
= self
.GetLabel() 
 155         tw
, th 
= dc
.GetTextExtent(label
) 
 158         dc
.DrawText(label
, (width
-tw
)/2+dw
, (height
-th
)/2+dy
) 
 161     def DrawFocusIndicator(self
, dc
, w
, h
): 
 163         dc
.SetLogicalFunction(wxINVERT
) 
 164         self
.focusIndPen
.SetColour(self
.GetForegroundColour()) 
 165         self
.focusIndPen
.SetDashes([1,2,1,2])  # This isn't quite working the way I expected... 
 166         dc
.SetPen(self
.focusIndPen
) 
 167         dc
.SetBrush(wxTRANSPARENT_BRUSH
) 
 168         dc
.DrawRectangle(bw
+2,bw
+2, w
-bw
*2-4, h
-bw
*2-4) 
 171     def OnPaint(self
, event
): 
 172         (width
, height
) = self
.GetClientSizeTuple() 
 177         dc
.SetBackground(wxBrush(self
.GetBackgroundColour(), wxSOLID
)) 
 179         self
.DrawBezel(dc
, x1
, y1
, x2
, y2
) 
 180         self
.DrawLabel(dc
, width
, height
) 
 181         if self
.hasFocus 
and self
.useFocusInd
: 
 182             self
.DrawFocusIndicator(dc
, width
, height
) 
 184     def OnEraseBackground(self
, event
): 
 188     def OnLeftDown(self
, event
): 
 189         if not self
.IsEnabled(): 
 197     def OnLeftUp(self
, event
): 
 198         if not self
.IsEnabled(): 
 200         if not self
.up
:    # if the button was down when the mouse was released... 
 207     def OnMotion(self
, event
): 
 208         if not self
.IsEnabled(): 
 210         if event
.LeftIsDown(): 
 211             x
,y 
= event
.GetPositionTuple() 
 212             w
,h 
= self
.GetClientSizeTuple() 
 213             if self
.up 
and x
<w 
and x
>=0 and y
<h 
and y
>=0: 
 217             if not self
.up 
and (x
<0 or y
<0 or x
>=w 
or y
>=h
): 
 223     def OnGainFocus(self
, event
): 
 225         dc 
= wxClientDC(self
) 
 226         w
, h 
= self
.GetClientSizeTuple() 
 228             self
.DrawFocusIndicator(dc
, w
, h
) 
 231     def OnLoseFocus(self
, event
): 
 232         self
.hasFocus 
= false
 
 233         dc 
= wxClientDC(self
) 
 234         w
, h 
= self
.GetClientSizeTuple() 
 236             self
.DrawFocusIndicator(dc
, w
, h
) 
 239     def OnKeyDown(self
, event
): 
 240         if self
.hasFocus 
and event
.KeyCode() == ord(" "): 
 245     def OnKeyUp(self
, event
): 
 246         if self
.hasFocus 
and event
.KeyCode() == ord(" "): 
 253 #---------------------------------------------------------------------- 
 255 class wxGenBitmapButton(wxGenButton
): 
 256     def __init__(self
, parent
, ID
, bitmap
, 
 257                  pos 
= wxDefaultPosition
, size 
= wxDefaultSize
, 
 258                  style 
= 0, validator 
= wxDefaultValidator
, 
 260         wxGenButton
.__init
__(self
, parent
, ID
, "", pos
, size
, style
, validator
, name
) 
 262         self
.bmpLabel 
= bitmap
 
 263         self
.bmpDisabled 
= None 
 265         self
.bmpSelected 
= None 
 267     def GetBitmapLabel(self
): 
 269     def GetBitmapDisabled(self
): 
 270         return self
.bmpDisabled
 
 271     def GetBitmapFocus(self
): 
 273     def GetBitmapSelected(self
): 
 274         return self
.bmpSelected
 
 276     def SetBitmapDisabled(self
, bitmap
): 
 277         self
.bmpDisabled 
= bitmap
 
 278     def SetBitmapFocus(self
, bitmap
): 
 279         self
.bmpFocus 
= bitmap
 
 280         self
.SetUseFocusIndicator(false
) 
 281     def SetBitmapSelected(self
, bitmap
): 
 282         self
.bmpSelected 
= bitmap
 
 283     def SetBitmapLabel(self
, bitmap
): 
 284         self
.bmpLabel 
= bitmap
 
 287     def DrawLabel(self
, dc
, width
, height
, dw
=0, dy
=0): 
 289         if self
.bmpDisabled 
and not self
.IsEnabled(): 
 290             bmp 
= self
.bmpDisabled
 
 291         if self
.bmpFocus 
and self
.hasFocus
: 
 293         if self
.bmpSelected 
and not self
.up
: 
 294             bmp 
= self
.bmpSelected
 
 295         bw
,bh 
= bmp
.GetWidth(), bmp
.GetHeight() 
 298         dc
.DrawBitmap(bmp
, (width
-bw
)/2+dw
, (height
-bh
)/2+dy
, true
) 
 302 #---------------------------------------------------------------------- 
 306     def SetToggle(self
, flag
): 
 312     def OnLeftDown(self
, event
): 
 313         if not self
.IsEnabled(): 
 315         self
.up 
= not self
.up
 
 320     def OnLeftUp(self
, event
): 
 321         if not self
.IsEnabled(): 
 327     def OnKeyDown(self
, event
): 
 330     def OnKeyUp(self
, event
): 
 331         if self
.hasFocus 
and event
.KeyCode() == ord(" "): 
 332             self
.up 
= not self
.up
 
 340 class wxGenToggleButton(__ToggleMixin
, wxGenButton
): 
 343 class wxGenBitmapToggleButton(__ToggleMixin
, wxGenBitmapButton
): 
 346 #----------------------------------------------------------------------