]> Dogcows Code - chaz/openbox/blobdiff - otk/label.cc
layout fixes, give widgets a default texture, etc.
[chaz/openbox] / otk / label.cc
index 0c18b5d03cc001616f4d4a87c8ef65a9cd218713..587e321250300a287153bcbd7fcc080b5814e8a1 100644 (file)
@@ -1,8 +1,6 @@
 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
 
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
+#include "config.h"
 
 #include "label.hh"
 #include "display.hh"
@@ -16,7 +14,8 @@ Label::Label(Widget *parent)
   : Widget(parent),
     _text(""),
     _justify_horz(RenderStyle::LeftTopJustify),
-    _justify_vert(RenderStyle::LeftTopJustify)
+    _justify_vert(RenderStyle::LeftTopJustify),
+    _highlight(false)
 {
   styleChanged(*RenderStyle::style(screen()));
 }
@@ -37,18 +36,33 @@ void Label::setVerticalJustify(RenderStyle::Justify j)
   refresh();
 }
 
+void Label::setHighlighted(bool h)
+{
+  _highlight = h;
+  styleChanged(*RenderStyle::style(screen()));
+  refresh();
+}
+
 void Label::setText(const ustring &text)
 {
   bool utf = text.utf8();
   std::string s = text.c_str(); // use a normal string, for its functionality
 
   _parsedtext.clear();
+  _text = text;
   
   // parse it into multiple lines
   std::string::size_type p = 0;
   while (p != std::string::npos) {
     std::string::size_type p2 = s.find('\n', p);
-    _parsedtext.push_back(s.substr(p, (p2==std::string::npos?p2:p2-p)));
+    std::string s(s.substr(p, (p2==std::string::npos?p2:p2-p)));
+
+    // turn tabs into spaces (multiples of 8)
+    std::string::size_type t;
+    while ((t = s.find('\t')) != std::string::npos)
+      s.replace(t, 1, std::string(8 - t % 8, ' '));
+
+    _parsedtext.push_back(s);
     _parsedtext.back().setUtf8(utf);
     p = (p2==std::string::npos?p2:p2+1);
   }
@@ -63,11 +77,12 @@ void Label::setFont(const Font *f)
 
 void Label::calcDefaultSizes()
 {
-  unsigned int longest = 0;
+  int longest = 0;
   // find the longest line
   std::vector<ustring>::iterator it, end = _parsedtext.end();
   for (it = _parsedtext.begin(); it != end; ++it) {
-    unsigned int length = _font->measureString(*it);
+    int length = _font->measureString(*it);
+    if (length < 0) continue; // lines too long get skipped
     if (length > longest) longest = length;
   }
   setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
@@ -77,20 +92,26 @@ void Label::calcDefaultSizes()
   
 void Label::styleChanged(const RenderStyle &style)
 {
-  _texture = style.labelFocusBackground();
-  _forecolor = style.textFocusColor();
-  _font = style.labelFont();
-  Widget::styleChanged(style);
-  calcDefaultSizes();
+  if (_highlight) {
+    _texture = style.labelFocusBackground();
+    _forecolor = style.textFocusColor();
+  } else {
+    _texture = style.labelUnfocusBackground();
+    _forecolor = style.textUnfocusColor();
+  }
+  if (_font != style.labelFont()) {
+    _font = style.labelFont();
+    calcDefaultSizes();
+  }
 }
 
 void Label::renderForeground(Surface &surface)
 {
   const RenderControl *control = display->renderControl(screen());
-  unsigned int sidemargin = bevel() * 2;
+  int sidemargin = bevel() * 2;
   int y = bevel();
-  unsigned int w = area().width() - borderWidth() * 2 - sidemargin * 2;
-  unsigned int h = area().height() - borderWidth() * 2 - bevel() * 2;
+  int w = area().width() - borderWidth() * 2 - sidemargin * 2;
+  int h = area().height() - borderWidth() * 2 - bevel() * 2;
 
   switch (_justify_vert) {
   case RenderStyle::RightBottomJustify:
@@ -114,12 +135,13 @@ void Label::renderForeground(Surface &surface)
 
     // find a string that will fit inside the area for text
     ustring::size_type text_len = t.size();
-    unsigned int length;
+    int length;
       
     do {
       t.resize(text_len);
       length = _font->measureString(t);
     } while (length > w && text_len-- > 0);
+    if (length < 0) continue; // lines too long get skipped
 
     if (text_len <= 0) continue; // won't fit anything
 
This page took 0.028398 seconds and 4 git commands to generate.