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

CompositeCell.cs « Xwt.Mac.CellViews « Xwt.XamMac - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1502aefe6d96c46de0a2dd4be1fe98d0c79b2ebe (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
// 
// CompositeCell.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// 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 System.Linq;
using AppKit;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using Xwt.Backends;

namespace Xwt.Mac
{
	class CompositeCell : NSView, ICopiableObject, ICellDataSource, INSCopying
	{
		ICellSource source;
		NSObject val;
		List<ICellRenderer> cells = new List<ICellRenderer> ();
		ITablePosition tablePosition;
		ApplicationContext context;

		public List<ICellRenderer> Cells {
			get {
				return cells;
			}
		}

		public CompositeCell (ApplicationContext context, ICellSource source)
		{
			if (source == null)
				throw new ArgumentNullException (nameof (source));
			this.context = context;
			this.source = source;
		}

		public CompositeCell (IntPtr p) : base (p)
		{
		}

		CompositeCell ()
		{
		}

		public ApplicationContext Context {
			get { return context; }
		}

		#region ICellDataSource implementation

		object ICellDataSource.GetValue (IDataField field)
		{
			return source.GetValue (tablePosition.Position, field.Index);
		}

		#endregion

		public void SetValue (IDataField field, object value)
		{
			source.SetValue (tablePosition.Position, field.Index, value);
		}

		public void SetCurrentEventRow ()
		{
			source.SetCurrentEventRow (tablePosition.Position);
		}

		bool recalculatingHeight = false;
		public void InvalidateRowHeight ()
		{
			if (tablePosition != null && !recalculatingHeight) {
				recalculatingHeight = true;
				source.InvalidateRowHeight (tablePosition.Position);
				recalculatingHeight = false;
			}
		}

		public double GetRequiredHeightForWidth (double width)
		{
			Fill (false);
			double height = 0;
			foreach (var c in GetCells (new CGSize (width, -1)))
				height = Math.Max (height, c.Frame.Height);
			return height;
		}

		public override NSObject Copy ()
		{
			var ob = (ICopiableObject)base.Copy ();
			ob.CopyFrom (this);
			return (NSObject)ob;
		}

		NSObject INSCopying.Copy (NSZone zone)
		{
			var ob = (ICopiableObject)new CompositeCell ();
			ob.CopyFrom (this);
			return (NSObject)ob;
		}

		void ICopiableObject.CopyFrom (object other)
		{
			var ob = (CompositeCell)other;
			if (ob.source == null)
				throw new ArgumentException ("Cannot copy from a CompositeCell with a null `source`");
			Identifier = ob.Identifier;
			context = ob.context;
			source = ob.source;
			cells = new List<ICellRenderer> ();
			foreach (var c in ob.cells) {
				var copy = (ICellRenderer)Activator.CreateInstance (c.GetType ());
				copy.CopyFrom (c);
				AddCell (copy);
			}
			if (tablePosition != null)
				Fill (false);
		}

		public virtual NSObject ObjectValue {
			[Export ("objectValue")]
			get {
				return val;
			}
			[Export ("setObjectValue:")]
			set {
				val = value;
				if (val is ITablePosition) {
					tablePosition = (ITablePosition)val;
					Fill ();
				} else if (val is NSNumber) {
					tablePosition = new TableRow () {
						Row = ((NSNumber)val).Int32Value
					};
					Fill ();
				} else
					tablePosition = null;
			}
		}

		internal ITablePosition TablePosition {
			get { return tablePosition; }
		}

		public void AddCell (ICellRenderer cell)
		{
			cell.CellContainer = this;
			cells.Add (cell);
			AddSubview ((NSView)cell);
		}

		public void ClearCells ()
		{
			foreach (NSView cell in cells) {
				cell.RemoveFromSuperview ();
			}
			cells.Clear ();
		}

		public override CGRect Frame {
			get { return base.Frame; }
			set {
				var oldSize = base.Frame.Size;
				base.Frame = value;
				if (oldSize != value.Size && tablePosition != null) {
					Fill(false);
					double height = 0;
					foreach (var c in GetCells (value.Size)) {
						c.Cell.Frame = c.Frame;
						c.Cell.NeedsDisplay = true;
						height = Math.Max (height, c.Frame.Height);
					}
					if (Math.Abs(value.Height - height) > double.Epsilon)
						InvalidateRowHeight ();
					
				}
			}
		}

		public void Fill (bool reallocateCells = true)
		{
			foreach (var c in cells) {
				c.Backend.Load (c);
				c.Fill ();
			}
			if (!reallocateCells || Frame.IsEmpty)
				return;

			foreach (var c in GetCells (Frame.Size)) {
				c.Cell.Frame = c.Frame;
				c.Cell.NeedsDisplay = true;
			}
		}

		public NSView GetCellViewForBackend (ICellViewBackend backend)
		{
			return cells.FirstOrDefault (c => c.Backend == backend) as NSView;
		}

		CGSize CalcSize ()
		{
			nfloat w = 0;
			nfloat h = 0;
			foreach (var cell in cells) {
				if (!cell.Backend.Frontend.Visible)
					continue;
				var c = (NSView)cell;
				var s = c.FittingSize;
				w += s.Width;
				if (s.Height > h)
					h = s.Height;
			}
			return new CGSize (w, h);
		}

		public override CGSize FittingSize {
			get {
				return CalcSize ();
			}
		}

		static readonly Selector selSetBackgroundStyle = new Selector ("setBackgroundStyle:");

		NSBackgroundStyle backgroundStyle;

		public virtual NSBackgroundStyle BackgroundStyle {
			[Export ("backgroundStyle")]
			get {
				return backgroundStyle;
			}
			[Export ("setBackgroundStyle:")]
			set {
				backgroundStyle = value;
				foreach (NSView cell in cells)
					if (cell.RespondsToSelector (selSetBackgroundStyle)) {
						if (IntPtr.Size == 8)
							Messaging.void_objc_msgSend_Int64 (cell.Handle, selSetBackgroundStyle.Handle, (long)value);
						else
							Messaging.void_objc_msgSend_int (cell.Handle, selSetBackgroundStyle.Handle, (int)value);
					} else
						cell.NeedsDisplay = true;
			}
		}
		
		List <CellPos> GetCells (CGSize cellSize)
		{
			int nexpands = 0;
			double requiredSize = 0;
			double availableSize = cellSize.Width;

			var cellFrames = new List<CellPos> (cells.Count);

			// Get the natural size of each child
			foreach (var cell in cells) {
				if (!cell.Backend.Frontend.Visible)
					continue;
				var cellPos = new CellPos { Cell = (NSView)cell, Frame = CGRect.Empty };
				cellFrames.Add (cellPos);
				var size = cellPos.Cell.FittingSize;
				cellPos.Frame.Width = size.Width;
				requiredSize += size.Width;
				if (cell.Backend.Frontend.Expands)
					nexpands++;
			}

			double remaining = availableSize - requiredSize;
			if (remaining > 0) {
				var expandRemaining = new SizeSplitter (remaining, nexpands);
				foreach (var cellFrame in cellFrames) {
					if (((ICellRenderer)cellFrame.Cell).Backend.Frontend.Expands)
						cellFrame.Frame.Width += (nfloat)expandRemaining.NextSizePart ();
				}
			}

			double x = 0;
			foreach (var cellFrame in cellFrames) {
				var width = cellFrame.Frame.Width;
				var canvas = cellFrame.Cell as ICanvasCellRenderer;
				var height = (canvas != null) ? canvas.GetRequiredSize (SizeConstraint.WithSize (width)).Height : cellFrame.Cell.FittingSize.Height;
				// y-align only if the cell has a valid height, otherwise we're just recalculating the required size
				var y = cellSize.Height > 0 ? (cellSize.Height - height) / 2 : 0;
				cellFrame.Frame = new CGRect (x, y, width, height);
				x += width;
			}
			return cellFrames;
		}

		class CellPos
		{
			public NSView Cell;
			public CGRect Frame;
		}

		class SizeSplitter
		{
			int rem;
			int part;

			public SizeSplitter (double total, int numParts)
			{
				if (numParts > 0)
				{
					part = ((int)total) / numParts;
					rem = ((int)total) % numParts;
				}
			}

			public double NextSizePart ()
			{
				if (rem > 0)
				{
					rem--;
					return part + 1;
				} else
					return part;
			}
		}

		bool isDisposed;

		public bool IsDisposed {
			get {
				try {
					// Cocoa may dispose the native view in NSView based table mode
					// in this case Handle and SuperHandle will become Zero.
					return isDisposed || Handle == IntPtr.Zero || SuperHandle == IntPtr.Zero;
				} catch {
					return true;
				}
			}
		}

		protected override void Dispose(bool disposing)
		{
			isDisposed = true;
			base.Dispose(disposing);
		}
	}
}