- s/sprintf/snprintf
[wmaker-crm.git] / WINGs / wruler.c
blobd3242b4459b0774af104fbeb582f9ade48b3d985
1 /*
2 * WINGs WMRuler: nifty ruler widget for WINGs :-)
4 * Copyright (c) 1999-2000 Nwanua Elumeze
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.
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.
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.
22 #include "WINGsP.h"
23 #include "wconfig.h"
25 #define MIN_DOC_WIDTH 10
27 typedef struct W_Ruler {
28 W_Class widgetClass;
29 W_View *view;
30 W_View *pview; /* the parent's view (for drawing the line) */
32 WMAction *moveAction; /* what to when while moving */
33 WMAction *releaseAction; /* what to do when released */
34 void *clientData;
36 GC fg, bg;
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) */
43 Pixmap drawBuffer;
45 struct {
46 unsigned int whichMarker:3;
47 /* 0, 1, 2, 3, 4, 5, 6 */
48 /* none, left, right, first, body, tabstop, first & body */
50 unsigned int buttonPressed:1;
51 unsigned int redraw:1;
52 unsigned int RESERVED:27;
53 } flags;
54 } Ruler;
58 /* Marker for left margin
61 | \
62 |__\
67 static void
68 drawLeftMarker(Ruler * rPtr)
70 XPoint points[4];
71 int xpos = (rPtr->flags.whichMarker == 1 ?
72 rPtr->motion : rPtr->margins.left);
74 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer,
75 rPtr->fg, xpos, 8, xpos, 22);
76 points[0].x = xpos;
77 points[0].y = 1;
78 points[1].x = points[0].x + 6;
79 points[1].y = 8;
80 points[2].x = points[0].x + 6;
81 points[2].y = 9;
82 points[3].x = points[0].x;
83 points[3].y = 9;
84 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer,
85 rPtr->fg, points, 4, Convex, CoordModeOrigin);
89 /* Marker for right margin
92 / |
93 /__|
98 static void drawRightMarker(Ruler * rPtr)
100 XPoint points[4];
101 int xpos = (rPtr->flags.whichMarker == 2 ?
102 rPtr->motion : rPtr->margins.right);
104 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer,
105 rPtr->fg, xpos, 8, xpos, 22);
106 points[0].x = xpos + 1;
107 points[0].y = 0;
108 points[1].x = points[0].x - 6;
109 points[1].y = 7;
110 points[2].x = points[0].x - 6;
111 points[2].y = 9;
112 points[3].x = points[0].x;
113 points[3].y = 9;
114 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer,
115 rPtr->fg, points, 4, Convex, CoordModeOrigin);
119 /* Marker for first line only
120 _____
121 |___|
125 static void drawFirstMarker(Ruler * rPtr)
127 int xpos = ((rPtr->flags.whichMarker == 3 || rPtr->flags.whichMarker == 6) ?
128 rPtr->motion : rPtr->margins.first);
130 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
131 rPtr->fg, xpos - 5, 10, 11, 5);
132 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer,
133 rPtr->fg, xpos, 12, xpos, 22);
136 /* Marker for rest of body
137 _____
141 static void drawBodyMarker(Ruler * rPtr)
143 XPoint points[4];
144 int xpos = ((rPtr->flags.whichMarker == 4 || rPtr->flags.whichMarker == 6) ?
145 rPtr->motion : rPtr->margins.body);
147 points[0].x = xpos - 5;
148 points[0].y = 16;
149 points[1].x = points[0].x + 11;
150 points[1].y = 16;
151 points[2].x = points[0].x + 5;
152 points[2].y = 22;
153 XFillPolygon(rPtr->view->screen->display, rPtr->drawBuffer,
154 rPtr->fg, points, 3, Convex, CoordModeOrigin);
158 static void createDrawBuffer(Ruler * rPtr)
160 if(!rPtr->view->flags.realized)
161 return;
163 if (rPtr->drawBuffer)
164 XFreePixmap(rPtr->view->screen->display, rPtr->drawBuffer);
166 rPtr->drawBuffer = XCreatePixmap(rPtr->view->screen->display,
167 rPtr->view->window, rPtr->view->size.width, 40,
168 rPtr->view->screen->depth);
169 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
170 rPtr->bg, 0, 0, rPtr->view->size.width, 40);
174 static void drawRulerOnPixmap(Ruler * rPtr)
176 int i, j, w, m;
177 char c[3];
178 int marks[9] =
179 {11, 3, 5, 3, 7, 3, 5, 3};
181 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
182 return;
185 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
186 rPtr->bg, 0, 0, rPtr->view->size.width, 40);
188 WMDrawString(rPtr->view->screen, rPtr->drawBuffer, rPtr->fg,
189 rPtr->font, rPtr->margins.left + 2, 26, _("0 inches"), 10);
191 /* marker ticks */
192 i = j = m = 0;
193 w = rPtr->view->size.width - rPtr->margins.left;
194 while (m < w) {
195 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer,
196 rPtr->fg, rPtr->margins.left + m, 23,
197 rPtr->margins.left + m, marks[i % 8] + 23);
198 if (i != 0 && i % 8 == 0) {
199 if (j < 10)
200 snprintf(c, 3, "%d", ++j);
201 else
202 snprintf(c, 3, "%2d", ++j);
203 WMDrawString(rPtr->view->screen, rPtr->drawBuffer, rPtr->fg,
204 rPtr->font, rPtr->margins.left + 2 + m, 26, c, 2);
206 m = (++i) * 10;
209 rPtr->end = rPtr->margins.left + m - 10;
210 if (rPtr->margins.right > rPtr->end)
211 rPtr->margins.right = rPtr->end;
212 /* base line */
213 XDrawLine(rPtr->view->screen->display, rPtr->drawBuffer, rPtr->fg,
214 rPtr->margins.left, 22, rPtr->margins.left + m - 10, 22);
216 drawLeftMarker(rPtr);
217 drawRightMarker(rPtr);
218 drawFirstMarker(rPtr);
219 drawBodyMarker(rPtr);
221 rPtr->flags.redraw = False;
225 static void paintRuler(Ruler * rPtr)
227 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
228 return;
230 if (rPtr->flags.redraw)
231 drawRulerOnPixmap(rPtr);
232 XCopyArea(rPtr->view->screen->display, rPtr->drawBuffer,
233 rPtr->view->window, rPtr->bg, 0, 0, rPtr->view->size.width, 40,
234 0, 0);
238 static Bool
239 verifyMarkerMove(Ruler * rPtr, int x)
241 if (rPtr->flags.whichMarker < 1 || rPtr->flags.whichMarker > 6)
242 return False;
244 switch (rPtr->flags.whichMarker) {
245 case 1:
246 if (x > rPtr->margins.right - 10 || x < rPtr->offset ||
247 rPtr->margins.body + x > rPtr->margins.right - MIN_DOC_WIDTH ||
248 rPtr->margins.first + x > rPtr->margins.right - MIN_DOC_WIDTH)
249 return False;
250 break;
252 case 2:
253 if (x < rPtr->margins.first + MIN_DOC_WIDTH ||
254 x < rPtr->margins.body + MIN_DOC_WIDTH ||
255 x < rPtr->margins.left + MIN_DOC_WIDTH ||
256 x > rPtr->end) /*rPtr->view->size.width) */
257 return False;
258 break;
260 case 3:
261 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
262 return False;
263 break;
265 case 4:
266 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
267 return False;
268 break;
270 case 6:
271 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
272 return False;
273 break;
275 default:
276 return False;
279 rPtr->motion = x;
280 return True;
284 static int whichMarker(Ruler * rPtr, int x, int y)
286 if (x < rPtr->offset || y > 22)
287 return 0;
289 if (rPtr->margins.left - x >= -6 && y <= 9
290 && (rPtr->margins.left - x <= 0) && y >= 4) {
291 rPtr->motion = rPtr->margins.left;
292 return 1;
294 if (rPtr->margins.right - x >= -1 && y <= 11
295 && rPtr->margins.right - x <= 5 && y >= 4) {
296 rPtr->motion = rPtr->margins.right;
297 return 2;
299 #if 0
300 /* both first and body? */
301 if (rPtr->margins.first - x <= 4 && rPtr->margins.first - x >= -5
302 && rPtr->margins.body - x <= 4 && rPtr->margins.body - x >= -5
303 && y >= 15 && y <= 17) {
304 rPtr->motion = rPtr->margins.first;
305 return 6;
307 #endif
309 if (rPtr->margins.first - x <= 4 && y <= 15
310 && rPtr->margins.first - x >= -5 && y >= 10) {
311 rPtr->motion = rPtr->margins.first;
312 return 3;
314 if (rPtr->margins.body - x <= 4 && y <= 21 &&
315 rPtr->margins.body - x >= -5 && y >= 17) {
316 rPtr->motion = rPtr->margins.body;
317 return 4;
319 /* do tabs (5) */
322 return 0;
325 static void rulerDidResize(W_ViewDelegate * self, WMView * view)
327 Ruler *rPtr = (Ruler *) view->self;
329 createDrawBuffer(rPtr);
330 rPtr->flags.redraw = True;
331 paintRuler(rPtr);
336 static void handleEvents(XEvent * event, void *data)
338 Ruler *rPtr = (Ruler *) data;
340 switch (event->type) {
341 case Expose:
342 rulerDidResize(rPtr->view->delegate, rPtr->view);
343 break;
345 case MotionNotify:
346 if (rPtr->flags.buttonPressed
347 && (event->xmotion.state & Button1Mask)) {
348 if (verifyMarkerMove(rPtr, event->xmotion.x)) {
349 GC gc = WMColorGC(WMDarkGrayColor(rPtr->view->screen));
351 if (rPtr->moveAction)
352 (rPtr->moveAction) (rPtr, rPtr->clientData);
353 rPtr->flags.redraw = True;
354 paintRuler(rPtr);
355 XSetLineAttributes(rPtr->view->screen->display, gc, 1,
356 LineSolid, CapNotLast, JoinMiter);
357 XDrawLine(rPtr->pview->screen->display,
358 rPtr->pview->window,
359 gc, rPtr->motion + 1, 40,
360 rPtr->motion + 1, rPtr->pview->size.height - 5);
363 break;
365 case ButtonPress:
366 if (event->xbutton.button != Button1)
367 return;
368 rPtr->flags.buttonPressed = True;
369 rPtr->flags.whichMarker =
370 whichMarker(rPtr, event->xmotion.x,
371 event->xmotion.y);
372 break;
374 case ButtonRelease:
375 if (event->xbutton.button != Button1)
376 return;
377 rPtr->flags.buttonPressed = False;
378 switch (rPtr->flags.whichMarker) {
379 case 1:{
380 int change = rPtr->margins.left - rPtr->motion;
382 rPtr->margins.first -= change;
383 rPtr->margins.body -= change;
384 rPtr->margins.left = rPtr->motion;
385 rPtr->flags.redraw = True;
386 paintRuler(rPtr);
387 break;
389 case 2:
390 rPtr->margins.right = rPtr->motion;
391 break;
392 case 3:
393 rPtr->margins.first = rPtr->motion;
394 break;
395 case 4:
396 rPtr->margins.body = rPtr->motion;
397 break;
398 case 6:
399 rPtr->margins.first = rPtr->margins.body
400 = rPtr->motion;
401 break;
403 if (rPtr->releaseAction)
404 (rPtr->releaseAction) (rPtr, rPtr->clientData);
405 break;
410 W_ViewDelegate _RulerViewDelegate =
412 NULL,
413 NULL,
414 rulerDidResize,
415 NULL,
416 NULL
420 WMRuler *
421 WMCreateRuler(WMWidget * parent)
423 Ruler *rPtr = wmalloc(sizeof(Ruler));
424 unsigned int w = WMWidgetWidth(parent);
426 memset(rPtr, 0, sizeof(Ruler));
428 rPtr->widgetClass = WC_Ruler;
430 rPtr->view = W_CreateView(W_VIEW(parent));
432 if (!rPtr->view) {
433 wfree(rPtr);
434 return NULL;
437 rPtr->view->self = rPtr;
439 rPtr->drawBuffer = (Pixmap) NULL;
441 W_ResizeView(rPtr->view, w, 40);
443 WMCreateEventHandler(rPtr->view, ExposureMask | StructureNotifyMask
444 | EnterWindowMask | LeaveWindowMask | FocusChangeMask
445 | ButtonReleaseMask | ButtonPressMask | KeyReleaseMask
446 | KeyPressMask | Button1MotionMask, handleEvents, rPtr);
448 rPtr->view->delegate = &_RulerViewDelegate;
450 rPtr->bg = WMColorGC(WMGrayColor(rPtr->view->screen));
451 rPtr->fg = WMColorGC(WMBlackColor(rPtr->view->screen));
452 rPtr->font = WMSystemFontOfSize(rPtr->view->screen, 8);
454 rPtr->offset = 22;
455 rPtr->margins.left = 22;
456 rPtr->margins.body = 22;
457 rPtr->margins.first = 42;
458 rPtr->margins.right = (w < 502 ? w : 502);
459 rPtr->margins.tabs = NULL;
461 rPtr->flags.whichMarker = 0; /* none */
462 rPtr->flags.buttonPressed = False;
463 rPtr->flags.redraw = True;
465 rPtr->moveAction = NULL;
466 rPtr->releaseAction = NULL;
468 rPtr->pview = W_VIEW(parent);
470 return rPtr;
474 void WMSetRulerMargins(WMRuler * rPtr, WMRulerMargins margins)
476 if (!rPtr)
477 return;
478 rPtr->margins.left = margins.left + rPtr->offset;
479 rPtr->margins.right = margins.right + rPtr->offset;
480 rPtr->margins.first = margins.first + rPtr->offset;
481 rPtr->margins.body = margins.body + rPtr->offset;
482 rPtr->margins.tabs = margins.tabs; /*for loop */
483 rPtr->flags.redraw = True;
484 paintRuler(rPtr);
489 WMRulerMargins *
490 WMGetRulerMargins(WMRuler * rPtr)
492 WMRulerMargins *margins = wmalloc(sizeof(WMRulerMargins));
494 if (!rPtr) {
495 margins->first = margins->body = margins->left = 0;
496 margins->right = 100;
497 return margins;
500 margins->left = rPtr->margins.left - rPtr->offset;
501 margins->right = rPtr->margins.right - rPtr->offset;
502 margins->first = rPtr->margins.first - rPtr->offset;
503 margins->body = rPtr->margins.body - rPtr->offset;
504 /*for */
505 margins->tabs = rPtr->margins.tabs;
507 return margins;
511 Bool
512 WMIsMarginEqualToMargin(WMRulerMargins *aMargin, WMRulerMargins *anotherMargin)
514 if(aMargin == anotherMargin)
515 return True;
516 else if(!aMargin || !anotherMargin)
517 return False;
518 if(aMargin->left != anotherMargin->left)
519 return False;
520 if(aMargin->first != anotherMargin->first)
521 return False;
522 if(aMargin->body != anotherMargin->body)
523 return False;
524 if(aMargin->right != anotherMargin->right)
525 return False;
527 return True;
533 void WMSetRulerOffset(WMRuler * rPtr, int pixels)
535 if (!rPtr || pixels < 0 || pixels + MIN_DOC_WIDTH >= rPtr->view->size.width)
536 return;
537 rPtr->offset = pixels;
538 /*rulerDidResize(rPtr, rPtr->view); */
542 int WMGetRulerOffset(WMRuler * rPtr)
544 if (!rPtr)
545 return 0; /* what value should return if no ruler? -1 or 0? */
546 return rPtr->offset;
550 void WMSetRulerReleaseAction(WMRuler * rPtr, WMAction * action, void *clientData)
552 if (!rPtr)
553 return;
555 rPtr->releaseAction = action;
556 rPtr->clientData = clientData;
560 void WMSetRulerMoveAction(WMRuler * rPtr, WMAction * action, void *clientData)
562 if (!rPtr)
563 return;
565 rPtr->moveAction = action;
566 rPtr->clientData = clientData;
570 /* _which_ one was released */
571 int WMGetReleasedRulerMargin(WMRuler * rPtr)
573 if (!rPtr)
574 return 0;
575 return rPtr->flags.whichMarker;
579 /* _which_ one is being grabbed */
580 int WMGetGrabbedRulerMargin(WMRuler * rPtr)
582 if (!rPtr)
583 return 0;
584 return rPtr->flags.whichMarker;