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/radiobutton.hpp"
00060
00061 namespace gcn
00062 {
00063 RadioButton::GroupMap RadioButton::mGroupMap;
00064
00065 RadioButton::RadioButton()
00066 {
00067 setMarked(false);
00068
00069 setFocusable(true);
00070 addMouseListener(this);
00071 addKeyListener(this);
00072 }
00073
00074 RadioButton::RadioButton(const std::string &caption,
00075 const std::string &group,
00076 bool marked)
00077 {
00078 setCaption(caption);
00079 setGroup(group);
00080 setMarked(marked);
00081
00082 setFocusable(true);
00083 addMouseListener(this);
00084 addKeyListener(this);
00085
00086 adjustSize();
00087 }
00088
00089 RadioButton::~RadioButton()
00090 {
00091
00092 setGroup("");
00093 }
00094
00095 void RadioButton::draw(Graphics* graphics)
00096 {
00097 drawBox(graphics);
00098
00099 graphics->setFont(getFont());
00100 graphics->setColor(getForegroundColor());
00101
00102 int h = getHeight() + getHeight() / 2;
00103
00104 graphics->drawText(getCaption(), h - 2, 0);
00105
00106 if (hasFocus())
00107 {
00108 graphics->drawRectangle(Rectangle(h - 4, 0, getWidth() - h + 3, getHeight()));
00109 }
00110 }
00111
00112 void RadioButton::drawBorder(Graphics* graphics)
00113 {
00114 Color faceColor = getBaseColor();
00115 Color highlightColor, shadowColor;
00116 int alpha = getBaseColor().a;
00117 int width = getWidth() + getBorderSize() * 2 - 1;
00118 int height = getHeight() + getBorderSize() * 2 - 1;
00119 highlightColor = faceColor + 0x303030;
00120 highlightColor.a = alpha;
00121 shadowColor = faceColor - 0x303030;
00122 shadowColor.a = alpha;
00123
00124 unsigned int i;
00125 for (i = 0; i < getBorderSize(); ++i)
00126 {
00127 graphics->setColor(shadowColor);
00128 graphics->drawLine(i,i, width - i, i);
00129 graphics->drawLine(i,i + 1, i, height - i - 1);
00130 graphics->setColor(highlightColor);
00131 graphics->drawLine(width - i,i + 1, width - i, height - i);
00132 graphics->drawLine(i,height - i, width - i - 1, height - i);
00133 }
00134 }
00135
00136 void RadioButton::drawBox(Graphics *graphics)
00137 {
00138 int h;
00139
00140 if (getHeight()%2 == 0)
00141 {
00142 h = getHeight() - 2;
00143 }
00144 else
00145 {
00146 h = getHeight() - 1;
00147 }
00148
00149 int alpha = getBaseColor().a;
00150 Color faceColor = getBaseColor();
00151 faceColor.a = alpha;
00152 Color highlightColor = faceColor + 0x303030;
00153 highlightColor.a = alpha;
00154 Color shadowColor = faceColor - 0x303030;
00155 shadowColor.a = alpha;
00156
00157 graphics->setColor(getBackgroundColor());
00158
00159 int i;
00160 int hh = (h + 1) / 2;
00161
00162 for (i = 1; i <= hh; ++i)
00163 {
00164 graphics->drawLine(hh - i + 1,
00165 i,
00166 hh + i - 1,
00167 i);
00168 }
00169
00170 for (i = 1; i < hh; ++i)
00171 {
00172 graphics->drawLine(hh - i + 1,
00173 h - i,
00174 hh + i - 1,
00175 h - i);
00176 }
00177
00178 graphics->setColor(shadowColor);
00179 graphics->drawLine(hh, 0, 0, hh);
00180 graphics->drawLine(hh + 1, 1, h - 1, hh - 1);
00181
00182 graphics->setColor(highlightColor);
00183 graphics->drawLine(1, hh + 1, hh, h);
00184 graphics->drawLine(hh + 1, h - 1, h, hh);
00185
00186 graphics->setColor(getForegroundColor());
00187
00188 int hhh = hh - 3;
00189 if (isMarked())
00190 {
00191 for (i = 0; i < hhh; ++i)
00192 {
00193 graphics->drawLine(hh - i, 4 + i, hh + i, 4 + i);
00194 }
00195 for (i = 0; i < hhh; ++i)
00196 {
00197 graphics->drawLine(hh - i, h - 4 - i, hh + i, h - 4 - i);
00198 }
00199
00200 }
00201 }
00202
00203 bool RadioButton::isMarked() const
00204 {
00205 return mMarked;
00206 }
00207
00208 void RadioButton::setMarked(bool marked)
00209 {
00210 if (marked && mGroup != "")
00211 {
00212 GroupIterator iter, iterEnd;
00213 iterEnd = mGroupMap.upper_bound(mGroup);
00214
00215 for (iter = mGroupMap.lower_bound(mGroup);
00216 iter != iterEnd;
00217 iter++)
00218 {
00219 if (iter->second->isMarked())
00220 {
00221 iter->second->setMarked(false);
00222 }
00223 }
00224 }
00225
00226 mMarked = marked;
00227 }
00228
00229 const std::string &RadioButton::getCaption() const
00230 {
00231 return mCaption;
00232 }
00233
00234 void RadioButton::setCaption(const std::string caption)
00235 {
00236 mCaption = caption;
00237 }
00238
00239 void RadioButton::keyPress(const Key& key)
00240 {
00241 if (key.getValue() == Key::ENTER ||
00242 key.getValue() == Key::SPACE)
00243 {
00244 setMarked(true);
00245 generateAction();
00246 }
00247 }
00248
00249 void RadioButton::mouseClick(int x, int y, int button, int count)
00250 {
00251 if (button == MouseInput::LEFT)
00252 {
00253 setMarked(true);
00254 generateAction();
00255 }
00256 }
00257
00258 void RadioButton::setGroup(const std::string &group)
00259 {
00260 if (mGroup != "")
00261 {
00262 GroupIterator iter, iterEnd;
00263 iterEnd = mGroupMap.upper_bound(mGroup);
00264
00265 for (iter = mGroupMap.lower_bound(mGroup);
00266 iter != iterEnd;
00267 iter++)
00268 {
00269 if (iter->second == this)
00270 {
00271 mGroupMap.erase(iter);
00272 break;
00273 }
00274 }
00275 }
00276
00277 if (group != "")
00278 {
00279 mGroupMap.insert(
00280 std::pair<std::string, RadioButton *>(group, this));
00281 }
00282
00283 mGroup = group;
00284 }
00285
00286 const std::string &RadioButton::getGroup() const
00287 {
00288 return mGroup;
00289 }
00290
00291 void RadioButton::adjustSize()
00292 {
00293 int height = getFont()->getHeight();
00294
00295 setHeight(height);
00296 setWidth(getFont()->getWidth(getCaption()) + height + height/2);
00297 }
00298 }