xref: /AOO41X/main/vcl/source/window/btndlg.cxx (revision 4d7c9de063a797b8b4f3d45e3561e82ad1f8ef1f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <tools/ref.hxx>
28 #include <tools/debug.hxx>
29 #include <tools/rc.h>
30 
31 #include <svdata.hxx>
32 
33 #include <vcl/button.hxx>
34 #include <vcl/btndlg.hxx>
35 
36 
37 
38 // =======================================================================
39 
40 struct ImplBtnDlgItem
41 {
42     sal_uInt16              mnId;
43     sal_Bool                mbOwnButton;
44     sal_Bool                mbDummyAlign;
45     long                mnSepSize;
46     PushButton*         mpPushButton;
47 };
48 
DECLARE_LIST(ImplBtnDlgItemList,ImplBtnDlgItem *)49 DECLARE_LIST( ImplBtnDlgItemList, ImplBtnDlgItem* )
50 
51 // =======================================================================
52 
53 void ButtonDialog::ImplInitButtonDialogData()
54 {
55     mpItemList              = new ImplBtnDlgItemList( 8, 8 );
56     mnButtonSize            = 0;
57     mnCurButtonId           = 0;
58     mnFocusButtonId         = BUTTONDIALOG_BUTTON_NOTFOUND;
59     mbFormat                = sal_True;
60 }
61 
62 // -----------------------------------------------------------------------
63 
ButtonDialog(WindowType nType)64 ButtonDialog::ButtonDialog( WindowType nType ) :
65     Dialog( nType )
66 {
67     ImplInitButtonDialogData();
68 }
69 
70 // -----------------------------------------------------------------------
71 
ButtonDialog(Window * pParent,WinBits nStyle)72 ButtonDialog::ButtonDialog( Window* pParent, WinBits nStyle ) :
73     Dialog( WINDOW_BUTTONDIALOG )
74 {
75     ImplInitButtonDialogData();
76     ImplInit( pParent, nStyle );
77 }
78 
79 // -----------------------------------------------------------------------
80 
ButtonDialog(Window * pParent,const ResId & rResId)81 ButtonDialog::ButtonDialog( Window* pParent, const ResId& rResId ) :
82     Dialog( WINDOW_BUTTONDIALOG )
83 {
84     ImplInitButtonDialogData();
85     rResId.SetRT( RSC_DIALOG );     // !!!!!!!!!! RSC_BUTTONDIALOG !!!!!!!!
86     ImplInit( pParent, ImplInitRes( rResId ) );
87     ImplLoadRes( rResId );
88 }
89 
90 // -----------------------------------------------------------------------
91 
~ButtonDialog()92 ButtonDialog::~ButtonDialog()
93 {
94     ImplBtnDlgItem* pItem = mpItemList->First();
95     while ( pItem )
96     {
97         if ( pItem->mpPushButton && pItem->mbOwnButton )
98             delete pItem->mpPushButton;
99         delete pItem;
100         pItem = mpItemList->Next();
101     }
102 
103     delete mpItemList;
104 }
105 
106 // -----------------------------------------------------------------------
107 
ImplCreatePushButton(sal_uInt16 nBtnFlags)108 PushButton* ButtonDialog::ImplCreatePushButton( sal_uInt16 nBtnFlags )
109 {
110     PushButton* pBtn;
111     WinBits     nStyle = 0;
112 
113     if ( nBtnFlags & BUTTONDIALOG_DEFBUTTON )
114         nStyle |= WB_DEFBUTTON;
115     if ( nBtnFlags & BUTTONDIALOG_CANCELBUTTON )
116         pBtn = new CancelButton( this, nStyle );
117     else if ( nBtnFlags & BUTTONDIALOG_OKBUTTON )
118         pBtn = new OKButton( this, nStyle );
119     else if ( nBtnFlags & BUTTONDIALOG_HELPBUTTON )
120         pBtn = new HelpButton( this, nStyle );
121     else
122         pBtn = new PushButton( this, nStyle );
123 
124     if ( !(nBtnFlags & BUTTONDIALOG_HELPBUTTON) )
125         pBtn->SetClickHdl( LINK( this, ButtonDialog, ImplClickHdl ) );
126 
127     return pBtn;
128 }
129 
130 // -----------------------------------------------------------------------
131 
ImplGetItem(sal_uInt16 nId) const132 ImplBtnDlgItem* ButtonDialog::ImplGetItem( sal_uInt16 nId ) const
133 {
134     ImplBtnDlgItem* pItem = mpItemList->First();
135     while ( pItem )
136     {
137         if ( pItem->mnId == nId )
138             return pItem;
139 
140         pItem = mpItemList->Next();
141     }
142 
143     return NULL;
144 }
145 
146 // -----------------------------------------------------------------------
147 
ImplGetButtonSize()148 long ButtonDialog::ImplGetButtonSize()
149 {
150     if ( !mbFormat )
151         return mnButtonSize;
152 
153     // Calculate ButtonSize
154     long    nLastSepSize = 0;
155     long    nSepSize = 0;
156     long    nButtonCount = 0;
157     maCtrlSize = Size( IMPL_MINSIZE_BUTTON_WIDTH, IMPL_MINSIZE_BUTTON_HEIGHT );
158     ImplBtnDlgItem* pItem = mpItemList->First();
159     while ( pItem )
160     {
161         nSepSize += nLastSepSize;
162 
163         long nTxtWidth = pItem->mpPushButton->GetCtrlTextWidth( pItem->mpPushButton->GetText() );
164         nTxtWidth += IMPL_EXTRA_BUTTON_WIDTH;
165         if ( nTxtWidth > maCtrlSize.Width() )
166             maCtrlSize.Width() = nTxtWidth;
167         long nTxtHeight = pItem->mpPushButton->GetTextHeight();
168         nTxtHeight += IMPL_EXTRA_BUTTON_HEIGHT;
169         if ( nTxtHeight > maCtrlSize.Height() )
170             maCtrlSize.Height() = nTxtHeight;
171 
172         nSepSize += pItem->mnSepSize;
173 
174         if ( GetStyle() & WB_HORZ )
175             nLastSepSize = IMPL_SEP_BUTTON_X;
176         else
177             nLastSepSize = IMPL_SEP_BUTTON_Y;
178 
179         nButtonCount++;
180 
181         pItem = mpItemList->Next();
182     }
183 
184     if ( GetStyle() & WB_HORZ )
185         mnButtonSize  = nSepSize + (nButtonCount*maCtrlSize.Width());
186     else
187         mnButtonSize = nSepSize + (nButtonCount*maCtrlSize.Height());
188 
189     return mnButtonSize;
190 }
191 
192 // -----------------------------------------------------------------------
193 
ImplPosControls()194 void ButtonDialog::ImplPosControls()
195 {
196     if ( !mbFormat )
197         return;
198 
199     // Create PushButtons and determine Sizes
200     ImplGetButtonSize();
201 
202     // determine dialog size
203     ImplBtnDlgItem* pItem;
204     Size            aDlgSize = maPageSize;
205     long            nX;
206     long            nY;
207     if ( GetStyle() & WB_HORZ )
208     {
209         if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Width() )
210             aDlgSize.Width() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
211         if ( GetStyle() & WB_LEFT )
212             nX = IMPL_DIALOG_OFFSET;
213         else if ( GetStyle() & WB_RIGHT )
214             nX = aDlgSize.Width()-mnButtonSize-IMPL_DIALOG_OFFSET;
215         else
216             nX = (aDlgSize.Width()-mnButtonSize)/2;
217 
218         aDlgSize.Height() += IMPL_DIALOG_OFFSET+maCtrlSize.Height();
219         nY = aDlgSize.Height()-maCtrlSize.Height()-IMPL_DIALOG_OFFSET;
220     }
221     else
222     {
223         if ( mnButtonSize+(IMPL_DIALOG_OFFSET*2) > aDlgSize.Height() )
224             aDlgSize.Height() = mnButtonSize+(IMPL_DIALOG_OFFSET*2);
225         if ( GetStyle() & WB_BOTTOM )
226             nY = aDlgSize.Height()-mnButtonSize-IMPL_DIALOG_OFFSET;
227         else if ( GetStyle() & WB_VCENTER )
228             nY = (aDlgSize.Height()-mnButtonSize)/2;
229         else
230             nY = IMPL_DIALOG_OFFSET;
231 
232         aDlgSize.Width() += IMPL_DIALOG_OFFSET+maCtrlSize.Width();
233         nX = aDlgSize.Width()-maCtrlSize.Width()-IMPL_DIALOG_OFFSET;
234     }
235 
236     // Arrange PushButtons
237     pItem = mpItemList->First();
238     while ( pItem )
239     {
240         if ( GetStyle() & WB_HORZ )
241             nX += pItem->mnSepSize;
242         else
243             nY += pItem->mnSepSize;
244         pItem->mpPushButton->SetPosSizePixel( Point( nX, nY ), maCtrlSize );
245         pItem->mpPushButton->Show();
246         if ( GetStyle() & WB_HORZ )
247             nX += maCtrlSize.Width()+IMPL_SEP_BUTTON_X;
248         else
249             nY += maCtrlSize.Height()+IMPL_SEP_BUTTON_Y;
250 
251         pItem = mpItemList->Next();
252     }
253 
254     SetOutputSizePixel( aDlgSize );
255 
256     mbFormat = sal_False;
257 }
258 
259 // -----------------------------------------------------------------------
260 
IMPL_LINK(ButtonDialog,ImplClickHdl,PushButton *,pBtn)261 IMPL_LINK( ButtonDialog, ImplClickHdl, PushButton*, pBtn )
262 {
263     ImplBtnDlgItem* pItem = mpItemList->First();
264     while ( pItem )
265     {
266         if ( pItem->mpPushButton == pBtn )
267         {
268             mnCurButtonId = pItem->mnId;
269             Click();
270             break;
271         }
272 
273         pItem = mpItemList->Next();
274     }
275 
276     return 0;
277 }
278 
279 // -----------------------------------------------------------------------
280 
Resize()281 void ButtonDialog::Resize()
282 {
283 }
284 
285 // -----------------------------------------------------------------------
286 
StateChanged(StateChangedType nType)287 void ButtonDialog::StateChanged( StateChangedType nType )
288 {
289     if ( nType == STATE_CHANGE_INITSHOW )
290     {
291         ImplPosControls();
292         ImplBtnDlgItem* pItem = mpItemList->First();
293         while ( pItem )
294         {
295             if ( pItem->mpPushButton && pItem->mbOwnButton )
296                 pItem->mpPushButton->SetZOrder(0, WINDOW_ZORDER_LAST);
297             pItem = mpItemList->Next();
298         }
299 
300         // Focus evt. auf den entsprechenden Button setzen
301         if ( mnFocusButtonId != BUTTONDIALOG_BUTTON_NOTFOUND )
302         {
303             ImplBtnDlgItem* pItem = mpItemList->First();
304             while ( pItem )
305             {
306                 if ( pItem->mnId == mnFocusButtonId )
307                 {
308                     if ( pItem->mpPushButton->IsVisible() )
309                         pItem->mpPushButton->GrabFocus();
310                     break;
311                 }
312 
313                 pItem = mpItemList->Next();
314             }
315         }
316     }
317 
318     Dialog::StateChanged( nType );
319 }
320 
321 // -----------------------------------------------------------------------
322 
Click()323 void ButtonDialog::Click()
324 {
325     if ( !maClickHdl )
326     {
327         if ( IsInExecute() )
328             EndDialog( GetCurButtonId() );
329     }
330     else
331         maClickHdl.Call( this );
332 }
333 
334 // -----------------------------------------------------------------------
335 
AddButton(const XubString & rText,sal_uInt16 nId,sal_uInt16 nBtnFlags,long nSepPixel)336 void ButtonDialog::AddButton( const XubString& rText, sal_uInt16 nId,
337                               sal_uInt16 nBtnFlags, long nSepPixel )
338 {
339     // PageItem anlegen
340     ImplBtnDlgItem* pItem   = new ImplBtnDlgItem;
341     pItem->mnId             = nId;
342     pItem->mbOwnButton      = sal_True;
343     pItem->mnSepSize        = nSepPixel;
344     pItem->mpPushButton     = ImplCreatePushButton( nBtnFlags );
345     if ( rText.Len() )
346         pItem->mpPushButton->SetText( rText );
347 
348     // In die Liste eintragen
349     mpItemList->Insert( pItem, LIST_APPEND );
350 
351     if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
352         mnFocusButtonId = nId;
353 
354     mbFormat = sal_True;
355 }
356 
357 // -----------------------------------------------------------------------
358 
AddButton(StandardButtonType eType,sal_uInt16 nId,sal_uInt16 nBtnFlags,long nSepPixel)359 void ButtonDialog::AddButton( StandardButtonType eType, sal_uInt16 nId,
360                               sal_uInt16 nBtnFlags, long nSepPixel )
361 {
362     // PageItem anlegen
363     ImplBtnDlgItem* pItem   = new ImplBtnDlgItem;
364     pItem->mnId             = nId;
365     pItem->mbOwnButton      = sal_True;
366     pItem->mnSepSize        = nSepPixel;
367 
368     if ( eType == BUTTON_OK )
369         nBtnFlags |= BUTTONDIALOG_OKBUTTON;
370     else if ( eType == BUTTON_HELP )
371         nBtnFlags |= BUTTONDIALOG_HELPBUTTON;
372     else if ( (eType == BUTTON_CANCEL) || (eType == BUTTON_CLOSE) )
373         nBtnFlags |= BUTTONDIALOG_CANCELBUTTON;
374     pItem->mpPushButton = ImplCreatePushButton( nBtnFlags );
375 
376     // Standard-Buttons have the right text already
377     if ( !((eType == BUTTON_OK)     && (pItem->mpPushButton->GetType() == WINDOW_OKBUTTON)) ||
378          !((eType == BUTTON_CANCEL) && (pItem->mpPushButton->GetType() == WINDOW_CANCELBUTTON)) ||
379          !((eType == BUTTON_HELP)   && (pItem->mpPushButton->GetType() == WINDOW_HELPBUTTON)) )
380     {
381         pItem->mpPushButton->SetText( Button::GetStandardText( eType ) );
382         pItem->mpPushButton->SetHelpText( Button::GetStandardHelpText( eType ) );
383     }
384 
385     if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
386         mnFocusButtonId = nId;
387 
388     // In die Liste eintragen
389     mpItemList->Insert( pItem, LIST_APPEND );
390 
391     mbFormat = sal_True;
392 }
393 
394 // -----------------------------------------------------------------------
395 
AddButton(PushButton * pBtn,sal_uInt16 nId,sal_uInt16 nBtnFlags,long nSepPixel)396 void ButtonDialog::AddButton( PushButton* pBtn, sal_uInt16 nId,
397                               sal_uInt16 nBtnFlags, long nSepPixel )
398 {
399     // PageItem anlegen
400     ImplBtnDlgItem* pItem   = new ImplBtnDlgItem;
401     pItem->mnId             = nId;
402     pItem->mbOwnButton      = sal_False;
403     pItem->mnSepSize        = nSepPixel;
404     pItem->mpPushButton     = pBtn;
405 
406     if ( nBtnFlags & BUTTONDIALOG_FOCUSBUTTON )
407         mnFocusButtonId = nId;
408 
409     // In die View-Liste eintragen
410     mpItemList->Insert( pItem, LIST_APPEND );
411 
412     mbFormat = sal_True;
413 }
414 
415 // -----------------------------------------------------------------------
416 
RemoveButton(sal_uInt16 nId)417 void ButtonDialog::RemoveButton( sal_uInt16 nId )
418 {
419     ImplBtnDlgItem* pItem = mpItemList->First();
420     while ( pItem )
421     {
422         if ( pItem->mnId == nId )
423         {
424             pItem->mpPushButton->Hide();
425             if ( pItem->mbOwnButton )
426                 delete pItem->mpPushButton;
427             delete pItem;
428             mpItemList->Remove();
429             mbFormat = sal_True;
430             break;
431         }
432 
433         pItem = mpItemList->Next();
434     }
435 
436     DBG_ERRORFILE( "ButtonDialog::RemoveButton(): ButtonId invalid" );
437 }
438 
439 // -----------------------------------------------------------------------
440 
Clear()441 void ButtonDialog::Clear()
442 {
443     ImplBtnDlgItem* pItem = mpItemList->First();
444     while ( pItem )
445     {
446         pItem->mpPushButton->Hide();
447         if ( pItem->mbOwnButton )
448             delete pItem->mpPushButton;
449         delete pItem;
450         pItem = mpItemList->Next();
451     }
452 
453     mpItemList->Clear();
454     mbFormat = sal_True;
455 }
456 
457 // -----------------------------------------------------------------------
458 
GetButtonCount() const459 sal_uInt16 ButtonDialog::GetButtonCount() const
460 {
461     return (sal_uInt16)mpItemList->Count();
462 }
463 
464 // -----------------------------------------------------------------------
465 
GetButtonId(sal_uInt16 nButton) const466 sal_uInt16 ButtonDialog::GetButtonId( sal_uInt16 nButton ) const
467 {
468     if ( nButton < mpItemList->Count() )
469         return (sal_uInt16)mpItemList->GetObject( nButton )->mnId;
470     else
471         return BUTTONDIALOG_BUTTON_NOTFOUND;
472 }
473 
474 // -----------------------------------------------------------------------
475 
GetPushButton(sal_uInt16 nId) const476 PushButton* ButtonDialog::GetPushButton( sal_uInt16 nId ) const
477 {
478     ImplBtnDlgItem* pItem = ImplGetItem( nId );
479 
480     if ( pItem )
481         return pItem->mpPushButton;
482     else
483         return NULL;
484 }
485 
486 // -----------------------------------------------------------------------
487 
SetButtonText(sal_uInt16 nId,const XubString & rText)488 void ButtonDialog::SetButtonText( sal_uInt16 nId, const XubString& rText )
489 {
490     ImplBtnDlgItem* pItem = ImplGetItem( nId );
491 
492     if ( pItem )
493     {
494         pItem->mpPushButton->SetText( rText );
495         mbFormat = sal_True;
496     }
497 }
498 
499 // -----------------------------------------------------------------------
500 
GetButtonText(sal_uInt16 nId) const501 XubString ButtonDialog::GetButtonText( sal_uInt16 nId ) const
502 {
503     ImplBtnDlgItem* pItem = ImplGetItem( nId );
504 
505     if ( pItem )
506         return pItem->mpPushButton->GetText();
507     else
508         return ImplGetSVEmptyStr();
509 }
510 
511 // -----------------------------------------------------------------------
512 
SetButtonHelpText(sal_uInt16 nId,const XubString & rText)513 void ButtonDialog::SetButtonHelpText( sal_uInt16 nId, const XubString& rText )
514 {
515     ImplBtnDlgItem* pItem = ImplGetItem( nId );
516 
517     if ( pItem )
518         pItem->mpPushButton->SetHelpText( rText );
519 }
520 
521 // -----------------------------------------------------------------------
522 
GetButtonHelpText(sal_uInt16 nId) const523 XubString ButtonDialog::GetButtonHelpText( sal_uInt16 nId ) const
524 {
525     ImplBtnDlgItem* pItem = ImplGetItem( nId );
526 
527     if ( pItem )
528         return pItem->mpPushButton->GetHelpText();
529     else
530         return ImplGetSVEmptyStr();
531 }
532 
533 // -----------------------------------------------------------------------
534 
SetButtonHelpId(sal_uInt16 nId,const rtl::OString & rHelpId)535 void ButtonDialog::SetButtonHelpId( sal_uInt16 nId, const rtl::OString& rHelpId )
536 {
537     ImplBtnDlgItem* pItem = ImplGetItem( nId );
538 
539     if ( pItem )
540         pItem->mpPushButton->SetHelpId( rHelpId );
541 }
542 
543 // -----------------------------------------------------------------------
544 
GetButtonHelpId(sal_uInt16 nId) const545 rtl::OString ButtonDialog::GetButtonHelpId( sal_uInt16 nId ) const
546 {
547     ImplBtnDlgItem* pItem = ImplGetItem( nId );
548 
549     return pItem ? rtl::OString( pItem->mpPushButton->GetHelpId() ) : rtl::OString();
550 }
551