LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/starmath/source - toolbox.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 120 176 68.2 %
Date: 2013-07-09 Functions: 19 26 73.1 %
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 <svl/eitem.hxx>
      21             : #include <sfx2/app.hxx>
      22             : #include <svl/intitem.hxx>
      23             : #include <svtools/imgdef.hxx>
      24             : #include <sfx2/dispatch.hxx>
      25             : #include <sfx2/imgmgr.hxx>
      26             : #include <vcl/wrkwin.hxx>
      27             : #include "toolbox.hxx"
      28             : #include "starmath.hrc"
      29             : #include "toolbox.hrc"
      30             : #include "view.hxx"
      31             : 
      32             : 
      33             : ////////////////////////////////////////////////////////////
      34             : 
      35          17 : static sal_uInt16  GetImageListRID( sal_uInt16 nCategoryRID )
      36             : {
      37          17 :     sal_uInt16 nRes = 0xFFFF;
      38          17 :     switch (nCategoryRID)
      39             :     {
      40          17 :         case RID_UNBINOPS_CAT       : nRes = RID_IL_UNBINOPS; break;
      41           0 :         case RID_RELATIONS_CAT      : nRes = RID_IL_RELATIONS; break;
      42           0 :         case RID_SETOPERATIONS_CAT  : nRes = RID_IL_SETOPERATIONS; break;
      43           0 :         case RID_FUNCTIONS_CAT      : nRes = RID_IL_FUNCTIONS; break;
      44           0 :         case RID_OPERATORS_CAT      : nRes = RID_IL_OPERATORS; break;
      45           0 :         case RID_ATTRIBUTES_CAT     : nRes = RID_IL_ATTRIBUTES; break;
      46           0 :         case RID_BRACKETS_CAT       : nRes = RID_IL_BRACKETS; break;
      47           0 :         case RID_FORMAT_CAT         : nRes = RID_IL_FORMAT; break;
      48           0 :         case RID_MISC_CAT           : nRes = RID_IL_MISC; break;
      49             :         default :
      50             :             SAL_WARN( "starmath", "unknown category" );
      51             :     }
      52          17 :     return nRes;
      53             : }
      54             : 
      55             : 
      56          85 : static sal_Int16  GetToolBoxCategoriesIndex( sal_uInt16 nCategoryRID )
      57             : {
      58          85 :     sal_Int16 nIdx = -1;
      59          85 :     switch (nCategoryRID)
      60             :     {
      61          68 :         case RID_UNBINOPS_CAT       : nIdx = 0; break;
      62           0 :         case RID_RELATIONS_CAT      : nIdx = 1; break;
      63           0 :         case RID_SETOPERATIONS_CAT  : nIdx = 2; break;
      64           0 :         case RID_FUNCTIONS_CAT      : nIdx = 3; break;
      65           0 :         case RID_OPERATORS_CAT      : nIdx = 4; break;
      66           0 :         case RID_ATTRIBUTES_CAT     : nIdx = 5; break;
      67           0 :         case RID_BRACKETS_CAT       : nIdx = 6; break;
      68           0 :         case RID_FORMAT_CAT         : nIdx = 7; break;
      69           0 :         case RID_MISC_CAT           : nIdx = 8; break;
      70             :         default:
      71             :             ;
      72             :     }
      73          85 :     return nIdx;
      74             : }
      75             : 
      76             : 
      77          34 : static sal_uInt16  GetCategoryRID( sal_uInt16 nResId )
      78             : {
      79          34 :     sal_uInt16 nRes = 0xFFFF;
      80          34 :     switch (nResId)
      81             :     {
      82          17 :         case RID_IL_UNBINOPS       : nRes = RID_UNBINOPS_CAT; break;
      83           0 :         case RID_IL_RELATIONS      : nRes = RID_RELATIONS_CAT; break;
      84           0 :         case RID_IL_SETOPERATIONS  : nRes = RID_SETOPERATIONS_CAT; break;
      85           0 :         case RID_IL_FUNCTIONS      : nRes = RID_FUNCTIONS_CAT; break;
      86           0 :         case RID_IL_OPERATORS      : nRes = RID_OPERATORS_CAT; break;
      87           0 :         case RID_IL_ATTRIBUTES     : nRes = RID_ATTRIBUTES_CAT; break;
      88           0 :         case RID_IL_BRACKETS       : nRes = RID_BRACKETS_CAT; break;
      89           0 :         case RID_IL_FORMAT         : nRes = RID_FORMAT_CAT; break;
      90           0 :         case RID_IL_MISC           : nRes = RID_MISC_CAT; break;
      91             :         default :
      92             :             if (nResId != RID_IL_CATALOG)
      93             :             {
      94             : #if OSL_DEBUG_LEVEL > 1
      95             :                 SAL_WARN( "starmath", "unknown category" );
      96             : #endif
      97             :             }
      98             :     }
      99          34 :     return nRes;
     100             : }
     101             : 
     102             : 
     103             : ////////////////////////////////////////////////////////////
     104             : 
     105             : 
     106          17 : SmToolBoxWindow::SmToolBoxWindow(SfxBindings *pTmpBindings,
     107             :                                  SfxChildWindow *pChildWindow,
     108             :                                  Window *pParent) :
     109             :     SfxFloatingWindow(pTmpBindings, pChildWindow, pParent, SmResId(RID_TOOLBOXWINDOW)),
     110             :     aToolBoxCat(this, SmResId(TOOLBOX_CATALOG)),
     111          17 :     aToolBoxCat_Delim(this, SmResId( FL_TOOLBOX_CAT_DELIM ))
     112             : {
     113             :     SAL_INFO( "starmath", "starmath: SmToolBoxWindow::SmToolBoxWindow" );
     114             : 
     115             :     // allow for cursor travelling between toolbox and sub-categories
     116          17 :     SetStyle( GetStyle() | WB_DIALOGCONTROL );
     117             : 
     118          17 :     nActiveCategoryRID = USHRT_MAX;
     119             : 
     120          17 :     aToolBoxCat.SetClickHdl(LINK(this, SmToolBoxWindow, CategoryClickHdl));
     121             : 
     122             :     sal_uInt16 i;
     123         170 :     for (i = 0;  i < NUM_TBX_CATEGORIES;  ++i)
     124             :     {
     125         153 :         ToolBox *pBox = new ToolBox(this, SmResId( TOOLBOX_CAT_A + i ));
     126         153 :         vToolBoxCategories[i] = pBox;
     127         153 :         pBox->SetSelectHdl(LINK(this, SmToolBoxWindow, CmdSelectHdl));
     128             :     }
     129          17 :     pToolBoxCmd = vToolBoxCategories[0];
     130             : 
     131         187 :     for (i = 0;  i <= NUM_TBX_CATEGORIES; ++i)
     132         170 :         aImageLists [i] = 0;
     133             : 
     134          17 :     FreeResource();
     135          17 : }
     136             : 
     137          51 : SmToolBoxWindow::~SmToolBoxWindow()
     138             : {
     139             :     int i;
     140         170 :     for (i = 0;  i < NUM_TBX_CATEGORIES;  ++i)
     141             :     {
     142         153 :         ToolBox *pBox = vToolBoxCategories[i];
     143         153 :         delete pBox;
     144             :     }
     145         187 :     for (i = 0;  i < NUM_TBX_CATEGORIES + 1;  ++i)
     146         170 :         delete aImageLists[i];
     147          34 : }
     148             : 
     149             : 
     150           2 : SmViewShell * SmToolBoxWindow::GetView()
     151             : {
     152           2 :     SfxViewShell *pView = GetBindings().GetDispatcher()->GetFrame()->GetViewShell();
     153           2 :     return PTR_CAST(SmViewShell, pView);
     154             : }
     155             : 
     156             : 
     157          34 : const ImageList * SmToolBoxWindow::GetImageList( sal_uInt16 nResId )
     158             : {
     159             :     // creates the image list via its resource id and stores that
     160             :     // list for later use in the respective array.
     161             : 
     162          34 :     const ImageList *pIL = 0;
     163             : 
     164             :     // get index to use
     165          34 :     sal_uInt16 nCategoryRID = GetCategoryRID( nResId );
     166          34 :     sal_Int16 nIndex = GetToolBoxCategoriesIndex( nCategoryRID );
     167          34 :     if (nIndex == -1 && (nResId == RID_IL_CATALOG))
     168          17 :         nIndex = NUM_TBX_CATEGORIES;
     169             : 
     170          34 :     if (nIndex >= 0)
     171             :     {
     172          34 :         ImageList **pImgList = aImageLists;
     173          34 :         if (!pImgList[ nIndex ])
     174          34 :             pImgList[ nIndex ] = new ImageList( SmResId(nResId) );
     175          34 :         pIL = pImgList[ nIndex ];
     176             :     }
     177             : 
     178             :     OSL_ENSURE( pIL, "image list not found!" );
     179          34 :     return pIL;
     180             : }
     181             : 
     182             : 
     183          17 : void SmToolBoxWindow::ApplyImageLists( sal_uInt16 nCategoryRID )
     184             : {
     185             :     // set image list for toolbox 'catalog'
     186          17 :     const ImageList *pImageList = GetImageList( RID_IL_CATALOG );
     187             :     OSL_ENSURE( pImageList, "image list missing" );
     188          17 :     if (pImageList)
     189          17 :         aToolBoxCat.SetImageList( *pImageList );
     190             : 
     191             :     // set image list for active (visible) category of 'catalog'
     192          17 :     sal_Int16 nIdx = GetToolBoxCategoriesIndex( nCategoryRID );
     193          17 :     sal_uInt16 nResId = GetImageListRID( nCategoryRID );
     194          17 :     pImageList = GetImageList( nResId );
     195             :     OSL_ENSURE( pImageList && nIdx >= 0, "image list or index missing" );
     196          17 :     if (pImageList && nIdx >= 0)
     197          17 :         vToolBoxCategories[ nIdx ]->SetImageList( *pImageList );
     198          17 : }
     199             : 
     200           0 : void SmToolBoxWindow::DataChanged( const DataChangedEvent &rEvt )
     201             : {
     202           0 :     if ( (rEvt.GetType() == DATACHANGED_SETTINGS) && (rEvt.GetFlags() & SETTINGS_STYLE) )
     203           0 :         ApplyImageLists( nActiveCategoryRID );
     204             : 
     205           0 :     SfxFloatingWindow::DataChanged( rEvt );
     206           0 : }
     207             : 
     208         102 : void SmToolBoxWindow::StateChanged( StateChangedType nStateChange )
     209             : {
     210             :     static bool bSetPosition = true;
     211         102 :     if (STATE_CHANGE_INITSHOW == nStateChange)
     212             :     {
     213          34 :         SetCategory( nActiveCategoryRID == USHRT_MAX ? RID_UNBINOPS_CAT : nActiveCategoryRID );
     214             : 
     215             :         // calculate initial position to be used after creation of the window...
     216          34 :         AdjustPosSize( bSetPosition );
     217          34 :         bSetPosition = false;
     218             :     }
     219             :     //... otherwise the base class will remember the last position of the window
     220         102 :     SfxFloatingWindow::StateChanged( nStateChange );
     221         102 : }
     222             : 
     223             : 
     224          34 : void SmToolBoxWindow::AdjustPosSize( bool bSetPos )
     225             : {
     226          34 :     Size aCatSize( aToolBoxCat.CalcWindowSizePixel( 2 ) );
     227          34 :     Size aCmdSize( pToolBoxCmd->CalcWindowSizePixel( 7 /* see nLines in SetCategory*/ ) );
     228             :     OSL_ENSURE( aCatSize.Width() == aCmdSize.Width(), "width mismatch" );
     229             : 
     230             :     // catalog settings
     231          34 :     aToolBoxCat.SetPosPixel( Point(0, 3) );
     232          34 :     aToolBoxCat.SetSizePixel( aCatSize );
     233             :     // settings for catalog / category delimiter
     234          34 :     Point aP( aToolBoxCat_Delim.GetPosPixel() );
     235          34 :     aP.X() = 0;
     236          34 :     aToolBoxCat_Delim.SetPosPixel( aP );
     237          34 :     aToolBoxCat_Delim.SetSizePixel( Size( aCatSize.Width(), aToolBoxCat_Delim.GetSizePixel().Height() ) );
     238             :     // category settings
     239          34 :     aP.Y() += aToolBoxCat_Delim.GetSizePixel().Height();
     240         340 :     for (int i = 0;  i < NUM_TBX_CATEGORIES;  ++i)
     241             :     {
     242         306 :         vToolBoxCategories[i]->SetPosPixel( aP );
     243         306 :         vToolBoxCategories[i]->SetSizePixel( aCmdSize );
     244             :     }
     245             :     // main window settings
     246          34 :     Size    aWndSize ( aCatSize.Width(), pToolBoxCmd->GetPosPixel().Y() + pToolBoxCmd->GetSizePixel().Height() + 3);
     247          34 :     SetOutputSizePixel( aWndSize );
     248             : 
     249          34 :     if (bSetPos)
     250             :     {
     251           2 :         SmViewShell *pView = GetView();
     252             :         OSL_ENSURE( pView, "view shell missing" );
     253           2 :         Point aPos( 50, 75 );
     254           2 :         if (pView)
     255             :         {
     256           2 :             SmGraphicWindow &rWin = pView->GetGraphicWindow();
     257             :             aPos = Point( rWin.OutputToScreenPixel(
     258           2 :                             Point( rWin.GetSizePixel().Width() - aWndSize.Width(), 0) ) );
     259             :         }
     260           2 :         if (aPos.X() < 0)
     261           2 :             aPos.X() = 0;
     262           2 :         if (aPos.Y() < 0)
     263           0 :             aPos.Y() = 0;
     264           2 :         SetPosPixel( aPos );
     265             :     }
     266          34 : }
     267             : 
     268             : 
     269           0 : sal_Bool SmToolBoxWindow::Close()
     270             : {
     271           0 :     SmViewShell *pViewSh = GetView();
     272           0 :     if (pViewSh)
     273             :         pViewSh->GetViewFrame()->GetDispatcher()->Execute(
     274             :                 SID_TOOLBOX, SFX_CALLMODE_STANDARD,
     275           0 :                 new SfxBoolItem(SID_TOOLBOX, false), 0L);
     276           0 :     return true;
     277             : }
     278             : 
     279           0 : void SmToolBoxWindow::GetFocus()
     280             : {
     281             :     // give focus to category toolbox
     282             :     // (allow for cursor travelling when a category is selected with the mouse)
     283           0 :     aToolBoxCat.GrabFocus();
     284           0 : }
     285             : 
     286          34 : void SmToolBoxWindow::SetCategory(sal_uInt16 nCategoryRID)
     287             : {
     288          34 :     if (nCategoryRID != nActiveCategoryRID)
     289          17 :         ApplyImageLists( nCategoryRID );
     290             : 
     291             :     sal_uInt16 nLines;
     292             :     // check for valid resource id
     293          34 :     switch (nCategoryRID)
     294             :     {
     295          34 :         case RID_UNBINOPS_CAT :     nLines = 4; break;
     296           0 :         case RID_RELATIONS_CAT:     nLines = 7; break;
     297           0 :         case RID_SETOPERATIONS_CAT: nLines = 5; break;
     298           0 :         case RID_FUNCTIONS_CAT:     nLines = 5; break;
     299           0 :         case RID_OPERATORS_CAT:     nLines = 3; break;
     300           0 :         case RID_ATTRIBUTES_CAT:    nLines = 5; break;
     301           0 :         case RID_MISC_CAT:          nLines = 4; break;
     302           0 :         case RID_BRACKETS_CAT:      nLines = 5; break;
     303           0 :         case RID_FORMAT_CAT:        nLines = 3; break;
     304             :         default:
     305             :             // nothing to be done
     306          34 :             return;
     307             :     }
     308             : 
     309          34 :     pToolBoxCmd->Hide();
     310             : 
     311          34 :     sal_Int16 nIdx = GetToolBoxCategoriesIndex( nCategoryRID );
     312             :         OSL_ENSURE( nIdx >= 0, "unknown category" );
     313          34 :     if (nIdx >= 0)
     314          34 :         pToolBoxCmd = vToolBoxCategories[nIdx];
     315             : 
     316             :     // calculate actual size of window to use
     317          34 :     Size aCatSize( aToolBoxCat.CalcWindowSizePixel( 2 ) );
     318          34 :     Size aCmdSize( pToolBoxCmd->CalcWindowSizePixel( nLines ) );
     319             :         OSL_ENSURE( aCatSize.Width() == aCmdSize.Width(), "width mismatch" );
     320             :     // main window settings
     321          34 :     Size  aWndSize ( aCatSize.Width(), pToolBoxCmd->GetPosPixel().Y() + aCmdSize.Height() + 3);
     322          34 :     SetOutputSizePixel( aWndSize );
     323             : 
     324          34 :     if (nActiveCategoryRID)
     325          34 :             aToolBoxCat.CheckItem(nActiveCategoryRID, false);
     326          34 :     nActiveCategoryRID = nCategoryRID;
     327          34 :         aToolBoxCat.CheckItem(nActiveCategoryRID, true);
     328             : 
     329          34 :     pToolBoxCmd->Show();
     330             : }
     331             : 
     332             : 
     333           0 : IMPL_LINK( SmToolBoxWindow, CategoryClickHdl, ToolBox*, pToolBox)
     334             : {
     335           0 :     int nItemId = pToolBox->GetCurItemId();
     336           0 :     if (nItemId != 0)
     337           0 :         SetCategory( sal::static_int_cast< sal_uInt16 >(nItemId) );
     338           0 :     return 0;
     339             : }
     340             : 
     341             : 
     342           0 : IMPL_LINK( SmToolBoxWindow, CmdSelectHdl, ToolBox*, pToolBox)
     343             : {
     344           0 :     SmViewShell *pViewSh = GetView();
     345           0 :     if (pViewSh)
     346             :         pViewSh->GetViewFrame()->GetDispatcher()->Execute(
     347             :                 SID_INSERTCOMMAND, SFX_CALLMODE_STANDARD,
     348           0 :                 new SfxInt16Item(SID_INSERTCOMMAND, pToolBox->GetCurItemId()), 0L);
     349           0 :     return 0;
     350             : }
     351             : 
     352             : 
     353             : /**************************************************************************/
     354             : 
     355          59 : SFX_IMPL_FLOATINGWINDOW_WITHID(SmToolBoxWrapper, SID_TOOLBOXWINDOW);
     356             : 
     357          17 : SmToolBoxWrapper::SmToolBoxWrapper(Window *pParentWindow,
     358             :                                    sal_uInt16 nId, SfxBindings* pBindings,
     359             :                                    SfxChildWinInfo *pInfo) :
     360          17 :     SfxChildWindow(pParentWindow, nId)
     361             : {
     362          17 :     eChildAlignment = SFX_ALIGN_NOALIGNMENT;
     363             : 
     364          17 :     pWindow = new SmToolBoxWindow(pBindings, this, pParentWindow);
     365          17 :     ((SfxFloatingWindow *)pWindow)->Initialize(pInfo);
     366          38 : }
     367             : 
     368             : 
     369             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10