Files
libreoffice/vcl/source/control/listctrl.cxx

171 lines
4.5 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <vcl/commandevent.hxx>
#include <vcl/listctrl.hxx>
ListControl::ListControl(vcl::Window* pParent, WinBits nBits):
Control(pParent, nBits),
mbHasScrollBar(false),
mpScrollBar(VclPtr<ScrollBar>::Create(this, WB_VERT))
{
mpScrollBar->SetScrollHdl( LINK( this, ListControl, ScrollHdl ) );
mpScrollBar->EnableDrag();
}
ListControl::~ListControl()
{
disposeOnce();
}
void ListControl::dispose()
{
mpScrollBar.disposeAndClear();
for (auto& aEntry : maEntries)
aEntry.disposeAndClear();
maEntries.clear();
Control::dispose();
}
void ListControl::RecalcAll()
{
// avoid recalculating while we are disposing
// children. This just leads to complex invalid memory
// access patterns that are not fixable.
Related tdf#113652: fix crash when closing Data provider Test also mpScrollBar to know if should keep on with RecalcAll bt: 5 0x00007fffec06b6f0 in vcl::Window::Hide() (this=0x0) at /home/julien/lo/libreoffice/include/vcl/window.hxx:948 6 0x00007fffec2eb344 in ListControl::RecalcAll() (this=0x555558b25090) at /home/julien/lo/libreoffice/vcl/source/control/listctrl.cxx:76 7 0x00007fffec2eb6ec in ListControl::queue_resize(StateChangedType) (this=0x555558b25090, eReason=StateChangedType::Layout) at /home/julien/lo/libreoffice/vcl/source/control/listctrl.cxx:110 8 0x00007fffec24c6b0 in vcl::(anonymous namespace)::queue_ungrouped_resize(vcl::Window const*) (pOrigWindow=0x555558b06620) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1343 9 0x00007fffec24c799 in vcl::Window::queue_resize(StateChangedType) (this=0x555558b06620, eReason=StateChangedType::Layout) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1362 10 0x00007fffec14a701 in VclContainer::queue_resize(StateChangedType) (this=0x555558b06620, eReason=StateChangedType::Layout) at /home/julien/lo/libreoffice/vcl/source/window/layout.cxx:179 11 0x00007fffec24c6b0 in vcl::(anonymous namespace)::queue_ungrouped_resize(vcl::Window const*) (pOrigWindow=0x555558b287b0) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1343 12 0x00007fffec24c799 in vcl::Window::queue_resize(StateChangedType) (this=0x555558b287b0, eReason=StateChangedType::Layout) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1362 13 0x00007fffec14a701 in VclContainer::queue_resize(StateChangedType) (this=0x555558b287b0, eReason=StateChangedType::Layout) at /home/julien/lo/libreoffice/vcl/source/window/layout.cxx:179 14 0x00007fffec24c6b0 in vcl::(anonymous namespace)::queue_ungrouped_resize(vcl::Window const*) (pOrigWindow=0x555558b26fd0) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1343 15 0x00007fffec24c799 in vcl::Window::queue_resize(StateChangedType) (this=0x555558b26fd0, eReason=StateChangedType::Visible) at /home/julien/lo/libreoffice/vcl/source/window/window2.cxx:1362 16 0x00007fffec25f2d5 in vcl::Window::StateChanged(StateChangedType) (this=0x555558b26fd0, eType=StateChangedType::Visible) at /home/julien/lo/libreoffice/vcl/source/window/window.cxx:1940 17 0x00007fffec2675ae in vcl::Window::CompatStateChanged(StateChangedType) (this=0x555558b26fd0, nStateChange=StateChangedType::Visible) at /home/julien/lo/libreoffice/vcl/source/window/window.cxx:3696 18 0x00007fffec260548 in vcl::Window::Show(bool, ShowFlags) (this=0x555558b26fd0, bVisible=false, nFlags=ShowFlags::NONE) at /home/julien/lo/libreoffice/vcl/source/window/window.cxx:2200 19 0x00007fffec06b6f0 in vcl::Window::Hide() (this=0x555558b26fd0) at /home/julien/lo/libreoffice/include/vcl/window.hxx:948 20 0x00007fffec256f11 in vcl::Window::dispose() (this=0x555558b26fd0) at /home/julien/lo/libreoffice/vcl/source/window/window.cxx:394 21 0x00007fffec2b1395 in Control::dispose() (this=0x555558b26fd0) at /home/julien/lo/libreoffice/vcl/source/control/ctrl.cxx:68 22 0x00007fffec289da8 in Button::dispose() (this=0x555558b26fd0) at /home/julien/lo/libreoffice/vcl/source/control/button.cxx:109 23 0x00007fffec3ef640 in VclReferenceBase::disposeOnce() (this=0x555558b27240) at /home/julien/lo/libreoffice/vcl/source/outdev/vclreferencebase.cxx:42 24 0x00007fffca047c8a in VclPtr<PushButton>::disposeAndClear() (this=0x555558b068c0) at /home/julien/lo/libreoffice/include/vcl/vclptr.hxx:208 25 0x00007fffca0432a6 in ScDataProviderBaseControl::dispose() (this=0x555558b06620) at /home/julien/lo/libreoffice/sc/source/ui/miscdlgs/dataproviderdlg.cxx:104 Change-Id: Ie1f2b156a5743354f39e95048e50bb0709095fd0 Reviewed-on: https://gerrit.libreoffice.org/44697 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Maxim Monastirsky <momonasmon@gmail.com> Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
2017-11-13 23:03:41 +01:00
if (isDisposed())
return;
sal_Int32 nTotalHeight = 0;
for (const auto& item : maEntries)
{
if (!item)
continue;
nTotalHeight += item->GetSizePixel().Height();
}
Size aCtrlSize = GetOutputSize();
long nSrcBarSize = GetSettings().GetStyleSettings().GetScrollBarSize();
if(nTotalHeight > GetSizePixel().Height())
{
mbHasScrollBar = true;
mpScrollBar->SetPosSizePixel(Point(aCtrlSize.Width() -nSrcBarSize, 0),
Size(nSrcBarSize, aCtrlSize.Height()) );
mpScrollBar->SetRangeMax(nTotalHeight);
mpScrollBar->SetVisibleSize(aCtrlSize.Height());
mpScrollBar->Show();
}
else
{
mbHasScrollBar = false;
mpScrollBar->Hide();
}
Point aPoint(0,-1*mpScrollBar->GetThumbPos());
for (const auto& item : maEntries)
{
if (!item)
continue;
item->SetPosPixel(aPoint);
Size aSize = item->GetSizePixel();
if(mbHasScrollBar)
aSize.setWidth( aCtrlSize.Width() - nSrcBarSize );
else
aSize.setWidth( aCtrlSize.Width() );
item->SetSizePixel(aSize);
aPoint.AdjustY(item->GetSizePixel().Height() );
}
}
Size ListControl::GetOptimalSize() const
{
return LogicToPixel(Size(300, 185), MapMode(MapUnit::MapAppFont));
}
void ListControl::Resize()
{
Control::Resize();
RecalcAll();
}
void ListControl::queue_resize(StateChangedType eReason)
{
Control::queue_resize(eReason);
RecalcAll();
}
void ListControl::DoScroll(long nDelta)
{
Point aNewPoint = mpScrollBar->GetPosPixel();
tools::Rectangle aRect(Point(), GetOutputSize());
aRect.AdjustRight( -(mpScrollBar->GetSizePixel().Width()) );
Scroll( 0, -nDelta, aRect );
mpScrollBar->SetPosPixel(aNewPoint);
}
IMPL_LINK_NOARG( ListControl, ScrollHdl, ScrollBar*, void )
{
DoScroll(mpScrollBar->GetDelta());
}
void ListControl::addEntry(const VclPtr<Window>& xEntry, sal_uInt32 nPos)
{
xEntry->Show();
if (nPos < maEntries.size())
{
maEntries.insert(maEntries.begin() + nPos, xEntry);
}
else
{
maEntries.push_back(xEntry);
}
RecalcAll();
}
void ListControl::deleteEntry(sal_uInt32 nPos)
{
if (nPos >= maEntries.size())
return;
maEntries[nPos].disposeAndClear();
maEntries.erase(maEntries.begin() + nPos);
RecalcAll();
}
std::vector<VclPtr<vcl::Window>> const & ListControl::getEntries() const
{
return maEntries;
}
bool ListControl::EventNotify( NotifyEvent& rNEvt )
{
if (rNEvt.GetType() == MouseNotifyEvent::COMMAND)
{
const CommandEvent* pEvent = rNEvt.GetCommandEvent();
if (pEvent && pEvent->GetCommand() == CommandEventId::Wheel)
{
HandleScrollCommand(*pEvent, nullptr, mpScrollBar.get());
}
}
return true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */