Ejemplo n.º 1
0
 internal void OnPrepareOpening(PrepareOpeningEventArgs args)
 {
     if (PrepareOpening != null)
     {
         PrepareOpening(this, args);
     }
 }
Ejemplo n.º 2
0
        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;

            PrepareOpeningEventArgs args = new PrepareOpeningEventArgs(false, text, fragment.Start);

            Menu.OnPrepareOpening(args);
            if (args.Cancel)
            {
                Menu.Close();
                return;
            }


            //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;
                //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 && !foundSelected)
                    {
                        foundSelected     = true;
                        FocussedItemIndex = visibleItems.Count - 1;
                    }
                }

                if (foundSelected)
                {
                    AdjustScroll();
                    DoSelectedVisible();
                }
                var sortingArgs = new PrepareSortingEventArgs(visibleItems, text);
                Menu.OnPrepareSorting(sortingArgs);
                visibleItems = sortingArgs.FilteredItems;
            }

            //show popup menu (only if at least one item that is not the already existing text)
            if ((Count > 1) ||
                ((Count == 1) &&
                 (visibleItems[0].Text.Length > text.Length)))
            {
                if (!Menu.Visible)
                {
                    CancelEventArgs args2 = new CancelEventArgs();
                    Menu.OnOpening(args2);
                    if (!args.Cancel)
                    {
                        Menu.Show(tb, point);
                    }
                }
                else
                {
                    Invalidate();
                }
            }
            else
            {
                Menu.Close();
            }
        }