Adjustement to the small display of Smartphones.
[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_OK = 1002;
51 static const int ID_CANCEL = 1003;
52 static const int ID_NEW = 1004;
53 static const int ID_SHOW_HIDDEN = 1005;
54 static const int ID_GO_HOME = 1006;
55
56 // ---------------------------------------------------------------------------
57 // macros
58 // ---------------------------------------------------------------------------
59
60 /* Macro for avoiding #ifdefs when value have to be different depending on size of
61 device we display on
62 */
63
64 #if defined(__SMARTPHONE__)
65 #define wxLARGESMALL(large,small) small
66 #else
67 #define wxLARGESMALL(large,small) large
68 #endif
69
70 //-----------------------------------------------------------------------------
71 // wxGenericDirDialog
72 //-----------------------------------------------------------------------------
73
74 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog)
75
76 BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
77 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
78 EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK)
79 EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew)
80 EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome)
81 EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown)
82 EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected)
83 EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK)
84 EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden)
85 END_EVENT_TABLE()
86
87 wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title,
88 const wxString& defaultPath, long style,
89 const wxPoint& pos, const wxSize& sz,
90 const wxString& name):
91 wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name)
92 {
93 wxBusyCursor cursor;
94
95 m_path = defaultPath;
96 if (m_path == wxT("~"))
97 wxGetHomeDir(&m_path);
98 if (m_path == wxT("."))
99 m_path = wxGetCwd();
100
101 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
102
103 // Smartphone does not have buttons
104 #if defined(__SMARTPHONE__)
105
106 // FIXME: make 'new dir' and 'home' in local dialog menu
107
108 #else
109
110 // 0) 'New' and 'Home' Buttons
111 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
112
113 // VS: 'Home directory' concept is unknown to MS-DOS
114 #if !defined(__DOS__)
115 wxBitmapButton* homeButton =
116 new wxBitmapButton(this, ID_GO_HOME,
117 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
118 buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 );
119 #endif
120
121 // I'm not convinced we need a New button, and we tend to get annoying
122 // accidental-editing with label editing enabled.
123 if (style & wxDD_NEW_DIR_BUTTON)
124 {
125 wxBitmapButton* newButton =
126 new wxBitmapButton(this, ID_NEW,
127 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
128 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
129 #if wxUSE_TOOLTIPS
130 newButton->SetToolTip(_("Create new directory"));
131 #endif
132 }
133
134 #if wxUSE_TOOLTIPS
135 homeButton->SetToolTip(_("Go to home directory"));
136 #endif
137
138 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
139
140 #endif // __SMARTPHONE__/!__SMARTPHONE__
141
142 // 1) dir ctrl
143 m_dirCtrl = NULL; // this is neccessary, event handler called from
144 // wxGenericDirCtrl would crash otherwise!
145 long dirStyle = wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER;
146
147 #ifdef __WXMSW__
148 if (style & wxDD_NEW_DIR_BUTTON)
149 {
150 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
151 // before we can call EditLabel (required for "New directory")
152 dirStyle |= wxDIRCTRL_EDIT_LABELS;
153 }
154 #endif
155
156 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
157 m_path, wxDefaultPosition,
158 wxSize(300, 200),
159 dirStyle);
160
161 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
162
163 #ifndef __SMARTPHONE__
164 // Make the an option depending on a flag?
165 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
166 topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 );
167 #endif // !__SMARTPHONE__
168
169 // 2) text ctrl
170 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
171 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
172
173 #ifdef __SMARTPHONE__
174
175 SetRightMenu(wxID_CANCEL, _("Cancel"));
176
177 #else // __SMARTPHONE__/!__SMARTPHONE__
178
179 #if wxUSE_STATLINE
180 // 3) Static line
181 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
182 #endif
183
184 // 4) Buttons
185 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
186
187 // OK and Cancel button should be at the right bottom
188 wxButton* okButton = new wxButton(this, wxID_OK);
189 buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 );
190 wxButton* cancelButton = new wxButton(this, wxID_CANCEL);
191 buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 );
192
193 topsizer->Add( buttonsizer, 0, wxLEFT|wxTOP|wxBOTTOM | wxALIGN_RIGHT, 10 );
194
195 okButton->SetDefault();
196
197 #endif // !__SMARTPHONE__
198
199 m_dirCtrl->SetFocus();
200
201 SetAutoLayout( true );
202 SetSizer( topsizer );
203
204 topsizer->SetSizeHints( this );
205 topsizer->Fit( this );
206
207 Centre( wxBOTH );
208 }
209
210 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
211 {
212 EndModal(wxID_CANCEL);
213 }
214
215 void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
216 {
217 m_path = m_input->GetValue();
218 // Does the path exist? (User may have typed anything in m_input)
219 if (wxPathExists(m_path)) {
220 // OK, path exists, we're done.
221 EndModal(wxID_OK);
222 return;
223 }
224 // Interact with user, find out if the dir is a typo or to be created
225 wxString msg;
226 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
227 m_path.c_str());
228 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
229 wxYES_NO | wxICON_WARNING);
230
231 if ( dialog.ShowModal() == wxID_YES ) {
232 // Okay, let's make it
233 wxLogNull log;
234 if (wxMkdir(m_path)) {
235 // The new dir was created okay.
236 EndModal(wxID_OK);
237 return;
238 }
239 else {
240 // Trouble...
241 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
242 m_path.c_str());
243 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
244 errmsg.ShowModal();
245 // We still don't have a valid dir. Back to the main dialog.
246 }
247 }
248 // User has answered NO to create dir.
249 }
250
251 void wxGenericDirDialog::SetPath(const wxString& path)
252 {
253 m_dirCtrl->SetPath(path);
254 m_path = path;
255 }
256
257 wxString wxGenericDirDialog::GetPath(void) const
258 {
259 return m_path;
260 }
261
262 int wxGenericDirDialog::ShowModal()
263 {
264 m_input->SetValue( m_path );
265 return wxDialog::ShowModal();
266 }
267
268 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
269 {
270 if (!m_dirCtrl)
271 return;
272
273 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
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 (wxFileExists(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 (wxFileExists(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