]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: extdlgg.cpp | |
3 | // Purpose: extended generic dialog | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "extdlgg.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include <stdio.h> | |
25 | #include "wx/intl.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/button.h" | |
28 | #endif | |
29 | ||
30 | #if wxUSE_STATLINE | |
31 | #include "wx/statline.h" | |
32 | #endif | |
33 | ||
34 | #include "wx/generic/extdlgg.h" | |
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | // wxExtDialog | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | #define STATIC_LINE_MARGIN 15 | |
41 | #define CLIENT_AREA_MARGIN 10 | |
42 | #define BUTTON_AREA_MARGIN 10 | |
43 | ||
44 | #if !USE_SHARED_LIBRARY | |
45 | IMPLEMENT_DYNAMIC_CLASS(wxExtDialog, wxDialog) | |
46 | ||
47 | BEGIN_EVENT_TABLE(wxExtDialog, wxDialog) | |
48 | EVT_SIZE(wxExtDialog::OnSize) | |
49 | EVT_BUTTON(wxID_YES, wxExtDialog::OnYes) | |
50 | EVT_BUTTON(wxID_NO, wxExtDialog::OnNo) | |
51 | EVT_BUTTON(wxID_CANCEL, wxExtDialog::OnCancel) | |
52 | END_EVENT_TABLE() | |
53 | #endif | |
54 | ||
55 | wxExtDialog::wxExtDialog( wxWindow *parent, wxWindowID id, | |
56 | const wxString& title, long extraStyle, | |
57 | const wxPoint& pos, const wxSize& size, | |
58 | long style, const wxString &name ) | |
59 | { | |
60 | Create( parent, id, title, extraStyle, pos, size, style, name ); | |
61 | } | |
62 | ||
63 | bool wxExtDialog::Create( wxWindow *parent, wxWindowID id, | |
64 | const wxString& title, long extraStyle, | |
65 | const wxPoint& pos, const wxSize& size, | |
66 | long style, const wxString &name ) | |
67 | { | |
68 | if (!wxDialog::Create( parent, id, title, pos, size, style, name )) | |
69 | return FALSE; | |
70 | ||
71 | m_extraStyle = extraStyle; | |
72 | ||
73 | wxButton *ok = (wxButton *) NULL; | |
74 | wxButton *cancel = (wxButton *) NULL; | |
75 | wxButton *yes = (wxButton *) NULL; | |
76 | wxButton *no = (wxButton *) NULL; | |
77 | ||
78 | ||
79 | if (m_extraStyle & wxYES_NO) | |
80 | { | |
81 | yes = new wxButton( this, wxID_YES, _("Yes") ); | |
82 | m_buttons.Append( yes ); | |
83 | no = new wxButton( this, wxID_NO, _("No") ); | |
84 | m_buttons.Append( no ); | |
85 | } | |
86 | ||
87 | if (m_extraStyle & wxYES) | |
88 | { | |
89 | yes = new wxButton( this, wxID_YES, _("Yes") ); | |
90 | m_buttons.Append( yes ); | |
91 | } | |
92 | ||
93 | if (m_extraStyle & wxNO) | |
94 | { | |
95 | no = new wxButton( this, wxID_NO, _("No") ); | |
96 | m_buttons.Append( no ); | |
97 | } | |
98 | ||
99 | if (m_extraStyle & wxOK) | |
100 | { | |
101 | ok = new wxButton( this, wxID_OK, _("OK") ); | |
102 | m_buttons.Append( ok ); | |
103 | } | |
104 | ||
105 | if (m_extraStyle & wxFORWARD) | |
106 | AddButton( new wxButton( this, wxID_FORWARD, _("Forward") ) ); | |
107 | ||
108 | if (m_extraStyle & wxBACKWARD) | |
109 | AddButton( new wxButton( this, wxID_BACKWARD, _("Backward") ) ); | |
110 | ||
111 | if (m_extraStyle & wxSETUP) | |
112 | AddButton( new wxButton( this, wxID_SETUP, _("Setup") ) ); | |
113 | ||
114 | if (m_extraStyle & wxMORE) | |
115 | AddButton( new wxButton( this, wxID_MORE, _("More..") ) ); | |
116 | ||
117 | if (m_extraStyle & wxCANCEL) | |
118 | { | |
119 | cancel = new wxButton( this, wxID_CANCEL, _("Cancel") ); | |
120 | m_buttons.Append( cancel ); | |
121 | } | |
122 | ||
123 | if ((m_extraStyle & wxNO_DEFAULT) == 0) | |
124 | { | |
125 | if (ok) | |
126 | { | |
127 | ok->SetDefault(); | |
128 | ok->SetFocus(); | |
129 | } | |
130 | else if (yes) | |
131 | { | |
132 | yes->SetDefault(); | |
133 | yes->SetFocus(); | |
134 | } | |
135 | } | |
136 | ||
137 | #if wxUSE_STATLINE | |
138 | if (style & wxED_STATIC_LINE) | |
139 | { | |
140 | int line_style = wxLI_HORIZONTAL; | |
141 | if (style & wxED_BUTTONS_RIGHT) line_style = wxLI_VERTICAL; | |
142 | ||
143 | m_statLine = new wxStaticLine( this, -1, wxDefaultPosition, wxDefaultSize, line_style ); | |
144 | } | |
145 | else | |
146 | m_statLine = (wxStaticLine*) NULL; | |
147 | #endif | |
148 | ||
149 | if (m_extraStyle & wxCENTRE) | |
150 | Centre( wxBOTH ); | |
151 | ||
152 | return TRUE; | |
153 | } | |
154 | ||
155 | void wxExtDialog::AddButton( wxButton *button ) | |
156 | { | |
157 | m_buttons.Append( button ); | |
158 | } | |
159 | ||
160 | void wxExtDialog::SetDefaultButton( wxWindowID button ) | |
161 | { | |
162 | wxNode *node = m_buttons.First(); | |
163 | while (node) | |
164 | { | |
165 | wxButton *but = (wxButton*) node->Data(); | |
166 | if (but->GetId() == button) | |
167 | { | |
168 | but->SetDefault(); | |
169 | but->SetFocus(); | |
170 | return; | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | void wxExtDialog::EnableButton( wxWindowID button, bool enable ) | |
176 | { | |
177 | wxNode *node = m_buttons.First(); | |
178 | while (node) | |
179 | { | |
180 | wxButton *but = (wxButton*) node->Data(); | |
181 | if (but->GetId() == button) | |
182 | { | |
183 | but->Enable(enable); | |
184 | return; | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | bool wxExtDialog::ButtonIsEnabled( wxWindowID button ) | |
190 | { | |
191 | wxNode *node = m_buttons.First(); | |
192 | while (node) | |
193 | { | |
194 | wxButton *but = (wxButton*) node->Data(); | |
195 | if (but->GetId() == button) | |
196 | return but->IsEnabled(); | |
197 | } | |
198 | return FALSE; | |
199 | } | |
200 | ||
201 | void wxExtDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
202 | { | |
203 | wxSize client_size( GetClientSize() ); | |
204 | wxSize button_area( LayoutButtons() ); | |
205 | ||
206 | if (HasFlag(wxED_BUTTONS_RIGHT)) | |
207 | client_size.x -= button_area.x; | |
208 | else | |
209 | client_size.y -= button_area.y; | |
210 | ||
211 | if (m_clientWindow) | |
212 | { | |
213 | if (m_windowStyle & wxED_CLIENT_MARGIN) | |
214 | m_clientWindow->SetSize( CLIENT_AREA_MARGIN, | |
215 | CLIENT_AREA_MARGIN, | |
216 | client_size.x - 2*CLIENT_AREA_MARGIN, | |
217 | client_size.y - 2*CLIENT_AREA_MARGIN ); | |
218 | else | |
219 | m_clientWindow->SetSize( 0, 0, client_size.x, client_size.y ); | |
220 | ||
221 | if (m_clientWindow->GetAutoLayout()) | |
222 | m_clientWindow->Layout(); | |
223 | } | |
224 | } | |
225 | ||
226 | void wxExtDialog::OnYes(wxCommandEvent& event) | |
227 | { | |
228 | EndModal( wxID_YES ); | |
229 | } | |
230 | ||
231 | void wxExtDialog::OnNo(wxCommandEvent& event) | |
232 | { | |
233 | EndModal( wxID_NO ); | |
234 | } | |
235 | ||
236 | void wxExtDialog::OnCancel(wxCommandEvent& event) | |
237 | { | |
238 | /* allow cancellation via ESC/Close button except if | |
239 | only YES and NO are specified. */ | |
240 | if ((m_extraStyle & wxYES_NO) != wxYES_NO || (m_extraStyle & wxCANCEL)) | |
241 | { | |
242 | EndModal( wxID_CANCEL ); | |
243 | } | |
244 | } | |
245 | ||
246 | wxSize wxExtDialog::GetButtonAreaSize() | |
247 | { | |
248 | if (m_buttons.GetCount() == 0) return wxSize(0,0); | |
249 | ||
250 | wxSize ret(0,0); | |
251 | ||
252 | // this routine can be improved to measure the string length | |
253 | // of the button text or the bitmap size if using wxBmpButton | |
254 | // or to query the standard button size somehow. | |
255 | ||
256 | int button_size_and_margin_x = 110; | |
257 | int button_size_and_margin_y = 44; | |
258 | ||
259 | if (m_windowStyle & wxED_BUTTONS_RIGHT) | |
260 | { | |
261 | ret.x = button_size_and_margin_x; | |
262 | ret.y = m_buttons.GetCount()*button_size_and_margin_y + 2*BUTTON_AREA_MARGIN; | |
263 | #if wxUSE_STATLINE | |
264 | if (m_statLine) | |
265 | ret.x += STATIC_LINE_MARGIN; | |
266 | #endif | |
267 | } | |
268 | else | |
269 | { | |
270 | ret.x = m_buttons.GetCount()*button_size_and_margin_x + 2*BUTTON_AREA_MARGIN; | |
271 | ret.y = button_size_and_margin_y; | |
272 | #if wxUSE_STATLINE | |
273 | if (m_statLine) | |
274 | ret.y += STATIC_LINE_MARGIN; | |
275 | #endif | |
276 | } | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
281 | wxSize wxExtDialog::LayoutButtons() | |
282 | { | |
283 | if (m_buttons.GetCount() == 0) return wxSize(0,0); | |
284 | ||
285 | wxSize area_used( GetButtonAreaSize() ); | |
286 | wxSize client_area( GetClientSize() ); | |
287 | ||
288 | if (m_windowStyle & wxED_BUTTONS_RIGHT) | |
289 | { | |
290 | area_used.y = client_area.y; | |
291 | ||
292 | int space_for_each_button = (client_area.y-2*BUTTON_AREA_MARGIN) / m_buttons.GetCount(); | |
293 | int n = 0; | |
294 | wxNode *node = m_buttons.First(); | |
295 | while (node) | |
296 | { | |
297 | wxButton *button = (wxButton*)node->Data(); | |
298 | ||
299 | wxSize button_size( button->GetSize() ); | |
300 | if (button_size.x < 80) button_size.x = 80; | |
301 | ||
302 | int center_of_button_y = n*space_for_each_button + space_for_each_button/2; | |
303 | int button_y = BUTTON_AREA_MARGIN + center_of_button_y - button_size.y/2; | |
304 | ||
305 | int center_of_button_x = client_area.x - area_used.x/2; | |
306 | int button_x = center_of_button_x - button_size.x/2; | |
307 | ||
308 | button->SetSize( button_x, button_y, button_size.x, button_size.y ); | |
309 | ||
310 | node = node->Next(); | |
311 | n++; | |
312 | } | |
313 | ||
314 | #if wxUSE_STATLINE | |
315 | if (m_statLine) | |
316 | m_statLine->SetSize( client_area.x - area_used.x, | |
317 | 0, | |
318 | wxStaticLine::GetDefaultSize(), | |
319 | client_area.y ); | |
320 | #endif | |
321 | } | |
322 | else | |
323 | { | |
324 | area_used.x = client_area.x; | |
325 | ||
326 | int space_for_each_button = (client_area.x-2*BUTTON_AREA_MARGIN) / m_buttons.GetCount(); | |
327 | int n = 0; | |
328 | wxNode *node = m_buttons.First(); | |
329 | while (node) | |
330 | { | |
331 | wxButton *button = (wxButton*)node->Data(); | |
332 | ||
333 | wxSize button_size( button->GetSize() ); | |
334 | if (button_size.x < 80) button_size.x = 80; | |
335 | ||
336 | int center_of_button_x = n*space_for_each_button + space_for_each_button/2; | |
337 | int button_x = BUTTON_AREA_MARGIN + center_of_button_x - button_size.x/2; | |
338 | ||
339 | int center_of_button_y = client_area.y - area_used.y/2; | |
340 | int button_y = center_of_button_y - button_size.y/2; | |
341 | ||
342 | button->SetSize( button_x, button_y, button_size.x, button_size.y ); | |
343 | ||
344 | node = node->Next(); | |
345 | n++; | |
346 | } | |
347 | ||
348 | #if wxUSE_STATLINE | |
349 | if (m_statLine) | |
350 | m_statLine->SetSize( 0, | |
351 | client_area.y - area_used.y, | |
352 | client_area.x, | |
353 | wxStaticLine::GetDefaultSize() ); | |
354 | #endif | |
355 | } | |
356 | ||
357 | return area_used; | |
358 | } | |
359 | ||
360 |