Yet another try to get 0.51.2-pre2 in cvs
[wmaker-crm.git] / WINGs / wmisc.c
blob7cdbaca1b5623cbf391ae7b24eef9991fd1b31d6
2 #include "WINGsP.h"
4 #include <wraster.h>
5 #include <ctype.h>
8 void
9 W_DrawRelief(W_Screen *scr, Drawable d, int x, int y, unsigned int width,
10 unsigned int height, WMReliefType relief)
12 Display *dpy = scr->display;
13 GC bgc;
14 GC wgc;
15 GC lgc;
16 GC dgc;
18 switch (relief) {
19 case WRSimple:
20 XDrawRectangle(dpy, d, WMColorGC(scr->black), x, y, width-1, height-1);
21 return;
22 break;
24 case WRRaised:
25 bgc = WMColorGC(scr->black);
26 dgc = WMColorGC(scr->darkGray);
27 wgc = WMColorGC(scr->white);
28 lgc = WMColorGC(scr->gray);
29 break;
31 case WRSunken:
32 wgc = WMColorGC(scr->darkGray);
33 lgc = WMColorGC(scr->black);
34 bgc = WMColorGC(scr->white);
35 dgc = WMColorGC(scr->gray);
36 break;
38 case WRPushed:
39 lgc = wgc = WMColorGC(scr->black);
40 dgc = bgc = WMColorGC(scr->white);
41 break;
43 case WRRidge:
44 lgc = bgc = WMColorGC(scr->darkGray);
45 dgc = wgc = WMColorGC(scr->white);
46 break;
48 case WRGroove:
49 wgc = dgc = WMColorGC(scr->darkGray);
50 lgc = bgc = WMColorGC(scr->white);
51 break;
53 default:
54 return;
56 /* top left */
57 XDrawLine(dpy, d, wgc, x, y, x+width-1, y);
58 if (width > 2 && relief != WRRaised && relief!=WRPushed) {
59 XDrawLine(dpy, d, lgc, x+1, y+1, x+width-3, y+1);
62 XDrawLine(dpy, d, wgc, x, y, x, y+height-1);
63 if (height > 2 && relief != WRRaised && relief!=WRPushed) {
64 XDrawLine(dpy, d, lgc, x+1, y+1, x+1, y+height-3);
67 /* bottom right */
68 XDrawLine(dpy, d, bgc, x, y+height-1, x+width-1, y+height-1);
69 if (width > 2 && relief!=WRPushed) {
70 XDrawLine(dpy, d, dgc, x+1, y+height-2, x+width-2, y+height-2);
73 XDrawLine(dpy, d, bgc, x+width-1, y, x+width-1, y+height-1);
74 if (height > 2 && relief!=WRPushed) {
75 XDrawLine(dpy, d, dgc, x+width-2, y+1, x+width-2, y+height-2);
80 static int
81 fitText(char *text, WMFont *font, int width, int wrap)
83 int i, j;
84 int w;
86 if (text[0]==0)
87 return 0;
88 i = 0;
89 if (wrap) {
90 do {
91 i++;
92 w = WMWidthOfString(font, text, i);
93 } while (w < width && text[i]!='\n' && text[i]!=0);
95 /* keep words complete */
96 if (!isspace(text[i])) {
97 j = i;
98 while (j>1 && !isspace(text[j]) && text[j]!=0)
99 j--;
100 if (j>1)
101 i = j;
103 } else {
104 while (text[i]!='\n' && text[i]!=0)
105 i++;
108 return i;
113 W_GetTextHeight(WMFont *font, char *text, int width, int wrap)
115 char *ptr = text;
116 int count;
117 int length = strlen(text);
118 int h;
119 int fheight = WMFontHeight(font);
121 h = 0;
122 while (length > 0) {
123 count = fitText(ptr, font, width, wrap);
125 h += fheight;
127 if (isspace(ptr[count]))
128 count++;
130 ptr += count;
131 length -= count;
133 return h;
137 void
138 W_PaintText(W_View *view, Drawable d, WMFont *font, int x, int y,
139 int width, WMAlignment alignment, GC gc,
140 int wrap, char *text, int length)
142 char *ptr = text;
143 int line_width;
144 int line_x;
145 int count;
146 int fheight = WMFontHeight(font);
148 while (length > 0) {
149 count = fitText(ptr, font, width, wrap);
151 line_width = WMWidthOfString(font, ptr, count);
152 if (alignment==WALeft)
153 line_x = x;
154 else if (alignment==WARight)
155 line_x = x + width - line_width;
156 else
157 line_x = x + (width - line_width) / 2;
159 WMDrawString(view->screen, d, gc, font, line_x, y, ptr, count);
161 y += fheight;
163 if (isspace(ptr[count]))
164 count++;
166 ptr += count;
167 length -= count;
172 void
173 W_PaintTextAndImage(W_View *view, int wrap, GC textGC, W_Font *font,
174 WMReliefType relief, char *text,
175 WMAlignment alignment, W_Pixmap *image,
176 WMImagePosition position, GC backGC, int ofs)
178 W_Screen *screen = view->screen;
179 int ix, iy;
180 int x, y, w, h;
181 Drawable d = view->window;
184 #ifdef DOUBLE_BUFFER
185 d = XCreatePixmap(screen->display, view->window,
186 view->size.width, view->size.height, screen->depth);
187 #endif
189 /* background */
190 #ifndef DOUBLE_BUFFER
191 if (backGC) {
192 XFillRectangle(screen->display, d, backGC,
193 0, 0, view->size.width, view->size.height);
194 } else {
195 XClearWindow(screen->display, d);
197 #else
198 if (backGC)
199 XFillRectangle(screen->display, d, backGC, 0, 0,
200 view->size.width, view->size.height);
201 else {
202 XSetForeground(screen->display, screen->copyGC,
203 view->attribs.background_pixel);
204 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
205 view->size.width, view->size.height);
207 #endif
210 if (relief == WRFlat) {
211 x = 0;
212 y = 0;
213 w = view->size.width;
214 h = view->size.height;
215 } else {
216 x = 2;
217 y = 2;
218 w = view->size.width - 4;
219 h = view->size.height - 4;
222 /* calc. image alignment */
223 if (position!=WIPNoImage && image!=NULL) {
224 switch (position) {
225 case WIPOverlaps:
226 case WIPImageOnly:
227 ix = (view->size.width - image->width) / 2;
228 iy = (view->size.height - image->height) / 2;
230 x = 2;
231 y = 0;
233 break;
235 case WIPLeft:
236 ix = x;
237 iy = y + (h - image->height) / 2;
238 x = x + image->width + 5;
239 y = 0;
240 w -= image->width + 5;
241 break;
243 case WIPRight:
244 ix = view->size.width - image->width - x;
245 iy = y + (h - image->height) / 2;
246 w -= image->width + 5;
247 break;
249 case WIPBelow:
250 ix = (view->size.width - image->width) / 2;
251 iy = h - image->height;
252 y = 0;
253 h -= image->height;
254 break;
256 default:
257 case WIPAbove:
258 ix = (view->size.width - image->width) / 2;
259 iy = y;
260 y = image->height;
261 h -= image->height;
262 break;
265 ix += ofs;
266 iy += ofs;
268 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
269 XSetClipMask(screen->display, screen->clipGC, image->mask);
271 if (image->depth==1)
272 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
273 0, 0, image->width, image->height, ix, iy, 1);
274 else
275 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
276 0, 0, image->width, image->height, ix, iy);
279 /* draw text */
280 if (position != WIPImageOnly && text!=NULL) {
281 int textHeight;
283 textHeight = W_GetTextHeight(font, text, w-8, wrap);
284 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
285 alignment, textGC, wrap, text, strlen(text));
289 /* draw relief */
290 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
292 #ifdef DOUBLE_BUFFER
293 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
294 view->size.width, view->size.height, 0, 0);
295 XFreePixmap(screen->display, d);
296 #endif