Fixes missing appicons after restarting Window Maker
[wmaker-crm.git] / WINGs / wframe.c
blobad2fc0172795226d03a450ef3672ca49ce288c14
2 #include "WINGsP.h"
4 typedef struct W_Frame {
5 W_Class widgetClass;
6 W_View *view;
8 char *caption;
9 WMColor *textColor;
11 struct {
12 WMReliefType relief:4;
13 WMTitlePosition titlePosition:4;
14 } flags;
15 } Frame;
17 #define DEFAULT_RELIEF WRGroove
18 #define DEFAULT_TITLE_POSITION WTPAtTop
19 #define DEFAULT_WIDTH 40
20 #define DEFAULT_HEIGHT 40
22 static void destroyFrame(Frame * fPtr);
23 static void paintFrame(Frame * fPtr);
24 static void repaintFrame(Frame * fPtr);
26 void WMSetFrameTitlePosition(WMFrame * fPtr, WMTitlePosition position)
28 fPtr->flags.titlePosition = position;
30 if (fPtr->view->flags.realized) {
31 repaintFrame(fPtr);
35 void WMSetFrameRelief(WMFrame * fPtr, WMReliefType relief)
37 fPtr->flags.relief = relief;
39 if (fPtr->view->flags.realized) {
40 repaintFrame(fPtr);
44 void WMSetFrameTitle(WMFrame * fPtr, const char *title)
46 if (fPtr->caption)
47 wfree(fPtr->caption);
49 if (title)
50 fPtr->caption = wstrdup(title);
51 else
52 fPtr->caption = NULL;
54 if (fPtr->view->flags.realized) {
55 repaintFrame(fPtr);
59 static void repaintFrame(Frame * fPtr)
61 W_View *view = fPtr->view;
62 W_Screen *scrPtr = view->screen;
64 XClearWindow(scrPtr->display, view->window);
65 paintFrame(fPtr);
68 static void paintFrame(Frame * fPtr)
70 W_View *view = fPtr->view;
71 W_Screen *scrPtr = view->screen;
72 WMFont *font = scrPtr->normalFont;
73 Display *display = scrPtr->display;
74 int tx, ty, tw, th, tlen;
75 int fy, fh;
76 Bool drawTitle;
78 if (fPtr->caption != NULL) {
79 th = WMFontHeight(font);
80 tlen = strlen(fPtr->caption);
81 } else {
82 th = 0;
83 tlen = 0;
86 switch (fPtr->flags.titlePosition) {
87 case WTPAboveTop:
88 ty = 0;
89 fy = th + 4;
90 fh = view->size.height - fy;
91 break;
93 case WTPAtTop:
94 ty = 0;
95 fy = th / 2;
96 fh = view->size.height - fy;
97 break;
99 case WTPBelowTop:
100 ty = 4;
101 fy = 0;
102 fh = view->size.height;
103 break;
105 case WTPAboveBottom:
106 ty = view->size.height - th - 4;
107 fy = 0;
108 fh = view->size.height;
109 break;
111 case WTPAtBottom:
112 ty = view->size.height - th;
113 fy = 0;
114 fh = view->size.height - th / 2;
115 break;
117 case WTPBelowBottom:
118 ty = view->size.height - th;
119 fy = 0;
120 fh = view->size.height - th - 4;
121 break;
123 default:
124 ty = 0;
125 fy = 0;
126 fh = view->size.height;
129 if (tlen > 0 && fPtr->flags.titlePosition != WTPNoTitle) {
130 tw = WMWidthOfString(font, fPtr->caption, tlen);
132 tx = (view->size.width - tw) / 2;
134 drawTitle = True;
135 } else {
136 tw = tx = 0;
137 drawTitle = False;
141 XRectangle rect;
142 Region region, tmp;
143 GC gc[4];
144 int i;
146 region = XCreateRegion();
148 rect.x = 0;
149 rect.y = 0;
150 rect.width = view->size.width;
151 rect.height = view->size.height;
152 XUnionRectWithRegion(&rect, region, region);
153 if (drawTitle) {
154 tmp = XCreateRegion();
155 rect.x = tx;
156 rect.y = ty;
157 rect.width = tw;
158 rect.height = th;
159 XUnionRectWithRegion(&rect, tmp, tmp);
160 XSubtractRegion(region, tmp, region);
161 XDestroyRegion(tmp);
163 gc[0] = WMColorGC(scrPtr->black);
164 gc[1] = WMColorGC(scrPtr->darkGray);
165 gc[2] = WMColorGC(scrPtr->gray);
166 gc[3] = WMColorGC(scrPtr->white);
168 for (i = 0; i < 4; i++) {
169 XSetRegion(display, gc[i], region);
171 XDestroyRegion(region);
173 W_DrawReliefWithGC(scrPtr, view->window, 0, fy, view->size.width, fh,
174 fPtr->flags.relief, gc[0], gc[1], gc[2], gc[3]);
176 for (i = 0; i < 4; i++) {
177 XSetClipMask(display, gc[i], None);
181 if (drawTitle) {
182 /* can't draw AA text over and over again because it gets messed */
183 /* // TODO create the dbl buffer pixmap when create/set frame title */
184 if (scrPtr->antialiasedText) {
185 Drawable d;
187 d = XCreatePixmap(display, view->window, tw, th, scrPtr->depth);
188 XFillRectangle(display, d, WMColorGC(view->backColor), 0, 0, tw, th);
190 WMDrawString(scrPtr, d, fPtr->textColor ? fPtr->textColor : scrPtr->black, font, 0, 0, fPtr->caption, tlen);
191 XCopyArea(display, d, view->window, scrPtr->copyGC, 0, 0, tw, th, tx, ty);
192 XFreePixmap(display, d);
193 } else {
194 WMDrawString(scrPtr, view->window, scrPtr->black, font, tx, ty, fPtr->caption, tlen);
199 static void handleEvents(XEvent * event, void *data)
201 Frame *fPtr = (Frame *) data;
203 CHECK_CLASS(data, WC_Frame);
205 switch (event->type) {
206 case Expose:
207 if (event->xexpose.count == 0)
208 paintFrame(fPtr);
209 break;
211 case DestroyNotify:
212 destroyFrame(fPtr);
213 break;
217 void WMSetFrameTitleColor(WMFrame *fPtr, WMColor *color)
219 if (fPtr->textColor)
220 WMReleaseColor(fPtr->textColor);
221 fPtr->textColor = WMRetainColor(color);
223 if (fPtr->view->flags.realized) {
224 repaintFrame(fPtr);
228 WMFrame *WMCreateFrame(WMWidget * parent)
230 Frame *fPtr;
232 fPtr = wmalloc(sizeof(Frame));
234 fPtr->widgetClass = WC_Frame;
236 fPtr->view = W_CreateView(W_VIEW(parent));
237 if (!fPtr->view) {
238 wfree(fPtr);
239 return NULL;
241 fPtr->view->self = fPtr;
242 fPtr->textColor = WMRetainColor(fPtr->view->screen->black);
244 WMCreateEventHandler(fPtr->view, ExposureMask | StructureNotifyMask, handleEvents, fPtr);
246 fPtr->flags.relief = DEFAULT_RELIEF;
247 fPtr->flags.titlePosition = DEFAULT_TITLE_POSITION;
249 WMResizeWidget(fPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
251 return fPtr;
254 static void destroyFrame(Frame * fPtr)
256 if (fPtr->caption)
257 wfree(fPtr->caption);
259 wfree(fPtr);