1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dirdlg.cpp
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/textctrl.h"
23 #include "wx/button.h"
24 #include "wx/checkbox.h"
28 #include "wx/msgdlg.h"
29 #include "wx/bmpbuttn.h"
32 #include "wx/statline.h"
33 #include "wx/dirctrl.h"
34 #include "wx/generic/dirdlgg.h"
35 #include "wx/artprov.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 static const int ID_DIRCTRL
= 1000;
43 static const int ID_TEXTCTRL
= 1001;
44 static const int ID_NEW
= 1004;
45 static const int ID_SHOW_HIDDEN
= 1005;
46 static const int ID_GO_HOME
= 1006;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog
, wxDialog
)
54 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
55 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
56 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
57 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
58 EVT_BUTTON (ID_GO_HOME
, wxGenericDirDialog::OnGoHome
)
59 EVT_TREE_KEY_DOWN (wxID_ANY
, wxGenericDirDialog::OnTreeKeyDown
)
60 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxGenericDirDialog::OnTreeSelected
)
61 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
62 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
65 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
66 const wxString
& defaultPath
, long style
,
67 const wxPoint
& pos
, const wxSize
& sz
,
70 Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
73 bool wxGenericDirDialog::Create(wxWindow
* parent
,
74 const wxString
& title
,
75 const wxString
& defaultPath
, long style
,
82 parent
= GetParentForModalDialog(parent
, style
);
84 if (!wxDirDialogBase::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
))
88 if (m_path
== wxT("~"))
89 wxGetHomeDir(&m_path
);
90 if (m_path
== wxT("."))
93 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
95 // smartphones does not support or do not waste space for wxButtons
96 #if defined(__SMARTPHONE__)
98 wxMenu
*dirMenu
= new wxMenu
;
99 dirMenu
->Append(ID_GO_HOME
, _("Home"));
101 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
103 dirMenu
->Append(ID_NEW
, _("New directory"));
106 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
107 dirMenu
->AppendSeparator();
108 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
112 // 0) 'New' and 'Home' Buttons
113 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
115 // VS: 'Home directory' concept is unknown to MS-DOS
116 #if !defined(__DOS__)
117 wxBitmapButton
* homeButton
=
118 new wxBitmapButton(this, ID_GO_HOME
,
119 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
120 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
123 // I'm not convinced we need a New button, and we tend to get annoying
124 // accidental-editing with label editing enabled.
125 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
127 wxBitmapButton
* newButton
=
128 new wxBitmapButton(this, ID_NEW
,
129 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
130 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
132 newButton
->SetToolTip(_("Create new directory"));
137 homeButton
->SetToolTip(_("Go to home directory"));
140 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
142 #endif // __SMARTPHONE__/!__SMARTPHONE__
145 m_dirCtrl
= NULL
; // this is necessary, event handler called from
146 // wxGenericDirCtrl would crash otherwise!
147 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
150 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
152 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
153 // before we can call EditLabel (required for "New directory")
154 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
158 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
159 m_path
, wxDefaultPosition
,
163 wxSizerFlags flagsBorder2
;
164 flagsBorder2
.DoubleBorder(wxTOP
| wxLEFT
| wxRIGHT
);
166 topsizer
->Add(m_dirCtrl
, wxSizerFlags(flagsBorder2
).Proportion(1).Expand());
168 #ifndef __SMARTPHONE__
169 // Make the an option depending on a flag?
171 check
= new wxCheckBox(this, ID_SHOW_HIDDEN
, _("Show &hidden directories"));
172 topsizer
->Add(check
, wxSizerFlags(flagsBorder2
).Right());
173 #endif // !__SMARTPHONE__
176 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
177 topsizer
->Add(m_input
, wxSizerFlags(flagsBorder2
).Expand());
180 wxSizer
*buttonSizer
= CreateSeparatedButtonSizer(wxOK
| wxCANCEL
);
183 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
186 #ifdef __SMARTPHONE__
187 // overwrite menu set by CreateSeparatedButtonSizer() call above
188 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
193 SetAutoLayout( true );
194 SetSizer( topsizer
);
196 topsizer
->SetSizeHints( this );
197 topsizer
->Fit( this );
204 void wxGenericDirDialog::EndModal(int retCode
)
206 // before proceeding, change the current working directory if user asked so
207 if (retCode
== wxID_OK
&& HasFlag(wxDD_CHANGE_DIR
))
208 wxSetWorkingDirectory(m_path
);
210 wxDialog::EndModal(retCode
);
213 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
215 EndModal(wxID_CANCEL
);
218 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
220 m_path
= m_input
->GetValue();
222 // Does the path exist? (User may have typed anything in m_input)
223 if (wxDirExists(m_path
))
225 // OK, path exists, we're done.
230 // Interact with user, find out if the dir is a typo or to be created
232 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
234 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
235 wxYES_NO
| wxICON_WARNING
);
237 if ( dialog
.ShowModal() == wxID_YES
)
239 // Okay, let's make it
243 // The new dir was created okay.
250 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
252 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
254 // We still don't have a valid dir. Back to the main dialog.
257 // User has answered NO to create dir.
260 void wxGenericDirDialog::SetPath(const wxString
& path
)
262 m_dirCtrl
->SetPath(path
);
266 wxString
wxGenericDirDialog::GetPath(void) const
271 int wxGenericDirDialog::ShowModal()
273 m_input
->SetValue( m_path
);
274 return wxDialog::ShowModal();
277 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
282 wxTreeItemId item
= event
.GetItem();
284 wxDirItemData
*data
= NULL
;
287 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
290 m_input
->SetValue( data
->m_path
);
293 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
298 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
300 m_input
->SetValue( data
->m_path
);
303 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
308 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
311 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
313 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
314 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
315 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
317 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
318 _("Create directory"), wxOK
| wxICON_INFORMATION
);
323 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
324 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
327 wxString
new_name( _("NewName") );
328 wxString
path( data
->m_path
);
329 if (!wxEndsWithPathSeparator(path
))
330 path
+= wxFILE_SEP_PATH
;
332 if (wxDirExists(path
))
334 // try NewName0, NewName1 etc.
337 new_name
= _("NewName");
339 num
.Printf( wxT("%d"), i
);
343 if (!wxEndsWithPathSeparator(path
))
344 path
+= wxFILE_SEP_PATH
;
347 } while (wxDirExists(path
));
353 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
358 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
360 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
362 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
363 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
364 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
367 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
369 SetPath(wxGetUserHome());
372 #endif // wxUSE_DIRDLG