1#!/usr/bin/env python
2
3"""
4This module implements a demo application that shows many of the features of
5the RibbonBar and related classes found in the wx.ribbon module.  It is very
6similar to the pure-python implementation found in the wx.lib.agw.ribbon
7package.  In fact, this demo was ported from the AGW version of the demo with
8just a few relatively minor edits.
9"""
10
11import wx
12import wx.ribbon as RB
13
14import images
15
16
17# --------------------------------------------------- #
18# Some constants for ribbon buttons
19
20ID_CIRCLE = wx.ID_HIGHEST + 1
21ID_CROSS = ID_CIRCLE + 1
22ID_TRIANGLE = ID_CIRCLE + 2
23ID_SQUARE = ID_CIRCLE + 3
24ID_POLYGON = ID_CIRCLE + 4
25ID_SELECTION_EXPAND_H = ID_CIRCLE + 5
26ID_SELECTION_EXPAND_V = ID_CIRCLE + 6
27ID_SELECTION_CONTRACT = ID_CIRCLE + 7
28ID_PRIMARY_COLOUR = ID_CIRCLE + 8
29ID_SECONDARY_COLOUR = ID_CIRCLE + 9
30ID_DEFAULT_PROVIDER = ID_CIRCLE + 10
31ID_AUI_PROVIDER = ID_CIRCLE + 11
32ID_MSW_PROVIDER = ID_CIRCLE + 12
33ID_MAIN_TOOLBAR = ID_CIRCLE + 13
34ID_POSITION_TOP = ID_CIRCLE + 14
35ID_POSITION_TOP_ICONS = ID_CIRCLE + 15
36ID_POSITION_TOP_BOTH = ID_CIRCLE + 16
37ID_POSITION_LEFT = ID_CIRCLE + 17
38ID_POSITION_LEFT_LABELS = ID_CIRCLE + 18
39ID_POSITION_LEFT_BOTH = ID_CIRCLE + 19
40ID_TOGGLE_PANELS = ID_CIRCLE + 20
41
42
43
44# --------------------------------------------------- #
45
46def CreateBitmap(imgName):
47    bmp = getattr(images, imgName).Bitmap
48    return bmp
49
50
51#---------------------------------------------------------------------------
52
53class ColourClientData(object):
54    def __init__(self, name, colour):
55        self._name = name
56        self._colour = colour
57
58    def GetName(self):
59        return self._name
60
61    def GetColour(self):
62        return self._colour
63
64    @property
65    def name(self):
66        return self._name
67
68    @property
69    def colour(self):
70        return self._colour
71
72#---------------------------------------------------------------------------
73
74class RibbonFrame(wx.Frame):
75
76    def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
77                 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, log=None):
78
79        wx.Frame.__init__(self, parent, id, title, pos, size, style)
80        panel = wx.Panel(self)
81
82        self._ribbon = RB.RibbonBar(panel, style=RB.RIBBON_BAR_DEFAULT_STYLE
83                                                |RB.RIBBON_BAR_SHOW_PANEL_EXT_BUTTONS)
84
85        self._bitmap_creation_dc = wx.MemoryDC()
86        self._colour_data = wx.ColourData()
87
88        home = RB.RibbonPage(self._ribbon, wx.ID_ANY, "Examples", CreateBitmap("ribbon"))
89        toolbar_panel = RB.RibbonPanel(home, wx.ID_ANY, "Toolbar",
90                                       style=RB.RIBBON_PANEL_NO_AUTO_MINIMISE
91                                             |RB.RIBBON_PANEL_EXT_BUTTON)
92
93        toolbar = RB.RibbonToolBar(toolbar_panel, ID_MAIN_TOOLBAR)
94        toolbar.AddTool(wx.ID_ANY, CreateBitmap("align_left"))
95        toolbar.AddTool(wx.ID_ANY, CreateBitmap("align_center"))
96        toolbar.AddTool(wx.ID_ANY, CreateBitmap("align_right"))
97        toolbar.AddSeparator()
98        toolbar.AddHybridTool(wx.ID_NEW, wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_OTHER, wx.Size(16, 15)))
99        toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, wx.Size(16, 15)))
100        toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_OTHER, wx.Size(16, 15)))
101        toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_OTHER, wx.Size(16, 15)))
102        toolbar.AddSeparator()
103        toolbar.AddDropdownTool(wx.ID_UNDO, wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_OTHER, wx.Size(16, 15)))
104        toolbar.AddDropdownTool(wx.ID_REDO, wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_OTHER, wx.Size(16, 15)))
105        toolbar.AddSeparator()
106        toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_REPORT_VIEW, wx.ART_OTHER, wx.Size(16, 15)))
107        toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_OTHER, wx.Size(16, 15)))
108        toolbar.AddSeparator()
109        toolbar.AddHybridTool(ID_POSITION_LEFT, CreateBitmap("position_left"), "Align ribbonbar vertically\non the left\nfor demonstration purposes")
110        toolbar.AddHybridTool(ID_POSITION_TOP, CreateBitmap("position_top"), "Align the ribbonbar horizontally\nat the top\nfor demonstration purposes")
111        toolbar.AddSeparator()
112        toolbar.AddHybridTool(wx.ID_PRINT, wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_OTHER, wx.Size(16, 15)),
113                              "This is the Print button tooltip\ndemonstrating a tooltip")
114        toolbar.SetRows(2, 3)
115
116        selection_panel = RB.RibbonPanel(home, wx.ID_ANY, "Selection", CreateBitmap("selection_panel"))
117        selection = RB.RibbonButtonBar(selection_panel)
118        selection.AddButton(ID_SELECTION_EXPAND_V, "Expand Vertically", CreateBitmap("expand_selection_v"),
119                            "This is a tooltip for Expand Vertically\ndemonstrating a tooltip")
120        selection.AddButton(ID_SELECTION_EXPAND_H, "Expand Horizontally", CreateBitmap("expand_selection_h"), "")
121        selection.AddButton(ID_SELECTION_CONTRACT, "Contract", CreateBitmap("auto_crop_selection"),
122                            CreateBitmap("auto_crop_selection_small"))
123
124        shapes_panel = RB.RibbonPanel(home, wx.ID_ANY, "Shapes", CreateBitmap("circle_small"))
125        shapes = RB.RibbonButtonBar(shapes_panel)
126        shapes.AddButton(ID_CIRCLE, "Circle", CreateBitmap("circle"), CreateBitmap("circle_small"),
127                         help_string="This is a tooltip for the circle button\ndemonstrating another tooltip",
128                         kind=RB.RIBBON_BUTTON_TOGGLE)
129        shapes.AddButton(ID_CROSS, "Cross", CreateBitmap("cross"), "")
130        shapes.AddHybridButton(ID_TRIANGLE, "Triangle", CreateBitmap("triangle"))
131        shapes.AddButton(ID_SQUARE, "Square", CreateBitmap("square"), "")
132        shapes.AddDropdownButton(ID_POLYGON, "Other Polygon", CreateBitmap("hexagon"), "")
133
134        sizer_panel = RB.RibbonPanel(home, wx.ID_ANY, "Panel with Sizer",
135                                     style=RB.RIBBON_PANEL_DEFAULT_STYLE)
136
137        strs = ["Item 1 using a box sizer now", "Item 2 using a box sizer now"]
138        sizer_panelcombo = wx.ComboBox(sizer_panel, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize,
139                                       strs, wx.CB_READONLY)
140
141        sizer_panelcombo2 = wx.ComboBox(sizer_panel, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize,
142                                        strs, wx.CB_READONLY)
143
144        sizer_panelcombo.Select(0)
145        sizer_panelcombo2.Select(1)
146        sizer_panelcombo.SetMinSize(wx.Size(150, -1))
147        sizer_panelcombo2.SetMinSize(wx.Size(150, -1))
148
149        # not using wx.WrapSizer(wx.HORIZONTAL) as it reports an incorrect min height
150        sizer_panelsizer = wx.BoxSizer(wx.VERTICAL)
151        sizer_panelsizer.AddStretchSpacer(1)
152        sizer_panelsizer.Add(sizer_panelcombo, 0, wx.ALL|wx.EXPAND, 2)
153        sizer_panelsizer.Add(sizer_panelcombo2, 0, wx.ALL|wx.EXPAND, 2)
154        sizer_panelsizer.AddStretchSpacer(1)
155        sizer_panel.SetSizer(sizer_panelsizer)
156
157        label_font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_LIGHT)
158        self._bitmap_creation_dc.SetFont(label_font)
159
160        scheme = RB.RibbonPage(self._ribbon, wx.ID_ANY, "Appearance", CreateBitmap("eye"))
161
162        (self._default_primary,
163         self._default_secondary,
164         self._default_tertiary) = self._ribbon.GetArtProvider().GetColourScheme()
165
166        provider_panel = RB.RibbonPanel(scheme, wx.ID_ANY, "Art",
167                                        style=RB.RIBBON_PANEL_NO_AUTO_MINIMISE)
168        provider_bar = RB.RibbonButtonBar(provider_panel, wx.ID_ANY)
169        provider_bar.AddButton(ID_DEFAULT_PROVIDER, "Default Provider",
170                                     wx.ArtProvider.GetBitmap(wx.ART_QUESTION, wx.ART_OTHER, wx.Size(32, 32)), "")
171        provider_bar.AddButton(ID_AUI_PROVIDER, "AUI Provider", CreateBitmap("aui_style"), "")
172        provider_bar.AddButton(ID_MSW_PROVIDER, "MSW Provider", CreateBitmap("msw_style"), "")
173
174        primary_panel = RB.RibbonPanel(scheme, wx.ID_ANY, "Primary Colour", CreateBitmap("colours"))
175        self._primary_gallery = self.PopulateColoursPanel(primary_panel, self._default_primary, ID_PRIMARY_COLOUR)
176
177        secondary_panel = RB.RibbonPanel(scheme, wx.ID_ANY, "Secondary Colour", CreateBitmap("colours"))
178        self._secondary_gallery = self.PopulateColoursPanel(secondary_panel, self._default_secondary, ID_SECONDARY_COLOUR)
179
180        dummy_2 = RB.RibbonPage(self._ribbon, wx.ID_ANY, "Empty Page", CreateBitmap("empty"))
181        dummy_3 = RB.RibbonPage(self._ribbon, wx.ID_ANY, "Another Page", CreateBitmap("empty"))
182
183        self._ribbon.Realize()
184
185        self._logwindow = wx.TextCtrl(panel, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize,
186                                      wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_LEFT | wx.TE_BESTWRAP | wx.BORDER_NONE)
187
188        self._togglePanels = wx.ToggleButton(panel, ID_TOGGLE_PANELS, "&Toggle panels")
189        self._togglePanels.SetValue(True)
190
191        s = wx.BoxSizer(wx.VERTICAL)
192
193        s.Add(self._ribbon, 0, wx.EXPAND)
194        s.Add(self._logwindow, 1, wx.EXPAND)
195        s.Add(self._togglePanels, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 10)
196
197        panel.SetSizer(s)
198        self.panel = panel
199
200        self.BindEvents(selection, shapes, provider_bar, toolbar_panel)
201
202        self.SetIcon(images.Mondrian.Icon)
203        self.CenterOnScreen()
204
205
206
207    def BindEvents(self, selection, shapes, provider_bar, toolbar_panel):
208        provider_bar.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnDefaultProvider, id=ID_DEFAULT_PROVIDER)
209        provider_bar.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnAUIProvider, id=ID_AUI_PROVIDER)
210        provider_bar.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnMSWProvider, id=ID_MSW_PROVIDER)
211        selection.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnSelectionExpandHButton, id=ID_SELECTION_EXPAND_H)
212        selection.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnSelectionExpandVButton, id=ID_SELECTION_EXPAND_V)
213        selection.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnSelectionContractButton, id=ID_SELECTION_CONTRACT)
214        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnCircleButton, id=ID_CIRCLE)
215        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnCrossButton, id=ID_CROSS)
216        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnTriangleButton, id=ID_TRIANGLE)
217        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.OnSquareButton, id=ID_SQUARE)
218        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_DROPDOWN_CLICKED, self.OnTriangleDropdown, id=ID_TRIANGLE)
219        shapes.Bind(RB.EVT_RIBBONBUTTONBAR_DROPDOWN_CLICKED, self.OnPolygonDropdown, id=ID_POLYGON)
220        toolbar_panel.Bind(RB.EVT_RIBBONPANEL_EXTBUTTON_ACTIVATED, self.OnExtButton)
221
222        self.Bind(RB.EVT_RIBBONGALLERY_HOVER_CHANGED, self.OnHoveredColourChange, id=ID_PRIMARY_COLOUR)
223        self.Bind(RB.EVT_RIBBONGALLERY_HOVER_CHANGED, self.OnHoveredColourChange, id=ID_SECONDARY_COLOUR)
224        self.Bind(RB.EVT_RIBBONGALLERY_SELECTED, self.OnPrimaryColourSelect, id=ID_PRIMARY_COLOUR)
225        self.Bind(RB.EVT_RIBBONGALLERY_SELECTED, self.OnSecondaryColourSelect, id=ID_SECONDARY_COLOUR)
226        self.Bind(RB.EVT_RIBBONTOOLBAR_CLICKED, self.OnNew, id=wx.ID_NEW)
227        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnNewDropdown, id=wx.ID_NEW)
228        self.Bind(RB.EVT_RIBBONTOOLBAR_CLICKED, self.OnPrint, id=wx.ID_PRINT)
229        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnPrintDropdown, id=wx.ID_PRINT)
230        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnRedoDropdown, id=wx.ID_REDO)
231        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnUndoDropdown, id=wx.ID_UNDO)
232        self.Bind(RB.EVT_RIBBONTOOLBAR_CLICKED, self.OnPositionLeft, id=ID_POSITION_LEFT)
233        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnPositionLeftDropdown, id=ID_POSITION_LEFT)
234        self.Bind(RB.EVT_RIBBONTOOLBAR_CLICKED, self.OnPositionTop, id=ID_POSITION_TOP)
235        self.Bind(RB.EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED, self.OnPositionTopDropdown, id=ID_POSITION_TOP)
236        self.Bind(wx.EVT_BUTTON, self.OnColourGalleryButton, id=ID_PRIMARY_COLOUR)
237        self.Bind(wx.EVT_BUTTON, self.OnColourGalleryButton, id=ID_SECONDARY_COLOUR)
238        self.Bind(wx.EVT_MENU, self.OnPositionLeftIcons, id=ID_POSITION_LEFT)
239        self.Bind(wx.EVT_MENU, self.OnPositionLeftLabels, id=ID_POSITION_LEFT_LABELS)
240        self.Bind(wx.EVT_MENU, self.OnPositionLeftBoth, id=ID_POSITION_LEFT_BOTH)
241        self.Bind(wx.EVT_MENU, self.OnPositionTopLabels, id=ID_POSITION_TOP)
242        self.Bind(wx.EVT_MENU, self.OnPositionTopIcons, id=ID_POSITION_TOP_ICONS)
243        self.Bind(wx.EVT_MENU, self.OnPositionTopBoth, id=ID_POSITION_TOP_BOTH)
244
245        self._togglePanels.Bind(wx.EVT_TOGGLEBUTTON, self.OnTogglePanels, id=ID_TOGGLE_PANELS)
246
247
248    def SetBarStyle(self, style):
249        self._ribbon.Freeze()
250        self._ribbon.SetWindowStyleFlag(style)
251
252        pTopSize = self.panel.GetSizer()
253        pToolbar = wx.FindWindowById(ID_MAIN_TOOLBAR)
254
255        if style & RB.RIBBON_BAR_FLOW_VERTICAL:
256            self._ribbon.SetTabCtrlMargins(10, 10)
257            pTopSize.SetOrientation(wx.HORIZONTAL)
258            if pToolbar:
259                pToolbar.SetRows(3, 5)
260
261        else:
262            self._ribbon.SetTabCtrlMargins(50, 20)
263            pTopSize.SetOrientation(wx.VERTICAL)
264            if pToolbar:
265                pToolbar.SetRows(2, 3)
266
267        self._ribbon.Realize()
268        self._ribbon.Thaw()
269        self.panel.Layout()
270
271
272    def PopulateColoursPanel(self, panel, defc, gallery_id):
273        gallery = wx.FindWindowById(gallery_id, panel)
274        if gallery:
275            gallery.Clear()
276        else:
277            gallery = RB.RibbonGallery(panel, gallery_id)
278
279        dc = self._bitmap_creation_dc
280        def_item = self.AddColourToGallery(gallery, "Default", dc, defc)
281        gallery.SetSelection(def_item)
282
283        self.AddColourToGallery(gallery, "BLUE", dc)
284        self.AddColourToGallery(gallery, "BLUE VIOLET", dc)
285        self.AddColourToGallery(gallery, "BROWN", dc)
286        self.AddColourToGallery(gallery, "CADET BLUE", dc)
287        self.AddColourToGallery(gallery, "CORAL", dc)
288        self.AddColourToGallery(gallery, "CYAN", dc)
289        self.AddColourToGallery(gallery, "DARK GREEN", dc)
290        self.AddColourToGallery(gallery, "DARK ORCHID", dc)
291        self.AddColourToGallery(gallery, "FIREBRICK", dc)
292        self.AddColourToGallery(gallery, "GOLD", dc)
293        self.AddColourToGallery(gallery, "GOLDENROD", dc)
294        self.AddColourToGallery(gallery, "GREEN", dc)
295        self.AddColourToGallery(gallery, "INDIAN RED", dc)
296        self.AddColourToGallery(gallery, "KHAKI", dc)
297        self.AddColourToGallery(gallery, "LIGHT BLUE", dc)
298        self.AddColourToGallery(gallery, "LIME GREEN", dc)
299        self.AddColourToGallery(gallery, "MAGENTA", dc)
300        self.AddColourToGallery(gallery, "MAROON", dc)
301        self.AddColourToGallery(gallery, "NAVY", dc)
302        self.AddColourToGallery(gallery, "ORANGE", dc)
303        self.AddColourToGallery(gallery, "ORCHID", dc)
304        self.AddColourToGallery(gallery, "PINK", dc)
305        self.AddColourToGallery(gallery, "PLUM", dc)
306        self.AddColourToGallery(gallery, "PURPLE", dc)
307        self.AddColourToGallery(gallery, "RED", dc)
308        self.AddColourToGallery(gallery, "SALMON", dc)
309        self.AddColourToGallery(gallery, "SEA GREEN", dc)
310        self.AddColourToGallery(gallery, "SIENNA", dc)
311        self.AddColourToGallery(gallery, "SKY BLUE", dc)
312        self.AddColourToGallery(gallery, "TAN", dc)
313        self.AddColourToGallery(gallery, "THISTLE", dc)
314        self.AddColourToGallery(gallery, "TURQUOISE", dc)
315        self.AddColourToGallery(gallery, "VIOLET", dc)
316        self.AddColourToGallery(gallery, "VIOLET RED", dc)
317        self.AddColourToGallery(gallery, "WHEAT", dc)
318        self.AddColourToGallery(gallery, "WHITE", dc)
319        self.AddColourToGallery(gallery, "YELLOW", dc)
320
321        return gallery
322
323
324    def GetGalleryColour(self, gallery, item, name=None):
325        data = gallery.GetItemClientData(item)
326
327        if name != None:
328            name = data.GetName()
329
330        return data.GetColour(), name
331
332
333    def OnHoveredColourChange(self, event):
334        # Set the background of the gallery to the hovered colour, or back to the
335        # default if there is no longer a hovered item.
336
337        gallery = event.GetGallery()
338        provider = gallery.GetArtProvider()
339
340        if event.GetGalleryItem() != None:
341            if provider == self._ribbon.GetArtProvider():
342                gallery.SetArtProvider(provider)
343
344            provider.SetColour(RB.RIBBON_ART_GALLERY_HOVER_BACKGROUND_COLOUR,
345                               self.GetGalleryColour(event.GetGallery(), event.GetGalleryItem(), None)[0])
346
347        else:
348            if provider != self._ribbon.GetArtProvider():
349                gallery.SetArtProvider(self._ribbon.GetArtProvider())
350                del provider
351
352
353    def OnPrimaryColourSelect(self, event):
354        colour, name = self.GetGalleryColour(event.GetGallery(), event.GetGalleryItem(), "")
355        self.AddText("Colour %s selected as primary."%name)
356
357        dummy, secondary, tertiary = self._ribbon.GetArtProvider().GetColourScheme()
358        self._ribbon.GetArtProvider().SetColourScheme(colour, secondary, tertiary)
359        self.ResetGalleryArtProviders()
360        self._ribbon.Refresh()
361
362
363    def OnSecondaryColourSelect(self, event):
364        colour, name = self.GetGalleryColour(event.GetGallery(), event.GetGalleryItem(), "")
365        self.AddText("Colour %s selected as secondary."%name)
366
367        primary, dummy, tertiary = self._ribbon.GetArtProvider().GetColourScheme()
368        self._ribbon.GetArtProvider().SetColourScheme(primary, colour, tertiary)
369        self.ResetGalleryArtProviders()
370        self._ribbon.Refresh()
371
372
373    def ResetGalleryArtProviders(self):
374        if self._primary_gallery.GetArtProvider() != self._ribbon.GetArtProvider():
375            self._primary_gallery.SetArtProvider(self._ribbon.GetArtProvider())
376
377        if self._secondary_gallery.GetArtProvider() != self._ribbon.GetArtProvider():
378            self._secondary_gallery.SetArtProvider(self._ribbon.GetArtProvider())
379
380
381    def OnSelectionExpandHButton(self, event):
382        self.AddText("Expand selection horizontally button clicked.")
383
384
385    def OnSelectionExpandVButton(self, event):
386        self.AddText("Expand selection vertically button clicked.")
387
388
389    def OnSelectionContractButton(self, event):
390        self.AddText("Contract selection button clicked.")
391
392
393    def OnCircleButton(self, event):
394        self.AddText("Circle button clicked.")
395
396
397    def OnCrossButton(self, event):
398        self.AddText("Cross button clicked.")
399
400
401    def OnTriangleButton(self, event):
402        self.AddText("Triangle button clicked.")
403
404
405    def OnTriangleDropdown(self, event):
406        menu = wx.Menu()
407        menu.Append(wx.ID_ANY, "Equilateral")
408        menu.Append(wx.ID_ANY, "Isosceles")
409        menu.Append(wx.ID_ANY, "Scalene")
410
411        event.PopupMenu(menu)
412
413
414    def OnSquareButton(self, event):
415        self.AddText("Square button clicked.")
416
417
418    def OnPolygonDropdown(self, event):
419        menu = wx.Menu()
420        menu.Append(wx.ID_ANY, "Pentagon (5 sided)")
421        menu.Append(wx.ID_ANY, "Hexagon (6 sided)")
422        menu.Append(wx.ID_ANY, "Heptagon (7 sided)")
423        menu.Append(wx.ID_ANY, "Octogon (8 sided)")
424        menu.Append(wx.ID_ANY, "Nonagon (9 sided)")
425        menu.Append(wx.ID_ANY, "Decagon (10 sided)")
426
427        event.PopupMenu(menu)
428
429
430    def OnNew(self, event):
431        self.AddText("New button clicked.")
432
433
434    def OnNewDropdown(self, event):
435        menu = wx.Menu()
436        menu.Append(wx.ID_ANY, "New Document")
437        menu.Append(wx.ID_ANY, "New Template")
438        menu.Append(wx.ID_ANY, "New Mail")
439
440        event.PopupMenu(menu)
441
442
443    def OnPrint(self, event):
444        self.AddText("Print button clicked.")
445
446
447    def OnPrintDropdown(self, event):
448        menu = wx.Menu()
449        menu.Append(wx.ID_ANY, "Print")
450        menu.Append(wx.ID_ANY, "Preview")
451        menu.Append(wx.ID_ANY, "Options")
452
453        event.PopupMenu(menu)
454
455
456    def OnRedoDropdown(self, event):
457        menu = wx.Menu()
458        menu.Append(wx.ID_ANY, "Redo E")
459        menu.Append(wx.ID_ANY, "Redo F")
460        menu.Append(wx.ID_ANY, "Redo G")
461
462        event.PopupMenu(menu)
463
464
465    def OnUndoDropdown(self, event):
466        menu = wx.Menu()
467        menu.Append(wx.ID_ANY, "Undo C")
468        menu.Append(wx.ID_ANY, "Undo B")
469        menu.Append(wx.ID_ANY, "Undo A")
470
471        event.PopupMenu(menu)
472
473
474    def OnPositionTopLabels(self, event):
475        self.SetBarStyle(RB.RIBBON_BAR_DEFAULT_STYLE)
476
477
478    def OnPositionTopIcons(self, event):
479        self.SetBarStyle((RB.RIBBON_BAR_DEFAULT_STYLE &~RB.RIBBON_BAR_SHOW_PAGE_LABELS)
480                         | RB.RIBBON_BAR_SHOW_PAGE_ICONS)
481
482
483    def OnPositionTopBoth(self, event):
484        self.SetBarStyle(RB.RIBBON_BAR_DEFAULT_STYLE | RB.RIBBON_BAR_SHOW_PAGE_ICONS)
485
486
487    def OnPositionLeftLabels(self, event):
488        self.SetBarStyle(RB.RIBBON_BAR_DEFAULT_STYLE | RB.RIBBON_BAR_FLOW_VERTICAL)
489
490
491    def OnPositionLeftIcons(self, event):
492        self.SetBarStyle((RB.RIBBON_BAR_DEFAULT_STYLE &~RB.RIBBON_BAR_SHOW_PAGE_LABELS) |
493                         RB.RIBBON_BAR_SHOW_PAGE_ICONS | RB.RIBBON_BAR_FLOW_VERTICAL)
494
495
496    def OnPositionLeftBoth(self, event):
497        self.SetBarStyle(RB.RIBBON_BAR_DEFAULT_STYLE | RB.RIBBON_BAR_SHOW_PAGE_ICONS |
498                         RB.RIBBON_BAR_FLOW_VERTICAL)
499
500
501    def OnPositionTop(self, event):
502        self.OnPositionTopLabels(event)
503
504
505    def OnPositionTopDropdown(self, event):
506        menu = wx.Menu()
507        menu.Append(ID_POSITION_TOP, "Top with Labels")
508        menu.Append(ID_POSITION_TOP_ICONS, "Top with Icons")
509        menu.Append(ID_POSITION_TOP_BOTH, "Top with Both")
510        event.PopupMenu(menu)
511
512
513    def OnPositionLeft(self, event):
514        self.OnPositionLeftIcons(event)
515
516
517    def OnPositionLeftDropdown(self, event):
518        menu = wx.Menu()
519        menu.Append(ID_POSITION_LEFT, "Left with Icons")
520        menu.Append(ID_POSITION_LEFT_LABELS, "Left with Labels")
521        menu.Append(ID_POSITION_LEFT_BOTH, "Left with Both")
522        event.PopupMenu(menu)
523
524
525    def OnTogglePanels(self, event):
526        self._ribbon.ShowPanels(self._togglePanels.GetValue())
527
528
529    def OnExtButton(self, event):
530        wx.MessageBox("Extended button activated")
531
532
533    def AddText(self, msg):
534        self._logwindow.AppendText(msg)
535        self._logwindow.AppendText("\n")
536        self._ribbon.DismissExpandedPanel()
537
538
539    def AddColourToGallery(self, gallery, colour, dc, value=None):
540        item = None
541
542        if colour != "Default":
543            c = wx.Colour(colour)
544
545        if value is not None:
546            c = value
547
548        if c.IsOk():
549
550            iWidth = 64
551            iHeight = 40
552
553            bitmap = wx.Bitmap(iWidth, iHeight)
554            dc.SelectObject(bitmap)
555            b = wx.Brush(c)
556            dc.SetPen(wx.BLACK_PEN)
557            dc.SetBrush(b)
558            dc.DrawRectangle(0, 0, iWidth, iHeight)
559
560            colour = colour[0] + colour[1:].lower()
561            size = wx.Size(*dc.GetTextExtent(colour))
562            notcred = min(abs(~c.Red()), 255)
563            notcgreen = min(abs(~c.Green()), 255)
564            notcblue = min(abs(~c.Blue()), 255)
565
566            foreground = wx.Colour(notcred, notcgreen, notcblue)
567
568            if abs(foreground.Red() - c.Red()) + abs(foreground.Blue() - c.Blue()) + abs(foreground.Green() - c.Green()) < 64:
569                # Foreground too similar to background - use a different
570                # strategy to find a contrasting colour
571                foreground = wx.Colour((c.Red() + 64) % 256, 255 - c.Green(),
572                                       (c.Blue() + 192) % 256)
573
574            dc.SetTextForeground(foreground)
575            dc.DrawText(colour, (iWidth - size.GetWidth() + 1) / 2, (iHeight - size.GetHeight()) / 2)
576            dc.SelectObjectAsSource(wx.NullBitmap)
577
578            item = gallery.Append(bitmap, wx.ID_ANY)
579            gallery.SetItemClientData(item, ColourClientData(colour, c))
580
581        return item
582
583
584    def OnColourGalleryButton(self, event):
585        gallery = event.GetEventObject()
586        if gallery is None:
587            return
588
589        self._ribbon.DismissExpandedPanel()
590        if gallery.GetSelection():
591            self._colour_data.SetColour(self.GetGalleryColour(gallery, gallery.GetSelection(), None)[0])
592
593        dlg = wx.ColourDialog(self, self._colour_data)
594
595        if dlg.ShowModal() == wx.ID_OK:
596
597            self._colour_data = dlg.GetColourData()
598            clr = self._colour_data.GetColour()
599
600            # Try to find colour in gallery
601            item = None
602            for i in range(gallery.GetCount()):
603                item = gallery.GetItem(i)
604                if self.GetGalleryColour(gallery, item, None)[0] == clr:
605                    break
606                else:
607                    item = None
608
609            # Colour not in gallery - add it
610            if item == None:
611                item = self.AddColourToGallery(gallery,
612                                               clr.GetAsString(wx.C2S_HTML_SYNTAX),
613                                               self._bitmap_creation_dc,
614                                               clr)
615                gallery.Realize()
616
617            # Set selection
618            gallery.EnsureVisible(item)
619            gallery.SetSelection(item)
620
621            # Send an event to respond to the selection change
622            dummy = RB.RibbonGalleryEvent(RB.wxEVT_RIBBONGALLERY_SELECTED,
623                                          gallery.GetId())
624            dummy.SetEventObject(gallery)
625            dummy.SetGallery(gallery)
626            dummy.SetGalleryItem(item)
627            self.GetEventHandler().ProcessEvent(dummy)
628
629
630    def OnDefaultProvider(self, event):
631        self._ribbon.DismissExpandedPanel()
632        self.SetArtProvider(RB.RibbonDefaultArtProvider())
633
634
635    def OnAUIProvider(self, event):
636        self._ribbon.DismissExpandedPanel()
637        self.SetArtProvider(RB.RibbonAUIArtProvider())
638
639
640    def OnMSWProvider(self, event):
641        self._ribbon.DismissExpandedPanel()
642        self.SetArtProvider(RB.RibbonMSWArtProvider())
643
644
645    def SetArtProvider(self, prov):
646        self._ribbon.Freeze()
647        self._ribbon.SetArtProvider(prov)
648
649        (self._default_primary,
650         self._default_secondary,
651         self._default_tertiary) = prov.GetColourScheme()
652
653        self.PopulateColoursPanel(self._primary_gallery.GetParent(),
654                                  self._default_primary,
655                                  ID_PRIMARY_COLOUR)
656        self.PopulateColoursPanel(self._secondary_gallery.GetParent(),
657                                  self._default_secondary,
658                                  ID_SECONDARY_COLOUR)
659
660        self._ribbon.Thaw()
661        self.panel.GetSizer().Layout()
662        self._ribbon.Realize()
663
664
665#---------------------------------------------------------------------------
666
667
668if __name__ == '__main__':
669    app = wx.App()
670    frm = RibbonFrame(None, -1, "wx.ribbon Sample Application", size=(800, 600))
671    frm.Show()
672
673    if False:
674        import wx.lib.inspection
675        wx.lib.inspection.InspectionTool().Show()
676
677    app.MainLoop()
678