summaryrefslogtreecommitdiff
path: root/src/gui/widgets/dropdown.cpp
blob: ac5cbaa232f4e1e090ccb066e3f0f3f47b27bb1f (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
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <algorithm>

#include "dropdown.h"

#include "../colour.h"

#include "../../configuration.h"
#include "../../graphics.h"

#include "../../resources/image.h"
#include "../../resources/resourcemanager.h"

#include "../../utils/dtor.h"

int DropDown::instances = 0;
Image *DropDown::buttons[2][2];
ImageRect DropDown::skin;
float DropDown::mAlpha = config.getValue("guialpha", 0.8);

DropDown::DropDown(gcn::ListModel *listModel, gcn::ScrollArea *scrollArea, 
                   gcn::ListBox *listBox, bool opacity):
    gcn::DropDown::DropDown(listModel, scrollArea, listBox),
    mOpaque(opacity)
{
    setFrameSize(2);

    // Initialize graphics
    if (instances == 0)
    {
        // Load the background skin
        ResourceManager *resman = ResourceManager::getInstance();

        // Get the button skin
        buttons[1][0] =
            resman->getImage("graphics/gui/vscroll_up_default.png");
        buttons[0][0] =
            resman->getImage("graphics/gui/vscroll_down_default.png");
        buttons[1][1] =
            resman->getImage("graphics/gui/vscroll_up_pressed.png");
        buttons[0][1] =
            resman->getImage("graphics/gui/vscroll_down_pressed.png");

        buttons[0][0]->setAlpha(mAlpha);
        buttons[0][1]->setAlpha(mAlpha);
        buttons[1][0]->setAlpha(mAlpha);
        buttons[1][1]->setAlpha(mAlpha);

        // get the border skin
        Image *boxBorder = resman->getImage("graphics/gui/deepbox.png");
        int gridx[4] = {0, 3, 28, 31};
        int gridy[4] = {0, 3, 28, 31};
        int a = 0, x, y;

        for (y = 0; y < 3; y++) {
            for (x = 0; x < 3; x++) {
                skin.grid[a] = boxBorder->getSubImage(
                        gridx[x], gridy[y],
                        gridx[x + 1] - gridx[x] + 1,
                        gridy[y + 1] - gridy[y] + 1);
                skin.grid[a]->setAlpha(mAlpha);
                a++;
            }
        }

        boxBorder->decRef();
    }

    instances++;
}

DropDown::~DropDown()
{
    instances--;
    // Free images memory
    if (instances == 0)
    {
        buttons[0][0]->decRef();
        buttons[0][1]->decRef();
        buttons[1][0]->decRef();
        buttons[1][1]->decRef();

        for_each(skin.grid, skin.grid + 9, dtor<Image*>());
    }
}

void DropDown::draw(gcn::Graphics* graphics)
{
    int h;

    if (mDroppedDown)
    {
        h = mFoldedUpHeight;
    }
    else
    {
        h = getHeight();
    }

    if (config.getValue("guialpha", 0.8) != mAlpha)
    {
        mAlpha = config.getValue("guialpha", 0.8);

        buttons[0][0]->setAlpha(mAlpha);
        buttons[0][1]->setAlpha(mAlpha);
        buttons[1][0]->setAlpha(mAlpha);
        buttons[1][1]->setAlpha(mAlpha);

        for (int a = 0; a < 9; a++)
        {
            skin.grid[a]->setAlpha(mAlpha);
        }
    }

    bool valid;
    const int alpha = mAlpha * 255;
    gcn::Color faceColor = getBaseColor();
    faceColor.a = alpha;
    gcn::Color highlightColor = textColour->getColour('H', valid);
    highlightColor.a = alpha;
    gcn::Color shadowColor = faceColor - 0x303030;
    shadowColor.a = alpha;

    if (mOpaque)
    {
        int red = getBackgroundColor().r;
        int green = getBackgroundColor().g;
        int blue = getBackgroundColor().b;
        graphics->setColor(gcn::Color(red, green, blue, alpha));
        graphics->fillRectangle(gcn::Rectangle(0, 0, getWidth(), h));

        red = getForegroundColor().r;
        green = getForegroundColor().g;
        blue = getForegroundColor().b;
        graphics->setColor(gcn::Color(red, green, blue, alpha));
    }

    graphics->setFont(getFont());

    if (mListBox->getListModel() && mListBox->getSelected() >= 0)
    {
        graphics->drawText(mListBox->getListModel()->getElementAt(mListBox->getSelected()), 1, 0);
    }

    if (isFocused())
    {
        graphics->setColor(highlightColor);
        graphics->drawRectangle(gcn::Rectangle(0, 0, getWidth() - h, h));
    }

    drawButton(graphics);

    if (mDroppedDown)
    {
        drawChildren(graphics);

        // Draw two lines separating the ListBox with selected
        // element view.
        graphics->setColor(highlightColor);
        graphics->drawLine(0, h, getWidth(), h);
        graphics->setColor(shadowColor);
        graphics->drawLine(0, h + 1, getWidth(), h + 1);
    }
}

void DropDown::drawFrame(gcn::Graphics *graphics)
{
    const int bs = getFrameSize();
    const int w = getWidth() + bs * 2;
    const int h = getHeight() + bs * 2;

    static_cast<Graphics*>(graphics)->drawImageRect(0, 0, w, h, skin);
}

void DropDown::drawButton(gcn::Graphics *graphics)
{
    int height = mDroppedDown ? mFoldedUpHeight : getHeight();

    static_cast<Graphics*>(graphics)->
        drawImage(buttons[mDroppedDown][mPushed], getWidth() - height + 2, 1);
}