]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/ribbon/ribbondemo.cpp
Add a possibility to change the layout direction in the widgets sample.
[wxWidgets.git] / samples / ribbon / ribbondemo.cpp
index 95b7a9dd2b45d7e21b218906f6b37335c8241889..7a326c8598e6814ebbf32d6c33805d70ae305710 100644 (file)
@@ -24,6 +24,7 @@
 #include "wx/ribbon/toolbar.h"
 #include "wx/sizer.h"
 #include "wx/menu.h"
+#include "wx/msgdlg.h"
 #include "wx/dcbuffer.h"
 #include "wx/colordlg.h"
 #include "wx/artprov.h"
@@ -72,9 +73,31 @@ public:
         ID_POSITION_LEFT,
         ID_POSITION_LEFT_LABELS,
         ID_POSITION_LEFT_BOTH,
-        ID_TOGGLE_PANELS
+        ID_TOGGLE_PANELS,
+        ID_ENABLE,
+        ID_DISABLE,
+        ID_DISABLED,
+        ID_UI_ENABLE_UPDATED,
+        ID_CHECK,
+        ID_UI_CHECK_UPDATED,
+        ID_CHANGE_TEXT1,
+        ID_CHANGE_TEXT2,
+        ID_UI_CHANGE_TEXT_UPDATED,
+        ID_REMOVE_PAGE,
+        ID_HIDE_PAGES,
+        ID_SHOW_PAGES
     };
 
+    void OnEnableUpdateUI(wxUpdateUIEvent& evt);
+    void OnCheckUpdateUI(wxUpdateUIEvent& evt);
+    void OnChangeTextUpdateUI(wxUpdateUIEvent& evt);
+    void OnCheck(wxRibbonButtonBarEvent& evt);
+    void OnEnable(wxRibbonButtonBarEvent& evt);
+    void OnDisable(wxRibbonButtonBarEvent& evt);
+    void OnDisabled(wxRibbonButtonBarEvent& evt);
+    void OnEnableUpdated(wxRibbonButtonBarEvent& evt);
+    void OnChangeText1(wxRibbonButtonBarEvent& evt);
+    void OnChangeText2(wxRibbonButtonBarEvent& evt);
     void OnCircleButton(wxRibbonButtonBarEvent& evt);
     void OnCrossButton(wxRibbonButtonBarEvent& evt);
     void OnTriangleButton(wxRibbonButtonBarEvent& evt);
@@ -91,6 +114,8 @@ public:
     void OnDefaultProvider(wxRibbonButtonBarEvent& evt);
     void OnAUIProvider(wxRibbonButtonBarEvent& evt);
     void OnMSWProvider(wxRibbonButtonBarEvent& evt);
+    void OnJustify(wxRibbonToolBarEvent& evt);
+    void OnJustifyUpdateUI(wxUpdateUIEvent& evt);
     void OnNew(wxRibbonToolBarEvent& evt);
     void OnNewDropdown(wxRibbonToolBarEvent& evt);
     void OnPrint(wxRibbonToolBarEvent& evt);
@@ -107,8 +132,16 @@ public:
     void OnPositionLeftIcons(wxCommandEvent& evt);
     void OnPositionLeftBoth(wxCommandEvent& evt);
     void OnPositionLeftDropdown(wxRibbonToolBarEvent& evt);
-
+    void OnRemovePage(wxRibbonButtonBarEvent& evt);
+    void OnHidePages(wxRibbonButtonBarEvent& evt);
+    void OnShowPages(wxRibbonButtonBarEvent& evt);
     void OnTogglePanels(wxCommandEvent& evt);
+    void OnRibbonBarToggled(wxRibbonBarEvent& evt);
+    void OnRibbonBarHelpClicked(wxRibbonBarEvent& evt);
+
+    void OnSizeEvent(wxSizeEvent& evt);
+
+    void OnExtButton(wxRibbonPanelEvent& evt);
 
 protected:
     wxRibbonGallery* PopulateColoursPanel(wxWindow* panel, wxColour def,
@@ -133,6 +166,9 @@ protected:
     wxColour m_default_secondary;
     wxColour m_default_tertiary;
     wxMemoryDC m_bitmap_creation_dc;
+    bool m_bEnabled;
+    bool m_bChecked;
+    wxString m_new_text;
 
     DECLARE_EVENT_TABLE()
 };
@@ -151,6 +187,16 @@ bool MyApp::OnInit()
 }
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_ENABLE, MyFrame::OnEnable)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_DISABLE, MyFrame::OnDisable)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_DISABLED, MyFrame::OnDisabled)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_UI_ENABLE_UPDATED, MyFrame::OnEnableUpdated)
+EVT_UPDATE_UI(ID_UI_ENABLE_UPDATED, MyFrame::OnEnableUpdateUI)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_CHECK, MyFrame::OnCheck)
+EVT_UPDATE_UI(ID_UI_CHECK_UPDATED, MyFrame::OnCheckUpdateUI)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_CHANGE_TEXT1, MyFrame::OnChangeText1)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_CHANGE_TEXT2, MyFrame::OnChangeText2)
+EVT_UPDATE_UI(ID_UI_CHANGE_TEXT_UPDATED, MyFrame::OnChangeTextUpdateUI)
 EVT_RIBBONBUTTONBAR_CLICKED(ID_DEFAULT_PROVIDER, MyFrame::OnDefaultProvider)
 EVT_RIBBONBUTTONBAR_CLICKED(ID_AUI_PROVIDER, MyFrame::OnAUIProvider)
 EVT_RIBBONBUTTONBAR_CLICKED(ID_MSW_PROVIDER, MyFrame::OnMSWProvider)
@@ -167,6 +213,12 @@ EVT_RIBBONGALLERY_HOVER_CHANGED(ID_PRIMARY_COLOUR, MyFrame::OnHoveredColourChang
 EVT_RIBBONGALLERY_HOVER_CHANGED(ID_SECONDARY_COLOUR, MyFrame::OnHoveredColourChange)
 EVT_RIBBONGALLERY_SELECTED(ID_PRIMARY_COLOUR, MyFrame::OnPrimaryColourSelect)
 EVT_RIBBONGALLERY_SELECTED(ID_SECONDARY_COLOUR, MyFrame::OnSecondaryColourSelect)
+EVT_RIBBONTOOLBAR_CLICKED(wxID_JUSTIFY_LEFT, MyFrame::OnJustify)
+EVT_RIBBONTOOLBAR_CLICKED(wxID_JUSTIFY_CENTER, MyFrame::OnJustify)
+EVT_RIBBONTOOLBAR_CLICKED(wxID_JUSTIFY_RIGHT, MyFrame::OnJustify)
+EVT_UPDATE_UI(wxID_JUSTIFY_LEFT, MyFrame::OnJustifyUpdateUI)
+EVT_UPDATE_UI(wxID_JUSTIFY_CENTER, MyFrame::OnJustifyUpdateUI)
+EVT_UPDATE_UI(wxID_JUSTIFY_RIGHT, MyFrame::OnJustifyUpdateUI)
 EVT_RIBBONTOOLBAR_CLICKED(wxID_NEW, MyFrame::OnNew)
 EVT_RIBBONTOOLBAR_DROPDOWN_CLICKED(wxID_NEW, MyFrame::OnNewDropdown)
 EVT_RIBBONTOOLBAR_CLICKED(wxID_PRINT, MyFrame::OnPrint)
@@ -186,6 +238,13 @@ EVT_MENU(ID_POSITION_TOP, MyFrame::OnPositionTopLabels)
 EVT_MENU(ID_POSITION_TOP_ICONS, MyFrame::OnPositionTopIcons)
 EVT_MENU(ID_POSITION_TOP_BOTH, MyFrame::OnPositionTopBoth)
 EVT_TOGGLEBUTTON(ID_TOGGLE_PANELS, MyFrame::OnTogglePanels)
+EVT_RIBBONPANEL_EXTBUTTON_ACTIVATED(wxID_ANY, MyFrame::OnExtButton)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_REMOVE_PAGE, MyFrame::OnRemovePage)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_HIDE_PAGES, MyFrame::OnHidePages)
+EVT_RIBBONBUTTONBAR_CLICKED(ID_SHOW_PAGES, MyFrame::OnShowPages)
+EVT_RIBBONBAR_TOGGLED(wxID_ANY, MyFrame::OnRibbonBarToggled)
+EVT_RIBBONBAR_HELP_CLICK(wxID_ANY, MyFrame::OnRibbonBarHelpClicked)
+EVT_SIZE(MyFrame::OnSizeEvent)
 END_EVENT_TABLE()
 
 #include "align_center.xpm"
@@ -214,22 +273,31 @@ END_EVENT_TABLE()
 MyFrame::MyFrame()
     : wxFrame(NULL, wxID_ANY, wxT("wxRibbon Sample Application"), wxDefaultPosition, wxSize(800, 600), wxDEFAULT_FRAME_STYLE)
 {
-    m_ribbon = new wxRibbonBar(this, wxID_ANY);
+    m_ribbon = new wxRibbonBar(this,-1,wxDefaultPosition, wxDefaultSize, wxRIBBON_BAR_FLOW_HORIZONTAL
+                                | wxRIBBON_BAR_SHOW_PAGE_LABELS
+                                | wxRIBBON_BAR_SHOW_PANEL_EXT_BUTTONS
+                                | wxRIBBON_BAR_SHOW_TOGGLE_BUTTON
+                                | wxRIBBON_BAR_SHOW_HELP_BUTTON
+                                );
 
     {
         wxRibbonPage* home = new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Examples"), ribbon_xpm);
         wxRibbonPanel *toolbar_panel = new wxRibbonPanel(home, wxID_ANY, wxT("Toolbar"), 
                                             wxNullBitmap, wxDefaultPosition, wxDefaultSize, 
-                                            wxRIBBON_PANEL_NO_AUTO_MINIMISE);
+                                            wxRIBBON_PANEL_NO_AUTO_MINIMISE |
+                                            wxRIBBON_PANEL_EXT_BUTTON);
         wxRibbonToolBar *toolbar = new wxRibbonToolBar(toolbar_panel, ID_MAIN_TOOLBAR);
-        toolbar->AddTool(wxID_ANY, align_left_xpm);
-        toolbar->AddTool(wxID_ANY, align_center_xpm);
-        toolbar->AddTool(wxID_ANY, align_right_xpm);
+        toolbar->AddToggleTool(wxID_JUSTIFY_LEFT, align_left_xpm);
+        toolbar->AddToggleTool(wxID_JUSTIFY_CENTER , align_center_xpm);
+        toolbar->AddToggleTool(wxID_JUSTIFY_RIGHT, align_right_xpm);
         toolbar->AddSeparator();
         toolbar->AddHybridTool(wxID_NEW, wxArtProvider::GetBitmap(wxART_NEW, wxART_OTHER, wxSize(16, 15)));
-        toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_OTHER, wxSize(16, 15)));
-        toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_OTHER, wxSize(16, 15)));
-        toolbar->AddTool(wxID_ANY, wxArtProvider::GetBitmap(wxART_FILE_SAVE_AS, wxART_OTHER, wxSize(16, 15)));
+        toolbar->AddTool(wxID_OPEN, wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_OTHER, wxSize(16, 15)), "Open something");
+        toolbar->AddTool(wxID_SAVE, wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_OTHER, wxSize(16, 15)), "Save something");
+        toolbar->AddTool(wxID_SAVEAS, wxArtProvider::GetBitmap(wxART_FILE_SAVE_AS, wxART_OTHER, wxSize(16, 15)), "Save something as ...");
+        toolbar->EnableTool(wxID_OPEN, false);
+        toolbar->EnableTool(wxID_SAVE, false);
+        toolbar->EnableTool(wxID_SAVEAS, false);
         toolbar->AddSeparator();
         toolbar->AddDropdownTool(wxID_UNDO, wxArtProvider::GetBitmap(wxART_UNDO, wxART_OTHER, wxSize(16, 15)));
         toolbar->AddDropdownTool(wxID_REDO, wxArtProvider::GetBitmap(wxART_REDO, wxART_OTHER, wxSize(16, 15)));
@@ -315,8 +383,45 @@ MyFrame::MyFrame()
         m_secondary_gallery = PopulateColoursPanel(secondary_panel,
             m_default_secondary, ID_SECONDARY_COLOUR);
     }
+    {
+        wxRibbonPage* page = new wxRibbonPage(m_ribbon, wxID_ANY, wxT("UI Updated"), ribbon_xpm);
+        wxRibbonPanel *panel = new wxRibbonPanel(page, wxID_ANY, wxT("Enable/Disable"), ribbon_xpm);
+        wxRibbonButtonBar *bar = new wxRibbonButtonBar(panel, wxID_ANY);
+        bar->AddButton(ID_DISABLED, wxT("Disabled"), ribbon_xpm);
+        bar->AddButton(ID_ENABLE,   wxT("Enable"), ribbon_xpm);
+        bar->AddButton(ID_DISABLE,  wxT("Disable"), ribbon_xpm);
+        bar->AddButton(ID_UI_ENABLE_UPDATED, wxT("Enable UI updated"), ribbon_xpm);
+        bar->EnableButton(ID_DISABLED, false);
+        m_bEnabled = true;
+
+        panel = new wxRibbonPanel(page, wxID_ANY, wxT("Toggle"), ribbon_xpm);
+        bar = new wxRibbonButtonBar(panel, wxID_ANY);
+        bar->AddButton(ID_CHECK, wxT("Toggle"), ribbon_xpm);
+        bar->AddToggleButton(ID_UI_CHECK_UPDATED, wxT("Toggled UI updated"), ribbon_xpm);
+        m_bChecked = true;
+
+        panel = new wxRibbonPanel(page, wxID_ANY, wxT("Change text"), ribbon_xpm);
+        bar = new wxRibbonButtonBar(panel, wxID_ANY);
+        bar->AddButton(ID_CHANGE_TEXT1, wxT("One"), ribbon_xpm);
+        bar->AddButton(ID_CHANGE_TEXT2, wxT("Two"), ribbon_xpm);
+        bar->AddButton(ID_UI_CHANGE_TEXT_UPDATED, wxT("Zero"), ribbon_xpm);
+
+        //Also set the general disabled text colour:
+        wxRibbonArtProvider* artProvider = m_ribbon->GetArtProvider();
+        wxColour tColour = artProvider->GetColor(wxRIBBON_ART_BUTTON_BAR_LABEL_COLOUR);
+        artProvider->SetColor(wxRIBBON_ART_BUTTON_BAR_LABEL_DISABLED_COLOUR, tColour.MakeDisabled());
+    }
     new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Empty Page"), empty_xpm);
-    new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Another Page"), empty_xpm);
+    {
+        wxRibbonPage* page = new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Another Page"), empty_xpm);
+        wxRibbonPanel *panel = new wxRibbonPanel(page, wxID_ANY, wxT("Page manipulation"), ribbon_xpm);
+        wxRibbonButtonBar *bar = new wxRibbonButtonBar(panel, wxID_ANY);
+        bar->AddButton(ID_REMOVE_PAGE, wxT("Remove"), wxArtProvider::GetBitmap(wxART_DELETE, wxART_OTHER, wxSize(24, 24)));
+        bar->AddButton(ID_HIDE_PAGES, wxT("Hide Pages"), ribbon_xpm);
+        bar->AddButton(ID_SHOW_PAGES, wxT("Show Pages"), ribbon_xpm);
+    }
+    new wxRibbonPage(m_ribbon, wxID_ANY, wxT("Highlight Page"), empty_xpm);
+    m_ribbon->AddPageHighlight(m_ribbon->GetPageCount()-1);
 
     m_ribbon->Realize();
 
@@ -507,6 +612,60 @@ void MyFrame::ResetGalleryArtProviders()
     }
 }
 
+void MyFrame::OnChangeText1(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_new_text = wxT("One");
+}
+
+void MyFrame::OnChangeText2(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_new_text = wxT("Two");
+}
+
+void MyFrame::OnEnable(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_bEnabled = true;
+}
+
+void MyFrame::OnDisable(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_bEnabled = false;
+}
+
+void MyFrame::OnDisabled(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    AddText("ERROR: Disabled button activated (not supposed to happen)");
+}
+
+void MyFrame::OnEnableUpdated(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    AddText("Button activated");
+}
+
+void MyFrame::OnCheck(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_bChecked = !m_bChecked;
+}
+
+void MyFrame::OnEnableUpdateUI(wxUpdateUIEvent& evt)
+{
+    evt.Enable(m_bEnabled);
+}
+
+void MyFrame::OnCheckUpdateUI(wxUpdateUIEvent& evt)
+{
+    evt.Check(m_bChecked);
+}
+
+void MyFrame::OnChangeTextUpdateUI(wxUpdateUIEvent& evt)
+{
+    if ( !m_new_text.IsEmpty() )
+    {
+        evt.SetText(m_new_text);
+        m_new_text = wxT("");
+    }
+}
+
 void MyFrame::OnSelectionExpandHButton(wxRibbonButtonBarEvent& WXUNUSED(evt))
 {
     AddText(wxT("Expand selection horizontally button clicked."));
@@ -565,6 +724,40 @@ void MyFrame::OnPolygonDropdown(wxRibbonButtonBarEvent& evt)
     evt.PopupMenu(&menu);
 }
 
+void MyFrame::OnJustify(wxRibbonToolBarEvent& evt)
+{
+    long style = m_logwindow->GetWindowStyle() &
+        ~(wxTE_LEFT | wxTE_CENTER | wxTE_RIGHT);
+    switch(evt.GetId())
+    {
+        case wxID_JUSTIFY_LEFT:
+            m_logwindow->SetWindowStyle(style | wxTE_LEFT);
+            break;
+        case wxID_JUSTIFY_CENTER:
+            m_logwindow->SetWindowStyle(style | wxTE_CENTER);
+            break;
+        case wxID_JUSTIFY_RIGHT:
+            m_logwindow->SetWindowStyle(style | wxTE_RIGHT);
+            break;
+    }
+}
+
+void MyFrame::OnJustifyUpdateUI(wxUpdateUIEvent& evt)
+{
+    switch(evt.GetId())
+    {
+        case wxID_JUSTIFY_LEFT:
+            evt.Check(!m_logwindow->HasFlag(wxTE_CENTER | wxTE_RIGHT));
+            break;
+        case wxID_JUSTIFY_CENTER:
+            evt.Check(m_logwindow->HasFlag(wxTE_CENTER));
+            break;
+        case wxID_JUSTIFY_RIGHT:
+            evt.Check(m_logwindow->HasFlag(wxTE_RIGHT));
+            break;
+    }
+}
+
 void MyFrame::OnNew(wxRibbonToolBarEvent& WXUNUSED(evt))
 {
     AddText(wxT("New button clicked."));
@@ -681,6 +874,11 @@ void MyFrame::OnTogglePanels(wxCommandEvent& WXUNUSED(evt))
     m_ribbon->ShowPanels(m_togglePanels->GetValue());
 }
 
+void MyFrame::OnExtButton(wxRibbonPanelEvent& WXUNUSED(evt))
+{
+    wxMessageBox("Extension button clicked");
+}
+
 void MyFrame::AddText(wxString msg)
 {
     m_logwindow->AppendText(msg);
@@ -772,7 +970,7 @@ void MyFrame::OnColourGalleryButton(wxCommandEvent& evt)
         gallery->SetSelection(item);
 
         // Send an event to respond to the selection change
-        wxRibbonGalleryEvent dummy(wxEVT_COMMAND_RIBBONGALLERY_SELECTED, gallery->GetId());
+        wxRibbonGalleryEvent dummy(wxEVT_RIBBONGALLERY_SELECTED, gallery->GetId());
         dummy.SetEventObject(gallery);
         dummy.SetGallery(gallery);
         dummy.SetGalleryItem(item);
@@ -814,3 +1012,53 @@ void MyFrame::SetArtProvider(wxRibbonArtProvider *prov)
     m_ribbon->Thaw();
     GetSizer()->Layout();
 }
+
+void MyFrame::OnRemovePage(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    size_t n = m_ribbon->GetPageCount();
+    if(n > 0)
+    {
+        m_ribbon->DeletePage(n-1);
+        m_ribbon->Realize();
+    }
+}
+
+void MyFrame::OnHidePages(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_ribbon->HidePage(1);
+    m_ribbon->HidePage(2);
+    m_ribbon->HidePage(3);
+    m_ribbon->Realize();
+}
+
+void MyFrame::OnShowPages(wxRibbonButtonBarEvent& WXUNUSED(evt))
+{
+    m_ribbon->ShowPage(1);
+    m_ribbon->ShowPage(2);
+    m_ribbon->ShowPage(3);
+    m_ribbon->Realize();
+}
+
+void MyFrame::OnRibbonBarToggled(wxRibbonBarEvent& WXUNUSED(evt))
+{
+    AddText(wxString::Format("Ribbon bar %s.",
+                             m_ribbon->ArePanelsShown()
+                                ? "expanded"
+                                : "collapsed"));
+}
+
+void MyFrame::OnRibbonBarHelpClicked(wxRibbonBarEvent& WXUNUSED(evt))
+{
+    AddText("Ribbon bar help clicked");
+}
+
+// This shows how to hide ribbon dynamically if there is not enough space.
+void MyFrame::OnSizeEvent(wxSizeEvent& evt)
+{
+    if ( evt.GetSize().GetWidth() < 200 )
+        m_ribbon->Hide();
+    else
+        m_ribbon->Show();
+
+    evt.Skip();
+}