diff options
author | Lluis Sanchez Gual <lluis@xamarin.com> | 2014-02-22 04:26:47 +0400 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@xamarin.com> | 2014-02-22 04:26:47 +0400 |
commit | 63a9168e9bf0c75dd2fb3b43558042a3927b14df (patch) | |
tree | b254b2e065e2d80f2d4da795eb25e9c367cf9953 /Xwt.Mac | |
parent | a59d93f117a8f1a4dda753c3239c7ee0b2d23269 (diff) |
[Mac] Implement new cell api
Diffstat (limited to 'Xwt.Mac')
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/CanvasTableCell.cs | 24 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/CellUtil.cs | 29 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/CellViewBackend.cs | 100 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/CheckBoxTableCell.cs | 15 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/CompositeCell.cs | 17 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/ICellRenderer.cs | 2 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/ImageTableCell.cs | 19 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.CellViews/TextTableCell.cs | 21 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac.csproj | 1 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac/ListViewBackend.cs | 11 | ||||
-rw-r--r-- | Xwt.Mac/Xwt.Mac/TableViewBackend.cs | 4 |
11 files changed, 179 insertions, 64 deletions
diff --git a/Xwt.Mac/Xwt.Mac.CellViews/CanvasTableCell.cs b/Xwt.Mac/Xwt.Mac.CellViews/CanvasTableCell.cs index 175fd4bc..a2967b7a 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/CanvasTableCell.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/CanvasTableCell.cs @@ -33,8 +33,6 @@ namespace Xwt.Mac { class CanvasTableCell: NSCell, ICellRenderer { - ICanvasCellViewFrontend cellView; - public CanvasTableCell (IntPtr p): base (p) { } @@ -43,32 +41,30 @@ namespace Xwt.Mac { } - public CanvasTableCell (ICanvasCellViewFrontend cellView) - { - this.cellView = cellView; - } - public CompositeCell CellContainer { get; set; } public void CopyFrom (object other) { var ob = (CanvasTableCell)other; - cellView = ob.cellView; + Backend = ob.Backend; } public void Fill () { } - public ICellViewFrontend Frontend { - get { return cellView; } + ICanvasCellViewFrontend Frontend { + get { return (ICanvasCellViewFrontend) Backend.Frontend; } } + public CellViewBackend Backend { get; set; } + + public override SizeF CellSizeForBounds (RectangleF bounds) { var size = new SizeF (); - cellView.ApplicationContext.InvokeUserCode (delegate { - var s = cellView.GetRequiredSize (); + Frontend.ApplicationContext.InvokeUserCode (delegate { + var s = Frontend.GetRequiredSize (); size = new SizeF ((float)s.Width, (float)s.Height); }); if (size.Width > bounds.Width) @@ -86,8 +82,8 @@ namespace Xwt.Mac Context = ctx, InverseViewTransform = ctx.GetCTM ().Invert () }; - cellView.ApplicationContext.InvokeUserCode (delegate { - cellView.Draw (backend, new Rectangle (cellFrame.X, cellFrame.Y, cellFrame.Width, cellFrame.Height)); + Frontend.ApplicationContext.InvokeUserCode (delegate { + Frontend.Draw (backend, new Rectangle (cellFrame.X, cellFrame.Y, cellFrame.Width, cellFrame.Height)); }); } } diff --git a/Xwt.Mac/Xwt.Mac.CellViews/CellUtil.cs b/Xwt.Mac/Xwt.Mac.CellViews/CellUtil.cs index e5a24c31..ea67e285 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/CellUtil.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/CellUtil.cs @@ -34,25 +34,32 @@ namespace Xwt.Mac { static class CellUtil { - public static NSCell CreateCell (ICellSource source, ICollection<CellView> cells) + public static NSCell CreateCell (NSTableView table, ICellSource source, ICollection<CellView> cells, int column) { CompositeCell c = new CompositeCell (Orientation.Horizontal, source); foreach (var cell in cells) - c.AddCell ((ICellRenderer) CreateCell (c, cell)); + c.AddCell ((ICellRenderer) CreateCell (table, c, cell, column)); return c; } - static NSCell CreateCell (CompositeCell source, CellView cell) + static NSCell CreateCell (NSTableView table, CompositeCell source, CellView cell, int column) { + ICellRenderer cr = null; + if (cell is ITextCellViewFrontend) - return new TextTableCell ((ITextCellViewFrontend) cell); - if (cell is IImageCellViewFrontend) - return new ImageTableCell ((IImageCellViewFrontend) cell); - if (cell is ICanvasCellViewFrontend) - return new CanvasTableCell ((ICanvasCellViewFrontend) cell); - if (cell is ICheckBoxCellViewFrontend) - return new CheckBoxTableCell ((ICheckBoxCellViewFrontend) cell); - throw new NotImplementedException (); + cr = new TextTableCell (); + else if (cell is IImageCellViewFrontend) + cr = new ImageTableCell (); + else if (cell is ICanvasCellViewFrontend) + cr = new CanvasTableCell (); + else if (cell is ICheckBoxCellViewFrontend) + cr = new CheckBoxTableCell (); + else + throw new NotImplementedException (); + cr.Backend = new CellViewBackend (table, column); + ICellViewFrontend fr = cell; + fr.AttachBackend (null, cr.Backend); + return (NSCell)cr; } } } diff --git a/Xwt.Mac/Xwt.Mac.CellViews/CellViewBackend.cs b/Xwt.Mac/Xwt.Mac.CellViews/CellViewBackend.cs new file mode 100644 index 00000000..332e556b --- /dev/null +++ b/Xwt.Mac/Xwt.Mac.CellViews/CellViewBackend.cs @@ -0,0 +1,100 @@ +// +// CellViewBackend.cs +// +// Author: +// Lluis Sanchez Gual <lluis@xamarin.com> +// +// Copyright (c) 2014 Xamarin, Inc (http://www.xamarin.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 Xwt.Backends; +using MonoMac.AppKit; + +namespace Xwt.Mac +{ + public class CellViewBackend: ICellViewBackend + { + NSTableView table; + int column; + + public CellViewBackend (NSTableView table, int column) + { + this.table = table; + this.column = column; + } + + public ICellViewFrontend Frontend { get; private set; } + + public virtual void InitializeBackend (object frontend, ApplicationContext context) + { + Frontend = (ICellViewFrontend)frontend; + } + + public NSCell CurrentCell { get; set; } + + public int CurrentRow { get; set; } + + internal ITablePosition CurrentPosition { get; set; } + + public virtual void EnableEvent (object eventId) + { + } + + public virtual void DisableEvent (object eventId) + { + } + + public Rectangle CellBounds { + get { + if (CurrentPosition is TableRow) { + var r = table.GetCellFrame (column, ((TableRow)CurrentPosition).Row); + r = ((ICellRenderer)CurrentCell).CellContainer.GetCellRect (r, CurrentCell); + return new Rectangle (r.X, r.Y, r.Width, r.Height); + } + return Rectangle.Zero; + } + } + + public Rectangle BackgroundBounds { + get { + // TODO + return CellBounds; + } + } + + public bool Selected { + get { + if (CurrentPosition is TableRow) { + return table.IsRowSelected (((TableRow)CurrentPosition).Row); + } + // TODO + return false; + } + } + + public bool HasFocus { + get { + // TODO + return false; + } + } + } +} + diff --git a/Xwt.Mac/Xwt.Mac.CellViews/CheckBoxTableCell.cs b/Xwt.Mac/Xwt.Mac.CellViews/CheckBoxTableCell.cs index 3c6a610d..d4a3c88f 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/CheckBoxTableCell.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/CheckBoxTableCell.cs @@ -31,8 +31,6 @@ namespace Xwt.Mac { class CheckBoxTableCell: NSButtonCell, ICellRenderer { - ICheckBoxCellViewFrontend cellView; - public CheckBoxTableCell () { SetButtonType (NSButtonType.Switch); @@ -42,6 +40,7 @@ namespace Xwt.Mac void HandleActivated (object sender, EventArgs e) { + var cellView = Frontend; if (cellView.Editable && !cellView.RaiseToggled () && (cellView.StateField != null || cellView.ActiveField != null)) { if (cellView.StateField != null) CellContainer.SetValue (cellView.StateField, State.ToXwtState ()); @@ -54,19 +53,17 @@ namespace Xwt.Mac { } - public CheckBoxTableCell (ICheckBoxCellViewFrontend cellView): this () - { - this.cellView = cellView; + ICheckBoxCellViewFrontend Frontend { + get { return (ICheckBoxCellViewFrontend) Backend.Frontend; } } - public ICellViewFrontend Frontend { - get { return cellView; } - } + public CellViewBackend Backend { get; set; } public CompositeCell CellContainer { get; set; } public void Fill () { + var cellView = Frontend; AllowsMixedState = cellView.AllowMixed || cellView.State == CheckBoxState.Mixed; State = cellView.State.ToMacState (); Editable = cellView.Editable; @@ -75,7 +72,7 @@ namespace Xwt.Mac public void CopyFrom (object other) { var ob = (CheckBoxTableCell)other; - cellView = ob.cellView; + Backend = ob.Backend; } public override void EditWithFrame (System.Drawing.RectangleF aRect, NSView inView, NSText editor, MonoMac.Foundation.NSObject delegateObject, NSEvent theEvent) diff --git a/Xwt.Mac/Xwt.Mac.CellViews/CompositeCell.cs b/Xwt.Mac/Xwt.Mac.CellViews/CompositeCell.cs index 045c1da6..6eefccfd 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/CompositeCell.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/CompositeCell.cs @@ -127,7 +127,9 @@ namespace Xwt.Mac public void Fill () { foreach (var c in cells) { - c.Frontend.Initialize (this); + c.Backend.CurrentCell = (NSCell) c; + c.Backend.CurrentPosition = tablePosition; + c.Backend.Frontend.Load (this); c.Fill (); } @@ -142,7 +144,7 @@ namespace Xwt.Mac } IEnumerable<ICellRenderer> VisibleCells { - get { return cells.Where (c => c.Frontend.Visible); } + get { return cells.Where (c => c.Backend.Frontend.Visible); } } SizeF CalcSize () @@ -223,6 +225,17 @@ namespace Xwt.Mac return base.TrackMouse (theEvent, cellFrame, controlView, untilMouseUp); } + public RectangleF GetCellRect (RectangleF cellFrame, NSCell cell) + { + if (tablePosition is TableRow) { + foreach (var c in GetCells (cellFrame)) { + if (c.Cell == cell) + return c.Frame; + } + } + return RectangleF.Empty; + } + CellPos GetHitCell (NSEvent theEvent, RectangleF cellFrame, NSView controlView) { foreach (CellPos cp in GetCells(cellFrame)) { diff --git a/Xwt.Mac/Xwt.Mac.CellViews/ICellRenderer.cs b/Xwt.Mac/Xwt.Mac.CellViews/ICellRenderer.cs index 8eda33d7..76e284cc 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/ICellRenderer.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/ICellRenderer.cs @@ -35,7 +35,7 @@ namespace Xwt.Mac { interface ICellRenderer: ICopiableObject { - ICellViewFrontend Frontend { get; } + CellViewBackend Backend { get; set; } CompositeCell CellContainer { get; set; } void Fill (); } diff --git a/Xwt.Mac/Xwt.Mac.CellViews/ImageTableCell.cs b/Xwt.Mac/Xwt.Mac.CellViews/ImageTableCell.cs index 2009b754..338e28d6 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/ImageTableCell.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/ImageTableCell.cs @@ -34,8 +34,6 @@ namespace Xwt.Mac { class ImageTableCell: NSImageCell, ICellRenderer { - IImageCellViewFrontend cellView; - public ImageTableCell () { } @@ -44,16 +42,17 @@ namespace Xwt.Mac { } - public ImageTableCell (IImageCellViewFrontend cellView) - { - this.cellView = cellView; + IImageCellViewFrontend Frontend { + get { return (IImageCellViewFrontend) Backend.Frontend; } } - + + public CellViewBackend Backend { get; set; } + public CompositeCell CellContainer { get; set; } public void Fill () { - ObjectValue = cellView.Image.ToImageDescription ().ToNSImage (); + ObjectValue = Frontend.Image.ToImageDescription ().ToNSImage (); } public override System.Drawing.SizeF CellSize { @@ -66,14 +65,10 @@ namespace Xwt.Mac } } - public ICellViewFrontend Frontend { - get { return cellView; } - } - public void CopyFrom (object other) { var ob = (ImageTableCell)other; - cellView = ob.cellView; + Backend = ob.Backend; } } } diff --git a/Xwt.Mac/Xwt.Mac.CellViews/TextTableCell.cs b/Xwt.Mac/Xwt.Mac.CellViews/TextTableCell.cs index 5292c50f..bc21628d 100644 --- a/Xwt.Mac/Xwt.Mac.CellViews/TextTableCell.cs +++ b/Xwt.Mac/Xwt.Mac.CellViews/TextTableCell.cs @@ -35,8 +35,6 @@ namespace Xwt.Mac { class TextTableCell: NSTextFieldCell, ICellRenderer { - ITextCellViewFrontend cellView; - public TextTableCell () { } @@ -45,29 +43,26 @@ namespace Xwt.Mac { } - public TextTableCell (ITextCellViewFrontend cellView) - { - this.cellView = cellView; + ITextCellViewFrontend Frontend { + get { return (ITextCellViewFrontend) Backend.Frontend; } } + public CellViewBackend Backend { get; set; } + public CompositeCell CellContainer { get; set; } public void Fill () { - if (cellView.Markup != null) - AttributedStringValue = FormattedText.FromMarkup (cellView.Markup).ToAttributedString (); + if (Frontend.Markup != null) + AttributedStringValue = FormattedText.FromMarkup (Frontend.Markup).ToAttributedString (); else - StringValue = cellView.Text ?? ""; - } - - public ICellViewFrontend Frontend { - get { return cellView; } + StringValue = Frontend.Text ?? ""; } public void CopyFrom (object other) { var ob = (TextTableCell)other; - cellView = ob.cellView; + Backend = ob.Backend; } } } diff --git a/Xwt.Mac/Xwt.Mac.csproj b/Xwt.Mac/Xwt.Mac.csproj index 389071b0..4a501465 100644 --- a/Xwt.Mac/Xwt.Mac.csproj +++ b/Xwt.Mac/Xwt.Mac.csproj @@ -127,6 +127,7 @@ <Compile Include="Xwt.Mac\PasswordEntryBackend.cs" /> <Compile Include="Xwt.Mac\WebViewBackend.cs" /> <Compile Include="Xwt.Mac\ScrollControlBackend.cs" /> + <Compile Include="Xwt.Mac.CellViews\CellViewBackend.cs" /> </ItemGroup> <Import Project="..\BuildHelpers.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> diff --git a/Xwt.Mac/Xwt.Mac/ListViewBackend.cs b/Xwt.Mac/Xwt.Mac/ListViewBackend.cs index 42f1bcc5..b683a814 100644 --- a/Xwt.Mac/Xwt.Mac/ListViewBackend.cs +++ b/Xwt.Mac/Xwt.Mac/ListViewBackend.cs @@ -85,6 +85,17 @@ namespace Xwt.Mac // TODO public bool BorderVisible { get; set; } + + + public int GetRowAtPosition (Point p) + { + return 0; + } + + public Rectangle GetCellBounds (int row, CellView cell, bool includeMargin) + { + return Rectangle.Zero; + } } class TableRow: NSObject, ITablePosition diff --git a/Xwt.Mac/Xwt.Mac/TableViewBackend.cs b/Xwt.Mac/Xwt.Mac/TableViewBackend.cs index 0a183b1e..f4891418 100644 --- a/Xwt.Mac/Xwt.Mac/TableViewBackend.cs +++ b/Xwt.Mac/Xwt.Mac/TableViewBackend.cs @@ -191,7 +191,7 @@ namespace Xwt.Mac var tcol = new NSTableColumn (); tcol.Editable = true; cols.Add (tcol); - var c = CellUtil.CreateCell (this, col.Views); + var c = CellUtil.CreateCell (Table, this, col.Views, cols.Count - 1); tcol.DataCell = c; Table.AddColumn (tcol); var hc = new NSTableHeaderCell (); @@ -209,7 +209,7 @@ namespace Xwt.Mac public void UpdateColumn (ListViewColumn col, object handle, ListViewColumnChange change) { NSTableColumn tcol = (NSTableColumn) handle; - tcol.DataCell = CellUtil.CreateCell (this, col.Views); + tcol.DataCell = CellUtil.CreateCell (Table, this, col.Views, cols.IndexOf (tcol)); } public void SelectAll () |