]>
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"
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"
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
,
83 Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
86 bool wxGenericDirDialog::Create(wxWindow
* parent
,
87 const wxString
& title
,
88 const wxString
& defaultPath
, long style
,
95 if (!wxDirDialogBase::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
))
99 if (m_path
== wxT("~"))
100 wxGetHomeDir(&m_path
);
101 if (m_path
== wxT("."))
104 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
106 // smartphones does not support or do not waste space for wxButtons
107 #if defined(__SMARTPHONE__)
109 wxMenu
*dirMenu
= new wxMenu
;
110 dirMenu
->Append(ID_GO_HOME
, _("Home"));
112 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
114 dirMenu
->Append(ID_NEW
, _("New directory"));
117 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
118 dirMenu
->AppendSeparator();
119 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
123 // 0) 'New' and 'Home' Buttons
124 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
126 // VS: 'Home directory' concept is unknown to MS-DOS
127 #if !defined(__DOS__)
128 wxBitmapButton
* homeButton
=
129 new wxBitmapButton(this, ID_GO_HOME
,
130 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
131 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
134 // I'm not convinced we need a New button, and we tend to get annoying
135 // accidental-editing with label editing enabled.
136 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
138 wxBitmapButton
* newButton
=
139 new wxBitmapButton(this, ID_NEW
,
140 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
141 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
143 newButton
->SetToolTip(_("Create new directory"));
148 homeButton
->SetToolTip(_("Go to home directory"));
151 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
153 #endif // __SMARTPHONE__/!__SMARTPHONE__
156 m_dirCtrl
= NULL
; // this is necessary, event handler called from
157 // wxGenericDirCtrl would crash otherwise!
158 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
161 if (!HasFlag(wxDD_DIR_MUST_EXIST
))
163 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
164 // before we can call EditLabel (required for "New directory")
165 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
169 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
170 m_path
, wxDefaultPosition
,
174 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
176 #ifndef __SMARTPHONE__
177 // Make the an option depending on a flag?
178 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
179 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
180 #endif // !__SMARTPHONE__
183 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
184 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
187 wxSizer
*buttonSizer
= CreateButtonSizer( wxOK
|wxCANCEL
, true, wxLARGESMALL(10,0) );
188 if(buttonSizer
->GetChildren().GetCount() > 0 )
190 topsizer
->Add( buttonSizer
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
194 topsizer
->AddSpacer( wxLARGESMALL(10,0) );
198 #ifdef __SMARTPHONE__
199 // overwrite menu achieved with earlier CreateButtonSizer() call
200 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
205 SetAutoLayout( true );
206 SetSizer( topsizer
);
208 topsizer
->SetSizeHints( this );
209 topsizer
->Fit( this );
216 void wxGenericDirDialog::EndModal(int retCode
)
218 // before proceeding, change the current working directory if user asked so
219 if (retCode
== wxID_OK
&& HasFlag(wxDD_CHANGE_DIR
))
220 wxSetWorkingDirectory(m_path
);
222 wxDialog::EndModal(retCode
);
225 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
227 EndModal(wxID_CANCEL
);
230 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
232 m_path
= m_input
->GetValue();
234 // Does the path exist? (User may have typed anything in m_input)
235 if (wxDirExists(m_path
))
237 // OK, path exists, we're done.
242 // Interact with user, find out if the dir is a typo or to be created
244 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
246 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
247 wxYES_NO
| wxICON_WARNING
);
249 if ( dialog
.ShowModal() == wxID_YES
)
251 // Okay, let's make it
255 // The new dir was created okay.
262 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
264 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
266 // We still don't have a valid dir. Back to the main dialog.
269 // User has answered NO to create dir.
272 void wxGenericDirDialog::SetPath(const wxString
& path
)
274 m_dirCtrl
->SetPath(path
);
278 wxString
wxGenericDirDialog::GetPath(void) const
283 int wxGenericDirDialog::ShowModal()
285 m_input
->SetValue( m_path
);
286 return wxDialog::ShowModal();
289 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
294 wxTreeItemId item
= event
.GetItem();
296 wxDirItemData
*data
= NULL
;
299 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
302 m_input
->SetValue( data
->m_path
);
305 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
310 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
312 m_input
->SetValue( data
->m_path
);
315 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
320 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
323 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
325 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
326 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
327 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
329 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
330 _("Create directory"), wxOK
| wxICON_INFORMATION
);
335 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
336 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
339 wxString
new_name( _("NewName") );
340 wxString
path( data
->m_path
);
341 if (!wxEndsWithPathSeparator(path
))
342 path
+= wxFILE_SEP_PATH
;
344 if (wxDirExists(path
))
346 // try NewName0, NewName1 etc.
349 new_name
= _("NewName");
351 num
.Printf( wxT("%d"), i
);
355 if (!wxEndsWithPathSeparator(path
))
356 path
+= wxFILE_SEP_PATH
;
359 } while (wxDirExists(path
));
365 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
370 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
372 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
374 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
375 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
376 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
379 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
381 SetPath(wxGetUserHome());
384 #endif // wxUSE_DIRDLG