1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/* -*- 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 <sfx2/thumbnailviewitem.hxx>
#include <sfx2/thumbnailview.hxx>
#include "thumbnailviewacc.hxx"
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <drawinglayer/attribute/fillgraphicattribute.hxx>
#include <drawinglayer/attribute/fontattribute.hxx>
#include <drawinglayer/attribute/lineattribute.hxx>
#include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
#include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
#include <drawinglayer/primitive2d/PolyPolygonSelectionPrimitive2D.hxx>
#include <drawinglayer/primitive2d/PolygonStrokePrimitive2D.hxx>
#include <drawinglayer/primitive2d/textlayoutdevice.hxx>
#include <drawinglayer/primitive2d/textprimitive2d.hxx>
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <vcl/event.hxx>
#include <vcl/graph.hxx>
#include <vcl/mnemonic.hxx>
#include <vcl/outdev.hxx>
#include <vcl/texteng.hxx>
#include <bitmaps.hlst>
#include <drawinglayer/primitive2d/discretebitmapprimitive2d.hxx>
#include <unotools/historyoptions.hxx>
using namespace basegfx;
using namespace basegfx::utils;
using namespace ::com::sun::star;
using namespace drawinglayer::attribute;
using namespace drawinglayer::primitive2d;
ThumbnailViewItem::ThumbnailViewItem(ThumbnailView& rView, sal_uInt16 nId)
: mrParent(rView)
, mnId(nId)
, mbVisible(true)
, mbBorder(true)
, mbSelected(false)
, mbHover(false)
{
}
ThumbnailViewItem::~ThumbnailViewItem()
{
if( mxAcc.is() )
{
mxAcc->ParentDestroyed();
}
}
void ThumbnailViewItem::show (bool bVisible)
{
mbVisible = bVisible;
}
void ThumbnailViewItem::setSelection (bool state)
{
mbSelected = state;
}
void ThumbnailViewItem::setHighlight (bool state)
{
mbHover = state;
}
::tools::Rectangle ThumbnailViewItem::updateHighlight(bool bVisible, const Point& rPoint)
{
bool bNeedsPaint = false;
if (bVisible && getDrawArea().Contains(rPoint))
{
if (!isHighlighted())
bNeedsPaint = true;
setHighlight(true);
}
else
{
if (isHighlighted())
bNeedsPaint = true;
setHighlight(false);
}
if (bNeedsPaint)
return getDrawArea();
return ::tools::Rectangle();
}
void ThumbnailViewItem::setTitle (const OUString& rTitle)
{
if (mrParent.renameItem(this, rTitle))
maTitle = rTitle;
}
const rtl::Reference< ThumbnailViewItemAcc > & ThumbnailViewItem::GetAccessible( bool bIsTransientChildrenDisabled )
{
if( !mxAcc.is() )
mxAcc = new ThumbnailViewItemAcc( this, bIsTransientChildrenDisabled );
return mxAcc;
}
void ThumbnailViewItem::setDrawArea (const ::tools::Rectangle &area)
{
maDrawArea = area;
}
void ThumbnailViewItem::calculateItemsPosition (const tools::Long nThumbnailHeight,
const tools::Long nPadding, sal_uInt32 nMaxTextLength,
const ThumbnailItemAttributes *pAttrs)
{
drawinglayer::primitive2d::TextLayouterDevice aTextDev;
aTextDev.setFontAttribute(pAttrs->aFontAttr,
pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(),
css::lang::Locale() );
Size aImageSize = maPreview1.GetSizePixel();
// Calculate thumbnail position
const Point aPos = maDrawArea.TopCenter();
maPrev1Pos = aPos + Point(-aImageSize.Width() / 2, nPadding + (nThumbnailHeight - aImageSize.Height()) / 2);
// Calculate pin position
maPinPos = maDrawArea.TopLeft() + Point(nPadding, nPadding);
// Calculate text position
maTextPos = aPos + Point(-aTextDev.getTextWidth(maTitle, 0, nMaxTextLength) / 2, nThumbnailHeight + nPadding * 2);
}
void ThumbnailViewItem::Paint (drawinglayer::processor2d::BaseProcessor2D *pProcessor,
const ThumbnailItemAttributes *pAttrs)
{
BColor aFillColor = pAttrs->aFillColor;
drawinglayer::primitive2d::Primitive2DContainer aSeq(4);
double fTransparence = 0.0;
// Draw background
if( mbSelected && mbHover)
aFillColor = pAttrs->aHighlightColor;
else if (mbSelected || mbHover)
{
aFillColor = pAttrs->aHighlightColor;
if (mbHover)
fTransparence = pAttrs->fHighlightTransparence;
}
sal_uInt32 nPrimitive = 0;
aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference(
new PolyPolygonSelectionPrimitive2D( B2DPolyPolygon(::tools::Polygon(maDrawArea, THUMBNAILVIEW_ITEM_CORNER, THUMBNAILVIEW_ITEM_CORNER).getB2DPolygon()),
aFillColor,
fTransparence,
0.0,
true));
// Draw thumbnail
Point aPos = maPrev1Pos;
Size aImageSize = maPreview1.GetSizePixel();
aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference( new FillGraphicPrimitive2D(
createTranslateB2DHomMatrix(aPos.X(),aPos.Y()),
FillGraphicAttribute(Graphic(maPreview1),
B2DRange(
B2DPoint(0,0),
B2DPoint(aImageSize.Width(),aImageSize.Height())),
false)
));
if (mbBorder)
{
// draw thumbnail borders
float fWidth = aImageSize.Width() - 1;
float fHeight = aImageSize.Height() - 1;
float fPosX = maPrev1Pos.getX();
float fPosY = maPrev1Pos.getY();
B2DPolygon aBounds;
aBounds.append(B2DPoint(fPosX,fPosY));
aBounds.append(B2DPoint(fPosX+fWidth,fPosY));
aBounds.append(B2DPoint(fPosX+fWidth,fPosY+fHeight));
aBounds.append(B2DPoint(fPosX,fPosY+fHeight));
aBounds.setClosed(true);
aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference(createBorderLine(aBounds));
}
// Draw text below thumbnail
addTextPrimitives(maTitle, pAttrs, maTextPos, aSeq);
pProcessor->process(aSeq);
}
void ThumbnailViewItem::addTextPrimitives (const OUString& rText, const ThumbnailItemAttributes *pAttrs, Point aPos, drawinglayer::primitive2d::Primitive2DContainer& rSeq)
{
// adjust text drawing position according to text font
drawinglayer::primitive2d::TextLayouterDevice aTextDev;
aTextDev.setFontAttribute(
pAttrs->aFontAttr,
pAttrs->aFontSize.getX(),
pAttrs->aFontSize.getY(),
css::lang::Locale());
aPos.setY(aPos.getY() + aTextDev.getTextHeight());
sal_Int32 nMnemonicPos = -1;
OUString aOrigText(mrParent.isDrawMnemonic() ? removeMnemonicFromString(rText, nMnemonicPos) : rText);
TextEngine aTextEngine;
aTextEngine.SetFont(getVclFontFromFontAttribute(pAttrs->aFontAttr,
pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(), 0,
css::lang::Locale()));
aTextEngine.SetMaxTextWidth(maDrawArea.getOpenWidth());
aTextEngine.SetText(aOrigText);
sal_Int32 nPrimitives = rSeq.size();
sal_Int32 nFinalPrimCount = nPrimitives + aTextEngine.GetLineCount(0);
rSeq.resize(nFinalPrimCount);
// Create the text primitives
sal_Int32 nLineStart = 0;
OUString aText(aOrigText);
for (sal_Int32 i=0; i < aTextEngine.GetLineCount(0); ++i)
{
sal_Int32 nLineLength = aTextEngine.GetLineLen(0, i);
double nLineWidth = aTextDev.getTextWidth (aText, nLineStart, nLineLength);
bool bTooLong = (aPos.getY() + aTextEngine.GetCharHeight()) > maDrawArea.Bottom();
if (bTooLong && (nLineLength + nLineStart) < aOrigText.getLength())
{
// Add the '...' to the last line to show, even though it may require to shorten the line
double nDotsWidth = aTextDev.getTextWidth("...",0,3);
sal_Int32 nLength = nLineLength - 1;
while ( nDotsWidth + aTextDev.getTextWidth(aText, nLineStart, nLength) > maDrawArea.getOpenWidth() && nLength > 0)
{
--nLength;
}
aText = OUString::Concat(aText.subView(0, nLineStart+nLength)) + "...";
nLineLength = nLength + 3;
}
double nLineX = maDrawArea.Left() + (maDrawArea.getOpenWidth() - nLineWidth) / 2.0;
basegfx::B2DHomMatrix aTextMatrix( createScaleTranslateB2DHomMatrix(
pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(),
nLineX, double( aPos.Y() ) ) );
// setup color
BColor aTextColor = pAttrs->aTextColor;
if(mbSelected)
{
aTextColor = pAttrs->aHighlightTextColor;
}
rSeq[nPrimitives++] = drawinglayer::primitive2d::Primitive2DReference(
new TextSimplePortionPrimitive2D(aTextMatrix,
aText, nLineStart, nLineLength,
std::vector<double>(),
{},
pAttrs->aFontAttr,
css::lang::Locale(),
aTextColor));
if (nMnemonicPos != -1 && nMnemonicPos >= nLineStart && nMnemonicPos < nLineStart + nLineLength)
{
rSeq.resize(nFinalPrimCount + 1);
auto aTextArray = aTextDev.getTextArray(aText, nLineStart, nLineLength, true);
auto nPos = nMnemonicPos - nLineStart;
auto lc_x1 = nPos ? aTextArray[nPos - 1] : 0;
auto lc_x2 = aTextArray[nPos];
auto fMnemonicWidth = std::abs(lc_x1 - lc_x2);
auto fMnemonicHeight = aTextDev.getUnderlineHeight();
auto fPosX = nLineX + std::min(lc_x1, lc_x2);
auto fPosY = aPos.Y() + aTextDev.getUnderlineOffset();
B2DPolygon aLine;
aLine.append(B2DPoint(fPosX, fPosY));
aLine.append(B2DPoint(fPosX + fMnemonicWidth, fPosY));
drawinglayer::attribute::LineAttribute aLineAttribute(Color(aTextColor).getBColor(), fMnemonicHeight);
rSeq[nPrimitives++] = drawinglayer::primitive2d::Primitive2DReference(
new PolygonStrokePrimitive2D(std::move(aLine), aLineAttribute));
}
nLineStart += nLineLength;
aPos.setY(aPos.getY() + aTextEngine.GetCharHeight());
if (bTooLong)
break;
}
}
rtl::Reference<drawinglayer::primitive2d::PolygonHairlinePrimitive2D>
ThumbnailViewItem::createBorderLine (const basegfx::B2DPolygon& rPolygon)
{
return new PolygonHairlinePrimitive2D(rPolygon, Color(128, 128, 128).getBColor());
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|