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
This commit is contained in:
Vadim Zeitlin
2015-01-02 14:26:53 +00:00
parent 55da8a0f86
commit 5662e86b61
17 changed files with 141 additions and 107 deletions

View File

@@ -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;

View File

@@ -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:

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -378,7 +378,7 @@ void ListboxWidgetsPage::Reset()
void ListboxWidgetsPage::CreateLbox()
{
int flags = ms_defaultFlags;
int flags = GetAttrs().m_defaultFlags;
switch ( m_radioSelMode->GetSelection() )
{
default:

View File

@@ -349,7 +349,7 @@ void BookWidgetsPage::RecreateBook()
if(!m_radioOrient)
return;
int flags = ms_defaultFlags;
int flags = GetAttrs().m_defaultFlags;
switch ( m_radioOrient->GetSelection() )
{

View File

@@ -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;

View File

@@ -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() )

View File

@@ -383,7 +383,7 @@ void SliderWidgetsPage::Reset()
void SliderWidgetsPage::CreateSlider()
{
int flags = ms_defaultFlags;
int flags = GetAttrs().m_defaultFlags;
if ( m_chkInverse->GetValue() )
{

View File

@@ -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 )

View File

@@ -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() )
{

View File

@@ -631,7 +631,7 @@ void TextWidgetsPage::Reset()
void TextWidgetsPage::CreateText()
{
int flags = ms_defaultFlags;
int flags = GetAttrs().m_defaultFlags;
switch ( m_radioTextLines->GetSelection() )
{
default:

View File

@@ -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() )
{

View File

@@ -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
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)

View File

@@ -89,6 +89,36 @@ typedef wxVector<wxControl *> 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