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

DockContainer.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: 6a7281fe2ae04bb29b77725f543a0feb3b7c72f2 (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
//
// DockContainer.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.Collections.Generic;
using Gtk;
using Gdk;

namespace MonoDevelop.Components.Docking
{
	class DockContainer: Container, IShadedWidget
	{
		DockLayout layout;
		DockFrame frame;
		
		List<TabStrip> notebooks = new List<TabStrip> ();
		List<DockItem> items = new List<DockItem> ();
		
		bool needsRelayout = true;
		
		DockGroup currentHandleGrp;
		int currentHandleIndex;
		bool dragging;
		int dragPos;
		int dragSize;
		
		PlaceholderWindow placeholderWindow;
		
		static Gdk.Cursor hresizeCursor = new Gdk.Cursor (CursorType.SbHDoubleArrow);
		static Gdk.Cursor vresizeCursor = new Gdk.Cursor (CursorType.SbVDoubleArrow);
		
		public DockContainer (DockFrame frame)
		{
			this.Events = EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.PointerMotionMask | EventMask.LeaveNotifyMask;
			this.frame = frame;
			frame.ShadedContainer.Add (this);
		}

		internal DockGroupItem FindDockGroupItem (string id)
		{
			if (layout == null)
				return null;
			else
				return layout.FindDockGroupItem (id);
		}
		
		public List<DockItem> Items {
			get { return items; }
		}

		public DockLayout Layout {
			get { return layout; }
			set { layout = value; }
		}

		public void Clear ()
		{
			layout = null;
		}

		public void LoadLayout (DockLayout dl)
		{
			// Sticky items currently selected in notebooks will remain
			// selected after switching the layout
			List<DockItem> sickyOnTop = new List<DockItem> ();
			foreach (DockItem it in items) {
				if ((it.Behavior & DockItemBehavior.Sticky) != 0) {
					DockGroupItem gitem = FindDockGroupItem (it.Id);
					if (gitem != null && gitem.ParentGroup.IsSelectedPage (it))
						sickyOnTop.Add (it);
				}
			}			
			
			if (layout != null)
				layout.StoreAllocation ();
			layout = dl;
			layout.RestoreAllocation ();
			
			// Make sure items not present in this layout are hidden
			foreach (DockItem it in items) {
				if ((it.Behavior & DockItemBehavior.Sticky) != 0)
					it.Visible = it.StickyVisible;
				if (layout.FindDockGroupItem (it.Id) == null)
					it.HideWidget ();
			}
			
			RelayoutWidgets ();

			foreach (DockItem it in sickyOnTop)
				it.Present (false);
		}
		
		public void StoreAllocation ()
		{
			if (layout != null)
				layout.StoreAllocation ();
		}
		
		protected override void OnSizeRequested (ref Requisition req)
		{
			if (layout != null) {
				LayoutWidgets ();
				req = layout.SizeRequest ();
			}
		}
		
		protected override void OnSizeAllocated (Gdk.Rectangle rect)
		{
			base.OnSizeAllocated (rect);
			if (layout == null)
				return;
			
			// This container has its own window, so allocation of children
			// is relative to 0,0
			rect.X = rect.Y = 0;
			LayoutWidgets ();
			layout.Size = -1;
			layout.SizeAllocate (rect);
		}
		
		protected override void ForAll (bool include_internals, Gtk.Callback callback)
		{
			List<Widget> widgets = new List<Widget> ();
			foreach (Widget w in notebooks)
				widgets.Add (w);
			foreach (DockItem it in items) {
				if (it.HasWidget && it.Widget.Parent == this)
					widgets.Add (it.Widget);
			}
			foreach (Widget w in widgets)
				callback (w);
		}
		
		protected override bool OnExposeEvent (Gdk.EventExpose evnt)
		{
			bool res = base.OnExposeEvent (evnt);
			
			if (layout != null) {
				layout.Draw (evnt.Area, currentHandleGrp, currentHandleIndex);
			}
			return res;
		}


		public void RelayoutWidgets ()
		{
			needsRelayout = true;
			QueueResize ();
		}
		
		void LayoutWidgets ()
		{
			if (!needsRelayout)
				return;
			needsRelayout = false;
			
			// Create the needed notebooks and place the widgets in there
			
			List<DockGroup> tabbedGroups = new List<DockGroup> ();
			GetTabbedGroups (layout, tabbedGroups);
			
			for (int n=0; n<tabbedGroups.Count; n++) {
				DockGroup grp = tabbedGroups [n];
				TabStrip ts;
				if (n < notebooks.Count) {
					ts = notebooks [n];
				}
				else {
					ts = new TabStrip (frame);
					ts.Show ();
					notebooks.Add (ts);
					ts.Parent = this;
				}
				grp.UpdateNotebook (ts);
			}
			
			// Remove spare tab strips
			for (int n = notebooks.Count - 1; n >= tabbedGroups.Count; n--) {
				TabStrip ts = notebooks [n];
				notebooks.RemoveAt (n);
				ts.Clear ();
				ts.Unparent ();
				ts.Destroy ();
			}
				
			// Add widgets to the container
			
			layout.LayoutWidgets ();
			NotifySeparatorsChanged ();
		}
		
		void GetTabbedGroups (DockGroup grp, List<DockGroup> tabbedGroups)
		{
			if (grp.Type == DockGroupType.Tabbed) {
				if (grp.VisibleObjects.Count > 1)
					tabbedGroups.Add (grp);
				else
					grp.ResetNotebook ();
			}
			else {
				// Make sure it doesn't have a notebook bound to it
				grp.ResetNotebook ();
				foreach (DockObject ob in grp.Objects) {
					if (ob is DockGroup)
						GetTabbedGroups ((DockGroup) ob, tabbedGroups);
				}
			}
		}
		
		protected override bool OnButtonPressEvent (Gdk.EventButton ev)
		{
			if (currentHandleGrp != null) {
				dragging = true;
				dragPos = (currentHandleGrp.Type == DockGroupType.Horizontal) ? (int)ev.X : (int)ev.Y;
				DockObject obj = currentHandleGrp.VisibleObjects [currentHandleIndex];
				dragSize = (currentHandleGrp.Type == DockGroupType.Horizontal) ? obj.Allocation.Width : obj.Allocation.Height;
			}
			return base.OnButtonPressEvent (ev);
		}
		
		protected override bool OnButtonReleaseEvent (Gdk.EventButton e)
		{
			dragging = false;
			return base.OnButtonReleaseEvent (e);
		}
		
		protected override bool OnMotionNotifyEvent (Gdk.EventMotion e)
		{
			if (dragging) {
				NotifySeparatorsChanged ();
				int newpos = (currentHandleGrp.Type == DockGroupType.Horizontal) ? (int)e.X : (int)e.Y;
				if (newpos != dragPos) {
					int nsize = dragSize + (newpos - dragPos);
					currentHandleGrp.ResizeItem (currentHandleIndex, nsize);
					layout.DrawSeparators (Allocation, currentHandleGrp, currentHandleIndex, true, null);
				}
			}
			else if (layout != null && placeholderWindow == null) {
				int index;
				DockGroup grp;
				if (FindHandle (layout, (int)e.X, (int)e.Y, out grp, out index)) {
					if (currentHandleGrp != grp || currentHandleIndex != index) {
						if (grp.Type == DockGroupType.Horizontal)
							this.GdkWindow.Cursor = hresizeCursor;
						else
							this.GdkWindow.Cursor = vresizeCursor;
						currentHandleGrp = grp;
						currentHandleIndex = index;
						layout.DrawSeparators (Allocation, currentHandleGrp, currentHandleIndex, true, null);
					}
				}
				else if (currentHandleGrp != null) {
					ResetHandleHighlight ();
				}
			}
			return base.OnMotionNotifyEvent (e);
		}
		
		void ResetHandleHighlight ()
		{
			this.GdkWindow.Cursor = null;
			currentHandleGrp = null;
			currentHandleIndex = -1;
			if (layout != null)
				layout.DrawSeparators (Allocation, null, -1, true, null);
		}
		
		protected override bool OnLeaveNotifyEvent (EventCrossing evnt)
		{
			if (!dragging && evnt.Mode != CrossingMode.Grab)
				ResetHandleHighlight ();
			return base.OnLeaveNotifyEvent (evnt);
		}

		
		bool FindHandle (DockGroup grp, int x, int y, out DockGroup foundGrp, out int objectIndex)
		{
			if (grp.Type != DockGroupType.Tabbed && grp.Allocation.Contains (x, y)) {
				for (int n=0; n<grp.VisibleObjects.Count; n++) {
					DockObject obj = grp.VisibleObjects [n];
					if (n < grp.Objects.Count - 1) {
						if ((grp.Type == DockGroupType.Horizontal && x > obj.Allocation.Right && x < obj.Allocation.Right + frame.TotalHandleSize) ||
						    (grp.Type == DockGroupType.Vertical && y > obj.Allocation.Bottom && y < obj.Allocation.Bottom + frame.TotalHandleSize))
						{
							foundGrp = grp;
							objectIndex = n;
							return true;
						}
					}
					if (obj is DockGroup) {
						if (FindHandle ((DockGroup) obj, x, y, out foundGrp, out objectIndex))
							return true;
					}
				}
			}
			
			foundGrp = null;
			objectIndex = 0;
			return false;
		}
		
		protected override void OnRealized ()
		{
			WidgetFlags |= WidgetFlags.Realized;
			
			Gdk.WindowAttr attributes = new Gdk.WindowAttr ();
			attributes.X = Allocation.X;
			attributes.Y = Allocation.Y;
			attributes.Height = Allocation.Height;
			attributes.Width = Allocation.Width;
			attributes.WindowType = Gdk.WindowType.Child;
			attributes.Wclass = Gdk.WindowClass.InputOutput;
			attributes.Visual = Visual;
			attributes.Colormap = Colormap;
			attributes.EventMask = (int)(Events |
				Gdk.EventMask.ExposureMask |
				Gdk.EventMask.Button1MotionMask |
				Gdk.EventMask.ButtonPressMask |
				Gdk.EventMask.ButtonReleaseMask);
		
			Gdk.WindowAttributesType attributes_mask =
				Gdk.WindowAttributesType.X |
				Gdk.WindowAttributesType.Y |
				Gdk.WindowAttributesType.Colormap |
				Gdk.WindowAttributesType.Visual;
			GdkWindow = new Gdk.Window (ParentWindow, attributes, (int)attributes_mask);
			GdkWindow.UserData = Handle;

			Style = Style.Attach (GdkWindow);
			Style.SetBackground (GdkWindow, State);
			
			//GdkWindow.SetBackPixmap (null, true);
		}
		
		internal void ShowPlaceholder ()
		{
			placeholderWindow = new PlaceholderWindow (frame);
		}
		
		internal bool UpdatePlaceholder (DockItem item, Gdk.Size size, bool allowDocking)
		{
			if (placeholderWindow == null)
				return false;
			
			int px, py;
			GetPointer (out px, out py);
			
			placeholderWindow.AllowDocking = allowDocking;
			
			DockDelegate dockDelegate;
			Gdk.Rectangle rect;
			if (allowDocking && layout.GetDockTarget (item, px, py, out dockDelegate, out rect)) {
				int ox, oy;
				GdkWindow.GetOrigin (out ox, out oy);
				
				placeholderWindow.Relocate (ox + rect.X, oy + rect.Y, rect.Width, rect.Height, true);
				placeholderWindow.Show ();
				return true;
			} else {
				int ox, oy;
				GdkWindow.GetOrigin (out ox, out oy);
				placeholderWindow.Relocate (ox + px - size.Width / 2, oy + py - 18, size.Width, size.Height, false);
				placeholderWindow.Show ();
			}
			return false;
		}
		
		internal void DockInPlaceholder (DockItem item)
		{
			if (placeholderWindow == null || !placeholderWindow.Visible)
				return;
			
			item.Status = DockItemStatus.Dockable;
			
			int px, py;
			GetPointer (out px, out py);
			
			DockDelegate dockDelegate;
			Gdk.Rectangle rect;
			if (placeholderWindow.AllowDocking && layout.GetDockTarget (item, px, py, out dockDelegate, out rect)) {
				DockGroupItem dummyItem = new DockGroupItem (frame, new DockItem (frame, "__dummy"));
				DockGroupItem gitem = layout.FindDockGroupItem (item.Id);
				gitem.ParentGroup.ReplaceItem (gitem, dummyItem);
				dockDelegate (item);
				dummyItem.ParentGroup.Remove (dummyItem);
				RelayoutWidgets ();
			} else {
				DockGroupItem gi = FindDockGroupItem (item.Id);
				int pw, ph;
				placeholderWindow.GetPosition (out px, out py);
				placeholderWindow.GetSize (out pw, out ph);
				gi.FloatRect = new Rectangle (px, py, pw, ph);
				item.Status = DockItemStatus.Floating;
			}
		}
		
		internal void HidePlaceholder ()
		{
			if (placeholderWindow != null) {
				placeholderWindow.Destroy ();
				placeholderWindow = null;
			}
		}
		
		public IEnumerable<Rectangle> GetShadedAreas ()
		{
			List<Gdk.Rectangle> rects = new List<Gdk.Rectangle> ();
			if (layout != null)
				layout.DrawSeparators (Allocation, currentHandleGrp, currentHandleIndex, true, rects);
			return rects;
		}
		
		internal void NotifySeparatorsChanged ()
		{
			if (AreasChanged != null)
				AreasChanged (this, EventArgs.Empty);
		}
		
		public event EventHandler AreasChanged;

	}
}