Exemplo n.º 1
0
 protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
 {
     e.IsInputKey = true;
     switch(e.KeyCode)
     {
         case Keys.Enter:
             if(_itemFocus.IsTracked)
             {
                 var item = _itemFocus.Item;
                 item.Activate();
             }
             break;
         case Keys.Escape:
             if(_columnResizeProcess != null)
             {
                 _columnResizeProcess.Cancel();
                 _columnResizeProcess = null;
             }
             break;
         case Keys.Right:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index < 0 || _itemFocus.Index >= _itemPlainList.Count)
             {
                 FocusAndSelectItem(0);
             }
             else
             {
                 var item = _itemPlainList[_itemFocus.Index];
                 if(item.Items.Count == 0) return;
                 if(item.IsExpanded)
                 {
                     FocusAndSelectItem(_itemFocus.Index + 1);
                 }
                 else
                 {
                     item.IsExpanded = true;
                 }
             }
             break;
         case Keys.Left:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index < 0 || _itemFocus.Index >= _itemPlainList.Count)
             {
                 FocusAndSelectItem(_itemPlainList.Count - 1);
             }
             else
             {
                 var item = _itemPlainList[_itemFocus.Index];
                 if(item.IsExpanded)
                 {
                     if(_showRootTreeLines || item.Level != 0)
                     {
                         item.IsExpanded = false;
                     }
                 }
                 else
                 {
                     if(item.Parent == null) return;
                     FocusAndSelectItem(_itemPlainList.IndexOf(item.Parent));
                 }
             }
             break;
         case Keys.Home:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index != 0)
             {
                 FocusAndSelectItem(0);
             }
             else
             {
                 EnsureVisible(_itemFocus.Index);
             }
             break;
         case Keys.End:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index != _itemPlainList.Count - 1)
             {
                 FocusAndSelectItem(_itemPlainList.Count - 1);
             }
             else
             {
                 EnsureVisible(_itemFocus.Index);
             }
             break;
         case Keys.PageUp:
             if(_itemPlainList.Count == 0) return;
             {
                 var index = _itemFocus.Index - GetItemsOnPage();
                 if(index < 0) index = 0;
                 if(_itemFocus.Index != index)
                 {
                     FocusAndSelectItem(index);
                 }
                 else
                 {
                     EnsureVisible(_itemFocus.Index);
                 }
             }
             break;
         case Keys.PageDown:
             if(_itemPlainList.Count == 0) return;
             {
                 var index = _itemFocus.Index + GetItemsOnPage();
                 if(index >= _itemPlainList.Count) index = _itemPlainList.Count - 1;
                 if(_itemFocus.Index != index)
                 {
                     FocusAndSelectItem(index);
                 }
                 else
                 {
                     EnsureVisible(_itemFocus.Index);
                 }
             }
             break;
         case Keys.Up:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index > 0)
             {
                 int index = _itemFocus.Index - 1;
                 if(Control.ModifierKeys == Keys.Control)
                 {
                     FocusItem(index);
                 }
                 else
                 {
                     FocusAndSelectItem(index);
                 }
             }
             break;
         case Keys.Down:
             if(_itemPlainList.Count == 0) return;
             if(_itemFocus.Index < _itemPlainList.Count - 1)
             {
                 int index = _itemFocus.Index + 1;
                 if(Control.ModifierKeys == Keys.Control)
                 {
                     FocusItem(index);
                 }
                 else
                 {
                     FocusAndSelectItem(index);
                 }
             }
             break;
         case Keys.Space:
             switch(_selectedItems.Count)
             {
                 case 0:
                     if(_itemFocus.IsTracked)
                     {
                         FocusAndSelectItem(_itemFocus.Index);
                     }
                     break;
                 default:
                     if(Control.ModifierKeys == Keys.Control)
                     {
                         if(_itemFocus.IsTracked)
                         {
                             var item = _itemPlainList[_itemFocus.Index];
                             if(item.IsSelected)
                             {
                                 _selectedItems.Remove(item);
                                 InvalidateItem(_itemFocus.Index);
                             }
                             else if(_multiselect)
                             {
                                 _selectedItems.Add(item);
                                 InvalidateItem(_itemFocus.Index);
                             }
                         }
                     }
                     else
                     {
                         if(_selectedItems.Count == 1)
                         {
                             if(_showCheckBoxes)
                             {
                                 var item = _selectedItems[0];
                                 switch(item.CheckedState)
                                 {
                                     case CheckedState.Checked:
                                         item.CheckedState = CheckedState.Unchecked;
                                         break;
                                     case CheckedState.Unchecked:
                                         item.CheckedState = CheckedState.Checked;
                                         break;
                                 }
                             }
                             else
                             {
                                 FocusAndSelectItem(_itemFocus.Index);
                             }
                         }
                     }
                     break;
             }
             break;
         case Keys.A:
             if(_multiselect && Control.ModifierKeys == Keys.Control)
             {
                 if(_selectedItems.Count != _itemPlainList.Count)
                 {
                     _selectedItems.Clear();
                     _selectedItems.AddRange(_itemPlainList);
                     Invalidate(_itemsArea);
                 }
             }
             break;
         case (Keys)0x5D:
             {
                 ToolStripDropDown menu;
                 var p = _itemsArea.Location;
                 p.X += _itemHeight / 2;
                 int columnIndex = -1;
                 CustomListBoxColumn column = null;
                 for(int i = 0; i < _columns.Count; ++i)
                 {
                     if(_columns[i].IsVisible)
                     {
                         columnIndex = i;
                         column = _columns[i];
                         break;
                     }
                 }
                 switch(_selectedItems.Count)
                 {
                     case 0:
                         {
                             var args = new ContextMenuRequestEventArgs(column, columnIndex, p.X, p.Y);
                             Events.Raise(ContextMenuRequestedEvent, this, args);
                             if(args.OverrideDefaultMenu)
                             {
                                 menu = args.ContextMenu;
                             }
                             else
                             {
                                 menu = GetFreeSpaceContextMenu(args);
                             }
                         }
                         break;
                     case 1:
                         {
                             var index = _itemPlainList.IndexOf(_selectedItems[0]);
                             EnsureVisible(index);
                             p.Y = GetItemY1Offset(index) + _itemsArea.Top + _itemHeight / 2;
                             var args = new ItemContextMenuRequestEventArgs(_selectedItems[0], column, columnIndex, p.X, p.Y);
                             Events.Raise(ItemContextMenuRequestedEvent, this, args);
                             if(args.OverrideDefaultMenu)
                             {
                                 menu = args.ContextMenu;
                             }
                             else
                             {
                                 menu = _selectedItems[0].GetContextMenu(args);
                             }
                         }
                         break;
                     default:
                         {
                             var args = new ItemsContextMenuRequestEventArgs(_selectedItems, column, columnIndex, p.X, p.Y);
                             Events.Raise(ItemsContextMenuRequestedEvent, this, args);
                             if(args.OverrideDefaultMenu)
                             {
                                 menu = args.ContextMenu;
                             }
                             else
                             {
                                 menu = GetMultiselectContextMenu(args);
                             }
                         }
                         break;
                 }
                 if(menu != null)
                 {
                     menu.Renderer = Style.ToolStripRenderer;
                     menu.Show(this, p, ToolStripDropDownDirection.Default);
                 }
             }
             break;
         default:
             e.IsInputKey = false;
             break;
     }
     base.OnPreviewKeyDown(e);
 }
Exemplo n.º 2
0
        private void HandleFreeSpaceMouseDown(MouseEventArgs e)
        {
            if(_selectedItems.Count > 0)
            {
                int[] indices = new int[_selectedItems.Count];
                for(int i = 0; i < indices.Length; ++i)
                {
                    indices[i] = _itemPlainList.IndexOf(_selectedItems[i]);
                }
                _selectedItems.Clear();
                for(int i = 0; i < indices.Length; ++i)
                {
                    Invalidate(GetItemDisplayRect(indices[i]));
                }
            }
            if(e.Button == MouseButtons.Right)
            {
                if(!_disableContextMenus)
                {
                    var x = e.X;
                    var y = e.Y;
                    var cid = GetColumn(ref x, y);
                    var col = (cid != -1) ? (_columns[cid]) : null;

                    var args = new ContextMenuRequestEventArgs(col, cid, x, y);
                    Events.Raise(ContextMenuRequestedEvent, this, args);
                    ContextMenuStrip menu;
                    if(args.OverrideDefaultMenu)
                    {
                        menu = args.ContextMenu;
                    }
                    else
                    {
                        menu = GetFreeSpaceContextMenu(args);
                    }
                    if(menu != null)
                    {
                        menu.Renderer = Style.ToolStripRenderer;
                        menu.Show(this, e.Location);
                    }
                }
            }
        }
Exemplo n.º 3
0
 protected virtual ContextMenuStrip GetFreeSpaceContextMenu(ContextMenuRequestEventArgs requestEventArgs)
 {
     return null;
 }