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 "PreviewValueSet.hxx"
21 #include <vcl/commandevent.hxx>
22 
23 namespace sd::sidebar {
24 
25 const int gnBorderWidth(3);
26 const int gnBorderHeight(3);
27 
PreviewValueSet()28 PreviewValueSet::PreviewValueSet()
29     : ValueSet(nullptr)
30     , maPreviewSize(10,10)
31 {
32     SetStyle (
33         GetStyle()
34         & ~(WB_ITEMBORDER)// | WB_MENUSTYLEVALUESET)
35         //        | WB_FLATVALUESET);
36         );
37 }
38 
SetDrawingArea(weld::DrawingArea * pDrawingArea)39 void PreviewValueSet::SetDrawingArea(weld::DrawingArea* pDrawingArea)
40 {
41     ValueSet::SetDrawingArea(pDrawingArea);
42 
43     SetColCount(2);
44     SetExtraSpacing (2);
45 }
46 
~PreviewValueSet()47 PreviewValueSet::~PreviewValueSet()
48 {
49 }
50 
SetPreviewSize(const Size & rSize)51 void PreviewValueSet::SetPreviewSize (const Size& rSize)
52 {
53     maPreviewSize = rSize;
54 }
55 
SetContextMenuHandler(const Link<const Point *,void> & rLink)56 void PreviewValueSet::SetContextMenuHandler(const Link<const Point*, void>& rLink)
57 {
58     maContextMenuHandler = rLink;
59 }
60 
Command(const CommandEvent & rEvent)61 bool PreviewValueSet::Command(const CommandEvent& rEvent)
62 {
63     if (rEvent.GetCommand() != CommandEventId::ContextMenu)
64         return ValueSet::Command(rEvent);
65     maContextMenuHandler.Call(rEvent.IsMouseEvent() ? &rEvent.GetMousePosPixel() : nullptr);
66     return true;
67 }
68 
Resize()69 void PreviewValueSet::Resize()
70 {
71     ValueSet::Resize();
72 
73     Size aWindowSize (GetOutputSizePixel());
74     if (!aWindowSize.IsEmpty())
75     {
76         Rearrange();
77     }
78 }
79 
Rearrange()80 void PreviewValueSet::Rearrange()
81 {
82     sal_uInt16 nNewColumnCount (CalculateColumnCount (
83         GetOutputSizePixel().Width()));
84     sal_uInt16 nNewRowCount (CalculateRowCount (nNewColumnCount));
85 
86     SetFormat();
87     SetColCount(nNewColumnCount);
88     SetLineCount(nNewRowCount);
89 }
90 
CalculateColumnCount(int nWidth) const91 sal_uInt16 PreviewValueSet::CalculateColumnCount (int nWidth) const
92 {
93     int nColumnCount = 0;
94     if (nWidth > 0)
95     {
96         nColumnCount = nWidth / (maPreviewSize.Width() + 2*gnBorderWidth);
97         if (nColumnCount < 1)
98             nColumnCount = 1;
99     }
100     return static_cast<sal_uInt16>(nColumnCount);
101 }
102 
CalculateRowCount(sal_uInt16 nColumnCount) const103 sal_uInt16 PreviewValueSet::CalculateRowCount (sal_uInt16 nColumnCount) const
104 {
105     int nRowCount = 0;
106     int nItemCount = GetItemCount();
107     if (nColumnCount > 0)
108     {
109         nRowCount = (nItemCount+nColumnCount-1) / nColumnCount;
110         if (nRowCount < 1)
111             nRowCount = 1;
112     }
113 
114     return static_cast<sal_uInt16>(nRowCount);
115 }
116 
GetPreferredHeight(sal_Int32 nWidth)117 sal_Int32 PreviewValueSet::GetPreferredHeight (sal_Int32 nWidth)
118 {
119     int nRowCount (CalculateRowCount(CalculateColumnCount(nWidth)));
120     int nItemHeight (maPreviewSize.Height());
121 
122     return nRowCount * (nItemHeight + 2*gnBorderHeight);
123 }
124 
125 } // end of namespace sd::sidebar
126 
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
128