]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
removed useless colMapAllocCounter (patch 1413456)
[wxWidgets.git] / src / generic / dirdlgg.cpp
CommitLineData
d7a15103 1/////////////////////////////////////////////////////////////////////////////
897b24cf 2// Name: src/generic/dirdlg.cpp
d7a15103 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
897b24cf 96 // smartphones 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
0d1f53ca
WS
111#else
112
2a0951be
VS
113 // 0) 'New' and 'Home' Buttons
114 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
115
e13ea14e 116 // VS: 'Home directory' concept is unknown to MS-DOS
0d1f53ca 117#if !defined(__DOS__)
ca65c044 118 wxBitmapButton* homeButton =
2a0951be 119 new wxBitmapButton(this, ID_GO_HOME,
36668b35 120 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
2a0951be 121 buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 );
e13ea14e 122#endif
ca65c044 123
2a0951be
VS
124 // I'm not convinced we need a New button, and we tend to get annoying
125 // accidental-editing with label editing enabled.
dabd1377
JS
126 if (style & wxDD_NEW_DIR_BUTTON)
127 {
ca65c044 128 wxBitmapButton* newButton =
dabd1377 129 new wxBitmapButton(this, ID_NEW,
36668b35 130 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
dabd1377
JS
131 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
132#if wxUSE_TOOLTIPS
133 newButton->SetToolTip(_("Create new directory"));
ca65c044 134#endif
dabd1377 135 }
2a0951be
VS
136
137#if wxUSE_TOOLTIPS
138 homeButton->SetToolTip(_("Go to home directory"));
2a0951be
VS
139#endif
140
141 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
142
0d1f53ca
WS
143#endif // __SMARTPHONE__/!__SMARTPHONE__
144
dc6c62a9 145 // 1) dir ctrl
3103e8a9 146 m_dirCtrl = NULL; // this is necessary, event handler called from
748fcded 147 // wxGenericDirCtrl would crash otherwise!
03775239 148 long dirStyle = wxDIRCTRL_DIR_ONLY | wxDEFAULT_CONTROL_BORDER;
ca65c044 149
dabd1377
JS
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 }
ca65c044 157#endif
dabd1377 158
748fcded 159 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
488eb04e 160 m_path, wxDefaultPosition,
ca65c044 161 wxSize(300, 200),
dabd1377 162 dirStyle);
748fcded 163
0d1f53ca 164 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
e90c1d2a 165
0d1f53ca 166#ifndef __SMARTPHONE__
ead95817
VS
167 // Make the an option depending on a flag?
168 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
488eb04e 169 topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 );
0d1f53ca 170#endif // !__SMARTPHONE__
ead95817 171
dc6c62a9 172 // 2) text ctrl
e90c1d2a 173 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
0d1f53ca
WS
174 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
175
897b24cf
WS
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 }
e90c1d2a 187
897b24cf
WS
188#ifdef __SMARTPHONE__
189 // overwrite menu achieved with earlier CreateButtonSizer() call
190 SetRightMenu(wxID_ANY, _("Options"), dirMenu);
dc6c62a9 191#endif
d7a15103 192
9026d6fd 193 m_input->SetFocus();
e90c1d2a 194
ca65c044 195 SetAutoLayout( true );
dc6c62a9 196 SetSizer( topsizer );
e90c1d2a 197
03775239 198#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
dc6c62a9
RR
199 topsizer->SetSizeHints( this );
200 topsizer->Fit( this );
201
202 Centre( wxBOTH );
03775239 203#endif
748fcded 204}
dc6c62a9 205
748fcded
VS
206void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
207{
208 EndModal(wxID_CANCEL);
209}
2a4dbd81 210
748fcded
VS
211void 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)
da865fdd 215 if (wxDirExists(m_path)) {
748fcded
VS
216 // OK, path exists, we're done.
217 EndModal(wxID_OK);
218 return;
f6bcfd97 219 }
748fcded
VS
220 // Interact with user, find out if the dir is a typo or to be created
221 wxString msg;
ca65c044 222 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
748fcded 223 m_path.c_str());
ca65c044 224 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
748fcded
VS
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...
ca65c044 237 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
748fcded
VS
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.
f6bcfd97 242 }
f6bcfd97 243 }
748fcded
VS
244 // User has answered NO to create dir.
245}
f6bcfd97 246
748fcded
VS
247void wxGenericDirDialog::SetPath(const wxString& path)
248{
249 m_dirCtrl->SetPath(path);
250 m_path = path;
251}
f6bcfd97 252
748fcded
VS
253wxString wxGenericDirDialog::GetPath(void) const
254{
255 return m_path;
d7a15103
JS
256}
257
748fcded 258int wxGenericDirDialog::ShowModal()
d7a15103 259{
dc6c62a9
RR
260 m_input->SetValue( m_path );
261 return wxDialog::ShowModal();
d7a15103
JS
262}
263
748fcded 264void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 265{
748fcded
VS
266 if (!m_dirCtrl)
267 return;
268
44d60c0b
WS
269 wxTreeItemId item = event.GetItem();
270
271 wxDirItemData *data = NULL;
272
273 if(item.IsOk())
274 data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item);
275
e90c1d2a 276 if (data)
dc6c62a9 277 m_input->SetValue( data->m_path );
17a1ebd1 278}
d7a15103 279
748fcded 280void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 281{
748fcded
VS
282 if (!m_dirCtrl)
283 return;
284
285 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 286 if (data)
dc6c62a9 287 m_input->SetValue( data->m_path );
17a1ebd1 288}
d7a15103 289
42dcacf0
RR
290void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
291{
292 if (!m_dirCtrl)
293 return;
294
2b5f62a0 295 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
42dcacf0
RR
296}
297
748fcded 298void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 299{
748fcded
VS
300 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
301 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
99006e44 302 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 303 {
e90c1d2a
VZ
304 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
305 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
306 msg.ShowModal();
307 return;
308 }
d7a15103 309
99006e44 310 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
748fcded 311 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 312 wxASSERT( data );
e90c1d2a 313
748fcded 314 wxString new_name( _("NewName") );
dc6c62a9 315 wxString path( data->m_path );
083f7497 316 if (!wxEndsWithPathSeparator(path))
748fcded 317 path += wxFILE_SEP_PATH;
dc6c62a9 318 path += new_name;
da865fdd 319 if (wxDirExists(path))
dc6c62a9
RR
320 {
321 // try NewName0, NewName1 etc.
322 int i = 0;
f6bcfd97 323 do {
748fcded 324 new_name = _("NewName");
f6bcfd97
BP
325 wxString num;
326 num.Printf( wxT("%d"), i );
327 new_name += num;
e90c1d2a 328
dc6c62a9 329 path = data->m_path;
083f7497 330 if (!wxEndsWithPathSeparator(path))
748fcded 331 path += wxFILE_SEP_PATH;
dc6c62a9 332 path += new_name;
f6bcfd97 333 i++;
da865fdd 334 } while (wxDirExists(path));
d7a15103 335 }
e90c1d2a 336
dc6c62a9 337 wxLogNull log;
e90c1d2a 338 if (!wxMkdir(path))
dc6c62a9
RR
339 {
340 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 341 dialog.ShowModal();
dc6c62a9
RR
342 return;
343 }
344
ca65c044 345 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
d7a15103 346
748fcded
VS
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 );
d7a15103 352}
ce4169a4 353
e13ea14e
VS
354void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
355{
356 SetPath(wxGetUserHome());
357}
358
f6bcfd97 359#endif // wxUSE_DIRDLG