2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
24 /// Enumeration for describing InputPanel layout type.
26 public enum InputPanelLayout
29 /// InputPanel layout type default.
34 /// InputPanel layout type number.
39 /// InputPanel layout type email.
44 /// InputPanel layout type url.
49 /// InputPanel layout type phone.
54 /// InputPanel layout type ip.
59 /// InputPanel layout type month.
64 /// InputPanel layout type number.
69 /// InputPanel layout type error type. Do not use it directly!
74 /// InputPanel layout type hexadecimal.
79 /// InputPanel layout type terminal type, esc, alt, ctrl, etc.
84 /// InputPanel layout type password.
89 /// Keyboard layout type date and time.
94 /// InputPanel layout type emoticons.
100 /// Enumeration that defines the "Return" key types on the input panel (virtual keyboard).
102 public enum InputPanelReturnKeyType
135 /// Search string or magnifier icon key type
151 /// Enumeration that defines the autocapitalization types.
153 public enum AutoCapital
156 /// No autocapitalization when typing
161 /// Autocapitalize each typed word
166 /// Autocapitalize the start of each sentence
171 /// Autocapitalize all letters
177 /// Enumeration that defines the entry's copy & paste policy.
179 public enum CopyAndPasteMode
182 /// Copy & paste text with markup tag
187 /// Copy & paste text without item(image) tag
192 /// Copy & paste text without markup tag
198 /// Enumeration that defines the text format types.
200 public enum TextFormat
214 /// Enumeration that defines the types of Input Hints.
216 public enum InputHints
224 /// suggest word auto completion
229 /// typed text should not be stored.
235 /// Enumeration that defines the input panel (virtual keyboard) language modes.
237 public enum InputPanelLanguage
240 /// Automatic language mode
245 /// Alphabet language mode
251 /// The entry is a convenience widget that shows a box in which the user can enter text.
253 public class Entry : Layout
256 SmartEvent _changedByUser;
257 SmartEvent _cursorChanged;
258 SmartEvent _activated;
260 Dictionary<Func<string, EvasObject>, Interop.Elementary.Elm_Entry_Item_Provider_Cb> _itemsProvider = new Dictionary<Func<string, EvasObject>, Interop.Elementary.Elm_Entry_Item_Provider_Cb>();
261 Dictionary<Func<Entry, string, string>, Interop.Elementary.Elm_Entry_Filter_Cb> _textFilters = new Dictionary<Func<Entry, string, string>, Interop.Elementary.Elm_Entry_Filter_Cb>();
264 /// Creates and initializes a new instance of the Entry class.
266 /// <param name="parent">The EvasObject to which the new Entry will be attached as a child.</param>
267 public Entry(EvasObject parent) : base(parent)
269 _clicked = new SmartEvent(this, this.RealHandle, "clicked");
270 _clicked.On += (s, e) => Clicked?.Invoke(this, EventArgs.Empty);
272 _changedByUser = new SmartEvent(this, this.RealHandle, "changed,user");
273 _changedByUser.On += (s, e) => ChangedByUser?.Invoke(this, EventArgs.Empty);
275 _cursorChanged = new SmartEvent(this, this.RealHandle, "cursor,changed");
276 _cursorChanged.On += (s, e) => CursorChanged?.Invoke(this, EventArgs.Empty);
278 _activated = new SmartEvent(this, this.RealHandle, "activated");
279 _activated.On += (s, e) => Activated?.Invoke(this, EventArgs.Empty);
283 /// Activated will be triggered when the entry in Activated stated.
285 public event EventHandler Activated;
288 /// Clicked will be triggered when the entry is clicked.
290 public event EventHandler Clicked;
293 /// ChangedByUser will be triggered when the entry changed by user.
295 public event EventHandler ChangedByUser;
298 /// CursorChanged will be triggered when the Cursor in the entry is changed.
300 public event EventHandler CursorChanged;
303 /// Sets or gets the entry to the single line mode.
305 public bool IsSingleLine
309 return Interop.Elementary.elm_entry_single_line_get(RealHandle);
313 Interop.Elementary.elm_entry_single_line_set(RealHandle, value);
318 /// Sets or gets the entry to the password mode.
320 public bool IsPassword
324 return Interop.Elementary.elm_entry_password_get(RealHandle);
328 Interop.Elementary.elm_entry_password_set(RealHandle, value);
333 /// Sets or gets whether the entry is editable.
335 public bool IsEditable
339 return Interop.Elementary.elm_entry_editable_get(RealHandle);
343 Interop.Elementary.elm_entry_editable_set(RealHandle, value);
348 /// Sets or gets whether the entry is empty.
354 return Interop.Elementary.elm_entry_is_empty(RealHandle);
359 /// Sets or gets text currently shown in the object entry.
361 public override string Text
365 return Interop.Elementary.elm_entry_entry_get(RealHandle);
369 Interop.Elementary.elm_entry_entry_set(RealHandle, value);
374 /// Sets or gets the style on the top of the user style stack.
376 /// <remarks>If there is styles in the user style stack, the properties in the top style of user style stack will replace the properties in current theme. The input style is specified in format tag='property=value' (i.e. DEFAULT='font=Sans font_size=60'hilight=' + font_weight=Bold').</remarks>
377 public string TextStyle
381 return Interop.Elementary.elm_entry_text_style_user_peek(RealHandle);
385 Interop.Elementary.elm_entry_text_style_user_push(RealHandle, value);
390 /// Sets or gets the current position of the cursor in the entry.
392 public int CursorPosition
396 return Interop.Elementary.elm_entry_cursor_pos_get(RealHandle);
400 Interop.Elementary.elm_entry_cursor_pos_set(RealHandle, value);
405 /// Sets or gets the scrollable state of the entry.
407 public bool Scrollable
411 return Interop.Elementary.elm_entry_scrollable_get(RealHandle);
415 // HACK: Enabling the scrollable property of an entry causes its internal
416 // hierarchy to change, making the internal edje object inaccessible.
417 // Access it before the property is set, to cache the edje object's handle.
420 var dummy = EdjeObject;
422 Interop.Elementary.elm_entry_scrollable_set(RealHandle, value);
427 /// Sets or Gets the autocapitalization type on the immodule.
429 public AutoCapital AutoCapital
433 return (AutoCapital)Interop.Elementary.elm_entry_autocapital_type_get(RealHandle);
437 Interop.Elementary.elm_entry_autocapital_type_set(RealHandle, (Interop.Elementary.AutocapitalType)value);
442 /// Sets or Gets the entry object's 'autosave' status.
444 public bool IsAutoSave
448 return Interop.Elementary.elm_entry_autosave_get(RealHandle);
452 Interop.Elementary.elm_entry_autosave_set(RealHandle, value);
457 /// Sets or Gets entry text paste/drop mode.
459 public CopyAndPasteMode CopyAndPasteMode
463 return (CopyAndPasteMode)Interop.Elementary.elm_entry_cnp_mode_get(RealHandle);
467 Interop.Elementary.elm_entry_cnp_mode_set(RealHandle, (Interop.Elementary.CopyAndPasteMode)value);
472 /// Gets the geometry of the cursor.
474 public Rect CursorGeometry
479 Interop.Elementary.elm_entry_cursor_geometry_get(RealHandle, out x, out y, out w, out h);
480 return new Rect(x, y, w, h);
485 /// Gets whether a format node exists at the current cursor position.
487 public bool IsCursorFormat
491 return Interop.Elementary.elm_entry_cursor_is_format_get(RealHandle);
496 /// Gets if the current cursor position holds a visible format node.
498 public bool IsCursorVisibelFormat
502 return Interop.Elementary.elm_entry_cursor_is_visible_format_get(RealHandle);
507 /// Sets or Gets the value of input hint.
509 public InputHints InputHint
513 return (InputHints)Interop.Elementary.elm_entry_input_hint_get(RealHandle);
517 Interop.Elementary.elm_entry_input_hint_set(RealHandle, (Interop.Elementary.InputHints)value);
522 /// Sets or gets the language mode of the input panel.
524 public InputPanelLanguage InputPanelLanguage
528 return (InputPanelLanguage)Interop.Elementary.elm_entry_input_panel_language_get(RealHandle);
532 Interop.Elementary.elm_entry_input_panel_language_set(RealHandle, (Interop.Elementary.InputPanelLanguage)value);
537 /// Sets or gets the input panel layout variation of the entry.
539 public int InputPanelVariation
543 return Interop.Elementary.elm_entry_input_panel_layout_variation_get(RealHandle);
547 Interop.Elementary.elm_entry_input_panel_layout_variation_set(RealHandle, value);
552 /// Sets or gets the line wrap type to use on multi-line entries.
554 public WrapType LineWrapType
558 return (WrapType)Interop.Elementary.elm_entry_line_wrap_get(RealHandle);
562 Interop.Elementary.elm_entry_line_wrap_set(RealHandle, (Interop.Elementary.WrapType)value);
567 /// Sets or gets whether the entry should allow to use the text prediction.
569 public bool PredictionAllowed
573 return Interop.Elementary.elm_entry_prediction_allow_get(RealHandle);
577 Interop.Elementary.elm_entry_prediction_allow_set(RealHandle, value);
582 /// Sets or gets whether the return key on the input panel should be disabled or not.
584 public bool InputPanelReturnKeyDisabled
588 return Interop.Elementary.elm_entry_input_panel_return_key_disabled_get(RealHandle);
592 Interop.Elementary.elm_entry_input_panel_return_key_disabled_set(RealHandle, value);
597 /// Sets or gets the attribute to show the input panel in case of only an user's explicit Mouse Up event.
598 /// It doesn't request to show the input panel even though it has focus.
599 /// If true, the input panel will be shown in case of only Mouse up event. (Focus event will be ignored.)
601 public bool InputPanelShowByOnDemand
605 return Interop.Elementary.elm_entry_input_panel_show_on_demand_get(RealHandle);
609 Interop.Elementary.elm_entry_input_panel_show_on_demand_set(RealHandle, value);
614 /// Sets the file (and implicitly loads it) for the text to display and then edit.
616 /// <param name="file">The path to the file to load and save</param>
617 /// <param name="textFormat">The file format</param>
618 public void SetFile(string file, TextFormat textFormat)
620 Interop.Elementary.elm_entry_file_set(RealHandle, file, (Interop.Elementary.TextFormat)textFormat);
624 /// Converts a markup (HTML-like) string into UTF-8.
626 /// <param name="markup">The string (in markup) to be converted</param>
627 /// <returns>The converted string (in UTF-8) </returns>
628 public static string ConvertMarkupToUtf8(string markup)
630 return Interop.Elementary.elm_entry_markup_to_utf8(markup);
634 /// Moves the cursor by one position to the right within the entry.
636 /// <returns></returns>
637 public bool MoveCursorNext()
639 return Interop.Elementary.elm_entry_cursor_next(RealHandle);
643 /// Moves the cursor one place to the left within the entry.
645 /// <returns>TRUE on success, otherwise FALSE on failure</returns>
646 public bool MoveCursorPrev()
648 return Interop.Elementary.elm_entry_cursor_prev(RealHandle);
652 /// Moves the cursor one line up within the entry.
654 /// <returns>TRUE on success, otherwise FALSE on failure</returns>
655 public bool MoveCursorUp()
657 return Interop.Elementary.elm_entry_cursor_up(RealHandle);
661 /// Moves the cursor one line down within the entry.
663 /// <returns>TRUE on success, otherwise FALSE on failure</returns>
664 public bool MoveCursorDown()
666 return Interop.Elementary.elm_entry_cursor_down(RealHandle);
670 /// Moves the cursor to the beginning of the entry.
672 public void MoveCursorBegin()
674 Interop.Elementary.elm_entry_cursor_begin_set(RealHandle);
678 /// Moves the cursor to the end of the entry.
680 public void MoveCursorEnd()
682 Interop.Elementary.elm_entry_cursor_end_set(RealHandle);
686 /// Moves the cursor to the beginning of the current line.
688 public void MoveCursorLineBegin()
690 Interop.Elementary.elm_entry_cursor_line_begin_set(RealHandle);
694 /// Moves the cursor to the end of the current line.
696 public void MoveCursorLineEnd()
698 Interop.Elementary.elm_entry_cursor_line_end_set(RealHandle);
702 /// Sets the input panel layout of the entry.
704 /// <param name="layout">The layout type</param>
705 public void SetInputPanelLayout(InputPanelLayout layout)
707 Interop.Elementary.elm_entry_input_panel_layout_set(RealHandle, (Interop.Elementary.InputPanelLayout)layout);
711 /// Sets the attribute to show the input panel automatically.
713 /// <param name="enabled">If true the input panel appears when the entry is clicked or has focus, otherwise false</param>
714 public void SetInputPanelEnabled(bool enabled)
716 Interop.Elementary.elm_entry_input_panel_enabled_set(RealHandle, enabled);
720 /// Sets the "return" key type. This type is used to set the string or icon on the "return" key of the input panel.
722 /// <param name="keyType">The type of "return" key on the input panel</param>
723 public void SetInputPanelReturnKeyType(InputPanelReturnKeyType keyType)
725 Interop.Elementary.elm_entry_input_panel_return_key_type_set(RealHandle, (Interop.Elementary.ReturnKeyType)keyType);
729 /// Hides the input panel (virtual keyboard).
732 /// Note that the input panel is shown or hidden automatically according to the focus state of the entry widget.
733 /// This API can be used in case of manually controlling by using SetInputPanelEnabled(false).
735 public void HideInputPanel()
737 Interop.Elementary.elm_entry_input_panel_hide(RealHandle);
741 /// Selects all the text within the entry.
743 public void SelectAll()
745 Interop.Elementary.elm_entry_select_all(RealHandle);
749 /// Drops any existing text selection within the entry.
751 public void SelectNone()
753 Interop.Elementary.elm_entry_select_none(RealHandle);
757 /// Sets the color of color class for a given widget.
759 /// <param name="part">The name of color class.</param>
760 /// <param name="color">The struct of color</param>
761 public override void SetPartColor(string part, Color color)
763 IntPtr handle = (part == "bg") ? Handle : RealHandle;
764 Interop.Elementary.elm_object_color_class_color_set(handle, part, color.R * color.A / 255,
765 color.G * color.A / 255,
766 color.B * color.A / 255,
771 /// Forces calculation of the entry size and text layouting.
773 public void ForceCalculation()
775 Interop.Elementary.elm_entry_calc_force(RealHandle);
779 /// Gets the string by the cursor at its current position.
781 /// <returns></returns>
782 public string GetCursorContent()
784 return Interop.Elementary.elm_entry_cursor_content_get(RealHandle);
788 /// Begins a selection within the entry as though the user were holding down the mouse button to make a selection.
790 public void BeginCursorSelection()
792 Interop.Elementary.elm_entry_cursor_selection_begin(RealHandle);
796 /// Appends the text of the entry.
798 /// <param name="text">The text to be displayed</param>
799 public void AppendText(string text)
801 Interop.Elementary.elm_entry_entry_append(RealHandle, text);
805 /// Inserts the given text into the entry at the current cursor position.
807 /// <param name="text"></param>
808 public void InsertTextToCursor(string text)
810 Interop.Elementary.elm_entry_entry_insert(RealHandle, text);
814 /// Ends a selection within the entry as though the user had just released the mouse button while making a selection.
816 public void EndCursorSelection()
818 Interop.Elementary.elm_entry_cursor_selection_end(RealHandle);
822 /// Writes any changes made to the file that is set by File.
824 public void SaveFile()
826 Interop.Elementary.elm_entry_file_save(RealHandle);
830 /// Show the input panel (virtual keyboard) based on the input panel property of entry such as layout, autocapital types, and so on.
833 /// Note that input panel is shown or hidden automatically according to the focus state of entry widget.
834 /// This API can be used in the case of manually controlling by using SetInputPanelEnabled(false).
836 public void ShowInputPanel()
838 Interop.Elementary.elm_entry_input_panel_show(RealHandle);
842 /// This appends a custom item provider to the list for that entry.
844 /// <param name="func">This function is used to provide items.</param>
845 public void AppendItemProvider(Func<string, EvasObject> func)
849 if (!_itemsProvider.ContainsKey(func))
851 Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback;
853 itemProviderCallback = (d, o, t) =>
855 return func?.Invoke(t);
857 Interop.Elementary.elm_entry_item_provider_append(RealHandle, itemProviderCallback, IntPtr.Zero);
858 _itemsProvider.Add(func, itemProviderCallback);
864 /// This prepends a custom item provider to the list for that entry.
866 /// <param name="func">This function is used to provide items.</param>
867 public void PrependItemProvider(Func<string, EvasObject> func)
869 if (!_itemsProvider.ContainsKey(func))
871 Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback;
873 itemProviderCallback = (d, o, t) =>
875 return func?.Invoke(t);
877 Interop.Elementary.elm_entry_item_provider_prepend(RealHandle, itemProviderCallback, IntPtr.Zero);
878 _itemsProvider.Add(func, itemProviderCallback);
883 /// This removes a custom item provider to the list for that entry.
885 /// <param name="itemProvider">This function is used to provide items.</param>
886 public void RemoveItemProvider(Func<string, EvasObject> func)
888 if (_itemsProvider.ContainsKey(func))
890 Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback;
891 _itemsProvider.TryGetValue(func, out itemProviderCallback);
893 Interop.Elementary.elm_entry_item_provider_remove(RealHandle, itemProviderCallback, IntPtr.Zero);
894 _itemsProvider.Remove(func);
899 /// Append a markup filter function for text inserted in the entry.
901 /// <param name="filter">This function type is used by entry filters to modify text.</param>
902 public void AppendMarkUpFilter(Func<Entry, string, string> filter)
904 if (!_textFilters.ContainsKey(filter))
906 Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback = (IntPtr d, IntPtr e, ref IntPtr t) =>
908 var text = Marshal.PtrToStringAnsi(t);
910 var updateText = filter(this, text);
912 if (updateText != text)
914 Interop.Libc.Free(t);
915 t = Marshal.StringToHGlobalAnsi(updateText);
918 Interop.Elementary.elm_entry_markup_filter_append(RealHandle, textFilterCallback, IntPtr.Zero);
919 _textFilters.Add(filter, textFilterCallback);
924 /// Prepend a markup filter function for text inserted in the entry.
926 /// <param name="filter">This function type is used by entry filters to modify text.</param>
927 public void PrependMarkUpFilter(Func<Entry, string, string> filter)
929 if (!_textFilters.ContainsKey(filter))
931 Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback = (IntPtr d, IntPtr e, ref IntPtr t) =>
933 var text = Marshal.PtrToStringAnsi(t);
935 var updateText = filter(this, text);
937 if (updateText != text)
939 Interop.Libc.Free(t);
940 t = Marshal.StringToHGlobalAnsi(updateText);
943 Interop.Elementary.elm_entry_markup_filter_prepend(RealHandle, textFilterCallback, IntPtr.Zero);
944 _textFilters.Add(filter, textFilterCallback);
949 /// Remove a markup filter
951 /// <param name="filter">This function type is used by entry filters to modify text.</param>
952 public void RemoveMarkUpFilter(Func<Entry, string, string> filter)
954 if (_textFilters.ContainsKey(filter))
956 Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback;
957 _textFilters.TryGetValue(filter, out textFilterCallback);
959 Interop.Elementary.elm_entry_markup_filter_remove(RealHandle, textFilterCallback, IntPtr.Zero);
960 _textFilters.Remove(filter);
965 /// This executes a "copy" action on the selected text in the entry.
967 public void CopySelection()
969 Interop.Elementary.elm_entry_selection_copy(RealHandle);
973 /// This executes a "cut" action on the selected text in the entry.
975 public void CutSelection()
977 Interop.Elementary.elm_entry_selection_cut(RealHandle);
981 /// This executes a "paste" action in the entry.
983 public void PasteSelection()
985 Interop.Elementary.elm_entry_selection_paste(RealHandle);
989 /// This disabled the entry's selection.
991 /// <param name="disable">If true, the selection are disabled.</param>
992 public void DisableSelection(bool disable)
994 Interop.Elementary.elm_entry_selection_handler_disabled_set(RealHandle, disable);
998 /// Get any selected text within the entry.
1000 /// <returns>Selection's value</returns>
1001 public string GetSelection()
1003 return Interop.Elementary.elm_entry_selection_get(RealHandle);
1007 /// This selects a region of text within the entry.
1009 /// <param name="start">The starting position.</param>
1010 /// <param name="end">The end position.</param>
1011 public void SetSelectionRegion(int start, int end)
1013 Interop.Elementary.elm_entry_select_region_set(RealHandle, start, end);
1017 /// Sets the visibility of the left-side widget of the entry
1019 /// <param name="isDisplay">true if the object should be displayed, false if not.</param>
1020 public void SetIconVisible(bool isDisplay)
1022 Interop.Elementary.elm_entry_icon_visible_set(RealHandle, isDisplay);
1026 /// Set whether the return key on the input panel is disabled automatically when entry has no text.
1028 /// <param name="enable">If enabled is true, the return key is automatically disabled when the entry has no text.</param>
1029 public void SetInputPanelReturnKeyAutoEnable(bool enable)
1031 Interop.Elementary.elm_entry_input_panel_return_key_autoenabled_set(RealHandle, enable);
1034 protected override IntPtr CreateHandle(EvasObject parent)
1036 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
1037 Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
1039 RealHandle = Interop.Elementary.elm_entry_add(handle);
1040 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);