ecere/gui: Fixed broken caption property watches (they were still referring to 'text'!)
[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          Size size;
21          if(window)
22          {
23             stopwatching(window, text);
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                text { 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, text);
62          delete window;
63       }
64       CommonControl::OnDestroy();
65    }
66
67    ~Label()
68    {
69       if(window)
70       {
71          stopwatching(window, text);
72          delete window;
73       }
74    }
75
76    bool OnResizing(int *w, int *h)
77    {
78       if(!*w || !*h)
79       {
80          Window labeledWindow = text ? this : window;
81          char * string = (labeledWindow && labeledWindow.text) ? labeledWindow.text : " ";
82
83          int width = 0, height = 0;
84
85          display.FontExtent(fontObject, string, strlen(string), &width, &height);
86          if(!*w)
87          {
88             if(labeledWindow && labeledWindow.text)
89                *w = Max(width, *w);
90             else
91                *w = Max(width, 80);
92          }
93          if(!*h)
94             *h = Max(height, *h);
95       }
96       return true;
97    }
98
99    bool OnLeftButtonDown(int x, int y, Modifiers mods)
100    {
101       if(window && window.visible && window.created)
102          window.Activate();
103       return isGroupBox;
104    }
105
106    static void Surface::GroupBevel(bool inner, int x, int y, int w, int h, int tw)
107    {
108       ColorAlpha foreground = this.foreground;
109
110       SetForeground(inner ? Color { 128,128,128 } : formColor);
111
112       HLine(x,   x+8 - 2, y);       // Top
113       HLine(x+8+tw,   x+w - 2, y);    // Top part 2
114
115       VLine(y+1, y+h - 2, x);
116
117       SetForeground(inner ? Color { 64,64,64 } : white);
118
119       HLine(x+1, x+8-3, y+1);       // Top
120       HLine(x+8+tw+1, x+w-3, y+1);    // Top part 2
121       VLine(y+2, y+h-3, x+1);
122
123       SetForeground(inner ? formColor : Color { 128,128,128 } );
124       HLine(x+1, x+w-2, y + h -2);
125       VLine(y+1, y+h-3, x + w - 2);
126
127       SetForeground(inner ? white : Color { 64,64,64 });
128       HLine(x, x+w-1, y + h - 1);
129       VLine(y, y+h-2, x + w - 1);
130
131       SetForeground(foreground);
132    }
133
134    void OnRedraw(Surface surface)
135    {
136       Window labeledWindow = text ? this : window;
137       int offset = 0;
138
139       if(isGroupBox)
140       {
141          if(labeledWindow)
142          {
143             int tw = 0;
144             char * caption = labeledWindow.caption;
145
146             surface.TextExtent(caption, strlen(caption), &tw, null);
147             GroupBevel(surface, true,  1,7, clientSize.w - 2, clientSize.h - 8, tw);
148             GroupBevel(surface, false, 0,6, clientSize.w, clientSize.h - 6, tw);
149          }
150          else
151          {
152             surface.Bevel(true,  1,7, clientSize.w - 2, clientSize.h - 8);
153             surface.Bevel(false, 0,6, clientSize.w, clientSize.h - 6);
154          }
155          /*if(parent.background)
156          {
157             surface.TextOpacity(true);
158             surface.SetBackground(parent.background);
159          }*/
160          offset = 8;
161       }
162       if(labeledWindow)
163          labeledWindow.WriteCaption(surface, offset, 0);
164    }
165
166    watch(font) { SetInitSize(initSize); };
167    watch(caption) { SetInitSize(initSize); };
168
169    Window window;
170    bool isGroupBox;
171 }