aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/Docker.cs
blob: 3b66eb7e1820bbe3e015ccbac156467e1376ad48 (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
//
// DocksView.cs
//
// Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// 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.Xml.Serialization;
using System.ComponentModel;
using Crow.IML;

namespace Crow
{
	public class Docker : Group
	{
		static Instantiator instStack, instSplit, instSpacer;
		#region CTOR
		static Docker () {
			instStack = Instantiator.CreateFromImlFragment(@"<GenericStack Background='Blue' AllowDrop='true' DragEnter='onStackDragEnter'/>");
			instSplit = Instantiator.CreateFromImlFragment(@"<Splitter/>");
			instSpacer = Instantiator.CreateFromImlFragment(@"<GraphicObject Background='Red' IsEnabled='false'/>");
		}
		public Docker () : base ()
		{
		}
		#endregion

		GenericStack rootStack = null;

		public override void AddChild (GraphicObject g)
		{
			DockWindow dw = g as DockWindow;
			if (dw == null)
				throw new Exception ("Docker accept only DockWindow as child");
			
			base.AddChild (g);

			dw.LogicalParent = this;
		}
		public override void RemoveChild (GraphicObject child)
		{
			child.LogicalParent = null;


			base.RemoveChild (child);
		}

		GenericStack mainStack = null;
		int dockingThreshold;

		[XmlAttributeAttribute][DefaultValue(10)]
		public virtual int DockingThreshold {
			get { return dockingThreshold; }
			set {
				if (dockingThreshold == value)
					return;
				dockingThreshold = value; 
				NotifyValueChanged ("DockingThreshold", dockingThreshold);

			}
		}

		bool showDockingTarget = false;
		Alignment dockingDirection = Alignment.Center;
		int dockingDiv = 5;

		public override void onMouseMove (object sender, MouseMoveEventArgs e)
		{			
			if (CurrentInterface.DragAndDropOperation?.DragSource as DockWindow != null) {
				DockWindow dw = CurrentInterface.DragAndDropOperation?.DragSource as DockWindow;
				if (CurrentInterface.DragAndDropOperation.DragSource.Parent == this && !dw.IsDocked)
					dw.MoveAndResize (e.XDelta, e.YDelta);

				Rectangle r = ClientRectangle;
				int vTreshold = r.Height / dockingDiv;
				int hTreshold = r.Width / dockingDiv;


				bool showDock = true;

				if (dw.Slot.Left < hTreshold)
					dockingDirection = Alignment.Left;
				else if (dw.Slot.Right > r.Right - hTreshold)
					dockingDirection = Alignment.Right;
				else if (dw.Slot.Top < vTreshold)
					dockingDirection = Alignment.Top;
				else if (dw.Slot.Bottom > r.Bottom - vTreshold)
					dockingDirection = Alignment.Bottom;
				else {
					dockingDirection = Alignment.Center;
					showDock = false;
				}

				showDockingTarget = showDock;

				RegisterForGraphicUpdate ();

				//System.Diagnostics.Debug.WriteLine ("Dock: {0}", dockingDirection);
			} else
				showDockingTarget = false;

			base.onMouseMove (sender, e);
		}
		protected override void onDragEnter (object sender, DragDropEventArgs e)
		{
			base.onDragEnter (sender, e);
		}
		void onStackDragEnter (object sender, DragDropEventArgs e)
		{
			base.onDragEnter (sender, e);

			mainStack = sender as GenericStack;
		}
		protected override void onDraw (Cairo.Context gr)
		{
			gr.Save ();

			Rectangle rBack = new Rectangle (Slot.Size);

			Background.SetAsSource (gr, rBack);
			CairoHelpers.CairoRectangle (gr, rBack, CornerRadius);
			gr.Fill ();

			if (ClipToClientRect) {
				//clip to client zone
				CairoHelpers.CairoRectangle (gr, ClientRectangle, CornerRadius);
				gr.Clip ();
			}

			lock (Children) {
				foreach (GraphicObject g in Children) {
					if (CurrentInterface.DragAndDropOperation?.DragSource == g)
						continue;
					g.Paint (ref gr);
				}
			}

			if (showDockingTarget) {
				Rectangle r;
				if (mainStack == null)
					r = ClientRectangle;
				else
					r = mainStack.ClientRectangle;
				
				switch (dockingDirection) {
				case Alignment.Top:
					gr.Rectangle (r.Left, r.Top, r.Width, r.Height / dockingDiv);
					break;
				case Alignment.Bottom:
					gr.Rectangle (r.Left, r.Bottom - r.Height / dockingDiv, r.Width, r.Height / dockingDiv);
					break;
				case Alignment.Left:
					gr.Rectangle (r.Left, r.Top, r.Width / dockingDiv, r.Height);
					break;
				case Alignment.Right:
					gr.Rectangle (r.Right - r.Width / dockingDiv, r.Top, r.Width / dockingDiv, r.Height);
					break;
				}
				gr.LineWidth = 1;
				gr.SetSourceRGBA (0.4, 0.4, 0.9, 0.4);
				gr.FillPreserve ();
				gr.SetSourceRGBA (0.9, 0.9, 1.0, 0.8);
				gr.Stroke ();
			}

			if (CurrentInterface.DragAndDropOperation != null)
				CurrentInterface.DragAndDropOperation.DragSource.Paint (ref gr);


			gr.Restore ();	
		}

		public void Dock(DockWindow dw){
			if (dockingDirection == Alignment.Center)
				return;
			lock (CurrentInterface.UpdateMutex) {

				Splitter splitter = instSplit.CreateInstance<Splitter> (CurrentInterface);

				dw.Resizable = false;
				dw.Left = dw.Top = 0;
				this.RemoveChild (dw);

				Rectangle r;
				if (mainStack == null) {
					mainStack = instStack.CreateInstance<GenericStack> (CurrentInterface);
					this.AddChild (mainStack);
					this.putWidgetOnBottom (mainStack);
					r = ClientRectangle;
				} else
					r = mainStack.ClientRectangle;				
				 
				int vTreshold = r.Height / dockingDiv;
				int hTreshold = r.Width / dockingDiv;

				switch (dockingDirection) {
				case Alignment.Top:						
					dw.Width = Measure.Stretched;
					mainStack.Orientation = Orientation.Vertical;
					mainStack.AddChild (dw);
					mainStack.AddChild (splitter);
					//mainStack.AddChild (instSpacer.CreateInstance (CurrentInterface));
					break;
				case Alignment.Bottom:
					dw.Width = Measure.Stretched;
					mainStack.Orientation = Orientation.Vertical;
					//mainStack.AddChild (instSpacer.CreateInstance (CurrentInterface));
					mainStack.AddChild (splitter);
					mainStack.AddChild (dw);
					break;
				case Alignment.Left:
					dw.Height = Measure.Stretched;
					mainStack.Orientation = Orientation.Horizontal;
					mainStack.AddChild (dw);
					mainStack.AddChild (splitter);
					//mainStack.AddChild (instSpacer.CreateInstance (CurrentInterface));
					break;
				case Alignment.Right:
					dw.Height = Measure.Stretched;
					mainStack.Orientation = Orientation.Horizontal;
					//mainStack.AddChild (instSpacer.CreateInstance (CurrentInterface));
					mainStack.AddChild (splitter);
					mainStack.AddChild (dw);
					break;
				}

			}
		}
		protected override void onDrop (object sender, DragDropEventArgs e)
		{
			base.onDrop (sender, e);
		}
	}
}