Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : //ScRangeManagerTable
30 : #include "global.hxx"
31 : #include "reffact.hxx"
32 : #include "document.hxx"
33 : #include "docfunc.hxx"
34 : #include "scresid.hxx"
35 : #include "globstr.hrc"
36 : #include "namedlg.hrc"
37 : #include "namedlg.hxx"
38 : #include "viewdata.hxx"
39 : #include "globalnames.hxx"
40 :
41 : #include "sfx2/app.hxx"
42 :
43 : #define ITEMID_NAME 1
44 : #define ITEMID_RANGE 2
45 : #define ITEMID_SCOPE 3
46 :
47 : #define MINSIZE 80
48 :
49 :
50 0 : String createEntryString(const ScRangeNameLine& rLine)
51 : {
52 0 : String aRet(rLine.aName);
53 0 : aRet += '\t';
54 0 : aRet += String(rLine.aExpression);
55 0 : aRet += '\t';
56 0 : aRet += String(rLine.aScope);
57 0 : return aRet;
58 : }
59 :
60 0 : ScRangeManagerTable::ScRangeManagerTable( Window* pWindow, boost::ptr_map<rtl::OUString, ScRangeName>& rRangeMap, const ScAddress& rPos ):
61 : SvTabListBox( pWindow, WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP ),
62 : maHeaderBar( pWindow, WB_BUTTONSTYLE | WB_BOTTOMBORDER ),
63 0 : maGlobalString( ScGlobal::GetRscString(STR_GLOBAL_SCOPE)),
64 : mrRangeMap( rRangeMap ),
65 0 : maPos( rPos )
66 : {
67 0 : Size aBoxSize( pWindow->GetOutputSizePixel() );
68 :
69 0 : maHeaderBar.SetPosSizePixel( Point(0, 0), Size( aBoxSize.Width(), 16 ) );
70 :
71 0 : String aNameStr(ScGlobal::GetRscString(STR_HEADER_NAME));
72 0 : String aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE));
73 0 : String aScopeStr(ScGlobal::GetRscString(STR_HEADER_SCOPE));
74 :
75 0 : long nTabSize = aBoxSize.Width()/3;
76 0 : maHeaderBar.InsertItem( ITEMID_NAME, aNameStr, nTabSize, HIB_LEFT| HIB_VCENTER );
77 0 : maHeaderBar.InsertItem( ITEMID_RANGE, aRangeStr, nTabSize, HIB_LEFT| HIB_VCENTER );
78 0 : maHeaderBar.InsertItem( ITEMID_SCOPE, aScopeStr, nTabSize, HIB_LEFT| HIB_VCENTER );
79 :
80 0 : static long nTabs[] = {3, 0, nTabSize, 2*nTabSize };
81 0 : Size aHeadSize( maHeaderBar.GetSizePixel() );
82 :
83 : //pParent->SetFocusControl( this );
84 0 : SetPosSizePixel( Point( 0, aHeadSize.Height() ), Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
85 0 : SetTabs( &nTabs[0], MAP_PIXEL );
86 :
87 0 : maHeaderBar.SetEndDragHdl( LINK( this, ScRangeManagerTable, HeaderEndDragHdl ) );
88 :
89 0 : Init();
90 0 : Show();
91 0 : maHeaderBar.Show();
92 0 : SetSelectionMode(MULTIPLE_SELECTION);
93 0 : if (GetEntryCount())
94 : {
95 0 : SetCurEntry(GetEntryOnPos(0));
96 0 : CheckForFormulaString();
97 : }
98 0 : SetScrolledHdl( LINK( this, ScRangeManagerTable, ScrollHdl ) );
99 0 : void* pNull = NULL;
100 0 : HeaderEndDragHdl(pNull);
101 0 : }
102 :
103 0 : ScRangeManagerTable::~ScRangeManagerTable()
104 : {
105 0 : Clear();
106 0 : }
107 :
108 0 : void ScRangeManagerTable::addEntry(const ScRangeNameLine& rLine, bool bSetCurEntry)
109 : {
110 0 : SvTreeListEntry* pEntry = InsertEntryToColumn( createEntryString(rLine), LIST_APPEND, 0xffff);
111 0 : if (bSetCurEntry)
112 0 : SetCurEntry(pEntry);
113 0 : }
114 :
115 0 : void ScRangeManagerTable::GetCurrentLine(ScRangeNameLine& rLine)
116 : {
117 0 : SvTreeListEntry* pCurrentEntry = GetCurEntry();
118 0 : GetLine(rLine, pCurrentEntry);
119 0 : }
120 :
121 0 : void ScRangeManagerTable::GetLine(ScRangeNameLine& rLine, SvTreeListEntry* pEntry)
122 : {
123 0 : rLine.aName = GetEntryText( pEntry, 0);
124 0 : rLine.aExpression = GetEntryText(pEntry, 1);
125 0 : rLine.aScope = GetEntryText(pEntry, 2);
126 0 : }
127 :
128 0 : void ScRangeManagerTable::Init()
129 : {
130 0 : SetUpdateMode(false);
131 0 : Clear();
132 0 : for (boost::ptr_map<rtl::OUString, ScRangeName>::const_iterator itr = mrRangeMap.begin();
133 0 : itr != mrRangeMap.end(); ++itr)
134 : {
135 0 : const ScRangeName* pLocalRangeName = itr->second;
136 0 : ScRangeNameLine aLine;
137 0 : if ( itr->first == STR_GLOBAL_RANGE_NAME )
138 0 : aLine.aScope = maGlobalString;
139 : else
140 0 : aLine.aScope = itr->first;
141 0 : for (ScRangeName::const_iterator it = pLocalRangeName->begin();
142 0 : it != pLocalRangeName->end(); ++it)
143 : {
144 0 : if (!it->second->HasType(RT_DATABASE) && !it->second->HasType(RT_SHARED))
145 : {
146 0 : aLine.aName = it->second->GetName();
147 0 : addEntry(aLine, false);
148 : }
149 : }
150 0 : }
151 0 : SetUpdateMode(true);
152 0 : }
153 :
154 0 : const ScRangeData* ScRangeManagerTable::findRangeData(const ScRangeNameLine& rLine)
155 : {
156 : const ScRangeName* pRangeName;
157 0 : if (rLine.aScope == maGlobalString)
158 0 : pRangeName = mrRangeMap.find(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(STR_GLOBAL_RANGE_NAME)))->second;
159 : else
160 0 : pRangeName = mrRangeMap.find(rLine.aScope)->second;
161 :
162 0 : return pRangeName->findByUpperName(ScGlobal::pCharClass->uppercase(rLine.aName));
163 : }
164 :
165 :
166 :
167 0 : void ScRangeManagerTable::CheckForFormulaString()
168 : {
169 0 : for (SvTreeListEntry* pEntry = GetFirstEntryInView(); pEntry ; pEntry = GetNextEntryInView(pEntry))
170 : {
171 0 : std::map<SvTreeListEntry*, bool>::const_iterator itr = maCalculatedFormulaEntries.find(pEntry);
172 0 : if (itr == maCalculatedFormulaEntries.end() || itr->second == false)
173 : {
174 0 : ScRangeNameLine aLine;
175 0 : GetLine( aLine, pEntry);
176 0 : const ScRangeData* pData = findRangeData( aLine );
177 0 : rtl::OUString aFormulaString;
178 0 : pData->GetSymbol(aFormulaString, maPos);
179 0 : SetEntryText(aFormulaString, pEntry, 1);
180 0 : maCalculatedFormulaEntries.insert( std::pair<SvTreeListEntry*, bool>(pEntry, true) );
181 : }
182 :
183 : }
184 0 : }
185 :
186 0 : void ScRangeManagerTable::DeleteSelectedEntries()
187 : {
188 0 : if (GetSelectionCount())
189 0 : RemoveSelection();
190 0 : }
191 :
192 0 : bool ScRangeManagerTable::IsMultiSelection()
193 : {
194 0 : return GetSelectionCount() > 1;
195 : }
196 :
197 0 : std::vector<ScRangeNameLine> ScRangeManagerTable::GetSelectedEntries()
198 : {
199 0 : std::vector<ScRangeNameLine> aSelectedEntries;
200 0 : if (GetSelectionCount())
201 : {
202 0 : for (SvTreeListEntry* pEntry = FirstSelected(); pEntry != LastSelected(); pEntry = NextSelected(pEntry))
203 : {
204 0 : ScRangeNameLine aLine;
205 0 : GetLine( aLine, pEntry );
206 0 : aSelectedEntries.push_back(aLine);
207 0 : }
208 0 : SvTreeListEntry* pEntry = LastSelected();
209 0 : ScRangeNameLine aLine;
210 0 : GetLine( aLine, pEntry );
211 0 : aSelectedEntries.push_back(aLine);
212 : }
213 0 : return aSelectedEntries;
214 : }
215 :
216 0 : void ScRangeManagerTable::SetEntry(const ScRangeNameLine& rLine)
217 : {
218 0 : for (SvTreeListEntry* pEntry = First(); pEntry; pEntry = Next(pEntry))
219 : {
220 0 : if (rLine.aName == rtl::OUString(GetEntryText(pEntry, 0))
221 0 : && rLine.aScope == rtl::OUString(GetEntryText(pEntry, 2)))
222 : {
223 0 : SetCurEntry(pEntry);
224 : }
225 : }
226 0 : }
227 :
228 : namespace {
229 :
230 : //ensure that the minimum column size is respected
231 0 : void CalculateItemSize(const long& rTableSize, long& rItemNameSize, long& rItemRangeSize)
232 : {
233 0 : long aItemScopeSize = rTableSize - rItemNameSize - rItemRangeSize;
234 :
235 0 : if (rItemNameSize >= MINSIZE && rItemRangeSize >= MINSIZE && aItemScopeSize >= MINSIZE)
236 0 : return;
237 :
238 0 : if (rItemNameSize < MINSIZE)
239 : {
240 0 : long aDiffSize = MINSIZE - rItemNameSize;
241 0 : if (rItemRangeSize > aItemScopeSize)
242 0 : rItemRangeSize -= aDiffSize;
243 : else
244 0 : aItemScopeSize -= aDiffSize;
245 0 : rItemNameSize = MINSIZE;
246 : }
247 :
248 0 : if (rItemRangeSize < MINSIZE)
249 : {
250 0 : long aDiffSize = MINSIZE - rItemRangeSize;
251 0 : if (rItemNameSize > aItemScopeSize)
252 0 : rItemNameSize -= aDiffSize;
253 : else
254 0 : aItemScopeSize -= aDiffSize;
255 0 : rItemRangeSize = MINSIZE;
256 : }
257 :
258 0 : if (aItemScopeSize < MINSIZE)
259 : {
260 0 : long aDiffSize = MINSIZE - aItemScopeSize;
261 0 : if (rItemNameSize > rItemRangeSize)
262 0 : rItemNameSize -= aDiffSize;
263 : else
264 0 : rItemRangeSize -= aDiffSize;
265 : }
266 : }
267 :
268 : }
269 :
270 0 : IMPL_LINK_NOARG(ScRangeManagerTable, HeaderEndDragHdl)
271 : {
272 0 : long aTableSize = maHeaderBar.GetSizePixel().Width();
273 0 : long aItemNameSize = maHeaderBar.GetItemSize(ITEMID_NAME);
274 0 : long aItemRangeSize = maHeaderBar.GetItemSize(ITEMID_RANGE);
275 :
276 : //calculate column size based on user input and minimum size
277 0 : CalculateItemSize(aTableSize, aItemNameSize, aItemRangeSize);
278 0 : long aItemScopeSize = aTableSize - aItemNameSize - aItemRangeSize;
279 :
280 0 : Size aSz;
281 0 : aSz.Width() = aItemNameSize;
282 0 : SetTab( ITEMID_NAME, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
283 0 : maHeaderBar.SetItemSize(ITEMID_NAME, aItemNameSize);
284 0 : aSz.Width() += aItemRangeSize;
285 0 : SetTab( ITEMID_RANGE, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
286 0 : maHeaderBar.SetItemSize(ITEMID_RANGE, aItemRangeSize);
287 0 : aSz.Width() += aItemScopeSize;
288 0 : SetTab( ITEMID_SCOPE, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
289 0 : maHeaderBar.SetItemSize(ITEMID_SCOPE, aItemScopeSize);
290 :
291 0 : return 0;
292 : }
293 :
294 0 : IMPL_LINK_NOARG(ScRangeManagerTable, ScrollHdl)
295 : {
296 0 : CheckForFormulaString();
297 0 : return 0;
298 15 : }
299 :
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|