Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ContextMenuExtensionsGtk.cs « MonoDevelop.Components « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 035a36cb79a728ad9738e7fc4dc6ccd5b9271951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// ContextMenuExtensionsGtk.cs
//
// Author:
//       Greg Munn <greg.munn@xamarin.com>
//
// Copyright (c) 2015 Xamarin Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Xwt.GtkBackend;

namespace MonoDevelop.Components
{
	static class ContextMenuExtensionsGtk
	{
		public static void ShowContextMenu (Gtk.Widget parent, Gdk.EventButton evt, ContextMenu menu)
		{
			if (parent == null)
				throw new ArgumentNullException ("parent");
			if (menu == null)
				throw new ArgumentNullException ("menu");

			var gtkMenu = FromMenu (menu);
			gtkMenu.ShowAll ();
			ShowContextMenu (parent, evt, gtkMenu);
		}

		public static void ShowContextMenu (Gtk.Widget parent, Gdk.EventButton evt, Gtk.Menu menu)
		{
			if (parent == null)
				throw new ArgumentNullException ("parent");
			if (menu == null)
				throw new ArgumentNullException ("menu");

			GtkWorkarounds.ShowContextMenu (menu, parent, evt);
		}

		static Gtk.MenuItem CreateMenuItem (ContextMenuItem item)
		{
			if (!item.Visible)
				return null;
			
			if (item.IsSeparator) {
				return new Gtk.SeparatorMenuItem ();
			}

			Gtk.MenuItem menuItem;
			if (item is RadioButtonContextMenuItem) {
				var radioItem = (RadioButtonContextMenuItem)item;
				menuItem = new Gtk.CheckMenuItem (item.Label) { Active = radioItem.Checked, DrawAsRadio = true };
			} else if (item is CheckBoxContextMenuItem) {
				var checkItem = (CheckBoxContextMenuItem)item;
				menuItem = new Gtk.CheckMenuItem (item.Label) { Active = checkItem.Checked };
			} else {
				menuItem = new Gtk.ImageMenuItem (item.Label);
			} 

			if (item.SubMenu != null && item.SubMenu.Items.Count > 0) {
				menuItem.Submenu = FromMenu (item.SubMenu);
			}
			else {
				menuItem.Activated += (sender, e) => item.Click ();
			}

			menuItem.Sensitive = item.Sensitive;

			var label = (Gtk.Label) menuItem.Child;
			label.UseUnderline = item.UseMnemonic;
			if (item.UseMnemonic)
				label.TextWithMnemonic = item.Label;

			if (item.Image != null) {
				Gtk.ImageMenuItem imageItem = menuItem as Gtk.ImageMenuItem;
				if (imageItem != null) {
					var img = new ImageView (item.Image);
					img.ShowAll ();
					imageItem.Image = img;
					Xwt.GtkBackend.GtkWorkarounds.ForceImageOnMenuItem (imageItem);
				}
			}

			return menuItem;
		}

		static Gtk.Menu FromMenu (ContextMenu menu)
		{
			var result = new Gtk.Menu ();

			foreach (var menuItem in menu.Items) {
				var item = CreateMenuItem (menuItem);
				if (item != null)
					result.Append (item);
			}

			return result;
		}
	}
}