GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabstrip.cpp Lines: 19 51 37.3 %
Date: 2018-09-09 Branches: 8 40 20.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/widgets/tabstrip.h"
22
23
#include "gui/widgets/button.h"
24
25
#include "utils/foreach.h"
26
27
#include "debug.h"
28
29
3
TabStrip::TabStrip(const Widget2 *const widget,
30
                   const std::string &group,
31
                   const int height,
32
3
                   const int spacing) :
33
    WidgetGroup(widget, group, height, spacing),
34
3
    mPressFirst(true)
35
{
36
3
    mAllowLogic = false;
37
3
}
38
39
2
TabStrip::TabStrip(const Widget2 *const widget,
40
                   const int height,
41
2
                   const int spacing) :
42
    WidgetGroup(widget, "", height, spacing),
43
8
    mPressFirst(true)
44
{
45
2
    mAllowLogic = false;
46
2
}
47
48
24
Widget *TabStrip::createWidget(const std::string &text,
49
                               const bool pressed) const
50
{
51
    Button *const widget = new Button(this,
52
24
        BUTTON_SKIN);
53
24
    widget->setStick(true);
54
24
    widget->setCaption(text);
55
24
    widget->adjustSize();
56

24
    if (((mCount == 0) && mPressFirst) || pressed)
57
        widget->setPressed(true);
58
72
    widget->setTag(CAST_S32(mWidgets.size()));
59
24
    return widget;
60
}
61
62
void TabStrip::action(const ActionEvent &event)
63
{
64
    WidgetGroup::action(event);
65
    if (event.getSource() != nullptr)
66
    {
67
        Widget *const widget = event.getSource();
68
        Button *const button = static_cast<Button*>(widget);
69
        if (button == nullptr)
70
            return;
71
        if (button->isPressed2())
72
        {
73
            FOR_EACH (WidgetListConstIterator, iter, mWidgets)
74
            {
75
                if (*iter != widget)
76
                {
77
                    Button *const button2 = static_cast<Button*>(*iter);
78
                    button2->setPressed(false);
79
                }
80
            }
81
        }
82
        else
83
        {
84
            button->setPressed(true);
85
        }
86
    }
87
}
88
89
void TabStrip::nextTab()
90
{
91
    FOR_EACH (WidgetListConstIterator, iter, mWidgets)
92
    {
93
        Button *button = static_cast<Button*>(*iter);
94
        if (button->isPressed2())
95
        {
96
            button->setPressed(false);
97
            ++iter;
98
            if (iter == mWidgets.end())
99
                iter = mWidgets.begin();
100
            button = static_cast<Button*>(*iter);
101
            action(ActionEvent(button, button->getActionEventId()));
102
            return;
103
        }
104
    }
105
}
106
107
void TabStrip::prevTab()
108
{
109
    FOR_EACH (WidgetListConstIterator, iter, mWidgets)
110
    {
111
        Button *button = static_cast<Button*>(*iter);
112
        if (button->isPressed2())
113
        {
114
            button->setPressed(false);
115
            if (iter == mWidgets.begin())
116
                iter = mWidgets.end();
117
            if (iter == mWidgets.begin())
118
                return;
119
            --iter;
120
            button = static_cast<Button*>(*iter);
121
            action(ActionEvent(button, button->getActionEventId()));
122
            return;
123
        }
124
    }
125

3
}