1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: David Webster
8 // Copyright: (c) David Webster
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 #define wxDIALOG_DEFAULT_X 300
29 #define wxDIALOG_DEFAULT_Y 300
31 // Lists to keep track of windows, so we can disable/enable them
33 wxWindowList wxModalDialogs
;
34 wxWindowList wxModelessWindows
; // Frames and modeless dialogs
35 extern wxList WXDLLEXPORT wxPendingDelete
;
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
40 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
41 EVT_SIZE(wxDialog::OnSize
)
42 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
43 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
44 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
45 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
46 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
47 EVT_CLOSE(wxDialog::OnCloseWindow
)
54 m_modalShowing
= FALSE
;
56 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
59 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
60 const wxString
& title
,
70 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
74 wxTopLevelWindows
.Append(this);
76 if (parent
) parent
->AddChild(this);
79 m_windowId
= (int)NewControlId();
88 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
89 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
91 m_windowStyle
= style
;
94 m_modalShowing
= FALSE
;
101 // TODO: convert below to OS/2 PM code
103 // All dialogs should really have this style
104 // m_windowStyle |= wxTAB_TRAVERSAL;
106 // WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
107 // if (m_windowStyle & wxSTAY_ON_TOP)
108 // extendedStyle |= WS_EX_TOPMOST;
110 // Allows creation of dialogs with & without captions under MSWindows,
111 // resizeable or not (but a resizeable dialog always has caption -
112 // otherwise it would look too strange)
113 // const wxChar *dlg;
114 // if ( style & wxRESIZE_BORDER )
115 // dlg = wxT("wxResizeableDialog");
116 // else if ( style & wxCAPTION )
117 // dlg = wxT("wxCaptionDialog");
119 // dlg = wxT("wxNoCaptionDialog");
120 // MSWCreate(m_windowId, parent, NULL, this, NULL,
121 // x, y, width, height,
122 // 0, // style is not used if we have dlg template
126 // HWND hwnd = (HWND)GetHWND();
130 // wxLogError(wxT("Failed to create dialog."));
135 // SubclassWin(GetHWND());
137 // SetWindowText(hwnd, title);
138 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
143 void wxDialog::SetModal(bool flag
)
146 m_windowStyle
|= wxDIALOG_MODAL
;
147 else if ( m_windowStyle
& wxDIALOG_MODAL
)
148 m_windowStyle
-= wxDIALOG_MODAL
;
150 wxModelessWindows
.DeleteObject(this);
152 wxModelessWindows
.Append(this);
155 wxDialog::~wxDialog()
157 m_isBeingDeleted
= TRUE
;
159 wxTopLevelWindows
.DeleteObject(this);
165 if (GetParent() && GetParent()->GetHWND())
166 // TODO: bring the parent to the top
170 m_modalShowing
= FALSE
;
171 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
172 wxModelessWindows
.DeleteObject(this);
175 // If this is the last top-level window, exit.
176 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
178 wxTheApp
->SetTopWindow(NULL
);
180 if (wxTheApp
->GetExitOnFrameDelete())
187 // By default, pressing escape cancels the dialog
188 void wxDialog::OnCharHook(wxKeyEvent
& event
)
192 if (event
.m_keyCode
== WXK_ESCAPE
)
194 // Behaviour changed in 2.0: we'll send a Cancel message
195 // to the dialog instead of Close.
196 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
197 cancelEvent
.SetEventObject( this );
198 GetEventHandler()->ProcessEvent(cancelEvent
);
203 // We didn't process this event.
207 void wxDialog::OnPaint(wxPaintEvent
& event
)
209 // No: if you call the default procedure, it makes
210 // the following painting code not work.
211 // wxWindow::OnPaint(event);
219 void wxDialog::Iconize(bool WXUNUSED(iconize
))
221 // Windows dialog boxes can't be iconized
224 bool wxDialog::IsIconized() const
229 void wxDialog::DoSetClientSize(int width
, int height
)
231 // TODO: Convert the below to OS/2 PM code
233 // HWND hWnd = (HWND) GetHWND();
235 // ::GetClientRect(hWnd, &rect);
238 // GetWindowRect(hWnd, &rect2);
240 // Find the difference between the entire window (title bar and all)
241 // and the client area; add this to the new client size to move the
243 // int actual_width = rect2.right - rect2.left - rect.right + width;
244 // int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
246 // MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
248 // wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
249 // event.SetEventObject( this );
250 // GetEventHandler()->ProcessEvent(event);
252 void wxDialog::GetPosition(int *x
, int *y
) const
255 // HWND hWnd = (HWND) GetHWND();
257 // GetWindowRect(hWnd, &rect);
263 bool wxDialog::IsShown() const
268 bool wxDialog::IsModal() const
270 return wxModalDialogs
.Find((wxDialog
*)this) != 0; // const_cast
273 bool wxDialog::Show(bool show
)
275 // TODO: This is involved code, look at msw port for details
279 void wxDialog::SetTitle(const wxString
& title
)
281 ::WinSetWindowText((HWND
) GetHWND(), title
.c_str());
284 wxString
wxDialog::GetTitle() const
286 ::WinQueryWindowText((HWND
) GetHWND(), 1000, wxBuffer
);
287 return wxString(wxBuffer
);
290 void wxDialog::Centre(int direction
)
292 int x_offset
,y_offset
;
293 int display_width
, display_height
;
294 int width
, height
, x
, y
;
295 wxWindow
*parent
= GetParent();
296 if ((direction
& wxCENTER_FRAME
) && parent
)
298 parent
->GetPosition(&x_offset
,&y_offset
) ;
299 parent
->GetSize(&display_width
,&display_height
) ;
303 wxDisplaySize(&display_width
, &display_height
);
308 GetSize(&width
, &height
);
311 if (direction
& wxHORIZONTAL
)
312 x
= (int)((display_width
- width
)/2);
313 if (direction
& wxVERTICAL
)
314 y
= (int)((display_height
- height
)/2);
316 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
319 // Replacement for Show(TRUE) for modal dialogs - returns return code
320 int wxDialog::ShowModal()
322 m_windowStyle
|= wxDIALOG_MODAL
;
324 return GetReturnCode();
327 void wxDialog::EndModal(int retCode
)
329 SetReturnCode(retCode
);
330 // TODO modal un-showing
334 // Define for each class of dialog and control
335 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
336 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
341 void wxDialog::OnOK(wxCommandEvent
& event
)
343 if ( Validate() && TransferDataFromWindow() )
349 SetReturnCode(wxID_OK
);
355 void wxDialog::OnApply(wxCommandEvent
& event
)
358 TransferDataFromWindow();
359 // TODO probably need to disable the Apply button until things change again
362 void wxDialog::OnCancel(wxCommandEvent
& event
)
365 EndModal(wxID_CANCEL
);
368 SetReturnCode(wxID_CANCEL
);
373 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
375 // We'll send a Cancel message by default,
376 // which may close the dialog.
377 // Check for looping if the Cancel event handler calls Close().
379 // Note that if a cancel button and handler aren't present in the dialog,
380 // nothing will happen when you close the dialog via the window manager, or
382 // We wouldn't want to destroy the dialog by default, since the dialog may have been
383 // created on the stack.
384 // However, this does mean that calling dialog->Close() won't delete the dialog
385 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
386 // sure to destroy the dialog.
387 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
389 static wxList closing
;
391 if ( closing
.Member(this) )
394 closing
.Append(this);
396 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
397 cancelEvent
.SetEventObject( this );
398 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
400 closing
.DeleteObject(this);
403 // Destroy the window (delayed, if a managed window)
404 bool wxDialog::Destroy()
406 if (!wxPendingDelete
.Member(this))
407 wxPendingDelete
.Append(this);
411 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
413 // if we're using constraints - do use them
414 #if wxUSE_CONSTRAINTS
415 if ( GetAutoLayout() )
422 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
428 MRESULT
wxDialog::OS2WindowProc(HWND hwnd
, WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
431 bool processed
= FALSE
;
436 // if we can't close, tell the system that we processed the
437 // message - otherwise it would close us
438 processed
= !Close();
443 rc
= wxWindow::OS2WindowProc(hwnd
, message
, wParam
, lParam
);