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