File: clControlWithItems.h

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (238 lines) | stat: -rw-r--r-- 8,158 bytes parent folder | download | duplicates (2)
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
#ifndef CLCONTROLWITHITEMS_H
#define CLCONTROLWITHITEMS_H

#include "clColours.h"
#include "clHeaderBar.h"
#include "clRowEntry.h"
#include "clScrolledPanel.h"

#include <array>
#include <memory>
#include <wx/imaglist.h>

#ifdef __WXOSX__
#define SCROLL_TICK 2
#else
#define SCROLL_TICK 2
#endif

class clSearchControl;
class clControlWithItems;
class clRowEntry;

// Search flags
#define wxTR_SEARCH_METHOD_EXACT (1 << 0)    // Use an exact string comparison method
#define wxTR_SEARCH_METHOD_CONTAINS (1 << 1) // Compare using wxString::Contains method
#define wxTR_SEARCH_VISIBLE_ITEMS (1 << 2)   // Search reachable items (i.e. they can be visible if scrolled enough)
#define wxTR_SEARCH_ICASE (1 << 3)           // Search incase-sensitive
#define wxTR_SEARCH_INCLUDE_CURRENT_ITEM \
    (1 << 4) // When calling the search API, FindNext/FindPrev include the 'starting' item
#define wxTR_SEARCH_DEFAULT \
    (wxTR_SEARCH_METHOD_CONTAINS | wxTR_SEARCH_VISIBLE_ITEMS | wxTR_SEARCH_ICASE | wxTR_SEARCH_INCLUDE_CURRENT_ITEM)

class WXDLLIMPEXP_SDK clSearchText
{
    bool m_enabled = false;

public:
    static bool Matches(const wxString& findWhat, size_t col, const wxString& text,
                        size_t searchFlags = wxTR_SEARCH_DEFAULT, clMatchResult* matches = nullptr);
    clSearchText();
    virtual ~clSearchText();
    void SetEnabled(bool enabled) { this->m_enabled = enabled; }
    bool IsEnabled() const { return m_enabled; }
};

/// base class for custom row renderers
class WXDLLIMPEXP_SDK clControlWithItemsRowRenderer
{
public:
    clControlWithItemsRowRenderer() {}
    virtual ~clControlWithItemsRowRenderer() {}

    /**
     * @brief override this method to provide a custom row drawing.
     * The text, bitmap and other info that needs to be drawn are stored
     * in the `entry` field.
     */
    virtual void RenderItem(wxWindow* window, wxDC& dc, const clColours& colours, int row_index, clRowEntry* entry) = 0;

    /**
     * @brief draw the background of a given entry
     */
    virtual void RenderItemBackground(wxDC& dc, long tree_style, const clColours& colours, int row_index,
                                      clRowEntry* entry) = 0;

    /**
     * @brief render the list background
     */
    virtual void RenderBackground(wxDC& dc, const wxRect& rect, long tree_style, const clColours& colours) = 0;
};

class WXDLLIMPEXP_SDK clControlWithItems : public clScrolledPanel
{
public:
    typedef std::vector<wxBitmap> BitmapVec_t;

protected:
    clHeaderBar* m_viewHeader = nullptr;
    clColours m_colours;
    clRowEntry* m_firstItemOnScreen = nullptr;
    int m_firstColumn = 0;
    int m_lineHeight = 0;
    int m_indent = 0;
    BitmapVec_t* m_bitmaps = nullptr;
    BitmapVec_t* m_bitmapsInternal = nullptr;
    int m_scrollTick = SCROLL_TICK;
    clSearchText m_search;
    clSearchControl* m_searchControl = nullptr;
    bool m_maxList = false;
    bool m_nativeTheme = false;
    std::unique_ptr<clControlWithItemsRowRenderer> m_customRenderer;
    wxFont m_defaultFont = wxNullFont;
    bool m_recalcColumnWidthOnPaint = true;
    bool m_disableView = false;

protected:
    void DoInitialize();
    int GetNumLineCanFitOnScreen(bool fully_fit = false) const;
    virtual clRowEntry* GetFirstItemOnScreen();
    virtual void SetFirstItemOnScreen(clRowEntry* item);
    void RenderItems(wxDC& dc, long tree_style, const clRowEntry::Vec_t& items);
    void AssignRects(const clRowEntry::Vec_t& items);
    void OnSize(wxSizeEvent& event);
    void DoUpdateHeader(clRowEntry* row);
    wxSize GetTextSize(const wxString& label) const;
    virtual void OnMouseScroll(wxMouseEvent& event);
    virtual bool DoKeyDown(const wxKeyEvent& event);
    virtual void DoMouseScroll(const wxMouseEvent& event);
    clSearchText& GetSearch() { return m_search; }
    const clSearchText& GetSearch() const { return m_search; }

    void DoPositionHScrollbar();
    void DoPositionVScrollbar();

public:
    clControlWithItems(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
                       const wxSize& size = wxDefaultSize, long style = 0);
    virtual ~clControlWithItems();
    clControlWithItems();

    virtual void SetDefaultFont(const wxFont& font);
    virtual wxFont GetDefaultFont() const;

    /**
     * @brief give the view a "disabled" look and feel
     */
    void SetDisabled(bool b);
    bool IsDisabled() const { return m_disableView; }

    /**
     * @brief set a custom renderer to draw the rows for this control
     * `this` takes ownership for the renderer (i.e. it will free it)
     */
    void SetCustomRenderer(clControlWithItemsRowRenderer* renderer);

    void SetNativeTheme(bool nativeTheme);
    bool IsNativeTheme() const { return m_nativeTheme; }
    bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
                const wxSize& size = wxDefaultSize, long style = 0);
    virtual int GetIndent() const { return m_indent; }

    virtual void SetFirstColumn(int firstColumn) { this->m_firstColumn = firstColumn; }
    virtual int GetFirstColumn() const { return m_firstColumn; }

    virtual void SetLineHeight(int lineHeight) { this->m_lineHeight = lineHeight; }
    virtual int GetLineHeight() const { return m_lineHeight; }

    virtual void SetBitmaps(BitmapVec_t* bitmaps) { this->m_bitmaps = bitmaps; }
    virtual void SetImageList(wxImageList* images);
    virtual const BitmapVec_t* GetBitmaps() const { return m_bitmaps; }
    virtual BitmapVec_t* GetBitmaps() { return m_bitmaps; }

    void SetScrollTick(int scrollTick) { this->m_scrollTick = scrollTick; }
    int GetScrollTick() const { return m_scrollTick; }

    /**
     * @brief return bitmap at a given index
     */
    const wxBitmap& GetBitmap(size_t index) const;

    /**
     * @brief set the item's indent size
     */
    virtual void SetIndent(int size) { m_indent = size; }

    /**
     * @brief return the items rect area, excluding header
     */
    wxRect GetItemsRect() const;

    /**
     * @brief draw the header + items
     */
    void Render(wxDC& dc);

    /**
     * @brief Get a pointer to the header, create one if needed
     */
    clHeaderBar* GetHeader() const;

    /**
     * @brief sets a column width. If the width is less than 0, this function does nothing. If the column is out of
     * bound this function does nothing. Two sepcial values are allowed here: wxCOL_WIDTH_DEFAULT and
     * wxCOL_WIDTH_AUTOSIZE
     * @param col the column index
     * @param width the width. Can contain one of the special values: wxCOL_WIDTH_DEFAULT and wxCOL_WIDTH_AUTOSIZE
     */
    void SetColumnWidth(size_t col, int width);

    /**
     * @brief should we show the header bar?
     */
    void SetShowHeader(bool b);
    /**
     * @brief is the heaer bar visible?
     */
    bool IsHeaderVisible() const;

    /**
     * @brief update the scrollbar with the current view status
     * subclass should call this method whenver the view changes (re-sized, items are expanding, collapsing etc)
     */
    virtual void UpdateScrollBar();

    void SetColours(const clColours& colours);

    const clColours& GetColours() const { return m_colours; }
    clColours& GetColours() { return m_colours; }

    // Horizontal scrolling implementation
    void ScollToColumn(int firstColumn);
    void ScrollColumns(int steps, wxDirection direction);

    //===-----------------------------------------
    //===-----------------------------------------

    /**
     * @brief override this
     */
    virtual bool IsEmpty() const = 0;
    /**
     * @brief return the total of numbers of items we can scroll
     * If the view has collpased items, the range _excludes_ them
     */
    virtual int GetRange() const = 0;

    /**
     * @brief return the row number of the first visible item in the view
     */
    virtual int GetFirstItemPosition() const = 0;

    void SearchControlDismissed();
};

wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_TREE_SEARCH_TEXT, wxTreeEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_TREE_CLEAR_SEARCH, wxTreeEvent);

#endif // CLCONTROLWITHITEMS_H