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