]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
do NOT hard code univ sources in x11 port
[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 #ifdef __GNUG__
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/generic/dirctrlg.h"
40 #include "wx/generic/dirdlgg.h"
41
42 //-----------------------------------------------------------------------------
43 // wxGenericDirDialog
44 //-----------------------------------------------------------------------------
45
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;
51 static const int ID_SHOW_HIDDEN = 1005;
52
53 BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
54 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
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)
61 END_EVENT_TABLE()
62
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)
68 {
69 wxBusyCursor cursor;
70
71 m_path = defaultPath;
72 if (m_path == wxT("~"))
73 wxGetHomeDir(&m_path);
74 if (m_path == wxT("."))
75 m_path = wxGetCwd();
76
77 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
78
79 // 1) dir ctrl
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 );
88
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
93 // 2) text ctrl
94 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
95 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
96
97 #if wxUSE_STATLINE
98 // 3) Static line
99 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
100 #endif
101
102 // 4) Buttons
103 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
104
105 // I'm not convinced we need a New button, and we tend to get annoying
106 // accidental-editing with label editing enabled.
107 wxButton* newButton = new wxButton( this, ID_NEW, _("New...") );
108 buttonsizer->Add( newButton, 0, wxLEFT|wxRIGHT, 10 );
109
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
116 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
117
118 okButton->SetDefault();
119 m_dirCtrl->SetFocus();
120
121 SetAutoLayout( TRUE );
122 SetSizer( topsizer );
123
124 topsizer->SetSizeHints( this );
125 topsizer->Fit( this );
126
127 Centre( wxBOTH );
128 }
129
130 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
131 {
132 EndModal(wxID_CANCEL);
133 }
134
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;
143 }
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.
166 }
167 }
168 // User has answered NO to create dir.
169 }
170
171 void wxGenericDirDialog::SetPath(const wxString& path)
172 {
173 m_dirCtrl->SetPath(path);
174 m_path = path;
175 }
176
177 wxString wxGenericDirDialog::GetPath(void) const
178 {
179 return m_path;
180 }
181
182 int wxGenericDirDialog::ShowModal()
183 {
184 m_input->SetValue( m_path );
185 return wxDialog::ShowModal();
186 }
187
188 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
189 {
190 if (!m_dirCtrl)
191 return;
192
193 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
194 if (data)
195 m_input->SetValue( data->m_path );
196 };
197
198 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
199 {
200 if (!m_dirCtrl)
201 return;
202
203 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
204 if (data)
205 m_input->SetValue( data->m_path );
206 };
207
208 void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
209 {
210 if (!m_dirCtrl)
211 return;
212
213 m_dirCtrl->ShowHidden( event.GetInt() );
214 }
215
216 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
217 {
218 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
219 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
220 (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
221 {
222 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
223 _("Create directory"), wxOK | wxICON_INFORMATION );
224 msg.ShowModal();
225 return;
226 }
227
228 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
229 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
230 wxASSERT( data );
231
232 wxString new_name( _("NewName") );
233 wxString path( data->m_path );
234 if (path.Last() != wxFILE_SEP_PATH)
235 path += wxFILE_SEP_PATH;
236 path += new_name;
237 if (wxFileExists(path))
238 {
239 // try NewName0, NewName1 etc.
240 int i = 0;
241 do {
242 new_name = _("NewName");
243 wxString num;
244 num.Printf( wxT("%d"), i );
245 new_name += num;
246
247 path = data->m_path;
248 if (path.Last() != wxFILE_SEP_PATH)
249 path += wxFILE_SEP_PATH;
250 path += new_name;
251 i++;
252 } while (wxFileExists(path));
253 }
254
255 wxLogNull log;
256 if (!wxMkdir(path))
257 {
258 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
259 dialog.ShowModal();
260 return;
261 }
262
263 wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE );
264
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 );
270 }
271
272 #endif // wxUSE_DIRDLG