]>
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"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 static const int ID_DIRCTRL
= 1000;
47 static const int ID_TEXTCTRL
= 1001;
48 static const int ID_OK
= 1002;
49 static const int ID_CANCEL
= 1003;
50 static const int ID_NEW
= 1004;
51 static const int ID_SHOW_HIDDEN
= 1005;
53 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
54 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
55 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
56 EVT_BUTTON (ID_NEW
, wxGenericDirDialog::OnNew
)
57 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
58 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
59 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
60 EVT_CHECKBOX (ID_SHOW_HIDDEN
, wxGenericDirDialog::OnShowHidden
)
63 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
64 const wxString
& defaultPath
, long style
,
65 const wxPoint
& pos
, const wxSize
& sz
,
66 const wxString
& name
):
67 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
72 if (m_path
== wxT("~"))
73 wxGetHomeDir(&m_path
);
74 if (m_path
== wxT("."))
77 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
80 m_dirCtrl
= NULL
; // this is neccessary, event handler called from
81 // wxGenericDirCtrl would crash otherwise!
82 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
83 m_path
, wxPoint(5, 5),
85 wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
87 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
89 // Make the an option depending on a flag?
90 wxCheckBox
* check
= new wxCheckBox( this, ID_SHOW_HIDDEN
, _("Show hidden directories") );
91 topsizer
->Add( check
, 0, wxLEFT
|wxTOP
| wxALIGN_RIGHT
, 5 );
94 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
95 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
99 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
103 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
105 // I'm not convinced we need a New button, and we tend to get annoying
106 // accidental-editing with label editing enabled.
107 wxButton
* newButton
= new wxButton( this, ID_NEW
, _("New...") );
108 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
110 // OK and Cancel button should be at the right bottom
111 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
112 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
113 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
114 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
116 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
118 okButton
->SetDefault();
119 m_dirCtrl
->SetFocus();
121 SetAutoLayout( TRUE
);
122 SetSizer( topsizer
);
124 topsizer
->SetSizeHints( this );
125 topsizer
->Fit( this );
130 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
132 EndModal(wxID_CANCEL
);
135 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
137 m_path
= m_input
->GetValue();
138 // Does the path exist? (User may have typed anything in m_input)
139 if (wxPathExists(m_path
)) {
140 // OK, path exists, we're done.
144 // Interact with user, find out if the dir is a typo or to be created
146 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
148 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
149 wxYES_NO
| wxICON_WARNING
);
151 if ( dialog
.ShowModal() == wxID_YES
) {
152 // Okay, let's make it
154 if (wxMkdir(m_path
)) {
155 // The new dir was created okay.
161 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
163 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
165 // We still don't have a valid dir. Back to the main dialog.
168 // User has answered NO to create dir.
171 void wxGenericDirDialog::SetPath(const wxString
& path
)
173 m_dirCtrl
->SetPath(path
);
177 wxString
wxGenericDirDialog::GetPath(void) const
182 int wxGenericDirDialog::ShowModal()
184 m_input
->SetValue( m_path
);
185 return wxDialog::ShowModal();
188 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
193 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
195 m_input
->SetValue( data
->m_path
);
198 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
203 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
205 m_input
->SetValue( data
->m_path
);
208 void wxGenericDirDialog::OnShowHidden( wxCommandEvent
& event
)
213 m_dirCtrl
->ShowHidden( event
.GetInt() );
216 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
218 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
219 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
220 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
222 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
223 _("Create directory"), wxOK
| wxICON_INFORMATION
);
228 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
229 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
232 wxString
new_name( _("NewName") );
233 wxString
path( data
->m_path
);
234 if (path
.Last() != wxFILE_SEP_PATH
)
235 path
+= wxFILE_SEP_PATH
;
237 if (wxFileExists(path
))
239 // try NewName0, NewName1 etc.
242 new_name
= _("NewName");
244 num
.Printf( wxT("%d"), i
);
248 if (path
.Last() != wxFILE_SEP_PATH
)
249 path
+= wxFILE_SEP_PATH
;
252 } while (wxFileExists(path
));
258 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
263 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, TRUE
);
265 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
267 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
268 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
269 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
272 #endif // wxUSE_DIRDLG