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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.Commands/CommandManager.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:
58
58
                ArrayList visitors = new ArrayList ();
59
59
                Dictionary<Gtk.Window,Gtk.Window> topLevelWindows = new Dictionary<Gtk.Window,Gtk.Window> ();
60
60
                Stack delegatorStack = new Stack ();
61
 
                
 
61
 
62
62
                HashSet<object> visitedTargets = new HashSet<object> ();
63
63
                
64
64
                bool disposed;
74
74
                internal static readonly object CommandRouteTerminator = new object ();
75
75
                
76
76
                internal bool handlerFoundInMulticast;
77
 
                
 
77
                Gtk.Widget lastActiveWidget;
 
78
 
78
79
                public CommandManager (): this (null)
79
80
                {
80
81
                }
266
267
                                isEnabled = value;
267
268
                        }
268
269
                }
269
 
                
 
270
 
 
271
 
 
272
                /// <summary>
 
273
                /// The command currently being executed or for which the status is being checked
 
274
                /// </summary>
 
275
                public Command CurrentCommand { get; private set; }
 
276
 
270
277
                bool CanUseBinding (KeyboardShortcut[] chords, KeyboardShortcut[] accels, out KeyBinding binding, out bool isChord)
271
278
                {
272
279
                        if (chords != null) {
996
1003
                                cmd = GetActionCommand (commandId);
997
1004
                                if (cmd == null)
998
1005
                                        return false;
999
 
                                
 
1006
 
 
1007
                                CurrentCommand = cmd;
1000
1008
                                CommandTargetRoute targetRoute = new CommandTargetRoute (initialTarget);
1001
1009
                                object cmdTarget = GetFirstCommandTarget (targetRoute);
1002
1010
                                CommandInfo info = new CommandInfo (cmd);
1074
1082
                                name = name.Replace ("_","");
1075
1083
                                ReportError (commandId, "Error while executing command: " + name, ex);
1076
1084
                        }
 
1085
                        finally {
 
1086
                                CurrentCommand = null;
 
1087
                        }
1077
1088
                        return false;
1078
1089
                }
1079
1090
                
1160
1171
                        try {
1161
1172
                                bool multiCastEnabled = true;
1162
1173
                                bool multiCastVisible = false;
1163
 
                                
 
1174
 
 
1175
                                CurrentCommand = cmd;
 
1176
 
1164
1177
                                object cmdTarget = GetFirstCommandTarget (targetRoute);
1165
1178
                                
1166
1179
                                while (cmdTarget != null)
1231
1244
                                info.Visible = true;
1232
1245
                        } finally {
1233
1246
                                NotifyCommandTargetScanFinished ();
 
1247
                                CurrentCommand = null;
1234
1248
                        }
1235
1249
 
1236
1250
                        if (guiLock > 0)
1273
1287
                {
1274
1288
                        CommandTargetRoute targetRoute = new CommandTargetRoute (initialTarget);
1275
1289
                        object cmdTarget = GetFirstCommandTarget (targetRoute);
1276
 
                        
1277
 
                        while (cmdTarget != null)
1278
 
                        {
1279
 
                                if (visitor.Visit (cmdTarget))
1280
 
                                        return cmdTarget;
1281
 
 
1282
 
                                cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
 
1290
 
 
1291
                        visitor.Start ();
 
1292
 
 
1293
                        try {
 
1294
                                while (cmdTarget != null)
 
1295
                                {
 
1296
                                        if (visitor.Visit (cmdTarget))
 
1297
                                                return cmdTarget;
 
1298
 
 
1299
                                        cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
 
1300
                                }
 
1301
                        } catch (Exception ex) {
 
1302
                                LoggingService.LogError ("Error while visiting command targets", ex);
 
1303
                        } finally {
 
1304
                                visitor.End ();
1283
1305
                        }
1284
 
                        
1285
 
                        visitor.Visit (null);
1286
1306
                        return null;
1287
1307
                }
1288
1308
                
1509
1529
                        return cmdTarget;
1510
1530
                }
1511
1531
                
1512
 
                object GetNextCommandTarget (CommandTargetRoute targetRoute, object cmdTarget)
 
1532
                object GetNextCommandTarget (CommandTargetRoute targetRoute, object cmdTarget, bool ignoreDelegator = false)
1513
1533
                {
1514
1534
                        if (cmdTarget is IMultiCastCommandRouter) 
1515
1535
                                cmdTarget = new MultiCastDelegator (this, (IMultiCastCommandRouter)cmdTarget, targetRoute);
1516
 
                        
1517
 
                        if (cmdTarget is ICommandDelegatorRouter) {
 
1536
 
 
1537
                        if (!ignoreDelegator && cmdTarget is ICommandDelegator) {
 
1538
                                if (cmdTarget is ICommandDelegatorRouter)
 
1539
                                        throw new InvalidOperationException ("A type can't implement both ICommandDelegator and ICommandDelegatorRouter");
 
1540
                                object oldCmdTarget = cmdTarget;
 
1541
                                cmdTarget = ((ICommandDelegator)oldCmdTarget).GetDelegatedCommandTarget ();
 
1542
                                if (cmdTarget != null)
 
1543
                                        delegatorStack.Push (oldCmdTarget);
 
1544
                                else
 
1545
                                        cmdTarget = GetNextCommandTarget (targetRoute, oldCmdTarget, true);
 
1546
                        }
 
1547
                        else if (cmdTarget is ICommandDelegatorRouter) {
1518
1548
                                object oldCmdTarget = cmdTarget;
1519
1549
                                cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetDelegatedCommandTarget ();
1520
1550
                                if (cmdTarget != null)
1531
1561
                        
1532
1562
                        if (cmdTarget == null || !visitedTargets.Add (cmdTarget)) {
1533
1563
                                if (delegatorStack.Count > 0) {
1534
 
                                        ICommandDelegatorRouter del = (ICommandDelegatorRouter) delegatorStack.Pop ();
1535
 
                                        cmdTarget = del.GetNextCommandTarget ();
 
1564
                                        var del = delegatorStack.Pop ();
 
1565
                                        if (del is ICommandDelegatorRouter)
 
1566
                                                cmdTarget = ((ICommandDelegatorRouter)del).GetNextCommandTarget ();
 
1567
                                        else
 
1568
                                                cmdTarget = GetNextCommandTarget (targetRoute, del, true);
1536
1569
                                        if (cmdTarget == CommandManager.CommandRouteTerminator)
1537
1570
                                                return null;
1538
1571
                                        if (cmdTarget != null)
1569
1602
                                                newFocused = w;
1570
1603
                                        }
1571
1604
                                        if (w.IsActive && w.Type == Gtk.WindowType.Toplevel && !(w is Gtk.Dialog)) {
1572
 
                                                win = w;
1573
 
                                                break;
 
1605
                                                if (win == null)
 
1606
                                                        win = w;
1574
1607
                                        }
1575
1608
                                        if (lastFocused == w) {
1576
1609
                                                lastFocusedExists = true;
1592
1625
                Gtk.Widget GetActiveWidget (Gtk.Window win)
1593
1626
                {
1594
1627
                        win = GetActiveWindow (win);
 
1628
                        Gtk.Widget widget = win;
1595
1629
                        if (win != null) {
1596
 
                                Gtk.Widget widget = win;
1597
1630
                                while (widget is Gtk.Container) {
1598
1631
                                        Gtk.Widget child = ((Gtk.Container)widget).FocusChild;
1599
1632
                                        if (child != null)
1601
1634
                                        else
1602
1635
                                                break;
1603
1636
                                }
1604
 
                                return widget;
1605
 
                        }
1606
 
                        return win;
 
1637
                        }
 
1638
                        if (widget != lastActiveWidget) {
 
1639
                                if (ActiveWidgetChanged != null)
 
1640
                                        ActiveWidgetChanged (this, new ActiveWidgetEventArgs () { OldActiveWidget = lastActiveWidget, NewActiveWidget = widget });
 
1641
                                lastActiveWidget = widget;
 
1642
                        }
 
1643
                        return widget;
1607
1644
                }
1608
 
                
 
1645
 
1609
1646
                bool UpdateStatus ()
1610
1647
                {
1611
1648
                        if (!disposed && toolbarUpdaterRunning)
1731
1768
                        if (this.disposed)
1732
1769
                                return;
1733
1770
                        
1734
 
                        object activeWidget = GetActiveWidget (rootWidget);
 
1771
                        var activeWidget = GetActiveWidget (rootWidget);
1735
1772
                        foreach (ICommandBar toolbar in toolbars) {
1736
1773
                                toolbar.Update (activeWidget);
1737
1774
                        }
1738
1775
                        foreach (ICommandTargetVisitor v in visitors)
1739
1776
                                VisitCommandTargets (v, null);
1740
1777
                }
1741
 
                
 
1778
 
1742
1779
                void UpdateAppFocusStatus (bool hasFocus, bool lastFocusedExists)
1743
1780
                {
1744
1781
                        if (hasFocus != appHasFocus) {
1745
1782
                                // The last focused window has been destroyed. Wait a few ms since another app's window
1746
1783
                                // may gain focus again
 
1784
 
1747
1785
                                DateTime now = DateTime.Now;
 
1786
                                if (focusCheckDelayTimeout == DateTime.MinValue) {
 
1787
                                        focusCheckDelayTimeout = now.AddMilliseconds (100);
 
1788
                                        return;
 
1789
                                }
 
1790
 
1748
1791
                                if (now < focusCheckDelayTimeout)
1749
1792
                                        return;
1750
 
                                if (!hasFocus && !lastFocusedExists) {
1751
 
                                        focusCheckDelayTimeout = now.AddMilliseconds (100);
1752
 
                                        return;
1753
 
                                }
 
1793
 
1754
1794
                                focusCheckDelayTimeout = DateTime.MinValue;
1755
1795
                                
1756
1796
                                appHasFocus = hasFocus;
1761
1801
                                        if (ApplicationFocusOut != null)
1762
1802
                                                ApplicationFocusOut (this, EventArgs.Empty);
1763
1803
                                }
1764
 
                        }
 
1804
                        } else
 
1805
                                focusCheckDelayTimeout = DateTime.MinValue;
1765
1806
                }
1766
1807
                
1767
1808
                public void ReportError (object commandId, string message, Exception ex)
1794
1835
                        if (CommandTargetScanFinished != null)
1795
1836
                                CommandTargetScanFinished (this, EventArgs.Empty);
1796
1837
                }
 
1838
 
 
1839
                internal bool ApplicationHasFocus {
 
1840
                        get { return appHasFocus; }
 
1841
                }
1797
1842
                
1798
1843
                /// <summary>
1799
1844
                /// Raised when there is an exception while executing or updating the status of a command
1813
1858
                /// <summary>
1814
1859
                /// Fired when the application gets the focus
1815
1860
                /// </summary>
1816
 
                public event EventHandler ApplicationFocusIn;
 
1861
                internal event EventHandler ApplicationFocusIn;
1817
1862
                
1818
1863
                /// <summary>
1819
1864
                /// Fired when the application loses the focus
1820
1865
                /// </summary>
1821
 
                public event EventHandler ApplicationFocusOut;
 
1866
                internal event EventHandler ApplicationFocusOut;
1822
1867
                
1823
1868
                /// <summary>
1824
1869
                /// Fired when the command route scan starts
1834
1879
                /// Fired when a key is pressed
1835
1880
                /// </summary>
1836
1881
                public event EventHandler<KeyPressArgs> KeyPressed;
1837
 
        }
1838
 
        
 
1882
 
 
1883
                /// <summary>
 
1884
                /// Occurs when active widget (the current command target) changes
 
1885
                /// </summary>
 
1886
                public event EventHandler<ActiveWidgetEventArgs> ActiveWidgetChanged;
 
1887
        }
 
1888
 
 
1889
 
 
1890
        public class ActiveWidgetEventArgs: EventArgs
 
1891
        {
 
1892
                public Gtk.Widget OldActiveWidget { get; internal set; }
 
1893
                public Gtk.Widget NewActiveWidget { get; internal set; }
 
1894
        }
 
1895
 
1839
1896
        internal class HandlerTypeInfo
1840
1897
        {
1841
1898
                public CommandHandlerInfo[] CommandHandlers;