protected override ContextMenuStrip GetMultiselectContextMenu(ItemsContextMenuRequestEventArgs requestEventArgs) { var stagedStatus = StagedStatus.None; var items = new TreeItem[requestEventArgs.Items.Count]; int id = 0; foreach(var item in requestEventArgs.Items) { var treeItem = ((ITreeItemListItem)item).TreeItem; items[id++] = treeItem; stagedStatus |= treeItem.StagedStatus; } ContextMenuStrip menu; switch(stagedStatus) { case StagedStatus.Staged: menu = new ContextMenuStrip(); menu.Items.Add(GuiItemFactory.GetUnstageItem<ToolStripMenuItem>(_repository, items)); break; case StagedStatus.Unstaged: menu = new ContextMenuStrip(); menu.Items.Add(GuiItemFactory.GetStageItem<ToolStripMenuItem>(_repository, items)); if(HasRevertableItems(items)) { menu.Items.Add(GuiItemFactory.GetRevertPathsItem<ToolStripMenuItem>(items)); } break; default: menu = null; break; } if(menu != null) { Utility.MarkDropDownForAutoDispose(menu); } return menu; }
protected override ContextMenuStrip GetMultiselectContextMenu(ItemsContextMenuRequestEventArgs requestEventArgs) { if(requestEventArgs.Items.Count != 2) return null; var revisions = new List<Revision>(requestEventArgs.Items.Count); foreach(var item in requestEventArgs.Items) { var revItem = item as RevisionListItem; if(revItem != null) { revisions.Add(revItem.DataContext); } } if(revisions.Count == 2) { var menu = new RevisionsMenu(revisions); Utility.MarkDropDownForAutoDispose(menu); return menu; } else { return null; } }
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); }
private void HandleItemDefaultMouseDown(int itemIndex, MouseEventArgs e) { EnsureVisible(itemIndex); switch(Control.ModifierKeys) { case Keys.Control: if(_multiselect) { var id = _selectedItems.IndexOf(_itemPlainList[itemIndex]); if(id != -1) { _selectedItems.RemoveAt(id); } else { _itemFocus.Track(itemIndex, _itemPlainList[itemIndex]); _selectedItems.Add(_itemPlainList[itemIndex]); } InvalidateItem(itemIndex); _lastClickedItemIndex = itemIndex; } break; case Keys.Shift: if(_multiselect) { int startId = _lastClickedItemIndex; if(startId == -1) startId = _itemFocus.Index; if(startId == -1) startId = 0; int endId = itemIndex; if(endId < startId) { var temp = endId; endId = startId; startId = temp; } int minY = GetItemY1Offset(startId); int maxY = GetItemY2Offset(endId); for(int i = 0; i < _selectedItems.Count; ++i) { int id = _itemPlainList.IndexOf(_selectedItems[i]); var y1 = GetItemY1Offset(id); if(y1 >= _itemsArea.Height || y1 <= -_itemHeight) { continue; } var y2 = y1 + _itemHeight - 1; if(y1 < minY) minY = y1; if(y2 > maxY) maxY = y2; } _selectedItems.Clear(); for(int i = startId; i <= endId; ++i) { _selectedItems.Add(_itemPlainList[i]); } Invalidate(Rectangle.Intersect(_itemsArea, new Rectangle(_itemsArea.X, _itemsArea.Y + minY, _itemsArea.Width, maxY - minY + 1))); } break; default: var item = _itemPlainList[itemIndex]; if(e.Button != MouseButtons.Right || !_multiselect || !item.IsSelected) { _itemFocus.Track(itemIndex, item); _lastClickedItemIndex = itemIndex; var rc = GetItemDisplayRect(itemIndex); if(_selectedItems.Count == 0) { _selectedItems.Add(item); Invalidate(GetItemDisplayRect(itemIndex)); } else { if(_selectedItems.Count == 1) { if(_selectedItems[0] != _itemPlainList[itemIndex]) { var index = _itemPlainList.IndexOf(_selectedItems[0]); _selectedItems[0] = item; Invalidate(GetItemDisplayRect(index)); Invalidate(rc); } } else { if(_selectedItems.Contains(item)) { var indices = new int[_selectedItems.Count - 1]; int j = 0; for(int i = 0; i < _selectedItems.Count; ++i) { if(_selectedItems[i] != item) { indices[j++] = _itemPlainList.IndexOf(_selectedItems[i]); } } _selectedItems.Clear(); _selectedItems.Add(item); for(int i = 0; i < indices.Length; ++i) { Invalidate(GetItemDisplayRect(indices[i])); } } else { var indices = new int[_selectedItems.Count + 1]; indices[0] = itemIndex; for(int i = 0; i < _selectedItems.Count; ++i) { indices[i + 1] = _itemPlainList.IndexOf(_selectedItems[i]); } _selectedItems.Clear(); _selectedItems.Add(item); for(int i = 0; i < indices.Length; ++i) { Invalidate(GetItemDisplayRect(indices[i])); } } } } item.OnMouseDown(e.Button, e.X - rc.X, e.Y - rc.Y); } switch(e.Button) { case MouseButtons.Left: if(_itemActivation == ItemActivation.SingleClick) { item.Activate(); } break; case 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; switch(_selectedItems.Count) { case 1: { int cmnuX = e.X - _itemsArea.X; int cmnuY = e.Y - _itemsArea.Y; var args = new ItemContextMenuRequestEventArgs(item, col, cid, cmnuX, cmnuY); Events.Raise(ItemContextMenuRequestedEvent, this, args); var menu = args.OverrideDefaultMenu ? args.ContextMenu : item.GetContextMenu(args); if(menu != null) { menu.Renderer = Style.ToolStripRenderer; menu.Show(this, e.Location); } } break; default: { int cmnuX = e.X - _itemsArea.X; int cmnuY = e.Y - _itemsArea.Y; var args = new ItemsContextMenuRequestEventArgs(_selectedItems, col, cid, cmnuX, cmnuY); Events.Raise(ItemsContextMenuRequestedEvent, this, args); var menu = args.OverrideDefaultMenu ? args.ContextMenu : GetMultiselectContextMenu(args); if(menu != null) { menu.Renderer = Style.ToolStripRenderer; menu.Show(this, e.Location); } } break; } } break; } break; } }
protected virtual ContextMenuStrip GetMultiselectContextMenu(ItemsContextMenuRequestEventArgs requestEventArgs) { return null; }