]>
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 static const int ID_DIRCTRL
= 1000;
49 static const int ID_TEXTCTRL
= 1001;
50 static const int ID_OK
= 1002;
51 static const int ID_CANCEL
= 1003;
52 static const int ID_NEW
= 1004;
53 static const int ID_SHOW_HIDDEN
= 1005;
54 static const int ID_GO_HOME
= 1006;
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 /* Macro for avoiding #ifdefs when value have to be different depending on size of
61 device we display on - take it from something like wxDesktopPolicy in the future
64 #if defined(__SMARTPHONE__)
65 #define wxLARGESMALL(large,small) small
67 #define wxLARGESMALL(large,small) large
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog
, wxDialog
)
76 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
77 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
78 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
79 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
80 EVT_BUTTON (ID_GO_HOME
, wxGenericDirDialog::OnGoHome
)
81 EVT_TREE_KEY_DOWN (wxID_ANY
, wxGenericDirDialog::OnTreeKeyDown
)
82 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxGenericDirDialog::OnTreeSelected
)
83 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
84 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
87 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
88 const wxString
& defaultPath
, long style
,
89 const wxPoint
& pos
, const wxSize
& sz
,
90 const wxString
& name
):
91 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
96 if (m_path
== wxT("~"))
97 wxGetHomeDir(&m_path
);
98 if (m_path
== wxT("."))
101 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
103 // smart phones does not support or do not waste space for wxButtons
104 #if defined(__SMARTPHONE__)
106 wxMenu
*dirMenu
= new wxMenu
;
107 dirMenu
->Append(ID_GO_HOME
, _("Home"));
109 if (style
& wxDD_NEW_DIR_BUTTON
)
111 dirMenu
->Append(ID_NEW
, _("New directory"));
114 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
115 dirMenu
->AppendSeparator();
116 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
118 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
122 // 0) 'New' and 'Home' Buttons
123 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
125 // VS: 'Home directory' concept is unknown to MS-DOS
126 #if !defined(__DOS__)
127 wxBitmapButton
* homeButton
=
128 new wxBitmapButton(this, ID_GO_HOME
,
129 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
130 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
133 // I'm not convinced we need a New button, and we tend to get annoying
134 // accidental-editing with label editing enabled.
135 if (style
& wxDD_NEW_DIR_BUTTON
)
137 wxBitmapButton
* newButton
=
138 new wxBitmapButton(this, ID_NEW
,
139 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
140 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
142 newButton
->SetToolTip(_("Create new directory"));
147 homeButton
->SetToolTip(_("Go to home directory"));
150 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
152 #endif // __SMARTPHONE__/!__SMARTPHONE__
155 m_dirCtrl
= NULL
; // this is neccessary, event handler called from
156 // wxGenericDirCtrl would crash otherwise!
157 long dirStyle
= wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
;
160 if (style
& wxDD_NEW_DIR_BUTTON
)
162 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
163 // before we can call EditLabel (required for "New directory")
164 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
168 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
169 m_path
, wxDefaultPosition
,
173 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
175 #ifndef __SMARTPHONE__
176 // Make the an option depending on a flag?
177 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
178 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
179 #endif // !__SMARTPHONE__
182 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
183 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
185 #ifndef __SMARTPHONE__
189 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
193 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
195 // OK and Cancel button should be at the right bottom
196 wxButton
* okButton
= new wxButton(this, wxID_OK
);
197 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
198 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
);
199 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
201 topsizer
->Add( buttonsizer
, 0, wxLEFT
|wxTOP
|wxBOTTOM
| wxALIGN_RIGHT
, 10 );
203 okButton
->SetDefault();
205 #endif // !__SMARTPHONE__
209 SetAutoLayout( true );
210 SetSizer( topsizer
);
212 topsizer
->SetSizeHints( this );
213 topsizer
->Fit( this );
218 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
220 EndModal(wxID_CANCEL
);
223 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
225 m_path
= m_input
->GetValue();
226 // Does the path exist? (User may have typed anything in m_input)
227 if (wxPathExists(m_path
)) {
228 // OK, path exists, we're done.
232 // Interact with user, find out if the dir is a typo or to be created
234 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
236 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
237 wxYES_NO
| wxICON_WARNING
);
239 if ( dialog
.ShowModal() == wxID_YES
) {
240 // Okay, let's make it
242 if (wxMkdir(m_path
)) {
243 // The new dir was created okay.
249 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
251 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
253 // We still don't have a valid dir. Back to the main dialog.
256 // User has answered NO to create dir.
259 void wxGenericDirDialog::SetPath(const wxString
& path
)
261 m_dirCtrl
->SetPath(path
);
265 wxString
wxGenericDirDialog::GetPath(void) const
270 int wxGenericDirDialog::ShowModal()
272 m_input
->SetValue( m_path
);
273 return wxDialog::ShowModal();
276 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
281 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
283 m_input
->SetValue( data
->m_path
);
286 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
291 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
293 m_input
->SetValue( data
->m_path
);
296 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
301 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
304 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
306 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
307 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
308 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
310 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
311 _("Create directory"), wxOK
| wxICON_INFORMATION
);
316 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
317 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
320 wxString
new_name( _("NewName") );
321 wxString
path( data
->m_path
);
322 if (!wxEndsWithPathSeparator(path
))
323 path
+= wxFILE_SEP_PATH
;
325 if (wxFileExists(path
))
327 // try NewName0, NewName1 etc.
330 new_name
= _("NewName");
332 num
.Printf( wxT("%d"), i
);
336 if (!wxEndsWithPathSeparator(path
))
337 path
+= wxFILE_SEP_PATH
;
340 } while (wxFileExists(path
));
346 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
351 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
353 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
355 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
356 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
357 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
360 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
362 SetPath(wxGetUserHome());
365 #endif // wxUSE_DIRDLG