From 5662e86b61a013596d5b904e112ef7bb9934c27b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 2 Jan 2015 14:26:53 +0000 Subject: [PATCH] Improve widgets sample UI consistency. Ensure that the various attributes (colours, font, border, ...) are preserved when a widget is recreated or the current page is changed. This is more convenient and also avoids discrepancies between the state of the menu items and the actual state of the widget. Closes #16576. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78332 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/widgets/bmpcombobox.cpp | 2 +- samples/widgets/button.cpp | 2 +- samples/widgets/checkbox.cpp | 2 +- samples/widgets/choice.cpp | 2 +- samples/widgets/combobox.cpp | 2 +- samples/widgets/gauge.cpp | 2 +- samples/widgets/listbox.cpp | 2 +- samples/widgets/notebook.cpp | 2 +- samples/widgets/odcombobox.cpp | 2 +- samples/widgets/radiobox.cpp | 2 +- samples/widgets/slider.cpp | 2 +- samples/widgets/spinbtn.cpp | 2 +- samples/widgets/static.cpp | 4 +- samples/widgets/textctrl.cpp | 2 +- samples/widgets/toggle.cpp | 2 +- samples/widgets/widgets.cpp | 179 ++++++++++++++++---------------- samples/widgets/widgets.h | 37 ++++++- 17 files changed, 141 insertions(+), 107 deletions(-) diff --git a/samples/widgets/bmpcombobox.cpp b/samples/widgets/bmpcombobox.cpp index 66e1e838be..fb697c9710 100644 --- a/samples/widgets/bmpcombobox.cpp +++ b/samples/widgets/bmpcombobox.cpp @@ -422,7 +422,7 @@ void BitmapComboBoxWidgetsPage::Reset() void BitmapComboBoxWidgetsPage::CreateCombo() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkSort->GetValue() ) flags |= wxCB_SORT; diff --git a/samples/widgets/button.cpp b/samples/widgets/button.cpp index ddd3268db4..a586ffaa94 100644 --- a/samples/widgets/button.cpp +++ b/samples/widgets/button.cpp @@ -408,7 +408,7 @@ void ButtonWidgetsPage::CreateButton() label = m_textLabel->GetValue(); } - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; switch ( m_radioHAlign->GetSelection() ) { case ButtonHAlign_Left: diff --git a/samples/widgets/checkbox.cpp b/samples/widgets/checkbox.cpp index aee900adf3..f7f42dcc61 100644 --- a/samples/widgets/checkbox.cpp +++ b/samples/widgets/checkbox.cpp @@ -252,7 +252,7 @@ void CheckBoxWidgetsPage::CreateCheckbox() delete m_checkbox; - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkRight->IsChecked() ) flags |= wxALIGN_RIGHT; diff --git a/samples/widgets/choice.cpp b/samples/widgets/choice.cpp index ffb4889280..94f9f1aad2 100644 --- a/samples/widgets/choice.cpp +++ b/samples/widgets/choice.cpp @@ -297,7 +297,7 @@ void ChoiceWidgetsPage::Reset() void ChoiceWidgetsPage::CreateChoice() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkSort->GetValue() ) flags |= wxCB_SORT; diff --git a/samples/widgets/combobox.cpp b/samples/widgets/combobox.cpp index c039c90d1e..702bd257fa 100644 --- a/samples/widgets/combobox.cpp +++ b/samples/widgets/combobox.cpp @@ -407,7 +407,7 @@ void ComboboxWidgetsPage::Reset() void ComboboxWidgetsPage::CreateCombo() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkSort->GetValue() ) flags |= wxCB_SORT; diff --git a/samples/widgets/gauge.cpp b/samples/widgets/gauge.cpp index 815d4ea0cd..b5f3756735 100644 --- a/samples/widgets/gauge.cpp +++ b/samples/widgets/gauge.cpp @@ -283,7 +283,7 @@ void GaugeWidgetsPage::Reset() void GaugeWidgetsPage::CreateGauge() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkVert->GetValue() ) flags |= wxGA_VERTICAL; diff --git a/samples/widgets/listbox.cpp b/samples/widgets/listbox.cpp index f7c33f11cb..9acf9ae092 100644 --- a/samples/widgets/listbox.cpp +++ b/samples/widgets/listbox.cpp @@ -378,7 +378,7 @@ void ListboxWidgetsPage::Reset() void ListboxWidgetsPage::CreateLbox() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; switch ( m_radioSelMode->GetSelection() ) { default: diff --git a/samples/widgets/notebook.cpp b/samples/widgets/notebook.cpp index 7b5347d875..9699e06c30 100644 --- a/samples/widgets/notebook.cpp +++ b/samples/widgets/notebook.cpp @@ -349,7 +349,7 @@ void BookWidgetsPage::RecreateBook() if(!m_radioOrient) return; - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; switch ( m_radioOrient->GetSelection() ) { diff --git a/samples/widgets/odcombobox.cpp b/samples/widgets/odcombobox.cpp index 66c9b9c4ab..a1ba063ad3 100644 --- a/samples/widgets/odcombobox.cpp +++ b/samples/widgets/odcombobox.cpp @@ -495,7 +495,7 @@ void ODComboboxWidgetsPage::Reset() void ODComboboxWidgetsPage::CreateCombo() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkSort->GetValue() ) flags |= wxCB_SORT; diff --git a/samples/widgets/radiobox.cpp b/samples/widgets/radiobox.cpp index 0d9858e642..df494e64dc 100644 --- a/samples/widgets/radiobox.cpp +++ b/samples/widgets/radiobox.cpp @@ -370,7 +370,7 @@ void RadioWidgetsPage::CreateRadio() int flags = m_chkSpecifyRows->GetValue() ? wxRA_SPECIFY_ROWS : wxRA_SPECIFY_COLS; - flags |= ms_defaultFlags; + flags |= GetAttrs().m_defaultFlags; #ifdef wxRA_LEFTTORIGHT switch ( m_radioDir->GetSelection() ) diff --git a/samples/widgets/slider.cpp b/samples/widgets/slider.cpp index 975dab5815..3af4d6fc9a 100644 --- a/samples/widgets/slider.cpp +++ b/samples/widgets/slider.cpp @@ -383,7 +383,7 @@ void SliderWidgetsPage::Reset() void SliderWidgetsPage::CreateSlider() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; if ( m_chkInverse->GetValue() ) { diff --git a/samples/widgets/spinbtn.cpp b/samples/widgets/spinbtn.cpp index 1def9fa4f8..a685b1c246 100644 --- a/samples/widgets/spinbtn.cpp +++ b/samples/widgets/spinbtn.cpp @@ -347,7 +347,7 @@ void SpinBtnWidgetsPage::Reset() void SpinBtnWidgetsPage::CreateSpin() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; bool isVert = m_chkVert->GetValue(); if ( isVert ) diff --git a/samples/widgets/static.cpp b/samples/widgets/static.cpp index 823c31b486..e66dfc0b5b 100644 --- a/samples/widgets/static.cpp +++ b/samples/widgets/static.cpp @@ -395,8 +395,8 @@ void StaticWidgetsPage::CreateStatic() } int flagsBox = 0, - flagsText = ms_defaultFlags, - flagsDummyText = ms_defaultFlags; + flagsText = GetAttrs().m_defaultFlags, + flagsDummyText = GetAttrs().m_defaultFlags; if ( !m_chkAutoResize->GetValue() ) { diff --git a/samples/widgets/textctrl.cpp b/samples/widgets/textctrl.cpp index e26fcbf836..741a2f5c1f 100644 --- a/samples/widgets/textctrl.cpp +++ b/samples/widgets/textctrl.cpp @@ -631,7 +631,7 @@ void TextWidgetsPage::Reset() void TextWidgetsPage::CreateText() { - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; switch ( m_radioTextLines->GetSelection() ) { default: diff --git a/samples/widgets/toggle.cpp b/samples/widgets/toggle.cpp index 0a5a78b3b4..e358098c3f 100644 --- a/samples/widgets/toggle.cpp +++ b/samples/widgets/toggle.cpp @@ -370,7 +370,7 @@ void ToggleWidgetsPage::CreateToggle() label = m_textLabel->GetValue(); } - int flags = ms_defaultFlags; + int flags = GetAttrs().m_defaultFlags; #ifdef wxHAS_BITMAPTOGGLEBUTTON switch ( m_radioHAlign->GetSelection() ) { diff --git a/samples/widgets/widgets.cpp b/samples/widgets/widgets.cpp index bd918ed23c..dddce470ff 100644 --- a/samples/widgets/widgets.cpp +++ b/samples/widgets/widgets.cpp @@ -210,13 +210,6 @@ private: // the book containing the test pages WidgetsBookCtrl *m_book; -#if wxUSE_MENUS - // last chosen fg/bg colours and font - wxColour m_colFg, - m_colBg; - wxFont m_font; -#endif // wxUSE_MENUS - // any class wishing to process wxWidgets events must use this macro wxDECLARE_EVENT_TABLE(); }; @@ -736,6 +729,8 @@ void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event) } page->SetSize(size); } + // re-apply the attributes to the widget(s) + page->SetUpWidget(); event.Skip(); } @@ -755,31 +750,21 @@ void WidgetsFrame::OnGoToPage(wxCommandEvent& event) void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event)) { - static wxString s_tip = wxT("This is a tooltip"); - wxTextEntryDialog dialog ( this, wxT("Tooltip text (may use \\n, leave empty to remove): "), wxT("Widgets sample"), - s_tip + WidgetsPage::GetAttrs().m_tooltip ); if ( dialog.ShowModal() != wxID_OK ) return; - s_tip = dialog.GetValue(); - s_tip.Replace(wxT("\\n"), wxT("\n")); + WidgetsPage::GetAttrs().m_tooltip = dialog.GetValue(); + WidgetsPage::GetAttrs().m_tooltip.Replace(wxT("\\n"), wxT("\n")); - WidgetsPage *page = CurrentPage(); - - const Widgets widgets = page->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetToolTip(s_tip); - } + CurrentPage()->SetUpWidget(); } #endif // wxUSE_TOOLTIPS @@ -809,46 +794,32 @@ void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event)) // allow for debugging the default colour the first time this is called WidgetsPage *page = CurrentPage(); - if (!m_colFg.IsOk()) - m_colFg = page->GetForegroundColour(); + if (!WidgetsPage::GetAttrs().m_colFg.IsOk()) + WidgetsPage::GetAttrs().m_colFg = page->GetForegroundColour(); - wxColour col = GetColourFromUser(this, m_colFg); + wxColour col = GetColourFromUser(this, WidgetsPage::GetAttrs().m_colFg); if ( !col.IsOk() ) return; - m_colFg = col; + WidgetsPage::GetAttrs().m_colFg = col; - const Widgets widgets = page->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetForegroundColour(m_colFg); - (*it)->Refresh(); - } + page->SetUpWidget(); } void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event)) { WidgetsPage *page = CurrentPage(); - if ( !m_colBg.IsOk() ) - m_colBg = page->GetBackgroundColour(); + if ( !WidgetsPage::GetAttrs().m_colBg.IsOk() ) + WidgetsPage::GetAttrs().m_colBg = page->GetBackgroundColour(); - wxColour col = GetColourFromUser(this, m_colBg); + wxColour col = GetColourFromUser(this, WidgetsPage::GetAttrs().m_colBg); if ( !col.IsOk() ) return; - m_colBg = col; + WidgetsPage::GetAttrs().m_colBg = col; - const Widgets widgets = page->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetBackgroundColour(m_colBg); - (*it)->Refresh(); - } + page->SetUpWidget(); } void WidgetsFrame::OnSetPageBg(wxCommandEvent& WXUNUSED(event)) @@ -857,8 +828,9 @@ void WidgetsFrame::OnSetPageBg(wxCommandEvent& WXUNUSED(event)) if ( !col.IsOk() ) return; - CurrentPage()->SetBackgroundColour(col); - CurrentPage()->Refresh(); + WidgetsPage::GetAttrs().m_colPageBg = col; + + CurrentPage()->SetUpWidget(); } void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event)) @@ -866,24 +838,16 @@ void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event)) #if wxUSE_FONTDLG WidgetsPage *page = CurrentPage(); - if (!m_font.IsOk()) - m_font = page->GetFont(); + if (!WidgetsPage::GetAttrs().m_font.IsOk()) + WidgetsPage::GetAttrs().m_font = page->GetFont(); - wxFont font = wxGetFontFromUser(this, m_font); + wxFont font = wxGetFontFromUser(this, WidgetsPage::GetAttrs().m_font); if ( !font.IsOk() ) return; - m_font = font; - - const Widgets widgets = page->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetFont(m_font); - (*it)->Refresh(); - } + WidgetsPage::GetAttrs().m_font = font; + page->SetUpWidget(); // The best size of the widget could have changed after changing its font, // so re-layout to show it correctly. page->Layout(); @@ -894,13 +858,9 @@ void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event)) void WidgetsFrame::OnEnable(wxCommandEvent& event) { - const Widgets widgets = CurrentPage()->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->Enable(event.IsChecked()); - } + WidgetsPage::GetAttrs().m_enabled = event.IsChecked(); + + CurrentPage()->SetUpWidget(); } void WidgetsFrame::OnSetBorder(wxCommandEvent& event) @@ -922,27 +882,22 @@ void WidgetsFrame::OnSetBorder(wxCommandEvent& event) case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break; } - WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK; - WidgetsPage::ms_defaultFlags |= border; + WidgetsPage::GetAttrs().m_defaultFlags &= ~wxBORDER_MASK; + WidgetsPage::GetAttrs().m_defaultFlags |= border; WidgetsPage *page = CurrentPage(); page->RecreateWidget(); + // re-apply the attributes to the widget(s) + page->SetUpWidget(); } void WidgetsFrame::OnToggleLayoutDirection(wxCommandEvent& event) { - wxLayoutDirection dir = event.IsChecked() ? wxLayout_RightToLeft - : wxLayout_LeftToRight; + WidgetsPage::GetAttrs().m_dir = event.IsChecked() ? wxLayout_RightToLeft + : wxLayout_LeftToRight; - const Widgets widgets = CurrentPage()->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetLayoutDirection(dir); - (*it)->Refresh(); - } + CurrentPage()->SetUpWidget(); } void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event) @@ -955,16 +910,10 @@ void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event) void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent& event) { - wxCursor cursor(*(event.IsChecked() ? wxHOURGLASS_CURSOR - : wxSTANDARD_CURSOR)); + WidgetsPage::GetAttrs().m_cursor = *(event.IsChecked() ? wxHOURGLASS_CURSOR + : wxSTANDARD_CURSOR); - const Widgets widgets = CurrentPage()->GetWidgets(); - for ( Widgets::const_iterator it = widgets.begin(); - it != widgets.end(); - ++it ) - { - (*it)->SetCursor(cursor); - } + CurrentPage()->SetUpWidget(); } void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent& WXUNUSED(event)) @@ -1230,7 +1179,6 @@ WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int cate // WidgetsPage // ---------------------------------------------------------------------------- -int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT; WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL; WidgetsPage::WidgetsPage(WidgetsBookCtrl *book, @@ -1250,6 +1198,59 @@ WidgetsPage::WidgetsPage(WidgetsBookCtrl *book, #endif } +/* static */ +WidgetAttributes& WidgetsPage::GetAttrs() +{ + static WidgetAttributes s_attrs; + + return s_attrs; +} + +void WidgetsPage::SetUpWidget() +{ + const Widgets widgets = GetWidgets(); + + for ( Widgets::const_iterator it = widgets.begin(); + it != widgets.end(); + ++it ) + { +#if wxUSE_TOOLTIPS + (*it)->SetToolTip(GetAttrs().m_tooltip); +#endif // wxUSE_TOOLTIPS +#if wxUSE_FONTDLG + if ( GetAttrs().m_font.IsOk() ) + { + (*it)->SetFont(GetAttrs().m_font); + } +#endif // wxUSE_FONTDLG + if ( GetAttrs().m_colFg.IsOk() ) + { + (*it)->SetForegroundColour(GetAttrs().m_colFg); + } + + if ( GetAttrs().m_colBg.IsOk() ) + { + (*it)->SetBackgroundColour(GetAttrs().m_colBg); + } + + (*it)->SetLayoutDirection(GetAttrs().m_dir); + (*it)->Enable(GetAttrs().m_enabled); + + if ( GetAttrs().m_cursor.IsOk() ) + { + (*it)->SetCursor(GetAttrs().m_cursor); + } + + (*it)->Refresh(); + } + + if ( GetAttrs().m_colPageBg.IsOk() ) + { + SetBackgroundColour(GetAttrs().m_colPageBg); + Refresh(); + } +} + wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control, wxWindowID id, wxTextCtrl **ppText) diff --git a/samples/widgets/widgets.h b/samples/widgets/widgets.h index f0e4deb07d..db1d800b09 100644 --- a/samples/widgets/widgets.h +++ b/samples/widgets/widgets.h @@ -89,6 +89,36 @@ typedef wxVector Widgets; // WidgetsPage: a book page demonstrating some widget // ---------------------------------------------------------------------------- +// struct to store common widget attributes +struct WidgetAttributes +{ + WidgetAttributes() + { +#if wxUSE_TOOLTIPS + m_tooltip = "This is a tooltip"; +#endif // wxUSE_TOOLTIPS + m_enabled = true; + m_dir = wxLayout_LeftToRight; + m_cursor = *wxSTANDARD_CURSOR; + m_defaultFlags = wxBORDER_DEFAULT; + } + +#if wxUSE_TOOLTIPS + wxString m_tooltip; +#endif // wxUSE_TOOLTIPS +#if wxUSE_FONTDLG + wxFont m_font; +#endif // wxUSE_FONTDLG + wxColour m_colFg; + wxColour m_colBg; + wxColour m_colPageBg; + bool m_enabled; + wxLayoutDirection m_dir; + wxCursor m_cursor; + // the default flags, currently only contains border flags + int m_defaultFlags; +}; + class WidgetsPage : public wxPanel { public: @@ -119,8 +149,11 @@ public: // this is currently used only to take into account the border flags virtual void RecreateWidget() = 0; - // the default flags for the widget, currently only contains border flags - static int ms_defaultFlags; + // apply current atrributes to the widget(s) + void SetUpWidget(); + + // the default attributes for the widget + static WidgetAttributes& GetAttrs(); protected: // several helper functions for page creation