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