]>
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 /////////////////////////////////////////////////////////////////////////////
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/generic/dirctrlg.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 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
57 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
58 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
59 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
60 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
61 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
62 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
63 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
66 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
67 const wxString
& defaultPath
, long style
,
68 const wxPoint
& pos
, const wxSize
& sz
,
69 const wxString
& name
):
70 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
75 if (m_path
== wxT("~"))
76 wxGetHomeDir(&m_path
);
77 if (m_path
== wxT("."))
80 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
82 // 0) 'New' and 'Home' Buttons
83 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
85 wxBitmapButton
* homeButton
=
86 new wxBitmapButton(this, ID_GO_HOME
,
87 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
88 buttonsizer
->Add( homeButton
, 0, wxLEFT
|wxRIGHT
, 10 );
90 // I'm not convinced we need a New button, and we tend to get annoying
91 // accidental-editing with label editing enabled.
92 wxBitmapButton
* newButton
=
93 new wxBitmapButton(this, ID_NEW
,
94 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
95 buttonsizer
->Add( newButton
, 0, wxRIGHT
, 10 );
98 homeButton
->SetToolTip(_("Go to home directory"));
99 newButton
->SetToolTip(_("Create new directory"));
102 topsizer
->Add( buttonsizer
, 0, wxTOP
| wxALIGN_RIGHT
, 10 );
105 m_dirCtrl
= NULL
; // this is neccessary, event handler called from
106 // wxGenericDirCtrl would crash otherwise!
107 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
108 m_path
, wxPoint(5, 5),
110 wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
112 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
114 // Make the an option depending on a flag?
115 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
116 topsizer
->Add( check
, 0, wxLEFT
|wxTOP
| wxALIGN_RIGHT
, 5 );
119 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
120 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
124 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
128 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
130 // OK and Cancel button should be at the right bottom
131 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
132 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
133 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
134 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
136 topsizer
->Add( buttonsizer
, 0, wxALL
| wxALIGN_RIGHT
, 10 );
138 okButton
->SetDefault();
139 m_dirCtrl
->SetFocus();
141 SetAutoLayout( TRUE
);
142 SetSizer( topsizer
);
144 topsizer
->SetSizeHints( this );
145 topsizer
->Fit( this );
150 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
152 EndModal(wxID_CANCEL
);
155 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
157 m_path
= m_input
->GetValue();
158 // Does the path exist? (User may have typed anything in m_input)
159 if (wxPathExists(m_path
)) {
160 // OK, path exists, we're done.
164 // Interact with user, find out if the dir is a typo or to be created
166 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
168 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
169 wxYES_NO
| wxICON_WARNING
);
171 if ( dialog
.ShowModal() == wxID_YES
) {
172 // Okay, let's make it
174 if (wxMkdir(m_path
)) {
175 // The new dir was created okay.
181 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
183 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
185 // We still don't have a valid dir. Back to the main dialog.
188 // User has answered NO to create dir.
191 void wxGenericDirDialog::SetPath(const wxString
& path
)
193 m_dirCtrl
->SetPath(path
);
197 wxString
wxGenericDirDialog::GetPath(void) const
202 int wxGenericDirDialog::ShowModal()
204 m_input
->SetValue( m_path
);
205 return wxDialog::ShowModal();
208 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
213 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
215 m_input
->SetValue( data
->m_path
);
218 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
223 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
225 m_input
->SetValue( data
->m_path
);
228 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
233 m_dirCtrl
->ShowHidden( event
.GetInt() );
236 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
238 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
239 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
240 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
242 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
243 _("Create directory"), wxOK
| wxICON_INFORMATION
);
248 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
249 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
252 wxString
new_name( _("NewName") );
253 wxString
path( data
->m_path
);
254 if (path
.Last() != wxFILE_SEP_PATH
)
255 path
+= wxFILE_SEP_PATH
;
257 if (wxFileExists(path
))
259 // try NewName0, NewName1 etc.
262 new_name
= _("NewName");
264 num
.Printf( wxT("%d"), i
);
268 if (path
.Last() != wxFILE_SEP_PATH
)
269 path
+= wxFILE_SEP_PATH
;
272 } while (wxFileExists(path
));
278 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
283 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, TRUE
);
285 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
287 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
288 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
289 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
292 #endif // wxUSE_DIRDLG