GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/scrollarea.h Lines: 4 4 100.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
/*      _______   __   __   __   ______   __   __   _______   __   __
25
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
26
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
28
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
29
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31
 *
32
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33
 *
34
 *
35
 * Per Larsson a.k.a finalman
36
 * Olof Naessén a.k.a jansem/yakslem
37
 *
38
 * Visit: http://guichan.sourceforge.net
39
 *
40
 * License: (BSD)
41
 * Redistribution and use in source and binary forms, with or without
42
 * modification, are permitted provided that the following conditions
43
 * are met:
44
 * 1. Redistributions of source code must retain the above copyright
45
 *    notice, this list of conditions and the following disclaimer.
46
 * 2. Redistributions in binary form must reproduce the above copyright
47
 *    notice, this list of conditions and the following disclaimer in
48
 *    the documentation and/or other materials provided with the
49
 *    distribution.
50
 * 3. Neither the name of Guichan nor the names of its contributors may
51
 *    be used to endorse or promote products derived from this software
52
 *    without specific prior written permission.
53
 *
54
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65
 */
66
67
#ifndef GUI_WIDGETS_SCROLLAREA_H
68
#define GUI_WIDGETS_SCROLLAREA_H
69
70
#include "gui/widgets/basiccontainer.h"
71
72
#include "enums/simpletypes/opaque.h"
73
74
#include "listeners/mouselistener.h"
75
#include "listeners/widgetlistener.h"
76
77
#include "localconsts.h"
78
79
class Image;
80
class ImageCollection;
81
82
/**
83
 * A scroll area.
84
 *
85
 * Contrary to Guichan's scroll area, this scroll area takes ownership over its
86
 * content. However, it won't delete a previously set content widget when
87
 * setContent is called!
88
 *
89
 * \ingroup GUI
90
 */
91
class ScrollArea final : public BasicContainer,
92
                         public MouseListener,
93
                         public WidgetListener
94
{
95
    public:
96
        /**
97
         * Scrollpolicies for the horizontal and vertical scrollbar.
98
         * The policies are:
99
         *
100
         * SHOW_ALWAYS - Always show the scrollbars no matter what.
101
         * SHOW_NEVER  - Never show the scrollbars no matter waht.
102
         * SHOW_AUTO   - Show the scrollbars only when needed. That is if the
103
         *               content grows larger then the ScrollArea.
104
         */
105
        enum ScrollPolicy
106
        {
107
            SHOW_ALWAYS = 0,
108
            SHOW_NEVER,
109
            SHOW_AUTO
110
        };
111
112
        ScrollArea(Widget2 *const widget2,
113
                   Widget *const widget,
114
                   const Opaque opaque,
115
                   const std::string &skin);
116
117
        A_DELETE_COPY(ScrollArea)
118
119
        /**
120
         * Destructor. Also deletes the content.
121
         */
122
        ~ScrollArea() override final;
123
124
        /**
125
         * Logic function optionally adapts width or height of contents. This
126
         * depends on the scrollbar settings.
127
         */
128
        void logic() override final;
129
130
        /**
131
         * Update the alpha value to the graphic components.
132
         */
133
        void updateAlpha();
134
135
        /**
136
         * Draws the scroll area.
137
         */
138
        void draw(Graphics *const graphics) override final A_NONNULL(2);
139
140
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
141
142
        /**
143
         * Draws the background and border of the scroll area.
144
         */
145
        void drawFrame(Graphics *const graphics) override final A_NONNULL(2);
146
147
        void safeDrawFrame(Graphics *const graphics) override final
148
                           A_NONNULL(2);
149
150
        /**
151
         * Sets whether the widget should draw its background or not.
152
         */
153
        void setOpaque(Opaque opaque);
154
155
        /**
156
         * Returns whether the widget draws its background or not.
157
         */
158
        bool isOpaque() const noexcept2 A_WARN_UNUSED
159
        { return mOpaque == Opaque_true; }
160
161
        /**
162
         * Called when the mouse moves in the widget area.
163
         */
164
        void mouseMoved(MouseEvent& event) override final;
165
166
        /**
167
         * Called when the mouse enteres the widget area.
168
         */
169
        void mouseEntered(MouseEvent& event) override final;
170
171
        /**
172
         * Called when the mouse leaves the widget area.
173
         */
174
        void mouseExited(MouseEvent& event) override final;
175
176
        void mousePressed(MouseEvent& event) override final;
177
178
        void mouseReleased(MouseEvent& event) override final;
179
180
        void mouseDragged(MouseEvent &event) override final;
181
182
        void widgetResized(const Event &event) override final;
183
184
        void widgetMoved(const Event &event) override final;
185
186
        Rect getVerticalBarDimension() const;
187
188
        Rect getHorizontalBarDimension() const;
189
190
        Rect getVerticalMarkerDimension();
191
192
        Rect getHorizontalMarkerDimension();
193
194
        Rect getUpButtonDimension() const;
195
196
        Rect getDownButtonDimension() const;
197
198
        Rect getLeftButtonDimension() const;
199
200
        Rect getRightButtonDimension() const;
201
202
        /**
203
         * Sets the content.
204
         *
205
         * @param widget The content of the scroll area.
206
         */
207
        void setContent(Widget* widget);
208
209
        /**
210
         * Gets the content.
211
         *
212
         * @return The content of the scroll area.
213
         */
214
        Widget* getContent();
215
216
        /**
217
         * Sets the horizontal scrollbar policy. See enum with policies.
218
         *
219
         * @param hPolicy The policy for the horizontal scrollbar.
220
         * @see getHorizontalScrollPolicy
221
         */
222
        void setHorizontalScrollPolicy(const ScrollPolicy hPolicy);
223
224
        /**
225
         * Gets the horizontal scrollbar policy. See enum with policies.
226
         *
227
         * @return The policy for the horizontal scrollbar policy.
228
         * @see setHorizontalScrollPolicy, setScrollPolicy
229
         */
230
        ScrollPolicy getHorizontalScrollPolicy() const
231
        { return mHPolicy; }
232
233
        /**
234
         * Sets the vertical scrollbar policy. See enum with policies.
235
         *
236
         * @param vPolicy The policy for the vertical scrollbar.
237
         * @see getVerticalScrollPolicy
238
         */
239
        void setVerticalScrollPolicy(const ScrollPolicy vPolicy);
240
241
        /**
242
         * Gets the vertical scrollbar policy. See enum with policies.
243
         *
244
         * @return The policy for the vertical scrollbar.
245
         * @see setVerticalScrollPolicy, setScrollPolicy
246
         */
247
        ScrollPolicy getVerticalScrollPolicy() const
248
        { return mVPolicy; }
249
250
        /**
251
         * Sets the horizontal and vertical scrollbar policy.
252
         *
253
         * @param hPolicy The policy for the horizontal scrollbar.
254
         * @param vPolicy The policy for the vertical scrollbar.
255
         * @see getVerticalScrollPolicy, getHorizontalScrollPolicy
256
         */
257
        void setScrollPolicy(const ScrollPolicy hPolicy,
258
                             const ScrollPolicy vPolicy);
259
260
        /**
261
         * Sets the amount to scroll vertically.
262
         *
263
         * @param vScroll The amount to scroll.
264
         * @see getVerticalScrollAmount
265
         */
266
        void setVerticalScrollAmount(const int vScroll);
267
268
        /**
269
         * Gets the amount that is scrolled vertically.
270
         *
271
         * @return The scroll amount on vertical scroll.
272
         * @see setVerticalScrollAmount, setScrollAmount
273
         */
274
        int getVerticalScrollAmount() const
275
        { return mVScroll; }
276
277
        /**
278
         * Sets the amount to scroll horizontally.
279
         *
280
         * @param hScroll The amount to scroll.
281
         * @see getHorizontalScrollAmount
282
         */
283
        void setHorizontalScrollAmount(int hScroll);
284
285
        /**
286
         * Gets the amount that is scrolled horizontally.
287
         *
288
         * @return The scroll amount on horizontal scroll.
289
         * @see setHorizontalScrollAmount, setScrollAmount
290
         */
291
        int getHorizontalScrollAmount() const
292
        { return mHScroll; }
293
294
        /**
295
         * Sets the amount to scroll horizontally and vertically.
296
         *
297
         * @param hScroll The amount to scroll on horizontal scroll.
298
         * @param vScroll The amount to scroll on vertical scroll.
299
         * @see getHorizontalScrollAmount, getVerticalScrollAmount
300
         */
301
        void setScrollAmount(const int hScroll, const int vScroll);
302
303
        /**
304
         * Gets the maximum amount of horizontal scroll.
305
         *
306
         * @return The horizontal max scroll.
307
         */
308
        int getHorizontalMaxScroll();
309
310
        /**
311
         * Gets the maximum amount of vertical scroll.
312
         *
313
         * @return The vertical max scroll.
314
         */
315
        int getVerticalMaxScroll();
316
317
        /**
318
         * Sets the width of the scroll bars.
319
         *
320
         * @param width The width of the scroll bars.
321
         * @see getScrollbarWidth
322
         */
323
        void setScrollbarWidth(const int width);
324
325
        /**
326
         * Gets the width of the scroll bars.
327
         *
328
         * @return the width of the ScrollBar.
329
         * @see setScrollbarWidth
330
         */
331
        int getScrollbarWidth() const
332
        { return mScrollbarWidth; }
333
334
        /**
335
         * Sets the amount to scroll in pixels when the left scroll button is
336
         * pushed.
337
         *
338
         * @param amount The amount to scroll in pixels.
339
         * @see getLeftButtonScrollAmount
340
         */
341
        void setLeftButtonScrollAmount(const int amount)
342
164
        { mLeftButtonScrollAmount = amount; }
343
344
        /**
345
         * Sets the amount to scroll in pixels when the right scroll button is
346
         * pushed.
347
         *
348
         * @param amount The amount to scroll in pixels.
349
         * @see getRightButtonScrollAmount
350
         */
351
        void setRightButtonScrollAmount(const int amount)
352
164
        { mRightButtonScrollAmount = amount; }
353
354
        /**
355
         * Sets the amount to scroll in pixels when the up scroll button is
356
         * pushed.
357
         *
358
         * @param amount The amount to scroll in pixels.
359
         * @see getUpButtonScrollAmount
360
         */
361
        void setUpButtonScrollAmount(const int amount)
362
164
        { mUpButtonScrollAmount = amount; }
363
364
        /**
365
         * Sets the amount to scroll in pixels when the down scroll button is
366
         * pushed.
367
         *
368
         * @param amount The amount to scroll in pixels.
369
         * @see getDownButtonScrollAmount
370
         */
371
        void setDownButtonScrollAmount(const int amount)
372
164
        { mDownButtonScrollAmount = amount; }
373
374
        /**
375
         * Gets the amount to scroll in pixels when the left scroll button is
376
         * pushed.
377
         *
378
         * @return The amount to scroll in pixels.
379
         * @see setLeftButtonScrollAmount
380
         */
381
        int getLeftButtonScrollAmount() const
382
        { return mLeftButtonScrollAmount; }
383
384
        /**
385
         * Gets the amount to scroll in pixels when the right scroll button is
386
         * pushed.
387
         *
388
         * @return The amount to scroll in pixels.
389
         * @see setRightButtonScrollAmount
390
         */
391
        int getRightButtonScrollAmount() const
392
        { return mRightButtonScrollAmount; }
393
394
        /**
395
         * Gets the amount to scroll in pixels when the up scroll button is
396
         * pushed.
397
         *
398
         * @return The amount to scroll in pixels.
399
         * @see setUpButtonScrollAmount
400
         */
401
        int getUpButtonScrollAmount() const
402
        { return mUpButtonScrollAmount; }
403
404
        /**
405
         * Gets the amount to scroll in pixels when the down scroll button is
406
         * pushed.
407
         *
408
         * @return The amount to scroll in pixels.
409
         * @see setDownButtonScrollAmount
410
         */
411
        int getDownButtonScrollAmount() const
412
        { return mDownButtonScrollAmount; }
413
414
        void showWidgetPart(Widget *const widget,
415
                            const Rect &area) override final;
416
417
        Rect getChildrenArea() override final;
418
419
        Widget *getWidgetAt(int x, int y) override final;
420
421
        void setWidth(int width);
422
423
        void setHeight(int height);
424
425
        void setDimension(const Rect& dimension);
426
427
        void mouseWheelMovedUp(MouseEvent& event) override final;
428
429
        void mouseWheelMovedDown(MouseEvent& event) override final;
430
431
        bool isSelectable() const noexcept2 override final A_WARN_UNUSED;
432
433
    protected:
434
        enum BUTTON_DIR
435
        {
436
            UP = 0,
437
            DOWN,
438
            LEFT,
439
            RIGHT,
440
            BUTTONS_DIR
441
        };
442
443
        /**
444
         * Initializes the scroll area.
445
         */
446
        void init(std::string skinName);
447
448
        /**
449
         * Checks the policies for the scroll bars.
450
         */
451
        void checkPolicies();
452
453
        void drawButton(Graphics *const graphics,
454
                        const BUTTON_DIR dir) A_NONNULL(2);
455
        void calcButton(Graphics *const graphics,
456
                        const BUTTON_DIR dir) A_NONNULL(2);
457
        void drawVBar(Graphics *const graphics) const A_NONNULL(2);
458
        void drawHBar(Graphics *const graphics) const A_NONNULL(2);
459
        void drawVMarker(Graphics *const graphics) A_NONNULL(2);
460
        void drawHMarker(Graphics *const graphics) A_NONNULL(2);
461
462
        void calcVBar(const Graphics *const graphics) A_NONNULL(2);
463
        void calcHBar(const Graphics *const graphics) A_NONNULL(2);
464
        void calcVMarker(Graphics *const graphics) A_NONNULL(2);
465
        void calcHMarker(Graphics *const graphics) A_NONNULL(2);
466
467
        Image *getImageByState(Rect &dim, const BUTTON_DIR dir);
468
469
        void updateCalcFlag(const Graphics *const graphics) A_NONNULL(2);
470
471
        static int instances;
472
        static float mAlpha;
473
        static bool mShowButtons;
474
        static int mMarkerSize;
475
        static int mScrollbarSize;
476
        static ImageRect background;
477
        static ImageRect vMarker;
478
        static ImageRect vMarkerHi;
479
        static ImageRect vBackground;
480
        static ImageRect hBackground;
481
        static Image *buttons[4][2];
482
483
        ImageCollection *mVertexes A_NONNULLPOINTER;
484
        ImageCollection *mVertexes2 A_NONNULLPOINTER;
485
486
        /**
487
         * Holds the horizontal scroll bar policy.
488
         */
489
        ScrollPolicy mHPolicy;
490
491
        /**
492
         * Holds the vertical scroll bar policy.
493
         */
494
        ScrollPolicy mVPolicy;
495
496
        /**
497
         * Holds the vertical scroll amount.
498
         */
499
        int mVScroll;
500
501
        /**
502
         * Holds the horizontal scroll amount.
503
         */
504
        int mHScroll;
505
506
        /**
507
         * Holds the width of the scroll bars.
508
         */
509
        int mScrollbarWidth;
510
511
        /**
512
         * Holds the up button scroll amount.
513
         */
514
        int mUpButtonScrollAmount;
515
516
        /**
517
         * Holds the down button scroll amount.
518
         */
519
        int mDownButtonScrollAmount;
520
521
        /**
522
         * Holds the left button scroll amount.
523
         */
524
        int mLeftButtonScrollAmount;
525
526
        /**
527
         * Holds the right button scroll amount.
528
         */
529
        int mRightButtonScrollAmount;
530
531
        /**
532
         * Holds the horizontal markers drag offset.
533
         */
534
        int mHorizontalMarkerDragOffset;
535
536
        /**
537
         * Holds the vertical markers drag offset.
538
         */
539
        int mVerticalMarkerDragOffset;
540
541
        int mX;
542
        int mY;
543
        int mClickX;
544
        int mClickY;
545
        int mXOffset;
546
        int mYOffset;
547
        int mDrawWidth;
548
        int mDrawHeight;
549
550
        /**
551
         * True if the vertical scroll bar is visible, false otherwise.
552
         */
553
        bool mVBarVisible;
554
555
        /**
556
         * True if the horizontal scroll bar is visible, false otherwise.
557
         */
558
        bool mHBarVisible;
559
560
        /**
561
         * True if the up button is pressed, false otherwise.
562
         */
563
        bool mUpButtonPressed;
564
565
        /**
566
         * True if the down button is pressed, false otherwise.
567
         */
568
        bool mDownButtonPressed;
569
570
        /**
571
         * True if the left button is pressed, false otherwise.
572
         */
573
        bool mLeftButtonPressed;
574
575
        /**
576
         * True if the right button is pressed, false otherwise.
577
         */
578
        bool mRightButtonPressed;
579
580
        /**
581
         * True if the vertical marked is dragged.
582
         */
583
        bool mIsVerticalMarkerDragged;
584
585
        /**
586
         * True if the horizontal marked is dragged.
587
         */
588
        bool mIsHorizontalMarkerDragged;
589
590
        /**
591
         * True if the scroll area should be opaque (that is
592
         * display its background), false otherwise.
593
         */
594
        Opaque mOpaque;
595
596
        bool mHasMouse;
597
};
598
599
#endif  // GUI_WIDGETS_SCROLLAREA_H