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

FlagsEditorCell.cs « MonoDevelop.Components.PropertyGrid.Editors « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 272cb84f5470285217e279a67b86ca20ce653741 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// FlagsEditorCell.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// 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 Gtk;
using System;
using System.Collections;
using System.ComponentModel;
using MonoDevelop.Ide.Fonts;

namespace MonoDevelop.Components.PropertyGrid.PropertyEditors {

	public class FlagsEditorCell: PropertyEditorCell
	{
		internal static int MaxCheckCount = 6;
		internal static int CheckSpacing = 3;
		static int indicatorSize;
		static int indicatorSpacing;
		static Gtk.Style style;

		static FlagsEditorCell ()
		{
			// reinit style
			MonoDevelop.Ide.Gui.Styles.Changed += (sender, e) => style = null;
		}

		// we can't override Initialize () or use the default constructor for this,
		// because a valid Gdk.Window is required for full Gtk.Style initialization
		static void InitializeStyle (Gtk.Widget container)
		{
			if (style == null && container.GdkWindow != null) {
				Gtk.CheckButton cb = new BooleanEditor (); // use the BooleanEditor style for the checks
				cb.GdkWindow = container.GdkWindow;
				cb.Parent = container;
				cb.Realize ();
				style = cb.Style;
				style.Attach (container.GdkWindow);
				indicatorSize = (int)cb.StyleGetProperty ("indicator-size");
				indicatorSpacing = (int)cb.StyleGetProperty ("indicator-spacing");
				style.Detach ();
				cb.Dispose ();
			}
		}

		protected override string GetValueText ()
		{
			if (Value == null)
				return "";

			ulong value = Convert.ToUInt64 (Value);
			Array values = System.Enum.GetValues (base.Property.PropertyType);
			string txt = "";
			foreach (object val in values) {
				ulong uintVal = Convert.ToUInt64 (val);
				if (uintVal == 0 && value == 0)
					return val.ToString (); // zero flag defined and no flags set
				if ((value & uintVal) != 0) {
					if (txt.Length > 0) txt += ", ";
					txt += val.ToString ();
				}
			}
			return txt;
		}

		public override void Render (Gdk.Drawable window, Cairo.Context ctx, Gdk.Rectangle bounds, Gtk.StateType state)
		{
			var values = Enum.GetValues (Property.PropertyType);
			if (values.Length < MaxCheckCount) {
				if (style == null)
					InitializeStyle (Container);

				var container = (Widget)Container;
				using (var layout = new Pango.Layout (container.PangoContext)) {
					layout.Width = -1;
					layout.FontDescription = FontService.SansFont.CopyModified (Ide.Gui.Styles.FontScale11);

					ulong value = Convert.ToUInt64 (Value);
					int dy = 2;
					foreach (var val in values) {
						ulong uintVal = Convert.ToUInt64 (val);
						Gtk.ShadowType sh = (value & uintVal) != 0 ? Gtk.ShadowType.In : Gtk.ShadowType.Out;
						if (value == 0 && uintVal == 0)
							sh = Gtk.ShadowType.In;
						int s = indicatorSize - 1;
						Gtk.Style.PaintCheck (style, window, state, sh, bounds, Container, "checkbutton", bounds.X + indicatorSpacing - 1, bounds.Y + dy, s, s);

						layout.SetText (val.ToString ());
						int tw, th;
						layout.GetPixelSize (out tw, out th);
						ctx.Save ();
						ctx.SetSourceColor (container.Style.Text (state).ToCairoColor ());
						ctx.MoveTo (bounds.X + indicatorSize + indicatorSpacing, dy + bounds.Y + ((indicatorSize - th) / 2));
						Pango.CairoHelper.ShowLayout (ctx, layout);
						ctx.Restore ();

						dy += indicatorSize + CheckSpacing;
					}
				}
			} else {
				base.Render (window, ctx, bounds, state);
				return;
			}
		}
		
		protected override IPropertyEditor CreateEditor (Gdk.Rectangle cell_area, Gtk.StateType state)
		{
			return new FlagsEditor ();
		}

		public override void GetSize (int availableWidth, out int width, out int height)
		{
			base.GetSize (availableWidth, out width, out height);

			var values = Enum.GetValues (Property.PropertyType);
			if (values.Length < MaxCheckCount) {
				if (style == null)
					InitializeStyle (Container);
				height = 4 + (indicatorSize * values.Length) + (CheckSpacing * (values.Length - 1));
			}
		}
	}
	
	public class FlagsEditor : Gtk.HBox, IPropertyEditor
	{
		Hashtable flags;
		Gtk.Entry flagsLabel;
		string property;
		Type propType;
		Array values;

		public FlagsEditor ()
		{
		}
		
		public void Initialize (EditSession session)
		{
			PropertyDescriptor prop = session.Property;
			
			if (!prop.PropertyType.IsEnum)
				throw new ApplicationException ("Flags editor does not support editing values of type " + prop.PropertyType);
			
			Spacing = FlagsEditorCell.CheckSpacing;
			propType = prop.PropertyType;
			
			property = prop.Description;
			if (property == null || property.Length == 0)
				property = prop.Name;

			// For small enums, the editor is a list of checkboxes inside a frame
			// For large enums (>5), use a selector dialog.

			values = System.Enum.GetValues (prop.PropertyType);
			
			if (values.Length < FlagsEditorCell.MaxCheckCount) 
			{
				Gtk.VBox vbox = new Gtk.VBox (true, FlagsEditorCell.CheckSpacing);

				flags = new Hashtable ();

				foreach (object value in values) {
					ulong uintVal = Convert.ToUInt64 (value);
					Gtk.CheckButton check = new BooleanEditor ();
					if (uintVal == 0)
						check.Active = true; // default for None is always enabled
					check.Label = value.ToString ();
					check.TooltipText = value.ToString ();
					flags[check] = uintVal;
					flags[uintVal] = check;
					
					check.Toggled += FlagToggled;
					vbox.PackStart (check, false, false, 3);
				}

				Gtk.Frame frame = new Gtk.Frame ();
				frame.Add (vbox);
				frame.ShowAll ();
				PackStart (frame, true, true, 0);
			} 
			else 
			{
				flagsLabel = new Gtk.Entry ();
				flagsLabel.IsEditable = false;
				flagsLabel.HasFrame = false;
				flagsLabel.ShowAll ();
				PackStart (flagsLabel, true, true, 0);
				
				Gtk.Button but = new Gtk.Button ("...");
				but.Clicked += OnSelectFlags;
				but.ShowAll ();
				PackStart (but, false, false, 0);
			}
		}
		
		protected override void OnDestroyed ()
		{
			base.OnDestroyed ();
			((IDisposable)this).Dispose ();
		}

		void IDisposable.Dispose ()
		{
		}

		public object Value {
			get {
				return Enum.ToObject (propType, UIntValue);
			}
			set {
				UIntValue = Convert.ToUInt64 (value);
			}
		}

		public event EventHandler ValueChanged;

		ulong uintValue;
		
		ulong UIntValue {
			get {
				return uintValue;
			}
			set {
				if (uintValue != value) {
					uintValue = value;
					UpdateFlags ();
					if (ValueChanged != null)
						ValueChanged (this, EventArgs.Empty);
				}
			}
		}

		void FlagToggled (object o, EventArgs args)
		{
			Gtk.CheckButton check = (Gtk.CheckButton)o;
			ulong val = (ulong)flags[o];

			if (check.Active) {
				if (val == 0)
					UIntValue = 0;
				else
					UIntValue |= val;
			} else
				UIntValue &= ~val;
		}

		void UpdateFlags ()
		{
			if (flagsLabel != null) {
				string txt = "";
				foreach (object val in values) {
					ulong uintVal = Convert.ToUInt64 (val);
					if (UIntValue == 0 && uintVal == 0) {
						txt = val.ToString (); // zero flag defined and no flags set
						break;
					}
					if ((UIntValue & uintVal) != 0) {
						if (txt.Length > 0) txt += ", ";
						txt += val.ToString ();
					}
				}
				flagsLabel.Text = txt;
			} else {
				foreach (object val in values) {
					ulong uintVal = Convert.ToUInt64 (val);
					CheckButton check = (CheckButton)flags [uintVal];
					if (check != null)
						check.Active = (UIntValue == 0 && uintVal == 0) || (UIntValue & uintVal) != 0;
				}
			}
		}
		
		void OnSelectFlags (object o, EventArgs args)
		{
			using (FlagsSelectorDialog dialog = new FlagsSelectorDialog (null, propType, UIntValue, property)) {
				if (dialog.Run () == (int) ResponseType.Ok) {
					Value = Enum.ToObject (propType, dialog.Value);
				}
			}
		}
	}
}