]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
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"
31 #include "wx/statline.h"
32 #include "wx/dirctrl.h"
33 #include "wx/generic/dirdlgg.h"
34 #include "wx/artprov.h"
35 #include "wx/bmpbuttn.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 static const int ID_DIRCTRL
= 1000;
42 static const int ID_TEXTCTRL
= 1001;
43 static const int ID_NEW
= 1004;
44 static const int ID_SHOW_HIDDEN
= 1005;
45 static const int ID_GO_HOME
= 1006;
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 /* Macro for avoiding #ifdefs when value have to be different depending on size of
52 device we display on - take it from something like wxDesktopPolicy in the future
55 #if defined(__SMARTPHONE__)
56 #define wxLARGESMALL(large,small) small
58 #define wxLARGESMALL(large,small) large
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog
, wxDialog
)
67 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
68 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
69 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
70 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
71 EVT_BUTTON (ID_GO_HOME
, wxGenericDirDialog::OnGoHome
)
72 EVT_TREE_KEY_DOWN (wxID_ANY
, wxGenericDirDialog::OnTreeKeyDown
)
73 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxGenericDirDialog::OnTreeSelected
)
74 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
75 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
78 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
79 const wxString
& defaultPath
, long style
,
80 const wxPoint
& pos
, const wxSize
& sz
,
81 const wxString
& name
):
82 wxDirDialogBase(parent
, title
, defaultPath
, style
, pos
, sz
, name
)
84 Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
87 bool wxGenericDirDialog::Create(wxWindow
* WXUNUSED(parent
),
88 const wxString
& WXUNUSED(title
),
89 const wxString
& defaultPath
, long style
,
90 const wxPoint
& WXUNUSED(pos
),
91 const wxSize
& WXUNUSED(sz
),
92 const wxString
& WXUNUSED(name
))
97 if (m_path
== wxT("~"))
98 wxGetHomeDir(&m_path
);
99 if (m_path
== wxT("."))
102 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
104 // smartphones does not support or do not waste space for wxButtons
105 #if defined(__SMARTPHONE__)
107 wxMenu
*dirMenu
= new wxMenu
;
108 dirMenu
->Append(ID_GO_HOME
, _("Home"));
110 if (style
& wxDD_NEW_DIR_BUTTON
)
112 dirMenu
->Append(ID_NEW
, _("New directory"));
115 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
116 dirMenu
->AppendSeparator();
117 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
121 // 0) 'New' and 'Home' Buttons
122 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
124 // VS: 'Home directory' concept is unknown to MS-DOS
125 #if !defined(__DOS__)
126 wxBitmapButton
* homeButton
=
127 new wxBitmapButton(this, ID_GO_HOME
,
128 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
129 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
132 // I'm not convinced we need a New button, and we tend to get annoying
133 // accidental-editing with label editing enabled.
134 if (style
& wxDD_NEW_DIR_BUTTON
)
136 wxBitmapButton
* newButton
=
137 new wxBitmapButton(this, ID_NEW
,
138 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
139 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
141 newButton
->SetToolTip(_("Create new directory"));
146 homeButton
->SetToolTip(_("Go to home directory"));
149 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
151 #endif // __SMARTPHONE__/!__SMARTPHONE__
154 m_dirCtrl
= NULL
; // this is necessary, event handler called from
155 // wxGenericDirCtrl would crash otherwise!
156 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
159 if (style
& wxDD_NEW_DIR_BUTTON
)
161 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
162 // before we can call EditLabel (required for "New directory")
163 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
167 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
168 m_path
, wxDefaultPosition
,
172 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
174 #ifndef __SMARTPHONE__
175 // Make the an option depending on a flag?
176 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
177 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
178 #endif // !__SMARTPHONE__
181 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
182 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
185 wxSizer
*buttonSizer
= CreateButtonSizer( wxOK
|wxCANCEL
, true, wxLARGESMALL(10,0) );
186 if(buttonSizer
->GetChildren().GetCount() > 0 )
188 topsizer
->Add( buttonSizer
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
192 topsizer
->AddSpacer( wxLARGESMALL(10,0) );
196 #ifdef __SMARTPHONE__
197 // overwrite menu achieved with earlier CreateButtonSizer() call
198 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
203 SetAutoLayout( true );
204 SetSizer( topsizer
);
206 topsizer
->SetSizeHints( this );
207 topsizer
->Fit( this );
214 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
216 EndModal(wxID_CANCEL
);
219 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
221 m_path
= m_input
->GetValue();
222 // Does the path exist? (User may have typed anything in m_input)
223 if (wxDirExists(m_path
)) {
224 // OK, path exists, we're done.
228 // Interact with user, find out if the dir is a typo or to be created
230 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
232 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
233 wxYES_NO
| wxICON_WARNING
);
235 if ( dialog
.ShowModal() == wxID_YES
) {
236 // Okay, let's make it
238 if (wxMkdir(m_path
)) {
239 // The new dir was created okay.
245 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
247 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
249 // We still don't have a valid dir. Back to the main dialog.
252 // User has answered NO to create dir.
255 void wxGenericDirDialog::SetPath(const wxString
& path
)
257 m_dirCtrl
->SetPath(path
);
261 wxString
wxGenericDirDialog::GetPath(void) const
266 int wxGenericDirDialog::ShowModal()
268 m_input
->SetValue( m_path
);
269 return wxDialog::ShowModal();
272 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
277 wxTreeItemId item
= event
.GetItem();
279 wxDirItemData
*data
= NULL
;
282 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
285 m_input
->SetValue( data
->m_path
);
288 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
293 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
295 m_input
->SetValue( data
->m_path
);
298 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
303 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
306 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
308 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
309 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
310 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
312 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
313 _("Create directory"), wxOK
| wxICON_INFORMATION
);
318 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
319 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
322 wxString
new_name( _("NewName") );
323 wxString
path( data
->m_path
);
324 if (!wxEndsWithPathSeparator(path
))
325 path
+= wxFILE_SEP_PATH
;
327 if (wxDirExists(path
))
329 // try NewName0, NewName1 etc.
332 new_name
= _("NewName");
334 num
.Printf( wxT("%d"), i
);
338 if (!wxEndsWithPathSeparator(path
))
339 path
+= wxFILE_SEP_PATH
;
342 } while (wxDirExists(path
));
348 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
353 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
355 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
357 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
358 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
359 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
362 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
364 SetPath(wxGetUserHome());
367 #endif // wxUSE_DIRDLG