No more avoiding wxSizer::Fit in wxWinCE builds.
[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 topsizer->SetSizeHints( this );
197 topsizer->Fit( this );
198
199 Centre( wxBOTH );
200 }
201
202 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
203 {
204 EndModal(wxID_CANCEL);
205 }
206
207 void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
208 {
209 m_path = m_input->GetValue();
210 // Does the path exist? (User may have typed anything in m_input)
211 if (wxDirExists(m_path)) {
212 // OK, path exists, we're done.
213 EndModal(wxID_OK);
214 return;
215 }
216 // Interact with user, find out if the dir is a typo or to be created
217 wxString msg;
218 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
219 m_path.c_str());
220 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
221 wxYES_NO | wxICON_WARNING);
222
223 if ( dialog.ShowModal() == wxID_YES ) {
224 // Okay, let's make it
225 wxLogNull log;
226 if (wxMkdir(m_path)) {
227 // The new dir was created okay.
228 EndModal(wxID_OK);
229 return;
230 }
231 else {
232 // Trouble...
233 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
234 m_path.c_str());
235 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
236 errmsg.ShowModal();
237 // We still don't have a valid dir. Back to the main dialog.
238 }
239 }
240 // User has answered NO to create dir.
241 }
242
243 void wxGenericDirDialog::SetPath(const wxString& path)
244 {
245 m_dirCtrl->SetPath(path);
246 m_path = path;
247 }
248
249 wxString wxGenericDirDialog::GetPath(void) const
250 {
251 return m_path;
252 }
253
254 int wxGenericDirDialog::ShowModal()
255 {
256 m_input->SetValue( m_path );
257 return wxDialog::ShowModal();
258 }
259
260 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
261 {
262 if (!m_dirCtrl)
263 return;
264
265 wxTreeItemId item = event.GetItem();
266
267 wxDirItemData *data = NULL;
268
269 if(item.IsOk())
270 data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item);
271
272 if (data)
273 m_input->SetValue( data->m_path );
274 }
275
276 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
277 {
278 if (!m_dirCtrl)
279 return;
280
281 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
282 if (data)
283 m_input->SetValue( data->m_path );
284 }
285
286 void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
287 {
288 if (!m_dirCtrl)
289 return;
290
291 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
292 }
293
294 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
295 {
296 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
297 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
298 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
299 {
300 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
301 _("Create directory"), wxOK | wxICON_INFORMATION );
302 msg.ShowModal();
303 return;
304 }
305
306 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
307 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
308 wxASSERT( data );
309
310 wxString new_name( _("NewName") );
311 wxString path( data->m_path );
312 if (!wxEndsWithPathSeparator(path))
313 path += wxFILE_SEP_PATH;
314 path += new_name;
315 if (wxDirExists(path))
316 {
317 // try NewName0, NewName1 etc.
318 int i = 0;
319 do {
320 new_name = _("NewName");
321 wxString num;
322 num.Printf( wxT("%d"), i );
323 new_name += num;
324
325 path = data->m_path;
326 if (!wxEndsWithPathSeparator(path))
327 path += wxFILE_SEP_PATH;
328 path += new_name;
329 i++;
330 } while (wxDirExists(path));
331 }
332
333 wxLogNull log;
334 if (!wxMkdir(path))
335 {
336 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
337 dialog.ShowModal();
338 return;
339 }
340
341 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
342
343 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
344 // of the parent.
345 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
346 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
347 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
348 }
349
350 void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
351 {
352 SetPath(wxGetUserHome());
353 }
354
355 #endif // wxUSE_DIRDLG