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