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

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

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