[NUI] Fix comments according to document review
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / Rectangle.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// The Rectangle class.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     [Binding.TypeConverter(typeof(RectangleTypeConverter))]
28     public class Rectangle : Disposable, ICloneable
29     {
30         /// <summary>
31         /// The constructor.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         public Rectangle() : this(Interop.Rectangle.NewRectangle(), true)
35         {
36             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
37         }
38
39         /// <summary>
40         /// The constructor.
41         /// </summary>
42         /// <param name="x">The x coordinate (or left).</param>
43         /// <param name="y">The y coordinate (or right).</param>
44         /// <param name="width">The width (or bottom).</param>
45         /// <param name="height">The height (or top).</param>
46         /// <since_tizen> 3 </since_tizen>
47         public Rectangle(int x, int y, int width, int height) : this(Interop.Rectangle.NewRectangle(x, y, width, height), true)
48         {
49             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
50         }
51
52         internal Rectangle(Rectangle other) : this(other.x, other.y, other.width, other.height)
53         {
54         }
55
56         internal Rectangle(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
57         {
58         }
59
60         internal Rectangle(RectangleChangedCallback cb, int x, int y, int width, int height) : this(Interop.Rectangle.NewRectangle(x, y, width, height), true)
61         {
62             callback = cb;
63             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
64         }
65
66         internal Rectangle(RectangleChangedCallback cb) : this()
67         {
68             callback = cb;
69         }
70
71         internal Rectangle(RectangleChangedCallback cb, Rectangle other) : this(cb, other.x, other.y, other.width, other.height)
72         {
73         }
74
75         /// <summary>
76         /// The type cast operator, int to Rectangle.
77         /// </summary>
78         /// <param name="value">A value of int type.</param>
79         /// <returns>return a Extents instance</returns>
80         [EditorBrowsable(EditorBrowsableState.Never)]
81         public static implicit operator Rectangle(int value)
82         {
83             return new Rectangle(value, value, value, value);
84         }
85
86         internal delegate void RectangleChangedCallback(int x, int y, int width, int height);
87         private RectangleChangedCallback callback = null;
88
89         /// <summary>
90         /// The x position of the rectangle.
91         /// </summary>
92         /// <remarks>
93         /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
94         /// </remarks>
95         /// <code>
96         /// // DO NOT use like the followings!
97         /// Rectangle rectangle = new Rectangle();
98         /// rectangle.X = 1; 
99         /// // USE like this
100         /// int x = 1, y = 2, width = 3, height = 4;
101         /// Rectangle rectangle = new Rectangle(x, y, width, height);
102         /// </code>
103         /// <since_tizen> 3 </since_tizen>
104         public int X
105         {
106             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
107             set
108             {
109                 x = (value);
110
111                 callback?.Invoke(X, Y, Width, Height);
112             }
113             get
114             {
115                 return x;
116             }
117         }
118
119         /// <summary>
120         /// The Y position of the rectangle.
121         /// </summary>
122         /// <remarks>
123         /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
124         /// </remarks>
125         /// <code>
126         /// // DO NOT use like the followings!
127         /// Rectangle rectangle = new Rectangle();
128         /// rectangle.Y = 2; 
129         /// // USE like this
130         /// int x = 1, y = 2, width = 3, height = 4;
131         /// Rectangle rectangle = new Rectangle(x, y, width, height);
132         /// </code>
133         /// <since_tizen> 3 </since_tizen>
134         public int Y
135         {
136             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
137             set
138             {
139                 y = (value);
140
141                 callback?.Invoke(X, Y, Width, Height);
142             }
143             get
144             {
145                 return y;
146             }
147         }
148
149         /// <summary>
150         /// The width of the rectangle.
151         /// </summary>
152         /// <remarks>
153         /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
154         /// </remarks>
155         /// <code>
156         /// // DO NOT use like the followings!
157         /// Rectangle rectangle = new Rectangle();
158         /// rectangle.Width = 3; 
159         /// // USE like this
160         /// int x = 1, y = 2, width = 3, height = 4;
161         /// Rectangle rectangle = new Rectangle(x, y, width, height);
162         /// </code>
163         /// <since_tizen> 3 </since_tizen>
164         public int Width
165         {
166             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
167             set
168             {
169                 width = (value);
170
171                 callback?.Invoke(X, Y, Width, Height);
172             }
173             get
174             {
175                 return width;
176             }
177         }
178
179         /// <summary>
180         /// The height of the rectangle.
181         /// </summary>
182         /// <remarks>
183         /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
184         /// </remarks>
185         /// <code>
186         /// // DO NOT use like the followings!
187         /// Rectangle rectangle = new Rectangle();
188         /// rectangle.Height = 4; 
189         /// // USE like this
190         /// int x = 1, y = 2, width = 3, height = 4;
191         /// Rectangle rectangle = new Rectangle(x, y, width, height);
192         /// </code>
193         /// <since_tizen> 3 </since_tizen>
194         public int Height
195         {
196             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
197             set
198             {
199                 height = (value);
200
201                 callback?.Invoke(X, Y, Width, Height);
202             }
203             get
204             {
205                 return height;
206             }
207         }
208
209         private int x
210         {
211             set
212             {
213                 Interop.Rectangle.XSet(SwigCPtr, value);
214                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
215             }
216             get
217             {
218                 int ret = Interop.Rectangle.XGet(SwigCPtr);
219                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220                 return ret;
221             }
222         }
223
224         private int left
225         {
226             set
227             {
228                 Interop.Rectangle.LeftSet(SwigCPtr, value);
229                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             }
231             get
232             {
233                 int ret = Interop.Rectangle.LeftGet(SwigCPtr);
234                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235                 return ret;
236             }
237         }
238
239         private int y
240         {
241             set
242             {
243                 Interop.Rectangle.YSet(SwigCPtr, value);
244                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
245             }
246             get
247             {
248                 int ret = Interop.Rectangle.YGet(SwigCPtr);
249                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
250                 return ret;
251             }
252         }
253
254         private int right
255         {
256             set
257             {
258                 Interop.Rectangle.RightSet(SwigCPtr, value);
259                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
260             }
261             get
262             {
263                 int ret = Interop.Rectangle.RightGet(SwigCPtr);
264                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
265                 return ret;
266             }
267         }
268
269         private int width
270         {
271             set
272             {
273                 Interop.Rectangle.WidthSet(SwigCPtr, value);
274                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
275             }
276             get
277             {
278                 int ret = Interop.Rectangle.WidthGet(SwigCPtr);
279                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
280                 return ret;
281             }
282         }
283
284         private int bottom
285         {
286             set
287             {
288                 Interop.Rectangle.BottomSet(SwigCPtr, value);
289                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
290             }
291             get
292             {
293                 int ret = Interop.Rectangle.BottomGet(SwigCPtr);
294                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
295                 return ret;
296             }
297         }
298
299         private int height
300         {
301             set
302             {
303                 Interop.Rectangle.HeightSet(SwigCPtr, value);
304                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             }
306             get
307             {
308                 int ret = Interop.Rectangle.HeightGet(SwigCPtr);
309                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
310                 return ret;
311             }
312         }
313
314         private int top
315         {
316             set
317             {
318                 Interop.Rectangle.TopSet(SwigCPtr, value);
319                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320             }
321             get
322             {
323                 int ret = Interop.Rectangle.TopGet(SwigCPtr);
324                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
325                 return ret;
326             }
327         }
328
329         /// <summary>
330         /// THe Equality operator.
331         /// </summary>
332         /// <param name="a">The first operand.</param>
333         /// <param name="b">The second operand.</param>
334         /// <returns>True if the boxes are exactly the same.</returns>
335         /// <since_tizen> 3 </since_tizen>
336         public static bool operator ==(Rectangle a, Rectangle b)
337         {
338             // If both are null, or both are same instance, return true.
339             if (System.Object.ReferenceEquals(a, b))
340             {
341                 return true;
342             }
343
344             // If one is null, but not both, return false.
345             if (((object)a == null) || ((object)b == null))
346             {
347                 return false;
348             }
349
350             // Return true if the fields match:
351             return a.X == b.X && a.Y == b.Y && a.Width == b.Width && a.Height == b.Height;
352         }
353
354         /// <summary>
355         /// Inequality operator.
356         /// </summary>
357         /// <param name="a">The first rectangle.</param>
358         /// <param name="b">The second rectangle.</param>
359         /// <returns>True if the rectangles are not identical.</returns>
360         /// <since_tizen> 3 </since_tizen>
361         public static bool operator !=(Rectangle a, Rectangle b)
362         {
363             return !(a == b);
364         }
365
366         /// <summary>
367         /// Equality operator.
368         /// </summary>
369         /// <param name="o">The object to compare with the current object.</param>
370         /// <returns>True if boxes are exactly same.</returns>
371         /// <since_tizen> 4 </since_tizen>
372         public override bool Equals(object o)
373         {
374             if (o == null)
375             {
376                 return false;
377             }
378             if (!(o is Rectangle))
379             {
380                 return false;
381             }
382             Rectangle r = (Rectangle)o;
383
384             // Return true if the fields match:
385             return X == r.X && Y == r.Y && Width == r.Width && Height == r.Height;
386         }
387
388         /// <summary>
389         /// Serves as the default hash function.
390         /// </summary>
391         /// <returns>A hash code for the current object.</returns>
392         /// <since_tizen> 4 </since_tizen>
393         public override int GetHashCode()
394         {
395             return base.GetHashCode();
396         }
397
398         /// <summary>
399         /// Assignment from individual values.
400         /// </summary>
401         /// <param name="newX">The x coordinate.</param>
402         /// <param name="newY">The y coordinate.</param>
403         /// <param name="newWidth">The width.</param>
404         /// <param name="newHeight">The height.</param>
405         /// <since_tizen> 3 </since_tizen>
406         public void Set(int newX, int newY, int newWidth, int newHeight)
407         {
408             Interop.Rectangle.Set(SwigCPtr, newX, newY, newWidth, newHeight);
409             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410         }
411
412         /// <summary>
413         /// Determines whether or not this rectangle is empty.
414         /// </summary>
415         /// <returns>True if width or height are zero.</returns>
416         /// <since_tizen> 3 </since_tizen>
417         public bool IsEmpty()
418         {
419             bool ret = Interop.Rectangle.IsEmpty(SwigCPtr);
420             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
421             return ret;
422         }
423
424         /// <summary>
425         /// Gets the left of the rectangle.
426         /// </summary>
427         /// <returns>The left edge of the rectangle.</returns>
428         /// <since_tizen> 3 </since_tizen>
429         public int Left()
430         {
431             int ret = Interop.Rectangle.Left(SwigCPtr);
432             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433             return ret;
434         }
435
436         /// <summary>
437         /// Gets the right of the rectangle.
438         /// </summary>
439         /// <returns>The right edge of the rectangle.</returns>
440         /// <since_tizen> 3 </since_tizen>
441         public int Right()
442         {
443             int ret = Interop.Rectangle.Right(SwigCPtr);
444             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
445             return ret;
446         }
447
448         /// <summary>
449         /// Gets the top of the rectangle.
450         /// </summary>
451         /// <returns>The top of the rectangle.</returns>
452         /// <since_tizen> 3 </since_tizen>
453         public int Top()
454         {
455             int ret = Interop.Rectangle.Top(SwigCPtr);
456             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457             return ret;
458         }
459
460         /// <summary>
461         /// Gets the bottom of the rectangle.
462         /// </summary>
463         /// <returns>The bottom of the rectangle.</returns>
464         /// <since_tizen> 3 </since_tizen>
465         public int Bottom()
466         {
467             int ret = Interop.Rectangle.Bottom(SwigCPtr);
468             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
469             return ret;
470         }
471
472         /// <summary>
473         /// Gets the area of the rectangle.
474         /// </summary>
475         /// <returns>The area of the rectangle.</returns>
476         /// <since_tizen> 3 </since_tizen>
477         public int Area()
478         {
479             int ret = Interop.Rectangle.Area(SwigCPtr);
480             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
481             return ret;
482         }
483
484         /// <summary>
485         /// Determines whether or not this rectangle and the specified rectangle intersect.
486         /// </summary>
487         /// <param name="other">The other rectangle to test against this rectangle.</param>
488         /// <returns>True if the rectangles intersect.</returns>
489         /// <since_tizen> 3 </since_tizen>
490         public bool Intersects(Rectangle other)
491         {
492             bool ret = Interop.Rectangle.Intersects(SwigCPtr, Rectangle.getCPtr(other));
493             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
494             return ret;
495         }
496
497         /// <summary>
498         /// Determines whether or not this rectangle contains the specified rectangle.
499         /// </summary>
500         /// <param name="other">The other rectangle to test against this rectangle.</param>
501         /// <returns>True if the specified rectangle is contained.</returns>
502         /// <since_tizen> 3 </since_tizen>
503         public bool Contains(Rectangle other)
504         {
505             bool ret = Interop.Rectangle.Contains(SwigCPtr, Rectangle.getCPtr(other));
506             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
507             return ret;
508         }
509
510         /// <inheritdoc/>
511         [EditorBrowsable(EditorBrowsableState.Never)]
512         public object Clone() => new Rectangle(this);
513
514         /// This will not be public opened.
515         [EditorBrowsable(EditorBrowsableState.Never)]
516         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
517         {
518             Interop.Rectangle.DeleteRectangle(swigCPtr);
519         }
520
521         /// <summary>
522         /// Determines whether the reference is null or the Rectangle has all 0 properties.
523         /// </summary>
524         internal static bool IsNullOrZero(Rectangle rectangle) => (rectangle == null || (rectangle.top == 0 && rectangle.right == 0 && rectangle.bottom == 0 && rectangle.left == 0));
525     }
526 }