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