]>
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 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
87 if (m_path
== wxT("~"))
88 wxGetHomeDir(&m_path
);
89 if (m_path
== wxT("."))
92 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
94 // smartphones does not support or do not waste space for wxButtons
95 #if defined(__SMARTPHONE__)
97 wxMenu
*dirMenu
= new wxMenu
;
98 dirMenu
->Append(ID_GO_HOME
, _("Home"));
100 if (style
& wxDD_NEW_DIR_BUTTON
)
102 dirMenu
->Append(ID_NEW
, _("New directory"));
105 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
106 dirMenu
->AppendSeparator();
107 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
111 // 0) 'New' and 'Home' Buttons
112 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
114 // VS: 'Home directory' concept is unknown to MS-DOS
115 #if !defined(__DOS__)
116 wxBitmapButton
* homeButton
=
117 new wxBitmapButton(this, ID_GO_HOME
,
118 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
119 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
122 // I'm not convinced we need a New button, and we tend to get annoying
123 // accidental-editing with label editing enabled.
124 if (style
& wxDD_NEW_DIR_BUTTON
)
126 wxBitmapButton
* newButton
=
127 new wxBitmapButton(this, ID_NEW
,
128 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
129 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
131 newButton
->SetToolTip(_("Create new directory"));
136 homeButton
->SetToolTip(_("Go to home directory"));
139 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
141 #endif // __SMARTPHONE__/!__SMARTPHONE__
144 m_dirCtrl
= NULL
; // this is necessary, event handler called from
145 // wxGenericDirCtrl would crash otherwise!
146 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
149 if (style
& wxDD_NEW_DIR_BUTTON
)
151 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
152 // before we can call EditLabel (required for "New directory")
153 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
157 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
158 m_path
, wxDefaultPosition
,
162 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
164 #ifndef __SMARTPHONE__
165 // Make the an option depending on a flag?
166 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
167 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
168 #endif // !__SMARTPHONE__
171 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
172 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
175 wxSizer
*buttonSizer
= CreateButtonSizer( wxOK
|wxCANCEL
, true, wxLARGESMALL(10,0) );
176 if(buttonSizer
->GetChildren().GetCount() > 0 )
178 topsizer
->Add( buttonSizer
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
182 topsizer
->AddSpacer( wxLARGESMALL(10,0) );
186 #ifdef __SMARTPHONE__
187 // overwrite menu achieved with earlier CreateButtonSizer() call
188 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
193 SetAutoLayout( true );
194 SetSizer( topsizer
);
196 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
197 topsizer
->SetSizeHints( this );
198 topsizer
->Fit( this );
204 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
206 EndModal(wxID_CANCEL
);
209 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
211 m_path
= m_input
->GetValue();
212 // Does the path exist? (User may have typed anything in m_input)
213 if (wxDirExists(m_path
)) {
214 // OK, path exists, we're done.
218 // Interact with user, find out if the dir is a typo or to be created
220 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
222 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
223 wxYES_NO
| wxICON_WARNING
);
225 if ( dialog
.ShowModal() == wxID_YES
) {
226 // Okay, let's make it
228 if (wxMkdir(m_path
)) {
229 // The new dir was created okay.
235 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
237 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
239 // We still don't have a valid dir. Back to the main dialog.
242 // User has answered NO to create dir.
245 void wxGenericDirDialog::SetPath(const wxString
& path
)
247 m_dirCtrl
->SetPath(path
);
251 wxString
wxGenericDirDialog::GetPath(void) const
256 int wxGenericDirDialog::ShowModal()
258 m_input
->SetValue( m_path
);
259 return wxDialog::ShowModal();
262 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
267 wxTreeItemId item
= event
.GetItem();
269 wxDirItemData
*data
= NULL
;
272 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
275 m_input
->SetValue( data
->m_path
);
278 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
283 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
285 m_input
->SetValue( data
->m_path
);
288 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
293 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
296 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
298 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
299 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
300 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
302 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
303 _("Create directory"), wxOK
| wxICON_INFORMATION
);
308 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
309 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
312 wxString
new_name( _("NewName") );
313 wxString
path( data
->m_path
);
314 if (!wxEndsWithPathSeparator(path
))
315 path
+= wxFILE_SEP_PATH
;
317 if (wxDirExists(path
))
319 // try NewName0, NewName1 etc.
322 new_name
= _("NewName");
324 num
.Printf( wxT("%d"), i
);
328 if (!wxEndsWithPathSeparator(path
))
329 path
+= wxFILE_SEP_PATH
;
332 } while (wxDirExists(path
));
338 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
343 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
345 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
347 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
348 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
349 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
352 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
354 SetPath(wxGetUserHome());
357 #endif // wxUSE_DIRDLG