]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
UMADrawControl is not to be used anymore
[wxWidgets.git] / src / generic / dirdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dirdlg.cpp
3 // Purpose: wxDirDialog
4 // Author: Harm van der Heijden, Robert Roebling & Julian Smart
5 // Modified by:
6 // Created: 12/12/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden, Robert Roebling, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dirdlgg.h"
14 #endif
15
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #include "wx/defs.h"
25
26 #if wxUSE_DIRDLG
27
28 #ifndef WX_PRECOMP
29 #include "wx/textctrl.h"
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
32 #include "wx/sizer.h"
33 #include "wx/intl.h"
34 #include "wx/log.h"
35 #include "wx/msgdlg.h"
36 #endif
37
38 #include "wx/statline.h"
39 #include "wx/dirctrl.h"
40 #include "wx/generic/dirdlgg.h"
41 #include "wx/artprov.h"
42 #include "wx/bmpbuttn.h"
43
44 //-----------------------------------------------------------------------------
45 // wxGenericDirDialog
46 //-----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog)
49
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;
55 static const int ID_SHOW_HIDDEN = 1005;
56 static const int ID_GO_HOME = 1006;
57
58 BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
59 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
60 EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK)
61 EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew)
62 EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome)
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)
67 END_EVENT_TABLE()
68
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)
74 {
75 wxBusyCursor cursor;
76
77 m_path = defaultPath;
78 if (m_path == wxT("~"))
79 wxGetHomeDir(&m_path);
80 if (m_path == wxT("."))
81 m_path = wxGetCwd();
82
83 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
84
85 // 0) 'New' and 'Home' Buttons
86 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
87
88 // VS: 'Home directory' concept is unknown to MS-DOS
89 #ifndef __DOS__
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 );
94 #endif
95
96 // I'm not convinced we need a New button, and we tend to get annoying
97 // accidental-editing with label editing enabled.
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 }
108
109 #if wxUSE_TOOLTIPS
110 homeButton->SetToolTip(_("Go to home directory"));
111 #endif
112
113 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
114
115 // 1) dir ctrl
116 m_dirCtrl = NULL; // this is neccessary, event handler called from
117 // wxGenericDirCtrl would crash otherwise!
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
129 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
130 m_path, wxPoint(5, 5),
131 wxSize(300, 200),
132 dirStyle);
133
134 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
135
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
140 // 2) text ctrl
141 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
142 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
143
144 #if wxUSE_STATLINE
145 // 3) Static line
146 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
147 #endif
148
149 // 4) Buttons
150 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
151
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
158 topsizer->Add( buttonsizer, 0, wxALL | wxALIGN_RIGHT, 10 );
159
160 okButton->SetDefault();
161 m_dirCtrl->SetFocus();
162
163 SetAutoLayout( TRUE );
164 SetSizer( topsizer );
165
166 topsizer->SetSizeHints( this );
167 topsizer->Fit( this );
168
169 Centre( wxBOTH );
170 }
171
172 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
173 {
174 EndModal(wxID_CANCEL);
175 }
176
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;
185 }
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.
208 }
209 }
210 // User has answered NO to create dir.
211 }
212
213 void wxGenericDirDialog::SetPath(const wxString& path)
214 {
215 m_dirCtrl->SetPath(path);
216 m_path = path;
217 }
218
219 wxString wxGenericDirDialog::GetPath(void) const
220 {
221 return m_path;
222 }
223
224 int wxGenericDirDialog::ShowModal()
225 {
226 m_input->SetValue( m_path );
227 return wxDialog::ShowModal();
228 }
229
230 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
231 {
232 if (!m_dirCtrl)
233 return;
234
235 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
236 if (data)
237 m_input->SetValue( data->m_path );
238 };
239
240 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
241 {
242 if (!m_dirCtrl)
243 return;
244
245 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
246 if (data)
247 m_input->SetValue( data->m_path );
248 };
249
250 void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
251 {
252 if (!m_dirCtrl)
253 return;
254
255 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
256 }
257
258 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
259 {
260 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
261 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
262 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
263 {
264 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
265 _("Create directory"), wxOK | wxICON_INFORMATION );
266 msg.ShowModal();
267 return;
268 }
269
270 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
271 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
272 wxASSERT( data );
273
274 wxString new_name( _("NewName") );
275 wxString path( data->m_path );
276 if (!wxEndsWithPathSeparator(path))
277 path += wxFILE_SEP_PATH;
278 path += new_name;
279 if (wxFileExists(path))
280 {
281 // try NewName0, NewName1 etc.
282 int i = 0;
283 do {
284 new_name = _("NewName");
285 wxString num;
286 num.Printf( wxT("%d"), i );
287 new_name += num;
288
289 path = data->m_path;
290 if (!wxEndsWithPathSeparator(path))
291 path += wxFILE_SEP_PATH;
292 path += new_name;
293 i++;
294 } while (wxFileExists(path));
295 }
296
297 wxLogNull log;
298 if (!wxMkdir(path))
299 {
300 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
301 dialog.ShowModal();
302 return;
303 }
304
305 wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE );
306
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 );
312 }
313
314 void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
315 {
316 SetPath(wxGetUserHome());
317 }
318
319 #endif // wxUSE_DIRDLG