]> git.saurik.com Git - wxWidgets.git/blame - src/generic/msgdlgg.cpp
1. wxChoice and wxComboBox support client data under MSW
[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
c801d85f
KB
48#if !USE_SHARED_LIBRARY
49BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
f03fc89f
VZ
50 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
51 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
52 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
c801d85f
KB
53END_EVENT_TABLE()
54
55IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
56#endif
57
917b3d40
VZ
58wxGenericMessageDialog::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 )
c801d85f 64{
dfc54541 65 m_dialogStyle = style;
c801d85f 66
dcf924a3
RR
67 wxBeginBusyCursor();
68
917b3d40
VZ
69 wxLayoutConstraints *c;
70 SetAutoLayout(TRUE);
c801d85f 71
ebea0891
KB
72 wxStaticBitmap *icon = new wxStaticBitmap(this, -1,
73 wxTheApp->GetStdIcon(style & wxICON_MASK));
917b3d40
VZ
74 const int iconSize = icon->GetBitmap().GetWidth();
75
76 // split the message in lines
77 // --------------------------
917b3d40
VZ
78
79 wxArrayString lines;
c50f1fb9
VZ
80 wxSize sizeText = SplitTextMessage(message, &lines);
81 long widthTextMax = sizeText.GetWidth(),
82 heightTextMax = sizeText.GetHeight();
83 size_t nLineCount = lines.GetCount();
917b3d40
VZ
84
85 // calculate the total dialog size
86 enum
dfc54541 87 {
917b3d40
VZ
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
96c5bd7f 109 if(style & wxNO_DEFAULT)
917b3d40 110 nDefaultBtn = Btn_No;
96c5bd7f 111 else
917b3d40 112 nDefaultBtn = Btn_Yes;
dfc54541 113 }
917b3d40
VZ
114
115 if (style & wxOK) {
116 buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
117
118 if ( nDefaultBtn == -1 )
119 nDefaultBtn = Btn_Ok;
15b24b14 120 }
c801d85f 121
917b3d40
VZ
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;
dfe1eee3 128 int width, widthBtnMax = 0;
917b3d40
VZ
129 for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
130 if ( buttons[nBtn] ) {
131 nButtons++;
dfe1eee3 132 GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
917b3d40
VZ
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
917b3d40
VZ
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
dcf924a3 234 wxEndBusyCursor();
c801d85f
KB
235}
236
237void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
238{
15b24b14 239 EndModal( wxID_YES );
c801d85f
KB
240}
241
242void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
243{
15b24b14 244 EndModal( wxID_NO );
c801d85f
KB
245}
246
247void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
248{
15b24b14
RR
249 /* Allow cancellation via ESC/Close button except if
250 only YES and NO are specified. */
dfc54541 251 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
15b24b14
RR
252 {
253 EndModal( wxID_CANCEL );
254 }
c801d85f
KB
255}
256
257