]>
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 static const int LAYOUT_X_MARGIN
= 5;
75 static const int LAYOUT_Y_MARGIN
= 5;
77 m_dialogStyle
= style
;
81 wxLayoutConstraints
*c
;
94 static char *icons
[] =
102 static char **icons
[] =
111 if ( style
& wxICON_EXCLAMATION
)
112 which
= Icon_Warning
;
113 else if ( style
& wxICON_HAND
)
115 else if ( style
& wxICON_QUESTION
)
116 which
= Icon_Question
;
118 which
= Icon_Information
;
120 wxStaticBitmap
*icon
= new wxStaticBitmap(this, -1, wxIcon(icons
[which
]));
121 const int iconSize
= icon
->GetBitmap().GetWidth();
123 // split the message in lines
124 // --------------------------
126 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
130 long height
, width
, heightTextMax
= 0, widthTextMax
= 0;
131 for ( const char *pc
= message
; ; pc
++ ) {
132 if ( *pc
== '\n' || *pc
== '\0' ) {
133 dc
.GetTextExtent(curLine
, &width
, &height
);
134 if ( width
> widthTextMax
)
135 widthTextMax
= width
;
136 if ( height
> heightTextMax
)
137 heightTextMax
= height
;
154 // calculate the total dialog size
163 wxButton
*buttons
[Btn_Max
] = { NULL
, NULL
, NULL
, NULL
};
164 int nDefaultBtn
= -1;
166 // some checks are in order...
167 wxASSERT_MSG( !(style
& wxOK
) || !(style
& wxYES_NO
),
168 "don't create dialog with both Yes/No and Ok buttons!" );
170 wxASSERT_MSG( (style
& wxOK
) || (style
& wxYES_NO
),
171 "don't create dialog with only the Cancel button!" );
173 if ( style
& wxYES_NO
) {
174 buttons
[Btn_Yes
] = new wxButton(this, wxID_YES
, _("Yes"));
175 buttons
[Btn_No
] = new wxButton(this, wxID_NO
, _("No"));
178 if(style
& wxNO_DEFAULT
)
179 nDefaultBtn
= Btn_No
;
181 nDefaultBtn
= Btn_Yes
;
185 buttons
[Btn_Ok
] = new wxButton(this, wxID_OK
, _("OK"));
187 if ( nDefaultBtn
== -1 )
188 nDefaultBtn
= Btn_Ok
;
191 if (style
& wxCANCEL
) {
192 buttons
[Btn_Cancel
] = new wxButton(this, wxID_CANCEL
, _("Cancel"));
195 // get the longest caption and also calc the number of buttons
196 size_t nBtn
, nButtons
= 0;
197 long widthBtnMax
= 0;
198 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
199 if ( buttons
[nBtn
] ) {
201 dc
.GetTextExtent(buttons
[nBtn
]->GetLabel(), &width
, NULL
);
202 if ( width
> widthBtnMax
)
207 // now we can place the buttons
208 if ( widthBtnMax
< 75 )
212 long heightButton
= widthBtnMax
*23/75;
218 size_t nLineCount
= lines
.Count();
220 long widthButtonsTotal
= nButtons
* (widthBtnMax
+ LAYOUT_X_MARGIN
) -
223 // the size of the dialog
224 long widthDlg
= wxMax(widthTextMax
+ iconSize
+ 4*LAYOUT_X_MARGIN
,
225 wxMax(widthButtonsTotal
, width
)) +
227 heightDlg
= 8*LAYOUT_Y_MARGIN
+ heightButton
+
228 heightTextMax
*(nLineCount
+ 1);
230 // create the controls
231 // -------------------
234 c
= new wxLayoutConstraints
;
235 c
->width
.Absolute(iconSize
);
236 c
->height
.Absolute(iconSize
);
237 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
238 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
239 icon
->SetConstraints(c
);
241 wxStaticText
*text
= NULL
;
242 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
243 c
= new wxLayoutConstraints
;
245 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
249 c
->left
.RightOf(icon
, 2*LAYOUT_X_MARGIN
);
250 c
->width
.Absolute(widthTextMax
);
251 c
->height
.Absolute(heightTextMax
);
252 text
= new wxStaticText(this, -1, lines
[nLine
]);
253 text
->SetConstraints(c
);
256 // create the buttons
257 wxButton
*btnPrevious
= (wxButton
*)NULL
;
258 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
259 if ( buttons
[nBtn
] ) {
260 c
= new wxLayoutConstraints
;
263 c
->left
.RightOf(btnPrevious
, LAYOUT_X_MARGIN
);
266 c
->left
.SameAs(this, wxLeft
,
267 (widthDlg
- widthButtonsTotal
) / 2);
270 c
->width
.Absolute(widthBtnMax
);
271 c
->top
.Below(text
, 4*LAYOUT_Y_MARGIN
);
272 c
->height
.Absolute(heightButton
);
273 buttons
[nBtn
]->SetConstraints(c
);
275 btnPrevious
= buttons
[nBtn
];
279 // set default button
280 // ------------------
282 if ( nDefaultBtn
!= -1 ) {
283 buttons
[nDefaultBtn
]->SetDefault();
284 buttons
[nDefaultBtn
]->SetFocus();
287 wxFAIL_MSG( "can't find default button for this dialog." );
290 // position the controls and the dialog itself
291 // -------------------------------------------
293 SetClientSize(widthDlg
, heightDlg
);
295 // SetSizeHints() wants the size of the whole dialog, not just client size
296 wxSize sizeTotal
= GetSize(),
297 sizeClient
= GetClientSize();
298 SetSizeHints(widthDlg
+ sizeTotal
.GetWidth() - sizeClient
.GetWidth(),
299 heightDlg
+ sizeTotal
.GetHeight() - sizeClient
.GetHeight());
303 Centre(wxCENTER_FRAME
| wxBOTH
);
308 void wxGenericMessageDialog::OnYes(wxCommandEvent
& WXUNUSED(event
))
310 EndModal( wxID_YES
);
313 void wxGenericMessageDialog::OnNo(wxCommandEvent
& WXUNUSED(event
))
318 void wxGenericMessageDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
320 /* Allow cancellation via ESC/Close button except if
321 only YES and NO are specified. */
322 if ( (m_dialogStyle
& wxYES_NO
) != wxYES_NO
|| (m_dialogStyle
& wxCANCEL
) )
324 EndModal( wxID_CANCEL
);