2016-11-13 08:06:20 +03:00
|
|
|
/* Rectangular class for Midnight Commander widgets
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2020-2024
|
2016-11-13 08:06:20 +03:00
|
|
|
The Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
2022-04-30 18:31:45 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2020-2022
|
2016-11-13 08:06:20 +03:00
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file widget-common.c
|
|
|
|
* \brief Source: shared stuff of widgets
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "rect.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Create new WRect object.
|
|
|
|
*
|
|
|
|
* @param y y-coordinate of left-up corner
|
|
|
|
* @param x x-coordinate of left-up corner
|
Fix various typos in the source code (closes MidnightCommander/mc#177).
Found via `codespell -S
po,doc,./misc/syntax,./src/vfs/extfs/helpers/README.it -L
parm,rouge,sav,ect,vie,te,dum,clen,wee,dynamc,childs,ths,fo,nin,unx,nd,iif,iterm,ser,makrs,wil`
Co-authored-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Kian-Meng Ang <kianmeng@cpan.org>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
2023-01-10 06:02:52 +03:00
|
|
|
* @param lines height
|
2016-11-13 08:06:20 +03:00
|
|
|
* @param cols width
|
|
|
|
*
|
|
|
|
* @return newly allocated WRect object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
WRect *
|
|
|
|
rect_new (int y, int x, int lines, int cols)
|
|
|
|
{
|
|
|
|
WRect *r;
|
|
|
|
|
|
|
|
r = g_try_new (WRect, 1);
|
|
|
|
|
|
|
|
if (r != NULL)
|
|
|
|
rect_init (r, y, x, lines, cols);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Initialize WRect object.
|
|
|
|
*
|
|
|
|
* @param r WRect object
|
|
|
|
* @param y y-coordinate of left-up corner
|
|
|
|
* @param x x-coordinate of left-up corner
|
Fix various typos in the source code (closes MidnightCommander/mc#177).
Found via `codespell -S
po,doc,./misc/syntax,./src/vfs/extfs/helpers/README.it -L
parm,rouge,sav,ect,vie,te,dum,clen,wee,dynamc,childs,ths,fo,nin,unx,nd,iif,iterm,ser,makrs,wil`
Co-authored-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Kian-Meng Ang <kianmeng@cpan.org>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
2023-01-10 06:02:52 +03:00
|
|
|
* @param lines height
|
2016-11-13 08:06:20 +03:00
|
|
|
* @param cols width
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_init (WRect *r, int y, int x, int lines, int cols)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
r->y = y;
|
|
|
|
r->x = x;
|
|
|
|
r->lines = lines;
|
|
|
|
r->cols = cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Change position of rectangle area.
|
|
|
|
*
|
|
|
|
* @param r WRect object
|
|
|
|
* @param dy y-shift of left-up corner
|
|
|
|
* @param dx x-shift of left-up corner
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_move (WRect *r, int dy, int dx)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
r->y += dy;
|
|
|
|
r->x += dx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
2022-04-30 18:31:45 +03:00
|
|
|
* Change size of rectangle area keeping it's position.
|
2016-11-13 08:06:20 +03:00
|
|
|
*
|
|
|
|
* @param r WRect object
|
Fix various typos in the source code (closes MidnightCommander/mc#177).
Found via `codespell -S
po,doc,./misc/syntax,./src/vfs/extfs/helpers/README.it -L
parm,rouge,sav,ect,vie,te,dum,clen,wee,dynamc,childs,ths,fo,nin,unx,nd,iif,iterm,ser,makrs,wil`
Co-authored-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Kian-Meng Ang <kianmeng@cpan.org>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
2023-01-10 06:02:52 +03:00
|
|
|
* @param dl change size value of height
|
2016-11-13 08:06:20 +03:00
|
|
|
* @param dc change size value of width
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_resize (WRect *r, int dl, int dc)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
r->lines += dl;
|
|
|
|
r->cols += dc;
|
|
|
|
}
|
|
|
|
|
2022-04-30 18:31:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Change size of rectangle area keeping it's center.
|
|
|
|
*
|
|
|
|
* @param r WRect object
|
Fix various typos in the source code (closes MidnightCommander/mc#177).
Found via `codespell -S
po,doc,./misc/syntax,./src/vfs/extfs/helpers/README.it -L
parm,rouge,sav,ect,vie,te,dum,clen,wee,dynamc,childs,ths,fo,nin,unx,nd,iif,iterm,ser,makrs,wil`
Co-authored-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Kian-Meng Ang <kianmeng@cpan.org>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
2023-01-10 06:02:52 +03:00
|
|
|
* @param dl change size value of y-coordinate and height
|
|
|
|
* Positive value means move up and increase height.
|
|
|
|
* Negative value means move down and decrease height.
|
2022-04-30 18:31:45 +03:00
|
|
|
* @param dc change size value of x-coordinate and width
|
|
|
|
* Positive value means move left and increase width.
|
|
|
|
* Negative value means move right and decrease width.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_grow (WRect *r, int dl, int dc)
|
2022-04-30 18:31:45 +03:00
|
|
|
{
|
|
|
|
r->y -= dl;
|
|
|
|
r->x -= dc;
|
|
|
|
r->lines += dl * 2;
|
|
|
|
r->cols += dc * 2;
|
|
|
|
}
|
|
|
|
|
2016-11-13 08:06:20 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Calculates the intersection of two rectangle areas.
|
|
|
|
* The resulting rectangle is the largest rectangle which contains intersection of rectangle areas.
|
|
|
|
*
|
|
|
|
* @param r first WRect object
|
|
|
|
* @param r1 second WRect object
|
|
|
|
*
|
|
|
|
* The resulting rectangle is stored in r.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_intersect (WRect *r, const WRect *r1)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
int y, x;
|
|
|
|
int y1, x1;
|
|
|
|
|
|
|
|
/* right-down corners */
|
|
|
|
y = r->y + r->lines;
|
|
|
|
x = r->x + r->cols;
|
|
|
|
y1 = r1->y + r1->lines;
|
|
|
|
x1 = r1->x + r1->cols;
|
|
|
|
|
|
|
|
/* right-down corner of intersection */
|
|
|
|
y = MIN (y, y1);
|
|
|
|
x = MIN (x, x1);
|
|
|
|
|
|
|
|
/* left-up corner of intersection */
|
|
|
|
r->y = MAX (r->y, r1->y);
|
|
|
|
r->x = MAX (r->x, r1->x);
|
|
|
|
|
|
|
|
/* intersection sizes */
|
|
|
|
r->lines = y - r->y;
|
|
|
|
r->cols = x - r->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Calculates the union of two rectangle areas.
|
|
|
|
* The resulting rectangle is the largest rectangle which contains both rectangle areas.
|
|
|
|
*
|
|
|
|
* @param r first WRect object
|
|
|
|
* @param r1 second WRect object
|
|
|
|
*
|
|
|
|
* The resulting rectangle is stored in r.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
rect_union (WRect *r, const WRect *r1)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int x1, y1;
|
|
|
|
|
|
|
|
/* right-down corners */
|
|
|
|
y = r->y + r->lines;
|
|
|
|
x = r->x + r->cols;
|
|
|
|
y1 = r1->y + r1->lines;
|
|
|
|
x1 = r1->x + r1->cols;
|
|
|
|
|
|
|
|
/* right-down corner of union */
|
|
|
|
y = MAX (y, y1);
|
|
|
|
x = MAX (x, x1);
|
|
|
|
|
|
|
|
/* left-up corner of union */
|
|
|
|
r->y = MIN (r->y, r1->y);
|
|
|
|
r->x = MIN (r->x, r1->x);
|
|
|
|
|
|
|
|
/* union sizes */
|
|
|
|
r->lines = y - r->y;
|
|
|
|
r->cols = x - r->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Check whether two rectangle areas are overlapped or not.
|
|
|
|
*
|
|
|
|
* @param r1 WRect object
|
|
|
|
* @param r2 WRect object
|
|
|
|
*
|
|
|
|
* @return TRUE if rectangle areas are overlapped, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
rects_are_overlapped (const WRect *r1, const WRect *r2)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
return !((r2->x >= r1->x + r1->cols) || (r1->x >= r2->x + r2->cols)
|
|
|
|
|| (r2->y >= r1->y + r1->lines) || (r1->y >= r2->y + r2->lines));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Check whether two rectangle areas are equal or not.
|
|
|
|
*
|
|
|
|
* @param r1 WRect object
|
|
|
|
* @param r2 WRect object
|
|
|
|
*
|
|
|
|
* @return TRUE if rectangle areas are equal, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
rects_are_equal (const WRect *r1, const WRect *r2)
|
2016-11-13 08:06:20 +03:00
|
|
|
{
|
|
|
|
return (r1->y == r2->y && r1->x == r2->x && r1->lines == r2->lines && r1->cols == r2->cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|