Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wruler.c
1 /*
2 * WINGs WMRuler: nifty ruler widget for WINGs :-)
3 *
4 * Copyright (c) 1999-2000 Nwanua Elumeze
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "WINGsP.h"
22 #include "wconfig.h"
23
24 #define MIN_DOC_WIDTH 10
25
26 typedef struct W_Ruler {
27 W_Class widgetClass;
28 W_View *view;
29 W_View *pview; /* the parent's view (for drawing the line) */
30
31 WMAction *moveAction; /* what to when while moving */
32 WMAction *releaseAction; /* what to do when released */
33 void *clientData;
34
35 WMColor *fg;
36 GC fgGC, bgGC;
37 WMFont *font;
38 WMRulerMargins margins;
39 int offset;
40 int motion; /* the position of the _moving_ marker(s) */
41 int end; /* the last tick on the baseline (restrict markers to it) */
42
43 Pixmap drawBuffer;
44
45 struct {
46 unsigned int whichMarker:3;
47 /* 0, 1, 2, 3, 4, 5, 6 */
48 /* none, left, right, first, body, tabstop, first & body */
49
50 unsigned int buttonPressed:1;
51 unsigned int redraw:1;
52 unsigned int RESERVED:27;
53 } flags;
54 } Ruler;
55
56 /* Marker for left margin
57
58 |\
59 | \
60 |__\
61 |
62 |
63
64 */
65 static void drawLeftMarker(Ruler * rPtr)
66 {
67 XPoint points[4];
68 int xpos = (rPtr->flags.whichMarker == 1 ? rPtr->motion : rPtr->margins.left);
69
70 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC, xpos, 8, xpos, 22);
71 points[0].x = xpos;
72 points[0].y = 1;
73 points[1].x = points[0].x + 6;
74 points[1].y = 8;
75 points[2].x = points[0].x + 6;
76 points[2].y = 9;
77 points[3].x = points[0].x;
78 points[3].y = 9;
79 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC,
80 points, 4, Convex, CoordModeOrigin);
81 }
82
83 /* Marker for right margin
84
85 /|
86 / |
87 /__|
88 |
89 |
90
91 */
92 static void drawRightMarker(Ruler * rPtr)
93 {
94 XPoint points[4];
95 int xpos = (rPtr->flags.whichMarker == 2 ? rPtr->motion : rPtr->margins.right);
96
97 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC, xpos, 8, xpos, 22);
98 points[0].x = xpos + 1;
99 points[0].y = 0;
100 points[1].x = points[0].x - 6;
101 points[1].y = 7;
102 points[2].x = points[0].x - 6;
103 points[2].y = 9;
104 points[3].x = points[0].x;
105 points[3].y = 9;
106 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC,
107 points, 4, Convex, CoordModeOrigin);
108 }
109
110 /* Marker for first line only
111 _____
112 |___|
113 |
114
115 */
116 static void drawFirstMarker(Ruler * rPtr)
117 {
118 int xpos = ((rPtr->flags.whichMarker == 3 || rPtr->flags.whichMarker == 6) ?
119 rPtr->motion : rPtr->margins.first);
120
121 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC, xpos - 5, 10, 11, 5);
122 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC, xpos, 12, xpos, 22);
123 }
124
125 /* Marker for rest of body
126 _____
127 \ /
128 \./
129 */
130 static void drawBodyMarker(Ruler * rPtr)
131 {
132 XPoint points[4];
133 int xpos = ((rPtr->flags.whichMarker == 4 || rPtr->flags.whichMarker == 6) ?
134 rPtr->motion : rPtr->margins.body);
135
136 points[0].x = xpos - 5;
137 points[0].y = 16;
138 points[1].x = points[0].x + 11;
139 points[1].y = 16;
140 points[2].x = points[0].x + 5;
141 points[2].y = 22;
142 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC,
143 points, 3, Convex, CoordModeOrigin);
144 }
145
146 static void createDrawBuffer(Ruler * rPtr)
147 {
148 if (!rPtr->view->flags.realized)
149 return;
150
151 if (rPtr->drawBuffer)
152 XFreePixmap(rPtr->view->screen->display, rPtr->drawBuffer);
153
154 rPtr->drawBuffer = XCreatePixmap(rPtr->view->screen->display,
155 rPtr->view->window, rPtr->view->size.width, 40,
156 rPtr->view->screen->depth);
157 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
158 rPtr->bgGC, 0, 0, rPtr->view->size.width, 40);
159 }
160
161 static void drawRulerOnPixmap(Ruler * rPtr)
162 {
163 int i, j, w, m;
164 char c[3];
165 int marks[9] = { 11, 3, 5, 3, 7, 3, 5, 3 };
166
167 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
168 return;
169
170 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
171 rPtr->bgGC, 0, 0, rPtr->view->size.width, 40);
172
173 WMDrawString(rPtr->view->screen, rPtr->drawBuffer, rPtr->fg,
174 rPtr->font, rPtr->margins.left + 2, 26, _("0 inches"), 10);
175
176 /* marker ticks */
177 i = j = m = 0;
178 w = rPtr->view->size.width - rPtr->margins.left;
179 while (m < w) {
180 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer,
181 rPtr->fgGC, rPtr->margins.left + m, 23, rPtr->margins.left + m, marks[i % 8] + 23);
182 if (i != 0 && i % 8 == 0) {
183 if (j < 10)
184 snprintf(c, 3, "%d", ++j);
185 else
186 snprintf(c, 3, "%2d", ++j);
187 WMDrawString(rPtr->view->screen, rPtr->drawBuffer, rPtr->fg,
188 rPtr->font, rPtr->margins.left + 2 + m, 26, c, 2);
189 }
190 m = (++i) * 10;
191 }
192
193 rPtr->end = rPtr->margins.left + m - 10;
194 if (rPtr->margins.right > rPtr->end)
195 rPtr->margins.right = rPtr->end;
196 /* base line */
197 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fgGC,
198 rPtr->margins.left, 22, rPtr->margins.left + m - 10, 22);
199
200 drawLeftMarker(rPtr);
201 drawRightMarker(rPtr);
202 drawFirstMarker(rPtr);
203 drawBodyMarker(rPtr);
204
205 rPtr->flags.redraw = False;
206 }
207
208 static void paintRuler(Ruler * rPtr)
209 {
210 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
211 return;
212
213 if (rPtr->flags.redraw)
214 drawRulerOnPixmap(rPtr);
215 XCopyArea(rPtr->view->screen->display, rPtr->drawBuffer,
216 rPtr->view->window, rPtr->bgGC, 0, 0, rPtr->view->size.width, 40, 0, 0);
217 }
218
219 static Bool verifyMarkerMove(Ruler * rPtr, int x)
220 {
221 if (rPtr->flags.whichMarker < 1 || rPtr->flags.whichMarker > 6)
222 return False;
223
224 switch (rPtr->flags.whichMarker) {
225 case 1:
226 if (x > rPtr->margins.right - 10 || x < rPtr->offset ||
227 rPtr->margins.body + x > rPtr->margins.right - MIN_DOC_WIDTH ||
228 rPtr->margins.first + x > rPtr->margins.right - MIN_DOC_WIDTH)
229 return False;
230 break;
231
232 case 2:
233 if (x < rPtr->margins.first + MIN_DOC_WIDTH || x < rPtr->margins.body + MIN_DOC_WIDTH || x < rPtr->margins.left + MIN_DOC_WIDTH || x > rPtr->end) /*rPtr->view->size.width) */
234 return False;
235 break;
236
237 case 3:
238 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
239 return False;
240 break;
241
242 case 4:
243 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
244 return False;
245 break;
246
247 case 6:
248 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
249 return False;
250 break;
251
252 default:
253 return False;
254 }
255
256 rPtr->motion = x;
257 return True;
258 }
259
260 static int whichMarker(Ruler * rPtr, int x, int y)
261 {
262 if (x < rPtr->offset || y > 22)
263 return 0;
264
265 if (rPtr->margins.left - x >= -6 && y <= 9 && (rPtr->margins.left - x <= 0) && y >= 4) {
266 rPtr->motion = rPtr->margins.left;
267 return 1;
268 }
269 if (rPtr->margins.right - x >= -1 && y <= 11 && rPtr->margins.right - x <= 5 && y >= 4) {
270 rPtr->motion = rPtr->margins.right;
271 return 2;
272 }
273 #if 0
274 /* both first and body? */
275 if (rPtr->margins.first - x <= 4 && rPtr->margins.first - x >= -5
276 && rPtr->margins.body - x <= 4 && rPtr->margins.body - x >= -5 && y >= 15 && y <= 17) {
277 rPtr->motion = rPtr->margins.first;
278 return 6;
279 }
280 #endif
281
282 if (rPtr->margins.first - x <= 4 && y <= 15 && rPtr->margins.first - x >= -5 && y >= 10) {
283 rPtr->motion = rPtr->margins.first;
284 return 3;
285 }
286 if (rPtr->margins.body - x <= 4 && y <= 21 && rPtr->margins.body - x >= -5 && y >= 17) {
287 rPtr->motion = rPtr->margins.body;
288 return 4;
289 }
290 /* do tabs (5) */
291
292 return 0;
293 }
294
295 static void rulerDidResize(W_ViewDelegate * self, WMView * view)
296 {
297 Ruler *rPtr = (Ruler *) view->self;
298
299 createDrawBuffer(rPtr);
300 rPtr->flags.redraw = True;
301 paintRuler(rPtr);
302
303 }
304
305 static void handleEvents(XEvent * event, void *data)
306 {
307 Ruler *rPtr = (Ruler *) data;
308
309 switch (event->type) {
310 case Expose:
311 rulerDidResize(rPtr->view->delegate, rPtr->view);
312 break;
313
314 case MotionNotify:
315 if (rPtr->flags.buttonPressed && (event->xmotion.state & Button1Mask)) {
316 if (verifyMarkerMove(rPtr, event->xmotion.x)) {
317 GC gc = WMColorGC(WMDarkGrayColor(rPtr->view->screen));
318
319 if (rPtr->moveAction)
320 (rPtr->moveAction) (rPtr, rPtr->clientData);
321 rPtr->flags.redraw = True;
322 paintRuler(rPtr);
323 XSetLineAttributes(rPtr->view->screen->display, gc, 1,
324 LineSolid, CapNotLast, JoinMiter);
325 XDrawLine(rPtr->pview->screen->display,
326 rPtr->pview->window,
327 gc, rPtr->motion + 1, 40,
328 rPtr->motion + 1, rPtr->pview->size.height - 5);
329 }
330 }
331 break;
332
333 case ButtonPress:
334 if (event->xbutton.button != Button1)
335 return;
336 rPtr->flags.buttonPressed = True;
337 rPtr->flags.whichMarker = whichMarker(rPtr, event->xmotion.x, event->xmotion.y);
338 break;
339
340 case ButtonRelease:
341 if (event->xbutton.button != Button1)
342 return;
343 rPtr->flags.buttonPressed = False;
344 switch (rPtr->flags.whichMarker) {
345 case 1:{
346 int change = rPtr->margins.left - rPtr->motion;
347
348 rPtr->margins.first -= change;
349 rPtr->margins.body -= change;
350 rPtr->margins.left = rPtr->motion;
351 rPtr->flags.redraw = True;
352 paintRuler(rPtr);
353 break;
354 }
355 case 2:
356 rPtr->margins.right = rPtr->motion;
357 break;
358 case 3:
359 rPtr->margins.first = rPtr->motion;
360 break;
361 case 4:
362 rPtr->margins.body = rPtr->motion;
363 break;
364 case 6:
365 rPtr->margins.first = rPtr->margins.body = rPtr->motion;
366 break;
367 }
368 if (rPtr->releaseAction)
369 (rPtr->releaseAction) (rPtr, rPtr->clientData);
370 break;
371 }
372 }
373
374 W_ViewDelegate _RulerViewDelegate = {
375 NULL,
376 NULL,
377 rulerDidResize,
378 NULL,
379 NULL
380 };
381
382 WMRuler *WMCreateRuler(WMWidget * parent)
383 {
384 Ruler *rPtr = wmalloc(sizeof(Ruler));
385 unsigned int w = WMWidgetWidth(parent);
386
387 memset(rPtr, 0, sizeof(Ruler));
388
389 rPtr->widgetClass = WC_Ruler;
390
391 rPtr->view = W_CreateView(W_VIEW(parent));
392
393 if (!rPtr->view) {
394 wfree(rPtr);
395 return NULL;
396 }
397
398 rPtr->view->self = rPtr;
399
400 rPtr->drawBuffer = (Pixmap) NULL;
401
402 W_ResizeView(rPtr->view, w, 40);
403
404 WMCreateEventHandler(rPtr->view, ExposureMask | StructureNotifyMask
405 | EnterWindowMask | LeaveWindowMask | FocusChangeMask
406 | ButtonReleaseMask | ButtonPressMask | KeyReleaseMask
407 | KeyPressMask | Button1MotionMask, handleEvents, rPtr);
408
409 rPtr->view->delegate = &_RulerViewDelegate;
410
411 rPtr->fg = WMBlackColor(rPtr->view->screen);
412 rPtr->fgGC = WMColorGC(rPtr->fg);
413 rPtr->bgGC = WMColorGC(WMGrayColor(rPtr->view->screen));
414 rPtr->font = WMSystemFontOfSize(rPtr->view->screen, 8);
415
416 rPtr->offset = 22;
417 rPtr->margins.left = 22;
418 rPtr->margins.body = 22;
419 rPtr->margins.first = 42;
420 rPtr->margins.right = (w < 502 ? w : 502);
421 rPtr->margins.tabs = NULL;
422
423 rPtr->flags.whichMarker = 0; /* none */
424 rPtr->flags.buttonPressed = False;
425 rPtr->flags.redraw = True;
426
427 rPtr->moveAction = NULL;
428 rPtr->releaseAction = NULL;
429
430 rPtr->pview = W_VIEW(parent);
431
432 return rPtr;
433 }
434
435 void WMSetRulerMargins(WMRuler * rPtr, WMRulerMargins margins)
436 {
437 if (!rPtr)
438 return;
439 rPtr->margins.left = margins.left + rPtr->offset;
440 rPtr->margins.right = margins.right + rPtr->offset;
441 rPtr->margins.first = margins.first + rPtr->offset;
442 rPtr->margins.body = margins.body + rPtr->offset;
443 rPtr->margins.tabs = margins.tabs; /*for loop */
444 rPtr->flags.redraw = True;
445 paintRuler(rPtr);
446
447 }
448
449 WMRulerMargins *WMGetRulerMargins(WMRuler * rPtr)
450 {
451 WMRulerMargins *margins = wmalloc(sizeof(WMRulerMargins));
452
453 if (!rPtr) {
454 margins->first = margins->body = margins->left = 0;
455 margins->right = 100;
456 return margins;
457 }
458
459 margins->left = rPtr->margins.left - rPtr->offset;
460 margins->right = rPtr->margins.right - rPtr->offset;
461 margins->first = rPtr->margins.first - rPtr->offset;
462 margins->body = rPtr->margins.body - rPtr->offset;
463 /*for */
464 margins->tabs = rPtr->margins.tabs;
465
466 return margins;
467 }
468
469 Bool WMIsMarginEqualToMargin(WMRulerMargins * aMargin, WMRulerMargins * anotherMargin)
470 {
471 if (aMargin == anotherMargin)
472 return True;
473 else if (!aMargin || !anotherMargin)
474 return False;
475 if (aMargin->left != anotherMargin->left)
476 return False;
477 if (aMargin->first != anotherMargin->first)
478 return False;
479 if (aMargin->body != anotherMargin->body)
480 return False;
481 if (aMargin->right != anotherMargin->right)
482 return False;
483
484 return True;
485 }
486
487 void WMSetRulerOffset(WMRuler * rPtr, int pixels)
488 {
489 if (!rPtr || pixels < 0 || pixels + MIN_DOC_WIDTH >= rPtr->view->size.width)
490 return;
491 rPtr->offset = pixels;
492 /*rulerDidResize(rPtr, rPtr->view); */
493 }
494
495 int WMGetRulerOffset(WMRuler * rPtr)
496 {
497 if (!rPtr)
498 return 0; /* what value should return if no ruler? -1 or 0? */
499 return rPtr->offset;
500 }
501
502 void WMSetRulerReleaseAction(WMRuler * rPtr, WMAction * action, void *clientData)
503 {
504 if (!rPtr)
505 return;
506
507 rPtr->releaseAction = action;
508 rPtr->clientData = clientData;
509 }
510
511 void WMSetRulerMoveAction(WMRuler * rPtr, WMAction * action, void *clientData)
512 {
513 if (!rPtr)
514 return;
515
516 rPtr->moveAction = action;
517 rPtr->clientData = clientData;
518 }
519
520 /* _which_ one was released */
521 int WMGetReleasedRulerMargin(WMRuler * rPtr)
522 {
523 if (!rPtr)
524 return 0;
525 return rPtr->flags.whichMarker;
526 }
527
528 /* _which_ one is being grabbed */
529 int WMGetGrabbedRulerMargin(WMRuler * rPtr)
530 {
531 if (!rPtr)
532 return 0;
533 return rPtr->flags.whichMarker;
534 }