LCOV - code coverage report
Current view: top level - vcl/source/window - btndlg.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 201 0.5 %
Date: 2014-11-03 Functions: 2 24 8.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10