~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Refactoring/MonoDevelop.CodeActions/CodeActionWidget.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
ļ»æ// 
 
1
// 
2
2
// QuickFixWidget.cs
3
3
//  
4
4
// Author:
39
39
using ICSharpCode.NRefactory.Semantics;
40
40
using MonoDevelop.CodeActions;
41
41
using MonoDevelop.Refactoring;
 
42
using MonoDevelop.Projects;
42
43
 
43
44
namespace MonoDevelop.CodeActions
44
45
{
79
80
                        base.OnDestroyed ();
80
81
                }
81
82
                
82
 
                public void PopupQuickFixMenu ()
 
83
                public void PopupQuickFixMenu (Action<Gtk.Menu> menuAction = null)
83
84
                {
84
 
                        PopupQuickFixMenu (null);
 
85
                        PopupQuickFixMenu (null, menuAction);
85
86
                }
86
87
 
87
88
                static CodeActionWidget ()
113
114
                        return result;
114
115
                }
115
116
 
116
 
                public void PopulateFixes (Gtk.Menu menu)
 
117
                public void PopulateFixes (Gtk.Menu menu, ref int items)
117
118
                {
118
119
                        int mnemonic = 1;
119
120
                        foreach (var fix_ in fixes.OrderByDescending (i => GetUsage (i.IdString))) {
120
121
                                var fix = fix_;
121
122
                                var escapedLabel = fix.Title.Replace ("_", "__");
122
123
                                var label = (mnemonic <= 10)
123
 
                                                ? "_" + (mnemonic++ % 10).ToString () + " " + escapedLabel
 
124
                                        ? "_" + (mnemonic++ % 10).ToString () + " " + escapedLabel
124
125
                                                : "  " + escapedLabel;
125
126
                                var menuItem = new Gtk.MenuItem (label);
126
127
                                menuItem.Activated += new ContextActionRunner (fix, document, loc).Run;
129
130
                                        menu.Destroy ();
130
131
                                };
131
132
                                menu.Add (menuItem);
 
133
                                items++;
132
134
                        }
133
135
                        var first = true;
134
136
                        var alreadyInserted = new HashSet<CodeIssueProvider> ();
145
147
                                if (alreadyInserted.Contains (ir.Inspector))
146
148
                                        continue;
147
149
                                alreadyInserted.Add (ir.Inspector);
148
 
                        
 
150
                                
149
151
                                var label = GettextCatalog.GetString ("_Inspection options for \"{0}\"", ir.Inspector.Title);
150
152
                                var menuItem = new Gtk.MenuItem (label);
151
153
                                menuItem.Activated += analysisFix.ShowOptions;
153
155
                                        menu.Destroy ();
154
156
                                };
155
157
                                menu.Add (menuItem);
156
 
                        }
157
 
 
 
158
                                items++;
 
159
                        }
 
160
 
 
161
                        foreach (var fix_ in fixes.Where (f => f.BoundToIssue != null)) {
 
162
                                var fix = fix_;
 
163
                                foreach (var inspector_ in RefactoringService.GetInspectors (document.Editor.MimeType).Where (i => i.GetSeverity () != ICSharpCode.NRefactory.CSharp.Severity.None)) {
 
164
                                        var inspector = inspector_;
 
165
 
 
166
                                        if (inspector.IdString.IndexOf (fix.BoundToIssue.FullName, StringComparison.Ordinal) < 0)
 
167
                                                continue;
 
168
                                        if (first) {
 
169
                                                menu.Add (new Gtk.SeparatorMenuItem ());
 
170
                                                first = false;
 
171
                                        }
 
172
                                        if (alreadyInserted.Contains (inspector))
 
173
                                                continue;
 
174
                                        alreadyInserted.Add (inspector);
 
175
                                        
 
176
                                        var label = GettextCatalog.GetString ("_Inspection options for \"{0}\"", inspector.Title);
 
177
                                        var menuItem = new Gtk.MenuItem (label);
 
178
                                        menuItem.Activated += delegate {
 
179
                                                MessageService.RunCustomDialog (new CodeIssueOptionsDialog (inspector), MessageService.RootWindow);
 
180
                                                menu.Destroy ();
 
181
                                        };
 
182
                                        menu.Add (menuItem);
 
183
                                        break;
 
184
                                }
 
185
 
 
186
                                items++;
 
187
                        }
158
188
                }
159
189
                
160
 
                void PopupQuickFixMenu (Gdk.EventButton evt)
 
190
                void PopupQuickFixMenu (Gdk.EventButton evt, Action<Gtk.Menu> menuAction)
161
191
                {
162
192
                        var menu = new Gtk.Menu ();
163
 
 
 
193
                        menu.Events |= Gdk.EventMask.AllEventsMask;
164
194
                        Gtk.Menu fixMenu = menu;
165
195
                        ResolveResult resolveResult;
166
196
                        ICSharpCode.NRefactory.CSharp.AstNode node;
 
197
                        int items = 0;
167
198
                        if (ResolveCommandHandler.ResolveAt (document, out resolveResult, out node)) {
168
199
                                var possibleNamespaces = MonoDevelop.Refactoring.ResolveCommandHandler.GetPossibleNamespaces (
169
200
                                        document,
170
201
                                        node,
171
 
                                        resolveResult
 
202
                                        ref resolveResult
172
203
                                );
173
204
        
174
205
                                bool addUsing = !(resolveResult is AmbiguousTypeResolveResult);
175
206
                                if (addUsing) {
176
 
                                        foreach (string ns_ in possibleNamespaces) {
177
 
                                                string ns = ns_;
 
207
                                        foreach (var t in possibleNamespaces.Where (tp => tp.IsAccessibleWithGlobalUsing)) {
 
208
                                                string ns = t.Namespace;
 
209
                                                var reference = t.Reference;
178
210
                                                var menuItem = new Gtk.MenuItem (string.Format ("using {0};", ns));
179
211
                                                menuItem.Activated += delegate {
180
 
                                                        new MonoDevelop.Refactoring.ResolveCommandHandler.AddImport (document, resolveResult, ns, true).Run ();
 
212
                                                        new ResolveCommandHandler.AddImport (document, resolveResult, ns, reference, true, node).Run ();
 
213
                                                        if (reference != null)
 
214
                                                                document.Project.Items.Add (reference);
181
215
                                                        menu.Destroy ();
182
216
                                                };
183
217
                                                menu.Add (menuItem);
 
218
                                                items++;
184
219
                                        }
185
220
                                }
186
221
                                
187
222
                                bool resolveDirect = !(resolveResult is UnknownMemberResolveResult);
188
223
                                if (resolveDirect) {
189
 
                                        foreach (string ns in possibleNamespaces) {
 
224
                                        foreach (var t in possibleNamespaces) {
 
225
                                                string ns = t.Namespace;
 
226
                                                var reference = t.Reference;
190
227
                                                var menuItem = new Gtk.MenuItem (GettextCatalog.GetString ("{0}", ns + "." + document.Editor.GetTextBetween (node.StartLocation, node.EndLocation)));
191
228
                                                menuItem.Activated += delegate {
192
 
                                                        new MonoDevelop.Refactoring.ResolveCommandHandler.AddImport (document, resolveResult, ns, false).Run ();
 
229
                                                        new ResolveCommandHandler.AddImport (document, resolveResult, ns, reference, false, node).Run ();
193
230
                                                        menu.Destroy ();
194
231
                                                };
195
232
                                                menu.Add (menuItem);
 
233
                                                items++;
196
234
                                        }
197
235
                                }
198
236
                                if (menu.Children.Any () && fixes.Any ()) {
200
238
                                        var menuItem = new Gtk.MenuItem (GettextCatalog.GetString ("Quick Fixes"));
201
239
                                        menuItem.Submenu = fixMenu;
202
240
                                        menu.Add (menuItem);
 
241
                                        items++;
203
242
                                }
204
243
                        }
205
244
                        
206
 
                        PopulateFixes (fixMenu);
207
 
                        
 
245
                        PopulateFixes (fixMenu, ref items);
 
246
                        if (items == 0) {
 
247
                                menu.Destroy ();
 
248
                                return;
 
249
                        }
 
250
                        document.Editor.SuppressTooltips = true;
 
251
                        document.Editor.Parent.HideTooltip ();
 
252
                        if (menuAction != null)
 
253
                                menuAction (menu);
208
254
                        menu.ShowAll ();
209
255
                        menu.SelectFirst (true);
210
256
                        menuPushed = true;
 
257
                        menu.Hidden += delegate {
 
258
                                document.Editor.SuppressTooltips = false;
 
259
                        };
211
260
                        menu.Destroyed += delegate {
212
261
                                menuPushed = false;
213
262
                                Hide ();
214
263
                        };
215
 
                        var container = (TextEditorContainer)document.Editor.Parent.Parent;
216
 
                        var child = (TextEditorContainer.EditorContainerChild)container [this];
217
 
                        GtkWorkarounds.ShowContextMenu (menu, document.Editor.Parent, null, new Gdk.Rectangle (child.X, child.Y + Allocation.Height - (int)document.Editor.VAdjustment.Value, 0, 0));
 
264
                        var container = document.Editor.Parent;
 
265
                        var child = (TextEditor.EditorContainerChild)container [this];
 
266
 
 
267
                        Gdk.Rectangle rect;
 
268
/*                      if (child != null) {
 
269
                                rect = new Gdk.Rectangle (child.X, child.Y + Allocation.Height - (int)document.Editor.VAdjustment.Value, 0, 0);
 
270
                        } else {*/
 
271
                                var p = container.LocationToPoint (loc);
 
272
                                rect = new Gdk.Rectangle (p.X + container.Allocation.X , p.Y + (int)document.Editor.LineHeight + container.Allocation.Y, 0, 0);
 
273
                        //}
 
274
                        GtkWorkarounds.ShowContextMenu (menu, document.Editor.Parent, null, rect);
218
275
                }
219
276
 
220
277
                class ContextActionRunner
239
296
                protected override bool OnButtonPressEvent (Gdk.EventButton evnt)
240
297
                {
241
298
                        if (!evnt.TriggersContextMenu () && evnt.Button == 1)
242
 
                                PopupQuickFixMenu (evnt);
 
299
                                PopupQuickFixMenu (evnt, null);
243
300
                        return base.OnButtonPressEvent (evnt);
244
301
                }
245
302
                
269
326
                        using (var cr = Gdk.CairoHelper.Create (evnt.Window)) {
270
327
                                cr.LineWidth = border;
271
328
                                cr.Rectangle (0, 0, Allocation.Width, Allocation.Height);
272
 
                                cr.Color = document.Editor.ColorStyle.Default.CairoBackgroundColor;
 
329
                                cr.Color = document.Editor.ColorStyle.PlainText.Background;
273
330
                                cr.Fill ();
274
331
                                
275
332
                                FoldingScreenbackgroundRenderer.DrawRoundRectangle (cr,
276
333
                                        true, true,
277
334
                                        0, 0, Allocation.Width / 2, 
278
335
                                        Allocation.Width, Allocation.Height);
279
 
                                cr.Color = isMouseInside || menuPushed ? document.Editor.ColorStyle.Default.CairoColor : document.Editor.ColorStyle.FoldLine.CairoColor;
 
336
                                cr.Color = isMouseInside || menuPushed ? document.Editor.ColorStyle.PlainText.Foreground : document.Editor.ColorStyle.FoldLineColor.Color;
280
337
                                cr.Stroke ();
281
 
                                
282
 
                                evnt.Window.DrawPixbuf (Style.BaseGC (State), icon, 
283
 
                                        0, 0, 
284
 
                                        (Allocation.Width - icon.Width) / 2, (Allocation.Height - icon.Height) / 2, 
285
 
                                        icon.Width, icon.Height, 
286
 
                                        Gdk.RgbDither.None, 0, 0);
 
338
 
 
339
                                Gdk.CairoHelper.SetSourcePixbuf (cr, icon, (Allocation.Width - icon.Width) / 2, (Allocation.Height - icon.Height) / 2);
 
340
                                cr.Paint ();
287
341
                        }
288
342
                        
289
343
                        return true;