Add MultiButtonEntry
authorSungHyun Min <shyun.min@samsung.com>
Tue, 7 Mar 2017 10:29:11 +0000 (19:29 +0900)
committerSungHyun Min <shyun.min@samsung.com>
Tue, 14 Mar 2017 09:43:49 +0000 (18:43 +0900)
Change-Id: Ifb5ae0a7e34fd9a09642f54307c6ed1d6b93cc05
Signed-off-by: SungHyun Min <shyun.min@samsung.com>
src/ElmSharp/ElmSharp.csproj
src/ElmSharp/ElmSharp/MultiButtonEntry.cs [new file with mode: 0644]
src/ElmSharp/ElmSharp/MultiButtonEntryItem.cs [new file with mode: 0644]
src/ElmSharp/Interop/Interop.Elementary.MultiButtonEntry.cs [new file with mode: 0644]
test/ElmSharp.Test/ElmSharp.Test.csproj
test/ElmSharp.Test/TC/MultibuttonEntryTest1.cs [new file with mode: 0644]

index 4f35a2a..ed31a4f 100755 (executable)
@@ -55,6 +55,8 @@
     <Compile Include="ElmSharp\ColorSelector.cs" />
     <Compile Include="ElmSharp\Conformant.cs" />
     <Compile Include="ElmSharp\Container.cs" />
+    <Compile Include="ElmSharp\MultiButtonEntryItem.cs" />
+    <Compile Include="ElmSharp\MultiButtonEntry.cs" />
     <Compile Include="ElmSharp\ContextPopup.cs" />
     <Compile Include="ElmSharp\ColorSelectorItem.cs" />
     <Compile Include="ElmSharp\ContextPopupItem.cs" />
     <Compile Include="ElmSharp\WrapType.cs" />
     <Compile Include="Interop\Interop.Eina.cs" />
     <Compile Include="Interop\Interop.Elementary.FlipSelector.cs" />
+    <Compile Include="Interop\Interop.Elementary.MultiButtonEntry.cs" />
     <Compile Include="Interop\Interop.Eo.cs" />
     <Compile Include="Interop\Interop.Ecore.cs" />
     <Compile Include="Interop\Interop.Elementary.Bg.cs" />
diff --git a/src/ElmSharp/ElmSharp/MultiButtonEntry.cs b/src/ElmSharp/ElmSharp/MultiButtonEntry.cs
new file mode 100644 (file)
index 0000000..dc377b8
--- /dev/null
@@ -0,0 +1,247 @@
+/*
+ * 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 class MultiButtonEntry : Layout
+    {
+        HashSet<MultiButtonEntryItem> _children = new HashSet<MultiButtonEntryItem>();
+        List<Func<string, bool>> _filters = new List<Func<string, bool>>();
+
+        Interop.Elementary.MultiButtonEntryItemFilterCallback _filtercallback;
+
+        SmartEvent _clicked;
+        SmartEvent _expanded;
+        SmartEvent _contracted;
+        SmartEvent _expandedStateChanged;
+        SmartEvent<MultiButtonEntryArgs> _itemSelected;
+        SmartEvent<MultiButtonEntryArgs> _itemClicked;
+        SmartEvent<MultiButtonEntryArgs> _itemLongPressed;
+        SmartEvent<MultiButtonEntryArgs> _itemAdded;
+
+        public MultiButtonEntry(EvasObject parent) : base(parent)
+        {
+            _clicked = new SmartEvent(this, "clicked");
+            _expanded = new SmartEvent(this, "expanded");
+            _contracted = new SmartEvent(this, "contracted");
+            _expandedStateChanged = new SmartEvent(this, "expand,state,changed");
+
+            _itemSelected = new SmartEvent<MultiButtonEntryArgs>(this, "item,selected", MultiButtonEntryArgs.CreateFromSmartEvent);
+            _itemClicked = new SmartEvent<MultiButtonEntryArgs>(this, "item,clicked", MultiButtonEntryArgs.CreateFromSmartEvent);
+            _itemLongPressed = new SmartEvent<MultiButtonEntryArgs>(this, "item,longpressed", MultiButtonEntryArgs.CreateFromSmartEvent);
+            _itemAdded = new SmartEvent<MultiButtonEntryArgs>(this, "item,added", MultiButtonEntryArgs.CreateAndAddFromSmartEvent);
+
+            _filtercallback = new Interop.Elementary.MultiButtonEntryItemFilterCallback(FilterCallbackHandler);
+
+            _clicked.On += (sender, e) => Clicked?.Invoke(this, EventArgs.Empty);
+            _expanded.On += (sender, e) => Expanded?.Invoke(this, EventArgs.Empty);
+            _contracted.On += (sender, e) => Contracted?.Invoke(this, EventArgs.Empty);
+            _expandedStateChanged.On += (sender, e) => ExpandedStateChanged?.Invoke(this, EventArgs.Empty);
+
+            _itemSelected.On += (sender, e) => { ItemSelected?.Invoke(this, e); };
+            _itemClicked.On += (sender, e) => { ItemClicked?.Invoke(this, e); };
+            _itemLongPressed.On += (sender, e) => { ItemLongPressed?.Invoke(this, e); };
+            _itemAdded.On += OnItemAdded;
+        }
+
+        public event EventHandler Clicked;
+
+        public event EventHandler Expanded;
+
+        public event EventHandler Contracted;
+
+        public event EventHandler ExpandedStateChanged;
+
+        public event EventHandler<MultiButtonEntryArgs> ItemSelected;
+
+        public event EventHandler<MultiButtonEntryArgs> ItemClicked;
+
+        public event EventHandler<MultiButtonEntryArgs> ItemLongPressed;
+
+        public event EventHandler<MultiButtonEntryArgs> ItemAdded;
+
+        public event EventHandler<MultiButtonEntryArgs> ItemDeleted;
+
+        public MultiButtonEntryItem SelectedItem
+        {
+            get
+            {
+                IntPtr handle = Interop.Elementary.elm_multibuttonentry_selected_item_get(Handle);
+                return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            }
+        }
+
+        public bool IsEditable
+        {
+            get
+            {
+                return Interop.Elementary.elm_multibuttonentry_editable_get(Handle);
+            }
+            set
+            {
+                Interop.Elementary.elm_multibuttonentry_editable_set(Handle, value);
+            }
+        }
+
+        public bool IsExpanded
+        {
+            get
+            {
+                return Interop.Elementary.elm_multibuttonentry_expanded_get(Handle);
+            }
+            set
+            {
+                Interop.Elementary.elm_multibuttonentry_expanded_set(Handle, value);
+            }
+        }
+
+        public MultiButtonEntryItem FirstItem
+        {
+            get
+            {
+                IntPtr handle = Interop.Elementary.elm_multibuttonentry_first_item_get(Handle);
+                return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            }
+        }
+
+        public MultiButtonEntryItem LastItem
+        {
+            get
+            {
+                IntPtr handle = Interop.Elementary.elm_multibuttonentry_last_item_get(Handle);
+                return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            }
+        }
+
+        protected override IntPtr CreateHandle(EvasObject parent)
+        {
+            return Interop.Elementary.elm_multibuttonentry_add(parent.Handle);
+        }
+
+        public MultiButtonEntryItem Append(string label)
+        {
+            var handle = Interop.Elementary.elm_multibuttonentry_item_append(Handle, label, null, IntPtr.Zero);
+            MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            return item;
+        }
+
+        public MultiButtonEntryItem Prepend(string label)
+        {
+            var handle = Interop.Elementary.elm_multibuttonentry_item_prepend(Handle, label, null, IntPtr.Zero);
+            MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            return item;
+        }
+
+        public MultiButtonEntryItem InsertBefore(MultiButtonEntryItem before, string label)
+        {
+            var handle = Interop.Elementary.elm_multibuttonentry_item_insert_before(Handle, before.Handle, label, null, IntPtr.Zero);
+            MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            return item;
+        }
+
+        public MultiButtonEntryItem InsertAfter(MultiButtonEntryItem after, string label)
+        {
+            var handle = Interop.Elementary.elm_multibuttonentry_item_insert_after(Handle, after.Handle, label, null, IntPtr.Zero);
+            MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
+            return item;
+        }
+
+        public void Clear()
+        {
+            Interop.Elementary.elm_multibuttonentry_clear(Handle);
+            _children.Clear();
+        }
+
+        public void AppendFilter(Func<string, bool> func)
+        {
+            _filters.Add(func);
+            if (_filters.Count == 1)
+            {
+                Interop.Elementary.elm_multibuttonentry_item_filter_append(Handle, _filtercallback, IntPtr.Zero);
+            }
+        }
+
+        public void PrependFilter(Func<string, bool> func)
+        {
+            _filters.Insert(0, func);
+            if (_filters.Count == 1)
+            {
+                Interop.Elementary.elm_multibuttonentry_item_filter_prepend(Handle, _filtercallback, IntPtr.Zero);
+            }
+        }
+
+        public void RemoveFilter(Func<string, bool> func)
+        {
+            _filters.Remove(func);
+            if (_filters.Count == 0)
+            {
+                Interop.Elementary.elm_multibuttonentry_item_filter_remove(Handle, _filtercallback, IntPtr.Zero);
+            }
+        }
+
+        void Item_Deleted(object sender, EventArgs e)
+        {
+            var removed = sender as MultiButtonEntryItem;
+            _children.Remove(removed);
+
+            // "item,deleted" event will be called after removing the item from ItemObject has been done.
+            // ItemObject will no longer have the item instance that is deleted after this.
+            // So, ItemDelete event with the removed item should be triggered here.
+            ItemDeleted?.Invoke(this, new MultiButtonEntryArgs() { Item = removed });
+        }
+
+        void OnItemAdded(object sender, MultiButtonEntryArgs e)
+        {
+            _children.Add(e.Item);
+            e.Item.Deleted += Item_Deleted;
+            ItemAdded?.Invoke(this, e);
+        }
+
+        bool FilterCallbackHandler(IntPtr obj, string label, IntPtr itemData, IntPtr data)
+        {
+            foreach (var func in _filters)
+            {
+                if (!func(label))
+                    return false;
+            }
+            return true;
+        }
+    }
+
+    public class MultiButtonEntryArgs : EventArgs
+    {
+        public MultiButtonEntryItem Item { get; set; }
+
+        internal static MultiButtonEntryArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
+        {
+            MultiButtonEntryItem item = ItemObject.GetItemByHandle(info) as MultiButtonEntryItem;
+            return new MultiButtonEntryArgs() { Item = item };
+        }
+
+        internal static MultiButtonEntryArgs CreateAndAddFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
+        {
+            // Item can be added throught calling Append method and user input.
+            // And since "item.added" event will be called before xx_append() method returns,
+            // ItemObject does NOT have an item that contains handle matched to "info" at this time.
+            // So, item should be created and added internally here.
+            MultiButtonEntryItem item = new MultiButtonEntryItem(info);
+            return new MultiButtonEntryArgs() { Item = item };
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/ElmSharp/ElmSharp/MultiButtonEntryItem.cs b/src/ElmSharp/ElmSharp/MultiButtonEntryItem.cs
new file mode 100644 (file)
index 0000000..2cce78e
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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;
+
+namespace ElmSharp
+{
+    public class MultiButtonEntryItem : ItemObject
+    {
+        public MultiButtonEntryItem(string text) : base(IntPtr.Zero)
+        {
+            Label = text;
+        }
+
+        internal MultiButtonEntryItem(IntPtr handle) : base(handle)
+        {
+            Label = Interop.Elementary.elm_object_item_part_text_get(handle, null);
+        }
+
+        public string Label { get; private set; }
+
+        public bool IsSelected
+        {
+            get
+            {
+                return Interop.Elementary.elm_multibuttonentry_item_selected_get(Handle);
+            }
+            set
+            {
+                Interop.Elementary.elm_multibuttonentry_item_selected_set(Handle, value);
+            }
+        }
+
+        public MultiButtonEntryItem Next
+        {
+            get
+            {
+                var next = Interop.Elementary.elm_multibuttonentry_item_next_get(Handle);
+                return ItemObject.GetItemByHandle(next) as MultiButtonEntryItem;
+            }
+        }
+
+        public MultiButtonEntryItem Prev
+        {
+            get
+            {
+                var prev = Interop.Elementary.elm_multibuttonentry_item_prev_get(Handle);
+                return ItemObject.GetItemByHandle(prev) as MultiButtonEntryItem;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/ElmSharp/Interop/Interop.Elementary.MultiButtonEntry.cs b/src/ElmSharp/Interop/Interop.Elementary.MultiButtonEntry.cs
new file mode 100644 (file)
index 0000000..cf9e0db
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    internal static partial class Elementary
+    {
+        public delegate bool MultiButtonEntryItemFilterCallback(IntPtr obj, string label, IntPtr itemData, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_add(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_entry_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern bool elm_multibuttonentry_expanded_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_expanded_set(IntPtr obj, bool expanded);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern bool elm_multibuttonentry_editable_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_editable_set(IntPtr obj, bool editable);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_prepend(IntPtr obj, string label, Evas.SmartCallback func, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_append(IntPtr obj, string label, Evas.SmartCallback func, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_insert_before(IntPtr obj, IntPtr before, string label, Evas.SmartCallback func, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_insert_after(IntPtr obj, IntPtr after, string label, Evas.SmartCallback func, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_items_get(IntPtr obj); //return list
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_first_item_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_last_item_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_selected_item_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_item_selected_set(IntPtr obj, bool selected);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern bool elm_multibuttonentry_item_selected_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_clear(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_prev_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern IntPtr elm_multibuttonentry_item_next_get(IntPtr obj);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_item_filter_append(IntPtr obj, MultiButtonEntryItemFilterCallback callback, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_item_filter_prepend(IntPtr obj, MultiButtonEntryItemFilterCallback callback, IntPtr data);
+
+        [DllImport(Libraries.Elementary)]
+        internal static extern void elm_multibuttonentry_item_filter_remove(IntPtr obj, MultiButtonEntryItemFilterCallback callback, IntPtr data);
+    }
+}
\ No newline at end of file
index 74232bd..b71210f 100755 (executable)
@@ -43,6 +43,7 @@
     <Compile Include="TC\BackgroundTest1.cs" />
     <Compile Include="TC\BackgroundTest2.cs" />
     <Compile Include="TC\BackgroundTest3.cs" />
+    <Compile Include="TC\MultibuttonEntryTest1.cs" />
     <Compile Include="TC\DateTimeSelectorTest2.cs" />
     <Compile Include="TC\EntryTest2.cs" />
     <Compile Include="TC\FlipSelectorTest.cs" />
diff --git a/test/ElmSharp.Test/TC/MultibuttonEntryTest1.cs b/test/ElmSharp.Test/TC/MultibuttonEntryTest1.cs
new file mode 100644 (file)
index 0000000..b27f0d2
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * 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;
+using ElmSharp;
+
+namespace ElmSharp.Test
+{
+    class MultiButtonEntryTest1 : TestCaseBase
+    {
+        public override string TestName => "MultiButtonEntryTest1";
+        public override string TestDescription => "To test basic operation of MultiButtonEntry";
+
+        public override void Run(Window window)
+        {
+            Background bg = new Background(window);
+            bg.Color = Color.White;
+            bg.Move(0, 0);
+            bg.Resize(window.ScreenSize.Width, window.ScreenSize.Height);
+            bg.Show();
+
+            MultiButtonEntry mbe = new MultiButtonEntry(window)
+            {
+                IsEditable = true,
+                IsExpanded = true,
+                Text = "To: "
+            };
+
+            var test = mbe.Append("test");
+            mbe.Prepend("prepend");
+            mbe.Append("append");
+            mbe.InsertBefore(test, "insertBefore");
+            mbe.InsertAfter(test, "insertAfter");
+
+            mbe.ItemSelected += (s, e) =>
+            {
+                Console.WriteLine("item selected: " + e.Item.Label);
+                if (e.Item.Next != null)
+                    Console.WriteLine("next item: " + e.Item.Next);
+                if (e.Item.Prev != null)
+                    Console.WriteLine("next item: " + e.Item.Prev);
+            };
+
+            mbe.ItemClicked += (s, e) =>
+            {
+                Console.WriteLine("item clicked: " + e.Item.Label);
+            };
+
+            mbe.ItemLongPressed += (s, e) =>
+            {
+                Console.WriteLine("item longpressed: " + e.Item.Label);
+            };
+
+            mbe.ItemAdded += (s, e) =>
+            {
+                Console.WriteLine("item added: " + e.Item.Label);
+            };
+
+            mbe.ItemDeleted += (s, e) =>
+            {
+                Console.WriteLine("item deleted: " + e.Item.Label);
+            };
+
+            mbe.AppendFilter((label) =>
+            {
+                if (label.Contains("a"))
+                {
+                    Console.WriteLine("appended filter : Item has 'a', It won't be added until 'a' is removed.");
+                    return false;
+                }
+                else
+                {
+                    return true;
+                }
+            });
+
+            mbe.PrependFilter((label) =>
+            {
+                if (label.Contains("p"))
+                {
+                    Console.WriteLine("prepended filter : Item has 'p', It won't be added until 'p' is removed.");
+                    return false;
+                }
+                else
+                {
+                    return true;
+                }
+            });
+
+            Label label1 = new Label(window)
+            {
+                Text = "MultiButtonEntry Test",
+                Color = Color.Blue
+            };
+
+            label1.Resize(600, 100);
+            label1.Move(50, 50);
+            label1.Show();
+
+            mbe.Resize(600, 600);
+            mbe.Move(0, 100);
+            mbe.Show();
+        }
+    }
+}
\ No newline at end of file