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

ActionGroupEditor.cs « editor « libstetic « MonoDevelop.GtkCore « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc94b834c6d7987787f80a9d8a26e3c90e0b7bc3 (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575

using System;
using System.Collections;
using Stetic.Wrapper;
using Mono.Unix;

namespace Stetic.Editor
{
	public class ActionGroupEditor: Gtk.EventBox, IMenuItemContainer
	{
		ActionGroup actionGroup;
		Gtk.Table table;
		IProject project;
		ArrayList items = new ArrayList ();
		Gtk.EventBox emptyLabel;
		EditableLabel headerLabel;
		uint columns = 2;
		bool modified;
		bool disposed;
		ObjectWrapperEventHandler changedEvent;
		IDesignArea darea;
		
		public EventHandler GroupModified;
		public EventHandler SelectionChanged;
		
		public ActionGroupEditor ()
		{
			changedEvent = new ObjectWrapperEventHandler (OnActionChanged);
			
			Gtk.Fixed fx = new Gtk.Fixed ();
			table = new Gtk.Table (0, 0, false);
			table.RowSpacing = 8;
			table.ColumnSpacing = 8;
			table.BorderWidth = 12;
			
			Gtk.EventBox ebox = new Gtk.EventBox ();
			ebox.ModifyBg (Gtk.StateType.Normal, this.Style.Backgrounds [0]);
			headerLabel = new EditableLabel ();
			headerLabel.MarkupTemplate = "<b>$TEXT</b>";
			headerLabel.Changed += OnGroupNameChanged;
			Gtk.VBox vbox = new Gtk.VBox ();
			Gtk.Label grpLabel = new Gtk.Label ();
			grpLabel.Xalign = 0;
			grpLabel.Markup = "<small><i>Action Group</i></small>";
//			vbox.PackStart (grpLabel, false, false, 0);
			vbox.PackStart (headerLabel, false, false, 3);
			vbox.BorderWidth = 12;
			ebox.Add (vbox);
			
			Gtk.VBox box = new Gtk.VBox ();
			box.Spacing = 6;
			box.PackStart (ebox, false, false, 0);
			box.PackStart (table, false, false, 0);
			
			fx.Put (box, 0, 0);
			Add (fx);
			ShowAll ();
		}
		
		public override void Dispose ()
		{
			if (disposed)
				return;
			disposed = true;
			headerLabel.Changed -= OnGroupNameChanged;
			if (emptyLabel != null)
				emptyLabel.ButtonPressEvent -= OnAddClicked;
			
			foreach (ActionMenuItem aitem in items) {
				aitem.KeyPressEvent -= OnItemKeyPress;
				aitem.Node.Dispose ();
				aitem.Detach ();
				aitem.Destroy ();
			}
			items.Clear ();
			ActionGroup = null;
			project = null;
			headerLabel = null;
			
			if (darea != null) {
				darea.SelectionChanged -= OnSelectionChanged;
				darea = null;
			}

			base.Dispose ();
		}
		
		public ActionGroup ActionGroup {
			get { return actionGroup; }
			set {
				if (actionGroup != null) {
					actionGroup.ObjectChanged -= OnGroupChanged;
					actionGroup.ActionAdded -= OnActionAdded;
					actionGroup.ActionRemoved -= OnActionRemoved;
					foreach (Wrapper.Action a in actionGroup.Actions)
						a.ObjectChanged -= changedEvent;
				}
				actionGroup = value;
				if (actionGroup != null) {
					headerLabel.Text = actionGroup.Name;
					actionGroup.ObjectChanged += OnGroupChanged;
					actionGroup.ActionAdded += OnActionAdded;
					actionGroup.ActionRemoved += OnActionRemoved;
					foreach (Wrapper.Action a in actionGroup.Actions)
						a.ObjectChanged += changedEvent;
				}
				if (!disposed)
					Fill ();
			}
		}
		
		public IProject Project {
			get { return project; }
			set { project = value; }
		}
		
		public bool Modified {
			get { return modified; }
			set { modified = value; }
		}
		
		public Wrapper.Action SelectedAction {
			get {
				IDesignArea designArea = GetDesignArea ();
				IObjectSelection sel = designArea.GetSelection ();
				if (sel != null)
					return ObjectWrapper.Lookup (sel.DataObject) as Wrapper.Action;
				else
					return null;
			}
			set {
				foreach (ActionMenuItem item in items) {
					if (item.Node.Action == value)
						item.Select ();
				}
			}
		}
		
		ActionMenuItem SelectedActionMenuItem {
			get {
				IDesignArea designArea = GetDesignArea ();
				IObjectSelection sel = designArea.GetSelection ();
				if (sel != null)
					return sel.Widget as ActionMenuItem;
				else
					return null;
			}
		}
		
		public void StartEditing ()
		{
			IDesignArea designArea = GetDesignArea ();
			designArea.SetSelection (headerLabel, null);
			headerLabel.StartEditing ();
		}
		
		void Fill ()
		{
			IDesignArea designArea = GetDesignArea ();
			if (designArea == null)
				return;

			Wrapper.Action selAction = null;
			
			foreach (ActionMenuItem item in items) {
				if (designArea.IsSelected (item))
					selAction = item.Node.Action;
				item.Node.Dispose ();
				item.Detach ();
				item.Destroy ();
			}
			items.Clear ();
			
			if (actionGroup != null) {
				Wrapper.Action[] sortedActions = new Wrapper.Action [actionGroup.Actions.Count];
				actionGroup.Actions.CopyTo (sortedActions, 0);
				Array.Sort (sortedActions, new ActionComparer ());
				for (int n = 0; n < sortedActions.Length; n++) {
					Wrapper.Action action = (Wrapper.Action) sortedActions [n];
					ActionMenuItem item = InsertAction (action, n);
					if (selAction == action)
						item.Select ();
				}
				
				if (selAction == null)
					designArea.SetSelection (null, null);
				
				headerLabel.Sensitive = true;
				PlaceAddLabel (actionGroup.Actions.Count);
			} else {
				HideAddLabel ();
				headerLabel.Text = Catalog.GetString ("No selection");
				headerLabel.Sensitive = false;
			}
			ShowAll ();
		}
		
		ActionMenuItem InsertAction (Wrapper.Action action, int n)
		{
			uint row = (uint) n / columns;
			uint col = (uint) (n % columns) * 3;
			
			IDesignArea designArea = GetDesignArea ();
			ActionTreeNode node = new ActionTreeNode (Gtk.UIManagerItemType.Menuitem, "", action);
			ActionMenuItem aitem = new ActionMenuItem (designArea, project, this, node);
			aitem.KeyPressEvent += OnItemKeyPress;
			aitem.MinWidth = 150;
			aitem.Attach (table, row, col);
			
			Gtk.Frame fr = new Gtk.Frame ();
			fr.Shadow = Gtk.ShadowType.Out;
			aitem.Add (fr);
			
			items.Add (aitem);
			return aitem;
		}
		
		void PlaceAddLabel (int n)
		{
			HideAddLabel ();

			uint r = (uint) n / columns;
			uint c = (uint) (n % columns) * 3;
			
			emptyLabel = new Gtk.EventBox ();
			emptyLabel.VisibleWindow = false;
			Gtk.Label label = new Gtk.Label ();
			label.Xalign = 0;
			label.Markup = "<i><span foreground='darkgrey'>" + Catalog.GetString ("Click to create action") + "</span></i>";
			emptyLabel.Add (label);
			emptyLabel.ButtonPressEvent += OnAddClicked;
			table.Attach (emptyLabel, c, c+3, r, r+1);
		}
		
		void HideAddLabel ()
		{
			if (emptyLabel != null) {
				table.Remove (emptyLabel);
				emptyLabel.ButtonPressEvent -= OnAddClicked;
			}
			emptyLabel = null;
		}
		
		void OnGroupChanged (object s, ObjectWrapperEventArgs args)
		{
			headerLabel.Text = actionGroup.Name;
			NotifyModified ();
		}
		
		void OnActionAdded (object s, ActionEventArgs args)
		{
			args.Action.ObjectChanged += changedEvent;
			Fill ();
			NotifyModified ();
		}
		
		void OnActionRemoved (object s, ActionEventArgs args)
		{
			args.Action.ObjectChanged -= changedEvent;
			Fill ();
			NotifyModified ();
		}
		
		void OnActionChanged (object s, ObjectWrapperEventArgs args)
		{
			NotifyModified ();
		}
		
		void NotifyModified ()
		{
			modified = true;
			if (GroupModified != null)
				GroupModified (this, EventArgs.Empty);
		}
		
		void OnAddClicked (object s, Gtk.ButtonPressEventArgs args)
		{
			Wrapper.Action ac = (Wrapper.Action) ObjectWrapper.Create (project, new Gtk.Action ("", "", null, null));
			ActionMenuItem item = InsertAction (ac, actionGroup.Actions.Count);
			item.EditingDone += OnEditDone;
			item.Select ();
			item.StartEditing ();
			HideAddLabel ();
			ShowAll ();
		}
		
		void OnEditDone (object sender, EventArgs args)
		{
			ActionMenuItem item = (ActionMenuItem) sender;
			item.EditingDone -= OnEditDone;
			if (item.Node.Action.GtkAction.Label.Length > 0 || item.Node.Action.GtkAction.StockId != null) {
				actionGroup.Actions.Add (item.Node.Action);
			} else {
				IDesignArea designArea = GetDesignArea ();
				designArea.ResetSelection (item);
				item.Detach ();
				item.Node.Dispose ();
				items.Remove (item);
				item.Destroy ();
				PlaceAddLabel (actionGroup.Actions.Count);
				ShowAll ();
			}
		}
		
		protected override bool OnButtonPressEvent (Gdk.EventButton ev)
		{
			IDesignArea designArea = GetDesignArea ();
			designArea.SetSelection (null, null);
			return true;
		}
		
		void OnItemKeyPress (object s, Gtk.KeyPressEventArgs args)
		{
			int pos = items.IndexOf (s);
			
			switch (args.Event.Key) {
				case Gdk.Key.Up:
					pos -= (int) columns;
					break;
				case Gdk.Key.Down:
					pos += (int) columns;
					break;
				case Gdk.Key.Right:
					pos ++;
					break;
				case Gdk.Key.Left:
					pos --;
					break;
			}
			if (pos >= 0 && pos < items.Count) {
				((ActionMenuItem)items[pos]).Select ();
				args.RetVal = true;
			}
			else if (pos == items.Count) {
				OnAddClicked (null, null);
				args.RetVal = true;
			}
		}
		
		void OnGroupNameChanged (object s, EventArgs args)
		{
			if (actionGroup != null)
				actionGroup.Name = headerLabel.Text;
		}
		
		void OnSelectionChanged (object s, EventArgs args)
		{
			if (SelectionChanged != null)
				SelectionChanged (this, args);
		}
		
		public void Cut ()
		{
			ActionMenuItem menuItem = SelectedActionMenuItem;
			if (menuItem != null)
				Cut (SelectedActionMenuItem);
		}
		
		public void Copy ()
		{
			ActionMenuItem menuItem = SelectedActionMenuItem;
			if (menuItem != null)
				Copy (SelectedActionMenuItem);
		}
		
		public void Paste ()
		{
		}
		
		public void Delete ()
		{
			ActionMenuItem menuItem = SelectedActionMenuItem;
			if (menuItem != null)
				Delete (SelectedActionMenuItem);
		}
		
		void Cut (ActionMenuItem menuItem)
		{
		}
		
		void Copy (ActionMenuItem menuItem)
		{
		}
		
		void Paste (ActionMenuItem menuItem)
		{
		}
		
		void Delete (ActionMenuItem menuItem)
		{
			string msg = string.Format (Catalog.GetString ("Are you sure you want to delete the action '{0}'? It will be removed from all menus and toolbars."), menuItem.Node.Action.Name);
			Gtk.MessageDialog md = new Gtk.MessageDialog (null, Gtk.DialogFlags.Modal, Gtk.MessageType.Question, Gtk.ButtonsType.YesNo, msg);
			md.TransientFor = this.Toplevel as Gtk.Window;
			if (md.Run () == (int) Gtk.ResponseType.Yes) {
				menuItem.Node.Action.Delete ();
				darea.SetSelection (null, null);
			}
			md.Destroy ();
		}
		
		void IMenuItemContainer.ShowContextMenu (ActionItem aitem)
		{
			ActionMenuItem menuItem = aitem as ActionMenuItem;
			
			Gtk.Menu m = new Gtk.Menu ();
			Gtk.MenuItem item = new Gtk.ImageMenuItem (Gtk.Stock.Cut, null);
			m.Add (item);
			item.Activated += delegate (object s, EventArgs a) {
				Cut (menuItem);
			};
			item.Visible = false;	// No copy & paste for now
			item = new Gtk.ImageMenuItem (Gtk.Stock.Copy, null);
			m.Add (item);
			item.Activated += delegate (object s, EventArgs a) {
				Copy (menuItem);
			};
			item.Visible = false;	// No copy & paste for now
			item = new Gtk.ImageMenuItem (Gtk.Stock.Paste, null);
			m.Add (item);
			item.Activated += delegate (object s, EventArgs a) {
				Paste (menuItem);
			};
			item.Visible = false;	// No copy & paste for now
			item = new Gtk.ImageMenuItem (Gtk.Stock.Delete, null);
			m.Add (item);
			item.Activated += delegate (object s, EventArgs a) {
				Delete (menuItem);
			};
			m.ShowAll ();
			m.Popup ();
		}		
		
		IDesignArea GetDesignArea ()
		{
			if (darea != null)
				return darea;
			
			darea = WidgetUtils.GetDesignArea (this);
			darea.SelectionChanged += OnSelectionChanged;
			return darea;
		}
		
		ActionMenu IMenuItemContainer.OpenSubmenu { 
			get { return null; } 
			set { }
		}
		
		bool IMenuItemContainer.IsTopMenu {
			get { return false; }
		}
		
		Gtk.Widget IMenuItemContainer.Widget {
			get { return this; }
		}
		
		class ActionComparer: IComparer
		{
			public int Compare (object x, object y)
			{
				return string.Compare (((Wrapper.Action)x).GtkAction.Label, ((Wrapper.Action)y).GtkAction.Label);
			}
		}
	}
	
	public class EditableLabel: Gtk.EventBox
	{
		string text;
		string markup;
		
		public EditableLabel (): this ("")
		{
		}
		
		public EditableLabel (string txt)
		{
			VisibleWindow = false;
			text = txt;
			Add (CreateLabel ());
		}
		
		public string Text {
			get { return text; }
			set {
				text = value;
				if (Child is Gtk.Entry)
					((Gtk.Entry)Child).Text = text;
				else
					((Gtk.Label)Child).Markup = Markup;
			}
		}
		
		public string MarkupTemplate {
			get { return markup; }
			set {
				markup = value;
				if (Child is Gtk.Label)
					((Gtk.Label)Child).Markup = Markup;
			}
		}
		
		public string Markup {
			get { return markup != null ? markup.Replace ("$TEXT",text) : text; }
		}
		
		protected override bool OnButtonPressEvent (Gdk.EventButton ev)
		{
			IDesignArea d = WidgetUtils.GetDesignArea (this);
			if (d.IsSelected (this)) {
				if (Child is Gtk.Label) {
					StartEditing ();
					return true;
				}
			} else {
				d.SetSelection (this, null);
				return true;
			}
			return false;
		}
		
		void SelectionDisposed (object s, EventArgs args)
		{
			EndEditing ();
		}
		
		public void StartEditing ()
		{
			if (Child is Gtk.Label) {
				IDesignArea d = WidgetUtils.GetDesignArea (this);
				IObjectSelection sel = d.GetSelection (this);
				if (sel == null)
					sel = d.SetSelection (this, null);
				
				sel.Disposed += SelectionDisposed;
					
				Remove (Child);
				Add (CreateEntry ());
				ShowAll ();
				Child.GrabFocus ();
			}
		}
		
		public void EndEditing ()
		{
			if (Child is Gtk.Entry) {
				Remove (Child);
				Add (CreateLabel ());
				ShowAll ();
			}
		}
		
		Gtk.Label CreateLabel ()
		{
			Gtk.Label label = new Gtk.Label ();
			label.Markup = Markup;
			label.Xalign = 0;
			return label;
		}
		
		Gtk.Entry CreateEntry ()
		{
			Gtk.Entry e = new Gtk.Entry (text);
			e.Changed += delegate (object s, EventArgs a) {
				text = e.Text;
				if (Changed != null)
					Changed (this, a);
			};
			e.Activated += delegate (object s, EventArgs a) {
				EndEditing ();
			};
			return e;
		}
		
		public event EventHandler Changed;
	}
}