]>
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 | ||
12 | #ifdef __GNUG__ | |
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" |
748fcded | 39 | #include "wx/generic/dirctrlg.h" |
d7a15103 JS |
40 | #include "wx/generic/dirdlgg.h" |
41 | ||
748fcded VS |
42 | //----------------------------------------------------------------------------- |
43 | // wxGenericDirDialog | |
44 | //----------------------------------------------------------------------------- | |
a17e237f | 45 | |
d7a15103 JS |
46 | static const int ID_DIRCTRL = 1000; |
47 | static const int ID_TEXTCTRL = 1001; | |
48 | static const int ID_OK = 1002; | |
49 | static const int ID_CANCEL = 1003; | |
50 | static const int ID_NEW = 1004; | |
42dcacf0 | 51 | static const int ID_SHOW_HIDDEN = 1005; |
d7a15103 | 52 | |
748fcded | 53 | BEGIN_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) | |
57 | EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown) | |
58 | EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected) | |
59 | EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK) | |
60 | EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden) | |
d7a15103 JS |
61 | END_EVENT_TABLE() |
62 | ||
748fcded VS |
63 | wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title, |
64 | const wxString& defaultPath, long style, | |
65 | const wxPoint& pos, const wxSize& sz, | |
66 | const wxString& name): | |
67 | wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name) | |
d7a15103 | 68 | { |
748fcded | 69 | wxBusyCursor cursor; |
e90c1d2a | 70 | |
dc6c62a9 | 71 | m_path = defaultPath; |
748fcded | 72 | if (m_path == wxT("~")) |
ab8d2b02 VS |
73 | wxGetHomeDir(&m_path); |
74 | if (m_path == wxT(".")) | |
75 | m_path = wxGetCwd(); | |
e90c1d2a | 76 | |
dc6c62a9 RR |
77 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
78 | ||
79 | // 1) dir ctrl | |
748fcded VS |
80 | m_dirCtrl = NULL; // this is neccessary, event handler called from |
81 | // wxGenericDirCtrl would crash otherwise! | |
82 | m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL, | |
83 | m_path, wxPoint(5, 5), | |
84 | wxSize(300, 200), | |
85 | wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER); | |
86 | ||
87 | topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); | |
e90c1d2a | 88 | |
ead95817 VS |
89 | // Make the an option depending on a flag? |
90 | wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") ); | |
91 | topsizer->Add( check, 0, wxLEFT|wxTOP | wxALIGN_RIGHT, 5 ); | |
92 | ||
dc6c62a9 | 93 | // 2) text ctrl |
e90c1d2a | 94 | m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition ); |
dc6c62a9 | 95 | topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); |
e90c1d2a | 96 | |
dc6c62a9 | 97 | #if wxUSE_STATLINE |
748fcded | 98 | // 3) Static line |
dc6c62a9 RR |
99 | topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 ); |
100 | #endif | |
d7a15103 | 101 | |
748fcded | 102 | // 4) Buttons |
dc6c62a9 | 103 | wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
42dcacf0 | 104 | |
748fcded VS |
105 | // I'm not convinced we need a New button, and we tend to get annoying |
106 | // accidental-editing with label editing enabled. | |
42dcacf0 | 107 | wxButton* newButton = new wxButton( this, ID_NEW, _("New...") ); |
748fcded | 108 | buttonsizer->Add( newButton, 0, wxLEFT|wxRIGHT, 10 ); |
e90c1d2a | 109 | |
42dcacf0 RR |
110 | // OK and Cancel button should be at the right bottom |
111 | wxButton* okButton = new wxButton(this, wxID_OK, _("OK")); | |
112 | buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 ); | |
113 | wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel")); | |
114 | buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 ); | |
115 | ||
dc6c62a9 RR |
116 | topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 ); |
117 | ||
748fcded VS |
118 | okButton->SetDefault(); |
119 | m_dirCtrl->SetFocus(); | |
e90c1d2a | 120 | |
dc6c62a9 RR |
121 | SetAutoLayout( TRUE ); |
122 | SetSizer( topsizer ); | |
e90c1d2a | 123 | |
dc6c62a9 RR |
124 | topsizer->SetSizeHints( this ); |
125 | topsizer->Fit( this ); | |
126 | ||
127 | Centre( wxBOTH ); | |
748fcded | 128 | } |
dc6c62a9 | 129 | |
748fcded VS |
130 | void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
131 | { | |
132 | EndModal(wxID_CANCEL); | |
133 | } | |
2a4dbd81 | 134 | |
748fcded VS |
135 | void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
136 | { | |
137 | m_path = m_input->GetValue(); | |
138 | // Does the path exist? (User may have typed anything in m_input) | |
139 | if (wxPathExists(m_path)) { | |
140 | // OK, path exists, we're done. | |
141 | EndModal(wxID_OK); | |
142 | return; | |
f6bcfd97 | 143 | } |
748fcded VS |
144 | // Interact with user, find out if the dir is a typo or to be created |
145 | wxString msg; | |
146 | msg.Printf(_("The directory '%s' does not exist\nCreate it now?"), | |
147 | m_path.c_str()); | |
148 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), | |
149 | wxYES_NO | wxICON_WARNING); | |
150 | ||
151 | if ( dialog.ShowModal() == wxID_YES ) { | |
152 | // Okay, let's make it | |
153 | wxLogNull log; | |
154 | if (wxMkdir(m_path)) { | |
155 | // The new dir was created okay. | |
156 | EndModal(wxID_OK); | |
157 | return; | |
158 | } | |
159 | else { | |
160 | // Trouble... | |
161 | msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"), | |
162 | m_path.c_str()); | |
163 | wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR); | |
164 | errmsg.ShowModal(); | |
165 | // We still don't have a valid dir. Back to the main dialog. | |
f6bcfd97 | 166 | } |
f6bcfd97 | 167 | } |
748fcded VS |
168 | // User has answered NO to create dir. |
169 | } | |
f6bcfd97 | 170 | |
748fcded VS |
171 | void wxGenericDirDialog::SetPath(const wxString& path) |
172 | { | |
173 | m_dirCtrl->SetPath(path); | |
174 | m_path = path; | |
175 | } | |
f6bcfd97 | 176 | |
748fcded VS |
177 | wxString wxGenericDirDialog::GetPath(void) const |
178 | { | |
179 | return m_path; | |
d7a15103 JS |
180 | } |
181 | ||
748fcded | 182 | int wxGenericDirDialog::ShowModal() |
d7a15103 | 183 | { |
dc6c62a9 RR |
184 | m_input->SetValue( m_path ); |
185 | return wxDialog::ShowModal(); | |
d7a15103 JS |
186 | } |
187 | ||
748fcded | 188 | void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event ) |
d7a15103 | 189 | { |
748fcded VS |
190 | if (!m_dirCtrl) |
191 | return; | |
192 | ||
193 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem()); | |
e90c1d2a | 194 | if (data) |
dc6c62a9 | 195 | m_input->SetValue( data->m_path ); |
d7a15103 JS |
196 | }; |
197 | ||
748fcded | 198 | void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) ) |
d7a15103 | 199 | { |
748fcded VS |
200 | if (!m_dirCtrl) |
201 | return; | |
202 | ||
203 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection()); | |
e90c1d2a | 204 | if (data) |
dc6c62a9 | 205 | m_input->SetValue( data->m_path ); |
d7a15103 JS |
206 | }; |
207 | ||
42dcacf0 RR |
208 | void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event ) |
209 | { | |
210 | if (!m_dirCtrl) | |
211 | return; | |
212 | ||
213 | m_dirCtrl->ShowHidden( event.GetInt() ); | |
214 | } | |
215 | ||
748fcded | 216 | void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) ) |
d7a15103 | 217 | { |
748fcded VS |
218 | wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection(); |
219 | if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) || | |
220 | (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem())) | |
dc6c62a9 | 221 | { |
e90c1d2a VZ |
222 | wxMessageDialog msg(this, _("You cannot add a new directory to this section."), |
223 | _("Create directory"), wxOK | wxICON_INFORMATION ); | |
dc6c62a9 RR |
224 | msg.ShowModal(); |
225 | return; | |
226 | } | |
d7a15103 | 227 | |
748fcded VS |
228 | wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id ); |
229 | wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent ); | |
dc6c62a9 | 230 | wxASSERT( data ); |
e90c1d2a | 231 | |
748fcded | 232 | wxString new_name( _("NewName") ); |
dc6c62a9 | 233 | wxString path( data->m_path ); |
748fcded VS |
234 | if (path.Last() != wxFILE_SEP_PATH) |
235 | path += wxFILE_SEP_PATH; | |
dc6c62a9 RR |
236 | path += new_name; |
237 | if (wxFileExists(path)) | |
238 | { | |
239 | // try NewName0, NewName1 etc. | |
240 | int i = 0; | |
f6bcfd97 | 241 | do { |
748fcded | 242 | new_name = _("NewName"); |
f6bcfd97 BP |
243 | wxString num; |
244 | num.Printf( wxT("%d"), i ); | |
245 | new_name += num; | |
e90c1d2a | 246 | |
dc6c62a9 | 247 | path = data->m_path; |
748fcded VS |
248 | if (path.Last() != wxFILE_SEP_PATH) |
249 | path += wxFILE_SEP_PATH; | |
dc6c62a9 | 250 | path += new_name; |
f6bcfd97 BP |
251 | i++; |
252 | } while (wxFileExists(path)); | |
d7a15103 | 253 | } |
e90c1d2a | 254 | |
dc6c62a9 | 255 | wxLogNull log; |
e90c1d2a | 256 | if (!wxMkdir(path)) |
dc6c62a9 RR |
257 | { |
258 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
f6bcfd97 | 259 | dialog.ShowModal(); |
dc6c62a9 RR |
260 | return; |
261 | } | |
262 | ||
748fcded | 263 | wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE ); |
d7a15103 | 264 | |
748fcded VS |
265 | // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child |
266 | // of the parent. | |
267 | wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data ); | |
268 | m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id ); | |
269 | m_dirCtrl->GetTreeCtrl()->EditLabel( new_id ); | |
d7a15103 | 270 | } |
ce4169a4 | 271 | |
f6bcfd97 | 272 | #endif // wxUSE_DIRDLG |