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 "otk/screeninfo.hh" 00008 #include "otk/display.hh" 00009 #include "labelwidget.hh" 00010 00011 namespace ob { 00012 00013 LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type) 00014 : otk::Widget(parent), 00015 WidgetBase(type) 00016 { 00017 } 00018 00019 00020 LabelWidget::~LabelWidget() 00021 { 00022 } 00023 00024 00025 void LabelWidget::setText(const otk::ustring &text) 00026 { 00027 _text = text; 00028 _dirty = true; 00029 } 00030 00031 00032 void LabelWidget::setTextures() 00033 { 00034 if (_focused) { 00035 setTexture(_style->labelFocusBackground()); 00036 _text_color = _style->textFocusColor(); 00037 } else { 00038 setTexture(_style->labelUnfocusBackground()); 00039 _text_color = _style->textUnfocusColor(); 00040 } 00041 } 00042 00043 00044 void LabelWidget::setStyle(otk::RenderStyle *style) 00045 { 00046 otk::Widget::setStyle(style); 00047 setTextures(); 00048 _font = style->labelFont(); 00049 _sidemargin = style->bevelWidth() * 2; 00050 _justify = style->labelTextJustify(); 00051 00052 assert(_font); 00053 } 00054 00055 00056 void LabelWidget::focus() 00057 { 00058 otk::Widget::focus(); 00059 setTextures(); 00060 } 00061 00062 00063 void LabelWidget::unfocus() 00064 { 00065 otk::Widget::unfocus(); 00066 setTextures(); 00067 } 00068 00069 00070 void LabelWidget::renderForeground() 00071 { 00072 bool draw = _dirty; 00073 00074 otk::Widget::renderForeground(); 00075 00076 if (draw) { 00077 otk::ustring t = _text; 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 = _font->measureString(t); 00091 } while (length > max_length && text_len-- > 0); 00092 00093 // justify the text 00094 switch (_justify) { 00095 case otk::RenderStyle::RightJustify: 00096 x += max_length - length; 00097 break; 00098 case otk::RenderStyle::CenterJustify: 00099 x += (max_length - length) / 2; 00100 break; 00101 case otk::RenderStyle::LeftJustify: 00102 break; 00103 } 00104 } 00105 00106 otk::display->renderControl(_screen)->drawString 00107 (*_surface, *_font, x, 0, *_text_color, t); 00108 } 00109 } 00110 00111 00112 void LabelWidget::adjust() 00113 { 00114 // nothing to adjust. no children. 00115 } 00116 00117 }