1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
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"
21 #include <sfx2/app.hxx>
24 #define ITEMID_RANGE 2
25 #define ITEMID_SCOPE 3
29 static OUString
createEntryString(const ScRangeNameLine
& rLine
)
31 OUString aRet
= rLine
.aName
+ "\t" + rLine
.aExpression
+ "\t" + rLine
.aScope
;
35 ScRangeManagerTable::InitListener::~InitListener() {}
37 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 maGlobalString( ScGlobal::GetRscString(STR_GLOBAL_SCOPE
)),
40 mrRangeMap( rRangeMap
),
44 static long aStaticTabs
[] = {3, 0, 0, 0 };
45 SetTabs( &aStaticTabs
[0], MAP_PIXEL
);
47 OUString
aNameStr(ScGlobal::GetRscString(STR_HEADER_NAME
));
48 OUString
aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE_OR_EXPR
));
49 OUString
aScopeStr(ScGlobal::GetRscString(STR_HEADER_SCOPE
));
51 HeaderBar
& rHeaderBar
= GetTheHeaderBar();
52 InsertHeaderEntry( aNameStr
);
53 InsertHeaderEntry( aRangeStr
);
54 InsertHeaderEntry( aScopeStr
);
55 rHeaderBar
.SetEndDragHdl( LINK( this, ScRangeManagerTable
, HeaderEndDragHdl
) );
61 SetSelectionMode(MULTIPLE_SELECTION
);
62 SetScrolledHdl( LINK( this, ScRangeManagerTable
, ScrollHdl
) );
64 HeaderEndDragHdl(pNull
);
67 void ScRangeManagerTable::Resize()
69 SvSimpleTable::Resize();
70 if (isInitialLayout(this))
74 void ScRangeManagerTable::StateChanged( StateChangedType nStateChange
)
76 SvSimpleTable::StateChanged(nStateChange
);
78 if (nStateChange
== StateChangedType::InitShow
)
82 SetCurEntry(GetEntryOnPos(0));
83 CheckForFormulaString();
87 mpInitListener
->tableInitialized();
91 void ScRangeManagerTable::setColWidths()
93 HeaderBar
&rHeaderBar
= GetTheHeaderBar();
94 if (rHeaderBar
.GetItemCount() < 3)
96 long nTabSize
= GetSizePixel().Width() / 3;
97 rHeaderBar
.SetItemSize( ITEMID_NAME
, nTabSize
);
98 rHeaderBar
.SetItemSize( ITEMID_RANGE
, nTabSize
);
99 rHeaderBar
.SetItemSize( ITEMID_SCOPE
, nTabSize
);
100 static long aStaticTabs
[] = {3, 0, nTabSize
, 2*nTabSize
};
101 SetTabs( &aStaticTabs
[0], MAP_PIXEL
);
103 HeaderEndDragHdl(pNull
);
106 ScRangeManagerTable::~ScRangeManagerTable()
111 void ScRangeManagerTable::dispose()
114 SvSimpleTable::dispose();
117 void ScRangeManagerTable::setInitListener( InitListener
* pListener
)
119 mpInitListener
= pListener
;
122 void ScRangeManagerTable::addEntry(const ScRangeNameLine
& rLine
, bool bSetCurEntry
)
124 SvTreeListEntry
* pEntry
= InsertEntryToColumn( createEntryString(rLine
), TREELIST_APPEND
, 0xffff);
129 void ScRangeManagerTable::GetCurrentLine(ScRangeNameLine
& rLine
)
131 SvTreeListEntry
* pCurrentEntry
= GetCurEntry();
132 GetLine(rLine
, pCurrentEntry
);
135 void ScRangeManagerTable::GetLine(ScRangeNameLine
& rLine
, SvTreeListEntry
* pEntry
)
137 rLine
.aName
= GetEntryText( pEntry
, 0);
138 rLine
.aExpression
= GetEntryText(pEntry
, 1);
139 rLine
.aScope
= GetEntryText(pEntry
, 2);
142 void ScRangeManagerTable::Init()
144 SetUpdateMode(false);
146 for (boost::ptr_map
<OUString
, ScRangeName
>::const_iterator itr
= mrRangeMap
.begin();
147 itr
!= mrRangeMap
.end(); ++itr
)
149 const ScRangeName
* pLocalRangeName
= itr
->second
;
150 ScRangeNameLine aLine
;
151 if ( itr
->first
== STR_GLOBAL_RANGE_NAME
)
152 aLine
.aScope
= maGlobalString
;
154 aLine
.aScope
= itr
->first
;
155 for (ScRangeName::const_iterator it
= pLocalRangeName
->begin();
156 it
!= pLocalRangeName
->end(); ++it
)
158 if (!it
->second
->HasType(RT_DATABASE
))
160 aLine
.aName
= it
->second
->GetName();
161 addEntry(aLine
, false);
168 const ScRangeData
* ScRangeManagerTable::findRangeData(const ScRangeNameLine
& rLine
)
170 const ScRangeName
* pRangeName
;
171 if (rLine
.aScope
== maGlobalString
)
172 pRangeName
= mrRangeMap
.find(OUString(STR_GLOBAL_RANGE_NAME
))->second
;
174 pRangeName
= mrRangeMap
.find(rLine
.aScope
)->second
;
176 return pRangeName
->findByUpperName(ScGlobal::pCharClass
->uppercase(rLine
.aName
));
179 void ScRangeManagerTable::CheckForFormulaString()
181 for (SvTreeListEntry
* pEntry
= GetFirstEntryInView(); pEntry
; pEntry
= GetNextEntryInView(pEntry
))
183 std::map
<SvTreeListEntry
*, bool>::const_iterator itr
= maCalculatedFormulaEntries
.find(pEntry
);
184 if (itr
== maCalculatedFormulaEntries
.end() || !itr
->second
)
186 ScRangeNameLine aLine
;
187 GetLine( aLine
, pEntry
);
188 const ScRangeData
* pData
= findRangeData( aLine
);
189 OUString aFormulaString
;
190 pData
->GetSymbol(aFormulaString
, maPos
);
191 SetEntryText(aFormulaString
, pEntry
, 1);
192 maCalculatedFormulaEntries
.insert( std::pair
<SvTreeListEntry
*, bool>(pEntry
, true) );
197 void ScRangeManagerTable::DeleteSelectedEntries()
199 if (GetSelectionCount())
203 bool ScRangeManagerTable::IsMultiSelection()
205 return GetSelectionCount() > 1;
208 std::vector
<ScRangeNameLine
> ScRangeManagerTable::GetSelectedEntries()
210 std::vector
<ScRangeNameLine
> aSelectedEntries
;
211 if (GetSelectionCount())
213 for (SvTreeListEntry
* pEntry
= FirstSelected(); pEntry
!= LastSelected(); pEntry
= NextSelected(pEntry
))
215 ScRangeNameLine aLine
;
216 GetLine( aLine
, pEntry
);
217 aSelectedEntries
.push_back(aLine
);
219 SvTreeListEntry
* pEntry
= LastSelected();
220 ScRangeNameLine aLine
;
221 GetLine( aLine
, pEntry
);
222 aSelectedEntries
.push_back(aLine
);
224 return aSelectedEntries
;
227 void ScRangeManagerTable::SetEntry(const ScRangeNameLine
& rLine
)
229 for (SvTreeListEntry
* pEntry
= First(); pEntry
; pEntry
= Next(pEntry
))
231 if (rLine
.aName
== GetEntryText(pEntry
, 0)
232 && rLine
.aScope
== GetEntryText(pEntry
, 2))
241 //ensure that the minimum column size is respected
242 void CalculateItemSize(const long& rTableSize
, long& rItemNameSize
, long& rItemRangeSize
)
244 long aItemScopeSize
= rTableSize
- rItemNameSize
- rItemRangeSize
;
246 if (rItemNameSize
>= MINSIZE
&& rItemRangeSize
>= MINSIZE
&& aItemScopeSize
>= MINSIZE
)
249 if (rItemNameSize
< MINSIZE
)
251 long aDiffSize
= MINSIZE
- rItemNameSize
;
252 if (rItemRangeSize
> aItemScopeSize
)
253 rItemRangeSize
-= aDiffSize
;
255 aItemScopeSize
-= aDiffSize
;
256 rItemNameSize
= MINSIZE
;
259 if (rItemRangeSize
< MINSIZE
)
261 long aDiffSize
= MINSIZE
- rItemRangeSize
;
262 if (rItemNameSize
> aItemScopeSize
)
263 rItemNameSize
-= aDiffSize
;
265 aItemScopeSize
-= aDiffSize
;
266 rItemRangeSize
= MINSIZE
;
269 if (aItemScopeSize
< MINSIZE
)
271 long aDiffSize
= MINSIZE
- aItemScopeSize
;
272 if (rItemNameSize
> rItemRangeSize
)
273 rItemNameSize
-= aDiffSize
;
275 rItemRangeSize
-= aDiffSize
;
281 IMPL_LINK_NOARG(ScRangeManagerTable
, HeaderEndDragHdl
)
283 HeaderBar
& rHeaderBar
= GetTheHeaderBar();
285 long nTableSize
= rHeaderBar
.GetSizePixel().Width();
286 long nItemNameSize
= rHeaderBar
.GetItemSize(ITEMID_NAME
);
287 long nItemRangeSize
= rHeaderBar
.GetItemSize(ITEMID_RANGE
);
289 //calculate column size based on user input and minimum size
290 CalculateItemSize(nTableSize
, nItemNameSize
, nItemRangeSize
);
291 long nItemScopeSize
= nTableSize
- nItemNameSize
- nItemRangeSize
;
293 Size
aSz(nItemNameSize
, 0);
294 rHeaderBar
.SetItemSize(ITEMID_NAME
, nItemNameSize
);
295 rHeaderBar
.SetItemSize(ITEMID_RANGE
, nItemRangeSize
);
296 rHeaderBar
.SetItemSize(ITEMID_SCOPE
, nItemScopeSize
);
298 SetTab(0, 0, MAP_APPFONT
);
299 SetTab(1, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
300 aSz
.Width() += nItemRangeSize
;
301 SetTab(2, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
306 IMPL_LINK_NOARG(ScRangeManagerTable
, ScrollHdl
)
308 CheckForFormulaString();
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */