Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wlabel.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
9d2e6ef9 scottc1998-09-29 22:36:29 +00002#include "WINGsP.h"
3
9d2e6ef9 scottc1998-09-29 22:36:29 +00004typedef struct W_Label {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02005 W_Class widgetClass;
6 W_View *view;
6830b057 dan2004-10-12 21:28:27 +00007
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02008 char *caption;
9d2e6ef9 scottc1998-09-29 22:36:29 +00009
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020010 WMColor *textColor;
11 WMFont *font; /* if NULL, use default */
6830b057 dan2004-10-12 21:28:27 +000012
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020013 W_Pixmap *image;
9d2e6ef9 scottc1998-09-29 22:36:29 +000014
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020015 struct {
16 WMReliefType relief:3;
17 WMImagePosition imagePosition:4;
18 WMAlignment alignment:2;
9d2e6ef9 scottc1998-09-29 22:36:29 +000019
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020020 unsigned int noWrap:1;
6830b057 dan2004-10-12 21:28:27 +000021
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020022 unsigned int redrawPending:1;
23 } flags;
9d2e6ef9 scottc1998-09-29 22:36:29 +000024} Label;
25
9d2e6ef9 scottc1998-09-29 22:36:29 +000026#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
31
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020032static void destroyLabel(Label * lPtr);
33static void paintLabel(Label * lPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000034
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020035static void handleEvents(XEvent * event, void *data);
9d2e6ef9 scottc1998-09-29 22:36:29 +000036
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020037WMLabel *WMCreateLabel(WMWidget * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +000038{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020039 Label *lPtr;
6830b057 dan2004-10-12 21:28:27 +000040
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020041 lPtr = wmalloc(sizeof(Label));
42 memset(lPtr, 0, sizeof(Label));
9d2e6ef9 scottc1998-09-29 22:36:29 +000043
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020044 lPtr->widgetClass = WC_Label;
6830b057 dan2004-10-12 21:28:27 +000045
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020046 lPtr->view = W_CreateView(W_VIEW(parent));
47 if (!lPtr->view) {
48 wfree(lPtr);
49 return NULL;
50 }
51 lPtr->view->self = lPtr;
6830b057 dan2004-10-12 21:28:27 +000052
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020053 lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
6830b057 dan2004-10-12 21:28:27 +000054
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020055 WMCreateEventHandler(lPtr->view, ExposureMask | StructureNotifyMask, handleEvents, lPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000056
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020057 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;
9d2e6ef9 scottc1998-09-29 22:36:29 +000062
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020063 return lPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +000064}
65
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020066void WMSetLabelImage(WMLabel * lPtr, WMPixmap * image)
9d2e6ef9 scottc1998-09-29 22:36:29 +000067{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020068 if (lPtr->image != NULL)
69 WMReleasePixmap(lPtr->image);
6830b057 dan2004-10-12 21:28:27 +000070
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020071 if (image)
72 lPtr->image = WMRetainPixmap(image);
73 else
74 lPtr->image = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000075
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020076 if (lPtr->view->flags.realized) {
77 paintLabel(lPtr);
78 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000079}
80
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020081WMPixmap *WMGetLabelImage(WMLabel * lPtr)
0261c326 dan1999-01-06 15:22:33 +000082{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020083 return lPtr->image;
0261c326 dan1999-01-06 15:22:33 +000084}
85
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020086char *WMGetLabelText(WMLabel * lPtr)
e29fce43 kojima2000-07-10 22:37:39 +000087{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088 return lPtr->caption;
e29fce43 kojima2000-07-10 22:37:39 +000089}
90
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020091void WMSetLabelImagePosition(WMLabel * lPtr, WMImagePosition position)
9d2e6ef9 scottc1998-09-29 22:36:29 +000092{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020093 lPtr->flags.imagePosition = position;
94 if (lPtr->view->flags.realized) {
95 paintLabel(lPtr);
96 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000097}
98
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099void WMSetLabelTextAlignment(WMLabel * lPtr, WMAlignment alignment)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000100{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200101 lPtr->flags.alignment = alignment;
102 if (lPtr->view->flags.realized) {
103 paintLabel(lPtr);
104 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000105}
106
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200107void WMSetLabelRelief(WMLabel * lPtr, WMReliefType relief)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200109 lPtr->flags.relief = relief;
110 if (lPtr->view->flags.realized) {
111 paintLabel(lPtr);
112 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000113}
114
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200115void WMSetLabelText(WMLabel * lPtr, char *text)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000116{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200117 if (lPtr->caption)
118 wfree(lPtr->caption);
119
120 if (text != NULL) {
121 lPtr->caption = wstrdup(text);
122 } else {
123 lPtr->caption = NULL;
124 }
125 if (lPtr->view->flags.realized) {
126 paintLabel(lPtr);
127 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000128}
129
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130WMFont *WMGetLabelFont(WMLabel * lPtr)
1a1775ba dan2001-02-26 14:51:17 +0000131{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200132 return lPtr->font;
1a1775ba dan2001-02-26 14:51:17 +0000133}
134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200135void WMSetLabelFont(WMLabel * lPtr, WMFont * font)
6830b057 dan2004-10-12 21:28:27 +0000136{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 if (lPtr->font != NULL)
138 WMReleaseFont(lPtr->font);
139 if (font)
140 lPtr->font = WMRetainFont(font);
141 else
142 lPtr->font = NULL;
143
144 if (lPtr->view->flags.realized) {
145 paintLabel(lPtr);
146 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000147}
148
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200149void WMSetLabelTextColor(WMLabel * lPtr, WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000150{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200151 if (lPtr->textColor)
152 WMReleaseColor(lPtr->textColor);
153 lPtr->textColor = WMRetainColor(color);
3b28027d kojima2000-04-18 03:31:36 +0000154
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200155 if (lPtr->view->flags.realized) {
156 paintLabel(lPtr);
157 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000158}
159
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200160void WMSetLabelWraps(WMLabel * lPtr, Bool flag)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000161{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200162 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);
167 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000168}
169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170static void paintLabel(Label * lPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200172 W_Screen *scrPtr = lPtr->view->screen;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000173
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200174 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);
179}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000180
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200181static void handleEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000182{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200183 Label *lPtr = (Label *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000184
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200185 CHECK_CLASS(data, WC_Label);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200187 switch (event->type) {
188 case Expose:
189 if (event->xexpose.count != 0)
190 break;
191 paintLabel(lPtr);
192 break;
6830b057 dan2004-10-12 21:28:27 +0000193
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200194 case DestroyNotify:
195 destroyLabel(lPtr);
196 break;
197 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000198}
199
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200200static void destroyLabel(Label * lPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000201{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200202 if (lPtr->textColor)
203 WMReleaseColor(lPtr->textColor);
6830b057 dan2004-10-12 21:28:27 +0000204
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200205 if (lPtr->caption)
206 wfree(lPtr->caption);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000207
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200208 if (lPtr->font)
209 WMReleaseFont(lPtr->font);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000210
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200211 if (lPtr->image)
212 WMReleasePixmap(lPtr->image);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000213
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200214 wfree(lPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000215}