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/label.hpp"
00062
00063 #include "guichan/exception.hpp"
00064 #include "guichan/font.hpp"
00065 #include "guichan/graphics.hpp"
00066
00067 namespace gcn
00068 {
00069 Label::Label()
00070 {
00071 mAlignment = Graphics::LEFT;
00072 }
00073
00074 Label::Label(const std::string& caption)
00075 {
00076 mCaption = caption;
00077 mAlignment = Graphics::LEFT;
00078
00079 setWidth(getFont()->getWidth(caption));
00080 setHeight(getFont()->getHeight());
00081 }
00082
00083 const std::string &Label::getCaption() const
00084 {
00085 return mCaption;
00086 }
00087
00088 void Label::setCaption(const std::string& caption)
00089 {
00090 mCaption = caption;
00091 }
00092
00093 void Label::setAlignment(unsigned int alignment)
00094 {
00095 mAlignment = alignment;
00096 }
00097
00098 unsigned int Label::getAlignment()
00099 {
00100 return mAlignment;
00101 }
00102
00103 void Label::draw(Graphics* graphics)
00104 {
00105 int textX;
00106 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00107
00108 switch (getAlignment())
00109 {
00110 case Graphics::LEFT:
00111 textX = 0;
00112 break;
00113 case Graphics::CENTER:
00114 textX = getWidth() / 2;
00115 break;
00116 case Graphics::RIGHT:
00117 textX = getWidth();
00118 break;
00119 default:
00120 throw GCN_EXCEPTION("Unknown alignment.");
00121 }
00122
00123 graphics->setFont(getFont());
00124 graphics->setColor(getForegroundColor());
00125 graphics->drawText(getCaption(), textX, textY, getAlignment());
00126 }
00127
00128 void Label::drawBorder(Graphics* graphics)
00129 {
00130 Color faceColor = getBaseColor();
00131 Color highlightColor, shadowColor;
00132 int alpha = getBaseColor().a;
00133 int width = getWidth() + getBorderSize() * 2 - 1;
00134 int height = getHeight() + getBorderSize() * 2 - 1;
00135 highlightColor = faceColor + 0x303030;
00136 highlightColor.a = alpha;
00137 shadowColor = faceColor - 0x303030;
00138 shadowColor.a = alpha;
00139
00140 unsigned int i;
00141 for (i = 0; i < getBorderSize(); ++i)
00142 {
00143 graphics->setColor(shadowColor);
00144 graphics->drawLine(i,i, width - i, i);
00145 graphics->drawLine(i,i + 1, i, height - i - 1);
00146 graphics->setColor(highlightColor);
00147 graphics->drawLine(width - i,i + 1, width - i, height - i);
00148 graphics->drawLine(i,height - i, width - i - 1, height - i);
00149 }
00150 }
00151
00152 void Label::adjustSize()
00153 {
00154 setWidth(getFont()->getWidth(getCaption()));
00155 setHeight(getFont()->getHeight());
00156 }
00157 }