updated changelog
[wmaker-crm.git] / WINGs / wframe.c
blob8cc5acc4d2197c1c9ba322ebe7694e14554d21e9
2 #include "WINGsP.h"
5 typedef struct W_Frame {
6 W_Class widgetClass;
7 W_View *view;
9 char *caption;
11 struct {
12 WMReliefType relief:4;
13 WMTitlePosition titlePosition:4;
14 } flags;
15 } Frame;
18 #define DEFAULT_RELIEF WRGroove
19 #define DEFAULT_TITLE_POSITION WTPAtTop
20 #define DEFAULT_WIDTH 40
21 #define DEFAULT_HEIGHT 40
24 static void destroyFrame(Frame *fPtr);
25 static void paintFrame(Frame *fPtr);
26 static void repaintFrame(Frame *fPtr);
30 void
31 WMSetFrameTitlePosition(WMFrame *fPtr, WMTitlePosition position)
33 fPtr->flags.titlePosition = position;
35 if (fPtr->view->flags.realized) {
36 repaintFrame(fPtr);
41 void
42 WMSetFrameRelief(WMFrame *fPtr, WMReliefType relief)
44 fPtr->flags.relief = relief;
46 if (fPtr->view->flags.realized) {
47 repaintFrame(fPtr);
52 void
53 WMSetFrameTitle(WMFrame *fPtr, char *title)
55 if (fPtr->caption)
56 wfree(fPtr->caption);
58 if (title)
59 fPtr->caption = wstrdup(title);
60 else
61 fPtr->caption = NULL;
63 if (fPtr->view->flags.realized) {
64 repaintFrame(fPtr);
69 static void
70 repaintFrame(Frame *fPtr)
72 W_View *view = fPtr->view;
73 W_Screen *scrPtr = view->screen;
75 XClearWindow(scrPtr->display, view->window);
76 paintFrame(fPtr);
80 static void
81 paintFrame(Frame *fPtr)
83 W_View *view = fPtr->view;
84 W_Screen *scrPtr = view->screen;
85 WMFont *font = scrPtr->normalFont;
86 Display *display = scrPtr->display;
87 int tx, ty, tw, th, tlen;
88 int fy, fh;
89 Bool drawTitle;
91 if (fPtr->caption!=NULL) {
92 th = WMFontHeight(font);
93 tlen = strlen(fPtr->caption);
94 } else {
95 th = 0;
96 tlen = 0;
99 fh = view->size.height;
100 fy = 0;
102 switch (fPtr->flags.titlePosition) {
103 case WTPAboveTop:
104 ty = 0;
105 fy = th + 4;
106 fh = view->size.height - fy;
107 break;
109 case WTPAtTop:
110 ty = 0;
111 fy = th/2;
112 fh = view->size.height - fy;
113 break;
115 case WTPBelowTop:
116 ty = 4;
117 fy = 0;
118 fh = view->size.height;
119 break;
121 case WTPAboveBottom:
122 ty = view->size.height - th - 4;
123 fy = 0;
124 fh = view->size.height;
125 break;
127 case WTPAtBottom:
128 ty = view->size.height - th;
129 fy = 0;
130 fh = view->size.height - th/2;
131 break;
133 case WTPBelowBottom:
134 ty = view->size.height - th;
135 fy = 0;
136 fh = view->size.height - th - 4;
137 break;
139 default:
140 ty = 0;
141 fy = 0;
142 fh = view->size.height;
145 if (tlen>0 && fPtr->flags.titlePosition!=WTPNoTitle) {
146 tw = WMWidthOfString(font, fPtr->caption, tlen);
148 tx = (view->size.width - tw) / 2;
150 drawTitle = True;
151 } else {
152 tw = tx = 0;
153 drawTitle = False;
157 XRectangle rect;
158 Region region, tmp;
159 GC gc[4];
160 int i;
162 region = XCreateRegion();
164 rect.x = 0;
165 rect.y = 0;
166 rect.width = view->size.width;
167 rect.height = view->size.height;
168 XUnionRectWithRegion(&rect, region, region);
169 if (drawTitle) {
170 tmp = XCreateRegion();
171 rect.x = tx;
172 rect.y = ty;
173 rect.width = tw;
174 rect.height = th;
175 XUnionRectWithRegion(&rect, tmp, tmp);
176 XSubtractRegion(region, tmp, region);
177 XDestroyRegion(tmp);
179 gc[0] = WMColorGC(scrPtr->black);
180 gc[1] = WMColorGC(scrPtr->darkGray);
181 gc[2] = WMColorGC(scrPtr->gray);
182 gc[3] = WMColorGC(scrPtr->white);
184 for (i = 0; i < 4; i++) {
185 XSetRegion(display, gc[i], region);
187 XDestroyRegion(region);
189 W_DrawReliefWithGC(scrPtr, view->window, 0, fy, view->size.width, fh,
190 fPtr->flags.relief, gc[0], gc[1], gc[2], gc[3]);
192 for (i = 0; i < 4; i++) {
193 XSetClipMask(display, gc[i], None);
197 if (drawTitle) {
198 /* can't draw AA text over and over again because it gets messed */
199 /* // TODO create the dbl buffer pixmap when create/set frame title */
200 if (scrPtr->antialiasedText) {
201 Drawable d;
203 d = XCreatePixmap(display, view->window, tw, th, scrPtr->depth);
204 XFillRectangle(display, d, WMColorGC(view->backColor), 0, 0, tw, th);
206 WMDrawString(scrPtr, d, scrPtr->black, font, 0, 0, fPtr->caption, tlen);
207 XCopyArea(display, d, view->window, scrPtr->copyGC, 0, 0, tw, th, tx, ty);
208 XFreePixmap(display, d);
209 } else {
210 WMDrawString(scrPtr, view->window, scrPtr->black, font, tx, ty,
211 fPtr->caption, tlen);
220 static void
221 handleEvents(XEvent *event, void *data)
223 Frame *fPtr = (Frame*)data;
225 CHECK_CLASS(data, WC_Frame);
227 switch (event->type) {
228 case Expose:
229 if (event->xexpose.count == 0)
230 paintFrame(fPtr);
231 break;
233 case DestroyNotify:
234 destroyFrame(fPtr);
235 break;
240 WMFrame*
241 WMCreateFrame(WMWidget *parent)
243 Frame *fPtr;
245 fPtr = wmalloc(sizeof(Frame));
246 memset(fPtr, 0, sizeof(Frame));
248 fPtr->widgetClass = WC_Frame;
250 fPtr->view = W_CreateView(W_VIEW(parent));
251 if (!fPtr->view) {
252 wfree(fPtr);
253 return NULL;
255 fPtr->view->self = fPtr;
257 WMCreateEventHandler(fPtr->view, ExposureMask|StructureNotifyMask,
258 handleEvents, fPtr);
261 fPtr->flags.relief = DEFAULT_RELIEF;
262 fPtr->flags.titlePosition = DEFAULT_TITLE_POSITION;
264 WMResizeWidget(fPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
266 return fPtr;
270 static void
271 destroyFrame(Frame *fPtr)
273 if (fPtr->caption)
274 wfree(fPtr->caption);
276 wfree(fPtr);