update dev300-m58
[ooovba.git] / vcl / source / window / btndlg.cxx
blob0667816de0fec063b87a4ffb173eb8652eb8b408
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: btndlg.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <tools/ref.hxx>
35 #include <tools/debug.hxx>
36 #include <vcl/svdata.hxx>
37 #include <vcl/button.hxx>
38 #include <vcl/btndlg.hxx>
40 #ifndef _SV_RC_H
41 #include <tools/rc.h>
42 #endif
46 // =======================================================================
48 struct ImplBtnDlgItem
50 USHORT mnId;
51 BOOL mbOwnButton;
52 BOOL mbDummyAlign;
53 long mnSepSize;
54 PushButton* mpPushButton;
57 DECLARE_LIST( ImplBtnDlgItemList, ImplBtnDlgItem* )
59 // =======================================================================
61 void ButtonDialog::ImplInitButtonDialogData()
63 mpItemList = new ImplBtnDlgItemList( 8, 8 );
64 mnButtonSize = 0;
65 mnCurButtonId = 0;
66 mnFocusButtonId = BUTTONDIALOG_BUTTON_NOTFOUND;
67 mbFormat = TRUE;
70 // -----------------------------------------------------------------------
72 ButtonDialog::ButtonDialog( WindowType nType ) :
73 Dialog( nType )
75 ImplInitButtonDialogData();
78 // -----------------------------------------------------------------------
80 ButtonDialog::ButtonDialog( Window* pParent, WinBits nStyle ) :
81 Dialog( WINDOW_BUTTONDIALOG )
83 ImplInitButtonDialogData();
84 ImplInit( pParent, nStyle );
87 // -----------------------------------------------------------------------
89 ButtonDialog::ButtonDialog( Window* pParent, const ResId& rResId ) :
90 Dialog( WINDOW_BUTTONDIALOG )
92 ImplInitButtonDialogData();
93 rResId.SetRT( RSC_DIALOG ); // !!!!!!!!!! RSC_BUTTONDIALOG !!!!!!!!
94 ImplInit( pParent, ImplInitRes( rResId ) );
95 ImplLoadRes( rResId );
98 // -----------------------------------------------------------------------
100 ButtonDialog::~ButtonDialog()
102 ImplBtnDlgItem* pItem = mpItemList->First();
103 while ( pItem )
105 if ( pItem->mpPushButton && pItem->mbOwnButton )
106 delete pItem->mpPushButton;
107 delete pItem;
108 pItem = mpItemList->Next();
111 delete mpItemList;
114 // -----------------------------------------------------------------------
116 PushButton* ButtonDialog::ImplCreatePushButton( USHORT nBtnFlags )
118 PushButton* pBtn;
119 WinBits nStyle = 0;
121 if ( nBtnFlags & BUTTONDIALOG_DEFBUTTON )
122 nStyle |= WB_DEFBUTTON;
123 if ( nBtnFlags & BUTTONDIALOG_CANCELBUTTON )
124 pBtn = new CancelButton( this, nStyle );
125 else if ( nBtnFlags & BUTTONDIALOG_OKBUTTON )
126 pBtn = new OKButton( this, nStyle );
127 else if ( nBtnFlags & BUTTONDIALOG_HELPBUTTON )
128 pBtn = new HelpButton( this, nStyle );
129 else
130 pBtn = new PushButton( this, nStyle );
132 if ( !(nBtnFlags & BUTTONDIALOG_HELPBUTTON) )
133 pBtn->SetClickHdl( LINK( this, ButtonDialog, ImplClickHdl ) );
135 return pBtn;
138 // -----------------------------------------------------------------------
140 ImplBtnDlgItem* ButtonDialog::ImplGetItem( USHORT nId ) const
142 ImplBtnDlgItem* pItem = mpItemList->First();
143 while ( pItem )
145 if ( pItem->mnId == nId )
146 return pItem;
148 pItem = mpItemList->Next();
151 return NULL;
154 // -----------------------------------------------------------------------
156 long ButtonDialog::ImplGetButtonSize()
158 if ( !mbFormat )
159 return mnButtonSize;
161 // Calculate ButtonSize
162 long nLastSepSize = 0;
163 long nSepSize = 0;
164 long nButtonCount = 0;
165 maCtrlSize = Size( IMPL_MINSIZE_BUTTON_WIDTH, IMPL_MINSIZE_BUTTON_HEIGHT );
166 ImplBtnDlgItem* pItem = mpItemList->First();
167 while ( pItem )
169 nSepSize += nLastSepSize;
171 long nTxtWidth = pItem->mpPushButton->GetCtrlTextWidth( pItem->mpPushButton->GetText() );
172 nTxtWidth += IMPL_EXTRA_BUTTON_WIDTH;
173 if ( nTxtWidth > maCtrlSize.Width() )
174 maCtrlSize.Width() = nTxtWidth;
175 long nTxtHeight = pItem->mpPushButton->GetTextHeight();
176 nTxtHeight += IMPL_EXTRA_BUTTON_HEIGHT;
177 if ( nTxtHeight > maCtrlSize.Height() )
178 maCtrlSize.Height() = nTxtHeight;
180 nSepSize += pItem->mnSepSize;
182 if ( GetStyle() & WB_HORZ )
183 nLastSepSize = IMPL_SEP_BUTTON_X;
184 else
185 nLastSepSize = IMPL_SEP_BUTTON_Y;
187 nButtonCount++;
189 pItem = mpItemList->Next();
192 if ( GetStyle() & WB_HORZ )
193 mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Width());
194 else
195 mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Height());
197 return mnButtonSize;
200 // -----------------------------------------------------------------------
202 void ButtonDialog::ImplPosControls()
204 if ( !mbFormat )
205 return;
207 // Create PushButtons and determine Sizes
208 ImplGetButtonSize();
210 // determine dialog size
211 ImplBtnDlgItem* pItem;
212 Size aDlgSize = maPageSize;
213 long nX;
214 long nY;
215 if ( GetStyle() & WB_HORZ )
217 if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Width() )
218 aDlgSize.Width() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
219 if ( GetStyle() & WB_LEFT )
220 nX = IMPL_DIALOG_OFFSET;
221 else if ( GetStyle() & WB_RIGHT )
222 nX = aDlgSize.Width()-mnButtonSize-IMPL_DIALOG_OFFSET;
223 else
224 nX = (aDlgSize.Width()-mnButtonSize)/2;
226 aDlgSize.Height() += IMPL_DIALOG_OFFSET+maCtrlSize.Height();
227 nY = aDlgSize.Height()-maCtrlSize.Height()-IMPL_DIALOG_OFFSET;
229 else
231 if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Height() )
232 aDlgSize.Height() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
233 if ( GetStyle() & WB_BOTTOM )
234 nY = aDlgSize.Height()-mnButtonSize-IMPL_DIALOG_OFFSET;
235 else if ( GetStyle() & WB_VCENTER )
236 nY = (aDlgSize.Height()-mnButtonSize)/2;
237 else
238 nY = IMPL_DIALOG_OFFSET;
240 aDlgSize.Width() += IMPL_DIALOG_OFFSET+maCtrlSize.Width();
241 nX = aDlgSize.Width()-maCtrlSize.Width()-IMPL_DIALOG_OFFSET;
244 // Arrange PushButtons
245 pItem = mpItemList->First();
246 while ( pItem )
248 if ( GetStyle() & WB_HORZ )
249 nX += pItem->mnSepSize;
250 else
251 nY += pItem->mnSepSize;
252 pItem->mpPushButton->SetPosSizePixel( Point( nX, nY ), maCtrlSize );
253 pItem->mpPushButton->Show();
254 if ( GetStyle() & WB_HORZ )
255 nX += maCtrlSize.Width()+IMPL_SEP_BUTTON_X;
256 else
257 nY += maCtrlSize.Height()+IMPL_SEP_BUTTON_Y;
259 pItem = mpItemList->Next();
262 SetOutputSizePixel( aDlgSize );
264 mbFormat = FALSE;
267 // -----------------------------------------------------------------------
269 IMPL_LINK( ButtonDialog, ImplClickHdl, PushButton*, pBtn )
271 ImplBtnDlgItem* pItem = mpItemList->First();
272 while ( pItem )
274 if ( pItem->mpPushButton == pBtn )
276 mnCurButtonId = pItem->mnId;
277 Click();
278 break;
281 pItem = mpItemList->Next();
284 return 0;
287 // -----------------------------------------------------------------------
289 void ButtonDialog::Resize()
293 // -----------------------------------------------------------------------
295 void ButtonDialog::StateChanged( StateChangedType nType )
297 if ( nType == STATE_CHANGE_INITSHOW )
299 ImplPosControls();
301 // Focus evt. auf den entsprechenden Button setzen
302 if ( mnFocusButtonId != BUTTONDIALOG_BUTTON_NOTFOUND )
304 ImplBtnDlgItem* pItem = mpItemList->First();
305 while ( pItem )
307 if ( pItem->mnId == mnFocusButtonId )
309 if ( pItem->mpPushButton->IsVisible() )
310 pItem->mpPushButton->GrabFocus();
311 break;
314 pItem = mpItemList->Next();
319 Dialog::StateChanged( nType );
322 // -----------------------------------------------------------------------
324 void ButtonDialog::Click()
326 if ( !maClickHdl )
328 if ( IsInExecute() )
329 EndDialog( GetCurButtonId() );
331 else
332 maClickHdl.Call( this );
335 // -----------------------------------------------------------------------
337 void ButtonDialog::AddButton( const XubString& rText, USHORT nId,
338 USHORT nBtnFlags, long nSepPixel )
340 // PageItem anlegen
341 ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
342 pItem->mnId = nId;
343 pItem->mbOwnButton = TRUE;
344 pItem->mnSepSize = nSepPixel;
345 pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
346 if ( rText.Len() )
347 pItem->mpPushButton->SetText( rText );
349 // In die Liste eintragen
350 mpItemList->Insert( pItem, LIST_APPEND );
352 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
353 mnFocusButtonId = nId;
355 mbFormat = TRUE;
358 // -----------------------------------------------------------------------
360 void ButtonDialog::AddButton( StandardButtonType eType, USHORT nId,
361 USHORT nBtnFlags, long nSepPixel )
363 // PageItem anlegen
364 ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
365 pItem->mnId = nId;
366 pItem->mbOwnButton = TRUE;
367 pItem->mnSepSize = nSepPixel;
369 if ( eType == BUTTON_OK )
370 nBtnFlags |= BUTTONDIALOG_OKBUTTON;
371 else if ( eType == BUTTON_HELP )
372 nBtnFlags |= BUTTONDIALOG_HELPBUTTON;
373 else if ( (eType == BUTTON_CANCEL) || (eType == BUTTON_CLOSE) )
374 nBtnFlags |= BUTTONDIALOG_CANCELBUTTON;
375 pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
377 // Standard-Buttons have the right text already
378 if ( !((eType == BUTTON_OK) && (pItem->mpPushButton->GetType() == WINDOW_OKBUTTON)) ||
379 !((eType == BUTTON_CANCEL) && (pItem->mpPushButton->GetType() == WINDOW_CANCELBUTTON)) ||
380 !((eType == BUTTON_HELP) && (pItem->mpPushButton->GetType() == WINDOW_HELPBUTTON)) )
382 pItem->mpPushButton->SetText( Button::GetStandardText( eType ) );
383 pItem->mpPushButton->SetHelpText( Button::GetStandardHelpText( eType ) );
386 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
387 mnFocusButtonId = nId;
389 // In die Liste eintragen
390 mpItemList->Insert( pItem, LIST_APPEND );
392 mbFormat = TRUE;
395 // -----------------------------------------------------------------------
397 void ButtonDialog::AddButton( PushButton* pBtn, USHORT nId,
398 USHORT nBtnFlags, long nSepPixel )
400 // PageItem anlegen
401 ImplBtnDlgItem* pItem = new ImplBtnDlgItem;
402 pItem->mnId = nId;
403 pItem->mbOwnButton = FALSE;
404 pItem->mnSepSize = nSepPixel;
405 pItem->mpPushButton = pBtn;
407 if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
408 mnFocusButtonId = nId;
410 // In die View-Liste eintragen
411 mpItemList->Insert( pItem, LIST_APPEND );
413 mbFormat = TRUE;
416 // -----------------------------------------------------------------------
418 void ButtonDialog::RemoveButton( USHORT nId )
420 ImplBtnDlgItem* pItem = mpItemList->First();
421 while ( pItem )
423 if ( pItem->mnId == nId )
425 pItem->mpPushButton->Hide();
426 if ( pItem->mbOwnButton )
427 delete pItem->mpPushButton;
428 delete pItem;
429 mpItemList->Remove();
430 mbFormat = TRUE;
431 break;
434 pItem = mpItemList->Next();
437 DBG_ERRORFILE( "ButtonDialog::RemoveButton(): ButtonId invalid" );
440 // -----------------------------------------------------------------------
442 void ButtonDialog::Clear()
444 ImplBtnDlgItem* pItem = mpItemList->First();
445 while ( pItem )
447 pItem->mpPushButton->Hide();
448 if ( pItem->mbOwnButton )
449 delete pItem->mpPushButton;
450 delete pItem;
451 pItem = mpItemList->Next();
454 mpItemList->Clear();
455 mbFormat = TRUE;
458 // -----------------------------------------------------------------------
460 USHORT ButtonDialog::GetButtonCount() const
462 return (USHORT)mpItemList->Count();
465 // -----------------------------------------------------------------------
467 USHORT ButtonDialog::GetButtonId( USHORT nButton ) const
469 if ( nButton < mpItemList->Count() )
470 return (USHORT)mpItemList->GetObject( nButton )->mnId;
471 else
472 return BUTTONDIALOG_BUTTON_NOTFOUND;
475 // -----------------------------------------------------------------------
477 PushButton* ButtonDialog::GetPushButton( USHORT nId ) const
479 ImplBtnDlgItem* pItem = ImplGetItem( nId );
481 if ( pItem )
482 return pItem->mpPushButton;
483 else
484 return NULL;
487 // -----------------------------------------------------------------------
489 void ButtonDialog::SetButtonText( USHORT nId, const XubString& rText )
491 ImplBtnDlgItem* pItem = ImplGetItem( nId );
493 if ( pItem )
495 pItem->mpPushButton->SetText( rText );
496 mbFormat = TRUE;
500 // -----------------------------------------------------------------------
502 XubString ButtonDialog::GetButtonText( USHORT nId ) const
504 ImplBtnDlgItem* pItem = ImplGetItem( nId );
506 if ( pItem )
507 return pItem->mpPushButton->GetText();
508 else
509 return ImplGetSVEmptyStr();
512 // -----------------------------------------------------------------------
514 void ButtonDialog::SetButtonHelpText( USHORT nId, const XubString& rText )
516 ImplBtnDlgItem* pItem = ImplGetItem( nId );
518 if ( pItem )
519 pItem->mpPushButton->SetHelpText( rText );
522 // -----------------------------------------------------------------------
524 XubString ButtonDialog::GetButtonHelpText( USHORT nId ) const
526 ImplBtnDlgItem* pItem = ImplGetItem( nId );
528 if ( pItem )
529 return pItem->mpPushButton->GetHelpText();
530 else
531 return ImplGetSVEmptyStr();
534 // -----------------------------------------------------------------------
536 void ButtonDialog::SetButtonHelpId( USHORT nId, ULONG nHelpId )
538 ImplBtnDlgItem* pItem = ImplGetItem( nId );
540 if ( pItem )
541 pItem->mpPushButton->SetHelpId( nHelpId );
544 // -----------------------------------------------------------------------
546 ULONG ButtonDialog::GetButtonHelpId( USHORT nId ) const
548 ImplBtnDlgItem* pItem = ImplGetItem( nId );
550 if ( pItem )
551 return pItem->mpPushButton->GetHelpId();
552 else
553 return 0;