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