1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: btndlg.cxx,v $
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>
46 // =======================================================================
54 PushButton
* mpPushButton
;
57 DECLARE_LIST( ImplBtnDlgItemList
, ImplBtnDlgItem
* )
59 // =======================================================================
61 void ButtonDialog::ImplInitButtonDialogData()
63 mpItemList
= new ImplBtnDlgItemList( 8, 8 );
66 mnFocusButtonId
= BUTTONDIALOG_BUTTON_NOTFOUND
;
70 // -----------------------------------------------------------------------
72 ButtonDialog::ButtonDialog( WindowType 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();
105 if ( pItem
->mpPushButton
&& pItem
->mbOwnButton
)
106 delete pItem
->mpPushButton
;
108 pItem
= mpItemList
->Next();
114 // -----------------------------------------------------------------------
116 PushButton
* ButtonDialog::ImplCreatePushButton( USHORT nBtnFlags
)
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
);
130 pBtn
= new PushButton( this, nStyle
);
132 if ( !(nBtnFlags
& BUTTONDIALOG_HELPBUTTON
) )
133 pBtn
->SetClickHdl( LINK( this, ButtonDialog
, ImplClickHdl
) );
138 // -----------------------------------------------------------------------
140 ImplBtnDlgItem
* ButtonDialog::ImplGetItem( USHORT nId
) const
142 ImplBtnDlgItem
* pItem
= mpItemList
->First();
145 if ( pItem
->mnId
== nId
)
148 pItem
= mpItemList
->Next();
154 // -----------------------------------------------------------------------
156 long ButtonDialog::ImplGetButtonSize()
161 // Calculate ButtonSize
162 long nLastSepSize
= 0;
164 long nButtonCount
= 0;
165 maCtrlSize
= Size( IMPL_MINSIZE_BUTTON_WIDTH
, IMPL_MINSIZE_BUTTON_HEIGHT
);
166 ImplBtnDlgItem
* pItem
= mpItemList
->First();
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
;
185 nLastSepSize
= IMPL_SEP_BUTTON_Y
;
189 pItem
= mpItemList
->Next();
192 if ( GetStyle() & WB_HORZ
)
193 mnButtonSize
= nSepSize
+ (nButtonCount
*maCtrlSize
.Width());
195 mnButtonSize
= nSepSize
+ (nButtonCount
*maCtrlSize
.Height());
200 // -----------------------------------------------------------------------
202 void ButtonDialog::ImplPosControls()
207 // Create PushButtons and determine Sizes
210 // determine dialog size
211 ImplBtnDlgItem
* pItem
;
212 Size aDlgSize
= maPageSize
;
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
;
224 nX
= (aDlgSize
.Width()-mnButtonSize
)/2;
226 aDlgSize
.Height() += IMPL_DIALOG_OFFSET
+maCtrlSize
.Height();
227 nY
= aDlgSize
.Height()-maCtrlSize
.Height()-IMPL_DIALOG_OFFSET
;
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;
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();
248 if ( GetStyle() & WB_HORZ
)
249 nX
+= pItem
->mnSepSize
;
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
;
257 nY
+= maCtrlSize
.Height()+IMPL_SEP_BUTTON_Y
;
259 pItem
= mpItemList
->Next();
262 SetOutputSizePixel( aDlgSize
);
267 // -----------------------------------------------------------------------
269 IMPL_LINK( ButtonDialog
, ImplClickHdl
, PushButton
*, pBtn
)
271 ImplBtnDlgItem
* pItem
= mpItemList
->First();
274 if ( pItem
->mpPushButton
== pBtn
)
276 mnCurButtonId
= pItem
->mnId
;
281 pItem
= mpItemList
->Next();
287 // -----------------------------------------------------------------------
289 void ButtonDialog::Resize()
293 // -----------------------------------------------------------------------
295 void ButtonDialog::StateChanged( StateChangedType nType
)
297 if ( nType
== STATE_CHANGE_INITSHOW
)
301 // Focus evt. auf den entsprechenden Button setzen
302 if ( mnFocusButtonId
!= BUTTONDIALOG_BUTTON_NOTFOUND
)
304 ImplBtnDlgItem
* pItem
= mpItemList
->First();
307 if ( pItem
->mnId
== mnFocusButtonId
)
309 if ( pItem
->mpPushButton
->IsVisible() )
310 pItem
->mpPushButton
->GrabFocus();
314 pItem
= mpItemList
->Next();
319 Dialog::StateChanged( nType
);
322 // -----------------------------------------------------------------------
324 void ButtonDialog::Click()
329 EndDialog( GetCurButtonId() );
332 maClickHdl
.Call( this );
335 // -----------------------------------------------------------------------
337 void ButtonDialog::AddButton( const XubString
& rText
, USHORT nId
,
338 USHORT nBtnFlags
, long nSepPixel
)
341 ImplBtnDlgItem
* pItem
= new ImplBtnDlgItem
;
343 pItem
->mbOwnButton
= TRUE
;
344 pItem
->mnSepSize
= nSepPixel
;
345 pItem
->mpPushButton
= ImplCreatePushButton( nBtnFlags
);
347 pItem
->mpPushButton
->SetText( rText
);
349 // In die Liste eintragen
350 mpItemList
->Insert( pItem
, LIST_APPEND
);
352 if ( nBtnFlags
& BUTTONDIALOG_FOCUSBUTTON
)
353 mnFocusButtonId
= nId
;
358 // -----------------------------------------------------------------------
360 void ButtonDialog::AddButton( StandardButtonType eType
, USHORT nId
,
361 USHORT nBtnFlags
, long nSepPixel
)
364 ImplBtnDlgItem
* pItem
= new ImplBtnDlgItem
;
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
);
395 // -----------------------------------------------------------------------
397 void ButtonDialog::AddButton( PushButton
* pBtn
, USHORT nId
,
398 USHORT nBtnFlags
, long nSepPixel
)
401 ImplBtnDlgItem
* pItem
= new ImplBtnDlgItem
;
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
);
416 // -----------------------------------------------------------------------
418 void ButtonDialog::RemoveButton( USHORT nId
)
420 ImplBtnDlgItem
* pItem
= mpItemList
->First();
423 if ( pItem
->mnId
== nId
)
425 pItem
->mpPushButton
->Hide();
426 if ( pItem
->mbOwnButton
)
427 delete pItem
->mpPushButton
;
429 mpItemList
->Remove();
434 pItem
= mpItemList
->Next();
437 DBG_ERRORFILE( "ButtonDialog::RemoveButton(): ButtonId invalid" );
440 // -----------------------------------------------------------------------
442 void ButtonDialog::Clear()
444 ImplBtnDlgItem
* pItem
= mpItemList
->First();
447 pItem
->mpPushButton
->Hide();
448 if ( pItem
->mbOwnButton
)
449 delete pItem
->mpPushButton
;
451 pItem
= mpItemList
->Next();
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
;
472 return BUTTONDIALOG_BUTTON_NOTFOUND
;
475 // -----------------------------------------------------------------------
477 PushButton
* ButtonDialog::GetPushButton( USHORT nId
) const
479 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
482 return pItem
->mpPushButton
;
487 // -----------------------------------------------------------------------
489 void ButtonDialog::SetButtonText( USHORT nId
, const XubString
& rText
)
491 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
495 pItem
->mpPushButton
->SetText( rText
);
500 // -----------------------------------------------------------------------
502 XubString
ButtonDialog::GetButtonText( USHORT nId
) const
504 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
507 return pItem
->mpPushButton
->GetText();
509 return ImplGetSVEmptyStr();
512 // -----------------------------------------------------------------------
514 void ButtonDialog::SetButtonHelpText( USHORT nId
, const XubString
& rText
)
516 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
519 pItem
->mpPushButton
->SetHelpText( rText
);
522 // -----------------------------------------------------------------------
524 XubString
ButtonDialog::GetButtonHelpText( USHORT nId
) const
526 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
529 return pItem
->mpPushButton
->GetHelpText();
531 return ImplGetSVEmptyStr();
534 // -----------------------------------------------------------------------
536 void ButtonDialog::SetButtonHelpId( USHORT nId
, ULONG nHelpId
)
538 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
541 pItem
->mpPushButton
->SetHelpId( nHelpId
);
544 // -----------------------------------------------------------------------
546 ULONG
ButtonDialog::GetButtonHelpId( USHORT nId
) const
548 ImplBtnDlgItem
* pItem
= ImplGetItem( nId
);
551 return pItem
->mpPushButton
->GetHelpId();