Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / vcl / source / window / btndlg.cxx
blob84694beac4c36d65b308c2ed108f9368c6c63a16
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 OUString& 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.isEmpty())
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 ) );
311 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
312 mnFocusButtonId = nId;
314 maItemList.push_back(pItem);
316 mbFormat = sal_True;
319 void ButtonDialog::RemoveButton( sal_uInt16 nId )
321 btn_iterator it;
322 for (it = maItemList.begin(); it != maItemList.end(); ++it)
324 if (it->mnId == nId)
326 it->mpPushButton->Hide();
328 if (it->mbOwnButton )
329 delete it->mpPushButton;
331 maItemList.erase(it);
332 break;
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;
350 maItemList.clear();
351 mbFormat = sal_True;
354 sal_uInt16 ButtonDialog::GetButtonId( sal_uInt16 nButton ) const
356 if ( nButton < maItemList.size() )
357 return maItemList[nButton].mnId;
358 else
359 return BUTTONDIALOG_BUTTON_NOTFOUND;
362 PushButton* ButtonDialog::GetPushButton( sal_uInt16 nId ) const
364 ImplBtnDlgItem* pItem = ImplGetItem( nId );
366 if ( pItem )
367 return pItem->mpPushButton;
368 else
369 return NULL;
372 void ButtonDialog::SetButtonText( sal_uInt16 nId, const OUString& rText )
374 ImplBtnDlgItem* pItem = ImplGetItem( nId );
376 if ( pItem )
378 pItem->mpPushButton->SetText( rText );
379 mbFormat = sal_True;
383 void ButtonDialog::SetButtonHelpText( sal_uInt16 nId, const OUString& rText )
385 ImplBtnDlgItem* pItem = ImplGetItem( nId );
387 if ( pItem )
388 pItem->mpPushButton->SetHelpText( rText );
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */