GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabstrip.cpp Lines: 19 51 37.3 %
Date: 2017-11-29 Branches: 9 42 21.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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
6
TabStrip::TabStrip(const Widget2 *const widget,
30
                   const std::string &group,
31
                   const int height,
32
6
                   const int spacing) :
33
    WidgetGroup(widget, group, height, spacing),
34
6
    mPressFirst(true)
35
{
36
6
    mAllowLogic = false;
37
6
}
38
39
4
TabStrip::TabStrip(const Widget2 *const widget,
40
                   const int height,
41
4
                   const int spacing) :
42
    WidgetGroup(widget, "", height, spacing),
43

16
    mPressFirst(true)
44
{
45
4
    mAllowLogic = false;
46
4
}
47
48
48
Widget *TabStrip::createWidget(const std::string &text,
49
                               const bool pressed) const
50
{
51
48
    Button *const widget = new Button(this);
52
48
    widget->setStick(true);
53
48
    widget->setCaption(text);
54
48
    widget->adjustSize();
55

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

6
}