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

Generated by: LCOV version 1.10