]> git.saurik.com Git - wxWidgets.git/blame - src/common/dlgcmn.cpp
add wxUSE_DATAVIEWCTRL check to fix a hundred compilation errors
[wxWidgets.git] / src / common / dlgcmn.cpp
CommitLineData
e37feda2 1/////////////////////////////////////////////////////////////////////////////
f1e01716 2// Name: src/common/dlgcmn.cpp
e37feda2
VZ
3// Purpose: common (to all ports) wxDialog functions
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 28.06.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
65571936 9// Licence: wxWindows licence
e37feda2
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
e37feda2
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
fdf565fe
WS
27#include "wx/dialog.h"
28
e37feda2 29#ifndef WX_PRECOMP
df57f6be 30 #include "wx/app.h"
f6bcfd97 31 #include "wx/button.h"
e37feda2 32 #include "wx/dcclient.h"
9f3a38fc 33 #include "wx/intl.h"
e37feda2 34 #include "wx/settings.h"
9f3a38fc 35 #include "wx/stattext.h"
92afa2b1 36 #include "wx/sizer.h"
7d9f12f3 37 #include "wx/containr.h"
e37feda2
VZ
38#endif
39
897b24cf
WS
40#include "wx/statline.h"
41#include "wx/sysopt.h"
39bc0347 42#include "wx/private/stattext.h"
897b24cf 43
5d1b4919
VZ
44
45// ----------------------------------------------------------------------------
92afa2b1 46// wxDialogBase
5d1b4919 47// ----------------------------------------------------------------------------
e37feda2 48
7d9f12f3 49BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
a9f620da 50 EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton)
2158f4d7
VZ
51
52 EVT_CLOSE(wxDialogBase::OnCloseWindow)
53
0be27418 54 EVT_CHAR_HOOK(wxDialogBase::OnCharHook)
7d9f12f3
VS
55END_EVENT_TABLE()
56
7d9f12f3
VS
57void wxDialogBase::Init()
58{
59 m_returnCode = 0;
9ceeecb9 60 m_affirmativeId = wxID_OK;
c6ece595
VZ
61 m_escapeId = wxID_ANY;
62
e4b713a2
VZ
63 // the dialogs have this flag on by default to prevent the events from the
64 // dialog controls from reaching the parent frame which is usually
65 // undesirable and can lead to unexpected and hard to find bugs
66 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
7d9f12f3
VS
67}
68
893f7840
VZ
69// helper of GetParentForModalDialog()
70static bool CanBeUsedAsParent(wxWindow *parent)
71{
72 extern WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
73
74 return !parent->HasExtraStyle(wxWS_EX_TRANSIENT) &&
75 parent->IsShownOnScreen() &&
76 !wxPendingDelete.Member(parent) &&
9dcf65d3 77 !parent->IsBeingDeleted();
893f7840
VZ
78}
79
2229243b
VZ
80wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const
81{
82 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
83 // in an unfocused dialog, so try to find a valid parent for it:
84 if ( parent )
85 parent = wxGetTopLevelParent(parent);
86
893f7840 87 if ( !parent || !CanBeUsedAsParent(parent) )
2229243b
VZ
88 parent = wxTheApp->GetTopWindow();
89
893f7840 90 if ( parent && !CanBeUsedAsParent(parent) )
2229243b
VZ
91 {
92 // can't use this one, it's going to disappear
93 parent = NULL;
94 }
95
96 return parent;
97}
98
5d1b4919 99#if wxUSE_STATTEXT
1e6feb95 100
cfd1ac21
MW
101class wxTextSizerWrapper : public wxTextWrapper
102{
103public:
104 wxTextSizerWrapper(wxWindow *win)
105 {
106 m_win = win;
107 m_hLine = 0;
108 }
109
110 wxSizer *CreateSizer(const wxString& text, int widthMax)
111 {
112 m_sizer = new wxBoxSizer(wxVERTICAL);
113 Wrap(m_win, text, widthMax);
114 return m_sizer;
115 }
116
117protected:
118 virtual void OnOutputLine(const wxString& line)
119 {
120 if ( !line.empty() )
121 {
122 m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line));
123 }
124 else // empty line, no need to create a control for it
125 {
126 if ( !m_hLine )
127 m_hLine = m_win->GetCharHeight();
128
129 m_sizer->Add(5, m_hLine);
130 }
131 }
132
133private:
134 wxWindow *m_win;
135 wxSizer *m_sizer;
136 int m_hLine;
137};
138
5d1b4919
VZ
139wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
140{
2b5f62a0
VZ
141 // I admit that this is complete bogus, but it makes
142 // message boxes work for pda screens temporarily..
5d1b4919
VZ
143 int widthMax = -1;
144 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
2b5f62a0
VZ
145 if (is_pda)
146 {
5d1b4919 147 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
2b5f62a0 148 }
68379eaf 149
5d1b4919
VZ
150 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
151 // the static messages created by CreateTextSizer() (used by wxMessageBox,
152 // for example), we don't want this special meaning, so we need to quote it
153 wxString text(message);
154 text.Replace(_T("&"), _T("&&"));
68379eaf 155
cfd1ac21 156 wxTextSizerWrapper wrapper(this);
b730516c 157
cfd1ac21
MW
158 return wrapper.CreateSizer(text, widthMax);
159}
5d1b4919 160
5d1b4919 161#endif // wxUSE_STATTEXT
1e6feb95 162
25eb10d2 163wxSizer *wxDialogBase::CreateButtonSizer(long flags)
e37feda2 164{
25eb10d2 165 wxSizer *sizer = NULL;
897b24cf 166
25eb10d2 167#ifdef __SMARTPHONE__
102c0454 168 wxDialog* dialog = (wxDialog*) this;
25eb10d2 169 if ( flags & wxOK )
102c0454 170 dialog->SetLeftMenu(wxID_OK);
102c0454 171
25eb10d2 172 if ( flags & wxCANCEL )
102c0454 173 dialog->SetRightMenu(wxID_CANCEL);
102c0454 174
25eb10d2 175 if ( flags & wxYES )
102c0454 176 dialog->SetLeftMenu(wxID_YES);
897b24cf 177
25eb10d2
VZ
178 if ( flags & wxNO )
179 dialog->SetRightMenu(wxID_NO);
897b24cf
WS
180#else // !__SMARTPHONE__
181
25eb10d2
VZ
182#if wxUSE_BUTTON
183
897b24cf 184#ifdef __POCKETPC__
25eb10d2
VZ
185 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
186 // located inside caption bar and implement Cancel functionality through
187 // Undo outside dialog. As native behaviour this will be default here but
188 // can be replaced with real wxButtons by setting the option below to 1
189 if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK ||
190 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
191#endif // __POCKETPC__
897b24cf 192 {
25eb10d2 193 sizer = CreateStdDialogButtonSizer(flags);
897b24cf 194 }
8d22935d
VZ
195#else // !wxUSE_BUTTON
196 wxUnusedVar(flags);
197#endif // wxUSE_BUTTON/!wxUSE_BUTTON
897b24cf 198
25eb10d2 199#endif // __SMARTPHONE__/!__SMARTPHONE__
897b24cf 200
25eb10d2
VZ
201 return sizer;
202}
897b24cf 203
25eb10d2
VZ
204wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags)
205{
206 wxSizer *sizer = CreateButtonSizer(flags);
207 if ( !sizer )
208 return NULL;
897b24cf 209
25eb10d2
VZ
210 // Mac Human Interface Guidelines recommend not to use static lines as
211 // grouping elements
212#if wxUSE_STATLINE && !defined(__WXMAC__)
213 wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
214 topsizer->Add(new wxStaticLine(this),
215 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM));
216 topsizer->Add(sizer, wxSizerFlags().Expand());
217 sizer = topsizer;
218#endif // wxUSE_STATLINE
897b24cf 219
897b24cf 220 return sizer;
acf2ac37 221}
68379eaf 222
897b24cf
WS
223#if wxUSE_BUTTON
224
acf2ac37
RR
225wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
226{
227 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
897b24cf 228
acf2ac37 229 wxButton *ok = NULL;
acf2ac37
RR
230 wxButton *yes = NULL;
231 wxButton *no = NULL;
52069700 232
25eb10d2
VZ
233 if (flags & wxOK)
234 {
331c1816 235 ok = new wxButton(this, wxID_OK);
52069700 236 sizer->AddButton(ok);
2b5f62a0 237 }
52069700 238
25eb10d2
VZ
239 if (flags & wxCANCEL)
240 {
331c1816 241 wxButton *cancel = new wxButton(this, wxID_CANCEL);
52069700 242 sizer->AddButton(cancel);
b5b49e42 243 }
52069700 244
25eb10d2
VZ
245 if (flags & wxYES)
246 {
331c1816 247 yes = new wxButton(this, wxID_YES);
52069700 248 sizer->AddButton(yes);
e37feda2 249 }
52069700 250
25eb10d2
VZ
251 if (flags & wxNO)
252 {
331c1816 253 no = new wxButton(this, wxID_NO);
52069700 254 sizer->AddButton(no);
e37feda2 255 }
52069700 256
57d7f988
VZ
257 if (flags & wxAPPLY)
258 {
259 wxButton *apply = new wxButton(this, wxID_APPLY);
260 sizer->AddButton(apply);
261 }
262
263 if (flags & wxCLOSE)
264 {
265 wxButton *close = new wxButton(this, wxID_CLOSE);
266 sizer->AddButton(close);
267 }
268
25eb10d2
VZ
269 if (flags & wxHELP)
270 {
331c1816 271 wxButton *help = new wxButton(this, wxID_HELP);
52069700 272 sizer->AddButton(help);
e37feda2 273 }
52069700 274
b730516c
RD
275 if (flags & wxNO_DEFAULT)
276 {
277 if (no)
278 {
279 no->SetDefault();
280 no->SetFocus();
281 }
282 }
283 else
92afa2b1
RR
284 {
285 if (ok)
286 {
287 ok->SetDefault();
288 ok->SetFocus();
289 }
290 else if (yes)
291 {
292 yes->SetDefault();
293 yes->SetFocus();
294 }
295 }
d30814cd 296
9ceeecb9
JS
297 if (flags & wxOK)
298 SetAffirmativeId(wxID_OK);
299 else if (flags & wxYES)
300 SetAffirmativeId(wxID_YES);
b730516c 301
d30814cd
MB
302 sizer->Realize();
303
acf2ac37 304 return sizer;
e37feda2
VZ
305}
306
1e6feb95 307#endif // wxUSE_BUTTON
0be27418 308
551f281b 309// ----------------------------------------------------------------------------
a9f620da 310// standard buttons handling
551f281b
VZ
311// ----------------------------------------------------------------------------
312
a9f620da
VZ
313void wxDialogBase::EndDialog(int rc)
314{
315 if ( IsModal() )
316 EndModal(rc);
317 else
318 Hide();
319}
320
551f281b
VZ
321void wxDialogBase::AcceptAndClose()
322{
323 if ( Validate() && TransferDataFromWindow() )
324 {
a9f620da 325 EndDialog(m_affirmativeId);
551f281b
VZ
326 }
327}
328
329void wxDialogBase::SetAffirmativeId(int affirmativeId)
330{
fabd7a7f 331 m_affirmativeId = affirmativeId;
551f281b
VZ
332}
333
334void wxDialogBase::SetEscapeId(int escapeId)
335{
fabd7a7f 336 m_escapeId = escapeId;
551f281b
VZ
337}
338
0be27418
VZ
339bool wxDialogBase::EmulateButtonClickIfPresent(int id)
340{
dbfa7f33 341#if wxUSE_BUTTON
0be27418
VZ
342 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
343
344 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
345 return false;
346
347 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id);
348 event.SetEventObject(btn);
349 btn->GetEventHandler()->ProcessEvent(event);
350
351 return true;
dbfa7f33
VS
352#else // !wxUSE_BUTTON
353 wxUnusedVar(id);
354 return false;
355#endif // wxUSE_BUTTON/!wxUSE_BUTTON
0be27418
VZ
356}
357
358bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event)
359{
360 // for most platforms, Esc key is used to close the dialogs
361 return event.GetKeyCode() == WXK_ESCAPE &&
362 event.GetModifiers() == wxMOD_NONE;
363}
364
365void wxDialogBase::OnCharHook(wxKeyEvent& event)
366{
367 if ( event.GetKeyCode() == WXK_ESCAPE )
368 {
369 int idCancel = GetEscapeId();
370 switch ( idCancel )
371 {
372 case wxID_NONE:
373 // don't handle Esc specially at all
374 break;
375
376 case wxID_ANY:
377 // this value is special: it means translate Esc to wxID_CANCEL
378 // but if there is no such button, then fall back to wxID_OK
379 if ( EmulateButtonClickIfPresent(wxID_CANCEL) )
380 return;
153a0b78 381 idCancel = GetAffirmativeId();
0be27418
VZ
382 // fall through
383
384 default:
385 // translate Esc to button press for the button with given id
386 if ( EmulateButtonClickIfPresent(idCancel) )
387 return;
388 }
389 }
390
391 event.Skip();
392}
393
a9f620da 394void wxDialogBase::OnButton(wxCommandEvent& event)
2158f4d7 395{
a9f620da
VZ
396 const int id = event.GetId();
397 if ( id == GetAffirmativeId() )
398 {
399 AcceptAndClose();
400 }
401 else if ( id == wxID_APPLY )
402 {
403 if ( Validate() )
404 TransferDataFromWindow();
2158f4d7 405
a9f620da
VZ
406 // TODO: disable the Apply button until things change again
407 }
408 else if ( id == GetEscapeId() ||
409 (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
410 {
411 EndDialog(wxID_CANCEL);
412 }
413 else // not a standard button
414 {
415 event.Skip();
416 }
2158f4d7
VZ
417}
418
a9f620da
VZ
419// ----------------------------------------------------------------------------
420// other event handlers
421// ----------------------------------------------------------------------------
2158f4d7
VZ
422
423void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
424{
425 // We'll send a Cancel message by default, which may close the dialog.
426 // Check for looping if the Cancel event handler calls Close().
427
428 // Note that if a cancel button and handler aren't present in the dialog,
429 // nothing will happen when you close the dialog via the window manager, or
430 // via Close(). We wouldn't want to destroy the dialog by default, since
431 // the dialog may have been created on the stack. However, this does mean
432 // that calling dialog->Close() won't delete the dialog unless the handler
433 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
434 // destroy the dialog. The default OnCancel (above) simply ends a modal
435 // dialog, and hides a modeless dialog.
436
437 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
438 // lists here? don't dare to change it now, but should be done later!
439 static wxList closing;
440
441 if ( closing.Member(this) )
442 return;
443
444 closing.Append(this);
445
446 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
447 cancelEvent.SetEventObject( this );
448 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
449
450 closing.DeleteObject(this);
451}
452
a45f904d 453void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2158f4d7 454{
a45f904d
JS
455#ifndef __WXGTK__
456 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
457 Refresh();
458#endif
459
460 event.Skip();
2158f4d7 461}