]> err.no Git - sope/blob - sopex/SOPEX/SOPEXConsole.m
SOPE:X 2.x
[sope] / sopex / SOPEX / SOPEXConsole.m
1 /*
2   Copyright (C) 2004 Marcus Mueller <znek@mulle-kybernetik.com>
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "SOPEXConsole.h"
23
24 @class NGLogEvent;
25
26 @interface SOPEXConsole (PrivateAPI)
27 - (NSFont *)stdoutFont;
28 - (NSFont *)stderrFont;
29 - (NSColor *)stdoutFontColor;
30 - (NSColor *)stderrFontColor;
31
32 - (void)appendLogEvent:(NGLogEvent *)_event;
33 @end
34
35 #include "SOPEXToolbarController.h"
36 #include "common.h"
37
38 @implementation SOPEXConsole
39
40 static NGLogEventFormatter *eventFormatter = nil;
41
42 + (void)initialize {
43   static BOOL     didInit = NO;
44   
45   if(didInit) return;
46   didInit = YES;
47   eventFormatter = [[NSClassFromString(@"SOPEXConsoleEventFormatter") alloc] init];
48 }
49
50 - (id)init {
51   self = [super init];
52   if(self) {
53     [NSBundle loadNibNamed:@"SOPEXConsole" owner:self];
54     NSAssert(self->window != nil, @"Problem loading SOPEXConsole.nib!");
55     
56     self->toolbar = [[SOPEXToolbarController alloc] initWithIdentifier:@"SOPEXConsole" target:self];
57     [self->toolbar applyOnWindow:self->window];
58     
59     self->stdoutAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:[self stdoutFont], NSFontAttributeName, [self stdoutFontColor], NSForegroundColorAttributeName, nil];
60     self->stderrAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:[self stderrFont], NSFontAttributeName, [self stderrFontColor], NSForegroundColorAttributeName, nil];
61   }
62   return self;
63 }
64
65 - (void)dealloc {
66   [self->window orderOut:self];
67   [self->stdoutAttributes release];
68   [self->stderrAttributes release];
69   [super dealloc];
70 }
71
72
73 /* console properties */
74
75 - (NSFont *)stdoutFont {
76   return [NSFont fontWithName:@"Courier" size:12];
77 }
78 - (NSFont *)stderrFont {
79   return [NSFont fontWithName:@"Courier" size:12];
80 }
81 - (NSColor *)stdoutFontColor {
82   return [NSColor blackColor];
83 }
84 - (NSColor *)stderrFontColor {
85   return [NSColor redColor];
86 }
87
88
89 /* window handling/delegate */
90
91 - (IBAction)orderFront:(id)sender {
92   [self->window makeKeyAndOrderFront:sender];
93 }
94 - (void)windowWillClose:(NSNotification *)_notif {
95 }
96
97
98 /* actions */
99
100 - (IBAction)clear:(id)sender {
101   NSTextStorage *storage;
102   
103   storage = [self->text textStorage];
104   [storage beginEditing];
105   [storage deleteCharactersInRange:NSMakeRange(0, [storage length])];
106   [storage endEditing];
107 }
108
109 - (BOOL)validateToolbarItem:(NSToolbarItem *)_item {
110   return [self validateMenuItem:(id <NSMenuItem>)_item];
111 }
112
113 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem {
114   SEL action = [menuItem action];
115   
116   if(action == @selector(clear:))
117     return [[self->text textStorage] length] > 0;
118   return YES;
119 }
120
121 - (void)appendLogEvent:(NGLogEvent *)_event {
122   NSTextStorage *storage;
123   NSString      *msg;
124   unsigned      loc;
125
126   storage = [self->text textStorage];
127   msg     = [eventFormatter formattedEvent:_event];
128
129   [storage beginEditing];
130   loc = [storage length];
131   [storage replaceCharactersInRange:NSMakeRange(loc, 0) withString:msg];
132   [storage replaceCharactersInRange:NSMakeRange([storage length], 0)
133            withString:@"\n"];
134   [storage setAttributes:self->stdoutAttributes
135            range:NSMakeRange(loc, [msg length] + 1)];
136   
137   if([storage length] > 50 * 1024)
138     [storage deleteCharactersInRange:NSMakeRange(0, [storage length] - 50 * 1024)];
139   [storage endEditing];
140   
141   // scroll to bottom if verticalScroller is at bottom
142   if([[(NSScrollView*)[[self->text superview] superview] verticalScroller] floatValue] == 1.0)
143     [self->text scrollRangeToVisible:NSMakeRange([storage length], 1)];
144 }
145
146 @end