root/lib/widget/rect.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. rect_new
  2. rect_init
  3. rect_move
  4. rect_resize
  5. rect_intersect
  6. rect_union
  7. rects_are_overlapped
  8. rects_are_equal

   1 /* Rectangular class for Midnight Commander widgets
   2 
   3    Copyright (C) 2020
   4    The Free Software Foundation, Inc.
   5 
   6    Written by:
   7    Andrew Borodin <aborodin@vmail.ru>, 2020
   8 
   9    This file is part of the Midnight Commander.
  10 
  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.
  15 
  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.
  20 
  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/>.
  23  */
  24 
  25 /** \file widget-common.c
  26  *  \brief Source: shared stuff of widgets
  27  */
  28 
  29 #include <config.h>
  30 
  31 #include <stdlib.h>
  32 
  33 #include "lib/global.h"
  34 
  35 #include "rect.h"
  36 
  37 /*** global variables ****************************************************************************/
  38 
  39 /*** file scope macro definitions ****************************************************************/
  40 
  41 /*** file scope type declarations ****************************************************************/
  42 
  43 /*** file scope variables ************************************************************************/
  44 
  45 /*** file scope functions ************************************************************************/
  46 
  47 /* --------------------------------------------------------------------------------------------- */
  48 /*** public functions ****************************************************************************/
  49 /* --------------------------------------------------------------------------------------------- */
  50 /**
  51   * Create new WRect object.
  52   *
  53   * @param y y-coordinate of left-up corner
  54   * @param x x-coordinate of left-up corner
  55   * @param lines heigth
  56   * @param cols width
  57   *
  58   * @return newly allocated WRect object.
  59   */
  60 
  61 WRect *
  62 rect_new (int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     WRect *r;
  65 
  66     r = g_try_new (WRect, 1);
  67 
  68     if (r != NULL)
  69         rect_init (r, y, x, lines, cols);
  70 
  71     return r;
  72 }
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 /**
  76   * Initialize WRect object.
  77   *
  78   * @param r WRect object
  79   * @param y y-coordinate of left-up corner
  80   * @param x x-coordinate of left-up corner
  81   * @param lines heigth
  82   * @param cols width
  83   */
  84 
  85 void
  86 rect_init (WRect * r, int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
  87 {
  88     r->y = y;
  89     r->x = x;
  90     r->lines = lines;
  91     r->cols = cols;
  92 }
  93 
  94 /* --------------------------------------------------------------------------------------------- */
  95 /**
  96   * Change position of rectangle area.
  97   *
  98   * @param r WRect object
  99   * @param dy y-shift of left-up corner
 100   * @param dx x-shift of left-up corner
 101   */
 102 
 103 void
 104 rect_move (WRect * r, int dy, int dx)
     /* [previous][next][first][last][top][bottom][index][help]  */
 105 {
 106     r->y += dy;
 107     r->x += dx;
 108 }
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 /**
 112   * Change size of rectangle area.
 113   *
 114   * @param r WRect object
 115   * @param dl change size value of heigth
 116   * @param dc change size value of width
 117   */
 118 
 119 void
 120 rect_resize (WRect * r, int dl, int dc)
     /* [previous][next][first][last][top][bottom][index][help]  */
 121 {
 122     r->lines += dl;
 123     r->cols += dc;
 124 }
 125 
 126 /* --------------------------------------------------------------------------------------------- */
 127 /**
 128   * Calculates the intersection of two rectangle areas.
 129   * The resulting rectangle is the largest rectangle which contains intersection of rectangle areas.
 130   *
 131   * @param r first WRect object
 132   * @param r1 second WRect object
 133   *
 134   * The resulting rectangle is stored in r.
 135   */
 136 
 137 void
 138 rect_intersect (WRect * r, const WRect * r1)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     int y, x;
 141     int y1, x1;
 142 
 143     /* right-down corners */
 144     y = r->y + r->lines;
 145     x = r->x + r->cols;
 146     y1 = r1->y + r1->lines;
 147     x1 = r1->x + r1->cols;
 148 
 149     /* right-down corner of intersection */
 150     y = MIN (y, y1);
 151     x = MIN (x, x1);
 152 
 153     /* left-up corner of intersection */
 154     r->y = MAX (r->y, r1->y);
 155     r->x = MAX (r->x, r1->x);
 156 
 157     /* intersection sizes */
 158     r->lines = y - r->y;
 159     r->cols = x - r->x;
 160 }
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 /**
 164   * Calculates the union of two rectangle areas.
 165   * The resulting rectangle is the largest rectangle which contains both rectangle areas.
 166   *
 167   * @param r first WRect object
 168   * @param r1 second WRect object
 169   *
 170   * The resulting rectangle is stored in r.
 171   */
 172 
 173 void
 174 rect_union (WRect * r, const WRect * r1)
     /* [previous][next][first][last][top][bottom][index][help]  */
 175 {
 176     int x, y;
 177     int x1, y1;
 178 
 179     /* right-down corners */
 180     y = r->y + r->lines;
 181     x = r->x + r->cols;
 182     y1 = r1->y + r1->lines;
 183     x1 = r1->x + r1->cols;
 184 
 185     /* right-down corner of union */
 186     y = MAX (y, y1);
 187     x = MAX (x, x1);
 188 
 189     /* left-up corner of union */
 190     r->y = MIN (r->y, r1->y);
 191     r->x = MIN (r->x, r1->x);
 192 
 193     /* union sizes */
 194     r->lines = y - r->y;
 195     r->cols = x - r->x;
 196 }
 197 
 198 /* --------------------------------------------------------------------------------------------- */
 199 /**
 200   * Check whether two rectangle areas are overlapped or not.
 201   *
 202   * @param r1 WRect object
 203   * @param r2 WRect object
 204   *
 205   * @return TRUE if rectangle areas are overlapped, FALSE otherwise.
 206   */
 207 
 208 gboolean
 209 rects_are_overlapped (const WRect * r1, const WRect * r2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 210 {
 211     return !((r2->x >= r1->x + r1->cols) || (r1->x >= r2->x + r2->cols)
 212              || (r2->y >= r1->y + r1->lines) || (r1->y >= r2->y + r2->lines));
 213 }
 214 
 215 /* --------------------------------------------------------------------------------------------- */
 216 /**
 217   * Check whether two rectangle areas are equal or not.
 218   *
 219   * @param r1 WRect object
 220   * @param r2 WRect object
 221   *
 222   * @return TRUE if rectangle areas are equal, FALSE otherwise.
 223   */
 224 
 225 gboolean
 226 rects_are_equal (const WRect * r1, const WRect * r2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     return (r1->y == r2->y && r1->x == r2->x && r1->lines == r2->lines && r1->cols == r2->cols);
 229 }
 230 
 231 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */