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
|
//
// ImlVisualEditor.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2016 jp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Crow;
using System.Threading;
using System.Xml.Serialization;
using System.ComponentModel;
using System.IO;
using System.Collections.Generic;
using Crow.IML;
namespace CrowIDE
{
public class ImlVisualEditor : GraphicObject
{
#region CTOR
public ImlVisualEditor () : base()
{
imlVE = new DesignInterface ();
Thread t = new Thread (interfaceThread);
t.IsBackground = true;
t.Start ();
}
#endregion
DesignInterface imlVE;
Instantiator itor;
GraphicObject selectedItem;
string imlSource;
ProjectItem projectItem;
bool drawGrid;
int gridSpacing;
[XmlAttributeAttribute][DefaultValue(true)]
public bool DrawGrid {
get { return drawGrid; }
set {
if (drawGrid == value)
return;
drawGrid = value;
NotifyValueChanged ("DrawGrid", drawGrid);
RegisterForRedraw ();
}
}
[XmlAttributeAttribute][DefaultValue(10)]
public int GridSpacing {
get { return gridSpacing; }
set {
if (gridSpacing == value)
return;
gridSpacing = value;
NotifyValueChanged ("GridSpacing", gridSpacing);
RegisterForRedraw ();
}
}
[XmlAttributeAttribute]public GraphicObject SelectedItem {
get { return selectedItem; }
set {
if (selectedItem == value)
return;
selectedItem = value;
NotifyValueChanged ("SelectedItem", selectedItem);
RegisterForRedraw ();
}
}
/// <summary>Pointer is over the widget</summary>
public virtual GraphicObject HoverWidget
{
get { return imlVE.HoverWidget; }
set {
if (HoverWidget == value)
return;
imlVE.HoverWidget = value;
NotifyValueChanged ("HoverWidget", HoverWidget);
}
}
[XmlIgnore]public List<LQIList> LQIs {
get { return imlVE.LQIs; }
}
[XmlIgnore]public string ImlSource {
get { return imlSource; }
set {
if (imlSource == value)
return;
imlSource = value;
NotifyValueChanged ("ImlSource", ImlSource);
reloadFromSource ();
}
}
[XmlAttributeAttribute][DefaultValue("")]
public string ImlPath {
get { return projectItem?.Path; }
}
[XmlAttributeAttribute]
public ProjectNode ProjectNode {
get { return projectItem; }
set {
if (projectItem == value)
return;
if (!(value is ProjectItem))
return;
projectItem = value as ProjectItem;
NotifyValueChanged ("ImlPath", ImlPath);
reloadFromPath ();
}
}
public List<GraphicObject> GraphicTree {
get { return imlVE.GraphicTree; }
}
void reloadFromSource(){
if (string.IsNullOrEmpty (imlSource)) {
reload_iTor (null);
return;
}
Instantiator iTmp;
try {
iTmp = Instantiator.CreateFromImlFragment (imlSource);
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine (ex.ToString());
return;
}
reload_iTor (iTmp);
}
void reloadFromPath(){
string path = Path.Combine (projectItem?.Project.RootDir,projectItem?.Path);
if (!File.Exists (path)){
System.Diagnostics.Debug.WriteLine ("Path not found: " + path);
reload_iTor (null);
return;
}
using (StreamReader sr = new StreamReader (path)) {
ImlSource = sr.ReadToEnd ();
}
NotifyValueChanged ("GraphicTree", null);
NotifyValueChanged ("GraphicTree", GraphicTree);
SelectedItem = null;
}
void reload_iTor(Instantiator new_iTot){
itor = new_iTot;
lock (imlVE.UpdateMutex) {
try {
imlVE.ClearInterface ();
if (itor != null)
imlVE.AddWidget(itor.CreateInstance(imlVE));
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine (ex.ToString());
}
}
}
void interfaceThread()
{
while (true) {
try {
imlVE.Update ();
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine (ex.ToString ());
if (Monitor.IsEntered(imlVE.UpdateMutex))
Monitor.Exit (imlVE.UpdateMutex);
}
bool isDirty = false;
lock (imlVE.RenderMutex)
isDirty = imlVE.IsDirty;
if (isDirty) {
lock (CurrentInterface.UpdateMutex)
RegisterForRedraw ();
}
Thread.Sleep (2);
}
}
#region GraphicObject overrides
public override void OnLayoutChanges (LayoutingType layoutType)
{
base.OnLayoutChanges (layoutType);
switch (layoutType) {
case LayoutingType.Width:
case LayoutingType.Height:
imlVE.ProcessResize (this.ClientRectangle.Size);
break;
}
}
public override void onMouseMove (object sender, MouseMoveEventArgs e)
{
//base.onMouseMove (sender, e);
GraphicObject oldHW = HoverWidget;
Rectangle scr = this.ScreenCoordinates (this.getSlot ());
ProcessMouseMove (e.X - scr.X, e.Y - scr.Y);
if (oldHW == HoverWidget)
return;
RegisterForRedraw ();
}
public override void onMouseDown (object sender, MouseButtonEventArgs e)
{
//base.onMouseDown (sender, e);
SelectedItem = HoverWidget;
}
protected override void onDraw (Cairo.Context gr)
{
base.onDraw (gr);
if (!drawGrid)
return;
Rectangle cb = ClientRectangle;
const double gridLineWidth = 0.1;
double glhw = gridLineWidth / 2.0;
int nbLines = cb.Width / gridSpacing ;
double d = cb.Left + gridSpacing;
for (int i = 0; i < nbLines; i++) {
gr.MoveTo (d-glhw, cb.Y);
gr.LineTo (d-glhw, cb.Bottom);
d += gridSpacing;
}
nbLines = cb.Height / gridSpacing;
d = cb.Top + gridSpacing;
for (int i = 0; i < nbLines; i++) {
gr.MoveTo (cb.X, d - glhw);
gr.LineTo (cb.Right, d -glhw);
d += gridSpacing;
}
gr.LineWidth = gridLineWidth;
Foreground.SetAsSource (gr, cb);
gr.Stroke ();
lock (imlVE.RenderMutex) {
using (Cairo.Surface surf = new Cairo.ImageSurface (imlVE.bmp, Cairo.Format.Argb32,
imlVE.ClientRectangle.Width, imlVE.ClientRectangle.Height, imlVE.ClientRectangle.Width * 4)) {
gr.SetSourceSurface (surf, cb.Left, cb.Top);
gr.Paint ();
}
imlVE.IsDirty = false;
}
Rectangle hr;
if (HoverWidget != null) {
hr = HoverWidget.ScreenCoordinates (HoverWidget.getSlot ());
// gr.SetSourceColor (Color.LightGray);
// gr.DrawCote (new Cairo.PointD (hr.X, hr.Center.Y), new Cairo.PointD (hr.Right, hr.Center.Y));
// gr.DrawCote (new Cairo.PointD (hr.Center.X, hr.Y), new Cairo.PointD (hr.Center.X, hr.Bottom));
//hr.Inflate (2);
gr.SetSourceColor (Color.LightGray);
gr.SetDash (new double[]{ 3.0, 3.0 }, 0.0);
gr.Rectangle (hr, 1.0);
}
if (SelectedItem == null)
return;
hr = SelectedItem.ScreenCoordinates(SelectedItem.getSlot ());
hr.Inflate (1);
gr.SetSourceColor (Color.Yellow);
gr.SetDash (new double[]{ 5.0, 3.0 },0.0);
gr.Rectangle (hr, 1.0);
}
#endregion
void WidgetCheckOver (GraphicObject go, MouseMoveEventArgs e){
Type tGo = go.GetType();
if (typeof(TemplatedGroup).IsAssignableFrom (tGo)) {
} else if (typeof(TemplatedContainer).IsAssignableFrom (tGo)) {
TemplatedContainer c = go as TemplatedContainer;
if (c.Content?.MouseIsIn (e.Position) == true) {
WidgetCheckOver (c.Content, e);
return;
}
} else if (typeof(TemplatedControl).IsAssignableFrom (tGo)) {
} else if (typeof(Group).IsAssignableFrom (tGo)) {
Group c = go as Group;
for (int i = c.Children.Count -1; i >= 0; i--) {
if (c.Children[i].MouseIsIn (e.Position)) {
WidgetCheckOver (c.Children[i], e);
return;
}
}
} else if (typeof(Crow.Container).IsAssignableFrom (tGo)) {
Crow.Container c = go as Crow.Container;
if (c.Child?.MouseIsIn (e.Position)==true) {
WidgetCheckOver (c.Child, e);
return;
}
}
HoverWidget = go;
WidgetMouseEnter (go, e);
}
void WidgetMouseLeave (GraphicObject go, MouseMoveEventArgs e){
}
void WidgetMouseEnter (GraphicObject go, MouseMoveEventArgs e){
}
void WidgetMouseMove (GraphicObject go, MouseMoveEventArgs e){}
public bool ProcessMouseMove(int x, int y)
{
int deltaX = x - imlVE.Mouse.X;
int deltaY = y - imlVE.Mouse.Y;
imlVE.Mouse.X = x;
imlVE.Mouse.Y = y;
MouseMoveEventArgs e = new MouseMoveEventArgs (x, y, deltaX, deltaY);
e.Mouse = imlVE.Mouse;
if (imlVE.ActiveWidget != null) {
//TODO, ensure object is still in the graphic tree
//send move evt even if mouse move outside bounds
WidgetMouseMove (imlVE.ActiveWidget, e);
return true;
}
if (HoverWidget != null) {
//TODO, ensure object is still in the graphic tree
//check topmost graphicobject first
GraphicObject tmp = HoverWidget;
GraphicObject topc = null;
while (tmp is GraphicObject) {
topc = tmp;
tmp = tmp.LogicalParent as GraphicObject;
}
int idxhw = imlVE.GraphicTree.IndexOf (topc);
if (idxhw != 0) {
int i = 0;
while (i < idxhw) {
if (imlVE.GraphicTree [i].LogicalParent == imlVE.GraphicTree [i].Parent) {
if (imlVE.GraphicTree [i].MouseIsIn (e.Position)) {
while (imlVE.HoverWidget != null) {
WidgetMouseLeave (imlVE.HoverWidget, e);
imlVE.HoverWidget = imlVE.HoverWidget.LogicalParent as GraphicObject;
}
WidgetCheckOver (GraphicTree [i], e);
return true;
}
}
i++;
}
}
if (imlVE.HoverWidget.MouseIsIn (e.Position)) {
WidgetCheckOver (imlVE.HoverWidget, (e));
return true;
} else {
WidgetMouseLeave (imlVE.HoverWidget, e);
//seek upward from last focused graph obj's
while (imlVE.HoverWidget.LogicalParent as GraphicObject != null) {
imlVE.HoverWidget = imlVE.HoverWidget.LogicalParent as GraphicObject;
if (imlVE.HoverWidget.MouseIsIn (e.Position)) {
WidgetCheckOver (imlVE.HoverWidget, e);
return true;
} else
WidgetMouseLeave (imlVE.HoverWidget, e);
}
}
}
//top level graphic obj's parsing
lock (imlVE.GraphicTree) {
for (int i = 0; i < imlVE.GraphicTree.Count; i++) {
GraphicObject g = imlVE.GraphicTree [i];
if (g.MouseIsIn (e.Position)) {
WidgetCheckOver (g, e);
return true;
}
}
}
imlVE.HoverWidget = null;
return false;
}
void GTView_SelectedItemChanged (object sender, SelectionChangeEventArgs e)
{
SelectedItem = e.NewValue as GraphicObject;
}
}
}
|