Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Background.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// The Background is a widget that use for setting (solid) background decorations to a window (unless it has transparency enabled)
23     /// or to any container object.
24     /// </summary>
25     public class Background : Layout
26     {
27         /// <summary>
28         /// Creates and initializes a new instance of the Background class.
29         /// </summary>
30         /// <param name="parent">The EvasObject to which the new Background will be attached as a child.</param>
31         public Background(EvasObject parent) : base(parent)
32         {
33             Style = "transparent";
34         }
35
36         /// <summary>
37         /// Sets or gets color to Background.
38         /// </summary>
39         public override Color Color
40         {
41             get
42             {
43                 int r = 0;
44                 int g = 0;
45                 int b = 0;
46                 int a = 0;
47                 var swallowContent = GetPartContent("elm.swallow.rectangle");
48                 if (swallowContent != IntPtr.Zero)
49                 {
50                     Interop.Evas.evas_object_color_get(swallowContent, out r, out g, out b, out a);
51                 }
52                 return new Color(r, g, b, a);
53             }
54             set
55             {
56                 var swallowContent = GetPartContent("elm.swallow.rectangle");
57                 if (swallowContent == IntPtr.Zero)
58                 {
59                     Interop.Elementary.elm_bg_color_set(RealHandle, value.R, value.G, value.B);
60                     swallowContent = GetPartContent("elm.swallow.rectangle");
61                 }
62                 Interop.Evas.evas_object_color_set(swallowContent, value.R, value.G, value.B, value.A);
63             }
64         }
65
66         /// <summary>
67         /// Sets or gets image to Background.
68         /// </summary>
69         public string File
70         {
71             get
72             {
73                 return Interop.Elementary.BackgroundFileGet(RealHandle);
74             }
75             set
76             {
77                 Interop.Elementary.elm_bg_file_set(RealHandle, value, IntPtr.Zero);
78             }
79         }
80
81         /// <summary>
82         /// Sets or gets the mode of display for a given background widget's image.
83         /// </summary>
84         /// <remarks>
85         /// This sets how the background widget will display its image.
86         /// This will only work if the File was previously set with an image file on obj.
87         /// The image can be display tiled, scaled, centered or stretched. scaled by default.
88         /// </remarks>
89         public BackgroundOptions BackgroundOption
90         {
91             get
92             {
93                 return (BackgroundOptions)Interop.Elementary.elm_bg_option_get(RealHandle);
94             }
95             set
96             {
97                 Interop.Elementary.elm_bg_option_set(RealHandle, (Interop.Elementary.BackgroundOptions)value);
98             }
99         }
100
101         /// <summary>
102         /// Set the size of the pixmap representation of the image set on a given background widget.
103         /// This method just makes sense if an image file was set.
104         /// This is just a hint for the underlying system.
105         /// The real size of the pixmap may differ depending on the type of image being loaded, being bigger than requested.
106         /// </summary>
107         /// <param name="w">The new width of the image pixmap representation.</param>
108         /// <param name="h">The new height of the image pixmap representation.</param>
109         public void SetFileLoadSize(int w, int h)
110         {
111             if (File != null)
112             {
113                 Interop.Elementary.elm_bg_load_size_set(RealHandle, w, h);
114             }
115             else
116             {
117                 throw new Exception("This method just makes sense if an image file was set.");
118             }
119         }
120
121         protected override IntPtr CreateHandle(EvasObject parent)
122         {
123             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
124             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
125
126             RealHandle = Interop.Elementary.elm_bg_add(handle);
127             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
128
129             return handle;
130         }
131     }
132
133     /// <summary>
134     /// Enumeration for the background type.
135     /// </summary>
136     public enum BackgroundOptions
137     {
138         /// <summary>
139         /// Centers the background image
140         /// </summary>
141         Center,
142
143         /// <summary>
144         /// Scales the background image, retaining the aspect ratio
145         /// </summary>
146         Scale,
147
148         /// <summary>
149         /// Stretches the background image to fill the UI component's area.
150         /// </summary>
151         Stretch,
152
153         /// <summary>
154         /// Tiles the background image at its original size
155         /// </summary>
156         Tile
157     }
158 }