]>
Commit | Line | Data |
---|---|---|
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 | |
6c9a19aa | 7 | // Copyright: (c) Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
2bda0e17 KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
b6c588e1 VZ |
11 | // ============================================================================ |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
2bda0e17 KB |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
b6c588e1 | 23 | #pragma hdrstop |
2bda0e17 KB |
24 | #endif |
25 | ||
fdf565fe | 26 | #include "wx/dialog.h" |
691745ab | 27 | #include "wx/modalhook.h" |
fdf565fe | 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 |
59 | class wxDialogModalData |
60 | { | |
61 | public: | |
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 | 74 | private: |
8c7f5f03 | 75 | wxModalEventLoop m_evtLoop; |
6757b5e3 VZ |
76 | }; |
77 | ||
259c43f6 | 78 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData) |
8c7f5f03 | 79 | |
b6c588e1 VZ |
80 | // ============================================================================ |
81 | // implementation | |
82 | // ============================================================================ | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // wxDialog construction | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
b0a6bb75 | 88 | void 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 |
100 | bool 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 |
139 | wxDialog::~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 | 153 | bool 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 | 198 | int wxDialog::ShowModal() |
2bda0e17 | 199 | { |
691745ab | 200 | WX_HOOK_MODAL_DIALOG(); |
643e9cf9 | 201 | |
9a83f860 | 202 | wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") ); |
f46f4c86 | 203 | |
f46f4c86 VZ |
204 | Show(); |
205 | ||
206 | // EndModal may have been called from InitDialog handler (called from | |
89424d9b | 207 | // inside Show()) and hidden the dialog back again |
4b5e178a | 208 | if ( IsShown() ) |
5e1febfa | 209 | { |
f46f4c86 | 210 | // enter and run the modal loop |
88a67391 VZ |
211 | wxDialogModalDataTiedPtr modalData(&m_modalData, |
212 | new wxDialogModalData(this)); | |
213 | modalData->RunLoop(); | |
f46f4c86 | 214 | } |
5e1febfa | 215 | |
b6c588e1 | 216 | return GetReturnCode(); |
2bda0e17 KB |
217 | } |
218 | ||
219 | void wxDialog::EndModal(int retCode) | |
220 | { | |
9a83f860 | 221 | wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") ); |
f46f4c86 | 222 | |
b6c588e1 | 223 | SetReturnCode(retCode); |
6a088435 | 224 | |
12b58624 VZ |
225 | Hide(); |
226 | } | |
227 | ||
2c66581e VZ |
228 | // ---------------------------------------------------------------------------- |
229 | // wxDialog gripper handling | |
230 | // ---------------------------------------------------------------------------- | |
231 | ||
40636dcb VZ |
232 | #if wxUSE_DIALOG_SIZEGRIP |
233 | ||
2c66581e VZ |
234 | void wxDialog::SetWindowStyleFlag(long style) |
235 | { | |
236 | wxDialogBase::SetWindowStyleFlag(style); | |
237 | ||
952f8d3c | 238 | if ( HasFlag(wxRESIZE_BORDER) ) |
2c66581e VZ |
239 | CreateGripper(); |
240 | else | |
241 | DestroyGripper(); | |
242 | } | |
243 | ||
244 | void wxDialog::CreateGripper() | |
245 | { | |
952f8d3c | 246 | if ( !m_hGripper ) |
2c66581e | 247 | { |
952f8d3c | 248 | // just create it here, it will be positioned and shown later |
2c66581e VZ |
249 | m_hGripper = (WXHWND)::CreateWindow |
250 | ( | |
251 | wxT("SCROLLBAR"), | |
252 | wxT(""), | |
952f8d3c VZ |
253 | WS_CHILD | |
254 | WS_CLIPSIBLINGS | | |
2c66581e VZ |
255 | SBS_SIZEGRIP | |
256 | SBS_SIZEBOX | | |
257 | SBS_SIZEBOXBOTTOMRIGHTALIGN, | |
258 | 0, 0, 0, 0, | |
259 | GetHwnd(), | |
260 | 0, | |
261 | wxGetInstance(), | |
262 | NULL | |
263 | ); | |
2c66581e VZ |
264 | } |
265 | } | |
266 | ||
267 | void wxDialog::DestroyGripper() | |
268 | { | |
269 | if ( m_hGripper ) | |
270 | { | |
eddc468e VZ |
271 | // we used to have trouble with gripper appearing on top (and hence |
272 | // overdrawing) the other, real, dialog children -- check that this | |
f78c70f3 VZ |
273 | // isn't the case automatically (but notice that this could be false if |
274 | // we're not shown at all as in this case ResizeGripper() might not | |
275 | // have been called yet) | |
276 | wxASSERT_MSG( !IsShown() || | |
c3b46177 | 277 | ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0, |
9a83f860 | 278 | wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") ); |
2c66581e VZ |
279 | ::DestroyWindow((HWND) m_hGripper); |
280 | m_hGripper = 0; | |
281 | } | |
282 | } | |
283 | ||
284 | void wxDialog::ShowGripper(bool show) | |
285 | { | |
9a83f860 | 286 | wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") ); |
2c66581e | 287 | |
952f8d3c VZ |
288 | if ( show ) |
289 | ResizeGripper(); | |
290 | ||
2c66581e VZ |
291 | ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE); |
292 | } | |
293 | ||
294 | void wxDialog::ResizeGripper() | |
295 | { | |
9a83f860 | 296 | wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") ); |
2c66581e VZ |
297 | |
298 | HWND hwndGripper = (HWND)m_hGripper; | |
299 | ||
300 | const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper)); | |
301 | const wxSize size = GetClientSize() - rectGripper.GetSize(); | |
302 | ||
303 | ::SetWindowPos(hwndGripper, HWND_BOTTOM, | |
304 | size.x, size.y, | |
305 | rectGripper.width, rectGripper.height, | |
306 | SWP_NOACTIVATE); | |
307 | } | |
308 | ||
eddc468e VZ |
309 | void wxDialog::OnWindowCreate(wxWindowCreateEvent& event) |
310 | { | |
311 | if ( m_hGripper && IsShown() && | |
312 | event.GetWindow() && event.GetWindow()->GetParent() == this ) | |
313 | { | |
314 | // Put gripper below the newly created child window | |
315 | ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0, | |
316 | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE); | |
317 | } | |
318 | ||
319 | event.Skip(); | |
320 | } | |
321 | ||
40636dcb VZ |
322 | #endif // wxUSE_DIALOG_SIZEGRIP |
323 | ||
b6c588e1 VZ |
324 | // ---------------------------------------------------------------------------- |
325 | // wxWin event handlers | |
326 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 327 | |
9ceeecb9 JS |
328 | #ifdef __POCKETPC__ |
329 | // Responds to the OK button in a PocketPC titlebar. This | |
330 | // can be overridden, or you can change the id used for | |
331 | // sending the event, by calling SetAffirmativeId. | |
332 | bool wxDialog::DoOK() | |
333 | { | |
f55fee08 VZ |
334 | const int idOk = GetAffirmativeId(); |
335 | if ( EmulateButtonClickIfPresent(idOk) ) | |
9ceeecb9 | 336 | return true; |
9ceeecb9 | 337 | |
ce7fe42e | 338 | wxCommandEvent event(wxEVT_BUTTON, GetAffirmativeId()); |
f55fee08 VZ |
339 | event.SetEventObject(this); |
340 | ||
937013e0 | 341 | return HandleWindowEvent(event); |
9ceeecb9 | 342 | } |
f55fee08 | 343 | #endif // __POCKETPC__ |
9ceeecb9 | 344 | |
ec5f0c24 JS |
345 | #if wxUSE_TOOLBAR && defined(__POCKETPC__) |
346 | // create main toolbar by calling OnCreateToolBar() | |
347 | wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name) | |
348 | { | |
349 | m_dialogToolBar = OnCreateToolBar(style, winid, name); | |
350 | ||
351 | return m_dialogToolBar; | |
352 | } | |
353 | ||
354 | // return a new toolbar | |
355 | wxToolBar *wxDialog::OnCreateToolBar(long style, | |
356 | wxWindowID winid, | |
357 | const wxString& name) | |
358 | { | |
359 | return new wxToolMenuBar(this, winid, | |
360 | wxDefaultPosition, wxDefaultSize, | |
361 | style, name); | |
660296aa | 362 | } |
ec5f0c24 | 363 | #endif |
9ceeecb9 | 364 | |
42e69d6b | 365 | // --------------------------------------------------------------------------- |
f55fee08 | 366 | // dialog Windows messages processing |
42e69d6b VZ |
367 | // --------------------------------------------------------------------------- |
368 | ||
c140b7e7 | 369 | WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
42e69d6b | 370 | { |
c140b7e7 | 371 | WXLRESULT rc = 0; |
044fe836 | 372 | bool processed = false; |
42e69d6b VZ |
373 | |
374 | switch ( message ) | |
375 | { | |
0fc58b86 RR |
376 | #ifdef __WXWINCE__ |
377 | // react to pressing the OK button in the title | |
378 | case WM_COMMAND: | |
ef6d716b WS |
379 | { |
380 | switch ( LOWORD(wParam) ) | |
0fc58b86 | 381 | { |
9ceeecb9 | 382 | #ifdef __POCKETPC__ |
ef6d716b | 383 | case IDOK: |
9ceeecb9 JS |
384 | processed = DoOK(); |
385 | if (!processed) | |
b554cf63 | 386 | processed = !Close(); |
9ceeecb9 JS |
387 | #endif |
388 | #ifdef __SMARTPHONE__ | |
ef6d716b WS |
389 | case IDM_LEFT: |
390 | case IDM_RIGHT: | |
391 | processed = HandleCommand( LOWORD(wParam) , 0 , NULL ); | |
0fc58b86 | 392 | break; |
ef6d716b | 393 | #endif // __SMARTPHONE__ |
0fc58b86 RR |
394 | } |
395 | break; | |
ef6d716b | 396 | } |
044fe836 | 397 | #endif |
42e69d6b VZ |
398 | case WM_CLOSE: |
399 | // if we can't close, tell the system that we processed the | |
400 | // message - otherwise it would close us | |
401 | processed = !Close(); | |
402 | break; | |
abceee76 | 403 | |
f73f67be | 404 | case WM_SIZE: |
40636dcb | 405 | #if wxUSE_DIALOG_SIZEGRIP |
2c66581e VZ |
406 | if ( m_hGripper ) |
407 | { | |
408 | switch ( wParam ) | |
409 | { | |
410 | case SIZE_MAXIMIZED: | |
411 | ShowGripper(false); | |
412 | break; | |
413 | ||
414 | case SIZE_RESTORED: | |
415 | ShowGripper(true); | |
2c66581e VZ |
416 | } |
417 | } | |
40636dcb | 418 | #endif // wxUSE_DIALOG_SIZEGRIP |
2c66581e | 419 | |
d13b34d3 | 420 | // the Windows dialogs unfortunately are not meant to be resizable |
f73f67be VZ |
421 | // at all and their standard class doesn't include CS_[VH]REDRAW |
422 | // styles which means that the window is not refreshed properly | |
423 | // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can | |
424 | // help with it - so we have to refresh it manually which certainly | |
425 | // creates flicker but at least doesn't show garbage on the screen | |
426 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
044fe836 | 427 | processed = true; |
e441e1f4 | 428 | if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) ) |
f73f67be | 429 | { |
044fe836 | 430 | ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */); |
f73f67be VZ |
431 | } |
432 | break; | |
433 | ||
04ef50df | 434 | #ifndef __WXMICROWIN__ |
abceee76 VZ |
435 | case WM_SETCURSOR: |
436 | // we want to override the busy cursor for modal dialogs: | |
437 | // typically, wxBeginBusyCursor() is called and then a modal dialog | |
bfbd6dc1 | 438 | // is shown, but the modal dialog shouldn't have hourglass cursor |
f46f4c86 | 439 | if ( IsModal() && wxIsBusy() ) |
abceee76 | 440 | { |
bfbd6dc1 VZ |
441 | // set our cursor for all windows (but see below) |
442 | wxCursor cursor = m_cursor; | |
a1b806b9 | 443 | if ( !cursor.IsOk() ) |
bfbd6dc1 | 444 | cursor = wxCURSOR_ARROW; |
abceee76 | 445 | |
bfbd6dc1 VZ |
446 | ::SetCursor(GetHcursorOf(cursor)); |
447 | ||
448 | // in any case, stop here and don't let wxWindow process this | |
449 | // message (it would set the busy cursor) | |
044fe836 | 450 | processed = true; |
bfbd6dc1 | 451 | |
044fe836 | 452 | // but return false to tell the child window (if the event |
bfbd6dc1 VZ |
453 | // comes from one of them and not from ourselves) that it can |
454 | // set its own cursor if it has one: thus, standard controls | |
455 | // (e.g. text ctrl) still have correct cursors in a dialog | |
456 | // invoked while wxIsBusy() | |
044fe836 | 457 | rc = false; |
abceee76 | 458 | } |
bfbd6dc1 | 459 | break; |
82c9f85c | 460 | #endif // __WXMICROWIN__ |
42e69d6b VZ |
461 | } |
462 | ||
463 | if ( !processed ) | |
95316a3f | 464 | rc = wxDialogBase::MSWWindowProc(message, wParam, lParam); |
42e69d6b VZ |
465 | |
466 | return rc; | |
467 | } |