~ubuntu-branches/ubuntu/trusty/python-traitsui/trusty

« back to all changes in this revision

Viewing changes to traitsui/wx/image_control.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2005, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
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
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: David C. Morrill
 
14
#  Date:   10/29/2004
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
 
 
18
""" Defines a wxPython ImageControl widget that is used by various trait
 
19
    editors to display trait values iconically.
 
20
"""
 
21
 
 
22
#-------------------------------------------------------------------------------
 
23
#  Imports:
 
24
#-------------------------------------------------------------------------------
 
25
 
 
26
import wx
 
27
 
 
28
#-------------------------------------------------------------------------------
 
29
#  'ImageControl' class:
 
30
#-------------------------------------------------------------------------------
 
31
 
 
32
class ImageControl ( wx.Window ):
 
33
    """ A wxPython control that displays an image, which can be selected or
 
34
        unselected by mouse clicks.
 
35
    """
 
36
 
 
37
    # Pens used to draw the 'selection' marker:
 
38
    _selectedPenDark = wx.Pen(
 
39
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DSHADOW ), 1,
 
40
        wx.SOLID )
 
41
 
 
42
    _selectedPenLight = wx.Pen(
 
43
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DHIGHLIGHT ), 1,
 
44
        wx.SOLID )
 
45
 
 
46
    #---------------------------------------------------------------------------
 
47
    #  Initializes the object:
 
48
    #---------------------------------------------------------------------------
 
49
 
 
50
    def __init__ ( self, parent, bitmap, selected = None, handler = None,
 
51
                         padding = 10 ):
 
52
        """ Initializes the object.
 
53
        """
 
54
        wx.Window.__init__( self, parent, -1,
 
55
                            size = wx.Size( bitmap.GetWidth()  + padding,
 
56
                                            bitmap.GetHeight() + padding ) )
 
57
        self._bitmap      = bitmap
 
58
        self._selected    = selected
 
59
        self._handler     = handler
 
60
        self._mouse_over  = False
 
61
        self._button_down = False
 
62
 
 
63
        # Set up the 'paint' event handler:
 
64
        wx.EVT_PAINT( self, self._on_paint )
 
65
 
 
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 )
 
71
 
 
72
    #---------------------------------------------------------------------------
 
73
    #  Gets/Sets the current selection state of the image:
 
74
    #---------------------------------------------------------------------------
 
75
 
 
76
    def Selected ( self, selected = None ):
 
77
        """ Gets or sets the selection state of the image.
 
78
        """
 
79
        if selected is not None:
 
80
            selected = (selected != 0)
 
81
            if selected != self._selected:
 
82
                if selected:
 
83
                    for control in self.GetParent().GetChildren():
 
84
                        if (isinstance( control, ImageControl ) and
 
85
                            control.Selected()):
 
86
                            control.Selected( False )
 
87
                            break
 
88
 
 
89
                self._selected = selected
 
90
                self.Refresh()
 
91
 
 
92
        return self._selected
 
93
 
 
94
    #---------------------------------------------------------------------------
 
95
    #  Gets/Sets the current bitmap image:
 
96
    #---------------------------------------------------------------------------
 
97
 
 
98
    def Bitmap ( self, bitmap = None ):
 
99
        """ Gets or sets the bitmap image.
 
100
        """
 
101
        if bitmap is not None:
 
102
            if bitmap != self._bitmap:
 
103
                self._bitmap = bitmap
 
104
                self.Refresh()
 
105
 
 
106
        return self._bitmap
 
107
 
 
108
    #---------------------------------------------------------------------------
 
109
    #  Gets/Sets the current click handler:
 
110
    #---------------------------------------------------------------------------
 
111
 
 
112
    def Handler ( self, handler = None ):
 
113
        """ Gets or sets the click handler.
 
114
        """
 
115
        if handler is not None:
 
116
            if handler != self._handler:
 
117
                self._handler = handler
 
118
                self.Refresh()
 
119
 
 
120
        return self._handler
 
121
 
 
122
    #---------------------------------------------------------------------------
 
123
    #  Handles the mouse entering the control:
 
124
    #---------------------------------------------------------------------------
 
125
 
 
126
    def _on_enter ( self, event = None ):
 
127
        """ Handles the mouse entering the control.
 
128
        """
 
129
        if self._selected is not None:
 
130
            self._mouse_over = True
 
131
            self.Refresh()
 
132
 
 
133
    #---------------------------------------------------------------------------
 
134
    #  Handles the mouse leaving the control:
 
135
    #---------------------------------------------------------------------------
 
136
 
 
137
    def _on_leave ( self, event = None ):
 
138
        """ Handles the mouse leaving the control.
 
139
        """
 
140
        if self._mouse_over:
 
141
            self._mouse_over = False
 
142
            self.Refresh()
 
143
 
 
144
    #---------------------------------------------------------------------------
 
145
    #  Handles the user pressing the mouse button:
 
146
    #---------------------------------------------------------------------------
 
147
 
 
148
    def _on_left_down ( self, event = None ):
 
149
        """ Handles the user pressing the mouse button.
 
150
        """
 
151
        if self._selected is not None:
 
152
            self.CaptureMouse()
 
153
            self._button_down = True
 
154
            self.Refresh()
 
155
 
 
156
    #---------------------------------------------------------------------------
 
157
    #  Handles the user clicking the control:
 
158
    #---------------------------------------------------------------------------
 
159
 
 
160
    def _on_left_up ( self, event = None ):
 
161
        """ Handles the user clicking the control.
 
162
        """
 
163
        need_refresh = self._button_down
 
164
        if need_refresh:
 
165
            self.ReleaseMouse()
 
166
            self._button_down = False
 
167
 
 
168
        if self._selected is not None:
 
169
            wdx, wdy = self.GetClientSizeTuple()
 
170
            x        = event.GetX()
 
171
            y        = event.GetY()
 
172
            if (0 <= x < wdx) and (0 <= y < wdy):
 
173
                if self._selected != -1:
 
174
                    self.Selected( True )
 
175
                elif need_refresh:
 
176
                    self.Refresh()
 
177
                if self._handler is not None:
 
178
                    self._handler( self )
 
179
                return
 
180
 
 
181
        if need_refresh:
 
182
            self.Refresh()
 
183
 
 
184
    #---------------------------------------------------------------------------
 
185
    #  Handles the control being re-painted:
 
186
    #---------------------------------------------------------------------------
 
187
 
 
188
    def _on_paint ( self, event = None ):
 
189
        """ Handles the control being re-painted.
 
190
        """
 
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 )
 
197
 
 
198
        pens = [ self._selectedPenLight, self._selectedPenDark ]
 
199
        bd   = self._button_down
 
200
 
 
201
        if self._mouse_over:
 
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 )
 
209
 
 
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 )
 
222