]>
Commit | Line | Data |
---|---|---|
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 |
e90c1d2a | 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 |
48 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog) |
49 | ||
d7a15103 JS |
50 | static const int ID_DIRCTRL = 1000; |
51 | static const int ID_TEXTCTRL = 1001; | |
52 | static const int ID_OK = 1002; | |
53 | static const int ID_CANCEL = 1003; | |
54 | static const int ID_NEW = 1004; | |
42dcacf0 | 55 | static const int ID_SHOW_HIDDEN = 1005; |
2a0951be | 56 | static const int ID_GO_HOME = 1006; |
d7a15103 | 57 | |
748fcded | 58 | BEGIN_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) |
42dcacf0 RR |
63 | EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown) |
64 | EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected) | |
65 | EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK) | |
66 | EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden) | |
d7a15103 JS |
67 | END_EVENT_TABLE() |
68 | ||
748fcded VS |
69 | wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title, |
70 | const wxString& defaultPath, long style, | |
71 | const wxPoint& pos, const wxSize& sz, | |
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__ | |
2a0951be VS |
90 | wxBitmapButton* homeButton = |
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 |
2a0951be VS |
95 | |
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 | { | |
100 | wxBitmapButton* newButton = | |
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")); | |
106 | #endif | |
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 |
748fcded VS |
116 | m_dirCtrl = NULL; // this is neccessary, event handler called from |
117 | // wxGenericDirCtrl would crash otherwise! | |
dabd1377 JS |
118 | long dirStyle = wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER; |
119 | ||
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 | } | |
127 | #endif | |
128 | ||
748fcded VS |
129 | m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL, |
130 | m_path, wxPoint(5, 5), | |
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") ); | |
138 | topsizer->Add( check, 0, wxLEFT|wxTOP | wxALIGN_RIGHT, 5 ); | |
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 |
dc6c62a9 RR |
146 | topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 ); |
147 | #endif | |
d7a15103 | 148 | |
748fcded | 149 | // 4) Buttons |
2a0951be | 150 | buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
42dcacf0 | 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 | ||
2a0951be | 158 | topsizer->Add( buttonsizer, 0, wxALL | wxALIGN_RIGHT, 10 ); |
dc6c62a9 | 159 | |
748fcded VS |
160 | okButton->SetDefault(); |
161 | m_dirCtrl->SetFocus(); | |
e90c1d2a | 162 | |
dc6c62a9 RR |
163 | SetAutoLayout( TRUE ); |
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 |
172 | void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
173 | { | |
174 | EndModal(wxID_CANCEL); | |
175 | } | |
2a4dbd81 | 176 | |
748fcded VS |
177 | void 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; | |
188 | msg.Printf(_("The directory '%s' does not exist\nCreate it now?"), | |
189 | m_path.c_str()); | |
190 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), | |
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... | |
203 | msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"), | |
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 |
213 | void wxGenericDirDialog::SetPath(const wxString& path) |
214 | { | |
215 | m_dirCtrl->SetPath(path); | |
216 | m_path = path; | |
217 | } | |
f6bcfd97 | 218 | |
748fcded VS |
219 | wxString wxGenericDirDialog::GetPath(void) const |
220 | { | |
221 | return m_path; | |
d7a15103 JS |
222 | } |
223 | ||
748fcded | 224 | int wxGenericDirDialog::ShowModal() |
d7a15103 | 225 | { |
dc6c62a9 RR |
226 | m_input->SetValue( m_path ); |
227 | return wxDialog::ShowModal(); | |
d7a15103 JS |
228 | } |
229 | ||
748fcded | 230 | void 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 | 240 | void 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 |
250 | void 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 | 258 | void 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 | ||
748fcded | 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 |
314 | void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event)) |
315 | { | |
316 | SetPath(wxGetUserHome()); | |
317 | } | |
318 | ||
f6bcfd97 | 319 | #endif // wxUSE_DIRDLG |