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