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