Luky/Gui/LLStringTable.m

00001 //
00002 //  LLStringTable.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 01/08/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "LLStringTable.h"
00010 
00011 // this is quite a hack to fit LLStringLists in a view
00012 // Views are not ment as helper objects
00013 
00014 @implementation LLStringTable
00015 
00016 - (void) awakeFromNib { // init is not called
00017     notificationCenter = [LLNotificationCenter defaultCenter];
00018     columns = [[NSMutableArray alloc] init];
00019     // need at least 1
00020     LLStringList *list = [[LLStringList alloc] init];
00021     [list setFrame:[self frame]];
00022     [list setBounds:[self bounds]];
00023     [list awakeFromNib];
00024     [columns addObject:list];
00025     
00026     hasChanged = NO;
00027 }
00028 
00029 - (bool) hasChanged {
00030     return hasChanged;
00031 }
00032 
00033 - (void) disableSelection {
00034     hasChanged = YES;
00035 }
00036 
00037 // my brain works the other way around
00038 - (bool) isFlipped {
00039     return YES;
00040 }
00041 
00042 - (void)drawRect:(NSRect)aRect {
00043     
00044     aRect = [self bounds]; // always completely redraw
00045     //NSLog(@"LLStringTable.drawRect x=%f, y=%f, w=%f, h=%f", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
00046     
00047     int nrOfColumns = [columns count];
00048     int columnWidth = [self bounds].size.width / [columns count];
00049     
00050     aRect.size.width = columnWidth;
00051     
00052     for (int i = 0; i < nrOfColumns; i++) {
00053         LLStringList *list = [columns objectAtIndex:i];
00054         [list setBounds:aRect]; // we have abused LLStringList so tell the bounds if we resized
00055         [list drawRect:aRect];
00056         aRect.origin.x += columnWidth;
00057     }    
00058     hasChanged = NO;
00059 }
00060 
00061 - (NSPoint) mousePos {
00062     // get mouse point in window
00063     NSPoint mouseBase = [[self window] mouseLocationOutsideOfEventStream];
00064     
00065     // convert to view coordinates
00066     NSPoint mouseLocation = [self convertPoint:mouseBase fromView:nil];
00067     
00068     return mouseLocation;
00069 }
00070 
00071 - (void) mouseDown:(NSEvent *)theEvent {
00072     
00073     NSPoint mousePosition = [self mousePos];
00074     
00075     int columnWidth = [self bounds].size.width / [columns count];
00076     int selectedColumn = mousePosition.x / columnWidth;
00077  //   NSLog(@"LLStringTable.mouseDown selected column is now %d", selectedColumn);
00078     
00079     int nrOfColumns = [columns count];
00080     for (int i = 0; i < nrOfColumns; i++) {
00081         LLStringList *list = [columns objectAtIndex:i];
00082         if (i == selectedColumn) {
00083             int row = mousePosition.y / [list rowHeigth];            
00084             [list setSelectedRow:row];
00085             [self newStringSelected:[list selectedString]];
00086         } else {
00087             [list disableSelection];
00088         }
00089 
00090     }      
00091        
00092     hasChanged = YES;
00093 }
00094 
00095 - (void) newStringSelected:(NSString*)str { // to be overwritten
00096     [notificationCenter postNotificationName:@"LL_STRING_TABLE_SELECTION" object:self userInfo:str];
00097 }
00098 
00099 - (void) emptyAllColumns {
00100     int nrOfColumns = [columns count];
00101     for (int i = 0; i < nrOfColumns; i++) {
00102         LLStringList *list = [columns objectAtIndex:i];
00103         [list emptyAllRows];
00104     }
00105 }
00106 
00107 - (int)  maxNrOfRows {
00108     LLStringList *col0 = [columns objectAtIndex:0]; // always there
00109     return [col0 maxNrOfStrings];
00110 }
00111 
00112 - (void) setNrOfColumns:(int)newNrOfColumns {
00113     if (newNrOfColumns < 1) {
00114         return;
00115     } 
00116     
00117     // set up the bounds of the columns
00118     NSRect listBounds = [self bounds];
00119     listBounds.size.width /= newNrOfColumns;
00120     
00121     // delete
00122     for (int i = newNrOfColumns; i < [columns count]; i++) {
00123         [columns removeLastObject];
00124 //        NSLog(@"LLStringTable.setNrOfColumns removing column");
00125     }
00126     
00127     // add
00128     for (int j = [columns count]; j < newNrOfColumns; j++) {
00129         LLStringList *list = [[LLStringList alloc] init];
00130         [list setIdentifer:[NSString stringWithFormat:@"[column %d]", j]];
00131         [columns addObject:list];
00132  //       NSLog(@"LLStringTable.setNrOfColumns adding column %d", j);
00133     }   
00134     
00135     // set bounds of ALL
00136     for (int k = 0; k < [columns count]; k++) {
00137         LLStringList *list = [columns objectAtIndex:k];
00138         NSLog(@"LLStringTable.setNrOfColumns (%d) x=%f, y=%f, w=%f, h=%f", k, listBounds.origin.x, listBounds.origin.y, listBounds.size.width, listBounds.size.height);
00139         [list setFrame:listBounds];
00140         [list setBounds:listBounds];
00141         [list awakeFromNib];
00142         listBounds.origin.x += listBounds.size.width;
00143     }
00144     hasChanged = YES;
00145 }
00146 
00147 - (void) removeString:(NSString *)str fromColumn:(int)column {
00148     if (column >= [columns count]) {
00149         NSLog(@"LLStringTable.removeString column %d does not exist", column);
00150         return; // column does not exist
00151     }
00152     LLStringList *list = [columns objectAtIndex:column];
00153     [list removeString:str];
00154     hasChanged = YES;    
00155 }
00156 
00157 - (void) addString:(NSString *)str toColumn:(int)column {
00158     if (column >= [columns count]) {
00159         NSLog(@"LLStringTable.addString column %d does not exist", column);
00160         return; // column does not exist
00161     }
00162 //    NSLog(@"LLStringTable.addString [%@] to column %d", str, column);
00163     LLStringList *list = [columns objectAtIndex:column];
00164     [list addString:str];
00165     hasChanged = YES;
00166 }
00167 
00168 - (void) addString:(NSString *)str withColor:(NSColor *)col toColumn:(int)column {
00169     if (column >= [columns count]) {
00170         NSLog(@"LLStringTable.addStringWithColor column %d does not exist", column);
00171         return; // column does not exist
00172     }
00173     LLStringList *list = [columns objectAtIndex:column];
00174  //   NSLog(@"LLStringTable.addStringWithColor [%@] to column %d", str, column);
00175     [list addString:str withColor:col];
00176     hasChanged = YES;
00177 }
00178 
00179 @end

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