API  0.9.10
CPText.j
Go to the documentation of this file.
1 /*
2  * CPText.j
3  * AppKit
4  *
5  * Created by Alexander Ljungberg.
6  * Copyright 2010, WireLoad, LLC.
7  *
8  * additions from
9  *
10  * Daniel Boehringer on 8/02/2014.
11  * Copyright Daniel Boehringer on 8/02/2014.
12  *
13  *
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 
31 
32 @global CPStringPboardType
33 
34 @protocol CPTextDelegate <CPObject>
35 
36 - (BOOL)textShouldBeginEditing:(CPText)aTextObject;
37 - (BOOL)textShouldEndEditing:(CPText)aTextObject;
38 - (void)textDidBeginEditing:(CPNotification)aNotification;
39 - (void)textDidChange:(CPNotification)aNotification;
40 - (void)textDidEndEditing:(CPNotification)aNotification;
41 
42 @end
43 
46 CPEnterCharacter = "\u0003";
48 CPTabCharacter = "\u0009";
49 CPNewlineCharacter = "\u000a";
52 CPBackTabCharacter = "\u0019";
53 CPDeleteCharacter = "\u007f";
54 
55 @typedef CPTextMovement
66 
67 @typedef CPWritingDirection
71 
72 @typedef CPTextAlignment
78 
79 /*
80  CPText notifications
81 */
82 CPTextDidBeginEditingNotification = @"CPTextDidBeginEditingNotification";
83 CPTextDidChangeNotification = @"CPTextDidChangeNotification";
84 CPTextDidEndEditingNotification = @"CPTextDidEndEditingNotification";
85 
86 /*
87  CPTextView Notifications
88 */
89 CPTextViewDidChangeSelectionNotification = @"CPTextViewDidChangeSelectionNotification";
90 CPTextViewDidChangeTypingAttributesNotification = @"CPTextViewDidChangeTypingAttributesNotification";
91 
92 /*
93  FIXME: move these to CPAttributed string
94  Make use of attributed keys in AppKit
95 */
96 CPFontAttributeName = @"CPFontAttributeName";
97 CPForegroundColorAttributeName = @"CPForegroundColorAttributeName";
98 CPBackgroundColorAttributeName = @"CPBackgroundColorAttributeName";
99 CPShadowAttributeName = @"CPShadowAttributeName";
100 CPUnderlineStyleAttributeName = @"CPUnderlineStyleAttributeName";
101 CPSuperscriptAttributeName = @"CPSuperscriptAttributeName";
102 CPBaselineOffsetAttributeName = @"CPBaselineOffsetAttributeName";
103 CPAttachmentAttributeName = @"CPAttachmentAttributeName";
104 CPLigatureAttributeName = @"CPLigatureAttributeName";
105 CPKernAttributeName = @"CPKernAttributeName";
106 
107 @implementation CPText : CPView
108 {
109  BOOL _isEditable;
110  BOOL _isSelectable;
111  BOOL _isRichText;
112 }
113 
114 - (void)setSelectable:(BOOL)flag
115 {
116  [self willChangeValueForKey:@"selectable"];
117  _isSelectable = flag;
118  [self didChangeValueForKey:@"selectable"];
119 
120  if (!flag)
121  [self setEditable:flag];
122 }
123 
124 - (void)setEditable:(BOOL)flag
125 {
126  [self willChangeValueForKey:@"editable"];
127  _isEditable = flag;
128  [self didChangeValueForKey:@"editable"];
129 
130  if (flag)
131  [self setSelectable:flag];
132 }
133 
134 - (void)changeFont:(id)sender
135 {
136  _CPRaiseInvalidAbstractInvocation(self, _cmd);
137 }
138 
139 - (void)copy:(id)sender
140 {
141  var selectedRange = [self selectedRange];
142 
143  if (selectedRange.length < 1)
144  return;
145 
146  var pasteboard = [CPPasteboard generalPasteboard];
147 
148  // put plain representation on the pasteboad unconditionally
149  [pasteboard declareTypes:[CPStringPboardType] owner:nil];
150  [pasteboard setString:[[self stringValue] substringWithRange:selectedRange] forType:CPStringPboardType];
151 }
152 
153 - (id)_plainStringForPasting
154 {
155  return [[CPPasteboard generalPasteboard] stringForType:CPStringPboardType];
156 }
157 
158 - (id)_stringForPasting
159 {
160  var pasteboard = [CPPasteboard generalPasteboard],
161  dataForPasting = [pasteboard stringForType:CPRTFPboardType],
162  stringForPasting = [pasteboard stringForType:CPStringPboardType];
163 
164  if (dataForPasting || [stringForPasting hasPrefix:"{\\rtf1\\ansi"])
165  stringForPasting = [[_CPRTFParser new] parseRTF:dataForPasting ? dataForPasting : stringForPasting];
166 
167  if (![self isRichText] && [stringForPasting isKindOfClass:[CPAttributedString class]])
168  stringForPasting = stringForPasting._string;
169 
170  return stringForPasting;
171 }
172 
173 - (void)paste:(id)sender
174 {
175  var stringForPasting = [self _stringForPasting];
176 
177  if (stringForPasting)
178  [self insertText:stringForPasting];
179 }
180 
181 - (void)copyFont:(id)sender
182 {
183  _CPRaiseInvalidAbstractInvocation(self, _cmd);
184 }
185 
186 - (void)delete:(id)sender
187 {
188  _CPRaiseInvalidAbstractInvocation(self, _cmd);
189 }
190 
191 - (CPFont)font:(CPFont)aFont
192 {
193  _CPRaiseInvalidAbstractInvocation(self, _cmd);
194 
195  return nil;
196 }
197 
199 {
200  _CPRaiseInvalidAbstractInvocation(self, _cmd);
201 
202  return NO;
203 }
204 
206 {
207  _CPRaiseInvalidAbstractInvocation(self, _cmd);
208 
209  return NO;
210 }
211 
213 {
214  _CPRaiseInvalidAbstractInvocation(self, _cmd);
215 
216  return NO;
217 }
218 
219 - (CGSize)maxSize
220 {
221  _CPRaiseInvalidAbstractInvocation(self, _cmd);
222 
223  return CGSizeMake(0,0);
224 }
225 
226 - (CGSize)minSize
227 {
228  _CPRaiseInvalidAbstractInvocation(self, _cmd);
229  return CGSizeMake(0,0);
230 }
231 
232 - (void)pasteFont:(id)sender
233 {
234  _CPRaiseInvalidAbstractInvocation(self, _cmd);
235 }
236 
237 - (void)replaceCharactersInRange:(CPRange)aRange withString:(CPString)aString
238 {
239  _CPRaiseInvalidAbstractInvocation(self, _cmd);
240 }
241 
242 - (void)scrollRangeToVisible:(CPRange)aRange
243 {
244  _CPRaiseInvalidAbstractInvocation(self, _cmd);
245 }
246 
247 - (void)selectedAll:(id)sender
248 {
249  _CPRaiseInvalidAbstractInvocation(self, _cmd);
250 }
251 
252 - (CPRange)selectedRange
253 {
254  _CPRaiseInvalidAbstractInvocation(self, _cmd);
255 
256  return CPMakeRange(CPNotFound, 0);
257 }
258 
259 - (void)setFont:(CPFont)aFont
260 {
261  _CPRaiseInvalidAbstractInvocation(self, _cmd);
262 }
263 
264 - (void)setFont:(CPFont)aFont range:(CPRange)aRange
265 {
266  _CPRaiseInvalidAbstractInvocation(self, _cmd);
267 }
268 
269 - (void)setHorizontallyResizable:(BOOL)flag
270 {
271  _CPRaiseInvalidAbstractInvocation(self, _cmd);
272 }
273 
274 - (void)setMaxSize:(CGSize)aSize
275 {
276  _CPRaiseInvalidAbstractInvocation(self, _cmd);
277 }
278 
279 - (void)setMinSize:(CGSize)aSize
280 {
281  _CPRaiseInvalidAbstractInvocation(self, _cmd);
282 }
283 
284 - (void)setString:(CPString)aString
285 {
286  [self replaceCharactersInRange:CPMakeRange(0, [[self string] length]) withString:aString];
287 }
288 
289 - (void)setUsesFontPanel:(BOOL)flag
290 {
291  _CPRaiseInvalidAbstractInvocation(self, _cmd);
292 }
293 
294 - (void)setVerticallyResizable:(BOOL)flag
295 {
296  _CPRaiseInvalidAbstractInvocation(self, _cmd);
297 }
298 
300 {
301  _CPRaiseInvalidAbstractInvocation(self, _cmd);
302 
303  return nil;
304 }
305 
306 - (void)underline:(id)sender
307 {
308  _CPRaiseInvalidAbstractInvocation(self, _cmd);
309 }
310 
312 {
313  _CPRaiseInvalidAbstractInvocation(self, _cmd);
314 
315  return NO;
316 }
317 
318 @end
319 
320 var CPTextViewIsEditableKey = @"CPTextViewIsEditableKey",
321  CPTextViewIsSelectableKey = @"CPTextViewIsSelectableKey",
322  CPTextViewIsRichTextKey = @"CPTextViewIsRichTextKey";
323 
324 @implementation CPText (CPCoding)
325 
326 - (id)initWithCoder:(CPCoder)aCoder
327 {
328  self = [super initWithCoder:aCoder];
329 
330  if (self)
331  {
332  [self setSelectable:[aCoder decodeBoolForKey:CPTextViewIsSelectableKey]];
333  [self setEditable:[aCoder decodeBoolForKey:CPTextViewIsEditableKey]];
334  [self setRichText:[aCoder decodeBoolForKey:CPTextViewIsRichTextKey]];
335  }
336 
337  return self;
338 }
339 
340 - (void)encodeWithCoder:(CPCoder)aCoder
341 {
342  [super encodeWithCoder:aCoder];
343  [aCoder encodeBool:_isEditable forKey:CPTextViewIsEditableKey];
344  [aCoder encodeBool:_isSelectable forKey:CPTextViewIsSelectableKey];
345  [aCoder encodeBool:_isRichText forKey:CPTextViewIsRichTextKey];
346 }
347 
348 @end
349 
351 
355 - (BOOL)isEditable
356 {
357  return _isEditable;
358 }
359 
363 - (void)setEditable:(BOOL)aValue
364 {
365  _isEditable = aValue;
366 }
367 
372 {
373  return _isSelectable;
374 }
375 
379 - (void)setSelectable:(BOOL)aValue
380 {
381  _isSelectable = aValue;
382 }
383 
387 - (BOOL)isRichText
388 {
389  return _isRichText;
390 }
391 
395 - (void)setRichText:(BOOL)aValue
396 {
397  _isRichText = aValue;
398 }
399 
400 @end
Definition: CPFont.h:2
CPUpTextMovement
Definition: CPText.j:63
CPTextViewDidChangeSelectionNotification
Definition: CPText.j:89
BOOL usesFontPanel()
Definition: CPText.j:311
id initWithCoder:(CPCoder aCoder)
Definition: CPView.j:3662
CPStringPboardType
Definition: CPPasteboard.j:37
CPLigatureAttributeName
Definition: CPText.j:104
CPEnterCharacter
Definition: CPText.j:46
var CPTextViewIsSelectableKey
Definition: CPText.j:321
CPWritingDirectionRightToLeft
Definition: CPText.j:70
CPRightTextAlignment
Definition: CPText.j:74
CPString stringForType:(CPString aType)
Definition: CPPasteboard.j:291
id generalPasteboard()
Definition: CPPasteboard.j:82
CPTabTextMovement
Definition: CPText.j:59
CPReturnTextMovement
Definition: CPText.j:58
CPLeftTextMovement
Definition: CPText.j:61
CPForegroundColorAttributeName
Definition: CPText.j:97
A mutable character string with attributes.
CPAttachmentAttributeName
Definition: CPText.j:103
CPDownTextMovement
Definition: CPText.j:64
CPTabCharacter
Definition: CPText.j:48
CPNaturalTextAlignment
Definition: CPText.j:77
CPLineSeparatorCharacter
Definition: CPText.j:45
An immutable string (collection of characters).
Definition: CPString.h:2
CGSize maxSize()
Definition: CPText.j:219
BOOL isRulerVisible()
Definition: CPText.j:205
CGSize minSize()
Definition: CPText.j:226
BOOL isVerticallyResizable()
Definition: CPText.j:212
CPBackgroundColorAttributeName
Definition: CPText.j:98
void encodeWithCoder:(CPCoder aCoder)
Definition: CPView.j:3770
CPTextMovement CPIllegalTextMovement
Definition: CPText.j:56
CPBackTabCharacter
Definition: CPText.j:52
int length()
Definition: CPString.j:186
CPTextDidEndEditingNotification
Definition: CPText.j:84
A notification that can be posted to a CPNotificationCenter.
Definition: CPNotification.h:2
CPTextViewDidChangeTypingAttributesNotification
Definition: CPText.j:90
void setSelectable:(BOOL flag)
Definition: CPText.j:114
CPTextDidChangeNotification
Definition: CPText.j:83
CPRightTextMovement
Definition: CPText.j:62
CPShadowAttributeName
Definition: CPText.j:99
CPUnderlineStyleAttributeName
Definition: CPText.j:100
CPJustifiedTextAlignment
Definition: CPText.j:76
CPWritingDirectionLeftToRight
Definition: CPText.j:69
CPOtherTextMovement
Definition: CPText.j:57
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
CPFontAttributeName
Definition: CPText.j:96
CPFormFeedCharacter
Definition: CPText.j:50
CPParagraphSeparatorCharacter
Definition: CPText.j:44
CPNotFound
Definition: CPObjJRuntime.j:62
CPSuperscriptAttributeName
Definition: CPText.j:101
CPNewlineCharacter
Definition: CPText.j:49
CPRange selectedRange()
Definition: CPText.j:252
CPKernAttributeName
Definition: CPText.j:105
CPTextAlignment CPLeftTextAlignment
Definition: CPText.j:73
CPCarriageReturnCharacter
Definition: CPText.j:51
CPCancelTextMovement
Definition: CPText.j:65
CPString string()
Definition: CPText.j:299
CPCenterTextAlignment
Definition: CPText.j:75
CPBaselineOffsetAttributeName
Definition: CPText.j:102
CPTextDidBeginEditingNotification
Definition: CPText.j:82
var CPTextViewIsEditableKey
Definition: CPText.j:320
CPDeleteCharacter
Definition: CPText.j:53
CPBackspaceCharacter
Definition: CPText.j:47
void setEditable:(BOOL flag)
Definition: CPText.j:124
CPWritingDirection CPWritingDirectionNatural
Definition: CPText.j:68
CPRange function CPMakeRange(location, length)
Definition: CPRange.j:37
void replaceCharactersInRange:withString:(CPRange aRange, [withString] CPString aString)
Definition: CPText.j:237
void setRichText:(BOOL aValue)
Definition: CPText.j:395
CPBacktabTextMovement
Definition: CPText.j:60
var CPTextViewIsRichTextKey
Definition: CPText.j:322
Definition: CPView.j:136
BOOL isHorizontallyResizable()
Definition: CPText.j:198
Definition: CPText.h:2