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