summaryrefslogtreecommitdiff
path: root/src/gui/widgets/tabbedarea.cpp
blob: e0bf9e12fc8fe56cee82f9a047f75a3e216349f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  The ManaPlus Client
 *  Copyright (C) 2008-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gui/widgets/tabbedarea.h"

#include "gui/widgets/tab.h"

#include "log.h"

#include <guichan/widgets/container.hpp>

TabbedArea::TabbedArea() : gcn::TabbedArea(),
                           mTabsWidth(0),
                           mVisibleTabsWidth(0),
                           mTabScrollIndex(0)
{
    mWidgetContainer->setOpaque(false);
    addWidgetListener(this);

    mArrowButton[0] = new Button("<", "shift_left", this);
    mArrowButton[1] = new Button(">", "shift_right", this);

    add(mArrowButton[0]);
    add(mArrowButton[1]);

    widgetResized(NULL);
}

int TabbedArea::getNumberOfTabs() const
{
    return static_cast<int>(mTabs.size());
}

Tab *TabbedArea::getTab(const std::string &name) const
{
    TabContainer::const_iterator itr = mTabs.begin(), itr_end = mTabs.end();
    while (itr != itr_end)
    {
        if ((*itr).first->getCaption() == name)
            return static_cast<Tab*>((*itr).first);

        ++itr;
    }
    return NULL;
}

void TabbedArea::draw(gcn::Graphics *graphics)
{
    if (mTabs.empty())
        return;

    drawChildren(graphics);
}

gcn::Widget *TabbedArea::getWidget(const std::string &name) const
{
    TabContainer::const_iterator itr = mTabs.begin(), itr_end = mTabs.end();
    while (itr != itr_end)
    {
        if ((*itr).first->getCaption() == name)
            return (*itr).second;

        ++itr;
    }

    return NULL;
}

gcn::Widget *TabbedArea::getCurrentWidget()
{
    gcn::Tab *tab = getSelectedTab();

    if (tab)
        return getWidget(tab->getCaption());
    else
        return NULL;
}

void TabbedArea::addTab(gcn::Tab* tab, gcn::Widget* widget)
{
    if (!tab || !widget)
        return;

    gcn::TabbedArea::addTab(tab, widget);

    int width = getWidth() - 2 * getFrameSize();
    int height = getHeight() - 2 * getFrameSize() - mTabContainer->getHeight();
    widget->setSize(width, height);

    updateTabsWidth();
    updateArrowEnableState();
}

void TabbedArea::addTab(const std::string &caption, gcn::Widget *widget)
{
    Tab *tab = new Tab;
    tab->setCaption(caption);
    mTabsToDelete.push_back(tab);

    addTab(tab, widget);
}

void TabbedArea::removeTab(Tab *tab)
{
    int tabIndexToBeSelected = -1;

    if (tab == mSelectedTab)
    {
        int index = getSelectedTabIndex();

        if (index == static_cast<int>(mTabs.size()) - 1 && mTabs.size() == 1)
            tabIndexToBeSelected = -1;
        else
            tabIndexToBeSelected = index - 1;
    }

    TabContainer::iterator iter;
    for (iter = mTabs.begin(); iter != mTabs.end(); ++iter)
    {
        if (iter->first == tab)
        {
            mTabContainer->remove(tab);
            mTabs.erase(iter);
            break;
        }
    }

    std::vector<gcn::Tab*>::iterator iter2;
    for (iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); ++iter2)
    {
        if (*iter2 == tab)
        {
            mTabsToDelete.erase(iter2);
            delete tab;
            break;
        }
    }

    if (tabIndexToBeSelected >= (signed)mTabs.size())
        tabIndexToBeSelected = mTabs.size() - 1;
    if (tabIndexToBeSelected < -1)
        tabIndexToBeSelected = -1;

    if (tabIndexToBeSelected == -1)
    {
        mSelectedTab = 0;
        mWidgetContainer->clear();
    }
    else
    {
        setSelectedTab(tabIndexToBeSelected);
    }

    adjustSize();
    updateTabsWidth();
    adjustTabPositions();
}

void TabbedArea::logic()
{
    logicChildren();
}

void TabbedArea::mousePressed(gcn::MouseEvent &mouseEvent)
{
    if (mouseEvent.isConsumed())
        return;

    if (mouseEvent.getButton() == gcn::MouseEvent::LEFT)
    {
        gcn::Widget *widget = mTabContainer->getWidgetAt(mouseEvent.getX(),
                                                         mouseEvent.getY());
        gcn::Tab *tab = dynamic_cast<gcn::Tab*>(widget);

        if (tab)
        {
            setSelectedTab(tab);
            requestFocus();
        }
    }
}

void TabbedArea::setSelectedTab(gcn::Tab *tab)
{
    gcn::TabbedArea::setSelectedTab(tab);

    Tab *newTab = dynamic_cast<Tab*>(tab);

    if (newTab)
        newTab->setCurrent();

    widgetResized(NULL);
}

void TabbedArea::widgetResized(const gcn::Event &event _UNUSED_)
{
    int width = getWidth() - 2 * getFrameSize()
                - 2 * mWidgetContainer->getFrameSize();
    int height = getHeight() - 2 * getFrameSize() - mWidgetContainer->getY()
                 - 2 * mWidgetContainer->getFrameSize();
    mWidgetContainer->setSize(width, height);

    gcn::Widget *w = getCurrentWidget();
    if (w)
        w->setSize(width, height);

    // Check whether there is room to show more tabs now.
    int innerWidth = getWidth() - 4 - mArrowButton[0]->getWidth()
        - mArrowButton[1]->getWidth();
    int newWidth = mVisibleTabsWidth;
    while (mTabScrollIndex && newWidth < innerWidth)
    {
        newWidth += mTabs[mTabScrollIndex - 1].first->getWidth();
        if (newWidth < innerWidth)
            --mTabScrollIndex;
    }

    // Move the right arrow to fit the windows content.
    mArrowButton[1]->setPosition(width - mArrowButton[1]->getWidth(), 0);

    updateArrowEnableState();
    adjustTabPositions();
}

void TabbedArea::updateTabsWidth()
{
    mTabsWidth = 0;
    for (TabContainer::const_iterator itr = mTabs.begin(), itr_end = mTabs.end();
         itr != itr_end; ++itr)
    {
        mTabsWidth += (*itr).first->getWidth();
    }
    updateVisibleTabsWidth();
}

void TabbedArea::updateVisibleTabsWidth()
{
    mVisibleTabsWidth = 0;
    for (unsigned int i = mTabScrollIndex; i < mTabs.size(); ++i)
    {
        mVisibleTabsWidth += mTabs[i].first->getWidth();
    }
}

void TabbedArea::adjustTabPositions()
{
    int maxTabHeight = 0;
    for (unsigned i = 0; i < mTabs.size(); ++i)
    {
        if (mTabs[i].first->getHeight() > maxTabHeight)
        {
            maxTabHeight = mTabs[i].first->getHeight();
        }
    }

    int x = mArrowButton[0]->isVisible() ? mArrowButton[0]->getWidth() : 0;
    for (unsigned i = mTabScrollIndex; i < mTabs.size(); ++i)
    {
        gcn::Tab* tab = mTabs[i].first;
        tab->setPosition(x, maxTabHeight - tab->getHeight());
        x += tab->getWidth();
    }

    // If the tabs are scrolled, we hide them away.
    if (mTabScrollIndex > 0)
    {
        x = 0;
        for (unsigned i = 0; i < mTabScrollIndex; ++i)
        {
            gcn::Tab* tab = mTabs[i].first;
            x -= tab->getWidth();
            tab->setPosition(x, maxTabHeight - tab->getHeight());
        }
    }
}

void TabbedArea::action(const gcn::ActionEvent& actionEvent)
{
    Widget* source = actionEvent.getSource();
    Tab* tab = dynamic_cast<Tab*>(source);

    if (tab)
    {
        setSelectedTab(tab);
    }
    else
    {
        if (actionEvent.getId() == "shift_left")
        {
            if (mTabScrollIndex)
                --mTabScrollIndex;
        }
        else if (actionEvent.getId() == "shift_right")
        {
            if (mTabScrollIndex < mTabs.size() - 1)
                ++mTabScrollIndex;
        }
        adjustTabPositions();

        updateArrowEnableState();
    }
}

void TabbedArea::updateArrowEnableState()
{
    updateTabsWidth();
    if (mTabsWidth > getWidth() - 4
        - mArrowButton[0]->getWidth()
        - mArrowButton[1]->getWidth())
    {
        mArrowButton[0]->setVisible(true);
        mArrowButton[1]->setVisible(true);
    }
    else
    {
        mArrowButton[0]->setVisible(false);
        mArrowButton[1]->setVisible(false);
        mTabScrollIndex = 0;
    }

    // Left arrow consistency check
    if (!mTabScrollIndex)
        mArrowButton[0]->setEnabled(false);
    else
        mArrowButton[0]->setEnabled(true);

    // Right arrow consistency check
    if (mVisibleTabsWidth < getWidth() - 4
        - mArrowButton[0]->getWidth()
        - mArrowButton[1]->getWidth())
    {
        mArrowButton[1]->setEnabled(false);
    }
    else
    {
        mArrowButton[1]->setEnabled(true);
    }
}

/*
void TabbedArea::moveLeft(gcn::Tab *tab)
{
}

void TabbedArea::moveRight(gcn::Tab *tab)
{
}
*/