]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/dialog.cpp | |
3 | // Purpose: wxDialog class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "dialog.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/dialog.h" | |
33 | #include "wx/utils.h" | |
34 | #include "wx/frame.h" | |
35 | #include "wx/app.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/log.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/msw/private.h" | |
42 | #include "wx/log.h" | |
43 | #include "wx/evtloop.h" | |
44 | #include "wx/ptr_scpd.h" | |
45 | ||
46 | #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__) | |
47 | #include <commdlg.h> | |
48 | #endif | |
49 | ||
50 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) | |
51 | #include "wx/msw/wince/resources.h" | |
52 | #endif // __SMARTPHONE__ && __WXWINCE__ | |
53 | ||
54 | #if wxUSE_TOOLBAR && defined(__POCKETPC__) | |
55 | #include "wx/toolbar.h" | |
56 | #endif | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // wxWin macros | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | #if wxUSE_EXTENDED_RTTI | |
63 | WX_DEFINE_FLAGS( wxDialogStyle ) | |
64 | ||
65 | wxBEGIN_FLAGS( wxDialogStyle ) | |
66 | // new style border flags, we put them first to | |
67 | // use them for streaming out | |
68 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
69 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
70 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
71 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
72 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
73 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
74 | ||
75 | // old style border flags | |
76 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
77 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
78 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
79 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
80 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
81 | wxFLAGS_MEMBER(wxNO_BORDER) | |
82 | ||
83 | // standard window styles | |
84 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
85 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
86 | ||
87 | // dialog styles | |
88 | wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY) | |
89 | wxFLAGS_MEMBER(wxSTAY_ON_TOP) | |
90 | wxFLAGS_MEMBER(wxCAPTION) | |
91 | wxFLAGS_MEMBER(wxTHICK_FRAME) | |
92 | wxFLAGS_MEMBER(wxSYSTEM_MENU) | |
93 | wxFLAGS_MEMBER(wxRESIZE_BORDER) | |
94 | wxFLAGS_MEMBER(wxRESIZE_BOX) | |
95 | wxFLAGS_MEMBER(wxCLOSE_BOX) | |
96 | wxFLAGS_MEMBER(wxMAXIMIZE_BOX) | |
97 | wxFLAGS_MEMBER(wxMINIMIZE_BOX) | |
98 | wxEND_FLAGS( wxDialogStyle ) | |
99 | ||
100 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h") | |
101 | ||
102 | wxBEGIN_PROPERTIES_TABLE(wxDialog) | |
103 | wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
104 | wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
105 | wxEND_PROPERTIES_TABLE() | |
106 | ||
107 | wxBEGIN_HANDLERS_TABLE(wxDialog) | |
108 | wxEND_HANDLERS_TABLE() | |
109 | ||
110 | wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle) | |
111 | ||
112 | #else | |
113 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) | |
114 | #endif | |
115 | ||
116 | BEGIN_EVENT_TABLE(wxDialog, wxDialogBase) | |
117 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) | |
118 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
119 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
120 | ||
121 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) | |
122 | ||
123 | EVT_CLOSE(wxDialog::OnCloseWindow) | |
124 | END_EVENT_TABLE() | |
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxDialogModalData | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | // this is simply a container for any data we need to implement modality which | |
131 | // allows us to avoid changing wxDialog each time the implementation changes | |
132 | class wxDialogModalData | |
133 | { | |
134 | public: | |
135 | wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { } | |
136 | ||
137 | void RunLoop() | |
138 | { | |
139 | m_evtLoop.Run(); | |
140 | } | |
141 | ||
142 | void ExitLoop() | |
143 | { | |
144 | m_evtLoop.Exit(); | |
145 | } | |
146 | ||
147 | private: | |
148 | wxModalEventLoop m_evtLoop; | |
149 | }; | |
150 | ||
151 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData); | |
152 | ||
153 | // ============================================================================ | |
154 | // implementation | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // wxDialog construction | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | void wxDialog::Init() | |
162 | { | |
163 | m_oldFocus = (wxWindow *)NULL; | |
164 | m_isShown = false; | |
165 | m_modalData = NULL; | |
166 | m_endModalCalled = false; | |
167 | #if wxUSE_TOOLBAR && defined(__POCKETPC__) | |
168 | m_dialogToolBar = NULL; | |
169 | #endif | |
170 | } | |
171 | ||
172 | bool wxDialog::Create(wxWindow *parent, | |
173 | wxWindowID id, | |
174 | const wxString& title, | |
175 | const wxPoint& pos, | |
176 | const wxSize& size, | |
177 | long style, | |
178 | const wxString& name) | |
179 | { | |
180 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); | |
181 | ||
182 | // save focus before doing anything which can potentially change it | |
183 | m_oldFocus = FindFocus(); | |
184 | ||
185 | // All dialogs should really have this style | |
186 | style |= wxTAB_TRAVERSAL; | |
187 | ||
188 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) | |
189 | return false; | |
190 | ||
191 | if ( !m_hasFont ) | |
192 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
193 | ||
194 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) | |
195 | SetLeftMenu(wxID_OK, _("OK")); | |
196 | #endif | |
197 | #if wxUSE_TOOLBAR && defined(__POCKETPC__) | |
198 | CreateToolBar(); | |
199 | #endif | |
200 | ||
201 | return true; | |
202 | } | |
203 | ||
204 | // deprecated ctor | |
205 | wxDialog::wxDialog(wxWindow *parent, | |
206 | const wxString& title, | |
207 | bool WXUNUSED(modal), | |
208 | int x, | |
209 | int y, | |
210 | int w, | |
211 | int h, | |
212 | long style, | |
213 | const wxString& name) | |
214 | { | |
215 | Init(); | |
216 | ||
217 | Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name); | |
218 | } | |
219 | ||
220 | void wxDialog::SetModal(bool WXUNUSED(flag)) | |
221 | { | |
222 | // nothing to do, obsolete method | |
223 | } | |
224 | ||
225 | wxDialog::~wxDialog() | |
226 | { | |
227 | m_isBeingDeleted = true; | |
228 | ||
229 | // this will also reenable all the other windows for a modal dialog | |
230 | Show(false); | |
231 | } | |
232 | ||
233 | // ---------------------------------------------------------------------------- | |
234 | // showing the dialogs | |
235 | // ---------------------------------------------------------------------------- | |
236 | ||
237 | bool wxDialog::IsModalShowing() const | |
238 | { | |
239 | return IsModal(); | |
240 | } | |
241 | ||
242 | wxWindow *wxDialog::FindSuitableParent() const | |
243 | { | |
244 | // first try to use the currently active window | |
245 | HWND hwndFg = ::GetForegroundWindow(); | |
246 | wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg) | |
247 | : NULL; | |
248 | if ( !parent ) | |
249 | { | |
250 | // next try the main app window | |
251 | parent = wxTheApp->GetTopWindow(); | |
252 | } | |
253 | ||
254 | // finally, check if the parent we found is really suitable | |
255 | if ( !parent || parent == (wxWindow *)this || !parent->IsShown() ) | |
256 | { | |
257 | // don't use this one | |
258 | parent = NULL; | |
259 | } | |
260 | ||
261 | return parent; | |
262 | } | |
263 | ||
264 | bool wxDialog::Show(bool show) | |
265 | { | |
266 | if ( show == IsShown() ) | |
267 | return false; | |
268 | ||
269 | if ( !show && m_modalData ) | |
270 | { | |
271 | // we need to do this before calling wxDialogBase version because if we | |
272 | // had disabled other app windows, they must be reenabled right now as | |
273 | // if they stay disabled Windows will activate another window (one | |
274 | // which is enabled, anyhow) when we're hidden in the base class Show() | |
275 | // and we will lose activation | |
276 | m_modalData->ExitLoop(); | |
277 | } | |
278 | ||
279 | if ( show ) | |
280 | { | |
281 | // this usually will result in TransferDataToWindow() being called | |
282 | // which will change the controls values so do it before showing as | |
283 | // otherwise we could have some flicker | |
284 | InitDialog(); | |
285 | } | |
286 | ||
287 | wxDialogBase::Show(show); | |
288 | ||
289 | if ( show ) | |
290 | { | |
291 | // dialogs don't get WM_SIZE message after creation unlike most (all?) | |
292 | // other windows and so could start their life non laid out correctly | |
293 | // if we didn't call Layout() from here | |
294 | // | |
295 | // NB: normally we should call it just the first time but doing it | |
296 | // every time is simpler than keeping a flag | |
297 | Layout(); | |
298 | } | |
299 | ||
300 | return true; | |
301 | } | |
302 | ||
303 | void wxDialog::Raise() | |
304 | { | |
305 | ::SetForegroundWindow(GetHwnd()); | |
306 | } | |
307 | ||
308 | // show dialog modally | |
309 | int wxDialog::ShowModal() | |
310 | { | |
311 | wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") ); | |
312 | ||
313 | m_endModalCalled = false; | |
314 | ||
315 | Show(); | |
316 | ||
317 | // EndModal may have been called from InitDialog handler (called from | |
318 | // inside Show()), which would cause an infinite loop if we didn't take it | |
319 | // into account | |
320 | if ( !m_endModalCalled ) | |
321 | { | |
322 | // modal dialog needs a parent window, so try to find one | |
323 | wxWindow *parent = GetParent(); | |
324 | if ( !parent ) | |
325 | { | |
326 | parent = FindSuitableParent(); | |
327 | } | |
328 | ||
329 | // remember where the focus was | |
330 | wxWindow *oldFocus = m_oldFocus; | |
331 | if ( !oldFocus ) | |
332 | { | |
333 | // VZ: do we really want to do this? | |
334 | oldFocus = parent; | |
335 | } | |
336 | ||
337 | // We have to remember the HWND because we need to check | |
338 | // the HWND still exists (oldFocus can be garbage when the dialog | |
339 | // exits, if it has been destroyed) | |
340 | HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL; | |
341 | ||
342 | ||
343 | // enter and run the modal loop | |
344 | { | |
345 | wxDialogModalDataTiedPtr modalData(&m_modalData, | |
346 | new wxDialogModalData(this)); | |
347 | modalData->RunLoop(); | |
348 | } | |
349 | ||
350 | ||
351 | // and restore focus | |
352 | // Note that this code MUST NOT access the dialog object's data | |
353 | // in case the object has been deleted (which will be the case | |
354 | // for a modal dialog that has been destroyed before calling EndModal). | |
355 | if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus)) | |
356 | { | |
357 | // This is likely to prove that the object still exists | |
358 | if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus) | |
359 | oldFocus->SetFocus(); | |
360 | } | |
361 | } | |
362 | ||
363 | return GetReturnCode(); | |
364 | } | |
365 | ||
366 | void wxDialog::EndModal(int retCode) | |
367 | { | |
368 | wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") ); | |
369 | ||
370 | m_endModalCalled = true; | |
371 | SetReturnCode(retCode); | |
372 | ||
373 | Hide(); | |
374 | } | |
375 | ||
376 | void wxDialog::EndDialog(int rc) | |
377 | { | |
378 | if ( IsModal() ) | |
379 | EndModal(rc); | |
380 | else | |
381 | Hide(); | |
382 | } | |
383 | ||
384 | // ---------------------------------------------------------------------------- | |
385 | // wxWin event handlers | |
386 | // ---------------------------------------------------------------------------- | |
387 | ||
388 | // Standard buttons | |
389 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
390 | { | |
391 | if ( Validate() && TransferDataFromWindow() ) | |
392 | { | |
393 | EndDialog(wxID_OK); | |
394 | } | |
395 | } | |
396 | ||
397 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) | |
398 | { | |
399 | if ( Validate() ) | |
400 | TransferDataFromWindow(); | |
401 | ||
402 | // TODO probably need to disable the Apply button until things change again | |
403 | } | |
404 | ||
405 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
406 | { | |
407 | EndDialog(wxID_CANCEL); | |
408 | } | |
409 | ||
410 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
411 | { | |
412 | // We'll send a Cancel message by default, which may close the dialog. | |
413 | // Check for looping if the Cancel event handler calls Close(). | |
414 | ||
415 | // Note that if a cancel button and handler aren't present in the dialog, | |
416 | // nothing will happen when you close the dialog via the window manager, or | |
417 | // via Close(). We wouldn't want to destroy the dialog by default, since | |
418 | // the dialog may have been created on the stack. However, this does mean | |
419 | // that calling dialog->Close() won't delete the dialog unless the handler | |
420 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
421 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
422 | // dialog, and hides a modeless dialog. | |
423 | ||
424 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
425 | // lists here? don't dare to change it now, but should be done later! | |
426 | static wxList closing; | |
427 | ||
428 | if ( closing.Member(this) ) | |
429 | return; | |
430 | ||
431 | closing.Append(this); | |
432 | ||
433 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
434 | cancelEvent.SetEventObject( this ); | |
435 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
436 | ||
437 | closing.DeleteObject(this); | |
438 | } | |
439 | ||
440 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
441 | { | |
442 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
443 | Refresh(); | |
444 | } | |
445 | ||
446 | #ifdef __POCKETPC__ | |
447 | // Responds to the OK button in a PocketPC titlebar. This | |
448 | // can be overridden, or you can change the id used for | |
449 | // sending the event, by calling SetAffirmativeId. | |
450 | bool wxDialog::DoOK() | |
451 | { | |
452 | wxButton *btn = wxDynamicCast(FindWindow(GetAffirmativeId()), wxButton); | |
453 | ||
454 | if ( btn && btn->IsEnabled() ) | |
455 | { | |
456 | // If we have this button, press it | |
457 | btn->MSWCommand(BN_CLICKED, 0 /* unused */); | |
458 | return true; | |
459 | } | |
460 | else | |
461 | { | |
462 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId()); | |
463 | event.SetEventObject(this); | |
464 | ||
465 | return GetEventHandler()->ProcessEvent(event); | |
466 | } | |
467 | } | |
468 | #endif | |
469 | ||
470 | #if wxUSE_TOOLBAR && defined(__POCKETPC__) | |
471 | // create main toolbar by calling OnCreateToolBar() | |
472 | wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name) | |
473 | { | |
474 | m_dialogToolBar = OnCreateToolBar(style, winid, name); | |
475 | ||
476 | return m_dialogToolBar; | |
477 | } | |
478 | ||
479 | // return a new toolbar | |
480 | wxToolBar *wxDialog::OnCreateToolBar(long style, | |
481 | wxWindowID winid, | |
482 | const wxString& name) | |
483 | { | |
484 | return new wxToolMenuBar(this, winid, | |
485 | wxDefaultPosition, wxDefaultSize, | |
486 | style, name); | |
487 | } | |
488 | #endif | |
489 | ||
490 | // --------------------------------------------------------------------------- | |
491 | // dialog window proc | |
492 | // --------------------------------------------------------------------------- | |
493 | ||
494 | WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
495 | { | |
496 | WXLRESULT rc = 0; | |
497 | bool processed = false; | |
498 | ||
499 | switch ( message ) | |
500 | { | |
501 | #ifdef __WXWINCE__ | |
502 | // react to pressing the OK button in the title | |
503 | case WM_COMMAND: | |
504 | { | |
505 | switch ( LOWORD(wParam) ) | |
506 | { | |
507 | #ifdef __POCKETPC__ | |
508 | case IDOK: | |
509 | processed = DoOK(); | |
510 | if (!processed) | |
511 | processed = !Close(); | |
512 | #endif | |
513 | #ifdef __SMARTPHONE__ | |
514 | case IDM_LEFT: | |
515 | case IDM_RIGHT: | |
516 | processed = HandleCommand( LOWORD(wParam) , 0 , NULL ); | |
517 | break; | |
518 | #endif // __SMARTPHONE__ | |
519 | } | |
520 | break; | |
521 | } | |
522 | #endif | |
523 | case WM_CLOSE: | |
524 | // if we can't close, tell the system that we processed the | |
525 | // message - otherwise it would close us | |
526 | processed = !Close(); | |
527 | break; | |
528 | ||
529 | case WM_SIZE: | |
530 | // the Windows dialogs unfortunately are not meant to be resizeable | |
531 | // at all and their standard class doesn't include CS_[VH]REDRAW | |
532 | // styles which means that the window is not refreshed properly | |
533 | // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can | |
534 | // help with it - so we have to refresh it manually which certainly | |
535 | // creates flicker but at least doesn't show garbage on the screen | |
536 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
537 | processed = true; | |
538 | if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) ) | |
539 | { | |
540 | ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */); | |
541 | } | |
542 | break; | |
543 | ||
544 | #ifndef __WXMICROWIN__ | |
545 | case WM_SETCURSOR: | |
546 | // we want to override the busy cursor for modal dialogs: | |
547 | // typically, wxBeginBusyCursor() is called and then a modal dialog | |
548 | // is shown, but the modal dialog shouldn't have hourglass cursor | |
549 | if ( IsModal() && wxIsBusy() ) | |
550 | { | |
551 | // set our cursor for all windows (but see below) | |
552 | wxCursor cursor = m_cursor; | |
553 | if ( !cursor.Ok() ) | |
554 | cursor = wxCURSOR_ARROW; | |
555 | ||
556 | ::SetCursor(GetHcursorOf(cursor)); | |
557 | ||
558 | // in any case, stop here and don't let wxWindow process this | |
559 | // message (it would set the busy cursor) | |
560 | processed = true; | |
561 | ||
562 | // but return false to tell the child window (if the event | |
563 | // comes from one of them and not from ourselves) that it can | |
564 | // set its own cursor if it has one: thus, standard controls | |
565 | // (e.g. text ctrl) still have correct cursors in a dialog | |
566 | // invoked while wxIsBusy() | |
567 | rc = false; | |
568 | } | |
569 | break; | |
570 | #endif // __WXMICROWIN__ | |
571 | } | |
572 | ||
573 | if ( !processed ) | |
574 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
575 | ||
576 | return rc; | |
577 | } | |
578 |