Exemplo n.º 1
0
        private void ShowPopup()
        {
            if ((presets == null) || (presets.Length == 0))
            {
                return;
            }

            EventHandler onActivated = delegate(object sender, EventArgs e) {
                Gtk.MenuItem item = (Gtk.MenuItem)sender;

                SetPlaceholderText(false);

                if (Text.Length > 0)
                {
                    if ((!Text.TrimEnd().EndsWith(" or")) && (!Text.TrimEnd().EndsWith(" OR")) &&
                        (!Text.TrimEnd().EndsWith(" and")) && (!Text.TrimEnd().EndsWith(" AND")))
                    {
                        if (!Text.EndsWith(" "))
                        {
                            Text += " ";
                        }

                        Text += "and ";
                    }
                    else
                    {
                        if (!Text.EndsWith(" "))
                        {
                            Text += " ";
                        }
                    }
                }

                var p = (SearchEntryPreset)item.Data["preset"];

                if (!string.IsNullOrEmpty(p.Value))
                {
                    Text += p.Value;

                    if (!string.IsNullOrEmpty(p.Suggestion))
                    {
                        Text += " " + p.Suggestion;

                        GrabFocus();
                        SelectRegion(Text.Length - p.Suggestion.Length, Text.Length);
                    }
                    else
                    {
                        GrabFocus();
                        SelectRegion(Text.Length, Text.Length);
                    }
                }
            };

            if (presetsChanged)
            {
                if (popup != null)
                {
                    popup.Dispose();
                }

                popup = new Gtk.Menu();

                foreach (var p in presets)
                {
                    Gtk.MenuItem item = new Gtk.MenuItem(p.Caption);
                    item.Activated     += onActivated;
                    item.Data["preset"] = p;

                    popup.Append(item);
                }

                popup.ShowAll();
                presetsChanged = false;
            }

            popup.Popup();
        }