]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
wxProcess-related code now works
[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 WXDLLEXPORT wxPendingDelete;
45
46 #if !USE_SHARED_LIBRARY
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()
58 #endif
59
60 bool wxDialog::MSWProcessMessage(WXMSG* pMsg)
61 {
62 return (::IsDialogMessage((HWND) GetHWND(), (MSG*)pMsg) != 0);
63 }
64
65 bool wxDialog::MSWOnClose(void)
66 {
67 return Close();
68 }
69
70 wxDialog::wxDialog(void)
71 {
72 m_isShown = FALSE;
73 m_modalShowing = FALSE;
74
75 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
76 }
77
78 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
79 const wxString& title,
80 const wxPoint& pos,
81 const wxSize& size,
82 long style,
83 const wxString& name)
84 {
85 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
86 SetName(name);
87
88 if (!parent)
89 wxTopLevelWindows.Append(this);
90
91 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
92
93 if (parent) parent->AddChild(this);
94
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);
137
138 HWND hwnd = (HWND)GetHWND();
139
140 if ( !hwnd )
141 {
142 wxLogError(_("Failed to created dialog."));
143
144 return FALSE;
145 }
146
147 SubclassWin(GetHWND());
148
149 SetWindowText(hwnd, title);
150 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
151
152 return TRUE;
153 }
154
155 void wxDialog::SetModal(bool flag)
156 {
157 if ( flag )
158 m_windowStyle |= wxDIALOG_MODAL ;
159 else
160 if ( m_windowStyle & wxDIALOG_MODAL )
161 m_windowStyle -= wxDIALOG_MODAL ;
162
163 wxModelessWindows.DeleteObject(this);
164 if (!flag)
165 wxModelessWindows.Append(this);
166 }
167
168 wxDialog::~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
209 void wxDialog::OnCharHook(wxKeyEvent& event)
210 {
211 if (GetHWND())
212 {
213 if (event.m_keyCode == WXK_ESCAPE)
214 {
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);
220
221 return;
222 }
223 }
224 // We didn't process this event.
225 event.Skip();
226 }
227
228 void wxDialog::OnPaint(wxPaintEvent& event)
229 {
230 // No: if you call the default procedure, it makes
231 // the following painting code not work.
232 // wxWindow::OnPaint(event);
233 }
234
235 void wxDialog::Fit(void)
236 {
237 wxWindow::Fit();
238 }
239
240 void wxDialog::Iconize(bool WXUNUSED(iconize))
241 {
242 // Windows dialog boxes can't be iconized
243 }
244
245 bool wxDialog::IsIconized(void) const
246 {
247 return FALSE;
248 }
249
250 void wxDialog::SetClientSize(int width, int height)
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);
266
267 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
268 event.SetEventObject( this );
269 GetEventHandler()->ProcessEvent(event);
270 }
271
272 void 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
282 bool wxDialog::IsShown(void) const
283 {
284 return m_isShown;
285 }
286
287 bool wxDialog::Show(bool show)
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))
300 wxModelessWindows.Append(this);
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 {
315 m_hwndOldFocus = (WXHWND)::GetFocus();
316
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 }
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
339 node = wxModelessWindows.First();
340 while (node)
341 {
342 wxWindow *win = (wxWindow *)node->Data();
343 if (::IsWindowEnabled((HWND) win->GetHWND()))
344 {
345 ::EnableWindow((HWND) win->GetHWND(), FALSE);
346 disabledWindows.Append(win);
347 }
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 {
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))
369 {
370 TranslateMessage(&msg);
371 DispatchMessage(&msg);
372 }
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
377 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
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.
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
389 node=disabledWindows.First();
390 while(node) {
391 wxWindow* win = (wxWindow*) node->Data();
392 if (wxModalDialogs.Member(win) || wxModelessWindows.Member(win))
393 {
394 HWND hWnd = (HWND) win->GetHWND();
395 if (::IsWindow(hWnd))
396 ::EnableWindow(hWnd,TRUE);
397 }
398 node=node->Next();
399 }
400 }
401 else // !show
402 {
403 ::SetFocus((HWND)m_hwndOldFocus);
404
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 }
444 else // !modal
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
467 void wxDialog::SetTitle(const wxString& title)
468 {
469 SetWindowText((HWND) GetHWND(), (const char *)title);
470 }
471
472 wxString wxDialog::GetTitle(void) const
473 {
474 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
475 return wxString(wxBuffer);
476 }
477
478 void wxDialog::Centre(int direction)
479 {
480 int x_offset,y_offset ;
481 int display_width, display_height;
482 int width, height, x, y;
483 wxWindow *parent = GetParent();
484 if ((direction & wxCENTER_FRAME) && parent)
485 {
486 parent->GetPosition(&x_offset,&y_offset) ;
487 parent->GetSize(&display_width,&display_height) ;
488 }
489 else
490 {
491 wxDisplaySize(&display_width, &display_height);
492 x_offset = 0 ;
493 y_offset = 0 ;
494 }
495
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
508 int wxDialog::ShowModal(void)
509 {
510 m_windowStyle |= wxDIALOG_MODAL;
511 Show(TRUE);
512 return GetReturnCode();
513 }
514
515 void wxDialog::EndModal(int retCode)
516 {
517 SetReturnCode(retCode);
518 Show(FALSE);
519 }
520
521 // Define for each class of dialog and control
522 WXHBRUSH wxDialog::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
523 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
524 {
525 #if wxUSE_CTL3D
526 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
527 return (WXHBRUSH) hbrush;
528 #else
529 return 0;
530 #endif
531 }
532
533 // Standard buttons
534 void wxDialog::OnOK(wxCommandEvent& event)
535 {
536 if ( Validate() && TransferDataFromWindow() )
537 {
538 if ( IsModal() )
539 EndModal(wxID_OK);
540 else
541 {
542 SetReturnCode(wxID_OK);
543 this->Show(FALSE);
544 }
545 }
546 }
547
548 void wxDialog::OnApply(wxCommandEvent& event)
549 {
550 if (Validate())
551 TransferDataFromWindow();
552 // TODO probably need to disable the Apply button until things change again
553 }
554
555 void wxDialog::OnCancel(wxCommandEvent& event)
556 {
557 if ( IsModal() )
558 EndModal(wxID_CANCEL);
559 else
560 {
561 SetReturnCode(wxID_CANCEL);
562 this->Show(FALSE);
563 }
564 }
565
566 void wxDialog::OnCloseWindow(wxCloseEvent& event)
567 {
568 // We'll send a Cancel message by default,
569 // which may close the dialog.
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.
581
582 static wxList closing;
583
584 if ( closing.Member(this) )
585 return;
586
587 closing.Append(this);
588
589 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
590 cancelEvent.SetEventObject( this );
591 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
592
593 closing.DeleteObject(this);
594 }
595
596 // Destroy the window (delayed, if a managed window)
597 bool wxDialog::Destroy(void)
598 {
599 if (!wxPendingDelete.Member(this))
600 wxPendingDelete.Append(this);
601 return TRUE;
602 }
603
604 void 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
614 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
615 {
616 #if wxUSE_CTL3D
617 Ctl3dColorChange();
618 #else
619 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
620 Refresh();
621 #endif
622 }