]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dialog.cpp
added notebook.cpp and tabmdi.cpp
[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
0e320a79 7// RCS-ID: $Id$
fb46a9a6 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
27476f73
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79
DW
14
15#include "wx/dialog.h"
e4db172a
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #include "wx/frame.h"
20 #include "wx/app.h"
21 #include "wx/settings.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
27476f73
DW
24#endif
25
26#include "wx/os2/private.h"
598dc9b7
SN
27#include "wx/evtloop.h"
28#include "wx/ptr_scpd.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
5d44b24e 36IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
27476f73 37
d491523b 38BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
5d44b24e
DW
39 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
40 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
41 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
42 EVT_CHAR_HOOK(wxDialog::OnCharHook)
43 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
86dc2301 44
5d44b24e
DW
45 EVT_CLOSE(wxDialog::OnCloseWindow)
46END_EVENT_TABLE()
0e320a79 47
598dc9b7
SN
48// ----------------------------------------------------------------------------
49// wxDialogModalData
50// ----------------------------------------------------------------------------
51
52// this is simply a container for any data we need to implement modality which
53// allows us to avoid changing wxDialog each time the implementation changes
54class wxDialogModalData
55{
56public:
57 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
58
59 void RunLoop()
60 {
61 m_evtLoop.Run();
62 }
63
64 void ExitLoop()
65 {
66 m_evtLoop.Exit();
67 }
68
69private:
70 wxModalEventLoop m_evtLoop;
71};
72
73wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData);
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// wxDialog construction
81// ----------------------------------------------------------------------------
82
c9cb56f7 83void wxDialog::Init()
0e320a79 84{
c9cb56f7 85 m_pOldFocus = (wxWindow *)NULL;
743e24aa 86 m_isShown = false;
c9cb56f7 87 m_pWindowDisabler = (wxWindowDisabler *)NULL;
ee22d7f3 88 m_modalData = NULL;
a756f210 89 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c9cb56f7
DW
90} // end of wxDialog::Init
91
6670f564
WS
92bool wxDialog::Create( wxWindow* pParent,
93 wxWindowID vId,
94 const wxString& rsTitle,
95 const wxPoint& rPos,
96 const wxSize& rSize,
97 long lStyle,
98 const wxString& rsName )
0e320a79 99{
c9cb56f7 100 Init();
5d44b24e 101 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
c9cb56f7
DW
102
103 //
5d44b24e 104 // Save focus before doing anything which can potentially change it
c9cb56f7 105 //
5d44b24e 106 m_pOldFocus = FindFocus();
c9cb56f7
DW
107
108 //
5d44b24e 109 // All dialogs should really have this style
c9cb56f7 110 //
5d44b24e
DW
111 lStyle |= wxTAB_TRAVERSAL;
112
113 if (!wxTopLevelWindow::Create( pParent
114 ,vId
115 ,rsTitle
116 ,rPos
117 ,rSize
118 ,lStyle
119 ,rsName
120 ))
6670f564
WS
121 return false;
122
a756f210 123 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
c9b9e020
DW
124
125 //
126 // Must defer setting the title until after dialog is created and sized
127 //
128 if (!rsTitle.IsNull())
129 SetTitle(rsTitle);
6670f564 130 return true;
c9cb56f7 131} // end of wxDialog::Create
0e320a79 132
89831b54
WS
133#if WXWIN_COMPATIBILITY_2_6
134
86dc2301
SN
135// deprecated ctor
136wxDialog::wxDialog(wxWindow *parent,
137 const wxString& title,
138 bool WXUNUSED(modal),
139 int x,
140 int y,
141 int w,
142 int h,
143 long style,
144 const wxString& name)
145{
146 Init();
147
148 Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name);
149}
150
89831b54 151void wxDialog::SetModal(bool WXUNUSED(bFlag))
0e320a79 152{
86dc2301 153 // nothing to do, obsolete method
c9cb56f7 154} // end of wxDialog::SetModal
0e320a79 155
89831b54
WS
156#endif // WXWIN_COMPATIBILITY_2_6
157
0e320a79
DW
158wxDialog::~wxDialog()
159{
6670f564 160 m_isBeingDeleted = true;
86dc2301
SN
161
162 // this will also reenable all the other windows for a modal dialog
163 Show(false);
c9cb56f7 164} // end of wxDialog::~wxDialog
0e320a79 165
c9cb56f7 166//
0e320a79 167// By default, pressing escape cancels the dialog
c9cb56f7
DW
168//
169void wxDialog::OnCharHook(
170 wxKeyEvent& rEvent
171)
0e320a79 172{
27476f73
DW
173 if (GetHWND())
174 {
c9cb56f7 175 if (rEvent.m_keyCode == WXK_ESCAPE)
27476f73 176 {
c9cb56f7 177 //
27476f73
DW
178 // Behaviour changed in 2.0: we'll send a Cancel message
179 // to the dialog instead of Close.
c9cb56f7
DW
180 //
181 wxCommandEvent vCancelEvent( wxEVT_COMMAND_BUTTON_CLICKED
182 ,wxID_CANCEL
183 );
184
185 vCancelEvent.SetEventObject( this );
186 GetEventHandler()->ProcessEvent(vCancelEvent);
187
188 //
189 // Ensure that there is another message for this window so the
190 // ShowModal loop will exit and won't get stuck in GetMessage().
191 //
192 ::WinPostMsg(GetHwnd(), WM_NULL, 0, 0);
27476f73
DW
193 return;
194 }
195 }
196 // We didn't process this event.
c9cb56f7 197 rEvent.Skip();
27476f73
DW
198}
199
5d44b24e
DW
200// ----------------------------------------------------------------------------
201// showing the dialogs
202// ----------------------------------------------------------------------------
27476f73 203
89831b54
WS
204#if WXWIN_COMPATIBILITY_2_6
205
c9cb56f7 206bool wxDialog::IsModalShowing() const
0e320a79 207{
86dc2301 208 return IsModal();
c9cb56f7 209} // end of wxDialog::IsModalShowing
0e320a79 210
89831b54 211#endif // WXWIN_COMPATIBILITY_2_6
0e320a79 212
86dc2301
SN
213wxWindow *wxDialog::FindSuitableParent() const
214{
215 // first try to use the currently active window
216 HWND hwndFg = ::WinQueryActiveWindow(HWND_DESKTOP);
217 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
218 : NULL;
219 if ( !parent )
c9cb56f7 220 {
86dc2301
SN
221 // next try the main app window
222 parent = wxTheApp->GetTopWindow();
c9cb56f7
DW
223 }
224
86dc2301
SN
225 // finally, check if the parent we found is really suitable
226 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
c9cb56f7 227 {
86dc2301
SN
228 // don't use this one
229 parent = NULL;
c9cb56f7
DW
230 }
231
86dc2301
SN
232 return parent;
233}
c9cb56f7 234
743e24aa 235bool wxDialog::Show( bool bShow )
0e320a79 236{
86dc2301
SN
237 if ( bShow == IsShown() )
238 return false;
239
240 if (!bShow && m_modalData )
c9cb56f7 241 {
86dc2301
SN
242 // we need to do this before calling wxDialogBase version because if we
243 // had disabled other app windows, they must be reenabled right now as
c9cb56f7 244 // if they stay disabled Windows will activate another window (one
86dc2301
SN
245 // which is enabled, anyhow) when we're hidden in the base class Show()
246 // and we will lose activation
247 m_modalData->ExitLoop();
248#if 0
c9cb56f7
DW
249 if (m_pWindowDisabler)
250 {
251 delete m_pWindowDisabler;
252 m_pWindowDisabler = NULL;
253 }
86dc2301 254#endif
c9cb56f7 255 }
0e320a79 256
86dc2301 257 if (bShow)
c9cb56f7 258 {
86dc2301
SN
259 // this usually will result in TransferDataToWindow() being called
260 // which will change the controls values so do it before showing as
261 // otherwise we could have some flicker
262 InitDialog();
c9cb56f7
DW
263 }
264
86dc2301
SN
265 wxDialogBase::Show(bShow);
266
743e24aa
WS
267 wxString title = GetTitle();
268 if (!title.empty())
269 ::WinSetWindowText((HWND)GetHwnd(), (PSZ)title.c_str());
86dc2301
SN
270
271 if ( bShow )
c9cb56f7 272 {
ee22d7f3 273 // dialogs don't get WM_SIZE message after creation unlike most (all?)
42782237 274 // other windows and so could start their life not laid out correctly
ee22d7f3
SN
275 // if we didn't call Layout() from here
276 //
277 // NB: normally we should call it just the first time but doing it
278 // every time is simpler than keeping a flag
86dc2301 279 Layout();
c9cb56f7
DW
280 }
281
86dc2301 282 return true;
c9cb56f7
DW
283} // end of wxDialog::Show
284
285//
6670f564 286// Replacement for Show(true) for modal dialogs - returns return code
c9cb56f7 287//
0e320a79
DW
288int wxDialog::ShowModal()
289{
86dc2301
SN
290 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
291
292 m_endModalCalled = false;
293
294 Show();
295
296 // EndModal may have been called from InitDialog handler (called from
297 // inside Show()), which would cause an infinite loop if we didn't take it
298 // into account
299 if ( !m_endModalCalled )
c9cb56f7 300 {
86dc2301
SN
301 // modal dialog needs a parent window, so try to find one
302 wxWindow *parent = GetParent();
303 if ( !parent )
304 {
305 parent = FindSuitableParent();
306 }
307
308 // remember where the focus was
309 wxWindow *oldFocus = m_pOldFocus;
310 if ( !oldFocus )
311 {
312 // VZ: do we really want to do this?
313 oldFocus = parent;
314 }
315
316 // We have to remember the HWND because we need to check
317 // the HWND still exists (oldFocus can be garbage when the dialog
318 // exits, if it has been destroyed)
319 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
320
321
322 //
323 // Before entering the modal loop, reset the "is in OnIdle()" flag (see
324 // comment in app.cpp)
325 //
326 extern bool gbInOnIdle;
327 bool bWasInOnIdle = gbInOnIdle;
328
743e24aa 329 gbInOnIdle = false;
86dc2301
SN
330
331 // enter and run the modal loop
332 {
333 wxDialogModalDataTiedPtr modalData(&m_modalData,
334 new wxDialogModalData(this));
335 modalData->RunLoop();
336 }
337 gbInOnIdle = bWasInOnIdle;
338
339 // and restore focus
340 // Note that this code MUST NOT access the dialog object's data
341 // in case the object has been deleted (which will be the case
342 // for a modal dialog that has been destroyed before calling EndModal).
343 if ( oldFocus && (oldFocus != this) && ::WinIsWindow(vHabmain, hwndOldFocus))
344 {
345 // This is likely to prove that the object still exists
346 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
347 oldFocus->SetFocus();
348 }
c9cb56f7 349 }
86dc2301 350
27476f73 351 return GetReturnCode();
c9cb56f7 352} // end of wxDialog::ShowModal
0e320a79 353
c9cb56f7
DW
354void wxDialog::EndModal(
355 int nRetCode
356)
0e320a79 357{
86dc2301
SN
358 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
359
360 m_endModalCalled = true;
c9cb56f7 361 SetReturnCode(nRetCode);
86dc2301
SN
362
363 Hide();
c9cb56f7 364} // end of wxDialog::EndModal
0e320a79 365
86dc2301
SN
366void wxDialog::EndDialog(int rc)
367{
368 if ( IsModal() )
369 EndModal(rc);
370 else
371 Hide();
372}
373
5d44b24e
DW
374// ----------------------------------------------------------------------------
375// wxWin event handlers
376// ----------------------------------------------------------------------------
377
6670f564 378void wxDialog::OnApply( wxCommandEvent& WXUNUSED(rEvent) )
27476f73 379{
c9cb56f7
DW
380 if (Validate())
381 TransferDataFromWindow();
382} // end of wxDialog::OnApply
383
0e320a79 384// Standard buttons
6670f564 385void wxDialog::OnOK( wxCommandEvent& WXUNUSED(rEvent) )
0e320a79 386{
27476f73
DW
387 if ( Validate() && TransferDataFromWindow() )
388 {
86dc2301 389 EndDialog(wxID_OK);
27476f73 390 }
c9cb56f7 391} // end of wxDialog::OnOK
0e320a79 392
6670f564 393void wxDialog::OnCancel( wxCommandEvent& WXUNUSED(rEvent) )
0e320a79 394{
86dc2301 395 EndDialog(wxID_CANCEL);
c9cb56f7 396} // end of wxDialog::OnCancel
0e320a79 397
6670f564 398void wxDialog::OnCloseWindow( wxCloseEvent& WXUNUSED(rEvent) )
0e320a79 399{
c9cb56f7 400 //
86dc2301 401 // We'll send a Cancel message by default, which may close the dialog.
0e320a79 402 // Check for looping if the Cancel event handler calls Close().
c9cb56f7 403 //
0e320a79
DW
404 // Note that if a cancel button and handler aren't present in the dialog,
405 // nothing will happen when you close the dialog via the window manager, or
406 // via Close().
407 // We wouldn't want to destroy the dialog by default, since the dialog may have been
408 // created on the stack.
409 // However, this does mean that calling dialog->Close() won't delete the dialog
410 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
411 // sure to destroy the dialog.
412 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
c9cb56f7 413 //
0e320a79 414
c9cb56f7
DW
415 //
416 // Ugh??? This is not good but until I figure out a global list it'll have to do
417 //
0e320a79 418 static wxList closing;
c3d43472 419
0e320a79
DW
420 if ( closing.Member(this) )
421 return;
c3d43472 422
0e320a79 423 closing.Append(this);
c3d43472 424
6670f564 425 wxCommandEvent vCancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
c9cb56f7
DW
426
427 vCancelEvent.SetEventObject( this );
428 GetEventHandler()->ProcessEvent(vCancelEvent); // This may close the dialog
0e320a79
DW
429
430 closing.DeleteObject(this);
c9cb56f7 431} // end of wxDialog::OnCloseWindow
0e320a79 432
6670f564 433void wxDialog::OnSysColourChanged( wxSysColourChangedEvent& WXUNUSED(rEvent) )
27476f73 434{
a756f210 435 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c9cb56f7
DW
436 Refresh();
437} // end of wxDialog::OnSysColourChanged
438
743e24aa 439MRESULT wxDialog::OS2WindowProc( WXUINT uMessage, WXWPARAM wParam, WXLPARAM lParam )
0e320a79 440{
743e24aa
WS
441 MRESULT rc = 0;
442 bool bProcessed = false;
27476f73 443
c9cb56f7 444 switch (uMessage)
27476f73
DW
445 {
446 case WM_CLOSE:
c9cb56f7
DW
447 //
448 // If we can't close, tell the system that we processed the
27476f73 449 // message - otherwise it would close us
c9cb56f7
DW
450 //
451 bProcessed = !Close();
27476f73
DW
452 break;
453 }
454
c9cb56f7
DW
455 if (!bProcessed)
456 rc = wxWindow::OS2WindowProc( uMessage
457 ,wParam
458 ,lParam
459 );
27476f73 460 return rc;
c9cb56f7 461} // end of wxDialog::OS2WindowProc