gtkroundedbox.c 25.5 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Benjamin Otte's avatar
Benjamin Otte committed
16 17 18 19 20
 */

#include "config.h"

#include "gtkroundedboxprivate.h"
21 22 23 24

#include "gtkcsscornervalueprivate.h"
#include "gtkcsstypesprivate.h"
#include "gtkstylecontextprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <string.h>

/**
 * _gtk_rounded_box_init_rect:
 * @box: box to initialize
 * @x: x coordinate of box
 * @y: y coordinate of box
 * @width: width of box
 * @height: height of box
 *
 * Initializes the given @box to represent the given rectangle.
 * The
 **/
void
_gtk_rounded_box_init_rect (GtkRoundedBox *box,
                            double         x,
                            double         y,
                            double         width,
                            double         height)
{
46 47
  memset (box, 0, sizeof (GtkRoundedBox));

Benjamin Otte's avatar
Benjamin Otte committed
48 49 50 51 52 53
  box->box.x = x;
  box->box.y = y;
  box->box.width = width;
  box->box.height = height;
}

54 55 56 57 58 59 60
/* clamp border radius, following CSS specs */
static void
gtk_rounded_box_clamp_border_radius (GtkRoundedBox *box)
{
  gdouble factor = 1.0;

  /* note: division by zero leads to +INF, which is > factor, so will be ignored */
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  factor = MIN (factor, box->box.width / (box->corner[GTK_CSS_TOP_LEFT].horizontal +
                                          box->corner[GTK_CSS_TOP_RIGHT].horizontal));
  factor = MIN (factor, box->box.height / (box->corner[GTK_CSS_TOP_RIGHT].vertical +
                                           box->corner[GTK_CSS_BOTTOM_RIGHT].vertical));
  factor = MIN (factor, box->box.width / (box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal +
                                          box->corner[GTK_CSS_BOTTOM_LEFT].horizontal));
  factor = MIN (factor, box->box.height / (box->corner[GTK_CSS_TOP_LEFT].vertical +
                                           box->corner[GTK_CSS_BOTTOM_LEFT].vertical));

  box->corner[GTK_CSS_TOP_LEFT].horizontal *= factor;
  box->corner[GTK_CSS_TOP_LEFT].vertical *= factor;
  box->corner[GTK_CSS_TOP_RIGHT].horizontal *= factor;
  box->corner[GTK_CSS_TOP_RIGHT].vertical *= factor;
  box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal *= factor;
  box->corner[GTK_CSS_BOTTOM_RIGHT].vertical *= factor;
  box->corner[GTK_CSS_BOTTOM_LEFT].horizontal *= factor;
  box->corner[GTK_CSS_BOTTOM_LEFT].vertical *= factor;
78 79
}

80
static void
81
_gtk_rounded_box_apply_border_radius (GtkRoundedBox *box,
82
                                      GtkCssValue **corner,
83
                                      GtkJunctionSides junction)
Benjamin Otte's avatar
Benjamin Otte committed
84
{
85
  if (corner[GTK_CSS_TOP_LEFT] && (junction & GTK_JUNCTION_CORNER_TOPLEFT) == 0)
86
    {
87 88 89 90
      box->corner[GTK_CSS_TOP_LEFT].horizontal = _gtk_css_corner_value_get_x (corner[GTK_CSS_TOP_LEFT],
                                                                              box->box.width);
      box->corner[GTK_CSS_TOP_LEFT].vertical = _gtk_css_corner_value_get_y (corner[GTK_CSS_TOP_LEFT],
                                                                            box->box.height);
91
    }
92
  if (corner[GTK_CSS_TOP_RIGHT] && (junction & GTK_JUNCTION_CORNER_TOPRIGHT) == 0)
93
    {
94 95 96 97
      box->corner[GTK_CSS_TOP_RIGHT].horizontal = _gtk_css_corner_value_get_x (corner[GTK_CSS_TOP_RIGHT],
                                                                               box->box.width);
      box->corner[GTK_CSS_TOP_RIGHT].vertical = _gtk_css_corner_value_get_y (corner[GTK_CSS_TOP_RIGHT],
                                                                             box->box.height);
98
    }
99
  if (corner[GTK_CSS_BOTTOM_RIGHT] && (junction & GTK_JUNCTION_CORNER_BOTTOMRIGHT) == 0)
100
    {
101 102 103 104
      box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal = _gtk_css_corner_value_get_x (corner[GTK_CSS_BOTTOM_RIGHT],
                                                                                  box->box.width);
      box->corner[GTK_CSS_BOTTOM_RIGHT].vertical = _gtk_css_corner_value_get_y (corner[GTK_CSS_BOTTOM_RIGHT],
                                                                                box->box.height);
105
    }
106
  if (corner[GTK_CSS_BOTTOM_LEFT] && (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT) == 0)
107
    {
108 109 110 111
      box->corner[GTK_CSS_BOTTOM_LEFT].horizontal = _gtk_css_corner_value_get_x (corner[GTK_CSS_BOTTOM_LEFT],
                                                                                 box->box.width);
      box->corner[GTK_CSS_BOTTOM_LEFT].vertical = _gtk_css_corner_value_get_y (corner[GTK_CSS_BOTTOM_LEFT],
                                                                               box->box.height);
112
    }
Benjamin Otte's avatar
Benjamin Otte committed
113

114
  gtk_rounded_box_clamp_border_radius (box);
115 116 117
}

void
118 119 120
_gtk_rounded_box_apply_border_radius_for_style (GtkRoundedBox    *box,
                                                GtkCssStyle      *style,
                                                GtkJunctionSides  junction)
121
{
122
  GtkCssValue *corner[4];
123

124 125 126 127
  corner[GTK_CSS_TOP_LEFT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_LEFT_RADIUS);
  corner[GTK_CSS_TOP_RIGHT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_RIGHT_RADIUS);
  corner[GTK_CSS_BOTTOM_LEFT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_LEFT_RADIUS);
  corner[GTK_CSS_BOTTOM_RIGHT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_RIGHT_RADIUS);
128

129
  _gtk_rounded_box_apply_border_radius (box, corner, junction);
Benjamin Otte's avatar
Benjamin Otte committed
130 131
}

132
void
133 134 135
_gtk_rounded_box_apply_outline_radius_for_style (GtkRoundedBox    *box,
                                                 GtkCssStyle      *style,
                                                 GtkJunctionSides  junction)
136 137 138
{
  GtkCssValue *corner[4];

139 140 141 142
  corner[GTK_CSS_TOP_LEFT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_TOP_LEFT_RADIUS);
  corner[GTK_CSS_TOP_RIGHT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_TOP_RIGHT_RADIUS);
  corner[GTK_CSS_BOTTOM_LEFT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_BOTTOM_LEFT_RADIUS);
  corner[GTK_CSS_BOTTOM_RIGHT] = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_BOTTOM_RIGHT_RADIUS);
143 144 145 146

  _gtk_rounded_box_apply_border_radius (box, corner, junction);
}

147
static void
148 149 150
gtk_css_border_radius_grow (GtkRoundedBoxCorner *corner,
                            double               horizontal,
                            double               vertical)
151
{
152 153 154 155
  if (corner->horizontal)
    corner->horizontal += horizontal;
  if (corner->vertical)
    corner->vertical += vertical;
156 157 158 159 160 161 162

  if (corner->horizontal <= 0 || corner->vertical <= 0)
    {
      corner->horizontal = 0;
      corner->vertical = 0;
    }
}
163

Benjamin Otte's avatar
Benjamin Otte committed
164 165 166 167 168 169 170
void
_gtk_rounded_box_grow (GtkRoundedBox *box,
                       double         top,
                       double         right,
                       double         bottom,
                       double         left)
{
171 172 173 174 175 176 177 178 179 180
  if (box->box.width + left + right < 0)
    {
      box->box.x -= left * box->box.width / (left + right);
      box->box.width = 0;
    }
  else
    {
      box->box.x -= left;
      box->box.width += left + right;
    }
Benjamin Otte's avatar
Benjamin Otte committed
181

182
  if (box->box.height + bottom + top < 0)
183 184 185 186 187 188 189 190 191
    {
      box->box.y -= top * box->box.height / (top + bottom);
      box->box.height = 0;
    }
  else
    {
      box->box.y -= top;
      box->box.height += top + bottom;
    }
Benjamin Otte's avatar
Benjamin Otte committed
192

193 194 195 196
  gtk_css_border_radius_grow (&box->corner[GTK_CSS_TOP_LEFT], left, top);
  gtk_css_border_radius_grow (&box->corner[GTK_CSS_TOP_RIGHT], right, bottom);
  gtk_css_border_radius_grow (&box->corner[GTK_CSS_BOTTOM_RIGHT], right, top);
  gtk_css_border_radius_grow (&box->corner[GTK_CSS_BOTTOM_LEFT], left, bottom);
Benjamin Otte's avatar
Benjamin Otte committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}

void
_gtk_rounded_box_shrink (GtkRoundedBox *box,
                         double         top,
                         double         right,
                         double         bottom,
                         double         left)
{
  _gtk_rounded_box_grow (box, -top, -right, -bottom, -left);
}

void
_gtk_rounded_box_move (GtkRoundedBox *box,
                       double         dx,
                       double         dy)
{
  box->box.x += dx;
  box->box.y += dy;
}

static void
_cairo_ellipsis (cairo_t *cr,
	         double xc, double yc,
	         double xradius, double yradius,
	         double angle1, double angle2)
{
224 225
  cairo_matrix_t save;

Benjamin Otte's avatar
Benjamin Otte committed
226 227 228 229 230 231
  if (xradius <= 0.0 || yradius <= 0.0)
    {
      cairo_line_to (cr, xc, yc);
      return;
    }

232
  cairo_get_matrix (cr, &save);
Benjamin Otte's avatar
Benjamin Otte committed
233 234 235
  cairo_translate (cr, xc, yc);
  cairo_scale (cr, xradius, yradius);
  cairo_arc (cr, 0, 0, 1.0, angle1, angle2);
236
  cairo_set_matrix (cr, &save);
Benjamin Otte's avatar
Benjamin Otte committed
237 238
}

239 240 241 242 243 244
static void
_cairo_ellipsis_negative (cairo_t *cr,
                          double xc, double yc,
                          double xradius, double yradius,
                          double angle1, double angle2)
{
245 246
  cairo_matrix_t save;

247 248 249 250 251 252
  if (xradius <= 0.0 || yradius <= 0.0)
    {
      cairo_line_to (cr, xc, yc);
      return;
    }

253
  cairo_get_matrix (cr, &save);
254 255 256
  cairo_translate (cr, xc, yc);
  cairo_scale (cr, xradius, yradius);
  cairo_arc_negative (cr, 0, 0, 1.0, angle1, angle2);
257
  cairo_set_matrix (cr, &save);
258 259
}

Benjamin Otte's avatar
Benjamin Otte committed
260 261 262 263 264 265 266
void
_gtk_rounded_box_path (const GtkRoundedBox *box,
                       cairo_t             *cr)
{
  cairo_new_sub_path (cr);

  _cairo_ellipsis (cr,
267 268 269 270
                   box->box.x + box->corner[GTK_CSS_TOP_LEFT].horizontal,
                   box->box.y + box->corner[GTK_CSS_TOP_LEFT].vertical,
                   box->corner[GTK_CSS_TOP_LEFT].horizontal,
                   box->corner[GTK_CSS_TOP_LEFT].vertical,
271
                   G_PI, 3 * G_PI_2);
Benjamin Otte's avatar
Benjamin Otte committed
272
  _cairo_ellipsis (cr, 
273 274 275 276
                   box->box.x + box->box.width - box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   box->box.y + box->corner[GTK_CSS_TOP_RIGHT].vertical,
                   box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   box->corner[GTK_CSS_TOP_RIGHT].vertical,
277
                   - G_PI_2, 0);
Benjamin Otte's avatar
Benjamin Otte committed
278
  _cairo_ellipsis (cr,
279 280 281 282
                   box->box.x + box->box.width - box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                   box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
283
                   0, G_PI_2);
Benjamin Otte's avatar
Benjamin Otte committed
284
  _cairo_ellipsis (cr,
285 286 287 288
                   box->box.x + box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                   box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
289
                   G_PI_2, G_PI);
Benjamin Otte's avatar
Benjamin Otte committed
290 291

  cairo_close_path (cr);
Benjamin Otte's avatar
Benjamin Otte committed
292 293
}

294 295 296 297 298
double
_gtk_rounded_box_guess_length (const GtkRoundedBox *box,
                               GtkCssSide           side)
{
  double length;
299
  GtkCssSide before, after;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

  before = side;
  after = (side + 1) % 4;

  if (side & 1)
    length = box->box.height
             - box->corner[before].vertical
             - box->corner[after].vertical;
  else
    length = box->box.width
             - box->corner[before].horizontal
             - box->corner[after].horizontal;

  length += G_PI * 0.125 * (box->corner[before].horizontal
                            + box->corner[before].vertical
                            + box->corner[after].horizontal
                            + box->corner[after].vertical);

  return length;
}

void
_gtk_rounded_box_path_side (const GtkRoundedBox *box,
                            cairo_t             *cr,
                            GtkCssSide           side)
{
  switch (side)
    {
    case GTK_CSS_TOP:
      _cairo_ellipsis (cr,
                       box->box.x + box->corner[GTK_CSS_TOP_LEFT].horizontal,
                       box->box.y + box->corner[GTK_CSS_TOP_LEFT].vertical,
                       box->corner[GTK_CSS_TOP_LEFT].horizontal,
                       box->corner[GTK_CSS_TOP_LEFT].vertical,
334
                       5 * G_PI_4, 3 * G_PI_2);
335 336 337 338 339
      _cairo_ellipsis (cr, 
                       box->box.x + box->box.width - box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                       box->box.y + box->corner[GTK_CSS_TOP_RIGHT].vertical,
                       box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                       box->corner[GTK_CSS_TOP_RIGHT].vertical,
340
                       - G_PI_2, -G_PI_4);
341 342 343 344 345 346 347
      break;
    case GTK_CSS_RIGHT:
      _cairo_ellipsis (cr, 
                       box->box.x + box->box.width - box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                       box->box.y + box->corner[GTK_CSS_TOP_RIGHT].vertical,
                       box->corner[GTK_CSS_TOP_RIGHT].horizontal,
                       box->corner[GTK_CSS_TOP_RIGHT].vertical,
348
                       - G_PI_4, 0);
349 350 351 352 353
      _cairo_ellipsis (cr,
                       box->box.x + box->box.width - box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                       box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                       box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                       box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
354
                       0, G_PI_4);
355 356 357 358 359 360 361
      break;
    case GTK_CSS_BOTTOM:
      _cairo_ellipsis (cr,
                       box->box.x + box->box.width - box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                       box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                       box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                       box->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
362
                       G_PI_4, G_PI_2);
363 364 365 366 367
      _cairo_ellipsis (cr,
                       box->box.x + box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                       box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                       box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                       box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
368
                       G_PI_2, 3 * G_PI_4);
369 370 371 372 373 374 375
      break;
    case GTK_CSS_LEFT:
      _cairo_ellipsis (cr,
                       box->box.x + box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                       box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                       box->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                       box->corner[GTK_CSS_BOTTOM_LEFT].vertical,
376
                       3 * G_PI_4, G_PI);
377 378 379 380 381
      _cairo_ellipsis (cr,
                       box->box.x + box->corner[GTK_CSS_TOP_LEFT].horizontal,
                       box->box.y + box->corner[GTK_CSS_TOP_LEFT].vertical,
                       box->corner[GTK_CSS_TOP_LEFT].horizontal,
                       box->corner[GTK_CSS_TOP_LEFT].vertical,
382
                       G_PI, 5 * G_PI_4);
383 384 385 386 387 388 389
      break;
    default:
      g_assert_not_reached ();
      break;
    }
}

390 391 392 393 394
void
_gtk_rounded_box_path_top (const GtkRoundedBox *outer,
                           const GtkRoundedBox *inner,
                           cairo_t             *cr)
{
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  double start_angle, middle_angle, end_angle;

  if (outer->box.y == inner->box.y)
    return;

  if (outer->box.x == inner->box.x)
    start_angle = G_PI;
  else
    start_angle = 5 * G_PI_4;
  middle_angle = 3 * G_PI_2;
  if (outer->box.x + outer->box.width == inner->box.x + inner->box.width)
    end_angle = 0;
  else
    end_angle = 7 * G_PI_4;

410 411 412
  cairo_new_sub_path (cr);

  _cairo_ellipsis (cr,
413 414 415 416
                   outer->box.x + outer->corner[GTK_CSS_TOP_LEFT].horizontal,
                   outer->box.y + outer->corner[GTK_CSS_TOP_LEFT].vertical,
                   outer->corner[GTK_CSS_TOP_LEFT].horizontal,
                   outer->corner[GTK_CSS_TOP_LEFT].vertical,
417
                   start_angle, middle_angle);
418
  _cairo_ellipsis (cr, 
419 420 421 422
                   outer->box.x + outer->box.width - outer->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   outer->box.y + outer->corner[GTK_CSS_TOP_RIGHT].vertical,
                   outer->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   outer->corner[GTK_CSS_TOP_RIGHT].vertical,
423
                   middle_angle, end_angle);
424 425

  _cairo_ellipsis_negative (cr, 
426 427 428 429
                            inner->box.x + inner->box.width - inner->corner[GTK_CSS_TOP_RIGHT].horizontal,
                            inner->box.y + inner->corner[GTK_CSS_TOP_RIGHT].vertical,
                            inner->corner[GTK_CSS_TOP_RIGHT].horizontal,
                            inner->corner[GTK_CSS_TOP_RIGHT].vertical,
430
                            end_angle, middle_angle);
431
  _cairo_ellipsis_negative (cr,
432 433 434 435
                            inner->box.x + inner->corner[GTK_CSS_TOP_LEFT].horizontal,
                            inner->box.y + inner->corner[GTK_CSS_TOP_LEFT].vertical,
                            inner->corner[GTK_CSS_TOP_LEFT].horizontal,
                            inner->corner[GTK_CSS_TOP_LEFT].vertical,
436
                            middle_angle, start_angle);
437 438 439 440 441 442 443 444 445

  cairo_close_path (cr);
}

void
_gtk_rounded_box_path_right (const GtkRoundedBox *outer,
                             const GtkRoundedBox *inner,
                             cairo_t             *cr)
{
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  double start_angle, middle_angle, end_angle;

  if (outer->box.x + outer->box.width == inner->box.x + inner->box.width)
    return;

  if (outer->box.y == inner->box.y)
    start_angle = 3 * G_PI_2;
  else
    start_angle = 7 * G_PI_4;
  middle_angle = 0;
  if (outer->box.y + outer->box.height == inner->box.y + inner->box.height)
    end_angle = G_PI_2;
  else
    end_angle = G_PI_4;

461 462 463
  cairo_new_sub_path (cr);

  _cairo_ellipsis (cr, 
464 465 466 467
                   outer->box.x + outer->box.width - outer->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   outer->box.y + outer->corner[GTK_CSS_TOP_RIGHT].vertical,
                   outer->corner[GTK_CSS_TOP_RIGHT].horizontal,
                   outer->corner[GTK_CSS_TOP_RIGHT].vertical,
468
                   start_angle, middle_angle);
469
  _cairo_ellipsis (cr,
470 471 472 473
                   outer->box.x + outer->box.width - outer->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   outer->box.y + outer->box.height - outer->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                   outer->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   outer->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
474
                   middle_angle, end_angle);
475 476

  _cairo_ellipsis_negative (cr,
477 478 479 480
                            inner->box.x + inner->box.width - inner->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                            inner->box.y + inner->box.height - inner->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                            inner->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                            inner->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
481
                            end_angle, middle_angle);
482
  _cairo_ellipsis_negative (cr, 
483 484 485 486
                            inner->box.x + inner->box.width - inner->corner[GTK_CSS_TOP_RIGHT].horizontal,
                            inner->box.y + inner->corner[GTK_CSS_TOP_RIGHT].vertical,
                            inner->corner[GTK_CSS_TOP_RIGHT].horizontal,
                            inner->corner[GTK_CSS_TOP_RIGHT].vertical,
487
                            middle_angle, start_angle);
488 489 490 491 492 493 494 495 496

  cairo_close_path (cr);
}

void
_gtk_rounded_box_path_bottom (const GtkRoundedBox *outer,
                              const GtkRoundedBox *inner,
                              cairo_t             *cr)
{
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
  double start_angle, middle_angle, end_angle;

  if (outer->box.y + outer->box.height == inner->box.y + inner->box.height)
    return;

  if (outer->box.x + outer->box.width == inner->box.x + inner->box.width)
    start_angle = 0;
  else
    start_angle = G_PI_4;
  middle_angle = G_PI_2;
  if (outer->box.x == inner->box.x)
    end_angle = G_PI;
  else
    end_angle = 3 * G_PI_4;

512 513 514
  cairo_new_sub_path (cr);

  _cairo_ellipsis (cr,
515 516 517 518
                   outer->box.x + outer->box.width - outer->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   outer->box.y + outer->box.height - outer->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                   outer->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                   outer->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
519
                   start_angle, middle_angle);
520
  _cairo_ellipsis (cr,
521 522 523 524
                   outer->box.x + outer->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   outer->box.y + outer->box.height - outer->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                   outer->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   outer->corner[GTK_CSS_BOTTOM_LEFT].vertical,
525
                   middle_angle, end_angle);
526 527

  _cairo_ellipsis_negative (cr,
528 529 530 531
                            inner->box.x + inner->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                            inner->box.y + inner->box.height - inner->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                            inner->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                            inner->corner[GTK_CSS_BOTTOM_LEFT].vertical,
532
                            end_angle, middle_angle);
533
  _cairo_ellipsis_negative (cr,
534 535 536 537
                            inner->box.x + inner->box.width - inner->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                            inner->box.y + inner->box.height - inner->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
                            inner->corner[GTK_CSS_BOTTOM_RIGHT].horizontal,
                            inner->corner[GTK_CSS_BOTTOM_RIGHT].vertical,
538
                            middle_angle, start_angle);
539 540 541 542 543 544 545 546 547

  cairo_close_path (cr);
}

void
_gtk_rounded_box_path_left (const GtkRoundedBox *outer,
                            const GtkRoundedBox *inner,
                            cairo_t             *cr)
{
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  double start_angle, middle_angle, end_angle;

  if (outer->box.x == inner->box.x)
    return;

  if (outer->box.y + outer->box.height == inner->box.y + inner->box.height)
    start_angle = G_PI_2;
  else
    start_angle = 3 * G_PI_4;
  middle_angle = G_PI;
  if (outer->box.y == inner->box.y)
    end_angle = 3 * G_PI_2;
  else
    end_angle = 5 * G_PI_4;

563 564 565
  cairo_new_sub_path (cr);

  _cairo_ellipsis (cr,
566 567 568 569
                   outer->box.x + outer->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   outer->box.y + outer->box.height - outer->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                   outer->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                   outer->corner[GTK_CSS_BOTTOM_LEFT].vertical,
570
                   start_angle, middle_angle);
571
  _cairo_ellipsis (cr,
572 573 574 575
                   outer->box.x + outer->corner[GTK_CSS_TOP_LEFT].horizontal,
                   outer->box.y + outer->corner[GTK_CSS_TOP_LEFT].vertical,
                   outer->corner[GTK_CSS_TOP_LEFT].horizontal,
                   outer->corner[GTK_CSS_TOP_LEFT].vertical,
576
                   middle_angle, end_angle);
577 578

  _cairo_ellipsis_negative (cr,
579 580 581 582
                            inner->box.x + inner->corner[GTK_CSS_TOP_LEFT].horizontal,
                            inner->box.y + inner->corner[GTK_CSS_TOP_LEFT].vertical,
                            inner->corner[GTK_CSS_TOP_LEFT].horizontal,
                            inner->corner[GTK_CSS_TOP_LEFT].vertical,
583
                            end_angle, middle_angle);
584
  _cairo_ellipsis_negative (cr,
585 586 587 588
                            inner->box.x + inner->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                            inner->box.y + inner->box.height - inner->corner[GTK_CSS_BOTTOM_LEFT].vertical,
                            inner->corner[GTK_CSS_BOTTOM_LEFT].horizontal,
                            inner->corner[GTK_CSS_BOTTOM_LEFT].vertical,
589
                            middle_angle, start_angle);
590 591 592 593

  cairo_close_path (cr);
}

Benjamin Otte's avatar
Benjamin Otte committed
594 595 596 597 598 599 600 601 602
void
_gtk_rounded_box_clip_path (const GtkRoundedBox *box,
                            cairo_t             *cr)
{
  cairo_rectangle (cr,
                   box->box.x, box->box.y,
                   box->box.width, box->box.height);
}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
gboolean
_gtk_rounded_box_intersects_rectangle (const GtkRoundedBox *box,
                                       gdouble              x1,
                                       gdouble              y1,
                                       gdouble              x2,
                                       gdouble              y2)
{
  if (x2 < box->box.x ||
      y2 < box->box.y ||
      x1 >= box->box.x + box->box.width ||
      y1 >= box->box.y + box->box.height)
    return FALSE;

  return TRUE;
}

gboolean
_gtk_rounded_box_contains_rectangle (const GtkRoundedBox *box,
                                     gdouble              x1,
                                     gdouble              y1,
                                     gdouble              x2,
                                     gdouble              y2)
{
  if (x1 < box->box.x ||
      y1 < box->box.y ||
      x2 >= box->box.x + box->box.width ||
629
      y2 >= box->box.y + box->box.height)
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    return FALSE;

  if (x1 < box->box.x + box->corner[GTK_CSS_TOP_LEFT].horizontal &&
      y1 < box->box.y + box->corner[GTK_CSS_TOP_LEFT].vertical)
    return FALSE;

  if (x2 > box->box.x + box->box.width - box->corner[GTK_CSS_TOP_RIGHT].horizontal &&
      y1 < box->box.y + box->corner[GTK_CSS_TOP_RIGHT].vertical)
    return FALSE;

  if (x2 > box->box.x + box->box.width - box->corner[GTK_CSS_BOTTOM_RIGHT].horizontal &&
      y2 > box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_RIGHT].vertical)
    return FALSE;

  if (x1 < box->box.x + box->corner[GTK_CSS_BOTTOM_LEFT].horizontal &&
      y2 > box->box.y + box->box.height - box->corner[GTK_CSS_BOTTOM_LEFT].vertical)
    return FALSE;

  return TRUE;
}