added greek flag pixmap
[wmaker-crm.git] / WINGs / wmisc.c
bloba352bc483862ab8a2df76ec42b9841bdc6f2d5bf
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 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, GC textGC, W_Font *font,
195 WMReliefType relief, char *text,
196 WMAlignment alignment, W_Pixmap *image,
197 WMImagePosition position, GC backGC, 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 #ifndef DOUBLE_BUFFER
212 if (backGC) {
213 XFillRectangle(screen->display, d, backGC,
214 0, 0, view->size.width, view->size.height);
215 } else {
216 XClearWindow(screen->display, d);
218 #else
219 if (backGC)
220 XFillRectangle(screen->display, d, backGC, 0, 0,
221 view->size.width, view->size.height);
222 else {
223 XSetForeground(screen->display, screen->copyGC,
224 view->attribs.background_pixel);
225 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
226 view->size.width, view->size.height);
228 #endif
231 if (relief == WRFlat) {
232 x = 0;
233 y = 0;
234 w = view->size.width;
235 h = view->size.height;
236 } else {
237 x = 1;
238 y = 1;
239 w = view->size.width - 3;
240 h = view->size.height - 3;
243 /* calc. image alignment */
244 if (position!=WIPNoImage && image!=NULL) {
245 switch (position) {
246 case WIPOverlaps:
247 case WIPImageOnly:
248 ix = (view->size.width - image->width) / 2;
249 iy = (view->size.height - image->height) / 2;
251 x = 2;
252 y = 0;
254 break;
256 case WIPLeft:
257 ix = x;
258 iy = y + (h - image->height) / 2;
259 x = x + image->width + 5;
260 y = 0;
261 w -= image->width + 5;
262 break;
264 case WIPRight:
265 ix = view->size.width - image->width - x;
266 iy = y + (h - image->height) / 2;
267 w -= image->width + 5;
268 break;
270 case WIPBelow:
271 ix = (view->size.width - image->width) / 2;
272 iy = h - image->height;
273 y = 0;
274 h -= image->height;
275 break;
277 default:
278 case WIPAbove:
279 ix = (view->size.width - image->width) / 2;
280 iy = y;
281 y = image->height;
282 h -= image->height;
283 break;
286 ix += ofs;
287 iy += ofs;
289 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
290 XSetClipMask(screen->display, screen->clipGC, image->mask);
292 if (image->depth==1)
293 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
294 0, 0, image->width, image->height, ix, iy, 1);
295 else
296 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
297 0, 0, image->width, image->height, ix, iy);
300 /* draw text */
301 if (position != WIPImageOnly && text!=NULL) {
302 int textHeight;
304 textHeight = W_GetTextHeight(font, text, w-8, wrap);
305 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
306 alignment, textGC, wrap, text, strlen(text));
310 /* draw relief */
311 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
313 #ifdef DOUBLE_BUFFER
314 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
315 view->size.width, view->size.height, 0, 0);
316 XFreePixmap(screen->display, d);
317 #endif
322 WMPoint
323 wmkpoint(int x, int y)
325 WMPoint point;
327 point.x = x;
328 point.y = y;
330 return point;
334 WMSize
335 wmksize(unsigned int width, unsigned int height)
337 WMSize size;
339 size.width = width;
340 size.height = height;
342 return size;