60e2219a39d09de0e2d7cd1801d9fa0c88991f52
[sdk] / ecere / src / gui / controls / Label.ec
1 namespace gui::controls;
2
3 import "Window"
4
5 public class Label : CommonControl
6 {
7    class_property(icon) = "<:ecere>controls/label.png";
8
9    inactive = true;
10    clickThrough = true;
11    opacity = 0;
12    drawBehind = true;
13
14 public:
15    property Window labeledWindow
16    {
17       property_category $"Behavior"
18       set
19       {
20          if(window)
21          {
22             stopwatching(window, caption);
23             stopwatching(window, disabled);
24             delete window;
25          }
26          window = value;
27          if(value)
28             incref value;
29
30          SetInitSize(initSize);
31          if(value)
32          {
33             watch(value)
34             {
35                caption { SetInitSize(initSize); }
36                disabled { Update(null); }
37             };
38          }
39       }
40
41       get { return window; }
42    };
43
44    property bool isGroupBox
45    {
46       property_category $"Appearance"
47       set
48       {
49          isGroupBox = value;
50          inactive = !value;
51       }
52       get { return isGroupBox; }
53    };
54
55 private:
56
57    void OnDestroy()
58    {
59       if(window && (window == master || window == parent))
60       {
61          stopwatching(window, caption);
62          stopwatching(window, disabled);
63          delete window;
64       }
65       CommonControl::OnDestroy();
66    }
67
68    ~Label()
69    {
70       if(window)
71       {
72          stopwatching(window, caption);
73          stopwatching(window, disabled);
74          delete window;
75       }
76    }
77
78    bool OnResizing(int *w, int *h)
79    {
80       if(!*w || !*h)
81       {
82          Window labeledWindow = text ? this : window;
83          char * string = (labeledWindow && labeledWindow.text) ? labeledWindow.text : " ";
84
85          int width = 0, height = 0;
86
87          display.FontExtent(fontObject, string, strlen(string), &width, &height);
88          if(!*w)
89          {
90             if(labeledWindow && labeledWindow.text)
91                *w = Max(width, *w);
92             else
93                *w = Max(width, 80);
94          }
95          if(!*h)
96             *h = Max(height, *h);
97       }
98       return true;
99    }
100
101    bool OnLeftButtonDown(int x, int y, Modifiers mods)
102    {
103       if(window && window.visible && window.created && !window.disabled)
104          window.Activate();
105       return isGroupBox;
106    }
107
108    static void Surface::GroupBevel(bool inner, int x, int y, int w, int h, int tw)
109    {
110       ColorAlpha foreground = this.foreground;
111
112       SetForeground(inner ? Color { 128,128,128 } : formColor);
113
114       HLine(x,   x+8 - 2, y);       // Top
115       HLine(x+8+tw,   x+w - 2, y);    // Top part 2
116
117       VLine(y+1, y+h - 2, x);
118
119       SetForeground(inner ? Color { 64,64,64 } : white);
120
121       HLine(x+1, x+8-3, y+1);       // Top
122       HLine(x+8+tw+1, x+w-3, y+1);    // Top part 2
123       VLine(y+2, y+h-3, x+1);
124
125       SetForeground(inner ? formColor : Color { 128,128,128 } );
126       HLine(x+1, x+w-2, y + h -2);
127       VLine(y+1, y+h-3, x + w - 2);
128
129       SetForeground(inner ? white : Color { 64,64,64 });
130       HLine(x, x+w-1, y + h - 1);
131       VLine(y, y+h-2, x + w - 1);
132
133       SetForeground(foreground);
134    }
135
136    void OnRedraw(Surface surface)
137    {
138       Window labeledWindow = text ? this : window;
139       int offset = 0;
140
141       if(isGroupBox)
142       {
143          if(labeledWindow)
144          {
145             int tw = 0;
146             char * caption = labeledWindow.caption;
147             if(caption)
148                surface.TextExtent(caption, strlen(caption), &tw, null);
149             GroupBevel(surface, true,  1,7, clientSize.w - 2, clientSize.h - 8, tw);
150             GroupBevel(surface, false, 0,6, clientSize.w, clientSize.h - 6, tw);
151          }
152          else
153          {
154             surface.Bevel(true,  1,7, clientSize.w - 2, clientSize.h - 8);
155             surface.Bevel(false, 0,6, clientSize.w, clientSize.h - 6);
156          }
157          /*if(parent.background)
158          {
159             surface.TextOpacity(true);
160             surface.SetBackground(parent.background);
161          }*/
162          offset = 8;
163       }
164       if(labeledWindow)
165          labeledWindow.WriteCaption(surface, offset, 0);
166    }
167
168    watch(font) { SetInitSize(initSize); };
169    watch(caption) { SetInitSize(initSize); };
170
171    Window window;
172    bool isGroupBox;
173 }