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.
25 #define MIN_DOC_WIDTH 10
27 typedef struct W_Ruler
{
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 */
39 WMRulerMargins margins
;
41 int motion
; /* the position of the _moving_ marker(s) */
42 int end
; /* the last tick on the baseline (restrict markers to it) */
47 unsigned int whichMarker
:3;
48 /* 0, 1, 2, 3, 4, 5, 6 */
49 /* none, left, right, first, body, tabstop, first & body */
51 unsigned int buttonPressed
:1;
52 unsigned int redraw
:1;
53 unsigned int RESERVED
:27;
59 /* Marker for left margin
69 drawLeftMarker(Ruler
* rPtr
)
72 int xpos
= (rPtr
->flags
.whichMarker
==1 ? rPtr
->motion
: rPtr
->margins
.left
);
74 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
78 points
[1].x
= points
[0].x
+ 6;
80 points
[2].x
= points
[0].x
+ 6;
82 points
[3].x
= points
[0].x
;
84 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
85 points
, 4, Convex
, CoordModeOrigin
);
89 /* Marker for right margin
99 drawRightMarker(Ruler
* rPtr
)
102 int xpos
= (rPtr
->flags
.whichMarker
==2 ? rPtr
->motion
: rPtr
->margins
.right
);
104 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
106 points
[0].x
= xpos
+ 1;
108 points
[1].x
= points
[0].x
- 6;
110 points
[2].x
= points
[0].x
- 6;
112 points
[3].x
= points
[0].x
;
114 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
115 points
, 4, Convex
, CoordModeOrigin
);
119 /* Marker for first line only
126 drawFirstMarker(Ruler
* rPtr
)
128 int xpos
= ((rPtr
->flags
.whichMarker
== 3 || rPtr
->flags
.whichMarker
== 6) ?
129 rPtr
->motion
: rPtr
->margins
.first
);
131 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
132 xpos
- 5, 10, 11, 5);
133 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
137 /* Marker for rest of body
143 drawBodyMarker(Ruler
* rPtr
)
146 int xpos
= ((rPtr
->flags
.whichMarker
== 4 || rPtr
->flags
.whichMarker
== 6) ?
147 rPtr
->motion
: rPtr
->margins
.body
);
149 points
[0].x
= xpos
- 5;
151 points
[1].x
= points
[0].x
+ 11;
153 points
[2].x
= points
[0].x
+ 5;
155 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
156 points
, 3, Convex
, CoordModeOrigin
);
161 createDrawBuffer(Ruler
* rPtr
)
163 if(!rPtr
->view
->flags
.realized
)
166 if (rPtr
->drawBuffer
)
167 XFreePixmap(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
);
169 rPtr
->drawBuffer
= XCreatePixmap(rPtr
->view
->screen
->display
,
170 rPtr
->view
->window
, rPtr
->view
->size
.width
, 40,
171 rPtr
->view
->screen
->depth
);
172 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
173 rPtr
->bgGC
, 0, 0, rPtr
->view
->size
.width
, 40);
178 drawRulerOnPixmap(Ruler
* rPtr
)
182 int marks
[9] = {11, 3, 5, 3, 7, 3, 5, 3};
184 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
188 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
189 rPtr
->bgGC
, 0, 0, rPtr
->view
->size
.width
, 40);
191 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
, rPtr
->fg
,
192 rPtr
->font
, rPtr
->margins
.left
+ 2, 26, _("0 inches"), 10);
196 w
= rPtr
->view
->size
.width
- rPtr
->margins
.left
;
198 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
199 rPtr
->fgGC
, rPtr
->margins
.left
+ m
, 23,
200 rPtr
->margins
.left
+ m
, marks
[i
% 8] + 23);
201 if (i
!= 0 && i
% 8 == 0) {
203 snprintf(c
, 3, "%d", ++j
);
205 snprintf(c
, 3, "%2d", ++j
);
206 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
, rPtr
->fg
,
207 rPtr
->font
, rPtr
->margins
.left
+ 2 + m
, 26, c
, 2);
212 rPtr
->end
= rPtr
->margins
.left
+ m
- 10;
213 if (rPtr
->margins
.right
> rPtr
->end
)
214 rPtr
->margins
.right
= rPtr
->end
;
216 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
217 rPtr
->margins
.left
, 22, rPtr
->margins
.left
+ m
- 10, 22);
219 drawLeftMarker(rPtr
);
220 drawRightMarker(rPtr
);
221 drawFirstMarker(rPtr
);
222 drawBodyMarker(rPtr
);
224 rPtr
->flags
.redraw
= False
;
229 paintRuler(Ruler
* rPtr
)
231 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
234 if (rPtr
->flags
.redraw
)
235 drawRulerOnPixmap(rPtr
);
236 XCopyArea(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
237 rPtr
->view
->window
, rPtr
->bgGC
, 0, 0,
238 rPtr
->view
->size
.width
, 40, 0, 0);
243 verifyMarkerMove(Ruler
* rPtr
, int x
)
245 if (rPtr
->flags
.whichMarker
< 1 || rPtr
->flags
.whichMarker
> 6)
248 switch (rPtr
->flags
.whichMarker
) {
250 if (x
> rPtr
->margins
.right
- 10 || x
< rPtr
->offset
||
251 rPtr
->margins
.body
+ x
> rPtr
->margins
.right
- MIN_DOC_WIDTH
||
252 rPtr
->margins
.first
+ x
> rPtr
->margins
.right
- MIN_DOC_WIDTH
)
257 if (x
< rPtr
->margins
.first
+ MIN_DOC_WIDTH
||
258 x
< rPtr
->margins
.body
+ MIN_DOC_WIDTH
||
259 x
< rPtr
->margins
.left
+ MIN_DOC_WIDTH
||
260 x
> rPtr
->end
) /*rPtr->view->size.width) */
265 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
270 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
275 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
289 whichMarker(Ruler
* rPtr
, int x
, int y
)
291 if (x
< rPtr
->offset
|| y
> 22)
294 if (rPtr
->margins
.left
- x
>= -6 && y
<= 9
295 && (rPtr
->margins
.left
- x
<= 0) && y
>= 4) {
296 rPtr
->motion
= rPtr
->margins
.left
;
299 if (rPtr
->margins
.right
- x
>= -1 && y
<= 11
300 && rPtr
->margins
.right
- x
<= 5 && y
>= 4) {
301 rPtr
->motion
= rPtr
->margins
.right
;
305 /* both first and body? */
306 if (rPtr
->margins
.first
- x
<= 4 && rPtr
->margins
.first
- x
>= -5
307 && rPtr
->margins
.body
- x
<= 4 && rPtr
->margins
.body
- x
>= -5
308 && y
>= 15 && y
<= 17) {
309 rPtr
->motion
= rPtr
->margins
.first
;
314 if (rPtr
->margins
.first
- x
<= 4 && y
<= 15
315 && rPtr
->margins
.first
- x
>= -5 && y
>= 10) {
316 rPtr
->motion
= rPtr
->margins
.first
;
319 if (rPtr
->margins
.body
- x
<= 4 && y
<= 21 &&
320 rPtr
->margins
.body
- x
>= -5 && y
>= 17) {
321 rPtr
->motion
= rPtr
->margins
.body
;
331 rulerDidResize(W_ViewDelegate
* self
, WMView
* view
)
333 Ruler
*rPtr
= (Ruler
*) view
->self
;
335 createDrawBuffer(rPtr
);
336 rPtr
->flags
.redraw
= True
;
343 handleEvents(XEvent
* event
, void *data
)
345 Ruler
*rPtr
= (Ruler
*) data
;
347 switch (event
->type
) {
349 rulerDidResize(rPtr
->view
->delegate
, rPtr
->view
);
353 if (rPtr
->flags
.buttonPressed
354 && (event
->xmotion
.state
& Button1Mask
)) {
355 if (verifyMarkerMove(rPtr
, event
->xmotion
.x
)) {
356 GC gc
= WMColorGC(WMDarkGrayColor(rPtr
->view
->screen
));
358 if (rPtr
->moveAction
)
359 (rPtr
->moveAction
) (rPtr
, rPtr
->clientData
);
360 rPtr
->flags
.redraw
= True
;
362 XSetLineAttributes(rPtr
->view
->screen
->display
, gc
, 1,
363 LineSolid
, CapNotLast
, JoinMiter
);
364 XDrawLine(rPtr
->pview
->screen
->display
,
366 gc
, rPtr
->motion
+ 1, 40,
367 rPtr
->motion
+ 1, rPtr
->pview
->size
.height
- 5);
373 if (event
->xbutton
.button
!= Button1
)
375 rPtr
->flags
.buttonPressed
= True
;
376 rPtr
->flags
.whichMarker
=
377 whichMarker(rPtr
, event
->xmotion
.x
,
382 if (event
->xbutton
.button
!= Button1
)
384 rPtr
->flags
.buttonPressed
= False
;
385 switch (rPtr
->flags
.whichMarker
) {
387 int change
= rPtr
->margins
.left
- rPtr
->motion
;
389 rPtr
->margins
.first
-= change
;
390 rPtr
->margins
.body
-= change
;
391 rPtr
->margins
.left
= rPtr
->motion
;
392 rPtr
->flags
.redraw
= True
;
397 rPtr
->margins
.right
= rPtr
->motion
;
400 rPtr
->margins
.first
= rPtr
->motion
;
403 rPtr
->margins
.body
= rPtr
->motion
;
406 rPtr
->margins
.first
= rPtr
->margins
.body
410 if (rPtr
->releaseAction
)
411 (rPtr
->releaseAction
) (rPtr
, rPtr
->clientData
);
417 W_ViewDelegate _RulerViewDelegate
=
428 WMCreateRuler(WMWidget
* parent
)
430 Ruler
*rPtr
= wmalloc(sizeof(Ruler
));
431 unsigned int w
= WMWidgetWidth(parent
);
433 memset(rPtr
, 0, sizeof(Ruler
));
435 rPtr
->widgetClass
= WC_Ruler
;
437 rPtr
->view
= W_CreateView(W_VIEW(parent
));
444 rPtr
->view
->self
= rPtr
;
446 rPtr
->drawBuffer
= (Pixmap
) NULL
;
448 W_ResizeView(rPtr
->view
, w
, 40);
450 WMCreateEventHandler(rPtr
->view
, ExposureMask
| StructureNotifyMask
451 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
452 | ButtonReleaseMask
| ButtonPressMask
| KeyReleaseMask
453 | KeyPressMask
| Button1MotionMask
, handleEvents
, rPtr
);
455 rPtr
->view
->delegate
= &_RulerViewDelegate
;
457 rPtr
->fg
= WMBlackColor(rPtr
->view
->screen
);
458 rPtr
->fgGC
= WMColorGC(rPtr
->fg
);
459 rPtr
->bgGC
= WMColorGC(WMGrayColor(rPtr
->view
->screen
));
460 rPtr
->font
= WMSystemFontOfSize(rPtr
->view
->screen
, 8);
463 rPtr
->margins
.left
= 22;
464 rPtr
->margins
.body
= 22;
465 rPtr
->margins
.first
= 42;
466 rPtr
->margins
.right
= (w
< 502 ? w
: 502);
467 rPtr
->margins
.tabs
= NULL
;
469 rPtr
->flags
.whichMarker
= 0; /* none */
470 rPtr
->flags
.buttonPressed
= False
;
471 rPtr
->flags
.redraw
= True
;
473 rPtr
->moveAction
= NULL
;
474 rPtr
->releaseAction
= NULL
;
476 rPtr
->pview
= W_VIEW(parent
);
483 WMSetRulerMargins(WMRuler
* rPtr
, WMRulerMargins margins
)
487 rPtr
->margins
.left
= margins
.left
+ rPtr
->offset
;
488 rPtr
->margins
.right
= margins
.right
+ rPtr
->offset
;
489 rPtr
->margins
.first
= margins
.first
+ rPtr
->offset
;
490 rPtr
->margins
.body
= margins
.body
+ rPtr
->offset
;
491 rPtr
->margins
.tabs
= margins
.tabs
; /*for loop */
492 rPtr
->flags
.redraw
= True
;
499 WMGetRulerMargins(WMRuler
* rPtr
)
501 WMRulerMargins
*margins
= wmalloc(sizeof(WMRulerMargins
));
504 margins
->first
= margins
->body
= margins
->left
= 0;
505 margins
->right
= 100;
509 margins
->left
= rPtr
->margins
.left
- rPtr
->offset
;
510 margins
->right
= rPtr
->margins
.right
- rPtr
->offset
;
511 margins
->first
= rPtr
->margins
.first
- rPtr
->offset
;
512 margins
->body
= rPtr
->margins
.body
- rPtr
->offset
;
514 margins
->tabs
= rPtr
->margins
.tabs
;
521 WMIsMarginEqualToMargin(WMRulerMargins
*aMargin
, WMRulerMargins
*anotherMargin
)
523 if(aMargin
== anotherMargin
)
525 else if(!aMargin
|| !anotherMargin
)
527 if(aMargin
->left
!= anotherMargin
->left
)
529 if(aMargin
->first
!= anotherMargin
->first
)
531 if(aMargin
->body
!= anotherMargin
->body
)
533 if(aMargin
->right
!= anotherMargin
->right
)
543 WMSetRulerOffset(WMRuler
* rPtr
, int pixels
)
545 if (!rPtr
|| pixels
< 0 || pixels
+ MIN_DOC_WIDTH
>= rPtr
->view
->size
.width
)
547 rPtr
->offset
= pixels
;
548 /*rulerDidResize(rPtr, rPtr->view); */
553 WMGetRulerOffset(WMRuler
* rPtr
)
556 return 0; /* what value should return if no ruler? -1 or 0? */
562 WMSetRulerReleaseAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
567 rPtr
->releaseAction
= action
;
568 rPtr
->clientData
= clientData
;
573 WMSetRulerMoveAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
578 rPtr
->moveAction
= action
;
579 rPtr
->clientData
= clientData
;
583 /* _which_ one was released */
585 WMGetReleasedRulerMargin(WMRuler
* rPtr
)
589 return rPtr
->flags
.whichMarker
;
593 /* _which_ one is being grabbed */
595 WMGetGrabbedRulerMargin(WMRuler
* rPtr
)
599 return rPtr
->flags
.whichMarker
;