Fix NSGLView invalidation
[ardour.git] / libs / canvas / canvas / types.h
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __CANVAS_TYPES_H__
21 #define __CANVAS_TYPES_H__
22
23 #include <iostream>
24 #include <vector>
25 #include <stdint.h>
26 #include <algorithm>
27 #include <boost/optional.hpp>
28
29 #include <cairomm/refptr.h>
30
31 #include "gtkmm2ext/colors.h"
32
33 #include "canvas/visibility.h"
34
35 namespace Cairo {
36         class Context;
37 }
38
39 namespace ArdourCanvas
40 {
41
42 typedef double Coord;
43 typedef double Distance;
44
45 extern LIBCANVAS_API Coord const COORD_MAX;
46
47 inline Coord
48 canvas_safe_add (Coord a, Coord b)
49 {
50         if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) {
51                 return COORD_MAX;
52         }
53
54         return a + b;
55 }
56
57
58 struct LIBCANVAS_API Duple
59 {
60         Duple ()
61                 : x (0)
62                 , y (0)
63         {}
64
65         Duple (Coord x_, Coord y_)
66                 : x (x_)
67                 , y (y_)
68         {}
69
70         Coord x;
71         Coord y;
72
73         Duple translate (const Duple& t) const throw() {
74                 return Duple (canvas_safe_add (x, t.x), canvas_safe_add (y, t.y));
75         }
76
77         Duple operator- () const throw () {
78                 return Duple (-x, -y);
79         }
80         Duple operator+ (Duple const & o) const throw () {
81                 return Duple (canvas_safe_add (x, o.x), canvas_safe_add (y, o.y));
82         }
83         bool operator== (Duple const & o) const throw () {
84                 return x == o.x && y == o.y;
85         }
86         bool operator!= (Duple const & o) const throw () {
87                 return x != o.x || y != o.y;
88         }
89         Duple operator- (Duple const & o) const throw () {
90                 return Duple (x - o.x, y - o.y);
91         }
92         Duple operator/ (double b) const throw () {
93                 return Duple (x / b, y / b);
94         }
95 };
96
97
98 extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Duple const &);
99
100 struct LIBCANVAS_API Rect
101 {
102         Rect ()
103                 : x0 (0)
104                 , y0 (0)
105                 , x1 (0)
106                 , y1 (0)
107         {}
108
109         Rect (Coord x0_, Coord y0_, Coord x1_, Coord y1_)
110                 : x0 (x0_)
111                 , y0 (y0_)
112                 , x1 (x1_)
113                 , y1 (y1_)
114         {}
115
116         Coord x0;
117         Coord y0;
118         Coord x1;
119         Coord y1;
120
121         Rect intersection (Rect const & o) const throw () {
122                 Rect i (std::max (x0, o.x0), std::max (y0, o.y0),
123                         std::min (x1, o.x1), std::min (y1, o.y1));
124
125                 if (i.x0 > i.x1 || i.y0 > i.y1) {
126                         return Rect();
127                 }
128
129                 return i;
130         }
131
132         Rect extend (Rect const & o) const throw () {
133                 return Rect (std::min (x0, o.x0), std::min (y0, o.y0),
134                              std::max (x1, o.x1), std::max (y1, o.y1));
135         }
136         Rect translate (Duple const& t) const throw () {
137                 return Rect (canvas_safe_add (x0, t.x), canvas_safe_add (y0, t.y),
138                              canvas_safe_add (x1, t.x),canvas_safe_add (y1, t.y));
139         }
140         Rect expand (Distance amount) const throw () {
141                 return Rect (x0 - amount, y0 - amount,
142                              canvas_safe_add (x1, amount),
143                              canvas_safe_add (y1, amount));
144         }
145         Rect expand (Distance top, Distance right, Distance bottom, Distance left) const throw () {
146                 return Rect (x0 - left, y0 - top,
147                              canvas_safe_add (x1, right),
148                              canvas_safe_add (y1, bottom));
149         }
150
151         Rect shrink (Distance amount) const throw () {
152                 /* This isn't the equivalent of expand (-distance) because
153                    of the peculiarities of canvas_safe_add() with negative values.
154                    Maybe.
155                 */
156                 return Rect (canvas_safe_add (x0, amount), canvas_safe_add (y0, amount),
157                              x1 - amount, y1 - amount);
158         }
159
160         bool contains (Duple const & point) const throw () {
161                 return point.x >= x0 && point.x < x1 && point.y >= y0 && point.y < y1;
162         }
163         Rect fix () const throw () {
164                 return Rect (std::min (x0, x1), std::min (y0, y1),
165                              std::max (x0, x1), std::max (y0, y1));
166         }
167
168         bool empty() const throw () { return (x0 == x1 && y0 == y1); }
169         operator bool() const throw () { return !empty(); }
170
171         Distance width () const  throw () {
172                 return x1 - x0;
173         }
174
175         Distance height () const throw () {
176                 return y1 - y0;
177         }
178         bool operator!= (Rect const & o) const throw () {
179                 return x0 != o.x0 ||
180                         x1 != o.x1 ||
181                         y0 != o.y0 ||
182                         y1 != o.y1;
183         }
184 };
185
186 extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Rect const &);
187
188 typedef std::vector<Duple> Points;
189
190 }
191
192 #endif