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