Item of autocomplete menu
Exemplo n.º 1
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();

            //replace text of fragment
            var tb = fragment.tb;

            tb.BeginAutoUndo();
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end   = tb.Selection.End;
                start.iChar        = fragment.Start.iChar;
                end.iChar          = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End   = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End   = fragment.End;
            }
            tb.InsertText(newText);
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            tb.EndAutoUndo();
            tb.Focus();
        }
Exemplo n.º 2
0
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            IWin32Window window   = this.Parent ?? this;
            Point        location = new Point((window == this ? Width : Right) + 3, 0);

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, window, location.X, location.Y, ToolTipDuration);
            }
            else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, window, location.X, location.Y, ToolTipDuration);
            }
        }
Exemplo n.º 3
0
 public void AddItem(AutocompleteItem item)
 {
     foreach (var i in sourceItems)
     {
         if (i.Text == item.Text)
         {
             return;
         }
     }
     sourceItems.Add(item);
 }
Exemplo n.º 4
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();

            //replace text of fragment
            var tb = fragment.tb;

            // < By WendyH ---------------------------
            if (tb.ToolTip4Function.Visible)
            {
                Menu.AfterComplete = true;
            }
            if (fragment.CharBeforeStart == '=')
            {
                newText = " " + newText;
            }
            int     iLine   = fragment.Start.iLine;
            int     iChar   = fragment.Start.iChar;
            HMSItem hmsItem = item as HMSItem;

            if ((hmsItem != null) && ((hmsItem.Kind == DefKind.Property) || (hmsItem.Kind == DefKind.Variable)) && !HMS.TypeIsClass(hmsItem.Type))
            {
                Range  fwords = fragment.GetFragmentLookedLeft();
                var    f      = new Range(tb, new Place(0, iLine), new Place(fwords.Start.iChar, iLine));
                string line   = f.Text.Trim();
                if (line.Length == 0)
                {
                    newText += (tb.Language == Language.PascalScript) ? " := " : " = ";
                }
            }

            // > By WendyH ---------------------------

            tb.BeginAutoUndo();
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end   = tb.Selection.End;
                start.iChar        = fragment.Start.iChar;
                end.iChar          = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End   = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End   = fragment.End;
            }
            tb.InsertText(newText);
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            tb.EndAutoUndo();
            tb.Focus();
        }
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();
            //replace text of fragment
            var tb = fragment.tb;

            tb.Selection.Start = fragment.Start;
            tb.Selection.End   = fragment.End;
            tb.InsertText(newText);
            tb.Focus();
        }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            Control window = tb;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.Hide(window);
                return;
            }

            if (this.Parent != null)
            {
                var location = new Point(Right + 3 + Menu.Left, Menu.Top);

                if ((this.PointToScreen(this.Location).X + MaxToolTipSize.Width + 105) < Screen.FromControl(this.Parent).WorkingArea.Right)
                {
                    location = new Point(Right + 5, 0);
                }
                else
                {
                    location = new Point(Left - 105 - MaximumSize.Width, 0);
                }

                if (string.IsNullOrEmpty(text))
                {
                    toolTip.ToolTipTitle = null;
                    if (ToolTipDuration == 0)
                    {
                        toolTip.Show(title, window, location);
                    }
                    else
                    {
                        toolTip.Show(title, window, location, ToolTipDuration);
                    }
                }
                else
                {
                    toolTip.ToolTipTitle = title;
                    if (ToolTipDuration == 0)
                    {
                        toolTip.Show(text, window, location);
                    }
                    else
                    {
                        toolTip.Show(text, window, location, ToolTipDuration);
                    }
                }
            }
        }
        internal virtual void OnSelecting()
        {
            if (selectedItemIndex < 0 || selectedItemIndex >= visibleItems.Count)
            {
                return;
            }
            tb.TextSource.Manager.BeginAutoUndoCommands();
            try
            {
                AutocompleteItem   item = visibleItems[selectedItemIndex];
                SelectingEventArgs args = new SelectingEventArgs()
                {
                    Item          = item,
                    SelectedIndex = selectedItemIndex
                };

                Menu.OnSelecting(args);

                if (args.Cancel)
                {
                    selectedItemIndex = args.SelectedIndex;
                    DoSelectedVisible();
                    Invalidate();
                    return;
                }

                if (!args.Handled)
                {
                    var fragment = Menu.Fragment;
                    DoAutocomplete(item, fragment);
                }

                Menu.Close();
                //
                SelectedEventArgs args2 = new SelectedEventArgs()
                {
                    Item = item,
                    Tb   = Menu.Fragment.tb
                };
                item.OnSelected(Menu, args2);
                Menu.OnSelected(args2);
            }
            finally
            {
                tb.TextSource.Manager.EndAutoUndoCommands();
            }
        }
Exemplo n.º 8
0
        internal virtual void OnSelecting()
        {
            if (this.FocussedItemIndex < 0 || this.FocussedItemIndex >= visibleItems.Count)
            {
                return;
            }
            tb.TextSource.Manager.BeginAutoUndoCommands();
            try
            {
                AutocompleteItem   item = this.FocussedItem;
                SelectingEventArgs args = new SelectingEventArgs()
                {
                    Item          = item,
                    SelectedIndex = FocussedItemIndex
                };

                this.Menu.OnSelecting(args);

                if (args.Cancel)
                {
                    this.FocussedItemIndex = args.SelectedIndex;
                    this.Invalidate();
                    return;
                }

                if (!args.Handled)
                {
                    var fragment = this.Menu.Fragment;
                    this.DoAutocomplete(item, fragment);
                }

                this.Menu.Close();
                //
                SelectedEventArgs args2 = new SelectedEventArgs()
                {
                    Item = item,
                    Tb   = this.Menu.Fragment.tb
                };
                item.OnSelected(this.Menu, args2);
                this.Menu.OnSelected(args2);
            }
            finally
            {
                tb.TextSource.Manager.EndAutoUndoCommands();
            }
        }
Exemplo n.º 9
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = fragment.Text;
            int    last    = newText.LastIndexOf('.');

            if (last > 0)
            {
                newText = newText.Substring(0, last + 1);
            }
            else
            {
                newText = "";
            }

            newText = newText + item;
            if (item.ToolTipText != null)
            {
                newText = newText + " ";
            }

            //replace text of fragment
            var tb = fragment.tb;

            tb.BeginAutoUndo();
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end   = tb.Selection.End;
                start.iChar        = fragment.Start.iChar;
                end.iChar          = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End   = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End   = fragment.End;
            }

            tb.InsertText(newText);
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            tb.EndAutoUndo();
            tb.Focus();
        }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            Control window = tb;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.Hide(window);
                return;
            }

            var location = window.PointToClient(Menu.PointToScreen(new Point(Menu.Width + 3, 0)));

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                if (ToolTipDuration == 0)
                {
                    toolTip.Show(title, window, location);
                }
                else
                {
                    toolTip.Show(title, window, location, ToolTipDuration);
                }
            }
            else
            {
                toolTip.ToolTipTitle = title;
                if (ToolTipDuration == 0)
                {
                    toolTip.Show(text, window, location);
                }
                else
                {
                    toolTip.Show(text, window, location, ToolTipDuration);
                }
            }
        }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            Control window = tb;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.Hide(window);
                return;
            }

            var location = new Point(Right + 3 + Menu.Left, Menu.Top);

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                if (ToolTipDuration == 0)
                {
                    toolTip.Show(title, window, location);
                }
                else
                {
                    toolTip.Show(title, window, location, ToolTipDuration);
                }
            }
            else
            {
                toolTip.ToolTipTitle = title;
                if (ToolTipDuration == 0)
                {
                    toolTip.Show(text, window, location);
                }
                else
                {
                    toolTip.Show(text, window, location, ToolTipDuration);
                }
            }
        }
Exemplo n.º 12
0
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            if (this.Parent != null)
            {
                IWin32Window window = this.Parent ?? this;
                Point        location;

                if ((this.PointToScreen(this.Location).X + MaxToolTipSize.Width + 105) < Screen.FromControl(this.Parent).WorkingArea.Right)
                {
                    location = new Point(Right + 5, 0);
                }
                else
                {
                    location = new Point(Left - 105 - MaximumSize.Width, 0);
                }

                if (string.IsNullOrEmpty(text))
                {
                    toolTip.ToolTipTitle = null;
                    toolTip.Show(title, window, location.X, location.Y
//                        , ToolTipDuration
                                 );
                }
                else
                {
                    toolTip.ToolTipTitle = title;
                    toolTip.Show(text, window, location.X, location.Y);
                }
            }
        }
Exemplo n.º 13
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();
            //replace text of fragment
            var tb = fragment.tb;

            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end   = tb.Selection.End;
                start.iChar        = fragment.Start.iChar;
                end.iChar          = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End   = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End   = fragment.End;
            }
            tb.InsertText(newText);
            tb.Focus();
        }
Exemplo n.º 14
0
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = visibleItems[selectedItemIndex].ToolTipTitle;
            var text  = visibleItems[selectedItemIndex].ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, this, Width + 3, 0, 3000);
            }
            else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, this, Width + 3, 0, 3000);
            }
        }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text  = autocompleteItem.ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, this, 0, Height + 3);
            }
            else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, this, 0, Height + 3);
            }
        }
Exemplo n.º 16
0
 public void ShowTooltipWithLabel(AutocompleteItem item)
 {
     this.ShowTooltipWithLabel(item.ToolTipTitle, item.ToolTipText);
 }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text = autocompleteItem.ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            IWin32Window window = this.Parent ?? this;
            Point location = new Point((window == this ? Width : Right) + 3, 0);

            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, window, location.X, location.Y, ToolTipDuration);
            }
            else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, window, location.X, location.Y, ToolTipDuration);
            }
        }
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();

            //replace text of fragment
            var tb = fragment.tb;

            tb.BeginAutoUndo();
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end = tb.Selection.End;
                start.iChar = fragment.Start.iChar;
                end.iChar = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End = fragment.End;
            }
            tb.InsertText(newText);
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            tb.EndAutoUndo();
            tb.Focus();
        }
Exemplo n.º 19
0
 public void InitAutoMenus()
 {
     items.Clear();
     foreach (string k in tooltipDic.Keys)
     {
         AutocompleteItem item = new AutocompleteItem(k);
         item.ToolTipTitle = k;
         item.ToolTipText = tooltipDic[k];
         items.Add(item);
     }
     foreach (string k in longTooltipDic.Keys)
     {
         if (tooltipDic.ContainsKey(k))
             continue;
         AutocompleteItem item = new AutocompleteItem(k);
         item.ToolTipTitle = k;
         item.ToolTipText = longTooltipDic[k];
         items.Add(item);
     }
 }
Exemplo n.º 20
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();

            //replace text of fragment
            var box = fragment.tb;
            // < By WendyH ---------------------------
            if (box.ToolTip4Function.Visible) Menu.AfterComplete = true;
            if (fragment.CharBeforeStart == '=') newText = " " + newText;
            int iLine = fragment.Start.iLine;
            HMSItem hmsItem = item as HMSItem;
            if (hmsItem != null) {
                if (((hmsItem.Kind == DefKind.Property) || (hmsItem.Kind == DefKind.Variable)) && !HMS.TypeIsClass(hmsItem.Type)) {
                    Range fwords = fragment.GetFragmentLookedLeft();
                    var f = new Range(box, new Place(0, iLine), new Place(fwords.Start.iChar, iLine));
                    string line = f.Text.Trim();
                    if ((line.Length == 0) && (box.Lines[iLine].IndexOf('=') < 0)) newText += (box.Language == Language.PascalScript) ? " := " : " = ";
                }
            }
            if (box.KeywordsToLowcase) {
                if (HMS.WordIsKeyword(newText))
                    newText = newText.ToLower();
            }
            // > By WendyH ---------------------------

            box.BeginAutoUndo();
            box.BeginUpdate();
            try {
                box.TextSource.Manager.ExecuteCommand(new SelectCommand(box.TextSource));
                if (box.Selection.ColumnSelectionMode)
                {
                    var start = box.Selection.Start;
                    var end   = box.Selection.End;
                    start.iChar = fragment.Start.iChar;
                    end  .iChar = fragment.End  .iChar;
                    box.Selection.Start = start;
                    box.Selection.End   = end;
                }
                else
                {
                    box.Selection.Start = fragment.Start;
                    box.Selection.End   = fragment.End;
                }
                box.InsertText(newText);
                box.TextSource.Manager.ExecuteCommand(new SelectCommand(box.TextSource));
            } finally {
                box.EndUpdate();
                box.EndAutoUndo();
            }
            if ((hmsItem != null) && (hmsItem.Params.Count > 0)) {
                if (HMSEditor.ActiveEditor != null) HMSEditor.ActiveEditor.WasCommaOrBracket = true;
            }
            box.Focus();
        }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text = autocompleteItem.ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            if(string.IsNullOrEmpty(text) )
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, this, 0, Height + 3);
            }else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, this, 0, Height + 3);
            }
        }
Exemplo n.º 22
0
 public void AddItem(AutocompleteItem item)
 {
     foreach (var i in sourceItems)
         if (i.Text == item.Text)
             return;
     sourceItems.Add(item);
 }
Exemplo n.º 23
0
 private void DoAutocomplete(AutocompleteItem item, Range fragment)
 {
     string newText = item.GetTextForReplace();
     //replace text of fragment
     var tb = fragment.tb;
     if (tb.Selection.ColumnSelectionMode)
     {
         var start = tb.Selection.Start;
         var end = tb.Selection.End;
         start.iChar = fragment.Start.iChar;
         end.iChar = fragment.End.iChar;
         tb.Selection.Start = start;
         tb.Selection.End = end;
     }
     else
     {
         tb.Selection.Start = fragment.Start;
         tb.Selection.End = fragment.End;
     }
     tb.InsertText(newText);
     tb.Focus();
 }
Exemplo n.º 24
0
 private void DoAutocomplete(AutocompleteItem item, Range fragment)
 {
     string newText = item.GetTextForReplace();
     //replace text of fragment
     var tb = fragment.tb;
     tb.Selection.Start = fragment.Start;
     tb.Selection.End = fragment.End;
     tb.InsertText(newText);
     tb.Focus();
 }
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = autocompleteItem.ToolTipTitle;
            var text = autocompleteItem.ToolTipText;

            Control window = tb;
            if (string.IsNullOrEmpty(title))
            {
                toolTip.Hide(window);
                return;
            }

            var location = new Point(Right + 3 + Menu.Left, Menu.Top);
            if (string.IsNullOrEmpty(text))
            {
                toolTip.ToolTipTitle = null;
                if (ToolTipDuration == 0)
                    toolTip.Show(title, window, location);
                else
                    toolTip.Show(title, window, location, ToolTipDuration);
            }
            else
            {
                toolTip.ToolTipTitle = title;
                if (ToolTipDuration == 0)
                    toolTip.Show(text, window, location);
                else
                    toolTip.Show(text, window, location, ToolTipDuration);
            }
        }
 private static int CompareItems(AutocompleteItem x, AutocompleteItem y)
 { 
     return (x.MenuText ?? x.Text).CompareTo(y.MenuText ?? y.Text);
 }
Exemplo n.º 27
0
        private void DoAutocomplete(AutocompleteItem item, Range fragment)
        {
            string newText = item.GetTextForReplace();

            //replace text of fragment
            var tb = fragment.tb;
            // < By WendyH ---------------------------
            if (tb.ToolTip4Function.Visible) Menu.AfterComplete = true;
            if (fragment.CharBeforeStart == '=') newText = " " + newText;
            int iLine = fragment.Start.iLine;
            int iChar = fragment.Start.iChar;
            HMSItem hmsItem = item as HMSItem;
            if ((hmsItem != null) && ((hmsItem.Kind == DefKind.Property) || (hmsItem.Kind == DefKind.Variable)) && !HMS.TypeIsClass(hmsItem.Type)) {
                Range fwords = fragment.GetFragmentLookedLeft();
                var f = new Range(tb, new Place(0, iLine), new Place(fwords.Start.iChar, iLine));
                string line = f.Text.Trim();
                if (line.Length == 0) newText += (tb.Language == Language.PascalScript) ? " := " : " = ";
            }

            // > By WendyH ---------------------------

            tb.BeginAutoUndo();
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            if (tb.Selection.ColumnSelectionMode)
            {
                var start = tb.Selection.Start;
                var end = tb.Selection.End;
                start.iChar = fragment.Start.iChar;
                end.iChar = fragment.End.iChar;
                tb.Selection.Start = start;
                tb.Selection.End = end;
            }
            else
            {
                tb.Selection.Start = fragment.Start;
                tb.Selection.End = fragment.End;
            }
            tb.InsertText(newText);
            tb.TextSource.Manager.ExecuteCommand(new SelectCommand(tb.TextSource));
            tb.EndAutoUndo();
            tb.Focus();
        }
Exemplo n.º 28
0
        private void SetToolTip(AutocompleteItem autocompleteItem)
        {
            var title = visibleItems[selectedItemIndex].ToolTipTitle;
            var text = visibleItems[selectedItemIndex].ToolTipText;

            if (string.IsNullOrEmpty(title))
            {
                toolTip.ToolTipTitle = null;
                toolTip.SetToolTip(this, null);
                return;
            }

            if(string.IsNullOrEmpty(text) )
            {
                toolTip.ToolTipTitle = null;
                toolTip.Show(title, this, Width + 3, 0, 3000);
            }else
            {
                toolTip.ToolTipTitle = title;
                toolTip.Show(text, this, Width + 3, 0, 3000);
            }
        }
        internal void DoAutocomplete(bool forced)
        {
            if (!Menu.Enabled)
            {
                Menu.Close();
                return;
            }

            visibleItems.Clear();
            FocussedItemIndex    = 0;
            VerticalScroll.Value = 0;
            //some magic for update scrolls
            AutoScrollMinSize -= new Size(1, 0);
            AutoScrollMinSize += new Size(1, 0);
            //get fragment around caret
            Range  fragment = tb.Selection.GetFragment(Menu.SearchPattern);
            string text     = fragment.Text;
            //calc screen point for popup menu
            Point point = tb.PlaceToPoint(fragment.End);

            point.Offset(2, tb.CharHeight);
            //
            if (forced || (text.Length >= Menu.MinFragmentLength &&
                           tb.Selection.IsEmpty && /*pops up only if selected range is empty*/
                           (tb.Selection.Start > fragment.Start || text.Length == 0 /*pops up only if caret is after first letter*/)))
            {
                Menu.Fragment = fragment;
                //bool foundSelected = false;
                AutocompleteItem foundItem = null;
                //build popup menu
                foreach (var item in sourceItems)
                {
                    item.Parent = Menu;
                    CompareResult res = item.Compare(text);
                    if (res != CompareResult.Hidden)
                    {
                        visibleItems.Add(item);
                    }

                    if (res == CompareResult.VisibleAndSelected && foundItem != null)
                    {
                        foundItem = item;
                        //FocussedItemIndex = visibleItems.Count - 1;
                    }
                }

                visibleItems.Sort((AutocompleteItem a, AutocompleteItem b) => {
                    var indexA = a.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase);
                    var indexB = b.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase);

                    if (indexA == indexB)
                    {
                        if (a.Text.Length == b.Text.Length)
                        {
                            return(string.Compare(a.Text, b.Text, true));
                        }

                        return(a.Text.Length - b.Text.Length);
                    }
                    else
                    {
                        if (indexA == -1)
                        {
                            return(1);
                        }

                        if (indexB == -1)
                        {
                            return(-1);
                        }
                    }

                    return(indexA - indexB);
                });

                if (foundItem != null)
                {
                    FocussedItemIndex = visibleItems.IndexOf(foundItem);

                    AdjustScroll();
                    DoSelectedVisible();
                }
            }

            //show popup menu
            if (Count > 0)
            {
                if (!Menu.Visible)
                {
                    CancelEventArgs args = new CancelEventArgs();
                    Menu.OnOpening(args);
                    if (!args.Cancel)
                    {
                        Menu.Show(tb, point);
                    }
                }

                DoSelectedVisible();
                Invalidate();
            }
            else
            {
                Menu.Close();
            }
        }