wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[wxWidgets.git] / src / generic / dirdlgg.cpp
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 wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name)
83 {
84 wxBusyCursor cursor;
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 (style & wxDD_NEW_DIR_BUTTON)
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 (style & wxDD_NEW_DIR_BUTTON)
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 (style & wxDD_NEW_DIR_BUTTON)
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 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
163
164 #ifndef __SMARTPHONE__
165 // Make the an option depending on a flag?
166 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
167 topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 );
168 #endif // !__SMARTPHONE__
169
170 // 2) text ctrl
171 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
172 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
173
174 // 3) buttons if any
175 wxSizer *buttonSizer = CreateButtonSizer( wxOK|wxCANCEL , true, wxLARGESMALL(10,0) );
176 if(buttonSizer->GetChildren().GetCount() > 0 )
177 {
178 topsizer->Add( buttonSizer, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
179 }
180 else
181 {
182 topsizer->AddSpacer( wxLARGESMALL(10,0) );
183 delete buttonSizer;
184 }
185
186 #ifdef __SMARTPHONE__
187 // overwrite menu achieved with earlier CreateButtonSizer() call
188 SetRightMenu(wxID_ANY, _("Options"), dirMenu);
189 #endif
190
191 m_input->SetFocus();
192
193 SetAutoLayout( true );
194 SetSizer( topsizer );
195
196 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
197 topsizer->SetSizeHints( this );
198 topsizer->Fit( this );
199
200 Centre( wxBOTH );
201 #endif
202 }
203
204 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
205 {
206 EndModal(wxID_CANCEL);
207 }
208
209 void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
210 {
211 m_path = m_input->GetValue();
212 // Does the path exist? (User may have typed anything in m_input)
213 if (wxDirExists(m_path)) {
214 // OK, path exists, we're done.
215 EndModal(wxID_OK);
216 return;
217 }
218 // Interact with user, find out if the dir is a typo or to be created
219 wxString msg;
220 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
221 m_path.c_str());
222 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
223 wxYES_NO | wxICON_WARNING);
224
225 if ( dialog.ShowModal() == wxID_YES ) {
226 // Okay, let's make it
227 wxLogNull log;
228 if (wxMkdir(m_path)) {
229 // The new dir was created okay.
230 EndModal(wxID_OK);
231 return;
232 }
233 else {
234 // Trouble...
235 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
236 m_path.c_str());
237 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
238 errmsg.ShowModal();
239 // We still don't have a valid dir. Back to the main dialog.
240 }
241 }
242 // User has answered NO to create dir.
243 }
244
245 void wxGenericDirDialog::SetPath(const wxString& path)
246 {
247 m_dirCtrl->SetPath(path);
248 m_path = path;
249 }
250
251 wxString wxGenericDirDialog::GetPath(void) const
252 {
253 return m_path;
254 }
255
256 int wxGenericDirDialog::ShowModal()
257 {
258 m_input->SetValue( m_path );
259 return wxDialog::ShowModal();
260 }
261
262 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
263 {
264 if (!m_dirCtrl)
265 return;
266
267 wxTreeItemId item = event.GetItem();
268
269 wxDirItemData *data = NULL;
270
271 if(item.IsOk())
272 data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item);
273
274 if (data)
275 m_input->SetValue( data->m_path );
276 }
277
278 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
279 {
280 if (!m_dirCtrl)
281 return;
282
283 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
284 if (data)
285 m_input->SetValue( data->m_path );
286 }
287
288 void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
289 {
290 if (!m_dirCtrl)
291 return;
292
293 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
294 }
295
296 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
297 {
298 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
299 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
300 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
301 {
302 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
303 _("Create directory"), wxOK | wxICON_INFORMATION );
304 msg.ShowModal();
305 return;
306 }
307
308 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
309 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
310 wxASSERT( data );
311
312 wxString new_name( _("NewName") );
313 wxString path( data->m_path );
314 if (!wxEndsWithPathSeparator(path))
315 path += wxFILE_SEP_PATH;
316 path += new_name;
317 if (wxDirExists(path))
318 {
319 // try NewName0, NewName1 etc.
320 int i = 0;
321 do {
322 new_name = _("NewName");
323 wxString num;
324 num.Printf( wxT("%d"), i );
325 new_name += num;
326
327 path = data->m_path;
328 if (!wxEndsWithPathSeparator(path))
329 path += wxFILE_SEP_PATH;
330 path += new_name;
331 i++;
332 } while (wxDirExists(path));
333 }
334
335 wxLogNull log;
336 if (!wxMkdir(path))
337 {
338 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
339 dialog.ShowModal();
340 return;
341 }
342
343 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
344
345 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
346 // of the parent.
347 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
348 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
349 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
350 }
351
352 void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
353 {
354 SetPath(wxGetUserHome());
355 }
356
357 #endif // wxUSE_DIRDLG