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