]> git.saurik.com Git - wxWidgets.git/blame - src/generic/msgdlgg.cpp
1. wxGrid fixes contributed by Gerhard Gruber (client data for cells...)
[wxWidgets.git] / src / generic / msgdlgg.cpp
CommitLineData
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
c50f1fb9 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
c50f1fb9
VZ
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"
dfe1eee3 31 #include "wx/icon.h"
c801d85f
KB
32#endif
33
34#include <stdio.h>
35#include <string.h>
36
37#include "wx/generic/msgdlgg.h"
38
dcf924a3
RR
39#if wxUSE_STATLINE
40 #include "wx/statline.h"
b0351fc9
RR
41#endif
42
917b3d40
VZ
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
c801d85f
KB
55
56#if !USE_SHARED_LIBRARY
57BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
f03fc89f
VZ
58 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
59 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
60 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
c801d85f
KB
61END_EVENT_TABLE()
62
63IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
64#endif
65
917b3d40
VZ
66wxGenericMessageDialog::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 )
c801d85f 72{
dfc54541 73 m_dialogStyle = style;
c801d85f 74
dcf924a3
RR
75 wxBeginBusyCursor();
76
917b3d40
VZ
77 wxLayoutConstraints *c;
78 SetAutoLayout(TRUE);
c801d85f 79
917b3d40
VZ
80 // create an icon
81 enum
15b24b14 82 {
917b3d40
VZ
83 Icon_Information,
84 Icon_Question,
85 Icon_Warning,
86 Icon_Error
87 } which;
dfc54541 88
917b3d40
VZ
89#ifdef __WXMSW__
90 static char *icons[] =
15b24b14 91 {
917b3d40
VZ
92 "wxICON_INFO",
93 "wxICON_QUESTION",
94 "wxICON_WARNING",
95 "wxICON_ERROR",
96 };
97#else // XPM icons
98 static char **icons[] =
15b24b14 99 {
917b3d40
VZ
100 info,
101 question,
102 warning,
103 error,
104 };
105#endif // !XPM/XPM
dfc54541 106
917b3d40
VZ
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 // --------------------------
917b3d40
VZ
121
122 wxArrayString lines;
c50f1fb9
VZ
123 wxSize sizeText = SplitTextMessage(message, &lines);
124 long widthTextMax = sizeText.GetWidth(),
125 heightTextMax = sizeText.GetHeight();
126 size_t nLineCount = lines.GetCount();
917b3d40
VZ
127
128 // calculate the total dialog size
129 enum
dfc54541 130 {
917b3d40
VZ
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
96c5bd7f 152 if(style & wxNO_DEFAULT)
917b3d40 153 nDefaultBtn = Btn_No;
96c5bd7f 154 else
917b3d40 155 nDefaultBtn = Btn_Yes;
dfc54541 156 }
917b3d40
VZ
157
158 if (style & wxOK) {
159 buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
160
161 if ( nDefaultBtn == -1 )
162 nDefaultBtn = Btn_Ok;
15b24b14 163 }
c801d85f 164
917b3d40
VZ
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;
dfe1eee3 171 int width, widthBtnMax = 0;
917b3d40
VZ
172 for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
173 if ( buttons[nBtn] ) {
174 nButtons++;
dfe1eee3 175 GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
917b3d40
VZ
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
917b3d40
VZ
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
dcf924a3 277 wxEndBusyCursor();
c801d85f
KB
278}
279
280void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
281{
15b24b14 282 EndModal( wxID_YES );
c801d85f
KB
283}
284
285void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
286{
15b24b14 287 EndModal( wxID_NO );
c801d85f
KB
288}
289
290void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
291{
15b24b14
RR
292 /* Allow cancellation via ESC/Close button except if
293 only YES and NO are specified. */
dfc54541 294 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
15b24b14
RR
295 {
296 EndModal( wxID_CANCEL );
297 }
c801d85f
KB
298}
299
300