bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / window / btndlg.cxx
blob5d96964706aa52c696641fe10c14e8c3f4013d3b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <tools/rc.h>
23 #include <svdata.hxx>
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;
31 struct ImplBtnDlgItem
33 sal_uInt16 mnId;
34 bool mbOwnButton;
35 bool mbDummyAlign;
36 long mnSepSize;
37 PushButton* mpPushButton;
40 void ButtonDialog::ImplInitButtonDialogData()
42 mnButtonSize = 0;
43 mnCurButtonId = 0;
44 mnFocusButtonId = BUTTONDIALOG_BUTTON_NOTFOUND;
45 mbFormat = sal_True;
48 ButtonDialog::ButtonDialog( WindowType nType ) :
49 Dialog( 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 )
72 PushButton* pBtn;
73 WinBits nStyle = 0;
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 );
83 else
84 pBtn = new PushButton( this, nStyle );
86 if ( !(nBtnFlags & BUTTONDIALOG_HELPBUTTON) )
87 pBtn->SetClickHdl( LINK( this, ButtonDialog, ImplClickHdl ) );
89 return pBtn;
92 ImplBtnDlgItem* ButtonDialog::ImplGetItem( sal_uInt16 nId ) const
94 for ( btn_const_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
96 if (it->mnId == nId)
97 return const_cast<ImplBtnDlgItem*>(&(*it));
100 return NULL;
103 long ButtonDialog::ImplGetButtonSize()
105 if ( !mbFormat )
106 return mnButtonSize;
108 // Calculate ButtonSize
109 long nLastSepSize = 0;
110 long nSepSize = 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;
133 else
134 nLastSepSize = IMPL_SEP_BUTTON_Y;
137 long nButtonCount = maItemList.size();
139 if ( GetStyle() & WB_HORZ )
140 mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Width());
141 else
142 mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Height());
144 return mnButtonSize;
147 void ButtonDialog::ImplPosControls()
149 if ( !mbFormat )
150 return;
152 // Create PushButtons and determine Sizes
153 ImplGetButtonSize();
155 // determine dialog size
156 Size aDlgSize = maPageSize;
157 long nX;
158 long nY;
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;
167 else
168 nX = (aDlgSize.Width()-mnButtonSize)/2;
170 aDlgSize.Height() += IMPL_DIALOG_OFFSET+maCtrlSize.Height();
171 nY = aDlgSize.Height()-maCtrlSize.Height()-IMPL_DIALOG_OFFSET;
173 else
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;
181 else
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 )
192 nX += it->mnSepSize;
193 else
194 nY += it->mnSepSize;
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;
201 else
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;
217 Click();
218 break;
222 return 0;
225 void ButtonDialog::Resize()
229 void ButtonDialog::StateChanged( StateChangedType nType )
231 if ( nType == STATE_CHANGE_INITSHOW )
233 ImplPosControls();
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();
245 break;
251 Dialog::StateChanged( nType );
254 void ButtonDialog::Click()
256 if ( !maClickHdl )
258 if ( IsInExecute() )
259 EndDialog( GetCurButtonId() );
261 else
262 maClickHdl.Call( this );
265 void ButtonDialog::AddButton( const XubString& rText, sal_uInt16 nId,
266 sal_uInt16 nBtnFlags, long nSepPixel )
268 // PageItem anlegen
269 ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
270 pItem->mnId = nId;
271 pItem->mbOwnButton = sal_True;
272 pItem->mnSepSize = nSepPixel;
273 pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
275 if ( rText.Len() )
276 pItem->mpPushButton->SetText( rText );
278 maItemList.push_back(pItem);
280 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
281 mnFocusButtonId = nId;
283 mbFormat = sal_True;
286 void ButtonDialog::AddButton( StandardButtonType eType, sal_uInt16 nId,
287 sal_uInt16 nBtnFlags, long nSepPixel )
289 // PageItem anlegen
290 ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
291 pItem->mnId = nId;
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 ) );
309 pItem->mpPushButton->SetHelpText( Button::GetStandardHelpText( eType ) );
312 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
313 mnFocusButtonId = nId;
315 maItemList.push_back(pItem);
317 mbFormat = sal_True;
320 void ButtonDialog::RemoveButton( sal_uInt16 nId )
322 btn_iterator it;
323 for (it = maItemList.begin(); it != maItemList.end(); ++it)
325 if (it->mnId == nId)
327 it->mpPushButton->Hide();
329 if (it->mbOwnButton )
330 delete it->mpPushButton;
332 maItemList.erase(it);
333 break;
337 if (it == maItemList.end())
338 SAL_WARN( "vcl.window", "ButtonDialog::RemoveButton(): ButtonId invalid" );
341 void ButtonDialog::Clear()
343 for (btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
345 it->mpPushButton->Hide();
347 if (it->mbOwnButton )
348 delete it->mpPushButton;
351 maItemList.clear();
352 mbFormat = sal_True;
355 sal_uInt16 ButtonDialog::GetButtonId( sal_uInt16 nButton ) const
357 if ( nButton < maItemList.size() )
358 return maItemList[nButton].mnId;
359 else
360 return BUTTONDIALOG_BUTTON_NOTFOUND;
363 PushButton* ButtonDialog::GetPushButton( sal_uInt16 nId ) const
365 ImplBtnDlgItem* pItem = ImplGetItem( nId );
367 if ( pItem )
368 return pItem->mpPushButton;
369 else
370 return NULL;
373 void ButtonDialog::SetButtonText( sal_uInt16 nId, const XubString& rText )
375 ImplBtnDlgItem* pItem = ImplGetItem( nId );
377 if ( pItem )
379 pItem->mpPushButton->SetText( rText );
380 mbFormat = sal_True;
384 void ButtonDialog::SetButtonHelpText( sal_uInt16 nId, const XubString& rText )
386 ImplBtnDlgItem* pItem = ImplGetItem( nId );
388 if ( pItem )
389 pItem->mpPushButton->SetHelpText( rText );
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */