Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <tools/rc.h>
31 : :
32 : : #include <svdata.hxx>
33 : :
34 : : #include <vcl/button.hxx>
35 : : #include <vcl/btndlg.hxx>
36 : :
37 : : typedef boost::ptr_vector<ImplBtnDlgItem>::iterator btn_iterator;
38 : : typedef boost::ptr_vector<ImplBtnDlgItem>::const_iterator btn_const_iterator;
39 : :
40 : : struct ImplBtnDlgItem
41 : : {
42 : : sal_uInt16 mnId;
43 : : bool mbOwnButton;
44 : : bool mbDummyAlign;
45 : : long mnSepSize;
46 : : PushButton* mpPushButton;
47 : : };
48 : :
49 : 0 : void ButtonDialog::ImplInitButtonDialogData()
50 : : {
51 : 0 : mnButtonSize = 0;
52 : 0 : mnCurButtonId = 0;
53 : 0 : mnFocusButtonId = BUTTONDIALOG_BUTTON_NOTFOUND;
54 : 0 : mbFormat = sal_True;
55 : 0 : }
56 : :
57 : 0 : ButtonDialog::ButtonDialog( WindowType nType ) :
58 [ # # ][ # # ]: 0 : Dialog( nType )
59 : : {
60 : 0 : ImplInitButtonDialogData();
61 : 0 : }
62 : :
63 : 0 : ButtonDialog::ButtonDialog( Window* pParent, WinBits nStyle ) :
64 [ # # ][ # # ]: 0 : Dialog( WINDOW_BUTTONDIALOG )
65 : : {
66 : 0 : ImplInitButtonDialogData();
67 [ # # ]: 0 : ImplInit( pParent, nStyle );
68 : 0 : }
69 : :
70 [ # # ]: 0 : ButtonDialog::~ButtonDialog()
71 : : {
72 [ # # ][ # # ]: 0 : for ( btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
73 : : {
74 [ # # ][ # # ]: 0 : if ( it->mpPushButton && it->mbOwnButton )
[ # # ][ # # ]
[ # # ]
75 [ # # ][ # # ]: 0 : delete it->mpPushButton;
[ # # ]
76 : : }
77 [ # # ]: 0 : }
78 : :
79 : 0 : PushButton* ButtonDialog::ImplCreatePushButton( sal_uInt16 nBtnFlags )
80 : : {
81 : : PushButton* pBtn;
82 : 0 : WinBits nStyle = 0;
83 : :
84 [ # # ]: 0 : if ( nBtnFlags & BUTTONDIALOG_DEFBUTTON )
85 : 0 : nStyle |= WB_DEFBUTTON;
86 [ # # ]: 0 : if ( nBtnFlags & BUTTONDIALOG_CANCELBUTTON )
87 [ # # ]: 0 : pBtn = new CancelButton( this, nStyle );
88 [ # # ]: 0 : else if ( nBtnFlags & BUTTONDIALOG_OKBUTTON )
89 [ # # ]: 0 : pBtn = new OKButton( this, nStyle );
90 [ # # ]: 0 : else if ( nBtnFlags & BUTTONDIALOG_HELPBUTTON )
91 [ # # ]: 0 : pBtn = new HelpButton( this, nStyle );
92 : : else
93 [ # # ]: 0 : pBtn = new PushButton( this, nStyle );
94 : :
95 [ # # ]: 0 : if ( !(nBtnFlags & BUTTONDIALOG_HELPBUTTON) )
96 : 0 : pBtn->SetClickHdl( LINK( this, ButtonDialog, ImplClickHdl ) );
97 : :
98 : 0 : return pBtn;
99 : : }
100 : :
101 : 0 : ImplBtnDlgItem* ButtonDialog::ImplGetItem( sal_uInt16 nId ) const
102 : : {
103 [ # # ][ # # ]: 0 : for ( btn_const_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
104 : : {
105 [ # # ][ # # ]: 0 : if (it->mnId == nId)
106 [ # # ]: 0 : return const_cast<ImplBtnDlgItem*>(&(*it));
107 : : }
108 : :
109 : 0 : return NULL;
110 : : }
111 : :
112 : 0 : long ButtonDialog::ImplGetButtonSize()
113 : : {
114 [ # # ]: 0 : if ( !mbFormat )
115 : 0 : return mnButtonSize;
116 : :
117 : : // Calculate ButtonSize
118 : 0 : long nLastSepSize = 0;
119 : 0 : long nSepSize = 0;
120 : 0 : maCtrlSize = Size( IMPL_MINSIZE_BUTTON_WIDTH, IMPL_MINSIZE_BUTTON_HEIGHT );
121 : :
122 [ # # ][ # # ]: 0 : for ( btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
123 : : {
124 : 0 : nSepSize += nLastSepSize;
125 : :
126 [ # # ][ # # ]: 0 : long nTxtWidth = it->mpPushButton->GetCtrlTextWidth(it->mpPushButton->GetText());
[ # # ][ # # ]
[ # # ]
127 : 0 : nTxtWidth += IMPL_EXTRA_BUTTON_WIDTH;
128 : :
129 [ # # ]: 0 : if ( nTxtWidth > maCtrlSize.Width() )
130 : 0 : maCtrlSize.Width() = nTxtWidth;
131 : :
132 [ # # ][ # # ]: 0 : long nTxtHeight = it->mpPushButton->GetTextHeight();
133 : 0 : nTxtHeight += IMPL_EXTRA_BUTTON_HEIGHT;
134 : :
135 [ # # ]: 0 : if ( nTxtHeight > maCtrlSize.Height() )
136 : 0 : maCtrlSize.Height() = nTxtHeight;
137 : :
138 [ # # ]: 0 : nSepSize += it->mnSepSize;
139 : :
140 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_HORZ )
141 : 0 : nLastSepSize = IMPL_SEP_BUTTON_X;
142 : : else
143 : 0 : nLastSepSize = IMPL_SEP_BUTTON_Y;
144 : : }
145 : :
146 : 0 : long nButtonCount = maItemList.size();
147 : :
148 [ # # ]: 0 : if ( GetStyle() & WB_HORZ )
149 : 0 : mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Width());
150 : : else
151 : 0 : mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Height());
152 : :
153 : 0 : return mnButtonSize;
154 : : }
155 : :
156 : 0 : void ButtonDialog::ImplPosControls()
157 : : {
158 [ # # ]: 0 : if ( !mbFormat )
159 : 0 : return;
160 : :
161 : : // Create PushButtons and determine Sizes
162 [ # # ]: 0 : ImplGetButtonSize();
163 : :
164 : : // determine dialog size
165 : 0 : Size aDlgSize = maPageSize;
166 : : long nX;
167 : : long nY;
168 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_HORZ )
169 : : {
170 [ # # ]: 0 : if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Width() )
171 : 0 : aDlgSize.Width() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
172 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_LEFT )
173 : 0 : nX = IMPL_DIALOG_OFFSET;
174 [ # # ][ # # ]: 0 : else if ( GetStyle() & WB_RIGHT )
175 : 0 : nX = aDlgSize.Width()-mnButtonSize-IMPL_DIALOG_OFFSET;
176 : : else
177 : 0 : nX = (aDlgSize.Width()-mnButtonSize)/2;
178 : :
179 : 0 : aDlgSize.Height() += IMPL_DIALOG_OFFSET+maCtrlSize.Height();
180 : 0 : nY = aDlgSize.Height()-maCtrlSize.Height()-IMPL_DIALOG_OFFSET;
181 : : }
182 : : else
183 : : {
184 [ # # ]: 0 : if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Height() )
185 : 0 : aDlgSize.Height() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
186 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_BOTTOM )
187 : 0 : nY = aDlgSize.Height()-mnButtonSize-IMPL_DIALOG_OFFSET;
188 [ # # ][ # # ]: 0 : else if ( GetStyle() & WB_VCENTER )
189 : 0 : nY = (aDlgSize.Height()-mnButtonSize)/2;
190 : : else
191 : 0 : nY = IMPL_DIALOG_OFFSET;
192 : :
193 : 0 : aDlgSize.Width() += IMPL_DIALOG_OFFSET+maCtrlSize.Width();
194 : 0 : nX = aDlgSize.Width()-maCtrlSize.Width()-IMPL_DIALOG_OFFSET;
195 : : }
196 : :
197 : : // Arrange PushButtons
198 [ # # ][ # # ]: 0 : for ( btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
199 : : {
200 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_HORZ )
201 [ # # ]: 0 : nX += it->mnSepSize;
202 : : else
203 [ # # ]: 0 : nY += it->mnSepSize;
204 : :
205 [ # # ][ # # ]: 0 : it->mpPushButton->SetPosSizePixel( Point( nX, nY ), maCtrlSize );
206 [ # # ][ # # ]: 0 : it->mpPushButton->Show();
207 : :
208 [ # # ][ # # ]: 0 : if ( GetStyle() & WB_HORZ )
209 : 0 : nX += maCtrlSize.Width()+IMPL_SEP_BUTTON_X;
210 : : else
211 : 0 : nY += maCtrlSize.Height()+IMPL_SEP_BUTTON_Y;
212 : : }
213 : :
214 [ # # ]: 0 : SetOutputSizePixel( aDlgSize );
215 : :
216 : 0 : mbFormat = sal_False;
217 : : }
218 : :
219 : 0 : IMPL_LINK( ButtonDialog, ImplClickHdl, PushButton*, pBtn )
220 : : {
221 [ # # ][ # # ]: 0 : for ( btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
222 : : {
223 [ # # ][ # # ]: 0 : if ( it->mpPushButton == pBtn )
224 : : {
225 [ # # ]: 0 : mnCurButtonId = it->mnId;
226 [ # # ]: 0 : Click();
227 : 0 : break;
228 : : }
229 : : }
230 : :
231 : 0 : return 0;
232 : : }
233 : :
234 : 0 : void ButtonDialog::Resize()
235 : : {
236 : 0 : }
237 : :
238 : 0 : void ButtonDialog::StateChanged( StateChangedType nType )
239 : : {
240 [ # # ]: 0 : if ( nType == STATE_CHANGE_INITSHOW )
241 : : {
242 : 0 : ImplPosControls();
243 : :
244 : : // Set focus on default button.
245 [ # # ]: 0 : if ( mnFocusButtonId != BUTTONDIALOG_BUTTON_NOTFOUND )
246 : : {
247 [ # # ][ # # ]: 0 : for ( btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
248 : : {
249 [ # # ][ # # ]: 0 : if (it->mnId == mnFocusButtonId )
250 : : {
251 [ # # ][ # # ]: 0 : if (it->mpPushButton->IsVisible())
[ # # ]
252 [ # # ][ # # ]: 0 : it->mpPushButton->GrabFocus();
253 : :
254 : 0 : break;
255 : : }
256 : : }
257 : : }
258 : : }
259 : :
260 : 0 : Dialog::StateChanged( nType );
261 : 0 : }
262 : :
263 : 0 : void ButtonDialog::Click()
264 : : {
265 [ # # ]: 0 : if ( !maClickHdl )
266 : : {
267 [ # # ]: 0 : if ( IsInExecute() )
268 : 0 : EndDialog( GetCurButtonId() );
269 : : }
270 : : else
271 : 0 : maClickHdl.Call( this );
272 : 0 : }
273 : :
274 : 0 : void ButtonDialog::AddButton( const XubString& rText, sal_uInt16 nId,
275 : : sal_uInt16 nBtnFlags, long nSepPixel )
276 : : {
277 : : // PageItem anlegen
278 : 0 : ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
279 : 0 : pItem->mnId = nId;
280 : 0 : pItem->mbOwnButton = sal_True;
281 : 0 : pItem->mnSepSize = nSepPixel;
282 : 0 : pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
283 : :
284 [ # # ]: 0 : if ( rText.Len() )
285 : 0 : pItem->mpPushButton->SetText( rText );
286 : :
287 : 0 : maItemList.push_back(pItem);
288 : :
289 [ # # ]: 0 : if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
290 : 0 : mnFocusButtonId = nId;
291 : :
292 : 0 : mbFormat = sal_True;
293 : 0 : }
294 : :
295 : 0 : void ButtonDialog::AddButton( StandardButtonType eType, sal_uInt16 nId,
296 : : sal_uInt16 nBtnFlags, long nSepPixel )
297 : : {
298 : : // PageItem anlegen
299 : 0 : ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
300 : 0 : pItem->mnId = nId;
301 : 0 : pItem->mbOwnButton = sal_True;
302 : 0 : pItem->mnSepSize = nSepPixel;
303 : :
304 [ # # ]: 0 : if ( eType == BUTTON_OK )
305 : 0 : nBtnFlags |= BUTTONDIALOG_OKBUTTON;
306 [ # # ]: 0 : else if ( eType == BUTTON_HELP )
307 : 0 : nBtnFlags |= BUTTONDIALOG_HELPBUTTON;
308 [ # # ][ # # ]: 0 : else if ( (eType == BUTTON_CANCEL) || (eType == BUTTON_CLOSE) )
309 : 0 : nBtnFlags |= BUTTONDIALOG_CANCELBUTTON;
310 : 0 : pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
311 : :
312 : : // Standard-Buttons have the right text already
313 [ # # ][ # # ]: 0 : if ( !((eType == BUTTON_OK) && (pItem->mpPushButton->GetType() == WINDOW_OKBUTTON)) ||
[ # # ][ # # ]
[ # # ]
314 [ # # ]: 0 : !((eType == BUTTON_CANCEL) && (pItem->mpPushButton->GetType() == WINDOW_CANCELBUTTON)) ||
315 [ # # ]: 0 : !((eType == BUTTON_HELP) && (pItem->mpPushButton->GetType() == WINDOW_HELPBUTTON)) )
316 : : {
317 [ # # ]: 0 : pItem->mpPushButton->SetText( Button::GetStandardText( eType ) );
318 [ # # ]: 0 : pItem->mpPushButton->SetHelpText( Button::GetStandardHelpText( eType ) );
319 : : }
320 : :
321 [ # # ]: 0 : if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
322 : 0 : mnFocusButtonId = nId;
323 : :
324 : 0 : maItemList.push_back(pItem);
325 : :
326 : 0 : mbFormat = sal_True;
327 : 0 : }
328 : :
329 : 0 : void ButtonDialog::RemoveButton( sal_uInt16 nId )
330 : : {
331 [ # # ]: 0 : btn_iterator it;
332 [ # # ][ # # ]: 0 : for (it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
333 : : {
334 [ # # ][ # # ]: 0 : if (it->mnId == nId)
335 : : {
336 [ # # ][ # # ]: 0 : it->mpPushButton->Hide();
337 : :
338 [ # # ][ # # ]: 0 : if (it->mbOwnButton )
339 [ # # ][ # # ]: 0 : delete it->mpPushButton;
[ # # ]
340 : :
341 [ # # ]: 0 : maItemList.erase(it);
342 : 0 : break;
343 : : }
344 : : }
345 : :
346 [ # # ][ # # ]: 0 : if (it == maItemList.end())
347 : : SAL_WARN( "vcl.window", "ButtonDialog::RemoveButton(): ButtonId invalid" );
348 : 0 : }
349 : :
350 : 0 : void ButtonDialog::Clear()
351 : : {
352 [ # # ][ # # ]: 0 : for (btn_iterator it = maItemList.begin(); it != maItemList.end(); ++it)
[ # # ][ # # ]
[ # # ]
353 : : {
354 [ # # ][ # # ]: 0 : it->mpPushButton->Hide();
355 : :
356 [ # # ][ # # ]: 0 : if (it->mbOwnButton )
357 [ # # ][ # # ]: 0 : delete it->mpPushButton;
[ # # ]
358 : : }
359 : :
360 : 0 : maItemList.clear();
361 : 0 : mbFormat = sal_True;
362 : 0 : }
363 : :
364 : 0 : sal_uInt16 ButtonDialog::GetButtonId( sal_uInt16 nButton ) const
365 : : {
366 [ # # ]: 0 : if ( nButton < maItemList.size() )
367 : 0 : return maItemList[nButton].mnId;
368 : : else
369 : 0 : return BUTTONDIALOG_BUTTON_NOTFOUND;
370 : : }
371 : :
372 : 0 : PushButton* ButtonDialog::GetPushButton( sal_uInt16 nId ) const
373 : : {
374 : 0 : ImplBtnDlgItem* pItem = ImplGetItem( nId );
375 : :
376 [ # # ]: 0 : if ( pItem )
377 : 0 : return pItem->mpPushButton;
378 : : else
379 : 0 : return NULL;
380 : : }
381 : :
382 : 0 : void ButtonDialog::SetButtonText( sal_uInt16 nId, const XubString& rText )
383 : : {
384 : 0 : ImplBtnDlgItem* pItem = ImplGetItem( nId );
385 : :
386 [ # # ]: 0 : if ( pItem )
387 : : {
388 : 0 : pItem->mpPushButton->SetText( rText );
389 : 0 : mbFormat = sal_True;
390 : : }
391 : 0 : }
392 : :
393 : 0 : void ButtonDialog::SetButtonHelpText( sal_uInt16 nId, const XubString& rText )
394 : : {
395 : 0 : ImplBtnDlgItem* pItem = ImplGetItem( nId );
396 : :
397 [ # # ]: 0 : if ( pItem )
398 : 0 : pItem->mpPushButton->SetHelpText( rText );
399 : 0 : }
400 : :
401 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|