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