]> git.saurik.com Git - wxWidgets.git/blame - src/generic/msgdlgg.cpp
CalcUnscrolledPosition takes int, not floats
[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"
ebea0891 32# include "wx/app.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
917b3d40 48
c801d85f
KB
49
50#if !USE_SHARED_LIBRARY
51BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
f03fc89f
VZ
52 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
53 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
54 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
c801d85f
KB
55END_EVENT_TABLE()
56
57IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
58#endif
59
ebea0891
KB
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
75wxIcon
76wxApp::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
917b3d40
VZ
100wxGenericMessageDialog::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 )
c801d85f 106{
dfc54541 107 m_dialogStyle = style;
c801d85f 108
dcf924a3
RR
109 wxBeginBusyCursor();
110
917b3d40
VZ
111 wxLayoutConstraints *c;
112 SetAutoLayout(TRUE);
c801d85f 113
ebea0891
KB
114 wxStaticBitmap *icon = new wxStaticBitmap(this, -1,
115 wxTheApp->GetStdIcon(style & wxICON_MASK));
917b3d40
VZ
116 const int iconSize = icon->GetBitmap().GetWidth();
117
118 // split the message in lines
119 // --------------------------
917b3d40
VZ
120
121 wxArrayString lines;
c50f1fb9
VZ
122 wxSize sizeText = SplitTextMessage(message, &lines);
123 long widthTextMax = sizeText.GetWidth(),
124 heightTextMax = sizeText.GetHeight();
125 size_t nLineCount = lines.GetCount();
917b3d40
VZ
126
127 // calculate the total dialog size
128 enum
dfc54541 129 {
917b3d40
VZ
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
96c5bd7f 151 if(style & wxNO_DEFAULT)
917b3d40 152 nDefaultBtn = Btn_No;
96c5bd7f 153 else
917b3d40 154 nDefaultBtn = Btn_Yes;
dfc54541 155 }
917b3d40
VZ
156
157 if (style & wxOK) {
158 buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
159
160 if ( nDefaultBtn == -1 )
161 nDefaultBtn = Btn_Ok;
15b24b14 162 }
c801d85f 163
917b3d40
VZ
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;
dfe1eee3 170 int width, widthBtnMax = 0;
917b3d40
VZ
171 for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
172 if ( buttons[nBtn] ) {
173 nButtons++;
dfe1eee3 174 GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
917b3d40
VZ
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
917b3d40
VZ
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
dcf924a3 276 wxEndBusyCursor();
c801d85f
KB
277}
278
279void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
280{
15b24b14 281 EndModal( wxID_YES );
c801d85f
KB
282}
283
284void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
285{
15b24b14 286 EndModal( wxID_NO );
c801d85f
KB
287}
288
289void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
290{
15b24b14
RR
291 /* Allow cancellation via ESC/Close button except if
292 only YES and NO are specified. */
dfc54541 293 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
15b24b14
RR
294 {
295 EndModal( wxID_CANCEL );
296 }
c801d85f
KB
297}
298
299