// // BPIconGroupLabel.m // Attempt2Recover // // Created by Bea on 28/06/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPIconGroupLabel.h" #import "BPIconGroup.h" @implementation BPIconGroupLabel // text to substitute (and represent) a blank group label static NSString *emptyGroupLabel; + (void)initialize { emptyGroupLabel = @"(Click to change label)"; } - (id)initWithFrame:(NSRect)frameRect { if ((self = [super initWithFrame:frameRect]) != nil) { //[self setBordered:YES]; [self setBezeled:NO]; //[self setBezelStyle:NSTextFieldRoundedBezel]; [self setDrawsBackground:NO]; [self setBackgroundColor:[NSColor colorWithCalibratedRed:0.9 green:0.9 blue:0.9 alpha:0.9]]; [[self currentEditor] alignCenter:nil]; editing = NO; trackingRectNum = 0; normalFont = [[NSFont labelFontOfSize:12] retain]; italicFont = [[[NSFontManager sharedFontManager] convertFont:[NSFont fontWithName:@"Verdana" size:12] toHaveTrait:NSItalicFontMask] retain]; [self useNormalFont]; } return self; } - (void)setupForGroup:(BPIconGroup *)group { if (![group isDeletable]) { [self setEditable:NO]; //NSFont *newFont = [[[NSFontManager sharedFontManager] convertFont:[NSFont fontWithName:@"Verdana" size:11] // toHaveTrait:NSBoldFontMask] retain]; NSFont *newFont = [[NSFont boldSystemFontOfSize:12] retain]; [normalFont release]; normalFont = newFont; [self useNormalFont]; // refresh display [self textChanged]; } } - (void)useNormalFont { [self setFont:normalFont]; } - (void)useItalicFont { [self setFont:italicFont]; } /* - (void)resetBackgroundColor { if ([[self text] isEqual:@""]) [self setBackgroundColor:[NSColor lightGrayColor]]; } */ - (void)textChanged { [self setStringValue:[self stringValue]]; [self sizeToFit]; } - (void)fixIfEmptyLabel { NSString *label = [self stringValue]; NSString *trimmedLabel = [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if ([trimmedLabel isEqual:@""]) { [self setText:emptyGroupLabel]; [self setTextColor:[NSColor grayColor]]; } else { [self setTextColor:[NSColor blackColor]]; } } - (void)setText:(NSString *)labelText { [self setStringValue:labelText]; [self fixIfEmptyLabel]; [self textChanged]; } - (NSString *)text { if ([[self stringValue] isEqual:emptyGroupLabel]) return @""; else return [self stringValue]; } - (BOOL)isBlank { return ([[self stringValue] isEqual:emptyGroupLabel]); } // hides this field if it's empty, and the label isn't currently being edited - (void)hideIfEmpty { if (!editing && [self isBlank]) [self setHidden:YES]; } - (void)editingCleanUp { [[self window] endEditingFor:nil]; [[self window] makeFirstResponder:nil]; } - (void)finishEditing:(NSNotification *)aNotification { if (!editing) return; // already finished editing? [self fixIfEmptyLabel]; /* // set minimum size NSRect frame = [self frame]; if (frame.size.width < 60) [self setFrameSize:NSMakeSize(60, frame.size.height)]; */ editing = NO; if (aNotification == nil) { NSDictionary *info = [NSDictionary dictionaryWithObject:[self currentEditor] forKey:@"NSFieldEditor"]; aNotification = [NSNotification notificationWithName:NSControlTextDidEndEditingNotification object:self userInfo:info]; } // notify delegate that the label changed // (super-notif posting stuffs up, says this class has no "string" selector // and just posting to default center doesn't get to the delegate) [[self delegate] controlTextDidEndEditing:aNotification]; //[[NSNotificationCenter defaultCenter] postNotification:aNotification]; // set this textfield to "lose focus" [self editingCleanUp]; } // ------ - (BOOL)mouseInside { NSPoint mouseLoc = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil]; return [self mouse:mouseLoc inRect:[self bounds]]; } // don't allow this textfield to be "focused" unless the mouse is inside it - (BOOL)acceptsFirstResponder { if (acceptResponderStatus || [self mouseInside]) { editing = YES; acceptResponderStatus = NO; return YES; } return NO; } - (void)mouseDown:(NSEvent *)event { editing = YES; [[self currentEditor] selectAll:nil]; //[self selectText:nil]; // if use this, label hides when mouseExit even if editing } // ------------- // ------------- // highlight text field when cursor moves into the text field. /* - (void)resetCursorRects { // reset tracking rect for bounds if (trackingRectNum > 0) { [self removeTrackingRect:trackingRectNum]; trackingRectNum = 0; } trackingRectNum = [self addTrackingRect:[self bounds] owner:self userData:nil assumeInside:[self mouseInside]]; } - (void)mouseEntered:(NSEvent *)theEvent { if ([self isEditable]) { [self setDrawsBackground:YES]; [self setNeedsDisplay:YES]; [[NSCursor IBeamCursor] set]; } } - (void)mouseExited:(NSEvent *)theEvent { if ([self isEditable]) { [self setDrawsBackground:NO]; [self setNeedsDisplay:YES]; [[NSCursor arrowCursor] set]; } } */ - (void)dealloc { NSLog(@"destroying %@", self); // make sure this textfield isn't the main responder, etc. if (editing) [self editingCleanUp]; [self removeTrackingRect:trackingRectNum]; [normalFont release]; [italicFont release]; [super dealloc]; } #pragma mark - #pragma mark override NSTextField action methods - (void)textDidBeginEditing:(NSNotification *)aNotification { [self useNormalFont]; [self setTextColor:[NSColor blackColor]]; // call super implementation to notify delegate and post related notification [super textDidBeginEditing:aNotification]; } // called when the text is being edited by the user - (void)textDidChange:(NSNotification *)aNotification { [self textChanged]; // call super implementation to notify delegate and post related notification [super textDidChange:aNotification]; } - (void)textDidEndEditing:(NSNotification *)aNotification { [self finishEditing:aNotification]; } /* #pragma mark - #pragma mark override NSTextField action methods - (void)textDidBeginEditing:(NSNotification *)aNotification { //[[self window] makeFirstResponder:self]; editing = YES; [self setTextColor:[NSColor blackColor]]; // call super implementation to notify delegate and post related notification [super textDidBeginEditing:aNotification]; } // called when the text is being edited by the user - (void)textDidChange:(NSNotification *)aNotification { [self textChanged]; // call super implementation to notify delegate and post related notification [super textDidChange:aNotification]; } - (void)textDidEndEditing:(NSNotification *)aNotification { [self fixIfEmptyLabel]; // call super implementation to notify delegate and post related notification [super textDidEndEditing:aNotification]; [self endEditing]; } */ @end