1
#------------------------------------------------------------------------------
3
# Copyright (c) 2005, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: David C. Morrill
16
#------------------------------------------------------------------------------
18
""" Defines a wxPython ImageControl widget that is used by various trait
19
editors to display trait values iconically.
22
#-------------------------------------------------------------------------------
24
#-------------------------------------------------------------------------------
28
#-------------------------------------------------------------------------------
29
# 'ImageControl' class:
30
#-------------------------------------------------------------------------------
32
class ImageControl ( wx.Window ):
33
""" A wxPython control that displays an image, which can be selected or
34
unselected by mouse clicks.
37
# Pens used to draw the 'selection' marker:
38
_selectedPenDark = wx.Pen(
39
wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DSHADOW ), 1,
42
_selectedPenLight = wx.Pen(
43
wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DHIGHLIGHT ), 1,
46
#---------------------------------------------------------------------------
47
# Initializes the object:
48
#---------------------------------------------------------------------------
50
def __init__ ( self, parent, bitmap, selected = None, handler = None,
52
""" Initializes the object.
54
wx.Window.__init__( self, parent, -1,
55
size = wx.Size( bitmap.GetWidth() + padding,
56
bitmap.GetHeight() + padding ) )
58
self._selected = selected
59
self._handler = handler
60
self._mouse_over = False
61
self._button_down = False
63
# Set up the 'paint' event handler:
64
wx.EVT_PAINT( self, self._on_paint )
66
# Set up mouse event handlers:
67
wx.EVT_LEFT_DOWN( self, self._on_left_down )
68
wx.EVT_LEFT_UP( self, self._on_left_up )
69
wx.EVT_ENTER_WINDOW( self, self._on_enter )
70
wx.EVT_LEAVE_WINDOW( self, self._on_leave )
72
#---------------------------------------------------------------------------
73
# Gets/Sets the current selection state of the image:
74
#---------------------------------------------------------------------------
76
def Selected ( self, selected = None ):
77
""" Gets or sets the selection state of the image.
79
if selected is not None:
80
selected = (selected != 0)
81
if selected != self._selected:
83
for control in self.GetParent().GetChildren():
84
if (isinstance( control, ImageControl ) and
86
control.Selected( False )
89
self._selected = selected
94
#---------------------------------------------------------------------------
95
# Gets/Sets the current bitmap image:
96
#---------------------------------------------------------------------------
98
def Bitmap ( self, bitmap = None ):
99
""" Gets or sets the bitmap image.
101
if bitmap is not None:
102
if bitmap != self._bitmap:
103
self._bitmap = bitmap
108
#---------------------------------------------------------------------------
109
# Gets/Sets the current click handler:
110
#---------------------------------------------------------------------------
112
def Handler ( self, handler = None ):
113
""" Gets or sets the click handler.
115
if handler is not None:
116
if handler != self._handler:
117
self._handler = handler
122
#---------------------------------------------------------------------------
123
# Handles the mouse entering the control:
124
#---------------------------------------------------------------------------
126
def _on_enter ( self, event = None ):
127
""" Handles the mouse entering the control.
129
if self._selected is not None:
130
self._mouse_over = True
133
#---------------------------------------------------------------------------
134
# Handles the mouse leaving the control:
135
#---------------------------------------------------------------------------
137
def _on_leave ( self, event = None ):
138
""" Handles the mouse leaving the control.
141
self._mouse_over = False
144
#---------------------------------------------------------------------------
145
# Handles the user pressing the mouse button:
146
#---------------------------------------------------------------------------
148
def _on_left_down ( self, event = None ):
149
""" Handles the user pressing the mouse button.
151
if self._selected is not None:
153
self._button_down = True
156
#---------------------------------------------------------------------------
157
# Handles the user clicking the control:
158
#---------------------------------------------------------------------------
160
def _on_left_up ( self, event = None ):
161
""" Handles the user clicking the control.
163
need_refresh = self._button_down
166
self._button_down = False
168
if self._selected is not None:
169
wdx, wdy = self.GetClientSizeTuple()
172
if (0 <= x < wdx) and (0 <= y < wdy):
173
if self._selected != -1:
174
self.Selected( True )
177
if self._handler is not None:
178
self._handler( self )
184
#---------------------------------------------------------------------------
185
# Handles the control being re-painted:
186
#---------------------------------------------------------------------------
188
def _on_paint ( self, event = None ):
189
""" Handles the control being re-painted.
191
wdc = wx.PaintDC( self )
192
wdx, wdy = self.GetClientSizeTuple()
193
bitmap = self._bitmap
194
bdx = bitmap.GetWidth()
195
bdy = bitmap.GetHeight()
196
wdc.DrawBitmap( bitmap, (wdx - bdx) / 2, (wdy - bdy) / 2, True )
198
pens = [ self._selectedPenLight, self._selectedPenDark ]
199
bd = self._button_down
202
wdc.SetBrush( wx.TRANSPARENT_BRUSH )
203
wdc.SetPen( pens[ bd ] )
204
wdc.DrawLine( 0, 0, wdx, 0 )
205
wdc.DrawLine( 0, 1, 0, wdy )
206
wdc.SetPen( pens[ 1 - bd ] )
207
wdc.DrawLine( wdx - 1, 1, wdx - 1, wdy )
208
wdc.DrawLine( 1, wdy - 1, wdx - 1, wdy - 1 )
210
if self._selected is True:
211
wdc.SetBrush( wx.TRANSPARENT_BRUSH )
212
wdc.SetPen( pens[ bd ] )
213
wdc.DrawLine( 1, 1, wdx - 1, 1 )
214
wdc.DrawLine( 1, 1, 1, wdy - 1 )
215
wdc.DrawLine( 2, 2, wdx - 2, 2 )
216
wdc.DrawLine( 2, 2, 2, wdy - 2 )
217
wdc.SetPen( pens[ 1 - bd ] )
218
wdc.DrawLine( wdx - 2, 2, wdx - 2, wdy - 1 )
219
wdc.DrawLine( 2, wdy - 2, wdx - 2, wdy - 2 )
220
wdc.DrawLine( wdx - 3, 3, wdx - 3, wdy - 2 )
221
wdc.DrawLine( 3, wdy - 3, wdx - 3, wdy - 3 )