don't use dialog size gripper under CE (should close #10765)
[wxWidgets.git] / src / msw / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dialog.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapcdlg.h"
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/toolbar.h"
39 #endif
40
41 #include "wx/msw/private.h"
42 #include "wx/evtloop.h"
43 #include "wx/scopedptr.h"
44
45 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
46 #include "wx/msw/wince/resources.h"
47 #endif // __SMARTPHONE__ && __WXWINCE__
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxDialogStyle )
55
56 wxBEGIN_FLAGS( wxDialogStyle )
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62 wxFLAGS_MEMBER(wxBORDER_RAISED)
63 wxFLAGS_MEMBER(wxBORDER_STATIC)
64 wxFLAGS_MEMBER(wxBORDER_NONE)
65
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70 wxFLAGS_MEMBER(wxRAISED_BORDER)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER)
72 wxFLAGS_MEMBER(wxNO_BORDER)
73
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
77
78 // dialog styles
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
81 wxFLAGS_MEMBER(wxCAPTION)
82 #if WXWIN_COMPATIBILITY_2_6
83 wxFLAGS_MEMBER(wxTHICK_FRAME)
84 #endif // WXWIN_COMPATIBILITY_2_6
85 wxFLAGS_MEMBER(wxSYSTEM_MENU)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER)
87 #if WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxRESIZE_BOX)
89 #endif // WXWIN_COMPATIBILITY_2_6
90 wxFLAGS_MEMBER(wxCLOSE_BOX)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
93 wxEND_FLAGS( wxDialogStyle )
94
95 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
96
97 wxBEGIN_PROPERTIES_TABLE(wxDialog)
98 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100 wxEND_PROPERTIES_TABLE()
101
102 wxBEGIN_HANDLERS_TABLE(wxDialog)
103 wxEND_HANDLERS_TABLE()
104
105 wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
106
107 #else
108 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
109 #endif
110
111 // ----------------------------------------------------------------------------
112 // wxDialogModalData
113 // ----------------------------------------------------------------------------
114
115 // this is simply a container for any data we need to implement modality which
116 // allows us to avoid changing wxDialog each time the implementation changes
117 class wxDialogModalData
118 {
119 public:
120 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
121
122 void RunLoop()
123 {
124 m_evtLoop.Run();
125 }
126
127 void ExitLoop()
128 {
129 m_evtLoop.Exit();
130 }
131
132 private:
133 wxModalEventLoop m_evtLoop;
134 };
135
136 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
137
138 // ============================================================================
139 // implementation
140 // ============================================================================
141
142 // ----------------------------------------------------------------------------
143 // wxDialog construction
144 // ----------------------------------------------------------------------------
145
146 void wxDialog::Init()
147 {
148 m_isShown = false;
149 m_modalData = NULL;
150 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
151 m_dialogToolBar = NULL;
152 #endif
153 #if wxUSE_DIALOG_SIZEGRIP
154 m_hGripper = 0;
155 #endif // wxUSE_DIALOG_SIZEGRIP
156 }
157
158 bool wxDialog::Create(wxWindow *parent,
159 wxWindowID id,
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
165 {
166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
167
168 // All dialogs should really have this style
169 style |= wxTAB_TRAVERSAL;
170
171 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
172 return false;
173
174 if ( !m_hasFont )
175 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
176
177 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
178 SetLeftMenu(wxID_OK, _("OK"));
179 #endif
180 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
181 CreateToolBar();
182 #endif
183
184 #if wxUSE_DIALOG_SIZEGRIP
185 if ( HasFlag(wxRESIZE_BORDER) )
186 {
187 CreateGripper();
188
189 Connect(wxEVT_CREATE,
190 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
191 }
192 #endif // wxUSE_DIALOG_SIZEGRIP
193
194 return true;
195 }
196
197 wxDialog::~wxDialog()
198 {
199 // this will also reenable all the other windows for a modal dialog
200 Show(false);
201
202 #if wxUSE_DIALOG_SIZEGRIP
203 DestroyGripper();
204 #endif // wxUSE_DIALOG_SIZEGRIP
205 }
206
207 // ----------------------------------------------------------------------------
208 // showing the dialogs
209 // ----------------------------------------------------------------------------
210
211 wxWindow *wxDialog::FindSuitableParent() const
212 {
213 // first try to use the currently active window
214 HWND hwndFg = ::GetForegroundWindow();
215 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
216 : NULL;
217 if ( !parent )
218 {
219 // next try the main app window
220 parent = wxTheApp->GetTopWindow();
221 }
222
223 // finally, check if the parent we found is really suitable
224 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
225 {
226 // don't use this one
227 parent = NULL;
228 }
229
230 return parent;
231 }
232
233 bool wxDialog::Show(bool show)
234 {
235 if ( show == IsShown() )
236 return false;
237
238 if ( !show && m_modalData )
239 {
240 // we need to do this before calling wxDialogBase version because if we
241 // had disabled other app windows, they must be reenabled right now as
242 // if they stay disabled Windows will activate another window (one
243 // which is enabled, anyhow) when we're hidden in the base class Show()
244 // and we will lose activation
245 m_modalData->ExitLoop();
246 }
247
248 if ( show )
249 {
250 if (CanDoLayoutAdaptation())
251 DoLayoutAdaptation();
252
253 // this usually will result in TransferDataToWindow() being called
254 // which will change the controls values so do it before showing as
255 // otherwise we could have some flicker
256 InitDialog();
257 }
258
259 wxDialogBase::Show(show);
260
261 if ( show )
262 {
263 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
264 // reason so generate it ourselves for consistency with frames and
265 // dialogs in other ports
266 //
267 // NB: normally we should call it just the first time but doing it
268 // every time is simpler than keeping a flag
269 const wxSize size = GetClientSize();
270 ::SendMessage(GetHwnd(), WM_SIZE,
271 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
272 }
273
274 return true;
275 }
276
277 void wxDialog::Raise()
278 {
279 ::SetForegroundWindow(GetHwnd());
280 }
281
282 // show dialog modally
283 int wxDialog::ShowModal()
284 {
285 wxASSERT_MSG( !IsModal(), _T("ShowModal() can't be called twice") );
286
287 Show();
288
289 // EndModal may have been called from InitDialog handler (called from
290 // inside Show()) and hidden the dialog back again
291 if ( IsShown() )
292 {
293 // enter and run the modal loop
294 wxDialogModalDataTiedPtr modalData(&m_modalData,
295 new wxDialogModalData(this));
296 modalData->RunLoop();
297 }
298
299 return GetReturnCode();
300 }
301
302 void wxDialog::EndModal(int retCode)
303 {
304 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
305
306 SetReturnCode(retCode);
307
308 Hide();
309 }
310
311 // ----------------------------------------------------------------------------
312 // wxDialog gripper handling
313 // ----------------------------------------------------------------------------
314
315 #if wxUSE_DIALOG_SIZEGRIP
316
317 void wxDialog::SetWindowStyleFlag(long style)
318 {
319 wxDialogBase::SetWindowStyleFlag(style);
320
321 if ( HasFlag(wxRESIZE_BORDER) )
322 CreateGripper();
323 else
324 DestroyGripper();
325 }
326
327 void wxDialog::CreateGripper()
328 {
329 if ( !m_hGripper )
330 {
331 // just create it here, it will be positioned and shown later
332 m_hGripper = (WXHWND)::CreateWindow
333 (
334 wxT("SCROLLBAR"),
335 wxT(""),
336 WS_CHILD |
337 WS_CLIPSIBLINGS |
338 SBS_SIZEGRIP |
339 SBS_SIZEBOX |
340 SBS_SIZEBOXBOTTOMRIGHTALIGN,
341 0, 0, 0, 0,
342 GetHwnd(),
343 0,
344 wxGetInstance(),
345 NULL
346 );
347 }
348 }
349
350 void wxDialog::DestroyGripper()
351 {
352 if ( m_hGripper )
353 {
354 // we used to have trouble with gripper appearing on top (and hence
355 // overdrawing) the other, real, dialog children -- check that this
356 // isn't the case automatically (but notice that this could be false if
357 // we're not shown at all as in this case ResizeGripper() might not
358 // have been called yet)
359 wxASSERT_MSG( !IsShown() ||
360 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
361 _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
362 ::DestroyWindow((HWND) m_hGripper);
363 m_hGripper = 0;
364 }
365 }
366
367 void wxDialog::ShowGripper(bool show)
368 {
369 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
370
371 if ( show )
372 ResizeGripper();
373
374 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
375 }
376
377 void wxDialog::ResizeGripper()
378 {
379 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
380
381 HWND hwndGripper = (HWND)m_hGripper;
382
383 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
384 const wxSize size = GetClientSize() - rectGripper.GetSize();
385
386 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
387 size.x, size.y,
388 rectGripper.width, rectGripper.height,
389 SWP_NOACTIVATE);
390 }
391
392 void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
393 {
394 if ( m_hGripper && IsShown() &&
395 event.GetWindow() && event.GetWindow()->GetParent() == this )
396 {
397 // Put gripper below the newly created child window
398 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
399 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
400 }
401
402 event.Skip();
403 }
404
405 #endif // wxUSE_DIALOG_SIZEGRIP
406
407 // ----------------------------------------------------------------------------
408 // wxWin event handlers
409 // ----------------------------------------------------------------------------
410
411 #ifdef __POCKETPC__
412 // Responds to the OK button in a PocketPC titlebar. This
413 // can be overridden, or you can change the id used for
414 // sending the event, by calling SetAffirmativeId.
415 bool wxDialog::DoOK()
416 {
417 const int idOk = GetAffirmativeId();
418 if ( EmulateButtonClickIfPresent(idOk) )
419 return true;
420
421 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
422 event.SetEventObject(this);
423
424 return HandleWindowEvent(event);
425 }
426 #endif // __POCKETPC__
427
428 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
429 // create main toolbar by calling OnCreateToolBar()
430 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
431 {
432 m_dialogToolBar = OnCreateToolBar(style, winid, name);
433
434 return m_dialogToolBar;
435 }
436
437 // return a new toolbar
438 wxToolBar *wxDialog::OnCreateToolBar(long style,
439 wxWindowID winid,
440 const wxString& name)
441 {
442 return new wxToolMenuBar(this, winid,
443 wxDefaultPosition, wxDefaultSize,
444 style, name);
445 }
446 #endif
447
448 // ---------------------------------------------------------------------------
449 // dialog Windows messages processing
450 // ---------------------------------------------------------------------------
451
452 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
453 {
454 WXLRESULT rc = 0;
455 bool processed = false;
456
457 switch ( message )
458 {
459 #ifdef __WXWINCE__
460 // react to pressing the OK button in the title
461 case WM_COMMAND:
462 {
463 switch ( LOWORD(wParam) )
464 {
465 #ifdef __POCKETPC__
466 case IDOK:
467 processed = DoOK();
468 if (!processed)
469 processed = !Close();
470 #endif
471 #ifdef __SMARTPHONE__
472 case IDM_LEFT:
473 case IDM_RIGHT:
474 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
475 break;
476 #endif // __SMARTPHONE__
477 }
478 break;
479 }
480 #endif
481 case WM_CLOSE:
482 // if we can't close, tell the system that we processed the
483 // message - otherwise it would close us
484 processed = !Close();
485 break;
486
487 case WM_SIZE:
488 #if wxUSE_DIALOG_SIZEGRIP
489 if ( m_hGripper )
490 {
491 switch ( wParam )
492 {
493 case SIZE_MAXIMIZED:
494 ShowGripper(false);
495 break;
496
497 case SIZE_RESTORED:
498 ShowGripper(true);
499 }
500 }
501 #endif // wxUSE_DIALOG_SIZEGRIP
502
503 // the Windows dialogs unfortunately are not meant to be resizeable
504 // at all and their standard class doesn't include CS_[VH]REDRAW
505 // styles which means that the window is not refreshed properly
506 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
507 // help with it - so we have to refresh it manually which certainly
508 // creates flicker but at least doesn't show garbage on the screen
509 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
510 processed = true;
511 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
512 {
513 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
514 }
515 break;
516
517 #ifndef __WXMICROWIN__
518 case WM_SETCURSOR:
519 // we want to override the busy cursor for modal dialogs:
520 // typically, wxBeginBusyCursor() is called and then a modal dialog
521 // is shown, but the modal dialog shouldn't have hourglass cursor
522 if ( IsModal() && wxIsBusy() )
523 {
524 // set our cursor for all windows (but see below)
525 wxCursor cursor = m_cursor;
526 if ( !cursor.Ok() )
527 cursor = wxCURSOR_ARROW;
528
529 ::SetCursor(GetHcursorOf(cursor));
530
531 // in any case, stop here and don't let wxWindow process this
532 // message (it would set the busy cursor)
533 processed = true;
534
535 // but return false to tell the child window (if the event
536 // comes from one of them and not from ourselves) that it can
537 // set its own cursor if it has one: thus, standard controls
538 // (e.g. text ctrl) still have correct cursors in a dialog
539 // invoked while wxIsBusy()
540 rc = false;
541 }
542 break;
543 #endif // __WXMICROWIN__
544 }
545
546 if ( !processed )
547 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
548
549 return rc;
550 }