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