]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
fixes for compilation without wxUSE_TOOLTIPS
[wxWidgets.git] / src / msw / dialog.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
dc1c4b62 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dialog.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/dialog.h"
25#include "wx/utils.h"
26#include "wx/frame.h"
27#include "wx/app.h"
28#include "wx/settings.h"
29#endif
30
31#include "wx/msw/private.h"
32
47d67540 33#if wxUSE_COMMON_DIALOGS
2bda0e17
KB
34#include <commdlg.h>
35#endif
36
37#define wxDIALOG_DEFAULT_X 300
38#define wxDIALOG_DEFAULT_Y 300
39
40// Lists to keep track of windows, so we can disable/enable them
41// for modal dialogs
42wxList wxModalDialogs;
43wxList wxModelessWindows; // Frames and modeless dialogs
cde9f08e 44extern wxList WXDLLEXPORT wxPendingDelete;
2bda0e17
KB
45
46#if !USE_SHARED_LIBRARY
462e2437
VZ
47 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
48
49 BEGIN_EVENT_TABLE(wxDialog, wxPanel)
50 EVT_SIZE(wxDialog::OnSize)
51 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
52 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
53 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
54 EVT_CHAR_HOOK(wxDialog::OnCharHook)
55 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
56 EVT_CLOSE(wxDialog::OnCloseWindow)
57 END_EVENT_TABLE()
2bda0e17
KB
58#endif
59
2bda0e17
KB
60bool wxDialog::MSWProcessMessage(WXMSG* pMsg)
61{
62 return (::IsDialogMessage((HWND) GetHWND(), (MSG*)pMsg) != 0);
63}
64
65bool wxDialog::MSWOnClose(void)
66{
67 return Close();
68}
69
70wxDialog::wxDialog(void)
71{
72 m_isShown = FALSE;
73 m_modalShowing = FALSE;
74
75 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
76}
77
debe6624 78bool wxDialog::Create(wxWindow *parent, wxWindowID id,
462e2437
VZ
79 const wxString& title,
80 const wxPoint& pos,
81 const wxSize& size,
82 long style,
83 const wxString& name)
2bda0e17 84{
462e2437
VZ
85 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
86 SetName(name);
87
88 if (!parent)
89 wxTopLevelWindows.Append(this);
2bda0e17 90
462e2437 91 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
2bda0e17 92
462e2437 93 if (parent) parent->AddChild(this);
2bda0e17 94
462e2437
VZ
95 if ( id == -1 )
96 m_windowId = (int)NewControlId();
97 else
98 m_windowId = id;
99
100 int x = pos.x;
101 int y = pos.y;
102 int width = size.x;
103 int height = size.y;
104
105 if (x < 0) x = wxDIALOG_DEFAULT_X;
106 if (y < 0) y = wxDIALOG_DEFAULT_Y;
107
108 m_windowStyle = style;
109
110 m_isShown = FALSE;
111 m_modalShowing = FALSE;
112
113 if (width < 0)
114 width = 500;
115 if (height < 0)
116 height = 500;
117
118 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
119 if (m_windowStyle & wxSTAY_ON_TOP)
120 extendedStyle |= WS_EX_TOPMOST;
121
122 // Allows creation of dialogs with & without captions under MSWindows,
123 // resizeable or not (but a resizeable dialog always has caption -
124 // otherwise it would look too strange)
125 const char *dlg;
126 if ( style & wxTHICK_FRAME )
127 dlg = "wxResizeableDialog";
128 else if ( style & wxCAPTION )
129 dlg = "wxCaptionDialog";
130 else
131 dlg = "wxNoCaptionDialog";
132 MSWCreate(m_windowId, parent, NULL, this, NULL,
133 x, y, width, height,
134 0, // style is not used if we have dlg template
135 dlg,
136 extendedStyle);
2bda0e17 137
462e2437 138 HWND hwnd = (HWND)GetHWND();
2bda0e17 139
462e2437
VZ
140 if ( !hwnd )
141 {
142 wxLogError(_("Failed to created dialog."));
2bda0e17 143
462e2437
VZ
144 return FALSE;
145 }
2bda0e17 146
462e2437
VZ
147 SubclassWin(GetHWND());
148
149 SetWindowText(hwnd, title);
150 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 151
462e2437 152 return TRUE;
2bda0e17
KB
153}
154
debe6624 155void wxDialog::SetModal(bool flag)
2bda0e17 156{
dc1c4b62
VZ
157 if ( flag )
158 m_windowStyle |= wxDIALOG_MODAL ;
159 else
160 if ( m_windowStyle & wxDIALOG_MODAL )
161 m_windowStyle -= wxDIALOG_MODAL ;
2bda0e17
KB
162
163 wxModelessWindows.DeleteObject(this);
164 if (!flag)
165 wxModelessWindows.Append(this);
166}
167
168wxDialog::~wxDialog()
169{
170 m_isBeingDeleted = TRUE;
171
172 wxTopLevelWindows.DeleteObject(this);
173
174 if (m_modalShowing)
175 {
176 Show(FALSE);
177 // For some reason, wxWindows can activate another task altogether
178 // when a frame is destroyed after a modal dialog has been invoked.
179 // Try to bring the parent to the top.
180 // dfgg: I moved this following line from end of function -
181 // must not call if another window is on top!!
182 // This can often happen with Close() and delayed deleting
183 if (GetParent() && GetParent()->GetHWND())
184 ::BringWindowToTop((HWND) GetParent()->GetHWND());
185 }
186
187 m_modalShowing = FALSE;
188 if ( GetHWND() )
189 ShowWindow((HWND) GetHWND(), SW_HIDE);
190
191 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL) != wxDIALOG_MODAL )
192 wxModelessWindows.DeleteObject(this);
193
194 UnsubclassWin();
195
196 // If this is the last top-level window, exit.
197 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
198 {
199 wxTheApp->SetTopWindow(NULL);
200
201 if (wxTheApp->GetExitOnFrameDelete())
202 {
203 PostQuitMessage(0);
204 }
205 }
206}
207
208// By default, pressing escape cancels the dialog
209void wxDialog::OnCharHook(wxKeyEvent& event)
210{
211 if (GetHWND())
212 {
213 if (event.m_keyCode == WXK_ESCAPE)
214 {
dc1c4b62
VZ
215 // Behaviour changed in 2.0: we'll send a Cancel message
216 // to the dialog instead of Close.
217 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
218 cancelEvent.SetEventObject( this );
219 GetEventHandler()->ProcessEvent(cancelEvent);
2bda0e17 220
dc1c4b62 221 return;
2bda0e17
KB
222 }
223 }
224 // We didn't process this event.
225 event.Skip();
226}
227
228void wxDialog::OnPaint(wxPaintEvent& event)
229{
dc1c4b62
VZ
230 // No: if you call the default procedure, it makes
231 // the following painting code not work.
232// wxWindow::OnPaint(event);
2bda0e17
KB
233}
234
235void wxDialog::Fit(void)
236{
237 wxWindow::Fit();
238}
239
debe6624 240void wxDialog::Iconize(bool WXUNUSED(iconize))
2bda0e17
KB
241{
242 // Windows dialog boxes can't be iconized
243}
244
245bool wxDialog::IsIconized(void) const
246{
247 return FALSE;
248}
249
debe6624 250void wxDialog::SetClientSize(int width, int height)
2bda0e17
KB
251{
252 HWND hWnd = (HWND) GetHWND();
253 RECT rect;
254 GetClientRect(hWnd, &rect);
255
256 RECT rect2;
257 GetWindowRect(hWnd, &rect2);
258
259 // Find the difference between the entire window (title bar and all)
260 // and the client area; add this to the new client size to move the
261 // window
262 int actual_width = rect2.right - rect2.left - rect.right + width;
263 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
264
265 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
debe6624 266
2bda0e17 267 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
debe6624 268 event.SetEventObject( this );
2bda0e17 269 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
270}
271
272void wxDialog::GetPosition(int *x, int *y) const
273{
274 HWND hWnd = (HWND) GetHWND();
275 RECT rect;
276 GetWindowRect(hWnd, &rect);
277
278 *x = rect.left;
279 *y = rect.top;
280}
281
282bool wxDialog::IsShown(void) const
283{
284 return m_isShown;
285}
286
debe6624 287bool wxDialog::Show(bool show)
2bda0e17
KB
288{
289 m_isShown = show;
290
291 if (show)
292 InitDialog();
293
294 bool modal = ((GetWindowStyleFlag() & wxDIALOG_MODAL) == wxDIALOG_MODAL) ;
295
296#if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
297 if (!modal) {
298 if (show) {
299 if (!wxModelessWindows.Member(this))
dc1c4b62 300 wxModelessWindows.Append(this);
2bda0e17
KB
301 } else
302 wxModelessWindows.DeleteObject(this);
303 }
304 if (show) {
305 if (!wxTopLevelWindows.Member(this))
306 wxTopLevelWindows.Append(this);
307 } else
308 wxTopLevelWindows.DeleteObject(this);
309#endif
310
311 if (modal)
312 {
313 if (show)
314 {
dc1c4b62
VZ
315 m_hwndOldFocus = (WXHWND)::GetFocus();
316
2bda0e17
KB
317 if (m_modalShowing)
318 {
319 BringWindowToTop((HWND) GetHWND());
320 return TRUE;
321 }
322
323 m_modalShowing = TRUE;
324 wxNode *node = wxModalDialogs.First();
325 while (node)
326 {
327 wxDialog *box = (wxDialog *)node->Data();
328 if (box != this)
329 ::EnableWindow((HWND) box->GetHWND(), FALSE);
330 node = node->Next();
331 }
9c9cff0b
VZ
332
333 // if we don't do it, some window might be deleted while we have pointers
334 // to them in our disabledWindows list and the program will crash when it
335 // will try to reenable them after the modal dialog end
336 wxTheApp->DeletePendingObjects();
337 wxList disabledWindows;
338
2bda0e17
KB
339 node = wxModelessWindows.First();
340 while (node)
341 {
342 wxWindow *win = (wxWindow *)node->Data();
dc1c4b62
VZ
343 if (::IsWindowEnabled((HWND) win->GetHWND()))
344 {
2bda0e17 345 ::EnableWindow((HWND) win->GetHWND(), FALSE);
9c9cff0b 346 disabledWindows.Append(win);
dc1c4b62 347 }
2bda0e17
KB
348 node = node->Next();
349 }
350
351 ShowWindow((HWND) GetHWND(), SW_SHOW);
352 EnableWindow((HWND) GetHWND(), TRUE);
353 BringWindowToTop((HWND) GetHWND());
354
355 if (!wxModalDialogs.Member(this))
356 wxModalDialogs.Append(this);
357
358 MSG msg;
359 // Must test whether this dialog still exists: we may not process
360 // a message before the deletion.
361 while (wxModalDialogs.Member(this) && m_modalShowing && GetMessage(&msg, NULL, 0, 0))
362 {
567da5c6
JS
363 if (m_acceleratorTable.Ok() &&
364 ::TranslateAccelerator((HWND) GetHWND(), (HACCEL) m_acceleratorTable.GetHACCEL(), &msg))
365 {
366 // Have processed the message
367 }
368 else if (!IsDialogMessage((HWND) GetHWND(), &msg))
2bda0e17
KB
369 {
370 TranslateMessage(&msg);
371 DispatchMessage(&msg);
372 }
9838df2c
JS
373
374 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
375 // we should try removing the m_modalShowing test
376
2bda0e17 377 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
dc1c4b62
VZ
378 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
379 // a Show(FALSE) in the mean time!!!
380 // Without the test, we might delete the dialog before the end of modal showing.
2bda0e17
KB
381 {
382 while (wxTheApp->ProcessIdle() && m_modalShowing)
383 {
384 // Keep going until we decide we've done enough
385 }
386 }
387 }
388 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
9c9cff0b 389 node=disabledWindows.First();
2bda0e17 390 while(node) {
dc1c4b62 391 wxWindow* win = (wxWindow*) node->Data();
9838df2c
JS
392 if (wxModalDialogs.Member(win) || wxModelessWindows.Member(win))
393 {
394 HWND hWnd = (HWND) win->GetHWND();
395 if (::IsWindow(hWnd))
396 ::EnableWindow(hWnd,TRUE);
397 }
2bda0e17
KB
398 node=node->Next();
399 }
400 }
dc1c4b62 401 else // !show
2bda0e17 402 {
dc1c4b62
VZ
403 ::SetFocus((HWND)m_hwndOldFocus);
404
2bda0e17
KB
405 wxModalDialogs.DeleteObject(this);
406
407 wxNode *last = wxModalDialogs.Last();
408
409 // If there's still a modal dialog active, we
410 // enable it, else we enable all modeless windows
411 if (last)
412 {
413 wxDialog *box = (wxDialog *)last->Data();
414 HWND hwnd = (HWND) box->GetHWND();
415 if (box->m_winEnabled)
416 EnableWindow(hwnd, TRUE);
417 BringWindowToTop(hwnd);
418 }
419 else
420 {
421 wxNode *node = wxModelessWindows.First();
422 while (node)
423 {
424 wxWindow *win = (wxWindow *)node->Data();
425 HWND hwnd = (HWND) win->GetHWND();
426 // Only enable again if not user-disabled.
427 if (win->IsUserEnabled())
428 EnableWindow(hwnd, TRUE);
429 node = node->Next();
430 }
431 }
432 // Try to highlight the correct window (the parent)
433 HWND hWndParent = 0;
434 if (GetParent())
435 {
436 hWndParent = (HWND) GetParent()->GetHWND();
437 if (hWndParent)
438 ::BringWindowToTop(hWndParent);
439 }
440 ShowWindow((HWND) GetHWND(), SW_HIDE);
441 m_modalShowing = FALSE;
442 }
443 }
dc1c4b62 444 else // !modal
2bda0e17
KB
445 {
446 if (show)
447 {
448 ShowWindow((HWND) GetHWND(), SW_SHOW);
449 BringWindowToTop((HWND) GetHWND());
450 }
451 else
452 {
453 // Try to highlight the correct window (the parent)
454 HWND hWndParent = 0;
455 if (GetParent())
456 {
457 hWndParent = (HWND) GetParent()->GetHWND();
458 if (hWndParent)
459 ::BringWindowToTop(hWndParent);
460 }
461 ShowWindow((HWND) GetHWND(), SW_HIDE);
462 }
463 }
464 return TRUE;
465}
466
467void wxDialog::SetTitle(const wxString& title)
468{
469 SetWindowText((HWND) GetHWND(), (const char *)title);
470}
471
472wxString wxDialog::GetTitle(void) const
473{
474 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
475 return wxString(wxBuffer);
476}
477
debe6624 478void wxDialog::Centre(int direction)
2bda0e17
KB
479{
480 int x_offset,y_offset ;
481 int display_width, display_height;
482 int width, height, x, y;
dfc54541
JS
483 wxWindow *parent = GetParent();
484 if ((direction & wxCENTER_FRAME) && parent)
2bda0e17 485 {
dfc54541
JS
486 parent->GetPosition(&x_offset,&y_offset) ;
487 parent->GetSize(&display_width,&display_height) ;
2bda0e17
KB
488 }
489 else
2bda0e17
KB
490 {
491 wxDisplaySize(&display_width, &display_height);
492 x_offset = 0 ;
493 y_offset = 0 ;
494 }
495
2bda0e17
KB
496 GetSize(&width, &height);
497 GetPosition(&x, &y);
498
499 if (direction & wxHORIZONTAL)
500 x = (int)((display_width - width)/2);
501 if (direction & wxVERTICAL)
502 y = (int)((display_height - height)/2);
503
504 SetSize(x+x_offset, y+y_offset, width, height);
505}
506
507// Replacement for Show(TRUE) for modal dialogs - returns return code
508int wxDialog::ShowModal(void)
509{
dc1c4b62
VZ
510 m_windowStyle |= wxDIALOG_MODAL;
511 Show(TRUE);
512 return GetReturnCode();
2bda0e17
KB
513}
514
515void wxDialog::EndModal(int retCode)
516{
dc1c4b62
VZ
517 SetReturnCode(retCode);
518 Show(FALSE);
2bda0e17
KB
519}
520
521// Define for each class of dialog and control
debe6624 522WXHBRUSH wxDialog::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
dc1c4b62 523 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 524{
1f112209 525#if wxUSE_CTL3D
2bda0e17
KB
526 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
527 return (WXHBRUSH) hbrush;
528#else
529 return 0;
530#endif
531}
532
533// Standard buttons
534void wxDialog::OnOK(wxCommandEvent& event)
535{
dc1c4b62
VZ
536 if ( Validate() && TransferDataFromWindow() )
537 {
2bda0e17
KB
538 if ( IsModal() )
539 EndModal(wxID_OK);
540 else
541 {
387a3b02
JS
542 SetReturnCode(wxID_OK);
543 this->Show(FALSE);
2bda0e17 544 }
dc1c4b62 545 }
2bda0e17
KB
546}
547
548void wxDialog::OnApply(wxCommandEvent& event)
549{
dc1c4b62
VZ
550 if (Validate())
551 TransferDataFromWindow();
552 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
553}
554
555void wxDialog::OnCancel(wxCommandEvent& event)
556{
557 if ( IsModal() )
558 EndModal(wxID_CANCEL);
559 else
560 {
561 SetReturnCode(wxID_CANCEL);
dc1c4b62 562 this->Show(FALSE);
2bda0e17
KB
563 }
564}
565
e3065973 566void wxDialog::OnCloseWindow(wxCloseEvent& event)
2bda0e17 567{
e3065973 568 // We'll send a Cancel message by default,
2bda0e17 569 // which may close the dialog.
e3065973
JS
570 // Check for looping if the Cancel event handler calls Close().
571
572 // Note that if a cancel button and handler aren't present in the dialog,
573 // nothing will happen when you close the dialog via the window manager, or
574 // via Close().
575 // We wouldn't want to destroy the dialog by default, since the dialog may have been
576 // created on the stack.
577 // However, this does mean that calling dialog->Close() won't delete the dialog
578 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
579 // sure to destroy the dialog.
580 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
2bda0e17
KB
581
582 static wxList closing;
e3065973 583
2bda0e17 584 if ( closing.Member(this) )
e3065973
JS
585 return;
586
2bda0e17 587 closing.Append(this);
e3065973 588
387a3b02
JS
589 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
590 cancelEvent.SetEventObject( this );
e3065973 591 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
2bda0e17
KB
592
593 closing.DeleteObject(this);
e3065973 594}
2bda0e17 595
e3065973
JS
596// Destroy the window (delayed, if a managed window)
597bool wxDialog::Destroy(void)
598{
599 if (!wxPendingDelete.Member(this))
600 wxPendingDelete.Append(this);
601 return TRUE;
2bda0e17
KB
602}
603
94b49b93
JS
604void wxDialog::OnSize(wxSizeEvent& WXUNUSED(event))
605{
606 // if we're using constraints - do use them
607 #if wxUSE_CONSTRAINTS
608 if ( GetAutoLayout() ) {
609 Layout();
610 }
611 #endif
612}
613
2bda0e17
KB
614void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
615{
1f112209 616#if wxUSE_CTL3D
2bda0e17
KB
617 Ctl3dColorChange();
618#else
619 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
620 Refresh();
621#endif
462e2437 622}