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