summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/inc/checklistmenu.hxx
blob: 5585bc3e846f3468821b8d0640a4cf945e228c0e (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
/* -*- 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/.
 */

#pragma once

#include <vcl/timer.hxx>
#include <vcl/virdev.hxx>
#include <vcl/weld.hxx>

#include <memory>
#include <unordered_set>
#include <map>
#include <set>

class ScCheckListMenuControl;
class ScViewData;
struct ScCheckListMember;
struct ImplSVEvent;

struct ScCheckListMember
{
    enum DatePartType
    {
        YEAR,
        MONTH,
        DAY,
    };

    OUString                 maName; // node name
    OUString                 maRealName;
    double                   mnValue; // number value of filter condition
    bool                     mbVisible;
    bool                     mbDate;
    bool                     mbLeaf;
    bool                     mbValue; // true if the filter condition is value
    DatePartType             meDatePartType;
    // To store Year and Month if the member if DAY type
    std::vector<OUString>    maDateParts;
    ScCheckListMember();
    std::unique_ptr<weld::TreeIter> mxParent;
};

class ScCheckListMenuWindow;
class ScListSubMenuControl;

/**
 * This class implements a popup window for the auto filter dropdown.
 */
class ScCheckListMenuControl final
{
public:
    static constexpr size_t MENU_NOT_SELECTED = 999;

    /**
     * Action to perform when an event takes place.  Create a sub-class of
     * this to implement the desired action.
     */
    class Action
    {
    public:
        virtual ~Action() {}
        // return true to dismiss the popup
        virtual bool execute() = 0;
    };

    struct ResultEntry
    {
        OUString aName;
        double nValue; // number value of filter condition
        bool bValid;
        bool bDate;
        bool bValue; // true if the filter condition is value

        bool operator<(const ResultEntry& rhs) const
        {
            return aName < rhs.aName;
        }

        bool operator == (const ResultEntry& rhs) const
        {
            return aName == rhs.aName &&
                   bValid == rhs.bValid &&
                   bDate == rhs.bDate &&
                   bValue == rhs.bValue &&
                   nValue == rhs.nValue;
        }
    };
    typedef std::set<ResultEntry> ResultType;

    struct MenuItemData
    {
        bool     mbEnabled:1;
        std::shared_ptr<Action> mxAction;
        std::unique_ptr<ScListSubMenuControl> mxSubMenuWin;

        MenuItemData();
    };

    /**
     * Extended data that the client code may need to store.  Create a
     * sub-class of this and store data there.
     */
    struct ExtendedData {

    virtual ~ExtendedData() {}

    };

    /**
     * Configuration options for this popup window.
     */
    struct Config
    {
        bool mbAllowEmptySet;
        bool mbRTL;
        Config();
    };

    ScCheckListMenuControl(weld::Widget* pParent, ScViewData& rViewData,
                           bool bTreeMode, int nWidth);
    ~ScCheckListMenuControl();

    void addMenuItem(const OUString& rText, Action* pAction);
    void addSeparator();
    ScListSubMenuControl* addSubMenuItem(const OUString& rText, bool bEnabled, bool bColorMenu);

    void selectMenuItem(size_t nPos, bool bSubMenuTimer);
    void queueLaunchSubMenu(size_t nPos, ScListSubMenuControl* pMenu);

    void setMemberSize(size_t n);
    void addDateMember(const OUString& rName, double nVal, bool bVisible);
    void addMember(const OUString& rName, const double nVal, bool bVisible,
                   bool bValue = false);
    size_t initMembers(int nMaxMemberWidth = -1);
    void setConfig(const Config& rConfig);

    bool isAllSelected() const;
    void getResult(ResultType& rResult);
    void launch(weld::Widget* pWidget, const tools::Rectangle& rRect);
    void close(bool bOK);

    void StartPopupMode(weld::Widget* pParent, const tools::Rectangle& rRect);
    void EndPopupMode();

    size_t getSubMenuPos(const ScListSubMenuControl* pSubMenu);
    void setSubMenuFocused(const ScListSubMenuControl* pSubMenu);
    void queueCloseSubMenu();
    void clearSelectedMenuItem();

    /**
     * Set auxiliary data that the client code might need.  Note that this
     * popup window class manages its life time; no explicit deletion of the
     * instance is needed in the client code.
     */
    void setExtendedData(std::unique_ptr<ExtendedData> p);

    /**
     * Get the store auxiliary data, or NULL if no such data is stored.
     */
    ExtendedData* getExtendedData();

    ScViewData& GetViewData() const { return mrViewData; }

    void GrabFocus();

    void setOKAction(Action* p);
    void setPopupEndAction(Action* p);

    int GetTextWidth(const OUString& rsName) const;
    int IncreaseWindowWidthToFitText(int nMaxTextWidth);

    /**
     * Dismiss all visible popup menus and set focus back to the application
     * window.  This method is called e.g. when a menu action is fired.
     */
    void terminateAllPopupMenus();

    void endSubMenu(ScListSubMenuControl& rSubMenu);
private:

    std::vector<MenuItemData>         maMenuItems;

    /**
     * Calculate the appropriate window size based on the menu items.
     */
    void prepWindow();
    void setAllMemberState(bool bSet);
    void selectCurrentMemberOnly(bool bSet);
    void updateMemberParents(const weld::TreeIter* pLeaf, size_t nIdx);

    std::unique_ptr<weld::TreeIter> ShowCheckEntry(const OUString& sName, ScCheckListMember& rMember, bool bShow = true, bool bCheck = true);
    void CheckEntry(std::u16string_view sName, const weld::TreeIter* pParent, bool bCheck);
    void CheckEntry(const weld::TreeIter& rEntry, bool bCheck);
    void GetRecursiveChecked(const weld::TreeIter* pEntry, std::unordered_set<OUString>& vOut, OUString& rLabel);
    std::unordered_set<OUString> GetAllChecked();
    bool IsChecked(std::u16string_view sName, const weld::TreeIter* pParent);
    int GetCheckedEntryCount() const;
    void CheckAllChildren(const weld::TreeIter& rEntry, bool bCheck);

    void setSelectedMenuItem(size_t nPos);

    std::unique_ptr<weld::TreeIter> FindEntry(const weld::TreeIter* pParent, std::u16string_view sNode);

    void executeMenuItem(size_t nPos);

    /**
     * Get the area of the active row. Suitable as the parent rectangle
     * argument for Executing a popup
    */
    tools::Rectangle GetSubMenuParentRect();

    struct SubMenuItemData;

    void handleMenuTimeout(const SubMenuItemData* pTimer);

    void launchSubMenu();

    void CreateDropDown();

    DECL_LINK(ButtonHdl, weld::Button&, void);
    DECL_LINK(TriStateHdl, weld::Toggleable&, void);

    void Check(const weld::TreeIter* pIter);

    DECL_LINK(CheckHdl, const weld::TreeView::iter_col&, void);

    DECL_LINK(PopupModeEndHdl, weld::Popover&, void);

    DECL_LINK(EdModifyHdl, weld::Entry&, void);
    DECL_LINK(EdActivateHdl, weld::Entry&, bool);

    DECL_LINK(RowActivatedHdl, weld::TreeView& rMEvt, bool);
    DECL_LINK(SelectHdl, weld::TreeView&, void);
    DECL_LINK(TreeSizeAllocHdl, const Size&, void);
    DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
    DECL_LINK(MenuKeyInputHdl, const KeyEvent&, bool);
    DECL_LINK(MouseEnterHdl, const MouseEvent&, bool);

    DECL_LINK(PostPopdownHdl, void*, void);

    void SetDropdownPos();

    DECL_LINK(SetDropdownPosHdl, void*, void);

    DECL_LINK(CommandHdl, const CommandEvent&, bool);

    void ResizeToRequest();

    void DropPendingEvents();

private:
    std::unique_ptr<weld::Builder> mxBuilder;
    std::unique_ptr<weld::Popover> mxPopover;
    std::unique_ptr<weld::Container> mxContainer;
    std::unique_ptr<weld::TreeView> mxMenu;
    std::unique_ptr<weld::TreeIter> mxScratchIter;
    std::unique_ptr<weld::Widget> mxNonMenu;
    std::unique_ptr<weld::Entry> mxEdSearch;
    std::unique_ptr<weld::Widget> mxBox;
    std::unique_ptr<weld::TreeView> mxListChecks;
    std::unique_ptr<weld::TreeView> mxTreeChecks;
    weld::TreeView* mpChecks;

    std::unique_ptr<weld::CheckButton> mxChkToggleAll;
    std::unique_ptr<weld::Button> mxBtnSelectSingle;
    std::unique_ptr<weld::Button> mxBtnUnselectSingle;

    std::unique_ptr<weld::Box> mxButtonBox;
    std::unique_ptr<weld::Button> mxBtnOk;
    std::unique_ptr<weld::Button> mxBtnCancel;
    std::unique_ptr<weld::Menu> mxContextMenu;

    ScopedVclPtr<VirtualDevice> mxDropDown;

    std::vector<ScCheckListMember> maMembers;
    // For Dates
    std::map<OUString, size_t>    maYearMonthMap;

    std::unique_ptr<ExtendedData> mxExtendedData;
    std::unique_ptr<Action>       mxOKAction;
    std::unique_ptr<Action>       mxPopupEndAction;

    Config maConfig;
    Size maAllocatedSize;
    int mnCheckWidthReq; /// matching width request for mxChecks
    int mnWndWidth;  /// whole window width.
    int mnCheckListVisibleRows;
    TriState mePrevToggleAllState;

    size_t  mnSelectedMenu;

    ScViewData& mrViewData;

    ImplSVEvent* mnAsyncPostPopdownId;
    ImplSVEvent* mnAsyncSetDropdownPosId;

    bool mbHasDates;
    bool mbIsPoppedUp;

    struct SubMenuItemData
    {
        Timer                   maTimer;
        ScListSubMenuControl*   mpSubMenu;
        size_t                  mnMenuPos;

        DECL_LINK( TimeoutHdl, Timer*, void );

        SubMenuItemData(ScCheckListMenuControl* pParent);
        void reset();

    private:
        ScCheckListMenuControl* mpParent;
    };

    SubMenuItemData   maOpenTimer;
    SubMenuItemData   maCloseTimer;
};

class ScListSubMenuControl final
{
public:
    ScListSubMenuControl(weld::Widget* pParent, ScCheckListMenuControl& rParentControl, bool bColorMenu);

    void setPopupStartAction(ScCheckListMenuControl::Action* p);

    void GrabFocus();
    bool IsVisible() const;

    void StartPopupMode(weld::Widget* pParent, const tools::Rectangle& rRect);
    void EndPopupMode();

    void addMenuItem(const OUString& rText, ScCheckListMenuControl::Action* pAction);
    // nMenu of 0 for background color, nMenu of 1 for text color
    void addMenuColorItem(const OUString& rText, bool bActive, VirtualDevice& rImage,
                          int nMenu, ScCheckListMenuControl::Action* pAction);
    void addSeparator();
    void clearMenuItems();
    void resizeToFitMenuItems();

    ScViewData& GetViewData() const { return mrParentControl.GetViewData(); }
    ScCheckListMenuControl::ExtendedData* getExtendedData() { return mrParentControl.getExtendedData(); }
    VclPtr<VirtualDevice> create_virtual_device() const { return mxMenu->create_virtual_device(); }

    /**
     * Dismiss all visible popup menus and set focus back to the application
     * window.  This method is called e.g. when a menu action is fired.
     */
    void terminateAllPopupMenus();

private:
    std::unique_ptr<weld::Builder> mxBuilder;
    std::unique_ptr<weld::Popover> mxPopover;
    std::unique_ptr<weld::Container> mxContainer;
    std::unique_ptr<weld::TreeView> mxMenu;
    std::unique_ptr<weld::TreeView> mxBackColorMenu;
    std::unique_ptr<weld::TreeView> mxTextColorMenu;
    std::unique_ptr<weld::TreeIter> mxScratchIter;
    std::unique_ptr<ScCheckListMenuControl::Action> mxPopupStartAction;
    std::vector<ScCheckListMenuControl::MenuItemData> maMenuItems;
    ScCheckListMenuControl& mrParentControl;
    int mnBackColorMenuPrefHeight;
    int mnTextColorMenuPrefHeight;
    bool mbColorMenu;

    DECL_LINK(RowActivatedHdl, weld::TreeView& rMEvt, bool);
    DECL_LINK(ColorSelChangedHdl, weld::TreeView&, void);
    DECL_LINK(MenuKeyInputHdl, const KeyEvent&, bool);

    void SetupMenu(weld::TreeView& rMenu);

    void executeMenuItem(ScCheckListMenuControl::Action* pAction);
    void addItem(ScCheckListMenuControl::Action* pAction);
};

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