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

PropertyEditorCell.cs « MonoDevelop.Components.PropertyGrid « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c727deb5656a8164507328e27652210da10e0b54 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// PropertyEditorCell.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 System;
using System.ComponentModel;
using Gdk;
using Gtk;
using Mono.TextEditor;

namespace MonoDevelop.Components.PropertyGrid
{
	public class PropertyEditorCell
	{
		Pango.Layout layout;
		ITypeDescriptorContext context;
		Widget container;
		EditorManager editorManager;
		
		public object Instance {
			get { return context.Instance; }
		}
		
		public PropertyDescriptor Property {
			get { return context.PropertyDescriptor; }
		}

		protected ITypeDescriptorContext Context {
			get { return context; }
		}
		
		public Widget Container {
			get { return container; }
		}
		
		internal EditorManager EditorManager {
			get { return editorManager; }
		}
		
		internal void Initialize (Widget container, EditorManager editorManager, ITypeDescriptorContext context)
		{
			this.container = container;
			this.editorManager = editorManager;
			
			layout = new Pango.Layout (container.PangoContext);
			layout.Width = -1;
			
			Pango.FontDescription des = container.Style.FontDescription.Copy();
			layout.FontDescription = des;
			
			this.context = context;
			Initialize ();
		}

		public EditSession StartEditing (Rectangle cellArea, StateType state)
		{
			IPropertyEditor ed = CreateEditor (cellArea, state);
			if (ed == null)
				return null;
			return new EditSession (container, context, ed);
		}
		
		protected virtual string GetValueText ()
		{
			var val = Value;
			if (val != null)
				return Property.Converter.ConvertToString (context, val);
			return "";
		}
		
		protected virtual string GetValueMarkup ()
		{
			return null;
		}
		
		string GetNormalizedText (string s)
		{
			if (s == null)
				return "";

			int i = s.IndexOf ('\n');
			if (i == -1)
				return s;
			
			s = s.TrimStart ('\n', ' ', '\t');
			i = s.IndexOf ('\n');
			if (i != -1)
				return s.Substring (0, i) + "...";
			return s;
		}
		
		public object Value {
			get { return Instance != null ? Property.GetValue (Instance) : null; }
		}
		
		protected virtual void Initialize ()
		{
			string s = GetValueMarkup ();
			if (s != null)
				layout.SetMarkup (GetNormalizedText (s));
			else
				layout.SetText (GetNormalizedText (GetValueText ()));
		}
		
		public virtual void GetSize (int availableWidth, out int width, out int height)
		{
			layout.GetPixelSize (out width, out height);
		}

		public virtual void Render (Drawable window, Cairo.Context ctx, Rectangle bounds, StateType state)
		{
			int w, h;
			layout.GetPixelSize (out w, out h);
			int dy = (bounds.Height - h) / 2;

			ctx.Save ();
			ctx.SetSourceColor (container.Style.Text (state).ToCairoColor ());
			ctx.MoveTo (bounds.X, dy + bounds.Y);
			Pango.CairoHelper.ShowLayout (ctx, layout);
			ctx.Restore ();
		}
		
		protected virtual IPropertyEditor CreateEditor (Rectangle cellArea, StateType state)
		{
			if (DialogueEdit && (!Property.IsReadOnly || EditsReadOnlyObject)) {
				return new PropertyDialogueEditor (this, context);
			}
			else {
				Type editorType = editorManager.GetEditorType (context);
				if (editorType == null)
					return null;
				
				var editor = Activator.CreateInstance (editorType) as IPropertyEditor;
				if (editor == null)
					throw new Exception ("The property editor '" + editorType + "' must implement the interface IPropertyEditor");
				return editor;
			}
		}
		
		/// <summary>
		/// Whether the editor should show a button.
		/// </summary>
		public virtual bool DialogueEdit {
			get { return false; }
		}

		/// <summary>
		/// If the property is read-only, is is usually not edited. If the editor
		/// can edit sub-properties of a read-only complex object, this must return true.
		/// <remarks>The default value is false.</remarks>
		/// </summary>
		/// <returns>True if the editor can edit read-only properties</returns>
		public virtual bool EditsReadOnlyObject {
			get { return false; }
		}
		
		public virtual void LaunchDialogue ()
		{
			if (DialogueEdit)
				throw new NotImplementedException ();
		}
	}
	
	
	class DefaultPropertyEditor: Entry, IPropertyEditor
	{
		PropertyDescriptor property;
		
		public void Initialize (EditSession session)
		{
			property = session.Property;
		}
		
		public object Value {
			get { 
				return Convert.ChangeType (Text, property.PropertyType); 
			}
			set {
				if (value == null)
					Text = "";
				else
					Text = Convert.ToString (value); 
			}
		}
		
		protected override void OnChanged ()
		{
			base.OnChanged ();
			if (ValueChanged != null)
				ValueChanged (this, EventArgs.Empty);
		}

		public event EventHandler ValueChanged;
	}
	
	public class EditSession : ITypeDescriptorContext
	{
		Widget container;
		IPropertyEditor currentEditor;
		bool syncing;
		readonly ITypeDescriptorContext context;
		
		public event EventHandler Changed;
		
		internal EditSession (Widget container, ITypeDescriptorContext context, IPropertyEditor currentEditor)
		{
			this.context = context;
			this.container = container;
			this.currentEditor = currentEditor;
			
			currentEditor.Initialize (this);
			if (Instance != null)
				currentEditor.Value = context.PropertyDescriptor.GetValue (Instance);
			
			currentEditor.ValueChanged += OnValueChanged;
		}
		
		public void Dispose ()
		{
			currentEditor.Dispose ();
		}
		
		public object Instance {
			get { return context.Instance; }
		}
		
		public PropertyDescriptor Property {
			get { return context.PropertyDescriptor; }
		}
		
		PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
			get { return context.PropertyDescriptor; }
		}
		
		public Widget Container {
			get { return container; }
		}
		
		public IPropertyEditor Editor {
			get { return currentEditor; }
		}
		
		void OnValueChanged (object s, EventArgs a)
		{
			if (!syncing) {
				syncing = true;
				if (!context.PropertyDescriptor.IsReadOnly) {
					context.PropertyDescriptor.SetValue (context.Instance, currentEditor.Value);
					if (Changed != null)
						Changed (s, a);
				}
				syncing = false;
			}
		}
		
/*		public void AttachObject (object ob)
		{
			if (ob == null)
				throw new ArgumentNullException ("ob");
			
			syncing = true;
			this.obj = ob;
			currentEditor.AttachObject (obj);
			
			// It is the responsibility of the editor to convert value types
			object initial = property.GetValue (obj);
			currentEditor.Value = initial;
			
			syncing = false;
		}
*/		
		public void UpdateEditor ()
		{
			if (!syncing) {
				syncing = true;
				currentEditor.Value = context.PropertyDescriptor.GetValue (context.Instance);
				syncing = false;
			}
		}

		object IServiceProvider.GetService (Type serviceType)
		{
			return context.GetService (serviceType);
		}
		
		void ITypeDescriptorContext.OnComponentChanged ()
		{
			context.OnComponentChanged ();
		}
		
		bool ITypeDescriptorContext.OnComponentChanging ()
		{
			return context.OnComponentChanging ();
		}
		
		IContainer ITypeDescriptorContext.Container { get { return context.Container; } }
	}
	
	class CellRendererWidget: DrawingArea
	{
		readonly PropertyEditorCell cell;
		readonly ITypeDescriptorContext context;
		readonly EditorManager em;
		
		public CellRendererWidget (PropertyEditorCell cell, ITypeDescriptorContext context)
		{
			this.cell = cell;
			this.context = context;
			em = cell.EditorManager;
			this.ModifyBg (StateType.Normal, this.Style.White);
		}
		
		protected override bool OnExposeEvent (EventExpose evnt)
		{
			bool res = base.OnExposeEvent (evnt);
			cell.Initialize (this, em, context);
			
			Rectangle rect = Allocation;
			rect.Inflate (-3, 0);// Add some margin

			using (Cairo.Context ctx = CairoHelper.Create (GdkWindow)) {
				cell.Render (GdkWindow, ctx, rect, StateType.Normal);
			}
			return res;
		}
	}
	
	class PropertyDialogueEditor: HBox, IPropertyEditor
	{
		PropertyEditorCell cell;
		
		public PropertyDialogueEditor (PropertyEditorCell cell, ITypeDescriptorContext context)
		{
			this.cell = cell;
			Spacing = 3;
			PackStart (new CellRendererWidget (cell, context), true, true, 0);
			Label buttonLabel = new Label ();
			buttonLabel.UseMarkup = true;
			buttonLabel.Xpad = 0; buttonLabel.Ypad = 0;
			buttonLabel.Markup = "<span size=\"small\">...</span>";
			Button dialogueButton = new Button (buttonLabel);
			dialogueButton.Clicked += DialogueButtonClicked;
			PackStart (dialogueButton, false, false, 0);
			this.ModifyBg (StateType.Normal, this.Style.White);
			ShowAll ();
		}
		
		void DialogueButtonClicked (object s, EventArgs args)
		{
			cell.LaunchDialogue ();
			if (ValueChanged != null)
				ValueChanged (this, args);
		}
		
		public void Initialize (EditSession session)
		{
		}

		public object Value {
			get { return cell.Value; }
			set {  }
		}

		public event EventHandler ValueChanged;

	}
	
	public interface IPropertyEditor: IDisposable
	{
		// Called once to initialize the editor.
		void Initialize (EditSession session);
		
		// Gets/Sets the value of the editor. If the editor supports
		// several value types, it is the responsibility of the editor 
		// to return values with the expected type.
		object Value { get; set; }

		// To be fired when the edited value changes.
		event EventHandler ValueChanged;
	}
}