Example #1
0
        protected override void OnMouseDown(int x, int y, MouseButtons button)
        {
            switch (button)
            {
            case MouseButtons.Left:
            {
                var htr = HitTest(x, y);
                if (htr.Line != -1)
                {
                    if (_selOrigin == -1 || Control.ModifierKeys != Keys.Shift)
                    {
                        _selOrigin = htr.Line;
                        SetSelection(htr.Line);
                    }
                    else
                    {
                        SetSelection(_selOrigin, htr.Line);
                    }
                    _selecting = true;
                }
                else
                {
                    DropSelection();
                }
            }
            break;

            case MouseButtons.Right:
            {
                var htr = HitTest(x, y);
                if (htr.Line != -1)
                {
                    if (htr.Line < _selStart || htr.Line > _selEnd)
                    {
                        _selOrigin = htr.Line;
                        SetSelection(htr.Line);
                    }
                    var menu  = new ContextMenuStrip();
                    var lines = GetSelectedLines();

                    if (_repository != null && _diffFile.Status == FileStatus.Modified && !_diffFile.IsBinary)
                    {
                        bool hasModifiedLines = false;
                        foreach (var line in lines)
                        {
                            if (line.State == DiffLineState.Added || line.State == DiffLineState.Removed)
                            {
                                hasModifiedLines = true;
                                break;
                            }
                        }
                        if (hasModifiedLines)
                        {
                            switch (_diffType)
                            {
                            case DiffType.StagedChanges:
                                menu.Items.Add(new ToolStripMenuItem(
                                                   Resources.StrUnstageSelection,
                                                   CachedResources.Bitmaps["ImgUnstage"],
                                                   OnUnstageSelectionClick));
                                menu.Items.Add(new ToolStripSeparator());
                                break;

                            case DiffType.UnstagedChanges:
                                menu.Items.Add(new ToolStripMenuItem(
                                                   Resources.StrStageSelection,
                                                   CachedResources.Bitmaps["ImgStage"],
                                                   OnStageSelectionClick));
                                menu.Items.Add(new ToolStripSeparator());
                                break;
                            }
                        }
                    }

                    menu.Items.Add(GuiItemFactory.GetCopyDiffLinesItem <ToolStripMenuItem>(lines, false));
                    menu.Items.Add(GuiItemFactory.GetCopyDiffLinesItem <ToolStripMenuItem>(lines, true));
                    if (lines.Length > 1)
                    {
                        menu.Items.Add(new ToolStripSeparator());
                        menu.Items.Add(GuiItemFactory.GetCopyDiffLinesItem <ToolStripMenuItem>(lines,
                                                                                               Resources.StrCopyNewVersion, false, DiffLineState.Added | DiffLineState.Context));
                        menu.Items.Add(GuiItemFactory.GetCopyDiffLinesItem <ToolStripMenuItem>(lines,
                                                                                               Resources.StrCopyOldVersion, false, DiffLineState.Removed | DiffLineState.Context));
                    }
                    Utility.MarkDropDownForAutoDispose(menu);
                    ShowContextMenu(menu, x, y);
                }
                else
                {
                    if (htr.Area == 0)
                    {
                        var viewer = FlowControl as DiffViewer;
                        if (viewer != null)
                        {
                            viewer.OnFileContextMenuRequested(_diffFile);
                        }
                    }
                }
            }
            break;
            }
            base.OnMouseDown(x, y, button);
        }