ConsoleView.m 3.1 KB

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