src/label.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2003 - 2004 Anselm R. Garbe <anselmg at t-online.de>
00002 // See ../LICENSE.txt for license details.
00003 //
00004 // $Id: label.cpp 2 2007-05-16 10:38:27Z eg $
00005 
00006 #include <sstream>
00007 
00008 #include "label.h"
00009 
00010 #include "draw.h"
00011 #include "font.h"
00012 #include "logger.h"
00013 #include "kernel.h"
00014 #include "monitor.h"
00015 #include "xcore.h"
00016 
00017 Label::Label(Monitor *monitor, Window window, Alignment align, GC gc)
00018     : Rectangle()
00019 {
00020 
00021     LOGDEBUG("initializing label");
00022     align_ = align;
00023     font_ = monitor->font();
00024     monitor_ = monitor;
00025     gc_ = gc;
00026     window_ = window;
00027 }
00028 
00029 Label::~Label() {
00030 }
00031 
00032 unsigned int Label::adjustWidth() {
00033     setWidth(textWidth());
00034     return width();
00035 }
00036 
00037 unsigned int Label::textWidth() {
00038     return font_->textWidth(text_) + 2 * font_->height();
00039 }
00040 
00041 void Label::update(unsigned long background, unsigned long text,
00042                 unsigned long shine, unsigned long shadow,
00043                 bool shineBorder, bool shadowBorder)
00044 
00045 {
00046 
00047     LOGDEBUG("label update");
00048 
00049     // draw background
00050     XCORE->setForeground(gc_, background);
00051     XCORE->fillRectangle(window_, gc_, this);
00052 
00053     // draw border if neccessary
00054     if (shadowBorder) {
00055         Draw::drawShadowBorder(window_, gc_, this, shadow);
00056     }
00057 
00058     if (shineBorder) {
00059 
00060         Draw::drawShineBorder(window_, gc_, this, shine);
00061     }
00062 
00063     // draw text
00064     if (text_ == "") {
00065         return; // nothing to draw
00066     }
00067     XCORE->setForeground(gc_, text);
00068     int textWidth = this->textWidth();
00069 
00070     if (textWidth > (int)width()) {
00071         int matchLength = (int)((text_.length() * width() / textWidth) - 3);
00072         if (matchLength < 0) {
00073             matchLength = 0;
00074         }
00075         text_ = Util::shortenString(text_, matchLength);
00076         textWidth = this->textWidth();
00077     }
00078 
00079     unsigned int fontBaseLine = 0;
00080     fontBaseLine = font_->ascent();
00081     unsigned int fontY = y() + height() / 2 - font_->height() / 2 + fontBaseLine;
00082 
00083     unsigned int fontX = 0;
00084     switch (align_) {
00085     case LEFT:
00086         fontX = x() + font_->height();
00087         break;
00088     case CENTER:
00089         fontX = x() + (width() / 2 - (textWidth - 2 * font_->height()) / 2);
00090         break;
00091     case RIGHT:
00092         fontX = x() + width() - textWidth + font_->height();
00093         break;
00094     }
00095 
00096    font_->drawText(window_, gc_, fontX, fontY, text_);
00097 }
00098 
00099 
00100 void Label::setText(string text) {
00101     text_ = text;
00102 }
00103 
00104 string Label::text() const {
00105     return text_;
00106 }
00107 
00108 void Label::setAlignment(const Alignment align) {
00109     align_ = align;
00110 }

Generated on Thu May 24 15:19:32 2007 for ncwm by  doxygen 1.5.1