]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/dirdlg.cpp | |
3 | // Purpose: wxDirDialog | |
4 | // Author: Harm van der Heijden, Robert Roebling & Julian Smart | |
5 | // Modified by: | |
6 | // Created: 12/12/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Harm van der Heijden, Robert Roebling, Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_DIRDLG | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/textctrl.h" | |
23 | #include "wx/button.h" | |
24 | #include "wx/checkbox.h" | |
25 | #include "wx/sizer.h" | |
26 | #include "wx/intl.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/msgdlg.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/statline.h" | |
32 | #include "wx/dirctrl.h" | |
33 | #include "wx/generic/dirdlgg.h" | |
34 | #include "wx/artprov.h" | |
35 | #include "wx/bmpbuttn.h" | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // constants | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | static const int ID_DIRCTRL = 1000; | |
42 | static const int ID_TEXTCTRL = 1001; | |
43 | static const int ID_NEW = 1004; | |
44 | static const int ID_SHOW_HIDDEN = 1005; | |
45 | static const int ID_GO_HOME = 1006; | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | // macros | |
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | /* Macro for avoiding #ifdefs when value have to be different depending on size of | |
52 | device we display on - take it from something like wxDesktopPolicy in the future | |
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 | ||
67 | BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog) | |
68 | EVT_CLOSE (wxGenericDirDialog::OnCloseWindow) | |
69 | EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK) | |
70 | EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew) | |
71 | EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome) | |
72 | EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown) | |
73 | EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected) | |
74 | EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK) | |
75 | EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden) | |
76 | END_EVENT_TABLE() | |
77 | ||
78 | wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title, | |
79 | const wxString& defaultPath, long style, | |
80 | const wxPoint& pos, const wxSize& sz, | |
81 | const wxString& name): | |
82 | wxDirDialogBase(parent, title, defaultPath, style, pos, sz, name) | |
83 | { | |
84 | Create(parent, title, defaultPath, style, pos, sz, name); | |
85 | } | |
86 | ||
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)) | |
93 | { | |
94 | wxBusyCursor cursor; | |
95 | ||
96 | m_path = defaultPath; | |
97 | if (m_path == wxT("~")) | |
98 | wxGetHomeDir(&m_path); | |
99 | if (m_path == wxT(".")) | |
100 | m_path = wxGetCwd(); | |
101 | ||
102 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
103 | ||
104 | // smartphones does not support or do not waste space for wxButtons | |
105 | #if defined(__SMARTPHONE__) | |
106 | ||
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 | ||
119 | #else | |
120 | ||
121 | // 0) 'New' and 'Home' Buttons | |
122 | wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL ); | |
123 | ||
124 | // VS: 'Home directory' concept is unknown to MS-DOS | |
125 | #if !defined(__DOS__) | |
126 | wxBitmapButton* homeButton = | |
127 | new wxBitmapButton(this, ID_GO_HOME, | |
128 | wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON)); | |
129 | buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 ); | |
130 | #endif | |
131 | ||
132 | // I'm not convinced we need a New button, and we tend to get annoying | |
133 | // accidental-editing with label editing enabled. | |
134 | if (style & wxDD_NEW_DIR_BUTTON) | |
135 | { | |
136 | wxBitmapButton* newButton = | |
137 | new wxBitmapButton(this, ID_NEW, | |
138 | wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON)); | |
139 | buttonsizer->Add( newButton, 0, wxRIGHT, 10 ); | |
140 | #if wxUSE_TOOLTIPS | |
141 | newButton->SetToolTip(_("Create new directory")); | |
142 | #endif | |
143 | } | |
144 | ||
145 | #if wxUSE_TOOLTIPS | |
146 | homeButton->SetToolTip(_("Go to home directory")); | |
147 | #endif | |
148 | ||
149 | topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 ); | |
150 | ||
151 | #endif // __SMARTPHONE__/!__SMARTPHONE__ | |
152 | ||
153 | // 1) dir ctrl | |
154 | m_dirCtrl = NULL; // this is necessary, event handler called from | |
155 | // wxGenericDirCtrl would crash otherwise! | |
156 | long dirStyle = wxDIRCTRL_DIR_ONLY | wxDEFAULT_CONTROL_BORDER; | |
157 | ||
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 | } | |
165 | #endif | |
166 | ||
167 | m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL, | |
168 | m_path, wxDefaultPosition, | |
169 | wxSize(300, 200), | |
170 | dirStyle); | |
171 | ||
172 | topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) ); | |
173 | ||
174 | #ifndef __SMARTPHONE__ | |
175 | // Make the an option depending on a flag? | |
176 | wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") ); | |
177 | topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 ); | |
178 | #endif // !__SMARTPHONE__ | |
179 | ||
180 | // 2) text ctrl | |
181 | m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition ); | |
182 | topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) ); | |
183 | ||
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 | } | |
195 | ||
196 | #ifdef __SMARTPHONE__ | |
197 | // overwrite menu achieved with earlier CreateButtonSizer() call | |
198 | SetRightMenu(wxID_ANY, _("Options"), dirMenu); | |
199 | #endif | |
200 | ||
201 | m_input->SetFocus(); | |
202 | ||
203 | SetAutoLayout( true ); | |
204 | SetSizer( topsizer ); | |
205 | ||
206 | topsizer->SetSizeHints( this ); | |
207 | topsizer->Fit( this ); | |
208 | ||
209 | Centre( wxBOTH ); | |
210 | ||
211 | return true; | |
212 | } | |
213 | ||
214 | void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
215 | { | |
216 | EndModal(wxID_CANCEL); | |
217 | } | |
218 | ||
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) | |
223 | if (wxDirExists(m_path)) { | |
224 | // OK, path exists, we're done. | |
225 | EndModal(wxID_OK); | |
226 | return; | |
227 | } | |
228 | // Interact with user, find out if the dir is a typo or to be created | |
229 | wxString msg; | |
230 | msg.Printf(_("The directory '%s' does not exist\nCreate it now?"), | |
231 | m_path.c_str()); | |
232 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), | |
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... | |
245 | msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"), | |
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. | |
250 | } | |
251 | } | |
252 | // User has answered NO to create dir. | |
253 | } | |
254 | ||
255 | void wxGenericDirDialog::SetPath(const wxString& path) | |
256 | { | |
257 | m_dirCtrl->SetPath(path); | |
258 | m_path = path; | |
259 | } | |
260 | ||
261 | wxString wxGenericDirDialog::GetPath(void) const | |
262 | { | |
263 | return m_path; | |
264 | } | |
265 | ||
266 | int wxGenericDirDialog::ShowModal() | |
267 | { | |
268 | m_input->SetValue( m_path ); | |
269 | return wxDialog::ShowModal(); | |
270 | } | |
271 | ||
272 | void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event ) | |
273 | { | |
274 | if (!m_dirCtrl) | |
275 | return; | |
276 | ||
277 | wxTreeItemId item = event.GetItem(); | |
278 | ||
279 | wxDirItemData *data = NULL; | |
280 | ||
281 | if(item.IsOk()) | |
282 | data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item); | |
283 | ||
284 | if (data) | |
285 | m_input->SetValue( data->m_path ); | |
286 | } | |
287 | ||
288 | void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) ) | |
289 | { | |
290 | if (!m_dirCtrl) | |
291 | return; | |
292 | ||
293 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection()); | |
294 | if (data) | |
295 | m_input->SetValue( data->m_path ); | |
296 | } | |
297 | ||
298 | void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event ) | |
299 | { | |
300 | if (!m_dirCtrl) | |
301 | return; | |
302 | ||
303 | m_dirCtrl->ShowHidden( event.GetInt() != 0 ); | |
304 | } | |
305 | ||
306 | void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) ) | |
307 | { | |
308 | wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection(); | |
309 | if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) || | |
310 | (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem())) | |
311 | { | |
312 | wxMessageDialog msg(this, _("You cannot add a new directory to this section."), | |
313 | _("Create directory"), wxOK | wxICON_INFORMATION ); | |
314 | msg.ShowModal(); | |
315 | return; | |
316 | } | |
317 | ||
318 | wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id ); | |
319 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent ); | |
320 | wxASSERT( data ); | |
321 | ||
322 | wxString new_name( _("NewName") ); | |
323 | wxString path( data->m_path ); | |
324 | if (!wxEndsWithPathSeparator(path)) | |
325 | path += wxFILE_SEP_PATH; | |
326 | path += new_name; | |
327 | if (wxDirExists(path)) | |
328 | { | |
329 | // try NewName0, NewName1 etc. | |
330 | int i = 0; | |
331 | do { | |
332 | new_name = _("NewName"); | |
333 | wxString num; | |
334 | num.Printf( wxT("%d"), i ); | |
335 | new_name += num; | |
336 | ||
337 | path = data->m_path; | |
338 | if (!wxEndsWithPathSeparator(path)) | |
339 | path += wxFILE_SEP_PATH; | |
340 | path += new_name; | |
341 | i++; | |
342 | } while (wxDirExists(path)); | |
343 | } | |
344 | ||
345 | wxLogNull log; | |
346 | if (!wxMkdir(path)) | |
347 | { | |
348 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
349 | dialog.ShowModal(); | |
350 | return; | |
351 | } | |
352 | ||
353 | wxDirItemData *new_data = new wxDirItemData( path, new_name, true ); | |
354 | ||
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 ); | |
360 | } | |
361 | ||
362 | void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event)) | |
363 | { | |
364 | SetPath(wxGetUserHome()); | |
365 | } | |
366 | ||
367 | #endif // wxUSE_DIRDLG |