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