Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wlabel.c
blob8b67d93f4d78816fa3bf73b7b09437e6529d5994
2 #include "WINGsP.h"
4 typedef struct W_Label {
5 W_Class widgetClass;
6 W_View *view;
8 char *caption;
10 WMColor *textColor;
11 WMFont *font; /* if NULL, use default */
13 W_Pixmap *image;
15 struct {
16 WMReliefType relief:3;
17 WMImagePosition imagePosition:4;
18 WMAlignment alignment:2;
20 unsigned int noWrap:1;
22 unsigned int redrawPending:1;
23 } flags;
24 } Label;
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
32 static void destroyLabel(Label * lPtr);
33 static void paintLabel(Label * lPtr);
35 static void handleEvents(XEvent * event, void *data);
37 WMLabel *WMCreateLabel(WMWidget * parent)
38 {
39 Label *lPtr;
41 lPtr = wmalloc(sizeof(Label));
42 memset(lPtr, 0, sizeof(Label));
44 lPtr->widgetClass = WC_Label;
46 lPtr->view = W_CreateView(W_VIEW(parent));
47 if (!lPtr->view) {
48 wfree(lPtr);
49 return NULL;
50 }
51 lPtr->view->self = lPtr;
53 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
55 WMCreateEventHandler(lPtr->view, ExposureMask | StructureNotifyMask, handleEvents, lPtr);
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;
63 return lPtr;
64 }
66 void WMSetLabelImage(WMLabel * lPtr, WMPixmap * image)
67 {
68 if (lPtr->image != NULL)
69 WMReleasePixmap(lPtr->image);
71 if (image)
72 lPtr->image = WMRetainPixmap(image);
73 else
74 lPtr->image = NULL;
76 if (lPtr->view->flags.realized) {
77 paintLabel(lPtr);
78 }
79 }
81 WMPixmap *WMGetLabelImage(WMLabel * lPtr)
82 {
83 return lPtr->image;
84 }
86 char *WMGetLabelText(WMLabel * lPtr)
87 {
88 return lPtr->caption;
89 }
91 void WMSetLabelImagePosition(WMLabel * lPtr, WMImagePosition position)
92 {
93 lPtr->flags.imagePosition = position;
94 if (lPtr->view->flags.realized) {
95 paintLabel(lPtr);
96 }
97 }
99 void WMSetLabelTextAlignment(WMLabel * lPtr, WMAlignment alignment)
101 lPtr->flags.alignment = alignment;
102 if (lPtr->view->flags.realized) {
103 paintLabel(lPtr);
107 void WMSetLabelRelief(WMLabel * lPtr, WMReliefType relief)
109 lPtr->flags.relief = relief;
110 if (lPtr->view->flags.realized) {
111 paintLabel(lPtr);
115 void WMSetLabelText(WMLabel * lPtr, char *text)
117 if (lPtr->caption)
118 wfree(lPtr->caption);
120 if (text != NULL) {
121 lPtr->caption = wstrdup(text);
122 } else {
123 lPtr->caption = NULL;
125 if (lPtr->view->flags.realized) {
126 paintLabel(lPtr);
130 WMFont *WMGetLabelFont(WMLabel * lPtr)
132 return lPtr->font;
135 void WMSetLabelFont(WMLabel * lPtr, WMFont * font)
137 if (lPtr->font != NULL)
138 WMReleaseFont(lPtr->font);
139 if (font)
140 lPtr->font = WMRetainFont(font);
141 else
142 lPtr->font = NULL;
144 if (lPtr->view->flags.realized) {
145 paintLabel(lPtr);
149 void WMSetLabelTextColor(WMLabel * lPtr, WMColor * color)
151 if (lPtr->textColor)
152 WMReleaseColor(lPtr->textColor);
153 lPtr->textColor = WMRetainColor(color);
155 if (lPtr->view->flags.realized) {
156 paintLabel(lPtr);
160 void WMSetLabelWraps(WMLabel * lPtr, Bool flag)
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);
170 static void paintLabel(Label * lPtr)
172 W_Screen *scrPtr = lPtr->view->screen;
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);
181 static void handleEvents(XEvent * event, void *data)
183 Label *lPtr = (Label *) data;
185 CHECK_CLASS(data, WC_Label);
187 switch (event->type) {
188 case Expose:
189 if (event->xexpose.count != 0)
190 break;
191 paintLabel(lPtr);
192 break;
194 case DestroyNotify:
195 destroyLabel(lPtr);
196 break;
200 static void destroyLabel(Label * lPtr)
202 if (lPtr->textColor)
203 WMReleaseColor(lPtr->textColor);
205 if (lPtr->caption)
206 wfree(lPtr->caption);
208 if (lPtr->font)
209 WMReleaseFont(lPtr->font);
211 if (lPtr->image)
212 WMReleasePixmap(lPtr->image);
214 wfree(lPtr);