]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Add missing wxEVT_COMMAND_TEXT_ENTER
[wxWidgets.git] / src / msw / dialog.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
b6c588e1 2// Name: src/msw/dialog.cpp
2bda0e17
KB
3// Purpose: wxDialog class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b6c588e1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
b6c588e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
fdf565fe
WS
27#include "wx/dialog.h"
28
2bda0e17 29#ifndef WX_PRECOMP
57bd4c60 30 #include "wx/msw/wrapcdlg.h"
b6c588e1
VZ
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
95e92d90 34 #include "wx/button.h"
b6c588e1
VZ
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
4e3e485b 38 #include "wx/toolbar.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
ac8d0c11 42#include "wx/evtloop.h"
664e1314 43#include "wx/scopedptr.h"
2bda0e17 44
3180bc0e 45#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b 46 #include "wx/msw/wince/resources.h"
3180bc0e 47#endif // __SMARTPHONE__ && __WXWINCE__
ef6d716b 48
b6c588e1
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
6757b5e3
VZ
53// ----------------------------------------------------------------------------
54// wxDialogModalData
55// ----------------------------------------------------------------------------
56
8c7f5f03
VZ
57// this is simply a container for any data we need to implement modality which
58// allows us to avoid changing wxDialog each time the implementation changes
6757b5e3
VZ
59class wxDialogModalData
60{
61public:
8c7f5f03 62 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 63
8c7f5f03 64 void RunLoop()
6757b5e3 65 {
6757b5e3
VZ
66 m_evtLoop.Run();
67 }
68
69 void ExitLoop()
70 {
6757b5e3
VZ
71 m_evtLoop.Exit();
72 }
73
6757b5e3 74private:
8c7f5f03 75 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
76};
77
259c43f6 78wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 79
b6c588e1
VZ
80// ============================================================================
81// implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// wxDialog construction
86// ----------------------------------------------------------------------------
87
b0a6bb75 88void wxDialog::Init()
2bda0e17 89{
044fe836 90 m_isShown = false;
6757b5e3 91 m_modalData = NULL;
ec5f0c24
JS
92#if wxUSE_TOOLBAR && defined(__POCKETPC__)
93 m_dialogToolBar = NULL;
94#endif
40636dcb 95#if wxUSE_DIALOG_SIZEGRIP
2c66581e 96 m_hGripper = 0;
40636dcb 97#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
98}
99
b3daa5a3
VZ
100bool wxDialog::Create(wxWindow *parent,
101 wxWindowID id,
462e2437
VZ
102 const wxString& title,
103 const wxPoint& pos,
104 const wxSize& size,
105 long style,
106 const wxString& name)
2bda0e17 107{
82c9f85c 108 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 109
706bb5f9 110 // All dialogs should really have this style
b225f659 111 style |= wxTAB_TRAVERSAL;
2bda0e17 112
b225f659 113 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 114 return false;
00233716
VZ
115
116 if ( !m_hasFont )
6f951810 117 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 118
3180bc0e 119#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
120 SetLeftMenu(wxID_OK, _("OK"));
121#endif
ec5f0c24
JS
122#if wxUSE_TOOLBAR && defined(__POCKETPC__)
123 CreateToolBar();
124#endif
ef6d716b 125
40636dcb 126#if wxUSE_DIALOG_SIZEGRIP
952f8d3c 127 if ( HasFlag(wxRESIZE_BORDER) )
eddc468e 128 {
2c66581e
VZ
129 CreateGripper();
130
eddc468e
VZ
131 Connect(wxEVT_CREATE,
132 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
133 }
40636dcb 134#endif // wxUSE_DIALOG_SIZEGRIP
eddc468e 135
044fe836 136 return true;
2bda0e17
KB
137}
138
2bda0e17
KB
139wxDialog::~wxDialog()
140{
b0a6bb75 141 // this will also reenable all the other windows for a modal dialog
044fe836 142 Show(false);
2c66581e 143
40636dcb 144#if wxUSE_DIALOG_SIZEGRIP
2c66581e 145 DestroyGripper();
40636dcb 146#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
147}
148
b6c588e1
VZ
149// ----------------------------------------------------------------------------
150// showing the dialogs
151// ----------------------------------------------------------------------------
22cf5fec 152
b6c588e1 153bool wxDialog::Show(bool show)
2bda0e17 154{
044fe836
VZ
155 if ( show == IsShown() )
156 return false;
157
6757b5e3 158 if ( !show && m_modalData )
86ad564e 159 {
6757b5e3
VZ
160 // we need to do this before calling wxDialogBase version because if we
161 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 162 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
163 // which is enabled, anyhow) when we're hidden in the base class Show()
164 // and we will lose activation
165 m_modalData->ExitLoop();
86ad564e
JS
166 }
167
044fe836 168 if ( show )
b6c588e1 169 {
3aa8e4ea
JS
170 if (CanDoLayoutAdaptation())
171 DoLayoutAdaptation();
172
044fe836
VZ
173 // this usually will result in TransferDataToWindow() being called
174 // which will change the controls values so do it before showing as
175 // otherwise we could have some flicker
176 InitDialog();
b6c588e1 177 }
2bda0e17 178
044fe836
VZ
179 wxDialogBase::Show(show);
180
b6c588e1
VZ
181 if ( show )
182 {
2bc44d62
VZ
183 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
184 // reason so generate it ourselves for consistency with frames and
185 // dialogs in other ports
2b5f62a0
VZ
186 //
187 // NB: normally we should call it just the first time but doing it
188 // every time is simpler than keeping a flag
2bc44d62
VZ
189 const wxSize size = GetClientSize();
190 ::SendMessage(GetHwnd(), WM_SIZE,
191 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
b6c588e1 192 }
2bda0e17 193
044fe836 194 return true;
2bda0e17
KB
195}
196
f46f4c86 197// show dialog modally
a23fd0e1 198int wxDialog::ShowModal()
2bda0e17 199{
9a83f860 200 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
f46f4c86 201
f46f4c86
VZ
202 Show();
203
204 // EndModal may have been called from InitDialog handler (called from
89424d9b 205 // inside Show()) and hidden the dialog back again
4b5e178a 206 if ( IsShown() )
5e1febfa 207 {
f46f4c86 208 // enter and run the modal loop
88a67391
VZ
209 wxDialogModalDataTiedPtr modalData(&m_modalData,
210 new wxDialogModalData(this));
211 modalData->RunLoop();
f46f4c86 212 }
5e1febfa 213
b6c588e1 214 return GetReturnCode();
2bda0e17
KB
215}
216
217void wxDialog::EndModal(int retCode)
218{
9a83f860 219 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
f46f4c86 220
b6c588e1 221 SetReturnCode(retCode);
6a088435 222
12b58624
VZ
223 Hide();
224}
225
2c66581e
VZ
226// ----------------------------------------------------------------------------
227// wxDialog gripper handling
228// ----------------------------------------------------------------------------
229
40636dcb
VZ
230#if wxUSE_DIALOG_SIZEGRIP
231
2c66581e
VZ
232void wxDialog::SetWindowStyleFlag(long style)
233{
234 wxDialogBase::SetWindowStyleFlag(style);
235
952f8d3c 236 if ( HasFlag(wxRESIZE_BORDER) )
2c66581e
VZ
237 CreateGripper();
238 else
239 DestroyGripper();
240}
241
242void wxDialog::CreateGripper()
243{
952f8d3c 244 if ( !m_hGripper )
2c66581e 245 {
952f8d3c 246 // just create it here, it will be positioned and shown later
2c66581e
VZ
247 m_hGripper = (WXHWND)::CreateWindow
248 (
249 wxT("SCROLLBAR"),
250 wxT(""),
952f8d3c
VZ
251 WS_CHILD |
252 WS_CLIPSIBLINGS |
2c66581e
VZ
253 SBS_SIZEGRIP |
254 SBS_SIZEBOX |
255 SBS_SIZEBOXBOTTOMRIGHTALIGN,
256 0, 0, 0, 0,
257 GetHwnd(),
258 0,
259 wxGetInstance(),
260 NULL
261 );
2c66581e
VZ
262 }
263}
264
265void wxDialog::DestroyGripper()
266{
267 if ( m_hGripper )
268 {
eddc468e
VZ
269 // we used to have trouble with gripper appearing on top (and hence
270 // overdrawing) the other, real, dialog children -- check that this
f78c70f3
VZ
271 // isn't the case automatically (but notice that this could be false if
272 // we're not shown at all as in this case ResizeGripper() might not
273 // have been called yet)
274 wxASSERT_MSG( !IsShown() ||
c3b46177 275 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
9a83f860 276 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
2c66581e
VZ
277 ::DestroyWindow((HWND) m_hGripper);
278 m_hGripper = 0;
279 }
280}
281
282void wxDialog::ShowGripper(bool show)
283{
9a83f860 284 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e 285
952f8d3c
VZ
286 if ( show )
287 ResizeGripper();
288
2c66581e
VZ
289 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
290}
291
292void wxDialog::ResizeGripper()
293{
9a83f860 294 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e
VZ
295
296 HWND hwndGripper = (HWND)m_hGripper;
297
298 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
299 const wxSize size = GetClientSize() - rectGripper.GetSize();
300
301 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
302 size.x, size.y,
303 rectGripper.width, rectGripper.height,
304 SWP_NOACTIVATE);
305}
306
eddc468e
VZ
307void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
308{
309 if ( m_hGripper && IsShown() &&
310 event.GetWindow() && event.GetWindow()->GetParent() == this )
311 {
312 // Put gripper below the newly created child window
313 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
314 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
315 }
316
317 event.Skip();
318}
319
40636dcb
VZ
320#endif // wxUSE_DIALOG_SIZEGRIP
321
b6c588e1
VZ
322// ----------------------------------------------------------------------------
323// wxWin event handlers
324// ----------------------------------------------------------------------------
2bda0e17 325
9ceeecb9
JS
326#ifdef __POCKETPC__
327// Responds to the OK button in a PocketPC titlebar. This
328// can be overridden, or you can change the id used for
329// sending the event, by calling SetAffirmativeId.
330bool wxDialog::DoOK()
331{
f55fee08
VZ
332 const int idOk = GetAffirmativeId();
333 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 334 return true;
9ceeecb9 335
f55fee08
VZ
336 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
337 event.SetEventObject(this);
338
937013e0 339 return HandleWindowEvent(event);
9ceeecb9 340}
f55fee08 341#endif // __POCKETPC__
9ceeecb9 342
ec5f0c24
JS
343#if wxUSE_TOOLBAR && defined(__POCKETPC__)
344// create main toolbar by calling OnCreateToolBar()
345wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
346{
347 m_dialogToolBar = OnCreateToolBar(style, winid, name);
348
349 return m_dialogToolBar;
350}
351
352// return a new toolbar
353wxToolBar *wxDialog::OnCreateToolBar(long style,
354 wxWindowID winid,
355 const wxString& name)
356{
357 return new wxToolMenuBar(this, winid,
358 wxDefaultPosition, wxDefaultSize,
359 style, name);
660296aa 360}
ec5f0c24 361#endif
9ceeecb9 362
42e69d6b 363// ---------------------------------------------------------------------------
f55fee08 364// dialog Windows messages processing
42e69d6b
VZ
365// ---------------------------------------------------------------------------
366
c140b7e7 367WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 368{
c140b7e7 369 WXLRESULT rc = 0;
044fe836 370 bool processed = false;
42e69d6b
VZ
371
372 switch ( message )
373 {
0fc58b86
RR
374#ifdef __WXWINCE__
375 // react to pressing the OK button in the title
376 case WM_COMMAND:
ef6d716b
WS
377 {
378 switch ( LOWORD(wParam) )
0fc58b86 379 {
9ceeecb9 380#ifdef __POCKETPC__
ef6d716b 381 case IDOK:
9ceeecb9
JS
382 processed = DoOK();
383 if (!processed)
b554cf63 384 processed = !Close();
9ceeecb9
JS
385#endif
386#ifdef __SMARTPHONE__
ef6d716b
WS
387 case IDM_LEFT:
388 case IDM_RIGHT:
389 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 390 break;
ef6d716b 391#endif // __SMARTPHONE__
0fc58b86
RR
392 }
393 break;
ef6d716b 394 }
044fe836 395#endif
42e69d6b
VZ
396 case WM_CLOSE:
397 // if we can't close, tell the system that we processed the
398 // message - otherwise it would close us
399 processed = !Close();
400 break;
abceee76 401
f73f67be 402 case WM_SIZE:
40636dcb 403#if wxUSE_DIALOG_SIZEGRIP
2c66581e
VZ
404 if ( m_hGripper )
405 {
406 switch ( wParam )
407 {
408 case SIZE_MAXIMIZED:
409 ShowGripper(false);
410 break;
411
412 case SIZE_RESTORED:
413 ShowGripper(true);
2c66581e
VZ
414 }
415 }
40636dcb 416#endif // wxUSE_DIALOG_SIZEGRIP
2c66581e 417
d13b34d3 418 // the Windows dialogs unfortunately are not meant to be resizable
f73f67be
VZ
419 // at all and their standard class doesn't include CS_[VH]REDRAW
420 // styles which means that the window is not refreshed properly
421 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
422 // help with it - so we have to refresh it manually which certainly
423 // creates flicker but at least doesn't show garbage on the screen
424 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 425 processed = true;
e441e1f4 426 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 427 {
044fe836 428 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
429 }
430 break;
431
04ef50df 432#ifndef __WXMICROWIN__
abceee76
VZ
433 case WM_SETCURSOR:
434 // we want to override the busy cursor for modal dialogs:
435 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 436 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 437 if ( IsModal() && wxIsBusy() )
abceee76 438 {
bfbd6dc1
VZ
439 // set our cursor for all windows (but see below)
440 wxCursor cursor = m_cursor;
a1b806b9 441 if ( !cursor.IsOk() )
bfbd6dc1 442 cursor = wxCURSOR_ARROW;
abceee76 443
bfbd6dc1
VZ
444 ::SetCursor(GetHcursorOf(cursor));
445
446 // in any case, stop here and don't let wxWindow process this
447 // message (it would set the busy cursor)
044fe836 448 processed = true;
bfbd6dc1 449
044fe836 450 // but return false to tell the child window (if the event
bfbd6dc1
VZ
451 // comes from one of them and not from ourselves) that it can
452 // set its own cursor if it has one: thus, standard controls
453 // (e.g. text ctrl) still have correct cursors in a dialog
454 // invoked while wxIsBusy()
044fe836 455 rc = false;
abceee76 456 }
bfbd6dc1 457 break;
82c9f85c 458#endif // __WXMICROWIN__
42e69d6b
VZ
459 }
460
461 if ( !processed )
462 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
463
464 return rc;
465}