14 #include <dispatch/dispatch.h>
20 "title" :
"VuoWindowTextInternal",
44 NSRect frame = NSMakeRect(0, 0, 400, 600);
45 NSUInteger styleMask = NSTitledWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
46 NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
48 if (
self = [super initWithContentRect:contentRect
50 backing:NSBackingStoreBuffered
59 self.releasedWhenClosed = NO;
62 [
self setTitle:[NSString stringWithUTF8String:title]];
65 NSFont *_textFont = [NSFont fontWithName:@"Monaco" size:0];
66 self.textFont = _textFont;
68 NSScrollView *_scrollView = [[NSScrollView alloc] initWithFrame:[[
self contentView] frame]];
69 self.scrollView = _scrollView;
70 [_scrollView release];
71 [scrollView setHasVerticalScroller:YES];
72 [scrollView setAutohidesScrollers:NO];
73 [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
74 [
self setContentView:scrollView];
76 NSTextView *_textView = [[NSTextView alloc] initWithFrame:[[scrollView contentView] frame]];
77 self.textView = _textView;
79 [textView setFont:textFont];
80 [textView setEditable:YES];
81 [textView setAllowsUndo:NO];
82 [textView setAutoresizingMask:NSViewWidthSizable];
83 [textView setAutomaticDashSubstitutionEnabled:NO];
84 [textView setAutomaticDataDetectionEnabled:NO];
85 [textView setAutomaticLinkDetectionEnabled:NO];
86 [textView setAutomaticQuoteSubstitutionEnabled:NO];
87 [textView setAutomaticSpellingCorrectionEnabled:NO];
88 [textView setAutomaticTextReplacementEnabled:NO];
89 [scrollView setDocumentView:textView];
92 NSMenu *textViewContextMenu = [textView menu];
93 int cutItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Cut"];
94 if (cutItemIndex >= 0)
95 [textViewContextMenu removeItemAtIndex: cutItemIndex];
98 int pasteItemIndex = [textViewContextMenu indexOfItemWithTitle: @"Paste"];
99 if (pasteItemIndex >= 0)
100 [textViewContextMenu removeItemAtIndex: pasteItemIndex];
114 - (BOOL)
textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
117 NSUInteger markedTextLength = [aTextView markedRange].length;
120 if (affectedCharRange.location + markedTextLength != [[
textView string] length])
127 if ([aTextView hasMarkedText] && markedTextLength == 0)
130 for (NSUInteger i = 0; i < [replacementString length]; ++i)
132 unichar charTyped = [replacementString characterAtIndex:i];
135 VuoText charTypedAsText =
VuoText_make([[NSString stringWithCharacters:&charTyped length:1] UTF8String]);
139 if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:charTyped])
142 NSRange previousWhitespaceOrNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
143 options:NSBackwardsSearch];
144 NSUInteger previousWordStart = (previousWhitespaceOrNewline.location != NSNotFound ?
145 previousWhitespaceOrNewline.location + 1 :
147 if (previousWordStart < [[
textView string] length])
149 NSString *wordTyped = [[textView string] substringFromIndex:previousWordStart];
156 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:charTyped])
159 NSRange previousNewline = [[textView string] rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]
160 options:NSBackwardsSearch];
161 NSUInteger previousLineStart = (previousNewline.location != NSNotFound ?
162 previousNewline.location + 1 :
164 if (previousLineStart < [[
textView string] length])
166 NSString *lineTyped = [[textView string] substringFromIndex:previousLineStart];
179 - (void)becomeMainWindow
181 [
super becomeMainWindow];
183 NSMenu *editMenu = [[[NSMenu alloc] initWithTitle:@"Edit"] autorelease];
186 self.editCopyMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyText) keyEquivalent:@"c"] autorelease];
187 [
self.editCopyMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
188 [editMenu addItem:self.editCopyMenuItem];
191 self.editSelectAllMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Select All" action:@selector(selectAllText) keyEquivalent:@"a"] autorelease];
192 [
self.editSelectAllMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
193 [editMenu addItem:self.editSelectAllMenuItem];
195 NSMenuItem *editMenuItem = [[NSMenuItem new] autorelease];
196 [editMenuItem setSubmenu:editMenu];
198 NSMutableArray *windowMenuItems = [NSMutableArray arrayWithCapacity:2];
199 [windowMenuItems addObject:editMenuItem];
200 oldMenu = [(NSMenu *)VuoApp_setMenuItems(windowMenuItems) retain];
206 - (void)resignMainWindow
208 [
super resignMainWindow];
218 - (void)windowWillClose:(NSNotification *)notification
220 [
super resignMainWindow];
230 - (void)enableTriggersWithTypedLine:(
void (*)(
VuoText))_typedLine
231 typedWord:(
void (*)(
VuoText))_typedWord
232 typedCharacter:(
void (*)(
VuoText))_typedCharacter
254 - (void)appendLine:(const
char *)text
257 BOOL autoscroll = abs(NSMaxY(
textView.visibleRect) - NSMaxY(
textView.bounds)) < 1;
259 NSString *line = [[NSString stringWithUTF8String:text] stringByAppendingString:@"\n"];
260 NSDictionary *attributes = @{
262 NSForegroundColorAttributeName: NSColor.textColor,
264 NSAttributedString *attributedLine = [[NSAttributedString alloc] initWithString:line attributes:attributes];
265 [[textView textStorage] appendAttributedString:attributedLine];
266 [attributedLine release];
268 NSTextContainer *textContainer = [textView textContainer];
269 NSLayoutManager *layoutManager = [textView layoutManager];
270 [layoutManager ensureLayoutForTextContainer:textContainer];
271 NSRect frameToFitText = [layoutManager usedRectForTextContainer:textContainer];
272 [textView setFrame:frameToFitText];
275 [textView scrollRangeToVisible:NSMakeRange([[textView string] length], 0)];
283 bool hasSelectedText = ([textView selectedRange].length > 0);
285 [
self.textView copy:self];
291 - (void)selectAllText
293 [
self.textView selectAll:self];
300 - (BOOL)validateUserInterfaceItem:(
id <NSValidatedUserInterfaceItem>)anItem
302 SEL theAction = [anItem action];
303 if (theAction ==
@selector(copyText)) {
305 bool hasSelectedText = ([textView selectedRange].length > 0);
306 return hasSelectedText;
309 return [
super validateUserInterfaceItem:anItem];