]> git.saurik.com Git - wxWidgets.git/blob - samples/ribbon/ribbondemo.cpp
976544ee40d133197cec5a1560377d90e3f80e5e
[wxWidgets.git] / samples / ribbon / ribbondemo.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: ribbondemo.cpp
3 // Purpose: wxRibbon: Ribbon user interface - sample/test program
4 // Author: Peter Cawley
5 // Modified by:
6 // Created: 2009-05-25
7 // RCS-ID: $Id$
8 // Copyright: (C) Copyright 2009, Peter Cawley
9 // Licence: wxWindows Library Licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/app.h"
19 #include "wx/frame.h"
20 #include "wx/textctrl.h"
21 #include "wx/ribbon/bar.h"
22 #include "wx/ribbon/buttonbar.h"
23 #include "wx/ribbon/gallery.h"
24 #include "wx/ribbon/toolbar.h"
25 #include "wx/sizer.h"
26 #include "wx/menu.h"
27 #include "wx/dcbuffer.h"
28 #include "wx/colordlg.h"
29 #include "wx/artprov.h"
30 #include "wx/combobox.h"
31 #include "wx/wrapsizer.h"
32
33 // -- application --
34
35 class MyApp : public wxApp
36 {
37 public:
38 bool OnInit();
39 };
40
41 DECLARE_APP(MyApp)
42 IMPLEMENT_APP(MyApp)
43
44 // -- frame --
45
46 class MyFrame : public wxFrame
47 {
48 public:
49 MyFrame();
50 ~MyFrame();
51
52 enum
53 {
54 ID_CIRCLE = wxID_HIGHEST + 1,
55 ID_CROSS,
56 ID_TRIANGLE,
57 ID_SQUARE,
58 ID_POLYGON,
59 ID_SELECTION_EXPAND_H,
60 ID_SELECTION_EXPAND_V,
61 ID_SELECTION_CONTRACT,
62 ID_PRIMARY_COLOUR,
63 ID_SECONDARY_COLOUR,
64 ID_DEFAULT_PROVIDER,
65 ID_AUI_PROVIDER,
66 ID_MSW_PROVIDER,
67 ID_MAIN_TOOLBAR,
68 ID_POSITION_TOP,
69 ID_POSITION_TOP_ICONS,
70 ID_POSITION_TOP_BOTH,
71 ID_POSITION_LEFT,
72 ID_POSITION_LEFT_LABELS,
73 ID_POSITION_LEFT_BOTH,
74 };
75
76 void OnCircleButton(wxRibbonButtonBarEvent& evt);
77 void OnCrossButton(wxRibbonButtonBarEvent& evt);
78 void OnTriangleButton(wxRibbonButtonBarEvent& evt);
79 void OnTriangleDropdown(wxRibbonButtonBarEvent& evt);
80 void OnSquareButton(wxRibbonButtonBarEvent& evt);
81 void OnPolygonDropdown(wxRibbonButtonBarEvent& evt);
82 void OnSelectionExpandVButton(wxRibbonButtonBarEvent& evt);
83 void OnSelectionExpandHButton(wxRibbonButtonBarEvent& evt);
84 void OnSelectionContractButton(wxRibbonButtonBarEvent& evt);
85 void OnHoveredColourChange(wxRibbonGalleryEvent& evt);
86 void OnPrimaryColourSelect(wxRibbonGalleryEvent& evt);
87 void OnSecondaryColourSelect(wxRibbonGalleryEvent& evt);
88 void OnColourGalleryButton(wxCommandEvent& evt);
89 void OnDefaultProvider(wxRibbonButtonBarEvent& evt);
90 void OnAUIProvider(wxRibbonButtonBarEvent& evt);
91 void OnMSWProvider(wxRibbonButtonBarEvent& evt);
92 void OnNew(wxRibbonToolBarEvent& evt);
93 void OnNewDropdown(wxRibbonToolBarEvent& evt);
94 void OnPrint(wxRibbonToolBarEvent& evt);
95 void OnPrintDropdown(wxRibbonToolBarEvent& evt);
96 void OnRedoDropdown(wxRibbonToolBarEvent& evt);
97 void OnUndoDropdown(wxRibbonToolBarEvent& evt);
98 void OnPositionTop(wxRibbonToolBarEvent& evt);
99 void OnPositionTopLabels(wxCommandEvent& evt);
100 void OnPositionTopIcons(wxCommandEvent& evt);
101 void OnPositionTopBoth(wxCommandEvent& evt);
102 void OnPositionTopDropdown(wxRibbonToolBarEvent& evt);
103 void OnPositionLeft(wxRibbonToolBarEvent& evt);
104 void OnPositionLeftLabels(wxCommandEvent& evt);
105 void OnPositionLeftIcons(wxCommandEvent& evt);
106 void OnPositionLeftBoth(wxCommandEvent& evt);
107 void OnPositionLeftDropdown(wxRibbonToolBarEvent& evt);
108
109 protected:
110 wxRibbonGallery* PopulateColoursPanel(wxWindow* panel, wxColour def,
111 int gallery_id);
112 void AddText(wxString msg);
113 wxRibbonGalleryItem* AddColourToGallery(wxRibbonGallery *gallery,
114 wxString name, wxMemoryDC& dc, wxColour* value = NULL);
115 wxColour GetGalleryColour(wxRibbonGallery *gallery,
116 wxRibbonGalleryItem* item, wxString* name);
117 void ResetGalleryArtProviders();
118 void SetArtProvider(wxRibbonArtProvider* prov);
119 void SetBarStyle(long style);
120
121 wxRibbonBar* m_ribbon;
122 wxRibbonGallery* m_primary_gallery;
123 wxRibbonGallery* m_secondary_gallery;
124 wxTextCtrl* m_logwindow;
125 wxColourData m_colour_data;
126 wxColour m_default_primary;
127 wxColour m_default_secondary;
128 wxColour m_default_tertiary;
129 wxMemoryDC m_bitmap_creation_dc;
130
131 DECLARE_EVENT_TABLE()
132 };
133
134 // -- implementations --
135
136 bool MyApp::OnInit()
137 {
138 if(!wxApp::OnInit())
139 return false;
140
141 wxFrame* frame = new MyFrame;
142 frame->Show();
143
144 return true;
145 }
146
147 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
148 EVT_RIBBONBUTTONBAR_CLICKED(ID_DEFAULT_PROVIDER, MyFrame::OnDefaultProvider)
149 EVT_RIBBONBUTTONBAR_CLICKED(ID_AUI_PROVIDER, MyFrame::OnAUIProvider)
150 EVT_RIBBONBUTTONBAR_CLICKED(ID_MSW_PROVIDER, MyFrame::OnMSWProvider)
151 EVT_RIBBONBUTTONBAR_CLICKED(ID_SELECTION_EXPAND_H, MyFrame::OnSelectionExpandHButton)
152 EVT_RIBBONBUTTONBAR_CLICKED(ID_SELECTION_EXPAND_V, MyFrame::OnSelectionExpandVButton)
153 EVT_RIBBONBUTTONBAR_CLICKED(ID_SELECTION_CONTRACT, MyFrame::OnSelectionContractButton)
154 EVT_RIBBONBUTTONBAR_CLICKED(ID_CIRCLE, MyFrame::OnCircleButton)
155 EVT_RIBBONBUTTONBAR_CLICKED(ID_CROSS, MyFrame::OnCrossButton)
156 EVT_RIBBONBUTTONBAR_CLICKED(ID_TRIANGLE, MyFrame::OnTriangleButton)
157 EVT_RIBBONBUTTONBAR_CLICKED(ID_SQUARE, MyFrame::OnSquareButton)
158 EVT_RIBBONBUTTONBAR_DROPDOWN_CLICKED(ID_TRIANGLE, MyFrame::OnTriangleDropdown)
159 EVT_RIBBONBUTTONBAR_DROPDOWN_CLICKED(ID_POLYGON, MyFrame::OnPolygonDropdown)
160 EVT_RIBBONGALLERY_HOVER_CHANGED(ID_PRIMARY_COLOUR, MyFrame::OnHoveredColourChange)
161 EVT_RIBBONGALLERY_HOVER_CHANGED(ID_SECONDARY_COLOUR, MyFrame::OnHoveredColourChange)
162 EVT_RIBBONGALLERY_SELECTED(ID_PRIMARY_COLOUR, MyFrame::OnPrimaryColourSelect)
163 EVT_RIBBONGALLERY_SELECTED(ID_SECONDARY_COLOUR, MyFrame::OnSecondaryColourSelect)
164 EVT_RIBBONTOOLBAR_CLICKED(wxID_NEW, MyFrame::OnNew)
165 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(wxID_NEW, MyFrame::OnNewDropdown)
166 EVT_RIBBONTOOLBAR_CLICKED(wxID_PRINT, MyFrame::OnPrint)
167 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(wxID_PRINT, MyFrame::OnPrintDropdown)
168 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(wxID_REDO, MyFrame::OnRedoDropdown)
169 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(wxID_UNDO, MyFrame::OnUndoDropdown)
170 EVT_RIBBONTOOLBAR_CLICKED(ID_POSITION_LEFT, MyFrame::OnPositionLeft)
171 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(ID_POSITION_LEFT, MyFrame::OnPositionLeftDropdown)
172 EVT_RIBBONTOOLBAR_CLICKED(ID_POSITION_TOP, MyFrame::OnPositionTop)
173 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(ID_POSITION_TOP, MyFrame::OnPositionTopDropdown)
174 EVT_BUTTON(ID_PRIMARY_COLOUR, MyFrame::OnColourGalleryButton)
175 EVT_BUTTON(ID_SECONDARY_COLOUR, MyFrame::OnColourGalleryButton)
176 EVT_MENU(ID_POSITION_LEFT, MyFrame::OnPositionLeftIcons)
177 EVT_MENU(ID_POSITION_LEFT_LABELS, MyFrame::OnPositionLeftLabels)
178 EVT_MENU(ID_POSITION_LEFT_BOTH, MyFrame::OnPositionLeftBoth)
179 EVT_MENU(ID_POSITION_TOP, MyFrame::OnPositionTopLabels)
180 EVT_MENU(ID_POSITION_TOP_ICONS, MyFrame::OnPositionTopIcons)
181 EVT_MENU(ID_POSITION_TOP_BOTH, MyFrame::OnPositionTopBoth)
182 END_EVENT_TABLE()
183
184 #include "align_center.xpm"
185 #include "align_left.xpm"
186 #include "align_right.xpm"
187 #include "aui_style.xpm"
188 #include "auto_crop_selection.xpm"
189 #include "auto_crop_selection_small.xpm"
190 #include "circle.xpm"
191 #include "circle_small.xpm"
192 #include "colours.xpm"
193 #include "cross.xpm"
194 #include "empty.xpm"
195 #include "expand_selection_v.xpm"
196 #include "expand_selection_h.xpm"
197 #include "eye.xpm"
198 #include "hexagon.xpm"
199 #include "msw_style.xpm"
200 #include "position_left_small.xpm"
201 #include "position_top_small.xpm"
202 #include "ribbon.xpm"
203 #include "selection_panel.xpm"
204 #include "square.xpm"
205 #include "triangle.xpm"
206
207 MyFrame::MyFrame()
208 : wxFrame(NULL, wxID_ANY, wxT("wxRibbon Sample Application"), wxDefaultPosition, wxSize(800, 600), wxDEFAULT_FRAME_STYLE)
209 {
210 m_ribbon = new wxRibbonBar(this, wxID_ANY);
211
212 {
213 wxRibbonPage* home = new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Examples"), ribbon_xpm);
214 wxRibbonPanel *toolbar_panel = new wxRibbonPanel(home, wxID_ANY, wxT("Toolbar"),
215 wxNullBitmap, wxDefaultPosition, wxDefaultSize,
216 wxRIBBON_PANEL_NO_AUTO_MINIMISE);
217 wxRibbonToolBar *toolbar = new wxRibbonToolBar(toolbar_panel, ID_MAIN_TOOLBAR);
218 toolbar->AddTool(wxID_ANY, align_left_xpm);
219 toolbar->AddTool(wxID_ANY, align_center_xpm);
220 toolbar->AddTool(wxID_ANY, align_right_xpm);
221 toolbar->AddSeparator();
222 toolbar->AddHybridTool(wxID_NEW, wxArtProvider::GetBitmap(wxART_NEW, wxART_OTHER, wxSize(16, 15)));
223 toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_OTHER, wxSize(16, 15)));
224 toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_OTHER, wxSize(16, 15)));
225 toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_SAVE_AS, wxART_OTHER, wxSize(16, 15)));
226 toolbar->AddSeparator();
227 toolbar->AddDropdownTool(wxID_UNDO, wxArtProvider::GetBitmap(wxART_UNDO, wxART_OTHER, wxSize(16, 15)));
228 toolbar->AddDropdownTool(wxID_REDO, wxArtProvider::GetBitmap(wxART_REDO, wxART_OTHER, wxSize(16, 15)));
229 toolbar->AddSeparator();
230 toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_OTHER, wxSize(16, 15)));
231 toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_OTHER, wxSize(16, 15)));
232 toolbar->AddSeparator();
233 toolbar->AddHybridTool(ID_POSITION_LEFT, position_left_xpm,
234 "Align ribbonbar vertically\non the left\nfor demonstration purposes");
235 toolbar->AddHybridTool(ID_POSITION_TOP, position_top_xpm,
236 "Align the ribbonbar horizontally\nat the top\nfor demonstration purposes");
237 toolbar->AddSeparator();
238 toolbar->AddHybridTool(wxID_PRINT, wxArtProvider::GetBitmap(wxART_PRINT, wxART_OTHER, wxSize(16, 15)),
239 "This is the Print button tooltip\ndemonstrating a tooltip");
240 toolbar->SetRows(2, 3);
241
242 wxRibbonPanel *selection_panel = new wxRibbonPanel(home, wxID_ANY, wxT("Selection"), wxBitmap(selection_panel_xpm));
243 wxRibbonButtonBar *selection = new wxRibbonButtonBar(selection_panel);
244 selection->AddButton(ID_SELECTION_EXPAND_V, wxT("Expand Vertically"), wxBitmap(expand_selection_v_xpm),
245 "This is a tooltip for Expand Vertically\ndemonstrating a tooltip");
246 selection->AddButton(ID_SELECTION_EXPAND_H, wxT("Expand Horizontally"), wxBitmap(expand_selection_h_xpm), wxEmptyString);
247 selection->AddButton(ID_SELECTION_CONTRACT, wxT("Contract"), wxBitmap(auto_crop_selection_xpm), wxBitmap(auto_crop_selection_small_xpm));
248
249 wxRibbonPanel *shapes_panel = new wxRibbonPanel(home, wxID_ANY, wxT("Shapes"), wxBitmap(circle_small_xpm));
250 wxRibbonButtonBar *shapes = new wxRibbonButtonBar(shapes_panel);
251 shapes->AddButton(ID_CIRCLE, wxT("Circle"), wxBitmap(circle_xpm), wxBitmap(circle_small_xpm),
252 wxNullBitmap, wxNullBitmap, wxRIBBON_BUTTON_NORMAL,
253 "This is a tooltip for the circle button\ndemonstrating another tooltip");
254 shapes->AddButton(ID_CROSS, wxT("Cross"), wxBitmap(cross_xpm), wxEmptyString);
255 shapes->AddHybridButton(ID_TRIANGLE, wxT("Triangle"), wxBitmap(triangle_xpm));
256 shapes->AddButton(ID_SQUARE, wxT("Square"), wxBitmap(square_xpm), wxEmptyString);
257 shapes->AddDropdownButton(ID_POLYGON, wxT("Other Polygon"), wxBitmap(hexagon_xpm), wxEmptyString);
258
259 wxRibbonPanel *sizer_panel = new wxRibbonPanel(home, wxID_ANY, wxT("Panel with Sizer"),
260 wxNullBitmap, wxDefaultPosition, wxDefaultSize,
261 wxRIBBON_PANEL_DEFAULT_STYLE);
262
263 wxArrayString as;
264 as.Add("Item 1 using a box sizer now");
265 as.Add("Item 2 using a box sizer now");
266 wxComboBox* sizer_panelcombo = new wxComboBox(sizer_panel, wxID_ANY,
267 wxEmptyString,
268 wxDefaultPosition, wxDefaultSize,
269 as, wxCB_READONLY);
270
271 wxComboBox* sizer_panelcombo2 = new wxComboBox(sizer_panel, wxID_ANY,
272 wxEmptyString,
273 wxDefaultPosition, wxDefaultSize,
274 as, wxCB_READONLY);
275
276 sizer_panelcombo->Select(0);
277 sizer_panelcombo2->Select(1);
278 sizer_panelcombo->SetMinSize(wxSize(150, -1));
279 sizer_panelcombo2->SetMinSize(wxSize(150, -1));
280
281 //not using wxWrapSizer(wxHORIZONTAL) as it reports an incorrect min height
282 wxSizer* sizer_panelsizer = new wxBoxSizer(wxVERTICAL);
283 sizer_panelsizer->AddStretchSpacer(1);
284 sizer_panelsizer->Add(sizer_panelcombo, 0, wxALL|wxEXPAND, 2);
285 sizer_panelsizer->Add(sizer_panelcombo2, 0, wxALL|wxEXPAND, 2);
286 sizer_panelsizer->AddStretchSpacer(1);
287 sizer_panel->SetSizer(sizer_panelsizer);
288
289 wxFont label_font(8, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_LIGHT);
290 m_bitmap_creation_dc.SetFont(label_font);
291
292 wxRibbonPage* scheme = new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Appearance"), eye_xpm);
293 m_ribbon->GetArtProvider()->GetColourScheme(&m_default_primary,
294 &m_default_secondary, &m_default_tertiary);
295 wxRibbonPanel *provider_panel = new wxRibbonPanel(scheme, wxID_ANY,
296 wxT("Art"), wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxRIBBON_PANEL_NO_AUTO_MINIMISE);
297 wxRibbonButtonBar *provider_bar = new wxRibbonButtonBar(provider_panel, wxID_ANY);
298 provider_bar->AddButton(ID_DEFAULT_PROVIDER, wxT("Default Provider"),
299 wxArtProvider::GetBitmap(wxART_QUESTION, wxART_OTHER, wxSize(32, 32)));
300 provider_bar->AddButton(ID_AUI_PROVIDER, wxT("AUI Provider"), aui_style_xpm);
301 provider_bar->AddButton(ID_MSW_PROVIDER, wxT("MSW Provider"), msw_style_xpm);
302 wxRibbonPanel *primary_panel = new wxRibbonPanel(scheme, wxID_ANY,
303 wxT("Primary Colour"), colours_xpm);
304 m_primary_gallery = PopulateColoursPanel(primary_panel,
305 m_default_primary, ID_PRIMARY_COLOUR);
306 wxRibbonPanel *secondary_panel = new wxRibbonPanel(scheme, wxID_ANY,
307 wxT("Secondary Colour"), colours_xpm);
308 m_secondary_gallery = PopulateColoursPanel(secondary_panel,
309 m_default_secondary, ID_SECONDARY_COLOUR);
310 }
311 new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Empty Page"), empty_xpm);
312 new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Another Page"), empty_xpm);
313
314 m_ribbon->Realize();
315
316 m_logwindow = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
317 wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY |
318 wxTE_LEFT | wxTE_BESTWRAP | wxBORDER_NONE);
319
320 wxSizer *s = new wxBoxSizer(wxVERTICAL);
321
322 s->Add(m_ribbon, 0, wxEXPAND);
323 s->Add(m_logwindow, 1, wxEXPAND);
324
325 SetSizer(s);
326 }
327
328 void MyFrame::SetBarStyle(long style)
329 {
330 m_ribbon->Freeze();
331 m_ribbon->SetWindowStyleFlag(style);
332 wxBoxSizer *pTopSize = reinterpret_cast<wxBoxSizer*>(GetSizer());
333 wxRibbonToolBar *pToolbar = wxDynamicCast(FindWindow(ID_MAIN_TOOLBAR), wxRibbonToolBar);
334 if(style & wxRIBBON_BAR_FLOW_VERTICAL)
335 {
336 m_ribbon->SetTabCtrlMargins(10, 10);
337 pTopSize->SetOrientation(wxHORIZONTAL);
338 if(pToolbar)
339 pToolbar->SetRows(3, 5);
340 }
341 else
342 {
343 m_ribbon->SetTabCtrlMargins(50, 20);
344 pTopSize->SetOrientation(wxVERTICAL);
345 if(pToolbar)
346 pToolbar->SetRows(2, 3);
347 }
348 m_ribbon->Realise();
349 Layout();
350 m_ribbon->Thaw();
351 }
352
353 MyFrame::~MyFrame()
354 {
355 }
356
357 class ColourClientData : public wxClientData
358 {
359 public:
360 ColourClientData(const wxString& name, const wxColour& colour)
361 : m_name(name), m_colour(colour) {}
362
363 const wxString& GetName() const {return m_name;}
364 const wxColour& GetColour() const {return m_colour;}
365
366 private:
367 wxString m_name;
368 wxColour m_colour;
369 };
370
371 wxRibbonGallery* MyFrame::PopulateColoursPanel(wxWindow* panel,
372 wxColour def, int gallery_id)
373 {
374 wxRibbonGallery *gallery = wxDynamicCast(panel->FindWindow(gallery_id), wxRibbonGallery);
375 if(gallery)
376 gallery->Clear();
377 else
378 gallery = new wxRibbonGallery(panel, gallery_id);
379 wxMemoryDC& dc = m_bitmap_creation_dc;
380 wxRibbonGalleryItem *def_item =
381 AddColourToGallery(gallery, wxT("Default"), dc, &def);
382 gallery->SetSelection(def_item);
383 AddColourToGallery(gallery, wxT("BLUE"), dc);
384 AddColourToGallery(gallery, wxT("BLUE VIOLET"), dc);
385 AddColourToGallery(gallery, wxT("BROWN"), dc);
386 AddColourToGallery(gallery, wxT("CADET BLUE"), dc);
387 AddColourToGallery(gallery, wxT("CORAL"), dc);
388 AddColourToGallery(gallery, wxT("CYAN"), dc);
389 AddColourToGallery(gallery, wxT("DARK GREEN"), dc);
390 AddColourToGallery(gallery, wxT("DARK ORCHID"), dc);
391 AddColourToGallery(gallery, wxT("FIREBRICK"), dc);
392 AddColourToGallery(gallery, wxT("GOLD"), dc);
393 AddColourToGallery(gallery, wxT("GOLDENROD"), dc);
394 AddColourToGallery(gallery, wxT("GREEN"), dc);
395 AddColourToGallery(gallery, wxT("INDIAN RED"), dc);
396 AddColourToGallery(gallery, wxT("KHAKI"), dc);
397 AddColourToGallery(gallery, wxT("LIGHT BLUE"), dc);
398 AddColourToGallery(gallery, wxT("LIME GREEN"), dc);
399 AddColourToGallery(gallery, wxT("MAGENTA"), dc);
400 AddColourToGallery(gallery, wxT("MAROON"), dc);
401 AddColourToGallery(gallery, wxT("NAVY"), dc);
402 AddColourToGallery(gallery, wxT("ORANGE"), dc);
403 AddColourToGallery(gallery, wxT("ORCHID"), dc);
404 AddColourToGallery(gallery, wxT("PINK"), dc);
405 AddColourToGallery(gallery, wxT("PLUM"), dc);
406 AddColourToGallery(gallery, wxT("PURPLE"), dc);
407 AddColourToGallery(gallery, wxT("RED"), dc);
408 AddColourToGallery(gallery, wxT("SALMON"), dc);
409 AddColourToGallery(gallery, wxT("SEA GREEN"), dc);
410 AddColourToGallery(gallery, wxT("SIENNA"), dc);
411 AddColourToGallery(gallery, wxT("SKY BLUE"), dc);
412 AddColourToGallery(gallery, wxT("TAN"), dc);
413 AddColourToGallery(gallery, wxT("THISTLE"), dc);
414 AddColourToGallery(gallery, wxT("TURQUOISE"), dc);
415 AddColourToGallery(gallery, wxT("VIOLET"), dc);
416 AddColourToGallery(gallery, wxT("VIOLET RED"), dc);
417 AddColourToGallery(gallery, wxT("WHEAT"), dc);
418 AddColourToGallery(gallery, wxT("WHITE"), dc);
419 AddColourToGallery(gallery, wxT("YELLOW"), dc);
420
421 return gallery;
422 }
423
424 wxColour MyFrame::GetGalleryColour(wxRibbonGallery *gallery,
425 wxRibbonGalleryItem* item, wxString* name)
426 {
427 ColourClientData *data = (ColourClientData*)gallery->GetItemClientObject(item);
428 if(name != NULL)
429 *name = data->GetName();
430 return data->GetColour();
431 }
432
433 void MyFrame::OnHoveredColourChange(wxRibbonGalleryEvent& evt)
434 {
435 // Set the background of the gallery to the hovered colour, or back to the
436 // default if there is no longer a hovered item.
437
438 wxRibbonGallery *gallery = evt.GetGallery();
439 wxRibbonArtProvider *provider = gallery->GetArtProvider();
440
441 if(evt.GetGalleryItem() != NULL)
442 {
443 if(provider == m_ribbon->GetArtProvider())
444 {
445 provider = provider->Clone();
446 gallery->SetArtProvider(provider);
447 }
448 provider->SetColour(wxRIBBON_ART_GALLERY_HOVER_BACKGROUND_COLOUR,
449 GetGalleryColour(evt.GetGallery(), evt.GetGalleryItem(), NULL));
450 }
451 else
452 {
453 if(provider != m_ribbon->GetArtProvider())
454 {
455 gallery->SetArtProvider(m_ribbon->GetArtProvider());
456 delete provider;
457 }
458 }
459 }
460
461 void MyFrame::OnPrimaryColourSelect(wxRibbonGalleryEvent& evt)
462 {
463 wxString name;
464 wxColour colour = GetGalleryColour(evt.GetGallery(), evt.GetGalleryItem(), &name);
465 AddText(wxT("Colour \"") + name + wxT("\" selected as primary."));
466 wxColour secondary, tertiary;
467 m_ribbon->GetArtProvider()->GetColourScheme(NULL, &secondary, &tertiary);
468 m_ribbon->GetArtProvider()->SetColourScheme(colour, secondary, tertiary);
469 ResetGalleryArtProviders();
470 m_ribbon->Refresh();
471 }
472
473 void MyFrame::OnSecondaryColourSelect(wxRibbonGalleryEvent& evt)
474 {
475 wxString name;
476 wxColour colour = GetGalleryColour(evt.GetGallery(), evt.GetGalleryItem(), &name);
477 AddText(wxT("Colour \"") + name + wxT("\" selected as secondary."));
478 wxColour primary, tertiary;
479 m_ribbon->GetArtProvider()->GetColourScheme(&primary, NULL, &tertiary);
480 m_ribbon->GetArtProvider()->SetColourScheme(primary, colour, tertiary);
481 ResetGalleryArtProviders();
482 m_ribbon->Refresh();
483 }
484
485 void MyFrame::ResetGalleryArtProviders()
486 {
487 if(m_primary_gallery->GetArtProvider() != m_ribbon->GetArtProvider())
488 {
489 delete m_primary_gallery->GetArtProvider();
490 m_primary_gallery->SetArtProvider(m_ribbon->GetArtProvider());
491 }
492 if(m_secondary_gallery->GetArtProvider() != m_ribbon->GetArtProvider())
493 {
494 delete m_secondary_gallery->GetArtProvider();
495 m_secondary_gallery->SetArtProvider(m_ribbon->GetArtProvider());
496 }
497 }
498
499 void MyFrame::OnSelectionExpandHButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
500 {
501 AddText(wxT("Expand selection horizontally button clicked."));
502 }
503
504 void MyFrame::OnSelectionExpandVButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
505 {
506 AddText(wxT("Expand selection vertically button clicked."));
507 }
508
509 void MyFrame::OnSelectionContractButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
510 {
511 AddText(wxT("Contract selection button clicked."));
512 }
513
514 void MyFrame::OnCircleButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
515 {
516 AddText(wxT("Circle button clicked."));
517 }
518
519 void MyFrame::OnCrossButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
520 {
521 AddText(wxT("Cross button clicked."));
522 }
523
524 void MyFrame::OnTriangleButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
525 {
526 AddText(wxT("Triangle button clicked."));
527 }
528
529 void MyFrame::OnTriangleDropdown(wxRibbonButtonBarEvent& evt)
530 {
531 wxMenu menu;
532 menu.Append(wxID_ANY, wxT("Equilateral"));
533 menu.Append(wxID_ANY, wxT("Isosceles"));
534 menu.Append(wxID_ANY, wxT("Scalene"));
535
536 evt.PopupMenu(&menu);
537 }
538
539 void MyFrame::OnSquareButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
540 {
541 AddText(wxT("Square button clicked."));
542 }
543
544 void MyFrame::OnPolygonDropdown(wxRibbonButtonBarEvent& evt)
545 {
546 wxMenu menu;
547 menu.Append(wxID_ANY, wxT("Pentagon (5 sided)"));
548 menu.Append(wxID_ANY, wxT("Hexagon (6 sided)"));
549 menu.Append(wxID_ANY, wxT("Heptagon (7 sided)"));
550 menu.Append(wxID_ANY, wxT("Octogon (8 sided)"));
551 menu.Append(wxID_ANY, wxT("Nonagon (9 sided)"));
552 menu.Append(wxID_ANY, wxT("Decagon (10 sided)"));
553
554 evt.PopupMenu(&menu);
555 }
556
557 void MyFrame::OnNew(wxRibbonToolBarEvent& WXUNUSED(evt))
558 {
559 AddText(wxT("New button clicked."));
560 }
561
562 void MyFrame::OnNewDropdown(wxRibbonToolBarEvent& evt)
563 {
564 wxMenu menu;
565 menu.Append(wxID_ANY, wxT("New Document"));
566 menu.Append(wxID_ANY, wxT("New Template"));
567 menu.Append(wxID_ANY, wxT("New Mail"));
568
569 evt.PopupMenu(&menu);
570 }
571
572 void MyFrame::OnPrint(wxRibbonToolBarEvent& WXUNUSED(evt))
573 {
574 AddText(wxT("Print button clicked."));
575 }
576
577 void MyFrame::OnPrintDropdown(wxRibbonToolBarEvent& evt)
578 {
579 wxMenu menu;
580 menu.Append(wxID_ANY, wxT("Print"));
581 menu.Append(wxID_ANY, wxT("Preview"));
582 menu.Append(wxID_ANY, wxT("Options"));
583
584 evt.PopupMenu(&menu);
585 }
586
587 void MyFrame::OnRedoDropdown(wxRibbonToolBarEvent& evt)
588 {
589 wxMenu menu;
590 menu.Append(wxID_ANY, wxT("Redo E"));
591 menu.Append(wxID_ANY, wxT("Redo F"));
592 menu.Append(wxID_ANY, wxT("Redo G"));
593
594 evt.PopupMenu(&menu);
595 }
596
597 void MyFrame::OnUndoDropdown(wxRibbonToolBarEvent& evt)
598 {
599 wxMenu menu;
600 menu.Append(wxID_ANY, wxT("Undo C"));
601 menu.Append(wxID_ANY, wxT("Undo B"));
602 menu.Append(wxID_ANY, wxT("Undo A"));
603
604 evt.PopupMenu(&menu);
605 }
606
607 void MyFrame::OnPositionTopLabels(wxCommandEvent& WXUNUSED(evt))
608 {
609 SetBarStyle(wxRIBBON_BAR_DEFAULT_STYLE);
610 }
611
612 void MyFrame::OnPositionTopIcons(wxCommandEvent& WXUNUSED(evt))
613 {
614 SetBarStyle((wxRIBBON_BAR_DEFAULT_STYLE &~wxRIBBON_BAR_SHOW_PAGE_LABELS)
615 | wxRIBBON_BAR_SHOW_PAGE_ICONS);
616 }
617
618 void MyFrame::OnPositionTopBoth(wxCommandEvent& WXUNUSED(evt))
619 {
620 SetBarStyle(wxRIBBON_BAR_DEFAULT_STYLE | wxRIBBON_BAR_SHOW_PAGE_ICONS);
621 }
622
623 void MyFrame::OnPositionLeftLabels(wxCommandEvent& WXUNUSED(evt))
624 {
625 SetBarStyle(wxRIBBON_BAR_DEFAULT_STYLE | wxRIBBON_BAR_FLOW_VERTICAL);
626 }
627
628 void MyFrame::OnPositionLeftIcons(wxCommandEvent& WXUNUSED(evt))
629 {
630 SetBarStyle((wxRIBBON_BAR_DEFAULT_STYLE &~wxRIBBON_BAR_SHOW_PAGE_LABELS) |
631 wxRIBBON_BAR_SHOW_PAGE_ICONS | wxRIBBON_BAR_FLOW_VERTICAL);
632 }
633
634 void MyFrame::OnPositionLeftBoth(wxCommandEvent& WXUNUSED(evt))
635 {
636 SetBarStyle(wxRIBBON_BAR_DEFAULT_STYLE | wxRIBBON_BAR_SHOW_PAGE_ICONS |
637 wxRIBBON_BAR_FLOW_VERTICAL);
638 }
639
640 void MyFrame::OnPositionTop(wxRibbonToolBarEvent& evt)
641 {
642 OnPositionTopLabels(evt);
643 }
644
645 void MyFrame::OnPositionTopDropdown(wxRibbonToolBarEvent& evt)
646 {
647 wxMenu menu;
648 menu.Append(ID_POSITION_TOP, wxT("Top with Labels"));
649 menu.Append(ID_POSITION_TOP_ICONS, wxT("Top with Icons"));
650 menu.Append(ID_POSITION_TOP_BOTH, wxT("Top with Both"));
651 evt.PopupMenu(&menu);
652 }
653
654 void MyFrame::OnPositionLeft(wxRibbonToolBarEvent& evt)
655 {
656 OnPositionLeftIcons(evt);
657 }
658
659 void MyFrame::OnPositionLeftDropdown(wxRibbonToolBarEvent& evt)
660 {
661 wxMenu menu;
662 menu.Append(ID_POSITION_LEFT, wxT("Left with Icons"));
663 menu.Append(ID_POSITION_LEFT_LABELS, wxT("Left with Labels"));
664 menu.Append(ID_POSITION_LEFT_BOTH, wxT("Left with Both"));
665 evt.PopupMenu(&menu);
666 }
667
668 void MyFrame::AddText(wxString msg)
669 {
670 m_logwindow->AppendText(msg);
671 m_logwindow->AppendText(wxT("\n"));
672 m_ribbon->DismissExpandedPanel();
673 }
674
675 wxRibbonGalleryItem* MyFrame::AddColourToGallery(wxRibbonGallery *gallery,
676 wxString colour, wxMemoryDC& dc,
677 wxColour* value)
678 {
679 wxRibbonGalleryItem* item = NULL;
680 wxColour c;
681 if (colour != wxT("Default"))
682 c = wxColour(colour);
683 if (!c.IsOk())
684 c = *value;
685 if(c.IsOk())
686 {
687 const int iWidth = 64;
688 const int iHeight = 40;
689
690 wxBitmap bitmap(iWidth, iHeight);
691 dc.SelectObject(bitmap);
692 wxBrush b(c);
693 dc.SetPen(*wxBLACK_PEN);
694 dc.SetBrush(b);
695 dc.DrawRectangle(0, 0, iWidth, iHeight);
696
697 colour = colour.Mid(0, 1) + colour.Mid(1).Lower();
698 wxSize size = dc.GetTextExtent(colour);
699 wxColour foreground = wxColour(~c.Red(), ~c.Green(), ~c.Blue());
700 if(abs(foreground.Red() - c.Red()) + abs(foreground.Blue() - c.Blue())
701 + abs(foreground.Green() - c.Green()) < 64)
702 {
703 // Foreground too similar to background - use a different
704 // strategy to find a contrasting colour
705 foreground = wxColour((c.Red() + 64) % 256, 255 - c.Green(),
706 (c.Blue() + 192) % 256);
707 }
708 dc.SetTextForeground(foreground);
709 dc.DrawText(colour, (iWidth - size.GetWidth() + 1) / 2,
710 (iHeight - size.GetHeight()) / 2);
711 dc.SelectObjectAsSource(wxNullBitmap);
712
713 item = gallery->Append(bitmap, wxID_ANY);
714 gallery->SetItemClientObject(item, new ColourClientData(colour, c));
715 }
716 return item;
717 }
718
719 void MyFrame::OnColourGalleryButton(wxCommandEvent& evt)
720 {
721 wxRibbonGallery *gallery = wxDynamicCast(evt.GetEventObject(), wxRibbonGallery);
722 if(gallery == NULL)
723 return;
724
725 m_ribbon->DismissExpandedPanel();
726 if(gallery->GetSelection())
727 m_colour_data.SetColour(GetGalleryColour(gallery, gallery->GetSelection(), NULL));
728 wxColourDialog dlg(this, &m_colour_data);
729 if(dlg.ShowModal() == wxID_OK)
730 {
731 m_colour_data = dlg.GetColourData();
732 wxColour clr = m_colour_data.GetColour();
733
734 // Try to find colour in gallery
735 wxRibbonGalleryItem *item = NULL;
736 for(unsigned int i = 0; i < gallery->GetCount(); ++i)
737 {
738 item = gallery->GetItem(i);
739 if(GetGalleryColour(gallery, item, NULL) == clr)
740 break;
741 else
742 item = NULL;
743 }
744
745 // Colour not in gallery - add it
746 if(item == NULL)
747 {
748 item = AddColourToGallery(gallery,
749 clr.GetAsString(wxC2S_HTML_SYNTAX), m_bitmap_creation_dc,
750 &clr);
751 gallery->Realise();
752 }
753
754 // Set selection
755 gallery->EnsureVisible(item);
756 gallery->SetSelection(item);
757
758 // Send an event to respond to the selection change
759 wxRibbonGalleryEvent dummy(wxEVT_COMMAND_RIBBONGALLERY_SELECTED, gallery->GetId());
760 dummy.SetEventObject(gallery);
761 dummy.SetGallery(gallery);
762 dummy.SetGalleryItem(item);
763 ProcessWindowEvent(dummy);
764 }
765 }
766
767 void MyFrame::OnDefaultProvider(wxRibbonButtonBarEvent& WXUNUSED(evt))
768 {
769 m_ribbon->DismissExpandedPanel();
770 SetArtProvider(new wxRibbonDefaultArtProvider);
771 }
772
773 void MyFrame::OnAUIProvider(wxRibbonButtonBarEvent& WXUNUSED(evt))
774 {
775 m_ribbon->DismissExpandedPanel();
776 SetArtProvider(new wxRibbonAUIArtProvider);
777 }
778
779 void MyFrame::OnMSWProvider(wxRibbonButtonBarEvent& WXUNUSED(evt))
780 {
781 m_ribbon->DismissExpandedPanel();
782 SetArtProvider(new wxRibbonMSWArtProvider);
783 }
784
785 void MyFrame::SetArtProvider(wxRibbonArtProvider *prov)
786 {
787 m_ribbon->Freeze();
788 m_ribbon->SetArtProvider(prov);
789
790 prov->GetColourScheme(&m_default_primary, &m_default_secondary,
791 &m_default_tertiary);
792 PopulateColoursPanel(m_primary_gallery->GetParent(), m_default_primary,
793 ID_PRIMARY_COLOUR);
794 PopulateColoursPanel(m_secondary_gallery->GetParent(), m_default_secondary,
795 ID_SECONDARY_COLOUR);
796
797 m_ribbon->Realize();
798 m_ribbon->Thaw();
799 GetSizer()->Layout();
800 }