Made icons configurable via a wxMApp virtual function. Tested on wxGTK only,
[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/icon.h"
32 # include "wx/app.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
49
50 #if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
52 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
53 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
54 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
55 END_EVENT_TABLE()
56
57 IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
58 #endif
59
60 #ifdef _WXGTK__
61 # include "wx/gtk/info.xpm"
62 # include "wx/gtk/error.xpm"
63 # include "wx/gtk/question.xpm"
64 # include "wx/gtk/warning.xpm"
65 #else
66 // MSW icons are in the ressources, for all other platforms - in XPM files
67 # ifndef __WXMSW__
68 # include "wx/generic/info.xpm"
69 # include "wx/generic/question.xpm"
70 # include "wx/generic/warning.xpm"
71 # include "wx/generic/error.xpm"
72 # endif // __WXMSW__
73 #endif
74
75 wxIcon
76 wxApp::GetStdIcon(int which) const
77 {
78 switch(which)
79 {
80 case wxICON_INFORMATION:
81 return wxIcon(info_xpm);
82 break;
83 case wxICON_HAND:
84 return wxIcon(error_xpm);
85 break;
86 case wxICON_QUESTION:
87 return wxIcon(question_xpm);
88 break;
89 case wxICON_EXCLAMATION:
90 return wxIcon(warning_xpm);
91 break;
92 default:
93 wxFAIL_MSG("requested non existent standard icon");
94 return wxIcon(error_xpm);
95 break;
96 }
97 }
98
99
100 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
101 const wxString& message,
102 const wxString& caption,
103 long style,
104 const wxPoint& pos)
105 : wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE )
106 {
107 m_dialogStyle = style;
108
109 wxBeginBusyCursor();
110
111 wxLayoutConstraints *c;
112 SetAutoLayout(TRUE);
113
114 wxStaticBitmap *icon = new wxStaticBitmap(this, -1,
115 wxTheApp->GetStdIcon(style & wxICON_MASK));
116 const int iconSize = icon->GetBitmap().GetWidth();
117
118 // split the message in lines
119 // --------------------------
120
121 wxArrayString lines;
122 wxSize sizeText = SplitTextMessage(message, &lines);
123 long widthTextMax = sizeText.GetWidth(),
124 heightTextMax = sizeText.GetHeight();
125 size_t nLineCount = lines.GetCount();
126
127 // calculate the total dialog size
128 enum
129 {
130 Btn_Ok,
131 Btn_Yes,
132 Btn_No,
133 Btn_Cancel,
134 Btn_Max
135 };
136 wxButton *buttons[Btn_Max] = { NULL, NULL, NULL, NULL };
137 int nDefaultBtn = -1;
138
139 // some checks are in order...
140 wxASSERT_MSG( !(style & wxOK) || !(style & wxYES_NO),
141 "don't create dialog with both Yes/No and Ok buttons!" );
142
143 wxASSERT_MSG( (style & wxOK ) || (style & wxYES_NO),
144 "don't create dialog with only the Cancel button!" );
145
146 if ( style & wxYES_NO ) {
147 buttons[Btn_Yes] = new wxButton(this, wxID_YES, _("Yes"));
148 buttons[Btn_No] = new wxButton(this, wxID_NO, _("No"));
149
150
151 if(style & wxNO_DEFAULT)
152 nDefaultBtn = Btn_No;
153 else
154 nDefaultBtn = Btn_Yes;
155 }
156
157 if (style & wxOK) {
158 buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
159
160 if ( nDefaultBtn == -1 )
161 nDefaultBtn = Btn_Ok;
162 }
163
164 if (style & wxCANCEL) {
165 buttons[Btn_Cancel] = new wxButton(this, wxID_CANCEL, _("Cancel"));
166 }
167
168 // get the longest caption and also calc the number of buttons
169 size_t nBtn, nButtons = 0;
170 int width, widthBtnMax = 0;
171 for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
172 if ( buttons[nBtn] ) {
173 nButtons++;
174 GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
175 if ( width > widthBtnMax )
176 widthBtnMax = width;
177 }
178 }
179
180 // now we can place the buttons
181 if ( widthBtnMax < 75 )
182 widthBtnMax = 75;
183 else
184 widthBtnMax += 10;
185 long heightButton = widthBtnMax*23/75;
186
187 // *1.2 baselineskip
188 heightTextMax *= 12;
189 heightTextMax /= 10;
190
191 long widthButtonsTotal = nButtons * (widthBtnMax + LAYOUT_X_MARGIN) -
192 LAYOUT_X_MARGIN;
193
194 // the size of the dialog
195 long widthDlg = wxMax(widthTextMax + iconSize + 4*LAYOUT_X_MARGIN,
196 wxMax(widthButtonsTotal, width)) +
197 2*LAYOUT_X_MARGIN,
198 heightDlg = 8*LAYOUT_Y_MARGIN + heightButton +
199 heightTextMax*(nLineCount + 1);
200
201 // create the controls
202 // -------------------
203
204 // the icon first
205 c = new wxLayoutConstraints;
206 c->width.Absolute(iconSize);
207 c->height.Absolute(iconSize);
208 c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN);
209 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
210 icon->SetConstraints(c);
211
212 wxStaticText *text = NULL;
213 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
214 c = new wxLayoutConstraints;
215 if ( text == NULL )
216 c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN);
217 else
218 c->top.Below(text);
219
220 c->left.RightOf(icon, 2*LAYOUT_X_MARGIN);
221 c->width.Absolute(widthTextMax);
222 c->height.Absolute(heightTextMax);
223 text = new wxStaticText(this, -1, lines[nLine]);
224 text->SetConstraints(c);
225 }
226
227 // create the buttons
228 wxButton *btnPrevious = (wxButton *)NULL;
229 for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
230 if ( buttons[nBtn] ) {
231 c = new wxLayoutConstraints;
232
233 if ( btnPrevious ) {
234 c->left.RightOf(btnPrevious, LAYOUT_X_MARGIN);
235 }
236 else {
237 c->left.SameAs(this, wxLeft,
238 (widthDlg - widthButtonsTotal) / 2);
239 }
240
241 c->width.Absolute(widthBtnMax);
242 c->top.Below(text, 4*LAYOUT_Y_MARGIN);
243 c->height.Absolute(heightButton);
244 buttons[nBtn]->SetConstraints(c);
245
246 btnPrevious = buttons[nBtn];
247 }
248 }
249
250 // set default button
251 // ------------------
252
253 if ( nDefaultBtn != -1 ) {
254 buttons[nDefaultBtn]->SetDefault();
255 buttons[nDefaultBtn]->SetFocus();
256 }
257 else {
258 wxFAIL_MSG( "can't find default button for this dialog." );
259 }
260
261 // position the controls and the dialog itself
262 // -------------------------------------------
263
264 SetClientSize(widthDlg, heightDlg);
265
266 // SetSizeHints() wants the size of the whole dialog, not just client size
267 wxSize sizeTotal = GetSize(),
268 sizeClient = GetClientSize();
269 SetSizeHints(widthDlg + sizeTotal.GetWidth() - sizeClient.GetWidth(),
270 heightDlg + sizeTotal.GetHeight() - sizeClient.GetHeight());
271
272 Layout();
273
274 Centre(wxCENTER_FRAME | wxBOTH);
275
276 wxEndBusyCursor();
277 }
278
279 void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
280 {
281 EndModal( wxID_YES );
282 }
283
284 void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
285 {
286 EndModal( wxID_NO );
287 }
288
289 void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
290 {
291 /* Allow cancellation via ESC/Close button except if
292 only YES and NO are specified. */
293 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
294 {
295 EndModal( wxID_CANCEL );
296 }
297 }
298
299