1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxDialog class 
   8 // Copyright:   (c) AUTHOR 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation "dialog.h" 
  16 #include "wx/dialog.h" 
  20 #include "wx/settings.h" 
  22 // Lists to keep track of windows, so we can disable/enable them 
  24 wxList wxModalDialogs
; 
  25 wxList wxModelessWindows
;  // Frames and modeless dialogs 
  26 extern wxList wxPendingDelete
; 
  28 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
) 
  30 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
) 
  31   EVT_BUTTON(wxID_OK
, wxDialog::OnOK
) 
  32   EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
) 
  33   EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
) 
  34   EVT_CHAR_HOOK(wxDialog::OnCharHook
) 
  35   EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
) 
  36   EVT_CLOSE(wxDialog::OnCloseWindow
) 
  42     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
)); 
  45 bool wxDialog::Create(wxWindow 
*parent
, wxWindowID id
, 
  46            const wxString
& title
, 
  52   m_windowStyle 
= style
; 
  54   SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
)); 
  58     wxTopLevelWindows
.Append(this); 
  60   if (parent
) parent
->AddChild(this); 
  63         m_windowId 
= (int)NewControlId(); 
  67   // TODO: create dialog 
  72 void wxDialog::SetModal(bool flag
) 
  75                 m_windowStyle 
|= wxDIALOG_MODAL 
; 
  77                 if ( m_windowStyle 
& wxDIALOG_MODAL 
) 
  78                         m_windowStyle 
-= wxDIALOG_MODAL 
; 
  80   wxModelessWindows
.DeleteObject(this); 
  82     wxModelessWindows
.Append(this); 
  88     wxTopLevelWindows
.DeleteObject(this); 
  90     if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL 
) 
  91       wxModelessWindows
.DeleteObject(this); 
  93     // If this is the last top-level window, exit. 
  94     if (wxTheApp 
&& (wxTopLevelWindows
.Number() == 0)) 
  96       wxTheApp
->SetTopWindow(NULL
); 
  98       if (wxTheApp
->GetExitOnFrameDelete()) 
 105 // By default, pressing escape cancels the dialog 
 106 void wxDialog::OnCharHook(wxKeyEvent
& event
) 
 110     if (event
.m_keyCode 
== WXK_ESCAPE
) 
 112                 // Behaviour changed in 2.0: we'll send a Cancel message 
 113                 // to the dialog instead of Close. 
 114                 wxCommandEvent 
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
); 
 115                 cancelEvent
.SetEventObject( this ); 
 116                 GetEventHandler()->ProcessEvent(cancelEvent
); 
 121   // We didn't process this event. 
 125 void wxDialog::Iconize(bool WXUNUSED(iconize
)) 
 130 bool wxDialog::IsIconized() const 
 136 void wxDialog::SetClientSize(int width
, int height
) 
 141 void wxDialog::GetPosition(int *x
, int *y
) const 
 146 bool wxDialog::Show(bool show
) 
 152 void wxDialog::SetTitle(const wxString
& title
) 
 157 wxString 
wxDialog::GetTitle() const 
 163 void wxDialog::Centre(int direction
) 
 165   int x_offset
,y_offset 
; 
 166   int display_width
, display_height
; 
 167   int  width
, height
, x
, y
; 
 169   if (direction 
& wxCENTER_FRAME
) 
 171     frame 
= (wxFrame
*)GetParent() ; 
 174       frame
->GetPosition(&x_offset
,&y_offset
) ; 
 175       frame
->GetSize(&display_width
,&display_height
) ; 
 183     wxDisplaySize(&display_width
, &display_height
); 
 188   GetSize(&width
, &height
); 
 191   if (direction 
& wxHORIZONTAL
) 
 192     x 
= (int)((display_width 
- width
)/2); 
 193   if (direction 
& wxVERTICAL
) 
 194     y 
= (int)((display_height 
- height
)/2); 
 196   SetSize(x
+x_offset
, y
+y_offset
, width
, height
); 
 199 // Replacement for Show(TRUE) for modal dialogs - returns return code 
 200 int wxDialog::ShowModal() 
 202     m_windowStyle 
|= wxDIALOG_MODAL
; 
 203     // TODO: modal showing 
 205         return GetReturnCode(); 
 208 void wxDialog::EndModal(int retCode
) 
 210         SetReturnCode(retCode
); 
 211     // TODO modal un-showing 
 216 void wxDialog::OnOK(wxCommandEvent
& event
) 
 218         if ( Validate() && TransferDataFromWindow() ) 
 224                     SetReturnCode(wxID_OK
); 
 230 void wxDialog::OnApply(wxCommandEvent
& event
) 
 233                 TransferDataFromWindow(); 
 234         // TODO probably need to disable the Apply button until things change again 
 237 void wxDialog::OnCancel(wxCommandEvent
& event
) 
 240         EndModal(wxID_CANCEL
); 
 243         SetReturnCode(wxID_CANCEL
); 
 248 void wxDialog::OnCloseWindow(wxCloseEvent
& event
) 
 250     // We'll send a Cancel message by default, 
 251     // which may close the dialog. 
 252     // Check for looping if the Cancel event handler calls Close(). 
 254     // Note that if a cancel button and handler aren't present in the dialog, 
 255     // nothing will happen when you close the dialog via the window manager, or 
 257     // We wouldn't want to destroy the dialog by default, since the dialog may have been 
 258     // created on the stack. 
 259     // However, this does mean that calling dialog->Close() won't delete the dialog 
 260     // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be 
 261     // sure to destroy the dialog. 
 262     // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. 
 264     static wxList closing
; 
 266     if ( closing
.Member(this) ) 
 269     closing
.Append(this); 
 271     wxCommandEvent 
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
); 
 272     cancelEvent
.SetEventObject( this ); 
 273     GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog 
 275     closing
.DeleteObject(this); 
 278 // Destroy the window (delayed, if a managed window) 
 279 bool wxDialog::Destroy() 
 281   if (!wxPendingDelete
.Member(this)) 
 282     wxPendingDelete
.Append(this); 
 286 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
) 
 288   SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));