File: pickers.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (316 lines) | stat: -rw-r--r-- 13,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#---------------------------------------------------------------------------
# Name:        etg/pickers.py
# Author:      Robin Dunn
#
# Created:     10-Apr-2012
# Copyright:   (c) 2012-2020 by Total Control Software
# License:     wxWindows License
#---------------------------------------------------------------------------

import etgtools
import etgtools.tweaker_tools as tools

PACKAGE   = "wx"
MODULE    = "_core"
NAME      = "pickers"   # Base name of the file to generate to for this script
DOCSTRING = ""

# The classes and/or the basename of the Doxygen XML files to be processed by
# this script.
ITEMS  = [ "wxPickerBase",
           "wxColourPickerCtrl",
           "wxColourPickerEvent",
           "wxFilePickerCtrl",
           "wxDirPickerCtrl",
           "wxFileDirPickerEvent",
           "wxFontPickerCtrl",
           "wxFontPickerEvent"
           ]

#---------------------------------------------------------------------------

def run():
    # Parse the XML file(s) building a collection of Extractor objects
    module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
    etgtools.parseDoxyXML(module, ITEMS)

    #-----------------------------------------------------------------
    # Tweak the parsed meta objects in the module object as needed for
    # customizing the generated code and docstrings.

    c = module.find('wxPickerBase')
    assert isinstance(c, etgtools.ClassDef)
    c.find('CreateBase.id').default = 'wxID_ANY'
    c.find('GetTextCtrlStyle').ignore(False)
    c.find('GetPickerStyle').ignore(False)
    c.find('PostCreation').ignore(False)

    #-----------------------------------------------------------------

    module.addHeaderCode('#include <wx/clrpicker.h>')

    c = module.find('wxColourPickerCtrl')
    tools.fixWindowClass(c)
    module.addGlobalStr('wxColourPickerWidgetNameStr', c)
    module.addGlobalStr('wxColourPickerCtrlNameStr', c)

    c.addItem(etgtools.WigCode("""\
        virtual void UpdatePickerFromTextCtrl();
        virtual void UpdateTextCtrlFromPicker();
        """))

    c = module.find('wxColourPickerEvent')
    tools.fixEventClass(c)

    module.addPyCode("""\
        EVT_COLOURPICKER_CHANGED = wx.PyEventBinder( wxEVT_COLOURPICKER_CHANGED, 1 )
        EVT_COLOURPICKER_CURRENT_CHANGED = wx.PyEventBinder( wxEVT_COLOURPICKER_CURRENT_CHANGED, 1 )
        EVT_COLOURPICKER_DIALOG_CANCELLED = wx.PyEventBinder( wxEVT_COLOURPICKER_DIALOG_CANCELLED, 1 )

        # deprecated wxEVT alias
        wxEVT_COMMAND_COLOURPICKER_CHANGED  = wxEVT_COLOURPICKER_CHANGED
        """)


    #-----------------------------------------------------------------

    module.addHeaderCode('#include <wx/filepicker.h>')

    c = module.find('wxFilePickerCtrl')
    tools.fixWindowClass(c)
    module.addGlobalStr('wxFilePickerWidgetLabel', c)
    module.addGlobalStr('wxFilePickerWidgetNameStr', c)
    module.addGlobalStr('wxFilePickerCtrlNameStr', c)
    module.addGlobalStr('wxFileSelectorPromptStr', c)
    module.addGlobalStr('wxFileSelectorDefaultWildcardStr', c)

    c.addItem(etgtools.WigCode("""\
        virtual void UpdatePickerFromTextCtrl();
        virtual void UpdateTextCtrlFromPicker();
        """))

    # we'll use the [G|S]etPath methods instead so we don't have to mess with wxFileName
    c.find('GetFileName').ignore()
    c.find('SetFileName').ignore()

    c = module.find('wxDirPickerCtrl')
    tools.fixWindowClass(c)
    module.addGlobalStr('wxDirPickerWidgetLabel', c)
    module.addGlobalStr('wxDirPickerWidgetNameStr', c)
    module.addGlobalStr('wxDirPickerCtrlNameStr', c)
    module.addGlobalStr('wxDirSelectorPromptStr', c)

    c.addItem(etgtools.WigCode("""\
        virtual void UpdatePickerFromTextCtrl();
        virtual void UpdateTextCtrlFromPicker();
        """))

    # we'll use the [G|S]etPath methods instead so we don't have to mess with wxFileName
    c.find('GetDirName').ignore()
    c.find('SetDirName').ignore()

    c = module.find('wxFileDirPickerEvent')
    tools.fixEventClass(c)

    module.addPyCode("""\
        EVT_FILEPICKER_CHANGED = wx.PyEventBinder( wxEVT_FILEPICKER_CHANGED, 1 )
        EVT_DIRPICKER_CHANGED = wx.PyEventBinder( wxEVT_DIRPICKER_CHANGED, 1 )

        # deprecated wxEVT aliases
        wxEVT_COMMAND_FILEPICKER_CHANGED   = wxEVT_FILEPICKER_CHANGED
        wxEVT_COMMAND_DIRPICKER_CHANGED    = wxEVT_DIRPICKER_CHANGED
        """)

    #-----------------------------------------------------------------

    module.addHeaderCode('#include <wx/fontpicker.h>')

    c = module.find('wxFontPickerCtrl')
    tools.fixWindowClass(c)
    module.addGlobalStr('wxFontPickerWidgetNameStr', c)
    module.addGlobalStr('wxFontPickerCtrlNameStr', c)

    c.addItem(etgtools.WigCode("""\
        virtual void UpdatePickerFromTextCtrl();
        virtual void UpdateTextCtrlFromPicker();
        """))

    c = module.find('wxFontPickerEvent')
    tools.fixEventClass(c)

    module.addPyCode("""\
        EVT_FONTPICKER_CHANGED = wx.PyEventBinder( wxEVT_FONTPICKER_CHANGED, 1 )

        # deprecated wxEVT alias
        wxEVT_COMMAND_FONTPICKER_CHANGED  = wxEVT_FONTPICKER_CHANGED
        """)

    #-----------------------------------------------------------------
    # The C++ wxColourPickerCtrl uses a wx.Button for the implementation,
    # but that looks and works very badly on Mac because the native
    # button can't change color.  So for the Mac we'll implement our own
    # picker using a wx.BitmapButton instead.
    module.addPyCode("""\
    if 'wxMac' in wx.PlatformInfo:
        class ColourPickerCtrl(PickerBase):
            '''
            This control allows the user to select a colour. The
            implementation varies by platform but is usually a button which
            brings up a `wx.ColourDialog` when clicked.


            Window Styles
            -------------

                ======================  ============================================
                wx.CLRP_DEFAULT         Default style.
                wx.CLRP_USE_TEXTCTRL    Creates a text control to the left of the
                                        picker button which is completely managed
                                        by the `wx.ColourPickerCtrl` and which can
                                        be used by the user to specify a colour.
                                        The text control is automatically synchronized
                                        with the button's value. Use functions defined in
                                        `wx.PickerBase` to modify the text control.
                wx.CLRP_SHOW_LABEL      Shows the colour in HTML form (AABBCC) as the
                                        colour button label (instead of no label at all).
                ======================  ============================================

            Events
            ------

                ========================  ==========================================
                EVT_COLOURPICKER_CHANGED  The user changed the colour selected in the
                                          control either using the button or using the
                                          text control (see wx.CLRP_USE_TEXTCTRL; note
                                          that in this case the event is fired only if
                                          the user's input is valid, i.e. recognizable).
                ========================  ==========================================
            '''

            # ColourData object to be shared by all colour pickers, so they can
            # share the custom colours
            _colourData = None

            #--------------------------------------------------
            class ColourPickerButton(BitmapButton):
                def __init__(self, parent, id=-1, colour=wx.BLACK,
                             pos=wx.DefaultPosition, size=wx.DefaultSize,
                             style = CLRP_DEFAULT_STYLE,
                             validator = wx.DefaultValidator,
                             name = "colourpickerwidget"):

                    wx.BitmapButton.__init__(self, parent, id, wx.Bitmap(1,1),
                                             pos, size, style, validator, name)
                    self.SetColour(colour)
                    self.InvalidateBestSize()
                    self.SetInitialSize(size)
                    self.Bind(wx.EVT_BUTTON, self.OnButtonClick)

                    if ColourPickerCtrl._colourData is None:
                        ColourPickerCtrl._colourData = wx.ColourData()
                        ColourPickerCtrl._colourData.SetChooseFull(True)
                        grey = 0
                        for i in range(16):
                            c = wx.Colour(grey, grey, grey)
                            ColourPickerCtrl._colourData.SetCustomColour(i, c)
                            grey += 16

                def SetColour(self, colour):
                    # force a copy, in case the _colorData is shared
                    self.colour = wx.Colour(colour)
                    bmp = self._makeBitmap()
                    self.SetBitmapLabel(wx.BitmapBundle(bmp))

                def GetColour(self):
                    return self.colour

                def OnButtonClick(self, evt):
                    ColourPickerCtrl._colourData.SetColour(self.colour)
                    dlg = wx.ColourDialog(self, ColourPickerCtrl._colourData)
                    if dlg.ShowModal() == wx.ID_OK:
                        ColourPickerCtrl._colourData = dlg.GetColourData()
                        self.SetColour(ColourPickerCtrl._colourData.GetColour())
                        evt = wx.ColourPickerEvent(self, self.GetId(), self.GetColour())
                        self.GetEventHandler().ProcessEvent(evt)

                def _makeBitmap(self):
                    width = height = 24
                    bg = self.GetColour()
                    if self.HasFlag(CLRP_SHOW_LABEL):
                        w, h = self.GetTextExtent(bg.GetAsString(wx.C2S_HTML_SYNTAX))
                        width += w
                    bmp = wx.Bitmap(width, height)
                    dc = wx.MemoryDC(bmp)
                    dc.SetBackground(wx.Brush(self.colour))
                    dc.Clear()
                    if self.HasFlag(CLRP_SHOW_LABEL):
                        from wx.lib.colourutils import BestLabelColour
                        fg = BestLabelColour(bg)
                        dc.SetTextForeground(fg)
                        dc.DrawText(bg.GetAsString(wx.C2S_HTML_SYNTAX),
                                    (width - w)/2, (height - h)/2)
                    return bmp

            #--------------------------------------------------

            def __init__(self, parent, id=-1, colour=wx.BLACK,
                         pos=wx.DefaultPosition, size=wx.DefaultSize,
                         style = CLRP_DEFAULT_STYLE,
                         validator = wx.DefaultValidator,
                         name = "colourpicker"):
                if type(colour) != wx.Colour:
                    colour = wx.Colour(colour)
                wx.PickerBase.__init__(self)
                self.CreateBase(parent, id, colour.GetAsString(),
                                pos, size, style, validator, name)
                widget = ColourPickerCtrl.ColourPickerButton(
                    self, -1, colour, style=self.GetPickerStyle(style))
                self.SetPickerCtrl(widget)
                widget.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnColourChange)
                self.PostCreation()


            def GetColour(self):
                '''Set the displayed colour.'''
                return self.GetPickerCtrl().GetColour()

            def SetColour(self, colour):
                '''Returns the currently selected colour.'''
                self.GetPickerCtrl().SetColour(colour)
                self.UpdateTextCtrlFromPicker()
            Colour = property(GetColour, SetColour)


            def UpdatePickerFromTextCtrl(self):
                col = wx.Colour(self.GetTextCtrl().GetValue())
                if not col.IsOk():
                    return
                if self.GetColour() != col:
                    self.GetPickerCtrl().SetColour(col)
                    evt = wx.ColourPickerEvent(self, self.GetId(), self.GetColour())
                    self.GetEventHandler().ProcessEvent(evt)

            def UpdateTextCtrlFromPicker(self):
                if not self.GetTextCtrl():
                    return
                self.GetTextCtrl().SetValue(self.GetColour().GetAsString())

            def GetPickerStyle(self, style):
                return style & CLRP_SHOW_LABEL

            def OnColourChange(self, evt):
                self.UpdateTextCtrlFromPicker()
                evt = wx.ColourPickerEvent(self, self.GetId(), self.GetColour())
                self.GetEventHandler().ProcessEvent(evt)
        """)


    #-----------------------------------------------------------------
    tools.doCommonTweaks(module)
    tools.runGenerators(module)


#---------------------------------------------------------------------------
if __name__ == '__main__':
    run()