Luky/Gui/LLStringList.m

00001 //
00002 //  LLStringList.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 01/08/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "LLStringList.h"
00010 
00011 
00012 @implementation LLStringList
00013 
00014 - (void) awakeFromNib { // init is not called
00015     notificationCenter = [LLNotificationCenter defaultCenter];
00016     NSFont *font = [NSFont fontWithName: @"Helvetica" size: 9.0];
00017     normalAttribute =[[NSMutableDictionary dictionaryWithObjectsAndKeys:
00018         [NSColor orangeColor], NSForegroundColorAttributeName,
00019         font , NSFontAttributeName, 
00020         nil] retain];
00021     stringList = [[NSMutableArray alloc] init];
00022     rowHeigth  = [@"teststring" sizeWithAttributes:normalAttribute].height;
00023     boxColor = [NSColor brownColor];
00024     selectedRow = -1;
00025     hasChanged = NO;
00026     [self setIdentifer:[NSString stringWithString:@"[LLStringList]"]];
00027 }
00028 
00029 - (int) maxNrOfStrings {
00030     return [self bounds].size.height / rowHeigth;
00031 }
00032 
00033 - (void) addString:(NSString *)str {
00034     [self addString:str withColor:[NSColor whiteColor]];
00035 }
00036 
00037 - (void) addString:(NSString *)str withColor:(NSColor *)col {
00038     
00039    // NSLog(@"LLStringList.addStringWithColor: [%@]  %@", str, name);
00040     
00041     // set up the attributes
00042     [normalAttribute setValue:col forKey:NSForegroundColorAttributeName];
00043     NSDictionary *attr = normalAttribute;
00044     
00045     /*
00046     NSDictionary *attr =[[NSDictionary dictionaryWithObjectsAndKeys:
00047         col, NSForegroundColorAttributeName,
00048         font , NSFontAttributeName, 
00049         nil] retain];
00050    */
00051     
00052     NSAttributedString *aStr = [[NSAttributedString alloc] initWithString:str
00053          attributes: [NSDictionary dictionaryWithDictionary:attr] ];
00054     
00055     // store
00056     [stringList addObject:aStr]; // add last
00057     if ([stringList count] > [self maxNrOfStrings]) {
00058         // remove first
00059         NSAttributedString *tempStr = [stringList objectAtIndex:0];
00060         [tempStr release];
00061         [stringList removeObjectAtIndex:0]; 
00062         
00063         // the selected row scrolled up
00064         if (selectedRow >= 0) {
00065             selectedRow--;
00066             //NSLog(@"LLStringList.addString selected row is now %d %@", selectedRow, name);
00067         }
00068     }
00069     
00070     // we changed
00071     hasChanged = YES;
00072     //[self setNeedsDisplay:YES];
00073 }
00074 
00075 // my brain works the other way around
00076 - (bool) isFlipped {
00077     return YES;
00078 }
00079 
00080 - (void) setIdentifer:(NSString *)newName {
00081     [name release];
00082     name = newName;
00083     [name retain];
00084 }
00085 
00086 - (NSString *)name {
00087     return name;
00088 }
00089 
00090 - (void)drawRect:(NSRect)aRect {
00091     
00092     
00093     //aRect = [self bounds]; // always completely redraw
00094     //NSLog(@"LLStringList.drawRect x=%f, y=%f, w=%f, h=%f", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
00095     
00096     // draw frame
00097     [[NSColor whiteColor] set];
00098     NSFrameRect(aRect);
00099     
00100     // start point
00101     NSPoint p = [self bounds].origin; 
00102     p.x += 1;
00103     
00104     // draw all the strings   
00105     for (int i = 0; i < [stringList count]; i++) {
00106         NSAttributedString *aStr = [stringList objectAtIndex:i];
00107         
00108         // draw selection box
00109         if (i == selectedRow) {
00110             NSRect box;
00111             box.size.height = rowHeigth;
00112             box.size.width = [self bounds].size.width - 2;
00113             box.origin = p;
00114             [boxColor set];
00115             NSFrameRect(box);
00116             [[boxColor colorWithAlphaComponent:0.3] set];
00117             NSRectFill(box);
00118         }
00119         
00120         // draw string
00121         [aStr drawAtPoint:p];
00122         //NSLog(@"LLStringList.draw string %@ at [%f, %f]", aStr, p.x, p.y);
00123         
00124         // specify the lower left corner
00125         p.y += rowHeigth;
00126     }
00127     hasChanged = NO;
00128 }
00129 
00130 - (bool) hasChanged {
00131     return hasChanged;
00132 }
00133 
00134 - (NSPoint) mousePos {
00135     // get mouse point in window
00136     NSPoint mouseBase = [[self window] mouseLocationOutsideOfEventStream];
00137     
00138     // convert to view coordinates
00139     NSPoint mouseLocation = [self convertPoint:mouseBase fromView:nil];
00140     
00141     return mouseLocation;
00142 }
00143 
00144 - (int) rowHeigth {
00145     return rowHeigth;
00146 }
00147 
00148 - (NSString*) selectedString {
00149    return [[stringList objectAtIndex:selectedRow] string]; 
00150 }
00151 
00152 - (void) setSelectedRow:(int)row {
00153     
00154     if (row > [stringList count]) {
00155         return;
00156     }
00157     
00158     selectedRow = row;
00159     
00160    // NSLog(@"LLStringList.setSelectedRow selected row is now %d %@", selectedRow, name);
00161     NSString *selectedString = [[stringList objectAtIndex:selectedRow] string];
00162     
00163     [self newStringSelected:selectedString]; // for derived classes
00164     
00165     hasChanged = YES;  
00166 }
00167 
00168 - (void) mouseDown:(NSEvent *)theEvent {
00169     
00170     NSPoint mousePosition = [self mousePos];
00171     
00172     int row = mousePosition.y / rowHeigth;
00173     
00174     [self setSelectedRow:row];
00175 }
00176 
00177 - (void) disableSelection {
00178     selectedRow = -1;
00179     hasChanged = YES;
00180 }
00181 
00182 - (void) removeString:(NSString *)str {
00183     int id = -1;
00184     for (int i = 0; i < [stringList count]; i++) {
00185         NSAttributedString *aStr = [stringList objectAtIndex:i];
00186         if ([[aStr string] isEqualToString:str]) {
00187             id = i; // cannot change while traversing
00188         }
00189     }
00190     if (id == -1) {
00191         NSLog(@"LLStringList.removeString error string [%@] not found %@", str, name);
00192         return;
00193     }
00194     [stringList removeObjectAtIndex:id];
00195     hasChanged = YES;
00196     
00197     if (selectedRow < 0) {
00198         return; // done
00199     }
00200     
00201     if (selectedRow == id) {
00202         selectedRow = -1; // deleted seleted row
00203         return;
00204     } 
00205     
00206     if (selectedRow > id) {
00207         selectedRow--; // shifted up
00208     }
00209 
00210 }
00211 
00212 - (void) emptyAllRows {
00213     [stringList removeAllObjects];
00214     hasChanged = YES;
00215 }
00216 
00217 - (void) newStringSelected:(NSString*)str { // to be overwritten
00218     [notificationCenter postNotificationName:@"LL_STRING_LIST_SELECTION" object:self userInfo:str];
00219 }
00220 
00221 @end

Generated on Sat Aug 26 21:14:16 2006 for MacTrek by  doxygen 1.4.7