From 178b18aaeb1fa1508d5ad60d7e9b249017efff9d Mon Sep 17 00:00:00 2001 From: Pedro Larouca Date: Wed, 21 Mar 2018 02:25:53 +0000 Subject: [PATCH] [Samples] Fix the completion entry code, Matchfunc was not correctly used. Added insensitive entry sample. --- .../Samples/Sections/Widgets/EntrySection.cs | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/Source/Samples/Sections/Widgets/EntrySection.cs b/Source/Samples/Sections/Widgets/EntrySection.cs index e9adcd39f..dbdcc7943 100644 --- a/Source/Samples/Sections/Widgets/EntrySection.cs +++ b/Source/Samples/Sections/Widgets/EntrySection.cs @@ -18,6 +18,7 @@ namespace Samples AddItem(CreateCustomActionsEntry()); AddItem(CreateProgressEntry()); AddItem(CreateCompletionEntry()); + AddItem(CreateInsensitiveEntry()); } public (string, Widget) CreateSimpleEntry() @@ -123,44 +124,41 @@ namespace Samples store.AppendValues("Better and bigger example"); store.AppendValues("Some other example"); - // create tree column - var column = new TreeViewColumn(); - column.Reorderable = false; - column.SortIndicator = false; - column.Clickable = true; - column.SortColumnId = 0; - - // create cells - var cell = new CellRendererText(); - column.PackStart(cell, true); - - // set cell functions - column.SetCellDataFunc(cell, (TreeViewColumn tree_column, CellRenderer cellRenderer, ITreeModel tree_model, TreeIter iter) => - { - var m = tree_model.GetValue(iter, 0); - (cellRenderer as CellRendererText).Text = (string)m; - }); - - // model filter - var filter = new TreeModelFilter(store, null); - filter.VisibleFunc = (model, iter) => - { - if (string.IsNullOrEmpty(entry.Text)) - return true; - - var o = model.GetValue(iter, 0); - var searchString = o as string; - if (!string.IsNullOrEmpty(searchString)) - return searchString.StartsWith(entry.Text, System.StringComparison.InvariantCultureIgnoreCase); - - return true; - }; - // assign treemodel as the completion - completion.Model = filter; + completion.Model = store; + + // lets override the default match function so we can use the contains mode + // instead of the default startswith + completion.MatchFunc = EntryCompletionMatchFunc; completion.TextColumn = 0; + return ("Completion Entry:",entry); } + + private bool EntryCompletionMatchFunc(EntryCompletion completion, string key, TreeIter iter) + { + if (string.IsNullOrEmpty(key)) + return false; + + var o = completion.Model.GetValue(iter, 0); + var stringToSearch = o as string; + + if (!string.IsNullOrEmpty(stringToSearch)) + return stringToSearch.IndexOf(key, System.StringComparison.InvariantCultureIgnoreCase) >= 0; + + return false; + } + + public (string, Widget) CreateInsensitiveEntry() + { + var entry = new Entry(); + entry.Text = "Cannot change this"; + + entry.Sensitive = false; + + return ("Insensitive entry:", entry); + } + } }