]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
7b4ad2baaa387e59cacc7acbbd5fe7ad17e9188f
[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/ptr_scpd.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 m_endModalCalled = false;
151 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
152 m_dialogToolBar = NULL;
153 #endif
154 m_hGripper = 0;
155 }
156
157 bool wxDialog::Create(wxWindow *parent,
158 wxWindowID id,
159 const wxString& title,
160 const wxPoint& pos,
161 const wxSize& size,
162 long style,
163 const wxString& name)
164 {
165 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
166
167 // All dialogs should really have this style
168 style |= wxTAB_TRAVERSAL;
169
170 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
171 return false;
172
173 if ( !m_hasFont )
174 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
175
176 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
177 SetLeftMenu(wxID_OK, _("OK"));
178 #endif
179 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
180 CreateToolBar();
181 #endif
182
183 if ( HasFlag(wxRESIZE_BORDER) )
184 {
185 CreateGripper();
186
187 Connect(wxEVT_CREATE,
188 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
189 }
190
191 return true;
192 }
193
194 wxDialog::~wxDialog()
195 {
196 m_isBeingDeleted = true;
197
198 // this will also reenable all the other windows for a modal dialog
199 Show(false);
200
201 DestroyGripper();
202 }
203
204 // ----------------------------------------------------------------------------
205 // showing the dialogs
206 // ----------------------------------------------------------------------------
207
208 wxWindow *wxDialog::FindSuitableParent() const
209 {
210 // first try to use the currently active window
211 HWND hwndFg = ::GetForegroundWindow();
212 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
213 : NULL;
214 if ( !parent )
215 {
216 // next try the main app window
217 parent = wxTheApp->GetTopWindow();
218 }
219
220 // finally, check if the parent we found is really suitable
221 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
222 {
223 // don't use this one
224 parent = NULL;
225 }
226
227 return parent;
228 }
229
230 bool wxDialog::Show(bool show)
231 {
232 if ( show == IsShown() )
233 return false;
234
235 if ( !show && m_modalData )
236 {
237 // we need to do this before calling wxDialogBase version because if we
238 // had disabled other app windows, they must be reenabled right now as
239 // if they stay disabled Windows will activate another window (one
240 // which is enabled, anyhow) when we're hidden in the base class Show()
241 // and we will lose activation
242 m_modalData->ExitLoop();
243 }
244
245 if ( show )
246 {
247 if (CanDoLayoutAdaptation())
248 DoLayoutAdaptation();
249
250 // this usually will result in TransferDataToWindow() being called
251 // which will change the controls values so do it before showing as
252 // otherwise we could have some flicker
253 InitDialog();
254 }
255
256 wxDialogBase::Show(show);
257
258 if ( show )
259 {
260 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
261 // reason so generate it ourselves for consistency with frames and
262 // dialogs in other ports
263 //
264 // NB: normally we should call it just the first time but doing it
265 // every time is simpler than keeping a flag
266 const wxSize size = GetClientSize();
267 ::SendMessage(GetHwnd(), WM_SIZE,
268 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
269 }
270
271 return true;
272 }
273
274 void wxDialog::Raise()
275 {
276 ::SetForegroundWindow(GetHwnd());
277 }
278
279 // show dialog modally
280 int wxDialog::ShowModal()
281 {
282 wxASSERT_MSG( !IsModal(), _T("ShowModal() can't be called twice") );
283
284 m_endModalCalled = false;
285
286 Show();
287
288 // EndModal may have been called from InitDialog handler (called from
289 // inside Show()), which would cause an infinite loop if we didn't take it
290 // into account
291 if ( !m_endModalCalled )
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 m_endModalCalled = true;
307 SetReturnCode(retCode);
308
309 Hide();
310 }
311
312 // ----------------------------------------------------------------------------
313 // wxDialog gripper handling
314 // ----------------------------------------------------------------------------
315
316 void wxDialog::SetWindowStyleFlag(long style)
317 {
318 wxDialogBase::SetWindowStyleFlag(style);
319
320 if ( HasFlag(wxRESIZE_BORDER) )
321 CreateGripper();
322 else
323 DestroyGripper();
324 }
325
326 void wxDialog::CreateGripper()
327 {
328 if ( !m_hGripper )
329 {
330 // just create it here, it will be positioned and shown later
331 m_hGripper = (WXHWND)::CreateWindow
332 (
333 wxT("SCROLLBAR"),
334 wxT(""),
335 WS_CHILD |
336 WS_CLIPSIBLINGS |
337 SBS_SIZEGRIP |
338 SBS_SIZEBOX |
339 SBS_SIZEBOXBOTTOMRIGHTALIGN,
340 0, 0, 0, 0,
341 GetHwnd(),
342 0,
343 wxGetInstance(),
344 NULL
345 );
346 }
347 }
348
349 void wxDialog::DestroyGripper()
350 {
351 if ( m_hGripper )
352 {
353 // we used to have trouble with gripper appearing on top (and hence
354 // overdrawing) the other, real, dialog children -- check that this
355 // isn't the case automatically (but notice that this could be false if
356 // we're not shown at all as in this case ResizeGripper() might not
357 // have been called yet)
358 wxASSERT_MSG( !IsShown() ||
359 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
360 _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
361 ::DestroyWindow((HWND) m_hGripper);
362 m_hGripper = 0;
363 }
364 }
365
366 void wxDialog::ShowGripper(bool show)
367 {
368 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
369
370 if ( show )
371 ResizeGripper();
372
373 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
374 }
375
376 void wxDialog::ResizeGripper()
377 {
378 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
379
380 HWND hwndGripper = (HWND)m_hGripper;
381
382 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
383 const wxSize size = GetClientSize() - rectGripper.GetSize();
384
385 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
386 size.x, size.y,
387 rectGripper.width, rectGripper.height,
388 SWP_NOACTIVATE);
389 }
390
391 void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
392 {
393 if ( m_hGripper && IsShown() &&
394 event.GetWindow() && event.GetWindow()->GetParent() == this )
395 {
396 // Put gripper below the newly created child window
397 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
398 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
399 }
400
401 event.Skip();
402 }
403
404 // ----------------------------------------------------------------------------
405 // wxWin event handlers
406 // ----------------------------------------------------------------------------
407
408 #ifdef __POCKETPC__
409 // Responds to the OK button in a PocketPC titlebar. This
410 // can be overridden, or you can change the id used for
411 // sending the event, by calling SetAffirmativeId.
412 bool wxDialog::DoOK()
413 {
414 const int idOk = GetAffirmativeId();
415 if ( EmulateButtonClickIfPresent(idOk) )
416 return true;
417
418 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
419 event.SetEventObject(this);
420
421 return HandleWindowEvent(event);
422 }
423 #endif // __POCKETPC__
424
425 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
426 // create main toolbar by calling OnCreateToolBar()
427 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
428 {
429 m_dialogToolBar = OnCreateToolBar(style, winid, name);
430
431 return m_dialogToolBar;
432 }
433
434 // return a new toolbar
435 wxToolBar *wxDialog::OnCreateToolBar(long style,
436 wxWindowID winid,
437 const wxString& name)
438 {
439 return new wxToolMenuBar(this, winid,
440 wxDefaultPosition, wxDefaultSize,
441 style, name);
442 }
443 #endif
444
445 // ---------------------------------------------------------------------------
446 // dialog Windows messages processing
447 // ---------------------------------------------------------------------------
448
449 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
450 {
451 WXLRESULT rc = 0;
452 bool processed = false;
453
454 switch ( message )
455 {
456 #ifdef __WXWINCE__
457 // react to pressing the OK button in the title
458 case WM_COMMAND:
459 {
460 switch ( LOWORD(wParam) )
461 {
462 #ifdef __POCKETPC__
463 case IDOK:
464 processed = DoOK();
465 if (!processed)
466 processed = !Close();
467 #endif
468 #ifdef __SMARTPHONE__
469 case IDM_LEFT:
470 case IDM_RIGHT:
471 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
472 break;
473 #endif // __SMARTPHONE__
474 }
475 break;
476 }
477 #endif
478 case WM_CLOSE:
479 // if we can't close, tell the system that we processed the
480 // message - otherwise it would close us
481 processed = !Close();
482 break;
483
484 case WM_SIZE:
485 if ( m_hGripper )
486 {
487 switch ( wParam )
488 {
489 case SIZE_MAXIMIZED:
490 ShowGripper(false);
491 break;
492
493 case SIZE_RESTORED:
494 ShowGripper(true);
495 }
496 }
497
498 // the Windows dialogs unfortunately are not meant to be resizeable
499 // at all and their standard class doesn't include CS_[VH]REDRAW
500 // styles which means that the window is not refreshed properly
501 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
502 // help with it - so we have to refresh it manually which certainly
503 // creates flicker but at least doesn't show garbage on the screen
504 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
505 processed = true;
506 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
507 {
508 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
509 }
510 break;
511
512 #ifndef __WXMICROWIN__
513 case WM_SETCURSOR:
514 // we want to override the busy cursor for modal dialogs:
515 // typically, wxBeginBusyCursor() is called and then a modal dialog
516 // is shown, but the modal dialog shouldn't have hourglass cursor
517 if ( IsModal() && wxIsBusy() )
518 {
519 // set our cursor for all windows (but see below)
520 wxCursor cursor = m_cursor;
521 if ( !cursor.Ok() )
522 cursor = wxCURSOR_ARROW;
523
524 ::SetCursor(GetHcursorOf(cursor));
525
526 // in any case, stop here and don't let wxWindow process this
527 // message (it would set the busy cursor)
528 processed = true;
529
530 // but return false to tell the child window (if the event
531 // comes from one of them and not from ourselves) that it can
532 // set its own cursor if it has one: thus, standard controls
533 // (e.g. text ctrl) still have correct cursors in a dialog
534 // invoked while wxIsBusy()
535 rc = false;
536 }
537 break;
538 #endif // __WXMICROWIN__
539 }
540
541 if ( !processed )
542 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
543
544 return rc;
545 }