1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/dialog.cpp 
   3 // Purpose:     wxDialog class 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  27 #include "wx/dialog.h" 
  30     #include "wx/msw/wrapcdlg.h" 
  34     #include "wx/button.h" 
  35     #include "wx/settings.h" 
  38     #include "wx/toolbar.h" 
  41 #include "wx/msw/private.h" 
  42 #include "wx/evtloop.h" 
  43 #include "wx/scopedptr.h" 
  45 #if defined(__SMARTPHONE__) && defined(__WXWINCE__) 
  46     #include "wx/msw/wince/resources.h" 
  47 #endif // __SMARTPHONE__ && __WXWINCE__ 
  49 // ---------------------------------------------------------------------------- 
  51 // ---------------------------------------------------------------------------- 
  53 #if wxUSE_EXTENDED_RTTI 
  54 WX_DEFINE_FLAGS( wxDialogStyle 
) 
  56 wxBEGIN_FLAGS( wxDialogStyle 
) 
  57     // new style border flags, we put them first to 
  58     // use them for streaming out 
  59     wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
  60     wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
  61     wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
  62     wxFLAGS_MEMBER(wxBORDER_RAISED
) 
  63     wxFLAGS_MEMBER(wxBORDER_STATIC
) 
  64     wxFLAGS_MEMBER(wxBORDER_NONE
) 
  66     // old style border flags 
  67     wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
  68     wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
  69     wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
  70     wxFLAGS_MEMBER(wxRAISED_BORDER
) 
  71     wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
  72     wxFLAGS_MEMBER(wxNO_BORDER
) 
  74     // standard window styles 
  75     wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
  76     wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
  79     wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
) 
  80     wxFLAGS_MEMBER(wxSTAY_ON_TOP
) 
  81     wxFLAGS_MEMBER(wxCAPTION
) 
  82 #if WXWIN_COMPATIBILITY_2_6 
  83     wxFLAGS_MEMBER(wxTHICK_FRAME
) 
  84 #endif // WXWIN_COMPATIBILITY_2_6 
  85     wxFLAGS_MEMBER(wxSYSTEM_MENU
) 
  86     wxFLAGS_MEMBER(wxRESIZE_BORDER
) 
  87 #if WXWIN_COMPATIBILITY_2_6 
  88     wxFLAGS_MEMBER(wxRESIZE_BOX
) 
  89 #endif // WXWIN_COMPATIBILITY_2_6 
  90     wxFLAGS_MEMBER(wxCLOSE_BOX
) 
  91     wxFLAGS_MEMBER(wxMAXIMIZE_BOX
) 
  92     wxFLAGS_MEMBER(wxMINIMIZE_BOX
) 
  93 wxEND_FLAGS( wxDialogStyle 
) 
  95 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h") 
  97 wxBEGIN_PROPERTIES_TABLE(wxDialog
) 
  98     wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
  99     wxPROPERTY_FLAGS( WindowStyle 
, wxDialogStyle 
, long , SetWindowStyleFlag 
, GetWindowStyleFlag 
, EMPTY_MACROVALUE 
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
 100 wxEND_PROPERTIES_TABLE() 
 102 wxBEGIN_HANDLERS_TABLE(wxDialog
) 
 103 wxEND_HANDLERS_TABLE() 
 105 wxCONSTRUCTOR_6( wxDialog 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxString 
, Title 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle
) 
 108 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
) 
 111 // ---------------------------------------------------------------------------- 
 113 // ---------------------------------------------------------------------------- 
 115 // this is simply a container for any data we need to implement modality which 
 116 // allows us to avoid changing wxDialog each time the implementation changes 
 117 class wxDialogModalData
 
 120     wxDialogModalData(wxDialog 
*dialog
) : m_evtLoop(dialog
) { } 
 133     wxModalEventLoop m_evtLoop
; 
 136 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
) 
 138 // ============================================================================ 
 140 // ============================================================================ 
 142 // ---------------------------------------------------------------------------- 
 143 // wxDialog construction 
 144 // ---------------------------------------------------------------------------- 
 146 void wxDialog::Init() 
 150 #if wxUSE_TOOLBAR && defined(__POCKETPC__) 
 151     m_dialogToolBar 
= NULL
; 
 153 #if wxUSE_DIALOG_SIZEGRIP 
 155 #endif // wxUSE_DIALOG_SIZEGRIP 
 158 bool wxDialog::Create(wxWindow 
*parent
, 
 160                       const wxString
& title
, 
 164                       const wxString
& name
) 
 166     SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
); 
 168     // All dialogs should really have this style 
 169     style 
|= wxTAB_TRAVERSAL
; 
 171     if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) ) 
 175         SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 177 #if defined(__SMARTPHONE__) && defined(__WXWINCE__) 
 178     SetLeftMenu(wxID_OK
, _("OK")); 
 180 #if wxUSE_TOOLBAR && defined(__POCKETPC__) 
 184 #if wxUSE_DIALOG_SIZEGRIP 
 185     if ( HasFlag(wxRESIZE_BORDER
) ) 
 189         Connect(wxEVT_CREATE
, 
 190                 wxWindowCreateEventHandler(wxDialog::OnWindowCreate
)); 
 192 #endif // wxUSE_DIALOG_SIZEGRIP 
 197 wxDialog::~wxDialog() 
 199     // this will also reenable all the other windows for a modal dialog 
 202 #if wxUSE_DIALOG_SIZEGRIP 
 204 #endif // wxUSE_DIALOG_SIZEGRIP 
 207 // ---------------------------------------------------------------------------- 
 208 // showing the dialogs 
 209 // ---------------------------------------------------------------------------- 
 211 bool wxDialog::Show(bool show
) 
 213     if ( show 
== IsShown() ) 
 216     if ( !show 
&& m_modalData 
) 
 218         // we need to do this before calling wxDialogBase version because if we 
 219         // had disabled other app windows, they must be reenabled right now as 
 220         // if they stay disabled Windows will activate another window (one 
 221         // which is enabled, anyhow) when we're hidden in the base class Show() 
 222         // and we will lose activation 
 223         m_modalData
->ExitLoop(); 
 228         if (CanDoLayoutAdaptation()) 
 229             DoLayoutAdaptation(); 
 231         // this usually will result in TransferDataToWindow() being called 
 232         // which will change the controls values so do it before showing as 
 233         // otherwise we could have some flicker 
 237     wxDialogBase::Show(show
); 
 241         // dialogs don't get WM_SIZE message from ::ShowWindow() for some 
 242         // reason so generate it ourselves for consistency with frames and 
 243         // dialogs in other ports 
 245         // NB: normally we should call it just the first time but doing it 
 246         //     every time is simpler than keeping a flag 
 247         const wxSize size 
= GetClientSize(); 
 248         ::SendMessage(GetHwnd(), WM_SIZE
, 
 249                       SIZE_RESTORED
, MAKELPARAM(size
.x
, size
.y
)); 
 255 void wxDialog::Raise() 
 257     ::SetForegroundWindow(GetHwnd()); 
 260 // show dialog modally 
 261 int wxDialog::ShowModal() 
 263     wxASSERT_MSG( !IsModal(), _T("ShowModal() can't be called twice") ); 
 267     // EndModal may have been called from InitDialog handler (called from 
 268     // inside Show()) and hidden the dialog back again 
 271         // enter and run the modal loop 
 272         wxDialogModalDataTiedPtr 
modalData(&m_modalData
, 
 273                                            new wxDialogModalData(this)); 
 274         modalData
->RunLoop(); 
 277     return GetReturnCode(); 
 280 void wxDialog::EndModal(int retCode
) 
 282     wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") ); 
 284     SetReturnCode(retCode
); 
 289 // ---------------------------------------------------------------------------- 
 290 // wxDialog gripper handling 
 291 // ---------------------------------------------------------------------------- 
 293 #if wxUSE_DIALOG_SIZEGRIP 
 295 void wxDialog::SetWindowStyleFlag(long style
) 
 297     wxDialogBase::SetWindowStyleFlag(style
); 
 299     if ( HasFlag(wxRESIZE_BORDER
) ) 
 305 void wxDialog::CreateGripper() 
 309         // just create it here, it will be positioned and shown later 
 310         m_hGripper 
= (WXHWND
)::CreateWindow
 
 318                                     SBS_SIZEBOXBOTTOMRIGHTALIGN
, 
 328 void wxDialog::DestroyGripper() 
 332         // we used to have trouble with gripper appearing on top (and hence 
 333         // overdrawing) the other, real, dialog children -- check that this 
 334         // isn't the case automatically (but notice that this could be false if 
 335         // we're not shown at all as in this case ResizeGripper() might not 
 336         // have been called yet) 
 337         wxASSERT_MSG( !IsShown() || 
 338                       ::GetWindow((HWND
)m_hGripper
, GW_HWNDNEXT
) == 0, 
 339             _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") ); 
 340         ::DestroyWindow((HWND
) m_hGripper
); 
 345 void wxDialog::ShowGripper(bool show
) 
 347     wxASSERT_MSG( m_hGripper
, _T("shouldn't be called if we have no gripper") ); 
 352     ::ShowWindow((HWND
)m_hGripper
, show 
? SW_SHOW 
: SW_HIDE
); 
 355 void wxDialog::ResizeGripper() 
 357     wxASSERT_MSG( m_hGripper
, _T("shouldn't be called if we have no gripper") ); 
 359     HWND hwndGripper 
= (HWND
)m_hGripper
; 
 361     const wxRect rectGripper 
= wxRectFromRECT(wxGetWindowRect(hwndGripper
)); 
 362     const wxSize size 
= GetClientSize() - rectGripper
.GetSize(); 
 364     ::SetWindowPos(hwndGripper
, HWND_BOTTOM
, 
 366                    rectGripper
.width
, rectGripper
.height
, 
 370 void wxDialog::OnWindowCreate(wxWindowCreateEvent
& event
) 
 372     if ( m_hGripper 
&& IsShown() && 
 373             event
.GetWindow() && event
.GetWindow()->GetParent() == this ) 
 375         // Put gripper below the newly created child window 
 376         ::SetWindowPos((HWND
)m_hGripper
, HWND_BOTTOM
, 0, 0, 0, 0, 
 377                        SWP_NOSIZE 
| SWP_NOMOVE 
| SWP_NOACTIVATE
); 
 383 #endif // wxUSE_DIALOG_SIZEGRIP 
 385 // ---------------------------------------------------------------------------- 
 386 // wxWin event handlers 
 387 // ---------------------------------------------------------------------------- 
 390 // Responds to the OK button in a PocketPC titlebar. This 
 391 // can be overridden, or you can change the id used for 
 392 // sending the event, by calling SetAffirmativeId. 
 393 bool wxDialog::DoOK() 
 395     const int idOk 
= GetAffirmativeId(); 
 396     if ( EmulateButtonClickIfPresent(idOk
) ) 
 399     wxCommandEvent 
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId()); 
 400     event
.SetEventObject(this); 
 402     return HandleWindowEvent(event
); 
 404 #endif // __POCKETPC__ 
 406 #if wxUSE_TOOLBAR && defined(__POCKETPC__) 
 407 // create main toolbar by calling OnCreateToolBar() 
 408 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
) 
 410     m_dialogToolBar 
= OnCreateToolBar(style
, winid
, name
); 
 412     return m_dialogToolBar
; 
 415 // return a new toolbar 
 416 wxToolBar 
*wxDialog::OnCreateToolBar(long style
, 
 418                                        const wxString
& name
) 
 420     return new wxToolMenuBar(this, winid
, 
 421                          wxDefaultPosition
, wxDefaultSize
, 
 426 // --------------------------------------------------------------------------- 
 427 // dialog Windows messages processing 
 428 // --------------------------------------------------------------------------- 
 430 WXLRESULT 
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
) 
 433     bool processed 
= false; 
 438         // react to pressing the OK button in the title 
 441             switch ( LOWORD(wParam
) ) 
 447                         processed 
= !Close(); 
 449 #ifdef __SMARTPHONE__ 
 452                     processed 
= HandleCommand( LOWORD(wParam
) , 0 , NULL 
); 
 454 #endif // __SMARTPHONE__ 
 460             // if we can't close, tell the system that we processed the 
 461             // message - otherwise it would close us 
 462             processed 
= !Close(); 
 466 #if wxUSE_DIALOG_SIZEGRIP 
 479 #endif // wxUSE_DIALOG_SIZEGRIP 
 481             // the Windows dialogs unfortunately are not meant to be resizeable 
 482             // at all and their standard class doesn't include CS_[VH]REDRAW 
 483             // styles which means that the window is not refreshed properly 
 484             // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can 
 485             // help with it - so we have to refresh it manually which certainly 
 486             // creates flicker but at least doesn't show garbage on the screen 
 487             rc 
= wxWindow::MSWWindowProc(message
, wParam
, lParam
); 
 489             if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) ) 
 491                 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */); 
 495 #ifndef __WXMICROWIN__ 
 497             // we want to override the busy cursor for modal dialogs: 
 498             // typically, wxBeginBusyCursor() is called and then a modal dialog 
 499             // is shown, but the modal dialog shouldn't have hourglass cursor 
 500             if ( IsModal() && wxIsBusy() ) 
 502                 // set our cursor for all windows (but see below) 
 503                 wxCursor cursor 
= m_cursor
; 
 505                     cursor 
= wxCURSOR_ARROW
; 
 507                 ::SetCursor(GetHcursorOf(cursor
)); 
 509                 // in any case, stop here and don't let wxWindow process this 
 510                 // message (it would set the busy cursor) 
 513                 // but return false to tell the child window (if the event 
 514                 // comes from one of them and not from ourselves) that it can 
 515                 // set its own cursor if it has one: thus, standard controls 
 516                 // (e.g. text ctrl) still have correct cursors in a dialog 
 517                 // invoked while wxIsBusy() 
 521 #endif // __WXMICROWIN__ 
 525         rc 
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);