]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
use the new wxSystemSettings API everywhere
[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
73 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
74
75 // 1) dir ctrl
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 );
84
85 // 2) text ctrl
86 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
87 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
88
89 #if wxUSE_STATLINE
90 // 3) Static line
91 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
92 #endif
93
94 // 4) Buttons
95 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
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 );
105
106 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
107
108 okButton->SetDefault();
109 m_dirCtrl->SetFocus();
110
111 SetAutoLayout( TRUE );
112 SetSizer( topsizer );
113
114 topsizer->SetSizeHints( this );
115 topsizer->Fit( this );
116
117 Centre( wxBOTH );
118 }
119
120 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
121 {
122 EndModal(wxID_CANCEL);
123 }
124
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;
133 }
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.
156 }
157 }
158 // User has answered NO to create dir.
159 }
160
161 void wxGenericDirDialog::SetPath(const wxString& path)
162 {
163 m_dirCtrl->SetPath(path);
164 m_path = path;
165 }
166
167 wxString wxGenericDirDialog::GetPath(void) const
168 {
169 return m_path;
170 }
171
172 int wxGenericDirDialog::ShowModal()
173 {
174 m_input->SetValue( m_path );
175 return wxDialog::ShowModal();
176 }
177
178 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
179 {
180 if (!m_dirCtrl)
181 return;
182
183 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
184 if (data)
185 m_input->SetValue( data->m_path );
186 };
187
188 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
189 {
190 if (!m_dirCtrl)
191 return;
192
193 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
194 if (data)
195 m_input->SetValue( data->m_path );
196 };
197
198 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
199 {
200 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
201 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
202 (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
203 {
204 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
205 _("Create directory"), wxOK | wxICON_INFORMATION );
206 msg.ShowModal();
207 return;
208 }
209
210 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
211 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
212 wxASSERT( data );
213
214 wxString new_name( _("NewName") );
215 wxString path( data->m_path );
216 if (path.Last() != wxFILE_SEP_PATH)
217 path += wxFILE_SEP_PATH;
218 path += new_name;
219 if (wxFileExists(path))
220 {
221 // try NewName0, NewName1 etc.
222 int i = 0;
223 do {
224 new_name = _("NewName");
225 wxString num;
226 num.Printf( wxT("%d"), i );
227 new_name += num;
228
229 path = data->m_path;
230 if (path.Last() != wxFILE_SEP_PATH)
231 path += wxFILE_SEP_PATH;
232 path += new_name;
233 i++;
234 } while (wxFileExists(path));
235 }
236
237 wxLogNull log;
238 if (!wxMkdir(path))
239 {
240 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
241 dialog.ShowModal();
242 return;
243 }
244
245 wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE );
246
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 );
252 }
253
254 #endif // wxUSE_DIRDLG