use stock buttons in generic dialogs
[wxWidgets.git] / src / common / dlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/dlgcmn.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dialogbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/button.h"
33 #include "wx/dialog.h"
34 #include "wx/dcclient.h"
35 #include "wx/intl.h"
36 #include "wx/settings.h"
37 #include "wx/stattext.h"
38 #include "wx/sizer.h"
39 #include "wx/button.h"
40 #include "wx/containr.h"
41 #endif
42
43 //--------------------------------------------------------------------------
44 // wxDialogBase
45 //--------------------------------------------------------------------------
46
47 // FIXME - temporary hack in absence of wxtopLevelWindow, should be always used
48 #ifdef wxTopLevelWindowNative
49 BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
50 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase)
51 END_EVENT_TABLE()
52
53 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase)
54 #endif
55
56 void wxDialogBase::Init()
57 {
58 m_returnCode = 0;
59
60 // the dialogs have this flag on by default to prevent the events from the
61 // dialog controls from reaching the parent frame which is usually
62 // undesirable and can lead to unexpected and hard to find bugs
63 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
64
65 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
66 m_container.SetContainerWindow(this);
67 #endif
68 }
69
70 #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
71
72 wxSizer *wxDialogBase::CreateTextSizer( const wxString& message )
73 {
74 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
75
76 wxString text = message;
77
78 // I admit that this is complete bogus, but it makes
79 // message boxes work for pda screens temporarily..
80 int max_width = -1;
81 if (is_pda)
82 {
83 max_width = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
84 text += wxT('\n');
85 }
86
87
88 wxBoxSizer *box = new wxBoxSizer( wxVERTICAL );
89
90 // get line height for empty lines
91 int y = 0;
92 wxFont font( GetFont() );
93 if (!font.Ok())
94 font = *wxSWISS_FONT;
95 GetTextExtent( wxT("H"), (int*)NULL, &y, (int*)NULL, (int*)NULL, &font);
96
97 size_t last_space = 0;
98 wxString line;
99 for ( size_t pos = 0; pos < text.length(); pos++ )
100 {
101 switch ( text[pos] )
102 {
103 case wxT('\n'):
104 if (!line.IsEmpty())
105 {
106 wxStaticText *s = new wxStaticText( this, -1, line );
107 box->Add( s );
108 line = wxT("");
109 }
110 else
111 {
112 box->Add( 5, y );
113 }
114 break;
115
116 case wxT('&'):
117 // this is used as accel mnemonic prefix in the wxWidgets
118 // controls but in the static messages created by
119 // CreateTextSizer() (used by wxMessageBox, for example), we
120 // don't want this special meaning, so we need to quote it
121 line += wxT('&');
122
123 // fall through to add it normally too
124
125 default:
126 if (text[pos] == wxT(' '))
127 last_space = pos;
128
129 line += message[pos];
130
131 if (is_pda)
132 {
133 int width = 0;
134 GetTextExtent( line, &width, (int*)NULL, (int*)NULL, (int*)NULL, &font );
135
136 if (width > max_width)
137 {
138 // exception if there was no previous space
139 if (last_space == 0)
140 last_space = pos;
141
142 int diff = pos-last_space;
143 int len = line.Len();
144 line.Remove( len-diff, diff );
145
146 wxStaticText *s = new wxStaticText( this, -1, line );
147 box->Add( s );
148
149 pos = last_space;
150 last_space = 0;
151 line = wxT("");
152 }
153 }
154 }
155 }
156
157 // remaining text behind last '\n'
158 if (!line.IsEmpty())
159 {
160 wxStaticText *s2 = new wxStaticText( this, -1, line );
161 box->Add( s2 );
162 }
163
164 return box;
165 }
166
167 #endif // wxUSE_STATTEXT // && wxUSE_TEXTCTRL
168
169 #if wxUSE_BUTTON
170
171 wxSizer *wxDialogBase::CreateButtonSizer( long flags )
172 {
173 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
174
175 // If we have a PDA screen, put yes/no button over
176 // all other buttons, otherwise on the left side.
177 wxBoxSizer *box = is_pda ? new wxBoxSizer( wxVERTICAL ) : new wxBoxSizer( wxHORIZONTAL );
178
179 wxBoxSizer *inner_yes_no = NULL;
180
181 // Only create sizer containing yes/no
182 // if it is actually required
183 if ( (flags & wxYES_NO) != 0 )
184 {
185 inner_yes_no = new wxBoxSizer( wxHORIZONTAL );
186 box->Add( inner_yes_no, 0, wxBOTTOM, 10 );
187 }
188
189 wxBoxSizer *inner_rest = new wxBoxSizer( wxHORIZONTAL );
190 box->Add( inner_rest, 0, 0, 0 );
191
192 #if defined(__WXMSW__) || defined(__WXMAC__)
193 static const int margin = 6;
194 #else
195 static const int margin = 10;
196 #endif
197
198 wxButton *ok = (wxButton *) NULL;
199 wxButton *yes = (wxButton *) NULL;
200 wxButton *no = (wxButton *) NULL;
201
202 // always show an OK button, unless we have both YES and NO
203 if ( (flags & wxYES_NO) != wxYES_NO )
204 flags |= wxOK;
205
206 if (flags & wxYES)
207 {
208 yes = new wxButton(this, wxID_YES, wxSTOCK_YES,
209 wxEmptyString, wxDefaultPosition, wxCLIP_SIBLINGS);
210 inner_yes_no->Add( yes, 0, wxLEFT|wxRIGHT, margin );
211 }
212 if (flags & wxNO)
213 {
214 no = new wxButton(this, wxID_NO, wxSTOCK_NO,
215 wxEmptyString, wxDefaultPosition, wxCLIP_SIBLINGS);
216 inner_yes_no->Add( no, 0, wxLEFT|wxRIGHT, margin );
217 }
218
219 if (flags & wxOK)
220 {
221 ok = new wxButton(this, wxID_OK, wxSTOCK_OK,
222 wxEmptyString, wxDefaultPosition, wxCLIP_SIBLINGS);
223 inner_rest->Add( ok, 0, wxLEFT|wxRIGHT, margin );
224 }
225
226 if (flags & wxFORWARD)
227 inner_rest->Add(new wxButton(this, wxID_FORWARD, wxSTOCK_GO_FORWARD,
228 wxEmptyString, wxDefaultPosition,
229 wxCLIP_SIBLINGS),
230 0, wxLEFT|wxRIGHT, margin);
231
232 if (flags & wxBACKWARD)
233 inner_rest->Add(new wxButton(this, wxID_BACKWARD, wxSTOCK_GO_BACK,
234 wxEmptyString, wxDefaultPosition,
235 wxCLIP_SIBLINGS),
236 0, wxLEFT|wxRIGHT, margin);
237
238 if (flags & wxSETUP)
239 inner_rest->Add( new wxButton( this, wxID_SETUP, _("Setup"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
240
241 if (flags & wxMORE)
242 inner_rest->Add( new wxButton( this, wxID_MORE, _("More..."),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
243
244 if (flags & wxHELP)
245 inner_rest->Add(new wxButton(this, wxID_HELP, wxSTOCK_HELP,
246 wxEmptyString, wxDefaultPosition,
247 wxCLIP_SIBLINGS),
248 0, wxLEFT|wxRIGHT, margin);
249
250 if (flags & wxCANCEL)
251 {
252 wxButton *cancel = new wxButton(this, wxID_CANCEL, wxSTOCK_CANCEL,
253 wxEmptyString, wxDefaultPosition,
254 wxCLIP_SIBLINGS);
255 inner_rest->Add( cancel, 0, wxLEFT|wxRIGHT, margin );
256 }
257
258 // choose the default button
259 if (flags & wxNO_DEFAULT)
260 {
261 if (no)
262 {
263 no->SetDefault();
264 no->SetFocus();
265 }
266 }
267 else
268 {
269 if (ok)
270 {
271 ok->SetDefault();
272 ok->SetFocus();
273 }
274 else if (yes)
275 {
276 yes->SetDefault();
277 yes->SetFocus();
278 }
279 }
280
281 return box;
282 }
283
284 #endif // wxUSE_BUTTON