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