Yet another try to get 0.51.2-pre2 in cvs
[wmaker-crm.git] / WINGs / wlabel.c
blob609ed7c9fe8ab946e308443b03045b19cf2a59e9
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;
33 W_ViewProcedureTable _LabelViewProcedures = {
34 NULL,
35 NULL,
36 NULL
40 #define DEFAULT_WIDTH 60
41 #define DEFAULT_HEIGHT 14
42 #define DEFAULT_ALIGNMENT WALeft
43 #define DEFAULT_RELIEF WRFlat
44 #define DEFAULT_IMAGE_POSITION WIPNoImage
47 static void destroyLabel(Label *lPtr);
48 static void paintLabel(Label *lPtr);
51 static void handleEvents(XEvent *event, void *data);
54 WMLabel*
55 WMCreateLabel(WMWidget *parent)
57 Label *lPtr;
59 lPtr = wmalloc(sizeof(Label));
60 memset(lPtr, 0, sizeof(Label));
62 lPtr->widgetClass = WC_Label;
64 lPtr->view = W_CreateView(W_VIEW(parent));
65 if (!lPtr->view) {
66 free(lPtr);
67 return NULL;
69 lPtr->view->self = lPtr;
71 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
73 WMCreateEventHandler(lPtr->view, ExposureMask|StructureNotifyMask,
74 handleEvents, lPtr);
76 W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
77 lPtr->flags.alignment = DEFAULT_ALIGNMENT;
78 lPtr->flags.relief = DEFAULT_RELIEF;
79 lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
81 return lPtr;
85 void
86 WMSetLabelImage(WMLabel *lPtr, WMPixmap *image)
88 if (lPtr->image!=NULL)
89 WMReleasePixmap(lPtr->image);
91 if (image)
92 lPtr->image = WMRetainPixmap(image);
93 else
94 lPtr->image = NULL;
96 if (lPtr->view->flags.realized) {
97 paintLabel(lPtr);
102 WMPixmap*
103 WMGetLabelImage(WMLabel *lPtr)
105 return lPtr->image;
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 free(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 void
157 WMSetLabelFont(WMLabel *lPtr, WMFont *font)
159 if (lPtr->font!=NULL)
160 WMReleaseFont(lPtr->font);
161 if (font)
162 lPtr->font = WMRetainFont(font);
163 else
164 lPtr->font = NULL;
166 if (lPtr->view->flags.realized) {
167 paintLabel(lPtr);
172 void
173 WMSetLabelTextColor(WMLabel *lPtr, WMColor *color)
175 if (lPtr->textColor)
176 WMReleaseColor(lPtr->textColor);
177 lPtr->textColor = WMRetainColor(color);
181 void
182 WMSetLabelWraps(WMLabel *lPtr, Bool flag)
184 if (lPtr->flags.noWrap != !flag) {
185 lPtr->flags.noWrap = !flag;
186 if (lPtr->view->flags.realized)
187 paintLabel(lPtr);
192 static void
193 paintLabel(Label *lPtr)
195 W_Screen *scrPtr = lPtr->view->screen;
196 GC gc;
198 if (lPtr->textColor)
199 gc = WMColorGC(lPtr->textColor);
200 else
201 gc = WMColorGC(scrPtr->black);
203 W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap, gc,
204 (lPtr->font!=NULL ? lPtr->font : scrPtr->normalFont),
205 lPtr->flags.relief, lPtr->caption,
206 lPtr->flags.alignment, lPtr->image,
207 lPtr->flags.imagePosition, NULL, 0);
212 static void
213 handleEvents(XEvent *event, void *data)
215 Label *lPtr = (Label*)data;
217 CHECK_CLASS(data, WC_Label);
220 switch (event->type) {
221 case Expose:
222 if (event->xexpose.count!=0)
223 break;
224 paintLabel(lPtr);
225 break;
227 case DestroyNotify:
228 destroyLabel(lPtr);
229 break;
234 static void
235 destroyLabel(Label *lPtr)
237 if (lPtr->textColor)
238 WMReleaseColor(lPtr->textColor);
240 if (lPtr->caption)
241 free(lPtr->caption);
243 if (lPtr->font)
244 WMReleaseFont(lPtr->font);
246 if (lPtr->image)
247 WMReleasePixmap(lPtr->image);
249 free(lPtr);