]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirdlgg.cpp
added wxEXEC_MAKE_GROUP_LEADER (patch 535422)
[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)
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)
d7a15103
JS
64END_EVENT_TABLE()
65
748fcded
VS
66wxGenericDirDialog::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)
d7a15103 71{
748fcded 72 wxBusyCursor cursor;
e90c1d2a 73
dc6c62a9 74 m_path = defaultPath;
748fcded 75 if (m_path == wxT("~"))
ab8d2b02
VS
76 wxGetHomeDir(&m_path);
77 if (m_path == wxT("."))
78 m_path = wxGetCwd();
e90c1d2a 79
dc6c62a9
RR
80 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
81
2a0951be
VS
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
dc6c62a9 104 // 1) dir ctrl
748fcded
VS
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 );
e90c1d2a 113
ead95817
VS
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
dc6c62a9 118 // 2) text ctrl
e90c1d2a 119 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
dc6c62a9 120 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
e90c1d2a 121
dc6c62a9 122#if wxUSE_STATLINE
748fcded 123 // 3) Static line
dc6c62a9
RR
124 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
125#endif
d7a15103 126
748fcded 127 // 4) Buttons
2a0951be 128 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
42dcacf0 129
42dcacf0
RR
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
2a0951be 136 topsizer->Add( buttonsizer, 0, wxALL | wxALIGN_RIGHT, 10 );
dc6c62a9 137
748fcded
VS
138 okButton->SetDefault();
139 m_dirCtrl->SetFocus();
e90c1d2a 140
dc6c62a9
RR
141 SetAutoLayout( TRUE );
142 SetSizer( topsizer );
e90c1d2a 143
dc6c62a9
RR
144 topsizer->SetSizeHints( this );
145 topsizer->Fit( this );
146
147 Centre( wxBOTH );
748fcded 148}
dc6c62a9 149
748fcded
VS
150void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
151{
152 EndModal(wxID_CANCEL);
153}
2a4dbd81 154
748fcded
VS
155void 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;
f6bcfd97 163 }
748fcded
VS
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.
f6bcfd97 186 }
f6bcfd97 187 }
748fcded
VS
188 // User has answered NO to create dir.
189}
f6bcfd97 190
748fcded
VS
191void wxGenericDirDialog::SetPath(const wxString& path)
192{
193 m_dirCtrl->SetPath(path);
194 m_path = path;
195}
f6bcfd97 196
748fcded
VS
197wxString wxGenericDirDialog::GetPath(void) const
198{
199 return m_path;
d7a15103
JS
200}
201
748fcded 202int wxGenericDirDialog::ShowModal()
d7a15103 203{
dc6c62a9
RR
204 m_input->SetValue( m_path );
205 return wxDialog::ShowModal();
d7a15103
JS
206}
207
748fcded 208void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
d7a15103 209{
748fcded
VS
210 if (!m_dirCtrl)
211 return;
212
213 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
e90c1d2a 214 if (data)
dc6c62a9 215 m_input->SetValue( data->m_path );
d7a15103
JS
216};
217
748fcded 218void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
d7a15103 219{
748fcded
VS
220 if (!m_dirCtrl)
221 return;
222
223 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
e90c1d2a 224 if (data)
dc6c62a9 225 m_input->SetValue( data->m_path );
d7a15103
JS
226};
227
42dcacf0
RR
228void wxGenericDirDialog::OnShowHidden( wxCommandEvent& event )
229{
230 if (!m_dirCtrl)
231 return;
232
233 m_dirCtrl->ShowHidden( event.GetInt() );
234}
235
748fcded 236void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
d7a15103 237{
748fcded
VS
238 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
239 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
240 (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
dc6c62a9 241 {
e90c1d2a
VZ
242 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
243 _("Create directory"), wxOK | wxICON_INFORMATION );
dc6c62a9
RR
244 msg.ShowModal();
245 return;
246 }
d7a15103 247
748fcded
VS
248 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
249 wxDirItemData *data = (wxDirItemData*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
dc6c62a9 250 wxASSERT( data );
e90c1d2a 251
748fcded 252 wxString new_name( _("NewName") );
dc6c62a9 253 wxString path( data->m_path );
748fcded
VS
254 if (path.Last() != wxFILE_SEP_PATH)
255 path += wxFILE_SEP_PATH;
dc6c62a9
RR
256 path += new_name;
257 if (wxFileExists(path))
258 {
259 // try NewName0, NewName1 etc.
260 int i = 0;
f6bcfd97 261 do {
748fcded 262 new_name = _("NewName");
f6bcfd97
BP
263 wxString num;
264 num.Printf( wxT("%d"), i );
265 new_name += num;
e90c1d2a 266
dc6c62a9 267 path = data->m_path;
748fcded
VS
268 if (path.Last() != wxFILE_SEP_PATH)
269 path += wxFILE_SEP_PATH;
dc6c62a9 270 path += new_name;
f6bcfd97
BP
271 i++;
272 } while (wxFileExists(path));
d7a15103 273 }
e90c1d2a 274
dc6c62a9 275 wxLogNull log;
e90c1d2a 276 if (!wxMkdir(path))
dc6c62a9
RR
277 {
278 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
f6bcfd97 279 dialog.ShowModal();
dc6c62a9
RR
280 return;
281 }
282
748fcded 283 wxDirItemData *new_data = new wxDirItemData( path, new_name, TRUE );
d7a15103 284
748fcded
VS
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 );
d7a15103 290}
ce4169a4 291
f6bcfd97 292#endif // wxUSE_DIRDLG