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 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
39 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
40 EVT_SIZE(wxDialog::OnSize
)
41 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
42 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
43 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
44 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
45 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
46 EVT_CLOSE(wxDialog::OnCloseWindow
)
52 m_modalShowing
= FALSE
;
54 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
57 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
58 const wxString
& title
,
68 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
72 wxTopLevelWindows
.Append(this);
74 if (parent
) parent
->AddChild(this);
77 m_windowId
= (int)NewControlId();
86 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
87 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
89 m_windowStyle
= style
;
92 m_modalShowing
= FALSE
;
99 // TODO: convert below to OS/2 PM code
101 // All dialogs should really have this style
102 // m_windowStyle |= wxTAB_TRAVERSAL;
104 // WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
105 // if (m_windowStyle & wxSTAY_ON_TOP)
106 // extendedStyle |= WS_EX_TOPMOST;
108 // Allows creation of dialogs with & without captions under MSWindows,
109 // resizeable or not (but a resizeable dialog always has caption -
110 // otherwise it would look too strange)
111 // const wxChar *dlg;
112 // if ( style & wxRESIZE_BORDER )
113 // dlg = wxT("wxResizeableDialog");
114 // else if ( style & wxCAPTION )
115 // dlg = wxT("wxCaptionDialog");
117 // dlg = wxT("wxNoCaptionDialog");
118 // MSWCreate(m_windowId, parent, NULL, this, NULL,
119 // x, y, width, height,
120 // 0, // style is not used if we have dlg template
124 // HWND hwnd = (HWND)GetHWND();
128 // wxLogError(wxT("Failed to create dialog."));
133 // SubclassWin(GetHWND());
135 // SetWindowText(hwnd, title);
136 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
141 void wxDialog::SetModal(bool flag
)
144 m_windowStyle
|= wxDIALOG_MODAL
;
145 else if ( m_windowStyle
& wxDIALOG_MODAL
)
146 m_windowStyle
-= wxDIALOG_MODAL
;
148 wxModelessWindows
.DeleteObject(this);
150 wxModelessWindows
.Append(this);
153 wxDialog::~wxDialog()
155 m_isBeingDeleted
= TRUE
;
157 wxTopLevelWindows
.DeleteObject(this);
163 if (GetParent() && GetParent()->GetHWND())
164 // TODO: bring the parent to the top
168 m_modalShowing
= FALSE
;
169 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
170 wxModelessWindows
.DeleteObject(this);
173 // If this is the last top-level window, exit.
174 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
176 wxTheApp
->SetTopWindow(NULL
);
178 if (wxTheApp
->GetExitOnFrameDelete())
185 // By default, pressing escape cancels the dialog
186 void wxDialog::OnCharHook(wxKeyEvent
& event
)
190 if (event
.m_keyCode
== WXK_ESCAPE
)
192 // Behaviour changed in 2.0: we'll send a Cancel message
193 // to the dialog instead of Close.
194 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
195 cancelEvent
.SetEventObject( this );
196 GetEventHandler()->ProcessEvent(cancelEvent
);
201 // We didn't process this event.
205 void wxDialog::OnPaint(wxPaintEvent
& event
)
207 // No: if you call the default procedure, it makes
208 // the following painting code not work.
209 // wxWindow::OnPaint(event);
217 void wxDialog::Iconize(bool WXUNUSED(iconize
))
219 // Windows dialog boxes can't be iconized
222 bool wxDialog::IsIconized() const
227 void wxDialog::DoSetClientSize(int width
, int height
)
229 // TODO: Convert the below to OS/2 PM code
231 // HWND hWnd = (HWND) GetHWND();
233 // ::GetClientRect(hWnd, &rect);
236 // GetWindowRect(hWnd, &rect2);
238 // Find the difference between the entire window (title bar and all)
239 // and the client area; add this to the new client size to move the
241 // int actual_width = rect2.right - rect2.left - rect.right + width;
242 // int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
244 // MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
246 // wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
247 // event.SetEventObject( this );
248 // GetEventHandler()->ProcessEvent(event);
250 void wxDialog::GetPosition(int *x
, int *y
) const
253 // HWND hWnd = (HWND) GetHWND();
255 // GetWindowRect(hWnd, &rect);
261 bool wxDialog::IsShown() const
266 bool wxDialog::IsModal() const
268 return wxModalDialogs
.Find((wxDialog
*)this) != 0; // const_cast
271 bool wxDialog::Show(bool show
)
273 // TODO: This is involved code, look at msw port for details
277 void wxDialog::SetTitle(const wxString
& title
)
279 ::WinSetWindowText((HWND
) GetHWND(), title
.c_str());
282 wxString
wxDialog::GetTitle() const
284 ::WinQueryWindowText((HWND
) GetHWND(), 1000, wxBuffer
);
285 return wxString(wxBuffer
);
288 void wxDialog::Centre(int direction
)
290 int x_offset
,y_offset
;
291 int display_width
, display_height
;
292 int width
, height
, x
, y
;
293 wxWindow
*parent
= GetParent();
294 if ((direction
& wxCENTER_FRAME
) && parent
)
296 parent
->GetPosition(&x_offset
,&y_offset
) ;
297 parent
->GetSize(&display_width
,&display_height
) ;
301 wxDisplaySize(&display_width
, &display_height
);
306 GetSize(&width
, &height
);
309 if (direction
& wxHORIZONTAL
)
310 x
= (int)((display_width
- width
)/2);
311 if (direction
& wxVERTICAL
)
312 y
= (int)((display_height
- height
)/2);
314 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
317 // Replacement for Show(TRUE) for modal dialogs - returns return code
318 int wxDialog::ShowModal()
320 m_windowStyle
|= wxDIALOG_MODAL
;
322 return GetReturnCode();
325 void wxDialog::EndModal(int retCode
)
327 SetReturnCode(retCode
);
328 // TODO modal un-showing
332 // Define for each class of dialog and control
333 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
334 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
339 void wxDialog::OnOK(wxCommandEvent
& event
)
341 if ( Validate() && TransferDataFromWindow() )
347 SetReturnCode(wxID_OK
);
353 void wxDialog::OnApply(wxCommandEvent
& event
)
356 TransferDataFromWindow();
357 // TODO probably need to disable the Apply button until things change again
360 void wxDialog::OnCancel(wxCommandEvent
& event
)
363 EndModal(wxID_CANCEL
);
366 SetReturnCode(wxID_CANCEL
);
371 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
373 // We'll send a Cancel message by default,
374 // which may close the dialog.
375 // Check for looping if the Cancel event handler calls Close().
377 // Note that if a cancel button and handler aren't present in the dialog,
378 // nothing will happen when you close the dialog via the window manager, or
380 // We wouldn't want to destroy the dialog by default, since the dialog may have been
381 // created on the stack.
382 // However, this does mean that calling dialog->Close() won't delete the dialog
383 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
384 // sure to destroy the dialog.
385 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
387 static wxList closing
;
389 if ( closing
.Member(this) )
392 closing
.Append(this);
394 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
395 cancelEvent
.SetEventObject( this );
396 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
398 closing
.DeleteObject(this);
401 // Destroy the window (delayed, if a managed window)
402 bool wxDialog::Destroy()
404 if (!wxPendingDelete
.Member(this))
405 wxPendingDelete
.Append(this);
409 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
411 // if we're using constraints - do use them
412 #if wxUSE_CONSTRAINTS
413 if ( GetAutoLayout() )
420 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
422 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
426 MRESULT
wxDialog::OS2WindowProc(HWND hwnd
, WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
429 bool processed
= FALSE
;
434 // if we can't close, tell the system that we processed the
435 // message - otherwise it would close us
436 processed
= !Close();
441 rc
= wxWindow::OS2WindowProc(hwnd
, message
, wParam
, lParam
);