00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include "guichan/widgets/button.h"
00060 #include "guichan/exception.h"
00061 #include "guichan/mouseinput.h"
00062
00063 namespace gcn
00064 {
00065 Button::Button()
00066 {
00067 mAlignment = Graphics::CENTER;
00068 addMouseListener(this);
00069 addKeyListener(this);
00070 adjustSize();
00071 setBorderSize(1);
00072 }
00073
00074 Button::Button(const std::string& caption)
00075 {
00076 mCaption = caption;
00077 mAlignment = Graphics::CENTER;
00078 setFocusable(true);
00079 adjustSize();
00080 setBorderSize(1);
00081
00082 mMouseDown = false;
00083 mKeyDown = false;
00084 mHotKeyDown = false;
00085
00086 addMouseListener(this);
00087 addKeyListener(this);
00088 }
00089
00090 void Button::setCaption(const std::string& caption)
00091 {
00092 mCaption = caption;
00093 setDirty(true);
00094 }
00095
00096 const std::string& Button::getCaption() const
00097 {
00098 return mCaption;
00099 }
00100
00101 void Button::setAlignment(unsigned int alignment)
00102 {
00103 mAlignment = alignment;
00104 }
00105
00106 unsigned int Button::getAlignment() const
00107 {
00108 return mAlignment;
00109 }
00110
00111 void Button::draw(Graphics* graphics)
00112 {
00113 Color faceColor = getBaseColor();
00114 Color highlightColor, shadowColor;
00115 int alpha = getBaseColor().a;
00116
00117 if (isPressed())
00118 {
00119 faceColor = faceColor - 0x303030;
00120 faceColor.a = alpha;
00121 highlightColor = faceColor - 0x303030;
00122 highlightColor.a = alpha;
00123 shadowColor = faceColor + 0x303030;
00124 shadowColor.a = alpha;
00125 }
00126 else if (isEnabled())
00127 {
00128 highlightColor = faceColor + 0x303030;
00129 highlightColor.a = alpha;
00130 shadowColor = faceColor - 0x303030;
00131 shadowColor.a = alpha;
00132 }
00133 else
00134 {
00135 faceColor = getDisabledColor();
00136 highlightColor = faceColor + 0x303030;
00137 highlightColor.a = alpha;
00138 shadowColor = faceColor - 0x303030;
00139 shadowColor.a = alpha;
00140 }
00141
00142 graphics->setColor(faceColor);
00143 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00144
00145 graphics->setColor(highlightColor);
00146 graphics->drawLine(0, 0, getWidth() - 1, 0);
00147 graphics->drawLine(0, 1, 0, getHeight() - 1);
00148
00149 graphics->setColor(shadowColor);
00150 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
00151 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
00152
00153 graphics->setColor(getForegroundColor());
00154
00155 int textX;
00156 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00157
00158 switch (getAlignment())
00159 {
00160 case Graphics::LEFT:
00161 textX = 4;
00162 break;
00163 case Graphics::CENTER:
00164 textX = getWidth() / 2;
00165 break;
00166 case Graphics::RIGHT:
00167 textX = getWidth() - 4;
00168 break;
00169 default:
00170 throw GCN_EXCEPTION("Unknown alignment.");
00171 }
00172
00173 graphics->setFont(getFont());
00174
00175 if (isPressed())
00176 {
00177 graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00178 }
00179 else
00180 {
00181 graphics->drawText(getCaption(), textX, textY, getAlignment());
00182
00183 if (hasFocus())
00184 {
00185 graphics->drawRectangle(Rectangle(2, 2, getWidth() - 4,
00186 getHeight() - 4));
00187 }
00188 }
00189 }
00190
00191 void Button::drawBorder(Graphics* graphics)
00192 {
00193 Color faceColor = getBaseColor();
00194 Color highlightColor, shadowColor;
00195 int alpha = getBaseColor().a;
00196 int width = getWidth() + getBorderSize() * 2 - 1;
00197 int height = getHeight() + getBorderSize() * 2 - 1;
00198 highlightColor = faceColor + 0x303030;
00199 highlightColor.a = alpha;
00200 shadowColor = faceColor - 0x303030;
00201 shadowColor.a = alpha;
00202
00203 unsigned int i;
00204 for (i = 0; i < getBorderSize(); ++i)
00205 {
00206 graphics->setColor(shadowColor);
00207 graphics->drawLine(i,i, width - i, i);
00208 graphics->drawLine(i,i + 1, i, height - i - 1);
00209 graphics->setColor(highlightColor);
00210 graphics->drawLine(width - i,i + 1, width - i, height - i);
00211 graphics->drawLine(i,height - i, width - i - 1, height - i);
00212 }
00213 }
00214
00215 void Button::adjustSize()
00216 {
00217 setWidth(getFont()->getWidth(mCaption) + 8);
00218 setHeight(getFont()->getHeight() + 8);
00219 }
00220
00221 bool Button::isPressed() const
00222 {
00223 return (hasMouse() && mMouseDown) || mKeyDown || mHotKeyDown;
00224 }
00225
00226 void Button::mouseClick(int x, int y, int button, int count)
00227 {
00228 if (button == MouseInput::LEFT)
00229 {
00230 generateAction();
00231 }
00232 }
00233
00234 void Button::mousePress(int x, int y, int button)
00235 {
00236 if (button == MouseInput::LEFT && hasMouse())
00237 {
00238 mMouseDown = true;
00239 }
00240 }
00241
00242 void Button::mouseRelease(int x, int y, int button)
00243 {
00244 if (button == MouseInput::LEFT)
00245 {
00246 mMouseDown = false;
00247 }
00248 }
00249
00250 bool Button::keyPress(const Key& key)
00251 {
00252 bool ret = false;
00253
00254 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00255 {
00256 mKeyDown = true;
00257 ret = true;
00258 }
00259
00260 mHotKeyDown = false;
00261 mMouseDown = false;
00262 return ret;
00263 }
00264
00265 bool Button::keyRelease(const Key& key)
00266 {
00267 bool ret = false;
00268
00269 if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyDown)
00270 {
00271 mKeyDown = false;
00272 generateAction();
00273 ret = true;
00274 }
00275 return ret;
00276 }
00277
00278 void Button::hotKeyPress()
00279 {
00280 mHotKeyDown = true;
00281 mMouseDown = false;
00282 }
00283
00284 void Button::hotKeyRelease()
00285 {
00286 if (mHotKeyDown)
00287 {
00288 mHotKeyDown = false;
00289 generateAction();
00290 }
00291 }
00292
00293 void Button::lostFocus()
00294 {
00295 mMouseDown = false;
00296 mKeyDown = false;
00297 mHotKeyDown = false;
00298 }
00299 }