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