Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Hoversel.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 HoverselItemEventArgs is an HoverselItem's EventArgs
23     /// </summary>
24     public class HoverselItemEventArgs : EventArgs
25     {
26         /// <summary>
27         /// Hoversel's Item
28         /// </summary>
29         public HoverselItem Item { get; set; }
30
31         internal static HoverselItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
32         {
33             HoverselItem item = ItemObject.GetItemByHandle(info) as HoverselItem;
34             return new HoverselItemEventArgs() { Item = item };
35         }
36     }
37
38     /// <summary>
39     /// The hoversel is a button that pops up a list of items.
40     /// </summary>
41     public class Hoversel : Layout
42     {
43         SmartEvent _clicked;
44         SmartEvent _expanded;
45         SmartEvent _dismissed;
46         SmartEvent<HoverselItemEventArgs> _selected;
47         Interop.Evas.SmartCallback _onItemSelected;
48
49         /// <summary>
50         /// Creates and initializes a new instance of the Hoversel class.
51         /// </summary>
52         /// <param name="parent">The parent is a given container which will be attached by Hoversel as a child. It's <see cref="EvasObject"/> type.</param>
53         public Hoversel(EvasObject parent) : base(parent)
54         {
55             _clicked = new SmartEvent(this, "clicked");
56             _clicked.On += (sender, e) =>
57             {
58                 Clicked?.Invoke(this, EventArgs.Empty);
59             };
60             _expanded = new SmartEvent(this, "expanded");
61             _expanded.On += (sender, e) =>
62             {
63                 Expanded?.Invoke(this, EventArgs.Empty);
64             };
65             _dismissed = new SmartEvent(this, "dismissed");
66             _dismissed.On += (sender, e) =>
67             {
68                 Dismissed?.Invoke(this, EventArgs.Empty);
69             };
70             _selected = new SmartEvent<HoverselItemEventArgs>(this, RealHandle, "selected", HoverselItemEventArgs.CreateFromSmartEvent);
71             _selected.On += (s, e) =>
72             {
73                 if (e.Item != null) ItemSelected?.Invoke(this, e);
74             };
75             _onItemSelected = (data, obj, info) =>
76             {
77                 HoverselItem item = ItemObject.GetItemById((int)data) as HoverselItem;
78                 item?.SendItemSelected();
79             };
80         }
81
82         /// <summary>
83         /// Clicked will be triggered when Hoversel is clicked
84         /// </summary>
85         public event EventHandler Clicked;
86
87         /// <summary>
88         /// Expanded will be triggered when Hoversel is activated by clicking the hoversel or by a function
89         /// </summary>
90         public event EventHandler Expanded;
91
92         /// <summary>
93         /// Dismissed will be triggered when Hoversel Dismissed
94         /// </summary>
95         public event EventHandler Dismissed;
96
97         /// <summary>
98         /// ItemSelected will be triggered when Hoversel's Item Selected
99         /// </summary>
100         public event EventHandler<HoverselItemEventArgs> ItemSelected;
101
102         /// <summary>
103         /// Gets or sets the status to control whether the hoversel should expand horizontally.
104         /// </summary>
105         public bool IsHorizontal
106         {
107             get
108             {
109                 return Interop.Elementary.elm_hoversel_horizontal_get(RealHandle);
110             }
111             set
112             {
113                 Interop.Elementary.elm_hoversel_horizontal_set(RealHandle, value);
114             }
115         }
116
117         /// <summary>
118         /// Gets or sets the hover parent.
119         /// </summary>
120         public IntPtr HoverParent
121         {
122             get
123             {
124                 return Interop.Elementary.elm_hoversel_hover_parent_get(RealHandle);
125             }
126             set
127             {
128                 Interop.Elementary.elm_hoversel_hover_parent_set(RealHandle, value);
129             }
130         }
131
132         /// <summary>
133         /// Gets the flag of whether the hoversel is expanded.
134         /// </summary>
135         public bool IsExpanded
136         {
137             get
138             {
139                 return Interop.Elementary.elm_hoversel_expanded_get(RealHandle);
140             }
141         }
142
143         /// <summary>
144         /// Gets or sets the status of whether update icon and text of hoversel same to those of selected item automatically.
145         /// </summary>
146         public bool AutoUpdate
147         {
148             get
149             {
150                 return Interop.Elementary.elm_hoversel_auto_update_get(RealHandle);
151             }
152             set
153             {
154                 Interop.Elementary.elm_hoversel_auto_update_set(RealHandle, value);
155             }
156         }
157
158         /// <summary>
159         /// This triggers the hoversel popup from code, the same as if the user had clicked the button.
160         /// </summary>
161         public void HoverBegin()
162         {
163             Interop.Elementary.elm_hoversel_hover_begin(RealHandle);
164         }
165
166         /// <summary>
167         /// This dismisses the hoversel popup as if the user had clicked outside the hover.
168         /// </summary>
169         public void HoverEnd()
170         {
171             Interop.Elementary.elm_hoversel_hover_end(RealHandle);
172         }
173
174         /// <summary>
175         /// This will remove all the children items from the hoversel.
176         /// </summary>
177         public void Clear()
178         {
179             Interop.Elementary.elm_hoversel_clear(RealHandle);
180         }
181
182         /// <summary>
183         /// Add an item to the hoversel button.
184         /// This adds an item to the hoversel to show when it is clicked.
185         /// </summary>
186         /// <param name="label">Item's label</param>
187         /// <returns>A handle to the added item.</returns>
188         public HoverselItem AddItem(string label)
189         {
190             HoverselItem item = new HoverselItem();
191             item.Label = label;
192             item.Handle = Interop.Elementary.elm_hoversel_item_add(RealHandle, label, null, 0, _onItemSelected, (IntPtr)item.Id);
193             return item;
194         }
195
196         protected override IntPtr CreateHandle(EvasObject parent)
197         {
198             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
199             Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
200
201             RealHandle = Interop.Elementary.elm_hoversel_add(handle);
202             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
203
204             return handle;
205         }
206     }
207 }