GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/guitable.h Lines: 3 3 100.0 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef GUI_WIDGETS_GUITABLE_H
24
#define GUI_WIDGETS_GUITABLE_H
25
26
#include "localconsts.h"
27
28
#include "enums/simpletypes/opaque.h"
29
30
#include "listeners/keylistener.h"
31
#include "listeners/mouselistener.h"
32
#include "listeners/tablemodellistener.h"
33
34
#include "gui/widgets/widget.h"
35
36
class GuiTableActionListener;
37
class TableModel;
38
39
/**
40
 * A table, with rows and columns made out of sub-widgets. Largely inspired by
41
 * (and can be thought of as a generalisation of) the guichan listbox
42
 * implementation.
43
 *
44
 * Normally you want this within a ScrollArea.
45
 *
46
 * \ingroup GUI
47
 */
48
class GuiTable final : public Widget,
49
                       public MouseListener,
50
                       public KeyListener,
51
                       public TableModelListener
52
{
53
    // so that the action listener can call distributeActionEvent
54
    friend class GuiTableActionListener;
55
56
    public:
57
        GuiTable(const Widget2 *const widget,
58
                 TableModel *const initial_model,
59
                 const Opaque opacity = Opaque_true);
60
61
        A_DELETE_COPY(GuiTable)
62
63
        ~GuiTable();
64
65
        /**
66
         * Sets the table model
67
         *
68
         * Note that actions issued by widgets returned from the model will
69
         * update the table selection, but only AFTER any event handlers
70
         * installed within the widget have been triggered. To be notified
71
         * after such an update, add an action listener to the table instead.
72
         */
73
        void setModel(TableModel *const m);
74
75
        /**
76
         * Retrieves the active table model
77
         */
78
        const TableModel *getModel() const A_WARN_UNUSED RETURNS_NONNULL;
79
80
        void setSelected(const int row, const int column);
81
82
        int getSelectedRow() const A_WARN_UNUSED;
83
84
        int getSelectedColumn() const A_WARN_UNUSED;
85
86
        void setSelectedRow(const int selected);
87
88
        void setSelectedColumn(const int selected);
89
90
        bool isWrappingEnabled() const noexcept2 A_WARN_UNUSED
91
        { return mWrappingEnabled; }
92
93
        void setWrappingEnabled(bool wrappingEnabled)
94
        { mWrappingEnabled = wrappingEnabled; }
95
96
        Rect getChildrenArea() override final A_WARN_UNUSED;
97
98
        /**
99
         * Toggle whether to use linewise selection mode, in which the table
100
         * selects an entire line at a time, rather than a single cell.
101
         *
102
         * Note that column information is tracked even in linewise selection
103
         * mode;
104
         *
105
         * this mode therefore only affects visualisation.
106
         *
107
         * Disabled by default.
108
         *
109
         * \param linewise: Whether to enable linewise selection mode
110
         */
111
        void setLinewiseSelection(bool linewise)
112
8
        { mLinewiseMode = linewise; }
113
114
        // Inherited from Widget
115
        void draw(Graphics *const graphics) override final A_NONNULL(2);
116
117
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
118
119
        Widget *getWidgetAt(int x, int y) override final A_WARN_UNUSED;
120
121
        void moveToTop(Widget *const widget) override final;
122
123
        void moveToBottom(Widget *const widget) override final;
124
125
        void setFocusHandler(FocusHandler *const focusHandler) override final;
126
127
        // Inherited from KeyListener
128
        void keyPressed(KeyEvent& event) override final;
129
130
        /**
131
         * Sets the table to be opaque, that is sets the table
132
         * to display its background.
133
         *
134
         * @param opaque True if the table should be opaque, false otherwise.
135
         */
136
        void setOpaque(Opaque opaque)
137
4
        { mOpaque = opaque; }
138
139
        /**
140
         * Checks if the table is opaque, that is if the table area displays
141
         * its background.
142
         *
143
         * @return True if the table is opaque, false otherwise.
144
         */
145
        bool isOpaque() const noexcept2 A_WARN_UNUSED
146
        { return mOpaque == Opaque_true; }
147
148
        // Inherited from MouseListener
149
        void mousePressed(MouseEvent& event) override final;
150
151
        void mouseWheelMovedUp(MouseEvent& event) override final;
152
153
        void mouseWheelMovedDown(MouseEvent& event) override final;
154
155
        void mouseDragged(MouseEvent& event) override final;
156
157
        // Constraints inherited from TableModelListener
158
        void modelUpdated(const bool completed) override final;
159
160
        void requestFocus() override;
161
162
        void setSelectableGui(bool b)
163
4
        { mSelectableGui = b; }
164
165
    protected:
166
        /** Frees all action listeners on inner widgets. */
167
        void uninstallActionListeners();
168
169
        /** Installs all action listeners on inner widgets. */
170
        void installActionListeners();
171
172
        int getRowHeight() const A_WARN_UNUSED;
173
174
        int getColumnWidth(const int i) const A_WARN_UNUSED;
175
176
    private:
177
        int getRowForY(const int y) const A_WARN_UNUSED;     // -1 on error
178
179
        int getColumnForX(const int x) const A_WARN_UNUSED;  // -1 on error
180
181
        void recomputeDimensions();
182
183
        static float mAlpha;
184
185
        TableModel *mModel A_NONNULLPOINTER;
186
187
        /** If someone moves a fresh widget to the top, we must display it. */
188
        Widget *mTopWidget;
189
190
        /** Vector for compactness; used as a list in practice. */
191
        STD_VECTOR<GuiTableActionListener *> mActionListeners2;
192
193
        /**
194
         * Holds the background color of the table.
195
         */
196
        Color mHighlightColor;
197
198
        int mSelectedRow;
199
        int mSelectedColumn;
200
201
        bool mLinewiseMode;
202
        bool mWrappingEnabled;
203
        Opaque mOpaque;
204
        bool mSelectableGui;
205
};
206
207
#endif  // GUI_WIDGETS_GUITABLE_H