04b6147b010338cf5992e06817dba91d05e8c338
[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    }
65
66    ~Label()
67    {
68       if(window)
69       {
70          stopwatching(window, text);
71          delete window;
72       }
73    }
74
75    bool OnResizing(int *w, int *h)
76    {
77       if(!*w || !*h)
78       {
79          Window labeledWindow = text ? this : window;
80          char * string = (labeledWindow && labeledWindow.text) ? labeledWindow.text : " ";
81
82          int width = 0, height = 0;
83
84          display.FontExtent(fontObject, string, strlen(string), &width, &height);
85          if(labeledWindow && labeledWindow.text)
86             *w = Max(width, *w);
87          else
88             *w = Max(width, 80);
89          *h = Max(height, *h);
90       }
91       return true;
92    }
93
94    bool OnLeftButtonDown(int x, int y, Modifiers mods)
95    {
96       if(window && window.visible && window.created)
97          window.Activate();
98       return isGroupBox;
99    }
100
101    void OnRedraw(Surface surface)
102    {
103       Window labeledWindow = text ? this : window;
104       int offset = 0;
105
106       if(isGroupBox)
107       {
108          surface.Bevel(true,  1,7, clientSize.w - 2, clientSize.h - 8);
109          surface.Bevel(false, 0,6, clientSize.w, clientSize.h - 6);
110          if(parent.background)
111          {
112             surface.TextOpacity(true);
113             surface.SetBackground(parent.background);
114          }
115          offset = 8;
116       }
117       if(labeledWindow)
118          labeledWindow.WriteCaption(surface, offset, 0);
119    }
120
121    watch(font) { SetInitSize(initSize); };
122    watch(text) { SetInitSize(initSize); };
123
124    Window window;
125    bool isGroupBox;
126 }