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