]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
don't pass NULL pointer to printf(), this crashes Solaris printf
[wxWidgets.git] / src / generic / dirdlgg.cpp
CommitLineData
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
65571936 9// Licence: wxWindows licence
d7a15103
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
d7a15103
JS
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"
42dcacf0 31 #include "wx/checkbox.h"
748fcded 32 #include "wx/sizer.h"
748fcded
VS
33 #include "wx/intl.h"
34 #include "wx/log.h"
35 #include "wx/msgdlg.h"
dc6c62a9 36#endif
d7a15103 37
d7d17624 38#include "wx/statline.h"
e78d4a23 39#include "wx/dirctrl.h"
bf2cac26 40#include "wx/generic/dirdlgg.h"
2a0951be
VS
41#include "wx/artprov.h"
42#include "wx/bmpbuttn.h"
d7a15103 43
0d1f53ca
WS
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
d6379fa3 47
d7a15103
JS
48static const int ID_DIRCTRL = 1000;
49static const int ID_TEXTCTRL = 1001;
d7a15103 50static const int ID_NEW = 1004;
42dcacf0 51static const int ID_SHOW_HIDDEN = 1005;
2a0951be 52static const int ID_GO_HOME = 1006;
d7a15103 53
0d1f53ca
WS
54// ---------------------------------------------------------------------------
55// macros
56// ---------------------------------------------------------------------------
57
58/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 59 device we display on - take it from something like wxDesktopPolicy in the future
0d1f53ca
WS
60 */
61
62#if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
64#else
65 #define wxLARGESMALL(large,small) large
66#endif
67
68//-----------------------------------------------------------------------------
69// wxGenericDirDialog
70//-----------------------------------------------------------------------------
71
72IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog, wxDialog)
73
748fcded 74BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
748fcded 75 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow)
42dcacf0
RR
76 EVT_BUTTON (wxID_OK, wxGenericDirDialog::OnOK)
77 EVT_BUTTON (ID_NEW, wxGenericDirDialog::OnNew)
e13ea14e 78 EVT_BUTTON (ID_GO_HOME, wxGenericDirDialog::OnGoHome)
ca65c044
WS
79 EVT_TREE_KEY_DOWN (wxID_ANY, wxGenericDirDialog::OnTreeKeyDown)
80 EVT_TREE_SEL_CHANGED (wxID_ANY, wxGenericDirDialog::OnTreeSelected)
42dcacf0
RR
81 EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK)
82 EVT_CHECKBOX (ID_SHOW_HIDDEN, wxGenericDirDialog::OnShowHidden)
d7a15103
JS
83END_EVENT_TABLE()
84
748fcded
VS
85wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title,
86 const wxString& defaultPath, long style,
ca65c044 87 const wxPoint& pos, const wxSize& sz,
748fcded
VS
88 const wxString& name):
89 wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name)
d7a15103 90{
748fcded 91 wxBusyCursor cursor;
e90c1d2a 92
dc6c62a9 93 m_path = defaultPath;
748fcded 94 if (m_path == wxT("~"))
ab8d2b02
VS
95 wxGetHomeDir(&m_path);
96 if (m_path == wxT("."))
97 m_path = wxGetCwd();
e90c1d2a 98
dc6c62a9
RR
99 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
100
9a357011 101 // smart phones does not support or do not waste space for wxButtons
0d1f53ca
WS
102#if defined(__SMARTPHONE__)
103
d2cdad17
WS
104 wxMenu *dirMenu = new wxMenu;
105 dirMenu->Append(ID_GO_HOME, _("Home"));
106
107 if (style & wxDD_NEW_DIR_BUTTON)
108 {
109 dirMenu->Append(ID_NEW, _("New directory"));
110 }
111
112 dirMenu->AppendCheckItem(ID_SHOW_HIDDEN, _("Show hidden directories"));
113 dirMenu->AppendSeparator();
114 dirMenu->Append(wxID_CANCEL, _("Cancel"));
115
116 SetRightMenu(wxID_ANY, _("Options"), dirMenu);
0d1f53ca
WS
117
118#else
119
2a0951be
VS
120 // 0) 'New' and 'Home' Buttons
121 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
122
e13ea14e 123 // VS: 'Home directory' concept is unknown to MS-DOS
0d1f53ca 124#if !defined(__DOS__)
ca65c044 125 wxBitmapButton* homeButton =
2a0951be 126 new wxBitmapButton(this, ID_GO_HOME,
36668b35 127 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
2a0951be 128 buttonsizer->Add( homeButton, 0, wxLEFT|wxRIGHT, 10 );
e13ea14e 129#endif
ca65c044 130
2a0951be
VS
131 // I'm not convinced we need a New button, and we tend to get annoying
132 // accidental-editing with label editing enabled.
dabd1377
JS
133 if (style & wxDD_NEW_DIR_BUTTON)
134 {
ca65c044 135 wxBitmapButton* newButton =
dabd1377 136 new wxBitmapButton(this, ID_NEW,
36668b35 137 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
dabd1377
JS
138 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
139#if wxUSE_TOOLTIPS
140 newButton->SetToolTip(_("Create new directory"));
ca65c044 141#endif
dabd1377 142 }
2a0951be
VS
143
144#if wxUSE_TOOLTIPS
145 homeButton->SetToolTip(_("Go to home directory"));
2a0951be
VS
146#endif
147
148 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
149
0d1f53ca
WS
150#endif // __SMARTPHONE__/!__SMARTPHONE__
151
dc6c62a9 152 // 1) dir ctrl
3103e8a9 153 m_dirCtrl = NULL; // this is necessary, event handler called from
748fcded 154 // wxGenericDirCtrl would crash otherwise!
03775239 155 long dirStyle = wxDIRCTRL_DIR_ONLY | wxDEFAULT_CONTROL_BORDER;
ca65c044 156
dabd1377
JS
157#ifdef __WXMSW__
158 if (style & wxDD_NEW_DIR_BUTTON)
159 {
160 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
161 // before we can call EditLabel (required for "New directory")
162 dirStyle |= wxDIRCTRL_EDIT_LABELS;
163 }
ca65c044 164#endif
dabd1377 165
748fcded 166 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
488eb04e 167 m_path, wxDefaultPosition,
ca65c044 168 wxSize(300, 200),
dabd1377 169 dirStyle);
748fcded 170
0d1f53ca 171 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
e90c1d2a 172
0d1f53ca 173#ifndef __SMARTPHONE__
ead95817
VS
174 // Make the an option depending on a flag?
175 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
488eb04e 176 topsizer->Add( check, 0, wxLEFT|wxRIGHT|wxTOP | wxALIGN_RIGHT, 10 );
0d1f53ca 177#endif // !__SMARTPHONE__
ead95817 178
dc6c62a9 179 // 2) text ctrl
e90c1d2a 180 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
0d1f53ca
WS
181 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, wxLARGESMALL(10,0) );
182
d2cdad17 183#ifndef __SMARTPHONE__
e90c1d2a 184
dc6c62a9 185#if wxUSE_STATLINE
748fcded 186 // 3) Static line
ca65c044 187 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
dc6c62a9 188#endif
d7a15103 189
748fcded 190 // 4) Buttons
acf2ac37 191 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxEXPAND | wxALL, 10 );
0d1f53ca
WS
192
193#endif // !__SMARTPHONE__
194
9026d6fd 195 m_input->SetFocus();
e90c1d2a 196
ca65c044 197 SetAutoLayout( true );
dc6c62a9 198 SetSizer( topsizer );
e90c1d2a 199
03775239 200#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
dc6c62a9
RR
201 topsizer->SetSizeHints( this );
202 topsizer->Fit( this );
203
204 Centre( wxBOTH );
03775239 205#endif
748fcded 206}
dc6c62a9 207
748fcded
VS
208void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
209{
210 EndModal(wxID_CANCEL);
211}
2a4dbd81 212
748fcded
VS
213void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
214{
215 m_path = m_input->GetValue();
216 // Does the path exist? (User may have typed anything in m_input)
da865fdd 217 if (wxDirExists(m_path)) {
748fcded
VS
218 // OK, path exists, we're done.
219 EndModal(wxID_OK);
220 return;
f6bcfd97 221 }
748fcded
VS
222 // Interact with user, find out if the dir is a typo or to be created
223 wxString msg;
ca65c044 224 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
748fcded 225 m_path.c_str());
ca65c044 226 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
748fcded
VS
227 wxYES_NO | wxICON_WARNING);
228
229 if ( dialog.ShowModal() == wxID_YES ) {
230 // Okay, let's make it
231 wxLogNull log;
232 if (wxMkdir(m_path)) {
233 // The new dir was created okay.
234 EndModal(wxID_OK);
235 return;
236 }
237 else {
238 // Trouble...
ca65c044 239 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
748fcded
VS
240 m_path.c_str());
241 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
242 errmsg.ShowModal();
243 // We still don't have a valid dir. Back to the main dialog.
f6bcfd97 244 }
f6bcfd97 245 }
748fcded
VS
246 // User has answered NO to create dir.
247}
f6bcfd97 248
748fcded
VS
249void wxGenericDirDialog::SetPath(const wxString& path)
250{
251 m_dirCtrl->SetPath(path);
252 m_path = path;
253}
f6bcfd97 254
748fcded
VS
255wxString wxGenericDirDialog::GetPath(void) const
256{
257 return m_path;
d7a15103
JS
258}
259
748fcded 260int wxGenericDirDialog::ShowModal()
d7a15103 261{
dc6c62a9
RR
262 m_input->SetValue( m_path );
263 return wxDialog::ShowModal();
d7a15103
JS
264}
265
748fcded 266void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 267{
748fcded
VS
268 if (!m_dirCtrl)
269 return;
270
44d60c0b
WS
271 wxTreeItemId item = event.GetItem();
272
273 wxDirItemData *data = NULL;
274
275 if(item.IsOk())
276 data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(item);
277
e90c1d2a 278 if (data)
dc6c62a9 279 m_input->SetValue( data->m_path );
d7a15103
JS
280};
281
748fcded 282void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 283{
748fcded
VS
284 if (!m_dirCtrl)
285 return;
286
287 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 288 if (data)
dc6c62a9 289 m_input->SetValue( data->m_path );
d7a15103
JS
290};
291
42dcacf0
RR
292void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
293{
294 if (!m_dirCtrl)
295 return;
296
2b5f62a0 297 m_dirCtrl->ShowHidden( event.GetInt() != 0 );
42dcacf0
RR
298}
299
748fcded 300void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 301{
748fcded
VS
302 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
303 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
99006e44 304 (m_dirCtrl->GetTreeCtrl()->GetItemParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 305 {
e90c1d2a
VZ
306 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
307 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
308 msg.ShowModal();
309 return;
310 }
d7a15103 311
99006e44 312 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
748fcded 313 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 314 wxASSERT( data );
e90c1d2a 315
748fcded 316 wxString new_name( _("NewName") );
dc6c62a9 317 wxString path( data->m_path );
083f7497 318 if (!wxEndsWithPathSeparator(path))
748fcded 319 path += wxFILE_SEP_PATH;
dc6c62a9 320 path += new_name;
da865fdd 321 if (wxDirExists(path))
dc6c62a9
RR
322 {
323 // try NewName0, NewName1 etc.
324 int i = 0;
f6bcfd97 325 do {
748fcded 326 new_name = _("NewName");
f6bcfd97
BP
327 wxString num;
328 num.Printf( wxT("%d"), i );
329 new_name += num;
e90c1d2a 330
dc6c62a9 331 path = data->m_path;
083f7497 332 if (!wxEndsWithPathSeparator(path))
748fcded 333 path += wxFILE_SEP_PATH;
dc6c62a9 334 path += new_name;
f6bcfd97 335 i++;
da865fdd 336 } while (wxDirExists(path));
d7a15103 337 }
e90c1d2a 338
dc6c62a9 339 wxLogNull log;
e90c1d2a 340 if (!wxMkdir(path))
dc6c62a9
RR
341 {
342 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 343 dialog.ShowModal();
dc6c62a9
RR
344 return;
345 }
346
ca65c044 347 wxDirItemData *new_data = new wxDirItemData( path, new_name, true );
d7a15103 348
748fcded
VS
349 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
350 // of the parent.
351 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
352 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
353 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
d7a15103 354}
ce4169a4 355
e13ea14e
VS
356void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
357{
358 SetPath(wxGetUserHome());
359}
360
f6bcfd97 361#endif // wxUSE_DIRDLG