]>
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 // ----------------------------------------------------------------------------
48 #if !USE_SHARED_LIBRARY
49 BEGIN_EVENT_TABLE(wxGenericMessageDialog
, wxDialog
)
50 EVT_BUTTON(wxID_YES
, wxGenericMessageDialog::OnYes
)
51 EVT_BUTTON(wxID_NO
, wxGenericMessageDialog::OnNo
)
52 EVT_BUTTON(wxID_CANCEL
, wxGenericMessageDialog::OnCancel
)
55 IMPLEMENT_CLASS(wxGenericMessageDialog
, wxDialog
)
58 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow
*parent
,
59 const wxString
& message
,
60 const wxString
& caption
,
63 : wxDialog( parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
)
65 m_dialogStyle
= style
;
69 wxLayoutConstraints
*c
;
72 wxStaticBitmap
*icon
= new wxStaticBitmap(this, -1,
73 wxTheApp
->GetStdIcon(style
& wxICON_MASK
));
74 const int iconSize
= icon
->GetBitmap().GetWidth();
76 // split the message in lines
77 // --------------------------
80 wxSize sizeText
= SplitTextMessage(message
, &lines
);
81 long widthTextMax
= sizeText
.GetWidth(),
82 heightTextMax
= sizeText
.GetHeight();
83 size_t nLineCount
= lines
.GetCount();
85 // calculate the total dialog size
94 wxButton
*buttons
[Btn_Max
] = { NULL
, NULL
, NULL
, NULL
};
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!" );
101 wxASSERT_MSG( (style
& wxOK
) || (style
& wxYES_NO
),
102 "don't create dialog with only the Cancel button!" );
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"));
109 if(style
& wxNO_DEFAULT
)
110 nDefaultBtn
= Btn_No
;
112 nDefaultBtn
= Btn_Yes
;
116 buttons
[Btn_Ok
] = new wxButton(this, wxID_OK
, _("OK"));
118 if ( nDefaultBtn
== -1 )
119 nDefaultBtn
= Btn_Ok
;
122 if (style
& wxCANCEL
) {
123 buttons
[Btn_Cancel
] = new wxButton(this, wxID_CANCEL
, _("Cancel"));
126 // get the longest caption and also calc the number of buttons
127 size_t nBtn
, nButtons
= 0;
128 int width
, widthBtnMax
= 0;
129 for ( nBtn
= 0; nBtn
< Btn_Max
; nBtn
++ ) {
130 if ( buttons
[nBtn
] ) {
132 GetTextExtent(buttons
[nBtn
]->GetLabel(), &width
, NULL
);
133 if ( width
> widthBtnMax
)
138 // now we can place the buttons
139 if ( widthBtnMax
< 75 )
143 long heightButton
= widthBtnMax
*23/75;
149 long widthButtonsTotal
= nButtons
* (widthBtnMax
+ LAYOUT_X_MARGIN
) -
152 // the size of the dialog
153 long widthDlg
= wxMax(widthTextMax
+ iconSize
+ 4*LAYOUT_X_MARGIN
,
154 wxMax(widthButtonsTotal
, width
)) +
156 heightDlg
= 8*LAYOUT_Y_MARGIN
+ heightButton
+
157 heightTextMax
*(nLineCount
+ 1);
159 // create the controls
160 // -------------------
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
);
170 wxStaticText
*text
= NULL
;
171 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
172 c
= new wxLayoutConstraints
;
174 c
->top
.SameAs(this, wxTop
, 3*LAYOUT_Y_MARGIN
);
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
);
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
;
192 c
->left
.RightOf(btnPrevious
, LAYOUT_X_MARGIN
);
195 c
->left
.SameAs(this, wxLeft
,
196 (widthDlg
- widthButtonsTotal
) / 2);
199 c
->width
.Absolute(widthBtnMax
);
200 c
->top
.Below(text
, 4*LAYOUT_Y_MARGIN
);
201 c
->height
.Absolute(heightButton
);
202 buttons
[nBtn
]->SetConstraints(c
);
204 btnPrevious
= buttons
[nBtn
];
208 // set default button
209 // ------------------
211 if ( nDefaultBtn
!= -1 ) {
212 buttons
[nDefaultBtn
]->SetDefault();
213 buttons
[nDefaultBtn
]->SetFocus();
216 wxFAIL_MSG( "can't find default button for this dialog." );
219 // position the controls and the dialog itself
220 // -------------------------------------------
222 SetClientSize(widthDlg
, heightDlg
);
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());
232 Centre(wxCENTER_FRAME
| wxBOTH
);
237 void wxGenericMessageDialog::OnYes(wxCommandEvent
& WXUNUSED(event
))
239 EndModal( wxID_YES
);
242 void wxGenericMessageDialog::OnNo(wxCommandEvent
& WXUNUSED(event
))
247 void wxGenericMessageDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
249 /* Allow cancellation via ESC/Close button except if
250 only YES and NO are specified. */
251 if ( (m_dialogStyle
& wxYES_NO
) != wxYES_NO
|| (m_dialogStyle
& wxCANCEL
) )
253 EndModal( wxID_CANCEL
);