WINGs: Place prototype of functions in WINGsP.h instead of local definition
[wmaker-crm.git] / WINGs / wlabel.c
blob9726cb15efe521eff5d4a4bab769acd7d9a18619
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)
39 Label *lPtr;
41 lPtr = wmalloc(sizeof(Label));
43 lPtr->widgetClass = WC_Label;
45 lPtr->view = W_CreateView(W_VIEW(parent));
46 if (!lPtr->view) {
47 wfree(lPtr);
48 return NULL;
50 lPtr->view->self = lPtr;
52 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
54 WMCreateEventHandler(lPtr->view, ExposureMask | StructureNotifyMask, handleEvents, lPtr);
56 W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
57 lPtr->flags.alignment = DEFAULT_ALIGNMENT;
58 lPtr->flags.relief = DEFAULT_RELIEF;
59 lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
60 lPtr->flags.noWrap = 1;
62 return lPtr;
65 void WMSetLabelImage(WMLabel * lPtr, WMPixmap * image)
67 if (lPtr->image != NULL)
68 WMReleasePixmap(lPtr->image);
70 if (image)
71 lPtr->image = WMRetainPixmap(image);
72 else
73 lPtr->image = NULL;
75 if (lPtr->view->flags.realized) {
76 paintLabel(lPtr);
80 WMPixmap *WMGetLabelImage(WMLabel * lPtr)
82 return lPtr->image;
85 char *WMGetLabelText(WMLabel * lPtr)
87 return lPtr->caption;
90 void WMSetLabelImagePosition(WMLabel * lPtr, WMImagePosition position)
92 lPtr->flags.imagePosition = position;
93 if (lPtr->view->flags.realized) {
94 paintLabel(lPtr);
98 void WMSetLabelTextAlignment(WMLabel * lPtr, WMAlignment alignment)
100 lPtr->flags.alignment = alignment;
101 if (lPtr->view->flags.realized) {
102 paintLabel(lPtr);
106 void WMSetLabelRelief(WMLabel * lPtr, WMReliefType relief)
108 lPtr->flags.relief = relief;
109 if (lPtr->view->flags.realized) {
110 paintLabel(lPtr);
114 void WMSetLabelText(WMLabel * lPtr, const char *text)
116 if (lPtr->caption)
117 wfree(lPtr->caption);
119 if (text != NULL) {
120 lPtr->caption = wstrdup(text);
121 } else {
122 lPtr->caption = NULL;
124 if (lPtr->view->flags.realized) {
125 paintLabel(lPtr);
129 WMFont *WMGetLabelFont(WMLabel * lPtr)
131 return lPtr->font;
134 void WMSetLabelFont(WMLabel * lPtr, WMFont * font)
136 if (lPtr->font != NULL)
137 WMReleaseFont(lPtr->font);
138 if (font)
139 lPtr->font = WMRetainFont(font);
140 else
141 lPtr->font = NULL;
143 if (lPtr->view->flags.realized) {
144 paintLabel(lPtr);
148 void WMSetLabelTextColor(WMLabel * lPtr, WMColor * color)
150 if (lPtr->textColor)
151 WMReleaseColor(lPtr->textColor);
152 lPtr->textColor = WMRetainColor(color);
154 if (lPtr->view->flags.realized) {
155 paintLabel(lPtr);
159 void WMSetLabelWraps(WMLabel * lPtr, Bool flag)
161 flag = ((flag == 0) ? 0 : 1);
162 if (lPtr->flags.noWrap != !flag) {
163 lPtr->flags.noWrap = !flag;
164 if (lPtr->view->flags.realized)
165 paintLabel(lPtr);
169 static void paintLabel(Label * lPtr)
171 W_Screen *scrPtr = lPtr->view->screen;
173 W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap,
174 lPtr->textColor ? lPtr->textColor : scrPtr->black,
175 (lPtr->font != NULL ? lPtr->font : scrPtr->normalFont),
176 lPtr->flags.relief, lPtr->caption,
177 lPtr->flags.alignment, lPtr->image, lPtr->flags.imagePosition, NULL, 0);
180 static void handleEvents(XEvent * event, void *data)
182 Label *lPtr = (Label *) data;
184 CHECK_CLASS(data, WC_Label);
186 switch (event->type) {
187 case Expose:
188 if (event->xexpose.count != 0)
189 break;
190 paintLabel(lPtr);
191 break;
193 case DestroyNotify:
194 destroyLabel(lPtr);
195 break;
199 static void destroyLabel(Label * lPtr)
201 if (lPtr->textColor)
202 WMReleaseColor(lPtr->textColor);
204 if (lPtr->caption)
205 wfree(lPtr->caption);
207 if (lPtr->font)
208 WMReleaseFont(lPtr->font);
210 if (lPtr->image)
211 WMReleasePixmap(lPtr->image);
213 wfree(lPtr);