fixed the problem with the ampersand [mis]handling in the generic wxMessageDialog
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 //--------------------------------------------------------------------------
45 // wxDialogBase
46 //--------------------------------------------------------------------------
47
48 // FIXME - temporary hack in absence of wxtopLevelWindow, should be always used
49 #ifdef wxTopLevelWindowNative
50 BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
51 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase)
52 END_EVENT_TABLE()
53
54 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase)
55 #endif
56
57 void wxDialogBase::Init()
58 {
59 m_returnCode = 0;
60 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
61 m_container.SetContainerWindow(this);
62 #endif
63 }
64
65 #if wxUSE_STATTEXT && wxUSE_TEXTCTRL
66
67 wxSizer *wxDialogBase::CreateTextSizer( const wxString& message )
68 {
69 wxBoxSizer *box = new wxBoxSizer( wxVERTICAL );
70
71 // get line height for empty lines
72 int y = 0;
73 wxFont font( GetFont() );
74 if (!font.Ok())
75 font = *wxSWISS_FONT;
76 GetTextExtent(_T("H"), (int*)NULL, &y, (int*)NULL, (int*)NULL, &font);
77
78 wxString line;
79 for ( size_t pos = 0; pos < message.length(); pos++ )
80 {
81 switch ( message[pos] )
82 {
83 case _T('\n'):
84 if (!line.IsEmpty())
85 {
86 wxStaticText *s1 = new wxStaticText( this, -1, line );
87 box->Add( s1 );
88 line = wxT("");
89 }
90 else
91 {
92 box->Add( 5, y );
93 }
94 break;
95
96 case _T('&'):
97 // this is used as accel mnemonic prefix in the wxWindows
98 // controls but in the static messages created by
99 // CreateTextSizer() (used by wxMessageBox, for example), we
100 // don't want this special meaning, so we need to quote it
101 line += _T('&');
102
103 // fall through to add it normally too
104
105 default:
106 line += message[pos];
107 }
108 }
109
110 // remaining text behind last '\n'
111 if (!line.IsEmpty())
112 {
113 wxStaticText *s2 = new wxStaticText( this, -1, line );
114 box->Add( s2 );
115 }
116
117 return box;
118 }
119
120 #endif // wxUSE_STATTEXT && wxUSE_TEXTCTRL
121
122 #if wxUSE_BUTTON
123
124 wxSizer *wxDialogBase::CreateButtonSizer( long flags )
125 {
126 wxBoxSizer *box = new wxBoxSizer( wxHORIZONTAL );
127
128 #if defined(__WXMSW__) || defined(__WXMAC__)
129 static const int margin = 6;
130 #else
131 static const int margin = 10;
132 #endif
133
134 wxButton *ok = (wxButton *) NULL;
135 wxButton *cancel = (wxButton *) NULL;
136 wxButton *yes = (wxButton *) NULL;
137 wxButton *no = (wxButton *) NULL;
138
139 // always show an OK button, unless only YES_NO is given
140 if ((flags & wxYES_NO) == 0) flags = flags | wxOK;
141
142 if (flags & wxYES_NO)
143 {
144 yes = new wxButton( this, wxID_YES, _("Yes"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS );
145 box->Add( yes, 0, wxLEFT|wxRIGHT, margin );
146 no = new wxButton( this, wxID_NO, _("No") ,wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS);
147 box->Add( no, 0, wxLEFT|wxRIGHT, margin );
148 } else
149 if (flags & wxYES)
150 {
151 yes = new wxButton( this, wxID_YES, _("Yes"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS );
152 box->Add( yes, 0, wxLEFT|wxRIGHT, margin );
153 } else
154 if (flags & wxNO)
155 {
156 no = new wxButton( this, wxID_NO, _("No"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS );
157 box->Add( no, 0, wxLEFT|wxRIGHT, margin );
158 }
159
160 if (flags & wxOK)
161 {
162 ok = new wxButton( this, wxID_OK, _("OK"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS );
163 box->Add( ok, 0, wxLEFT|wxRIGHT, margin );
164 }
165
166 if (flags & wxFORWARD)
167 box->Add( new wxButton( this, wxID_FORWARD, _("Forward"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
168
169 if (flags & wxBACKWARD)
170 box->Add( new wxButton( this, wxID_BACKWARD, _("Backward"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
171
172 if (flags & wxSETUP)
173 box->Add( new wxButton( this, wxID_SETUP, _("Setup"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
174
175 if (flags & wxMORE)
176 box->Add( new wxButton( this, wxID_MORE, _("More..."),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
177
178 if (flags & wxHELP)
179 box->Add( new wxButton( this, wxID_HELP, _("Help"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS ), 0, wxLEFT|wxRIGHT, margin );
180
181 if (flags & wxCANCEL)
182 {
183 cancel = new wxButton( this, wxID_CANCEL, _("Cancel"),wxDefaultPosition,wxDefaultSize,wxCLIP_SIBLINGS );
184 box->Add( cancel, 0, wxLEFT|wxRIGHT, margin );
185 }
186
187 if (flags & wxNO_DEFAULT)
188 {
189 if (no)
190 {
191 no->SetDefault();
192 no->SetFocus();
193 }
194 }
195 else
196 {
197 if (ok)
198 {
199 ok->SetDefault();
200 ok->SetFocus();
201 }
202 else if (yes)
203 {
204 yes->SetDefault();
205 yes->SetFocus();
206 }
207 }
208
209 return box;
210 }
211
212 #endif // wxUSE_BUTTON