Re: Disabling Display of JPG and PDF Files in NSTextView
Re: Disabling Display of JPG and PDF Files in NSTextView
- Subject: Re: Disabling Display of JPG and PDF Files in NSTextView
- From: "Kirt Cathey" <email@hidden>
- Date: Thu, 15 Sep 2005 03:53:36 +0000
Douglas,
Okay, got that cleared up -- it has nothing to do with glyph stuff. Thanks
that has been eating at me for a while. Perhaps, if I could see what I am
doing wrong, I will become enlightened, but I am inclined to say that this
NSTextView implementation is stupid.
//BEGIN RANT//
First, I would like to know who at Apple came up with this brilliant idea?
I can hear it,
"Yeah, just design/program NSTextView to attach bmp, doc, rtfd, and
anything else with icons and filewrapper names. EXCEPT, yeah almost forgot,
make sure pdf and jpg files open in the view! Yeah, everybody'll love
that!"
//END RANT//
The implementation is very straightforward. An NSTextView in a MyDocument
that is setup as a delegate to MyDocument with an append file button. When
the user presses the append file button, the following code is executed....
most of which has already been pasted, but here it is for completeness
sake.
///////////////// BEGIN CODE ////////////////////////
- (void) appendFile: (id) sender
{
NSOpenPanel *opanel = [NSOpenPanel openPanel];
int opRet;
opRet = [opanel runModalForDirectory:NSHomeDirectory()
file:nil
types:nil];
if (opRet == NSOKButton)
{
NSLog([opanel filename]);
NSString *filePath = [[NSString alloc] initWithString:[opanel filename]];
NSString *attachmentName = [[NSString alloc] initWithString: [filePath
lastPathComponent]];
NSFileWrapper *theFileWrapper =[[NSFileWrapper alloc]
initWithPath:filePath];
[theFileWrapper setPreferredFilename:attachmentName];
NSTextAttachment *theAttachment = [[NSTextAttachment
alloc]initWithFileWrapper:theFileWrapper];
NSTextAttachmentCell *bCell = [[NSTextAttachmentCell alloc]
initImageCell:[theFileWrapper icon]];
// BEGIN Another attempt at resolving jpg and pdf file attachment!
if ([filePath hasSuffix:@"jpg"] || [filePath hasSuffix:@"pdf"] ||
[filePath hasSuffix:@"jpeg"])
{
[theAttachment setAttachmentCell:bCell];
[bCell setAttachment:theAttachment];
}
// END Another attempt at resolving jpg and pdf file attachment
[[attachmentTextView textStorage] appendAttributedString:
[NSAttributedString attributedStringWithAttachment:theAttachment]];
NSAttributedString *displayName = [[NSAttributedString alloc]
initWithString:attachmentName];
NSAttributedString *spacer = [[NSAttributedString alloc]
initWithString:@"\n"];
[[attachmentTextView textStorage] appendAttributedString:displayName];
[[attachmentTextView textStorage] appendAttributedString:spacer];
[self sendHandleWorkPapers:currentRow];
[theFileWrapper release];
[attachmentName release];
[filePath release];
[theAttachment release];
[bCell release];
[displayName release];
[spacer release];
}else{
NSLog(@"Cancel");
}
}
///////////////// END CODE ////////////////////////
When the user clicks on an attachment icon in the NSTextView
(attachmentTextView above), the following triggers opening of the file.
///////////////// BEGIN CODE ////////////////////////
- (void)textView:(NSTextView *)textView clickedOnCell:(id
<NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame
atIndex:(unsigned)charIndex
{
NSTextAttachment *myAttachment = [[[NSTextAttachment
alloc]init]autorelease];
myAttachment = [cell attachment];
NSFileWrapper *myFileWrapper = [myAttachment fileWrapper];
NSData *myData = [myFileWrapper regularFileContents];
NSWorkspace *myWorkspace = [NSWorkspace sharedWorkspace];
NSFileManager *projectFile = [NSFileManager defaultManager];
// New way using temporary directory
NSMutableString *myPath = [[[NSMutableString alloc]
initWithString:NSTemporaryDirectory()]autorelease];
[myPath appendString:[myFileWrapper preferredFilename]];
[projectFile createFileAtPath:myPath
contents:myData
attributes:nil];
[myWorkspace openFile:myPath];
}
///////////////// END CODE ////////////////////////
Just one more rant.... it is rediculous to have to go through so much to
disable such a feature. Where does one go to submit a Cocoa feature request
to have this disabled? Most implementors of NSTextView will NOT want a pdf
to display in a text view. Admittedly, some may.... those who are writing
PDF Viewers (like we need a dozen or so more of those) or parsing PDF
files, but the vast, vast majority of programmers do not want this. At the
very minimum, there should be a flag within NSTextView that programmers
flip with YES or NO to turn this feature off.
Regards,
-------------------------
Kirt S. Cathey
http://www.bizolutions.com
-------------------------
From: Douglas Davidson <email@hidden>
To: Kirt Cathey <email@hidden>
CC: email@hidden
Subject: Re: Disabling Display of JPG and PDF Files in NSTextView
Date: Wed, 14 Sep 2005 18:07:09 -0700
On Sep 14, 2005, at 2:40 PM, Kirt Cathey wrote:
First of all, thanks for helping out. Everything works without any
changes to the attachment cell, however, pdf and jpg do not appear
as icons -- they show up as opened images.
None of the text storage features are changed. Am just trying to
store those particular files as icons. Other files whose cells I do
not change -- Word, Excel -- work great.
I'm afraid I don't understand when you are calling this code, how
you are importing attachments into your text, how you are storing
your text, and so on. It's difficult to diagnose this without more
context.
As for glyphs, a glyph is a single displayable element in a
particular font. Glyphs often match up on a one-to-one basis with
characters, but not always--for example, the characters "fi" may be
displayed with a single fi ligature glyph in certain fonts; in some
cases a single character may require more than one glyph. In
addition, glyphs can be inserted without corresponding characters--
for example, hyphen glyphs in automatically hyphenated text.
The primary job of the text system is to convert the characters and
attributes in the text storage into a list of glyphs and positions
in the various text containers, so that the text views can be
displayed.
For most glyphs, we call upon Quartz to perform the display. In the
case of attachments, however, a placeholder glyph value
(NSControlGlyph) is used, and at display time the text attachment
cell is called upon to draw itself in the appropriate region.
None of this really has anything to do with how the text is saved;
that is entirely a function of the text storage.
Douglas Davidson
_________________________________________________________________
無料でメールボックス250MBの 「MSN Hotmail」 http://messenger.msn.co.jp/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden