/src/libreoffice/vcl/source/treelist/svtabbx.cxx
Line | Count | Source |
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 <SvHeaderTabListBox.hxx> |
21 | | #include <SvLBoxButton.hxx> |
22 | | |
23 | | #include <comphelper/types.hxx> |
24 | | #include <vcl/headbar.hxx> |
25 | | #include <vcl/toolkit/svlbitm.hxx> |
26 | | #include <vcl/toolkit/treelistbox.hxx> |
27 | | #include <vcl/toolkit/treelistentry.hxx> |
28 | | #include <rtl/ustrbuf.hxx> |
29 | | #include <sal/log.hxx> |
30 | | #include <o3tl/safeint.hxx> |
31 | | #include <o3tl/string_view.hxx> |
32 | | #include <osl/diagnose.h> |
33 | | #include <strings.hrc> |
34 | | #include <svdata.hxx> |
35 | | #include <memory> |
36 | | |
37 | | // SvTreeListBox callback |
38 | | |
39 | | void SvTabListBox::SetTabs() |
40 | 0 | { |
41 | 0 | SvTreeListBox::SetTabs(); |
42 | 0 | if( mvTabList.empty() ) |
43 | 0 | return; |
44 | | |
45 | | // The tree listbox has now inserted its tabs into the list. Now we |
46 | | // fluff up the list with additional tabs and adjust the rightmost tab |
47 | | // of the tree listbox. |
48 | | |
49 | | // the 1st column (index 1 or 2 depending on button flags) is always set |
50 | | // editable by SvTreeListBox::SetTabs(), |
51 | | // which prevents setting a different column to editable as the first |
52 | | // one with the flag is picked in SvTreeListBox::ImplEditEntry() |
53 | 0 | assert(m_aTabs.back()->nFlags & SvLBoxTabFlags::EDITABLE); |
54 | 0 | if (!(mvTabList[0].nFlags & SvLBoxTabFlags::EDITABLE)) |
55 | 0 | { |
56 | 0 | m_aTabs.back()->nFlags &= ~SvLBoxTabFlags::EDITABLE; |
57 | 0 | } |
58 | | |
59 | | // append all other tabs to the list |
60 | 0 | for( sal_uInt16 nCurTab = 1; nCurTab < sal_uInt16(mvTabList.size()); nCurTab++ ) |
61 | 0 | { |
62 | 0 | SvLBoxTab& rTab = mvTabList[nCurTab]; |
63 | 0 | AddTab( rTab.GetPos(), rTab.nFlags ); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | SvTabListBox::SvTabListBox( vcl::Window* pParent, WinBits nBits ) |
68 | 0 | : SvTreeListBox( pParent, nBits ) |
69 | 0 | { |
70 | 0 | SetHighlightRange(); // select full width |
71 | 0 | } Unexecuted instantiation: SvTabListBox::SvTabListBox(vcl::Window*, long) Unexecuted instantiation: SvTabListBox::SvTabListBox(vcl::Window*, long) |
72 | | |
73 | | SvTabListBox::~SvTabListBox() |
74 | 0 | { |
75 | 0 | disposeOnce(); |
76 | 0 | } |
77 | | |
78 | | void SvTabListBox::dispose() |
79 | 0 | { |
80 | 0 | mvTabList.clear(); |
81 | 0 | SvTreeListBox::dispose(); |
82 | 0 | } |
83 | | |
84 | | void SvTabListBox::SetTabWidth(sal_uInt16 nTab, tools::Long tabWidth) |
85 | 0 | { |
86 | | // Ensure that mvTabList[nTab + 1] exists because it is required to calculate diff |
87 | 0 | if (nTab + 2 > tools::Long(mvTabList.size())) |
88 | 0 | mvTabList.resize(nTab + 2); |
89 | |
|
90 | 0 | tools::Long diff = tabWidth - |
91 | 0 | (mvTabList[nTab + 1].GetPos() - mvTabList[nTab].GetPos()); // Width change |
92 | | |
93 | | // Shift all tab positions after nTab by diff |
94 | 0 | for( sal_uInt16 nIdx = nTab + 1; nIdx < sal_uInt16(mvTabList.size()); nIdx++) |
95 | 0 | mvTabList[nIdx].SetPos(mvTabList[nIdx].GetPos() + diff); |
96 | 0 | SvTreeListBox::m_nTreeFlags |= SvTreeFlags::RECALCTABS; |
97 | 0 | if( IsUpdateMode() ) |
98 | 0 | Invalidate(); |
99 | 0 | } |
100 | | |
101 | | void SvTabListBox::SetTabs(const std::vector<tools::Long>& rTabPositions) |
102 | 0 | { |
103 | 0 | assert(!rTabPositions.empty()); |
104 | 0 | mvTabList.resize(rTabPositions.size()); |
105 | |
|
106 | 0 | for( sal_uInt16 nIdx = 0; nIdx < sal_uInt16(mvTabList.size()); nIdx++) |
107 | 0 | mvTabList[nIdx].SetPos(rTabPositions.at(nIdx)); |
108 | |
|
109 | 0 | SvTreeListBox::m_nTreeFlags |= SvTreeFlags::RECALCTABS; |
110 | 0 | if( IsUpdateMode() ) |
111 | 0 | Invalidate(); |
112 | 0 | } |
113 | | |
114 | | SvTreeListEntry& SvTabListBox::InsertEntry(const OUString& rText, SvTreeListEntry* pParent, |
115 | | sal_uInt32 nPos) |
116 | 0 | { |
117 | 0 | m_nTreeFlags |= SvTreeFlags::MANINS; |
118 | |
|
119 | 0 | SvTreeListEntry* pEntry = new SvTreeListEntry; |
120 | |
|
121 | 0 | if (m_nTreeFlags & SvTreeFlags::CHKBTN) |
122 | 0 | { |
123 | 0 | assert(m_pCheckButtonData); |
124 | 0 | pEntry->AddItem(std::make_unique<SvLBoxButton>(*m_pCheckButtonData)); |
125 | 0 | } |
126 | |
|
127 | 0 | pEntry->AddItem(std::make_unique<SvLBoxContextBmp>(Image(), Image())); |
128 | |
|
129 | 0 | pEntry->AddItem(std::make_unique<SvLBoxString>(rText)); |
130 | | |
131 | | // TODO: verify if nTabCount is always >0 here! |
132 | 0 | const sal_uInt16 nCount = mvTabList.size() - 1; |
133 | 0 | for (sal_uInt16 nTab = 0; nTab < nCount; nTab++) |
134 | 0 | pEntry->AddItem(std::make_unique<SvLBoxString>()); |
135 | |
|
136 | 0 | Insert(pEntry, nPos, pParent); |
137 | |
|
138 | 0 | m_nTreeFlags &= ~SvTreeFlags::MANINS; |
139 | |
|
140 | 0 | return *pEntry; |
141 | 0 | } |
142 | | |
143 | | OUString SvTabListBox::GetEntryText( SvTreeListEntry* pEntry ) const |
144 | 0 | { |
145 | 0 | return GetEntryText( pEntry, 0xffff ); |
146 | 0 | } |
147 | | |
148 | | OUString SvTabListBox::GetEntryText( const SvTreeListEntry* pEntry, sal_uInt16 nCol ) |
149 | 0 | { |
150 | 0 | DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry"); |
151 | 0 | OUStringBuffer aResult; |
152 | 0 | if( pEntry ) |
153 | 0 | { |
154 | 0 | sal_uInt16 nCount = pEntry->ItemCount(); |
155 | 0 | sal_uInt16 nCur = 0; |
156 | 0 | while( nCur < nCount ) |
157 | 0 | { |
158 | 0 | const SvLBoxItem& rStr = pEntry->GetItem( nCur ); |
159 | 0 | if (rStr.GetType() == SvLBoxItemType::String) |
160 | 0 | { |
161 | 0 | if( nCol == 0xffff ) |
162 | 0 | { |
163 | 0 | if (!aResult.isEmpty()) |
164 | 0 | aResult.append("\t"); |
165 | 0 | aResult.append(static_cast<const SvLBoxString&>(rStr).GetText()); |
166 | 0 | } |
167 | 0 | else |
168 | 0 | { |
169 | 0 | if( nCol == 0 ) |
170 | 0 | return static_cast<const SvLBoxString&>(rStr).GetText(); |
171 | 0 | nCol--; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | nCur++; |
175 | 0 | } |
176 | 0 | } |
177 | 0 | return aResult.makeStringAndClear(); |
178 | 0 | } |
179 | | |
180 | | OUString SvTabListBox::GetCellText( sal_uInt32 nPos, sal_uInt16 nCol ) const |
181 | 0 | { |
182 | 0 | SvTreeListEntry* pEntry = GetEntryOnPos( nPos ); |
183 | 0 | DBG_ASSERT( pEntry, "SvTabListBox::GetCellText(): Invalid Entry" ); |
184 | 0 | OUString aResult; |
185 | 0 | if (pEntry && pEntry->ItemCount() > o3tl::make_unsigned(nCol+1)) |
186 | 0 | { |
187 | 0 | const SvLBoxItem& rStr = pEntry->GetItem( nCol + 1 ); |
188 | 0 | if (rStr.GetType() == SvLBoxItemType::String) |
189 | 0 | aResult = static_cast<const SvLBoxString&>(rStr).GetText(); |
190 | 0 | } |
191 | 0 | return aResult; |
192 | 0 | } |
193 | | |
194 | | OUString SvTabListBox::GetTabEntryText( sal_uInt32 nPos, sal_uInt16 nCol ) const |
195 | 0 | { |
196 | 0 | SvTreeListEntry* pEntry = GetEntryOnPos( nPos ); |
197 | 0 | DBG_ASSERT( pEntry, "GetTabEntryText(): Invalid entry " ); |
198 | 0 | OUStringBuffer aResult; |
199 | 0 | if ( pEntry ) |
200 | 0 | { |
201 | 0 | sal_uInt16 nCount = pEntry->ItemCount(); |
202 | 0 | sal_uInt16 nCur = 0; |
203 | 0 | while( nCur < nCount ) |
204 | 0 | { |
205 | 0 | const SvLBoxItem& rBoxItem = pEntry->GetItem( nCur ); |
206 | 0 | if (rBoxItem.GetType() == SvLBoxItemType::String) |
207 | 0 | { |
208 | 0 | if ( nCol == 0xffff ) |
209 | 0 | { |
210 | 0 | if (!aResult.isEmpty()) |
211 | 0 | aResult.append("\t"); |
212 | 0 | aResult.append(static_cast<const SvLBoxString&>(rBoxItem).GetText()); |
213 | 0 | } |
214 | 0 | else |
215 | 0 | { |
216 | 0 | if ( nCol == 0 ) |
217 | 0 | { |
218 | 0 | OUString sRet = static_cast<const SvLBoxString&>(rBoxItem).GetText(); |
219 | 0 | if ( sRet.isEmpty() ) |
220 | 0 | sRet = VclResId( STR_SVT_ACC_EMPTY_FIELD ); |
221 | 0 | return sRet; |
222 | 0 | } |
223 | 0 | --nCol; |
224 | 0 | } |
225 | 0 | } |
226 | 0 | ++nCur; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | return aResult.makeStringAndClear(); |
230 | 0 | } |
231 | | |
232 | | SvTreeListEntry* SvTabListBox::GetEntryOnPos( sal_uInt32 _nEntryPos ) const |
233 | 0 | { |
234 | 0 | SvTreeListEntry* pEntry = nullptr; |
235 | 0 | sal_uInt32 i, nPos = 0, nCount = GetLevelChildCount( nullptr ); |
236 | 0 | for ( i = 0; i < nCount; ++i ) |
237 | 0 | { |
238 | 0 | SvTreeListEntry* pParent = GetEntry(i); |
239 | 0 | if ( nPos == _nEntryPos ) |
240 | 0 | { |
241 | 0 | pEntry = pParent; |
242 | 0 | break; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | nPos++; |
247 | 0 | pEntry = GetChildOnPos( pParent, _nEntryPos, nPos ); |
248 | 0 | if ( pEntry ) |
249 | 0 | break; |
250 | 0 | } |
251 | 0 | } |
252 | |
|
253 | 0 | return pEntry; |
254 | 0 | } |
255 | | |
256 | | SvTreeListEntry* SvTabListBox::GetChildOnPos( SvTreeListEntry* _pParent, sal_uInt32 _nEntryPos, sal_uInt32& _rPos ) const |
257 | 0 | { |
258 | 0 | sal_uInt32 i, nCount = GetLevelChildCount( _pParent ); |
259 | 0 | for ( i = 0; i < nCount; ++i ) |
260 | 0 | { |
261 | 0 | SvTreeListEntry* pParent = GetEntry( _pParent, i ); |
262 | 0 | if ( _rPos == _nEntryPos ) |
263 | 0 | return pParent; |
264 | 0 | else |
265 | 0 | { |
266 | 0 | _rPos++; |
267 | 0 | SvTreeListEntry* pEntry = GetChildOnPos( pParent, _nEntryPos, _rPos ); |
268 | 0 | if ( pEntry ) |
269 | 0 | return pEntry; |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | 0 | return nullptr; |
274 | 0 | } |
275 | | |
276 | | void SvTabListBox::SetTabAlignCenter(sal_uInt16 nTab) |
277 | 0 | { |
278 | 0 | DBG_ASSERT(nTab<mvTabList.size(),"GetTabPos:Invalid Tab"); |
279 | 0 | if( nTab >= mvTabList.size() ) |
280 | 0 | return; |
281 | 0 | SvLBoxTab& rTab = mvTabList[ nTab ]; |
282 | 0 | SvLBoxTabFlags nFlags = rTab.nFlags; |
283 | 0 | nFlags &= ~SvLBoxTabFlags::ADJUST_FLAGS; |
284 | | // see SvLBoxTab::CalcOffset for force, which only matters for centering |
285 | 0 | nFlags |= SvLBoxTabFlags::ADJUST_CENTER | SvLBoxTabFlags::FORCE; |
286 | 0 | rTab.nFlags = nFlags; |
287 | 0 | SvTreeListBox::m_nTreeFlags |= SvTreeFlags::RECALCTABS; |
288 | 0 | if( IsUpdateMode() ) |
289 | 0 | Invalidate(); |
290 | 0 | } |
291 | | |
292 | | void SvTabListBox::SetTabEditable(sal_uInt16 nTab, bool bEditable) |
293 | 0 | { |
294 | 0 | DBG_ASSERT(nTab<mvTabList.size(),"GetTabPos:Invalid Tab"); |
295 | 0 | if( nTab >= mvTabList.size() ) |
296 | 0 | return; |
297 | 0 | SvLBoxTab& rTab = mvTabList[ nTab ]; |
298 | 0 | if (bEditable) |
299 | 0 | rTab.nFlags |= SvLBoxTabFlags::EDITABLE; |
300 | 0 | else |
301 | 0 | rTab.nFlags &= ~SvLBoxTabFlags::EDITABLE; |
302 | 0 | } |
303 | | |
304 | | void SvTabListBox::SetTabVisible(sal_uInt16 nTab, bool bVisible) |
305 | 0 | { |
306 | 0 | DBG_ASSERT(nTab<mvTabList.size(),"SetTabVisible:Invalid Tab"); |
307 | 0 | if( nTab >= mvTabList.size() ) |
308 | 0 | return; |
309 | | |
310 | 0 | if( SvTreeListBox::m_nTreeFlags & SvTreeFlags::RECALCTABS ) |
311 | 0 | SetTabs(); |
312 | | |
313 | | // Find index in aTabs |
314 | 0 | nTab += m_aTabs.size() - mvTabList.size(); |
315 | 0 | SvLBoxTab* pTab = m_aTabs[ nTab ].get(); |
316 | |
|
317 | 0 | if (!bVisible) |
318 | 0 | pTab->nFlags |= SvLBoxTabFlags::HIDDEN; |
319 | 0 | else |
320 | 0 | pTab->nFlags &= ~SvLBoxTabFlags::HIDDEN; |
321 | 0 | } |
322 | | |
323 | | bool SvTabListBox::GetTabVisible(sal_uInt16 nTab) |
324 | 0 | { |
325 | 0 | DBG_ASSERT(nTab<mvTabList.size(),"GetTabVisible:Invalid Tab"); |
326 | 0 | if( nTab >= mvTabList.size() ) |
327 | 0 | return true; |
328 | | |
329 | 0 | if( SvTreeListBox::m_nTreeFlags & SvTreeFlags::RECALCTABS ) |
330 | 0 | SetTabs(); |
331 | |
|
332 | 0 | nTab += m_aTabs.size() - mvTabList.size(); |
333 | 0 | SvLBoxTab* pTab = m_aTabs[ nTab ].get(); |
334 | 0 | return !pTab->IsHidden(); |
335 | 0 | } |
336 | | |
337 | | tools::Long SvTabListBox::GetLogicTab( sal_uInt16 nTab ) |
338 | 0 | { |
339 | 0 | if (SvTreeListBox::m_nTreeFlags & SvTreeFlags::RECALCTABS) |
340 | 0 | SetTabs(); |
341 | |
|
342 | 0 | DBG_ASSERT(nTab<mvTabList.size(),"GetTabPos:Invalid Tab"); |
343 | 0 | return m_aTabs[nTab]->GetPos(); |
344 | 0 | } |
345 | | |
346 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |