1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <vcl/button.hxx>
26 #include <vcl/btndlg.hxx>
28 typedef boost::ptr_vector
<ImplBtnDlgItem
>::iterator btn_iterator
;
29 typedef boost::ptr_vector
<ImplBtnDlgItem
>::const_iterator btn_const_iterator
;
37 PushButton
* mpPushButton
;
40 void ButtonDialog::ImplInitButtonDialogData()
44 mnFocusButtonId
= BUTTONDIALOG_BUTTON_NOTFOUND
;
48 ButtonDialog::ButtonDialog( WindowType nType
) :
51 ImplInitButtonDialogData();
54 ButtonDialog::ButtonDialog( Window
* pParent
, WinBits nStyle
) :
55 Dialog( WINDOW_BUTTONDIALOG
)
57 ImplInitButtonDialogData();
58 ImplInit( pParent
, nStyle
);
61 ButtonDialog::~ButtonDialog()
63 for ( btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
65 if ( it
->mpPushButton
&& it
->mbOwnButton
)
66 delete it
->mpPushButton
;
70 PushButton
* ButtonDialog::ImplCreatePushButton( sal_uInt16 nBtnFlags
)
75 if ( nBtnFlags
& BUTTONDIALOG_DEFBUTTON
)
76 nStyle
|= WB_DEFBUTTON
;
77 if ( nBtnFlags
& BUTTONDIALOG_CANCELBUTTON
)
78 pBtn
= new CancelButton( this, nStyle
);
79 else if ( nBtnFlags
& BUTTONDIALOG_OKBUTTON
)
80 pBtn
= new OKButton( this, nStyle
);
81 else if ( nBtnFlags
& BUTTONDIALOG_HELPBUTTON
)
82 pBtn
= new HelpButton( this, nStyle
);
84 pBtn
= new PushButton( this, nStyle
);
86 if ( !(nBtnFlags
& BUTTONDIALOG_HELPBUTTON
) )
87 pBtn
->SetClickHdl( LINK( this, ButtonDialog
, ImplClickHdl
) );
92 ImplBtnDlgItem
* ButtonDialog::ImplGetItem( sal_uInt16 nId
) const
94 for ( btn_const_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
97 return const_cast<ImplBtnDlgItem
*>(&(*it
));
103 long ButtonDialog::ImplGetButtonSize()
108 // Calculate ButtonSize
109 long nLastSepSize
= 0;
111 maCtrlSize
= Size( IMPL_MINSIZE_BUTTON_WIDTH
, IMPL_MINSIZE_BUTTON_HEIGHT
);
113 for ( btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
115 nSepSize
+= nLastSepSize
;
117 long nTxtWidth
= it
->mpPushButton
->GetCtrlTextWidth(it
->mpPushButton
->GetText());
118 nTxtWidth
+= IMPL_EXTRA_BUTTON_WIDTH
;
120 if ( nTxtWidth
> maCtrlSize
.Width() )
121 maCtrlSize
.Width() = nTxtWidth
;
123 long nTxtHeight
= it
->mpPushButton
->GetTextHeight();
124 nTxtHeight
+= IMPL_EXTRA_BUTTON_HEIGHT
;
126 if ( nTxtHeight
> maCtrlSize
.Height() )
127 maCtrlSize
.Height() = nTxtHeight
;
129 nSepSize
+= it
->mnSepSize
;
131 if ( GetStyle() & WB_HORZ
)
132 nLastSepSize
= IMPL_SEP_BUTTON_X
;
134 nLastSepSize
= IMPL_SEP_BUTTON_Y
;
137 long nButtonCount
= maItemList
.size();
139 if ( GetStyle() & WB_HORZ
)
140 mnButtonSize
= nSepSize
+ (nButtonCount
*maCtrlSize
.Width());
142 mnButtonSize
= nSepSize
+ (nButtonCount
*maCtrlSize
.Height());
147 void ButtonDialog::ImplPosControls()
152 // Create PushButtons and determine Sizes
155 // determine dialog size
156 Size aDlgSize
= maPageSize
;
159 if ( GetStyle() & WB_HORZ
)
161 if ( mnButtonSize
+(IMPL_DIALOG_OFFSET
*2) > aDlgSize
.Width() )
162 aDlgSize
.Width() = mnButtonSize
+(IMPL_DIALOG_OFFSET
*2);
163 if ( GetStyle() & WB_LEFT
)
164 nX
= IMPL_DIALOG_OFFSET
;
165 else if ( GetStyle() & WB_RIGHT
)
166 nX
= aDlgSize
.Width()-mnButtonSize
-IMPL_DIALOG_OFFSET
;
168 nX
= (aDlgSize
.Width()-mnButtonSize
)/2;
170 aDlgSize
.Height() += IMPL_DIALOG_OFFSET
+maCtrlSize
.Height();
171 nY
= aDlgSize
.Height()-maCtrlSize
.Height()-IMPL_DIALOG_OFFSET
;
175 if ( mnButtonSize
+(IMPL_DIALOG_OFFSET
*2) > aDlgSize
.Height() )
176 aDlgSize
.Height() = mnButtonSize
+(IMPL_DIALOG_OFFSET
*2);
177 if ( GetStyle() & WB_BOTTOM
)
178 nY
= aDlgSize
.Height()-mnButtonSize
-IMPL_DIALOG_OFFSET
;
179 else if ( GetStyle() & WB_VCENTER
)
180 nY
= (aDlgSize
.Height()-mnButtonSize
)/2;
182 nY
= IMPL_DIALOG_OFFSET
;
184 aDlgSize
.Width() += IMPL_DIALOG_OFFSET
+maCtrlSize
.Width();
185 nX
= aDlgSize
.Width()-maCtrlSize
.Width()-IMPL_DIALOG_OFFSET
;
188 // Arrange PushButtons
189 for ( btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
191 if ( GetStyle() & WB_HORZ
)
196 it
->mpPushButton
->SetPosSizePixel( Point( nX
, nY
), maCtrlSize
);
197 it
->mpPushButton
->Show();
199 if ( GetStyle() & WB_HORZ
)
200 nX
+= maCtrlSize
.Width()+IMPL_SEP_BUTTON_X
;
202 nY
+= maCtrlSize
.Height()+IMPL_SEP_BUTTON_Y
;
205 SetOutputSizePixel( aDlgSize
);
207 mbFormat
= sal_False
;
210 IMPL_LINK( ButtonDialog
, ImplClickHdl
, PushButton
*, pBtn
)
212 for ( btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
214 if ( it
->mpPushButton
== pBtn
)
216 mnCurButtonId
= it
->mnId
;
225 void ButtonDialog::Resize()
229 void ButtonDialog::StateChanged( StateChangedType nType
)
231 if ( nType
== STATE_CHANGE_INITSHOW
)
235 // Set focus on default button.
236 if ( mnFocusButtonId
!= BUTTONDIALOG_BUTTON_NOTFOUND
)
238 for ( btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
240 if (it
->mnId
== mnFocusButtonId
)
242 if (it
->mpPushButton
->IsVisible())
243 it
->mpPushButton
->GrabFocus();
251 Dialog::StateChanged( nType
);
254 void ButtonDialog::Click()
259 EndDialog( GetCurButtonId() );
262 maClickHdl
.Call( this );
265 void ButtonDialog::AddButton( const OUString
& rText
, sal_uInt16 nId
,
266 sal_uInt16 nBtnFlags
, long nSepPixel
)
269 ImplBtnDlgItem
* pItem
= new ImplBtnDlgItem
;
271 pItem
->mbOwnButton
= sal_True
;
272 pItem
->mnSepSize
= nSepPixel
;
273 pItem
->mpPushButton
= ImplCreatePushButton( nBtnFlags
);
275 if (!rText
.isEmpty())
276 pItem
->mpPushButton
->SetText( rText
);
278 maItemList
.push_back(pItem
);
280 if ( nBtnFlags
& BUTTONDIALOG_FOCUSBUTTON
)
281 mnFocusButtonId
= nId
;
286 void ButtonDialog::AddButton( StandardButtonType eType
, sal_uInt16 nId
,
287 sal_uInt16 nBtnFlags
, long nSepPixel
)
290 ImplBtnDlgItem
* pItem
= new ImplBtnDlgItem
;
292 pItem
->mbOwnButton
= sal_True
;
293 pItem
->mnSepSize
= nSepPixel
;
295 if ( eType
== BUTTON_OK
)
296 nBtnFlags
|= BUTTONDIALOG_OKBUTTON
;
297 else if ( eType
== BUTTON_HELP
)
298 nBtnFlags
|= BUTTONDIALOG_HELPBUTTON
;
299 else if ( (eType
== BUTTON_CANCEL
) || (eType
== BUTTON_CLOSE
) )
300 nBtnFlags
|= BUTTONDIALOG_CANCELBUTTON
;
301 pItem
->mpPushButton
= ImplCreatePushButton( nBtnFlags
);
303 // Standard-Buttons have the right text already
304 if ( !((eType
== BUTTON_OK
) && (pItem
->mpPushButton
->GetType() == WINDOW_OKBUTTON
)) ||
305 !((eType
== BUTTON_CANCEL
) && (pItem
->mpPushButton
->GetType() == WINDOW_CANCELBUTTON
)) ||
306 !((eType
== BUTTON_HELP
) && (pItem
->mpPushButton
->GetType() == WINDOW_HELPBUTTON
)) )
308 pItem
->mpPushButton
->SetText( Button::GetStandardText( eType
) );
311 if ( nBtnFlags
& BUTTONDIALOG_FOCUSBUTTON
)
312 mnFocusButtonId
= nId
;
314 maItemList
.push_back(pItem
);
319 void ButtonDialog::RemoveButton( sal_uInt16 nId
)
322 for (it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
326 it
->mpPushButton
->Hide();
328 if (it
->mbOwnButton
)
329 delete it
->mpPushButton
;
331 maItemList
.erase(it
);
336 if (it
== maItemList
.end())
337 SAL_WARN( "vcl.window", "ButtonDialog::RemoveButton(): ButtonId invalid" );
340 void ButtonDialog::Clear()
342 for (btn_iterator it
= maItemList
.begin(); it
!= maItemList
.end(); ++it
)
344 it
->mpPushButton
->Hide();
346 if (it
->mbOwnButton
)
347 delete it
->mpPushButton
;
354 sal_uInt16
ButtonDialog::GetButtonId( sal_uInt16 nButton
) const
356 if ( nButton
< maItemList
.size() )
357 return maItemList
[nButton
].mnId
;
359 return BUTTONDIALOG_BUTTON_NOTFOUND
;
362 PushButton
* ButtonDialog::GetPushButton( sal_uInt16 nId
) const
364 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
367 return pItem
->mpPushButton
;
372 void ButtonDialog::SetButtonText( sal_uInt16 nId
, const OUString
& rText
)
374 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
378 pItem
->mpPushButton
->SetText( rText
);
383 void ButtonDialog::SetButtonHelpText( sal_uInt16 nId
, const OUString
& rText
)
385 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
388 pItem
->mpPushButton
->SetHelpText( rText
);
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */