summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/ContextPopup.cs
blob: 3be2baa55acf210eaf17e7126ec941c57315a70d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;

namespace ElmSharp
{
    /// <summary>
    /// Enumeration of ContextPopup direction type.
    /// </summary>
    public enum ContextPopupDirection
    {
        /// <summary>
        /// ContextPopup show appear below clicked area
        /// /// </summary>
        Down,
        /// <summary>
        /// ContextPopup show appear to the right of the clicked area
        /// </summary>
        Right,
        /// <summary>
        /// ContextPopup show appear to the left of the clicked area
        /// </summary>
        Left,
        /// <summary>
        /// ContextPopup show appear above the clicked area
        /// </summary>
        Up,
        /// <summary>
        /// ContextPopup does not determine it's direction yet
        /// </summary>
        Unknown
    }

    /// <summary>
    /// It inherits <see cref="Layout"/>.
    /// The ContextPopup is a widget that when it shown, pops up a list of items.
    /// </summary>
    public class ContextPopup : Layout
    {
        HashSet<ContextPopupItem> _children = new HashSet<ContextPopupItem>();
        SmartEvent _dismissed;
        Interop.Evas.SmartCallback _onSelected;

        /// <summary>
        /// Creates and initializes a new instance of the ContextPopup class.
        /// </summary>
        /// <param name="parent">The parent is a given container which will be attached by ContextPopup
        /// as a child.It's <see cref="EvasObject"/> type.</param>
        public ContextPopup(EvasObject parent) : base(parent)
        {
            _dismissed = new SmartEvent(this, this.RealHandle, "dismissed");
            _dismissed.On += (sender, e) =>
            {
                Dismissed?.Invoke(this, EventArgs.Empty);
            };
            _onSelected = (data, obj, info) =>
            {
                ContextPopupItem item = ItemObject.GetItemById((int)data) as ContextPopupItem;
                item?.SendSelected();
            };
        }

        /// <summary>
        /// Dismissed is raised when the ContextPopup item is dismissed.
        /// </summary>
        /// <remarks>
        /// Outside of ContextPopup was clicked or it's parent area is changed or the language is changed. and then ContextPopup is dismissed.
        /// </remarks>
        public event EventHandler Dismissed;

        /// <summary>
        /// Gets the current direction of a ContextPopup.
        /// </summary>
        /// <remarks>
        /// Once the ContextPopup showed up, the direction would be determined.
        /// </remarks>
        public ContextPopupDirection Direction
        {
            get
            {
                return (ContextPopupDirection)Interop.Elementary.elm_ctxpopup_direction_get(RealHandle);
            }
        }

        /// <summary>
        /// Gets or sets the value of current ContextPopup object's orientation.
        /// True for horizontal mode, False for vertical mode (or errors)
        /// </summary>
        public bool IsHorizontal
        {
            get
            {
                return Interop.Elementary.elm_ctxpopup_horizontal_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_ctxpopup_horizontal_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or sets whether ContextPopup hide automatically
        /// or not when parent of ContextPopup is resized.
        /// </summary>
        /// <remarks>
        /// Default value of AutoHide is False.
        /// </remarks>
        public bool AutoHide
        {
            get
            {
                return !Interop.Elementary.elm_ctxpopup_auto_hide_disabled_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_ctxpopup_auto_hide_disabled_set(RealHandle, !value);
            }
        }

        /// <summary>
        /// Clears all items in the given ContextPopup object.
        /// </summary>
        public void Clear()
        {
            Interop.Elementary.elm_ctxpopup_clear(Handle);
        }

        /// <summary>
        /// Sets the direction priority of a ContextPopup.
        /// </summary>
        /// <param name="first">1st priority of direction </param>
        /// <param name="second">2nd priority of direction </param>
        /// <param name="third">3th priority of direction </param>
        /// <param name="fourth">4th priority of direction</param>
        public void SetDirectionPriorty(ContextPopupDirection first, ContextPopupDirection second, ContextPopupDirection third, ContextPopupDirection fourth)
        {
            Interop.Elementary.elm_ctxpopup_direction_priority_set(RealHandle, (int)first, (int)second, (int)third, (int)fourth);
        }

        /// <summary>
        /// Gets the direction priority of a ContextPopup.
        /// </summary>
        /// <param name="first">1st priority of direction to be returned</param>
        /// <param name="second">2nd priority of direction to be returned</param>
        /// <param name="third">2nd priority of direction to be returned </param>
        /// <param name="fourth">4th priority of direction to be returned</param>
        public void GetDirectionPriority(out ContextPopupDirection first, out ContextPopupDirection second, out ContextPopupDirection third, out ContextPopupDirection fourth)
        {
            int firstOut, secondOut, thirdOut, fourthOut;
            Interop.Elementary.elm_ctxpopup_direction_priority_get(Handle, out firstOut, out secondOut, out thirdOut, out fourthOut);
            first = (ContextPopupDirection)firstOut;
            second = (ContextPopupDirection)secondOut;
            third = (ContextPopupDirection)thirdOut;
            fourth = (ContextPopupDirection)fourthOut;
        }

        /// <summary>
        /// Adds a new item to a ContextPopup object with label.
        /// </summary>
        /// <param name="label">The Label of the new item</param>
        /// <returns>
        /// A ContextPopupItem added or NULL, on errors
        /// </returns>
        public ContextPopupItem Append(string label)
        {
            return Append(label, null);
        }

        /// <summary>
        /// Adds a new item to a ContextPopup object with label and icon.
        /// </summary>
        /// <param name="label">The Label of the new item</param>
        /// <param name="icon">Icon to be set on new item</param>
        /// <returns>A ContextPopupItem added or NULL, on errors</returns>
        public ContextPopupItem Append(string label, EvasObject icon)
        {
            ContextPopupItem item = new ContextPopupItem(label, icon);
            item.Handle = Interop.Elementary.elm_ctxpopup_item_append(RealHandle, label, icon, _onSelected, (IntPtr)item.Id);
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Dismiss a ContextPopup object. The ContextPopup will be hidden and the "clicked" signal will be emitted.
        /// </summary>
        public void Dismiss()
        {
            Interop.Elementary.elm_ctxpopup_dismiss(RealHandle);
        }

        /// <summary>
        /// Gets the possibility that the direction would be available
        /// </summary>
        /// <param name="direction">A direction user wants to check</param>
        /// <returns>
        /// Get false if you cannot put it in the direction. Gets true if it's possible.
        /// </returns>
        public bool IsAvailableDirection(ContextPopupDirection direction)
        {
            return Interop.Elementary.elm_ctxpopup_direction_available_get(RealHandle, (int)direction);
        }

        /// <summary>
        /// Gets Alpha of a default Color Class.
        /// </summary>
        public override int Opacity
        {
            get
            {
                return Color.Default.A;
            }

            set
            {
                Console.WriteLine("ContextPopup instance doesn't support to set Opacity.");
            }
        }

        protected override IntPtr CreateHandle(EvasObject parent)
        {
            return Interop.Elementary.elm_ctxpopup_add(parent.Handle);
        }

        void AddInternal(ContextPopupItem item)
        {
            _children.Add(item);
            item.Deleted += Item_Deleted;
        }

        void Item_Deleted(object sender, EventArgs e)
        {
            _children.Remove((ContextPopupItem)sender);
        }
    }
}