WINGs: Small assorted formatting changes
[wmaker-crm.git] / WINGs / wruler.c
blobf8d4af03c3f6c89ad2aa21fa8499ffb1b0b20eb8
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.
21 #include "WINGsP.h"
22 #include "wconfig.h"
24 #define MIN_DOC_WIDTH 10
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) */
31 WMAction *moveAction; /* what to when while moving */
32 WMAction *releaseAction; /* what to do when released */
33 void *clientData;
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) */
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;
56 /* Marker for left margin
59 | \
60 |__\
65 static void drawLeftMarker(Ruler * rPtr)
67 XPoint points[4];
68 int xpos = (rPtr->flags.whichMarker == 1 ? rPtr->motion : rPtr->margins.left);
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);
83 /* Marker for right margin
86 / |
87 /__|
92 static void drawRightMarker(Ruler * rPtr)
94 XPoint points[4];
95 int xpos = (rPtr->flags.whichMarker == 2 ? rPtr->motion : rPtr->margins.right);
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);
110 /* Marker for first line only
111 _____
112 |___|
116 static void drawFirstMarker(Ruler * rPtr)
118 int xpos = ((rPtr->flags.whichMarker == 3 || rPtr->flags.whichMarker == 6) ?
119 rPtr->motion : rPtr->margins.first);
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);
125 /* Marker for rest of body
126 _____
130 static void drawBodyMarker(Ruler * rPtr)
132 XPoint points[4];
133 int xpos = ((rPtr->flags.whichMarker == 4 || rPtr->flags.whichMarker == 6) ?
134 rPtr->motion : rPtr->margins.body);
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);
146 static void createDrawBuffer(Ruler * rPtr)
148 if (!rPtr->view->flags.realized)
149 return;
151 if (rPtr->drawBuffer)
152 XFreePixmap(rPtr->view->screen->display, rPtr->drawBuffer);
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);
161 static void drawRulerOnPixmap(Ruler * rPtr)
163 int i, j, w, m;
164 char c[3];
165 int marks[9] = { 11, 3, 5, 3, 7, 3, 5, 3 };
167 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
168 return;
170 XFillRectangle(rPtr->view->screen->display, rPtr->drawBuffer,
171 rPtr->bgGC, 0, 0, rPtr->view->size.width, 40);
173 WMDrawString(rPtr->view->screen, rPtr->drawBuffer, rPtr->fg,
174 rPtr->font, rPtr->margins.left + 2, 26, _("0 inches"), 10);
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);
190 m = (++i) * 10;
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);
200 drawLeftMarker(rPtr);
201 drawRightMarker(rPtr);
202 drawFirstMarker(rPtr);
203 drawBodyMarker(rPtr);
205 rPtr->flags.redraw = False;
208 static void paintRuler(Ruler * rPtr)
210 if (!rPtr->drawBuffer || !rPtr->view->flags.realized)
211 return;
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);
219 static Bool verifyMarkerMove(Ruler * rPtr, int x)
221 if (rPtr->flags.whichMarker < 1 || rPtr->flags.whichMarker > 6)
222 return False;
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;
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;
237 case 3:
238 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
239 return False;
240 break;
242 case 4:
243 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
244 return False;
245 break;
247 case 6:
248 if (x >= rPtr->margins.right - MIN_DOC_WIDTH || x < rPtr->margins.left)
249 return False;
250 break;
252 default:
253 return False;
256 rPtr->motion = x;
257 return True;
260 static int whichMarker(Ruler * rPtr, int x, int y)
262 if (x < rPtr->offset || y > 22)
263 return 0;
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;
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;
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;
280 #endif
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;
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;
290 /* do tabs (5) */
292 return 0;
295 static void rulerDidResize(W_ViewDelegate * self, WMView * view)
297 Ruler *rPtr = (Ruler *) view->self;
299 createDrawBuffer(rPtr);
300 rPtr->flags.redraw = True;
301 paintRuler(rPtr);
305 static void handleEvents(XEvent * event, void *data)
307 Ruler *rPtr = (Ruler *) data;
309 switch (event->type) {
310 case Expose:
311 rulerDidResize(rPtr->view->delegate, rPtr->view);
312 break;
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));
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);
331 break;
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;
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;
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;
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;
368 if (rPtr->releaseAction)
369 (rPtr->releaseAction) (rPtr, rPtr->clientData);
370 break;
374 W_ViewDelegate _RulerViewDelegate = {
375 NULL,
376 NULL,
377 rulerDidResize,
378 NULL,
379 NULL
382 WMRuler *WMCreateRuler(WMWidget * parent)
384 Ruler *rPtr = wmalloc(sizeof(Ruler));
385 unsigned int w = WMWidgetWidth(parent);
387 rPtr->widgetClass = WC_Ruler;
389 rPtr->view = W_CreateView(W_VIEW(parent));
391 if (!rPtr->view) {
392 wfree(rPtr);
393 return NULL;
396 rPtr->view->self = rPtr;
398 rPtr->drawBuffer = (Pixmap) NULL;
400 W_ResizeView(rPtr->view, w, 40);
402 WMCreateEventHandler(rPtr->view, ExposureMask | StructureNotifyMask
403 | EnterWindowMask | LeaveWindowMask | FocusChangeMask
404 | ButtonReleaseMask | ButtonPressMask | KeyReleaseMask
405 | KeyPressMask | Button1MotionMask, handleEvents, rPtr);
407 rPtr->view->delegate = &_RulerViewDelegate;
409 rPtr->fg = WMBlackColor(rPtr->view->screen);
410 rPtr->fgGC = WMColorGC(rPtr->fg);
411 rPtr->bgGC = WMColorGC(WMGrayColor(rPtr->view->screen));
412 rPtr->font = WMSystemFontOfSize(rPtr->view->screen, 8);
414 rPtr->offset = 22;
415 rPtr->margins.left = 22;
416 rPtr->margins.body = 22;
417 rPtr->margins.first = 42;
418 rPtr->margins.right = (w < 502 ? w : 502);
419 rPtr->margins.tabs = NULL;
421 rPtr->flags.whichMarker = 0; /* none */
422 rPtr->flags.buttonPressed = False;
423 rPtr->flags.redraw = True;
425 rPtr->moveAction = NULL;
426 rPtr->releaseAction = NULL;
428 rPtr->pview = W_VIEW(parent);
430 return rPtr;
433 void WMSetRulerMargins(WMRuler * rPtr, WMRulerMargins margins)
435 if (!rPtr)
436 return;
437 rPtr->margins.left = margins.left + rPtr->offset;
438 rPtr->margins.right = margins.right + rPtr->offset;
439 rPtr->margins.first = margins.first + rPtr->offset;
440 rPtr->margins.body = margins.body + rPtr->offset;
441 rPtr->margins.tabs = margins.tabs; /*for loop */
442 rPtr->flags.redraw = True;
443 paintRuler(rPtr);
447 WMRulerMargins *WMGetRulerMargins(WMRuler * rPtr)
449 WMRulerMargins *margins = wmalloc(sizeof(WMRulerMargins));
451 if (!rPtr) {
452 margins->first = margins->body = margins->left = 0;
453 margins->right = 100;
454 return margins;
457 margins->left = rPtr->margins.left - rPtr->offset;
458 margins->right = rPtr->margins.right - rPtr->offset;
459 margins->first = rPtr->margins.first - rPtr->offset;
460 margins->body = rPtr->margins.body - rPtr->offset;
461 /*for */
462 margins->tabs = rPtr->margins.tabs;
464 return margins;
467 Bool WMIsMarginEqualToMargin(WMRulerMargins * aMargin, WMRulerMargins * anotherMargin)
469 if (aMargin == anotherMargin)
470 return True;
471 else if (!aMargin || !anotherMargin)
472 return False;
473 if (aMargin->left != anotherMargin->left)
474 return False;
475 if (aMargin->first != anotherMargin->first)
476 return False;
477 if (aMargin->body != anotherMargin->body)
478 return False;
479 if (aMargin->right != anotherMargin->right)
480 return False;
482 return True;
485 void WMSetRulerOffset(WMRuler * rPtr, int pixels)
487 if (!rPtr || pixels < 0 || pixels + MIN_DOC_WIDTH >= rPtr->view->size.width)
488 return;
489 rPtr->offset = pixels;
490 /*rulerDidResize(rPtr, rPtr->view); */
493 int WMGetRulerOffset(WMRuler * rPtr)
495 if (!rPtr)
496 return 0; /* what value should return if no ruler? -1 or 0? */
497 return rPtr->offset;
500 void WMSetRulerReleaseAction(WMRuler * rPtr, WMAction * action, void *clientData)
502 if (!rPtr)
503 return;
505 rPtr->releaseAction = action;
506 rPtr->clientData = clientData;
509 void WMSetRulerMoveAction(WMRuler * rPtr, WMAction * action, void *clientData)
511 if (!rPtr)
512 return;
514 rPtr->moveAction = action;
515 rPtr->clientData = clientData;
518 /* _which_ one was released */
519 int WMGetReleasedRulerMargin(WMRuler * rPtr)
521 if (!rPtr)
522 return 0;
523 return rPtr->flags.whichMarker;
526 /* _which_ one is being grabbed */
527 int WMGetGrabbedRulerMargin(WMRuler * rPtr)
529 if (!rPtr)
530 return 0;
531 return rPtr->flags.whichMarker;