LibreOffice Module vcl (master)  1
listctrl.cxx
Go to the documentation of this file.
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 <vcl/commandevent.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/listctrl.hxx>
23 #include <vcl/settings.hxx>
24 
26  Control(pParent, nBits),
27  mbHasScrollBar(false),
28  mpScrollBar(VclPtr<ScrollBar>::Create(this, WB_VERT))
29 {
30  mpScrollBar->SetScrollHdl( LINK( this, ListControl, ScrollHdl ) );
32 }
33 
35 {
36  disposeOnce();
37 }
38 
40 {
42  for (auto& aEntry : maEntries)
43  aEntry.disposeAndClear();
44  maEntries.clear();
45 
47 }
48 
50 {
51  // avoid recalculating while we are disposing
52  // children. This just leads to complex invalid memory
53  // access patterns that are not fixable.
54  if (isDisposed())
55  return;
56 
57  sal_Int32 nTotalHeight = 0;
58  for (const auto& item : maEntries)
59  {
60  if (!item)
61  continue;
62  nTotalHeight += item->GetSizePixel().Height();
63  }
64 
65  Size aCtrlSize = GetOutputSize();
66  long nSrcBarSize = GetSettings().GetStyleSettings().GetScrollBarSize();
67  if(nTotalHeight > GetSizePixel().Height())
68  {
69  mbHasScrollBar = true;
70  mpScrollBar->SetPosSizePixel(Point(aCtrlSize.Width() -nSrcBarSize, 0),
71  Size(nSrcBarSize, aCtrlSize.Height()) );
72  mpScrollBar->SetRangeMax(nTotalHeight);
73  mpScrollBar->SetVisibleSize(aCtrlSize.Height());
74  mpScrollBar->Show();
75  }
76  else
77  {
78  mbHasScrollBar = false;
79  mpScrollBar->Hide();
80  }
81 
82  Point aPoint(0,-1*mpScrollBar->GetThumbPos());
83  for (const auto& item : maEntries)
84  {
85  if (!item)
86  continue;
87  item->SetPosPixel(aPoint);
88  Size aSize = item->GetSizePixel();
89  if(mbHasScrollBar)
90  aSize.setWidth( aCtrlSize.Width() - nSrcBarSize );
91  else
92  aSize.setWidth( aCtrlSize.Width() );
93  item->SetSizePixel(aSize);
94 
95  aPoint.AdjustY(item->GetSizePixel().Height() );
96  }
97 }
98 
100 {
101  return LogicToPixel(Size(300, 185), MapMode(MapUnit::MapAppFont));
102 }
103 
105 {
106  Control::Resize();
107  RecalcAll();
108 }
109 
111 {
112  Control::queue_resize(eReason);
113  RecalcAll();
114 }
115 
116 void ListControl::DoScroll(long nDelta)
117 {
118  Point aNewPoint = mpScrollBar->GetPosPixel();
120  aRect.AdjustRight( -(mpScrollBar->GetSizePixel().Width()) );
121  Scroll( 0, -nDelta, aRect );
122  mpScrollBar->SetPosPixel(aNewPoint);
123 }
124 
125 IMPL_LINK_NOARG( ListControl, ScrollHdl, ScrollBar*, void )
126 {
127  DoScroll(mpScrollBar->GetDelta());
128 }
129 
130 void ListControl::addEntry(const VclPtr<Window>& xEntry, sal_uInt32 nPos)
131 {
132  xEntry->Show();
133  if (nPos < maEntries.size())
134  {
135  maEntries.insert(maEntries.begin() + nPos, xEntry);
136  }
137  else
138  {
139  maEntries.push_back(xEntry);
140  }
141  RecalcAll();
142 }
143 
144 void ListControl::deleteEntry(sal_uInt32 nPos)
145 {
146  if (nPos >= maEntries.size())
147  return;
148 
149  maEntries[nPos].disposeAndClear();
150  maEntries.erase(maEntries.begin() + nPos);
151  RecalcAll();
152 }
153 
154 std::vector<VclPtr<vcl::Window>> const & ListControl::getEntries() const
155 {
156  return maEntries;
157 }
158 
160 {
161  if (rNEvt.GetType() == MouseNotifyEvent::COMMAND)
162  {
163  const CommandEvent* pEvent = rNEvt.GetCommandEvent();
164  if (pEvent)
165  {
166  CommandEventId nCommand = pEvent->GetCommand();
167 
168  if (nCommand == CommandEventId::Wheel ||
169  nCommand == CommandEventId::Gesture)
170  {
171  HandleScrollCommand(*pEvent, nullptr, mpScrollBar.get());
172  }
173  }
174  }
175  return true;
176 }
177 
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Point GetPosPixel() const
Definition: window.cxx:2806
long Width() const
virtual void queue_resize(StateChangedType eReason=StateChangedType::Layout) override
Definition: listctrl.cxx:110
CommandEventId
void addEntry(const VclPtr< vcl::Window > &xEntry, sal_uInt32 nPos=std::numeric_limits< sal_uInt16 >::max())
Definition: listctrl.cxx:130
const CommandEvent * GetCommandEvent() const
Definition: event.hxx:318
A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for refer...
Definition: button.hxx:32
long Height() const
void disposeAndClear()
Definition: vclptr.hxx:200
const StyleSettings & GetStyleSettings() const
virtual bool EventNotify(NotifyEvent &rNEvt) override
Definition: listctrl.cxx:159
virtual Size GetSizePixel() const
Definition: window.cxx:2364
bool mbHasScrollBar
Definition: listctrl.hxx:35
long GetThumbPos() const
Definition: scrbar.hxx:122
std::vector< VclPtr< vcl::Window > > const & getEntries() const
Definition: listctrl.cxx:154
bool HandleScrollCommand(const CommandEvent &rCmd, ScrollBar *pHScrl, ScrollBar *pVScrl)
Definition: window2.cxx:609
virtual void Resize() override
Definition: listctrl.cxx:104
StateChangedType
Definition: window.hxx:309
sal_Int64 WinBits
WinBits const WB_VERT
void Hide()
Definition: window.hxx:930
virtual void queue_resize(StateChangedType eReason=StateChangedType::Layout)
Definition: window2.cxx:1308
void SetRangeMax(long nNewRange)
Definition: scrbar.cxx:1334
void SetVisibleSize(long nNewSize)
Definition: scrbar.cxx:1378
Size GetOutputSize() const
Definition: outdev.hxx:450
CommandEventId GetCommand() const
MouseNotifyEvent GetType() const
Definition: event.hxx:294
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: ctrl.cxx:59
const AllSettings & GetSettings() const
Definition: outdev.hxx:420
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: listctrl.cxx:39
std::vector< VclPtr< vcl::Window > > maEntries
Definition: listctrl.hxx:34
virtual void SetPosSizePixel(const Point &rNewPos, const Size &rNewSize)
Definition: window2.cxx:1262
Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:940
virtual void Scroll(long nHorzScroll, long nVertScroll, ScrollFlags nFlags=ScrollFlags::NONE)
Definition: window.cxx:2944
virtual ~ListControl() override
Definition: listctrl.cxx:34
virtual void Resize() override
Definition: ctrl.cxx:74
void DoScroll(long nDiff)
Definition: listctrl.cxx:116
long AdjustRight(long nHorzMoveDelta)
IMPL_LINK_NOARG(ListControl, ScrollHdl, ScrollBar *, void)
Definition: listctrl.cxx:125
Definition: ctrl.hxx:33
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
void deleteEntry(sal_uInt32 nPos)
Definition: listctrl.cxx:144
void SetScrollHdl(const Link< ScrollBar *, void > &rLink)
Definition: scrbar.hxx:133
void EnableDrag()
Definition: scrbar.hxx:113
ListControl(vcl::Window *pParent, WinBits nStyle)
Definition: listctrl.cxx:25
reference_type * get() const
Get the body.
Definition: vclptr.hxx:143
void RecalcAll()
Definition: listctrl.cxx:49
VclPtr< ScrollBar > mpScrollBar
Definition: listctrl.hxx:36
long GetScrollBarSize() const
void setWidth(long nWidth)
virtual void SetPosPixel(const Point &rNewPos)
Definition: window2.cxx:1251
bool isDisposed() const
virtual Size GetOptimalSize() const override
Definition: listctrl.cxx:99
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2150