Re: Attack My Code! Part 1

2008-06-13 Thread Manfred Schwind
 NSData* data = [ attrString RTFDFromRange:wholeStringRange  
documentAttributes:nil ];

 [ thePasteboard setData:data forType:NSRTFPboardType ];


Either use RTFDFromRange and NSRTFDPboardType or RTFFromRange  
and NSRTFPboardType, but do not mix them.

Maybe that's the reason for the garbage, I don't know.

Regards,
Mani
--
http://mani.de - friendly software
iVolume - listen to music freehand
LittleSecrets - the encrypted notepad



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Attack My Code! Part 1

2008-06-12 Thread Charles Jenkins
I'm new at programming in Cocoa, and if it is not rude to post a big  
block of code to this list, I'd like to show you all a class I  
developed and ask you to tell me about all the ways I've screwed it up!


BACKGROUND INFO

I'm trying to learn Chinese and I have a bunch of comic books to (try  
to) read. Because writing is done with ideograms instead of an  
alphabet, it's a bit of a pain to look words up in a dictionary, so  
you wanna make notes to avoid having to look up the same character  
again when you find it on the next page. Making notes on paper is not  
ideal because it's hard to make them into a good reference; but making  
notes on the Mac is hugely annoying because it's annoying to start  
typing and find you haven't selected the right keyboard layout.


So what I want is an app that lets me enter word, pronunciation, and  
definition in separate fields that REMEMBER the proper keyboard layout  
and switch automatically when you enter and exit the fields.


I want to somehow connect my app to another app's text editor window  
and tell it to transfer in a given format (hopefully HTML or RTF so  
that the pronunciation and definition can be hidden until I need to  
refer to them) so that I can do most of the typing in my little app  
window and each time I've finished typing the word, pronunciation, and  
definition, I can smack a button that will copy the finished product  
to the pasteboard and send a signal to the other app to paste it.


The class I have to show to you today is the formatter that holds the  
word, pronunciation, and definition as the user types them in, and is  
ready at any time to provide them properly formatted in a variety of  
ways. The best of these so far is HTML format, which hides the  
pronunciation and definition in a tooltip!


Actually, the class seems to work pretty well, but the copyAsHtml and  
copyAsRtf methods seem to put garbage on the clipboard. I have pored  
over Apple's documentation regarding the pasteboard and converting an  
attributed string to RTF, but there must be some crucial point I am  
missing.


So I am looking for advice about how to get copyAsHtml and copyAsRtf  
to work correctly, but also advice about anything else that I may be  
doing wrong in the class.


Um, I guess I should ask: If this is not the proper place to be  
sharing code like this, I would be happy to learn the better  
alternative. I'm hoping to occasionally post classes as the app  
develops and get feedback.


Here's my class:

//  Formatter.h

#import Cocoa/Cocoa.h

@interface Formatter : NSObject {
  NSString* word;
  NSString* pronounciation;
  NSString* definition;
  NSPasteboard* thePasteboard;
}

-(id)init;
-(NSString*)pronounciationSurroundedBy:(NSString*)surroundChars;
-(NSString*)pronounciationSurroundedBy:(NSString*)surroundChars  
withDefinitionSeparatedBy:(NSString*)sep;


-(NSString*)asText;
-(NSString*)asCsv;
-(NSString*)asHtml;
-(NSAttributedString*)asAttributedString;

-(void)copyAsText;
-(void)copyAsCsv;
-(void)copyAsHtml;
-(void)copyAsRtf;

@property(readwrite, copy) NSString* word;
@property(readwrite, copy) NSString* pronounciation;
@property(readwrite, copy) NSString* definition;

@end

//  Formatter.m

#import Formatter.h

#define DOUBLE_QUOTE @\

@implementation Formatter

//-
+(NSString*)trimmedString:(NSString*)s
{
  if ( s == NULL ) {
return @;
  } else {
return [ s stringByTrimmingCharactersInSet:[ NSCharacterSet  
whitespaceAndNewlineCharacterSet ] ];

  }
}

//-
+(NSString*)surroundNonEmptyString:(NSString*)text with: 
(NSString*)surroundChars

{
  if ( [ text length ]  0  surroundChars != NULL ) {
int numChars = [ surroundChars length ];
if ( numChars  0 ) {
  unichar l = [ surroundChars characterAtIndex:0 ];
  unichar r = ( numChars == 1 )? l : [ surroundChars  
characterAtIndex:1 ];

  return [ NSString stringWithFormat:@[EMAIL PROTECTED], l, text, r ];
}
  }
  return text;
}

//-
+(NSString*)concatString:(NSString*)a withString:(NSString*)b  
separatedBy:(NSString*)sep

{
  int aLen = ( a != NULL ) ? [ a length ] : 0;
  int bLen = ( b != NULL ) ? [ b length ] : 0;
  if ( aLen  0  bLen  0 ) {
return [ NSString stringWithFormat:@[EMAIL PROTECTED]@%@, a, sep, b ];
  } else if ( aLen  0 ) {
return a;
  } else if ( bLen  0 ) {
return b;
  } else {
return @;
  }
}

//-
-(id)init
{
  // Ensure all our data member variables have legal values
  word = @;
  pronounciation = @;
  definition = @;

  // Prepare the pasteboard (clipboard) to recieve our clippings
  thePasteboard = [ NSPasteboard generalPasteboard ];

  NSArray* clippingTypes = [ NSArray arrayWithObjects:  
NSStringPboardType, NSRTFPboardType, NSHTMLPboardType, nil ];

  [ thePasteboard declareTypes:clippingTypes owner:self ];
  return self;
}

//-

Attack My Code! Part 1 (postscript)

2008-06-12 Thread Charles Jenkins
Oops, I forgot to mention, I'm using garbage collection so that I  
won't have to struggle with retention!


I wish I had edited my previous post a bit more for clarity, but it's  
late and I gotta get to bed.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]