Add wxAddRemoveCtrl class.

This is a simple high level helper combining an arbitrary control showing
multiple items with the buttons allowing to add items to and remove items from
this control, but using the buttons and the layout appropriate for the current
platform.

Add the implementation itself, an example of using it to the dialogs sample
and the documentation.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78462 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2015-02-09 00:26:11 +00:00
parent adb339078e
commit 453897149f
36 changed files with 1388 additions and 184 deletions

109
include/wx/addremovectrl.h Normal file
View File

@@ -0,0 +1,109 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/addremovectrl.h
// Purpose: wxAddRemoveCtrl declaration.
// Author: Vadim Zeitlin
// Created: 2015-01-29
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_ADDREMOVECTRL_H_
#define _WX_ADDREMOVECTRL_H_
#include "wx/panel.h"
#if wxUSE_ADDREMOVECTRL
extern WXDLLIMPEXP_DATA_ADV(const char) wxAddRemoveCtrlNameStr[];
// ----------------------------------------------------------------------------
// wxAddRemoveAdaptor: used by wxAddRemoveCtrl to work with the list control
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxAddRemoveAdaptor
{
public:
// Default ctor and trivial but virtual dtor.
wxAddRemoveAdaptor() { }
virtual ~wxAddRemoveAdaptor() { }
// Override to return the associated control.
virtual wxWindow* GetItemsCtrl() const = 0;
// Override to return whether a new item can be added to the control.
virtual bool CanAdd() const = 0;
// Override to return whether the currently selected item (if any) can be
// removed from the control.
virtual bool CanRemove() const = 0;
// Called when an item should be added, can only be called if CanAdd()
// currently returns true.
virtual void OnAdd() = 0;
// Called when the current item should be removed, can only be called if
// CanRemove() currently returns true.
virtual void OnRemove() = 0;
private:
wxDECLARE_NO_COPY_CLASS(wxAddRemoveAdaptor);
};
// ----------------------------------------------------------------------------
// wxAddRemoveCtrl: a list-like control combined with add/remove buttons
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxAddRemoveCtrl : public wxPanel
{
public:
wxAddRemoveCtrl()
{
Init();
}
wxAddRemoveCtrl(wxWindow* parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxAddRemoveCtrlNameStr)
{
Init();
Create(parent, winid, pos, size, style, name);
}
bool Create(wxWindow* parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxAddRemoveCtrlNameStr);
virtual ~wxAddRemoveCtrl();
// Must be called for the control to be usable, takes ownership of the
// pointer.
void SetAdaptor(wxAddRemoveAdaptor* adaptor);
// Set tooltips to use for the add and remove buttons.
void SetButtonsToolTips(const wxString& addtip, const wxString& removetip);
protected:
virtual wxSize DoGetBestClientSize() const wxOVERRIDE;
private:
// Common part of all ctors.
void Init()
{
m_impl = NULL;
}
class wxAddRemoveImpl* m_impl;
wxDECLARE_NO_COPY_CLASS(wxAddRemoveCtrl);
};
#endif // wxUSE_ADDREMOVECTRL
#endif // _WX_ADDREMOVECTRL_H_

View File

@@ -992,6 +992,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -0,0 +1,66 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/generic/private/addremovectrl.h
// Purpose: Generic wxAddRemoveImpl implementation, also used in wxMSW
// Author: Vadim Zeitlin
// Created: 2015-02-05
// RCS-ID: $Id$
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_GENERIC_PRIVATE_ADDREMOVECTRL_H_
#define _WX_GENERIC_PRIVATE_ADDREMOVECTRL_H_
// ----------------------------------------------------------------------------
// wxAddRemoveImpl
// ----------------------------------------------------------------------------
class wxAddRemoveImpl : public wxAddRemoveImplWithButtons
{
public:
wxAddRemoveImpl(wxAddRemoveAdaptor* adaptor,
wxAddRemoveCtrl* parent,
wxWindow* ctrlItems)
: wxAddRemoveImplWithButtons(adaptor, parent, ctrlItems)
{
m_btnAdd = new wxButton(parent, wxID_ADD, GetAddButtonLabel(),
wxDefaultPosition, wxDefaultSize,
wxBU_EXACTFIT | wxBORDER_NONE);
m_btnRemove = new wxButton(parent, wxID_REMOVE, GetRemoveButtonLabel(),
wxDefaultPosition, wxDefaultSize,
wxBU_EXACTFIT | wxBORDER_NONE);
wxSizer* const sizerBtns = new wxBoxSizer(wxVERTICAL);
sizerBtns->Add(m_btnAdd, wxSizerFlags().Expand());
sizerBtns->Add(m_btnRemove, wxSizerFlags().Expand());
wxSizer* const sizerTop = new wxBoxSizer(wxHORIZONTAL);
sizerTop->Add(ctrlItems, wxSizerFlags(1).Expand());
sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border(wxLEFT));
parent->SetSizer(sizerTop);
SetUpEvents();
}
private:
static wxString GetAddButtonLabel()
{
#if wxUSE_UNICODE
return wchar_t(0xFF0B); // FULLWIDTH PLUS SIGN
#else
return "+";
#endif
}
static wxString GetRemoveButtonLabel()
{
#if wxUSE_UNICODE
return wchar_t(0x2012); // FIGURE DASH
#else
return "-";
#endif
}
};
#endif // _WX_GENERIC_PRIVATE_ADDREMOVECTRL_H_

View File

@@ -0,0 +1,81 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/gtk/private/addremovectrl.h
// Purpose: GTK specific wxAddRemoveImpl implementation
// Author: Vadim Zeitlin
// Created: 2015-02-05
// RCS-ID: $Id$
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_GTK_PRIVATE_ADDREMOVECTRL_H_
#define _WX_GTK_PRIVATE_ADDREMOVECTRL_H_
#include "wx/artprov.h"
#include "wx/bmpbuttn.h"
#include "wx/toolbar.h"
#include <gtk/gtk.h>
// ----------------------------------------------------------------------------
// wxAddRemoveImpl
// ----------------------------------------------------------------------------
class wxAddRemoveImpl : public wxAddRemoveImplBase
{
public:
wxAddRemoveImpl(wxAddRemoveAdaptor* adaptor,
wxAddRemoveCtrl* parent,
wxWindow* ctrlItems)
: wxAddRemoveImplBase(adaptor, parent, ctrlItems),
m_tbar(new wxToolBar(parent, wxID_ANY))
{
m_tbar->AddTool(wxID_ADD, wxString(), GetNamedBitmap("list-add"));
m_tbar->AddTool(wxID_REMOVE, wxString(), GetNamedBitmap("list-remove"));
#ifdef __WXGTK3__
// Tweak the toolbar appearance to correspond to how the toolbars used
// in other GNOME applications for similar purposes look.
GtkToolbar* const toolbar = m_tbar->GTKGetToolbar();
GtkStyleContext* context = gtk_widget_get_style_context(GTK_WIDGET(toolbar));
gtk_style_context_add_class(context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
gtk_style_context_set_junction_sides(context, GTK_JUNCTION_TOP);
#endif // GTK+3
wxSizer* const sizerTop = new wxBoxSizer(wxVERTICAL);
sizerTop->Add(ctrlItems, wxSizerFlags(1).Expand());
sizerTop->Add(m_tbar, wxSizerFlags().Expand());
parent->SetSizer(sizerTop);
m_tbar->Bind(wxEVT_UPDATE_UI,
&wxAddRemoveImplBase::OnUpdateUIAdd, this, wxID_ADD);
m_tbar->Bind(wxEVT_UPDATE_UI,
&wxAddRemoveImplBase::OnUpdateUIRemove, this, wxID_REMOVE);
m_tbar->Bind(wxEVT_TOOL, &wxAddRemoveImplBase::OnAdd, this, wxID_ADD);
m_tbar->Bind(wxEVT_TOOL, &wxAddRemoveImplBase::OnRemove, this, wxID_REMOVE);
}
virtual void SetButtonsToolTips(const wxString& addtip,
const wxString& removetip) wxOVERRIDE
{
m_tbar->SetToolShortHelp(wxID_ADD, addtip);
m_tbar->SetToolShortHelp(wxID_REMOVE, removetip);
}
private:
static wxBitmap GetNamedBitmap(const wxString& name)
{
// GTK UI guidelines recommend using "symbolic" versions of the icons
// for these buttons, so try them first but fall back to the normal
// ones if symbolic theme is not installed.
wxBitmap bmp = wxArtProvider::GetBitmap(name + "-symbolic", wxART_MENU);
if ( !bmp.IsOk() )
bmp = wxArtProvider::GetBitmap(name, wxART_MENU);
return bmp;
}
wxToolBar* const m_tbar;
};
#endif // _WX_GTK_PRIVATE_ADDREMOVECTRL_H_

View File

@@ -993,6 +993,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -993,6 +993,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -993,6 +993,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -993,6 +993,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -0,0 +1,118 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/osx/private/addremovectrl.h
// Purpose: OS X specific wxAddRemoveImpl implementation
// Author: Vadim Zeitlin
// Created: 2015-02-05
// RCS-ID: $Id$
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_OSX_PRIVATE_ADDREMOVECTRL_H_
#define _WX_OSX_PRIVATE_ADDREMOVECTRL_H_
#include "wx/artprov.h"
#include "wx/bmpbuttn.h"
#include "wx/panel.h"
#include "wx/osx/private.h"
// ----------------------------------------------------------------------------
// wxAddRemoveImpl itself
// ----------------------------------------------------------------------------
class wxAddRemoveImpl : public wxAddRemoveImplWithButtons
{
public:
wxAddRemoveImpl(wxAddRemoveAdaptor* adaptor,
wxAddRemoveCtrl* parent,
wxWindow* ctrlItems)
: wxAddRemoveImplWithButtons(adaptor, parent, ctrlItems),
m_ctrlItems(ctrlItems)
{
// This size is hard coded for now as this is what the system dialogs
// themselves (e.g. the buttons under the lists in the "Users" or
// "Network" panes of the "System Preferences") use under OS X 10.8.
const wxSize sizeBtn(25, 23);
m_btnAdd = new wxBitmapButton(parent, wxID_ADD,
wxArtProvider::GetBitmap("NSAddTemplate"),
wxDefaultPosition,
sizeBtn,
wxBORDER_SIMPLE);
m_btnRemove = new wxBitmapButton(parent, wxID_REMOVE,
wxArtProvider::GetBitmap("NSRemoveTemplate"),
wxDefaultPosition,
sizeBtn,
wxBORDER_SIMPLE);
// Under OS X the space to the right of the buttons is actually
// occupied by an inactive gradient button, so create one.
m_btnPlaceholder = new wxButton(parent, wxID_ANY, "",
wxDefaultPosition,
sizeBtn,
wxBORDER_SIMPLE);
m_btnPlaceholder->Disable();
// We need to lay out our windows manually under OS X as it is the only
// way to achieve the required, for the correct look, overlap between
// their borders -- sizers would never allow this.
parent->Bind(wxEVT_SIZE, &wxAddRemoveImpl::OnSize, this);
// We also have to ensure that the window with the items doesn't have
// any border as it wouldn't look correctly if it did.
long style = ctrlItems->GetWindowStyle();
style &= ~wxBORDER_MASK;
style |= wxBORDER_SIMPLE;
ctrlItems->SetWindowStyle(style);
SetUpEvents();
}
// As we don't use sizers, we also need to compute our best size ourselves.
virtual wxSize GetBestClientSize() const wxOVERRIDE
{
wxSize size = m_ctrlItems->GetBestSize();
const wxSize sizeBtn = m_btnAdd->GetSize();
size.y += sizeBtn.y;
size.IncTo(wxSize(3*sizeBtn.x, -1));
return size;
}
private:
void OnSize(wxSizeEvent& event)
{
const wxSize size = event.GetSize();
const wxSize sizeBtn = m_btnAdd->GetSize();
const int yBtn = size.y - sizeBtn.y;
// There is a vertical overlap which hides the items control bottom
// border.
m_ctrlItems->SetSize(0, 0, size.x, yBtn + 2);
// And there is also a horizontal 1px overlap between the buttons
// themselves, so subtract 1 from the next button position.
int x = 0;
m_btnAdd->Move(x, yBtn);
x += sizeBtn.x - 1;
m_btnRemove->Move(x, yBtn);
x += sizeBtn.x - 1;
// The last one needs to be resized to take up all the remaining space.
m_btnPlaceholder->SetSize(x, yBtn, size.x - x, sizeBtn.y);
}
wxWindow* m_ctrlItems;
wxButton* /* const */ m_btnPlaceholder;
};
#endif // _WX_OSX_PRIVATE_ADDREMOVECTRL_H_

View File

@@ -994,6 +994,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -0,0 +1,155 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/addremovectrl.h
// Purpose: wxAddRemoveImpl helper class declaration
// Author: Vadim Zeitlin
// Created: 2015-02-04
// RCS-ID: $Id$
// Copyright: (c) 2015 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_ADDREMOVECTRL_H_
#define _WX_PRIVATE_ADDREMOVECTRL_H_
#include "wx/button.h"
#include "wx/sizer.h"
// ----------------------------------------------------------------------------
// wxAddRemoveImplBase: implementation-only part of wxAddRemoveCtrl, base part
// ----------------------------------------------------------------------------
class wxAddRemoveImplBase
{
public:
// Base class ctor just initializes the associated adaptor, the derived
// class is supposed to create the buttons and layout everything.
//
// Takes ownership of the adaptor pointer.
wxEXPLICIT wxAddRemoveImplBase(wxAddRemoveAdaptor* adaptor,
wxAddRemoveCtrl* WXUNUSED(parent),
wxWindow* ctrlItems)
: m_adaptor(adaptor)
{
ctrlItems->Bind(wxEVT_CHAR, &wxAddRemoveImplBase::OnChar, this);
}
// wxOSX implementation needs to override this as it doesn't use sizers,
// for the others it is not necessary.
virtual wxSize GetBestClientSize() const { return wxDefaultSize; }
virtual void SetButtonsToolTips(const wxString& addtip,
const wxString& removetip) = 0;
virtual ~wxAddRemoveImplBase()
{
delete m_adaptor;
}
// Event handlers which must be connected to the appropriate sources by the
// derived classes.
void OnUpdateUIAdd(wxUpdateUIEvent& event)
{
event.Enable( m_adaptor->CanAdd() );
}
void OnUpdateUIRemove(wxUpdateUIEvent& event)
{
event.Enable( m_adaptor->CanRemove() );
}
void OnAdd(wxCommandEvent& WXUNUSED(event))
{
m_adaptor->OnAdd();
}
void OnRemove(wxCommandEvent& WXUNUSED(event))
{
m_adaptor->OnRemove();
}
private:
// This event handler is connected by this class itself and doesn't need to
// be accessible to the derived classes.
void OnChar(wxKeyEvent& event)
{
switch ( event.GetKeyCode() )
{
case '+':
case WXK_INSERT:
case WXK_NUMPAD_INSERT:
if ( m_adaptor->CanAdd() )
m_adaptor->OnAdd();
return;
case '-':
case WXK_DELETE:
case WXK_NUMPAD_DELETE:
if ( m_adaptor->CanRemove() )
m_adaptor->OnRemove();
return;
}
event.Skip();
}
wxAddRemoveAdaptor* const m_adaptor;
wxDECLARE_NO_COPY_CLASS(wxAddRemoveImplBase);
};
// GTK+ uses a wxToolBar-based implementation and so doesn't need this class.
#ifndef __WXGTK__
// Base class for the ports using actual wxButtons for the "+"/"-" buttons.
class wxAddRemoveImplWithButtons : public wxAddRemoveImplBase
{
public:
wxEXPLICIT wxAddRemoveImplWithButtons(wxAddRemoveAdaptor* adaptor,
wxAddRemoveCtrl* parent,
wxWindow* ctrlItems)
: wxAddRemoveImplBase(adaptor, parent, ctrlItems)
{
m_btnAdd =
m_btnRemove = NULL;
}
virtual void SetButtonsToolTips(const wxString& addtip,
const wxString& removetip) wxOVERRIDE
{
m_btnAdd->SetToolTip(addtip);
m_btnRemove->SetToolTip(removetip);
}
protected:
// Must be called by the derived class ctor after creating the buttons to
// set up the event handlers.
void SetUpEvents()
{
m_btnAdd->Bind(wxEVT_UPDATE_UI,
&wxAddRemoveImplBase::OnUpdateUIAdd, this);
m_btnRemove->Bind(wxEVT_UPDATE_UI,
&wxAddRemoveImplBase::OnUpdateUIRemove, this);
m_btnAdd->Bind(wxEVT_BUTTON, &wxAddRemoveImplBase::OnAdd, this);
m_btnRemove->Bind(wxEVT_BUTTON, &wxAddRemoveImplBase::OnRemove, this);
}
wxButton *m_btnAdd,
*m_btnRemove;
wxDECLARE_NO_COPY_CLASS(wxAddRemoveImplWithButtons);
};
#endif // !wxGTK
#ifdef __WXOSX__
#include "wx/osx/private/addremovectrl.h"
#elif defined(__WXGTK__)
#include "wx/gtk/private/addremovectrl.h"
#else
#include "wx/generic/private/addremovectrl.h"
#endif
#endif // _WX_PRIVATE_ADDREMOVECTRL_H_

View File

@@ -989,6 +989,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------

View File

@@ -992,6 +992,16 @@
// wxHeaderCtrl)
#define wxUSE_REARRANGECTRL 1
// wxAddRemoveCtrl is a composite control containing a control showing some
// items (e.g. wxListBox, wxListCtrl, wxTreeCtrl, wxDataViewCtrl, ...) and "+"/
// "-" buttons allowing to add and remove items to/from the control.
//
// Default is 1.
//
// Recommended setting: 1 but can be safely set to 0 if you don't need it (not
// used by the library itself).
#define wxUSE_ADDREMOVECTRL 1
// ----------------------------------------------------------------------------
// Miscellaneous GUI stuff
// ----------------------------------------------------------------------------