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/checkbox.hpp"
00060
00061 namespace gcn
00062 {
00063
00064 CheckBox::CheckBox()
00065 {
00066 setMarked(false);
00067
00068 setFocusable(true);
00069 addMouseListener(this);
00070 addKeyListener(this);
00071 }
00072
00073 CheckBox::CheckBox(const std::string &caption, bool marked)
00074 {
00075 setCaption(caption);
00076 setMarked(marked);
00077
00078 setFocusable(true);
00079 addMouseListener(this);
00080 addKeyListener(this);
00081
00082 adjustSize();
00083 }
00084
00085 void CheckBox::draw(Graphics* graphics)
00086 {
00087 drawBox(graphics);
00088
00089 graphics->setFont(getFont());
00090 graphics->setColor(getForegroundColor());
00091
00092 int h = getHeight() + getHeight() / 2;
00093
00094 graphics->drawText(getCaption(), h - 2, 0);
00095
00096 if (hasFocus())
00097 {
00098 graphics->drawRectangle(Rectangle(h - 4, 0, getWidth() - h + 3, getHeight()));
00099 }
00100 }
00101
00102 void CheckBox::drawBorder(Graphics* graphics)
00103 {
00104 Color faceColor = getBaseColor();
00105 Color highlightColor, shadowColor;
00106 int alpha = getBaseColor().a;
00107 int width = getWidth() + getBorderSize() * 2 - 1;
00108 int height = getHeight() + getBorderSize() * 2 - 1;
00109 highlightColor = faceColor + 0x303030;
00110 highlightColor.a = alpha;
00111 shadowColor = faceColor - 0x303030;
00112 shadowColor.a = alpha;
00113
00114 unsigned int i;
00115 for (i = 0; i < getBorderSize(); ++i)
00116 {
00117 graphics->setColor(shadowColor);
00118 graphics->drawLine(i,i, width - i, i);
00119 graphics->drawLine(i,i + 1, i, height - i - 1);
00120 graphics->setColor(highlightColor);
00121 graphics->drawLine(width - i,i + 1, width - i, height - i);
00122 graphics->drawLine(i,height - i, width - i - 1, height - i);
00123 }
00124 }
00125
00126 void CheckBox::drawBox(Graphics *graphics)
00127 {
00128 int h = getHeight() - 1;
00129
00130 int alpha = getBaseColor().a;
00131 Color faceColor = getBaseColor();
00132 faceColor.a = alpha;
00133 Color highlightColor = faceColor + 0x303030;
00134 highlightColor.a = alpha;
00135 Color shadowColor = faceColor - 0x303030;
00136 shadowColor.a = alpha;
00137
00138 graphics->setColor(shadowColor);
00139 graphics->drawLine(0, 0, h, 0);
00140 graphics->drawLine(0, 1, 0, h);
00141
00142 graphics->setColor(highlightColor);
00143 graphics->drawLine(h, 1, h, h);
00144 graphics->drawLine(1, h, h - 1, h);
00145
00146 graphics->setColor(getBackgroundColor());
00147 graphics->fillRectangle(Rectangle(1, 1, h - 1, h - 1));
00148
00149 graphics->setColor(getForegroundColor());
00150
00151 if (mMarked)
00152 {
00153 graphics->drawLine(3, 5, 3, h - 3);
00154 graphics->drawLine(4, 5, 4, h - 3);
00155
00156 graphics->drawLine(5, h - 4, h - 2, 3);
00157 graphics->drawLine(5, h - 5, h - 4, 4);
00158 }
00159 }
00160
00161 bool CheckBox::isMarked() const
00162 {
00163 return mMarked;
00164 }
00165
00166 void CheckBox::setMarked(bool marked)
00167 {
00168 mMarked = marked;
00169 }
00170
00171 const std::string &CheckBox::getCaption() const
00172 {
00173 return mCaption;
00174 }
00175
00176 void CheckBox::setCaption(const std::string caption)
00177 {
00178 mCaption = caption;
00179 }
00180
00181 void CheckBox::keyPress(const Key& key)
00182 {
00183 if (key.getValue() == Key::ENTER ||
00184 key.getValue() == Key::SPACE)
00185 {
00186 toggle();
00187 }
00188 }
00189
00190 void CheckBox::mouseClick(int x, int y, int button, int count)
00191 {
00192 if (button == MouseInput::LEFT)
00193 {
00194 toggle();
00195 }
00196 }
00197
00198 void CheckBox::adjustSize()
00199 {
00200 int height = getFont()->getHeight();
00201
00202 setHeight(height);
00203 setWidth(getFont()->getWidth(mCaption) + height + height / 2);
00204 }
00205
00206 void CheckBox::toggle()
00207 {
00208 mMarked = !mMarked;
00209 generateAction();
00210 }
00211 }
00212