LibreOffice Module vcl (master)  1
svlbitm.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/treelistbox.hxx>
21 #include <vcl/svlbitm.hxx>
22 #include <vcl/treelistentry.hxx>
23 #include <vcl/viewdataentry.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/button.hxx>
26 #include <vcl/decoview.hxx>
27 #include <vcl/salnativewidgets.hxx>
28 
30 {
35 
36  SvLBoxButtonData_Impl() : pEntry(nullptr), pBox(nullptr), bDefaultImages(false), bShowRadioButton(false) {}
37 };
38 
39 void SvLBoxButtonData::InitData( bool _bRadioBtn, const Control* pCtrl )
40 {
41  nWidth = nHeight = 0;
42 
43  aBmps.resize(int(SvBmp::HITRISTATE)+1);
44 
45  bDataOk = false;
46  pImpl->bDefaultImages = true;
47  pImpl->bShowRadioButton = _bRadioBtn;
48 
49  SetDefaultImages( pCtrl );
50 }
51 
52 SvLBoxButtonData::SvLBoxButtonData( const Control* pControlForSettings )
53  : pImpl( new SvLBoxButtonData_Impl )
54 {
55  InitData( false, pControlForSettings );
56 }
57 
58 SvLBoxButtonData::SvLBoxButtonData( const Control* pControlForSettings, bool _bRadioBtn )
59  : pImpl( new SvLBoxButtonData_Impl )
60 {
61  InitData( _bRadioBtn, pControlForSettings );
62 }
63 
65 {
66 }
67 
69 {
70  aLink.Call( this );
71 }
72 
74 {
75  SvBmp nIdx;
76  if (nItemState == SvItemStateFlags::UNCHECKED)
77  nIdx = SvBmp::UNCHECKED;
78  else if (nItemState == SvItemStateFlags::CHECKED)
79  nIdx = SvBmp::CHECKED;
80  else if (nItemState == SvItemStateFlags::TRISTATE)
81  nIdx = SvBmp::TRISTATE;
83  nIdx = SvBmp::HIUNCHECKED;
84  else if (nItemState == (SvItemStateFlags::CHECKED | SvItemStateFlags::HILIGHTED))
85  nIdx = SvBmp::HICHECKED;
87  nIdx = SvBmp::HITRISTATE;
88  else
89  nIdx = SvBmp::UNCHECKED;
90  return nIdx;
91 }
92 
94 {
95  Size aSize = aBmps[int(SvBmp::UNCHECKED)].GetSizePixel();
96  nWidth = aSize.Width();
97  nHeight = aSize.Height();
98  bDataOk = true;
99 }
100 
102 {
103  pImpl->pEntry = pActEntry;
104  pImpl->pBox = pActBox;
105 }
106 
108 {
109  nItemFlags &= SvItemStateFlags::UNCHECKED |
112  switch( nItemFlags )
113  {
117  return SvButtonState::Checked;
120  default:
122  }
123 }
124 
126 {
127  assert(pImpl && "-SvLBoxButtonData::GetActEntry(): don't use me that way!");
128  return pImpl->pEntry;
129 }
130 
132 {
133  assert(pImpl && "-SvLBoxButtonData::GetActBox(): don't use me that way!");
134  return pImpl->pBox;
135 }
136 
138 {
139  const AllSettings& rSettings = pCtrl? pCtrl->GetSettings() : Application::GetSettings();
140 
141  if ( pImpl->bShowRadioButton )
142  {
149  }
150  else
151  {
158  }
159 }
160 
162 {
163  return pImpl->bDefaultImages;
164 }
165 
167  return pImpl->bShowRadioButton;
168 }
169 
170 // ***************************************************************
171 // class SvLBoxString
172 // ***************************************************************
173 
174 
175 SvLBoxString::SvLBoxString(const OUString& rStr)
176  : mbEmphasized(false)
177  , maText(rStr)
178 {
179 }
180 
182  : mbEmphasized(false)
183 {
184 }
185 
187 {
188 }
189 
191 {
192  return SvLBoxItemType::String;
193 }
194 
196  const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
197  const SvViewDataEntry* /*pView*/, const SvTreeListEntry& rEntry)
198 {
199  Size aSize;
201  if (rDev.IsEntryMnemonicsEnabled())
202  nStyle |= DrawTextFlags::Mnemonic;
203  if (rDev.TextCenterAndClipEnabled())
204  {
206  aSize.setWidth( rDev.GetEntryWidth() );
207  }
208  else
209  aSize.setWidth(GetWidth(&rDev, &rEntry));
210  aSize.setHeight(GetHeight(&rDev, &rEntry));
211 
212  if (mbEmphasized)
213  {
214  rRenderContext.Push();
215  vcl::Font aFont(rRenderContext.GetFont());
216  aFont.SetWeight(WEIGHT_BOLD);
217  rRenderContext.SetFont(aFont);
218  }
219 
220  rRenderContext.DrawText(tools::Rectangle(rPos, aSize), maText, nStyle);
221 
222  if (mbEmphasized)
223  rRenderContext.Pop();
224 }
225 
226 std::unique_ptr<SvLBoxItem> SvLBoxString::Clone(SvLBoxItem const * pSource) const
227 {
228  std::unique_ptr<SvLBoxString> pNew(new SvLBoxString);
229  pNew->maText = static_cast<SvLBoxString const *>(pSource)->maText;
230  return std::unique_ptr<SvLBoxItem>(pNew.release());
231 }
232 
234  SvTreeListBox* pView, SvTreeListEntry* pEntry, SvViewDataItem* pViewData)
235 {
236  if( !pViewData )
237  pViewData = pView->GetViewDataItem( pEntry, this );
238 
239  if (mbEmphasized)
240  {
241  pView->Push();
242  vcl::Font aFont( pView->GetFont());
243  aFont.SetWeight(WEIGHT_BOLD);
244  pView->Control::SetFont( aFont );
245  }
246 
247  pViewData->mnWidth = -1; // calc on demand
248  pViewData->mnHeight = pView->GetTextHeight();
249 
250  if (mbEmphasized)
251  pView->Pop();
252 }
253 
254 int SvLBoxString::CalcWidth(const SvTreeListBox* pView) const
255 {
256  return pView->GetTextWidth(maText);
257 }
258 
259 // ***************************************************************
260 // class SvLBoxButton
261 // ***************************************************************
262 
263 
265  : isVis(true)
266  , pData(pBData)
267  , nItemFlags(SvItemStateFlags::NONE)
268 {
270 }
271 
273  : SvLBoxItem()
274  , isVis(false)
275  , pData(nullptr)
276  , nItemFlags(SvItemStateFlags::NONE)
277 {
279 }
280 
282 {
283 }
284 
286 {
287  return SvLBoxItemType::Button;
288 }
289 
291 {
292  if ( IsStateChecked() )
294  else
295  SetStateChecked();
296  pData->StoreButtonState(pEntry, this);
297  pData->CallLink();
298 }
299 
301  const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
302  const SvViewDataEntry* /*pView*/, const SvTreeListEntry& /*rEntry*/)
303 {
306 
307  //Native drawing
308  bool bNativeOK = false;
310  if ( rRenderContext.IsNativeControlSupported( eCtrlType, ControlPart::Entire) )
311  {
312  Size aSize(pData->Width(), pData->Height());
313  ImplAdjustBoxSize(aSize, eCtrlType, rRenderContext);
314  ImplControlValue aControlValue;
315  tools::Rectangle aCtrlRegion( rPos, aSize );
317 
318  //states ControlState::DEFAULT, ControlState::PRESSED and ControlState::ROLLOVER are not implemented
319  if (IsStateHilighted())
320  nState |= ControlState::FOCUSED;
321  if (nStyle != DrawImageFlags::Disable)
322  nState |= ControlState::ENABLED;
323  if (IsStateChecked())
324  aControlValue.setTristateVal(ButtonValue::On);
325  else if (IsStateUnchecked())
326  aControlValue.setTristateVal(ButtonValue::Off);
327  else if (IsStateTristate())
328  aControlValue.setTristateVal( ButtonValue::Mixed );
329 
330  if (isVis)
331  bNativeOK = rRenderContext.DrawNativeControl(eCtrlType, ControlPart::Entire,
332  aCtrlRegion, nState, aControlValue, OUString());
333  }
334 
335  if (!bNativeOK && isVis)
336  rRenderContext.DrawImage(rPos, pData->GetImage(nIndex), nStyle);
337 }
338 
339 std::unique_ptr<SvLBoxItem> SvLBoxButton::Clone(SvLBoxItem const * pSource) const
340 {
341  std::unique_ptr<SvLBoxButton> pNew(new SvLBoxButton);
342  pNew->pData = static_cast<SvLBoxButton const *>(pSource)->pData;
343  return std::unique_ptr<SvLBoxItem>(pNew.release());
344 }
345 
346 void SvLBoxButton::ImplAdjustBoxSize(Size& io_rSize, ControlType i_eType, vcl::RenderContext const & rRenderContext)
347 {
348  if (!rRenderContext.IsNativeControlSupported( i_eType, ControlPart::Entire) )
349  return;
350 
351  ImplControlValue aControlValue;
352  tools::Rectangle aCtrlRegion( Point( 0, 0 ), io_rSize );
353 
354  aControlValue.setTristateVal( ButtonValue::On );
355 
356  tools::Rectangle aNativeBounds, aNativeContent;
357  bool bNativeOK = rRenderContext.GetNativeControlRegion( i_eType,
359  aCtrlRegion,
361  aControlValue,
362  aNativeBounds,
363  aNativeContent );
364  if( bNativeOK )
365  {
366  Size aContentSize( aNativeContent.GetSize() );
367  // leave a little space around the box image (looks better)
368  if( aContentSize.Height() + 2 > io_rSize.Height() )
369  io_rSize.setHeight( aContentSize.Height() + 2 );
370  if( aContentSize.Width() + 2 > io_rSize.Width() )
371  io_rSize.setWidth( aContentSize.Width() + 2 );
372  }
373 }
374 
376 {
377  if( !pViewData )
378  pViewData = pView->GetViewDataItem( pEntry, this );
379  Size aSize( pData->Width(), pData->Height() );
380 
382  if ( pView )
383  ImplAdjustBoxSize(aSize, eCtrlType, *pView);
384  pViewData->mnWidth = aSize.Width();
385  pViewData->mnHeight = aSize.Height();
386 }
387 
388 // ***************************************************************
389 // class SvLBoxContextBmp
390 // ***************************************************************
391 
393 {
396 
398 };
399 
400 // ***************************************************************
401 
402 SvLBoxContextBmp::SvLBoxContextBmp(const Image& aBmp1, const Image& aBmp2,
403  bool bExpanded)
404  :m_pImpl( new SvLBoxContextBmp_Impl )
405 {
406 
407  m_pImpl->m_bExpanded = bExpanded;
408  SetModeImages( aBmp1, aBmp2 );
409 }
410 
412  :SvLBoxItem( )
413  ,m_pImpl( new SvLBoxContextBmp_Impl )
414 {
415  m_pImpl->m_bExpanded = false;
416 }
417 
419 {
420 }
421 
423 {
425 }
426 
427 void SvLBoxContextBmp::SetModeImages( const Image& _rBitmap1, const Image& _rBitmap2 )
428 {
429  m_pImpl->m_aImage1 = _rBitmap1;
430  m_pImpl->m_aImage2 = _rBitmap2;
431 }
432 
434 {
435 
436  // OJ: #i27071# wrong mode so we just return the normal images
437  return _bFirst ? m_pImpl->m_aImage1 : m_pImpl->m_aImage2;
438 }
439 
441  SvViewDataItem* pViewData)
442 {
443  if( !pViewData )
444  pViewData = pView->GetViewDataItem( pEntry, this );
445  Size aSize = m_pImpl->m_aImage1.GetSizePixel();
446  pViewData->mnWidth = aSize.Width();
447  pViewData->mnHeight = aSize.Height();
448 }
449 
451  const Point& _rPos, SvTreeListBox& _rDev, vcl::RenderContext& rRenderContext,
452  const SvViewDataEntry* pView, const SvTreeListEntry& rEntry)
453 {
454 
455  // get the image.
456  const Image& rImage = implGetImageStore(pView->IsExpanded() != m_pImpl->m_bExpanded);
457 
458  bool _bSemiTransparent = bool( SvTLEntryFlags::SEMITRANSPARENT & rEntry.GetFlags( ) );
459  // draw
461  if (_bSemiTransparent)
463  rRenderContext.DrawImage(_rPos, rImage, nStyle);
464 }
465 
466 std::unique_ptr<SvLBoxItem> SvLBoxContextBmp::Clone(SvLBoxItem const * pSource) const
467 {
468  std::unique_ptr<SvLBoxContextBmp> pNew(new SvLBoxContextBmp);
469  pNew->m_pImpl->m_aImage1 = static_cast< SvLBoxContextBmp const * >( pSource )->m_pImpl->m_aImage1;
470  pNew->m_pImpl->m_aImage2 = static_cast< SvLBoxContextBmp const * >( pSource )->m_pImpl->m_aImage2;
471  pNew->m_pImpl->m_bExpanded = static_cast<SvLBoxContextBmp const *>(pSource)->m_pImpl->m_bExpanded;
472  return std::unique_ptr<SvLBoxItem>(pNew.release());
473 }
474 
476 {
477  if ( !bDataOk )
479  return nWidth;
480 }
481 
483 {
484  if ( !bDataOk )
486  return nHeight;
487 }
488 
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvLBoxButton * pBox
Definition: svlbitm.cxx:32
long Width() const
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:779
Image & GetImage(SvBmp nIndex)
Definition: svlbitm.hxx:94
VCL_DLLPRIVATE void InitData(bool _bRadioBtn, const Control *pControlForSettings)
Definition: svlbitm.cxx:39
bool IsRadio() const
Definition: svlbitm.cxx:166
SvLBoxButton * GetActBox() const
Definition: svlbitm.cxx:131
virtual ~SvLBoxContextBmp() override
Definition: svlbitm.cxx:418
static SvButtonState ConvertToButtonState(SvItemStateFlags nItemFlags)
Definition: svlbitm.cxx:107
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static Image GetRadioImage(const AllSettings &rSettings, DrawButtonFlags nFlags)
Definition: button.cxx:2714
bool DrawNativeControl(ControlType nType, ControlPart nPart, const tools::Rectangle &rControlRegion, ControlState nState, const ImplControlValue &aValue, const OUString &aCaption)
Request rendering of a particular control and/or part.
bool GetNativeControlRegion(ControlType nType, ControlPart nPart, const tools::Rectangle &rControlRegion, ControlState nState, const ImplControlValue &aValue, tools::Rectangle &rNativeBoundingRegion, tools::Rectangle &rNativeContentRegion) const
Query the native control's actual drawing region (including adornment)
long Height() const
static void ImplAdjustBoxSize(Size &io_rCtrlSize, ControlType i_eType, vcl::RenderContext const &pRenderContext)
Definition: svlbitm.cxx:346
Image & implGetImageStore(bool bFirst)
Definition: svlbitm.cxx:433
std::unique_ptr< SvLBoxButtonData_Impl > pImpl
Definition: svlbitm.hxx:66
SvTLEntryFlags GetFlags() const
virtual void Paint(const Point &rPos, SvTreeListBox &rOutDev, vcl::RenderContext &rRenderContext, const SvViewDataEntry *pView, const SvTreeListEntry &rEntry) override
Definition: svlbitm.cxx:195
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:704
void SetImage(SvBmp nIndex, const Image &aImage)
Definition: svlbitm.hxx:93
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
SvItemStateFlags
Definition: svlbitm.hxx:45
bool HasDefaultImages() const
Definition: svlbitm.cxx:161
SvTreeListEntry * GetActEntry() const
Definition: svlbitm.cxx:125
void SetWeight(FontWeight)
Definition: font/font.cxx:215
bool mbEmphasized
Definition: svlbitm.hxx:107
void setTristateVal(ButtonValue nTristate)
SvLBoxButtonData(const Control *pControlForSettings)
Definition: svlbitm.cxx:52
virtual SvLBoxItemType GetType() const override
Definition: svlbitm.cxx:190
bool IsEntryMnemonicsEnabled() const
SvLBoxButtonData * pData
Definition: svlbitm.hxx:146
View-dependent data for a tree list entry created in the virtual function SvTreeListBox::CreateViewDa...
virtual SvLBoxItemType GetType() const override
Definition: svlbitm.cxx:422
bool IsNativeControlSupported(ControlType nType, ControlPart nPart) const
Query the platform layer for control support.
std::unique_ptr< SvLBoxContextBmp_Impl > m_pImpl
Definition: svlbitm.hxx:227
WEIGHT_BOLD
bool IsExpanded() const
virtual void InitViewData(SvTreeListBox *pView, SvTreeListEntry *pEntry, SvViewDataItem *pViewData=nullptr) override
Definition: svlbitm.cxx:440
SvLBoxItemType
int GetHeight(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
bool IsStateHilighted() const
Definition: svlbitm.hxx:189
SvViewDataItem * GetViewDataItem(SvTreeListEntry const *, SvLBoxItem const *)
const vcl::Font & GetFont() const
Definition: outdev.hxx:637
virtual std::unique_ptr< SvLBoxItem > Clone(SvLBoxItem const *pSource) const override
Definition: svlbitm.cxx:226
static Image GetCheckImage(const AllSettings &rSettings, DrawButtonFlags nFlags)
Definition: button.cxx:3542
void SetModeImages(const Image &rBitmap1, const Image &rBitmap2)
Definition: svlbitm.cxx:427
virtual void Paint(const Point &rPos, SvTreeListBox &rOutDev, vcl::RenderContext &rRenderContext, const SvViewDataEntry *pView, const SvTreeListEntry &rEntry) override
Definition: svlbitm.cxx:300
static SvBmp GetIndex(SvItemStateFlags nItemState)
Definition: svlbitm.cxx:73
std::vector< Image > aBmps
Definition: svlbitm.hxx:68
virtual void Paint(const Point &rPos, SvTreeListBox &rOutDev, vcl::RenderContext &rRenderContext, const SvViewDataEntry *pView, const SvTreeListEntry &rEntry) override
Definition: svlbitm.cxx:450
short GetEntryWidth() const
NONE
SvTreeListEntry * pEntry
Definition: svlbitm.cxx:31
VCL_DLLPRIVATE void SetWidthAndHeight()
Definition: svlbitm.cxx:93
SvBmp
Definition: svlbitm.hxx:35
void SetStateUnchecked()
Definition: svlbitm.hxx:205
void CallLink()
Definition: svlbitm.cxx:68
virtual void InitViewData(SvTreeListBox *pView, SvTreeListEntry *pEntry, SvViewDataItem *pViewData=nullptr) override
Definition: svlbitm.cxx:375
Link< SvLBoxButtonData *, void > aLink
Definition: svlbitm.hxx:63
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:304
bool IsStateChecked() const
Definition: svlbitm.hxx:177
virtual std::unique_ptr< SvLBoxItem > Clone(SvLBoxItem const *pSource) const override
Definition: svlbitm.cxx:339
SvButtonState
Definition: treelistbox.hxx:57
bool TextCenterAndClipEnabled() const
const AllSettings & GetSettings() const
Definition: outdev.hxx:420
SvItemStateFlags nItemFlags
Definition: svlbitm.hxx:147
DrawTextFlags
Definition: outdev.hxx:144
long GetTextHeight() const
Height where any character of the current font fits; in logic coordinates.
Definition: text.cxx:888
Size GetSize() const
virtual ~SvLBoxButton() override
Definition: svlbitm.cxx:281
ControlType
These types are all based on the supported variants vcl/salnativewidgets.hxx and must be kept in-sync...
virtual std::unique_ptr< SvLBoxItem > Clone(SvLBoxItem const *pSource) const override
Definition: svlbitm.cxx:466
virtual SvLBoxItemType GetType() const override
Definition: svlbitm.cxx:285
virtual ~SvLBoxString() override
Definition: svlbitm.cxx:186
int GetWidth(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
bool IsStateTristate() const
Definition: svlbitm.hxx:185
Text maText
void ClickHdl(SvTreeListEntry *)
Definition: svlbitm.cxx:290
Definition: ctrl.hxx:33
void SetFont(const vcl::Font &rNewFont)
void SetDefaultImages(const Control *pControlForSettings)
Definition: svlbitm.cxx:137
Definition: image.hxx:40
bool IsStateUnchecked() const
Definition: svlbitm.hxx:181
OUString maText
Definition: svlbitm.hxx:109
long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
Width of the text.
Definition: text.cxx:877
bool IsEnabled() const
Definition: window2.cxx:1111
void Push(PushFlags nFlags=PushFlags::ALL)
Definition: outdevstate.cxx:60
virtual void InitViewData(SvTreeListBox *pView, SvTreeListEntry *pEntry, SvViewDataItem *pViewData=nullptr) override
Definition: svlbitm.cxx:233
void setWidth(long nWidth)
DrawImageFlags
Definition: outdev.hxx:172
virtual int CalcWidth(const SvTreeListBox *pView) const override
Definition: svlbitm.cxx:254
void SetStateChecked()
Definition: svlbitm.hxx:199
void StoreButtonState(SvTreeListEntry *pActEntry, SvLBoxButton *pActBox)
Definition: svlbitm.cxx:101
void setHeight(long nHeight)