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