]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
cca162da5d3b8768cd9b26d671df114ac76f1318
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/textctrl.h"
25 #include "wx/button.h"
26 #include "wx/checkbox.h"
30 #include "wx/msgdlg.h"
33 #include "wx/statline.h"
34 #include "wx/dirctrl.h"
35 #include "wx/generic/dirdlgg.h"
36 #include "wx/artprov.h"
37 #include "wx/bmpbuttn.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 static const int ID_DIRCTRL
= 1000;
44 static const int ID_TEXTCTRL
= 1001;
45 static const int ID_NEW
= 1004;
46 static const int ID_SHOW_HIDDEN
= 1005;
47 static const int ID_GO_HOME
= 1006;
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 /* Macro for avoiding #ifdefs when value have to be different depending on size of
54 device we display on - take it from something like wxDesktopPolicy in the future
57 #if defined(__SMARTPHONE__)
58 #define wxLARGESMALL(large,small) small
60 #define wxLARGESMALL(large,small) large
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirDialog
, wxDialog
)
69 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
70 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
71 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
72 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
73 EVT_BUTTON (ID_GO_HOME
, wxGenericDirDialog::OnGoHome
)
74 EVT_TREE_KEY_DOWN (wxID_ANY
, wxGenericDirDialog::OnTreeKeyDown
)
75 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxGenericDirDialog::OnTreeSelected
)
76 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
77 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
80 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
81 const wxString
& defaultPath
, long style
,
82 const wxPoint
& pos
, const wxSize
& sz
,
83 const wxString
& name
):
84 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
89 if (m_path
== wxT("~"))
90 wxGetHomeDir(&m_path
);
91 if (m_path
== wxT("."))
94 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
96 // smart phones does not support or do not waste space for wxButtons
97 #if defined(__SMARTPHONE__)
99 wxMenu
*dirMenu
= new wxMenu
;
100 dirMenu
->Append(ID_GO_HOME
, _("Home"));
102 if (style
& wxDD_NEW_DIR_BUTTON
)
104 dirMenu
->Append(ID_NEW
, _("New directory"));
107 dirMenu
->AppendCheckItem(ID_SHOW_HIDDEN
, _("Show hidden directories"));
108 dirMenu
->AppendSeparator();
109 dirMenu
->Append(wxID_CANCEL
, _("Cancel"));
111 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
115 // 0) 'New' and 'Home' Buttons
116 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
118 // VS: 'Home directory' concept is unknown to MS-DOS
119 #if !defined(__DOS__)
120 wxBitmapButton
* homeButton
=
121 new wxBitmapButton(this, ID_GO_HOME
,
122 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
123 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
126 // I'm not convinced we need a New button, and we tend to get annoying
127 // accidental-editing with label editing enabled.
128 if (style
& wxDD_NEW_DIR_BUTTON
)
130 wxBitmapButton
* newButton
=
131 new wxBitmapButton(this, ID_NEW
,
132 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
133 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
135 newButton
->SetToolTip(_("Create new directory"));
140 homeButton
->SetToolTip(_("Go to home directory"));
143 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
145 #endif // __SMARTPHONE__/!__SMARTPHONE__
148 m_dirCtrl
= NULL
; // this is necessary, event handler called from
149 // wxGenericDirCtrl would crash otherwise!
150 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
153 if (style
& wxDD_NEW_DIR_BUTTON
)
155 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
156 // before we can call EditLabel (required for "New directory")
157 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
161 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
162 m_path
, wxDefaultPosition
,
166 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
168 #ifndef __SMARTPHONE__
169 // Make the an option depending on a flag?
170 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
171 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
172 #endif // !__SMARTPHONE__
175 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
176 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
178 #ifndef __SMARTPHONE__
182 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
186 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxEXPAND
| wxALL
, 10 );
188 #endif // !__SMARTPHONE__
192 SetAutoLayout( true );
193 SetSizer( topsizer
);
195 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
196 topsizer
->SetSizeHints( this );
197 topsizer
->Fit( this );
203 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
205 EndModal(wxID_CANCEL
);
208 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
210 m_path
= m_input
->GetValue();
211 // Does the path exist? (User may have typed anything in m_input)
212 if (wxDirExists(m_path
)) {
213 // OK, path exists, we're done.
217 // Interact with user, find out if the dir is a typo or to be created
219 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
221 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
222 wxYES_NO
| wxICON_WARNING
);
224 if ( dialog
.ShowModal() == wxID_YES
) {
225 // Okay, let's make it
227 if (wxMkdir(m_path
)) {
228 // The new dir was created okay.
234 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
236 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
238 // We still don't have a valid dir. Back to the main dialog.
241 // User has answered NO to create dir.
244 void wxGenericDirDialog::SetPath(const wxString
& path
)
246 m_dirCtrl
->SetPath(path
);
250 wxString
wxGenericDirDialog::GetPath(void) const
255 int wxGenericDirDialog::ShowModal()
257 m_input
->SetValue( m_path
);
258 return wxDialog::ShowModal();
261 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
266 wxTreeItemId item
= event
.GetItem();
268 wxDirItemData
*data
= NULL
;
271 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
274 m_input
->SetValue( data
->m_path
);
277 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
282 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
284 m_input
->SetValue( data
->m_path
);
287 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
292 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
295 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
297 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
298 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
299 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
301 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
302 _("Create directory"), wxOK
| wxICON_INFORMATION
);
307 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
308 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
311 wxString
new_name( _("NewName") );
312 wxString
path( data
->m_path
);
313 if (!wxEndsWithPathSeparator(path
))
314 path
+= wxFILE_SEP_PATH
;
316 if (wxDirExists(path
))
318 // try NewName0, NewName1 etc.
321 new_name
= _("NewName");
323 num
.Printf( wxT("%d"), i
);
327 if (!wxEndsWithPathSeparator(path
))
328 path
+= wxFILE_SEP_PATH
;
331 } while (wxDirExists(path
));
337 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
342 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
344 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
346 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
347 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
348 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
351 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
353 SetPath(wxGetUserHome());
356 #endif // wxUSE_DIRDLG