/*************************************/ /**** Public Methods ****/ /*************************************/ public override bool SetParent(object parent) { Panel container = parent as Panel; if (container == null) { return(false); } if (m_Popup == null) { m_Popup = new Popup { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Placement = PlacementMode.MousePoint }; Grid grid = new Grid(); grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.RowDefinitions.Add(new RowDefinition()); grid.RowDefinitions.Add(new RowDefinition()); m_Popup.Child = grid; m_SearchTextBox = new TextBox { MinWidth = m_MinWidth, MinHeight = 24, MaxLines = 1 }; m_SearchTextBox.TextChanged += (o, e) => RefreshSearchResults(PossibleItems.Hits(m_SearchTextBox.Text, NbHits, HitsOnEmptySearch)); m_SearchTextBox.LostFocus += M_SearchTextBox_LostFocus; m_SearchTextBox.Loaded += M_SearchTextBox_Loaded; m_SearchTextBox.Initialized += M_SearchTextBox_Initialized; Grid.SetRow(m_SearchTextBox, 0); grid.Children.Add(m_SearchTextBox); m_SearchResultGrid = new Grid { Background = System.Windows.Media.Brushes.White }; m_SearchResultGrid.ColumnDefinitions.Add(new ColumnDefinition()); Grid.SetRow(m_SearchResultGrid, 1); grid.Children.Add(m_SearchResultGrid); container.Children.Add(m_Popup); m_SearchTextBox.PreviewKeyDown += M_SearchTextBox_PreviewKeyDown; } m_Popup.Unloaded += M_Popup_Unloaded; m_SearchTextBox.Text = ""; m_SearchResultGrid.Children.Clear(); m_Popup.IsOpen = true; m_SearchTextBox.Focus(); return(true); }
/*************************************/ private void M_Popup_KeyDown(object sender, KeyEventArgs e) { e.SuppressKeyPress = true; switch (e.KeyData) { case Keys.Up: if (--m_selected < 0) { m_selected = m_SearchResultPanel.Controls.Count - 1; } break; case Keys.Down: m_selected = (m_selected + 1) % m_SearchResultPanel.Controls.Count; break; case Keys.Enter: List <SearchItem> hits = PossibleItems.Hits(m_SearchTextBox.Text, NbHits, HitsOnEmptySearch); if (m_selected < hits.Count) { NotifySelection(hits[m_selected], new BH.oM.Geometry.Point { X = m_LastPosition.X, Y = m_LastPosition.Y }); m_Popup.Hide(); } return; case Keys.Escape: m_Popup.Hide(); return; default: e.SuppressKeyPress = false; return; } for (int i = 0; i < m_SearchResultPanel.Controls.Count; i++) { Control row = m_SearchResultPanel.Controls[i]; row.BackColor = (i == m_selected) ? System.Drawing.SystemColors.MenuHighlight : Color.Transparent; } }
/*************************************/ private void M_SearchTextBox_PreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = true; switch (e.Key) { case Key.Up: if (--m_selected < 0) { m_selected = m_hits - 1; } break; case Key.Down: m_selected = (m_selected + 1) % m_hits; break; case Key.Enter: List <SearchItem> hits = PossibleItems.Hits(m_SearchTextBox.Text, NbHits, HitsOnEmptySearch); if (m_selected < hits.Count) { NotifySelection(hits[m_selected], new BH.oM.Geometry.Point { X = m_Popup.PlacementRectangle.X, Y = m_Popup.PlacementRectangle.Y }); } m_Popup.IsOpen = false; return; case Key.Escape: m_Popup.IsOpen = false; return; default: e.Handled = false; return; } foreach (Label element in m_SearchResultGrid.Children.OfType <Label>()) { int i = Grid.GetRow(element); element.Background = (i == m_selected) ? System.Windows.SystemColors.HighlightBrush : System.Windows.Media.Brushes.White; } }
/*************************************/ /**** Public Methods ****/ /*************************************/ public override bool SetParent(object parent) { ContainerControl container = parent as ContainerControl; if (container == null) { return(false); } if (m_Popup == null || m_Popup.IsDisposed) { // Create the popup form m_Popup = new Form() { FormBorderStyle = FormBorderStyle.FixedSingle, KeyPreview = true, MaximizeBox = false, MinimizeBox = false, ShowIcon = false, ShowInTaskbar = false, SizeGripStyle = SizeGripStyle.Hide, StartPosition = FormStartPosition.Manual, ClientSize = new System.Drawing.Size(m_MinWidth, 22), ControlBox = false, BackColor = Color.White }; m_Popup.SuspendLayout(); m_Popup.Disposed += M_Popup_Disposed; //Add the search box m_SearchTextBox = new TextBox() { BorderStyle = BorderStyle.FixedSingle, Dock = DockStyle.Top, Font = new Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, GraphicsUnit.Point, 0), Location = new System.Drawing.Point(0, 0), Size = new System.Drawing.Size(m_MinWidth, 22), TabIndex = 0, TextAlign = System.Windows.Forms.HorizontalAlignment.Left, WordWrap = false, Text = "" }; m_SearchTextBox.TextChanged += (o, e) => RefreshSearchResults(PossibleItems.Hits(m_SearchTextBox.Text, NbHits, HitsOnEmptySearch)); m_SearchTextBox.LostFocus += M_SearchTextBox_LostFocus; m_Popup.Controls.Add(m_SearchTextBox); //Add the result panel m_SearchResultPanel = new Panel() { Location = new System.Drawing.Point(0, 25), Size = new System.Drawing.Size(m_MinWidth, 1), TabIndex = 1, }; m_Popup.Controls.Add(m_SearchResultPanel); m_Popup.KeyDown += M_Popup_KeyDown; } // Finish the popup form m_SearchTextBox.Text = ""; m_SearchTextBox.Width = m_MinWidth; m_SearchResultPanel.Controls.Clear(); m_SearchResultPanel.Size = new System.Drawing.Size(m_MinWidth, 1); m_LastPosition = System.Windows.Forms.Cursor.Position; int h = m_SearchTextBox.Height; int x = m_LastPosition.X - m_Popup.Width / 2; int y = m_LastPosition.Y - m_SearchTextBox.Height / 2; m_Popup.SetBounds(x, y, m_Popup.Width, h); m_Popup.ResumeLayout(false); m_Popup.PerformLayout(); m_Popup.Visible = false; // Make sure it is not visible before asking to show (otherwise, causes crash) m_Popup.Show(container); m_SearchTextBox.Focus(); RefreshSearchResults(new List <SearchItem>()); return(true); }
/*************************************/ public void ShowResults(string searchText) { SetSearchText(searchText); RefreshSearchResults(PossibleItems.Hits(searchText, NbHits, HitsOnEmptySearch)); }