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