]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
Commented out code that seems to produce choice/combobox height problems
[wxWidgets.git] / src / msw / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dialog.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/dialog.h"
33 #include "wx/utils.h"
34 #include "wx/frame.h"
35 #include "wx/app.h"
36 #include "wx/settings.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #include "wx/msw/private.h"
42 #include "wx/log.h"
43 #include "wx/evtloop.h"
44 #include "wx/ptr_scpd.h"
45
46 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
47 #include <commdlg.h>
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // wxWin macros
52 // ----------------------------------------------------------------------------
53
54 #if wxUSE_EXTENDED_RTTI
55 WX_DEFINE_FLAGS( wxDialogStyle )
56
57 wxBEGIN_FLAGS( wxDialogStyle )
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
63 wxFLAGS_MEMBER(wxBORDER_RAISED)
64 wxFLAGS_MEMBER(wxBORDER_STATIC)
65 wxFLAGS_MEMBER(wxBORDER_NONE)
66
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
71 wxFLAGS_MEMBER(wxRAISED_BORDER)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER)
73 wxFLAGS_MEMBER(wxNO_BORDER)
74
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
78
79 // dialog styles
80 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
81 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
82 wxFLAGS_MEMBER(wxCAPTION)
83 wxFLAGS_MEMBER(wxTHICK_FRAME)
84 wxFLAGS_MEMBER(wxSYSTEM_MENU)
85 wxFLAGS_MEMBER(wxRESIZE_BORDER)
86 wxFLAGS_MEMBER(wxRESIZE_BOX)
87 wxFLAGS_MEMBER(wxCLOSE_BOX)
88 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
89 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
90 wxEND_FLAGS( wxDialogStyle )
91
92 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
93
94 wxBEGIN_PROPERTIES_TABLE(wxDialog)
95 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
97 wxEND_PROPERTIES_TABLE()
98
99 wxBEGIN_HANDLERS_TABLE(wxDialog)
100 wxEND_HANDLERS_TABLE()
101
102 wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
103
104 #else
105 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
106 #endif
107
108 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
109 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
110 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
111 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
112
113 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
114
115 EVT_CLOSE(wxDialog::OnCloseWindow)
116 END_EVENT_TABLE()
117
118 // ----------------------------------------------------------------------------
119 // wxDialogModalData
120 // ----------------------------------------------------------------------------
121
122 // this is simply a container for any data we need to implement modality which
123 // allows us to avoid changing wxDialog each time the implementation changes
124 class wxDialogModalData
125 {
126 public:
127 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
128
129 void RunLoop()
130 {
131 m_evtLoop.Run();
132 }
133
134 void ExitLoop()
135 {
136 m_evtLoop.Exit();
137 }
138
139 private:
140 wxModalEventLoop m_evtLoop;
141 };
142
143 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData);
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // wxDialog construction
151 // ----------------------------------------------------------------------------
152
153 void wxDialog::Init()
154 {
155 m_oldFocus = (wxWindow *)NULL;
156 m_isShown = false;
157 m_modalData = NULL;
158 m_endModalCalled = false;
159 }
160
161 bool wxDialog::Create(wxWindow *parent,
162 wxWindowID id,
163 const wxString& title,
164 const wxPoint& pos,
165 const wxSize& size,
166 long style,
167 const wxString& name)
168 {
169 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
170
171 // save focus before doing anything which can potentially change it
172 m_oldFocus = FindFocus();
173
174 // All dialogs should really have this style
175 style |= wxTAB_TRAVERSAL;
176
177 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
178 return false;
179
180 if ( !m_hasFont )
181 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
182
183 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
184
185 return true;
186 }
187
188 // deprecated ctor
189 wxDialog::wxDialog(wxWindow *parent,
190 const wxString& title,
191 bool WXUNUSED(modal),
192 int x,
193 int y,
194 int w,
195 int h,
196 long style,
197 const wxString& name)
198 {
199 Init();
200
201 Create(parent, -1, title, wxPoint(x, y), wxSize(w, h), style, name);
202 }
203
204 void wxDialog::SetModal(bool WXUNUSED(flag))
205 {
206 // nothing to do, obsolete method
207 }
208
209 wxDialog::~wxDialog()
210 {
211 m_isBeingDeleted = true;
212
213 // this will also reenable all the other windows for a modal dialog
214 Show(false);
215 }
216
217 // ----------------------------------------------------------------------------
218 // showing the dialogs
219 // ----------------------------------------------------------------------------
220
221 bool wxDialog::IsModalShowing() const
222 {
223 return IsModal();
224 }
225
226 wxWindow *wxDialog::FindSuitableParent() const
227 {
228 // first try to use the currently active window
229 HWND hwndFg = ::GetForegroundWindow();
230 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
231 : NULL;
232 if ( !parent )
233 {
234 // next try the main app window
235 parent = wxTheApp->GetTopWindow();
236 }
237
238 // finally, check if the parent we found is really suitable
239 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
240 {
241 // don't use this one
242 parent = NULL;
243 }
244
245 return parent;
246 }
247
248 bool wxDialog::Show(bool show)
249 {
250 if ( show == IsShown() )
251 return false;
252
253 if ( !show && m_modalData )
254 {
255 // we need to do this before calling wxDialogBase version because if we
256 // had disabled other app windows, they must be reenabled right now as
257 // if they stay disabled Windows will activate another window (one
258 // which is enabled, anyhow) when we're hidden in the base class Show()
259 // and we will lose activation
260 m_modalData->ExitLoop();
261 }
262
263 if ( show )
264 {
265 // this usually will result in TransferDataToWindow() being called
266 // which will change the controls values so do it before showing as
267 // otherwise we could have some flicker
268 InitDialog();
269 }
270
271 wxDialogBase::Show(show);
272
273 if ( show )
274 {
275 // dialogs don't get WM_SIZE message after creation unlike most (all?)
276 // other windows and so could start their life non laid out correctly
277 // if we didn't call Layout() from here
278 //
279 // NB: normally we should call it just the first time but doing it
280 // every time is simpler than keeping a flag
281 Layout();
282 }
283
284 return true;
285 }
286
287 void wxDialog::Raise()
288 {
289 ::SetForegroundWindow(GetHwnd());
290 }
291
292 // show dialog modally
293 int wxDialog::ShowModal()
294 {
295 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
296
297 m_endModalCalled = false;
298
299 Show();
300
301 // EndModal may have been called from InitDialog handler (called from
302 // inside Show()), which would cause an infinite loop if we didn't take it
303 // into account
304 if ( !m_endModalCalled )
305 {
306 // modal dialog needs a parent window, so try to find one
307 wxWindow *parent = GetParent();
308 if ( !parent )
309 {
310 parent = FindSuitableParent();
311 }
312
313 // remember where the focus was
314 wxWindow *oldFocus = m_oldFocus;
315 if ( !oldFocus )
316 {
317 // VZ: do we really want to do this?
318 oldFocus = parent;
319 }
320
321 // We have to remember the HWND because we need to check
322 // the HWND still exists (oldFocus can be garbage when the dialog
323 // exits, if it has been destroyed)
324 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
325
326
327 // enter and run the modal loop
328 {
329 wxDialogModalDataTiedPtr modalData(&m_modalData,
330 new wxDialogModalData(this));
331 modalData->RunLoop();
332 }
333
334
335 // and restore focus
336 // Note that this code MUST NOT access the dialog object's data
337 // in case the object has been deleted (which will be the case
338 // for a modal dialog that has been destroyed before calling EndModal).
339 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
340 {
341 // This is likely to prove that the object still exists
342 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
343 oldFocus->SetFocus();
344 }
345 }
346
347 return GetReturnCode();
348 }
349
350 void wxDialog::EndModal(int retCode)
351 {
352 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
353
354 m_endModalCalled = true;
355 SetReturnCode(retCode);
356
357 Show(false);
358 }
359
360 // ----------------------------------------------------------------------------
361 // wxWin event handlers
362 // ----------------------------------------------------------------------------
363
364 // Standard buttons
365 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
366 {
367 if ( Validate() && TransferDataFromWindow() )
368 {
369 EndModal(wxID_OK);
370 }
371 }
372
373 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
374 {
375 if ( Validate() )
376 TransferDataFromWindow();
377
378 // TODO probably need to disable the Apply button until things change again
379 }
380
381 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
382 {
383 EndModal(wxID_CANCEL);
384 }
385
386 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
387 {
388 // We'll send a Cancel message by default, which may close the dialog.
389 // Check for looping if the Cancel event handler calls Close().
390
391 // Note that if a cancel button and handler aren't present in the dialog,
392 // nothing will happen when you close the dialog via the window manager, or
393 // via Close(). We wouldn't want to destroy the dialog by default, since
394 // the dialog may have been created on the stack. However, this does mean
395 // that calling dialog->Close() won't delete the dialog unless the handler
396 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
397 // destroy the dialog. The default OnCancel (above) simply ends a modal
398 // dialog, and hides a modeless dialog.
399
400 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
401 // lists here? don't dare to change it now, but should be done later!
402 static wxList closing;
403
404 if ( closing.Member(this) )
405 return;
406
407 closing.Append(this);
408
409 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
410 cancelEvent.SetEventObject( this );
411 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
412
413 closing.DeleteObject(this);
414 }
415
416 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
417 {
418 #if wxUSE_CTL3D
419 Ctl3dColorChange();
420 #else
421 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
422 Refresh();
423 #endif
424 }
425
426 // ---------------------------------------------------------------------------
427 // dialog window proc
428 // ---------------------------------------------------------------------------
429
430 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
431 {
432 WXLRESULT rc = 0;
433 bool processed = false;
434
435 switch ( message )
436 {
437 #ifdef __WXWINCE__
438 // react to pressing the OK button in the title
439 case WM_COMMAND:
440 if (LOWORD(wParam) == IDOK)
441 {
442 wxButton *btn = wxDynamicCast(FindWindow(wxID_CANCEL), wxButton);
443 if ( btn && btn->IsEnabled() )
444 {
445 // if we do have a cancel button, do press it
446 btn->MSWCommand(BN_CLICKED, 0 /* unused */);
447 processed = true;
448 break;
449 }
450 }
451 break;
452 #endif
453 case WM_CLOSE:
454 // if we can't close, tell the system that we processed the
455 // message - otherwise it would close us
456 processed = !Close();
457 break;
458
459 case WM_SIZE:
460 // the Windows dialogs unfortunately are not meant to be resizeable
461 // at all and their standard class doesn't include CS_[VH]REDRAW
462 // styles which means that the window is not refreshed properly
463 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
464 // help with it - so we have to refresh it manually which certainly
465 // creates flicker but at least doesn't show garbage on the screen
466 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
467 processed = true;
468 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
469 {
470 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
471 }
472 break;
473
474 #ifndef __WXMICROWIN__
475 case WM_SETCURSOR:
476 // we want to override the busy cursor for modal dialogs:
477 // typically, wxBeginBusyCursor() is called and then a modal dialog
478 // is shown, but the modal dialog shouldn't have hourglass cursor
479 if ( IsModal() && wxIsBusy() )
480 {
481 // set our cursor for all windows (but see below)
482 wxCursor cursor = m_cursor;
483 if ( !cursor.Ok() )
484 cursor = wxCURSOR_ARROW;
485
486 ::SetCursor(GetHcursorOf(cursor));
487
488 // in any case, stop here and don't let wxWindow process this
489 // message (it would set the busy cursor)
490 processed = true;
491
492 // but return false to tell the child window (if the event
493 // comes from one of them and not from ourselves) that it can
494 // set its own cursor if it has one: thus, standard controls
495 // (e.g. text ctrl) still have correct cursors in a dialog
496 // invoked while wxIsBusy()
497 rc = false;
498 }
499 break;
500 #endif // __WXMICROWIN__
501 }
502
503 if ( !processed )
504 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
505
506 return rc;
507 }
508
509 #if wxUSE_CTL3D
510
511 // Define for each class of dialog and control
512 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
513 WXHWND WXUNUSED(pWnd),
514 WXUINT WXUNUSED(nCtlColor),
515 WXUINT message,
516 WXWPARAM wParam,
517 WXLPARAM lParam)
518 {
519 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
520 }
521
522 #endif // wxUSE_CTL3D
523