]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
removed support for Salford compiler (which was almost certainly broken anyhow) ...
[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_oldFocus = (wxWindow *)NULL;
149 m_isShown = false;
150 m_modalData = NULL;
151 m_endModalCalled = false;
152 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
153 m_dialogToolBar = NULL;
154 #endif
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 // save focus before doing anything which can potentially change it
168 m_oldFocus = FindFocus();
169
170 // All dialogs should really have this style
171 style |= wxTAB_TRAVERSAL;
172
173 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
174 return false;
175
176 if ( !m_hasFont )
177 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
178
179 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
180 SetLeftMenu(wxID_OK, _("OK"));
181 #endif
182 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
183 CreateToolBar();
184 #endif
185
186 return true;
187 }
188
189 wxDialog::~wxDialog()
190 {
191 m_isBeingDeleted = true;
192
193 // this will also reenable all the other windows for a modal dialog
194 Show(false);
195 }
196
197 // ----------------------------------------------------------------------------
198 // showing the dialogs
199 // ----------------------------------------------------------------------------
200
201 wxWindow *wxDialog::FindSuitableParent() const
202 {
203 // first try to use the currently active window
204 HWND hwndFg = ::GetForegroundWindow();
205 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
206 : NULL;
207 if ( !parent )
208 {
209 // next try the main app window
210 parent = wxTheApp->GetTopWindow();
211 }
212
213 // finally, check if the parent we found is really suitable
214 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
215 {
216 // don't use this one
217 parent = NULL;
218 }
219
220 return parent;
221 }
222
223 bool wxDialog::Show(bool show)
224 {
225 if ( show == IsShown() )
226 return false;
227
228 if ( !show && m_modalData )
229 {
230 // we need to do this before calling wxDialogBase version because if we
231 // had disabled other app windows, they must be reenabled right now as
232 // if they stay disabled Windows will activate another window (one
233 // which is enabled, anyhow) when we're hidden in the base class Show()
234 // and we will lose activation
235 m_modalData->ExitLoop();
236 }
237
238 if ( show )
239 {
240 if (CanDoLayoutAdaptation())
241 DoLayoutAdaptation();
242
243 // this usually will result in TransferDataToWindow() being called
244 // which will change the controls values so do it before showing as
245 // otherwise we could have some flicker
246 InitDialog();
247 }
248
249 wxDialogBase::Show(show);
250
251 if ( show )
252 {
253 // dialogs don't get WM_SIZE message after creation unlike most (all?)
254 // other windows and so could start their life non laid out correctly
255 // if we didn't call Layout() from here
256 //
257 // NB: normally we should call it just the first time but doing it
258 // every time is simpler than keeping a flag
259 Layout();
260 }
261
262 return true;
263 }
264
265 void wxDialog::Raise()
266 {
267 ::SetForegroundWindow(GetHwnd());
268 }
269
270 // show dialog modally
271 int wxDialog::ShowModal()
272 {
273 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
274
275 m_endModalCalled = false;
276
277 Show();
278
279 // EndModal may have been called from InitDialog handler (called from
280 // inside Show()), which would cause an infinite loop if we didn't take it
281 // into account
282 if ( !m_endModalCalled )
283 {
284 // modal dialog needs a parent window, so try to find one
285 wxWindow *parent = GetParent();
286 if ( !parent )
287 {
288 parent = FindSuitableParent();
289 }
290
291 // remember where the focus was
292 wxWindow *oldFocus = m_oldFocus;
293 if ( !oldFocus )
294 {
295 // VZ: do we really want to do this?
296 oldFocus = parent;
297 }
298
299 // We have to remember the HWND because we need to check
300 // the HWND still exists (oldFocus can be garbage when the dialog
301 // exits, if it has been destroyed)
302 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
303
304
305 // enter and run the modal loop
306 {
307 wxDialogModalDataTiedPtr modalData(&m_modalData,
308 new wxDialogModalData(this));
309 modalData->RunLoop();
310 }
311
312
313 // and restore focus
314 // Note that this code MUST NOT access the dialog object's data
315 // in case the object has been deleted (which will be the case
316 // for a modal dialog that has been destroyed before calling EndModal).
317 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
318 {
319 // This is likely to prove that the object still exists
320 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
321 oldFocus->SetFocus();
322 }
323 }
324
325 return GetReturnCode();
326 }
327
328 void wxDialog::EndModal(int retCode)
329 {
330 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
331
332 m_endModalCalled = true;
333 SetReturnCode(retCode);
334
335 Hide();
336 }
337
338 // ----------------------------------------------------------------------------
339 // wxWin event handlers
340 // ----------------------------------------------------------------------------
341
342 #ifdef __POCKETPC__
343 // Responds to the OK button in a PocketPC titlebar. This
344 // can be overridden, or you can change the id used for
345 // sending the event, by calling SetAffirmativeId.
346 bool wxDialog::DoOK()
347 {
348 const int idOk = GetAffirmativeId();
349 if ( EmulateButtonClickIfPresent(idOk) )
350 return true;
351
352 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
353 event.SetEventObject(this);
354
355 return HandleWindowEvent(event);
356 }
357 #endif // __POCKETPC__
358
359 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
360 // create main toolbar by calling OnCreateToolBar()
361 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
362 {
363 m_dialogToolBar = OnCreateToolBar(style, winid, name);
364
365 return m_dialogToolBar;
366 }
367
368 // return a new toolbar
369 wxToolBar *wxDialog::OnCreateToolBar(long style,
370 wxWindowID winid,
371 const wxString& name)
372 {
373 return new wxToolMenuBar(this, winid,
374 wxDefaultPosition, wxDefaultSize,
375 style, name);
376 }
377 #endif
378
379 // ---------------------------------------------------------------------------
380 // dialog Windows messages processing
381 // ---------------------------------------------------------------------------
382
383 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
384 {
385 WXLRESULT rc = 0;
386 bool processed = false;
387
388 switch ( message )
389 {
390 #ifdef __WXWINCE__
391 // react to pressing the OK button in the title
392 case WM_COMMAND:
393 {
394 switch ( LOWORD(wParam) )
395 {
396 #ifdef __POCKETPC__
397 case IDOK:
398 processed = DoOK();
399 if (!processed)
400 processed = !Close();
401 #endif
402 #ifdef __SMARTPHONE__
403 case IDM_LEFT:
404 case IDM_RIGHT:
405 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
406 break;
407 #endif // __SMARTPHONE__
408 }
409 break;
410 }
411 #endif
412 case WM_CLOSE:
413 // if we can't close, tell the system that we processed the
414 // message - otherwise it would close us
415 processed = !Close();
416 break;
417
418 case WM_SIZE:
419 // the Windows dialogs unfortunately are not meant to be resizeable
420 // at all and their standard class doesn't include CS_[VH]REDRAW
421 // styles which means that the window is not refreshed properly
422 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
423 // help with it - so we have to refresh it manually which certainly
424 // creates flicker but at least doesn't show garbage on the screen
425 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
426 processed = true;
427 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
428 {
429 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
430 }
431 break;
432
433 #ifndef __WXMICROWIN__
434 case WM_SETCURSOR:
435 // we want to override the busy cursor for modal dialogs:
436 // typically, wxBeginBusyCursor() is called and then a modal dialog
437 // is shown, but the modal dialog shouldn't have hourglass cursor
438 if ( IsModal() && wxIsBusy() )
439 {
440 // set our cursor for all windows (but see below)
441 wxCursor cursor = m_cursor;
442 if ( !cursor.Ok() )
443 cursor = wxCURSOR_ARROW;
444
445 ::SetCursor(GetHcursorOf(cursor));
446
447 // in any case, stop here and don't let wxWindow process this
448 // message (it would set the busy cursor)
449 processed = true;
450
451 // but return false to tell the child window (if the event
452 // comes from one of them and not from ourselves) that it can
453 // set its own cursor if it has one: thus, standard controls
454 // (e.g. text ctrl) still have correct cursors in a dialog
455 // invoked while wxIsBusy()
456 rc = false;
457 }
458 break;
459 #endif // __WXMICROWIN__
460 }
461
462 if ( !processed )
463 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
464
465 return rc;
466 }