Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name valueset.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SVT_DLLIMPLEMENTATION -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/svtools/source/inc -I /home/maarten/src/libreoffice/core/svtools/inc -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx

/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx

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 <sal/config.h>
21
22#include <o3tl/safeint.hxx>
23#include <tools/debug.hxx>
24#include <tools/stream.hxx>
25#include <comphelper/base64.hxx>
26#include <vcl/decoview.hxx>
27#include <vcl/event.hxx>
28#include <vcl/graph.hxx>
29#include <vcl/svapp.hxx>
30#include <vcl/cvtgrf.hxx>
31#include <vcl/help.hxx>
32#include <vcl/settings.hxx>
33#include <vcl/commandevent.hxx>
34#include <vcl/virdev.hxx>
35
36#include <com/sun/star/accessibility/AccessibleEventId.hpp>
37#include <com/sun/star/lang/XComponent.hpp>
38#include <rtl/ustring.hxx>
39#include <sal/log.hxx>
40#include "valueimp.hxx"
41
42#include <svtools/valueset.hxx>
43#include <boost/property_tree/ptree.hpp>
44
45#include <uiobject.hxx>
46#include <vcl/uitest/logger.hxx>
47#include <vcl/uitest/eventdescription.hxx>
48
49using namespace css::uno;
50using namespace css::lang;
51using namespace css::accessibility;
52
53namespace
54{
55void collectUIInformation( const OUString& aID , const OUString& aParentID , const OUString& aPos )
56{
57 EventDescription aDescription;
58 aDescription.aID = aID ;
59 aDescription.aParameters = {{"POS", aPos }};
60 aDescription.aAction = "SELECT";
61 aDescription.aKeyWord = "ValueSet";
62 aDescription.aParent = aParentID;
63 UITestLogger::getInstance().logEvent(aDescription);
64}
65
66enum
67{
68 ITEM_OFFSET = 4,
69 ITEM_OFFSET_DOUBLE = 6,
70 NAME_LINE_OFF_X = 2,
71 NAME_LINE_OFF_Y = 2,
72 NAME_LINE_HEIGHT = 2,
73 NAME_OFFSET = 2,
74};
75
76}
77
78ValueSet::ValueSet(std::unique_ptr<weld::ScrolledWindow> pScrolledWindow)
79 : maVirDev( VclPtr<VirtualDevice>::Create())
80 , mxScrolledWindow(std::move(pScrolledWindow))
81 , mnHighItemId(0)
82 , maColor(COL_TRANSPARENT)
83 , mnStyle(0)
84 , mbFormat(true)
85 , mbHighlight(false)
86{
87 maVirDev->SetBackground(Application::GetSettings().GetStyleSettings().GetFaceColor());
88
89 mnItemWidth = 0;
90 mnItemHeight = 0;
91 mnTextOffset = 0;
92 mnVisLines = 0;
93 mnLines = 0;
94 mnUserItemWidth = 0;
95 mnUserItemHeight = 0;
96 mnFirstLine = 0;
97 mnSelItemId = 0;
98 mnSavedItemId = -1;
99 mnCols = 0;
100 mnCurCol = 0;
101 mnUserCols = 0;
102 mnUserVisLines = 0;
103 mnSpacing = 0;
104 mnFrameStyle = DrawFrameStyle::NONE;
105 mbNoSelection = true;
106 mbDrawSelection = true;
107 mbBlackSel = false;
108 mbDoubleSel = false;
109 mbScroll = false;
110 mbFullMode = true;
111 mbEdgeBlending = false;
112 mbHasVisibleItems = false;
113
114 if (mxScrolledWindow)
115 mxScrolledWindow->connect_vadjustment_changed(LINK(this, ValueSet, ImplScrollHdl)::tools::detail::makeLink( ::tools::detail::castTo<ValueSet
*>(this), &ValueSet::LinkStubImplScrollHdl)
);
116}
117
118void ValueSet::SetDrawingArea(weld::DrawingArea* pDrawingArea)
119{
120 CustomWidgetController::SetDrawingArea(pDrawingArea);
121 // #106446#, #106601# force mirroring of virtual device
122 maVirDev->EnableRTL(pDrawingArea->get_direction());
123}
124
125Reference<XAccessible> ValueSet::CreateAccessible()
126{
127 if (!mxAccessible)
128 mxAccessible.set(new ValueSetAcc(this));
129 return mxAccessible;
130}
131
132ValueSet::~ValueSet()
133{
134 Reference<XComponent> xComponent(mxAccessible, UNO_QUERY);
135 if (xComponent.is())
1
Taking false branch
136 xComponent->dispose();
137
138 ImplDeleteItems();
139}
2
Calling '~ScopedVclPtr'
140
141void ValueSet::ImplDeleteItems()
142{
143 const size_t n = mItemList.size();
144
145 for ( size_t i = 0; i < n; ++i )
146 {
147 ValueSetItem* pItem = mItemList[i].get();
148 if ( pItem->mbVisible && ImplHasAccessibleListeners() )
149 {
150 Any aOldAny;
151 Any aNewAny;
152
153 aOldAny <<= pItem->GetAccessible( false/*bIsTransientChildrenDisabled*/ );
154 ImplFireAccessibleEvent(AccessibleEventId::CHILD, aOldAny, aNewAny);
155 }
156
157 mItemList[i].reset();
158 }
159
160 mItemList.clear();
161}
162
163void ValueSet::Select()
164{
165 collectUIInformation(OStringToOUString(GetDrawingArea()->get_buildable_name(),RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76))) , OStringToOUString(GetDrawingArea()->get_help_id(),RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76))) , OUString::number(GetSelectedItemId()));
166 maSelectHdl.Call( this );
167}
168
169void ValueSet::UserDraw( const UserDrawEvent& )
170{
171}
172
173size_t ValueSet::ImplGetItem( const Point& rPos ) const
174{
175 if (!mbHasVisibleItems)
176 {
177 return VALUESET_ITEM_NOTFOUND(size_t(-1));
178 }
179
180 if (mpNoneItem && maNoneItemRect.IsInside(rPos))
181 {
182 return VALUESET_ITEM_NONEITEM0xFFFE;
183 }
184
185 if (maItemListRect.IsInside(rPos))
186 {
187 const int xc = rPos.X() - maItemListRect.Left();
188 const int yc = rPos.Y() - maItemListRect.Top();
189 // The point is inside the area of item list,
190 // let's find the containing item.
191 const int col = xc / (mnItemWidth + mnSpacing);
192 const int x = xc % (mnItemWidth + mnSpacing);
193 const int row = yc / (mnItemHeight + mnSpacing);
194 const int y = yc % (mnItemHeight + mnSpacing);
195
196 if (x < mnItemWidth && y < mnItemHeight)
197 {
198 // the point is inside item rect and not inside spacing
199 const size_t item = (mnFirstLine + row) * static_cast<size_t>(mnCols) + col;
200 if (item < mItemList.size())
201 {
202 return item;
203 }
204 }
205 }
206
207 return VALUESET_ITEM_NOTFOUND(size_t(-1));
208}
209
210ValueSetItem* ValueSet::ImplGetItem( size_t nPos )
211{
212 if (nPos == VALUESET_ITEM_NONEITEM0xFFFE)
213 return mpNoneItem.get();
214 else
215 return (nPos < mItemList.size()) ? mItemList[nPos].get() : nullptr;
216}
217
218ValueSetItem* ValueSet::ImplGetFirstItem()
219{
220 return !mItemList.empty() ? mItemList[0].get() : nullptr;
221}
222
223sal_uInt16 ValueSet::ImplGetVisibleItemCount() const
224{
225 sal_uInt16 nRet = 0;
226 const size_t nItemCount = mItemList.size();
227
228 for ( size_t n = 0; n < nItemCount; ++n )
229 {
230 if ( mItemList[n]->mbVisible )
231 ++nRet;
232 }
233
234 return nRet;
235}
236
237void ValueSet::ImplFireAccessibleEvent( short nEventId, const Any& rOldValue, const Any& rNewValue )
238{
239 ValueSetAcc* pAcc = ValueSetAcc::getImplementation(mxAccessible);
240
241 if( pAcc )
242 pAcc->FireAccessibleEvent( nEventId, rOldValue, rNewValue );
243}
244
245bool ValueSet::ImplHasAccessibleListeners()
246{
247 ValueSetAcc* pAcc = ValueSetAcc::getImplementation(mxAccessible);
248 return( pAcc && pAcc->HasAccessibleListeners() );
249}
250
251IMPL_LINK(ValueSet, ImplScrollHdl, weld::ScrolledWindow&, rScrollWin, void)void ValueSet::LinkStubImplScrollHdl(void * instance, weld::ScrolledWindow
& data) { return static_cast<ValueSet *>(instance)->
ImplScrollHdl(data); } void ValueSet::ImplScrollHdl(weld::ScrolledWindow
& rScrollWin)
252{
253 auto nNewFirstLine = rScrollWin.vadjustment_get_value();
254 if ( nNewFirstLine != mnFirstLine )
255 {
256 mnFirstLine = nNewFirstLine;
257 mbFormat = true;
258 Invalidate();
259 }
260}
261
262void ValueSet::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
263{
264 if (GetStyle() & WB_FLATVALUESET(WinBits(0x02000000)))
265 {
266 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
267 rRenderContext.SetLineColor();
268 rRenderContext.SetFillColor(rStyleSettings.GetFaceColor());
269 long nOffY = maVirDev->GetOutputSizePixel().Height();
270 Size aWinSize(GetOutputSizePixel());
271 rRenderContext.DrawRect(tools::Rectangle(Point(0, nOffY ), Point( aWinSize.Width(), aWinSize.Height())));
272 }
273
274 ImplDraw(rRenderContext);
275}
276
277void ValueSet::GetFocus()
278{
279 SAL_INFO("svtools", "value set getting focus")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "svtools")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "value set getting focus"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("svtools"
), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "279" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "value set getting focus"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"value set getting focus"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "279" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "value set getting focus") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "279" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "value set getting focus"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"value set getting focus"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "279" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
280 Invalidate();
281 CustomWidgetController::GetFocus();
282
283 // Tell the accessible object that we got the focus.
284 ValueSetAcc* pAcc = ValueSetAcc::getImplementation(mxAccessible);
285 if (pAcc)
286 pAcc->GetFocus();
287}
288
289void ValueSet::LoseFocus()
290{
291 SAL_INFO("svtools", "value set losing focus")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "svtools")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "value set losing focus"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("svtools"
), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "291" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "value set losing focus"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"value set losing focus"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "291" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "value set losing focus") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "291" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "value set losing focus"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"value set losing focus"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("svtools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "291" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
292 Invalidate();
293 CustomWidgetController::LoseFocus();
294
295 // Tell the accessible object that we lost the focus.
296 ValueSetAcc* pAcc = ValueSetAcc::getImplementation(mxAccessible);
297 if( pAcc )
298 pAcc->LoseFocus();
299}
300
301void ValueSet::Resize()
302{
303 mbFormat = true;
304 if ( IsReallyVisible() && IsUpdateMode() )
305 Invalidate();
306 CustomWidgetController::Resize();
307}
308
309bool ValueSet::KeyInput( const KeyEvent& rKeyEvent )
310{
311 size_t nLastItem = mItemList.size();
312
313 if ( !nLastItem || !ImplGetFirstItem() )
314 return CustomWidgetController::KeyInput(rKeyEvent);
315
316 if (mbFormat)
317 Invalidate();
318
319 --nLastItem;
320
321 const size_t nCurPos
322 = mnSelItemId ? GetItemPos(mnSelItemId) : (mpNoneItem ? VALUESET_ITEM_NONEITEM0xFFFE : 0);
323 size_t nItemPos = VALUESET_ITEM_NOTFOUND(size_t(-1));
324 size_t nVStep = mnCols;
325
326 switch (rKeyEvent.GetKeyCode().GetCode())
327 {
328 case KEY_HOME:
329 nItemPos = mpNoneItem ? VALUESET_ITEM_NONEITEM0xFFFE : 0;
330 break;
331
332 case KEY_END:
333 nItemPos = nLastItem;
334 break;
335
336 case KEY_LEFT:
337 if (nCurPos != VALUESET_ITEM_NONEITEM0xFFFE)
338 {
339 if (nCurPos)
340 {
341 nItemPos = nCurPos-1;
342 }
343 else if (mpNoneItem)
344 {
345 nItemPos = VALUESET_ITEM_NONEITEM0xFFFE;
346 }
347 }
348 break;
349
350 case KEY_RIGHT:
351 if (nCurPos < nLastItem)
352 {
353 if (nCurPos == VALUESET_ITEM_NONEITEM0xFFFE)
354 {
355 nItemPos = 0;
356 }
357 else
358 {
359 nItemPos = nCurPos+1;
360 }
361 }
362 break;
363
364 case KEY_PAGEUP:
365 if (rKeyEvent.GetKeyCode().IsShift() || rKeyEvent.GetKeyCode().IsMod1() || rKeyEvent.GetKeyCode().IsMod2())
366 {
367 return CustomWidgetController::KeyInput(rKeyEvent);
368 }
369 nVStep *= mnVisLines;
370 [[fallthrough]];
371 case KEY_UP:
372 if (nCurPos != VALUESET_ITEM_NONEITEM0xFFFE)
373 {
374 if (nCurPos == nLastItem)
375 {
376 const size_t nCol = mnCols ? nLastItem % mnCols : 0;
377 if (nCol < mnCurCol)
378 {
379 // Move to previous row/page, keeping the old column
380 nVStep -= mnCurCol - nCol;
381 }
382 }
383 if (nCurPos >= nVStep)
384 {
385 // Go up of a whole page
386 nItemPos = nCurPos-nVStep;
387 }
388 else if (mpNoneItem)
389 {
390 nItemPos = VALUESET_ITEM_NONEITEM0xFFFE;
391 }
392 else if (nCurPos > mnCols)
393 {
394 // Go to same column in first row
395 nItemPos = nCurPos % mnCols;
396 }
397 }
398 break;
399
400 case KEY_PAGEDOWN:
401 if (rKeyEvent.GetKeyCode().IsShift() || rKeyEvent.GetKeyCode().IsMod1() || rKeyEvent.GetKeyCode().IsMod2())
402 {
403 return CustomWidgetController::KeyInput(rKeyEvent);
404 }
405 nVStep *= mnVisLines;
406 [[fallthrough]];
407 case KEY_DOWN:
408 if (nCurPos != nLastItem)
409 {
410 if (nCurPos == VALUESET_ITEM_NONEITEM0xFFFE)
411 {
412 nItemPos = nVStep-mnCols+mnCurCol;
413 }
414 else
415 {
416 nItemPos = nCurPos+nVStep;
417 }
418 if (nItemPos > nLastItem)
419 {
420 nItemPos = nLastItem;
421 }
422 }
423 break;
424
425 case KEY_RETURN:
426 if (GetStyle() & WB_NO_DIRECTSELECT(WinBits(0x04000000)))
427 {
428 Select();
429 break;
430 }
431 [[fallthrough]];
432 default:
433 return CustomWidgetController::KeyInput(rKeyEvent);
434 }
435
436 if ( nItemPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
437 return true;
438
439 if ( nItemPos!=VALUESET_ITEM_NONEITEM0xFFFE && nItemPos<nLastItem )
440 {
441 // update current column only in case of a new position
442 // which is also not a "specially" handled one.
443 mnCurCol = mnCols ? nItemPos % mnCols : 0;
444 }
445 const sal_uInt16 nItemId = (nItemPos != VALUESET_ITEM_NONEITEM0xFFFE) ? GetItemId( nItemPos ) : 0;
446 if ( nItemId != mnSelItemId )
447 {
448 SelectItem( nItemId );
449 if (!(GetStyle() & WB_NO_DIRECTSELECT(WinBits(0x04000000))))
450 {
451 // select only if WB_NO_DIRECTSELECT is not set
452 Select();
453 }
454 }
455
456 return true;
457}
458
459void ValueSet::ImplTracking(const Point& rPos)
460{
461 ValueSetItem* pItem = ImplGetItem( ImplGetItem( rPos ) );
462 if ( pItem )
463 {
464 if( GetStyle() & WB_MENUSTYLEVALUESET(WinBits(0x08000000)) || GetStyle() & WB_FLATVALUESET(WinBits(0x02000000)) )
465 mbHighlight = true;
466
467 ImplHighlightItem( pItem->mnId );
468 }
469 else
470 {
471 if( GetStyle() & WB_MENUSTYLEVALUESET(WinBits(0x08000000)) || GetStyle() & WB_FLATVALUESET(WinBits(0x02000000)) )
472 mbHighlight = true;
473
474 ImplHighlightItem( mnSelItemId, false );
475 }
476}
477
478bool ValueSet::MouseButtonDown( const MouseEvent& rMouseEvent )
479{
480 if ( rMouseEvent.IsLeft() )
481 {
482 ValueSetItem* pItem = ImplGetItem( ImplGetItem( rMouseEvent.GetPosPixel() ) );
483 if (pItem && !rMouseEvent.IsMod2())
484 {
485 if (rMouseEvent.GetClicks() == 1)
486 {
487 SelectItem( pItem->mnId );
488 if (!(GetStyle() & WB_NOPOINTERFOCUS))
489 GrabFocus();
490 }
491 else if ( rMouseEvent.GetClicks() == 2 )
492 maDoubleClickHdl.Call( this );
493
494 return true;
495 }
496 }
497
498 return CustomWidgetController::MouseButtonDown( rMouseEvent );
499}
500
501bool ValueSet::MouseButtonUp( const MouseEvent& rMouseEvent )
502{
503 if (rMouseEvent.IsLeft() && !rMouseEvent.IsMod2())
504 {
505 Select();
506 return true;
507 }
508
509 return CustomWidgetController::MouseButtonUp( rMouseEvent );
510}
511
512bool ValueSet::MouseMove(const MouseEvent& rMouseEvent)
513{
514 // because of SelectionMode
515 if ((GetStyle() & WB_MENUSTYLEVALUESET(WinBits(0x08000000))) || (GetStyle() & WB_FLATVALUESET(WinBits(0x02000000))))
516 ImplTracking(rMouseEvent.GetPosPixel());
517 return CustomWidgetController::MouseMove(rMouseEvent);
518}
519
520void ValueSet::QueueReformat()
521{
522 queue_resize();
523 RecalcScrollBar();
524 mbFormat = true;
525 if ( IsReallyVisible() && IsUpdateMode() )
526 Invalidate();
527}
528
529void ValueSet::RemoveItem( sal_uInt16 nItemId )
530{
531 size_t nPos = GetItemPos( nItemId );
532
533 if ( nPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
534 return;
535
536 if ( nPos < mItemList.size() ) {
537 mItemList.erase( mItemList.begin() + nPos );
538 }
539
540 // reset variables
541 if (mnHighItemId == nItemId || mnSelItemId == nItemId)
542 {
543 mnCurCol = 0;
544 mnHighItemId = 0;
545 mnSelItemId = 0;
546 mbNoSelection = true;
547 }
548
549 QueueReformat();
550}
551
552void ValueSet::TurnOffScrollBar()
553{
554 if (mxScrolledWindow->get_vpolicy() == VclPolicyType::NEVER)
555 return;
556 mxScrolledWindow->set_vpolicy(VclPolicyType::NEVER);
557 weld::DrawingArea* pDrawingArea = GetDrawingArea();
558 Size aPrefSize(pDrawingArea->get_preferred_size());
559 pDrawingArea->set_size_request(aPrefSize.Width() + GetScrollWidth(), aPrefSize.Height());
560}
561
562void ValueSet::TurnOnScrollBar()
563{
564 if (mxScrolledWindow->get_vpolicy() == VclPolicyType::ALWAYS)
565 return;
566 mxScrolledWindow->set_vpolicy(VclPolicyType::ALWAYS);
567 weld::DrawingArea* pDrawingArea = GetDrawingArea();
568 Size aPrefSize(pDrawingArea->get_preferred_size());
569 pDrawingArea->set_size_request(aPrefSize.Width() - GetScrollWidth(), aPrefSize.Height());
570}
571
572void ValueSet::RecalcScrollBar()
573{
574 // reset scrolled window state to initial value
575 // so it will get configured to the right adjustment
576 WinBits nStyle = GetStyle();
577 if (mxScrolledWindow && (nStyle & WB_VSCROLL))
578 TurnOffScrollBar();
579}
580
581void ValueSet::Clear()
582{
583 ImplDeleteItems();
584
585 // reset variables
586 mnFirstLine = 0;
587 mnCurCol = 0;
588 mnHighItemId = 0;
589 mnSelItemId = 0;
590 mbNoSelection = true;
591
592 RecalcScrollBar();
593
594 mbFormat = true;
595 if ( IsReallyVisible() && IsUpdateMode() )
596 Invalidate();
597}
598
599size_t ValueSet::GetItemCount() const
600{
601 return mItemList.size();
602}
603
604size_t ValueSet::GetItemPos( sal_uInt16 nItemId ) const
605{
606 for ( size_t i = 0, n = mItemList.size(); i < n; ++i ) {
607 if ( mItemList[i]->mnId == nItemId ) {
608 return i;
609 }
610 }
611 return VALUESET_ITEM_NOTFOUND(size_t(-1));
612}
613
614sal_uInt16 ValueSet::GetItemId( size_t nPos ) const
615{
616 return ( nPos < mItemList.size() ) ? mItemList[nPos]->mnId : 0 ;
617}
618
619sal_uInt16 ValueSet::GetItemId( const Point& rPos ) const
620{
621 size_t nItemPos = ImplGetItem( rPos );
622 if ( nItemPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
623 return GetItemId( nItemPos );
624
625 return 0;
626}
627
628tools::Rectangle ValueSet::GetItemRect( sal_uInt16 nItemId ) const
629{
630 const size_t nPos = GetItemPos( nItemId );
631
632 if ( nPos!=VALUESET_ITEM_NOTFOUND(size_t(-1)) && mItemList[nPos]->mbVisible )
633 return ImplGetItemRect( nPos );
634
635 return tools::Rectangle();
636}
637
638tools::Rectangle ValueSet::ImplGetItemRect( size_t nPos ) const
639{
640 const size_t nVisibleBegin = static_cast<size_t>(mnFirstLine)*mnCols;
641 const size_t nVisibleEnd = nVisibleBegin + static_cast<size_t>(mnVisLines)*mnCols;
642
643 // Check if the item is inside the range of the displayed ones,
644 // taking into account that last row could be incomplete
645 if ( nPos<nVisibleBegin || nPos>=nVisibleEnd || nPos>=mItemList.size() )
646 return tools::Rectangle();
647
648 nPos -= nVisibleBegin;
649
650 const size_t row = mnCols ? nPos/mnCols : 0;
651 const size_t col = mnCols ? nPos%mnCols : 0;
652 const long x = maItemListRect.Left()+col*(mnItemWidth+mnSpacing);
653 const long y = maItemListRect.Top()+row*(mnItemHeight+mnSpacing);
654
655 return tools::Rectangle( Point(x, y), Size(mnItemWidth, mnItemHeight) );
656}
657
658void ValueSet::ImplHighlightItem( sal_uInt16 nItemId, bool bIsSelection )
659{
660 if ( mnHighItemId == nItemId )
661 return;
662
663 // remember the old item to delete the previous selection
664 mnHighItemId = nItemId;
665
666 // don't draw the selection if nothing is selected
667 if ( !bIsSelection && mbNoSelection )
668 mbDrawSelection = false;
669
670 // remove the old selection and draw the new one
671 Invalidate();
672 mbDrawSelection = true;
673}
674
675void ValueSet::ImplDraw(vcl::RenderContext& rRenderContext)
676{
677 if (mbFormat)
678 Format(rRenderContext);
679
680 Point aDefPos;
681 Size aSize = maVirDev->GetOutputSizePixel();
682
683 rRenderContext.DrawOutDev(aDefPos, aSize, aDefPos, aSize, *maVirDev);
684
685 // draw parting line to the Namefield
686 if (GetStyle() & WB_NAMEFIELD(WinBits(0x00040000)))
687 {
688 if (!(GetStyle() & WB_FLATVALUESET(WinBits(0x02000000))))
689 {
690 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
691 Size aWinSize(GetOutputSizePixel());
692 Point aPos1(NAME_LINE_OFF_X, mnTextOffset + NAME_LINE_OFF_Y);
693 Point aPos2(aWinSize.Width() - (NAME_LINE_OFF_X * 2), mnTextOffset + NAME_LINE_OFF_Y);
694 if (!(rStyleSettings.GetOptions() & StyleSettingsOptions::Mono))
695 {
696 rRenderContext.SetLineColor(rStyleSettings.GetShadowColor());
697 rRenderContext.DrawLine(aPos1, aPos2);
698 aPos1.AdjustY( 1 );
699 aPos2.AdjustY( 1 );
700 rRenderContext.SetLineColor(rStyleSettings.GetLightColor());
701 }
702 else
703 rRenderContext.SetLineColor(rStyleSettings.GetWindowTextColor());
704 rRenderContext.DrawLine(aPos1, aPos2);
705 }
706 }
707
708 ImplDrawSelect(rRenderContext);
709}
710
711/**
712 * An inelegant method; sets the item width & height such that
713 * all of the included items and their labels fit; if we can
714 * calculate that.
715 */
716void ValueSet::RecalculateItemSizes()
717{
718 Size aLargestItem = GetLargestItemSize();
719
720 if ( mnUserItemWidth != aLargestItem.Width() ||
721 mnUserItemHeight != aLargestItem.Height() )
722 {
723 mnUserItemWidth = aLargestItem.Width();
724 mnUserItemHeight = aLargestItem.Height();
725 QueueReformat();
726 }
727}
728
729void ValueSet::SetFirstLine(sal_uInt16 nNewFirstLine)
730{
731 if (nNewFirstLine != mnFirstLine)
732 {
733 mnFirstLine = nNewFirstLine;
734 if (mxScrolledWindow)
735 mxScrolledWindow->vadjustment_set_value(mnFirstLine);
736 }
737}
738
739void ValueSet::SelectItem( sal_uInt16 nItemId )
740{
741 size_t nItemPos = 0;
742
743 if ( nItemId )
744 {
745 nItemPos = GetItemPos( nItemId );
746 if ( nItemPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
747 return;
748 }
749
750 if ( !((mnSelItemId != nItemId) || mbNoSelection) )
751 return;
752
753 const sal_uInt16 nOldItem = mnSelItemId;
754 mnSelItemId = nItemId;
755 mbNoSelection = false;
756
757 bool bNewOut = !mbFormat && IsReallyVisible() && IsUpdateMode();
758 bool bNewLine = false;
759
760 if (weld::DrawingArea* pNeedsFormatToScroll = !mnCols ? GetDrawingArea() : nullptr)
761 {
762 Format(pNeedsFormatToScroll->get_ref_device());
763 // reset scrollbar so its set to the later calculated mnFirstLine on
764 // the next Format
765 RecalcScrollBar(); // reset scrollbar so its set to the later calculated
766 }
767
768 // if necessary scroll to the visible area
769 if (mbScroll && nItemId && mnCols)
770 {
771 sal_uInt16 nNewLine = static_cast<sal_uInt16>(nItemPos / mnCols);
772 if ( nNewLine < mnFirstLine )
773 {
774 SetFirstLine(nNewLine);
775 bNewLine = true;
776 }
777 else if ( nNewLine > o3tl::make_unsigned(mnFirstLine+mnVisLines-1) )
778 {
779 SetFirstLine(static_cast<sal_uInt16>(nNewLine-mnVisLines+1));
780 bNewLine = true;
781 }
782 }
783
784 if ( bNewOut )
785 {
786 if ( bNewLine )
787 {
788 // redraw everything if the visible area has changed
789 mbFormat = true;
790 }
791 Invalidate();
792 }
793
794 if( !ImplHasAccessibleListeners() )
795 return;
796
797 // focus event (deselect)
798 if( nOldItem )
799 {
800 const size_t nPos = GetItemPos( nItemId );
801
802 if( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
803 {
804 ValueItemAcc* pItemAcc = ValueItemAcc::getImplementation(
805 mItemList[nPos]->GetAccessible( false/*bIsTransientChildrenDisabled*/ ) );
806
807 if( pItemAcc )
808 {
809 Any aOldAny;
810 Any aNewAny;
811 aOldAny <<= Reference<XInterface>(static_cast<cppu::OWeakObject*>(pItemAcc));
812 ImplFireAccessibleEvent(AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldAny, aNewAny );
813 }
814 }
815 }
816
817 // focus event (select)
818 const size_t nPos = GetItemPos( mnSelItemId );
819
820 ValueSetItem* pItem;
821 if( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
822 pItem = mItemList[nPos].get();
823 else
824 pItem = mpNoneItem.get();
825
826 ValueItemAcc* pItemAcc = nullptr;
827 if (pItem != nullptr)
828 pItemAcc = ValueItemAcc::getImplementation( pItem->GetAccessible( false/*bIsTransientChildrenDisabled*/ ) );
829
830 if( pItemAcc )
831 {
832 Any aOldAny;
833 Any aNewAny;
834 aNewAny <<= Reference<XInterface>(static_cast<cppu::OWeakObject*>(pItemAcc));
835 ImplFireAccessibleEvent(AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldAny, aNewAny);
836 }
837
838 // selection event
839 Any aOldAny;
840 Any aNewAny;
841 ImplFireAccessibleEvent(AccessibleEventId::SELECTION_CHANGED, aOldAny, aNewAny);
842}
843
844void ValueSet::SetNoSelection()
845{
846 mbNoSelection = true;
847 mbHighlight = false;
848
849 if (IsReallyVisible() && IsUpdateMode())
850 Invalidate();
851}
852
853void ValueSet::SetStyle(WinBits nStyle)
854{
855 if (nStyle != mnStyle)
856 {
857 mnStyle = nStyle;
858 mbFormat = true;
859 Invalidate();
860 }
861}
862
863void ValueSet::Format(vcl::RenderContext const & rRenderContext)
864{
865 Size aWinSize(GetOutputSizePixel());
866 size_t nItemCount = mItemList.size();
867 WinBits nStyle = GetStyle();
868 long nTxtHeight = rRenderContext.GetTextHeight();
869 long nOff;
870 long nNoneHeight;
871 long nNoneSpace;
872
873 if (mxScrolledWindow && !(nStyle & WB_VSCROLL) && mxScrolledWindow->get_vpolicy() != VclPolicyType::NEVER)
874 TurnOffScrollBar();
875
876 // calculate item offset
877 if (nStyle & WB_ITEMBORDER(WinBits(0x00010000)))
878 {
879 if (nStyle & WB_DOUBLEBORDER(WinBits(0x00020000)))
880 nOff = ITEM_OFFSET_DOUBLE;
881 else
882 nOff = ITEM_OFFSET;
883 }
884 else
885 nOff = 0;
886
887 // consider size, if NameField does exist
888 if (nStyle & WB_NAMEFIELD(WinBits(0x00040000)))
889 {
890 mnTextOffset = aWinSize.Height() - nTxtHeight - NAME_OFFSET;
891 aWinSize.AdjustHeight( -(nTxtHeight + NAME_OFFSET) );
892
893 if (!(nStyle & WB_FLATVALUESET(WinBits(0x02000000))))
894 {
895 mnTextOffset -= NAME_LINE_HEIGHT + NAME_LINE_OFF_Y;
896 aWinSize.AdjustHeight( -(NAME_LINE_HEIGHT + NAME_LINE_OFF_Y) );
897 }
898 }
899 else
900 mnTextOffset = 0;
901
902 // consider offset and size, if NoneField does exist
903 if (nStyle & WB_NONEFIELD(WinBits(0x00080000)))
904 {
905 nNoneHeight = nTxtHeight + nOff;
906 nNoneSpace = mnSpacing;
907 }
908 else
909 {
910 nNoneHeight = 0;
911 nNoneSpace = 0;
912 mpNoneItem.reset();
913 }
914
915 // calculate number of columns
916 if (!mnUserCols)
917 {
918 if (mnUserItemWidth)
919 {
920 mnCols = static_cast<sal_uInt16>((aWinSize.Width() - mnSpacing) / (mnUserItemWidth + mnSpacing));
921 if (mnCols <= 0)
922 mnCols = 1;
923 }
924 else
925 {
926 mnCols = 1;
927 }
928 }
929 else
930 {
931 mnCols = mnUserCols;
932 }
933
934 // calculate number of rows
935 mbScroll = false;
936
937 auto nOldLines = mnLines;
938 // Floor( (M+N-1)/N )==Ceiling( M/N )
939 mnLines = (static_cast<long>(nItemCount) + mnCols - 1) / mnCols;
940 if (mnLines <= 0)
941 mnLines = 1;
942
943 bool bAdjustmentOutOfDate = nOldLines != mnLines;
944
945 auto nOldVisLines = mnVisLines;
946
947 long nCalcHeight = aWinSize.Height() - nNoneHeight;
948 if (mnUserVisLines)
949 {
950 mnVisLines = mnUserVisLines;
951 }
952 else if (mnUserItemHeight)
953 {
954 mnVisLines = (nCalcHeight - nNoneSpace + mnSpacing) / (mnUserItemHeight + mnSpacing);
955 if (!mnVisLines)
956 mnVisLines = 1;
957 }
958 else
959 {
960 mnVisLines = mnLines;
961 }
962
963 bAdjustmentOutOfDate |= nOldVisLines != mnVisLines;
964
965 if (mnLines > mnVisLines)
966 mbScroll = true;
967
968 if (mnLines <= mnVisLines)
969 {
970 SetFirstLine(0);
971 }
972 else
973 {
974 if (mnFirstLine > o3tl::make_unsigned(mnLines - mnVisLines))
975 SetFirstLine(static_cast<sal_uInt16>(mnLines - mnVisLines));
976 }
977
978 // calculate item size
979 const long nColSpace = (mnCols - 1) * static_cast<long>(mnSpacing);
980 const long nLineSpace = ((mnVisLines - 1) * mnSpacing) + nNoneSpace;
981 if (mnUserItemWidth && !mnUserCols)
982 {
983 mnItemWidth = mnUserItemWidth;
984 if (mnItemWidth > aWinSize.Width() - nColSpace)
985 mnItemWidth = aWinSize.Width() - nColSpace;
986 }
987 else
988 mnItemWidth = (aWinSize.Width() - nColSpace) / mnCols;
989 if (mnUserItemHeight && !mnUserVisLines)
990 {
991 mnItemHeight = mnUserItemHeight;
992 if (mnItemHeight > nCalcHeight - nNoneSpace)
993 mnItemHeight = nCalcHeight - nNoneSpace;
994 }
995 else
996 {
997 nCalcHeight -= nLineSpace;
998 mnItemHeight = nCalcHeight / mnVisLines;
999 }
1000
1001 // Init VirDev
1002 maVirDev->SetSettings(rRenderContext.GetSettings());
1003 maVirDev->SetOutputSizePixel(aWinSize);
1004
1005 // nothing is changed in case of too small items
1006 if ((mnItemWidth <= 0) ||
1007 (mnItemHeight <= ((nStyle & WB_ITEMBORDER(WinBits(0x00010000))) ? 4 : 2)) ||
1008 !nItemCount)
1009 {
1010 mbHasVisibleItems = false;
1011
1012 if ((nStyle & WB_NONEFIELD(WinBits(0x00080000))) && mpNoneItem)
1013 {
1014 mpNoneItem->mbVisible = false;
1015 mpNoneItem->maText = GetText();
1016 }
1017
1018 for (size_t i = 0; i < nItemCount; i++)
1019 {
1020 mItemList[i]->mbVisible = false;
1021 }
1022
1023 if (mxScrolledWindow && mxScrolledWindow->get_vpolicy() != VclPolicyType::NEVER)
1024 TurnOffScrollBar();
1025 }
1026 else
1027 {
1028 mbHasVisibleItems = true;
1029
1030 // determine Frame-Style
1031 if (nStyle & WB_DOUBLEBORDER(WinBits(0x00020000)))
1032 mnFrameStyle = DrawFrameStyle::DoubleIn;
1033 else
1034 mnFrameStyle = DrawFrameStyle::In;
1035
1036 // determine selected color and width
1037 // if necessary change the colors, to make the selection
1038 // better detectable
1039 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
1040 Color aHighColor(rStyleSettings.GetHighlightColor());
1041 if (((aHighColor.GetRed() > 0x80) || (aHighColor.GetGreen() > 0x80) ||
1042 (aHighColor.GetBlue() > 0x80)) ||
1043 ((aHighColor.GetRed() == 0x80) && (aHighColor.GetGreen() == 0x80) &&
1044 (aHighColor.GetBlue() == 0x80)))
1045 {
1046 mbBlackSel = true;
1047 }
1048 else
1049 {
1050 mbBlackSel = false;
1051 }
1052 // draw the selection with double width if the items are bigger
1053 if ((nStyle & WB_DOUBLEBORDER(WinBits(0x00020000))) &&
1054 ((mnItemWidth >= 25) && (mnItemHeight >= 20)))
1055 {
1056 mbDoubleSel = true;
1057 }
1058 else
1059 {
1060 mbDoubleSel = false;
1061 }
1062
1063 // calculate offsets
1064 long nStartX;
1065 long nStartY;
1066 if (mbFullMode)
1067 {
1068 long nAllItemWidth = (mnItemWidth * mnCols) + nColSpace;
1069 long nAllItemHeight = (mnItemHeight * mnVisLines) + nNoneHeight + nLineSpace;
1070 nStartX = (aWinSize.Width() - nAllItemWidth) / 2;
1071 nStartY = (aWinSize.Height() - nAllItemHeight) / 2;
1072 }
1073 else
1074 {
1075 nStartX = 0;
1076 nStartY = 0;
1077 }
1078
1079 // calculate and draw items
1080 maVirDev->SetLineColor();
1081 long x = nStartX;
1082 long y = nStartY;
1083
1084 // create NoSelection field and show it
1085 if (nStyle & WB_NONEFIELD(WinBits(0x00080000)))
1086 {
1087 if (!mpNoneItem)
1088 mpNoneItem.reset(new ValueSetItem(*this));
1089
1090 mpNoneItem->mnId = 0;
1091 mpNoneItem->meType = VALUESETITEM_NONE;
1092 mpNoneItem->mbVisible = true;
1093 maNoneItemRect.SetLeft( x );
1094 maNoneItemRect.SetTop( y );
1095 maNoneItemRect.SetRight( maNoneItemRect.Left() + aWinSize.Width() - x - 1 );
1096 maNoneItemRect.SetBottom( y + nNoneHeight - 1 );
1097
1098 ImplFormatItem(rRenderContext, mpNoneItem.get(), maNoneItemRect);
1099
1100 y += nNoneHeight + nNoneSpace;
1101 }
1102
1103 // draw items
1104 sal_uLong nFirstItem = static_cast<sal_uLong>(mnFirstLine) * mnCols;
1105 sal_uLong nLastItem = nFirstItem + (mnVisLines * mnCols);
1106
1107 maItemListRect.SetLeft( x );
1108 maItemListRect.SetTop( y );
1109 maItemListRect.SetRight( x + mnCols * (mnItemWidth + mnSpacing) - mnSpacing - 1 );
1110 maItemListRect.SetBottom( y + mnVisLines * (mnItemHeight + mnSpacing) - mnSpacing - 1 );
1111
1112 if (!mbFullMode)
1113 {
1114 // If want also draw parts of items in the last line,
1115 // then we add one more line if parts of these line are
1116 // visible
1117 if (y + (mnVisLines * (mnItemHeight + mnSpacing)) < aWinSize.Height())
1118 nLastItem += mnCols;
1119 maItemListRect.SetBottom( aWinSize.Height() - y );
1120 }
1121 for (size_t i = 0; i < nItemCount; i++)
1122 {
1123 ValueSetItem* pItem = mItemList[i].get();
1124
1125 if (i >= nFirstItem && i < nLastItem)
1126 {
1127 if (!pItem->mbVisible && ImplHasAccessibleListeners())
1128 {
1129 Any aOldAny;
1130 Any aNewAny;
1131
1132 aNewAny <<= pItem->GetAccessible(false/*bIsTransientChildrenDisabled*/);
1133 ImplFireAccessibleEvent(AccessibleEventId::CHILD, aOldAny, aNewAny);
1134 }
1135
1136 pItem->mbVisible = true;
1137 ImplFormatItem(rRenderContext, pItem, tools::Rectangle(Point(x, y), Size(mnItemWidth, mnItemHeight)));
1138
1139 if (!((i + 1) % mnCols))
1140 {
1141 x = nStartX;
1142 y += mnItemHeight + mnSpacing;
1143 }
1144 else
1145 x += mnItemWidth + mnSpacing;
1146 }
1147 else
1148 {
1149 if (pItem->mbVisible && ImplHasAccessibleListeners())
1150 {
1151 Any aOldAny;
1152 Any aNewAny;
1153
1154 aOldAny <<= pItem->GetAccessible(false/*bIsTransientChildrenDisabled*/);
1155 ImplFireAccessibleEvent(AccessibleEventId::CHILD, aOldAny, aNewAny);
1156 }
1157
1158 pItem->mbVisible = false;
1159 }
1160 }
1161
1162 // arrange ScrollBar, set values and show it
1163 if (mxScrolledWindow && (nStyle & WB_VSCROLL))
1164 {
1165 bool bTurnScrollbarOn = mxScrolledWindow->get_vpolicy() != VclPolicyType::ALWAYS;
1166 if (bAdjustmentOutOfDate || bTurnScrollbarOn)
1167 {
1168 long nPageSize = mnVisLines;
1169 if (nPageSize < 1)
1170 nPageSize = 1;
1171 mxScrolledWindow->vadjustment_configure(mnFirstLine, 0, mnLines, 1,
1172 mnVisLines, nPageSize);
1173 }
1174
1175 if (bTurnScrollbarOn)
1176 TurnOnScrollBar();
1177 }
1178 }
1179
1180 // waiting for the next since the formatting is finished
1181 mbFormat = false;
1182}
1183
1184void ValueSet::ImplDrawSelect(vcl::RenderContext& rRenderContext)
1185{
1186 if (!IsReallyVisible())
1187 return;
1188
1189 const bool bFocus = HasFocus();
1190 const bool bDrawSel = !((mbNoSelection && !mbHighlight) || (!mbDrawSelection && mbHighlight));
1191
1192 if (!bFocus && !bDrawSel)
1193 {
1194 ImplDrawItemText(rRenderContext, OUString());
1195 return;
1196 }
1197
1198 ImplDrawSelect(rRenderContext, mnSelItemId, bFocus, bDrawSel);
1199 if (mbHighlight)
1200 {
1201 ImplDrawSelect(rRenderContext, mnHighItemId, bFocus, bDrawSel);
1202 }
1203}
1204
1205void ValueSet::ImplDrawSelect(vcl::RenderContext& rRenderContext, sal_uInt16 nItemId, const bool bFocus, const bool bDrawSel )
1206{
1207 ValueSetItem* pItem;
1208 tools::Rectangle aRect;
1209 if (nItemId)
1210 {
1211 const size_t nPos = GetItemPos( nItemId );
1212 pItem = mItemList[ nPos ].get();
1213 aRect = ImplGetItemRect( nPos );
1214 }
1215 else if (mpNoneItem)
1216 {
1217 pItem = mpNoneItem.get();
1218 aRect = maNoneItemRect;
1219 }
1220 else if (bFocus && (pItem = ImplGetFirstItem()))
1221 {
1222 aRect = ImplGetItemRect(0);
1223 }
1224 else
1225 {
1226 return;
1227 }
1228
1229 if (!pItem->mbVisible)
1230 return;
1231
1232 // draw selection
1233 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
1234 rRenderContext.SetFillColor();
1235
1236 Color aDoubleColor(rStyleSettings.GetHighlightColor());
1237 Color aSingleColor(rStyleSettings.GetHighlightTextColor());
1238 if (!mbDoubleSel)
1239 {
1240 /*
1241 * #99777# contrast enhancement for thin mode
1242 */
1243 const Wallpaper& rWall = maVirDev->GetBackground();
1244 if (!rWall.IsBitmap() && ! rWall.IsGradient())
1245 {
1246 const Color& rBack = rWall.GetColor();
1247 if (rBack.IsDark() && ! aDoubleColor.IsBright())
1248 {
1249 aDoubleColor = COL_WHITE;
1250 aSingleColor = COL_BLACK;
1251 }
1252 else if (rBack.IsBright() && !aDoubleColor.IsDark())
1253 {
1254 aDoubleColor = COL_BLACK;
1255 aSingleColor = COL_WHITE;
1256 }
1257 }
1258 }
1259
1260 // specify selection output
1261 WinBits nStyle = GetStyle();
1262 if (nStyle & WB_MENUSTYLEVALUESET(WinBits(0x08000000)))
1263 {
1264 if (bFocus)
1265 DrawFocusRect(rRenderContext, aRect);
1266 if (bDrawSel)
1267 {
1268 rRenderContext.SetLineColor(mbBlackSel ? COL_BLACK : aDoubleColor);
1269 rRenderContext.DrawRect(aRect);
1270 }
1271 }
1272 else
1273 {
1274 if (bDrawSel)
1275 {
1276 rRenderContext.SetLineColor(mbBlackSel ? COL_BLACK : aDoubleColor);
1277 rRenderContext.DrawRect(aRect);
1278 }
1279 if (mbDoubleSel)
1280 {
1281 aRect.AdjustLeft( 1 );
1282 aRect.AdjustTop( 1 );
1283 aRect.AdjustRight( -1 );
1284 aRect.AdjustBottom( -1 );
1285 if (bDrawSel)
1286 rRenderContext.DrawRect(aRect);
1287 }
1288 aRect.AdjustLeft( 1 );
1289 aRect.AdjustTop( 1 );
1290 aRect.AdjustRight( -1 );
1291 aRect.AdjustBottom( -1 );
1292 tools::Rectangle aRect2 = aRect;
1293 aRect.AdjustLeft( 1 );
1294 aRect.AdjustTop( 1 );
1295 aRect.AdjustRight( -1 );
1296 aRect.AdjustBottom( -1 );
1297 if (bDrawSel)
1298 rRenderContext.DrawRect(aRect);
1299 if (mbDoubleSel)
1300 {
1301 aRect.AdjustLeft( 1 );
1302 aRect.AdjustTop( 1 );
1303 aRect.AdjustRight( -1 );
1304 aRect.AdjustBottom( -1 );
1305 if (bDrawSel)
1306 rRenderContext.DrawRect(aRect);
1307 }
1308
1309 if (bDrawSel)
1310 {
1311 rRenderContext.SetLineColor(mbBlackSel ? COL_WHITE : aSingleColor);
1312 }
1313 else
1314 {
1315 rRenderContext.SetLineColor(COL_LIGHTGRAY);
1316 }
1317 rRenderContext.DrawRect(aRect2);
1318 if (bFocus)
1319 DrawFocusRect(rRenderContext, aRect2);
1320 }
1321
1322 ImplDrawItemText(rRenderContext, pItem->maText);
1323}
1324
1325void ValueSet::ImplFormatItem(vcl::RenderContext const & rRenderContext, ValueSetItem* pItem, tools::Rectangle aRect)
1326{
1327 WinBits nStyle = GetStyle();
1328 if (nStyle & WB_ITEMBORDER(WinBits(0x00010000)))
1329 {
1330 aRect.AdjustLeft(1 );
1331 aRect.AdjustTop(1 );
1332 aRect.AdjustRight( -1 );
1333 aRect.AdjustBottom( -1 );
1334
1335 if (nStyle & WB_FLATVALUESET(WinBits(0x02000000)))
1336 {
1337 sal_Int32 nBorder = (nStyle & WB_DOUBLEBORDER(WinBits(0x00020000))) ? 2 : 1;
1338
1339 aRect.AdjustLeft(nBorder );
1340 aRect.AdjustTop(nBorder );
1341 aRect.AdjustRight( -nBorder );
1342 aRect.AdjustBottom( -nBorder );
1343 }
1344 else
1345 {
1346 DecorationView aView(maVirDev.get());
1347 aRect = aView.DrawFrame(aRect, mnFrameStyle);
1348 }
1349 }
1350
1351 if (pItem == mpNoneItem.get())
1352 pItem->maText = GetText();
1353
1354 if ((aRect.GetHeight() <= 0) || (aRect.GetWidth() <= 0))
1355 return;
1356
1357 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
1358
1359 if (pItem == mpNoneItem.get())
1360 {
1361 maVirDev->SetFont(rRenderContext.GetFont());
1362 maVirDev->SetTextColor((nStyle & WB_MENUSTYLEVALUESET(WinBits(0x08000000))) ? rStyleSettings.GetMenuTextColor() : rStyleSettings.GetWindowTextColor());
1363 maVirDev->SetTextFillColor();
1364 maVirDev->SetFillColor((nStyle & WB_MENUSTYLEVALUESET(WinBits(0x08000000))) ? rStyleSettings.GetMenuColor() : rStyleSettings.GetWindowColor());
1365 maVirDev->DrawRect(aRect);
1366 Point aTxtPos(aRect.Left() + 2, aRect.Top());
1367 long nTxtWidth = rRenderContext.GetTextWidth(pItem->maText);
1368 if ((aTxtPos.X() + nTxtWidth) > aRect.Right())
1369 {
1370 maVirDev->SetClipRegion(vcl::Region(aRect));
1371 maVirDev->DrawText(aTxtPos, pItem->maText);
1372 maVirDev->SetClipRegion();
1373 }
1374 else
1375 maVirDev->DrawText(aTxtPos, pItem->maText);
1376 }
1377 else if (pItem->meType == VALUESETITEM_COLOR)
1378 {
1379 maVirDev->SetFillColor(pItem->maColor);
1380 maVirDev->DrawRect(aRect);
1381 }
1382 else
1383 {
1384 if (IsColor())
1385 maVirDev->SetFillColor(maColor);
1386 else if (nStyle & WB_MENUSTYLEVALUESET(WinBits(0x08000000)))
1387 maVirDev->SetFillColor(rStyleSettings.GetMenuColor());
1388 else if (IsEnabled())
1389 maVirDev->SetFillColor(rStyleSettings.GetWindowColor());
1390 else
1391 maVirDev->SetFillColor(rStyleSettings.GetFaceColor());
1392 maVirDev->DrawRect(aRect);
1393
1394 if (pItem->meType == VALUESETITEM_USERDRAW)
1395 {
1396 UserDrawEvent aUDEvt(nullptr, maVirDev.get(), aRect, pItem->mnId);
1397 UserDraw(aUDEvt);
1398 }
1399 else
1400 {
1401 Size aImageSize = pItem->maImage.GetSizePixel();
1402 Size aRectSize = aRect.GetSize();
1403 Point aPos(aRect.Left(), aRect.Top());
1404 aPos.AdjustX((aRectSize.Width() - aImageSize.Width()) / 2 );
1405
1406 if (pItem->meType != VALUESETITEM_IMAGE_AND_TEXT)
1407 aPos.AdjustY((aRectSize.Height() - aImageSize.Height()) / 2 );
1408
1409 DrawImageFlags nImageStyle = DrawImageFlags::NONE;
1410 if (!IsEnabled())
1411 nImageStyle |= DrawImageFlags::Disable;
1412
1413 if (aImageSize.Width() > aRectSize.Width() ||
1414 aImageSize.Height() > aRectSize.Height())
1415 {
1416 maVirDev->SetClipRegion(vcl::Region(aRect));
1417 maVirDev->DrawImage(aPos, pItem->maImage, nImageStyle);
1418 maVirDev->SetClipRegion();
1419 }
1420 else
1421 maVirDev->DrawImage(aPos, pItem->maImage, nImageStyle);
1422
1423 if (pItem->meType == VALUESETITEM_IMAGE_AND_TEXT)
1424 {
1425 maVirDev->SetFont(rRenderContext.GetFont());
1426 maVirDev->SetTextColor((nStyle & WB_MENUSTYLEVALUESET(WinBits(0x08000000))) ? rStyleSettings.GetMenuTextColor() : rStyleSettings.GetWindowTextColor());
1427 maVirDev->SetTextFillColor();
1428
1429 long nTxtWidth = maVirDev->GetTextWidth(pItem->maText);
1430
1431 if (nTxtWidth > aRect.GetWidth())
1432 maVirDev->SetClipRegion(vcl::Region(aRect));
1433
1434 maVirDev->DrawText(Point(aRect.Left() +
1435 (aRect.GetWidth() - nTxtWidth) / 2,
1436 aRect.Bottom() - maVirDev->GetTextHeight()),
1437 pItem->maText);
1438
1439 if (nTxtWidth > aRect.GetWidth())
1440 maVirDev->SetClipRegion();
1441 }
1442 }
1443 }
1444
1445 const sal_uInt16 nEdgeBlendingPercent(GetEdgeBlending() ? rStyleSettings.GetEdgeBlending() : 0);
1446
1447 if (nEdgeBlendingPercent)
1448 {
1449 const Color& rTopLeft(rStyleSettings.GetEdgeBlendingTopLeftColor());
1450 const Color& rBottomRight(rStyleSettings.GetEdgeBlendingBottomRightColor());
1451 const sal_uInt8 nAlpha((nEdgeBlendingPercent * 255) / 100);
1452 const BitmapEx aBlendFrame(createBlendFrame(aRect.GetSize(), nAlpha, rTopLeft, rBottomRight));
1453
1454 if (!aBlendFrame.IsEmpty())
1455 {
1456 maVirDev->DrawBitmapEx(aRect.TopLeft(), aBlendFrame);
1457 }
1458 }
1459}
1460
1461void ValueSet::ImplDrawItemText(vcl::RenderContext& rRenderContext, const OUString& rText)
1462{
1463 if (!(GetStyle() & WB_NAMEFIELD(WinBits(0x00040000))))
1464 return;
1465
1466 Size aWinSize(GetOutputSizePixel());
1467 long nTxtWidth = rRenderContext.GetTextWidth(rText);
1468 long nTxtOffset = mnTextOffset;
1469
1470 // delete rectangle and show text
1471 if (GetStyle() & WB_FLATVALUESET(WinBits(0x02000000)))
1472 {
1473 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
1474 rRenderContext.SetLineColor();
1475 rRenderContext.SetFillColor(rStyleSettings.GetFaceColor());
1476 rRenderContext.DrawRect(tools::Rectangle(Point(0, nTxtOffset), Point(aWinSize.Width(), aWinSize.Height())));
1477 rRenderContext.SetTextColor(rStyleSettings.GetButtonTextColor());
1478 }
1479 else
1480 {
1481 nTxtOffset += NAME_LINE_HEIGHT+NAME_LINE_OFF_Y;
1482 rRenderContext.SetBackground(Application::GetSettings().GetStyleSettings().GetFaceColor());
1483 rRenderContext.Erase(tools::Rectangle(Point(0, nTxtOffset), Point(aWinSize.Width(), aWinSize.Height())));
1484 }
1485 rRenderContext.DrawText(Point((aWinSize.Width() - nTxtWidth) / 2, nTxtOffset + (NAME_OFFSET / 2)), rText);
1486}
1487
1488void ValueSet::StyleUpdated()
1489{
1490 mbFormat = true;
1491 CustomWidgetController::StyleUpdated();
1492}
1493
1494void ValueSet::EnableFullItemMode( bool bFullMode )
1495{
1496 mbFullMode = bFullMode;
1497}
1498
1499void ValueSet::SetColCount( sal_uInt16 nNewCols )
1500{
1501 if ( mnUserCols != nNewCols )
1502 {
1503 mnUserCols = nNewCols;
1504 QueueReformat();
1505 }
1506}
1507
1508void ValueSet::SetItemImage( sal_uInt16 nItemId, const Image& rImage )
1509{
1510 size_t nPos = GetItemPos( nItemId );
1511
1512 if ( nPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1513 return;
1514
1515 ValueSetItem* pItem = mItemList[nPos].get();
1516 pItem->meType = VALUESETITEM_IMAGE;
1517 pItem->maImage = rImage;
1518
1519 if ( !mbFormat && IsReallyVisible() && IsUpdateMode() )
1520 {
1521 const tools::Rectangle aRect = ImplGetItemRect(nPos);
1522 Invalidate(aRect);
1523 }
1524 else
1525 mbFormat = true;
1526}
1527
1528void ValueSet::SetItemColor( sal_uInt16 nItemId, const Color& rColor )
1529{
1530 size_t nPos = GetItemPos( nItemId );
1531
1532 if ( nPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1533 return;
1534
1535 ValueSetItem* pItem = mItemList[nPos].get();
1536 pItem->meType = VALUESETITEM_COLOR;
1537 pItem->maColor = rColor;
1538
1539 if ( !mbFormat && IsReallyVisible() && IsUpdateMode() )
1540 {
1541 const tools::Rectangle aRect = ImplGetItemRect(nPos);
1542 Invalidate( aRect );
1543 }
1544 else
1545 mbFormat = true;
1546}
1547
1548Color ValueSet::GetItemColor( sal_uInt16 nItemId ) const
1549{
1550 size_t nPos = GetItemPos( nItemId );
1551
1552 if ( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1553 return mItemList[nPos]->maColor;
1554 else
1555 return Color();
1556}
1557
1558Size ValueSet::CalcWindowSizePixel( const Size& rItemSize, sal_uInt16 nDesireCols,
1559 sal_uInt16 nDesireLines ) const
1560{
1561 size_t nCalcCols = nDesireCols;
1562 size_t nCalcLines = nDesireLines;
1563
1564 if ( !nCalcCols )
1565 {
1566 if ( mnUserCols )
1567 nCalcCols = mnUserCols;
1568 else
1569 nCalcCols = 1;
1570 }
1571
1572 if ( !nCalcLines )
1573 {
1574 nCalcLines = mnVisLines;
1575
1576 if ( mbFormat )
1577 {
1578 if ( mnUserVisLines )
1579 nCalcLines = mnUserVisLines;
1580 else
1581 {
1582 // Floor( (M+N-1)/N )==Ceiling( M/N )
1583 nCalcLines = (mItemList.size()+nCalcCols-1) / nCalcCols;
1584 if ( !nCalcLines )
1585 nCalcLines = 1;
1586 }
1587 }
1588 }
1589
1590 Size aSize( rItemSize.Width() * nCalcCols, rItemSize.Height() * nCalcLines );
1591 WinBits nStyle = GetStyle();
1592 long nTxtHeight = GetTextHeight();
1593 long n;
1594
1595 if ( nStyle & WB_ITEMBORDER(WinBits(0x00010000)) )
1596 {
1597 if ( nStyle & WB_DOUBLEBORDER(WinBits(0x00020000)) )
1598 n = ITEM_OFFSET_DOUBLE;
1599 else
1600 n = ITEM_OFFSET;
1601
1602 aSize.AdjustWidth(n * nCalcCols );
1603 aSize.AdjustHeight(n * nCalcLines );
1604 }
1605 else
1606 n = 0;
1607
1608 if ( mnSpacing )
1609 {
1610 aSize.AdjustWidth(mnSpacing * (nCalcCols - 1) );
1611 aSize.AdjustHeight(mnSpacing * (nCalcLines - 1) );
1612 }
1613
1614 if ( nStyle & WB_NAMEFIELD(WinBits(0x00040000)) )
1615 {
1616 aSize.AdjustHeight(nTxtHeight + NAME_OFFSET );
1617 if ( !(nStyle & WB_FLATVALUESET(WinBits(0x02000000))) )
1618 aSize.AdjustHeight(NAME_LINE_HEIGHT + NAME_LINE_OFF_Y );
1619 }
1620
1621 if ( nStyle & WB_NONEFIELD(WinBits(0x00080000)) )
1622 {
1623 aSize.AdjustHeight(nTxtHeight + n + mnSpacing );
1624 }
1625
1626 return aSize;
1627}
1628
1629void ValueSet::InsertItem( sal_uInt16 nItemId, const Image& rImage )
1630{
1631 std::unique_ptr<ValueSetItem> pItem(new ValueSetItem( *this ));
1632 pItem->mnId = nItemId;
1633 pItem->meType = VALUESETITEM_IMAGE;
1634 pItem->maImage = rImage;
1635 ImplInsertItem( std::move(pItem), VALUESET_APPEND(size_t(-1)) );
1636}
1637
1638void ValueSet::InsertItem( sal_uInt16 nItemId, const Image& rImage,
1639 const OUString& rText, size_t nPos,
1640 bool bShowLegend )
1641{
1642 std::unique_ptr<ValueSetItem> pItem(new ValueSetItem( *this ));
1643 pItem->mnId = nItemId;
1644 pItem->meType = bShowLegend ? VALUESETITEM_IMAGE_AND_TEXT : VALUESETITEM_IMAGE;
1645 pItem->maImage = rImage;
1646 pItem->maText = rText;
1647 ImplInsertItem( std::move(pItem), nPos );
1648}
1649
1650void ValueSet::InsertItem( sal_uInt16 nItemId, size_t nPos )
1651{
1652 std::unique_ptr<ValueSetItem> pItem(new ValueSetItem( *this ));
1653 pItem->mnId = nItemId;
1654 pItem->meType = VALUESETITEM_USERDRAW;
1655 ImplInsertItem( std::move(pItem), nPos );
1656}
1657
1658void ValueSet::InsertItem( sal_uInt16 nItemId, const Color& rColor,
1659 const OUString& rText )
1660{
1661 std::unique_ptr<ValueSetItem> pItem(new ValueSetItem( *this ));
1662 pItem->mnId = nItemId;
1663 pItem->meType = VALUESETITEM_COLOR;
1664 pItem->maColor = rColor;
1665 pItem->maText = rText;
1666 ImplInsertItem( std::move(pItem), VALUESET_APPEND(size_t(-1)) );
1667}
1668
1669void ValueSet::ImplInsertItem( std::unique_ptr<ValueSetItem> pItem, const size_t nPos )
1670{
1671 DBG_ASSERT( pItem->mnId, "ValueSet::InsertItem(): ItemId == 0" )do { if (true && (!(pItem->mnId))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1671" ": "), "%s", "ValueSet::InsertItem(): ItemId == 0"
); } } while (false)
;
1672 DBG_ASSERT( GetItemPos( pItem->mnId ) == VALUESET_ITEM_NOTFOUND,do { if (true && (!(GetItemPos( pItem->mnId ) == (
size_t(-1))))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1673" ": "), "%s", "ValueSet::InsertItem(): ItemId already exists"
); } } while (false)
1673 "ValueSet::InsertItem(): ItemId already exists" )do { if (true && (!(GetItemPos( pItem->mnId ) == (
size_t(-1))))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1673" ": "), "%s", "ValueSet::InsertItem(): ItemId already exists"
); } } while (false)
;
1674
1675 if ( nPos < mItemList.size() ) {
1676 mItemList.insert( mItemList.begin() + nPos, std::move(pItem) );
1677 } else {
1678 mItemList.push_back( std::move(pItem) );
1679 }
1680
1681 QueueReformat();
1682}
1683
1684int ValueSet::GetScrollWidth() const
1685{
1686 if (mxScrolledWindow)
1687 return mxScrolledWindow->get_vscroll_width();
1688 return 0;
1689}
1690
1691void ValueSet::SetEdgeBlending(bool bNew)
1692{
1693 if(mbEdgeBlending != bNew)
1694 {
1695 mbEdgeBlending = bNew;
1696 mbFormat = true;
1697
1698 if (GetDrawingArea() && IsReallyVisible() && IsUpdateMode())
1699 {
1700 Invalidate();
1701 }
1702 }
1703}
1704
1705Size ValueSet::CalcItemSizePixel( const Size& rItemSize) const
1706{
1707 Size aSize = rItemSize;
1708
1709 WinBits nStyle = GetStyle();
1710 if ( nStyle & WB_ITEMBORDER(WinBits(0x00010000)) )
1711 {
1712 long n;
1713
1714 if ( nStyle & WB_DOUBLEBORDER(WinBits(0x00020000)) )
1715 n = ITEM_OFFSET_DOUBLE;
1716 else
1717 n = ITEM_OFFSET;
1718
1719 aSize.AdjustWidth(n );
1720 aSize.AdjustHeight(n );
1721 }
1722
1723 return aSize;
1724}
1725
1726void ValueSet::SetLineCount( sal_uInt16 nNewLines )
1727{
1728 if ( mnUserVisLines != nNewLines )
1729 {
1730 mnUserVisLines = nNewLines;
1731 QueueReformat();
1732 }
1733}
1734
1735void ValueSet::SetItemWidth( long nNewItemWidth )
1736{
1737 if ( mnUserItemWidth != nNewItemWidth )
1738 {
1739 mnUserItemWidth = nNewItemWidth;
1740 QueueReformat();
1741 }
1742}
1743
1744//method to set accessible when the style is user draw.
1745void ValueSet::InsertItem( sal_uInt16 nItemId, const OUString& rText, size_t nPos )
1746{
1747 DBG_ASSERT( nItemId, "ValueSet::InsertItem(): ItemId == 0" )do { if (true && (!(nItemId))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1747" ": "), "%s", "ValueSet::InsertItem(): ItemId == 0"
); } } while (false)
;
1748 DBG_ASSERT( GetItemPos( nItemId ) == VALUESET_ITEM_NOTFOUND,do { if (true && (!(GetItemPos( nItemId ) == (size_t(
-1))))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), (
"legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1749" ": "), "%s", "ValueSet::InsertItem(): ItemId already exists"
); } } while (false)
1749 "ValueSet::InsertItem(): ItemId already exists" )do { if (true && (!(GetItemPos( nItemId ) == (size_t(
-1))))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), (
"legacy.tools"), ("/home/maarten/src/libreoffice/core/svtools/source/control/valueset.cxx"
":" "1749" ": "), "%s", "ValueSet::InsertItem(): ItemId already exists"
); } } while (false)
;
1750 std::unique_ptr<ValueSetItem> pItem(new ValueSetItem( *this ));
1751 pItem->mnId = nItemId;
1752 pItem->meType = VALUESETITEM_USERDRAW;
1753 pItem->maText = rText;
1754 ImplInsertItem( std::move(pItem), nPos );
1755}
1756
1757void ValueSet::SetItemHeight( long nNewItemHeight )
1758{
1759 if ( mnUserItemHeight != nNewItemHeight )
1760 {
1761 mnUserItemHeight = nNewItemHeight;
1762 QueueReformat();
1763 }
1764}
1765
1766OUString ValueSet::RequestHelp(tools::Rectangle& rHelpRect)
1767{
1768 Point aPos = rHelpRect.TopLeft();
1769 const size_t nItemPos = ImplGetItem( aPos );
1770 OUString sRet;
1771 if (nItemPos != VALUESET_ITEM_NOTFOUND(size_t(-1)))
1772 {
1773 rHelpRect = ImplGetItemRect(nItemPos);
1774 sRet = GetItemText(ImplGetItem(nItemPos)->mnId);
1775 }
1776 return sRet;
1777}
1778
1779OUString ValueSet::GetItemText(sal_uInt16 nItemId) const
1780{
1781 const size_t nPos = GetItemPos(nItemId);
1782
1783 if ( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1784 return mItemList[nPos]->maText;
1785
1786 return OUString();
1787}
1788
1789void ValueSet::SetExtraSpacing( sal_uInt16 nNewSpacing )
1790{
1791 if ( GetStyle() & WB_ITEMBORDER(WinBits(0x00010000)) )
1792 {
1793 mnSpacing = nNewSpacing;
1794 QueueReformat();
1795 }
1796}
1797
1798void ValueSet::SetFormat()
1799{
1800 mbFormat = true;
1801}
1802
1803void ValueSet::SetItemData( sal_uInt16 nItemId, void* pData )
1804{
1805 size_t nPos = GetItemPos( nItemId );
1806
1807 if ( nPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1808 return;
1809
1810 ValueSetItem* pItem = mItemList[nPos].get();
1811 pItem->mpData = pData;
1812
1813 if ( pItem->meType == VALUESETITEM_USERDRAW )
1814 {
1815 if ( !mbFormat && IsReallyVisible() && IsUpdateMode() )
1816 {
1817 const tools::Rectangle aRect = ImplGetItemRect(nPos);
1818 Invalidate(aRect);
1819 }
1820 else
1821 mbFormat = true;
1822 }
1823}
1824
1825void* ValueSet::GetItemData( sal_uInt16 nItemId ) const
1826{
1827 size_t nPos = GetItemPos( nItemId );
1828
1829 if ( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1830 return mItemList[nPos]->mpData;
1831 else
1832 return nullptr;
1833}
1834
1835void ValueSet::SetItemText(sal_uInt16 nItemId, const OUString& rText)
1836{
1837 size_t nPos = GetItemPos( nItemId );
1838
1839 if ( nPos == VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1840 return;
1841
1842 ValueSetItem* pItem = mItemList[nPos].get();
1843
1844 // Remember old and new name for accessibility event.
1845 Any aOldName;
1846 Any aNewName;
1847 OUString sString (pItem->maText);
1848 aOldName <<= sString;
1849 sString = rText;
1850 aNewName <<= sString;
1851
1852 pItem->maText = rText;
1853
1854 if (!mbFormat && IsReallyVisible() && IsUpdateMode())
1855 {
1856 sal_uInt16 nTempId = mnSelItemId;
1857
1858 if (mbHighlight)
1859 nTempId = mnHighItemId;
1860
1861 if (nTempId == nItemId)
1862 Invalidate();
1863 }
1864
1865 if (ImplHasAccessibleListeners())
1866 {
1867 Reference<XAccessible> xAccessible(pItem->GetAccessible( false/*bIsTransientChildrenDisabled*/));
1868 ValueItemAcc* pValueItemAcc = static_cast<ValueItemAcc*>(xAccessible.get());
1869 pValueItemAcc->FireAccessibleEvent(AccessibleEventId::NAME_CHANGED, aOldName, aNewName);
1870 }
1871}
1872
1873Size ValueSet::GetLargestItemSize()
1874{
1875 Size aLargestItem;
1876
1877 for (const std::unique_ptr<ValueSetItem>& pItem : mItemList)
1878 {
1879 if (!pItem->mbVisible)
1880 continue;
1881
1882 if (pItem->meType != VALUESETITEM_IMAGE &&
1883 pItem->meType != VALUESETITEM_IMAGE_AND_TEXT)
1884 {
1885 // handle determining an optimal size for this case
1886 continue;
1887 }
1888
1889 Size aSize = pItem->maImage.GetSizePixel();
1890 if (pItem->meType == VALUESETITEM_IMAGE_AND_TEXT)
1891 {
1892 aSize.AdjustHeight(3 * NAME_LINE_HEIGHT +
1893 maVirDev->GetTextHeight() );
1894 aSize.setWidth( std::max(aSize.Width(),
1895 maVirDev->GetTextWidth(pItem->maText) + NAME_OFFSET) );
1896 }
1897
1898 aLargestItem.setWidth( std::max(aLargestItem.Width(), aSize.Width()) );
1899 aLargestItem.setHeight( std::max(aLargestItem.Height(), aSize.Height()) );
1900 }
1901
1902 return aLargestItem;
1903}
1904
1905void ValueSet::SetOptimalSize()
1906{
1907 Size aLargestSize(GetLargestItemSize());
1908 aLargestSize.setWidth(std::max(aLargestSize.Width(), mnUserItemWidth));
1909 aLargestSize.setHeight(std::max(aLargestSize.Height(), mnUserItemHeight));
1910 Size aPrefSize(CalcWindowSizePixel(aLargestSize));
1911 GetDrawingArea()->set_size_request(aPrefSize.Width(), aPrefSize.Height());
1912}
1913
1914Image ValueSet::GetItemImage(sal_uInt16 nItemId) const
1915{
1916 size_t nPos = GetItemPos( nItemId );
1917
1918 if ( nPos != VALUESET_ITEM_NOTFOUND(size_t(-1)) )
1919 return mItemList[nPos]->maImage;
1920 else
1921 return Image();
1922}
1923
1924void ValueSet::SetColor(const Color& rColor)
1925{
1926 maColor = rColor;
1927 mbFormat = true;
1928 if (IsReallyVisible() && IsUpdateMode())
1929 Invalidate();
1930}
1931
1932void ValueSet::Show()
1933{
1934 if (mxScrolledWindow)
1935 mxScrolledWindow->show();
1936 CustomWidgetController::Show();
1937}
1938
1939void ValueSet::Hide()
1940{
1941 CustomWidgetController::Hide();
1942 if (mxScrolledWindow)
1943 mxScrolledWindow->hide();
1944}
1945
1946FactoryFunction ValueSet::GetUITestFactory() const
1947{
1948 return ValueSetUIObject::create;
1949}
1950
1951/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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#ifndef INCLUDED_VCL_PTR_HXX
21#define INCLUDED_VCL_PTR_HXX
22
23#include <sal/config.h>
24
25#include <rtl/ref.hxx>
26
27#include <utility>
28#include <type_traits>
29
30#ifdef DBG_UTIL
31#ifndef _WIN32
32#include <vcl/vclmain.hxx>
33#endif
34#endif
35
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase(
44 int (*)[sizeof(T)])
45{ return std::is_base_of<VclReferenceBase, T>::value; }
46
47} // namespace vcl::detail
48
49/**
50 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
51 *
52 * For more details on the design please see vcl/README.lifecycle
53 *
54 * @param reference_type must be a subclass of vcl::Window
55 */
56template <class reference_type>
57class VclPtr
58{
59 static_assert(
60 vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>(
61 nullptr),
62 "template argument type must be derived from VclReferenceBase");
63
64 ::rtl::Reference<reference_type> m_rInnerRef;
65
66public:
67 /** Constructor...
68 */
69 VclPtr()
70 : m_rInnerRef()
71 {}
72
73 /** Constructor...
74 */
75 VclPtr (reference_type * pBody)
76 : m_rInnerRef(pBody)
77 {}
78
79 /** Constructor... that doesn't take a ref.
80 */
81 VclPtr (reference_type * pBody, __sal_NoAcquire)
82 : m_rInnerRef(pBody, SAL_NO_ACQUIRE)
83 {}
84
85 /** Up-casting conversion constructor: Copies interface reference.
86
87 Does not work for up-casts to ambiguous bases. For the special case of
88 up-casting to Reference< XInterface >, see the corresponding conversion
89 operator.
90
91 @param rRef another reference
92 */
93 template< class derived_type >
94 VclPtr(
95 const VclPtr< derived_type > & rRef,
96 typename std::enable_if<
97 std::is_base_of<reference_type, derived_type>::value, int>::type
98 = 0 )
99 : m_rInnerRef( static_cast<reference_type*>(rRef) )
100 {
101 }
102
103#if defined(DBG_UTIL) && !defined(_WIN32)
104 virtual ~VclPtr()
105 {
106 assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain
::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 106, __extension__ __PRETTY_FUNCTION__))
;
107 // We can be one of the intermediate counts, but if we are the last
108 // VclPtr keeping this object alive, then something forgot to call dispose().
109 assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
110 && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
;
111 }
112 VclPtr(VclPtr const &) = default;
113 VclPtr(VclPtr &&) = default;
114 VclPtr & operator =(VclPtr const &) = default;
115 VclPtr & operator =(VclPtr &&) = default;
116#endif
117
118 /**
119 * A construction helper for VclPtr. Since VclPtr types are created
120 * with a reference-count of one - to help fit into the existing
121 * code-flow; this helps us to construct them easily.
122 *
123 * For more details on the design please see vcl/README.lifecycle
124 *
125 * @tparam reference_type must be a subclass of vcl::Window
126 */
127 template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg)
128 {
129 return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE );
130 }
131
132 /** Probably most common used: handle->someBodyOp().
133 */
134 reference_type * operator->() const
135 {
136 return m_rInnerRef.get();
137 }
138
139 /** Get the body. Can be used instead of operator->().
140 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
141 are the same.
142 */
143 reference_type * get() const
144 {
145 return m_rInnerRef.get();
146 }
147
148 void set(reference_type *pBody)
149 {
150 m_rInnerRef.set(pBody);
151 }
152
153 void reset(reference_type *pBody)
154 {
155 m_rInnerRef.set(pBody);
156 }
157
158 /** Up-casting copy assignment operator.
159
160 Does not work for up-casts to ambiguous bases.
161
162 @param rRef another reference
163 */
164 template<typename derived_type>
165 typename std::enable_if<
166 std::is_base_of<reference_type, derived_type>::value,
167 VclPtr &>::type
168 operator =(VclPtr<derived_type> const & rRef)
169 {
170 m_rInnerRef.set(rRef.get());
171 return *this;
172 }
173
174 VclPtr & operator =(reference_type * pBody)
175 {
176 m_rInnerRef.set(pBody);
177 return *this;
178 }
179
180 operator reference_type * () const
181 {
182 return m_rInnerRef.get();
183 }
184
185 explicit operator bool () const
186 {
187 return m_rInnerRef.get() != nullptr;
188 }
189
190 void clear()
191 {
192 m_rInnerRef.clear();
193 }
194
195 void reset()
196 {
197 m_rInnerRef.clear();
198 }
199
200 void disposeAndClear()
201 {
202 // hold it alive for the lifetime of this method
203 ::rtl::Reference<reference_type> aTmp(m_rInnerRef);
4
Calling copy constructor for 'Reference<VirtualDevice>'
7
Returning from copy constructor for 'Reference<VirtualDevice>'
204 m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-)
8
Calling 'Reference::clear'
15
Returning; memory was released
205 if (aTmp.get()) {
16
Calling 'Reference::get'
206 aTmp->disposeOnce();
207 }
208 }
209
210 /** Needed to place VclPtr's into STL collection.
211 */
212 bool operator< (const VclPtr<reference_type> & handle) const
213 {
214 return (m_rInnerRef < handle.m_rInnerRef);
215 }
216}; // class VclPtr
217
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) {
261 return !(p1 == p2);
262}
263
264/**
265 * A construction helper for a temporary VclPtr. Since VclPtr types
266 * are created with a reference-count of one - to help fit into
267 * the existing code-flow; this helps us to construct them easily.
268 * see also VclPtr::Create and ScopedVclPtr
269 *
270 * For more details on the design please see vcl/README.lifecycle
271 *
272 * @param reference_type must be a subclass of vcl::Window
273 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
278 template<typename... Arg> VclPtrInstance(Arg &&... arg)
279 : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
280 {
281 }
282
283 /**
284 * Override and disallow this, to prevent people accidentally calling it and actually
285 * getting VclPtr::Create and getting a naked VclPtr<> instance
286 */
287 template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete;
288};
289
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
294 /** Constructor...
295 */
296 ScopedVclPtr()
297 : VclPtr<reference_type>()
298 {}
299
300 /** Constructor
301 */
302 ScopedVclPtr (reference_type * pBody)
303 : VclPtr<reference_type>(pBody)
304 {}
305
306 /** Copy constructor...
307 */
308 ScopedVclPtr (const VclPtr<reference_type> & handle)
309 : VclPtr<reference_type>(handle)
310 {}
311
312 /**
313 Assignment that releases the last reference.
314 */
315 void disposeAndReset(reference_type *pBody)
316 {
317 if (pBody != this->get()) {
318 VclPtr<reference_type>::disposeAndClear();
319 VclPtr<reference_type>::set(pBody);
320 }
321 }
322
323 /**
324 Assignment that releases the last reference.
325 */
326 ScopedVclPtr<reference_type>& operator = (reference_type * pBody)
327 {
328 disposeAndReset(pBody);
329 return *this;
330 }
331
332 /** Up-casting conversion constructor: Copies interface reference.
333
334 Does not work for up-casts to ambiguous bases. For the special case of
335 up-casting to Reference< XInterface >, see the corresponding conversion
336 operator.
337
338 @param rRef another reference
339 */
340 template< class derived_type >
341 ScopedVclPtr(
342 const VclPtr< derived_type > & rRef,
343 typename std::enable_if<
344 std::is_base_of<reference_type, derived_type>::value, int>::type
345 = 0 )
346 : VclPtr<reference_type>( rRef )
347 {
348 }
349
350 /** Up-casting assignment operator.
351
352 Does not work for up-casts to ambiguous bases.
353
354 @param rRef another VclPtr
355 */
356 template<typename derived_type>
357 typename std::enable_if<
358 std::is_base_of<reference_type, derived_type>::value,
359 ScopedVclPtr &>::type
360 operator =(VclPtr<derived_type> const & rRef)
361 {
362 disposeAndReset(rRef.get());
363 return *this;
364 }
365
366 /**
367 * Override and disallow this, to prevent people accidentally calling it and actually
368 * getting VclPtr::Create and getting a naked VclPtr<> instance
369 */
370 template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete;
371
372 ~ScopedVclPtr()
373 {
374 VclPtr<reference_type>::disposeAndClear();
3
Calling 'VclPtr::disposeAndClear'
375 assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get(
) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 375, __extension__ __PRETTY_FUNCTION__))
; // make sure there are no lingering references
376 }
377
378private:
379 // Most likely we don't want this default copy-constructor.
380 ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete;
381 // And certainly we don't want a default assignment operator.
382 ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete;
383 // And disallow reset as that doesn't call disposeAndClear on the original reference
384 void reset() = delete;
385 void reset(reference_type *pBody) = delete;
386
387protected:
388 ScopedVclPtr (reference_type * pBody, __sal_NoAcquire)
389 : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE)
390 {}
391};
392
393/**
394 * A construction helper for ScopedVclPtr. Since VclPtr types are created
395 * with a reference-count of one - to help fit into the existing
396 * code-flow; this helps us to construct them easily.
397 *
398 * For more details on the design please see vcl/README.lifecycle
399 *
400 * @param reference_type must be a subclass of vcl::Window
401 */
402#if defined _MSC_VER
403#pragma warning(push)
404#pragma warning(disable: 4521) // " multiple copy constructors specified"
405#endif
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
410 template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg)
411 : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
412 {
413 }
414
415 /**
416 * Override and disallow this, to prevent people accidentally calling it and actually
417 * getting VclPtr::Create and getting a naked VclPtr<> instance
418 */
419 template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete;
420
421private:
422 // Prevent the above perfect forwarding ctor from hijacking (accidental)
423 // attempts at ScopedVclPtrInstance copy construction (where the hijacking
424 // would typically lead to somewhat obscure error messages); both non-const
425 // and const variants are needed here, as the ScopedVclPtr base class has a
426 // const--variant copy ctor, so the implicitly declared copy ctor for
427 // ScopedVclPtrInstance would also be the const variant, so non-const copy
428 // construction attempts would be hijacked by the perfect forwarding ctor;
429 // but if we only declared a non-const variant here, the const variant would
430 // no longer be implicitly declared (as there would already be an explicitly
431 // declared copy ctor), so const copy construction attempts would then be
432 // hijacked by the perfect forwarding ctor:
433 ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete;
434 ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete;
435};
436#if defined _MSC_VER
437#pragma warning(pop)
438#endif
439
440#endif // INCLUDED_VCL_PTR_HXX
441
442/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
5
Assuming field 'm_pBody' is non-null
6
Taking true branch
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
113 m_pBody->release();
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody
8.1
Field 'm_pBody' is non-null
8.1
Field 'm_pBody' is non-null
8.1
Field 'm_pBody' is non-null
8.1
Field 'm_pBody' is non-null
)
9
Taking true branch
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
10
Calling 'VclReferenceBase::release'
14
Returning; memory was released
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
17
Use of memory after it is freed
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
11
Assuming the condition is true
12
Taking true branch
40 delete this;
13
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif