]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
Added logic, API and docs for wxDataViewModel::HasDefaultCompare indicating a compare...
[wxWidgets.git] / src / generic / dirdlgg.cpp
CommitLineData
d7a15103 1/////////////////////////////////////////////////////////////////////////////
897b24cf 2// Name: src/generic/dirdlg.cpp
d7a15103 3// Purpose: wxDirDialog
748fcded 4// Author: Harm van der Heijden, Robert Roebling & Julian Smart
d7a15103
JS
5// Modified by:
6// Created: 12/12/98
8b17ba72 7// RCS-ID: $Id$
748fcded 8// Copyright: (c) Harm van der Heijden, Robert Roebling, Julian Smart
65571936 9// Licence: wxWindows licence
d7a15103
JS
10/////////////////////////////////////////////////////////////////////////////
11
1e6d9499
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
93763ad5 16 #pragma hdrstop
1e6d9499
JS
17#endif
18
ce4169a4
RR
19#if wxUSE_DIRDLG
20
748fcded
VS
21#ifndef WX_PRECOMP
22 #include "wx/textctrl.h"
23 #include "wx/button.h"
42dcacf0 24 #include "wx/checkbox.h"
748fcded 25 #include "wx/sizer.h"
748fcded
VS
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/msgdlg.h"
910b0053 29 #include "wx/bmpbuttn.h"
dc6c62a9 30#endif
d7a15103 31
d7d17624 32#include "wx/statline.h"
e78d4a23 33#include "wx/dirctrl.h"
bf2cac26 34#include "wx/generic/dirdlgg.h"
2a0951be 35#include "wx/artprov.h"
d7a15103 36
0d1f53ca
WS
37// ----------------------------------------------------------------------------
38// constants
39// ----------------------------------------------------------------------------
d6379fa3 40
d7a15103
JS
41static const int ID_DIRCTRL = 1000;
42static const int ID_TEXTCTRL = 1001;
d7a15103 43static const int ID_NEW = 1004;
42dcacf0 44static const int ID_SHOW_HIDDEN = 1005;
2a0951be 45static const int ID_GO_HOME = 1006;
d7a15103 46
0d1f53ca
WS
47//-----------------------------------------------------------------------------
48// wxGenericDirDialog
49//-----------------------------------------------------------------------------
50
51IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog)
52
748fcded 53BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
748fcded 54 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
42dcacf0
RR
55 EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK)
56 EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew)
e13ea14e 57 EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome)
ca65c044
WS
58 EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown)
59 EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected)
42dcacf0
RR
60 EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK)
61 EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden)
d7a15103
JS
62END_EVENT_TABLE()
63
748fcded
VS
64wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title,
65 const wxString& defaultPath, long style,
ca65c044 66 const wxPoint& pos, const wxSize& sz,
141d782d 67 const wxString& name)
b50747ea
RR
68{
69 Create(parent, title, defaultPath, style, pos, sz, name);
70}
71
141d782d
VZ
72bool wxGenericDirDialog::Create(wxWindow* parent,
73 const wxString& title,
1fb351fa 74 const wxString& defaultPath, long style,
141d782d
VZ
75 const wxPoint& pos,
76 const wxSize& sz,
77 const wxString& name)
d7a15103 78{
748fcded 79 wxBusyCursor cursor;
e90c1d2a 80
2229243b
VZ
81 parent = GetParentForModalDialog(parent);
82
141d782d
VZ
83 if (!wxDirDialogBase::Create(parent, title, defaultPath, style, pos, sz, name))
84 return false;
85
dc6c62a9 86 m_path = defaultPath;
748fcded 87 if (m_path == wxT("~"))
ab8d2b02
VS
88 wxGetHomeDir(&m_path);
89 if (m_path == wxT("."))
90 m_path = wxGetCwd();
e90c1d2a 91
dc6c62a9
RR
92 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
93
897b24cf 94 // smartphones does not support or do not waste space for wxButtons
0d1f53ca
WS
95#if defined(__SMARTPHONE__)
96
d2cdad17
WS
97 wxMenu *dirMenu = new wxMenu;
98 dirMenu->Append(ID_GO_HOME, _("Home"));
99
ff3e84ff 100 if (!HasFlag(wxDD_DIR_MUST_EXIST))
d2cdad17
WS
101 {
102 dirMenu->Append(ID_NEW, _("New directory"));
103 }
104
105 dirMenu->AppendCheckItem(ID_SHOW_HIDDEN, _("Show hidden directories"));
106 dirMenu->AppendSeparator();
107 dirMenu->Append(wxID_CANCEL, _("Cancel"));
108
0d1f53ca
WS
109#else
110
2a0951be
VS
111 // 0) 'New' and 'Home' Buttons
112 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
113
e13ea14e 114 // VS: 'Home directory' concept is unknown to MS-DOS
0d1f53ca 115#if !defined(__DOS__)
ca65c044 116 wxBitmapButton* homeButton =
2a0951be 117 new wxBitmapButton(this, ID_GO_HOME,
36668b35 118 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
2a0951be 119 buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 );
e13ea14e 120#endif
ca65c044 121
2a0951be
VS
122 // I'm not convinced we need a New button, and we tend to get annoying
123 // accidental-editing with label editing enabled.
ff3e84ff 124 if (!HasFlag(wxDD_DIR_MUST_EXIST))
dabd1377 125 {
ca65c044 126 wxBitmapButton* newButton =
dabd1377 127 new wxBitmapButton(this, ID_NEW,
36668b35 128 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
dabd1377
JS
129 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
130#if wxUSE_TOOLTIPS
131 newButton->SetToolTip(_("Create new directory"));
ca65c044 132#endif
dabd1377 133 }
2a0951be
VS
134
135#if wxUSE_TOOLTIPS
136 homeButton->SetToolTip(_("Go to home directory"));
2a0951be
VS
137#endif
138
139 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
140
0d1f53ca
WS
141#endif // __SMARTPHONE__/!__SMARTPHONE__
142
dc6c62a9 143 // 1) dir ctrl
3103e8a9 144 m_dirCtrl = NULL; // this is necessary, event handler called from
748fcded 145 // wxGenericDirCtrl would crash otherwise!
03775239 146 long dirStyle = wxDIRCTRL_DIR_ONLY | wxDEFAULT_CONTROL_BORDER;
ca65c044 147
dabd1377 148#ifdef __WXMSW__
ff3e84ff 149 if (!HasFlag(wxDD_DIR_MUST_EXIST))
dabd1377
JS
150 {
151 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
152 // before we can call EditLabel (required for "New directory")
153 dirStyle |= wxDIRCTRL_EDIT_LABELS;
154 }
ca65c044 155#endif
dabd1377 156
748fcded 157 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
488eb04e 158 m_path, wxDefaultPosition,
ca65c044 159 wxSize(300, 200),
dabd1377 160 dirStyle);
748fcded 161
bd9f3519
VZ
162 wxSizerFlags flagsBorder2;
163 flagsBorder2.DoubleBorder(wxTOP | wxLEFT | wxRIGHT);
164
165 topsizer->Add(m_dirCtrl, wxSizerFlags(flagsBorder2).Proportion(1).Expand());
e90c1d2a 166
0d1f53ca 167#ifndef __SMARTPHONE__
ead95817 168 // Make the an option depending on a flag?
bd9f3519
VZ
169 wxCheckBox *
170 check = new wxCheckBox(this, ID_SHOW_HIDDEN, _("Show &hidden directories"));
171 topsizer->Add(check, wxSizerFlags(flagsBorder2).Right());
0d1f53ca 172#endif // !__SMARTPHONE__
ead95817 173
dc6c62a9 174 // 2) text ctrl
e90c1d2a 175 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
bd9f3519 176 topsizer->Add(m_input, wxSizerFlags(flagsBorder2).Expand());
0d1f53ca 177
897b24cf 178 // 3) buttons if any
bd9f3519
VZ
179 wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
180 if ( buttonSizer )
897b24cf 181 {
bd9f3519 182 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
897b24cf 183 }
e90c1d2a 184
897b24cf 185#ifdef __SMARTPHONE__
bd9f3519 186 // overwrite menu set by CreateSeparatedButtonSizer() call above
897b24cf 187 SetRightMenu(wxID_ANY, _("Options"), dirMenu);
dc6c62a9 188#endif
d7a15103 189
9026d6fd 190 m_input->SetFocus();
e90c1d2a 191
ca65c044 192 SetAutoLayout( true );
dc6c62a9 193 SetSizer( topsizer );
e90c1d2a 194
dc6c62a9
RR
195 topsizer->SetSizeHints( this );
196 topsizer->Fit( this );
197
198 Centre( wxBOTH );
b50747ea
RR
199
200 return true;
748fcded 201}
dc6c62a9 202
141d782d
VZ
203void wxGenericDirDialog::EndModal(int retCode)
204{
205 // before proceeding, change the current working directory if user asked so
206 if (retCode == wxID_OK && HasFlag(wxDD_CHANGE_DIR))
207 wxSetWorkingDirectory(m_path);
208
209 wxDialog::EndModal(retCode);
210}
211
748fcded
VS
212void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
213{
214 EndModal(wxID_CANCEL);
215}
2a4dbd81 216
748fcded
VS
217void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
218{
219 m_path = m_input->GetValue();
141d782d 220
748fcded 221 // Does the path exist? (User may have typed anything in m_input)
141d782d
VZ
222 if (wxDirExists(m_path))
223 {
748fcded
VS
224 // OK, path exists, we're done.
225 EndModal(wxID_OK);
226 return;
f6bcfd97 227 }
141d782d 228
748fcded
VS
229 // Interact with user, find out if the dir is a typo or to be created
230 wxString msg;
ca65c044 231 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
748fcded 232 m_path.c_str());
ca65c044 233 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
748fcded
VS
234 wxYES_NO | wxICON_WARNING);
235
141d782d
VZ
236 if ( dialog.ShowModal() == wxID_YES )
237 {
748fcded
VS
238 // Okay, let's make it
239 wxLogNull log;
141d782d
VZ
240 if (wxMkdir(m_path))
241 {
748fcded
VS
242 // The new dir was created okay.
243 EndModal(wxID_OK);
244 return;
245 }
141d782d
VZ
246 else
247 {
748fcded 248 // Trouble...
ca65c044 249 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
748fcded
VS
250 m_path.c_str());
251 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
252 errmsg.ShowModal();
253 // We still don't have a valid dir. Back to the main dialog.
f6bcfd97 254 }
f6bcfd97 255 }
748fcded
VS
256 // User has answered NO to create dir.
257}
f6bcfd97 258
748fcded
VS
259void wxGenericDirDialog::SetPath(const wxString& path)
260{
261 m_dirCtrl->SetPath(path);
262 m_path = path;
263}
f6bcfd97 264
748fcded
VS
265wxString wxGenericDirDialog::GetPath(void) const
266{
267 return m_path;
d7a15103
JS
268}
269
748fcded 270int wxGenericDirDialog::ShowModal()
d7a15103 271{
dc6c62a9
RR
272 m_input->SetValue( m_path );
273 return wxDialog::ShowModal();
d7a15103
JS
274}
275
748fcded 276void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 277{
748fcded
VS
278 if (!m_dirCtrl)
279 return;
280
44d60c0b
WS
281 wxTreeItemId item = event.GetItem();
282
283 wxDirItemData *data = NULL;
284
285 if(item.IsOk())
286 data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item);
287
e90c1d2a 288 if (data)
dc6c62a9 289 m_input->SetValue( data->m_path );
17a1ebd1 290}
d7a15103 291
748fcded 292void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 293{
748fcded
VS
294 if (!m_dirCtrl)
295 return;
296
297 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 298 if (data)
dc6c62a9 299 m_input->SetValue( data->m_path );
17a1ebd1 300}
d7a15103 301
42dcacf0
RR
302void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
303{
304 if (!m_dirCtrl)
305 return;
306
2b5f62a0 307 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
42dcacf0
RR
308}
309
748fcded 310void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 311{
748fcded
VS
312 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
313 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
99006e44 314 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 315 {
e90c1d2a
VZ
316 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
317 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
318 msg.ShowModal();
319 return;
320 }
d7a15103 321
99006e44 322 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
748fcded 323 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 324 wxASSERT( data );
e90c1d2a 325
748fcded 326 wxString new_name( _("NewName") );
dc6c62a9 327 wxString path( data->m_path );
083f7497 328 if (!wxEndsWithPathSeparator(path))
748fcded 329 path += wxFILE_SEP_PATH;
dc6c62a9 330 path += new_name;
da865fdd 331 if (wxDirExists(path))
dc6c62a9
RR
332 {
333 // try NewName0, NewName1 etc.
334 int i = 0;
f6bcfd97 335 do {
748fcded 336 new_name = _("NewName");
f6bcfd97
BP
337 wxString num;
338 num.Printf( wxT("%d"), i );
339 new_name += num;
e90c1d2a 340
dc6c62a9 341 path = data->m_path;
083f7497 342 if (!wxEndsWithPathSeparator(path))
748fcded 343 path += wxFILE_SEP_PATH;
dc6c62a9 344 path += new_name;
f6bcfd97 345 i++;
da865fdd 346 } while (wxDirExists(path));
d7a15103 347 }
e90c1d2a 348
dc6c62a9 349 wxLogNull log;
e90c1d2a 350 if (!wxMkdir(path))
dc6c62a9
RR
351 {
352 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 353 dialog.ShowModal();
dc6c62a9
RR
354 return;
355 }
356
ca65c044 357 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
d7a15103 358
748fcded
VS
359 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
360 // of the parent.
361 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
362 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
363 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
d7a15103 364}
ce4169a4 365
e13ea14e
VS
366void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
367{
368 SetPath(wxGetUserHome());
369}
370
f6bcfd97 371#endif // wxUSE_DIRDLG