mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-29 23:50:37 +00:00
Basic implementation of NSTextAttachment and NSTextAttachmentCell
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6805 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
0b3cff5cc0
commit
f331085fa7
1 changed files with 220 additions and 0 deletions
220
Source/NSTextAttachment.m
Normal file
220
Source/NSTextAttachment.m
Normal file
|
@ -0,0 +1,220 @@
|
|||
/*
|
||||
NSTextAttachment.m
|
||||
|
||||
Classes to represent text attachments.
|
||||
|
||||
NSTextAttachment is used to represent text attachments. When inline,
|
||||
text attachments appear as the value of the NSAttachmentAttributeName
|
||||
attached to the special character NSAttachmentCharacter.
|
||||
|
||||
NSTextAttachment uses an object obeying the NSTextAttachmentCell
|
||||
protocol to get input from the user and to display an image.
|
||||
|
||||
NSTextAttachmentCell is a simple subclass of NSCell which provides
|
||||
the NSTextAttachment protocol.
|
||||
|
||||
Copyright (C) 2000 Free Software Foundation, Inc.
|
||||
|
||||
Author: Fred Kiefer <fredkiefer@gmx.de>
|
||||
Date: June 2000
|
||||
|
||||
This file is part of the GNUstep GUI Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
#include <AppKit/NSCell.h>
|
||||
#include <AppKit/NSFileWrapper.h>
|
||||
#include <AppKit/NSImage.h>
|
||||
#include <AppKit/NSTextContainer.h>
|
||||
#include <AppKit/NSTextAttachment.h>
|
||||
|
||||
|
||||
@implementation NSTextAttachmentCell
|
||||
|
||||
- (void)drawWithFrame:(NSRect)cellFrame
|
||||
inView:(NSView *)controlView
|
||||
characterIndex:(unsigned)charIndex
|
||||
{
|
||||
[self drawWithFrame: cellFrame
|
||||
inView: controlView];
|
||||
}
|
||||
|
||||
- (NSPoint)cellBaselineOffset
|
||||
{
|
||||
return NSZeroPoint;
|
||||
}
|
||||
|
||||
- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer
|
||||
proposedLineFragment:(NSRect)lineFrag
|
||||
glyphPosition:(NSPoint)position
|
||||
characterIndex:(unsigned)charIndex
|
||||
{
|
||||
NSRect aRect;
|
||||
|
||||
// FIXME: We ignore the proposedLineFragment
|
||||
aRect.origin = position;
|
||||
aRect.size = [self cellSize];
|
||||
return aRect;
|
||||
}
|
||||
|
||||
- (BOOL)wantsToTrackMouse
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)trackMouse:(NSEvent *)theEvent
|
||||
inRect:(NSRect)cellFrame
|
||||
ofView:(NSView *)controlView
|
||||
untilMouseUp:(BOOL)flag
|
||||
{
|
||||
// FIXME: This is not the correct behaviour
|
||||
return [super trackMouse: theEvent
|
||||
inRect: cellFrame
|
||||
ofView: controlView
|
||||
untilMouseUp: flag];
|
||||
}
|
||||
|
||||
- (BOOL)trackMouse:(NSEvent *)theEvent
|
||||
inRect:(NSRect)cellFrame
|
||||
ofView:(NSView *)controlView
|
||||
atCharacterIndex:(unsigned)charIndex
|
||||
untilMouseUp:(BOOL)flag
|
||||
{
|
||||
// FIXME: This is not the correct behaviour
|
||||
return [self trackMouse: theEvent
|
||||
inRect: cellFrame
|
||||
ofView: controlView
|
||||
untilMouseUp: flag];
|
||||
}
|
||||
|
||||
- (void)setAttachment:(NSTextAttachment *)anObject
|
||||
{
|
||||
NSFileWrapper *fileWrap = [anObject fileWrapper];
|
||||
|
||||
// Do not retain the attachment
|
||||
_attachment = anObject;
|
||||
|
||||
if (fileWrap != nil)
|
||||
{
|
||||
NSImage *icon = nil;
|
||||
NSString *fileName = [fileWrap filename];
|
||||
|
||||
if (fileName != nil)
|
||||
{
|
||||
// Try to set the image to the file wrapper content
|
||||
icon = [[NSImage alloc] initByReferencingFile: fileName];
|
||||
}
|
||||
if (icon == nil)
|
||||
icon = [fileWrap icon];
|
||||
|
||||
[self setImage: icon];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSTextAttachment *)attachment
|
||||
{
|
||||
return _attachment;
|
||||
}
|
||||
|
||||
//FIXME: I had to add those methods to keep the compiler quite.
|
||||
// This are already defined on the super class and should be taken from there.
|
||||
|
||||
- (NSSize)cellSize
|
||||
{
|
||||
return [super cellSize];
|
||||
}
|
||||
|
||||
- (void)highlight:(BOOL)flag
|
||||
withFrame:(NSRect)cellFrame
|
||||
inView:(NSView *)controlView
|
||||
{
|
||||
[super highlight: flag
|
||||
withFrame: cellFrame
|
||||
inView: controlView];
|
||||
}
|
||||
|
||||
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
|
||||
{
|
||||
[super drawWithFrame: cellFrame inView: controlView];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation NSTextAttachment
|
||||
|
||||
- (id)init
|
||||
{
|
||||
return [self initWithFileWrapper: nil];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
DESTROY(_fileWrapper);
|
||||
DESTROY(_cell);
|
||||
}
|
||||
|
||||
- (id)initWithFileWrapper:(NSFileWrapper *)fileWrapper
|
||||
{
|
||||
ASSIGN(_fileWrapper, fileWrapper);
|
||||
_cell = [[NSTextAttachmentCell alloc ] init];
|
||||
[_cell setAttachment: self];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setFileWrapper:(NSFileWrapper *)fileWrapper
|
||||
{
|
||||
ASSIGN(_fileWrapper, fileWrapper);
|
||||
}
|
||||
|
||||
- (NSFileWrapper *)fileWrapper
|
||||
{
|
||||
return _fileWrapper;
|
||||
}
|
||||
|
||||
- (id <NSTextAttachmentCell>)attachmentCell
|
||||
{
|
||||
return _cell;
|
||||
}
|
||||
|
||||
- (void)setAttachmentCell:(id <NSTextAttachmentCell>)cell
|
||||
{
|
||||
ASSIGN(_cell, cell);
|
||||
[_cell setAttachment: self];
|
||||
}
|
||||
|
||||
/*
|
||||
* NSCoding protocol
|
||||
*/
|
||||
- (void) encodeWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[aCoder encodeObject: _fileWrapper];
|
||||
[aCoder encodeObject: _cell];
|
||||
}
|
||||
|
||||
- (id) initWithCoder: (NSCoder*)aDecoder
|
||||
{
|
||||
[aDecoder decodeValueOfObjCType: @encode(id) at: &_fileWrapper];
|
||||
[aDecoder decodeValueOfObjCType: @encode(id) at: &_cell];
|
||||
|
||||
// Reconnect the cell, so the cell does not have to store the attachment
|
||||
[_cell setAttachment: self];
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
Loading…
Add table
Add a link
Reference in a new issue