00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #include <boost/lexical_cast.hpp> 00022 00023 #include <wt/label.h> 00024 #include <wt/painter.h> 00025 #include <wt/font.h> 00026 00027 namespace Wt { 00028 00029 void Label::init() { 00030 sigc::slot0<void> update_slot = sigc::mem_fun(*this, &Label::update); 00031 00032 alignment.changed.connect(update_slot); 00033 indent.changed.connect(update_slot); 00034 /// \todo scaledContents implementation is missing 00035 scaledContents.changed.connect(update_slot); 00036 } 00037 00038 Size Label::calc_size_hint() const { 00039 int w, h; 00040 int align = alignment; 00041 00042 w = h = 2 * margin; 00043 00044 if (pixmap_) { 00045 w += pixmap_.width(); 00046 h += pixmap_.height(); 00047 } else { 00048 Size s = font().size(text_); 00049 w += s.width(); 00050 h += s.height(); 00051 } 00052 00053 if (align & AlignLeft || align & AlignRight) 00054 w += effectiveIndent(); 00055 00056 if (align & AlignTop|| align & AlignBottom) 00057 h += effectiveIndent(); 00058 00059 return Size(w, h); 00060 } 00061 00062 void Label::update() { 00063 const Size& size = calc_size_hint(); 00064 setSizeHint(size); 00065 setMinimumSize(size); 00066 Frame::update(); 00067 } 00068 00069 Label::Label(Widget *parent, const std::string& name, int wflags) : 00070 Frame(parent, name, wflags), 00071 alignment(AlignCenter), 00072 indent(-1), 00073 scaledContents(false) { 00074 init(); 00075 } 00076 00077 Label::Label(const std::string& text, Widget *parent, 00078 const std::string& name, int wflags) : 00079 Frame(parent, name, wflags), 00080 alignment(AlignCenter), 00081 indent(-1), 00082 scaledContents(false) { 00083 init(); 00084 setText(text); 00085 } 00086 00087 void Label::setText(const std::string& text) { 00088 pixmap_ = Pixmap(); 00089 text_ = text; 00090 update(); 00091 } 00092 00093 void Label::setPixmap(const Pixmap& pixmap) { 00094 pixmap_ = pixmap; 00095 update(); 00096 } 00097 00098 void Label::setNum(int num) { 00099 setText(boost::lexical_cast<std::string>(num)); 00100 } 00101 00102 void Label::setNum(double num) { 00103 setText(boost::lexical_cast<std::string>(num)); 00104 } 00105 00106 void Label::clear() { 00107 setText(""); 00108 } 00109 00110 void Label::fontChange(const Font&) { 00111 update(); 00112 } 00113 00114 int Label::effectiveIndent() const { 00115 int ind = indent; 00116 00117 if (ind < 0) { 00118 ind = (frameWidth() > 0) ? font().width('x') : 0; 00119 } 00120 00121 return ind; 00122 } 00123 00124 void Label::drawContents(Painter *p) { 00125 Frame::drawContents(p); 00126 Rect r = contentsRect(); 00127 int align = alignment; 00128 int ind = effectiveIndent(); 00129 00130 if (align & AlignLeft) 00131 r.setX(geometry().x() + ind); 00132 00133 if (align & AlignRight) 00134 r.setWidth(width() - ind); 00135 00136 if (align & AlignTop) 00137 r.setY(geometry().y() + ind); 00138 00139 if (align & AlignBottom) 00140 r.setHeight(height() - ind); 00141 00142 const Size& s = (pixmap_) ? pixmap_.size() : font().size(text_); 00143 const Rect& dst = Rect::align(r, s, align); 00144 Point offset = dst.topLeft(); 00145 00146 if (pixmap_) { 00147 p->drawPixmap(offset, pixmap_); 00148 } else { 00149 offset += Point(0, font().ascent()); 00150 p->drawText(offset, text_); 00151 } 00152 } 00153 00154 } // namespace Wt
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.