]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dialog.cpp
Don't crash when laying out wxGridBagSizer with only hidden elements.
[wxWidgets.git] / src / os2 / dialog.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
743e24aa 2// Name: src/os2/dialog.cpp
0e320a79 3// Purpose: wxDialog class
fb46a9a6 4// Author: David Webster
0e320a79 5// Modified by:
fb46a9a6 6// Created: 10/14/99
fb46a9a6 7// Copyright: (c) David Webster
65571936 8// Licence: wxWindows licence
0e320a79
DW
9/////////////////////////////////////////////////////////////////////////////
10
27476f73
DW
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
0e320a79
DW
13
14#include "wx/dialog.h"
e4db172a
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/utils.h"
18 #include "wx/frame.h"
19 #include "wx/app.h"
20 #include "wx/settings.h"
21 #include "wx/intl.h"
22 #include "wx/log.h"
27476f73
DW
23#endif
24
25#include "wx/os2/private.h"
598dc9b7 26#include "wx/evtloop.h"
664e1314 27#include "wx/scopedptr.h"
691745ab 28#include "wx/modalhook.h"
27476f73 29
27476f73
DW
30#define wxDIALOG_DEFAULT_X 300
31#define wxDIALOG_DEFAULT_Y 300
0e320a79 32
c9cb56f7
DW
33#define wxDIALOG_DEFAULT_WIDTH 500
34#define wxDIALOG_DEFAULT_HEIGHT 500
35
27476f73 36
598dc9b7
SN
37// ----------------------------------------------------------------------------
38// wxDialogModalData
39// ----------------------------------------------------------------------------
40
41// this is simply a container for any data we need to implement modality which
42// allows us to avoid changing wxDialog each time the implementation changes
43class wxDialogModalData
44{
45public:
46 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
47
48 void RunLoop()
49 {
50 m_evtLoop.Run();
51 }
52
53 void ExitLoop()
54 {
55 m_evtLoop.Exit();
56 }
57
58private:
59 wxModalEventLoop m_evtLoop;
60};
61
62wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData);
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxDialog construction
70// ----------------------------------------------------------------------------
71
c9cb56f7 72void wxDialog::Init()
0e320a79 73{
d3b9f782 74 m_pOldFocus = NULL;
743e24aa 75 m_isShown = false;
d3b9f782 76 m_pWindowDisabler = NULL;
ee22d7f3 77 m_modalData = NULL;
a756f210 78 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c9cb56f7
DW
79} // end of wxDialog::Init
80
6670f564
WS
81bool wxDialog::Create( wxWindow* pParent,
82 wxWindowID vId,
83 const wxString& rsTitle,
84 const wxPoint& rPos,
85 const wxSize& rSize,
86 long lStyle,
87 const wxString& rsName )
0e320a79 88{
c9cb56f7 89 Init();
5d44b24e 90 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
c9cb56f7
DW
91
92 //
5d44b24e 93 // Save focus before doing anything which can potentially change it
c9cb56f7 94 //
5d44b24e 95 m_pOldFocus = FindFocus();
c9cb56f7
DW
96
97 //
5d44b24e 98 // All dialogs should really have this style
c9cb56f7 99 //
5d44b24e
DW
100 lStyle |= wxTAB_TRAVERSAL;
101
102 if (!wxTopLevelWindow::Create( pParent
103 ,vId
104 ,rsTitle
105 ,rPos
106 ,rSize
107 ,lStyle
108 ,rsName
109 ))
6670f564
WS
110 return false;
111
a756f210 112 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
c9b9e020
DW
113
114 //
115 // Must defer setting the title until after dialog is created and sized
116 //
6636ef8d 117 if ( !rsTitle.empty() )
c9b9e020 118 SetTitle(rsTitle);
6670f564 119 return true;
c9cb56f7 120} // end of wxDialog::Create
0e320a79 121
89831b54
WS
122#if WXWIN_COMPATIBILITY_2_6
123
86dc2301
SN
124// deprecated ctor
125wxDialog::wxDialog(wxWindow *parent,
126 const wxString& title,
127 bool WXUNUSED(modal),
128 int x,
129 int y,
130 int w,
131 int h,
132 long style,
133 const wxString& name)
134{
135 Init();
136
137 Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name);
138}
139
89831b54 140void wxDialog::SetModal(bool WXUNUSED(bFlag))
0e320a79 141{
86dc2301 142 // nothing to do, obsolete method
c9cb56f7 143} // end of wxDialog::SetModal
0e320a79 144
89831b54
WS
145#endif // WXWIN_COMPATIBILITY_2_6
146
0e320a79
DW
147wxDialog::~wxDialog()
148{
c6212a0c 149 SendDestroyEvent();
86dc2301
SN
150
151 // this will also reenable all the other windows for a modal dialog
152 Show(false);
c9cb56f7 153} // end of wxDialog::~wxDialog
0e320a79 154
5d44b24e
DW
155// ----------------------------------------------------------------------------
156// showing the dialogs
157// ----------------------------------------------------------------------------
27476f73 158
89831b54
WS
159#if WXWIN_COMPATIBILITY_2_6
160
c9cb56f7 161bool wxDialog::IsModalShowing() const
0e320a79 162{
86dc2301 163 return IsModal();
c9cb56f7 164} // end of wxDialog::IsModalShowing
0e320a79 165
89831b54 166#endif // WXWIN_COMPATIBILITY_2_6
0e320a79 167
743e24aa 168bool wxDialog::Show( bool bShow )
0e320a79 169{
86dc2301
SN
170 if ( bShow == IsShown() )
171 return false;
172
173 if (!bShow && m_modalData )
c9cb56f7 174 {
86dc2301
SN
175 // we need to do this before calling wxDialogBase version because if we
176 // had disabled other app windows, they must be reenabled right now as
c9cb56f7 177 // if they stay disabled Windows will activate another window (one
86dc2301
SN
178 // which is enabled, anyhow) when we're hidden in the base class Show()
179 // and we will lose activation
180 m_modalData->ExitLoop();
181#if 0
5276b0a5 182 wxDELETE(m_pWindowDisabler);
86dc2301 183#endif
c9cb56f7 184 }
0e320a79 185
86dc2301 186 if (bShow)
c9cb56f7 187 {
3aa8e4ea
JS
188 if (CanDoLayoutAdaptation())
189 DoLayoutAdaptation();
190
86dc2301
SN
191 // this usually will result in TransferDataToWindow() being called
192 // which will change the controls values so do it before showing as
193 // otherwise we could have some flicker
194 InitDialog();
c9cb56f7
DW
195 }
196
86dc2301
SN
197 wxDialogBase::Show(bShow);
198
743e24aa
WS
199 wxString title = GetTitle();
200 if (!title.empty())
65f3f920 201 ::WinSetWindowText((HWND)GetHwnd(), title.c_str());
86dc2301
SN
202
203 if ( bShow )
c9cb56f7 204 {
ee22d7f3 205 // dialogs don't get WM_SIZE message after creation unlike most (all?)
42782237 206 // other windows and so could start their life not laid out correctly
ee22d7f3
SN
207 // if we didn't call Layout() from here
208 //
209 // NB: normally we should call it just the first time but doing it
210 // every time is simpler than keeping a flag
86dc2301 211 Layout();
c9cb56f7
DW
212 }
213
86dc2301 214 return true;
c9cb56f7
DW
215} // end of wxDialog::Show
216
217//
6670f564 218// Replacement for Show(true) for modal dialogs - returns return code
c9cb56f7 219//
0e320a79
DW
220int wxDialog::ShowModal()
221{
691745ab 222 WX_HOOK_MODAL_DIALOG();
643e9cf9 223
9a83f860 224 wxASSERT_MSG( !IsModal(), wxT("wxDialog::ShowModal() reentered?") );
86dc2301
SN
225
226 m_endModalCalled = false;
227
228 Show();
229
230 // EndModal may have been called from InitDialog handler (called from
231 // inside Show()), which would cause an infinite loop if we didn't take it
232 // into account
233 if ( !m_endModalCalled )
c9cb56f7 234 {
86dc2301 235 // modal dialog needs a parent window, so try to find one
9fb0eb46 236 wxWindow * const parent = GetParentForModalDialog();
86dc2301
SN
237
238 // remember where the focus was
239 wxWindow *oldFocus = m_pOldFocus;
240 if ( !oldFocus )
241 {
242 // VZ: do we really want to do this?
243 oldFocus = parent;
244 }
245
246 // We have to remember the HWND because we need to check
247 // the HWND still exists (oldFocus can be garbage when the dialog
248 // exits, if it has been destroyed)
249 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
250
251
252 //
253 // Before entering the modal loop, reset the "is in OnIdle()" flag (see
254 // comment in app.cpp)
255 //
256 extern bool gbInOnIdle;
257 bool bWasInOnIdle = gbInOnIdle;
258
743e24aa 259 gbInOnIdle = false;
86dc2301
SN
260
261 // enter and run the modal loop
262 {
263 wxDialogModalDataTiedPtr modalData(&m_modalData,
264 new wxDialogModalData(this));
265 modalData->RunLoop();
266 }
267 gbInOnIdle = bWasInOnIdle;
268
269 // and restore focus
270 // Note that this code MUST NOT access the dialog object's data
271 // in case the object has been deleted (which will be the case
272 // for a modal dialog that has been destroyed before calling EndModal).
273 if ( oldFocus && (oldFocus != this) && ::WinIsWindow(vHabmain, hwndOldFocus))
274 {
275 // This is likely to prove that the object still exists
276 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
277 oldFocus->SetFocus();
278 }
c9cb56f7 279 }
86dc2301 280
27476f73 281 return GetReturnCode();
c9cb56f7 282} // end of wxDialog::ShowModal
0e320a79 283
c9cb56f7
DW
284void wxDialog::EndModal(
285 int nRetCode
286)
0e320a79 287{
9a83f860 288 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
86dc2301
SN
289
290 m_endModalCalled = true;
c9cb56f7 291 SetReturnCode(nRetCode);
86dc2301
SN
292
293 Hide();
c9cb56f7 294} // end of wxDialog::EndModal
0e320a79 295
743e24aa 296MRESULT wxDialog::OS2WindowProc( WXUINT uMessage, WXWPARAM wParam, WXLPARAM lParam )
0e320a79 297{
743e24aa
WS
298 MRESULT rc = 0;
299 bool bProcessed = false;
27476f73 300
c9cb56f7 301 switch (uMessage)
27476f73
DW
302 {
303 case WM_CLOSE:
c9cb56f7
DW
304 //
305 // If we can't close, tell the system that we processed the
27476f73 306 // message - otherwise it would close us
c9cb56f7
DW
307 //
308 bProcessed = !Close();
27476f73
DW
309 break;
310 }
311
c9cb56f7
DW
312 if (!bProcessed)
313 rc = wxWindow::OS2WindowProc( uMessage
314 ,wParam
315 ,lParam
316 );
27476f73 317 return rc;
c9cb56f7 318} // end of wxDialog::OS2WindowProc