From b63cf77dd30150c34e6bbb59e305a0199dfd5667 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2020 13:04:57 +0100 Subject: [PATCH 1/5] Fix persistent classes build without implicit wxString encoding Ensure that all headers in wx/persist can be compiled with wxNO_IMPLICIT_WXSTRING_ENCODING defined. --- include/wx/persist/bookctrl.h | 4 ++-- include/wx/persist/splitter.h | 4 ++-- include/wx/persist/treebook.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/wx/persist/bookctrl.h b/include/wx/persist/bookctrl.h index 6b48ebe0b4..cf3397e3ed 100644 --- a/include/wx/persist/bookctrl.h +++ b/include/wx/persist/bookctrl.h @@ -18,9 +18,9 @@ // string constants used by wxPersistentBookCtrl // ---------------------------------------------------------------------------- -#define wxPERSIST_BOOK_KIND "Book" +#define wxPERSIST_BOOK_KIND wxASCII_STR("Book") -#define wxPERSIST_BOOK_SELECTION "Selection" +#define wxPERSIST_BOOK_SELECTION wxASCII_STR("Selection") // ---------------------------------------------------------------------------- // wxPersistentBookCtrl: supports saving/restoring book control selection diff --git a/include/wx/persist/splitter.h b/include/wx/persist/splitter.h index 46979d673f..7c9e6584f4 100644 --- a/include/wx/persist/splitter.h +++ b/include/wx/persist/splitter.h @@ -18,10 +18,10 @@ // string constants used by wxPersistentSplitter // ---------------------------------------------------------------------------- -#define wxPERSIST_SPLITTER_KIND "Splitter" +#define wxPERSIST_SPLITTER_KIND wxASCII_STR("Splitter") // Special position value of -1 means the splitter is not split at all. -#define wxPERSIST_SPLITTER_POSITION "Position" +#define wxPERSIST_SPLITTER_POSITION wxASCII_STR("Position") // ---------------------------------------------------------------------------- // wxPersistentSplitter: supports saving/restoring splitter position diff --git a/include/wx/persist/treebook.h b/include/wx/persist/treebook.h index f3026642f4..379447fdab 100644 --- a/include/wx/persist/treebook.h +++ b/include/wx/persist/treebook.h @@ -19,11 +19,11 @@ // string constants used by wxPersistentTreeBookCtrl // ---------------------------------------------------------------------------- -#define wxPERSIST_TREEBOOK_KIND "TreeBook" +#define wxPERSIST_TREEBOOK_KIND wxASCII_STR("TreeBook") // this key contains the indices of all expanded nodes in the tree book // separated by wxPERSIST_TREEBOOK_EXPANDED_SEP -#define wxPERSIST_TREEBOOK_EXPANDED_BRANCHES "Expanded" +#define wxPERSIST_TREEBOOK_EXPANDED_BRANCHES wxASCII_STR("Expanded") #define wxPERSIST_TREEBOOK_EXPANDED_SEP ',' // ---------------------------------------------------------------------------- From 846964295968ab22bcfbd66e4d530cb1c1ba14c3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2020 03:51:05 +0100 Subject: [PATCH 2/5] Include persistent controls headers in the all headers test Check for the absence of warnings when compiling these headers too, they were simply forgotten (and not intentionally excluded) before. --- tests/allheaders.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/allheaders.h b/tests/allheaders.h index ad8280067e..545724635b 100644 --- a/tests/allheaders.h +++ b/tests/allheaders.h @@ -254,6 +254,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include #include #include From 7f5d45b9b4ef33ec55833c2bb464409cbcb01191 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2020 03:47:37 +0100 Subject: [PATCH 3/5] Add wxPersistentComboBox for saving and restoring combobox items This allows to preserve the history of user input. --- include/wx/persist/combobox.h | 99 +++++++++++++++++++++++++++++++++ interface/wx/persist/combobox.h | 75 +++++++++++++++++++++++++ tests/allheaders.h | 1 + 3 files changed, 175 insertions(+) create mode 100644 include/wx/persist/combobox.h create mode 100644 interface/wx/persist/combobox.h diff --git a/include/wx/persist/combobox.h b/include/wx/persist/combobox.h new file mode 100644 index 0000000000..52efe25474 --- /dev/null +++ b/include/wx/persist/combobox.h @@ -0,0 +1,99 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/persist/combobox.h +// Purpose: Persistence adapter for wxComboBox +// Author: Vadim Zeitlin +// Created: 2020-11-19 +// Copyright: (c) 2020 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PERSIST_COMBOBOX_H_ +#define _WX_PERSIST_COMBOBOX_H_ + +#include "wx/persist/window.h" + +#include "wx/combobox.h" + +#define wxPERSIST_COMBOBOX_KIND wxASCII_STR("Combobox") + +// Stores semicolon-separated list of combobox entries (real semicolons are +// escaped using backslash), i.e. "first;second;...". +#define wxPERSIST_COMBOBOX_ITEMS wxASCII_STR("Items") + +#define wxPERSIST_COMBOBOX_ITEMS_SEP ';' + +// ---------------------------------------------------------------------------- +// wxPersistentComboBox: supports saving/restoring combobox items +// ---------------------------------------------------------------------------- + +class wxPersistentComboBox : public wxPersistentWindow +{ +public: + // The maximum number of items to save is currently hard-coded. + // + // Notice that we must have some limit, as otherwise the length of the + // items string in the config would be unbounded, which certainly wouldn't + // be a good idea. + enum { MaxSavedItemsCount = 10 }; + + explicit wxPersistentComboBox(wxComboBox* combobox) + : wxPersistentWindow(combobox) + { + } + + virtual void Save() const wxOVERRIDE + { + const wxComboBox* const combobox = Get(); + + wxArrayString items = combobox->GetStrings(); + + const wxString value = combobox->GetValue(); + if ( !value.empty() ) + { + wxArrayString::iterator it; + for ( it = items.begin(); it != items.end(); ++it ) + { + if ( *it == value ) + { + // There is no need to add the text of an item already + // present in the combobox again, but do move it to the top + // of it to indicate that it was the last one used. + wxSwap(*items.begin(), *it); + break; + } + } + + if ( it == items.end() ) + { + // This is a genuinely new item, so just insert it front. + items.insert(items.begin(), value); + + if ( items.size() > MaxSavedItemsCount ) + items.erase(items.begin() + MaxSavedItemsCount, items.end()); + } + } + + SaveValue(wxPERSIST_COMBOBOX_ITEMS, + wxJoin(items, wxPERSIST_COMBOBOX_ITEMS_SEP)); + } + + virtual bool Restore() wxOVERRIDE + { + wxString items; + if ( !RestoreValue(wxPERSIST_COMBOBOX_ITEMS, &items) ) + return false; + + Get()->Set(wxSplit(items, wxPERSIST_COMBOBOX_ITEMS_SEP)); + + return true; + } + + virtual wxString GetKind() const wxOVERRIDE { return wxPERSIST_COMBOBOX_KIND; } +}; + +inline wxPersistentObject *wxCreatePersistentObject(wxComboBox* combobox) +{ + return new wxPersistentComboBox(combobox); +} + +#endif // _WX_PERSIST_COMBOBOX_H_ diff --git a/interface/wx/persist/combobox.h b/interface/wx/persist/combobox.h new file mode 100644 index 0000000000..5b7e2d81b6 --- /dev/null +++ b/interface/wx/persist/combobox.h @@ -0,0 +1,75 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/persist/combobox.h +// Purpose: Interface of wxPersistentComboBox +// Author: Vadim Zeitlin +// Created: 2020-11-19 +// Copyright: (c) 2020 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +/** + Persistence adapter for wxComboBox. + + This adapter saves and restores the items of wxComboBox. A persistent + combobox can be used to preserve history of user entries. + + Example of using it: + @code + // Suppose you need to ask the user to select their favourite Linux + // distribution, for some reason: + wxComboBox* combo = new wxComboBox(this, wxID_ANY); + if ( !wxPersistentRegisterAndRestore(combo, "distribution") ) + { + // Seed it with some default contents. + combo->Append("Debian"); + combo->Append("Fedora"); + combo->Append("Ubuntu"); + } + + // Optionally, you might want to restore the last used entry: + combo->SetSelection(0); + + @endcode + */ +class wxPersistentComboBox : public wxPersistentWindow +{ +public: + /** + Constructor. + + @param combobox + The associated combobox. + */ + explicit wxPersistentComboBox(wxComboBox *combobox); + + /** + Save the current items and value. + + The current control value is saved as the first item, so that calling + @c SetSelection(0) when the control is created the next time will + restore the value which was last used. If the current value is the same + as one of the existing items, this item is moved to the front of the + list, instead of being added again. + + If the current value is empty, it is not saved at all. + + At most 10 items are saved, if the combobox has more than 10 items, or + exactly 10 items and the current value is different from all of them, + the items beyond the tenth one are discarded. + */ + virtual void Save() const; + + /** + Restore the combobox items. + + This function doesn't change the current combobox value, you need to + call @c SetSelection(0) explicitly, after verifying that the combobox + is not empty using its IsListEmpty() method, if you want to restore the + last used value automatically. Otherwise the user can always do it by + opening the combobox and selecting it manually. + */ + virtual bool Restore(); +}; + +/// Overload allowing persistence adapter creation for wxComboBox objects. +wxPersistentObject *wxCreatePersistentObject(wxComboBox *combobox); diff --git a/tests/allheaders.h b/tests/allheaders.h index 545724635b..1425eae2b6 100644 --- a/tests/allheaders.h +++ b/tests/allheaders.h @@ -255,6 +255,7 @@ #include #include #include +#include #include #include #include From 152ec5103367d6d5e2c77ab180e9e432b58deac7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2020 16:34:17 +0100 Subject: [PATCH 4/5] Add missing wxUSE_XXX checks to persistent adapters headers Make sure these headers can be compiled even when the control they're written for is not available in the build. Including them in this case doesn't make much sense, of course, but not giving any errors is still nicer and consistent with the rest of wx headers. --- include/wx/persist/bookctrl.h | 4 ++++ include/wx/persist/combobox.h | 4 ++++ include/wx/persist/dataview.h | 4 ++++ include/wx/persist/treebook.h | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/include/wx/persist/bookctrl.h b/include/wx/persist/bookctrl.h index cf3397e3ed..f6370cc973 100644 --- a/include/wx/persist/bookctrl.h +++ b/include/wx/persist/bookctrl.h @@ -12,6 +12,8 @@ #include "wx/persist/window.h" +#if wxUSE_BOOKCTRL + #include "wx/bookctrl.h" // ---------------------------------------------------------------------------- @@ -63,4 +65,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxBookCtrlBase *book) return new wxPersistentBookCtrl(book); } +#endif // wxUSE_BOOKCTRL + #endif // _WX_PERSIST_BOOKCTRL_H_ diff --git a/include/wx/persist/combobox.h b/include/wx/persist/combobox.h index 52efe25474..1f69d1fa03 100644 --- a/include/wx/persist/combobox.h +++ b/include/wx/persist/combobox.h @@ -12,6 +12,8 @@ #include "wx/persist/window.h" +#if wxUSE_COMBOBOX + #include "wx/combobox.h" #define wxPERSIST_COMBOBOX_KIND wxASCII_STR("Combobox") @@ -96,4 +98,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxComboBox* combobox) return new wxPersistentComboBox(combobox); } +#endif // wxUSE_COMBOBOX + #endif // _WX_PERSIST_COMBOBOX_H_ diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index e673eedac6..fe6dac1490 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -12,6 +12,8 @@ #include "wx/persist/window.h" +#if wxUSE_DATAVIEWCTRL + #include "wx/dataview.h" // ---------------------------------------------------------------------------- @@ -168,4 +170,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control) return new wxPersistentDataViewCtrl(control); } +#endif // wxUSE_DATAVIEWCTRL + #endif // _WX_PERSIST_DATAVIEW_H_ diff --git a/include/wx/persist/treebook.h b/include/wx/persist/treebook.h index 379447fdab..c7b4cf002b 100644 --- a/include/wx/persist/treebook.h +++ b/include/wx/persist/treebook.h @@ -12,6 +12,8 @@ #include "wx/persist/bookctrl.h" +#if wxUSE_TREEBOOK + #include "wx/arrstr.h" #include "wx/treebook.h" @@ -93,4 +95,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxTreebook *book) return new wxPersistentTreeBookCtrl(book); } +#endif // wxUSE_TREEBOOK + #endif // _WX_PERSIST_TREEBOOK_H_ From 4b375971b14ca80aa5c7b413fcf57d897932a152 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2020 23:30:13 +0100 Subject: [PATCH 5/5] Add version information to wxPersistentComboBox documentation Just add the previously forgotten "@since" tag. --- interface/wx/persist/combobox.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interface/wx/persist/combobox.h b/interface/wx/persist/combobox.h index 5b7e2d81b6..34bab51187 100644 --- a/interface/wx/persist/combobox.h +++ b/interface/wx/persist/combobox.h @@ -30,6 +30,8 @@ combo->SetSelection(0); @endcode + + @since 3.1.5 */ class wxPersistentComboBox : public wxPersistentWindow {