fmcustomizedlg.py
16.8 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
"""
This module contains a custom dialog class used to personalize the appearance of a
:class:`FlatMenu` on the fly, allowing also the user of your application to do the same.
"""
import wx
from UserDict import UserDict
from artmanager import ArtManager
from fmresources import *
from labelbook import LabelBook
_ = wx.GetTranslation
# ---------------------------------------------------------------------------- #
# Class OrderedDict
# ---------------------------------------------------------------------------- #
class OrderedDict(UserDict):
"""
An ordered dictionary implementation.
"""
def __init__(self, dict = None):
self._keys = []
UserDict.__init__(self, dict)
def __delitem__(self, key):
UserDict.__delitem__(self, key)
self._keys.remove(key)
def __setitem__(self, key, item):
UserDict.__setitem__(self, key, item)
if key not in self._keys: self._keys.append(key)
def clear(self):
UserDict.clear(self)
self._keys = []
def copy(self):
dict = UserDict.copy(self)
dict._keys = self._keys[:]
return dict
def items(self):
return zip(self._keys, self.values())
def keys(self):
return self._keys
def popitem(self):
try:
key = self._keys[-1]
except IndexError:
raise KeyError('dictionary is empty')
val = self[key]
del self[key]
return (key, val)
def setdefault(self, key, failobj = None):
UserDict.setdefault(self, key, failobj)
if key not in self._keys: self._keys.append(key)
def update(self, dict):
UserDict.update(self, dict)
for key in dict.keys():
if key not in self._keys: self._keys.append(key)
def values(self):
return map(self.get, self._keys)
# ---------------------------------------------------------------------------- #
# Class FMTitlePanel
# ---------------------------------------------------------------------------- #
class FMTitlePanel(wx.Panel):
"""
Helper class to draw gradient shadings on the dialog.
"""
def __init__(self, parent, title):
"""
Default class constructor.
:param `parent`: the :class:`FMTitlePanel` parent;
:param `title`: the string to use as a dialog title.
"""
wx.Panel.__init__(self, parent)
self._title = title
# Set the panel size
dc = wx.MemoryDC()
dc.SelectObject(wx.EmptyBitmap(1, 1))
dc.SetFont(wx.SystemSettings_GetFont( wx.SYS_DEFAULT_GUI_FONT ))
ww, hh = dc.GetTextExtent("Tp")
dc.SelectObject(wx.NullBitmap)
# Set minimum panel size
if ww < 250:
ww = 250
self.SetSize(wx.Size(ww, hh + 10))
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnEraseBackground(self, event):
"""
Handles the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`FMTitlePanel`.
:param `event`: a :class:`EraseEvent` event to be processed.
:note: This method is intentionally empty to reduce flicker.
"""
pass
def OnPaint(self, event):
"""
Handles the ``wx.EVT_PAINT`` event for :class:`FMTitlePanel`.
:param `event`: a :class:`PaintEvent` event to be processed.
"""
dc = wx.BufferedPaintDC(self)
# Draw the background
colour1 = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
colour2 = ArtManager.Get().LightColour(colour1, 70)
ArtManager.Get().PaintStraightGradientBox(dc, self.GetClientRect(), colour1, colour2, False)
# Draw the text
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetWeight(wx.BOLD)
dc.SetFont(font)
dc.SetTextForeground(wx.BLACK)
dc.DrawText(self._title, 5, 5)
# ---------------------------------------------------------------------------- #
# Class FMCustomizeDlg
# ---------------------------------------------------------------------------- #
class FMCustomizeDlg(wx.Dialog):
"""
Class used to customize the appearance of :class:`FlatMenu` and :class:`FlatMenuBar`.
"""
def __init__(self, parent=None):
"""
Default class constructor.
:param `parent`: the :class:`FMCustomizeDlg` parent window.
"""
self._book = None
if not parent:
wx.Dialog.__init__(self)
return
wx.Dialog.__init__(self, parent, wx.ID_ANY, _("Customize"), wx.DefaultPosition,
wx.DefaultSize, wx.DEFAULT_DIALOG_STYLE)
self._visibleMenus = OrderedDict()
self._hiddenMenus = OrderedDict()
self.CreateDialog()
self.ConnectEvents()
self.GetSizer().Fit(self)
self.GetSizer().SetSizeHints(self)
self.GetSizer().Layout()
self.Centre()
def CreateDialog(self):
""" Actually creates the dialog. """
sz = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sz)
# Create the main book and add some pages into it
style = INB_NO_RESIZE | INB_LEFT | INB_DRAW_SHADOW | INB_BORDER
self._book = LabelBook(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, style)
sz.Add(self._book, 1, wx.EXPAND)
self._book.SetColour(INB_TAB_AREA_BACKGROUND_COLOUR, ArtManager.Get().GetMenuFaceColour())
colour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
self._book.SetColour(INB_ACTIVE_TAB_COLOUR, colour)
self.created = False
self.Initialise()
hsizer = wx.BoxSizer(wx.HORIZONTAL)
# add a separator between the book & the buttons area
hsizer.Add(wx.Button(self, wx.ID_OK, _("&Close")), 0, wx.EXPAND | wx.ALIGN_RIGHT)
sz.Add(wx.StaticLine(self), 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 3)
sz.Add(hsizer, 0, wx.ALIGN_RIGHT | wx.ALL, 2)
def Initialise(self):
""" Initialzes the :class:`LabelBook` pages. """
self._book.DeleteAllPages()
self._book.AddPage(self.CreateMenusPage(), _("Menus"), True)
self._book.AddPage(self.CreateOptionsPage(), _("Options"), False)
def CloseDialog(self):
""" Closes the dialog. """
self.EndModal(wx.ID_OK)
def ConnectEvents(self):
""" Does nothing at the moment. """
pass
def CreateMenusPage(self):
""" Creates the :class:`LabelBook` pages with :class:`FlatMenu` information. """
menus = wx.Panel(self._book, wx.ID_ANY, wx.DefaultPosition, wx.Size(300, 300))
sz = wx.BoxSizer(wx.VERTICAL)
menus.SetSizer(sz)
choices = []
mb = self.GetParent()
if not self.created:
self.order = []
# Add all the menu items that are currently visible to the list
for i in xrange(len(mb._items)):
dummy, lableOnly = ArtManager.Get().GetAccelIndex(mb._items[i].GetTitle())
choices.append(lableOnly)
# Add the menu to the visible menus map
self._visibleMenus.update({lableOnly: mb._items[i].GetMenu()})
if not self.created:
self.order.append(lableOnly)
# Add all hidden menus to the menu bar
for key in self._hiddenMenus.keys():
choices.append(key)
if self.created:
visible = OrderedDict()
hidden = OrderedDict()
for items in self.order:
if items in self._visibleMenus:
visible[items] = self._visibleMenus[items]
elif items in self._hiddenMenus:
hidden[items] = self._hiddenMenus[items]
self._visibleMenus = visible
self._hiddenMenus = hidden
self._menuListId = wx.NewId()
self._checkListMenus = wx.CheckListBox(menus, self._menuListId, pos=wx.DefaultPosition, size=wx.Size(250, 250),
choices=self.order, style=wx.BORDER_SIMPLE)
self._checkListMenus.Bind(wx.EVT_CHECKLISTBOX, self.OnMenuChecked)
# check all visible items
for indx, item in enumerate(self.order):
if item in self._visibleMenus:
self._checkListMenus.Check(indx)
# Add title panel
title = FMTitlePanel(menus, _("Select Menu To Add/Remove:"))
sz.Add(title, 0, wx.EXPAND | wx.ALL, 2)
sz.Add(self._checkListMenus, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, 2)
self.created = True
return menus
def CreateShortcutsPage(self):
""" Creates the :class:`LabelBook` shorcuts page. """
shorcuts = wx.Panel(self._book, wx.ID_ANY, wx.DefaultPosition, wx.Size(300, 300))
return shorcuts
def CreateOptionsPage(self):
""" Creates the :class:`LabelBook` option page which holds the :class:`FlatMenu` styles. """
options = wx.Panel(self._book, wx.ID_ANY, wx.DefaultPosition, wx.Size(300, 300))
# Create some options here
vsizer = wx.BoxSizer(wx.VERTICAL)
options.SetSizer(vsizer)
#-----------------------------------------------------------
# options page layout
# - Menu Style: Default or 2007 (radio group)
#
# - Default Style Settings: (static box)
# + Draw vertical gradient (check box)
# + Draw border (check box)
# + Drop toolbar shadow (check box)
#
# - Colour Scheme (static box)
# + Menu bar background colour (combo button)
#-----------------------------------------------------------
self._menuStyleID = wx.NewId()
choices = [_("Default Style"), _("Metallic")]
self._menuStyle = wx.RadioBox(options, self._menuStyleID, _("Menu bar style"),
wx.DefaultPosition, wx.DefaultSize, choices)
# update the selection
theme = ArtManager.Get().GetMenuTheme()
if theme == Style2007:
self._menuStyle.SetSelection(1)
else:
self._menuStyle.SetSelection(0)
# connect event to the control
self._menuStyle.Bind(wx.EVT_RADIOBOX, self.OnChangeStyle)
vsizer.Add(self._menuStyle, 0, wx.EXPAND | wx.ALL, 5)
self._sbStyle = wx.StaticBoxSizer(wx.StaticBox(options, -1, _("Default style settings")), wx.VERTICAL)
self._drawVertGradID = wx.NewId()
self._verticalGradient = wx.CheckBox(options, self._drawVertGradID, _("Draw vertical gradient"))
self._verticalGradient.Bind(wx.EVT_CHECKBOX, self.OnChangeStyle)
self._sbStyle.Add(self._verticalGradient, 0, wx.EXPAND | wx.ALL, 3)
self._verticalGradient.SetValue(ArtManager.Get().GetMBVerticalGradient())
self._drawBorderID = wx.NewId()
self._drawBorder = wx.CheckBox(options, self._drawBorderID, _("Draw border around menu bar"))
self._drawBorder.Bind(wx.EVT_CHECKBOX, self.OnChangeStyle)
self._sbStyle.Add(self._drawBorder, 0, wx.EXPAND | wx.ALL, 3)
self._drawBorder.SetValue(ArtManager.Get().GetMenuBarBorder())
self._shadowUnderTBID = wx.NewId()
self._shadowUnderTB = wx.CheckBox(options, self._shadowUnderTBID, _("Toolbar float over menu bar"))
self._shadowUnderTB.Bind(wx.EVT_CHECKBOX, self.OnChangeStyle)
self._sbStyle.Add(self._shadowUnderTB, 0, wx.EXPAND | wx.ALL, 3)
self._shadowUnderTB.SetValue(ArtManager.Get().GetRaiseToolbar())
vsizer.Add(self._sbStyle, 0, wx.EXPAND | wx.ALL, 5)
# Misc
sb = wx.StaticBoxSizer(wx.StaticBox(options, -1, _("Colour Scheme")), wx.VERTICAL)
self._colourID = wx.NewId()
colourChoices = ArtManager.Get().GetColourSchemes()
colourChoices.sort()
self._colour = wx.ComboBox(options, self._colourID, ArtManager.Get().GetMenuBarColourScheme(), choices=colourChoices,
style=wx.CB_DROPDOWN | wx.CB_READONLY)
sb.Add(self._colour, 0, wx.EXPAND)
vsizer.Add(sb, 0, wx.EXPAND | wx.ALL, 5)
self._colour.Bind(wx.EVT_COMBOBOX, self.OnChangeStyle)
# update the dialog by sending all possible events to us
event = wx.CommandEvent(wx.wxEVT_COMMAND_RADIOBOX_SELECTED, self._menuStyleID)
event.SetEventObject(self)
event.SetInt(self._menuStyle.GetSelection())
self._menuStyle.ProcessEvent(event)
event.SetEventType(wx.wxEVT_COMMAND_CHECKBOX_CLICKED)
event.SetId(self._drawVertGradID)
event.SetInt(ArtManager.Get().GetMBVerticalGradient())
self._verticalGradient.ProcessEvent(event)
event.SetEventType(wx.wxEVT_COMMAND_CHECKBOX_CLICKED)
event.SetId(self._shadowUnderTBID)
event.SetInt(ArtManager.Get().GetRaiseToolbar())
self._shadowUnderTB.ProcessEvent(event)
event.SetEventType(wx.wxEVT_COMMAND_CHECKBOX_CLICKED)
event.SetId(self._drawBorderID)
event.SetInt(ArtManager.Get().GetMenuBarBorder())
self._drawBorder.ProcessEvent(event)
event.SetEventType(wx.wxEVT_COMMAND_COMBOBOX_SELECTED)
event.SetId(self._colourID)
self._colour.ProcessEvent(event)
return options
def OnMenuChecked(self, event):
"""
Handles the ``wx.EVT_CHECKBOX`` event for :class:`FMCustomizeDlg`.
:param `event`: a :class:`CommandEvent` event to be processed.
:note: This method handles the :class:`FlatMenu` menus visibility.
"""
id = event.GetInt()
checked = self._checkListMenus.IsChecked(id)
menuName = self._checkListMenus.GetString(id)
menu = None
mb = self.GetParent()
if checked:
# remove the item from the hidden map
if self._hiddenMenus.has_key(menuName):
menu = self._hiddenMenus.pop(menuName)
# add it to the visible map
if menu:
self._visibleMenus.update({menuName: menu})
indx = self._checkListMenus.GetItems().index(menuName)
# update the menubar
mb.Insert(indx, menu, menu._menuBarFullTitle)
mb.Refresh()
else:
# remove the item from the visible items
if self._visibleMenus.has_key(menuName):
menu = self._visibleMenus.pop(menuName)
# add it to the hidden map
if menu:
self._hiddenMenus.update({menuName: menu})
# update the menubar
pos = mb.FindMenu(menuName)
if pos != wx.NOT_FOUND:
mb.Remove(pos)
mb.Refresh()
if self.created:
visible = OrderedDict()
hidden = OrderedDict()
for items in self.order:
if items in self._visibleMenus:
visible[items] = self._visibleMenus[items]
elif items in self._hiddenMenus:
hidden[items] = self._hiddenMenus[items]
self._visibleMenus = visible
self._hiddenMenus = hidden
def OnChangeStyle(self, event):
"""
Handles the ``wx.EVT_CHECKBOX`` event for :class:`FMCustomizeDlg`.
:param `event`: a :class:`CommandEvent` event to be processed.
:note: This method handles the :class:`FlatMenu` styles.
"""
mb = self.GetParent()
if event.GetId() == self._menuStyleID:
if event.GetSelection() == 0:
# Default style
ArtManager.Get().SetMenuTheme(StyleXP)
self._drawBorder.Enable()
self._verticalGradient.Enable()
mb.Refresh()
else:
ArtManager.Get().SetMenuTheme(Style2007)
self._drawBorder.Enable(False)
self._verticalGradient.Enable(False)
mb.Refresh()
return
if event.GetId() == self._drawBorderID:
ArtManager.Get().DrawMenuBarBorder(event.IsChecked())
mb.Refresh()
return
if event.GetId() == self._drawVertGradID:
ArtManager.Get().SetMBVerticalGradient(event.IsChecked())
mb.Refresh()
return
if event.GetId() == self._colourID:
selection = _("Default")
sel = self._colour.GetSelection()
if sel != wx.NOT_FOUND:
# select new colour scheme
selection = self._colour.GetStringSelection()
ArtManager.Get().SetMenuBarColour(selection)
mb.Refresh()
return
if event.GetId() == self._shadowUnderTBID:
ArtManager.Get().SetRaiseToolbar(event.IsChecked())
mb.Refresh()
return