1 /*
2  * IceWM
3  *
4  * Copyright (C) 1997-2001 Marko Macek
5  */
6 #include "config.h"
7 
8 #include "ylabel.h"
9 #include "wpixmaps.h"
10 #include "base.h"
11 #include "prefs.h"
12 
13 YColorName YLabel::labelFg(&clrLabelText);
14 YColorName YLabel::labelBg(&clrLabel);
15 YFont YLabel::labelFont;
16 int YLabel::labelObjectCount;
17 
YLabel(const mstring & label,YWindow * parent)18 YLabel::YLabel(const mstring &label, YWindow *parent):
19     YWindow(parent),
20     fLabel(label),
21     fPainted(false)
22 {
23     setParentRelative();
24     setBitGravity(NorthWestGravity);
25 
26     if (labelFont == null)
27         labelFont = labelFontName;
28     ++labelObjectCount;
29 
30     autoSize();
31 }
32 
~YLabel()33 YLabel::~YLabel() {
34     if (--labelObjectCount == 0)
35         labelFont = null;
36 }
37 
handleExpose(const XExposeEvent & expose)38 void YLabel::handleExpose(const XExposeEvent& expose) {
39     if (fPainted == false) {
40         repaint();
41     }
42 }
43 
configure(const YRect2 & r)44 void YLabel::configure(const YRect2& r) {
45     if (visible() && created()) {
46         repaint();
47     }
48 }
49 
repaint()50 void YLabel::repaint() {
51     fPainted = true;
52     GraphicsBuffer(this).paint();
53 }
54 
paint(Graphics & g,const YRect &)55 void YLabel::paint(Graphics &g, const YRect &/*r*/) {
56     ref<YImage> gradient(getGradient());
57 
58     if (gradient != null)
59         g.drawImage(gradient, x() - 1, y() - 1, width(), height(), 0, 0);
60     else
61     if (dialogbackPixmap != null)
62         g.fillPixmap(dialogbackPixmap, 0, 0, width(), height(), x() - 1, y() - 1);
63     else {
64         g.setColor(labelBg);
65         g.fillRect(0, 0, width(), height());
66     }
67 
68     if (fLabel != null && labelFont) {
69         int y = 1 + labelFont->ascent();
70         int x = 1;
71         int h = labelFont->height();
72         mstring s(null), r(null);
73 
74         g.setColor(labelFg);
75         g.setFont(labelFont);
76 
77         for (s = fLabel; s.splitall('\n', &s, &r); s = r) {
78             g.drawChars(s, x, y);
79             y += h;
80         }
81     }
82 }
83 
setText(const char * label)84 void YLabel::setText(const char* label) {
85     fLabel = label;
86     autoSize();
87 }
88 
autoSize()89 void YLabel::autoSize() {
90     int h = labelFont ? labelFont->height() : 8;
91     int w = 0;
92     if (fLabel != null) {
93         int w1;
94         mstring s(null), r(null);
95         int n = 0;
96 
97         for (s = fLabel; s.splitall('\n', &s, &r); s = r) {
98             w1 = labelFont ? labelFont->textWidth(s) : 16;
99             if (w1 > w)
100                 w = w1;
101             n++;
102         }
103         h *= n;
104     }
105     setSize(1 + w + 1, 1 + h + 1);
106 }
107 
108 // vim: set sw=4 ts=4 et:
109