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