]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: msgdlgg.cpp | |
3 | // Purpose: wxGenericMessageDialog | |
15b24b14 | 4 | // Author: Julian Smart, Robert Roebling |
c801d85f KB |
5 | // Modified by: |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
15b24b14 | 8 | // Copyright: (c) Julian Smart, Markus Holzem, Robert Roebling |
f03fc89f | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "msgdlgg.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
dcf924a3 RR |
24 | #include "wx/utils.h" |
25 | #include "wx/dialog.h" | |
26 | #include "wx/button.h" | |
27 | #include "wx/stattext.h" | |
917b3d40 VZ |
28 | #include "wx/statbmp.h" |
29 | #include "wx/layout.h" | |
dcf924a3 | 30 | #include "wx/intl.h" |
917b3d40 VZ |
31 | #include "wx/dcclient.h" |
32 | #include "wx/settings.h" | |
c801d85f KB |
33 | #endif |
34 | ||
35 | #include <stdio.h> | |
36 | #include <string.h> | |
37 | ||
38 | #include "wx/generic/msgdlgg.h" | |
39 | ||
dcf924a3 RR |
40 | #if wxUSE_STATLINE |
41 | #include "wx/statline.h" | |
b0351fc9 RR |
42 | #endif |
43 | ||
917b3d40 VZ |
44 | // ---------------------------------------------------------------------------- |
45 | // icons | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | // MSW icons are in the ressources, for all other platforms - in XPM files | |
49 | #ifndef __WXMSW__ | |
50 | #include "wx/generic/info.xpm" | |
51 | #include "wx/generic/question.xpm" | |
52 | #include "wx/generic/warning.xpm" | |
53 | #include "wx/generic/error.xpm" | |
54 | #endif // __WXMSW__ | |
55 | ||
c801d85f KB |
56 | |
57 | #if !USE_SHARED_LIBRARY | |
58 | BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog) | |
f03fc89f VZ |
59 | EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes) |
60 | EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo) | |
61 | EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel) | |
c801d85f KB |
62 | END_EVENT_TABLE() |
63 | ||
64 | IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog) | |
65 | #endif | |
66 | ||
917b3d40 VZ |
67 | wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent, |
68 | const wxString& message, | |
69 | const wxString& caption, | |
70 | long style, | |
71 | const wxPoint& pos) | |
72 | : wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE ) | |
c801d85f | 73 | { |
917b3d40 VZ |
74 | static const int LAYOUT_X_MARGIN = 5; |
75 | static const int LAYOUT_Y_MARGIN = 5; | |
76 | ||
dfc54541 | 77 | m_dialogStyle = style; |
c801d85f | 78 | |
dcf924a3 RR |
79 | wxBeginBusyCursor(); |
80 | ||
917b3d40 VZ |
81 | wxLayoutConstraints *c; |
82 | SetAutoLayout(TRUE); | |
c801d85f | 83 | |
917b3d40 VZ |
84 | // create an icon |
85 | enum | |
15b24b14 | 86 | { |
917b3d40 VZ |
87 | Icon_Information, |
88 | Icon_Question, | |
89 | Icon_Warning, | |
90 | Icon_Error | |
91 | } which; | |
dfc54541 | 92 | |
917b3d40 VZ |
93 | #ifdef __WXMSW__ |
94 | static char *icons[] = | |
15b24b14 | 95 | { |
917b3d40 VZ |
96 | "wxICON_INFO", |
97 | "wxICON_QUESTION", | |
98 | "wxICON_WARNING", | |
99 | "wxICON_ERROR", | |
100 | }; | |
101 | #else // XPM icons | |
102 | static char **icons[] = | |
15b24b14 | 103 | { |
917b3d40 VZ |
104 | info, |
105 | question, | |
106 | warning, | |
107 | error, | |
108 | }; | |
109 | #endif // !XPM/XPM | |
dfc54541 | 110 | |
917b3d40 VZ |
111 | if ( style & wxICON_EXCLAMATION ) |
112 | which = Icon_Warning; | |
113 | else if ( style & wxICON_HAND ) | |
114 | which = Icon_Error; | |
115 | else if ( style & wxICON_QUESTION ) | |
116 | which = Icon_Question; | |
117 | else | |
118 | which = Icon_Information; | |
119 | ||
120 | wxStaticBitmap *icon = new wxStaticBitmap(this, -1, wxIcon(icons[which])); | |
121 | const int iconSize = icon->GetBitmap().GetWidth(); | |
122 | ||
123 | // split the message in lines | |
124 | // -------------------------- | |
125 | wxClientDC dc(this); | |
126 | dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT)); | |
127 | ||
128 | wxArrayString lines; | |
129 | wxString curLine; | |
130 | long height, width, heightTextMax = 0, widthTextMax = 0; | |
131 | for ( const char *pc = message; ; pc++ ) { | |
132 | if ( *pc == '\n' || *pc == '\0' ) { | |
133 | dc.GetTextExtent(curLine, &width, &height); | |
134 | if ( width > widthTextMax ) | |
135 | widthTextMax = width; | |
136 | if ( height > heightTextMax ) | |
137 | heightTextMax = height; | |
138 | ||
139 | lines.Add(curLine); | |
140 | ||
141 | if ( *pc == '\n' ) { | |
142 | curLine.Empty(); | |
143 | } | |
144 | else { | |
145 | // the end of string | |
146 | break; | |
147 | } | |
148 | } | |
149 | else { | |
150 | curLine += *pc; | |
151 | } | |
dfc54541 | 152 | } |
917b3d40 VZ |
153 | |
154 | // calculate the total dialog size | |
155 | enum | |
dfc54541 | 156 | { |
917b3d40 VZ |
157 | Btn_Ok, |
158 | Btn_Yes, | |
159 | Btn_No, | |
160 | Btn_Cancel, | |
161 | Btn_Max | |
162 | }; | |
163 | wxButton *buttons[Btn_Max] = { NULL, NULL, NULL, NULL }; | |
164 | int nDefaultBtn = -1; | |
165 | ||
166 | // some checks are in order... | |
167 | wxASSERT_MSG( !(style & wxOK) || !(style & wxYES_NO), | |
168 | "don't create dialog with both Yes/No and Ok buttons!" ); | |
169 | ||
170 | wxASSERT_MSG( (style & wxOK ) || (style & wxYES_NO), | |
171 | "don't create dialog with only the Cancel button!" ); | |
172 | ||
173 | if ( style & wxYES_NO ) { | |
174 | buttons[Btn_Yes] = new wxButton(this, wxID_YES, _("Yes")); | |
175 | buttons[Btn_No] = new wxButton(this, wxID_NO, _("No")); | |
176 | ||
177 | ||
96c5bd7f | 178 | if(style & wxNO_DEFAULT) |
917b3d40 | 179 | nDefaultBtn = Btn_No; |
96c5bd7f | 180 | else |
917b3d40 | 181 | nDefaultBtn = Btn_Yes; |
dfc54541 | 182 | } |
917b3d40 VZ |
183 | |
184 | if (style & wxOK) { | |
185 | buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK")); | |
186 | ||
187 | if ( nDefaultBtn == -1 ) | |
188 | nDefaultBtn = Btn_Ok; | |
15b24b14 | 189 | } |
c801d85f | 190 | |
917b3d40 VZ |
191 | if (style & wxCANCEL) { |
192 | buttons[Btn_Cancel] = new wxButton(this, wxID_CANCEL, _("Cancel")); | |
193 | } | |
194 | ||
195 | // get the longest caption and also calc the number of buttons | |
196 | size_t nBtn, nButtons = 0; | |
197 | long widthBtnMax = 0; | |
198 | for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) { | |
199 | if ( buttons[nBtn] ) { | |
200 | nButtons++; | |
201 | dc.GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL); | |
202 | if ( width > widthBtnMax ) | |
203 | widthBtnMax = width; | |
204 | } | |
205 | } | |
206 | ||
207 | // now we can place the buttons | |
208 | if ( widthBtnMax < 75 ) | |
209 | widthBtnMax = 75; | |
210 | else | |
211 | widthBtnMax += 10; | |
212 | long heightButton = widthBtnMax*23/75; | |
213 | ||
214 | // *1.2 baselineskip | |
215 | heightTextMax *= 12; | |
216 | heightTextMax /= 10; | |
217 | ||
218 | size_t nLineCount = lines.Count(); | |
219 | ||
220 | long widthButtonsTotal = nButtons * (widthBtnMax + LAYOUT_X_MARGIN) - | |
221 | LAYOUT_X_MARGIN; | |
222 | ||
223 | // the size of the dialog | |
224 | long widthDlg = wxMax(widthTextMax + iconSize + 4*LAYOUT_X_MARGIN, | |
225 | wxMax(widthButtonsTotal, width)) + | |
226 | 2*LAYOUT_X_MARGIN, | |
227 | heightDlg = 8*LAYOUT_Y_MARGIN + heightButton + | |
228 | heightTextMax*(nLineCount + 1); | |
229 | ||
230 | // create the controls | |
231 | // ------------------- | |
232 | ||
233 | // the icon first | |
234 | c = new wxLayoutConstraints; | |
235 | c->width.Absolute(iconSize); | |
236 | c->height.Absolute(iconSize); | |
237 | c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN); | |
238 | c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN); | |
239 | icon->SetConstraints(c); | |
240 | ||
241 | wxStaticText *text = NULL; | |
242 | for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) { | |
243 | c = new wxLayoutConstraints; | |
244 | if ( text == NULL ) | |
245 | c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN); | |
246 | else | |
247 | c->top.Below(text); | |
248 | ||
249 | c->left.RightOf(icon, 2*LAYOUT_X_MARGIN); | |
250 | c->width.Absolute(widthTextMax); | |
251 | c->height.Absolute(heightTextMax); | |
252 | text = new wxStaticText(this, -1, lines[nLine]); | |
253 | text->SetConstraints(c); | |
254 | } | |
255 | ||
256 | // create the buttons | |
257 | wxButton *btnPrevious = (wxButton *)NULL; | |
258 | for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) { | |
259 | if ( buttons[nBtn] ) { | |
260 | c = new wxLayoutConstraints; | |
261 | ||
262 | if ( btnPrevious ) { | |
263 | c->left.RightOf(btnPrevious, LAYOUT_X_MARGIN); | |
264 | } | |
265 | else { | |
266 | c->left.SameAs(this, wxLeft, | |
267 | (widthDlg - widthButtonsTotal) / 2); | |
268 | } | |
269 | ||
270 | c->width.Absolute(widthBtnMax); | |
271 | c->top.Below(text, 4*LAYOUT_Y_MARGIN); | |
272 | c->height.Absolute(heightButton); | |
273 | buttons[nBtn]->SetConstraints(c); | |
274 | ||
275 | btnPrevious = buttons[nBtn]; | |
276 | } | |
277 | } | |
278 | ||
279 | // set default button | |
280 | // ------------------ | |
281 | ||
282 | if ( nDefaultBtn != -1 ) { | |
283 | buttons[nDefaultBtn]->SetDefault(); | |
284 | buttons[nDefaultBtn]->SetFocus(); | |
285 | } | |
286 | else { | |
287 | wxFAIL_MSG( "can't find default button for this dialog." ); | |
288 | } | |
289 | ||
290 | // position the controls and the dialog itself | |
291 | // ------------------------------------------- | |
292 | ||
293 | SetClientSize(widthDlg, heightDlg); | |
294 | ||
295 | // SetSizeHints() wants the size of the whole dialog, not just client size | |
296 | wxSize sizeTotal = GetSize(), | |
297 | sizeClient = GetClientSize(); | |
298 | SetSizeHints(widthDlg + sizeTotal.GetWidth() - sizeClient.GetWidth(), | |
299 | heightDlg + sizeTotal.GetHeight() - sizeClient.GetHeight()); | |
300 | ||
301 | Layout(); | |
302 | ||
303 | Centre(wxCENTER_FRAME | wxBOTH); | |
304 | ||
dcf924a3 | 305 | wxEndBusyCursor(); |
c801d85f KB |
306 | } |
307 | ||
308 | void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event)) | |
309 | { | |
15b24b14 | 310 | EndModal( wxID_YES ); |
c801d85f KB |
311 | } |
312 | ||
313 | void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event)) | |
314 | { | |
15b24b14 | 315 | EndModal( wxID_NO ); |
c801d85f KB |
316 | } |
317 | ||
318 | void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
319 | { | |
15b24b14 RR |
320 | /* Allow cancellation via ESC/Close button except if |
321 | only YES and NO are specified. */ | |
dfc54541 | 322 | if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) ) |
15b24b14 RR |
323 | { |
324 | EndModal( wxID_CANCEL ); | |
325 | } | |
c801d85f KB |
326 | } |
327 | ||
328 |