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

« back to all changes in this revision

Viewing changes to src/addins/MacPlatform/MacMenu/MDMenu.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
//
 
2
// MDMenu.cs
 
3
//
 
4
// Author:
 
5
//       Michael Hutchinson <m.j.hutchinson@gmail.com>
 
6
//
 
7
// Copyright (c) 2013 Xamarin Inc.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Linq;
 
29
using MonoMac.AppKit;
 
30
using MonoDevelop.Components.Commands;
 
31
using MonoMac.Foundation;
 
32
using System.Diagnostics;
 
33
using MonoDevelop.Core;
 
34
 
 
35
namespace MonoDevelop.MacIntegration.MacMenu
 
36
{
 
37
        class MDMenu : NSMenu
 
38
        {
 
39
                static readonly string servicesID = (string) CommandManager.ToCommandId (MacIntegrationCommands.Services);
 
40
 
 
41
                public MDMenu (CommandManager manager, CommandEntrySet ces)
 
42
                {
 
43
                        this.WeakDelegate = this;
 
44
 
 
45
                        AutoEnablesItems = false;
 
46
 
 
47
                        Title = (ces.Name ?? "").Replace ("_", "");
 
48
 
 
49
                        foreach (CommandEntry ce in ces) {
 
50
                                if (ce.CommandId == Command.Separator) {
 
51
                                        AddItem (NSMenuItem.SeparatorItem);
 
52
                                        continue;
 
53
                                }
 
54
 
 
55
                                if (string.Equals (ce.CommandId as string, servicesID, StringComparison.Ordinal)) {
 
56
                                        AddItem (new MDServicesMenuItem ());
 
57
                                        continue;
 
58
                                }
 
59
 
 
60
                                var subset = ce as CommandEntrySet;
 
61
                                if (subset != null) {
 
62
                                        AddItem (new MDSubMenuItem (manager, subset));
 
63
                                        continue;
 
64
                                }
 
65
 
 
66
                                var lce = ce as LinkCommandEntry;
 
67
                                if (lce != null) {
 
68
                                        AddItem (new MDLinkMenuItem (lce));
 
69
                                        continue;
 
70
                                }
 
71
 
 
72
                                Command cmd = manager.GetCommand (ce.CommandId);
 
73
                                if (cmd == null) {
 
74
                                        LoggingService.LogError ("MacMenu: '{0}' maps to null command", ce.CommandId);
 
75
                                        continue;
 
76
                                }
 
77
 
 
78
                                if (cmd is CustomCommand) {
 
79
                                        LoggingService.LogWarning ("MacMenu: '{0}' is unsupported custom-rendered command' '", ce.CommandId);
 
80
                                        continue;
 
81
                                }
 
82
 
 
83
                                var acmd = cmd as ActionCommand;
 
84
                                if (acmd == null) {
 
85
                                        LoggingService.LogWarning ("MacMenu: '{0}' has unknown command type '{1}'", cmd.GetType (), ce.CommandId);
 
86
                                        continue;
 
87
                                }
 
88
 
 
89
                                AddItem (new MDMenuItem (manager, ce, acmd));
 
90
                        }
 
91
                }
 
92
 
 
93
                // http://lists.apple.com/archives/cocoa-dev/2008/Apr/msg01696.html
 
94
                void FlashMenu ()
 
95
                {
 
96
                        var f35 = ((char)0xF726).ToString ();
 
97
                        var blink = new NSMenuItem ("* blink *") {
 
98
                                KeyEquivalent = f35,
 
99
                        };
 
100
                        var f35Event = NSEvent.KeyEvent (
 
101
                                NSEventType.KeyDown, System.Drawing.PointF.Empty, NSEventModifierMask.CommandKeyMask, 0, 0,
 
102
                                NSGraphicsContext.CurrentContext, f35, f35, false, 0);
 
103
                        AddItem (blink);
 
104
                        PerformKeyEquivalent (f35Event);
 
105
                        RemoveItem (blink);
 
106
                }
 
107
 
 
108
                public bool FlashIfContainsCommand (object command)
 
109
                {
 
110
                        foreach (var item in ItemArray ().OfType<MDMenuItem> ()) {
 
111
                                if (item.CommandEntry.CommandId == command) {
 
112
                                        FlashMenu ();
 
113
                                        return true;
 
114
                                }
 
115
                                var submenu = item.Submenu as MDMenu;
 
116
                                if (submenu != null && submenu.FlashIfContainsCommand (command))
 
117
                                        return true;
 
118
                        }
 
119
                        return false;
 
120
                }
 
121
 
 
122
                public void UpdateCommands ()
 
123
                {
 
124
                        NSMenuItem lastSeparator = NSMenuItem.SeparatorItem;
 
125
 
 
126
                        for (int i = 0; i < Count; i++) {
 
127
                                var item = this.ItemAt (i);
 
128
 
 
129
                                if (item.IsSeparatorItem) {
 
130
                                        if (lastSeparator == null) {
 
131
                                                lastSeparator = item;
 
132
                                        }
 
133
                                        item.Hidden = true;
 
134
                                        continue;
 
135
                                }
 
136
 
 
137
                                var mdItem = item as IUpdatableMenuItem;
 
138
                                if (mdItem != null) {
 
139
                                        mdItem.Update (this, ref lastSeparator, ref i);
 
140
                                        continue;
 
141
                                }
 
142
 
 
143
                                //hide unknown builtins
 
144
                                item.Hidden = true;
 
145
                        }
 
146
                }
 
147
 
 
148
                [ExportAttribute ("menuNeedsUpdate:")]
 
149
                void MenuNeedsUpdate (NSMenu menu)
 
150
                {
 
151
                        Debug.Assert (menu == this);
 
152
                        UpdateCommands ();
 
153
                }
 
154
 
 
155
                public static void ShowLastSeparator (ref NSMenuItem lastSeparator)
 
156
                {
 
157
                        if (lastSeparator != null) {
 
158
                                lastSeparator.Hidden = false;
 
159
                                lastSeparator = null;
 
160
                        }
 
161
                }
 
162
        }
 
163
 
 
164
        interface IUpdatableMenuItem
 
165
        {
 
166
                void Update (MDMenu parent, ref NSMenuItem lastSeparator, ref int index);
 
167
        }
 
168
}