]>
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"
37 #include "wx/generic/msgdlgg.h"
40 #include "wx/statline.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // MSW icons are in the ressources, for all other platforms - in XPM files
49 #include "wx/generic/info.xpm"
50 #include "wx/generic/question.xpm"
51 #include "wx/generic/warning.xpm"
52 #include "wx/generic/error.xpm"
56 #if !USE_SHARED_LIBRARY
57 BEGIN_EVENT_TABLE(wxGenericMessageDialog
, wxDialog
)
58 EVT_BUTTON(wxID_YES
, wxGenericMessageDialog::OnYes
)
59 EVT_BUTTON(wxID_NO
, wxGenericMessageDialog::OnNo
)
60 EVT_BUTTON(wxID_CANCEL
, wxGenericMessageDialog::OnCancel
)
63 IMPLEMENT_CLASS(wxGenericMessageDialog
, wxDialog
)
66 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow
*parent
,
67 const wxString
& message
,
68 const wxString
& caption
,
71 : wxDialog( parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
)
73 m_dialogStyle
= style
;
77 wxLayoutConstraints
*c
;
90 static char *icons
[] =
98 static char **icons
[] =
107 if ( style
& wxICON_EXCLAMATION
)
108 which
= Icon_Warning
;
109 else if ( style
& wxICON_HAND
)
111 else if ( style
& wxICON_QUESTION
)
112 which
= Icon_Question
;
114 which
= Icon_Information
;
116 wxStaticBitmap
*icon
= new wxStaticBitmap(this, -1, wxIcon(icons
[which
]));
117 const int iconSize
= icon
->GetBitmap().GetWidth();
119 // split the message in lines
120 // --------------------------
123 wxSize sizeText
= SplitTextMessage(message
, &lines
);
124 long widthTextMax
= sizeText
.GetWidth(),
125 heightTextMax
= sizeText
.GetHeight();
126 size_t nLineCount
= lines
.GetCount();
128 // calculate the total dialog size
137 wxButton
*buttons
[Btn_Max
] = { NULL
, NULL
, NULL
, NULL
};
138 int nDefaultBtn
= -1;
140 // some checks are in order...
141 wxASSERT_MSG( !(style
& wxOK
) || !(style
& wxYES_NO
),
142 "don't create dialog with both Yes/No and Ok buttons!" );
144 wxASSERT_MSG( (style
& wxOK
) || (style
& wxYES_NO
),
145 "don't create dialog with only the Cancel button!" );
147 if ( style
& wxYES_NO
) {
148 buttons
[Btn_Yes
] = new wxButton(this, wxID_YES
, _("Yes"));
149 buttons
[Btn_No
] = new wxButton(this, wxID_NO
, _("No"));
152 if(style
& wxNO_DEFAULT
)
153 nDefaultBtn
= Btn_No
;
155 nDefaultBtn
= Btn_Yes
;
159 buttons
[Btn_Ok
] = new wxButton(this, wxID_OK
, _("OK"));
161 if ( nDefaultBtn
== -1 )
162 nDefaultBtn
= Btn_Ok
;
165 if (style
& wxCANCEL
) {
166 buttons
[Btn_Cancel
] = new wxButton(this, wxID_CANCEL
, _("Cancel"));
169 // get the longest caption and also calc the number of buttons
170 size_t nBtn
, nButtons
= 0;
171 int width
, widthBtnMax
= 0;
172 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
173 if ( buttons
[nBtn
] ) {
175 GetTextExtent(buttons
[nBtn
]->GetLabel(), &width
, NULL
);
176 if ( width
> widthBtnMax
)
181 // now we can place the buttons
182 if ( widthBtnMax
< 75 )
186 long heightButton
= widthBtnMax
*23/75;
192 long widthButtonsTotal
= nButtons
* (widthBtnMax
+ LAYOUT_X_MARGIN
) -
195 // the size of the dialog
196 long widthDlg
= wxMax(widthTextMax
+ iconSize
+ 4*LAYOUT_X_MARGIN
,
197 wxMax(widthButtonsTotal
, width
)) +
199 heightDlg
= 8*LAYOUT_Y_MARGIN
+ heightButton
+
200 heightTextMax
*(nLineCount
+ 1);
202 // create the controls
203 // -------------------
206 c
= new wxLayoutConstraints
;
207 c
->width
.Absolute(iconSize
);
208 c
->height
.Absolute(iconSize
);
209 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
210 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
211 icon
->SetConstraints(c
);
213 wxStaticText
*text
= NULL
;
214 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
215 c
= new wxLayoutConstraints
;
217 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
221 c
->left
.RightOf(icon
, 2*LAYOUT_X_MARGIN
);
222 c
->width
.Absolute(widthTextMax
);
223 c
->height
.Absolute(heightTextMax
);
224 text
= new wxStaticText(this, -1, lines
[nLine
]);
225 text
->SetConstraints(c
);
228 // create the buttons
229 wxButton
*btnPrevious
= (wxButton
*)NULL
;
230 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
231 if ( buttons
[nBtn
] ) {
232 c
= new wxLayoutConstraints
;
235 c
->left
.RightOf(btnPrevious
, LAYOUT_X_MARGIN
);
238 c
->left
.SameAs(this, wxLeft
,
239 (widthDlg
- widthButtonsTotal
) / 2);
242 c
->width
.Absolute(widthBtnMax
);
243 c
->top
.Below(text
, 4*LAYOUT_Y_MARGIN
);
244 c
->height
.Absolute(heightButton
);
245 buttons
[nBtn
]->SetConstraints(c
);
247 btnPrevious
= buttons
[nBtn
];
251 // set default button
252 // ------------------
254 if ( nDefaultBtn
!= -1 ) {
255 buttons
[nDefaultBtn
]->SetDefault();
256 buttons
[nDefaultBtn
]->SetFocus();
259 wxFAIL_MSG( "can't find default button for this dialog." );
262 // position the controls and the dialog itself
263 // -------------------------------------------
265 SetClientSize(widthDlg
, heightDlg
);
267 // SetSizeHints() wants the size of the whole dialog, not just client size
268 wxSize sizeTotal
= GetSize(),
269 sizeClient
= GetClientSize();
270 SetSizeHints(widthDlg
+ sizeTotal
.GetWidth() - sizeClient
.GetWidth(),
271 heightDlg
+ sizeTotal
.GetHeight() - sizeClient
.GetHeight());
275 Centre(wxCENTER_FRAME
| wxBOTH
);
280 void wxGenericMessageDialog::OnYes(wxCommandEvent
& WXUNUSED(event
))
282 EndModal( wxID_YES
);
285 void wxGenericMessageDialog::OnNo(wxCommandEvent
& WXUNUSED(event
))
290 void wxGenericMessageDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
292 /* Allow cancellation via ESC/Close button except if
293 only YES and NO are specified. */
294 if ( (m_dialogStyle
& wxYES_NO
) != wxYES_NO
|| (m_dialogStyle
& wxCANCEL
) )
296 EndModal( wxID_CANCEL
);