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.
24 #define MIN_DOC_WIDTH 10
26 typedef struct W_Ruler
{
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 */
38 WMRulerMargins margins
;
40 int motion
; /* the position of the _moving_ marker(s) */
41 int end
; /* the last tick on the baseline (restrict markers to it) */
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;
56 /* Marker for left margin
65 static void drawLeftMarker(Ruler
* rPtr
)
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);
73 points
[1].x
= points
[0].x
+ 6;
75 points
[2].x
= points
[0].x
+ 6;
77 points
[3].x
= points
[0].x
;
79 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
80 points
, 4, Convex
, CoordModeOrigin
);
83 /* Marker for right margin
92 static void drawRightMarker(Ruler
* rPtr
)
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;
100 points
[1].x
= points
[0].x
- 6;
102 points
[2].x
= points
[0].x
- 6;
104 points
[3].x
= points
[0].x
;
106 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
107 points
, 4, Convex
, CoordModeOrigin
);
110 /* Marker for first line only
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
130 static void drawBodyMarker(Ruler
* rPtr
)
133 int xpos
= ((rPtr
->flags
.whichMarker
== 4 || rPtr
->flags
.whichMarker
== 6) ?
134 rPtr
->motion
: rPtr
->margins
.body
);
136 points
[0].x
= xpos
- 5;
138 points
[1].x
= points
[0].x
+ 11;
140 points
[2].x
= points
[0].x
+ 5;
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
)
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
)
165 int marks
[9] = { 11, 3, 5, 3, 7, 3, 5, 3 };
167 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
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);
178 w
= rPtr
->view
->size
.width
- rPtr
->margins
.left
;
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) {
184 snprintf(c
, 3, "%d", ++j
);
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);
193 rPtr
->end
= rPtr
->margins
.left
+ m
- 10;
194 if (rPtr
->margins
.right
> rPtr
->end
)
195 rPtr
->margins
.right
= rPtr
->end
;
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
)
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)
224 switch (rPtr
->flags
.whichMarker
) {
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
)
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) */
238 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
243 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
248 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
260 static int whichMarker(Ruler
* rPtr
, int x
, int y
)
262 if (x
< rPtr
->offset
|| y
> 22)
265 if (rPtr
->margins
.left
- x
>= -6 && y
<= 9 && (rPtr
->margins
.left
- x
<= 0) && y
>= 4) {
266 rPtr
->motion
= rPtr
->margins
.left
;
269 if (rPtr
->margins
.right
- x
>= -1 && y
<= 11 && rPtr
->margins
.right
- x
<= 5 && y
>= 4) {
270 rPtr
->motion
= rPtr
->margins
.right
;
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
;
282 if (rPtr
->margins
.first
- x
<= 4 && y
<= 15 && rPtr
->margins
.first
- x
>= -5 && y
>= 10) {
283 rPtr
->motion
= rPtr
->margins
.first
;
286 if (rPtr
->margins
.body
- x
<= 4 && y
<= 21 && rPtr
->margins
.body
- x
>= -5 && y
>= 17) {
287 rPtr
->motion
= rPtr
->margins
.body
;
295 static void rulerDidResize(W_ViewDelegate
* self
, WMView
* view
)
297 Ruler
*rPtr
= (Ruler
*) view
->self
;
299 createDrawBuffer(rPtr
);
300 rPtr
->flags
.redraw
= True
;
305 static void handleEvents(XEvent
* event
, void *data
)
307 Ruler
*rPtr
= (Ruler
*) data
;
309 switch (event
->type
) {
311 rulerDidResize(rPtr
->view
->delegate
, rPtr
->view
);
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
;
323 XSetLineAttributes(rPtr
->view
->screen
->display
, gc
, 1,
324 LineSolid
, CapNotLast
, JoinMiter
);
325 XDrawLine(rPtr
->pview
->screen
->display
,
327 gc
, rPtr
->motion
+ 1, 40,
328 rPtr
->motion
+ 1, rPtr
->pview
->size
.height
- 5);
334 if (event
->xbutton
.button
!= Button1
)
336 rPtr
->flags
.buttonPressed
= True
;
337 rPtr
->flags
.whichMarker
= whichMarker(rPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
341 if (event
->xbutton
.button
!= Button1
)
343 rPtr
->flags
.buttonPressed
= False
;
344 switch (rPtr
->flags
.whichMarker
) {
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
;
356 rPtr
->margins
.right
= rPtr
->motion
;
359 rPtr
->margins
.first
= rPtr
->motion
;
362 rPtr
->margins
.body
= rPtr
->motion
;
365 rPtr
->margins
.first
= rPtr
->margins
.body
= rPtr
->motion
;
368 if (rPtr
->releaseAction
)
369 (rPtr
->releaseAction
) (rPtr
, rPtr
->clientData
);
374 W_ViewDelegate _RulerViewDelegate
= {
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
));
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);
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
);
433 void WMSetRulerMargins(WMRuler
* rPtr
, WMRulerMargins margins
)
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
;
447 WMRulerMargins
*WMGetRulerMargins(WMRuler
* rPtr
)
449 WMRulerMargins
*margins
= wmalloc(sizeof(WMRulerMargins
));
452 margins
->first
= margins
->body
= margins
->left
= 0;
453 margins
->right
= 100;
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
;
462 margins
->tabs
= rPtr
->margins
.tabs
;
467 Bool
WMIsMarginEqualToMargin(WMRulerMargins
* aMargin
, WMRulerMargins
* anotherMargin
)
469 if (aMargin
== anotherMargin
)
471 else if (!aMargin
|| !anotherMargin
)
473 if (aMargin
->left
!= anotherMargin
->left
)
475 if (aMargin
->first
!= anotherMargin
->first
)
477 if (aMargin
->body
!= anotherMargin
->body
)
479 if (aMargin
->right
!= anotherMargin
->right
)
485 void WMSetRulerOffset(WMRuler
* rPtr
, int pixels
)
487 if (!rPtr
|| pixels
< 0 || pixels
+ MIN_DOC_WIDTH
>= rPtr
->view
->size
.width
)
489 rPtr
->offset
= pixels
;
490 /*rulerDidResize(rPtr, rPtr->view); */
493 int WMGetRulerOffset(WMRuler
* rPtr
)
496 return 0; /* what value should return if no ruler? -1 or 0? */
500 void WMSetRulerReleaseAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
505 rPtr
->releaseAction
= action
;
506 rPtr
->clientData
= clientData
;
509 void WMSetRulerMoveAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
514 rPtr
->moveAction
= action
;
515 rPtr
->clientData
= clientData
;
518 /* _which_ one was released */
519 int WMGetReleasedRulerMargin(WMRuler
* rPtr
)
523 return rPtr
->flags
.whichMarker
;
526 /* _which_ one is being grabbed */
527 int WMGetGrabbedRulerMargin(WMRuler
* rPtr
)
531 return rPtr
->flags
.whichMarker
;