LCOV - code coverage report
Current view: top level - sc/source/ui/namedlg - namemgrtable.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 172 0.6 %
Date: 2014-11-03 Functions: 2 27 7.4 %
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             : 
      10             : //ScRangeManagerTable
      11             : #include "global.hxx"
      12             : #include "reffact.hxx"
      13             : #include "document.hxx"
      14             : #include "docfunc.hxx"
      15             : #include "scresid.hxx"
      16             : #include "globstr.hrc"
      17             : #include "namedlg.hxx"
      18             : #include "viewdata.hxx"
      19             : #include "globalnames.hxx"
      20             : 
      21             : #include <sfx2/app.hxx>
      22             : 
      23             : #define ITEMID_NAME 1
      24             : #define ITEMID_RANGE 2
      25             : #define ITEMID_SCOPE 3
      26             : 
      27             : #define MINSIZE 80
      28             : 
      29           0 : static OUString createEntryString(const ScRangeNameLine& rLine)
      30             : {
      31           0 :     OUString aRet = rLine.aName + "\t" + rLine.aExpression + "\t" + rLine.aScope;
      32           0 :     return aRet;
      33             : }
      34             : 
      35           0 : ScRangeManagerTable::InitListener::~InitListener() {}
      36             : 
      37           0 : ScRangeManagerTable::ScRangeManagerTable( SvSimpleTableContainer& rParent, boost::ptr_map<OUString, ScRangeName>& rRangeMap, const ScAddress& rPos ):
      38             :     SvSimpleTable( rParent, WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP ),
      39           0 :     maGlobalString( ScGlobal::GetRscString(STR_GLOBAL_SCOPE)),
      40             :     mrRangeMap( rRangeMap ),
      41             :     maPos( rPos ),
      42           0 :     mpInitListener(NULL)
      43             : {
      44             :     static long aStaticTabs[] = {3, 0, 0, 0 };
      45           0 :     SetTabs( &aStaticTabs[0], MAP_PIXEL );
      46             : 
      47           0 :     OUString aNameStr(ScGlobal::GetRscString(STR_HEADER_NAME));
      48           0 :     OUString aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE));
      49           0 :     OUString aScopeStr(ScGlobal::GetRscString(STR_HEADER_SCOPE));
      50             : 
      51           0 :     HeaderBar& rHeaderBar = GetTheHeaderBar();
      52           0 :     rHeaderBar.InsertItem( ITEMID_NAME, aNameStr, 0, HIB_LEFT| HIB_VCENTER );
      53           0 :     rHeaderBar.InsertItem( ITEMID_RANGE, aRangeStr, 0, HIB_LEFT| HIB_VCENTER );
      54           0 :     rHeaderBar.InsertItem( ITEMID_SCOPE, aScopeStr, 0, HIB_LEFT| HIB_VCENTER );
      55           0 :     rHeaderBar.SetEndDragHdl( LINK( this, ScRangeManagerTable, HeaderEndDragHdl ) );
      56             : 
      57           0 :     setColWidths();
      58           0 :     UpdateViewSize();
      59           0 :     Init();
      60           0 :     ShowTable();
      61           0 :     SetSelectionMode(MULTIPLE_SELECTION);
      62           0 :     SetScrolledHdl( LINK( this, ScRangeManagerTable, ScrollHdl ) );
      63           0 :     void* pNull = NULL;
      64           0 :     HeaderEndDragHdl(pNull);
      65           0 : }
      66             : 
      67           0 : void ScRangeManagerTable::Resize()
      68             : {
      69           0 :     SvSimpleTable::Resize();
      70           0 :     if (isInitialLayout(this))
      71           0 :         setColWidths();
      72           0 : }
      73             : 
      74           0 : void ScRangeManagerTable::StateChanged( StateChangedType nStateChange )
      75             : {
      76           0 :     SvSimpleTable::StateChanged(nStateChange);
      77             : 
      78           0 :     if (nStateChange == StateChangedType::INITSHOW)
      79             :     {
      80           0 :         if (GetEntryCount())
      81             :         {
      82           0 :             SetCurEntry(GetEntryOnPos(0));
      83           0 :             CheckForFormulaString();
      84             :         }
      85             : 
      86           0 :         if (mpInitListener)
      87           0 :             mpInitListener->tableInitialized();
      88             :     }
      89           0 : }
      90             : 
      91           0 : void ScRangeManagerTable::setColWidths()
      92             : {
      93           0 :     HeaderBar &rHeaderBar = GetTheHeaderBar();
      94           0 :     if (rHeaderBar.GetItemCount() < 3)
      95           0 :         return;
      96           0 :     long nTabSize = GetSizePixel().Width() / 3;
      97           0 :     rHeaderBar.SetItemSize( ITEMID_NAME, nTabSize);
      98           0 :     rHeaderBar.SetItemSize( ITEMID_RANGE, nTabSize);
      99           0 :     rHeaderBar.SetItemSize( ITEMID_SCOPE, nTabSize);
     100           0 :     static long aStaticTabs[] = {3, 0, nTabSize, 2*nTabSize };
     101           0 :     SetTabs( &aStaticTabs[0], MAP_PIXEL );
     102           0 :     void* pNull = NULL;
     103           0 :     HeaderEndDragHdl(pNull);
     104             : }
     105             : 
     106           0 : ScRangeManagerTable::~ScRangeManagerTable()
     107             : {
     108           0 :     Clear();
     109           0 : }
     110             : 
     111           0 : void ScRangeManagerTable::setInitListener( InitListener* pListener )
     112             : {
     113           0 :     mpInitListener = pListener;
     114           0 : }
     115             : 
     116           0 : void ScRangeManagerTable::addEntry(const ScRangeNameLine& rLine, bool bSetCurEntry)
     117             : {
     118           0 :     SvTreeListEntry* pEntry = InsertEntryToColumn( createEntryString(rLine), TREELIST_APPEND, 0xffff);
     119           0 :     if (bSetCurEntry)
     120           0 :         SetCurEntry(pEntry);
     121           0 : }
     122             : 
     123           0 : void ScRangeManagerTable::GetCurrentLine(ScRangeNameLine& rLine)
     124             : {
     125           0 :     SvTreeListEntry* pCurrentEntry = GetCurEntry();
     126           0 :     GetLine(rLine, pCurrentEntry);
     127           0 : }
     128             : 
     129           0 : void ScRangeManagerTable::GetLine(ScRangeNameLine& rLine, SvTreeListEntry* pEntry)
     130             : {
     131           0 :     rLine.aName = GetEntryText( pEntry, 0);
     132           0 :     rLine.aExpression = GetEntryText(pEntry, 1);
     133           0 :     rLine.aScope = GetEntryText(pEntry, 2);
     134           0 : }
     135             : 
     136           0 : void ScRangeManagerTable::Init()
     137             : {
     138           0 :     SetUpdateMode(false);
     139           0 :     Clear();
     140           0 :     for (boost::ptr_map<OUString, ScRangeName>::const_iterator itr = mrRangeMap.begin();
     141           0 :             itr != mrRangeMap.end(); ++itr)
     142             :     {
     143           0 :         const ScRangeName* pLocalRangeName = itr->second;
     144           0 :         ScRangeNameLine aLine;
     145           0 :         if ( itr->first == STR_GLOBAL_RANGE_NAME )
     146           0 :             aLine.aScope = maGlobalString;
     147             :         else
     148           0 :             aLine.aScope = itr->first;
     149           0 :         for (ScRangeName::const_iterator it = pLocalRangeName->begin();
     150           0 :                 it != pLocalRangeName->end(); ++it)
     151             :         {
     152           0 :             if (!it->second->HasType(RT_DATABASE))
     153             :             {
     154           0 :                 aLine.aName = it->second->GetName();
     155           0 :                 addEntry(aLine, false);
     156             :             }
     157             :         }
     158           0 :     }
     159           0 :     SetUpdateMode(true);
     160           0 : }
     161             : 
     162           0 : const ScRangeData* ScRangeManagerTable::findRangeData(const ScRangeNameLine& rLine)
     163             : {
     164             :     const ScRangeName* pRangeName;
     165           0 :     if (rLine.aScope == maGlobalString)
     166           0 :         pRangeName = mrRangeMap.find(OUString(STR_GLOBAL_RANGE_NAME))->second;
     167             :     else
     168           0 :         pRangeName = mrRangeMap.find(rLine.aScope)->second;
     169             : 
     170           0 :     return pRangeName->findByUpperName(ScGlobal::pCharClass->uppercase(rLine.aName));
     171             : }
     172             : 
     173           0 : void ScRangeManagerTable::CheckForFormulaString()
     174             : {
     175           0 :     for (SvTreeListEntry* pEntry = GetFirstEntryInView(); pEntry ; pEntry = GetNextEntryInView(pEntry))
     176             :     {
     177           0 :         std::map<SvTreeListEntry*, bool>::const_iterator itr = maCalculatedFormulaEntries.find(pEntry);
     178           0 :         if (itr == maCalculatedFormulaEntries.end() || itr->second == false)
     179             :         {
     180           0 :             ScRangeNameLine aLine;
     181           0 :             GetLine( aLine, pEntry);
     182           0 :             const ScRangeData* pData = findRangeData( aLine );
     183           0 :             OUString aFormulaString;
     184           0 :             pData->GetSymbol(aFormulaString, maPos);
     185           0 :             SetEntryText(aFormulaString, pEntry, 1);
     186           0 :             maCalculatedFormulaEntries.insert( std::pair<SvTreeListEntry*, bool>(pEntry, true) );
     187             :         }
     188             :     }
     189           0 : }
     190             : 
     191           0 : void ScRangeManagerTable::DeleteSelectedEntries()
     192             : {
     193           0 :     if (GetSelectionCount())
     194           0 :         RemoveSelection();
     195           0 : }
     196             : 
     197           0 : bool ScRangeManagerTable::IsMultiSelection()
     198             : {
     199           0 :     return GetSelectionCount() > 1;
     200             : }
     201             : 
     202           0 : std::vector<ScRangeNameLine> ScRangeManagerTable::GetSelectedEntries()
     203             : {
     204           0 :     std::vector<ScRangeNameLine> aSelectedEntries;
     205           0 :     if (GetSelectionCount())
     206             :     {
     207           0 :         for (SvTreeListEntry* pEntry = FirstSelected(); pEntry != LastSelected(); pEntry = NextSelected(pEntry))
     208             :         {
     209           0 :             ScRangeNameLine aLine;
     210           0 :             GetLine( aLine, pEntry );
     211           0 :             aSelectedEntries.push_back(aLine);
     212           0 :         }
     213           0 :         SvTreeListEntry* pEntry = LastSelected();
     214           0 :         ScRangeNameLine aLine;
     215           0 :         GetLine( aLine, pEntry );
     216           0 :         aSelectedEntries.push_back(aLine);
     217             :     }
     218           0 :     return aSelectedEntries;
     219             : }
     220             : 
     221           0 : void ScRangeManagerTable::SetEntry(const ScRangeNameLine& rLine)
     222             : {
     223           0 :     for (SvTreeListEntry* pEntry = First(); pEntry; pEntry = Next(pEntry))
     224             :     {
     225           0 :         if (rLine.aName == GetEntryText(pEntry, 0)
     226           0 :                 && rLine.aScope == GetEntryText(pEntry, 2))
     227             :         {
     228           0 :             SetCurEntry(pEntry);
     229             :         }
     230             :     }
     231           0 : }
     232             : 
     233             : namespace {
     234             : 
     235             : //ensure that the minimum column size is respected
     236           0 : void CalculateItemSize(const long& rTableSize, long& rItemNameSize, long& rItemRangeSize)
     237             : {
     238           0 :     long aItemScopeSize = rTableSize - rItemNameSize - rItemRangeSize;
     239             : 
     240           0 :     if (rItemNameSize >= MINSIZE && rItemRangeSize >= MINSIZE && aItemScopeSize >= MINSIZE)
     241           0 :         return;
     242             : 
     243           0 :     if (rItemNameSize < MINSIZE)
     244             :     {
     245           0 :         long aDiffSize = MINSIZE - rItemNameSize;
     246           0 :         if (rItemRangeSize > aItemScopeSize)
     247           0 :             rItemRangeSize -= aDiffSize;
     248             :         else
     249           0 :             aItemScopeSize -= aDiffSize;
     250           0 :         rItemNameSize = MINSIZE;
     251             :     }
     252             : 
     253           0 :     if (rItemRangeSize < MINSIZE)
     254             :     {
     255           0 :         long aDiffSize = MINSIZE - rItemRangeSize;
     256           0 :         if (rItemNameSize > aItemScopeSize)
     257           0 :             rItemNameSize -= aDiffSize;
     258             :         else
     259           0 :             aItemScopeSize -= aDiffSize;
     260           0 :         rItemRangeSize = MINSIZE;
     261             :     }
     262             : 
     263           0 :     if (aItemScopeSize < MINSIZE)
     264             :     {
     265           0 :         long aDiffSize = MINSIZE - aItemScopeSize;
     266           0 :         if (rItemNameSize > rItemRangeSize)
     267           0 :             rItemNameSize -= aDiffSize;
     268             :         else
     269           0 :             rItemRangeSize -= aDiffSize;
     270             :     }
     271             : }
     272             : 
     273             : }
     274             : 
     275           0 : IMPL_LINK_NOARG(ScRangeManagerTable, HeaderEndDragHdl)
     276             : {
     277           0 :     HeaderBar& rHeaderBar = GetTheHeaderBar();
     278             : 
     279           0 :     long nTableSize = rHeaderBar.GetSizePixel().Width();
     280           0 :     long nItemNameSize = rHeaderBar.GetItemSize(ITEMID_NAME);
     281           0 :     long nItemRangeSize = rHeaderBar.GetItemSize(ITEMID_RANGE);
     282             : 
     283             :     //calculate column size based on user input and minimum size
     284           0 :     CalculateItemSize(nTableSize, nItemNameSize, nItemRangeSize);
     285           0 :     long nItemScopeSize = nTableSize - nItemNameSize - nItemRangeSize;
     286             : 
     287           0 :     Size aSz(nItemNameSize, 0);
     288           0 :     rHeaderBar.SetItemSize(ITEMID_NAME, nItemNameSize);
     289           0 :     rHeaderBar.SetItemSize(ITEMID_RANGE, nItemRangeSize);
     290           0 :     rHeaderBar.SetItemSize(ITEMID_SCOPE, nItemScopeSize);
     291             : 
     292           0 :     SetTab(0, 0, MAP_APPFONT );
     293           0 :     SetTab(1, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
     294           0 :     aSz.Width() += nItemRangeSize;
     295           0 :     SetTab(2, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
     296             : 
     297           0 :     return 0;
     298             : }
     299             : 
     300           0 : IMPL_LINK_NOARG(ScRangeManagerTable, ScrollHdl)
     301             : {
     302           0 :     CheckForFormulaString();
     303           0 :     return 0;
     304         228 : }
     305             : 
     306             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10