1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxDialog class 
   8 // Copyright:   (c) AUTHOR 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  16 #include "wx/dialog.h" 
  20 #include "wx/settings.h" 
  25 #include "wx/os2/private.h" 
  28 #if wxUSE_COMMON_DIALOGS 
  32 #define wxDIALOG_DEFAULT_X 300 
  33 #define wxDIALOG_DEFAULT_Y 300 
  35 // Lists to keep track of windows, so we can disable/enable them 
  37 wxWindowList wxModalDialogs
; 
  38 wxWindowList wxModelessWindows
;  // Frames and modeless dialogs 
  39 extern wxList WXDLLEXPORT wxPendingDelete
; 
  41 #if !USE_SHARED_LIBRARY 
  42    IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
) 
  44    BEGIN_EVENT_TABLE(wxDialog
, wxPanel
) 
  45       EVT_SIZE(wxDialog::OnSize
) 
  46       EVT_BUTTON(wxID_OK
, wxDialog::OnOK
) 
  47       EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
) 
  48       EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
) 
  49       EVT_CHAR_HOOK(wxDialog::OnCharHook
) 
  50       EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
) 
  51       EVT_CLOSE(wxDialog::OnCloseWindow
) 
  58     m_modalShowing 
= FALSE
; 
  60     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
)); 
  63 bool wxDialog::Create(wxWindow 
*parent
, wxWindowID id
, 
  64            const wxString
& title
, 
  74     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
)); 
  78         wxTopLevelWindows
.Append(this); 
  80     if (parent
) parent
->AddChild(this); 
  83         m_windowId 
= (int)NewControlId(); 
  92     if (x 
< 0) x 
= wxDIALOG_DEFAULT_X
; 
  93     if (y 
< 0) y 
= wxDIALOG_DEFAULT_Y
; 
  95     m_windowStyle 
= style
; 
  98     m_modalShowing 
= FALSE
; 
 105     // TODO: convert below to OS/2 PM code 
 107     // All dialogs should really have this style 
 108 //    m_windowStyle |= wxTAB_TRAVERSAL; 
 110 //    WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle); 
 111 //    if (m_windowStyle & wxSTAY_ON_TOP) 
 112 //        extendedStyle |= WS_EX_TOPMOST; 
 114     // Allows creation of dialogs with & without captions under MSWindows, 
 115     // resizeable or not (but a resizeable dialog always has caption - 
 116     // otherwise it would look too strange) 
 117 //    const wxChar *dlg; 
 118 //    if ( style & wxRESIZE_BORDER ) 
 119 //        dlg = wxT("wxResizeableDialog"); 
 120 //    else if ( style & wxCAPTION ) 
 121 //        dlg = wxT("wxCaptionDialog"); 
 123 //        dlg = wxT("wxNoCaptionDialog"); 
 124 //    MSWCreate(m_windowId, parent, NULL, this, NULL, 
 125 //              x, y, width, height, 
 126 //              0, // style is not used if we have dlg template 
 130 //    HWND hwnd = (HWND)GetHWND(); 
 134 //        wxLogError(wxT("Failed to create dialog.")); 
 139 //    SubclassWin(GetHWND()); 
 141 //    SetWindowText(hwnd, title); 
 142 //    SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT)); 
 147 void wxDialog::SetModal(bool flag
) 
 150         m_windowStyle 
|= wxDIALOG_MODAL 
; 
 151     else if ( m_windowStyle 
& wxDIALOG_MODAL 
) 
 152         m_windowStyle 
-= wxDIALOG_MODAL 
; 
 154       wxModelessWindows
.DeleteObject(this); 
 156           wxModelessWindows
.Append(this); 
 159 wxDialog::~wxDialog() 
 161     m_isBeingDeleted 
= TRUE
; 
 163     wxTopLevelWindows
.DeleteObject(this); 
 169         if (GetParent() && GetParent()->GetHWND()) 
 170            // TODO: bring the parent to the top 
 174     m_modalShowing 
= FALSE
; 
 175     if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL 
) 
 176         wxModelessWindows
.DeleteObject(this); 
 179     // If this is the last top-level window, exit. 
 180     if (wxTheApp 
&& (wxTopLevelWindows
.Number() == 0)) 
 182       wxTheApp
->SetTopWindow(NULL
); 
 184       if (wxTheApp
->GetExitOnFrameDelete()) 
 191 // By default, pressing escape cancels the dialog 
 192 void wxDialog::OnCharHook(wxKeyEvent
& event
) 
 196         if (event
.m_keyCode 
== WXK_ESCAPE
) 
 198             // Behaviour changed in 2.0: we'll send a Cancel message 
 199             // to the dialog instead of Close. 
 200             wxCommandEvent 
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
); 
 201             cancelEvent
.SetEventObject( this ); 
 202             GetEventHandler()->ProcessEvent(cancelEvent
); 
 207     // We didn't process this event. 
 211 void wxDialog::OnPaint(wxPaintEvent
& event
) 
 213   // No: if you call the default procedure, it makes 
 214   // the following painting code not work. 
 215 //  wxWindow::OnPaint(event); 
 223 void wxDialog::Iconize(bool WXUNUSED(iconize
)) 
 225     // Windows dialog boxes can't be iconized 
 228 bool wxDialog::IsIconized() const 
 233 void wxDialog::DoSetClientSize(int width
, int height
) 
 235     // TODO: Convert the below to OS/2 PM code 
 237 //    HWND hWnd = (HWND) GetHWND(); 
 239 //    ::GetClientRect(hWnd, &rect); 
 242 //    GetWindowRect(hWnd, &rect2); 
 244     // Find the difference between the entire window (title bar and all) 
 245     // and the client area; add this to the new client size to move the 
 247 //    int actual_width = rect2.right - rect2.left - rect.right + width; 
 248 //    int actual_height = rect2.bottom - rect2.top - rect.bottom + height; 
 250 //    MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE); 
 252 //    wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId); 
 253 //    event.SetEventObject( this ); 
 254 //    GetEventHandler()->ProcessEvent(event); 
 256 void wxDialog::GetPosition(int *x
, int *y
) const 
 259 //  HWND hWnd = (HWND) GetHWND(); 
 261 //  GetWindowRect(hWnd, &rect); 
 267 bool wxDialog::IsShown() const 
 272 bool wxDialog::IsModal() const 
 274     return wxModalDialogs
.Find((wxDialog 
*)this) != 0; // const_cast 
 277 bool wxDialog::Show(bool show
) 
 279     // TODO: This is involved code, look at msw port for details 
 283 void wxDialog::SetTitle(const wxString
& title
) 
 285     ::WinSetWindowText((HWND
) GetHWND(), title
.c_str()); 
 288 wxString 
wxDialog::GetTitle() const 
 290     ::WinQueryWindowText((HWND
) GetHWND(), 1000, wxBuffer
); 
 291     return wxString(wxBuffer
); 
 294 void wxDialog::Centre(int direction
) 
 296   int x_offset
,y_offset 
; 
 297   int display_width
, display_height
; 
 298   int  width
, height
, x
, y
; 
 299   wxWindow 
*parent 
= GetParent(); 
 300   if ((direction 
& wxCENTER_FRAME
) && parent
) 
 302       parent
->GetPosition(&x_offset
,&y_offset
) ; 
 303       parent
->GetSize(&display_width
,&display_height
) ; 
 307     wxDisplaySize(&display_width
, &display_height
); 
 312   GetSize(&width
, &height
); 
 315   if (direction 
& wxHORIZONTAL
) 
 316     x 
= (int)((display_width 
- width
)/2); 
 317   if (direction 
& wxVERTICAL
) 
 318     y 
= (int)((display_height 
- height
)/2); 
 320   SetSize(x
+x_offset
, y
+y_offset
, width
, height
); 
 323 // Replacement for Show(TRUE) for modal dialogs - returns return code 
 324 int wxDialog::ShowModal() 
 326     m_windowStyle 
|= wxDIALOG_MODAL
; 
 328     return GetReturnCode(); 
 331 void wxDialog::EndModal(int retCode
) 
 333     SetReturnCode(retCode
); 
 334     // TODO modal un-showing 
 338 // Define for each class of dialog and control 
 339 WXHBRUSH 
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
, 
 340       WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
) 
 345 void wxDialog::OnOK(wxCommandEvent
& event
) 
 347     if ( Validate() && TransferDataFromWindow() ) 
 353             SetReturnCode(wxID_OK
); 
 359 void wxDialog::OnApply(wxCommandEvent
& event
) 
 362         TransferDataFromWindow(); 
 363     // TODO probably need to disable the Apply button until things change again 
 366 void wxDialog::OnCancel(wxCommandEvent
& event
) 
 369         EndModal(wxID_CANCEL
); 
 372         SetReturnCode(wxID_CANCEL
); 
 377 void wxDialog::OnCloseWindow(wxCloseEvent
& event
) 
 379     // We'll send a Cancel message by default, 
 380     // which may close the dialog. 
 381     // Check for looping if the Cancel event handler calls Close(). 
 383     // Note that if a cancel button and handler aren't present in the dialog, 
 384     // nothing will happen when you close the dialog via the window manager, or 
 386     // We wouldn't want to destroy the dialog by default, since the dialog may have been 
 387     // created on the stack. 
 388     // However, this does mean that calling dialog->Close() won't delete the dialog 
 389     // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be 
 390     // sure to destroy the dialog. 
 391     // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. 
 393     static wxList closing
; 
 395     if ( closing
.Member(this) ) 
 398     closing
.Append(this); 
 400     wxCommandEvent 
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
); 
 401     cancelEvent
.SetEventObject( this ); 
 402     GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog 
 404     closing
.DeleteObject(this); 
 407 // Destroy the window (delayed, if a managed window) 
 408 bool wxDialog::Destroy() 
 410     if (!wxPendingDelete
.Member(this)) 
 411         wxPendingDelete
.Append(this); 
 415 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
)) 
 417   // if we're using constraints - do use them 
 418   #if wxUSE_CONSTRAINTS 
 419     if ( GetAutoLayout() ) 
 426 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
) 
 428   SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
)); 
 432 MRESULT 
wxDialog::OS2WindowProc(HWND hwnd
, WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
) 
 435     bool    processed 
= FALSE
; 
 440             // if we can't close, tell the system that we processed the 
 441             // message - otherwise it would close us 
 442             processed 
= !Close(); 
 447         rc 
= wxWindow::OS2WindowProc(hwnd
, message
, wParam
, lParam
);