summaryrefslogtreecommitdiff
path: root/include/sfx2/thumbnailview.hxx
blob: 19190bf605c8d13985fcfdc0bc85d33e68dd71cb (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* -*- 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/.
 */

#ifndef INCLUDED_SFX2_THUMBNAILVIEW_HXX
#define INCLUDED_SFX2_THUMBNAILVIEW_HXX

#include <sfx2/dllapi.h>

#include <functional>
#include <memory>
#include <vector>

#include <vcl/ctrl.hxx>
#include <vcl/customweld.hxx>

class BitmapEx;
class MouseEvent;
class KeyEvent;
class DataChangedEvent;
class ScrollBar;
class ThumbnailViewItem;
typedef ::std::vector< ThumbnailViewItem* > ThumbnailValueItemList;


struct ThumbnailItemAttributes;

/*************************************************************************

    Description
    ============

    class ThumbnailView

    This class allows the selection of an item. In the process items are
    drawn side by side. The selection of items can be more clear than in a
    ListBox shape for example in case of colors or samples.
    The amount of columns drawn by the control and whether the items
    should be encircled can be specified. Optional a NoSelection or name
    field could be shown. By default image and color items are supported.
    Items could be drawn by oneself if InsertItem() is only called with
    an ID. To achieve this the UserDraw handler needs to be overridden. The
    description text could be specified afterwards in case of UserDraw
    and any other items.

    Cross references

    class ListBox

    --------------------------------------------------------------------------

    WinBits

    WB_VSCROLL          A scrollbar will be always shown. The visible number of
                        lines have to be specified with SetLineCount() if this
                        flag is set.
    WB_TABSTOP          It is possible to jump into the ValueSet with the tab key.
    WB_NOTABSTOP        It is not possible to jump into the ValueSet with the
                        tab key.
    --------------------------------------------------------------------------

    The number of columns must be either set with SetColCount() or
    SetItemWidth(). If the number of columns is specified by SetColCount()
    the width of the items will be calculated by the visible range.
    If the items should have a static width, it has to be specified
    with SetItemWidth(). In this case the number of columns will be calculated
    by the visible range.

    The number of rows is given by the number of items / number of columns. The
    number of visible rows must either specified by SetLineCount() or
    SetItemWidth(). If the number of visible rows is specified by SetLineCount(),
    the height of the items will be calculated from the visible height. If the
    items should have a fixed height it has to be specified with SetItemHeight().
    In this case the number of visible rows is then calculated from the visible
    height. If the number of visible rows is neither specified by SetLineCount()
    nor by SetItemHeight() all rows will be shown. The height of the items will
    be calculated by the visible height. If the number of visible rows is
    specified by SetLineCount() or SetItemHeight() ValueSet does scroll
    automatically when more lines are available, as are visible. If scrolling
    should be also possible with a ScrollBar  WB_VSCROLL needs to be set.

    The distance between the items can be increased by SetExtraSpacing(). The
    distance, which will be shown between two items (both in x and in y), is
    measured in pixels.

    The exact window size for a specific item size can be calculated by
    CalcWindowSizePixel(). To do this all relevant data (number of columns/...)
    have to be specified and if no number of rows was set, all items need to
    be inserted. If the window was created with WB_BORDER/Border=sal_True the
    size has to be specified with SetOutputSizePixel(). In other cases different
    size-methods can be used. With CalcItemSize() the inner and outer size of
    an item could be calculated (for this the free space defined by
    SetExtraSpacing() will not be included).

    The background color could be specified by SetColor(), with which the image
    or UserDraw items will be underlaid. If no color is specified the color
    of other windows (WindowColor) will be used for the background.

    --------------------------------------------------------------------------

    At first all items should be inserted and only then Show() should be called
    since the output area will be precomputed. If this is not done the first
    Paint will appear a little bit slower. Therefore the Control, if it is loaded
    from the resource and only supplied with items during runtime, should be
    loaded with Hide = sal_True and then displayed with Show().

    In case of a visible Control the creation of the new output area could be
    activated before Paint by calling Format().

    --------------------------------------------------------------------------

    If Drag and Drop will be called from the ValueSet the Command-Handler has to
    be overridden. From this StartDrag needs to be called. If this method returns
    sal_True the drag-process could be initiated by  ExecuteDrag(), otherwise no
    processing will take place. This method makes sure that ValueSet stops its
    processing and as appropriate selects the entry. Therefore the calling of
    Select-Handler within this function must be expected.

    For dropping QueryDrop() and Drop() need to be overridden and ShowDropPos()
    and HideDropPos() should be called within these methods.
    To show the insertion point ShowDropPos() has to be called within the
    QueryDrop-Handler. ShowDropPos() also scrolls the ValueSet if the passed
    position is located at the window border. Furthermore ShowDropPos() returns
    the position, at which the item should be inserted respectively which
    insertion point was shown. If no insertion point was determined
    VALUESET_ITEM_NOTFOUND will be returned. If the window was left during dragging
    or the drag process is terminated HideDropPos() should be called in any case.

    --------------------------------------------------------------------------

    This class is currently still in the SV-Tools. That's why the ValueSet needs
    to be loaded as a Control out of the resource and the desired WinBits have
    to be set (before Show) with SetStyle().

*************************************************************************/

/* ThumbnailView types */

#define THUMBNAILVIEW_ITEM_NOTFOUND  (sal_uInt16(-1))

// Display all the available items in the thumbnail.
class ViewFilterAll
{
public:

    bool operator () (const ThumbnailViewItem*) const
    {
        return true;
    }
};

/**
 *
 *  Class to display thumbnails with their names below their respective icons
 *
 **/

class SFX2_DLLPUBLIC ThumbnailViewBase
{
    friend class ThumbnailViewAcc;
    friend class ThumbnailViewItemAcc;

    SFX2_DLLPRIVATE virtual sal_uInt16 ImplGetVisibleItemCount() const = 0;
    SFX2_DLLPRIVATE virtual ThumbnailViewItem* ImplGetVisibleItem(sal_uInt16 nVisiblePos) = 0;

    virtual css::uno::Reference<css::accessibility::XAccessible> getAccessible() = 0;

public:
    /// Updates information in the view; used only in RecentDocsView ATM.
    virtual void Reload() {}

    virtual bool renameItem(ThumbnailViewItem* pItem, const OUString& sNewTitle);

    virtual ~ThumbnailViewBase();
};

class SFX2_DLLPUBLIC ThumbnailView : public Control, public ThumbnailViewBase
{
public:

    ThumbnailView(vcl::Window* pParent, WinBits nWinStyle = WB_TABSTOP);

    virtual ~ThumbnailView() override;
    virtual void dispose() override;

    virtual void MouseMove(const MouseEvent& rMEvt) override;

    void AppendItem(std::unique_ptr<ThumbnailViewItem> pItem);

    virtual void Clear();

    // Change current thumbnail item list with new one (invalidates all pointers to a thumbnail item)
    void updateItems(std::vector<std::unique_ptr<ThumbnailViewItem>> items);

    size_t GetItemPos( sal_uInt16 nItemId ) const;

    sal_uInt16 GetItemId( size_t nPos ) const;

    sal_uInt16 GetItemId( const Point& rPos ) const;

    sal_uInt16 getNextItemId () const;

    void setItemMaxTextLength (sal_uInt32 nLength);

    void setItemDimensions (long ItemWidth, long ThumbnailHeight,
                            long DisplayHeight, int itemPadding);

    void SelectItem( sal_uInt16 nItemId );

    bool IsItemSelected( sal_uInt16 nItemId ) const;

    /**
     *
     * @brief deselect all current selected items.
     *
     **/

    void deselectItems ();

    void ShowTooltips( bool bShowTooltips );

    void filterItems (const std::function<bool (const ThumbnailViewItem*) > &func);

    virtual void Resize() override;

    static BitmapEx readThumbnail(const OUString &msURL);

protected:

    virtual void KeyInput( const KeyEvent& rKEvt ) override;

    virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;

    virtual void Command( const CommandEvent& rCEvt ) override;

    virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;

    virtual void GetFocus() override;

    virtual void LoseFocus() override;

    virtual void StateChanged( StateChangedType nStateChange ) override;

    virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;

    virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessible() override;

    virtual css::uno::Reference<css::accessibility::XAccessible> getAccessible() override;

protected:

    // Drawing item related functions, override them to make your own custom ones.

    void DrawItem (ThumbnailViewItem const *pItem);

    virtual void OnItemDblClicked (ThumbnailViewItem *pItem);

protected:

    friend class ThumbnailViewAcc;
    friend class ThumbnailViewItemAcc;
    using Window::ImplInit;

    void CalculateItemPositions (bool bScrollBarUsed = false);
    void MakeItemVisible( sal_uInt16 nId );

    SFX2_DLLPRIVATE void         ImplInit();

    virtual void ApplySettings(vcl::RenderContext& rRenderContext) override;

    SFX2_DLLPRIVATE void         ImplDeleteItems();
    SFX2_DLLPRIVATE size_t       ImplGetItem( const Point& rPoint ) const;
    SFX2_DLLPRIVATE ThumbnailViewItem*    ImplGetItem( size_t nPos );
    SFX2_DLLPRIVATE virtual sal_uInt16 ImplGetVisibleItemCount() const override;
    SFX2_DLLPRIVATE virtual ThumbnailViewItem* ImplGetVisibleItem(sal_uInt16 nVisiblePos) override;
    SFX2_DLLPRIVATE void         ImplFireAccessibleEvent( short nEventId, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue );
    SFX2_DLLPRIVATE bool         ImplHasAccessibleListeners();
    DECL_DLLPRIVATE_LINK( ImplScrollHdl, ScrollBar*, void );

protected:

    std::vector< std::unique_ptr<ThumbnailViewItem> > mItemList;
    ThumbnailValueItemList mFilteredItemList; ///< Cache to store the filtered items
    ThumbnailValueItemList::iterator mpStartSelRange;
    VclPtr<ScrollBar> mpScrBar;
    long mnItemWidth;
    long mnItemHeight;
    long mnItemPadding;
    long mnThumbnailHeight;     // Maximum height of the thumbnail
    long mnDisplayHeight;       // Height of the data display box (name, etc)
    long mnVisLines;
    long mnLines;

    sal_uInt16 mnCols;
    sal_uInt16 mnFirstLine;
    bool mbScroll : 1;
    bool mbHasVisibleItems : 1;
    bool mbShowTooltips : 1;
    Color maFillColor;              ///< Background color of the thumbnail view widget.
    Color maTextColor;              ///< Text color.
    Color maHighlightColor;         ///< Color of the highlight (background) of the hovered item.
    Color maHighlightTextColor;     ///< Color of the text for the highlighted item.
    Color maSelectHighlightColor;   ///< Color of the highlight (background) of the selected and hovered item.
    Color maSelectHighlightTextColor;   ///< Color of the text of the selected and hovered item.
    double mfHighlightTransparence; ///< Transparence of the highlight.

    std::unique_ptr<ThumbnailItemAttributes> mpItemAttrs;

    std::function<bool (const ThumbnailViewItem*)> maFilterFunc;
};

class SFX2_DLLPUBLIC SfxThumbnailView : public weld::CustomWidgetController, public ThumbnailViewBase
{
public:
    SfxThumbnailView(std::unique_ptr<weld::ScrolledWindow> xWindow, std::unique_ptr<weld::Menu> xMenu);

    virtual ~SfxThumbnailView() override;

    virtual bool MouseMove(const MouseEvent& rMEvt) override;

    void AppendItem(std::unique_ptr<ThumbnailViewItem> pItem);

    void RemoveItem(sal_uInt16 nItemId);

    void Clear();

    // Change current thumbnail item list with new one (invalidates all pointers to a thumbnail item)
    void updateItems(std::vector<std::unique_ptr<ThumbnailViewItem>> items);

    size_t GetItemPos( sal_uInt16 nItemId ) const;

    sal_uInt16 GetItemId( size_t nPos ) const;

    sal_uInt16 GetItemId( const Point& rPos ) const;

    sal_uInt16 getNextItemId () const;

    void setItemMaxTextLength (sal_uInt32 nLength);

    void setItemDimensions (long ItemWidth, long ThumbnailHeight,
                            long DisplayHeight, int itemPadding);

    void SelectItem( sal_uInt16 nItemId );

    bool IsItemSelected( sal_uInt16 nItemId ) const;

    /**
     *
     * @brief deselect all current selected items.
     *
     **/

    void deselectItems ();

    void ShowTooltips( bool bShowTooltips );

    void SetMultiSelectionEnabled( bool bIsMultiSelectionEnabled );

    void filterItems (const std::function<bool (const ThumbnailViewItem*) > &func);

    void setItemStateHdl (const Link<const ThumbnailViewItem*,void> &aLink) { maItemStateHdl = aLink; }

    virtual void Resize() override;

    virtual void Show() override
    {
        mxScrolledWindow->show();
        CustomWidgetController::Show();
    }

    virtual void Hide() override
    {
        mxScrolledWindow->hide();
        CustomWidgetController::Hide();
    }

    virtual void SetDrawingArea(weld::DrawingArea* pDrawingArea) override;

protected:

    virtual bool KeyInput( const KeyEvent& rKEvt ) override;

    virtual bool MouseButtonDown( const MouseEvent& rMEvt ) override;

    virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;

    virtual void GetFocus() override;

    virtual void LoseFocus() override;

    virtual OUString RequestHelp(tools::Rectangle& rRect) override;

    virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessible() override;

    virtual css::uno::Reference<css::accessibility::XAccessible> getAccessible() override;

protected:

    // Drawing item related functions, override them to make your own custom ones.

    void DrawItem (ThumbnailViewItem const *pItem);

    virtual void OnItemDblClicked (ThumbnailViewItem *pItem);

protected:

    friend class SfxThumbnailViewAcc;
    friend class ThumbnailViewItemAcc;

    void CalculateItemPositions (bool bScrollBarUsed = false);
    void MakeItemVisible( sal_uInt16 nId );

    SFX2_DLLPRIVATE void         ImplInit();

    SFX2_DLLPRIVATE void         ImplDeleteItems();
    SFX2_DLLPRIVATE size_t       ImplGetItem( const Point& rPoint ) const;
    SFX2_DLLPRIVATE ThumbnailViewItem*    ImplGetItem( size_t nPos );
    SFX2_DLLPRIVATE virtual sal_uInt16 ImplGetVisibleItemCount() const override;
    SFX2_DLLPRIVATE virtual ThumbnailViewItem* ImplGetVisibleItem(sal_uInt16 nVisiblePos) override;
    SFX2_DLLPRIVATE void         ImplFireAccessibleEvent( short nEventId, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue );
    SFX2_DLLPRIVATE bool         ImplHasAccessibleListeners();
    DECL_DLLPRIVATE_LINK( ImplScrollHdl, weld::ScrolledWindow&, void );

protected:

    std::vector< std::unique_ptr<ThumbnailViewItem> > mItemList;
    css::uno::Reference<css::accessibility::XAccessible> mxAccessible;
    ThumbnailValueItemList mFilteredItemList; ///< Cache to store the filtered items
    ThumbnailValueItemList::iterator mpStartSelRange;
    long mnItemWidth;
    long mnItemHeight;
    long mnItemPadding;
    long mnThumbnailHeight;     // Maximum height of the thumbnail
    long mnDisplayHeight;       // Height of the data display box (name, etc)
    long mnVisLines;
    long mnLines;

    sal_uInt16 mnCols;
    sal_uInt16 mnFirstLine;
    bool mbScroll : 1;
    bool mbHasVisibleItems : 1;
    bool mbShowTooltips : 1;
    bool mbIsMultiSelectionEnabled: 1;
    Color maFillColor;              ///< Background color of the thumbnail view widget.
    Color maTextColor;              ///< Text color.
    Color maHighlightColor;         ///< Color of the highlight (background) of the hovered item.
    Color maHighlightTextColor;     ///< Color of the text for the highlighted item.
    Color maSelectHighlightColor;   ///< Color of the highlight (background) of the selected and hovered item.
    Color maSelectHighlightTextColor;   ///< Color of the text of the selected and hovered item.
    double mfHighlightTransparence; ///< Transparence of the highlight.

    Link<const ThumbnailViewItem*, void> maItemStateHdl;
    std::unique_ptr<ThumbnailItemAttributes> mpItemAttrs;
    std::unique_ptr<weld::ScrolledWindow> mxScrolledWindow;
    std::unique_ptr<weld::Menu> mxContextMenu;

    std::function<bool (const ThumbnailViewItem*)> maFilterFunc;
};


#endif // INCLUDED_SFX2_THUMBNAILVIEW_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */