1
/***************************************************************************
2
* Copyright (C) 2008 by Bjorn Hansen *
3
* holomorph@users.sourceforge.net *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
22
#include <guichan/font.hpp>
23
#include <guichan/graphics.hpp>
24
#include "menu/widgets/skinneddropdown.h"
25
#include "balder2dtypes.h"
27
using namespace Balder;
30
void SkinnedListBox::draw(Graphics* graphics)
32
Color bgColor = MenuColors::UNFOCUSED;
33
//bgColor.a = MenuColors::bgAlpha;
34
graphics->setColor(bgColor);
35
graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
37
if (mListModel == NULL)
42
graphics->setColor(getForegroundColor());
43
graphics->setFont(getFont());
45
// Check the current clip area so we don't draw unnecessary items
46
// that are not visible.
47
const ClipRectangle currentClipArea = graphics->getCurrentClipArea();
48
int rowHeight = getRowHeight();
50
// Calculate the number of rows to draw by checking the clip area.
51
// The addition of two makes covers a partial visible row at the top
52
// and a partial visible row at the bottom.
53
int numberOfRows = currentClipArea.height / rowHeight + 2;
55
if (numberOfRows > mListModel->getNumberOfElements())
57
numberOfRows = mListModel->getNumberOfElements();
60
// Calculate which row to start drawing. If the list box
61
// has a negative y coordinate value we should check if
62
// we should drop rows in the begining of the list as
63
// they might not be visible. A negative y value is very
64
// common if the list box for instance resides in a scroll
65
// area and the user has scrolled the list box downwards.
69
startRow = -1 * (getY() / rowHeight);
76
// end row should not exceed list size
77
int endRow = std::min(startRow+numberOfRows, mListModel->getNumberOfElements());
78
// The y coordinate where we start to draw the text is
79
// simply the y coordinate multiplied with the font height.
80
int y = rowHeight * startRow;
81
for (int i = startRow; i < endRow; ++i)
85
graphics->setColor(getSelectionColor());
86
graphics->fillRectangle(Rectangle(0, y, getWidth(), rowHeight));
87
graphics->setColor(getForegroundColor());
90
// If the row height is greater than the font height we
91
// draw the text with a center vertical alignment.
92
if (rowHeight > getFont()->getHeight())
94
graphics->drawText(mListModel->getElementAt(i), 1, y + rowHeight / 2 - getFont()->getHeight() / 2);
98
graphics->drawText(mListModel->getElementAt(i), 1, y);
105
void SkinnedDropDown::keyPressed(KeyEvent& keyEvent)
107
// work around guichan bug where when the dropdown is open arrow keys get counted twice (skips elements)
108
if (keyEvent.isConsumed()) return;
109
gcn::DropDown::keyPressed(keyEvent);
112
void SkinnedDropDown::draw(Graphics* graphics)
125
Color highlightColor, shadowColor;
126
highlightColor = MenuColors::HIGHLIGHT;
127
shadowColor = MenuColors::SHADOW;
130
for (int i=0; i<5 ;++i)
132
shadowColor.a -= i*0x10;
133
graphics->setColor(shadowColor);
134
graphics->drawLine(i,i,getWidth()-i,i);
135
graphics->drawLine(i,i,i,getHeight()-i);
137
for (int i=0; i<5 ;++i)
139
highlightColor.a -= i*0x10;
140
graphics->setColor(highlightColor);
141
graphics->drawLine(getWidth()-i,i,getWidth()-i,getHeight()-i);
142
graphics->drawLine(i,getHeight()-i,getWidth()-i,getHeight()-i);
145
// Push a clip area so the other drawings don't need to worry
147
graphics->pushClipArea(Rectangle(1, 1, getWidth() - 2, h - 2));
148
const Rectangle currentClipArea = graphics->getCurrentClipArea();
150
Color bgColor = isFocused() ? MenuColors::FOCUSED : MenuColors::UNFOCUSED;
151
bgColor.a = MenuColors::bgAlpha;
152
graphics->setColor(bgColor);
153
graphics->fillRectangle(Rectangle(0, 0, currentClipArea.width, currentClipArea.height));
155
if (mListBox->getListModel()
156
&& mListBox->getSelected() >= 0)
158
graphics->setColor(getForegroundColor());
159
graphics->setFont(getFont());
161
graphics->drawText(mListBox->getListModel()->getElementAt(mListBox->getSelected()), 1, 5);
164
// Push a clip area before drawing the button.
165
graphics->pushClipArea(Rectangle(currentClipArea.width - currentClipArea.height,
167
currentClipArea.height,
168
currentClipArea.height));
169
drawButton(graphics);
170
graphics->popClipArea();
171
graphics->popClipArea();
175
// Draw a border around the children.
176
// graphics->setColor(shadowColor);
177
// graphics->drawRectangle(Rectangle(0,
180
// getHeight() - mFoldedUpHeight));
181
drawChildren(graphics);
185
void SkinnedDropDown::drawButton(Graphics *graphics)
187
Color faceColor, highlightColor, shadowColor;
189
int alpha = MenuColors::bgAlpha;
193
faceColor = MenuColors::FOCUSED;
195
highlightColor = MenuColors::SHADOW;
196
highlightColor.a = alpha;
197
shadowColor = MenuColors::HIGHLIGHT;
198
shadowColor.a = alpha;
203
faceColor = MenuColors::FOCUSED;
205
highlightColor = MenuColors::HIGHLIGHT;
206
highlightColor.a = alpha;
207
shadowColor = MenuColors::SHADOW;
208
shadowColor.a = alpha;
212
const Rectangle currentClipArea = graphics->getCurrentClipArea();
213
for (int i = 0; i<3; ++i)
215
graphics->setColor(highlightColor);
216
graphics->drawLine(i, i, currentClipArea.width - i, i);
217
graphics->drawLine(i, i, i, currentClipArea.height - i);
218
graphics->setColor(shadowColor);
219
graphics->drawLine(currentClipArea.width - i, i, currentClipArea.width - i,currentClipArea.height - i);
220
graphics->drawLine(i, currentClipArea.height - i, currentClipArea.width - i,currentClipArea.height - i);
223
graphics->setColor(faceColor);
224
graphics->fillRectangle(Rectangle(1,
226
currentClipArea.width - 2,
227
currentClipArea.height - 2));
229
graphics->setColor(MenuColors::fontColor);
232
int n = currentClipArea.height / 3;
233
int dx = currentClipArea.height / 2;
234
int dy = (currentClipArea.height * 2) / 3;
235
for (i = 0; i < n; i++)
237
graphics->drawLine(dx - i + offset,
244
void SkinnedScrollArea::draw(Graphics *graphics)
246
drawBackground(graphics);
250
// drawUpButton(graphics);
251
// drawDownButton(graphics);
253
drawVMarker(graphics);
257
drawChildren(graphics);
260
void SkinnedScrollArea::drawVBar(Graphics* graphics)
262
Rectangle dim(getWidth() - mScrollbarWidth, 0, mScrollbarWidth, getHeight());
265
graphics->pushClipArea(dim);
267
int alpha = MenuColors::bgAlpha;
268
Color trackColor = MenuColors::UNFOCUSED;
269
trackColor.a = alpha;
270
Color shadowColor = MenuColors::SHADOW;
271
shadowColor.a = alpha;
273
graphics->setColor(trackColor);
274
graphics->fillRectangle(Rectangle(0, 0, dim.width, dim.height));
276
graphics->setColor(shadowColor);
277
graphics->drawLine(0, 0, 0, dim.height);
279
graphics->popClipArea();
282
void SkinnedScrollArea::drawVMarker(Graphics* graphics)
284
Rectangle dim = getVerticalMarkerDimension();
285
graphics->pushClipArea(dim);
287
int alpha = MenuColors::bgAlpha;
288
Color faceColor = MenuColors::fontColor;
289
Color highlightColor = MenuColors::HIGHLIGHT;
290
highlightColor.a = alpha;
291
Color shadowColor = MenuColors::SHADOW;
292
shadowColor.a = alpha;
294
graphics->setColor(faceColor);
295
graphics->fillRectangle(Rectangle(1, 1, dim.width - 1, dim.height - 1));
297
graphics->setColor(highlightColor);
298
graphics->drawLine(0, 0, dim.width - 1, 0);
299
graphics->drawLine(0, 1, 0, dim.height - 1);
301
graphics->setColor(shadowColor);
302
graphics->drawLine(1, dim.height - 1, dim.width - 1, dim.height - 1);
303
graphics->drawLine(dim.width - 1, 0, dim.width - 1, dim.height - 1);
305
graphics->popClipArea();