This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Project.cs
812 lines (691 loc) · 19.6 KB
/
Project.cs
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace Stetic
{
public class Project: MarshalByRefObject
{
Application app;
ProjectBackend backend;
string fileName;
IResourceProvider resourceProvider;
Component selection;
string tmpProjectFile;
bool reloadRequested;
List<WidgetInfo> widgets = new List<WidgetInfo> ();
List<ActionGroupInfo> groups = new List<ActionGroupInfo> ();
bool modified;
ImportFileDelegate importFileDelegate;
public event WidgetInfoEventHandler WidgetAdded;
public event WidgetInfoEventHandler WidgetRemoved;
public event ComponentNameEventHandler ComponentNameChanged;
public event EventHandler ComponentTypesChanged;
public event EventHandler ActionGroupsChanged;
public event EventHandler ModifiedChanged;
public event EventHandler Changed;
// Internal events
internal event BackendChangingHandler BackendChanging;
internal event BackendChangedHandler BackendChanged;
internal event ComponentSignalEventHandler SignalAdded;
internal event ComponentSignalEventHandler SignalRemoved;
internal event ComponentSignalEventHandler SignalChanged;
public event EventHandler ProjectReloading;
public event EventHandler ProjectReloaded;
internal Project (Application app): this (app, null)
{
}
internal Project (Application app, ProjectBackend backend)
{
this.app = app;
if (backend != null) {
this.backend = backend;
backend.SetFrontend (this);
}
if (app is IsolatedApplication) {
IsolatedApplication iapp = app as IsolatedApplication;
iapp.BackendChanging += OnBackendChanging;
iapp.BackendChanged += OnBackendChanged;
}
}
internal ProjectBackend ProjectBackend {
get {
if (backend == null) {
backend = app.Backend.CreateProject ();
backend.SetFrontend (this);
if (resourceProvider != null)
backend.ResourceProvider = resourceProvider;
if (fileName != null)
backend.Load (fileName);
}
return backend;
}
}
internal bool IsBackendLoaded {
get { return backend != null; }
}
internal Application App {
get { return app; }
}
internal event EventHandler Disposed;
public void Dispose ()
{
if (app is IsolatedApplication) {
IsolatedApplication iapp = app as IsolatedApplication;
iapp.BackendChanging -= OnBackendChanging;
iapp.BackendChanged -= OnBackendChanged;
}
if (tmpProjectFile != null && File.Exists (tmpProjectFile)) {
File.Delete (tmpProjectFile);
tmpProjectFile = null;
}
if (backend != null)
backend.Dispose ();
if (Disposed != null)
Disposed (this, EventArgs.Empty);
System.Runtime.Remoting.RemotingServices.Disconnect (this);
}
public override object InitializeLifetimeService ()
{
// Will be disconnected when calling Dispose
return null;
}
public string FileName {
get { return fileName; }
}
public IResourceProvider ResourceProvider {
get { return resourceProvider; }
set {
resourceProvider = value;
if (backend != null)
backend.ResourceProvider = value;
}
}
public Stetic.ProjectIconFactory IconFactory {
get { return ProjectBackend.IconFactory; }
set { backend.IconFactory = value; }
}
public ImportFileDelegate ImportFileCallback {
get { return importFileDelegate; }
set { importFileDelegate = value; }
}
public bool CanGenerateCode {
get { return ProjectBackend.CanGenerateCode; }
}
public Component Selection {
get { return selection; }
}
public string ImagesRootPath {
get { return ProjectBackend.ImagesRootPath; }
set { ProjectBackend.ImagesRootPath = value; }
}
public string TargetGtkVersion {
get { return ProjectBackend.TargetGtkVersion; }
set { ProjectBackend.TargetGtkVersion = value; }
}
public void Close ()
{
if (backend != null)
backend.Close ();
}
public void Load (string fileName)
{
this.fileName = fileName;
if (backend != null)
backend.Load (fileName);
using (StreamReader sr = new StreamReader (fileName)) {
XmlTextReader reader = new XmlTextReader (sr);
reader.MoveToContent ();
if (reader.IsEmptyElement)
return;
reader.ReadStartElement ("stetic-interface");
if (reader.IsEmptyElement)
return;
while (reader.NodeType != XmlNodeType.EndElement) {
if (reader.NodeType == XmlNodeType.Element) {
if (reader.LocalName == "widget")
ReadWidget (reader);
else if (reader.LocalName == "action-group")
ReadActionGroup (reader);
else
reader.Skip ();
}
else {
reader.Skip ();
}
reader.MoveToContent ();
}
}
}
void ReadWidget (XmlTextReader reader)
{
WidgetInfo w = new WidgetInfo (this, reader.GetAttribute ("id"), reader.GetAttribute ("class"));
widgets.Add (w);
if (reader.IsEmptyElement) {
reader.Skip ();
return;
}
reader.ReadStartElement ();
reader.MoveToContent ();
while (reader.NodeType != XmlNodeType.EndElement) {
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "action-group") {
w.AddGroup (reader.GetAttribute ("name"));
}
reader.Skip ();
reader.MoveToContent ();
}
reader.ReadEndElement ();
}
void ReadActionGroup (XmlTextReader reader)
{
groups.Add (new ActionGroupInfo (this, reader.GetAttribute ("name")));
reader.Skip ();
}
public void Save (string fileName)
{
this.fileName = fileName;
if (backend != null)
backend.Save (fileName);
}
public void ImportGlade (string fileName)
{
ProjectBackend.ImportGlade (fileName);
}
public void ExportGlade (string fileName)
{
ProjectBackend.ExportGlade (fileName);
}
public bool Modified {
get {
if (backend != null)
return backend.Modified;
else
return modified;
}
set {
if (backend != null)
backend.Modified = value;
else
modified = true;
}
}
public IEnumerable<WidgetInfo> Widgets {
get { return widgets; }
}
public IEnumerable<ActionGroupInfo> ActionGroups {
get { return groups; }
}
public WidgetInfo GetWidget (string name)
{
foreach (WidgetInfo w in widgets)
if (w.Name == name)
return w;
return null;
}
public ActionGroupInfo GetActionGroup (string name)
{
foreach (ActionGroupInfo w in groups)
if (w.Name == name)
return w;
return null;
}
public WidgetDesigner CreateWidgetDesigner (WidgetInfo widgetInfo, bool autoCommitChanges)
{
return new WidgetDesigner (this, widgetInfo.Name, autoCommitChanges);
}
public ActionGroupDesigner CreateActionGroupDesigner (ActionGroupInfo actionGroup, bool autoCommitChanges)
{
return new ActionGroupDesigner (this, null, actionGroup.Name, null, autoCommitChanges);
}
public WidgetInfo AddNewComponent (ComponentType type, string name)
{
object ob = ProjectBackend.AddNewWidget (type.Name, name);
WidgetComponent wc = (WidgetComponent) App.GetComponent (ob, null, null);
WidgetInfo wi = GetWidget (wc.Name);
if (wi == null) {
wi = new WidgetInfo (this, wc);
widgets.Add (wi);
}
return wi;
}
public WidgetInfo AddNewComponent (XmlElement template)
{
object ob = ProjectBackend.AddNewWidgetFromTemplate (template.OuterXml);
WidgetComponent wc = (WidgetComponent) App.GetComponent (ob, null, null);
WidgetInfo wi = GetWidget (wc.Name);
if (wi == null) {
wi = new WidgetInfo (this, wc);
widgets.Add (wi);
}
return wi;
}
public ComponentType[] GetComponentTypes ()
{
ArrayList types = new ArrayList ();
ArrayList typeNames = ProjectBackend.GetComponentTypes ();
for (int n=0; n<typeNames.Count; n++)
types.Add (app.GetComponentType ((string) typeNames [n]));
// Global action groups
foreach (ActionGroupComponent grp in GetActionGroups ()) {
foreach (ActionComponent ac in grp.GetActions ())
types.Add (new ComponentType (app, ac));
}
return (ComponentType[]) types.ToArray (typeof(ComponentType));
}
// Returns a list of all support widget types (including base classes)
public string[] GetWidgetTypes ()
{
return ProjectBackend.GetWidgetTypes ();
}
public void RemoveComponent (WidgetInfo component)
{
ProjectBackend.RemoveWidget (component.Name);
}
public WidgetComponent GetComponent (string name)
{
object ob = ProjectBackend.GetTopLevelWrapper (name, false);
if (ob != null)
return (WidgetComponent) App.GetComponent (ob, name, null);
else
return null;
}
public ActionGroupComponent AddNewActionGroup (string id)
{
object ob = ProjectBackend.AddNewActionGroup (id);
ActionGroupComponent ac = (ActionGroupComponent) App.GetComponent (ob, id, null);
// Don't wait for the group added event to come to update the groups list since
// it may be too late.
ActionGroupInfo gi = GetActionGroup (ac.Name);
if (gi == null) {
gi = new ActionGroupInfo (this, ac.Name);
groups.Add (gi);
}
return ac;
}
public ActionGroupComponent AddNewActionGroup (XmlElement template)
{
object ob = ProjectBackend.AddNewActionGroupFromTemplate (template.OuterXml);
ActionGroupComponent ac = (ActionGroupComponent) App.GetComponent (ob, null, null);
// Don't wait for the group added event to come to update the groups list since
// it may be too late.
ActionGroupInfo gi = GetActionGroup (ac.Name);
if (gi == null) {
gi = new ActionGroupInfo (this, ac.Name);
groups.Add (gi);
}
return ac;
}
public void RemoveActionGroup (ActionGroupInfo group)
{
ActionGroupComponent ac = (ActionGroupComponent) group.Component;
ProjectBackend.RemoveActionGroup ((Stetic.Wrapper.ActionGroup) ac.Backend);
}
internal ActionGroupComponent[] GetActionGroups ()
{
Wrapper.ActionGroup[] acs = ProjectBackend.GetActionGroups ();
ArrayList comps = new ArrayList (acs.Length);
for (int n=0; n<acs.Length; n++) {
ActionGroupComponent ag = (ActionGroupComponent) App.GetComponent (acs[n], null, null);
if (ag != null)
comps.Add (ag);
}
return (ActionGroupComponent[]) comps.ToArray (typeof(ActionGroupComponent));
}
public void AddWidgetLibrary (string assemblyPath)
{
AddWidgetLibrary (assemblyPath, false);
}
public void AddWidgetLibrary (string assemblyPath, bool isInternal)
{
reloadRequested = false;
ProjectBackend.AddWidgetLibrary (assemblyPath);
app.UpdateWidgetLibraries (false, false);
if (!reloadRequested)
ProjectBackend.Reload ();
}
public void RemoveWidgetLibrary (string assemblyPath)
{
reloadRequested = false;
ProjectBackend.RemoveWidgetLibrary (assemblyPath);
app.UpdateWidgetLibraries (false, false);
if (!reloadRequested)
ProjectBackend.Reload ();
}
public string[] WidgetLibraries {
get {
return (string[]) ProjectBackend.WidgetLibraries.ToArray (typeof(string));
}
}
public void SetWidgetLibraries (string[] libraries, string[] internalLibraries)
{
reloadRequested = false;
ArrayList libs = new ArrayList ();
libs.AddRange (libraries);
libs.AddRange (internalLibraries);
ProjectBackend.WidgetLibraries = libs;
libs = new ArrayList ();
libs.AddRange (internalLibraries);
ProjectBackend.InternalWidgetLibraries = libs;
app.UpdateWidgetLibraries (false, false);
if (!reloadRequested)
ProjectBackend.Reload ();
}
/* public bool CanCopySelection {
get { return Selection != null ? Selection.CanCopy : false; }
}
public bool CanCutSelection {
get { return Selection != null ? Selection.CanCut : false; }
}
public bool CanPasteToSelection {
get { return Selection != null ? Selection.CanPaste : false; }
}
public void CopySelection ()
{
if (Selection != null)
backend.ClipboardCopySelection ();
}
public void CutSelection ()
{
if (Selection != null)
backend.ClipboardCutSelection ();
}
public void PasteToSelection ()
{
if (Selection != null)
backend.ClipboardPaste ();
}
public void DeleteSelection ()
{
backend.DeleteSelection ();
}
*/
public void EditIcons ()
{
ProjectBackend.EditIcons ();
}
internal void NotifyWidgetAdded (object obj, string name, string typeName)
{
GuiDispatch.InvokeSync (
delegate {
Component c = App.GetComponent (obj, name, typeName);
if (c != null) {
WidgetInfo wi = GetWidget (c.Name);
if (wi == null) {
wi = new WidgetInfo (this, c);
widgets.Add (wi);
}
if (WidgetAdded != null)
WidgetAdded (this, new WidgetInfoEventArgs (this, wi));
}
}
);
}
internal void NotifyWidgetRemoved (string name)
{
GuiDispatch.InvokeSync (
delegate {
WidgetInfo wi = GetWidget (name);
if (wi != null) {
widgets.Remove (wi);
if (WidgetRemoved != null)
WidgetRemoved (this, new WidgetInfoEventArgs (this, wi));
}
}
);
}
internal void NotifyModifiedChanged ()
{
GuiDispatch.InvokeSync (
delegate {
if (ModifiedChanged != null)
ModifiedChanged (this, EventArgs.Empty);
}
);
}
internal void NotifyChanged ()
{
GuiDispatch.InvokeSync (
delegate {
if (Changed != null)
Changed (this, EventArgs.Empty);
// TODO: Optimize
foreach (ProjectItemInfo it in widgets)
it.NotifyChanged ();
foreach (ProjectItemInfo it in groups)
it.NotifyChanged ();
}
);
}
internal void NotifyWidgetNameChanged (object obj, string oldName, string newName, bool isRoot)
{
WidgetComponent c = obj != null ? (WidgetComponent) App.GetComponent (obj, null, null) : null;
if (c != null)
c.UpdateName (newName);
if (isRoot) {
WidgetInfo wi = GetWidget (oldName);
if (wi != null)
wi.NotifyNameChanged (newName);
}
GuiDispatch.InvokeSync (
delegate {
if (c != null) {
if (ComponentNameChanged != null)
ComponentNameChanged (this, new ComponentNameEventArgs (this, c, oldName));
}
}
);
}
internal void NotifyActionGroupAdded (string group)
{
GuiDispatch.InvokeSync (delegate {
ActionGroupInfo gi = GetActionGroup (group);
if (gi == null) {
gi = new ActionGroupInfo (this, group);
groups.Add (gi);
}
if (ActionGroupsChanged != null)
ActionGroupsChanged (this, EventArgs.Empty);
});
}
internal void NotifyActionGroupRemoved (string group)
{
GuiDispatch.InvokeSync (delegate {
ActionGroupInfo gi = GetActionGroup (group);
if (gi != null) {
groups.Remove (gi);
if (ActionGroupsChanged != null)
ActionGroupsChanged (this, EventArgs.Empty);
}
});
}
internal void NotifySignalAdded (object obj, string name, Signal signal)
{
GuiDispatch.InvokeSync (delegate {
if (SignalAdded != null) {
Component c = App.GetComponent (obj, name, null);
if (c != null)
SignalAdded (this, new ComponentSignalEventArgs (this, c, null, signal));
}
});
}
internal void NotifySignalRemoved (object obj, string name, Signal signal)
{
GuiDispatch.InvokeSync (delegate {
if (SignalRemoved != null) {
Component c = App.GetComponent (obj, name, null);
if (c != null)
SignalRemoved (this, new ComponentSignalEventArgs (this, c, null, signal));
}
});
}
internal void NotifySignalChanged (object obj, string name, Signal oldSignal, Signal signal)
{
GuiDispatch.InvokeSync (delegate {
if (SignalChanged != null) {
Component c = App.GetComponent (obj, name, null);
if (c != null)
SignalChanged (this, new ComponentSignalEventArgs (this, c, oldSignal, signal));
}
});
}
internal void NotifyProjectReloaded ()
{
GuiDispatch.InvokeSync (delegate {
if (ProjectReloaded != null)
ProjectReloaded (this, EventArgs.Empty);
});
}
internal void NotifyProjectReloading ()
{
GuiDispatch.InvokeSync (delegate {
if (ProjectReloading != null)
ProjectReloading (this, EventArgs.Empty);
});
}
internal void NotifyComponentTypesChanged ()
{
GuiDispatch.InvokeSync (delegate {
if (ComponentTypesChanged != null)
ComponentTypesChanged (this, EventArgs.Empty);
});
}
internal string ImportFile (string filePath)
{
if (importFileDelegate != null) {
string res = null;
GuiDispatch.InvokeSync (delegate {
res = importFileDelegate (filePath);
});
return res;
}
else
return filePath;
}
internal void NotifyUpdateLibraries ()
{
app.UpdateWidgetLibraries (false, false);
}
void OnBackendChanging ()
{
selection = null;
if (BackendChanging != null)
BackendChanging ();
}
void OnBackendChanged (ApplicationBackend oldBackend)
{
if (oldBackend != null) {
tmpProjectFile = Path.GetTempFileName ();
backend.Save (tmpProjectFile);
}
backend = app.Backend.CreateProject ();
backend.SetFrontend (this);
if (tmpProjectFile != null && File.Exists (tmpProjectFile)) {
backend.Load (tmpProjectFile, fileName);
File.Delete (tmpProjectFile);
tmpProjectFile = null;
} else if (fileName != null) {
backend.Load (fileName);
}
if (resourceProvider != null)
backend.ResourceProvider = resourceProvider;
if (BackendChanged != null)
BackendChanged (oldBackend);
if (ProjectReloaded != null)
ProjectReloaded (this, EventArgs.Empty);
}
}
public abstract class ProjectItemInfo
{
string name;
protected Project project;
public event EventHandler Changed;
internal ProjectItemInfo (Project project, string name)
{
this.name = name;
this.project = project;
}
public string Name {
get { return name; }
set { Component.Name = value; name = value; }
}
internal void NotifyNameChanged (string name)
{
this.name = name;
NotifyChanged ();
}
internal void NotifyChanged ()
{
if (Changed != null)
Changed (this, EventArgs.Empty);
}
public abstract Component Component { get; }
}
public class WidgetInfo: ProjectItemInfo
{
string type;
List<ActionGroupInfo> groups;
internal WidgetInfo (Project project, string name, string type): base (project, name)
{
this.type = type;
}
internal WidgetInfo (Project project, Component c): base (project, c.Name)
{
type = c.Type.Name;
}
public IEnumerable<ActionGroupInfo> ActionGroups {
get {
if (groups != null)
return groups;
else
return new ActionGroupInfo [0];
}
}
public string Type {
get { return type; }
}
public bool IsWindow {
get { return type == "Gtk.Dialog" || type == "Gtk.Window"; }
}
public override Component Component {
get { return project.GetComponent (Name); }
}
internal void AddGroup (string group)
{
if (groups == null)
groups = new List<ActionGroupInfo> ();
groups.Add (new ActionGroupInfo (project, group, Name));
}
}
public class ActionGroupInfo: ProjectItemInfo
{
string widgetName;
internal ActionGroupInfo (Project project, string name): base (project, name)
{
}
internal ActionGroupInfo (Project project, string name, string widgetName): base (project, name)
{
this.widgetName = widgetName;
}
public override Component Component {
get {
ActionGroupComponent[] ags;
if (widgetName != null) {
WidgetComponent c = project.GetComponent (widgetName) as WidgetComponent;
if (c == null)
return null;
ags = c.GetActionGroups ();
}
else {
ags = project.GetActionGroups ();
}
foreach (ActionGroupComponent ag in ags)
if (ag.Name == Name)
return ag;
return null;
}
}
}
public delegate string ImportFileDelegate (string fileName);
}