removed FocusFollowMouse
[wmaker-crm.git] / WINGs / wlabel.c
blob67d543f17ae407cf6a217fd0b506271c359c8d7f
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 if (lPtr->flags.noWrap != !flag) {
196 lPtr->flags.noWrap = !flag;
197 if (lPtr->view->flags.realized)
198 paintLabel(lPtr);
203 static void
204 paintLabel(Label *lPtr)
206 W_Screen *scrPtr = lPtr->view->screen;
207 GC gc;
209 if (lPtr->textColor)
210 gc = WMColorGC(lPtr->textColor);
211 else
212 gc = WMColorGC(scrPtr->black);
214 W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap, gc,
215 (lPtr->font!=NULL ? lPtr->font : scrPtr->normalFont),
216 lPtr->flags.relief, lPtr->caption,
217 lPtr->flags.alignment, lPtr->image,
218 lPtr->flags.imagePosition, NULL, 0);
223 static void
224 handleEvents(XEvent *event, void *data)
226 Label *lPtr = (Label*)data;
228 CHECK_CLASS(data, WC_Label);
231 switch (event->type) {
232 case Expose:
233 if (event->xexpose.count!=0)
234 break;
235 paintLabel(lPtr);
236 break;
238 case DestroyNotify:
239 destroyLabel(lPtr);
240 break;
245 static void
246 destroyLabel(Label *lPtr)
248 if (lPtr->textColor)
249 WMReleaseColor(lPtr->textColor);
251 if (lPtr->caption)
252 wfree(lPtr->caption);
254 if (lPtr->font)
255 WMReleaseFont(lPtr->font);
257 if (lPtr->image)
258 WMReleasePixmap(lPtr->image);
260 wfree(lPtr);