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