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 */
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;
58 /* Marker for left margin
68 drawLeftMarker(Ruler
* rPtr
)
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);
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
,
85 rPtr
->fg
, points
, 4, Convex
, CoordModeOrigin
);
89 /* Marker for right margin
98 static void drawRightMarker(Ruler
* rPtr
)
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;
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
,
115 rPtr
->fg
, points
, 4, Convex
, CoordModeOrigin
);
119 /* Marker for first line only
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
141 static void drawBodyMarker(Ruler
* rPtr
)
144 int xpos
= ((rPtr
->flags
.whichMarker
== 4 || rPtr
->flags
.whichMarker
== 6) ?
145 rPtr
->motion
: rPtr
->margins
.body
);
147 points
[0].x
= xpos
- 5;
149 points
[1].x
= points
[0].x
+ 11;
151 points
[2].x
= points
[0].x
+ 5;
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
)
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
)
179 {11, 3, 5, 3, 7, 3, 5, 3};
181 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
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);
193 w
= rPtr
->view
->size
.width
- rPtr
->margins
.left
;
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) {
200 snprintf(c
, 3, "%d", ++j
);
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);
209 rPtr
->end
= rPtr
->margins
.left
+ m
- 10;
210 if (rPtr
->margins
.right
> rPtr
->end
)
211 rPtr
->margins
.right
= rPtr
->end
;
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
)
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,
239 verifyMarkerMove(Ruler
* rPtr
, int x
)
241 if (rPtr
->flags
.whichMarker
< 1 || rPtr
->flags
.whichMarker
> 6)
244 switch (rPtr
->flags
.whichMarker
) {
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
)
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) */
261 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
266 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
271 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
284 static int whichMarker(Ruler
* rPtr
, int x
, int y
)
286 if (x
< rPtr
->offset
|| y
> 22)
289 if (rPtr
->margins
.left
- x
>= -6 && y
<= 9
290 && (rPtr
->margins
.left
- x
<= 0) && y
>= 4) {
291 rPtr
->motion
= rPtr
->margins
.left
;
294 if (rPtr
->margins
.right
- x
>= -1 && y
<= 11
295 && rPtr
->margins
.right
- x
<= 5 && y
>= 4) {
296 rPtr
->motion
= rPtr
->margins
.right
;
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
;
309 if (rPtr
->margins
.first
- x
<= 4 && y
<= 15
310 && rPtr
->margins
.first
- x
>= -5 && y
>= 10) {
311 rPtr
->motion
= rPtr
->margins
.first
;
314 if (rPtr
->margins
.body
- x
<= 4 && y
<= 21 &&
315 rPtr
->margins
.body
- x
>= -5 && y
>= 17) {
316 rPtr
->motion
= rPtr
->margins
.body
;
325 static void rulerDidResize(W_ViewDelegate
* self
, WMView
* view
)
327 Ruler
*rPtr
= (Ruler
*) view
->self
;
329 createDrawBuffer(rPtr
);
330 rPtr
->flags
.redraw
= True
;
336 static void handleEvents(XEvent
* event
, void *data
)
338 Ruler
*rPtr
= (Ruler
*) data
;
340 switch (event
->type
) {
342 rulerDidResize(rPtr
->view
->delegate
, rPtr
->view
);
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
;
355 XSetLineAttributes(rPtr
->view
->screen
->display
, gc
, 1,
356 LineSolid
, CapNotLast
, JoinMiter
);
357 XDrawLine(rPtr
->pview
->screen
->display
,
359 gc
, rPtr
->motion
+ 1, 40,
360 rPtr
->motion
+ 1, rPtr
->pview
->size
.height
- 5);
366 if (event
->xbutton
.button
!= Button1
)
368 rPtr
->flags
.buttonPressed
= True
;
369 rPtr
->flags
.whichMarker
=
370 whichMarker(rPtr
, event
->xmotion
.x
,
375 if (event
->xbutton
.button
!= Button1
)
377 rPtr
->flags
.buttonPressed
= False
;
378 switch (rPtr
->flags
.whichMarker
) {
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
;
390 rPtr
->margins
.right
= rPtr
->motion
;
393 rPtr
->margins
.first
= rPtr
->motion
;
396 rPtr
->margins
.body
= rPtr
->motion
;
399 rPtr
->margins
.first
= rPtr
->margins
.body
403 if (rPtr
->releaseAction
)
404 (rPtr
->releaseAction
) (rPtr
, rPtr
->clientData
);
410 W_ViewDelegate _RulerViewDelegate
=
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
));
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);
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
);
474 void WMSetRulerMargins(WMRuler
* rPtr
, WMRulerMargins margins
)
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
;
490 WMGetRulerMargins(WMRuler
* rPtr
)
492 WMRulerMargins
*margins
= wmalloc(sizeof(WMRulerMargins
));
495 margins
->first
= margins
->body
= margins
->left
= 0;
496 margins
->right
= 100;
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
;
505 margins
->tabs
= rPtr
->margins
.tabs
;
512 WMIsMarginEqualToMargin(WMRulerMargins
*aMargin
, WMRulerMargins
*anotherMargin
)
514 if(aMargin
== anotherMargin
)
516 else if(!aMargin
|| !anotherMargin
)
518 if(aMargin
->left
!= anotherMargin
->left
)
520 if(aMargin
->first
!= anotherMargin
->first
)
522 if(aMargin
->body
!= anotherMargin
->body
)
524 if(aMargin
->right
!= anotherMargin
->right
)
533 void WMSetRulerOffset(WMRuler
* rPtr
, int pixels
)
535 if (!rPtr
|| pixels
< 0 || pixels
+ MIN_DOC_WIDTH
>= rPtr
->view
->size
.width
)
537 rPtr
->offset
= pixels
;
538 /*rulerDidResize(rPtr, rPtr->view); */
542 int WMGetRulerOffset(WMRuler
* rPtr
)
545 return 0; /* what value should return if no ruler? -1 or 0? */
550 void WMSetRulerReleaseAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
555 rPtr
->releaseAction
= action
;
556 rPtr
->clientData
= clientData
;
560 void WMSetRulerMoveAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
565 rPtr
->moveAction
= action
;
566 rPtr
->clientData
= clientData
;
570 /* _which_ one was released */
571 int WMGetReleasedRulerMargin(WMRuler
* rPtr
)
575 return rPtr
->flags
.whichMarker
;
579 /* _which_ one is being grabbed */
580 int WMGetGrabbedRulerMargin(WMRuler
* rPtr
)
584 return rPtr
->flags
.whichMarker
;