]>
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"
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 // smartphones 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"));
113 // 0) 'New' and 'Home' Buttons
114 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
116 // VS: 'Home directory' concept is unknown to MS-DOS
117 #if !defined(__DOS__)
118 wxBitmapButton
* homeButton
=
119 new wxBitmapButton(this, ID_GO_HOME
,
120 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
121 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
124 // I'm not convinced we need a New button, and we tend to get annoying
125 // accidental-editing with label editing enabled.
126 if (style
& wxDD_NEW_DIR_BUTTON
)
128 wxBitmapButton
* newButton
=
129 new wxBitmapButton(this, ID_NEW
,
130 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
131 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
133 newButton
->SetToolTip(_("Create new directory"));
138 homeButton
->SetToolTip(_("Go to home directory"));
141 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
143 #endif // __SMARTPHONE__/!__SMARTPHONE__
146 m_dirCtrl
= NULL
; // this is necessary, event handler called from
147 // wxGenericDirCtrl would crash otherwise!
148 long dirStyle
= wxDIRCTRL_DIR_ONLY
| wxDEFAULT_CONTROL_BORDER
;
151 if (style
& wxDD_NEW_DIR_BUTTON
)
153 // Only under Windows do we need the wxTR_EDIT_LABEL tree control style
154 // before we can call EditLabel (required for "New directory")
155 dirStyle
|= wxDIRCTRL_EDIT_LABELS
;
159 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
160 m_path
, wxDefaultPosition
,
164 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
166 #ifndef __SMARTPHONE__
167 // Make the an option depending on a flag?
168 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
169 topsizer
->Add( check
, 0, wxLEFT
|wxRIGHT
|wxTOP
| wxALIGN_RIGHT
, 10 );
170 #endif // !__SMARTPHONE__
173 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
174 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, wxLARGESMALL(10,0) );
177 wxSizer
*buttonSizer
= CreateButtonSizer( wxOK
|wxCANCEL
, true, wxLARGESMALL(10,0) );
178 if(buttonSizer
->GetChildren().GetCount() > 0 )
180 topsizer
->Add( buttonSizer
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
184 topsizer
->AddSpacer( wxLARGESMALL(10,0) );
188 #ifdef __SMARTPHONE__
189 // overwrite menu achieved with earlier CreateButtonSizer() call
190 SetRightMenu(wxID_ANY
, _("Options"), dirMenu
);
195 SetAutoLayout( true );
196 SetSizer( topsizer
);
198 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
199 topsizer
->SetSizeHints( this );
200 topsizer
->Fit( this );
206 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
208 EndModal(wxID_CANCEL
);
211 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
213 m_path
= m_input
->GetValue();
214 // Does the path exist? (User may have typed anything in m_input)
215 if (wxDirExists(m_path
)) {
216 // OK, path exists, we're done.
220 // Interact with user, find out if the dir is a typo or to be created
222 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
224 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
225 wxYES_NO
| wxICON_WARNING
);
227 if ( dialog
.ShowModal() == wxID_YES
) {
228 // Okay, let's make it
230 if (wxMkdir(m_path
)) {
231 // The new dir was created okay.
237 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
239 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
241 // We still don't have a valid dir. Back to the main dialog.
244 // User has answered NO to create dir.
247 void wxGenericDirDialog::SetPath(const wxString
& path
)
249 m_dirCtrl
->SetPath(path
);
253 wxString
wxGenericDirDialog::GetPath(void) const
258 int wxGenericDirDialog::ShowModal()
260 m_input
->SetValue( m_path
);
261 return wxDialog::ShowModal();
264 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
269 wxTreeItemId item
= event
.GetItem();
271 wxDirItemData
*data
= NULL
;
274 data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(item
);
277 m_input
->SetValue( data
->m_path
);
280 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
285 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
287 m_input
->SetValue( data
->m_path
);
290 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
295 m_dirCtrl
->ShowHidden( event
.GetInt() != 0 );
298 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
300 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
301 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
302 (m_dirCtrl
->GetTreeCtrl()->GetItemParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
304 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
305 _("Create directory"), wxOK
| wxICON_INFORMATION
);
310 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetItemParent( id );
311 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
314 wxString
new_name( _("NewName") );
315 wxString
path( data
->m_path
);
316 if (!wxEndsWithPathSeparator(path
))
317 path
+= wxFILE_SEP_PATH
;
319 if (wxDirExists(path
))
321 // try NewName0, NewName1 etc.
324 new_name
= _("NewName");
326 num
.Printf( wxT("%d"), i
);
330 if (!wxEndsWithPathSeparator(path
))
331 path
+= wxFILE_SEP_PATH
;
334 } while (wxDirExists(path
));
340 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
345 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, true );
347 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
349 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
350 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
351 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
354 void wxGenericDirDialog::OnGoHome(wxCommandEvent
& WXUNUSED(event
))
356 SetPath(wxGetUserHome());
359 #endif // wxUSE_DIRDLG