]> icculus.org git repositories - mikachu/openbox.git/blob - otk/focuslabel.cc
don't wait for x events if any timers fired
[mikachu/openbox.git] / otk / focuslabel.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "focuslabel.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 namespace otk {
12
13 FocusLabel::FocusLabel(Widget *parent)
14   : FocusWidget(parent), _text("")
15 {
16   setStyle(_style);
17 }
18
19 FocusLabel::~FocusLabel()
20 {
21 }
22
23
24 void FocusLabel::setStyle(RenderStyle *style)
25 {
26   FocusWidget::setStyle(style);
27
28   setTexture(style->labelFocusBackground());
29   setUnfocusTexture(style->labelUnfocusBackground());
30 }
31
32 void FocusLabel::fitString(const std::string &str)
33 {
34   const Font *ft = style()->labelFont();
35   fitSize(ft->measureString(str), ft->height());
36 }
37
38 void FocusLabel::fitSize(int w, int h)
39 {
40   unsigned int sidemargin = style()->bevelWidth() * 2;
41   resize(w + sidemargin * 2, h);
42 }
43
44 void FocusLabel::update()
45 {
46   if (_dirty) {
47     int w = _rect.width(), h = _rect.height();
48     const Font *ft = style()->labelFont();
49     unsigned int sidemargin = style()->bevelWidth() * 2;
50     if (!_fixed_width)
51       w = ft->measureString(_text) + sidemargin * 2;
52     if (!_fixed_height)
53       h = ft->height();
54
55     // enforce a minimum size
56     if (w > _rect.width()) {
57       if (h > _rect.height())
58         internalResize(w, h);
59       else
60         internalResize(w, _rect.height());
61     } else if (h > _rect.height())
62       internalResize(_rect.width(), h);
63   }
64   FocusWidget::update();
65 }
66
67
68 void FocusLabel::renderForeground()
69 {
70   FocusWidget::renderForeground();
71
72   const Font *ft = style()->labelFont();
73   RenderColor *text_color = (isFocused() ? style()->textFocusColor()
74                              : style()->textUnfocusColor());
75   unsigned int sidemargin = style()->bevelWidth() * 2;
76
77   ustring t = _text; // the actual text to draw
78   int x = sidemargin;    // x coord for the text
79
80   // find a string that will fit inside the area for text
81   int max_length = width() - sidemargin * 2;
82   if (max_length <= 0) {
83     t = ""; // can't fit anything
84   } else {
85     size_t text_len = t.size();
86     int length;
87       
88     do {
89       t.resize(text_len);
90       length = ft->measureString(t);
91     } while (length > max_length && text_len-- > 0);
92
93     // justify the text
94     switch (style()->labelTextJustify()) {
95     case RenderStyle::RightJustify:
96       x += max_length - length;
97       break;
98     case RenderStyle::CenterJustify:
99       x += (max_length - length) / 2;
100       break;
101     case RenderStyle::LeftJustify:
102       break;
103     }
104   }
105
106   display->renderControl(_screen)->
107     drawString(*_surface, *ft, x, 0, *text_color, t);
108 }
109
110 }