- s/sprintf/snprintf
[wmaker-crm.git] / WINGs / wmisc.c
blob1b1e20e8dbc403683e055b1616d39286dc36b518
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 1;
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, GC gc,
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, gc, font, line_x, y, ptr, count);
179 y += fheight;
181 if (isspace(ptr[count]))
182 count++;
184 ptr += count;
185 length -= count;
190 void
191 W_PaintTextAndImage(W_View *view, int wrap, GC textGC, W_Font *font,
192 WMReliefType relief, char *text,
193 WMAlignment alignment, W_Pixmap *image,
194 WMImagePosition position, GC backGC, int ofs)
196 W_Screen *screen = view->screen;
197 int ix, iy;
198 int x, y, w, h;
199 Drawable d = view->window;
202 #ifdef DOUBLE_BUFFER
203 d = XCreatePixmap(screen->display, view->window,
204 view->size.width, view->size.height, screen->depth);
205 #endif
207 /* background */
208 #ifndef DOUBLE_BUFFER
209 if (backGC) {
210 XFillRectangle(screen->display, d, backGC,
211 0, 0, view->size.width, view->size.height);
212 } else {
213 XClearWindow(screen->display, d);
215 #else
216 if (backGC)
217 XFillRectangle(screen->display, d, backGC, 0, 0,
218 view->size.width, view->size.height);
219 else {
220 XSetForeground(screen->display, screen->copyGC,
221 view->attribs.background_pixel);
222 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
223 view->size.width, view->size.height);
225 #endif
228 if (relief == WRFlat) {
229 x = 0;
230 y = 0;
231 w = view->size.width;
232 h = view->size.height;
233 } else {
234 x = 1;
235 y = 1;
236 w = view->size.width - 3;
237 h = view->size.height - 3;
240 /* calc. image alignment */
241 if (position!=WIPNoImage && image!=NULL) {
242 switch (position) {
243 case WIPOverlaps:
244 case WIPImageOnly:
245 ix = (view->size.width - image->width) / 2;
246 iy = (view->size.height - image->height) / 2;
248 x = 2;
249 y = 0;
251 break;
253 case WIPLeft:
254 ix = x;
255 iy = y + (h - image->height) / 2;
256 x = x + image->width + 5;
257 y = 0;
258 w -= image->width + 5;
259 break;
261 case WIPRight:
262 ix = view->size.width - image->width - x;
263 iy = y + (h - image->height) / 2;
264 w -= image->width + 5;
265 break;
267 case WIPBelow:
268 ix = (view->size.width - image->width) / 2;
269 iy = h - image->height;
270 y = 0;
271 h -= image->height;
272 break;
274 default:
275 case WIPAbove:
276 ix = (view->size.width - image->width) / 2;
277 iy = y;
278 y = image->height;
279 h -= image->height;
280 break;
283 ix += ofs;
284 iy += ofs;
286 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
287 XSetClipMask(screen->display, screen->clipGC, image->mask);
289 if (image->depth==1)
290 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
291 0, 0, image->width, image->height, ix, iy, 1);
292 else
293 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
294 0, 0, image->width, image->height, ix, iy);
297 /* draw text */
298 if (position != WIPImageOnly && text!=NULL) {
299 int textHeight;
301 textHeight = W_GetTextHeight(font, text, w-8, wrap);
302 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
303 alignment, textGC, wrap, text, strlen(text));
307 /* draw relief */
308 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
310 #ifdef DOUBLE_BUFFER
311 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
312 view->size.width, view->size.height, 0, 0);
313 XFreePixmap(screen->display, d);
314 #endif
319 WMPoint
320 wmkpoint(int x, int y)
322 WMPoint point;
324 point.x = x;
325 point.y = y;
327 return point;
331 WMSize
332 wmksize(unsigned int width, unsigned int height)
334 WMSize size;
336 size.width = width;
337 size.height = height;
339 return size;