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