forked from wxGlade/wxGlade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_dialog.py
167 lines (147 loc) · 6.5 KB
/
color_dialog.py
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
"""
Dialog to select a color
@copyright: 2007 Marcello Semboli
@copyright: 2016 Carsten Grohmann
@copyright: 2016-2020 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import wx
from wx.lib.colourchooser import PyColourChooser
import misc, compat
try:
from wx.lib.colourchooser import ColourChangedEvent, ColourChangedEventBase
ColourChangedEvent(None)
except TypeError:
class ColourChangedEvent(ColourChangedEventBase):
"""Adds GetColour()/GetValue() for compatibility with ColourPickerCtrl and colourselect"""
def __init__(self, newColour):
super(ColourChangedEvent, self).__init__(newColour = newColour)
def GetColour(self):
return self.newColour
def GetValue(self):
return self.newColour
class PyColourChooser(PyColourChooser):
def updateDisplayColour(self, colour):
"""Update the displayed color box (solid) and send the EVT_COLOUR_CHANGED"""
self.solid.SetColour(colour)
evt = ColourChangedEvent(newColour=colour)
wx.PostEvent(self, evt)
except ImportError:
pass # for wxPython 2.8
class wxGladeColorDialog(wx.Dialog):
def __init__(self, colors_dict, parent=None):
wx.Dialog.__init__(self, parent, -1, "")
self.colors_dict = colors_dict
choices = list( self.colors_dict.keys() )
choices.sort()
self.panel_1 = wx.Panel(self, -1)
self.use_null_color = wx.RadioButton( self.panel_1, -1, "wxNullColour", style=wx.RB_GROUP )
self.use_sys_color = wx.RadioButton( self.panel_1, -1, _("System Color") )
self.sys_color = wx.ComboBox( self.panel_1, -1, choices=choices, style=wx.CB_DROPDOWN | wx.CB_READONLY)
self.sys_color_panel = wx.Panel(self.panel_1, -1, size=(250, 20))
self.sys_color_panel.SetBackgroundColour(wx.RED)
self.use_chooser = wx.RadioButton(self.panel_1, -1, _("Custom Color"))
self.color_chooser = PyColourChooser(self, -1)
self.ok = wx.Button(self, wx.ID_OK, _("OK"))
self.cancel = wx.Button(self, wx.ID_CANCEL, _("Cancel"))
self.__set_properties()
self.__do_layout()
self.use_null_color.Bind(wx.EVT_RADIOBUTTON, self.on_use_null_color)
self.use_sys_color.Bind(wx.EVT_RADIOBUTTON, self.on_use_sys_color)
self.use_chooser.Bind(wx.EVT_RADIOBUTTON, self.on_use_chooser)
self.sys_color.Bind(wx.EVT_COMBOBOX, self.display_sys_color)
self.display_sys_color()
for ctrl in (self.use_null_color, self.use_sys_color, self.use_chooser):
ctrl.Bind(wx.EVT_LEFT_DCLICK, lambda evt: self.EndModal(wx.ID_OK) )
def display_sys_color(self, event=None):
colour = self.sys_color.GetValue().strip()
if colour.startswith("wxSYS_COLOUR_"):
colour = getattr(wx, colour[2:], None)
else:
colour = None
if colour:
self.sys_color.SetBackgroundColour(wx.WHITE)
colour = compat.wx_SystemSettings_GetColour(colour)
else:
self.sys_color.SetBackgroundColour(wx.RED)
colour = wx.NullColour
self.sys_color_panel.SetBackgroundColour(colour)
self.sys_color_panel.Refresh()
def on_use_null_color(self, event):
self.sys_color.Enable(False)
self.color_chooser.Enable(False)
self.use_chooser.SetValue(0)
self.use_sys_color.SetValue(0)
def on_use_sys_color(self, event):
self.sys_color.Enable(True)
self.color_chooser.Enable(False)
self.use_chooser.SetValue(0)
self.use_null_color.SetValue(0)
def on_use_chooser(self, event):
self.sys_color.Enable(False)
self.color_chooser.Enable(True)
self.use_sys_color.SetValue(0)
self.use_null_color.SetValue(0)
def get_value(self):
if self.use_null_color.GetValue():
return "wxNullColour"
if self.use_sys_color.GetValue():
return self.sys_color.GetStringSelection()
else:
return misc.color_to_string(self.color_chooser.GetValue())
def set_value(self, value):
value = value.strip()
if value=="wxNullColour":
self.use_null_color.SetValue(1)
self.use_sys_color.SetValue(0)
self.use_chooser.SetValue(0)
self.sys_color.Enable(False)
self.color_chooser.Enable(False)
elif value in self.colors_dict:
self.use_sys_color.SetValue(1)
self.use_chooser.SetValue(0)
self.use_null_color.SetValue(0)
self.sys_color.SetValue(value)
self.sys_color.Enable(True)
self.color_chooser.Enable(False)
else:
self.use_chooser.SetValue(1)
self.use_sys_color.SetValue(0)
self.use_null_color.SetValue(0)
try: self.color_chooser.SetValue(misc.string_to_color(value))
except: pass
self.sys_color.Enable(False)
self.color_chooser.Enable(True)
def __set_properties(self):
self.SetTitle(_("Select widget colour"))
self.use_sys_color.SetValue(1)
self.sys_color.SetSelection(0)
self.ok.SetDefault()
self.use_chooser.SetValue(0)
self.color_chooser.Enable(False)
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(self.use_null_color, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.EXPAND, 5)
sizer_2.Add(wx.StaticLine(self.panel_1, -1), 0, wx.ALL|wx.EXPAND, 5)
sizer_2.Add(self.use_sys_color, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.EXPAND, 5)
sizer_2.Add(self.sys_color, 0, wx.ALL|wx.EXPAND, 5)
sizer_2.Add(self.sys_color_panel, 0, wx.ALL|wx.EXPAND, 5)
sizer_2.Add(wx.StaticLine(self.panel_1, -1), 0, wx.ALL|wx.EXPAND, 5)
sizer_2.Add(self.use_chooser, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.EXPAND, 5)
self.panel_1.SetAutoLayout(1)
self.panel_1.SetSizer(sizer_2)
sizer_2.Fit(self.panel_1)
sizer_2.SetSizeHints(self.panel_1)
sizer_1.Add(self.panel_1, 0, wx.EXPAND, 0)
sizer_1.Add(self.color_chooser, 0, wx.ALL, 5)
sizer_2.Add(wx.StaticLine(self.panel_1, -1), 0, wx.ALL|wx.EXPAND, 5)
sizer_3.Add(self.ok, 0, wx.RIGHT, 13)
sizer_3.Add(self.cancel, 0, 0, 5)
sizer_1.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 10)
self.SetAutoLayout(1)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Layout()