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