]>
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"
34 #include "wx/msgdlg.h"
37 #include "wx/statline.h"
38 #include "wx/generic/dirctrlg.h"
39 #include "wx/generic/dirdlgg.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static const int ID_DIRCTRL
= 1000;
46 static const int ID_TEXTCTRL
= 1001;
47 static const int ID_OK
= 1002;
48 static const int ID_CANCEL
= 1003;
49 static const int ID_NEW
= 1004;
50 //static const int ID_CHECK = 1005;
52 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
53 EVT_BUTTON (wxID_OK
, wxGenericDirDialog::OnOK
)
54 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
55 EVT_CLOSE (wxGenericDirDialog::OnCloseWindow
)
56 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
57 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
58 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
61 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
62 const wxString
& defaultPath
, long style
,
63 const wxPoint
& pos
, const wxSize
& sz
,
64 const wxString
& name
):
65 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
70 if (m_path
== wxT("~"))
71 wxGetHomeDir( &m_path
);
73 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
76 m_dirCtrl
= NULL
; // this is neccessary, event handler called from
77 // wxGenericDirCtrl would crash otherwise!
78 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
79 m_path
, wxPoint(5, 5),
81 wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
83 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
86 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
87 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
91 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
95 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
96 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
97 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
98 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
99 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
101 // I'm not convinced we need a New button, and we tend to get annoying
102 // accidental-editing with label editing enabled.
103 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
104 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
106 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
108 okButton
->SetDefault();
109 m_dirCtrl
->SetFocus();
111 SetAutoLayout( TRUE
);
112 SetSizer( topsizer
);
114 topsizer
->SetSizeHints( this );
115 topsizer
->Fit( this );
120 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
122 EndModal(wxID_CANCEL
);
125 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
127 m_path
= m_input
->GetValue();
128 // Does the path exist? (User may have typed anything in m_input)
129 if (wxPathExists(m_path
)) {
130 // OK, path exists, we're done.
134 // Interact with user, find out if the dir is a typo or to be created
136 msg
.Printf(_("The directory '%s' does not exist\nCreate it now?"),
138 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"),
139 wxYES_NO
| wxICON_WARNING
);
141 if ( dialog
.ShowModal() == wxID_YES
) {
142 // Okay, let's make it
144 if (wxMkdir(m_path
)) {
145 // The new dir was created okay.
151 msg
.Printf(_("Failed to create directory '%s'\n(Do you have the required permissions?)"),
153 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
155 // We still don't have a valid dir. Back to the main dialog.
158 // User has answered NO to create dir.
161 void wxGenericDirDialog::SetPath(const wxString
& path
)
163 m_dirCtrl
->SetPath(path
);
167 wxString
wxGenericDirDialog::GetPath(void) const
172 int wxGenericDirDialog::ShowModal()
174 m_input
->SetValue( m_path
);
175 return wxDialog::ShowModal();
178 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
183 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
185 m_input
->SetValue( data
->m_path
);
188 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
193 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
195 m_input
->SetValue( data
->m_path
);
198 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
200 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
201 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
202 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
204 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
205 _("Create directory"), wxOK
| wxICON_INFORMATION
);
210 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
211 wxDirItemData
*data
= (wxDirItemData
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
214 wxString
new_name( _("NewName") );
215 wxString
path( data
->m_path
);
216 if (path
.Last() != wxFILE_SEP_PATH
)
217 path
+= wxFILE_SEP_PATH
;
219 if (wxFileExists(path
))
221 // try NewName0, NewName1 etc.
224 new_name
= _("NewName");
226 num
.Printf( wxT("%d"), i
);
230 if (path
.Last() != wxFILE_SEP_PATH
)
231 path
+= wxFILE_SEP_PATH
;
234 } while (wxFileExists(path
));
240 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
245 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
, TRUE
);
247 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
249 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
250 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
251 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
254 #endif // wxUSE_DIRDLG