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.hpp"
00060 #include "guichan/exception.hpp"
00061 #include "guichan/mouseinput.hpp"
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
00085 addMouseListener(this);
00086 addKeyListener(this);
00087 }
00088
00089 void Button::setCaption(const std::string& caption)
00090 {
00091 mCaption = caption;
00092 }
00093
00094 const std::string& Button::getCaption() const
00095 {
00096 return mCaption;
00097 }
00098
00099 void Button::setAlignment(unsigned int alignment)
00100 {
00101 mAlignment = alignment;
00102 }
00103
00104 unsigned int Button::getAlignment() const
00105 {
00106 return mAlignment;
00107 }
00108
00109 void Button::draw(Graphics* graphics)
00110 {
00111 Color faceColor = getBaseColor();
00112 Color highlightColor, shadowColor;
00113 int alpha = getBaseColor().a;
00114
00115 if (isPressed())
00116 {
00117 faceColor = faceColor - 0x303030;
00118 faceColor.a = alpha;
00119 highlightColor = faceColor - 0x303030;
00120 highlightColor.a = alpha;
00121 shadowColor = faceColor + 0x303030;
00122 shadowColor.a = alpha;
00123 }
00124 else
00125 {
00126 highlightColor = faceColor + 0x303030;
00127 highlightColor.a = alpha;
00128 shadowColor = faceColor - 0x303030;
00129 shadowColor.a = alpha;
00130 }
00131
00132 graphics->setColor(faceColor);
00133 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00134
00135 graphics->setColor(highlightColor);
00136 graphics->drawLine(0, 0, getWidth() - 1, 0);
00137 graphics->drawLine(0, 1, 0, getHeight() - 1);
00138
00139 graphics->setColor(shadowColor);
00140 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
00141 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
00142
00143 graphics->setColor(getForegroundColor());
00144
00145 int textX;
00146 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00147
00148 switch (getAlignment())
00149 {
00150 case Graphics::LEFT:
00151 textX = 4;
00152 break;
00153 case Graphics::CENTER:
00154 textX = getWidth() / 2;
00155 break;
00156 case Graphics::RIGHT:
00157 textX = getWidth() - 4;
00158 break;
00159 default:
00160 throw GCN_EXCEPTION("Unknown alignment.");
00161 }
00162
00163 graphics->setFont(getFont());
00164
00165 if (isPressed())
00166 {
00167 graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00168 }
00169 else
00170 {
00171 graphics->drawText(getCaption(), textX, textY, getAlignment());
00172
00173 if (hasFocus())
00174 {
00175 graphics->drawRectangle(Rectangle(2, 2, getWidth() - 4,
00176 getHeight() - 4));
00177 }
00178 }
00179 }
00180
00181 void Button::drawBorder(Graphics* graphics)
00182 {
00183 Color faceColor = getBaseColor();
00184 Color highlightColor, shadowColor;
00185 int alpha = getBaseColor().a;
00186 int width = getWidth() + getBorderSize() * 2 - 1;
00187 int height = getHeight() + getBorderSize() * 2 - 1;
00188 highlightColor = faceColor + 0x303030;
00189 highlightColor.a = alpha;
00190 shadowColor = faceColor - 0x303030;
00191 shadowColor.a = alpha;
00192
00193 unsigned int i;
00194 for (i = 0; i < getBorderSize(); ++i)
00195 {
00196 graphics->setColor(shadowColor);
00197 graphics->drawLine(i,i, width - i, i);
00198 graphics->drawLine(i,i + 1, i, height - i - 1);
00199 graphics->setColor(highlightColor);
00200 graphics->drawLine(width - i,i + 1, width - i, height - i);
00201 graphics->drawLine(i,height - i, width - i - 1, height - i);
00202 }
00203 }
00204
00205 void Button::adjustSize()
00206 {
00207 setWidth(getFont()->getWidth(mCaption) + 8);
00208 setHeight(getFont()->getHeight() + 8);
00209 }
00210
00211 bool Button::isPressed() const
00212 {
00213 return (hasMouse() && mMouseDown) || mKeyDown;
00214 }
00215
00216 void Button::mouseClick(int x, int y, int button, int count)
00217 {
00218 if (button == MouseInput::LEFT)
00219 {
00220 generateAction();
00221 }
00222 }
00223
00224 void Button::mousePress(int x, int y, int button)
00225 {
00226 if (button == MouseInput::LEFT && hasMouse())
00227 {
00228 mMouseDown = true;
00229 }
00230 }
00231
00232 void Button::mouseRelease(int x, int y, int button)
00233 {
00234 if (button == MouseInput::LEFT)
00235 {
00236 mMouseDown = false;
00237 }
00238 }
00239
00240 void Button::keyPress(const Key& key)
00241 {
00242 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00243 {
00244 mKeyDown = true;
00245 }
00246
00247 mMouseDown = false;
00248 }
00249
00250 void Button::keyRelease(const Key& key)
00251 {
00252 if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyDown)
00253 {
00254 mKeyDown = false;
00255 generateAction();
00256 }
00257 }
00258
00259 void Button::lostFocus()
00260 {
00261 mMouseDown = false;
00262 mKeyDown = false;
00263 }
00264 }