Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/otk/label.cc

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 # include "../config.h"
00005 #endif
00006 
00007 #include "label.hh"
00008 
00009 namespace otk {
00010 
00011 Label::Label(Widget *parent)
00012   : Widget(parent), _text("")
00013 {
00014   setStyle(_style);
00015 }
00016 
00017 Label::~Label()
00018 {
00019 }
00020 
00021 void Label::setStyle(RenderStyle *style)
00022 {
00023   Widget::setStyle(style);
00024 
00025   setTexture(style->labelUnfocusBackground());
00026 }
00027 
00028 void Label::fitString(const std::string &str)
00029 {
00030   const Font *ft = style()->labelFont();
00031   fitSize(ft->measureString(str), ft->height());
00032 }
00033 
00034 void Label::fitSize(int w, int h)
00035 {
00036   unsigned int sidemargin = style()->bevelWidth() * 2;
00037   resize(w + sidemargin * 2, h);
00038 }
00039 
00040 void Label::update()
00041 {
00042   if (_dirty) {
00043     int w = _rect.width(), h = _rect.height();
00044     const Font *ft = style()->labelFont();
00045     unsigned int sidemargin = style()->bevelWidth() * 2;
00046     if (!_fixed_width)
00047       w = ft->measureString(_text) + sidemargin * 2;
00048     if (!_fixed_height)
00049       h = ft->height();
00050 
00051     // enforce a minimum size
00052     if (w > _rect.width()) {
00053       if (h > _rect.height())
00054         internalResize(w, h);
00055       else
00056         internalResize(w, _rect.height());
00057     } else if (h > _rect.height())
00058       internalResize(_rect.width(), h);
00059   }
00060   Widget::update();
00061 }
00062 
00063 
00064 void Label::renderForeground(void)
00065 {
00066   Widget::renderForeground();
00067 
00068   const Font *ft = style()->labelFont();
00069   unsigned int sidemargin = style()->bevelWidth() * 2;
00070 
00071   ustring t = _text; // the actual text to draw
00072   int x = sidemargin;    // x coord for the text
00073 
00074   // find a string that will fit inside the area for text
00075   int max_length = width() - sidemargin * 2;
00076   if (max_length <= 0) {
00077     t = ""; // can't fit anything
00078   } else {
00079     size_t text_len = t.size();
00080     int length;
00081       
00082     do {
00083       t.resize(text_len);
00084       length = ft->measureString(t);
00085     } while (length > max_length && text_len-- > 0);
00086 
00087     // justify the text
00088     switch (style()->labelTextJustify()) {
00089     case RenderStyle::RightJustify:
00090       x += max_length - length;
00091       break;
00092     case RenderStyle::CenterJustify:
00093       x += (max_length - length) / 2;
00094       break;
00095     case RenderStyle::LeftJustify:
00096       break;
00097     }
00098   }
00099 
00100   display->renderControl(_screen)->
00101     drawString(*_surface, *ft, x, 0, *style()->textUnfocusColor(), t);
00102 }
00103 
00104 }

Generated on Tue Feb 4 22:58:56 2003 for Openbox by doxygen1.3-rc2