]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
implemented wxDisplaySizeMM for gtk, msw & motif.
[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 objects to be deleted during next idle processing - from window.cpp
65 extern wxList WXDLLEXPORT wxPendingDelete;
66
67 // all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68 wxWindowList wxModelessWindows;
69
70 // all modal dialogs currently shown
71 static wxWindowList wxModalDialogs;
72
73 // ----------------------------------------------------------------------------
74 // wxWin macros
75 // ----------------------------------------------------------------------------
76
77 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
78
79 BEGIN_EVENT_TABLE(wxDialog, wxPanel)
80 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
81 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
82 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
83
84 EVT_CHAR_HOOK(wxDialog::OnCharHook)
85
86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
87
88 EVT_CLOSE(wxDialog::OnCloseWindow)
89 END_EVENT_TABLE()
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxDialog construction
97 // ----------------------------------------------------------------------------
98
99 wxDialog::wxDialog()
100 {
101 m_oldFocus = (wxWindow *)NULL;
102 m_isShown = FALSE;
103
104 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
105 }
106
107 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
108 const wxString& title,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
113 {
114 m_oldFocus = FindFocus();
115
116 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
117 SetName(name);
118
119 wxTopLevelWindows.Append(this);
120
121 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
122
123 if (parent) parent->AddChild(this);
124
125 if ( id == -1 )
126 m_windowId = (int)NewControlId();
127 else
128 m_windowId = id;
129
130 int x = pos.x;
131 int y = pos.y;
132 int width = size.x;
133 int height = size.y;
134
135 if (x < 0)
136 x = wxDIALOG_DEFAULT_X;
137 if (y < 0)
138 y = wxDIALOG_DEFAULT_Y;
139
140 m_windowStyle = style;
141
142 m_isShown = FALSE;
143
144 if (width < 0)
145 width = wxDIALOG_DEFAULT_WIDTH;
146 if (height < 0)
147 height = wxDIALOG_DEFAULT_HEIGHT;
148
149 // All dialogs should really have this style
150 m_windowStyle |= wxTAB_TRAVERSAL;
151
152 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
153 if (m_windowStyle & wxSTAY_ON_TOP)
154 extendedStyle |= WS_EX_TOPMOST;
155
156 #ifndef __WIN16__
157 if (m_exStyle & wxDIALOG_EX_CONTEXTHELP)
158 extendedStyle |= WS_EX_CONTEXTHELP;
159 #endif
160 // Allows creation of dialogs with & without captions under MSWindows,
161 // resizeable or not (but a resizeable dialog always has caption -
162 // otherwise it would look too strange)
163 const wxChar *dlg;
164 if ( style & wxRESIZE_BORDER )
165 dlg = wxT("wxResizeableDialog");
166 else if ( style & wxCAPTION )
167 dlg = wxT("wxCaptionDialog");
168 else
169 dlg = wxT("wxNoCaptionDialog");
170 MSWCreate(m_windowId, parent, NULL, this, NULL,
171 x, y, width, height,
172 0, // style is not used if we have dlg template
173 dlg,
174 extendedStyle);
175
176 HWND hwnd = (HWND)GetHWND();
177
178 if ( !hwnd )
179 {
180 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
181
182 return FALSE;
183 }
184
185 SubclassWin(GetHWND());
186
187 SetWindowText(hwnd, title);
188 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
189
190 return TRUE;
191 }
192
193 void wxDialog::SetModal(bool flag)
194 {
195 if ( flag )
196 {
197 m_windowStyle |= wxDIALOG_MODAL;
198
199 wxModelessWindows.DeleteObject(this);
200 }
201 else
202 {
203 m_windowStyle &= ~wxDIALOG_MODAL;
204
205 wxModelessWindows.Append(this);
206 }
207 }
208
209 wxDialog::~wxDialog()
210 {
211 m_isBeingDeleted = TRUE;
212
213 wxTopLevelWindows.DeleteObject(this);
214
215 // this will call BringWindowToTop() if necessary to bring back our parent
216 // window to top
217 Show(FALSE);
218
219 if ( !IsModal() )
220 wxModelessWindows.DeleteObject(this);
221
222 // If this is the last top-level window, exit.
223 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
224 {
225 wxTheApp->SetTopWindow(NULL);
226
227 if ( wxTheApp->GetExitOnFrameDelete() )
228 {
229 ::PostQuitMessage(0);
230 }
231 }
232 }
233
234 // ----------------------------------------------------------------------------
235 // kbd handling
236 // ----------------------------------------------------------------------------
237
238 // By default, pressing escape cancels the dialog
239 void wxDialog::OnCharHook(wxKeyEvent& event)
240 {
241 if (GetHWND())
242 {
243 // "Esc" works as an accelerator for the "Cancel" button, but it
244 // shouldn't close the dialog which doesn't have any cancel button
245 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
246 {
247 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
248 cancelEvent.SetEventObject( this );
249 GetEventHandler()->ProcessEvent(cancelEvent);
250
251 // ensure that there is another message for this window so the
252 // ShowModal loop will exit and won't get stuck in GetMessage().
253 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
254
255 return;
256 }
257 }
258
259 // We didn't process this event.
260 event.Skip();
261 }
262
263 // ----------------------------------------------------------------------------
264 // Windows dialog boxes can't be iconized
265 // ----------------------------------------------------------------------------
266
267 void wxDialog::Iconize(bool WXUNUSED(iconize))
268 {
269 }
270
271 bool wxDialog::IsIconized() const
272 {
273 return FALSE;
274 }
275
276 // ----------------------------------------------------------------------------
277 // size/position handling
278 // ----------------------------------------------------------------------------
279
280 void wxDialog::DoSetClientSize(int width, int height)
281 {
282 HWND hWnd = (HWND) GetHWND();
283 RECT rect;
284 ::GetClientRect(hWnd, &rect);
285
286 RECT rect2;
287 GetWindowRect(hWnd, &rect2);
288
289 // Find the difference between the entire window (title bar and all)
290 // and the client area; add this to the new client size to move the
291 // window
292 int actual_width = rect2.right - rect2.left - rect.right + width;
293 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
294
295 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
296
297 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
298 event.SetEventObject( this );
299 GetEventHandler()->ProcessEvent(event);
300 }
301
302 void wxDialog::DoGetPosition(int *x, int *y) const
303 {
304 RECT rect;
305 GetWindowRect(GetHwnd(), &rect);
306
307 if ( x )
308 *x = rect.left;
309 if ( y )
310 *y = rect.top;
311 }
312
313 // ----------------------------------------------------------------------------
314 // showing the dialogs
315 // ----------------------------------------------------------------------------
316
317 bool wxDialog::IsModal() const
318 {
319 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
320 }
321
322 bool wxDialog::IsModalShowing() const
323 {
324 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
325 }
326
327 void wxDialog::DoShowModal()
328 {
329 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
330 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
331
332 wxModalDialogs.Append(this);
333
334 wxWindow *parent = GetParent();
335
336 wxWindow* oldFocus = m_oldFocus;
337
338 // We have to remember the HWND because we need to check
339 // the HWND still exists (oldFocus can be garbage when the dialog
340 // exits, if it has been destroyed)
341 HWND hwndOldFocus = 0;
342 if (oldFocus)
343 hwndOldFocus = (HWND) oldFocus->GetHWND();
344
345 // inside this block, all app windows are disabled
346 {
347 wxWindowDisabler wd(this);
348
349 // remember where the focus was
350 if ( !oldFocus )
351 {
352 oldFocus = parent;
353 if (parent)
354 hwndOldFocus = (HWND) parent->GetHWND();
355 }
356
357 // enter the modal loop
358 while ( IsModalShowing() )
359 {
360 #if wxUSE_THREADS
361 wxMutexGuiLeaveOrEnter();
362 #endif // wxUSE_THREADS
363
364 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
365 ;
366
367 // a message came or no more idle processing to do
368 wxTheApp->DoMessage();
369 }
370 }
371
372 #ifdef __WIN32__
373 if ( parent )
374 ::SetActiveWindow(GetHwndOf(parent));
375 #endif // __WIN32__
376
377 // and restore focus
378 // Note that this code MUST NOT access the dialog object's data
379 // in case the object has been deleted (which will be the case
380 // for a modal dialog that has been destroyed before calling EndModal).
381 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
382 {
383 // This is likely to prove that the object still exists
384 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
385 oldFocus->SetFocus();
386 }
387 }
388
389 bool wxDialog::Show(bool show)
390 {
391 // The following is required when the parent has been disabled, (modal
392 // dialogs, or modeless dialogs with disabling such as wxProgressDialog).
393 // Otherwise the parent disappears behind other windows when the dialog is
394 // hidden.
395 if ( !show )
396 {
397 wxWindow *parent = GetParent();
398 if ( parent )
399 {
400 ::BringWindowToTop(GetHwndOf(parent));
401 }
402 }
403
404 // ShowModal() may be called for already shown dialog
405 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
406 {
407 // nothing to do
408 return FALSE;
409 }
410
411 if ( show )
412 {
413 // usually will result in TransferDataToWindow() being called
414 InitDialog();
415 }
416
417 if ( IsModal() )
418 {
419 if ( show )
420 {
421 // modal dialog needs a parent window, so try to find one
422 if ( !GetParent() )
423 {
424 wxWindow *parent = wxTheApp->GetTopWindow();
425 if ( parent && parent != this && parent->IsShown() )
426 {
427 // use it
428 m_parent = parent;
429 }
430 }
431
432 DoShowModal();
433 }
434 else // end of modal dialog
435 {
436 // this will cause IsModalShowing() return FALSE and our local
437 // message loop will terminate
438 wxModalDialogs.DeleteObject(this);
439 }
440 }
441
442 return TRUE;
443 }
444
445 // a special version for Show(TRUE) for modal dialogs which returns return code
446 int wxDialog::ShowModal()
447 {
448 if ( !IsModal() )
449 {
450 SetModal(TRUE);
451 }
452
453 Show(TRUE);
454
455 return GetReturnCode();
456 }
457
458 // NB: this function (surprizingly) may be called for both modal and modeless
459 // dialogs and should work for both of them
460 void wxDialog::EndModal(int retCode)
461 {
462 SetReturnCode(retCode);
463
464 Show(FALSE);
465 }
466
467 // ----------------------------------------------------------------------------
468 // wxWin event handlers
469 // ----------------------------------------------------------------------------
470
471 // Standard buttons
472 void wxDialog::OnOK(wxCommandEvent& event)
473 {
474 if ( Validate() && TransferDataFromWindow() )
475 {
476 EndModal(wxID_OK);
477 }
478 }
479
480 void wxDialog::OnApply(wxCommandEvent& event)
481 {
482 if ( Validate() )
483 TransferDataFromWindow();
484
485 // TODO probably need to disable the Apply button until things change again
486 }
487
488 void wxDialog::OnCancel(wxCommandEvent& event)
489 {
490 EndModal(wxID_CANCEL);
491 }
492
493 void wxDialog::OnCloseWindow(wxCloseEvent& event)
494 {
495 // We'll send a Cancel message by default, which may close the dialog.
496 // Check for looping if the Cancel event handler calls Close().
497
498 // Note that if a cancel button and handler aren't present in the dialog,
499 // nothing will happen when you close the dialog via the window manager, or
500 // via Close(). We wouldn't want to destroy the dialog by default, since
501 // the dialog may have been created on the stack. However, this does mean
502 // that calling dialog->Close() won't delete the dialog unless the handler
503 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
504 // destroy the dialog. The default OnCancel (above) simply ends a modal
505 // dialog, and hides a modeless dialog.
506
507 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
508 // lists here? don't dare to change it now, but should be done later!
509 static wxList closing;
510
511 if ( closing.Member(this) )
512 return;
513
514 closing.Append(this);
515
516 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
517 cancelEvent.SetEventObject( this );
518 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
519
520 closing.DeleteObject(this);
521 }
522
523 // Destroy the window (delayed, if a managed window)
524 bool wxDialog::Destroy()
525 {
526 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
527 _T("wxDialog destroyed twice") );
528
529 wxPendingDelete.Append(this);
530
531 return TRUE;
532 }
533
534 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
535 {
536 #if wxUSE_CTL3D
537 Ctl3dColorChange();
538 #else
539 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
540 Refresh();
541 #endif
542 }
543
544 // ---------------------------------------------------------------------------
545 // dialog window proc
546 // ---------------------------------------------------------------------------
547
548 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
549 {
550 long rc = 0;
551 bool processed = FALSE;
552
553 switch ( message )
554 {
555 case WM_ACTIVATE:
556 switch ( LOWORD(wParam) )
557 {
558 case WA_ACTIVE:
559 case WA_CLICKACTIVE:
560 if ( IsModalShowing() && GetParent() )
561 {
562 // bring the owner window to top as the standard dialog
563 // boxes do
564 if ( !::SetWindowPos
565 (
566 GetHwndOf(GetParent()),
567 GetHwnd(),
568 0, 0,
569 0, 0,
570 SWP_NOACTIVATE |
571 SWP_NOMOVE |
572 SWP_NOSIZE
573 ) )
574 {
575 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
576 }
577 }
578 // fall through to process it normally as well
579 }
580 break;
581
582 case WM_CLOSE:
583 // if we can't close, tell the system that we processed the
584 // message - otherwise it would close us
585 processed = !Close();
586 break;
587
588 case WM_SETCURSOR:
589 // we want to override the busy cursor for modal dialogs:
590 // typically, wxBeginBusyCursor() is called and then a modal dialog
591 // is shown, but the modal dialog shouldn't have hourglass cursor
592 if ( IsModalShowing() && wxIsBusy() )
593 {
594 // set our cursor for all windows (but see below)
595 wxCursor cursor = m_cursor;
596 if ( !cursor.Ok() )
597 cursor = wxCURSOR_ARROW;
598
599 ::SetCursor(GetHcursorOf(cursor));
600
601 // in any case, stop here and don't let wxWindow process this
602 // message (it would set the busy cursor)
603 processed = TRUE;
604
605 // but return FALSE to tell the child window (if the event
606 // comes from one of them and not from ourselves) that it can
607 // set its own cursor if it has one: thus, standard controls
608 // (e.g. text ctrl) still have correct cursors in a dialog
609 // invoked while wxIsBusy()
610 rc = FALSE;
611 }
612 break;
613 }
614
615 if ( !processed )
616 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
617
618 return rc;
619 }
620
621 #if wxUSE_CTL3D
622
623 // Define for each class of dialog and control
624 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
625 WXHWND WXUNUSED(pWnd),
626 WXUINT WXUNUSED(nCtlColor),
627 WXUINT message,
628 WXWPARAM wParam,
629 WXLPARAM lParam)
630 {
631 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
632 }
633
634 #endif // wxUSE_CTL3D
635