]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
fixing osx_cocoa
[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 // ----------------------------------------------------------------------------
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 void wxDialog::Raise()
198 {
199 ::SetForegroundWindow(GetHwnd());
200 }
201
202 // show dialog modally
203 int wxDialog::ShowModal()
204 {
205 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
206
207 Show();
208
209 // EndModal may have been called from InitDialog handler (called from
210 // inside Show()) and hidden the dialog back again
211 if ( IsShown() )
212 {
213 // enter and run the modal loop
214 wxDialogModalDataTiedPtr modalData(&m_modalData,
215 new wxDialogModalData(this));
216 modalData->RunLoop();
217 }
218
219 return GetReturnCode();
220 }
221
222 void wxDialog::EndModal(int retCode)
223 {
224 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
225
226 SetReturnCode(retCode);
227
228 Hide();
229 }
230
231 // ----------------------------------------------------------------------------
232 // wxDialog gripper handling
233 // ----------------------------------------------------------------------------
234
235 #if wxUSE_DIALOG_SIZEGRIP
236
237 void wxDialog::SetWindowStyleFlag(long style)
238 {
239 wxDialogBase::SetWindowStyleFlag(style);
240
241 if ( HasFlag(wxRESIZE_BORDER) )
242 CreateGripper();
243 else
244 DestroyGripper();
245 }
246
247 void wxDialog::CreateGripper()
248 {
249 if ( !m_hGripper )
250 {
251 // just create it here, it will be positioned and shown later
252 m_hGripper = (WXHWND)::CreateWindow
253 (
254 wxT("SCROLLBAR"),
255 wxT(""),
256 WS_CHILD |
257 WS_CLIPSIBLINGS |
258 SBS_SIZEGRIP |
259 SBS_SIZEBOX |
260 SBS_SIZEBOXBOTTOMRIGHTALIGN,
261 0, 0, 0, 0,
262 GetHwnd(),
263 0,
264 wxGetInstance(),
265 NULL
266 );
267 }
268 }
269
270 void wxDialog::DestroyGripper()
271 {
272 if ( m_hGripper )
273 {
274 // we used to have trouble with gripper appearing on top (and hence
275 // overdrawing) the other, real, dialog children -- check that this
276 // isn't the case automatically (but notice that this could be false if
277 // we're not shown at all as in this case ResizeGripper() might not
278 // have been called yet)
279 wxASSERT_MSG( !IsShown() ||
280 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
281 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
282 ::DestroyWindow((HWND) m_hGripper);
283 m_hGripper = 0;
284 }
285 }
286
287 void wxDialog::ShowGripper(bool show)
288 {
289 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
290
291 if ( show )
292 ResizeGripper();
293
294 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
295 }
296
297 void wxDialog::ResizeGripper()
298 {
299 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
300
301 HWND hwndGripper = (HWND)m_hGripper;
302
303 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
304 const wxSize size = GetClientSize() - rectGripper.GetSize();
305
306 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
307 size.x, size.y,
308 rectGripper.width, rectGripper.height,
309 SWP_NOACTIVATE);
310 }
311
312 void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
313 {
314 if ( m_hGripper && IsShown() &&
315 event.GetWindow() && event.GetWindow()->GetParent() == this )
316 {
317 // Put gripper below the newly created child window
318 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
319 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
320 }
321
322 event.Skip();
323 }
324
325 #endif // wxUSE_DIALOG_SIZEGRIP
326
327 // ----------------------------------------------------------------------------
328 // wxWin event handlers
329 // ----------------------------------------------------------------------------
330
331 #ifdef __POCKETPC__
332 // Responds to the OK button in a PocketPC titlebar. This
333 // can be overridden, or you can change the id used for
334 // sending the event, by calling SetAffirmativeId.
335 bool wxDialog::DoOK()
336 {
337 const int idOk = GetAffirmativeId();
338 if ( EmulateButtonClickIfPresent(idOk) )
339 return true;
340
341 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
342 event.SetEventObject(this);
343
344 return HandleWindowEvent(event);
345 }
346 #endif // __POCKETPC__
347
348 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
349 // create main toolbar by calling OnCreateToolBar()
350 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
351 {
352 m_dialogToolBar = OnCreateToolBar(style, winid, name);
353
354 return m_dialogToolBar;
355 }
356
357 // return a new toolbar
358 wxToolBar *wxDialog::OnCreateToolBar(long style,
359 wxWindowID winid,
360 const wxString& name)
361 {
362 return new wxToolMenuBar(this, winid,
363 wxDefaultPosition, wxDefaultSize,
364 style, name);
365 }
366 #endif
367
368 // ---------------------------------------------------------------------------
369 // dialog Windows messages processing
370 // ---------------------------------------------------------------------------
371
372 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
373 {
374 WXLRESULT rc = 0;
375 bool processed = false;
376
377 switch ( message )
378 {
379 #ifdef __WXWINCE__
380 // react to pressing the OK button in the title
381 case WM_COMMAND:
382 {
383 switch ( LOWORD(wParam) )
384 {
385 #ifdef __POCKETPC__
386 case IDOK:
387 processed = DoOK();
388 if (!processed)
389 processed = !Close();
390 #endif
391 #ifdef __SMARTPHONE__
392 case IDM_LEFT:
393 case IDM_RIGHT:
394 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
395 break;
396 #endif // __SMARTPHONE__
397 }
398 break;
399 }
400 #endif
401 case WM_CLOSE:
402 // if we can't close, tell the system that we processed the
403 // message - otherwise it would close us
404 processed = !Close();
405 break;
406
407 case WM_SIZE:
408 #if wxUSE_DIALOG_SIZEGRIP
409 if ( m_hGripper )
410 {
411 switch ( wParam )
412 {
413 case SIZE_MAXIMIZED:
414 ShowGripper(false);
415 break;
416
417 case SIZE_RESTORED:
418 ShowGripper(true);
419 }
420 }
421 #endif // wxUSE_DIALOG_SIZEGRIP
422
423 // the Windows dialogs unfortunately are not meant to be resizable
424 // at all and their standard class doesn't include CS_[VH]REDRAW
425 // styles which means that the window is not refreshed properly
426 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
427 // help with it - so we have to refresh it manually which certainly
428 // creates flicker but at least doesn't show garbage on the screen
429 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
430 processed = true;
431 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
432 {
433 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
434 }
435 break;
436
437 #ifndef __WXMICROWIN__
438 case WM_SETCURSOR:
439 // we want to override the busy cursor for modal dialogs:
440 // typically, wxBeginBusyCursor() is called and then a modal dialog
441 // is shown, but the modal dialog shouldn't have hourglass cursor
442 if ( IsModal() && wxIsBusy() )
443 {
444 // set our cursor for all windows (but see below)
445 wxCursor cursor = m_cursor;
446 if ( !cursor.IsOk() )
447 cursor = wxCURSOR_ARROW;
448
449 ::SetCursor(GetHcursorOf(cursor));
450
451 // in any case, stop here and don't let wxWindow process this
452 // message (it would set the busy cursor)
453 processed = true;
454
455 // but return false to tell the child window (if the event
456 // comes from one of them and not from ourselves) that it can
457 // set its own cursor if it has one: thus, standard controls
458 // (e.g. text ctrl) still have correct cursors in a dialog
459 // invoked while wxIsBusy()
460 rc = false;
461 }
462 break;
463 #endif // __WXMICROWIN__
464 }
465
466 if ( !processed )
467 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
468
469 return rc;
470 }