]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/msgdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericMessageDialog
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Markus Holzem, Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "msgdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.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"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
38 #include "wx/generic/msgdlgg.h"
41 #include "wx/statline.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // MSW icons are in the ressources, for all other platforms - in XPM files
50 #include "wx/generic/info.xpm"
51 #include "wx/generic/question.xpm"
52 #include "wx/generic/warning.xpm"
53 #include "wx/generic/error.xpm"
57 #if !USE_SHARED_LIBRARY
58 BEGIN_EVENT_TABLE(wxGenericMessageDialog
, wxDialog
)
59 EVT_BUTTON(wxID_YES
, wxGenericMessageDialog::OnYes
)
60 EVT_BUTTON(wxID_NO
, wxGenericMessageDialog::OnNo
)
61 EVT_BUTTON(wxID_CANCEL
, wxGenericMessageDialog::OnCancel
)
64 IMPLEMENT_CLASS(wxGenericMessageDialog
, wxDialog
)
67 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow
*parent
,
68 const wxString
& message
,
69 const wxString
& caption
,
72 : wxDialog( parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
)
74 m_dialogStyle
= style
;
78 wxLayoutConstraints
*c
;
91 static char *icons
[] =
99 static char **icons
[] =
108 if ( style
& wxICON_EXCLAMATION
)
109 which
= Icon_Warning
;
110 else if ( style
& wxICON_HAND
)
112 else if ( style
& wxICON_QUESTION
)
113 which
= Icon_Question
;
115 which
= Icon_Information
;
117 wxStaticBitmap
*icon
= new wxStaticBitmap(this, -1, wxIcon(icons
[which
]));
118 const int iconSize
= icon
->GetBitmap().GetWidth();
120 // split the message in lines
121 // --------------------------
124 wxSize sizeText
= SplitTextMessage(message
, &lines
);
125 long widthTextMax
= sizeText
.GetWidth(),
126 heightTextMax
= sizeText
.GetHeight();
127 size_t nLineCount
= lines
.GetCount();
129 // calculate the total dialog size
138 wxButton
*buttons
[Btn_Max
] = { NULL
, NULL
, NULL
, NULL
};
139 int nDefaultBtn
= -1;
141 // some checks are in order...
142 wxASSERT_MSG( !(style
& wxOK
) || !(style
& wxYES_NO
),
143 "don't create dialog with both Yes/No and Ok buttons!" );
145 wxASSERT_MSG( (style
& wxOK
) || (style
& wxYES_NO
),
146 "don't create dialog with only the Cancel button!" );
148 if ( style
& wxYES_NO
) {
149 buttons
[Btn_Yes
] = new wxButton(this, wxID_YES
, _("Yes"));
150 buttons
[Btn_No
] = new wxButton(this, wxID_NO
, _("No"));
153 if(style
& wxNO_DEFAULT
)
154 nDefaultBtn
= Btn_No
;
156 nDefaultBtn
= Btn_Yes
;
160 buttons
[Btn_Ok
] = new wxButton(this, wxID_OK
, _("OK"));
162 if ( nDefaultBtn
== -1 )
163 nDefaultBtn
= Btn_Ok
;
166 if (style
& wxCANCEL
) {
167 buttons
[Btn_Cancel
] = new wxButton(this, wxID_CANCEL
, _("Cancel"));
170 // get the longest caption and also calc the number of buttons
171 size_t nBtn
, nButtons
= 0;
172 long width
, widthBtnMax
= 0;
173 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
174 if ( buttons
[nBtn
] ) {
176 dc
.GetTextExtent(buttons
[nBtn
]->GetLabel(), &width
, NULL
);
177 if ( width
> widthBtnMax
)
182 // now we can place the buttons
183 if ( widthBtnMax
< 75 )
187 long heightButton
= widthBtnMax
*23/75;
193 long widthButtonsTotal
= nButtons
* (widthBtnMax
+ LAYOUT_X_MARGIN
) -
196 // the size of the dialog
197 long widthDlg
= wxMax(widthTextMax
+ iconSize
+ 4*LAYOUT_X_MARGIN
,
198 wxMax(widthButtonsTotal
, width
)) +
200 heightDlg
= 8*LAYOUT_Y_MARGIN
+ heightButton
+
201 heightTextMax
*(nLineCount
+ 1);
203 // create the controls
204 // -------------------
207 c
= new wxLayoutConstraints
;
208 c
->width
.Absolute(iconSize
);
209 c
->height
.Absolute(iconSize
);
210 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
211 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
212 icon
->SetConstraints(c
);
214 wxStaticText
*text
= NULL
;
215 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
216 c
= new wxLayoutConstraints
;
218 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
222 c
->left
.RightOf(icon
, 2*LAYOUT_X_MARGIN
);
223 c
->width
.Absolute(widthTextMax
);
224 c
->height
.Absolute(heightTextMax
);
225 text
= new wxStaticText(this, -1, lines
[nLine
]);
226 text
->SetConstraints(c
);
229 // create the buttons
230 wxButton
*btnPrevious
= (wxButton
*)NULL
;
231 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
232 if ( buttons
[nBtn
] ) {
233 c
= new wxLayoutConstraints
;
236 c
->left
.RightOf(btnPrevious
, LAYOUT_X_MARGIN
);
239 c
->left
.SameAs(this, wxLeft
,
240 (widthDlg
- widthButtonsTotal
) / 2);
243 c
->width
.Absolute(widthBtnMax
);
244 c
->top
.Below(text
, 4*LAYOUT_Y_MARGIN
);
245 c
->height
.Absolute(heightButton
);
246 buttons
[nBtn
]->SetConstraints(c
);
248 btnPrevious
= buttons
[nBtn
];
252 // set default button
253 // ------------------
255 if ( nDefaultBtn
!= -1 ) {
256 buttons
[nDefaultBtn
]->SetDefault();
257 buttons
[nDefaultBtn
]->SetFocus();
260 wxFAIL_MSG( "can't find default button for this dialog." );
263 // position the controls and the dialog itself
264 // -------------------------------------------
266 SetClientSize(widthDlg
, heightDlg
);
268 // SetSizeHints() wants the size of the whole dialog, not just client size
269 wxSize sizeTotal
= GetSize(),
270 sizeClient
= GetClientSize();
271 SetSizeHints(widthDlg
+ sizeTotal
.GetWidth() - sizeClient
.GetWidth(),
272 heightDlg
+ sizeTotal
.GetHeight() - sizeClient
.GetHeight());
276 Centre(wxCENTER_FRAME
| wxBOTH
);
281 void wxGenericMessageDialog::OnYes(wxCommandEvent
& WXUNUSED(event
))
283 EndModal( wxID_YES
);
286 void wxGenericMessageDialog::OnNo(wxCommandEvent
& WXUNUSED(event
))
291 void wxGenericMessageDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
293 /* Allow cancellation via ESC/Close button except if
294 only YES and NO are specified. */
295 if ( (m_dialogStyle
& wxYES_NO
) != wxYES_NO
|| (m_dialogStyle
& wxCANCEL
) )
297 EndModal( wxID_CANCEL
);