reverted the last test commit (duh)
[wmaker-crm.git] / WINGs / wmisc.c
blobf8bfdb4745df13d4fa4d6730b68a03e350c48beb
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 W_DrawReliefWithGC(scr, d, x, y, width, height, relief,
13 WMColorGC(scr->black), WMColorGC(scr->darkGray),
14 WMColorGC(scr->gray), WMColorGC(scr->white));
18 void
19 W_DrawReliefWithGC(W_Screen *scr, Drawable d, int x, int y, unsigned int width,
20 unsigned int height, WMReliefType relief,
21 GC black, GC dark, GC light, GC white)
23 Display *dpy = scr->display;
24 GC bgc;
25 GC wgc;
26 GC lgc;
27 GC dgc;
29 switch (relief) {
30 case WRSimple:
31 XDrawRectangle(dpy, d, black, x, y, width-1, height-1);
32 return;
34 case WRRaised:
35 bgc = black;
36 dgc = dark;
37 wgc = white;
38 lgc = light;
39 break;
41 case WRSunken:
42 wgc = dark;
43 lgc = black;
44 bgc = white;
45 dgc = light;
46 break;
48 case WRPushed:
49 lgc = wgc = black;
50 dgc = bgc = white;
51 break;
53 case WRRidge:
54 lgc = bgc = dark;
55 dgc = wgc = white;
56 break;
58 case WRGroove:
59 wgc = dgc = dark;
60 lgc = bgc = white;
61 break;
63 default:
64 return;
66 /* top left */
67 XDrawLine(dpy, d, wgc, x, y, x+width-1, y);
68 if (width > 2 && relief != WRRaised && relief!=WRPushed) {
69 XDrawLine(dpy, d, lgc, x+1, y+1, x+width-3, y+1);
72 XDrawLine(dpy, d, wgc, x, y, x, y+height-1);
73 if (height > 2 && relief != WRRaised && relief!=WRPushed) {
74 XDrawLine(dpy, d, lgc, x+1, y+1, x+1, y+height-3);
77 /* bottom right */
78 XDrawLine(dpy, d, bgc, x, y+height-1, x+width-1, y+height-1);
79 if (width > 2 && relief!=WRPushed) {
80 XDrawLine(dpy, d, dgc, x+1, y+height-2, x+width-2, y+height-2);
83 XDrawLine(dpy, d, bgc, x+width-1, y, x+width-1, y+height-1);
84 if (height > 2 && relief!=WRPushed) {
85 XDrawLine(dpy, d, dgc, x+width-2, y+1, x+width-2, y+height-2);
92 static int
93 fitText(char *text, WMFont *font, int width, int wrap)
95 int i, j;
96 int w;
98 if (text[0]==0)
99 return 0;
101 i = 0;
102 if (wrap) {
103 if (text[0]=='\n')
104 return 0;
106 do {
107 i++;
108 w = WMWidthOfString(font, text, i);
109 } while (w < width && text[i]!='\n' && text[i]!=0);
111 if (text[i]=='\n')
112 return i;
114 /* keep words complete */
115 if (!isspace(text[i])) {
116 j = i;
117 while (j>1 && !isspace(text[j]) && text[j]!=0)
118 j--;
119 if (j>1)
120 i = j;
122 } else {
123 while (text[i]!='\n' && text[i]!=0)
124 i++;
126 return i;
131 W_GetTextHeight(WMFont *font, char *text, int width, int wrap)
133 char *ptr = text;
134 int count;
135 int length = strlen(text);
136 int h;
137 int fheight = WMFontHeight(font);
139 h = 0;
140 while (length > 0) {
141 count = fitText(ptr, font, width, wrap);
143 h += fheight;
145 if (isspace(ptr[count]))
146 count++;
148 ptr += count;
149 length -= count;
151 return h;
155 void
156 W_PaintText(W_View *view, Drawable d, WMFont *font, int x, int y,
157 int width, WMAlignment alignment, WMColor *color,
158 int wrap, char *text, int length)
160 char *ptr = text;
161 int line_width;
162 int line_x;
163 int count;
164 int fheight = WMFontHeight(font);
166 while (length > 0) {
167 count = fitText(ptr, font, width, wrap);
169 line_width = WMWidthOfString(font, ptr, count);
170 if (alignment==WALeft)
171 line_x = x;
172 else if (alignment==WARight)
173 line_x = x + width - line_width;
174 else
175 line_x = x + (width - line_width) / 2;
177 WMDrawString(view->screen, d, color, font, line_x, y, ptr, count);
179 if (wrap && ptr[count]!='\n')
180 y += fheight;
182 while (ptr[count] && ptr[count]=='\n') {
183 y += fheight;
184 count++;
187 ptr += count;
188 length -= count;
193 void
194 W_PaintTextAndImage(W_View *view, int wrap, WMColor *textColor, W_Font *font,
195 WMReliefType relief, char *text,
196 WMAlignment alignment, W_Pixmap *image,
197 WMImagePosition position, WMColor *backColor, int ofs)
199 W_Screen *screen = view->screen;
200 int ix, iy;
201 int x, y, w, h;
202 Drawable d = view->window;
205 #ifdef DOUBLE_BUFFER
206 d = XCreatePixmap(screen->display, view->window,
207 view->size.width, view->size.height, screen->depth);
208 #endif
210 /* background */
211 if (backColor) {
212 XFillRectangle(screen->display, d, WMColorGC(backColor),
213 0, 0, view->size.width, view->size.height);
214 } else {
215 #ifndef DOUBLE_BUFFER
216 XClearWindow(screen->display, d);
217 #else
218 XSetForeground(screen->display, screen->copyGC,
219 view->attribs.background_pixel);
220 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
221 view->size.width, view->size.height);
222 #endif
226 if (relief == WRFlat) {
227 x = 0;
228 y = 0;
229 w = view->size.width;
230 h = view->size.height;
231 } else {
232 x = 1;
233 y = 1;
234 w = view->size.width - 3;
235 h = view->size.height - 3;
238 /* calc. image alignment */
239 if (position!=WIPNoImage && image!=NULL) {
240 switch (position) {
241 case WIPOverlaps:
242 case WIPImageOnly:
243 ix = (view->size.width - image->width) / 2;
244 iy = (view->size.height - image->height) / 2;
246 x = 2;
247 y = 0;
249 break;
251 case WIPLeft:
252 ix = x;
253 iy = y + (h - image->height) / 2;
254 x = x + image->width + 5;
255 y = 0;
256 w -= image->width + 5;
257 break;
259 case WIPRight:
260 ix = view->size.width - image->width - x;
261 iy = y + (h - image->height) / 2;
262 w -= image->width + 5;
263 break;
265 case WIPBelow:
266 ix = (view->size.width - image->width) / 2;
267 iy = h - image->height;
268 y = 0;
269 h -= image->height;
270 break;
272 default:
273 case WIPAbove:
274 ix = (view->size.width - image->width) / 2;
275 iy = y;
276 y = image->height;
277 h -= image->height;
278 break;
281 ix += ofs;
282 iy += ofs;
284 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
285 XSetClipMask(screen->display, screen->clipGC, image->mask);
287 if (image->depth==1)
288 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
289 0, 0, image->width, image->height, ix, iy, 1);
290 else
291 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
292 0, 0, image->width, image->height, ix, iy);
295 /* draw text */
296 if (position != WIPImageOnly && text!=NULL) {
297 int textHeight;
299 textHeight = W_GetTextHeight(font, text, w-8, wrap);
300 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
301 alignment, textColor, wrap, text, strlen(text));
305 /* draw relief */
306 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
308 #ifdef DOUBLE_BUFFER
309 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
310 view->size.width, view->size.height, 0, 0);
311 XFreePixmap(screen->display, d);
312 #endif
317 WMPoint
318 wmkpoint(int x, int y)
320 WMPoint point;
322 point.x = x;
323 point.y = y;
325 return point;
329 WMSize
330 wmksize(unsigned int width, unsigned int height)
332 WMSize size;
334 size.width = width;
335 size.height = height;
337 return size;
341 WMRect
342 wmkrect(int x, int y, unsigned int width, unsigned int height)
344 WMRect rect;
346 rect.pos.x = x;
347 rect.pos.y = y;
348 rect.size.width = width;
349 rect.size.height = height;
351 return rect;