]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
for built-in dialogs, there is no m_peer instance, and still we might have a Refresh...
[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
748fcded
VS
44//-----------------------------------------------------------------------------
45// wxGenericDirDialog
46//-----------------------------------------------------------------------------
a17e237f 47
d6379fa3
MB
48IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog)
49
d7a15103
JS
50static const int ID_DIRCTRL = 1000;
51static const int ID_TEXTCTRL = 1001;
52static const int ID_OK = 1002;
53static const int ID_CANCEL = 1003;
54static const int ID_NEW = 1004;
42dcacf0 55static const int ID_SHOW_HIDDEN = 1005;
2a0951be 56static const int ID_GO_HOME = 1006;
d7a15103 57
748fcded 58BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
748fcded 59 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
42dcacf0
RR
60 EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK)
61 EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew)
e13ea14e 62 EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome)
ca65c044
WS
63 EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown)
64 EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected)
42dcacf0
RR
65 EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK)
66 EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden)
d7a15103
JS
67END_EVENT_TABLE()
68
748fcded
VS
69wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title,
70 const wxString& defaultPath, long style,
ca65c044 71 const wxPoint& pos, const wxSize& sz,
748fcded
VS
72 const wxString& name):
73 wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name)
d7a15103 74{
748fcded 75 wxBusyCursor cursor;
e90c1d2a 76
dc6c62a9 77 m_path = defaultPath;
748fcded 78 if (m_path == wxT("~"))
ab8d2b02
VS
79 wxGetHomeDir(&m_path);
80 if (m_path == wxT("."))
81 m_path = wxGetCwd();
e90c1d2a 82
dc6c62a9
RR
83 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
84
2a0951be
VS
85 // 0) 'New' and 'Home' Buttons
86 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
87
e13ea14e
VS
88 // VS: 'Home directory' concept is unknown to MS-DOS
89#ifndef __DOS__
ca65c044 90 wxBitmapButton* homeButton =
2a0951be
VS
91 new wxBitmapButton(this, ID_GO_HOME,
92 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
93 buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 );
e13ea14e 94#endif
ca65c044 95
2a0951be
VS
96 // I'm not convinced we need a New button, and we tend to get annoying
97 // accidental-editing with label editing enabled.
dabd1377
JS
98 if (style & wxDD_NEW_DIR_BUTTON)
99 {
ca65c044 100 wxBitmapButton* newButton =
dabd1377
JS
101 new wxBitmapButton(this, ID_NEW,
102 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
103 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
104#if wxUSE_TOOLTIPS
105 newButton->SetToolTip(_("Create new directory"));
ca65c044 106#endif
dabd1377 107 }
2a0951be
VS
108
109#if wxUSE_TOOLTIPS
110 homeButton->SetToolTip(_("Go to home directory"));
2a0951be
VS
111#endif
112
113 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
114
dc6c62a9 115 // 1) dir ctrl
ca65c044 116 m_dirCtrl = NULL; // this is neccessary, event handler called from
748fcded 117 // wxGenericDirCtrl would crash otherwise!
dabd1377 118 long dirStyle = wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER;
ca65c044 119
dabd1377
JS
120#ifdef __WXMSW__
121 if (style & wxDD_NEW_DIR_BUTTON)
122 {
123 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
124 // before we can call EditLabel (required for "New directory")
125 dirStyle |= wxDIRCTRL_EDIT_LABELS;
126 }
ca65c044 127#endif
dabd1377 128
748fcded 129 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
488eb04e 130 m_path, wxDefaultPosition,
ca65c044 131 wxSize(300, 200),
dabd1377 132 dirStyle);
748fcded
VS
133
134 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
e90c1d2a 135
ead95817
VS
136 // Make the an option depending on a flag?
137 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
488eb04e 138 topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 );
ead95817 139
dc6c62a9 140 // 2) text ctrl
e90c1d2a 141 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
dc6c62a9 142 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
e90c1d2a 143
dc6c62a9 144#if wxUSE_STATLINE
748fcded 145 // 3) Static line
ca65c044 146 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
dc6c62a9 147#endif
d7a15103 148
748fcded 149 // 4) Buttons
2a0951be 150 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
ca65c044 151
42dcacf0
RR
152 // OK and Cancel button should be at the right bottom
153 wxButton* okButton = new wxButton(this, wxID_OK, _("OK"));
154 buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 );
155 wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
156 buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 );
157
488eb04e 158 topsizer->Add( buttonsizer, 0, wxLEFT|wxTOP|wxBOTTOM | wxALIGN_RIGHT, 10 );
dc6c62a9 159
748fcded
VS
160 okButton->SetDefault();
161 m_dirCtrl->SetFocus();
e90c1d2a 162
ca65c044 163 SetAutoLayout( true );
dc6c62a9 164 SetSizer( topsizer );
e90c1d2a 165
dc6c62a9
RR
166 topsizer->SetSizeHints( this );
167 topsizer->Fit( this );
168
169 Centre( wxBOTH );
748fcded 170}
dc6c62a9 171
748fcded
VS
172void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
173{
174 EndModal(wxID_CANCEL);
175}
2a4dbd81 176
748fcded
VS
177void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
178{
179 m_path = m_input->GetValue();
180 // Does the path exist? (User may have typed anything in m_input)
181 if (wxPathExists(m_path)) {
182 // OK, path exists, we're done.
183 EndModal(wxID_OK);
184 return;
f6bcfd97 185 }
748fcded
VS
186 // Interact with user, find out if the dir is a typo or to be created
187 wxString msg;
ca65c044 188 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
748fcded 189 m_path.c_str());
ca65c044 190 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
748fcded
VS
191 wxYES_NO | wxICON_WARNING);
192
193 if ( dialog.ShowModal() == wxID_YES ) {
194 // Okay, let's make it
195 wxLogNull log;
196 if (wxMkdir(m_path)) {
197 // The new dir was created okay.
198 EndModal(wxID_OK);
199 return;
200 }
201 else {
202 // Trouble...
ca65c044 203 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
748fcded
VS
204 m_path.c_str());
205 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
206 errmsg.ShowModal();
207 // We still don't have a valid dir. Back to the main dialog.
f6bcfd97 208 }
f6bcfd97 209 }
748fcded
VS
210 // User has answered NO to create dir.
211}
f6bcfd97 212
748fcded
VS
213void wxGenericDirDialog::SetPath(const wxString& path)
214{
215 m_dirCtrl->SetPath(path);
216 m_path = path;
217}
f6bcfd97 218
748fcded
VS
219wxString wxGenericDirDialog::GetPath(void) const
220{
221 return m_path;
d7a15103
JS
222}
223
748fcded 224int wxGenericDirDialog::ShowModal()
d7a15103 225{
dc6c62a9
RR
226 m_input->SetValue( m_path );
227 return wxDialog::ShowModal();
d7a15103
JS
228}
229
748fcded 230void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 231{
748fcded
VS
232 if (!m_dirCtrl)
233 return;
234
235 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
e90c1d2a 236 if (data)
dc6c62a9 237 m_input->SetValue( data->m_path );
d7a15103
JS
238};
239
748fcded 240void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 241{
748fcded
VS
242 if (!m_dirCtrl)
243 return;
244
245 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 246 if (data)
dc6c62a9 247 m_input->SetValue( data->m_path );
d7a15103
JS
248};
249
42dcacf0
RR
250void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
251{
252 if (!m_dirCtrl)
253 return;
254
2b5f62a0 255 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
42dcacf0
RR
256}
257
748fcded 258void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 259{
748fcded
VS
260 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
261 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
99006e44 262 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 263 {
e90c1d2a
VZ
264 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
265 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
266 msg.ShowModal();
267 return;
268 }
d7a15103 269
99006e44 270 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
748fcded 271 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 272 wxASSERT( data );
e90c1d2a 273
748fcded 274 wxString new_name( _("NewName") );
dc6c62a9 275 wxString path( data->m_path );
083f7497 276 if (!wxEndsWithPathSeparator(path))
748fcded 277 path += wxFILE_SEP_PATH;
dc6c62a9
RR
278 path += new_name;
279 if (wxFileExists(path))
280 {
281 // try NewName0, NewName1 etc.
282 int i = 0;
f6bcfd97 283 do {
748fcded 284 new_name = _("NewName");
f6bcfd97
BP
285 wxString num;
286 num.Printf( wxT("%d"), i );
287 new_name += num;
e90c1d2a 288
dc6c62a9 289 path = data->m_path;
083f7497 290 if (!wxEndsWithPathSeparator(path))
748fcded 291 path += wxFILE_SEP_PATH;
dc6c62a9 292 path += new_name;
f6bcfd97
BP
293 i++;
294 } while (wxFileExists(path));
d7a15103 295 }
e90c1d2a 296
dc6c62a9 297 wxLogNull log;
e90c1d2a 298 if (!wxMkdir(path))
dc6c62a9
RR
299 {
300 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 301 dialog.ShowModal();
dc6c62a9
RR
302 return;
303 }
304
ca65c044 305 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
d7a15103 306
748fcded
VS
307 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
308 // of the parent.
309 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
310 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
311 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
d7a15103 312}
ce4169a4 313
e13ea14e
VS
314void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
315{
316 SetPath(wxGetUserHome());
317}
318
f6bcfd97 319#endif // wxUSE_DIRDLG