]>
Commit | Line | Data |
---|---|---|
ec376c8f VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/generic/filepickerg.cpp | |
3 | // Purpose: wxGenericFileDirButton class implementation | |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 15/04/2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Francesco Montorsi | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
c757b5fe | 27 | #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL |
ec376c8f | 28 | |
75cb911c | 29 | #include "wx/filename.h" |
ec376c8f | 30 | #include "wx/filepicker.h" |
ec376c8f | 31 | |
b9bb74e9 VZ |
32 | #include "wx/scopedptr.h" |
33 | ||
ec376c8f VZ |
34 | |
35 | // ============================================================================ | |
36 | // implementation | |
37 | // ============================================================================ | |
38 | ||
ec376c8f VZ |
39 | IMPLEMENT_DYNAMIC_CLASS(wxGenericFileButton, wxButton) |
40 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDirButton, wxButton) | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // wxGenericFileButton | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
68623b59 VZ |
46 | bool wxGenericFileDirButton::Create(wxWindow *parent, |
47 | wxWindowID id, | |
48 | const wxString& label, | |
49 | const wxString& path, | |
50 | const wxString& message, | |
51 | const wxString& wildcard, | |
52 | const wxPoint& pos, | |
53 | const wxSize& size, | |
8eca1205 | 54 | long style, |
68623b59 VZ |
55 | const wxValidator& validator, |
56 | const wxString& name) | |
ec376c8f | 57 | { |
8eca1205 VZ |
58 | m_pickerStyle = style; |
59 | ||
75bc8b34 VZ |
60 | // If the special wxPB_SMALL flag is used, ignore the provided label and |
61 | // use the shortest possible label and the smallest possible button fitting | |
62 | // it. | |
63 | long styleButton = 0; | |
64 | wxString labelButton; | |
65 | if ( m_pickerStyle & wxPB_SMALL ) | |
66 | { | |
67 | labelButton = _("..."); | |
68 | styleButton = wxBU_EXACTFIT; | |
69 | } | |
70 | else | |
71 | { | |
72 | labelButton = label; | |
73 | } | |
74 | ||
ec376c8f | 75 | // create this button |
75bc8b34 VZ |
76 | if ( !wxButton::Create(parent, id, labelButton, |
77 | pos, size, styleButton, validator, name) ) | |
ec376c8f VZ |
78 | { |
79 | wxFAIL_MSG( wxT("wxGenericFileButton creation failed") ); | |
80 | return false; | |
81 | } | |
82 | ||
83 | // and handle user clicks on it | |
29f571de | 84 | Connect(GetId(), wxEVT_COMMAND_BUTTON_CLICKED, |
ec376c8f VZ |
85 | wxCommandEventHandler(wxGenericFileDirButton::OnButtonClick), |
86 | NULL, this); | |
87 | ||
88 | // create the dialog associated with this button | |
89 | m_path = path; | |
556151f5 MW |
90 | m_message = message; |
91 | m_wildcard = wildcard; | |
92 | ||
93 | return true; | |
ec376c8f VZ |
94 | } |
95 | ||
96 | void wxGenericFileDirButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev)) | |
97 | { | |
b9bb74e9 | 98 | wxScopedPtr<wxDialog> p(CreateDialog()); |
556151f5 | 99 | if (p->ShowModal() == wxID_OK) |
ec376c8f | 100 | { |
556151f5 | 101 | // save updated path in m_path |
b9bb74e9 | 102 | UpdatePathFromDialog(p.get()); |
ec376c8f VZ |
103 | |
104 | // fire an event | |
105 | wxFileDirPickerEvent event(GetEventType(), this, GetId(), m_path); | |
106 | GetEventHandler()->ProcessEvent(event); | |
107 | } | |
108 | } | |
109 | ||
75cb911c VZ |
110 | void wxGenericFileDirButton::SetInitialDirectory(const wxString& dir) |
111 | { | |
112 | m_initialDir = dir; | |
113 | } | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // wxGenericFileutton | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | void | |
120 | wxGenericFileButton::DoSetInitialDirectory(wxFileDialog* dialog, | |
121 | const wxString& dir) | |
122 | { | |
123 | if ( m_path.find_first_of(wxFileName::GetPathSeparators()) == | |
124 | wxString::npos ) | |
125 | { | |
126 | dialog->SetDirectory(dir); | |
127 | } | |
128 | } | |
129 | ||
130 | wxDialog *wxGenericFileButton::CreateDialog() | |
131 | { | |
132 | wxFileDialog* const dialog = new wxFileDialog | |
133 | ( | |
134 | GetDialogParent(), | |
135 | m_message, | |
136 | wxEmptyString, | |
137 | wxEmptyString, | |
138 | m_wildcard, | |
139 | GetDialogStyle() | |
140 | ); | |
141 | ||
75cb911c VZ |
142 | // If there is no default file or if it doesn't have any path, use the |
143 | // explicitly set initial directory. | |
74bde874 VZ |
144 | // |
145 | // Notice that it is important to call this before SetPath() below as if we | |
146 | // do have m_initialDir and no directory in m_path, we need to interpret | |
147 | // the path as being relative with respect to m_initialDir. | |
75cb911c VZ |
148 | if ( !m_initialDir.empty() ) |
149 | DoSetInitialDirectory(dialog, m_initialDir); | |
150 | ||
74bde874 VZ |
151 | // This sets both the default file name and the default directory of the |
152 | // dialog if m_path contains directory part. | |
153 | dialog->SetPath(m_path); | |
154 | ||
75cb911c VZ |
155 | return dialog; |
156 | } | |
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // wxGenericDirButton | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | wxDialog *wxGenericDirButton::CreateDialog() | |
163 | { | |
164 | wxDirDialog* const dialog = new wxDirDialog | |
165 | ( | |
166 | GetDialogParent(), | |
167 | m_message, | |
168 | m_path.empty() ? m_initialDir : m_path, | |
169 | GetDialogStyle() | |
170 | ); | |
171 | return dialog; | |
172 | } | |
173 | ||
ec376c8f | 174 | #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL |