another test for cvs notifications
[wmaker-crm.git] / WINGs / wmisc.c
blobe3b65ce31f01e9879361a5e6c460fd971a5e2afe
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 //i = strcspn(text, "\n\r");
124 while (text[i]!='\n' && text[i]!=0)
125 i++;
127 return i;
132 W_GetTextHeight(WMFont *font, char *text, int width, int wrap)
134 char *ptr = text;
135 int count;
136 int length = strlen(text);
137 int h;
138 int fheight = WMFontHeight(font);
140 h = 0;
141 while (length > 0) {
142 count = fitText(ptr, font, width, wrap);
144 h += fheight;
146 if (isspace(ptr[count]))
147 count++;
149 ptr += count;
150 length -= count;
152 return h;
156 void
157 W_PaintText(W_View *view, Drawable d, WMFont *font, int x, int y,
158 int width, WMAlignment alignment, WMColor *color,
159 int wrap, char *text, int length)
161 char *ptr = text;
162 int line_width;
163 int line_x;
164 int count;
165 int fheight = WMFontHeight(font);
167 while (length > 0) {
168 count = fitText(ptr, font, width, wrap);
170 line_width = WMWidthOfString(font, ptr, count);
171 if (alignment==WALeft)
172 line_x = x;
173 else if (alignment==WARight)
174 line_x = x + width - line_width;
175 else
176 line_x = x + (width - line_width) / 2;
178 WMDrawString(view->screen, d, color, font, line_x, y, ptr, count);
180 if (wrap && ptr[count]!='\n')
181 y += fheight;
183 while (ptr[count] && ptr[count]=='\n') {
184 y += fheight;
185 count++;
188 ptr += count;
189 length -= count;
194 void
195 W_PaintTextAndImage(W_View *view, int wrap, WMColor *textColor, W_Font *font,
196 WMReliefType relief, char *text,
197 WMAlignment alignment, W_Pixmap *image,
198 WMImagePosition position, WMColor *backColor, int ofs)
200 W_Screen *screen = view->screen;
201 int ix, iy;
202 int x, y, w, h;
203 Drawable d = view->window;
206 #ifdef DOUBLE_BUFFER
207 d = XCreatePixmap(screen->display, view->window,
208 view->size.width, view->size.height, screen->depth);
209 #endif
211 /* background */
212 if (backColor) {
213 XFillRectangle(screen->display, d, WMColorGC(backColor),
214 0, 0, view->size.width, view->size.height);
215 } else {
216 #ifndef DOUBLE_BUFFER
217 XClearWindow(screen->display, d);
218 #else
219 XSetForeground(screen->display, screen->copyGC,
220 view->attribs.background_pixel);
221 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
222 view->size.width, view->size.height);
223 #endif
227 if (relief == WRFlat) {
228 x = 0;
229 y = 0;
230 w = view->size.width;
231 h = view->size.height;
232 } else {
233 x = 1;
234 y = 1;
235 w = view->size.width - 3;
236 h = view->size.height - 3;
239 /* calc. image alignment */
240 if (position!=WIPNoImage && image!=NULL) {
241 switch (position) {
242 case WIPOverlaps:
243 case WIPImageOnly:
244 ix = (view->size.width - image->width) / 2;
245 iy = (view->size.height - image->height) / 2;
247 x = 2;
248 y = 0;
250 break;
252 case WIPLeft:
253 ix = x;
254 iy = y + (h - image->height) / 2;
255 x = x + image->width + 5;
256 y = 0;
257 w -= image->width + 5;
258 break;
260 case WIPRight:
261 ix = view->size.width - image->width - x;
262 iy = y + (h - image->height) / 2;
263 w -= image->width + 5;
264 break;
266 case WIPBelow:
267 ix = (view->size.width - image->width) / 2;
268 iy = h - image->height;
269 y = 0;
270 h -= image->height;
271 break;
273 default:
274 case WIPAbove:
275 ix = (view->size.width - image->width) / 2;
276 iy = y;
277 y = image->height;
278 h -= image->height;
279 break;
282 ix += ofs;
283 iy += ofs;
285 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
286 XSetClipMask(screen->display, screen->clipGC, image->mask);
288 if (image->depth==1)
289 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
290 0, 0, image->width, image->height, ix, iy, 1);
291 else
292 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
293 0, 0, image->width, image->height, ix, iy);
296 /* draw text */
297 if (position != WIPImageOnly && text!=NULL) {
298 int textHeight;
300 textHeight = W_GetTextHeight(font, text, w-8, wrap);
301 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
302 alignment, textColor, wrap, text, strlen(text));
306 /* draw relief */
307 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
309 #ifdef DOUBLE_BUFFER
310 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
311 view->size.width, view->size.height, 0, 0);
312 XFreePixmap(screen->display, d);
313 #endif
318 WMPoint
319 wmkpoint(int x, int y)
321 WMPoint point;
323 point.x = x;
324 point.y = y;
326 return point;
330 WMSize
331 wmksize(unsigned int width, unsigned int height)
333 WMSize size;
335 size.width = width;
336 size.height = height;
338 return size;
342 WMRect
343 wmkrect(int x, int y, unsigned int width, unsigned int height)
345 WMRect rect;
347 rect.pos.x = x;
348 rect.pos.y = y;
349 rect.size.width = width;
350 rect.size.height = height;
352 return rect;