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