]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
wxTempFile bug corrected: the temp file is now created in the same dir
[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 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
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 (FARPROC) 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 SetDefaultBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
82 }
83
84 bool wxDialog::Create(wxWindow *parent, const wxWindowID id,
85 const wxString& title,
86 const wxPoint& pos,
87 const wxSize& size,
88 const long style,
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 )
103 m_windowId = (int)NewControlId();
104 else
105 m_windowId = id;
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",
132 extendedStyle);
133 }
134 else{
135 MSWCreate(m_windowId, (wxWindow *)parent, NULL, this, NULL, x, y, width, height, 0, "wxNoCaptionDialog",
136 extendedStyle);
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
147 void wxDialog::SetModal(const bool flag)
148 {
149 if ( flag )
150 m_windowStyle |= wxDIALOG_MODAL ;
151 else
152 if ( m_windowStyle & wxDIALOG_MODAL )
153 m_windowStyle -= wxDIALOG_MODAL ;
154
155 wxModelessWindows.DeleteObject(this);
156 if (!flag)
157 wxModelessWindows.Append(this);
158 }
159
160 wxDialog::~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
201 void wxDialog::OnCharHook(wxKeyEvent& event)
202 {
203 if (GetHWND())
204 {
205 if (event.m_keyCode == WXK_ESCAPE)
206 {
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);
212
213 return;
214 }
215 }
216 // We didn't process this event.
217 event.Skip();
218 }
219
220 void wxDialog::OnPaint(wxPaintEvent& event)
221 {
222 // No: if you call the default procedure, it makes
223 // the following painting code not work.
224 // wxWindow::OnPaint(event);
225 }
226
227 void wxDialog::Fit(void)
228 {
229 wxWindow::Fit();
230 }
231
232 void wxDialog::Iconize(const bool WXUNUSED(iconize))
233 {
234 // Windows dialog boxes can't be iconized
235 }
236
237 bool wxDialog::IsIconized(void) const
238 {
239 return FALSE;
240 }
241
242 void wxDialog::SetSize(const int x, const int y, const int width, const int height, const int WXUNUSED(sizeFlags))
243 {
244 wxWindow::SetSize(x, y, width, height);
245 }
246
247 void wxDialog::SetClientSize(const int width, const int height)
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);
263 #if WXWIN_COMPATIBILITY
264 GetEventHandler()->OldOnSize(actual_width, actual_height);
265 #else
266 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
267 event.eventObject = this;
268 GetEventHandler()->ProcessEvent(event);
269 #endif
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(const 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 wxList DisabledWindows;
316 if (m_modalShowing)
317 {
318 BringWindowToTop((HWND) GetHWND());
319 return TRUE;
320 }
321
322 m_modalShowing = TRUE;
323 wxNode *node = wxModalDialogs.First();
324 while (node)
325 {
326 wxDialog *box = (wxDialog *)node->Data();
327 if (box != this)
328 ::EnableWindow((HWND) box->GetHWND(), FALSE);
329 node = node->Next();
330 }
331 node = wxModelessWindows.First();
332 while (node)
333 {
334 wxWindow *win = (wxWindow *)node->Data();
335 if (::IsWindowEnabled((HWND) win->GetHWND()))
336 {
337 ::EnableWindow((HWND) win->GetHWND(), FALSE);
338 DisabledWindows.Append(win);
339 }
340 node = node->Next();
341 }
342
343 ShowWindow((HWND) GetHWND(), SW_SHOW);
344 EnableWindow((HWND) GetHWND(), TRUE);
345 BringWindowToTop((HWND) GetHWND());
346
347 if (!wxModalDialogs.Member(this))
348 wxModalDialogs.Append(this);
349
350 MSG msg;
351 // Must test whether this dialog still exists: we may not process
352 // a message before the deletion.
353 while (wxModalDialogs.Member(this) && m_modalShowing && GetMessage(&msg, NULL, 0, 0))
354 {
355 if (!IsDialogMessage((HWND) GetHWND(), &msg))
356 {
357 TranslateMessage(&msg);
358 DispatchMessage(&msg);
359 }
360 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
361 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
362 // a Show(FALSE) in the mean time!!!
363 // Without the test, we might delete the dialog before the end of modal showing.
364 {
365 while (wxTheApp->ProcessIdle() && m_modalShowing)
366 {
367 // Keep going until we decide we've done enough
368 }
369 }
370 }
371 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
372 node=DisabledWindows.First();
373 while(node) {
374 wxWindow* win = (wxWindow*) node->Data();
375 HWND hWnd = (HWND) win->GetHWND();
376 if (::IsWindow(hWnd) && (wxModalDialogs.Member(win) || wxModelessWindows.Member(win) ))
377 ::EnableWindow(hWnd,TRUE);
378 node=node->Next();
379 }
380 }
381 else
382 {
383 wxModalDialogs.DeleteObject(this);
384
385 wxNode *last = wxModalDialogs.Last();
386
387 // If there's still a modal dialog active, we
388 // enable it, else we enable all modeless windows
389 if (last)
390 {
391 wxDialog *box = (wxDialog *)last->Data();
392 HWND hwnd = (HWND) box->GetHWND();
393 if (box->m_winEnabled)
394 EnableWindow(hwnd, TRUE);
395 BringWindowToTop(hwnd);
396 }
397 else
398 {
399 wxNode *node = wxModelessWindows.First();
400 while (node)
401 {
402 wxWindow *win = (wxWindow *)node->Data();
403 HWND hwnd = (HWND) win->GetHWND();
404 // Only enable again if not user-disabled.
405 if (win->IsUserEnabled())
406 EnableWindow(hwnd, TRUE);
407 node = node->Next();
408 }
409 }
410 // Try to highlight the correct window (the parent)
411 HWND hWndParent = 0;
412 if (GetParent())
413 {
414 hWndParent = (HWND) GetParent()->GetHWND();
415 if (hWndParent)
416 ::BringWindowToTop(hWndParent);
417 }
418 ShowWindow((HWND) GetHWND(), SW_HIDE);
419 m_modalShowing = FALSE;
420 }
421 }
422 else
423 {
424 if (show)
425 {
426 ShowWindow((HWND) GetHWND(), SW_SHOW);
427 BringWindowToTop((HWND) GetHWND());
428 }
429 else
430 {
431 // Try to highlight the correct window (the parent)
432 HWND hWndParent = 0;
433 if (GetParent())
434 {
435 hWndParent = (HWND) GetParent()->GetHWND();
436 if (hWndParent)
437 ::BringWindowToTop(hWndParent);
438 }
439 ShowWindow((HWND) GetHWND(), SW_HIDE);
440 }
441 }
442 return TRUE;
443 }
444
445 void wxDialog::SetTitle(const wxString& title)
446 {
447 SetWindowText((HWND) GetHWND(), (const char *)title);
448 }
449
450 wxString wxDialog::GetTitle(void) const
451 {
452 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
453 return wxString(wxBuffer);
454 }
455
456 void wxDialog::Centre(const int direction)
457 {
458 int x_offset,y_offset ;
459 int display_width, display_height;
460 int width, height, x, y;
461 wxFrame *frame ;
462 if (direction & wxCENTER_FRAME)
463 {
464 frame = (wxFrame*)GetParent() ;
465 if (frame)
466 {
467 frame->GetPosition(&x_offset,&y_offset) ;
468 frame->GetSize(&display_width,&display_height) ;
469 }
470 }
471 else
472 frame = NULL ;
473
474 if (frame==NULL)
475 {
476 wxDisplaySize(&display_width, &display_height);
477 x_offset = 0 ;
478 y_offset = 0 ;
479 }
480
481
482 GetSize(&width, &height);
483 GetPosition(&x, &y);
484
485 if (direction & wxHORIZONTAL)
486 x = (int)((display_width - width)/2);
487 if (direction & wxVERTICAL)
488 y = (int)((display_height - height)/2);
489
490 SetSize(x+x_offset, y+y_offset, width, height);
491 }
492
493 // Replacement for Show(TRUE) for modal dialogs - returns return code
494 int wxDialog::ShowModal(void)
495 {
496 Show(TRUE);
497 return GetReturnCode();
498 }
499
500 void wxDialog::EndModal(int retCode)
501 {
502 SetReturnCode(retCode);
503 Show(FALSE);
504 }
505
506 // Define for each class of dialog and control
507 WXHBRUSH wxDialog::OnCtlColor(const WXHDC pDC, const WXHWND pWnd, const WXUINT nCtlColor,
508 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
509 {
510 #if CTL3D
511 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
512 return (WXHBRUSH) hbrush;
513 #else
514 return 0;
515 #endif
516 }
517
518 // Standard buttons
519 void wxDialog::OnOK(wxCommandEvent& event)
520 {
521 if ( Validate() && TransferDataFromWindow() )
522 {
523 if ( IsModal() )
524 EndModal(wxID_OK);
525 else
526 {
527 SetReturnCode(wxID_OK);
528 this->Show(FALSE);
529 }
530 }
531 }
532
533 void wxDialog::OnApply(wxCommandEvent& event)
534 {
535 if (Validate())
536 TransferDataFromWindow();
537 // TODO probably need to disable the Apply button until things change again
538 }
539
540 void wxDialog::OnCancel(wxCommandEvent& event)
541 {
542 if ( IsModal() )
543 EndModal(wxID_CANCEL);
544 else
545 {
546 SetReturnCode(wxID_CANCEL);
547 this->Show(FALSE);
548 }
549 }
550
551 bool wxDialog::OnClose(void)
552 {
553 // Behaviour changed in 2.0: we'll send a Cancel message by default,
554 // which may close the dialog.
555 // Check for looping if the Cancel event handler calls Close()
556
557 static wxList closing;
558
559 if ( closing.Member(this) )
560 return FALSE;
561
562 closing.Append(this);
563
564 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
565 cancelEvent.SetEventObject( this );
566 GetEventHandler()->ProcessEvent(cancelEvent);
567
568 closing.DeleteObject(this);
569
570 return FALSE;
571 }
572
573 void wxDialog::OnCloseWindow(wxCloseEvent& event)
574 {
575 // Compatibility
576 if ( GetEventHandler()->OnClose() || event.GetForce())
577 {
578 this->Destroy();
579 }
580 }
581
582 // Destroy the window (delayed, if a managed window)
583 bool wxDialog::Destroy(void)
584 {
585 if (!wxPendingDelete.Member(this))
586 wxPendingDelete.Append(this);
587 return TRUE;
588 }
589
590 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
591 {
592 #if CTL3D
593 Ctl3dColorChange();
594 #else
595 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
596 SetDefaultBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
597 Refresh();
598 #endif
599 }
600
601 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
602 {
603 return wxWindow::MSWWindowProc(message, wParam, lParam);
604 }
605