Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wlabel.c
1
2 #include "WINGsP.h"
3
4 typedef struct W_Label {
5 W_Class widgetClass;
6 W_View *view;
7
8 char *caption;
9
10 WMColor *textColor;
11 WMFont *font; /* if NULL, use default */
12
13 W_Pixmap *image;
14
15 struct {
16 WMReliefType relief:3;
17 WMImagePosition imagePosition:4;
18 WMAlignment alignment:2;
19
20 unsigned int noWrap:1;
21
22 unsigned int redrawPending:1;
23 } flags;
24 } Label;
25
26 #define DEFAULT_WIDTH 60
27 #define DEFAULT_HEIGHT 14
28 #define DEFAULT_ALIGNMENT WALeft
29 #define DEFAULT_RELIEF WRFlat
30 #define DEFAULT_IMAGE_POSITION WIPNoImage
31
32 static void destroyLabel(Label * lPtr);
33 static void paintLabel(Label * lPtr);
34
35 static void handleEvents(XEvent * event, void *data);
36
37 WMLabel *WMCreateLabel(WMWidget * parent)
38 {
39 Label *lPtr;
40
41 lPtr = wmalloc(sizeof(Label));
42 memset(lPtr, 0, sizeof(Label));
43
44 lPtr->widgetClass = WC_Label;
45
46 lPtr->view = W_CreateView(W_VIEW(parent));
47 if (!lPtr->view) {
48 wfree(lPtr);
49 return NULL;
50 }
51 lPtr->view->self = lPtr;
52
53 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
54
55 WMCreateEventHandler(lPtr->view, ExposureMask | StructureNotifyMask, handleEvents, lPtr);
56
57 W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
58 lPtr->flags.alignment = DEFAULT_ALIGNMENT;
59 lPtr->flags.relief = DEFAULT_RELIEF;
60 lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
61 lPtr->flags.noWrap = 1;
62
63 return lPtr;
64 }
65
66 void WMSetLabelImage(WMLabel * lPtr, WMPixmap * image)
67 {
68 if (lPtr->image != NULL)
69 WMReleasePixmap(lPtr->image);
70
71 if (image)
72 lPtr->image = WMRetainPixmap(image);
73 else
74 lPtr->image = NULL;
75
76 if (lPtr->view->flags.realized) {
77 paintLabel(lPtr);
78 }
79 }
80
81 WMPixmap *WMGetLabelImage(WMLabel * lPtr)
82 {
83 return lPtr->image;
84 }
85
86 char *WMGetLabelText(WMLabel * lPtr)
87 {
88 return lPtr->caption;
89 }
90
91 void WMSetLabelImagePosition(WMLabel * lPtr, WMImagePosition position)
92 {
93 lPtr->flags.imagePosition = position;
94 if (lPtr->view->flags.realized) {
95 paintLabel(lPtr);
96 }
97 }
98
99 void WMSetLabelTextAlignment(WMLabel * lPtr, WMAlignment alignment)
100 {
101 lPtr->flags.alignment = alignment;
102 if (lPtr->view->flags.realized) {
103 paintLabel(lPtr);
104 }
105 }
106
107 void WMSetLabelRelief(WMLabel * lPtr, WMReliefType relief)
108 {
109 lPtr->flags.relief = relief;
110 if (lPtr->view->flags.realized) {
111 paintLabel(lPtr);
112 }
113 }
114
115 void WMSetLabelText(WMLabel * lPtr, char *text)
116 {
117 if (lPtr->caption)
118 wfree(lPtr->caption);
119
120 if (text != NULL) {
121 lPtr->caption = wstrdup(text);
122 } else {
123 lPtr->caption = NULL;
124 }
125 if (lPtr->view->flags.realized) {
126 paintLabel(lPtr);
127 }
128 }
129
130 WMFont *WMGetLabelFont(WMLabel * lPtr)
131 {
132 return lPtr->font;
133 }
134
135 void WMSetLabelFont(WMLabel * lPtr, WMFont * font)
136 {
137 if (lPtr->font != NULL)
138 WMReleaseFont(lPtr->font);
139 if (font)
140 lPtr->font = WMRetainFont(font);
141 else
142 lPtr->font = NULL;
143
144 if (lPtr->view->flags.realized) {
145 paintLabel(lPtr);
146 }
147 }
148
149 void WMSetLabelTextColor(WMLabel * lPtr, WMColor * color)
150 {
151 if (lPtr->textColor)
152 WMReleaseColor(lPtr->textColor);
153 lPtr->textColor = WMRetainColor(color);
154
155 if (lPtr->view->flags.realized) {
156 paintLabel(lPtr);
157 }
158 }
159
160 void WMSetLabelWraps(WMLabel * lPtr, Bool flag)
161 {
162 flag = ((flag == 0) ? 0 : 1);
163 if (lPtr->flags.noWrap != !flag) {
164 lPtr->flags.noWrap = !flag;
165 if (lPtr->view->flags.realized)
166 paintLabel(lPtr);
167 }
168 }
169
170 static void paintLabel(Label * lPtr)
171 {
172 W_Screen *scrPtr = lPtr->view->screen;
173
174 W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap,
175 lPtr->textColor ? lPtr->textColor : scrPtr->black,
176 (lPtr->font != NULL ? lPtr->font : scrPtr->normalFont),
177 lPtr->flags.relief, lPtr->caption,
178 lPtr->flags.alignment, lPtr->image, lPtr->flags.imagePosition, NULL, 0);
179 }
180
181 static void handleEvents(XEvent * event, void *data)
182 {
183 Label *lPtr = (Label *) data;
184
185 CHECK_CLASS(data, WC_Label);
186
187 switch (event->type) {
188 case Expose:
189 if (event->xexpose.count != 0)
190 break;
191 paintLabel(lPtr);
192 break;
193
194 case DestroyNotify:
195 destroyLabel(lPtr);
196 break;
197 }
198 }
199
200 static void destroyLabel(Label * lPtr)
201 {
202 if (lPtr->textColor)
203 WMReleaseColor(lPtr->textColor);
204
205 if (lPtr->caption)
206 wfree(lPtr->caption);
207
208 if (lPtr->font)
209 WMReleaseFont(lPtr->font);
210
211 if (lPtr->image)
212 WMReleasePixmap(lPtr->image);
213
214 wfree(lPtr);
215 }