1 /* ------------- rect.c --------------- */
3 #include <dflat32/dflat.h>
5 /* --- Produce the vector end points produced by the overlap
6 of two other vectors --- */
7 static void subVector(int *v1, int *v2,
8 int t1, int t2, int o1, int o2)
9 {
10 *v1 = *v2 = -1;
11 if (within(o1, t1, t2)) {
12 *v1 = o1;
13 if (within(o2, t1, t2))
14 *v2 = o2;
15 else
16 *v2 = t2;
17 }
18 else if (within(o2, t1, t2)) {
19 *v2 = o2;
20 if (within(o1, t1, t2))
21 *v1 = o1;
22 else
23 *v1 = t1;
24 }
25 else if (within(t1, o1, o2)) {
26 *v1 = t1;
27 if (within(t2, o1, o2))
28 *v2 = t2;
29 else
30 *v2 = o2;
31 }
32 else if (within(t2, o1, o2)) {
33 *v2 = t2;
34 if (within(t1, o1, o2))
35 *v1 = t1;
36 else
37 *v1 = o1;
38 }
39 }
41 /* --- Return the rectangle produced by the overlap
42 of two other rectangles ---- */
43 DFRECT subRectangle(DFRECT r1, DFRECT r2)
44 {
45 DFRECT r = {0,0,0,0};
46 subVector((int *) &RectLeft(r), (int *) &RectRight(r),
47 RectLeft(r1), RectRight(r1),
48 RectLeft(r2), RectRight(r2));
49 subVector((int *) &RectTop(r), (int *) &RectBottom(r),
50 RectTop(r1), RectBottom(r1),
51 RectTop(r2), RectBottom(r2));
52 if (RectRight(r) == -1 || RectTop(r) == -1)
53 RectRight(r) =
54 RectLeft(r) =
55 RectTop(r) =
56 RectBottom(r) = 0;
57 return r;
58 }
60 /* ------- return the client rectangle of a window ------ */
61 DFRECT ClientRect(void *wnd)
62 {
63 DFRECT rc;
65 if (wnd == NULL)
66 {
67 RectLeft(rc) = 1; // GetClientLeft((DFWINDOW)wnd);
68 RectTop(rc) = 2; // GetClientTop((DFWINDOW)wnd);
69 RectRight(rc) = DfGetScreenWidth () - 2; // GetClientRight((DFWINDOW)wnd);
70 RectBottom(rc) = DfGetScreenHeight () - 2; // GetClientBottom((DFWINDOW)wnd);
71 return rc;
72 }
74 RectLeft(rc) = GetClientLeft((DFWINDOW)wnd);
75 RectTop(rc) = GetClientTop((DFWINDOW)wnd);
76 RectRight(rc) = GetClientRight((DFWINDOW)wnd);
77 RectBottom(rc) = GetClientBottom((DFWINDOW)wnd);
79 return rc;
80 }
82 /* ----- return the rectangle relative to
83 its window's screen position -------- */
84 DFRECT RelativeWindowRect(void *wnd, DFRECT rc)
85 {
86 RectLeft(rc) -= GetLeft((DFWINDOW)wnd);
87 RectRight(rc) -= GetLeft((DFWINDOW)wnd);
88 RectTop(rc) -= GetTop((DFWINDOW)wnd);
89 RectBottom(rc) -= GetTop((DFWINDOW)wnd);
90 return rc;
91 }
93 /* ----- clip a rectangle to the parents of the window ----- */
94 DFRECT ClipRectangle(void *wnd, DFRECT rc)
95 {
96 DFRECT sr;
97 RectLeft(sr) = RectTop(sr) = 0;
98 RectRight(sr) = DfGetScreenWidth()-1;
99 RectBottom(sr) = DfGetScreenHeight()-1;
100 if (!TestAttribute((DFWINDOW)wnd, NOCLIP))
101 while ((wnd = GetParent((DFWINDOW)wnd)) != NULL)
102 rc = subRectangle(rc, ClientRect(wnd));
103 return subRectangle(rc, sr);
104 }
106 /* EOF */