/* * 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 System.Runtime.InteropServices; namespace ElmSharp { /// /// Enumeration for describing InputPanel layout type. /// public enum InputPanelLayout { /// /// InputPanel layout type default. /// Normal, /// /// InputPanel layout type number. /// Number, /// /// InputPanel layout type email. /// Email, /// /// InputPanel layout type url. /// Url, /// /// InputPanel layout type phone. /// PhoneNumber, /// /// InputPanel layout type ip. /// Ip, /// /// InputPanel layout type month. /// Month, /// /// InputPanel layout type number. /// NumberOnly, /// /// InputPanel layout type error type. Do not use it directly! /// Invalid, /// /// InputPanel layout type hexadecimal. /// Hex, /// /// InputPanel layout type terminal type, esc, alt, ctrl, etc. /// Terminal, /// /// InputPanel layout type password. /// Password, /// /// Keyboard layout type date and time. /// DateTime, /// /// InputPanel layout type emoticons. /// Emoticon } /// /// Enumeration that defines the "Return" key types on the input panel (virtual keyboard). /// public enum InputPanelReturnKeyType { /// /// Default key type /// Default, /// /// Done key type /// Done, /// /// Go key type /// Go, /// /// Join key type /// Join, /// /// Login key type /// Login, /// /// Next key type /// Next, /// /// Search string or magnifier icon key type /// Search, /// /// Send key type /// Send, /// /// Sign-in key type /// Signin } /// /// Enumeration that defines the autocapitalization types. /// public enum AutoCapital { /// /// No autocapitalization when typing /// None, /// /// Autocapitalize each typed word /// Word, /// /// Autocapitalize the start of each sentence /// Sentence, /// /// Autocapitalize all letters /// All } /// /// Enumeration that defines the entry's copy & paste policy. /// public enum CopyAndPasteMode { /// /// Copy & paste text with markup tag /// Markup, /// /// Copy & paste text without item(image) tag /// NoImage, /// /// Copy & paste text without markup tag /// PlainText } /// /// Enumeration that defines the text format types. /// public enum TextFormat { /// /// Plain type /// Plain, /// /// Markup type /// Markup } /// /// Enumeration that defines the types of Input Hints. /// public enum InputHints { /// /// No active hints /// None, /// /// suggest word auto completion /// AutoComplete, /// /// typed text should not be stored. /// SensitiveData, } /// /// Enumeration that defines the input panel (virtual keyboard) language modes. /// public enum InputPanelLanguage { /// /// Automatic language mode /// Automatic, /// /// Alphabet language mode /// Alphabet, } /// /// The entry is a convenience widget that shows a box in which the user can enter text. /// public class Entry : Layout { SmartEvent _clicked; SmartEvent _changedByUser; SmartEvent _cursorChanged; SmartEvent _activated; Dictionary, Interop.Elementary.Elm_Entry_Item_Provider_Cb> _itemsProvider = new Dictionary, Interop.Elementary.Elm_Entry_Item_Provider_Cb>(); Dictionary, Interop.Elementary.Elm_Entry_Filter_Cb> _textFilters = new Dictionary, Interop.Elementary.Elm_Entry_Filter_Cb>(); /// /// Creates and initializes a new instance of the Entry class. /// /// The EvasObject to which the new Entry will be attached as a child. public Entry(EvasObject parent) : base(parent) { _clicked = new SmartEvent(this, this.RealHandle, "clicked"); _clicked.On += (s, e) => Clicked?.Invoke(this, EventArgs.Empty); _changedByUser = new SmartEvent(this, this.RealHandle, "changed,user"); _changedByUser.On += (s, e) => ChangedByUser?.Invoke(this, EventArgs.Empty); _cursorChanged = new SmartEvent(this, this.RealHandle, "cursor,changed"); _cursorChanged.On += (s, e) => CursorChanged?.Invoke(this, EventArgs.Empty); _activated = new SmartEvent(this, this.RealHandle, "activated"); _activated.On += (s, e) => Activated?.Invoke(this, EventArgs.Empty); } /// /// Activated will be triggered when the entry in Activated stated. /// public event EventHandler Activated; /// /// Clicked will be triggered when the entry is clicked. /// public event EventHandler Clicked; /// /// ChangedByUser will be triggered when the entry changed by user. /// public event EventHandler ChangedByUser; /// /// CursorChanged will be triggered when the Cursor in the entry is changed. /// public event EventHandler CursorChanged; /// /// Sets or gets the entry to the single line mode. /// public bool IsSingleLine { get { return Interop.Elementary.elm_entry_single_line_get(RealHandle); } set { Interop.Elementary.elm_entry_single_line_set(RealHandle, value); } } /// /// Sets or gets the entry to the password mode. /// public bool IsPassword { get { return Interop.Elementary.elm_entry_password_get(RealHandle); } set { Interop.Elementary.elm_entry_password_set(RealHandle, value); } } /// /// Sets or gets whether the entry is editable. /// public bool IsEditable { get { return Interop.Elementary.elm_entry_editable_get(RealHandle); } set { Interop.Elementary.elm_entry_editable_set(RealHandle, value); } } /// /// Sets or gets whether the entry is empty. /// public bool IsEmpty { get { return Interop.Elementary.elm_entry_is_empty(RealHandle); } } /// /// Sets or gets text currently shown in the object entry. /// public override string Text { get { return Interop.Elementary.elm_entry_entry_get(RealHandle); } set { Interop.Elementary.elm_entry_entry_set(RealHandle, value); } } /// /// Sets or gets the style on the top of the user style stack. /// /// 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'). public string TextStyle { get { return Interop.Elementary.elm_entry_text_style_user_peek(RealHandle); } set { Interop.Elementary.elm_entry_text_style_user_push(RealHandle, value); } } /// /// Sets or gets the current position of the cursor in the entry. /// public int CursorPosition { get { return Interop.Elementary.elm_entry_cursor_pos_get(RealHandle); } set { Interop.Elementary.elm_entry_cursor_pos_set(RealHandle, value); } } /// /// Sets or gets the scrollable state of the entry. /// public bool Scrollable { get { return Interop.Elementary.elm_entry_scrollable_get(RealHandle); } set { // HACK: Enabling the scrollable property of an entry causes its internal // hierarchy to change, making the internal edje object inaccessible. // Access it before the property is set, to cache the edje object's handle. if (value) { var dummy = EdjeObject; } Interop.Elementary.elm_entry_scrollable_set(RealHandle, value); } } /// /// Sets or Gets the autocapitalization type on the immodule. /// public AutoCapital AutoCapital { get { return (AutoCapital)Interop.Elementary.elm_entry_autocapital_type_get(RealHandle); } set { Interop.Elementary.elm_entry_autocapital_type_set(RealHandle, (Interop.Elementary.AutocapitalType)value); } } /// /// Sets or Gets the entry object's 'autosave' status. /// public bool IsAutoSave { get { return Interop.Elementary.elm_entry_autosave_get(RealHandle); } set { Interop.Elementary.elm_entry_autosave_set(RealHandle, value); } } /// /// Sets or Gets entry text paste/drop mode. /// public CopyAndPasteMode CopyAndPasteMode { get { return (CopyAndPasteMode)Interop.Elementary.elm_entry_cnp_mode_get(RealHandle); } set { Interop.Elementary.elm_entry_cnp_mode_set(RealHandle, (Interop.Elementary.CopyAndPasteMode)value); } } /// /// Gets the geometry of the cursor. /// public Rect CursorGeometry { get { int x, y, w, h; Interop.Elementary.elm_entry_cursor_geometry_get(RealHandle, out x, out y, out w, out h); return new Rect(x, y, w, h); } } /// /// Gets whether a format node exists at the current cursor position. /// public bool IsCursorFormat { get { return Interop.Elementary.elm_entry_cursor_is_format_get(RealHandle); } } /// /// Gets if the current cursor position holds a visible format node. /// public bool IsCursorVisibelFormat { get { return Interop.Elementary.elm_entry_cursor_is_visible_format_get(RealHandle); } } /// /// Sets or Gets the value of input hint. /// public InputHints InputHint { get { return (InputHints)Interop.Elementary.elm_entry_input_hint_get(RealHandle); } set { Interop.Elementary.elm_entry_input_hint_set(RealHandle, (Interop.Elementary.InputHints)value); } } /// /// Sets or gets the language mode of the input panel. /// public InputPanelLanguage InputPanelLanguage { get { return (InputPanelLanguage)Interop.Elementary.elm_entry_input_panel_language_get(RealHandle); } set { Interop.Elementary.elm_entry_input_panel_language_set(RealHandle, (Interop.Elementary.InputPanelLanguage)value); } } /// /// Sets or gets the input panel layout variation of the entry. /// public int InputPanelVariation { get { return Interop.Elementary.elm_entry_input_panel_layout_variation_get(RealHandle); } set { Interop.Elementary.elm_entry_input_panel_layout_variation_set(RealHandle, value); } } /// /// Sets or gets the line wrap type to use on multi-line entries. /// public WrapType LineWrapType { get { return (WrapType)Interop.Elementary.elm_entry_line_wrap_get(RealHandle); } set { Interop.Elementary.elm_entry_line_wrap_set(RealHandle, (Interop.Elementary.WrapType)value); } } /// /// Sets or gets whether the entry should allow to use the text prediction. /// public bool PredictionAllowed { get { return Interop.Elementary.elm_entry_prediction_allow_get(RealHandle); } set { Interop.Elementary.elm_entry_prediction_allow_set(RealHandle, value); } } /// /// Sets or gets whether the return key on the input panel should be disabled or not. /// public bool InputPanelReturnKeyDisabled { get { return Interop.Elementary.elm_entry_input_panel_return_key_disabled_get(RealHandle); } set { Interop.Elementary.elm_entry_input_panel_return_key_disabled_set(RealHandle, value); } } /// /// Sets or gets the attribute to show the input panel in case of only an user's explicit Mouse Up event. /// It doesn't request to show the input panel even though it has focus. /// If true, the input panel will be shown in case of only Mouse up event. (Focus event will be ignored.) /// public bool InputPanelShowByOnDemand { get { return Interop.Elementary.elm_entry_input_panel_show_on_demand_get(RealHandle); } set { Interop.Elementary.elm_entry_input_panel_show_on_demand_set(RealHandle, value); } } /// /// Sets the file (and implicitly loads it) for the text to display and then edit. /// /// The path to the file to load and save /// The file format public void SetFile(string file, TextFormat textFormat) { Interop.Elementary.elm_entry_file_set(RealHandle, file, (Interop.Elementary.TextFormat)textFormat); } /// /// Converts a markup (HTML-like) string into UTF-8. /// /// The string (in markup) to be converted /// The converted string (in UTF-8) public static string ConvertMarkupToUtf8(string markup) { return Interop.Elementary.elm_entry_markup_to_utf8(markup); } /// /// Moves the cursor by one position to the right within the entry. /// /// public bool MoveCursorNext() { return Interop.Elementary.elm_entry_cursor_next(RealHandle); } /// /// Moves the cursor one place to the left within the entry. /// /// TRUE on success, otherwise FALSE on failure public bool MoveCursorPrev() { return Interop.Elementary.elm_entry_cursor_prev(RealHandle); } /// /// Moves the cursor one line up within the entry. /// /// TRUE on success, otherwise FALSE on failure public bool MoveCursorUp() { return Interop.Elementary.elm_entry_cursor_up(RealHandle); } /// /// Moves the cursor one line down within the entry. /// /// TRUE on success, otherwise FALSE on failure public bool MoveCursorDown() { return Interop.Elementary.elm_entry_cursor_down(RealHandle); } /// /// Moves the cursor to the beginning of the entry. /// public void MoveCursorBegin() { Interop.Elementary.elm_entry_cursor_begin_set(RealHandle); } /// /// Moves the cursor to the end of the entry. /// public void MoveCursorEnd() { Interop.Elementary.elm_entry_cursor_end_set(RealHandle); } /// /// Moves the cursor to the beginning of the current line. /// public void MoveCursorLineBegin() { Interop.Elementary.elm_entry_cursor_line_begin_set(RealHandle); } /// /// Moves the cursor to the end of the current line. /// public void MoveCursorLineEnd() { Interop.Elementary.elm_entry_cursor_line_end_set(RealHandle); } /// /// Sets the input panel layout of the entry. /// /// The layout type public void SetInputPanelLayout(InputPanelLayout layout) { Interop.Elementary.elm_entry_input_panel_layout_set(RealHandle, (Interop.Elementary.InputPanelLayout)layout); } /// /// Sets the attribute to show the input panel automatically. /// /// If true the input panel appears when the entry is clicked or has focus, otherwise false public void SetInputPanelEnabled(bool enabled) { Interop.Elementary.elm_entry_input_panel_enabled_set(RealHandle, enabled); } /// /// Sets the "return" key type. This type is used to set the string or icon on the "return" key of the input panel. /// /// The type of "return" key on the input panel public void SetInputPanelReturnKeyType(InputPanelReturnKeyType keyType) { Interop.Elementary.elm_entry_input_panel_return_key_type_set(RealHandle, (Interop.Elementary.ReturnKeyType)keyType); } /// /// Hides the input panel (virtual keyboard). /// /// /// Note that the input panel is shown or hidden automatically according to the focus state of the entry widget. /// This API can be used in case of manually controlling by using SetInputPanelEnabled(false). /// public void HideInputPanel() { Interop.Elementary.elm_entry_input_panel_hide(RealHandle); } /// /// Selects all the text within the entry. /// public void SelectAll() { Interop.Elementary.elm_entry_select_all(RealHandle); } /// /// Drops any existing text selection within the entry. /// public void SelectNone() { Interop.Elementary.elm_entry_select_none(RealHandle); } /// /// Forces calculation of the entry size and text layouting. /// public void ForceCalculation() { Interop.Elementary.elm_entry_calc_force(RealHandle); } /// /// Gets the string by the cursor at its current position. /// /// public string GetCursorContent() { return Interop.Elementary.elm_entry_cursor_content_get(RealHandle); } /// /// Begins a selection within the entry as though the user were holding down the mouse button to make a selection. /// public void BeginCursorSelection() { Interop.Elementary.elm_entry_cursor_selection_begin(RealHandle); } /// /// Appends the text of the entry. /// /// The text to be displayed public void AppendText(string text) { Interop.Elementary.elm_entry_entry_append(RealHandle, text); } /// /// Inserts the given text into the entry at the current cursor position. /// /// public void InsertTextToCursor(string text) { Interop.Elementary.elm_entry_entry_insert(RealHandle, text); } /// /// Ends a selection within the entry as though the user had just released the mouse button while making a selection. /// public void EndCursorSelection() { Interop.Elementary.elm_entry_cursor_selection_end(RealHandle); } /// /// Writes any changes made to the file that is set by File. /// public void SaveFile() { Interop.Elementary.elm_entry_file_save(RealHandle); } /// /// Show the input panel (virtual keyboard) based on the input panel property of entry such as layout, autocapital types, and so on. /// /// /// Note that input panel is shown or hidden automatically according to the focus state of entry widget. /// This API can be used in the case of manually controlling by using SetInputPanelEnabled(false). /// public void ShowInputPanel() { Interop.Elementary.elm_entry_input_panel_show(RealHandle); } /// /// This appends a custom item provider to the list for that entry. /// /// This function is used to provide items. public void AppendItemProvider(Func func) { if (func != null) { if (!_itemsProvider.ContainsKey(func)) { Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback; itemProviderCallback = (d, o, t) => { return func?.Invoke(t); }; Interop.Elementary.elm_entry_item_provider_append(RealHandle, itemProviderCallback, IntPtr.Zero); _itemsProvider.Add(func, itemProviderCallback); } } } /// /// This prepends a custom item provider to the list for that entry. /// /// This function is used to provide items. public void PrependItemProvider(Func func) { if (!_itemsProvider.ContainsKey(func)) { Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback; itemProviderCallback = (d, o, t) => { return func?.Invoke(t); }; Interop.Elementary.elm_entry_item_provider_prepend(RealHandle, itemProviderCallback, IntPtr.Zero); _itemsProvider.Add(func, itemProviderCallback); } } /// /// This removes a custom item provider to the list for that entry. /// /// This function is used to provide items. public void RemoveItemProvider(Func func) { if (_itemsProvider.ContainsKey(func)) { Interop.Elementary.Elm_Entry_Item_Provider_Cb itemProviderCallback; _itemsProvider.TryGetValue(func, out itemProviderCallback); Interop.Elementary.elm_entry_item_provider_remove(RealHandle, itemProviderCallback, IntPtr.Zero); _itemsProvider.Remove(func); } } /// /// Append a markup filter function for text inserted in the entry. /// /// This function type is used by entry filters to modify text. public void AppendMarkUpFilter(Func filter) { if (!_textFilters.ContainsKey(filter)) { Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback = (IntPtr d, IntPtr e, ref IntPtr t) => { var text = Marshal.PtrToStringAnsi(t); var updateText = filter(this, text); if (updateText != text) { Interop.Libc.Free(t); t = Marshal.StringToHGlobalAnsi(updateText); } }; Interop.Elementary.elm_entry_markup_filter_append(RealHandle, textFilterCallback, IntPtr.Zero); _textFilters.Add(filter, textFilterCallback); } } /// /// Prepend a markup filter function for text inserted in the entry. /// /// This function type is used by entry filters to modify text. public void PrependMarkUpFilter(Func filter) { if (!_textFilters.ContainsKey(filter)) { Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback = (IntPtr d, IntPtr e, ref IntPtr t) => { var text = Marshal.PtrToStringAnsi(t); var updateText = filter(this, text); if (updateText != text) { Interop.Libc.Free(t); t = Marshal.StringToHGlobalAnsi(updateText); } }; Interop.Elementary.elm_entry_markup_filter_prepend(RealHandle, textFilterCallback, IntPtr.Zero); _textFilters.Add(filter, textFilterCallback); } } /// /// Remove a markup filter /// /// This function type is used by entry filters to modify text. public void RemoveMarkUpFilter(Func filter) { if (_textFilters.ContainsKey(filter)) { Interop.Elementary.Elm_Entry_Filter_Cb textFilterCallback; _textFilters.TryGetValue(filter, out textFilterCallback); Interop.Elementary.elm_entry_markup_filter_remove(RealHandle, textFilterCallback, IntPtr.Zero); _textFilters.Remove(filter); } } /// /// This executes a "copy" action on the selected text in the entry. /// public void CopySelection() { Interop.Elementary.elm_entry_selection_copy(RealHandle); } /// /// This executes a "cut" action on the selected text in the entry. /// public void CutSelection() { Interop.Elementary.elm_entry_selection_cut(RealHandle); } /// /// This executes a "paste" action in the entry. /// public void PasteSelection() { Interop.Elementary.elm_entry_selection_paste(RealHandle); } /// /// This disabled the entry's selection. /// /// If true, the selection are disabled. public void DisableSelection(bool disable) { Interop.Elementary.elm_entry_selection_handler_disabled_set(RealHandle, disable); } /// /// Get any selected text within the entry. /// /// Selection's value public string GetSelection() { return Interop.Elementary.elm_entry_selection_get(RealHandle); } /// /// This selects a region of text within the entry. /// /// The starting position. /// The end position. public void SetSelectionRegion(int start, int end) { Interop.Elementary.elm_entry_select_region_set(RealHandle, start, end); } /// /// Sets the visibility of the left-side widget of the entry /// /// true if the object should be displayed, false if not. public void SetIconVisible(bool isDisplay) { Interop.Elementary.elm_entry_icon_visible_set(RealHandle, isDisplay); } /// /// Set whether the return key on the input panel is disabled automatically when entry has no text. /// /// If enabled is true, the return key is automatically disabled when the entry has no text. public void SetInputPanelReturnKeyAutoEnable(bool enable) { Interop.Elementary.elm_entry_input_panel_return_key_autoenabled_set(RealHandle, enable); } protected override IntPtr CreateHandle(EvasObject parent) { return Interop.Elementary.elm_entry_add(parent.Handle); } } }