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