1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include "variable.hpp"
29 #include "observer.hpp"
30 #include "pointer.hpp"
33 /// Interface for rectangular objects
39 /// Get the size of the box
40 virtual int getWidth() const = 0;
41 virtual int getHeight() const = 0;
45 /// Interface for rectangular objects with a position
46 class GenericRect
: public Box
49 virtual int getLeft() const = 0;
50 virtual int getTop() const = 0;
54 /// Characterization of a rectangle
55 class SkinsRect
: public GenericRect
58 SkinsRect( int left
, int top
, int right
, int bottom
);
60 virtual int getLeft() const { return m_left
; }
61 virtual int getTop() const { return m_top
; }
62 virtual int getRight() const { return m_right
; }
63 virtual int getBottom() const { return m_bottom
; }
64 virtual int getWidth() const { return m_right
- m_left
; }
65 virtual int getHeight() const { return m_bottom
- m_top
; }
75 /// Relative position of a rectangle in a box
77 * Note: Even if the object is tied to its direct container rectangle, the
78 * coordinates returned by getLeft(), getTop(), getRight() and getBottom()
79 * are not relative to the direct container (which is usually a panel or
80 * the layout) but to the root container (i.e. the layout).
82 class Position
: public GenericRect
85 /// Type for reference edge/corner
88 /// Coordinates are relative to the upper left corner
90 /// Coordinates are relative to the upper right corner
92 /// Coordinates are relative to the lower left corner
94 /// Coordinates are relative to the lower right corner
98 /// Create a new position relative to the given box
99 Position( int left
, int top
, int right
, int bottom
,
100 const GenericRect
&rRect
,
101 Ref_t refLeftTop
, Ref_t refRightBottom
,
102 bool xKeepRatio
, bool yKeepRatio
);
106 /// Get the position relative to the left top corner of the box
107 virtual int getLeft() const;
108 virtual int getTop() const;
109 int getRight() const;
110 int getBottom() const;
111 /// Get the size of the rectangle
112 virtual int getWidth() const;
113 virtual int getHeight() const;
114 /// Get the reference corners
115 Ref_t
getRefLeftTop() const { return m_refLeftTop
; }
116 Ref_t
getRefRightBottom() const { return m_refRighBottom
; }
119 /// Position and reference edge/corner
124 const GenericRect
&m_rRect
;
126 Ref_t m_refRighBottom
;
127 /// "Keep ratio" mode
130 /// Initial width ratio (usually between 0 and 1)
132 /// Initial height ratio (usually between 0 and 1)
136 typedef CountedPtr
<Position
> PositionPtr
;
139 /// Variable implementing the Box interface
140 class VarBox
: public Variable
, public Box
, public Subject
<VarBox
>
143 VarBox( intf_thread_t
*pIntf
, int width
= 0, int height
= 0 );
145 virtual ~VarBox() { }
147 /// Get the variable type
148 virtual const string
&getType() const { return m_type
; }
150 /// Get the size of the box
151 virtual int getWidth() const;
152 virtual int getHeight() const;
154 /// Change the size of the box
155 void setSize( int width
, int height
);
159 static const string m_type
;
161 int m_width
, m_height
;
168 rect( int v_x
= 0, int v_y
= 0, int v_width
= 0, int v_height
= 0 )
169 : x( v_x
), y( v_y
), width( v_width
), height( v_height
) { }
176 // rect2 fully included in rect1
177 static bool isIncluded( const rect
& rect2
, const rect
& rect1
)
181 int w1
= rect1
.width
;
182 int h1
= rect1
.height
;
186 int w2
= rect2
.width
;
187 int h2
= rect2
.height
;
189 return x2
>= x1
&& x2
+ w2
<= x1
+ w1
190 && y2
>= y1
&& y2
+ h2
<= y1
+ h1
;
193 static bool areDisjunct( const rect
& rect2
, const rect
& rect1
)
197 int w1
= rect1
.width
;
198 int h1
= rect1
.height
;
202 int w2
= rect2
.width
;
203 int h2
= rect2
.height
;
205 return y2
+ h2
-1 < y1
// rect2 above rect1
206 || y2
> y1
+ h1
- 1 // rect2 under rect1
207 || x2
> x1
+ w1
-1 // rect2 right of rect1
208 || x2
+ w2
- 1 < x1
; // rect2 left of rect1
211 static bool intersect( const rect
& rect1
, const rect
& rect2
, rect
* pRect
)
215 int w1
= rect1
.width
;
216 int h1
= rect1
.height
;
220 int w2
= rect2
.width
;
221 int h2
= rect2
.height
;
223 if( areDisjunct( rect1
, rect2
) )
227 int left
= max( x1
, x2
);
228 int right
= min( x1
+ w1
- 1, x2
+ w2
- 1 );
229 int top
= max( y1
, y2
);
230 int bottom
= min( y1
+ h1
- 1, y2
+ h2
-1 );
233 pRect
->width
= right
- left
+ 1;
234 pRect
->height
= bottom
- top
+ 1;
236 return pRect
->width
> 0 && pRect
->height
> 0;
240 static bool join( const rect
& rect1
, const rect
& rect2
, rect
* pRect
)
244 int w1
= rect1
.width
;
245 int h1
= rect1
.height
;
249 int w2
= rect2
.width
;
250 int h2
= rect2
.height
;
252 int left
= min( x1
, x2
);
253 int right
= max( x1
+ w1
- 1, x2
+ w2
- 1 );
254 int top
= min( y1
, y2
);
255 int bottom
= max( y1
+ h1
- 1, y2
+ h2
-1 );
258 pRect
->width
= right
- left
+ 1;
259 pRect
->height
= bottom
- top
+ 1;
261 return pRect
->width
> 0 && pRect
->height
> 0;
263 static int min( int x
, int y
) { return x
< y
? x
: y
; }
264 static int max( int x
, int y
) { return x
< y
? y
: x
; }
266 bool operator==( const rect
& other
) const
268 return x
== other
.x
&&
270 width
== other
.width
&&
271 height
== other
.height
;