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., 51 Franklin St, Fifth Floor, Boston,
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;
57 /* Marker for left margin
66 static void drawLeftMarker(Ruler
* rPtr
)
69 int xpos
= (rPtr
->flags
.whichMarker
== 1 ? rPtr
->motion
: rPtr
->margins
.left
);
71 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
, xpos
, 8, xpos
, 22);
74 points
[1].x
= points
[0].x
+ 6;
76 points
[2].x
= points
[0].x
+ 6;
78 points
[3].x
= points
[0].x
;
80 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
81 points
, 4, Convex
, CoordModeOrigin
);
84 /* Marker for right margin
93 static void drawRightMarker(Ruler
* rPtr
)
96 int xpos
= (rPtr
->flags
.whichMarker
== 2 ? rPtr
->motion
: rPtr
->margins
.right
);
98 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
, xpos
, 8, xpos
, 22);
99 points
[0].x
= xpos
+ 1;
101 points
[1].x
= points
[0].x
- 6;
103 points
[2].x
= points
[0].x
- 6;
105 points
[3].x
= points
[0].x
;
107 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
108 points
, 4, Convex
, CoordModeOrigin
);
111 /* Marker for first line only
117 static void drawFirstMarker(Ruler
* rPtr
)
119 int xpos
= ((rPtr
->flags
.whichMarker
== 3 || rPtr
->flags
.whichMarker
== 6) ?
120 rPtr
->motion
: rPtr
->margins
.first
);
122 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
, xpos
- 5, 10, 11, 5);
123 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
, xpos
, 12, xpos
, 22);
126 /* Marker for rest of body
131 static void drawBodyMarker(Ruler
* rPtr
)
134 int xpos
= ((rPtr
->flags
.whichMarker
== 4 || rPtr
->flags
.whichMarker
== 6) ?
135 rPtr
->motion
: rPtr
->margins
.body
);
137 points
[0].x
= xpos
- 5;
139 points
[1].x
= points
[0].x
+ 11;
141 points
[2].x
= points
[0].x
+ 5;
143 XFillPolygon(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
144 points
, 3, Convex
, CoordModeOrigin
);
147 static void createDrawBuffer(Ruler
* rPtr
)
149 if (!rPtr
->view
->flags
.realized
)
152 if (rPtr
->drawBuffer
)
153 XFreePixmap(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
);
155 rPtr
->drawBuffer
= XCreatePixmap(rPtr
->view
->screen
->display
,
156 rPtr
->view
->window
, rPtr
->view
->size
.width
, 40,
157 rPtr
->view
->screen
->depth
);
158 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
159 rPtr
->bgGC
, 0, 0, rPtr
->view
->size
.width
, 40);
162 static void drawRulerOnPixmap(Ruler
* rPtr
)
166 int marks
[9] = { 11, 3, 5, 3, 7, 3, 5, 3 };
168 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
171 XFillRectangle(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
172 rPtr
->bgGC
, 0, 0, rPtr
->view
->size
.width
, 40);
174 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
, rPtr
->fg
,
175 rPtr
->font
, rPtr
->margins
.left
+ 2, 26, _("0 inches"), 10);
179 w
= rPtr
->view
->size
.width
- rPtr
->margins
.left
;
181 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
182 rPtr
->fgGC
, rPtr
->margins
.left
+ m
, 23, rPtr
->margins
.left
+ m
, marks
[i
% 8] + 23);
183 if (i
!= 0 && i
% 8 == 0) {
184 snprintf(c
, sizeof(c
), "%hu", ++j
);
185 WMDrawString(rPtr
->view
->screen
, rPtr
->drawBuffer
, rPtr
->fg
,
186 rPtr
->font
, rPtr
->margins
.left
+ 2 + m
, 26, c
, 2);
191 rPtr
->end
= rPtr
->margins
.left
+ m
- 10;
192 if (rPtr
->margins
.right
> rPtr
->end
)
193 rPtr
->margins
.right
= rPtr
->end
;
195 XDrawLine(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
, rPtr
->fgGC
,
196 rPtr
->margins
.left
, 22, rPtr
->margins
.left
+ m
- 10, 22);
198 drawLeftMarker(rPtr
);
199 drawRightMarker(rPtr
);
200 drawFirstMarker(rPtr
);
201 drawBodyMarker(rPtr
);
203 rPtr
->flags
.redraw
= False
;
206 static void paintRuler(Ruler
* rPtr
)
208 if (!rPtr
->drawBuffer
|| !rPtr
->view
->flags
.realized
)
211 if (rPtr
->flags
.redraw
)
212 drawRulerOnPixmap(rPtr
);
213 XCopyArea(rPtr
->view
->screen
->display
, rPtr
->drawBuffer
,
214 rPtr
->view
->window
, rPtr
->bgGC
, 0, 0, rPtr
->view
->size
.width
, 40, 0, 0);
217 static Bool
verifyMarkerMove(Ruler
* rPtr
, int x
)
219 if (rPtr
->flags
.whichMarker
< 1 || rPtr
->flags
.whichMarker
> 6)
222 switch (rPtr
->flags
.whichMarker
) {
224 if (x
> rPtr
->margins
.right
- 10 || x
< rPtr
->offset
||
225 rPtr
->margins
.body
+ x
> rPtr
->margins
.right
- MIN_DOC_WIDTH
||
226 rPtr
->margins
.first
+ x
> rPtr
->margins
.right
- MIN_DOC_WIDTH
)
231 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) */
236 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
241 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
246 if (x
>= rPtr
->margins
.right
- MIN_DOC_WIDTH
|| x
< rPtr
->margins
.left
)
258 static int whichMarker(Ruler
* rPtr
, int x
, int y
)
260 if (x
< rPtr
->offset
|| y
> 22)
263 if (rPtr
->margins
.left
- x
>= -6 && y
<= 9 && (rPtr
->margins
.left
- x
<= 0) && y
>= 4) {
264 rPtr
->motion
= rPtr
->margins
.left
;
267 if (rPtr
->margins
.right
- x
>= -1 && y
<= 11 && rPtr
->margins
.right
- x
<= 5 && y
>= 4) {
268 rPtr
->motion
= rPtr
->margins
.right
;
272 /* both first and body? */
273 if (rPtr
->margins
.first
- x
<= 4 && rPtr
->margins
.first
- x
>= -5
274 && rPtr
->margins
.body
- x
<= 4 && rPtr
->margins
.body
- x
>= -5 && y
>= 15 && y
<= 17) {
275 rPtr
->motion
= rPtr
->margins
.first
;
280 if (rPtr
->margins
.first
- x
<= 4 && y
<= 15 && rPtr
->margins
.first
- x
>= -5 && y
>= 10) {
281 rPtr
->motion
= rPtr
->margins
.first
;
284 if (rPtr
->margins
.body
- x
<= 4 && y
<= 21 && rPtr
->margins
.body
- x
>= -5 && y
>= 17) {
285 rPtr
->motion
= rPtr
->margins
.body
;
293 static void rulerDidResize(W_ViewDelegate
* self
, WMView
* view
)
295 Ruler
*rPtr
= (Ruler
*) view
->self
;
297 /* Parameter not used, but tell the compiler that it is ok */
300 createDrawBuffer(rPtr
);
301 rPtr
->flags
.redraw
= True
;
306 static void handleEvents(XEvent
* event
, void *data
)
308 Ruler
*rPtr
= (Ruler
*) data
;
310 switch (event
->type
) {
312 rulerDidResize(rPtr
->view
->delegate
, rPtr
->view
);
316 if (rPtr
->flags
.buttonPressed
&& (event
->xmotion
.state
& Button1Mask
)) {
317 if (verifyMarkerMove(rPtr
, event
->xmotion
.x
)) {
318 GC gc
= WMColorGC(WMDarkGrayColor(rPtr
->view
->screen
));
320 if (rPtr
->moveAction
)
321 (rPtr
->moveAction
) (rPtr
, rPtr
->clientData
);
322 rPtr
->flags
.redraw
= True
;
324 XSetLineAttributes(rPtr
->view
->screen
->display
, gc
, 1,
325 LineSolid
, CapNotLast
, JoinMiter
);
326 XDrawLine(rPtr
->pview
->screen
->display
,
328 gc
, rPtr
->motion
+ 1, 40,
329 rPtr
->motion
+ 1, rPtr
->pview
->size
.height
- 5);
335 if (event
->xbutton
.button
!= Button1
)
337 rPtr
->flags
.buttonPressed
= True
;
338 rPtr
->flags
.whichMarker
= whichMarker(rPtr
, event
->xmotion
.x
, event
->xmotion
.y
);
342 if (event
->xbutton
.button
!= Button1
)
344 rPtr
->flags
.buttonPressed
= False
;
345 switch (rPtr
->flags
.whichMarker
) {
347 int change
= rPtr
->margins
.left
- rPtr
->motion
;
349 rPtr
->margins
.first
-= change
;
350 rPtr
->margins
.body
-= change
;
351 rPtr
->margins
.left
= rPtr
->motion
;
352 rPtr
->flags
.redraw
= True
;
357 rPtr
->margins
.right
= rPtr
->motion
;
360 rPtr
->margins
.first
= rPtr
->motion
;
363 rPtr
->margins
.body
= rPtr
->motion
;
366 rPtr
->margins
.first
= rPtr
->margins
.body
= rPtr
->motion
;
369 if (rPtr
->releaseAction
)
370 (rPtr
->releaseAction
) (rPtr
, rPtr
->clientData
);
375 W_ViewDelegate _RulerViewDelegate
= {
383 WMRuler
*WMCreateRuler(WMWidget
* parent
)
385 Ruler
*rPtr
= wmalloc(sizeof(Ruler
));
386 unsigned int w
= WMWidgetWidth(parent
);
388 rPtr
->widgetClass
= WC_Ruler
;
390 rPtr
->view
= W_CreateView(W_VIEW(parent
));
397 rPtr
->view
->self
= rPtr
;
399 rPtr
->drawBuffer
= (Pixmap
) NULL
;
401 W_ResizeView(rPtr
->view
, w
, 40);
403 WMCreateEventHandler(rPtr
->view
, ExposureMask
| StructureNotifyMask
404 | EnterWindowMask
| LeaveWindowMask
| FocusChangeMask
405 | ButtonReleaseMask
| ButtonPressMask
| KeyReleaseMask
406 | KeyPressMask
| Button1MotionMask
, handleEvents
, rPtr
);
408 rPtr
->view
->delegate
= &_RulerViewDelegate
;
410 rPtr
->fg
= WMBlackColor(rPtr
->view
->screen
);
411 rPtr
->fgGC
= WMColorGC(rPtr
->fg
);
412 rPtr
->bgGC
= WMColorGC(WMGrayColor(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);
420 rPtr
->margins
.tabs
= NULL
;
422 rPtr
->flags
.whichMarker
= 0; /* none */
423 rPtr
->flags
.buttonPressed
= False
;
424 rPtr
->flags
.redraw
= True
;
426 rPtr
->moveAction
= NULL
;
427 rPtr
->releaseAction
= NULL
;
429 rPtr
->pview
= W_VIEW(parent
);
434 void 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 */
443 rPtr
->flags
.redraw
= True
;
448 WMRulerMargins
*WMGetRulerMargins(WMRuler
* rPtr
)
450 WMRulerMargins
*margins
= wmalloc(sizeof(WMRulerMargins
));
453 margins
->first
= margins
->body
= margins
->left
= 0;
454 margins
->right
= 100;
458 margins
->left
= rPtr
->margins
.left
- rPtr
->offset
;
459 margins
->right
= rPtr
->margins
.right
- rPtr
->offset
;
460 margins
->first
= rPtr
->margins
.first
- rPtr
->offset
;
461 margins
->body
= rPtr
->margins
.body
- rPtr
->offset
;
463 margins
->tabs
= rPtr
->margins
.tabs
;
468 Bool
WMIsMarginEqualToMargin(WMRulerMargins
* aMargin
, WMRulerMargins
* anotherMargin
)
470 if (aMargin
== anotherMargin
)
472 else if (!aMargin
|| !anotherMargin
)
474 if (aMargin
->left
!= anotherMargin
->left
)
476 if (aMargin
->first
!= anotherMargin
->first
)
478 if (aMargin
->body
!= anotherMargin
->body
)
480 if (aMargin
->right
!= anotherMargin
->right
)
486 void WMSetRulerOffset(WMRuler
* rPtr
, int pixels
)
488 if (!rPtr
|| pixels
< 0 || pixels
+ MIN_DOC_WIDTH
>= rPtr
->view
->size
.width
)
490 rPtr
->offset
= pixels
;
491 /*rulerDidResize(rPtr, rPtr->view); */
494 int WMGetRulerOffset(WMRuler
* rPtr
)
497 return 0; /* what value should return if no ruler? -1 or 0? */
501 void WMSetRulerReleaseAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
506 rPtr
->releaseAction
= action
;
507 rPtr
->clientData
= clientData
;
510 void WMSetRulerMoveAction(WMRuler
* rPtr
, WMAction
* action
, void *clientData
)
515 rPtr
->moveAction
= action
;
516 rPtr
->clientData
= clientData
;
519 /* _which_ one was released */
520 int WMGetReleasedRulerMargin(WMRuler
* rPtr
)
524 return rPtr
->flags
.whichMarker
;
527 /* _which_ one is being grabbed */
528 int WMGetGrabbedRulerMargin(WMRuler
* rPtr
)
532 return rPtr
->flags
.whichMarker
;