5 // Created by Doug Dickinson on Fri May 30 2003.
6 // Copyright (c) 2003 Doug Dickinson (dasher@DressTheMonkey.plus.com). All rights reserved.
11 #import "DasherUtil.h"
12 #import "PreferencesController.h"
13 #include "libdasher.h"
14 #import "TextDocument.h"
16 // TODO this is a no-no; see similar in DasherApp
17 DasherEdit *XXXdasherEdit;
20 void edit_output_callback(symbol Symbol)
22 NSString *s = NSStringFromStdString(dasher_get_edit_text(Symbol));
23 [XXXdasherEdit outputCallback:s];
26 void edit_flush_callback(symbol Symbol)
28 NSString *s = NSStringFromStdString(dasher_get_edit_text(Symbol));
29 [XXXdasherEdit flushCallback:s];
32 void edit_unflush_callback()
34 [XXXdasherEdit unflushCallback];
37 void edit_delete_callback()
39 [XXXdasherEdit deleteCallback];
42 void get_new_context_callback( std::string &str, int max )
44 NSString *s = [XXXdasherEdit getNewContextCallback:max];
45 str = StdStringFromNSString(s);
48 void clipboard_callback( clipboard_action act )
50 [XXXdasherEdit clipboardCallbackWithAction:(clipboard_action)act];
53 static void registerCallbacks()
55 dasher_set_edit_output_callback( edit_output_callback );
56 dasher_set_edit_delete_callback(edit_delete_callback);
57 dasher_set_get_new_context_callback( get_new_context_callback );
59 dasher_set_clipboard_callback( clipboard_callback );
62 static DasherEdit *dasherEdit = nil;
65 @implementation DasherEdit
69 if (dasherEdit == nil)
71 dasherEdit = [[self alloc] init]; // retain for use as singleton
78 - (void)textViewDidChangeSelection:(NSNotification *)aNotification
80 // only start and redraw if it was the user who manipulated the text
81 if (dasherIsModifyingText == NO) {
89 if (self = [super init]) {
92 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeSelection:) name:NSTextViewDidChangeSelectionNotification object:nil];
93 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeSelection:) name:NSWindowDidBecomeMainNotification object:nil];
99 - (void)awakeFromNib {
101 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
103 dasherIsModifyingText = YES;
106 // TODO this should actually happen on creating a new document
108 dasherIsModifyingText = NO;
111 - (NSTextView *)currentTextUI
113 return [[[NSDocumentController sharedDocumentController] currentDocument] textUI];
116 - (void)outputCallback:(NSString *)aString
118 dasherIsModifyingText = YES;
119 [[self currentTextUI] insertText:aString];
120 dasherIsModifyingText = NO;
123 - (void)flushCallback:(NSString *)aString
125 dasherIsModifyingText = YES;
127 if (aString != nil && ![aString isEqualToString:@""])
129 [[self currentTextUI] insertText:aString];
130 flushCount += [aString length];
133 dasherIsModifyingText = NO;
136 - (void)unflushCallback
138 dasherIsModifyingText = YES;
140 if (flushCount > 0) {
141 NSRange r = [[self currentTextUI] selectedRange];
142 if (r.location <= flushCount) {
145 r.location -= flushCount;
148 r.length += flushCount;
150 [[self currentTextUI] replaceCharactersInRange:r withString:@""];
155 dasherIsModifyingText = NO;
158 - (void)deleteCallback
160 NSRange r = [[self currentTextUI] selectedRange];
161 if (r.location <= 0) {
165 dasherIsModifyingText = YES;
166 [[self currentTextUI] replaceCharactersInRange:NSMakeRange(r.location - 1, 1) withString:@""];
167 dasherIsModifyingText = NO;
171 - (NSString *)getNewContextCallback:(int)maxChars
173 NSString *s = [[self currentTextUI] string];
174 NSRange r = [[self currentTextUI] selectedRange];
175 unsigned int location = 0;
176 unsigned int length = maxChars;
177 NSString *result = nil;
179 if (r.location < maxChars) {
183 location = r.location - maxChars;
187 r = NSMakeRange(location, length);
191 result = r.length <= 0 ? @"" : [s substringWithRange:r];
195 - clipboardCallbackWithAction:(clipboard_action)act
199 dasherIsModifyingText = YES;
204 [[self currentTextUI] cut:self];
207 [[self currentTextUI] copy:self];
209 case CLIPBOARD_PASTE:
210 [[self currentTextUI] paste:self];
212 case CLIPBOARD_COPYALL:
213 r = [[self currentTextUI] selectedRange];
214 [[self currentTextUI] selectAll:self];
215 [[self currentTextUI] copy:self];
216 [[self currentTextUI] setSelectedRange:r];
218 case CLIPBOARD_SELECTALL:
219 [[self currentTextUI] selectAll:self];
221 case CLIPBOARD_CLEAR:
222 [[self currentTextUI] setString:nil];
226 dasherIsModifyingText = NO;