1 /* WMRuler: nifty ruler widget for WINGs (OK, for WMText ;-) */
2 /* Copyleft (>) 1999, 2000 Nwanua Elumeze <nwanua@colorado.edu> */
7 #define MIN_DOC_WIDTH 10
9 typedef struct W_Ruler
{
12 W_View
*pview
; /* the parent's view (for drawing the line) */
14 WMAction
*moveAction
; /* what to when while moving */
15 WMAction
*releaseAction
; /* what to do when released */
20 WMRulerMargins margins
;
22 int motion
; /* the position of the _moving_ marker(s) */
23 int end
; /* the last tick on the baseline (restrict markers to it) */
28 unsigned int buttonPressed
:1;
29 /* 0, 1, 2, 3, 4, 5, 6 */
30 unsigned int whichMarker
:3; /* none, left, right, first, body, tabstop, both */
31 unsigned int RESERVED
:28;
37 /* Marker for left margin
45 drawLeftMarker(Ruler
*rPtr
)
48 int xpos
= (rPtr
->flags
.whichMarker
==1?
49 rPtr
->motion
:rPtr
->margins
.left
);
51 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
52 rPtr
->fg
, xpos
, 8, xpos
, 22);
55 points
[1].x
= points
[0].x
+6;
57 points
[2].x
= points
[0].x
+6;
59 points
[3].x
= points
[0].x
;
61 XFillPolygon (rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
62 rPtr
->fg
, points
, 4, Convex
, CoordModeOrigin
);
65 /* Marker for right margin
74 drawRightMarker(Ruler
*rPtr
)
77 int xpos
= (rPtr
->flags
.whichMarker
==2?
78 rPtr
->motion
:rPtr
->margins
.right
);
80 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
81 rPtr
->fg
, xpos
, 8, xpos
, 22);
84 points
[1].x
= points
[0].x
-6;
86 points
[2].x
= points
[0].x
-6;
88 points
[3].x
= points
[0].x
;
90 XFillPolygon (rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
91 rPtr
->fg
, points
, 4, Convex
, CoordModeOrigin
);
95 /* Marker for first line only
100 drawFirstMarker(Ruler
*rPtr
)
102 int xpos
= ((rPtr
->flags
.whichMarker
==3 || rPtr
->flags
.whichMarker
==6)?
103 rPtr
->motion
:rPtr
->margins
.first
);
104 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
105 rPtr
->fg
, xpos
-5, 10, 11, 5);
106 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
107 rPtr
->fg
, xpos
, 12, xpos
, 22);
110 /* Marker for rest of body
115 drawBodyMarker(Ruler
*rPtr
)
118 int xpos
= ((rPtr
->flags
.whichMarker
==4 || rPtr
->flags
.whichMarker
==6)?
119 rPtr
->motion
:rPtr
->margins
.body
);
120 points
[0].x
= xpos
-5;
122 points
[1].x
= points
[0].x
+11;
124 points
[2].x
= points
[0].x
+5;
126 XFillPolygon (rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
127 rPtr
->fg
, points
, 3, Convex
, CoordModeOrigin
);
131 createDrawBuffer(Ruler
*rPtr
)
134 XFreePixmap(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
);
135 rPtr
->drawBuffer
= XCreatePixmap(rPtr
->view
->screen
->display
,
136 rPtr
->view
->window
, rPtr
->view
->size
.width
, 40,
137 rPtr
->view
->screen
->depth
);
138 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
139 rPtr
->bg
, 0, 0, rPtr
->view
->size
.width
, 40);
143 drawRulerOnPixmap(Ruler
*rPtr
)
147 int marks
[9] = {11, 3, 5, 3, 7, 3, 5, 3};
149 if(!rPtr
->drawBuffer
)
150 createDrawBuffer(rPtr
);
152 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
153 rPtr
->bg
, 0, 0, rPtr
->view
->size
.width
, 40);
156 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
,
157 rPtr
->fg
, rPtr
->font
, rPtr
->margins
.left
+2, 26, "0 inches", 10);
161 w
= rPtr
->view
->size
.width
- rPtr
->margins
.left
;
163 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
164 rPtr
->fg
, rPtr
->margins
.left
+m
, 23,
165 rPtr
->margins
.left
+m
, marks
[i
%8]+23);
168 snprintf(c
,3,"%d",++j
);
170 snprintf(c
,3,"%2d",++j
);
171 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
,
172 rPtr
->fg
, rPtr
->font
, rPtr
->margins
.left
+2+m
, 26, c
, 2);
177 rPtr
->end
= rPtr
->margins
.left
+m
-10;
178 if(rPtr
->margins
.right
> rPtr
->end
)
179 rPtr
->margins
.right
= rPtr
->end
;
181 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
182 rPtr
->fg
, rPtr
->margins
.left
, 22, rPtr
->margins
.left
+m
-10, 22);
184 drawLeftMarker(rPtr
);
185 drawRightMarker(rPtr
);
186 drawFirstMarker(rPtr
);
187 drawBodyMarker(rPtr
);
191 paintRuler(Ruler
*rPtr
)
193 WMScreen
*screen
= rPtr
->view
->screen
;
194 if(1||!rPtr
->drawBuffer
) { //first exposure
195 drawRulerOnPixmap(rPtr
);
198 XCopyArea(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
199 rPtr
->view
->window
, rPtr
->bg
, 0, 0, rPtr
->view
->size
.width
, 40,
204 verifyMarkerMove(Ruler
*rPtr
, int x
)
206 if(rPtr
->flags
.whichMarker
<1 || rPtr
->flags
.whichMarker
>6)
209 switch(rPtr
->flags
.whichMarker
) {
211 if( x
> rPtr
->margins
.right
- 10
212 || rPtr
->margins
.body
+ x
> rPtr
->margins
.right
-MIN_DOC_WIDTH
213 || rPtr
->margins
.first
+ x
> rPtr
->margins
.right
-MIN_DOC_WIDTH
219 if( x
< rPtr
->margins
.first
+MIN_DOC_WIDTH
220 || x
< rPtr
->margins
.body
+MIN_DOC_WIDTH
221 || x
< rPtr
->margins
.left
+MIN_DOC_WIDTH
222 || x
> rPtr
->end
) //rPtr->view->size.width)
227 if( x
>= rPtr
->margins
.right
-MIN_DOC_WIDTH
228 || x
< rPtr
->margins
.left
)
233 if( x
>= rPtr
->margins
.right
-MIN_DOC_WIDTH
234 || x
< rPtr
->margins
.left
)
239 if( x
>= rPtr
->margins
.right
-MIN_DOC_WIDTH
240 || x
< rPtr
->margins
.left
)
244 default: return False
;
253 whichMarker(Ruler
*rPtr
, int x
, int y
)
255 if(x
<rPtr
->offset
|| y
>22)
258 if( rPtr
->margins
.left
-x
>= -6 && y
<= 9
259 && (rPtr
->margins
.left
-x
<=0) && y
>=4) {
260 rPtr
->motion
= rPtr
->margins
.left
;
264 if(rPtr
->margins
.right
-x
>= -1 && y
<= 11
265 && rPtr
->margins
.right
-x
<=5 && y
>=4) {
266 rPtr
->motion
= rPtr
->margins
.right
;
271 /* both first and body? */
272 if( rPtr
->margins
.first
-x
<= 4 && rPtr
->margins
.first
-x
>= -5
273 && rPtr
->margins
.body
-x
<= 4 && rPtr
->margins
.body
-x
>= -5
275 rPtr
->motion
= rPtr
->margins
.first
;
280 if(rPtr
->margins
.first
-x
<= 4 && y
<=15
281 && rPtr
->margins
.first
-x
>= -5 && y
>=10) {
282 rPtr
->motion
= rPtr
->margins
.first
;
286 if( rPtr
->margins
.body
-x
<= 4 && y
<=21 &&
287 rPtr
->margins
.body
-x
>= -5 && y
>=17) {
288 rPtr
->motion
= rPtr
->margins
.body
;
301 handleEvents(XEvent
*event
, void *data
)
303 Ruler
*rPtr
= (Ruler
*)data
;
304 Display
*dpy
= event
->xany
.display
;
306 switch (event
->type
) {
312 if(rPtr
->flags
.buttonPressed
313 && (event
->xmotion
.state
& Button1Mask
)) {
314 if(verifyMarkerMove(rPtr
, event
->xmotion
.x
)){
315 GC gc
= WMColorGC(WMDarkGrayColor(rPtr
->view
->screen
));
318 (rPtr
->moveAction
)(rPtr
, rPtr
->clientData
);
319 XSetLineAttributes(rPtr
->view
->screen
->display
, gc
, 1,
320 LineSolid
, CapNotLast
, JoinMiter
);
321 XDrawLine(rPtr
->pview
->screen
->display
,
323 gc
, rPtr
->motion
+1, 40,
324 rPtr
->motion
+1, rPtr
->pview
->size
.height
-5);
330 if(event
->xbutton
.button
!= Button1
)
332 rPtr
->flags
.buttonPressed
= True
;
333 rPtr
->flags
.whichMarker
=
334 whichMarker(rPtr
, event
->xmotion
.x
,
339 if(event
->xbutton
.button
!= Button1
)
341 rPtr
->flags
.buttonPressed
= False
;
342 switch(rPtr
->flags
.whichMarker
) {
344 int change
= rPtr
->margins
.left
-rPtr
->motion
;
345 rPtr
->margins
.first
-=change
;
346 rPtr
->margins
.body
-= change
;
347 rPtr
->margins
.left
= rPtr
->motion
;
348 paintRuler(rPtr
); break;
350 case 2: rPtr
->margins
.right
= rPtr
->motion
; break;
351 case 3: rPtr
->margins
.first
= rPtr
->motion
; break;
352 case 4: rPtr
->margins
.body
= rPtr
->motion
; break;
353 case 6: rPtr
->margins
.first
= rPtr
->margins
.body
354 = rPtr
->motion
; break;
356 if(rPtr
->releaseAction
)
357 (rPtr
->releaseAction
)(rPtr
, rPtr
->clientData
);
363 rulerDidResize(W_ViewDelegate
*self
, WMView
*view
)
365 Ruler
*rPtr
= (Ruler
*)view
->self
;
367 createDrawBuffer(rPtr
);
373 W_ViewDelegate _RulerViewDelegate
=
384 WMCreateRuler(WMWidget
*parent
)
386 Ruler
*rPtr
= wmalloc(sizeof(Ruler
));
387 unsigned int w
= WMWidgetWidth(parent
);
389 memset(rPtr
, 0, sizeof(Ruler
));
391 rPtr
->widgetClass
= WC_Ruler
;
393 rPtr
->view
= W_CreateView(W_VIEW(parent
));
398 rPtr
->view
->self
= rPtr
;
400 rPtr
->drawBuffer
= (Pixmap
) NULL
;
402 W_ResizeView(rPtr
->view
, w
, 40);
404 WMCreateEventHandler(rPtr
->view
, ExposureMask
|StructureNotifyMask
405 |EnterWindowMask
|LeaveWindowMask
|FocusChangeMask
406 |ButtonReleaseMask
|ButtonPressMask
|KeyReleaseMask
407 |KeyPressMask
|Button1MotionMask
, handleEvents
, rPtr
);
409 rPtr
->view
->delegate
= &_RulerViewDelegate
;
411 rPtr
->bg
= WMColorGC(WMGrayColor(rPtr
->view
->screen
));
412 rPtr
->fg
= WMColorGC(WMBlackColor(rPtr
->view
->screen
));
413 rPtr
->font
= WMSystemFontOfSize(rPtr
->view
->screen
, 8);
416 rPtr
->margins
.left
= 22;
417 rPtr
->margins
.body
= 22;
418 rPtr
->margins
.first
= 42;
419 rPtr
->margins
.right
= (w
<502?w
:502);
421 rPtr
->flags
.whichMarker
= 0; /* none */
422 rPtr
->flags
.buttonPressed
= False
;
424 rPtr
->moveAction
= NULL
;
425 rPtr
->releaseAction
= NULL
;
427 rPtr
->pview
= W_VIEW(parent
);
434 WMSetRulerMargins(WMRuler
*rPtr
, WMRulerMargins margins
)
438 rPtr
->margins
.left
= margins
.left
+ rPtr
->offset
;
439 rPtr
->margins
.right
= margins
.right
+ rPtr
->offset
;
440 rPtr
->margins
.first
= margins
.first
+ rPtr
->offset
;
441 rPtr
->margins
.body
= margins
.body
+ rPtr
->offset
;
442 rPtr
->margins
.tabs
= margins
.tabs
; //for loop
448 WMGetRulerMargins(WMRuler
*rPtr
)
450 WMRulerMargins margins
;
454 margins
.left
= rPtr
->margins
.left
- rPtr
->offset
;
455 margins
.right
= rPtr
->margins
.right
- rPtr
->offset
;
456 margins
.first
= rPtr
->margins
.first
- rPtr
->offset
;
457 margins
.body
= rPtr
->margins
.body
- rPtr
->offset
;
459 margins
.tabs
= rPtr
->margins
.tabs
;
461 return rPtr
->margins
;
465 WMSetRulerOffset(WMRuler
*rPtr
, int pixels
)
467 if(!rPtr
|| pixels
<0 || pixels
+MIN_DOC_WIDTH
>=rPtr
->view
->size
.width
)
469 rPtr
->offset
= pixels
;
470 //rulerDidResize(rPtr, rPtr->view);
474 WMGetRulerOffset(WMRuler
*rPtr
)
482 WMSetRulerReleaseAction(WMRuler
*rPtr
, WMAction
*action
, void *clientData
)
487 rPtr
->releaseAction
= action
;
488 rPtr
->clientData
= clientData
;
492 WMSetRulerMoveAction(WMRuler
*rPtr
, WMAction
*action
, void *clientData
)
497 rPtr
->moveAction
= action
;
498 rPtr
->clientData
= clientData
;
501 /* _which_ one was released */
503 WMGetReleasedRulerMargin(WMRuler
*rPtr
)
507 return rPtr
->flags
.whichMarker
;
510 /* _which_ one is being grabbed */
512 WMGetGrabbedRulerMargin(WMRuler
*rPtr
)
516 return rPtr
->flags
.whichMarker
;