]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
Hack compilation using the latest MinGW release.
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/dialog.h"
27 #include "wx/modalhook.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 // ----------------------------------------------------------------------------
54 // wxDialogModalData
55 // ----------------------------------------------------------------------------
56
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
59 class wxDialogModalData
60 {
61 public:
62 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
63
64 void RunLoop()
65 {
66 m_evtLoop.Run();
67 }
68
69 void ExitLoop()
70 {
71 m_evtLoop.Exit();
72 }
73
74 private:
75 wxModalEventLoop m_evtLoop;
76 };
77
78 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
79
80 // ============================================================================
81 // implementation
82 // ============================================================================
83
84 // ----------------------------------------------------------------------------
85 // wxDialog construction
86 // ----------------------------------------------------------------------------
87
88 void wxDialog::Init()
89 {
90 m_isShown = false;
91 m_modalData = NULL;
92 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
93 m_dialogToolBar = NULL;
94 #endif
95 #if wxUSE_DIALOG_SIZEGRIP
96 m_hGripper = 0;
97 #endif // wxUSE_DIALOG_SIZEGRIP
98 }
99
100 bool wxDialog::Create(wxWindow *parent,
101 wxWindowID id,
102 const wxString& title,
103 const wxPoint& pos,
104 const wxSize& size,
105 long style,
106 const wxString& name)
107 {
108 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
109
110 // All dialogs should really have this style
111 style |= wxTAB_TRAVERSAL;
112
113 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
114 return false;
115
116 if ( !m_hasFont )
117 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
118
119 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
120 SetLeftMenu(wxID_OK, _("OK"));
121 #endif
122 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
123 CreateToolBar();
124 #endif
125
126 #if wxUSE_DIALOG_SIZEGRIP
127 if ( HasFlag(wxRESIZE_BORDER) )
128 {
129 CreateGripper();
130
131 Connect(wxEVT_CREATE,
132 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
133 }
134 #endif // wxUSE_DIALOG_SIZEGRIP
135
136 return true;
137 }
138
139 wxDialog::~wxDialog()
140 {
141 // this will also reenable all the other windows for a modal dialog
142 Show(false);
143
144 #if wxUSE_DIALOG_SIZEGRIP
145 DestroyGripper();
146 #endif // wxUSE_DIALOG_SIZEGRIP
147 }
148
149 // ----------------------------------------------------------------------------
150 // showing the dialogs
151 // ----------------------------------------------------------------------------
152
153 bool wxDialog::Show(bool show)
154 {
155 if ( show == IsShown() )
156 return false;
157
158 if ( !show && m_modalData )
159 {
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
162 // if they stay disabled Windows will activate another window (one
163 // which is enabled, anyhow) when we're hidden in the base class Show()
164 // and we will lose activation
165 m_modalData->ExitLoop();
166 }
167
168 if ( show )
169 {
170 if (CanDoLayoutAdaptation())
171 DoLayoutAdaptation();
172
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();
177 }
178
179 wxDialogBase::Show(show);
180
181 if ( show )
182 {
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
186 //
187 // NB: normally we should call it just the first time but doing it
188 // every time is simpler than keeping a flag
189 const wxSize size = GetClientSize();
190 ::SendMessage(GetHwnd(), WM_SIZE,
191 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
192 }
193
194 return true;
195 }
196
197 // show dialog modally
198 int wxDialog::ShowModal()
199 {
200 WX_HOOK_MODAL_DIALOG();
201
202 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
203
204 Show();
205
206 // EndModal may have been called from InitDialog handler (called from
207 // inside Show()) and hidden the dialog back again
208 if ( IsShown() )
209 {
210 // enter and run the modal loop
211 wxDialogModalDataTiedPtr modalData(&m_modalData,
212 new wxDialogModalData(this));
213 modalData->RunLoop();
214 }
215
216 return GetReturnCode();
217 }
218
219 void wxDialog::EndModal(int retCode)
220 {
221 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
222
223 SetReturnCode(retCode);
224
225 Hide();
226 }
227
228 // ----------------------------------------------------------------------------
229 // wxDialog gripper handling
230 // ----------------------------------------------------------------------------
231
232 #if wxUSE_DIALOG_SIZEGRIP
233
234 void wxDialog::SetWindowStyleFlag(long style)
235 {
236 wxDialogBase::SetWindowStyleFlag(style);
237
238 if ( HasFlag(wxRESIZE_BORDER) )
239 CreateGripper();
240 else
241 DestroyGripper();
242 }
243
244 void wxDialog::CreateGripper()
245 {
246 if ( !m_hGripper )
247 {
248 // just create it here, it will be positioned and shown later
249 m_hGripper = (WXHWND)::CreateWindow
250 (
251 wxT("SCROLLBAR"),
252 wxT(""),
253 WS_CHILD |
254 WS_CLIPSIBLINGS |
255 SBS_SIZEGRIP |
256 SBS_SIZEBOX |
257 SBS_SIZEBOXBOTTOMRIGHTALIGN,
258 0, 0, 0, 0,
259 GetHwnd(),
260 0,
261 wxGetInstance(),
262 NULL
263 );
264 }
265 }
266
267 void wxDialog::DestroyGripper()
268 {
269 if ( m_hGripper )
270 {
271 // we used to have trouble with gripper appearing on top (and hence
272 // overdrawing) the other, real, dialog children -- check that this
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() ||
277 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
278 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
279 ::DestroyWindow((HWND) m_hGripper);
280 m_hGripper = 0;
281 }
282 }
283
284 void wxDialog::ShowGripper(bool show)
285 {
286 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
287
288 if ( show )
289 ResizeGripper();
290
291 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
292 }
293
294 void wxDialog::ResizeGripper()
295 {
296 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
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
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
322 #endif // wxUSE_DIALOG_SIZEGRIP
323
324 // ----------------------------------------------------------------------------
325 // wxWin event handlers
326 // ----------------------------------------------------------------------------
327
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 {
334 const int idOk = GetAffirmativeId();
335 if ( EmulateButtonClickIfPresent(idOk) )
336 return true;
337
338 wxCommandEvent event(wxEVT_BUTTON, GetAffirmativeId());
339 event.SetEventObject(this);
340
341 return HandleWindowEvent(event);
342 }
343 #endif // __POCKETPC__
344
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);
362 }
363 #endif
364
365 // ---------------------------------------------------------------------------
366 // dialog Windows messages processing
367 // ---------------------------------------------------------------------------
368
369 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
370 {
371 WXLRESULT rc = 0;
372 bool processed = false;
373
374 switch ( message )
375 {
376 #ifdef __WXWINCE__
377 // react to pressing the OK button in the title
378 case WM_COMMAND:
379 {
380 switch ( LOWORD(wParam) )
381 {
382 #ifdef __POCKETPC__
383 case IDOK:
384 processed = DoOK();
385 if (!processed)
386 processed = !Close();
387 #endif
388 #ifdef __SMARTPHONE__
389 case IDM_LEFT:
390 case IDM_RIGHT:
391 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
392 break;
393 #endif // __SMARTPHONE__
394 }
395 break;
396 }
397 #endif
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;
403
404 case WM_SIZE:
405 #if wxUSE_DIALOG_SIZEGRIP
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);
416 }
417 }
418 #endif // wxUSE_DIALOG_SIZEGRIP
419
420 // the Windows dialogs unfortunately are not meant to be resizable
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);
427 processed = true;
428 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
429 {
430 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
431 }
432 break;
433
434 #ifndef __WXMICROWIN__
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
438 // is shown, but the modal dialog shouldn't have hourglass cursor
439 if ( IsModal() && wxIsBusy() )
440 {
441 // set our cursor for all windows (but see below)
442 wxCursor cursor = m_cursor;
443 if ( !cursor.IsOk() )
444 cursor = wxCURSOR_ARROW;
445
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)
450 processed = true;
451
452 // but return false to tell the child window (if the event
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()
457 rc = false;
458 }
459 break;
460 #endif // __WXMICROWIN__
461 }
462
463 if ( !processed )
464 rc = wxDialogBase::MSWWindowProc(message, wParam, lParam);
465
466 return rc;
467 }