View unanswered posts | View active topics



Reply to topic  [ 1 post ] 
BreakpointUI context menu 
Author Message
Member

Joined: Thu Feb 02, 2012 5:59 pm
Posts: 78
Location: Alabama
Reply with quote
I use the debugger quite a bit and somehow ended up with breakpoints registered for negative line numbers. I wanted to get rid of them but was unable to navigate to them (obviously). I was also unable to use the delete key or a context menu to do so.

This patch hooks KeyUp and on delete will delete the breakpoints that have selected cells. It also adds a context menu with Enable, Disable, and Delete menu items that act upon breakpoints that have selected cells.

I wanted to use labels like 'Delete selected' instead of simply 'Delete' to indicate that these actions act upon the selected breakpoints, however, I am not multilingual. I searched the existing resources for singular instances of the words I needed and made the appropriate changes to the locale strings.

Edit #1 - Added null check within dgv_SelectionChange to account for rows with empty Enabled cells.
Edit #2 - Preinitialized the context menu items to be disabled.
Edit #3 - Update context menu items enabled state after performing an action

Code:
Index: External/Plugins/FlashDebugger/Controls/BreakPointUI.cs
===================================================================
--- External/Plugins/FlashDebugger/Controls/BreakPointUI.cs   (revision 2207)
+++ External/Plugins/FlashDebugger/Controls/BreakPointUI.cs   (working copy)
@@ -4,6 +4,7 @@
 using System.Windows.Forms;
 using PluginCore.Localization;
 using PluginCore;
+using System.Collections.Generic;
 
 namespace FlashDebugger
 {
@@ -18,6 +19,9 @@
         private DataGridViewTextBoxColumn ColumnBreakPointLine;
         private DataGridViewTextBoxColumn ColumnBreakPointExp;
         private Color defaultColor;
+        private ToolStripMenuItem enableMenuItem;
+        private ToolStripMenuItem disableMenuItem;
+        private ToolStripMenuItem deleteMenuItem;
 
         public BreakPointUI(PluginMain pluginMain, BreakPointManager breakPointManager)
         {
@@ -63,7 +67,24 @@
             this.dgv.CellBorderStyle = DataGridViewCellBorderStyle.Single;
             this.dgv.EnableHeadersVisualStyles = true;
             this.dgv.RowHeadersVisible = false;
+            this.dgv.KeyUp += new KeyEventHandler(dgv_KeyUp);
 
+            ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
+         if (PluginBase.MainForm != null && PluginBase.Settings != null)
+         {
+            contextMenuStrip.Font = PluginBase.Settings.DefaultFont;
+                contextMenuStrip.Renderer = new DockPanelStripRenderer(false);
+         }
+            enableMenuItem = new ToolStripMenuItem(TextHelper.GetString("Label.Enable"), null, new EventHandler(this.EnableItemClick));
+            enableMenuItem.Enabled = false;
+            disableMenuItem = new ToolStripMenuItem(TextHelper.GetString("Label.Disable"), null, new EventHandler(this.DisableItemClick));
+            disableMenuItem.Enabled = false;
+            deleteMenuItem = new ToolStripMenuItem(TextHelper.GetString("Label.Delete"), null, new EventHandler(this.DeleteItemClick));
+            deleteMenuItem.Enabled = false;
+            contextMenuStrip.Items.AddRange(new ToolStripMenuItem[] { enableMenuItem, disableMenuItem, deleteMenuItem });
+            this.dgv.ContextMenuStrip = contextMenuStrip;
+            this.dgv.SelectionChanged += new EventHandler(dgv_SelectionChanged);
+
             DataGridViewCellStyle viewStyle = new DataGridViewCellStyle();
             viewStyle.Padding = new Padding(1);
             this.dgv.ColumnHeadersDefaultCellStyle = viewStyle;
@@ -110,6 +131,173 @@
             this.dgv.CellDoubleClick += new DataGridViewCellEventHandler(dgv_CellDoubleClick);
         }
 
+        void dgv_SelectionChanged(object sender, EventArgs e)
+        {
+            UpdateContextMenuItems();
+        }
+
+        void dgv_KeyUp(object sender, KeyEventArgs e)
+        {
+            if ((e.KeyValue & (int)Keys.Delete) == (int)Keys.Delete)
+            {
+                DeleteSelectedRows();
+            }
+        }
+
+        private void EnableItemClick(object sender, EventArgs e)
+        {
+            SetSelectedRowsEnabledState(true);
+        }
+
+        private void DisableItemClick(object sender, EventArgs e)
+        {
+            SetSelectedRowsEnabledState(false);
+        }
+
+        private void DeleteItemClick(object sender, EventArgs e)
+        {
+            DeleteSelectedRows();
+        }
+
+        private void UpdateContextMenuItems()
+        {
+            enableMenuItem.Enabled = false;
+            disableMenuItem.Enabled = false;
+            deleteMenuItem.Enabled = false;
+
+            List<DataGridViewRow> selectedRows = GetSelectedRows();
+            if (selectedRows.Count > 0)
+            {
+                bool hasDisabledBreakpoint = false;
+                bool hasEnabledBreakpoint = false;
+
+                foreach (var breakpointRow in selectedRows)
+                {
+                    if (breakpointRow.Cells["Enable"].Value != null)
+                    {
+                        bool isEnabled = (Boolean)breakpointRow.Cells["Enable"].Value;
+                        if (isEnabled == true)
+                            hasEnabledBreakpoint = true;
+                        else
+                            hasDisabledBreakpoint = true;
+                    }
+                }
+
+                enableMenuItem.Enabled = hasDisabledBreakpoint;
+                disableMenuItem.Enabled = hasEnabledBreakpoint;
+                deleteMenuItem.Enabled = true;
+            }
+        }
+
+        private void SetSelectedRowsEnabledState(bool setEnabled)
+        {
+            List<DataGridViewRow> selectedRows = GetSelectedRows();
+
+            foreach (var breakpointRow in selectedRows)
+            {
+                bool isEnabled = (Boolean)breakpointRow.Cells["Enable"].Value;
+
+                if (isEnabled != setEnabled)
+                {
+                    // Update breakpoint row enabled state
+                    breakpointRow.Cells["Enable"].Value = setEnabled;
+
+                    string filePath = (string)breakpointRow.Cells["FilePath"].Value;
+                    int line = int.Parse((string)breakpointRow.Cells["Line"].Value);
+
+                    // Update breakpoint marker enabled state
+                    ITabbedDocument doc = ScintillaHelper.GetDocument(filePath);
+                    if (doc != null)
+                    {
+                        // This logic should be handled by BPMAnager, wo we'll just work arround bad BPs and ignore them
+                        if (line < 1 || (doc.SciControl != null && line > doc.SciControl.LineCount)) return;
+                        Boolean isEnabledMarker = ScintillaHelper.IsMarkerSet(doc.SciControl, ScintillaHelper.markerBPEnabled, line - 1);
+                        if (setEnabled && !isEnabledMarker)
+                        {
+                            doc.SciControl.MarkerAdd(line - 1, ScintillaHelper.markerBPEnabled);
+                            doc.SciControl.MarkerDelete(line - 1, ScintillaHelper.markerBPDisabled);
+                        }
+                        else if (!setEnabled && isEnabledMarker)
+                        {
+                            doc.SciControl.MarkerAdd(line - 1, ScintillaHelper.markerBPDisabled);
+                            doc.SciControl.MarkerDelete(line - 1, ScintillaHelper.markerBPEnabled);
+                        }
+                    }
+
+                    // Update breakpoint enabled state
+                    BreakPointInfo breakpointInfo = GetBreakpoint(filePath, line);
+                    if (breakpointInfo != null)
+                        breakpointInfo.IsEnabled = setEnabled;
+                }
+            }
+
+            UpdateContextMenuItems();
+        }
+
+        private void DeleteSelectedRows()
+        {
+            List<DataGridViewRow> selectedRows = GetSelectedRows();
+
+            foreach (var breakpointRow in selectedRows)
+            {
+                string filePath = (string)breakpointRow.Cells["FilePath"].Value;
+                int line = int.Parse((string)breakpointRow.Cells["Line"].Value);
+
+                // Remove breakpoint marker from editor control
+                ITabbedDocument doc = ScintillaHelper.GetDocument(filePath);
+                if (doc != null)
+                {
+                    // This logic should be handled by BPMAnager, wo we'll just work arround bad BPs and ignore them
+                    if (line > 0 &&
+                        doc.SciControl != null &&
+                        line <= doc.SciControl.LineCount)
+                    {
+                        Boolean isDisabledMarker = ScintillaHelper.IsMarkerSet(doc.SciControl, ScintillaHelper.markerBPDisabled, line - 1);
+                        if (isDisabledMarker)
+                            doc.SciControl.MarkerDelete(line - 1, ScintillaHelper.markerBPDisabled);
+                        else
+                            doc.SciControl.MarkerDelete(line - 1, ScintillaHelper.markerBPEnabled);
+                    }
+                }
+
+                // Set breakpoint to deleted
+                BreakPointInfo breakpointInfo = GetBreakpoint(filePath, line);
+                if (breakpointInfo != null)
+                    breakpointInfo.IsDeleted = true;
+
+                // Remove breakpoint row from grid
+                this.DeleteItem(filePath, line);
+            }
+
+            UpdateContextMenuItems();
+        }
+
+        private List<DataGridViewRow> GetSelectedRows()
+        {
+            List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
+            foreach (DataGridViewCell selectedCell in dgv.SelectedCells)
+            {
+                if (!selectedRows.Contains(selectedCell.OwningRow))
+                {
+                    selectedRows.Add(selectedCell.OwningRow);
+                }
+            }
+            return selectedRows;
+        }
+
+        private BreakPointInfo GetBreakpoint(string filePath, int line)
+        {
+            foreach (var breakpointInfo in this.breakPointManager.BreakPoints)
+            {
+                if (breakpointInfo.FileFullPath == filePath &&
+                    breakpointInfo.Line == line)
+                {
+                    return breakpointInfo;
+                }
+            }
+            return null;
+        }
+
         void dgv_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
         {
             if (e.RowIndex < 0 || e.RowIndex >= dgv.Rows.Count) return;
Index: PluginCore/PluginCore/Resources/de_DE.resX
===================================================================
--- PluginCore/PluginCore/Resources/de_DE.resX   (revision 2207)
+++ PluginCore/PluginCore/Resources/de_DE.resX   (working copy)
@@ -4831,4 +4831,10 @@
     <value>Direkt</value>
     <comment>Added after 3.3.4</comment>
   </data>
+  <data name="FlashDebugger.Label.Delete" xml:space="preserve">
+    <value>Löschen</value>
+  </data>
+  <data name="FlashDebugger.Label.Disable" xml:space="preserve">
+    <value>Deaktivieren</value>
+  </data>
 </root>
\ No newline at end of file
Index: PluginCore/PluginCore/Resources/en_US.resX
===================================================================
--- PluginCore/PluginCore/Resources/en_US.resX   (revision 2207)
+++ PluginCore/PluginCore/Resources/en_US.resX   (working copy)
@@ -4831,4 +4831,10 @@
     <value>Direct</value>
     <comment>Added after 3.3.4</comment>
   </data>
+  <data name="FlashDebugger.Label.Delete" xml:space="preserve">
+    <value>Delete</value>
+  </data>
+  <data name="FlashDebugger.Label.Disable" xml:space="preserve">
+    <value>Disable</value>
+  </data>
 </root>
\ No newline at end of file
Index: PluginCore/PluginCore/Resources/eu_EU.resX
===================================================================
--- PluginCore/PluginCore/Resources/eu_EU.resX   (revision 2207)
+++ PluginCore/PluginCore/Resources/eu_EU.resX   (working copy)
@@ -4824,4 +4824,10 @@
     <value>Zuzena</value>
     <comment>Added after 3.3.4</comment>
   </data>
+  <data name="FlashDebugger.Label.Delete" xml:space="preserve">
+    <value>Ezabatu</value>
+  </data>
+  <data name="FlashDebugger.Label.Disable" xml:space="preserve">
+    <value>Desgaitu</value>
+  </data>
 </root>
\ No newline at end of file
Index: PluginCore/PluginCore/Resources/ja_JP.resX
===================================================================
--- PluginCore/PluginCore/Resources/ja_JP.resX   (revision 2207)
+++ PluginCore/PluginCore/Resources/ja_JP.resX   (working copy)
@@ -4888,4 +4888,10 @@
     <value>Direct</value>
     <comment>Added after 3.3.4</comment>
   </data>
+  <data name="FlashDebugger.Label.Delete" xml:space="preserve">
+    <value>削除</value>
+  </data>
+  <data name="FlashDebugger.Label.Disable" xml:space="preserve">
+    <value>無効</value>
+  </data>
 </root>
\ No newline at end of file



Sun Feb 05, 2012 8:28 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.