14 #include <dispatch/dispatch.h>
20 "title" :
"VuoWindowTextInternal",
45 NSRect frame = NSMakeRect(0, 0, 400, 600);
46 NSUInteger styleMask = NSTitledWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
47 NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
49 if (
self = [super initWithContentRect:contentRect
51 backing:NSBackingStoreBuffered
60 self.releasedWhenClosed = NO;
63 [
self setTitle:[NSString stringWithUTF8String:title]];
66 NSFont *_textFont = [NSFont fontWithName:@"Monaco" size:0];
67 self.textFont = _textFont;
69 NSScrollView *_scrollView = [[NSScrollView alloc] initWithFrame:[[
self contentView] frame]];
70 self.scrollView = _scrollView;
71 [_scrollView release];
72 [scrollView setHasVerticalScroller:YES];
73 [scrollView setAutohidesScrollers:NO];
74 [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
75 [
self setContentView:scrollView];
77 NSTextView *_textView = [[NSTextView alloc] initWithFrame:[[scrollView contentView] frame]];
78 self.textView = _textView;
80 [textView setFont:textFont];
81 [textView setEditable:YES];
82 [textView setAllowsUndo:NO];
83 [textView setAutoresizingMask:NSViewWidthSizable];
84 [textView setAutomaticDashSubstitutionEnabled:NO];
85 [textView setAutomaticDataDetectionEnabled:NO];
86 [textView setAutomaticLinkDetectionEnabled:NO];
87 [textView setAutomaticQuoteSubstitutionEnabled:NO];
88 [textView setAutomaticSpellingCorrectionEnabled:NO];
89 [textView setAutomaticTextReplacementEnabled:NO];
90 [scrollView setDocumentView:textView];
93 NSMenu *textViewContextMenu = [textView menu];
94 int cutItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Cut"];
95 if (cutItemIndex >= 0)
96 [textViewContextMenu removeItemAtIndex: cutItemIndex];
99 int pasteItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Paste"];
100 if (pasteItemIndex >= 0)
101 [textViewContextMenu removeItemAtIndex: pasteItemIndex];
115 - (BOOL)
textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
118 NSUInteger markedTextLength = [aTextView markedRange].length;
121 if (affectedCharRange.location + markedTextLength != [[
textView string] length])
128 if ([aTextView hasMarkedText] && markedTextLength == 0)
131 for (NSUInteger i = 0; i < [replacementString length]; ++i)
133 unichar charTyped = [replacementString characterAtIndex:i];
136 VuoText charTypedAsText =
VuoText_make([[NSString stringWithCharacters:&charTyped length:1] UTF8String]);
140 if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:charTyped])
143 NSRange previousWhitespaceOrNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
144 options:NSBackwardsSearch];
145 NSUInteger previousWordStart = (previousWhitespaceOrNewline.location != NSNotFound ?
146 previousWhitespaceOrNewline.location + 1 :
148 if (previousWordStart < [[
textView string] length])
150 NSString *wordTyped = [[textView string] substringFromIndex:previousWordStart];
157 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:charTyped])
160 NSRange previousNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]
161 options:NSBackwardsSearch];
162 NSUInteger previousLineStart = (previousNewline.location != NSNotFound ?
163 previousNewline.location + 1 :
165 if (previousLineStart < [[
textView string] length])
167 NSString *lineTyped = [[textView string] substringFromIndex:previousLineStart];
180 - (void)becomeMainWindow
182 [
super becomeMainWindow];
184 NSMenu *editMenu = [[[NSMenu alloc] initWithTitle:@"Edit"] autorelease];
187 self.editCopyMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyText) keyEquivalent:@"c"] autorelease];
188 [
self.editCopyMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
189 [editMenu addItem:self.editCopyMenuItem];
192 self.editSelectAllMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Select All" action:@selector(selectAllText) keyEquivalent:@"a"] autorelease];
193 [
self.editSelectAllMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
194 [editMenu addItem:self.editSelectAllMenuItem];
197 self.editClearMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Clear" action:@selector(clear) keyEquivalent:@"k"] autorelease];
198 [
self.editClearMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
199 [editMenu addItem:self.editClearMenuItem];
201 NSMenuItem *editMenuItem = [[NSMenuItem new] autorelease];
202 [editMenuItem setSubmenu:editMenu];
204 NSMutableArray *windowMenuItems = [NSMutableArray arrayWithCapacity:2];
205 [windowMenuItems addObject:editMenuItem];
206 oldMenu = [(NSMenu *)VuoApp_setMenuItems(windowMenuItems) retain];
212 - (void)resignMainWindow
214 [
super resignMainWindow];
224 - (void)windowWillClose:(NSNotification *)notification
226 [
super resignMainWindow];
236 - (void)enableTriggersWithTypedLine:(
void (*)(
VuoText))_typedLine
237 typedWord:(
void (*)(
VuoText))_typedWord
238 typedCharacter:(
void (*)(
VuoText))_typedCharacter
260 - (void)appendLine:(const
char *)text
263 BOOL autoscroll = abs(NSMaxY(
textView.visibleRect) - NSMaxY(
textView.bounds)) < 1;
265 NSString *line = [[NSString stringWithUTF8String:text] stringByAppendingString:@"\n"];
268 VUserLog(
"Warning: Attempting to write non-UTF-8 text to a console window. Ignoring.");
272 NSDictionary *attributes = @{
274 NSForegroundColorAttributeName: NSColor.textColor,
276 NSAttributedString *attributedLine = [[NSAttributedString alloc] initWithString:line attributes:attributes];
277 [[textView textStorage] appendAttributedString:attributedLine];
278 [attributedLine release];
280 NSTextContainer *textContainer = [textView textContainer];
281 NSLayoutManager *layoutManager = [textView layoutManager];
282 [layoutManager ensureLayoutForTextContainer:textContainer];
283 NSRect frameToFitText = [layoutManager usedRectForTextContainer:textContainer];
284 [textView setFrame:frameToFitText];
287 [textView scrollRangeToVisible:NSMakeRange([[textView string] length], 0)];
295 [textView.textStorage deleteCharactersInRange:NSMakeRange(0, textView.textStorage.length)];
303 bool hasSelectedText = ([textView selectedRange].length > 0);
305 [
self.textView copy:self];
311 - (void)selectAllText
313 [
self.textView selectAll:self];
320 - (BOOL)validateUserInterfaceItem:(
id <NSValidatedUserInterfaceItem>)anItem
322 SEL theAction = [anItem action];
323 if (theAction ==
@selector(copyText)) {
325 bool hasSelectedText = ([textView selectedRange].length > 0);
326 return hasSelectedText;
329 return [
super validateUserInterfaceItem:anItem];