added multiple selections support to wxDirCtrl (closes #10830)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60909 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-06-06 14:00:51 +00:00
parent 608129e541
commit 80f624ec0c
5 changed files with 146 additions and 9 deletions

View File

@@ -51,7 +51,9 @@ enum
// Use 3D borders on internal controls // Use 3D borders on internal controls
wxDIRCTRL_3D_INTERNAL = 0x0080, wxDIRCTRL_3D_INTERNAL = 0x0080,
// Editable labels // Editable labels
wxDIRCTRL_EDIT_LABELS = 0x0100 wxDIRCTRL_EDIT_LABELS = 0x0100,
// Allow multiple selection
wxDIRCTRL_MULTIPLE = 0x0200
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -128,12 +130,17 @@ public:
// Get dir or filename // Get dir or filename
virtual wxString GetPath() const; virtual wxString GetPath() const;
virtual void GetPaths(wxArrayString& paths) const;
// Get selected filename path only (else empty string). // Get selected filename path only (else empty string).
// I.e. don't count a directory as a selection // I.e. don't count a directory as a selection
virtual wxString GetFilePath() const; virtual wxString GetFilePath() const;
virtual void GetFilePaths(wxArrayString& paths) const;
virtual void SetPath(const wxString& path); virtual void SetPath(const wxString& path);
virtual void SelectPath(const wxString& path, bool select = true);
virtual void SelectPaths(const wxArrayString& paths);
virtual void ShowHidden( bool show ); virtual void ShowHidden( bool show );
virtual bool GetShowHidden() { return m_showHidden; } virtual bool GetShowHidden() { return m_showHidden; }
@@ -148,6 +155,8 @@ public:
virtual wxTreeCtrl* GetTreeCtrl() const { return m_treeCtrl; } virtual wxTreeCtrl* GetTreeCtrl() const { return m_treeCtrl; }
virtual wxDirFilterListCtrl* GetFilterListCtrl() const { return m_filterListCtrl; } virtual wxDirFilterListCtrl* GetFilterListCtrl() const { return m_filterListCtrl; }
virtual void UnselectAll();
// Helper // Helper
virtual void SetupSections(); virtual void SetupSections();
@@ -166,7 +175,6 @@ public:
// Collapse the entire tree // Collapse the entire tree
virtual void CollapseTree(); virtual void CollapseTree();
// overridden base class methods // overridden base class methods
virtual void SetFocus(); virtual void SetFocus();

View File

@@ -25,6 +25,8 @@
directory. directory.
@style{wxDIRCTRL_EDIT_LABELS} @style{wxDIRCTRL_EDIT_LABELS}
Allow the folder and file labels to be editable. Allow the folder and file labels to be editable.
@style{wxDIRCTRL_MULTIPLE}
Allows multiple files and folders to be selected.
@endStyleTable @endStyleTable
@library{wxbase} @library{wxbase}
@@ -118,6 +120,13 @@ public:
*/ */
virtual wxString GetFilePath() const; virtual wxString GetFilePath() const;
/**
Fills the array @a paths with the currently selected filepaths.
This function doesn't count a directory as a selection.
*/
virtual void GetFilePaths(wxArrayString& paths) const;
/** /**
Returns the filter string. Returns the filter string.
*/ */
@@ -138,6 +147,11 @@ public:
*/ */
virtual wxString GetPath() const; virtual wxString GetPath() const;
/**
Fills the array @a paths with the selected directories and filenames.
*/
virtual void GetPaths(wxArrayString& paths) const;
/** /**
Returns the root id for the tree control. Returns the root id for the tree control.
*/ */
@@ -185,5 +199,27 @@ public:
control. If @false, they will not be displayed. control. If @false, they will not be displayed.
*/ */
virtual void ShowHidden(bool show); virtual void ShowHidden(bool show);
/**
Selects the given item.
In multiple selection controls, can be also used to deselect a
currently selected item if the value of @a select is false.
Existing selections are not changed. Only visible items can be
(de)selected, otherwise use ExpandPath().
*/
virtual void SelectPath(const wxString& path, bool select = true);
/**
Selects only the specified paths, clearing any previous selection.
Only supported when wxDIRCTRL_MULTIPLE is set.
*/
virtual void SelectPaths(const wxArrayString& paths);
/**
Removes the selection from all currently selected items.
*/
virtual void UnselectAll();
}; };

View File

@@ -133,7 +133,9 @@ protected:
wxCheckBox *m_chkDirOnly, wxCheckBox *m_chkDirOnly,
*m_chk3D, *m_chk3D,
*m_chkFirst, *m_chkFirst,
*m_chkLabels; *m_chkLabels,
*m_chkMulti;
// filters // filters
wxCheckBox *m_fltr[3]; wxCheckBox *m_fltr[3];
@@ -185,6 +187,7 @@ void DirCtrlWidgetsPage::CreateContent()
m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL")); m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL"));
m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST")); m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST"));
m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS")); m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS"));
m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_MULTIPLE"));
sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border()); sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
wxSizer *sizerFilters = wxSizer *sizerFilters =
@@ -245,13 +248,14 @@ void DirCtrlWidgetsPage::CreateDirCtrl()
( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) | ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) | ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) | ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
); );
wxString filter; wxString filter;
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
{ {
if (m_fltr[i]->IsChecked()) if (m_fltr[i]->IsChecked())
{ {
if (!filter.IsEmpty()) if (!filter.IsEmpty())
filter += wxT("|"); filter += wxT("|");

View File

@@ -472,6 +472,7 @@ wxBEGIN_FLAGS( wxGenericDirCtrlStyle )
wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY) wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY)
wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL) wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL)
wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST) wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST)
wxFLAGS_MEMBER(wxDIRCTRL_MULTIPLE)
wxEND_FLAGS( wxGenericDirCtrlStyle ) wxEND_FLAGS( wxGenericDirCtrlStyle )
@@ -560,6 +561,9 @@ bool wxGenericDirCtrl::Create(wxWindow *parent,
if (style & wxDIRCTRL_EDIT_LABELS) if (style & wxDIRCTRL_EDIT_LABELS)
treeStyle |= wxTR_EDIT_LABELS; treeStyle |= wxTR_EDIT_LABELS;
if (style & wxDIRCTRL_MULTIPLE)
treeStyle |= wxTR_MULTIPLE;
if ((style & wxDIRCTRL_3D_INTERNAL) == 0) if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
treeStyle |= wxNO_BORDER; treeStyle |= wxNO_BORDER;
@@ -629,9 +633,22 @@ void wxGenericDirCtrl::ShowHidden( bool show )
m_showHidden = show; m_showHidden = show;
wxString path = GetPath(); if ( HasFlag(wxDIRCTRL_MULTIPLE) )
ReCreateTree(); {
SetPath(path); wxArrayString paths;
GetPaths(paths);
ReCreateTree();
for ( unsigned n = 0; n < paths.size(); n++ )
{
ExpandPath(paths[n]);
}
}
else
{
wxString path = GetPath();
ReCreateTree();
SetPath(path);
}
} }
const wxTreeItemId const wxTreeItemId
@@ -1095,6 +1112,20 @@ wxString wxGenericDirCtrl::GetPath() const
return wxEmptyString; return wxEmptyString;
} }
void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const
{
paths.clear();
wxArrayTreeItemIds items;
m_treeCtrl->GetSelections(items);
for ( unsigned n = 0; n < items.size(); n++ )
{
wxTreeItemId id = items[n];
wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
paths.Add(data->m_path);
}
}
wxString wxGenericDirCtrl::GetFilePath() const wxString wxGenericDirCtrl::GetFilePath() const
{ {
wxTreeItemId id = m_treeCtrl->GetSelection(); wxTreeItemId id = m_treeCtrl->GetSelection();
@@ -1110,6 +1141,21 @@ wxString wxGenericDirCtrl::GetFilePath() const
return wxEmptyString; return wxEmptyString;
} }
void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const
{
paths.clear();
wxArrayTreeItemIds items;
m_treeCtrl->GetSelections(items);
for ( unsigned n = 0; n < items.size(); n++ )
{
wxTreeItemId id = items[n];
wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
if ( !data->m_isDir )
paths.Add(data->m_path);
}
}
void wxGenericDirCtrl::SetPath(const wxString& path) void wxGenericDirCtrl::SetPath(const wxString& path)
{ {
m_defaultPath = path; m_defaultPath = path;
@@ -1117,6 +1163,48 @@ void wxGenericDirCtrl::SetPath(const wxString& path)
ExpandPath(path); ExpandPath(path);
} }
void wxGenericDirCtrl::SelectPath(const wxString& path, bool select)
{
bool done = false;
wxTreeItemId id = FindChild(m_rootId, path, done);
wxTreeItemId lastId = id; // The last non-zero id
while ( id.IsOk() && !done )
{
id = FindChild(id, path, done);
if ( id.IsOk() )
lastId = id;
}
if ( !lastId.IsOk() )
return;
if ( done )
{
if(select && m_treeCtrl->IsSelected(id))
return;
else
{
m_treeCtrl->SelectItem(id, select);
}
}
}
void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths)
{
if ( HasFlag(wxDIRCTRL_MULTIPLE) )
{
UnselectAll();
for ( unsigned n = 0; n < paths.size(); n++ )
{
SelectPath(paths[n]);
}
}
}
void wxGenericDirCtrl::UnselectAll()
{
m_treeCtrl->UnselectAll();
}
// Not used // Not used
#if 0 #if 0
void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames) void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)

View File

@@ -34,6 +34,7 @@ wxGenericDirCtrlXmlHandler::wxGenericDirCtrlXmlHandler()
XRC_ADD_STYLE(wxDIRCTRL_3D_INTERNAL); XRC_ADD_STYLE(wxDIRCTRL_3D_INTERNAL);
XRC_ADD_STYLE(wxDIRCTRL_SELECT_FIRST); XRC_ADD_STYLE(wxDIRCTRL_SELECT_FIRST);
XRC_ADD_STYLE(wxDIRCTRL_EDIT_LABELS); XRC_ADD_STYLE(wxDIRCTRL_EDIT_LABELS);
XRC_ADD_STYLE(wxDIRCTRL_MULTIPLE);
AddWindowStyles(); AddWindowStyles();
} }