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