home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2007 September / maximum-cd-2007-09.iso / Assets / data / AssaultCube_v0.93.exe / source / xcode / ConsoleView.m < prev    next >
Encoding:
Text File  |  2007-06-04  |  3.2 KB  |  121 lines

  1. #import "ConsoleView.h"
  2.  
  3. #define MAX_LINES 200
  4. #define LINE_WIDTH 500
  5. #define LINE_HEIGHT 12
  6.  
  7. /*
  8.  * This VScroller sends a scrollToEnd message to the console based on whether you hit page/line down or not
  9.  */
  10. @interface VScroller : NSScroller {
  11. }
  12. @end
  13. @implementation VScroller
  14. - (NSScrollerPart)hitPart {
  15.     ConsoleView *view =  (ConsoleView *)[(NSScrollView*)[self superview] documentView];
  16.     NSScrollerPart part = [super hitPart];
  17.     [view scrollToEnd:((part == NSScrollerIncrementPage) || (part == NSScrollerIncrementLine))];
  18.     return part;
  19. }
  20. @end
  21.  
  22.  
  23. @implementation ConsoleView
  24.  
  25. - (id)initWithFrame:(NSRect)frame {
  26.     self = [super initWithFrame:frame];
  27.     if (self) {
  28.         array = [[[NSMutableArray alloc] init] retain];
  29.         
  30.         attr = [[NSDictionary dictionaryWithObjectsAndKeys:
  31.             [NSFont userFontOfSize:(LINE_HEIGHT-2)], NSFontAttributeName, 
  32.             [NSColor colorWithCalibratedRed:0.2 green:0.8 blue:0.2 alpha:1.0], NSForegroundColorAttributeName, 
  33.             nil] retain];
  34.     }
  35.     return self;
  36. }
  37.  
  38. - (void)awakeFromNib {
  39.     [self scrollToEnd:YES];
  40.     
  41.     NSScroller *vscroll = [[VScroller alloc] init];
  42.     [vscroll setControlSize:[[[self enclosingScrollView] verticalScroller] controlSize]];
  43.     [[self enclosingScrollView] setVerticalScroller:vscroll];
  44. }
  45.  
  46. - (void)dealloc {
  47.     [attr release];
  48.     [array release];
  49.     [super dealloc];
  50. }
  51.  
  52. - (BOOL)isFlipped { 
  53.     return YES; 
  54. }
  55.  
  56. - (void)drawRect:(NSRect)rect {    
  57.     //draw the visible lines only
  58.     int startLine = rect.origin.y/LINE_HEIGHT;
  59.     int endLine = 1 + (rect.origin.y+rect.size.height)/LINE_HEIGHT;
  60.     if(startLine < 0) startLine = 0;
  61.     if(endLine > [array count]) endLine = [array count];
  62.     int i;    
  63.     for(i = startLine; i < endLine; i++) {
  64.         NSString *str = [array objectAtIndex:i];
  65.         [str drawAtPoint:NSMakePoint(2, i * LINE_HEIGHT) withAttributes:attr];
  66.     }
  67. }
  68.  
  69. - (void)scrollToEnd:(BOOL)enable {
  70.     endScroll = enable;
  71. }
  72.  
  73. - (void)appendLine:(NSString*)line {
  74.     BOOL chop = [array count] > MAX_LINES;
  75.     if(chop) {
  76.         [array removeObjectAtIndex:0]; // limit the number of lines
  77.     }
  78.     [array addObject:line];    
  79.     int i = [array count];
  80.     [self setFrame:NSMakeRect(0, 0, LINE_WIDTH, i*LINE_HEIGHT)]; // increase the frame size    
  81.         
  82.     NSRect rect = NSMakeRect(0, (i-1)*LINE_HEIGHT, LINE_WIDTH, LINE_HEIGHT);
  83.     if(endScroll) {
  84.         // Scroll to the line just added
  85.         if([self scrollRectToVisible:rect]) return;
  86.     } else {
  87.         // Lock the scrolling on the first visible line
  88.         i = [self visibleRect].origin.y/LINE_HEIGHT;
  89.         if(!chop) i++;
  90.         if(i < 0) i = 0; 
  91.         if(i > [array count]) i = [array count];
  92.         NSRect vrect = NSMakeRect(0, (i-1)*LINE_HEIGHT, LINE_WIDTH, LINE_HEIGHT);
  93.         if([self scrollRectToVisible:vrect]) return;
  94.     }
  95.     if(chop)
  96.         [self setNeedsDisplay:YES];
  97.     else
  98.         [self setNeedsDisplayInRect:rect];
  99. }
  100.  
  101. - (void)appendText:(NSString*)text {
  102.     NSArray *lines = [text componentsSeparatedByString:@"\n"]; //@TODO assumes we get given lines rather than fragments...
  103.     int i;
  104.     for(i = 0; i < [lines count]; i++) {
  105.         NSString *line = [lines objectAtIndex:i];
  106.         if([line length] == 0) continue; //skip empty
  107.         [self appendLine:line];
  108.     }
  109. }
  110.  
  111. - (BOOL)acceptsFirstResponder { 
  112.     return YES; 
  113. }
  114.  
  115. - (IBAction)delete:(id)sender {
  116.     [array removeAllObjects];
  117.     [self setFrame:NSMakeRect(0,0,0,0)];
  118. }
  119.  
  120. @end
  121.