2012-11-04 05:45:51 +00:00
|
|
|
using System;
|
2012-11-05 04:18:25 +00:00
|
|
|
using MonoMac.Foundation;
|
|
|
|
using MonoMac.AppKit;
|
|
|
|
using MonoMac.CoreGraphics;
|
|
|
|
using System.Drawing;
|
|
|
|
using MonoMac.ObjCRuntime;
|
|
|
|
using System.Collections.Generic;
|
2012-11-04 05:45:51 +00:00
|
|
|
|
|
|
|
namespace NBTExplorer.Mac
|
|
|
|
{
|
2012-11-05 04:18:25 +00:00
|
|
|
[Register("ImageAndTextCell")]
|
|
|
|
public class ImageAndTextCell : NSTextFieldCell
|
2012-11-04 05:45:51 +00:00
|
|
|
{
|
2012-11-05 04:18:25 +00:00
|
|
|
private NSImage _image;
|
|
|
|
private bool _copyCalled;
|
|
|
|
private bool _disposeCalled;
|
|
|
|
private bool _deallocCalled;
|
|
|
|
private bool _drawCalled;
|
|
|
|
|
2012-11-04 05:45:51 +00:00
|
|
|
public ImageAndTextCell ()
|
|
|
|
{
|
|
|
|
}
|
2012-11-05 04:18:25 +00:00
|
|
|
|
|
|
|
public ImageAndTextCell (IntPtr handle)
|
|
|
|
: base(handle)
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when created directly from a XIB file
|
|
|
|
[Export ("initWithCoder:")]
|
|
|
|
public ImageAndTextCell (NSCoder coder)
|
|
|
|
: base (coder)
|
|
|
|
{
|
|
|
|
Initialize ();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Initialize ()
|
|
|
|
{
|
|
|
|
LineBreakMode = NSLineBreakMode.TruncatingTail;
|
|
|
|
Selectable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
//if (_image != null)
|
|
|
|
// _image.Dispose();
|
|
|
|
|
|
|
|
//if (_noDispose)
|
|
|
|
// Handle = IntPtr.Zero;
|
|
|
|
_disposeCalled = true;
|
|
|
|
|
|
|
|
base.Dispose (disposing);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[Export("copyWithZone:")]
|
|
|
|
public virtual NSObject CopyWithZone(IntPtr zone) {
|
|
|
|
ImageAndTextCell cell = new ImageAndTextCell() {
|
|
|
|
Title = Title,
|
|
|
|
Image = Image,
|
|
|
|
};
|
|
|
|
cell._noDispose = true;
|
|
|
|
return cell;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
static List<ImageAndTextCell> _refPool = new List<ImageAndTextCell>();
|
|
|
|
|
|
|
|
//static IntPtr selRetain = Selector.GetHandle ("retain");
|
|
|
|
//static IntPtr selAutoRelease = Selector.GetHandle("autorelease");
|
|
|
|
//static IntPtr selRelease = Selector.GetHandle("release");
|
|
|
|
//static IntPtr selCopyWithZone = Selector.GetHandle("copyWithZone:");
|
|
|
|
|
|
|
|
[Export("copyWithZone:")]
|
|
|
|
public NSObject CopyWithZone (IntPtr zone)
|
|
|
|
{
|
|
|
|
//IntPtr copy = Messaging.IntPtr_objc_msgSendSuper_IntPtr(SuperHandle, selCopyWithZone, zone);
|
|
|
|
//var cloned = new ImageAndTextCell(copy);
|
|
|
|
//cloned.Title = Title;
|
|
|
|
//cloned.Image = Image;
|
|
|
|
|
|
|
|
var cloned = new ImageAndTextCell {
|
|
|
|
Title = Title,
|
|
|
|
Image = Image,
|
|
|
|
};
|
|
|
|
cloned._copyCalled = true;
|
|
|
|
|
|
|
|
_refPool.Add(cloned);
|
|
|
|
|
|
|
|
//Messaging.void_objc_msgSend (cloned.Handle, selRetain);
|
|
|
|
return cloned;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static IntPtr selDealloc = Selector.GetHandle("dealloc");
|
|
|
|
|
|
|
|
[Export("dealloc")]
|
|
|
|
public void Dealloc ()
|
|
|
|
{
|
|
|
|
_deallocCalled = true;
|
|
|
|
_refPool.Remove(this);
|
|
|
|
|
|
|
|
//Messaging.void_objc_msgSendSuper(SuperHandle, selDealloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public new NSImage Image
|
|
|
|
{
|
|
|
|
get { return _image; }
|
|
|
|
set { _image = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public override RectangleF ImageRectForBounds (RectangleF theRect)
|
|
|
|
{
|
|
|
|
if (_image != null) {
|
|
|
|
PointF origin = new PointF(theRect.X + 3, theRect.Y + (float)Math.Ceiling((theRect.Height - _image.Size.Height) / 2));
|
|
|
|
return new RectangleF(origin, _image.Size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return RectangleF.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override RectangleF TitleRectForBounds (RectangleF theRect)
|
|
|
|
{
|
|
|
|
if (_image != null) {
|
|
|
|
PointF origin = new PointF(theRect.X + 3 + _image.Size.Width, theRect.Y);
|
|
|
|
SizeF size = new SizeF(theRect.Width - 3 - _image.Size.Width, theRect.Height);
|
|
|
|
return new RectangleF(origin, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return base.TitleRectForBounds(theRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void EditWithFrame (RectangleF aRect, NSView inView, NSText editor, NSObject delegateObject, NSEvent theEvent)
|
|
|
|
{
|
|
|
|
RectangleF textFrame, imageFrame;
|
|
|
|
aRect.Divide(3 + _image.Size.Width, CGRectEdge.MinXEdge, out imageFrame, out textFrame);
|
|
|
|
base.EditWithFrame(textFrame, inView, editor, delegateObject, theEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SelectWithFrame (RectangleF aRect, NSView inView, NSText editor, NSObject delegateObject, int selStart, int selLength)
|
|
|
|
{
|
|
|
|
RectangleF textFrame, imageFrame;
|
|
|
|
aRect.Divide(3 + _image.Size.Width, CGRectEdge.MinXEdge, out imageFrame, out textFrame);
|
|
|
|
base.SelectWithFrame(textFrame, inView, editor, delegateObject, selStart, selLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void DrawWithFrame (RectangleF cellFrame, NSView inView)
|
|
|
|
{
|
|
|
|
Assert (!_deallocCalled, "DrawWithFrame: Dealloc was called on object");
|
|
|
|
Assert (!_disposeCalled, "DrawWithFrame: Dispose was called on object");
|
|
|
|
|
|
|
|
_drawCalled = true;
|
|
|
|
if (_image != null) {
|
|
|
|
RectangleF imageFrame;
|
|
|
|
cellFrame.Divide (3 + _image.Size.Width, CGRectEdge.MinXEdge, out imageFrame, out cellFrame);
|
|
|
|
|
|
|
|
if (DrawsBackground) {
|
|
|
|
BackgroundColor.Set ();
|
|
|
|
NSGraphics.RectFill (imageFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
imageFrame.X += 3;
|
|
|
|
imageFrame.Size = _image.Size;
|
|
|
|
|
|
|
|
//if (inView.IsFlipped) {
|
|
|
|
// imageFrame.Y += (float)Math.Ceiling((cellFrame.Height + imageFrame.Height) / 2);
|
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
imageFrame.Y += (float)Math.Ceiling ((cellFrame.Height - imageFrame.Height) / 2);
|
|
|
|
//}
|
|
|
|
|
|
|
|
_image.Draw (imageFrame, new RectangleF (PointF.Empty, _image.Size), NSCompositingOperation.SourceOver, 1f, true, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.DrawWithFrame (cellFrame, inView);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override SizeF CellSize
|
|
|
|
{
|
|
|
|
get {
|
|
|
|
if (_image != null)
|
|
|
|
return new SizeF(base.CellSize.Width + 3 + _image.Size.Width, base.CellSize.Height);
|
|
|
|
else
|
|
|
|
return new SizeF(base.CellSize.Width + 3, base.CellSize.Height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override NSCellHit HitTest (NSEvent forEvent, RectangleF inRect, NSView ofView)
|
|
|
|
{
|
|
|
|
PointF point = ofView.ConvertPointFromView (forEvent.LocationInWindow, null);
|
|
|
|
|
|
|
|
if (_image != null) {
|
|
|
|
RectangleF imageFrame;
|
|
|
|
inRect.Divide(3 + _image.Size.Width, CGRectEdge.MinXEdge, out imageFrame, out inRect);
|
|
|
|
|
|
|
|
imageFrame.X += 3;
|
|
|
|
imageFrame.Size = _image.Size;
|
|
|
|
if (ofView.MouseinRect(point, imageFrame))
|
|
|
|
return NSCellHit.ContentArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
return base.HitTest (forEvent, inRect, ofView);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Assert (bool condition, string message)
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
throw new Exception("Assert failed: " + message);
|
|
|
|
}
|
2012-11-04 05:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|