Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wframe.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
2#include "WINGsP.h"
3
9d2e6ef9 scottc1998-09-29 22:36:29 +00004typedef struct W_Frame {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02005 W_Class widgetClass;
6 W_View *view;
9d2e6ef9 scottc1998-09-29 22:36:29 +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 struct {
11 WMReliefType relief:4;
12 WMTitlePosition titlePosition:4;
13 } flags;
9d2e6ef9 scottc1998-09-29 22:36:29 +000014} Frame;
15
9d2e6ef9 scottc1998-09-29 22:36:29 +000016#define DEFAULT_RELIEF WRGroove
17#define DEFAULT_TITLE_POSITION WTPAtTop
18#define DEFAULT_WIDTH 40
19#define DEFAULT_HEIGHT 40
20
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020021static void destroyFrame(Frame * fPtr);
22static void paintFrame(Frame * fPtr);
23static void repaintFrame(Frame * fPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000024
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020025void WMSetFrameTitlePosition(WMFrame * fPtr, WMTitlePosition position)
9d2e6ef9 scottc1998-09-29 22:36:29 +000026{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020027 fPtr->flags.titlePosition = position;
ebf002b8 kojima2000-06-09 02:15:38 +000028
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020029 if (fPtr->view->flags.realized) {
30 repaintFrame(fPtr);
31 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000032}
33
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034void WMSetFrameRelief(WMFrame * fPtr, WMReliefType relief)
9d2e6ef9 scottc1998-09-29 22:36:29 +000035{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036 fPtr->flags.relief = relief;
1e922744 dan2002-10-16 04:05:45 +000037
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020038 if (fPtr->view->flags.realized) {
39 repaintFrame(fPtr);
40 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000041}
42
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043void WMSetFrameTitle(WMFrame * fPtr, char *title)
9d2e6ef9 scottc1998-09-29 22:36:29 +000044{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020045 if (fPtr->caption)
46 wfree(fPtr->caption);
1e922744 dan2002-10-16 04:05:45 +000047
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020048 if (title)
49 fPtr->caption = wstrdup(title);
50 else
51 fPtr->caption = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000052
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020053 if (fPtr->view->flags.realized) {
54 repaintFrame(fPtr);
55 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000056}
57
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020058static void repaintFrame(Frame * fPtr)
ebf002b8 kojima2000-06-09 02:15:38 +000059{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020060 W_View *view = fPtr->view;
61 W_Screen *scrPtr = view->screen;
ebf002b8 kojima2000-06-09 02:15:38 +000062
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020063 XClearWindow(scrPtr->display, view->window);
64 paintFrame(fPtr);
ebf002b8 kojima2000-06-09 02:15:38 +000065}
66
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020067static void paintFrame(Frame * fPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000068{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020069 W_View *view = fPtr->view;
70 W_Screen *scrPtr = view->screen;
71 WMFont *font = scrPtr->normalFont;
72 Display *display = scrPtr->display;
73 int tx, ty, tw, th, tlen;
74 int fy, fh;
75 Bool drawTitle;
76
77 if (fPtr->caption != NULL) {
78 th = WMFontHeight(font);
79 tlen = strlen(fPtr->caption);
80 } else {
81 th = 0;
82 tlen = 0;
83 }
84
85 fh = view->size.height;
86 fy = 0;
87
88 switch (fPtr->flags.titlePosition) {
89 case WTPAboveTop:
90 ty = 0;
91 fy = th + 4;
92 fh = view->size.height - fy;
93 break;
94
95 case WTPAtTop:
96 ty = 0;
97 fy = th / 2;
98 fh = view->size.height - fy;
99 break;
100
101 case WTPBelowTop:
102 ty = 4;
103 fy = 0;
104 fh = view->size.height;
105 break;
106
107 case WTPAboveBottom:
108 ty = view->size.height - th - 4;
109 fy = 0;
110 fh = view->size.height;
111 break;
112
113 case WTPAtBottom:
114 ty = view->size.height - th;
115 fy = 0;
116 fh = view->size.height - th / 2;
117 break;
118
119 case WTPBelowBottom:
120 ty = view->size.height - th;
121 fy = 0;
122 fh = view->size.height - th - 4;
123 break;
124
125 default:
126 ty = 0;
127 fy = 0;
128 fh = view->size.height;
129 }
130
131 if (tlen > 0 && fPtr->flags.titlePosition != WTPNoTitle) {
132 tw = WMWidthOfString(font, fPtr->caption, tlen);
133
134 tx = (view->size.width - tw) / 2;
135
136 drawTitle = True;
137 } else {
138 tw = tx = 0;
139 drawTitle = False;
140 }
141
142 {
143 XRectangle rect;
144 Region region, tmp;
145 GC gc[4];
146 int i;
147
148 region = XCreateRegion();
149
150 rect.x = 0;
151 rect.y = 0;
152 rect.width = view->size.width;
153 rect.height = view->size.height;
154 XUnionRectWithRegion(&rect, region, region);
155 if (drawTitle) {
156 tmp = XCreateRegion();
157 rect.x = tx;
158 rect.y = ty;
159 rect.width = tw;
160 rect.height = th;
161 XUnionRectWithRegion(&rect, tmp, tmp);
162 XSubtractRegion(region, tmp, region);
163 XDestroyRegion(tmp);
164 }
165 gc[0] = WMColorGC(scrPtr->black);
166 gc[1] = WMColorGC(scrPtr->darkGray);
167 gc[2] = WMColorGC(scrPtr->gray);
168 gc[3] = WMColorGC(scrPtr->white);
169
170 for (i = 0; i < 4; i++) {
171 XSetRegion(display, gc[i], region);
172 }
173 XDestroyRegion(region);
174
175 W_DrawReliefWithGC(scrPtr, view->window, 0, fy, view->size.width, fh,
176 fPtr->flags.relief, gc[0], gc[1], gc[2], gc[3]);
177
178 for (i = 0; i < 4; i++) {
179 XSetClipMask(display, gc[i], None);
180 }
181 }
182
183 if (drawTitle) {
184 /* can't draw AA text over and over again because it gets messed */
185 /* // TODO create the dbl buffer pixmap when create/set frame title */
186 if (scrPtr->antialiasedText) {
187 Drawable d;
188
189 d = XCreatePixmap(display, view->window, tw, th, scrPtr->depth);
190 XFillRectangle(display, d, WMColorGC(view->backColor), 0, 0, tw, th);
191
192 WMDrawString(scrPtr, d, scrPtr->black, font, 0, 0, fPtr->caption, tlen);
193 XCopyArea(display, d, view->window, scrPtr->copyGC, 0, 0, tw, th, tx, ty);
194 XFreePixmap(display, d);
195 } else {
196 WMDrawString(scrPtr, view->window, scrPtr->black, font, tx, ty, fPtr->caption, tlen);
197 }
198 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000199}
200
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200201static void handleEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000202{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200203 Frame *fPtr = (Frame *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000204
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200205 CHECK_CLASS(data, WC_Frame);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000206
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200207 switch (event->type) {
208 case Expose:
209 if (event->xexpose.count == 0)
210 paintFrame(fPtr);
211 break;
e7d0c5d9 dan2004-04-07 02:50:52 +0000212
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200213 case DestroyNotify:
214 destroyFrame(fPtr);
215 break;
216 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000217}
218
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200219WMFrame *WMCreateFrame(WMWidget * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000220{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200221 Frame *fPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000222
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200223 fPtr = wmalloc(sizeof(Frame));
224 memset(fPtr, 0, sizeof(Frame));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000225
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200226 fPtr->widgetClass = WC_Frame;
e7d0c5d9 dan2004-04-07 02:50:52 +0000227
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200228 fPtr->view = W_CreateView(W_VIEW(parent));
229 if (!fPtr->view) {
230 wfree(fPtr);
231 return NULL;
232 }
233 fPtr->view->self = fPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000234
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200235 WMCreateEventHandler(fPtr->view, ExposureMask | StructureNotifyMask, handleEvents, fPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000236
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200237 fPtr->flags.relief = DEFAULT_RELIEF;
238 fPtr->flags.titlePosition = DEFAULT_TITLE_POSITION;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000239
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200240 WMResizeWidget(fPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
e7d0c5d9 dan2004-04-07 02:50:52 +0000241
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200242 return fPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000243}
244
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200245static void destroyFrame(Frame * fPtr)
e7d0c5d9 dan2004-04-07 02:50:52 +0000246{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200247 if (fPtr->caption)
248 wfree(fPtr->caption);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000249
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200250 wfree(fPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000251}