]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDirDialog
4 // Author: Harm van der Heijden, Robert Roebling & Julian Smart
8 // Copyright: (c) Harm van der Heijden, Robert Roebling, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dirdlgg.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/textctrl.h"
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
35 #include "wx/msgdlg.h"
38 #include "wx/statline.h"
39 #include "wx/dirctrl.h"
40 #include "wx/generic/dirdlgg.h"
41 #include "wx/artprov.h"
42 #include "wx/bmpbuttn.h"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog
, wxDialog
)
50 static const int ID_DIRCTRL
= 1000;
51 static const int ID_TEXTCTRL
= 1001;
52 static const int ID_OK
= 1002;
53 static const int ID_CANCEL
= 1003;
54 static const int ID_NEW
= 1004;
55 static const int ID_SHOW_HIDDEN
= 1005;
56 static const int ID_GO_HOME
= 1006;
58 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
59 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
60 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
61 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
62 EVT_BUTTON (ID_GO_HOME
, wxGenericDirDialog::OnGoHome
)
63 EVT_TREE_KEY_DOWN (wxID_ANY
, wxGenericDirDialog::OnTreeKeyDown
)
64 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxGenericDirDialog::OnTreeSelected
)
65 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
66 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
69 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
70 const wxString
& defaultPath
, long style
,
71 const wxPoint
& pos
, const wxSize
& sz
,
72 const wxString
& name
):
73 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
78 if (m_path
== wxT("~"))
79 wxGetHomeDir(&m_path
);
80 if (m_path
== wxT("."))
83 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
85 // 0) 'New' and 'Home' Buttons
86 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
88 // VS: 'Home directory' concept is unknown to MS-DOS
90 wxBitmapButton
* homeButton
=
91 new wxBitmapButton(this, ID_GO_HOME
,
92 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
93 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
96 // I'm not convinced we need a New button, and we tend to get annoying
97 // accidental-editing with label editing enabled.
98 if (style
& wxDD_NEW_DIR_BUTTON
)
100 wxBitmapButton
* newButton
=
101 new wxBitmapButton(this, ID_NEW
,
102 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
103 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
105 newButton
->SetToolTip(_("Create new directory"));
110 homeButton
->SetToolTip(_("Go to home directory"));
113 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
116 m_dirCtrl
= NULL
; // this is neccessary, event handler called from
117 // wxGenericDirCtrl would crash otherwise!
118 long dirStyle
= wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
;
121 if (style
& wxDD_NEW_DIR_BUTTON
)
123 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
124 // before we can call EditLabel (required for "New directory")
125 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
129 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
130 m_path
, wxDefaultPosition
,
134 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
136 // Make the an option depending on a flag?
137 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
138 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
141 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
142 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
146 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
150 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
152 // OK and Cancel button should be at the right bottom
153 wxButton
* okButton
= new wxButton(this, wxID_OK
, wxSTOCK_OK
);
154 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
155 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, wxSTOCK_CANCEL
);
156 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
158 topsizer
->Add( buttonsizer
, 0, wxLEFT
|wxTOP
|wxBOTTOM
| wxALIGN_RIGHT
, 10 );
160 okButton
->SetDefault();
161 m_dirCtrl
->SetFocus();
163 SetAutoLayout( true );
164 SetSizer( topsizer
);
166 topsizer
->SetSizeHints( this );
167 topsizer
->Fit( this );
172 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
174 EndModal(wxID_CANCEL
);
177 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
179 m_path
= m_input
->GetValue();
180 // Does the path exist? (User may have typed anything in m_input)
181 if (wxPathExists(m_path
)) {
182 // OK, path exists, we're done.
186 // Interact with user, find out if the dir is a typo or to be created
188 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
190 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
191 wxYES_NO
| wxICON_WARNING
);
193 if ( dialog
.ShowModal() == wxID_YES
) {
194 // Okay, let's make it
196 if (wxMkdir(m_path
)) {
197 // The new dir was created okay.
203 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
205 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
207 // We still don't have a valid dir. Back to the main dialog.
210 // User has answered NO to create dir.
213 void wxGenericDirDialog::SetPath(const wxString
& path
)
215 m_dirCtrl
->SetPath(path
);
219 wxString
wxGenericDirDialog::GetPath(void) const
224 int wxGenericDirDialog::ShowModal()
226 m_input
->SetValue( m_path
);
227 return wxDialog::ShowModal();
230 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
235 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
237 m_input
->SetValue( data
->m_path
);
240 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
245 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
247 m_input
->SetValue( data
->m_path
);
250 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
255 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
258 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
260 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
261 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
262 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
264 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
265 _("Create directory"), wxOK
| wxICON_INFORMATION
);
270 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
271 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
274 wxString
new_name( _("NewName") );
275 wxString
path( data
->m_path
);
276 if (!wxEndsWithPathSeparator(path
))
277 path
+= wxFILE_SEP_PATH
;
279 if (wxFileExists(path
))
281 // try NewName0, NewName1 etc.
284 new_name
= _("NewName");
286 num
.Printf( wxT("%d"), i
);
290 if (!wxEndsWithPathSeparator(path
))
291 path
+= wxFILE_SEP_PATH
;
294 } while (wxFileExists(path
));
300 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
305 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
307 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
309 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
310 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
311 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
314 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
316 SetPath(wxGetUserHome());
319 #endif // wxUSE_DIRDLG