1 /* Rectangular class for Midnight Commander widgets
3 Copyright (C) 2020-2024
4 The Free Software Foundation, Inc.
7 Andrew Borodin <aborodin@vmail.ru>, 2020-2022
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software: you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation, either version 3 of the License,
14 or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** \file widget-common.c
26 * \brief Source: shared stuff of widgets
33 #include "lib/global.h"
37 /*** global variables ****************************************************************************/
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 /*** file scope functions ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
48 /*** public functions ****************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 * Create new WRect object.
53 * @param y y-coordinate of left-up corner
54 * @param x x-coordinate of left-up corner
58 * @return newly allocated WRect object.
62 rect_new (int y
, int x
, int lines
, int cols
)
66 r
= g_try_new (WRect
, 1);
69 rect_init (r
, y
, x
, lines
, cols
);
74 /* --------------------------------------------------------------------------------------------- */
76 * Initialize WRect object.
78 * @param r WRect object
79 * @param y y-coordinate of left-up corner
80 * @param x x-coordinate of left-up corner
86 rect_init (WRect
*r
, int y
, int x
, int lines
, int cols
)
94 /* --------------------------------------------------------------------------------------------- */
96 * Change position of rectangle area.
98 * @param r WRect object
99 * @param dy y-shift of left-up corner
100 * @param dx x-shift of left-up corner
104 rect_move (WRect
*r
, int dy
, int dx
)
110 /* --------------------------------------------------------------------------------------------- */
112 * Change size of rectangle area keeping it's position.
114 * @param r WRect object
115 * @param dl change size value of height
116 * @param dc change size value of width
120 rect_resize (WRect
*r
, int dl
, int dc
)
126 /* --------------------------------------------------------------------------------------------- */
128 * Change size of rectangle area keeping it's center.
130 * @param r WRect object
131 * @param dl change size value of y-coordinate and height
132 * Positive value means move up and increase height.
133 * Negative value means move down and decrease height.
134 * @param dc change size value of x-coordinate and width
135 * Positive value means move left and increase width.
136 * Negative value means move right and decrease width.
140 rect_grow (WRect
*r
, int dl
, int dc
)
148 /* --------------------------------------------------------------------------------------------- */
150 * Calculates the intersection of two rectangle areas.
151 * The resulting rectangle is the largest rectangle which contains intersection of rectangle areas.
153 * @param r first WRect object
154 * @param r1 second WRect object
156 * The resulting rectangle is stored in r.
160 rect_intersect (WRect
*r
, const WRect
*r1
)
165 /* right-down corners */
168 y1
= r1
->y
+ r1
->lines
;
169 x1
= r1
->x
+ r1
->cols
;
171 /* right-down corner of intersection */
175 /* left-up corner of intersection */
176 r
->y
= MAX (r
->y
, r1
->y
);
177 r
->x
= MAX (r
->x
, r1
->x
);
179 /* intersection sizes */
184 /* --------------------------------------------------------------------------------------------- */
186 * Calculates the union of two rectangle areas.
187 * The resulting rectangle is the largest rectangle which contains both rectangle areas.
189 * @param r first WRect object
190 * @param r1 second WRect object
192 * The resulting rectangle is stored in r.
196 rect_union (WRect
*r
, const WRect
*r1
)
201 /* right-down corners */
204 y1
= r1
->y
+ r1
->lines
;
205 x1
= r1
->x
+ r1
->cols
;
207 /* right-down corner of union */
211 /* left-up corner of union */
212 r
->y
= MIN (r
->y
, r1
->y
);
213 r
->x
= MIN (r
->x
, r1
->x
);
220 /* --------------------------------------------------------------------------------------------- */
222 * Check whether two rectangle areas are overlapped or not.
224 * @param r1 WRect object
225 * @param r2 WRect object
227 * @return TRUE if rectangle areas are overlapped, FALSE otherwise.
231 rects_are_overlapped (const WRect
*r1
, const WRect
*r2
)
233 return !((r2
->x
>= r1
->x
+ r1
->cols
) || (r1
->x
>= r2
->x
+ r2
->cols
)
234 || (r2
->y
>= r1
->y
+ r1
->lines
) || (r1
->y
>= r2
->y
+ r2
->lines
));
237 /* --------------------------------------------------------------------------------------------- */
239 * Check whether two rectangle areas are equal or not.
241 * @param r1 WRect object
242 * @param r2 WRect object
244 * @return TRUE if rectangle areas are equal, FALSE otherwise.
248 rects_are_equal (const WRect
*r1
, const WRect
*r2
)
250 return (r1
->y
== r2
->y
&& r1
->x
== r2
->x
&& r1
->lines
== r2
->lines
&& r1
->cols
== r2
->cols
);
253 /* --------------------------------------------------------------------------------------------- */