summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/ContextPopup.cs
blob: e812c8c7ca5d92e0146a6fd085e51b28d58eb38d (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
/*
 * 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
{
    public enum ContextPopupDirection
    {
        Down,
        Right,
        Left,
        Up,
        Unknown
    }

    public class ContextPopup : Layout
    {
        HashSet<ContextPopupItem> _children = new HashSet<ContextPopupItem>();
        SmartEvent _dismissed;
        Interop.Evas.SmartCallback _onSelected;

        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();
            };
        }

        public event EventHandler Dismissed;

        public ContextPopupDirection Direction
        {
            get
            {
                return (ContextPopupDirection)Interop.Elementary.elm_ctxpopup_direction_get(RealHandle);
            }
        }

        public bool IsHorizontal
        {
            get
            {
                return Interop.Elementary.elm_ctxpopup_horizontal_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_ctxpopup_horizontal_set(RealHandle, value);
            }
        }

        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);
            }
        }

        public void Clear()
        {
            Interop.Elementary.elm_ctxpopup_clear(Handle);
        }

        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);
        }

        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;
        }

        public ContextPopupItem Append(string label)
        {
            return Append(label, null);
        }

        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;
        }

        public void Dismiss()
        {
            Interop.Elementary.elm_ctxpopup_dismiss(RealHandle);
        }

        public bool IsAvailableDirection(ContextPopupDirection direction)
        {
            return Interop.Elementary.elm_ctxpopup_direction_available_get(RealHandle, (int)direction);
        }

        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);
        }
    }
}