Removed some redundant code.
[wmaker-crm.git] / WINGs / wmisc.c
blob43476622b85cf92d69a22c7a978428fdff6b1e8b
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;
100 i = 0;
101 if (wrap) {
102 do {
103 i++;
104 w = WMWidthOfString(font, text, i);
105 } while (w < width && text[i]!='\n' && text[i]!=0);
107 /* keep words complete */
108 if (!isspace(text[i])) {
109 j = i;
110 while (j>1 && !isspace(text[j]) && text[j]!=0)
111 j--;
112 if (j>1)
113 i = j;
115 } else {
116 while (text[i]!='\n' && text[i]!=0)
117 i++;
120 return i;
125 W_GetTextHeight(WMFont *font, char *text, int width, int wrap)
127 char *ptr = text;
128 int count;
129 int length = strlen(text);
130 int h;
131 int fheight = WMFontHeight(font);
133 h = 0;
134 while (length > 0) {
135 count = fitText(ptr, font, width, wrap);
137 h += fheight;
139 if (isspace(ptr[count]))
140 count++;
142 ptr += count;
143 length -= count;
145 return h;
149 void
150 W_PaintText(W_View *view, Drawable d, WMFont *font, int x, int y,
151 int width, WMAlignment alignment, GC gc,
152 int wrap, char *text, int length)
154 char *ptr = text;
155 int line_width;
156 int line_x;
157 int count;
158 int fheight = WMFontHeight(font);
160 while (length > 0) {
161 count = fitText(ptr, font, width, wrap);
163 line_width = WMWidthOfString(font, ptr, count);
164 if (alignment==WALeft)
165 line_x = x;
166 else if (alignment==WARight)
167 line_x = x + width - line_width;
168 else
169 line_x = x + (width - line_width) / 2;
171 WMDrawString(view->screen, d, gc, font, line_x, y, ptr, count);
173 y += fheight;
175 if (isspace(ptr[count]))
176 count++;
178 ptr += count;
179 length -= count;
184 void
185 W_PaintTextAndImage(W_View *view, int wrap, GC textGC, W_Font *font,
186 WMReliefType relief, char *text,
187 WMAlignment alignment, W_Pixmap *image,
188 WMImagePosition position, GC backGC, int ofs)
190 W_Screen *screen = view->screen;
191 int ix, iy;
192 int x, y, w, h;
193 Drawable d = view->window;
196 #ifdef DOUBLE_BUFFER
197 d = XCreatePixmap(screen->display, view->window,
198 view->size.width, view->size.height, screen->depth);
199 #endif
201 /* background */
202 #ifndef DOUBLE_BUFFER
203 if (backGC) {
204 XFillRectangle(screen->display, d, backGC,
205 0, 0, view->size.width, view->size.height);
206 } else {
207 XClearWindow(screen->display, d);
209 #else
210 if (backGC)
211 XFillRectangle(screen->display, d, backGC, 0, 0,
212 view->size.width, view->size.height);
213 else {
214 XSetForeground(screen->display, screen->copyGC,
215 view->attribs.background_pixel);
216 XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
217 view->size.width, view->size.height);
219 #endif
222 if (relief == WRFlat) {
223 x = 0;
224 y = 0;
225 w = view->size.width;
226 h = view->size.height;
227 } else {
228 x = 2;
229 y = 2;
230 w = view->size.width - 4;
231 h = view->size.height - 4;
234 /* calc. image alignment */
235 if (position!=WIPNoImage && image!=NULL) {
236 switch (position) {
237 case WIPOverlaps:
238 case WIPImageOnly:
239 ix = (view->size.width - image->width) / 2;
240 iy = (view->size.height - image->height) / 2;
242 x = 2;
243 y = 0;
245 break;
247 case WIPLeft:
248 ix = x;
249 iy = y + (h - image->height) / 2;
250 x = x + image->width + 5;
251 y = 0;
252 w -= image->width + 5;
253 break;
255 case WIPRight:
256 ix = view->size.width - image->width - x;
257 iy = y + (h - image->height) / 2;
258 w -= image->width + 5;
259 break;
261 case WIPBelow:
262 ix = (view->size.width - image->width) / 2;
263 iy = h - image->height;
264 y = 0;
265 h -= image->height;
266 break;
268 default:
269 case WIPAbove:
270 ix = (view->size.width - image->width) / 2;
271 iy = y;
272 y = image->height;
273 h -= image->height;
274 break;
277 ix += ofs;
278 iy += ofs;
280 XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
281 XSetClipMask(screen->display, screen->clipGC, image->mask);
283 if (image->depth==1)
284 XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
285 0, 0, image->width, image->height, ix, iy, 1);
286 else
287 XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
288 0, 0, image->width, image->height, ix, iy);
291 /* draw text */
292 if (position != WIPImageOnly && text!=NULL) {
293 int textHeight;
295 textHeight = W_GetTextHeight(font, text, w-8, wrap);
296 W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
297 alignment, textGC, wrap, text, strlen(text));
301 /* draw relief */
302 W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
304 #ifdef DOUBLE_BUFFER
305 XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
306 view->size.width, view->size.height, 0, 0);
307 XFreePixmap(screen->display, d);
308 #endif
313 WMRange
314 wmkrange(int start, int count)
316 WMRange range;
318 range.position = start;
319 range.count = count;
321 return range;
325 WMPoint
326 wmkpoint(int x, int y)
328 WMPoint point;
330 point.x = x;
331 point.y = y;
333 return point;
337 WMSize
338 wmksize(unsigned int width, unsigned int height)
340 WMSize size;
342 size.width = width;
343 size.height = height;
345 return size;