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