Fixed synchronization problem between hair cursor and mouse cursor in
[wmaker-crm.git] / WINGs / wlabel.c
blobfd51a464b0df8b556a807092ea4c26c9da566903
5 #include "WINGsP.h"
9 typedef struct W_Label {
10 W_Class widgetClass;
11 W_View *view;
13 char *caption;
15 WMColor *textColor;
16 WMFont *font; /* if NULL, use default */
18 W_Pixmap *image;
20 struct {
21 WMReliefType relief:3;
22 WMImagePosition imagePosition:4;
23 WMAlignment alignment:2;
25 unsigned int noWrap:1;
27 unsigned int redrawPending:1;
28 } flags;
29 } Label;
32 #define DEFAULT_WIDTH 60
33 #define DEFAULT_HEIGHT 14
34 #define DEFAULT_ALIGNMENT WALeft
35 #define DEFAULT_RELIEF WRFlat
36 #define DEFAULT_IMAGE_POSITION WIPNoImage
39 static void destroyLabel(Label *lPtr);
40 static void paintLabel(Label *lPtr);
43 static void handleEvents(XEvent *event, void *data);
46 WMLabel*
47 WMCreateLabel(WMWidget *parent)
49 Label *lPtr;
51 lPtr = wmalloc(sizeof(Label));
52 memset(lPtr, 0, sizeof(Label));
54 lPtr->widgetClass = WC_Label;
56 lPtr->view = W_CreateView(W_VIEW(parent));
57 if (!lPtr->view) {
58 wfree(lPtr);
59 return NULL;
61 lPtr->view->self = lPtr;
63 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
65 WMCreateEventHandler(lPtr->view, ExposureMask|StructureNotifyMask,
66 handleEvents, lPtr);
68 W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
69 lPtr->flags.alignment = DEFAULT_ALIGNMENT;
70 lPtr->flags.relief = DEFAULT_RELIEF;
71 lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
72 lPtr->flags.noWrap = 1;
74 return lPtr;
78 void
79 WMSetLabelImage(WMLabel *lPtr, WMPixmap *image)
81 if (lPtr->image!=NULL)
82 WMReleasePixmap(lPtr->image);
84 if (image)
85 lPtr->image = WMRetainPixmap(image);
86 else
87 lPtr->image = NULL;
89 if (lPtr->view->flags.realized) {
90 paintLabel(lPtr);
95 WMPixmap*
96 WMGetLabelImage(WMLabel *lPtr)
98 return lPtr->image;
102 char *
103 WMGetLabelText(WMLabel *lPtr)
105 return lPtr->caption;
109 void
110 WMSetLabelImagePosition(WMLabel *lPtr, WMImagePosition position)
112 lPtr->flags.imagePosition = position;
113 if (lPtr->view->flags.realized) {
114 paintLabel(lPtr);
119 void
120 WMSetLabelTextAlignment(WMLabel *lPtr, WMAlignment alignment)
122 lPtr->flags.alignment = alignment;
123 if (lPtr->view->flags.realized) {
124 paintLabel(lPtr);
129 void
130 WMSetLabelRelief(WMLabel *lPtr, WMReliefType relief)
132 lPtr->flags.relief = relief;
133 if (lPtr->view->flags.realized) {
134 paintLabel(lPtr);
139 void
140 WMSetLabelText(WMLabel *lPtr, char *text)
142 if (lPtr->caption)
143 wfree(lPtr->caption);
145 if (text!=NULL) {
146 lPtr->caption = wstrdup(text);
147 } else {
148 lPtr->caption = NULL;
150 if (lPtr->view->flags.realized) {
151 paintLabel(lPtr);
156 WMFont*
157 WMGetLabelFont(WMLabel *lPtr)
159 return lPtr->font;
163 void
164 WMSetLabelFont(WMLabel *lPtr, WMFont *font)
166 if (lPtr->font!=NULL)
167 WMReleaseFont(lPtr->font);
168 if (font)
169 lPtr->font = WMRetainFont(font);
170 else
171 lPtr->font = NULL;
173 if (lPtr->view->flags.realized) {
174 paintLabel(lPtr);
179 void
180 WMSetLabelTextColor(WMLabel *lPtr, WMColor *color)
182 if (lPtr->textColor)
183 WMReleaseColor(lPtr->textColor);
184 lPtr->textColor = WMRetainColor(color);
186 if (lPtr->view->flags.realized) {
187 paintLabel(lPtr);
192 void
193 WMSetLabelWraps(WMLabel *lPtr, Bool flag)
195 flag = ((flag==0) ? 0 : 1);
196 if (lPtr->flags.noWrap != !flag) {
197 lPtr->flags.noWrap = !flag;
198 if (lPtr->view->flags.realized)
199 paintLabel(lPtr);
204 static void
205 paintLabel(Label *lPtr)
207 W_Screen *scrPtr = lPtr->view->screen;
209 W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap,
210 lPtr->textColor ? lPtr->textColor : scrPtr->black,
211 (lPtr->font!=NULL ? lPtr->font : scrPtr->normalFont),
212 lPtr->flags.relief, lPtr->caption,
213 lPtr->flags.alignment, lPtr->image,
214 lPtr->flags.imagePosition, NULL, 0);
219 static void
220 handleEvents(XEvent *event, void *data)
222 Label *lPtr = (Label*)data;
224 CHECK_CLASS(data, WC_Label);
227 switch (event->type) {
228 case Expose:
229 if (event->xexpose.count!=0)
230 break;
231 paintLabel(lPtr);
232 break;
234 case DestroyNotify:
235 destroyLabel(lPtr);
236 break;
241 static void
242 destroyLabel(Label *lPtr)
244 if (lPtr->textColor)
245 WMReleaseColor(lPtr->textColor);
247 if (lPtr->caption)
248 wfree(lPtr->caption);
250 if (lPtr->font)
251 WMReleaseFont(lPtr->font);
253 if (lPtr->image)
254 WMReleasePixmap(lPtr->image);
256 wfree(lPtr);