]>
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 | #include "wx/bmpbuttn.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/statline.h" | |
33 | #include "wx/dirctrl.h" | |
34 | #include "wx/generic/dirdlgg.h" | |
35 | #include "wx/artprov.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 | // wxGenericDirDialog | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog) | |
52 | ||
53 | BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog) | |
54 | EVT_CLOSE (wxGenericDirDialog::OnCloseWindow) | |
55 | EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK) | |
56 | EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew) | |
57 | EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome) | |
58 | EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown) | |
59 | EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected) | |
60 | EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK) | |
61 | EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden) | |
62 | END_EVENT_TABLE() | |
63 | ||
64 | wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title, | |
65 | const wxString& defaultPath, long style, | |
66 | const wxPoint& pos, const wxSize& sz, | |
67 | const wxString& name) | |
68 | { | |
69 | Create(parent, title, defaultPath, style, pos, sz, name); | |
70 | } | |
71 | ||
72 | bool wxGenericDirDialog::Create(wxWindow* parent, | |
73 | const wxString& title, | |
74 | const wxString& defaultPath, long style, | |
75 | const wxPoint& pos, | |
76 | const wxSize& sz, | |
77 | const wxString& name) | |
78 | { | |
79 | wxBusyCursor cursor; | |
80 | ||
81 | parent = GetParentForModalDialog(parent); | |
82 | ||
83 | if (!wxDirDialogBase::Create(parent, title, defaultPath, style, pos, sz, name)) | |
84 | return false; | |
85 | ||
86 | m_path = defaultPath; | |
87 | if (m_path == wxT("~")) | |
88 | wxGetHomeDir(&m_path); | |
89 | if (m_path == wxT(".")) | |
90 | m_path = wxGetCwd(); | |
91 | ||
92 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
93 | ||
94 | // smartphones does not support or do not waste space for wxButtons | |
95 | #if defined(__SMARTPHONE__) | |
96 | ||
97 | wxMenu *dirMenu = new wxMenu; | |
98 | dirMenu->Append(ID_GO_HOME, _("Home")); | |
99 | ||
100 | if (!HasFlag(wxDD_DIR_MUST_EXIST)) | |
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 | ||
109 | #else | |
110 | ||
111 | // 0) 'New' and 'Home' Buttons | |
112 | wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL ); | |
113 | ||
114 | // VS: 'Home directory' concept is unknown to MS-DOS | |
115 | #if !defined(__DOS__) | |
116 | wxBitmapButton* homeButton = | |
117 | new wxBitmapButton(this, ID_GO_HOME, | |
118 | wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON)); | |
119 | buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 ); | |
120 | #endif | |
121 | ||
122 | // I'm not convinced we need a New button, and we tend to get annoying | |
123 | // accidental-editing with label editing enabled. | |
124 | if (!HasFlag(wxDD_DIR_MUST_EXIST)) | |
125 | { | |
126 | wxBitmapButton* newButton = | |
127 | new wxBitmapButton(this, ID_NEW, | |
128 | wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON)); | |
129 | buttonsizer->Add( newButton, 0, wxRIGHT, 10 ); | |
130 | #if wxUSE_TOOLTIPS | |
131 | newButton->SetToolTip(_("Create new directory")); | |
132 | #endif | |
133 | } | |
134 | ||
135 | #if wxUSE_TOOLTIPS | |
136 | homeButton->SetToolTip(_("Go to home directory")); | |
137 | #endif | |
138 | ||
139 | topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 ); | |
140 | ||
141 | #endif // __SMARTPHONE__/!__SMARTPHONE__ | |
142 | ||
143 | // 1) dir ctrl | |
144 | m_dirCtrl = NULL; // this is necessary, event handler called from | |
145 | // wxGenericDirCtrl would crash otherwise! | |
146 | long dirStyle = wxDIRCTRL_DIR_ONLY | wxDEFAULT_CONTROL_BORDER; | |
147 | ||
148 | #ifdef __WXMSW__ | |
149 | if (!HasFlag(wxDD_DIR_MUST_EXIST)) | |
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 | } | |
155 | #endif | |
156 | ||
157 | m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL, | |
158 | m_path, wxDefaultPosition, | |
159 | wxSize(300, 200), | |
160 | dirStyle); | |
161 | ||
162 | wxSizerFlags flagsBorder2; | |
163 | flagsBorder2.DoubleBorder(wxTOP | wxLEFT | wxRIGHT); | |
164 | ||
165 | topsizer->Add(m_dirCtrl, wxSizerFlags(flagsBorder2).Proportion(1).Expand()); | |
166 | ||
167 | #ifndef __SMARTPHONE__ | |
168 | // Make the an option depending on a flag? | |
169 | wxCheckBox * | |
170 | check = new wxCheckBox(this, ID_SHOW_HIDDEN, _("Show &hidden directories")); | |
171 | topsizer->Add(check, wxSizerFlags(flagsBorder2).Right()); | |
172 | #endif // !__SMARTPHONE__ | |
173 | ||
174 | // 2) text ctrl | |
175 | m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition ); | |
176 | topsizer->Add(m_input, wxSizerFlags(flagsBorder2).Expand()); | |
177 | ||
178 | // 3) buttons if any | |
179 | wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL); | |
180 | if ( buttonSizer ) | |
181 | { | |
182 | topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder()); | |
183 | } | |
184 | ||
185 | #ifdef __SMARTPHONE__ | |
186 | // overwrite menu set by CreateSeparatedButtonSizer() call above | |
187 | SetRightMenu(wxID_ANY, _("Options"), dirMenu); | |
188 | #endif | |
189 | ||
190 | m_input->SetFocus(); | |
191 | ||
192 | SetAutoLayout( true ); | |
193 | SetSizer( topsizer ); | |
194 | ||
195 | topsizer->SetSizeHints( this ); | |
196 | topsizer->Fit( this ); | |
197 | ||
198 | Centre( wxBOTH ); | |
199 | ||
200 | return true; | |
201 | } | |
202 | ||
203 | void 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 | ||
212 | void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
213 | { | |
214 | EndModal(wxID_CANCEL); | |
215 | } | |
216 | ||
217 | void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
218 | { | |
219 | m_path = m_input->GetValue(); | |
220 | ||
221 | // Does the path exist? (User may have typed anything in m_input) | |
222 | if (wxDirExists(m_path)) | |
223 | { | |
224 | // OK, path exists, we're done. | |
225 | EndModal(wxID_OK); | |
226 | return; | |
227 | } | |
228 | ||
229 | // Interact with user, find out if the dir is a typo or to be created | |
230 | wxString msg; | |
231 | msg.Printf(_("The directory '%s' does not exist\nCreate it now?"), | |
232 | m_path.c_str()); | |
233 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), | |
234 | wxYES_NO | wxICON_WARNING); | |
235 | ||
236 | if ( dialog.ShowModal() == wxID_YES ) | |
237 | { | |
238 | // Okay, let's make it | |
239 | wxLogNull log; | |
240 | if (wxMkdir(m_path)) | |
241 | { | |
242 | // The new dir was created okay. | |
243 | EndModal(wxID_OK); | |
244 | return; | |
245 | } | |
246 | else | |
247 | { | |
248 | // Trouble... | |
249 | msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"), | |
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. | |
254 | } | |
255 | } | |
256 | // User has answered NO to create dir. | |
257 | } | |
258 | ||
259 | void wxGenericDirDialog::SetPath(const wxString& path) | |
260 | { | |
261 | m_dirCtrl->SetPath(path); | |
262 | m_path = path; | |
263 | } | |
264 | ||
265 | wxString wxGenericDirDialog::GetPath(void) const | |
266 | { | |
267 | return m_path; | |
268 | } | |
269 | ||
270 | int wxGenericDirDialog::ShowModal() | |
271 | { | |
272 | m_input->SetValue( m_path ); | |
273 | return wxDialog::ShowModal(); | |
274 | } | |
275 | ||
276 | void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event ) | |
277 | { | |
278 | if (!m_dirCtrl) | |
279 | return; | |
280 | ||
281 | wxTreeItemId item = event.GetItem(); | |
282 | ||
283 | wxDirItemData *data = NULL; | |
284 | ||
285 | if(item.IsOk()) | |
286 | data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item); | |
287 | ||
288 | if (data) | |
289 | m_input->SetValue( data->m_path ); | |
290 | } | |
291 | ||
292 | void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) ) | |
293 | { | |
294 | if (!m_dirCtrl) | |
295 | return; | |
296 | ||
297 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection()); | |
298 | if (data) | |
299 | m_input->SetValue( data->m_path ); | |
300 | } | |
301 | ||
302 | void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event ) | |
303 | { | |
304 | if (!m_dirCtrl) | |
305 | return; | |
306 | ||
307 | m_dirCtrl->ShowHidden( event.GetInt() != 0 ); | |
308 | } | |
309 | ||
310 | void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) ) | |
311 | { | |
312 | wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection(); | |
313 | if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) || | |
314 | (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem())) | |
315 | { | |
316 | wxMessageDialog msg(this, _("You cannot add a new directory to this section."), | |
317 | _("Create directory"), wxOK | wxICON_INFORMATION ); | |
318 | msg.ShowModal(); | |
319 | return; | |
320 | } | |
321 | ||
322 | wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id ); | |
323 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent ); | |
324 | wxASSERT( data ); | |
325 | ||
326 | wxString new_name( _("NewName") ); | |
327 | wxString path( data->m_path ); | |
328 | if (!wxEndsWithPathSeparator(path)) | |
329 | path += wxFILE_SEP_PATH; | |
330 | path += new_name; | |
331 | if (wxDirExists(path)) | |
332 | { | |
333 | // try NewName0, NewName1 etc. | |
334 | int i = 0; | |
335 | do { | |
336 | new_name = _("NewName"); | |
337 | wxString num; | |
338 | num.Printf( wxT("%d"), i ); | |
339 | new_name += num; | |
340 | ||
341 | path = data->m_path; | |
342 | if (!wxEndsWithPathSeparator(path)) | |
343 | path += wxFILE_SEP_PATH; | |
344 | path += new_name; | |
345 | i++; | |
346 | } while (wxDirExists(path)); | |
347 | } | |
348 | ||
349 | wxLogNull log; | |
350 | if (!wxMkdir(path)) | |
351 | { | |
352 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
353 | dialog.ShowModal(); | |
354 | return; | |
355 | } | |
356 | ||
357 | wxDirItemData *new_data = new wxDirItemData( path, new_name, true ); | |
358 | ||
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 ); | |
364 | } | |
365 | ||
366 | void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event)) | |
367 | { | |
368 | SetPath(wxGetUserHome()); | |
369 | } | |
370 | ||
371 | #endif // wxUSE_DIRDLG |