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

DockGroupItem.cs « MonoDevelop.Components.Docking « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89e01c2b90e437b39d9f27fba946fbe036dd4198 (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
//
// DockGroupItem.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.Xml;
using Gtk;

namespace MonoDevelop.Components.Docking
{
	internal class DockGroupItem: DockObject
	{
		DockItem item;
		bool visibleFlag;
		DockItemStatus status;
		Gdk.Rectangle floatRect;
		Gtk.PositionType barDocPosition;
		int autoHideSize = -1;
		
		public DockItem Item {
			get {
				return item;
			}
			set {
				item = value;
			}
		}
		
		public string Id {
			get { return item.Id; }
		}
		
		public DockGroupItem (DockFrame frame, DockItem item): base (frame)
		{
			this.item = item;
			visibleFlag = item.Visible;
		}
		
		internal override void GetDefaultSize (out int width, out int height)
		{
			width = item.DefaultWidth;
			height = item.DefaultHeight;
		}
		
		internal override void GetMinSize (out int width, out int height)
		{
			Requisition req = SizeRequest ();
			width = req.Width;
			height = req.Height;
		}
		
		internal override Requisition SizeRequest ()
		{
			return item.Widget.SizeRequest ();
		}

		public override void SizeAllocate (Gdk.Rectangle newAlloc)
		{
			item.Widget.SizeAllocate (newAlloc);
			base.SizeAllocate (newAlloc);
		}
		
		public override bool Expand {
			get { return item.Expand; }
		}
		
		internal override void QueueResize ()
		{
			item.Widget.QueueResize ();
		}
		
		internal override bool GetDockTarget (DockItem item, int px, int py, out DockDelegate dockDelegate, out Gdk.Rectangle rect)
		{
			return GetDockTarget (item, px, py, Allocation, out dockDelegate, out rect);
		}
		
		public bool GetDockTarget (DockItem item, int px, int py, Gdk.Rectangle rect, out DockDelegate dockDelegate, out Gdk.Rectangle outrect)
		{
			dockDelegate = null;
			
			if (item != this.item && this.item.Visible && rect.Contains (px, py)) {
				int xdockMargin = (int) ((double)rect.Width * (1.0 - DockFrame.ItemDockCenterArea)) / 2;
				int ydockMargin = (int) ((double)rect.Height * (1.0 - DockFrame.ItemDockCenterArea)) / 2;
				DockPosition pos;
				
/*				if (ParentGroup.Type == DockGroupType.Tabbed) {
					rect = new Gdk.Rectangle (rect.X + xdockMargin, rect.Y + ydockMargin, rect.Width - xdockMargin*2, rect.Height - ydockMargin*2);
					pos = DockPosition.CenterAfter;
				}
*/				if (px <= rect.X + xdockMargin && ParentGroup.Type != DockGroupType.Horizontal) {
					outrect = new Gdk.Rectangle (rect.X, rect.Y, xdockMargin, rect.Height);
					pos = DockPosition.Left;
				}
				else if (px >= rect.Right - xdockMargin && ParentGroup.Type != DockGroupType.Horizontal) {
					outrect = new Gdk.Rectangle (rect.Right - xdockMargin, rect.Y, xdockMargin, rect.Height);
					pos = DockPosition.Right;
				}
				else if (py <= rect.Y + ydockMargin && ParentGroup.Type != DockGroupType.Vertical) {
					outrect = new Gdk.Rectangle (rect.X, rect.Y, rect.Width, ydockMargin);
					pos = DockPosition.Top;
				}
				else if (py >= rect.Bottom - ydockMargin && ParentGroup.Type != DockGroupType.Vertical) {
					outrect = new Gdk.Rectangle (rect.X, rect.Bottom - ydockMargin, rect.Width, ydockMargin);
					pos = DockPosition.Bottom;
				}
				else {
					outrect = new Gdk.Rectangle (rect.X + xdockMargin, rect.Y + ydockMargin, rect.Width - xdockMargin*2, rect.Height - ydockMargin*2);
					pos = DockPosition.Center;
				}
				
				dockDelegate = delegate (DockItem dit) {
					DockGroupItem it = ParentGroup.AddObject (dit, pos, Id);
					it.SetVisible (true);
					ParentGroup.FocusItem (it);
				};
				return true;
			}
			outrect = Gdk.Rectangle.Zero;
			return false;
		}
		
		internal override void Dump (int ind)
		{
			Console.WriteLine (new string (' ', ind) + item.Id + " size:" + Size + " alloc:" + Allocation);
		}
		
		internal override void Write (XmlWriter writer)
		{
			base.Write (writer);
			writer.WriteAttributeString ("id", item.Id);
			writer.WriteAttributeString ("visible", visibleFlag.ToString ());
			writer.WriteAttributeString ("status", status.ToString ());
			
			if (status == DockItemStatus.AutoHide)
				writer.WriteAttributeString ("autoHidePosition", barDocPosition.ToString ());
			
			if (autoHideSize != -1)
				writer.WriteAttributeString ("autoHideSize", autoHideSize.ToString ());
			
			if (!floatRect.Equals (Gdk.Rectangle.Zero)) {
				writer.WriteAttributeString ("floatX", floatRect.X.ToString ());
				writer.WriteAttributeString ("floatY", floatRect.Y.ToString ());
				writer.WriteAttributeString ("floatWidth", floatRect.Width.ToString ());
				writer.WriteAttributeString ("floatHeight", floatRect.Height.ToString ());
			}
		}
		
		internal override void Read (XmlReader reader)
		{
			base.Read (reader);
			visibleFlag = bool.Parse (reader.GetAttribute ("visible")) && !item.IsPositionMarker;
			status = (DockItemStatus) Enum.Parse (typeof (DockItemStatus), reader.GetAttribute ("status"));
			int fx=0, fy=0, fw=0, fh=0;
			string s = reader.GetAttribute ("floatX");
			if (s != null)
				fx = int.Parse (s);
			s = reader.GetAttribute ("floatY");
			if (s != null)
				fy = int.Parse (s);
			s = reader.GetAttribute ("floatWidth");
			if (s != null)
				fw = int.Parse (s);
			s = reader.GetAttribute ("floatHeight");
			if (s != null)
				fh = int.Parse (s);
			s = reader.GetAttribute ("autoHidePosition");
			if (s != null)
				barDocPosition = (PositionType) Enum.Parse (typeof (PositionType), s);
			s = reader.GetAttribute ("autoHideSize");
			if (s != null)
				autoHideSize = int.Parse (s);
			floatRect = new Gdk.Rectangle (fx, fy, fw, fh);
		}
		
		public override void CopyFrom (DockObject ob)
		{
			base.CopyFrom (ob);
			DockGroupItem it = (DockGroupItem)ob;
			item = it.item;
			visibleFlag = it.visibleFlag;
			floatRect = it.floatRect;
		}
		
		internal override bool Visible {
			get { return visibleFlag && status == DockItemStatus.Dockable; }
		}

		internal bool VisibleFlag {
			get { return visibleFlag; }
		}

		public DockItemStatus Status {
			get {
				return status;
			}
			set {
				if (status == value)
					return;
					
				DockItemStatus oldValue = status;
				status = value;
				
				if (status == DockItemStatus.Floating) {
					if (floatRect.Equals (Gdk.Rectangle.Zero)) {
						int x, y;
						item.Widget.TranslateCoordinates (item.Widget.Toplevel, 0, 0, out x, out y);
						Gtk.Window win = Frame.Toplevel as Window;
						if (win != null) {
							int wx, wy;
							win.GetPosition (out wx, out wy);
							floatRect = new Gdk.Rectangle (wx + x, wy + y, Allocation.Width, Allocation.Height);
						}
					}
					item.SetFloatMode (floatRect);
				}
				else if (status == DockItemStatus.AutoHide) {
					SetBarDocPosition ();
					item.SetAutoHideMode (barDocPosition, GetAutoHideSize (barDocPosition));
				}
				else
					item.ResetMode ();
				
				if (oldValue == DockItemStatus.Dockable || status == DockItemStatus.Dockable) {
					// Update visibility if changing from/to dockable mode
					if (ParentGroup != null)
						ParentGroup.UpdateVisible (this);
				}
			}
		}
		
		void SetBarDocPosition ()
		{
			// Determine the best position for docking the item
			
			if (Allocation.IsEmpty) {
				// If the item is in a group, use the dock location of other items
				DockObject current = this;
				do {
					if (EstimateBarDocPosition (current.ParentGroup, current, out barDocPosition, out autoHideSize))
						return;
					current = current.ParentGroup;
				} while (current.ParentGroup != null);
				
				// Can't find a good location. Just guess.
				barDocPosition = PositionType.Bottom;
				autoHideSize = 200;
				return;
			}
			barDocPosition = CalcBarDocPosition ();
		}
		
		bool EstimateBarDocPosition (DockGroup grp, DockObject ignoreChild, out PositionType pos, out int size)
		{
			foreach (DockObject ob in grp.Objects) {
				if (ob == ignoreChild)
					continue;
				if (ob is DockGroup) {
					if (EstimateBarDocPosition ((DockGroup)ob, null, out pos, out size))
						return true;
				} else if (ob is DockGroupItem) {
					DockGroupItem it = (DockGroupItem) ob;
					if (it.status == DockItemStatus.AutoHide) {
						pos = it.barDocPosition;
						size = it.autoHideSize;
						return true;
					}
					if (!it.Allocation.IsEmpty) {
						pos = it.CalcBarDocPosition ();
						size = it.GetAutoHideSize (pos);
						return true;
					}
				}
			}
			pos = PositionType.Bottom;
			size = 0;
			return false;
		}
		
		PositionType CalcBarDocPosition ()
		{
			if (Allocation.Width < Allocation.Height) {
				int mid = Allocation.Left + Allocation.Width / 2;
				if (mid > Frame.Allocation.Left + Frame.Allocation.Width / 2)
					return PositionType.Right;
				else
					return PositionType.Left;
			} else {
				int mid = Allocation.Top + Allocation.Height / 2;
				if (mid > Frame.Allocation.Top + Frame.Allocation.Height / 2)
					return PositionType.Bottom;
				else
					return PositionType.Top;
			}
		}

		internal void SetVisible (bool value)
		{
			if (visibleFlag != value) {
				visibleFlag = value;
				if (visibleFlag)
					item.ShowWidget ();
				else
					item.HideWidget ();
				if (ParentGroup != null)
					ParentGroup.UpdateVisible (this);
			}
		}
		
		internal override void StoreAllocation ()
		{
			base.StoreAllocation ();
			if (Status == DockItemStatus.Floating)
				floatRect = item.FloatingPosition;
			else if (Status == DockItemStatus.AutoHide)
				autoHideSize = item.AutoHideSize;
		}

		internal override void RestoreAllocation ()
		{
			base.RestoreAllocation ();
			item.UpdateVisibleStatus ();
			
			if (Status == DockItemStatus.Floating)
				item.SetFloatMode (floatRect);
			else if (Status == DockItemStatus.AutoHide)
				item.SetAutoHideMode (barDocPosition, GetAutoHideSize (barDocPosition));
			else
				item.ResetMode ();
			
			if (!visibleFlag)
				item.HideWidget ();
		}
		
		int GetAutoHideSize (Gtk.PositionType pos)
		{
			if (autoHideSize != -1)
				return autoHideSize;

			if (pos == PositionType.Left || pos == PositionType.Right)
				return Allocation.Width;
			else
				return Allocation.Height;
		}

		public Gdk.Rectangle FloatRect {
			get {
				return floatRect;
			}
			set {
				floatRect = value;
			}
		}
		
		public override string ToString ()
		{
			return "[DockItem " + Item.Id + "]";
		}
	}
}