14#include <dispatch/dispatch.h>
18 "title" :
"VuoWindowTextInternal",
43 NSRect frame = NSMakeRect(0, 0, 400, 600);
44 NSUInteger styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
45 NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
47 if (
self = [super initWithContentRect:contentRect
49 backing:NSBackingStoreBuffered
58 self.releasedWhenClosed = NO;
61 [
self setTitle:[NSString stringWithUTF8String:title]];
64 NSFont *_textFont = [NSFont fontWithName:@"Monaco" size:0];
65 self.textFont = _textFont;
67 NSScrollView *_scrollView = [[NSScrollView alloc] initWithFrame:[[
self contentView] frame]];
68 self.scrollView = _scrollView;
69 [_scrollView release];
70 [scrollView setHasVerticalScroller:YES];
71 [scrollView setAutohidesScrollers:NO];
72 [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
73 [
self setContentView:scrollView];
75 NSTextView *_textView = [[NSTextView alloc] initWithFrame:[[scrollView contentView] frame]];
76 self.textView = _textView;
78 [textView setFont:textFont];
79 [textView setEditable:YES];
80 [textView setAllowsUndo:NO];
81 [textView setAutoresizingMask:NSViewWidthSizable];
82 [textView setAutomaticDashSubstitutionEnabled:NO];
83 [textView setAutomaticDataDetectionEnabled:NO];
84 [textView setAutomaticLinkDetectionEnabled:NO];
85 [textView setAutomaticQuoteSubstitutionEnabled:NO];
86 [textView setAutomaticSpellingCorrectionEnabled:NO];
87 [textView setAutomaticTextReplacementEnabled:NO];
88 [scrollView setDocumentView:textView];
91 NSMenu *textViewContextMenu = [textView menu];
92 int cutItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Cut"];
93 if (cutItemIndex >= 0)
94 [textViewContextMenu removeItemAtIndex: cutItemIndex];
97 int pasteItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Paste"];
98 if (pasteItemIndex >= 0)
99 [textViewContextMenu removeItemAtIndex: pasteItemIndex];
113- (
BOOL)
textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
116 NSUInteger markedTextLength = [aTextView markedRange].length;
119 if (affectedCharRange.location + markedTextLength != [[
textView string] length])
126 if ([aTextView hasMarkedText] && markedTextLength == 0)
129 for (NSUInteger i = 0; i < [replacementString length]; ++i)
131 unichar charTyped = [replacementString characterAtIndex:i];
134 VuoText charTypedAsText =
VuoText_make([[NSString stringWithCharacters:&charTyped length:1] UTF8String]);
138 if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:charTyped])
141 NSRange previousWhitespaceOrNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
142 options:NSBackwardsSearch];
143 NSUInteger previousWordStart = (previousWhitespaceOrNewline.location != NSNotFound ?
144 previousWhitespaceOrNewline.location + 1 :
146 if (previousWordStart < [[
textView string] length])
148 NSString *wordTyped = [[textView string] substringFromIndex:previousWordStart];
155 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:charTyped])
158 NSRange previousNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]
159 options:NSBackwardsSearch];
160 NSUInteger previousLineStart = (previousNewline.location != NSNotFound ?
161 previousNewline.location + 1 :
163 if (previousLineStart < [[
textView string] length])
165 NSString *lineTyped = [[textView string] substringFromIndex:previousLineStart];
178- (void)becomeMainWindow
180 [
super becomeMainWindow];
182 NSMenu *editMenu = [[[NSMenu alloc] initWithTitle:@"Edit"] autorelease];
185 self.editCopyMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyText) keyEquivalent:@"c"] autorelease];
186 [
self.editCopyMenuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
187 [editMenu addItem:self.editCopyMenuItem];
190 self.editSelectAllMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Select All" action:@selector(selectAllText) keyEquivalent:@"a"] autorelease];
191 [
self.editSelectAllMenuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
192 [editMenu addItem:self.editSelectAllMenuItem];
195 self.editClearMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Clear" action:@selector(clear) keyEquivalent:@"k"] autorelease];
196 [
self.editClearMenuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
197 [editMenu addItem:self.editClearMenuItem];
199 NSMenuItem *editMenuItem = [[NSMenuItem new] autorelease];
200 [editMenuItem setSubmenu:editMenu];
202 NSMutableArray *windowMenuItems = [NSMutableArray arrayWithCapacity:2];
203 [windowMenuItems addObject:editMenuItem];
204 oldMenu = [(NSMenu *)VuoApp_setMenuItems(windowMenuItems) retain];
210- (void)resignMainWindow
212 [
super resignMainWindow];
222- (void)windowWillClose:(NSNotification *)notification
224 [
super resignMainWindow];
234- (void)enableTriggersWithTypedLine:(
void (*)(
VuoText))_typedLine
235 typedWord:(
void (*)(
VuoText))_typedWord
236 typedCharacter:(
void (*)(
VuoText))_typedCharacter
258- (void)appendLine:(const
char *)text
263 NSString *line = [[NSString stringWithUTF8String:text] stringByAppendingString:@"\n"];
266 VUserLog(
"Warning: Attempting to write non-UTF-8 text to a console window. Ignoring.");
270 NSDictionary *attributes = @{
272 NSForegroundColorAttributeName: NSColor.textColor,
274 NSAttributedString *attributedLine = [[NSAttributedString alloc] initWithString:line attributes:attributes];
275 [[textView textStorage] appendAttributedString:attributedLine];
276 [attributedLine release];
278 NSTextContainer *textContainer = [textView textContainer];
279 NSLayoutManager *layoutManager = [textView layoutManager];
280 [layoutManager ensureLayoutForTextContainer:textContainer];
281 NSRect frameToFitText = [layoutManager usedRectForTextContainer:textContainer];
282 [textView setFrame:frameToFitText];
285 [textView scrollRangeToVisible:NSMakeRange([[textView string] length], 0)];