tagging release
[dasher.git] / trunk / Src / MacOSX / DasherEdit.mm
blob0dcc9dcabdb85b532f7f582cd081f8913fe4b785
1 //
2 //  DasherEdit.mm
3 //  Dasher
4 //
5 //  Created by Doug Dickinson on Fri May 30 2003.
6 //  Copyright (c) 2003 Doug Dickinson (dasher@DressTheMonkey.plus.com). All rights reserved.
7 //
9 #import "DasherEdit.h"
10 #import "DasherApp.h"
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
67 + dasherEdit
69   if (dasherEdit == nil)
70     {
71     dasherEdit = [[self alloc] init];  // retain for use as singleton
73     }
75   return dasherEdit;
78 - (void)textViewDidChangeSelection:(NSNotification *)aNotification
80   // only start and redraw if it was the user who manipulated the text
81   if (dasherIsModifyingText == NO) {
82     dasher_start();
83     dasher_redraw();
84   }
87 - init
89   if (self = [super init]) {
90     registerCallbacks();
91     XXXdasherEdit = self;
92     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeSelection:) name:NSTextViewDidChangeSelectionNotification object:nil];
93     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeSelection:) name:NSWindowDidBecomeMainNotification object:nil];
94   }
96   return self;
99 - (void)awakeFromNib {
101   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
103   dasherIsModifyingText = YES;
105   
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:@""])
128     {
129     [[self currentTextUI] insertText:aString];
130     flushCount += [aString length];
131     }
133   dasherIsModifyingText = NO;
136 - (void)unflushCallback
138   dasherIsModifyingText = YES;
140   if (flushCount > 0) {
141     NSRange r = [[self currentTextUI] selectedRange];
142     if (r.location <= flushCount) {
143       r.location = 0;
144     } else {
145       r.location -= flushCount;
146     }
148     r.length += flushCount;
150     [[self currentTextUI] replaceCharactersInRange:r withString:@""];
151   }
153   flushCount = 0;
155   dasherIsModifyingText = NO;
158 - (void)deleteCallback
160   NSRange r = [[self currentTextUI] selectedRange];
161   if (r.location <= 0) {
162     return;
163   }
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) {
180     location = 0;
181     length = r.location;
182   } else {
183     location = r.location - maxChars;
184     length = maxChars;
185   }
187   r = NSMakeRange(location, length);
189   flushCount = 0;
191   result = r.length <= 0 ? @"" : [s substringWithRange:r];
192   return result;
195 - clipboardCallbackWithAction:(clipboard_action)act
197   NSRange r;
198   
199   dasherIsModifyingText = YES;
201   switch( act )
202     {
203     case CLIPBOARD_CUT:
204       [[self currentTextUI] cut:self];
205       break;
206     case CLIPBOARD_COPY:
207       [[self currentTextUI] copy:self];
208       break;
209     case CLIPBOARD_PASTE:
210       [[self currentTextUI] paste:self];
211       break;
212     case CLIPBOARD_COPYALL:
213       r = [[self currentTextUI] selectedRange];
214       [[self currentTextUI] selectAll:self];
215       [[self currentTextUI] copy:self];
216       [[self currentTextUI] setSelectedRange:r];
217       break;
218     case CLIPBOARD_SELECTALL:
219       [[self currentTextUI] selectAll:self];
220       break;
221     case CLIPBOARD_CLEAR:
222       [[self currentTextUI] setString:nil];
223       break;
224     }
226   dasherIsModifyingText = NO;
230 @end