00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "LLStringList.h"
00010
00011
00012 @implementation LLStringList
00013
00014 - (void) awakeFromNib {
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
00040
00041
00042 [normalAttribute setValue:col forKey:NSForegroundColorAttributeName];
00043 NSDictionary *attr = normalAttribute;
00044
00045
00046
00047
00048
00049
00050
00051
00052 NSAttributedString *aStr = [[NSAttributedString alloc] initWithString:str
00053 attributes: [NSDictionary dictionaryWithDictionary:attr] ];
00054
00055
00056 [stringList addObject:aStr];
00057 if ([stringList count] > [self maxNrOfStrings]) {
00058
00059 NSAttributedString *tempStr = [stringList objectAtIndex:0];
00060 [tempStr release];
00061 [stringList removeObjectAtIndex:0];
00062
00063
00064 if (selectedRow >= 0) {
00065 selectedRow--;
00066
00067 }
00068 }
00069
00070
00071 hasChanged = YES;
00072
00073 }
00074
00075
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
00094
00095
00096
00097 [[NSColor whiteColor] set];
00098 NSFrameRect(aRect);
00099
00100
00101 NSPoint p = [self bounds].origin;
00102 p.x += 1;
00103
00104
00105 for (int i = 0; i < [stringList count]; i++) {
00106 NSAttributedString *aStr = [stringList objectAtIndex:i];
00107
00108
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
00121 [aStr drawAtPoint:p];
00122
00123
00124
00125 p.y += rowHeigth;
00126 }
00127 hasChanged = NO;
00128 }
00129
00130 - (bool) hasChanged {
00131 return hasChanged;
00132 }
00133
00134 - (NSPoint) mousePos {
00135
00136 NSPoint mouseBase = [[self window] mouseLocationOutsideOfEventStream];
00137
00138
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
00161 NSString *selectedString = [[stringList objectAtIndex:selectedRow] string];
00162
00163 [self newStringSelected:selectedString];
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;
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;
00199 }
00200
00201 if (selectedRow == id) {
00202 selectedRow = -1;
00203 return;
00204 }
00205
00206 if (selectedRow > id) {
00207 selectedRow--;
00208 }
00209
00210 }
00211
00212 - (void) emptyAllRows {
00213 [stringList removeAllObjects];
00214 hasChanged = YES;
00215 }
00216
00217 - (void) newStringSelected:(NSString*)str {
00218 [notificationCenter postNotificationName:@"LL_STRING_LIST_SELECTION" object:self userInfo:str];
00219 }
00220
00221 @end