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