Merge branch 'persistent-combobox'

Add wxPersistentComboBox class and fix persistent classes compilation
when wxString implicit conversions are disabled.

See https://github.com/wxWidgets/wxWidgets/pull/2123
This commit is contained in:
Vadim Zeitlin
2020-11-19 15:45:58 +01:00
7 changed files with 205 additions and 6 deletions

View File

@@ -12,15 +12,17 @@
#include "wx/persist/window.h"
#if wxUSE_BOOKCTRL
#include "wx/bookctrl.h"
// ----------------------------------------------------------------------------
// 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
@@ -63,4 +65,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxBookCtrlBase *book)
return new wxPersistentBookCtrl(book);
}
#endif // wxUSE_BOOKCTRL
#endif // _WX_PERSIST_BOOKCTRL_H_

View File

@@ -0,0 +1,103 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/persist/combobox.h
// Purpose: Persistence adapter for wxComboBox
// Author: Vadim Zeitlin
// Created: 2020-11-19
// Copyright: (c) 2020 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PERSIST_COMBOBOX_H_
#define _WX_PERSIST_COMBOBOX_H_
#include "wx/persist/window.h"
#if wxUSE_COMBOBOX
#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<wxComboBox>
{
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<wxComboBox>(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 // wxUSE_COMBOBOX
#endif // _WX_PERSIST_COMBOBOX_H_

View File

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

View File

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

View File

@@ -12,6 +12,8 @@
#include "wx/persist/bookctrl.h"
#if wxUSE_TREEBOOK
#include "wx/arrstr.h"
#include "wx/treebook.h"
@@ -19,11 +21,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 ','
// ----------------------------------------------------------------------------
@@ -93,4 +95,6 @@ inline wxPersistentObject *wxCreatePersistentObject(wxTreebook *book)
return new wxPersistentTreeBookCtrl(book);
}
#endif // wxUSE_TREEBOOK
#endif // _WX_PERSIST_TREEBOOK_H_