]>
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"
38 #include "wx/generic/msgdlgg.h"
41 #include "wx/statline.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
50 #if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxGenericMessageDialog
, wxDialog
)
52 EVT_BUTTON(wxID_YES
, wxGenericMessageDialog::OnYes
)
53 EVT_BUTTON(wxID_NO
, wxGenericMessageDialog::OnNo
)
54 EVT_BUTTON(wxID_CANCEL
, wxGenericMessageDialog::OnCancel
)
57 IMPLEMENT_CLASS(wxGenericMessageDialog
, wxDialog
)
61 # include "wx/gtk/info.xpm"
62 # include "wx/gtk/error.xpm"
63 # include "wx/gtk/question.xpm"
64 # include "wx/gtk/warning.xpm"
66 // MSW icons are in the ressources, for all other platforms - in XPM files
68 # include "wx/generic/info.xpm"
69 # include "wx/generic/question.xpm"
70 # include "wx/generic/warning.xpm"
71 # include "wx/generic/error.xpm"
76 wxApp::GetStdIcon(int which
) const
80 case wxICON_INFORMATION
:
81 return wxIcon(info_xpm
);
84 return wxIcon(error_xpm
);
87 return wxIcon(question_xpm
);
89 case wxICON_EXCLAMATION
:
90 return wxIcon(warning_xpm
);
93 wxFAIL_MSG("requested non existent standard icon");
94 return wxIcon(error_xpm
);
100 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow
*parent
,
101 const wxString
& message
,
102 const wxString
& caption
,
105 : wxDialog( parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
)
107 m_dialogStyle
= style
;
111 wxLayoutConstraints
*c
;
114 wxStaticBitmap
*icon
= new wxStaticBitmap(this, -1,
115 wxTheApp
->GetStdIcon(style
& wxICON_MASK
));
116 const int iconSize
= icon
->GetBitmap().GetWidth();
118 // split the message in lines
119 // --------------------------
122 wxSize sizeText
= SplitTextMessage(message
, &lines
);
123 long widthTextMax
= sizeText
.GetWidth(),
124 heightTextMax
= sizeText
.GetHeight();
125 size_t nLineCount
= lines
.GetCount();
127 // calculate the total dialog size
136 wxButton
*buttons
[Btn_Max
] = { NULL
, NULL
, NULL
, NULL
};
137 int nDefaultBtn
= -1;
139 // some checks are in order...
140 wxASSERT_MSG( !(style
& wxOK
) || !(style
& wxYES_NO
),
141 "don't create dialog with both Yes/No and Ok buttons!" );
143 wxASSERT_MSG( (style
& wxOK
) || (style
& wxYES_NO
),
144 "don't create dialog with only the Cancel button!" );
146 if ( style
& wxYES_NO
) {
147 buttons
[Btn_Yes
] = new wxButton(this, wxID_YES
, _("Yes"));
148 buttons
[Btn_No
] = new wxButton(this, wxID_NO
, _("No"));
151 if(style
& wxNO_DEFAULT
)
152 nDefaultBtn
= Btn_No
;
154 nDefaultBtn
= Btn_Yes
;
158 buttons
[Btn_Ok
] = new wxButton(this, wxID_OK
, _("OK"));
160 if ( nDefaultBtn
== -1 )
161 nDefaultBtn
= Btn_Ok
;
164 if (style
& wxCANCEL
) {
165 buttons
[Btn_Cancel
] = new wxButton(this, wxID_CANCEL
, _("Cancel"));
168 // get the longest caption and also calc the number of buttons
169 size_t nBtn
, nButtons
= 0;
170 int width
, widthBtnMax
= 0;
171 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
172 if ( buttons
[nBtn
] ) {
174 GetTextExtent(buttons
[nBtn
]->GetLabel(), &width
, NULL
);
175 if ( width
> widthBtnMax
)
180 // now we can place the buttons
181 if ( widthBtnMax
< 75 )
185 long heightButton
= widthBtnMax
*23/75;
191 long widthButtonsTotal
= nButtons
* (widthBtnMax
+ LAYOUT_X_MARGIN
) -
194 // the size of the dialog
195 long widthDlg
= wxMax(widthTextMax
+ iconSize
+ 4*LAYOUT_X_MARGIN
,
196 wxMax(widthButtonsTotal
, width
)) +
198 heightDlg
= 8*LAYOUT_Y_MARGIN
+ heightButton
+
199 heightTextMax
*(nLineCount
+ 1);
201 // create the controls
202 // -------------------
205 c
= new wxLayoutConstraints
;
206 c
->width
.Absolute(iconSize
);
207 c
->height
.Absolute(iconSize
);
208 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
209 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
210 icon
->SetConstraints(c
);
212 wxStaticText
*text
= NULL
;
213 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
214 c
= new wxLayoutConstraints
;
216 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
220 c
->left
.RightOf(icon
, 2*LAYOUT_X_MARGIN
);
221 c
->width
.Absolute(widthTextMax
);
222 c
->height
.Absolute(heightTextMax
);
223 text
= new wxStaticText(this, -1, lines
[nLine
]);
224 text
->SetConstraints(c
);
227 // create the buttons
228 wxButton
*btnPrevious
= (wxButton
*)NULL
;
229 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
230 if ( buttons
[nBtn
] ) {
231 c
= new wxLayoutConstraints
;
234 c
->left
.RightOf(btnPrevious
, LAYOUT_X_MARGIN
);
237 c
->left
.SameAs(this, wxLeft
,
238 (widthDlg
- widthButtonsTotal
) / 2);
241 c
->width
.Absolute(widthBtnMax
);
242 c
->top
.Below(text
, 4*LAYOUT_Y_MARGIN
);
243 c
->height
.Absolute(heightButton
);
244 buttons
[nBtn
]->SetConstraints(c
);
246 btnPrevious
= buttons
[nBtn
];
250 // set default button
251 // ------------------
253 if ( nDefaultBtn
!= -1 ) {
254 buttons
[nDefaultBtn
]->SetDefault();
255 buttons
[nDefaultBtn
]->SetFocus();
258 wxFAIL_MSG( "can't find default button for this dialog." );
261 // position the controls and the dialog itself
262 // -------------------------------------------
264 SetClientSize(widthDlg
, heightDlg
);
266 // SetSizeHints() wants the size of the whole dialog, not just client size
267 wxSize sizeTotal
= GetSize(),
268 sizeClient
= GetClientSize();
269 SetSizeHints(widthDlg
+ sizeTotal
.GetWidth() - sizeClient
.GetWidth(),
270 heightDlg
+ sizeTotal
.GetHeight() - sizeClient
.GetHeight());
274 Centre(wxCENTER_FRAME
| wxBOTH
);
279 void wxGenericMessageDialog::OnYes(wxCommandEvent
& WXUNUSED(event
))
281 EndModal( wxID_YES
);
284 void wxGenericMessageDialog::OnNo(wxCommandEvent
& WXUNUSED(event
))
289 void wxGenericMessageDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
291 /* Allow cancellation via ESC/Close button except if
292 only YES and NO are specified. */
293 if ( (m_dialogStyle
& wxYES_NO
) != wxYES_NO
|| (m_dialogStyle
& wxCANCEL
) )
295 EndModal( wxID_CANCEL
);