]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
signed/unsigned comparison warning (and probably even error) fixed
[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"
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
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.
dabd1377
JS
96 if (style & wxDD_NEW_DIR_BUTTON)
97 {
98 wxBitmapButton* newButton =
99 new wxBitmapButton(this, ID_NEW,
100 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
101 buttonsizer->Add( newButton, 0, wxRIGHT, 10 );
102#if wxUSE_TOOLTIPS
103 newButton->SetToolTip(_("Create new directory"));
104#endif
105 }
2a0951be
VS
106
107#if wxUSE_TOOLTIPS
108 homeButton->SetToolTip(_("Go to home directory"));
2a0951be
VS
109#endif
110
111 topsizer->Add( buttonsizer, 0, wxTOP | wxALIGN_RIGHT, 10 );
112
dc6c62a9 113 // 1) dir ctrl
748fcded
VS
114 m_dirCtrl = NULL; // this is neccessary, event handler called from
115 // wxGenericDirCtrl would crash otherwise!
dabd1377
JS
116 long dirStyle = wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER;
117
118#ifdef __WXMSW__
119 if (style & wxDD_NEW_DIR_BUTTON)
120 {
121 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
122 // before we can call EditLabel (required for "New directory")
123 dirStyle |= wxDIRCTRL_EDIT_LABELS;
124 }
125#endif
126
748fcded
VS
127 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
128 m_path, wxPoint(5, 5),
129 wxSize(300, 200),
dabd1377 130 dirStyle);
748fcded
VS
131
132 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
e90c1d2a 133
ead95817
VS
134 // Make the an option depending on a flag?
135 wxCheckBox* check = new wxCheckBox( this, ID_SHOW_HIDDEN, _("Show hidden directories") );
136 topsizer->Add( check, 0, wxLEFT|wxTOP | wxALIGN_RIGHT, 5 );
137
dc6c62a9 138 // 2) text ctrl
e90c1d2a 139 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
dc6c62a9 140 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
e90c1d2a 141
dc6c62a9 142#if wxUSE_STATLINE
748fcded 143 // 3) Static line
dc6c62a9
RR
144 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
145#endif
d7a15103 146
748fcded 147 // 4) Buttons
2a0951be 148 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
42dcacf0 149
42dcacf0
RR
150 // OK and Cancel button should be at the right bottom
151 wxButton* okButton = new wxButton(this, wxID_OK, _("OK"));
152 buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 );
153 wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
154 buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 );
155
2a0951be 156 topsizer->Add( buttonsizer, 0, wxALL | wxALIGN_RIGHT, 10 );
dc6c62a9 157
748fcded
VS
158 okButton->SetDefault();
159 m_dirCtrl->SetFocus();
e90c1d2a 160
dc6c62a9
RR
161 SetAutoLayout( TRUE );
162 SetSizer( topsizer );
e90c1d2a 163
dc6c62a9
RR
164 topsizer->SetSizeHints( this );
165 topsizer->Fit( this );
166
167 Centre( wxBOTH );
748fcded 168}
dc6c62a9 169
748fcded
VS
170void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
171{
172 EndModal(wxID_CANCEL);
173}
2a4dbd81 174
748fcded
VS
175void wxGenericDirDialog::OnOK(wxCommandEvent& WXUNUSED(event))
176{
177 m_path = m_input->GetValue();
178 // Does the path exist? (User may have typed anything in m_input)
179 if (wxPathExists(m_path)) {
180 // OK, path exists, we're done.
181 EndModal(wxID_OK);
182 return;
f6bcfd97 183 }
748fcded
VS
184 // Interact with user, find out if the dir is a typo or to be created
185 wxString msg;
186 msg.Printf(_("The directory '%s' does not exist\nCreate it now?"),
187 m_path.c_str());
188 wxMessageDialog dialog(this, msg, _("Directory does not exist"),
189 wxYES_NO | wxICON_WARNING);
190
191 if ( dialog.ShowModal() == wxID_YES ) {
192 // Okay, let's make it
193 wxLogNull log;
194 if (wxMkdir(m_path)) {
195 // The new dir was created okay.
196 EndModal(wxID_OK);
197 return;
198 }
199 else {
200 // Trouble...
201 msg.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
202 m_path.c_str());
203 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
204 errmsg.ShowModal();
205 // We still don't have a valid dir. Back to the main dialog.
f6bcfd97 206 }
f6bcfd97 207 }
748fcded
VS
208 // User has answered NO to create dir.
209}
f6bcfd97 210
748fcded
VS
211void wxGenericDirDialog::SetPath(const wxString& path)
212{
213 m_dirCtrl->SetPath(path);
214 m_path = path;
215}
f6bcfd97 216
748fcded
VS
217wxString wxGenericDirDialog::GetPath(void) const
218{
219 return m_path;
d7a15103
JS
220}
221
748fcded 222int wxGenericDirDialog::ShowModal()
d7a15103 223{
dc6c62a9
RR
224 m_input->SetValue( m_path );
225 return wxDialog::ShowModal();
d7a15103
JS
226}
227
748fcded 228void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 229{
748fcded
VS
230 if (!m_dirCtrl)
231 return;
232
233 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
e90c1d2a 234 if (data)
dc6c62a9 235 m_input->SetValue( data->m_path );
d7a15103
JS
236};
237
748fcded 238void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 239{
748fcded
VS
240 if (!m_dirCtrl)
241 return;
242
243 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 244 if (data)
dc6c62a9 245 m_input->SetValue( data->m_path );
d7a15103
JS
246};
247
42dcacf0
RR
248void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
249{
250 if (!m_dirCtrl)
251 return;
252
253 m_dirCtrl->ShowHidden( event.GetInt() );
254}
255
748fcded 256void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 257{
748fcded
VS
258 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
259 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
260 (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 261 {
e90c1d2a
VZ
262 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
263 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
264 msg.ShowModal();
265 return;
266 }
d7a15103 267
748fcded
VS
268 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
269 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 270 wxASSERT( data );
e90c1d2a 271
748fcded 272 wxString new_name( _("NewName") );
dc6c62a9 273 wxString path( data->m_path );
748fcded
VS
274 if (path.Last() != wxFILE_SEP_PATH)
275 path += wxFILE_SEP_PATH;
dc6c62a9
RR
276 path += new_name;
277 if (wxFileExists(path))
278 {
279 // try NewName0, NewName1 etc.
280 int i = 0;
f6bcfd97 281 do {
748fcded 282 new_name = _("NewName");
f6bcfd97
BP
283 wxString num;
284 num.Printf( wxT("%d"), i );
285 new_name += num;
e90c1d2a 286
dc6c62a9 287 path = data->m_path;
748fcded
VS
288 if (path.Last() != wxFILE_SEP_PATH)
289 path += wxFILE_SEP_PATH;
dc6c62a9 290 path += new_name;
f6bcfd97
BP
291 i++;
292 } while (wxFileExists(path));
d7a15103 293 }
e90c1d2a 294
dc6c62a9 295 wxLogNull log;
e90c1d2a 296 if (!wxMkdir(path))
dc6c62a9
RR
297 {
298 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 299 dialog.ShowModal();
dc6c62a9
RR
300 return;
301 }
302
748fcded 303 wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE );
d7a15103 304
748fcded
VS
305 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
306 // of the parent.
307 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
308 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
309 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
d7a15103 310}
ce4169a4 311
e13ea14e
VS
312void wxGenericDirDialog::OnGoHome(wxCommandEvent& WXUNUSED(event))
313{
314 SetPath(wxGetUserHome());
315}
316
f6bcfd97 317#endif // wxUSE_DIRDLG