]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
Committing in .
[wxWidgets.git] / src / msw / dialog.cpp
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 and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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
44 #if wxUSE_COMMON_DIALOGS
45 #include <commdlg.h>
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // default dialog pos and size
53
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
56
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
59
60 // ----------------------------------------------------------------------------
61 // globals
62 // ----------------------------------------------------------------------------
63
64 // all modal dialogs currently shown
65 static wxWindowList wxModalDialogs;
66
67 // ----------------------------------------------------------------------------
68 // wxWin macros
69 // ----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
72
73 BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
74 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
75 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
76 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
77
78 EVT_CHAR_HOOK(wxDialog::OnCharHook)
79
80 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
81
82 EVT_CLOSE(wxDialog::OnCloseWindow)
83 END_EVENT_TABLE()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // wxDialog construction
91 // ----------------------------------------------------------------------------
92
93 void wxDialog::Init()
94 {
95 m_oldFocus = (wxWindow *)NULL;
96
97 m_isShown = FALSE;
98
99 m_windowDisabler = (wxWindowDisabler *)NULL;
100
101 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
102 }
103
104 bool wxDialog::Create(wxWindow *parent,
105 wxWindowID id,
106 const wxString& title,
107 const wxPoint& pos,
108 const wxSize& size,
109 long style,
110 const wxString& name)
111 {
112 Init();
113
114 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
115
116 // save focus before doing anything which can potentially change it
117 m_oldFocus = FindFocus();
118
119 // All dialogs should really have this style
120 style |= wxTAB_TRAVERSAL;
121
122 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
123 return FALSE;
124
125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
126
127 return TRUE;
128 }
129
130 bool wxDialog::EnableCloseButton(bool enable)
131 {
132 #ifndef __WXMICROWIN__
133 // get system (a.k.a. window) menu
134 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE /* get it */);
135 if ( !hmenu )
136 {
137 wxLogLastError(_T("GetSystemMenu"));
138
139 return FALSE;
140 }
141
142 // enabling/disabling the close item from it also automatically
143 // disables/enabling the close title bar button
144 if ( !::EnableMenuItem(hmenu, SC_CLOSE,
145 MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED)) )
146 {
147 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
148
149 return FALSE;
150 }
151
152 // update appearance immediately
153 if ( !::DrawMenuBar(GetHwnd()) )
154 {
155 wxLogLastError(_T("DrawMenuBar"));
156 }
157 #endif
158
159 return TRUE;
160 }
161
162 void wxDialog::SetModal(bool flag)
163 {
164 if ( flag )
165 {
166 m_windowStyle |= wxDIALOG_MODAL;
167
168 wxModelessWindows.DeleteObject(this);
169 }
170 else
171 {
172 m_windowStyle &= ~wxDIALOG_MODAL;
173
174 wxModelessWindows.Append(this);
175 }
176 }
177
178 wxDialog::~wxDialog()
179 {
180 m_isBeingDeleted = TRUE;
181
182 // this will also reenable all the other windows for a modal dialog
183 Show(FALSE);
184 }
185
186 // ----------------------------------------------------------------------------
187 // kbd handling
188 // ----------------------------------------------------------------------------
189
190 // By default, pressing escape cancels the dialog
191 void wxDialog::OnCharHook(wxKeyEvent& event)
192 {
193 if (GetHWND())
194 {
195 // "Esc" works as an accelerator for the "Cancel" button, but it
196 // shouldn't close the dialog which doesn't have any cancel button
197 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
198 {
199 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
200 cancelEvent.SetEventObject( this );
201 GetEventHandler()->ProcessEvent(cancelEvent);
202
203 // ensure that there is another message for this window so the
204 // ShowModal loop will exit and won't get stuck in GetMessage().
205 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
206
207 return;
208 }
209 }
210
211 // We didn't process this event.
212 event.Skip();
213 }
214
215 // ----------------------------------------------------------------------------
216 // showing the dialogs
217 // ----------------------------------------------------------------------------
218
219 bool wxDialog::IsModal() const
220 {
221 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
222 }
223
224 bool wxDialog::IsModalShowing() const
225 {
226 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
227 }
228
229 void wxDialog::DoShowModal()
230 {
231 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
232 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
233
234 wxModalDialogs.Append(this);
235
236 wxWindow *parent = GetParent();
237
238 wxWindow* oldFocus = m_oldFocus;
239
240 // We have to remember the HWND because we need to check
241 // the HWND still exists (oldFocus can be garbage when the dialog
242 // exits, if it has been destroyed)
243 HWND hwndOldFocus = 0;
244 if (oldFocus)
245 hwndOldFocus = (HWND) oldFocus->GetHWND();
246
247 // remember where the focus was
248 if ( !oldFocus )
249 {
250 oldFocus = parent;
251 if ( parent )
252 hwndOldFocus = GetHwndOf(parent);
253 }
254
255 // disable all other app windows
256 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
257
258 m_windowDisabler = new wxWindowDisabler(this);
259
260 // enter the modal loop
261 while ( IsModalShowing() )
262 {
263 #if wxUSE_THREADS
264 wxMutexGuiLeaveOrEnter();
265 #endif // wxUSE_THREADS
266
267 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
268 ;
269
270 // a message came or no more idle processing to do
271 wxTheApp->DoMessage();
272 }
273
274 // and restore focus
275 // Note that this code MUST NOT access the dialog object's data
276 // in case the object has been deleted (which will be the case
277 // for a modal dialog that has been destroyed before calling EndModal).
278 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
279 {
280 // This is likely to prove that the object still exists
281 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
282 oldFocus->SetFocus();
283 }
284 }
285
286 bool wxDialog::Show(bool show)
287 {
288 if ( !show )
289 {
290 // if we had disabled other app windows, reenable them back now because
291 // if they stay disabled Windows will activate another window (one
292 // which is enabled, anyhow) and we will lose activation
293 if ( m_windowDisabler )
294 {
295 delete m_windowDisabler;
296 m_windowDisabler = NULL;
297 }
298 }
299
300 // ShowModal() may be called for already shown dialog
301 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
302 {
303 // nothing to do
304 return FALSE;
305 }
306
307 if ( show )
308 {
309 // usually will result in TransferDataToWindow() being called
310 InitDialog();
311 }
312
313 if ( IsModal() )
314 {
315 if ( show )
316 {
317 // modal dialog needs a parent window, so try to find one
318 if ( !GetParent() )
319 {
320 wxWindow *parent = wxTheApp->GetTopWindow();
321 if ( parent && parent != this && parent->IsShown() )
322 {
323 // use it
324 m_parent = parent;
325 }
326 }
327
328 DoShowModal();
329 }
330 else // end of modal dialog
331 {
332 // this will cause IsModalShowing() return FALSE and our local
333 // message loop will terminate
334 wxModalDialogs.DeleteObject(this);
335 }
336 }
337
338 return TRUE;
339 }
340
341 // a special version for Show(TRUE) for modal dialogs which returns return code
342 int wxDialog::ShowModal()
343 {
344 if ( !IsModal() )
345 {
346 SetModal(TRUE);
347 }
348
349 Show(TRUE);
350
351 return GetReturnCode();
352 }
353
354 // NB: this function (surprizingly) may be called for both modal and modeless
355 // dialogs and should work for both of them
356 void wxDialog::EndModal(int retCode)
357 {
358 SetReturnCode(retCode);
359
360 Show(FALSE);
361 }
362
363 // ----------------------------------------------------------------------------
364 // wxWin event handlers
365 // ----------------------------------------------------------------------------
366
367 // Standard buttons
368 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
369 {
370 if ( Validate() && TransferDataFromWindow() )
371 {
372 EndModal(wxID_OK);
373 }
374 }
375
376 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
377 {
378 if ( Validate() )
379 TransferDataFromWindow();
380
381 // TODO probably need to disable the Apply button until things change again
382 }
383
384 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
385 {
386 EndModal(wxID_CANCEL);
387 }
388
389 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
390 {
391 // We'll send a Cancel message by default, which may close the dialog.
392 // Check for looping if the Cancel event handler calls Close().
393
394 // Note that if a cancel button and handler aren't present in the dialog,
395 // nothing will happen when you close the dialog via the window manager, or
396 // via Close(). We wouldn't want to destroy the dialog by default, since
397 // the dialog may have been created on the stack. However, this does mean
398 // that calling dialog->Close() won't delete the dialog unless the handler
399 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
400 // destroy the dialog. The default OnCancel (above) simply ends a modal
401 // dialog, and hides a modeless dialog.
402
403 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
404 // lists here? don't dare to change it now, but should be done later!
405 static wxList closing;
406
407 if ( closing.Member(this) )
408 return;
409
410 closing.Append(this);
411
412 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
413 cancelEvent.SetEventObject( this );
414 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
415
416 closing.DeleteObject(this);
417 }
418
419 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
420 {
421 #if wxUSE_CTL3D
422 Ctl3dColorChange();
423 #else
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
425 Refresh();
426 #endif
427 }
428
429 // ---------------------------------------------------------------------------
430 // dialog window proc
431 // ---------------------------------------------------------------------------
432
433 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
434 {
435 long rc = 0;
436 bool processed = FALSE;
437
438 switch ( message )
439 {
440 #if 0 // now that we got owner window right it doesn't seem to be needed
441 case WM_ACTIVATE:
442 switch ( LOWORD(wParam) )
443 {
444 case WA_ACTIVE:
445 case WA_CLICKACTIVE:
446 if ( IsModalShowing() && GetParent() )
447 {
448 // bring the owner window to top as the standard dialog
449 // boxes do
450 if ( !::SetWindowPos
451 (
452 GetHwndOf(GetParent()),
453 GetHwnd(),
454 0, 0,
455 0, 0,
456 SWP_NOACTIVATE |
457 SWP_NOMOVE |
458 SWP_NOSIZE
459 ) )
460 {
461 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
462 }
463 }
464 // fall through to process it normally as well
465 }
466 break;
467 #endif // 0
468
469 case WM_CLOSE:
470 // if we can't close, tell the system that we processed the
471 // message - otherwise it would close us
472 processed = !Close();
473 break;
474
475 #ifndef __WXMICROWIN__
476 case WM_SETCURSOR:
477 // we want to override the busy cursor for modal dialogs:
478 // typically, wxBeginBusyCursor() is called and then a modal dialog
479 // is shown, but the modal dialog shouldn't have hourglass cursor
480 if ( IsModalShowing() && wxIsBusy() )
481 {
482 // set our cursor for all windows (but see below)
483 wxCursor cursor = m_cursor;
484 if ( !cursor.Ok() )
485 cursor = wxCURSOR_ARROW;
486
487 ::SetCursor(GetHcursorOf(cursor));
488
489 // in any case, stop here and don't let wxWindow process this
490 // message (it would set the busy cursor)
491 processed = TRUE;
492
493 // but return FALSE to tell the child window (if the event
494 // comes from one of them and not from ourselves) that it can
495 // set its own cursor if it has one: thus, standard controls
496 // (e.g. text ctrl) still have correct cursors in a dialog
497 // invoked while wxIsBusy()
498 rc = FALSE;
499 }
500 break;
501 #endif // __WXMICROWIN__
502 }
503
504 if ( !processed )
505 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
506
507 return rc;
508 }
509
510 #if wxUSE_CTL3D
511
512 // Define for each class of dialog and control
513 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
514 WXHWND WXUNUSED(pWnd),
515 WXUINT WXUNUSED(nCtlColor),
516 WXUINT message,
517 WXWPARAM wParam,
518 WXLPARAM lParam)
519 {
520 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
521 }
522
523 #endif // wxUSE_CTL3D
524