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