API  1.0.0
CPPasteboard.j
Go to the documentation of this file.
1 /*
2  * CPPasteboard.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 @typedef DataTransfer
26 
27 CPGeneralPboard = @"CPGeneralPboard";
28 CPFontPboard = @"CPFontPboard";
29 CPRulerPboard = @"CPRulerPboard";
30 CPFindPboard = @"CPFindPboard";
31 CPDragPboard = @"CPDragPboard";
32 
33 CPColorPboardType = @"CPColorPboardType";
34 CPFilenamesPboardType = @"CPFilenamesPboardType";
35 CPFontPboardType = @"CPFontPboardType";
36 CPHTMLPboardType = @"CPHTMLPboardType";
37 CPStringPboardType = @"CPStringPboardType";
38 CPURLPboardType = @"CPURLPboardType";
39 CPImagesPboardType = @"CPImagesPboardType";
40 CPVideosPboardType = @"CPVideosPboardType";
41 CPRTFPboardType = @"CPRTFPboardType";
42 _CPSmartPboardType = @"_CPSmartPboardType";
43 
44 UTF8PboardType = @"public.utf8-plain-text";
45 
46 // Deprecated
47 CPImagePboardType = @"CPImagePboardType";
48 
49 
50 var CPPasteboards = nil;
51 
57 @implementation CPPasteboard : CPObject
58 {
59  CPArray _types;
60  CPDictionary _owners;
61  CPDictionary _provided;
62 
63  unsigned _changeCount;
64  CPString _stateUID;
65 }
66 
67 /*
68  @ignore
69 */
70 + (void)initialize
71 {
72  if (self !== [CPPasteboard class])
73  return;
74 
75  [self setVersion:1.0];
76 
77  CPPasteboards = @{};
78 }
79 
83 + (id)generalPasteboard
84 {
85  return [CPPasteboard pasteboardWithName:CPGeneralPboard];
86 }
87 
93 + (id)pasteboardWithName:(CPString)aName
94 {
95  var pasteboard = [CPPasteboards objectForKey:aName];
96 
97  if (pasteboard)
98  return pasteboard;
99 
100  pasteboard = [[CPPasteboard alloc] _initWithName:aName];
101  [CPPasteboards setObject:pasteboard forKey:aName];
102 
103  return pasteboard;
104 }
105 
106 /* @ignore */
107 - (id)_initWithName:(CPString)aName
108 {
109  self = [super init];
110 
111  if (self)
112  {
113 // _name = aName;
114  _types = [];
115 
116  _owners = @{};
117  _provided = @{};
118 
119  _changeCount = 0;
120  }
121 
122  return self;
123 }
124 
131 - (unsigned)addTypes:(CPArray)types owner:(id)anOwner
132 {
133  var i = 0,
134  count = types.length;
135 
136  for (; i < count; ++i)
137  {
138  var type = types[i];
139 
140  if (![_owners objectForKey:type])
141  {
142  [_types addObject:type];
143  [_provided removeObjectForKey:type];
144  }
145 
146  [_owners setObject:anOwner forKey:type];
147  }
148 
149  return ++_changeCount;
150 }
151 
158 - (unsigned)declareTypes:(CPArray)types owner:(id)anOwner
159 {
160  [_types setArray:types];
161 
162  _owners = @{};
163  _provided = @{};
164 
165  if (anOwner)
166  {
167  var count = _types.length;
168  while (count--)
169  [_owners setObject:anOwner forKey:_types[count]];
170  }
171 
172  return ++_changeCount;
173 }
174 
181 - (BOOL)setData:(CPData)aData forType:(CPString)aType
182 {
183  [_provided setObject:aData forKey:aType];
184 
185  if (aType === CPStringPboardType)
186  [self setData:aData forType:UTF8PboardType];
187 
188  return YES;
189 }
190 
197 - (BOOL)setPropertyList:(id)aPropertyList forType:(CPString)aType
198 {
199  return [self setData:[CPPropertyListSerialization dataFromPropertyList:aPropertyList format:CPPropertyList280NorthFormat_v1_0] forType:aType];
200 }
201 
208 - (void)setString:(CPString)aString forType:(CPString)aType
209 {
210  // Putting a non-string on the string pasteboard can lead to strange crashes.
211  if (aString && aString.isa && ![aString isKindOfClass:CPString])
212  [CPException raise:CPInvalidArgumentException reason:"CPPasteboard setString:forType: must be called with a string."];
213 
214  [self setPropertyList:aString forType:aType];
215 }
216 
217 // Determining Types
224 - (CPString)availableTypeFromArray:(CPArray)anArray
225 {
226  return [anArray firstObjectCommonWithArray:[self types]];
227 }
228 
232 - (CPArray)types
233 {
234  return _types;
235 }
236 
237 // Reading data
241 - (unsigned)changeCount
242 {
243  return _changeCount;
244 }
245 
251 - (CPData)dataForType:(CPString)aType
252 {
253  var data = [_provided objectForKey:aType];
254 
255  if (data)
256  return data;
257 
258  var owner = [_owners objectForKey:aType];
259 
260  if (owner)
261  {
262  [owner pasteboard:self provideDataForType:aType];
263  return [_provided objectForKey:aType];
264  }
265 
266  if (aType === CPStringPboardType)
267  return [self dataForType:UTF8PboardType];
268 
269  return nil;
270 }
271 
277 - (id)propertyListForType:(CPString)aType
278 {
279  var data = [self dataForType:aType];
280 
281  if (data)
282  return [CPPropertyListSerialization propertyListFromData:data format:CPPropertyList280NorthFormat_v1_0];
283 
284  return nil;
285 }
286 
292 - (CPString)stringForType:(CPString)aType
293 {
294  return [self propertyListForType:aType];
295 }
296 
297 /* @ignore */
298 - (CPString)_generateStateUID
299 {
300  var bits = 32;
301 
302  _stateUID = @"";
303 
304  while (bits--)
305  _stateUID += FLOOR(RAND() * 16.0).toString(16).toUpperCase();
306 
307  return _stateUID;
308 }
309 
310 /* @ignore */
311 - (CPString)_stateUID
312 {
313  return _stateUID;
314 }
315 
316 @end
317 
318 #if PLATFORM(DOM)
319 
320 var DOMDataTransferPasteboard = nil;
321 
322 @implementation _CPDOMDataTransferPasteboard : CPPasteboard
323 {
324  DataTransfer _dataTransfer;
325 }
326 
327 + (_CPDOMDataTransferPasteboard)DOMDataTransferPasteboard
328 {
329  if (!DOMDataTransferPasteboard)
330  DOMDataTransferPasteboard = [[_CPDOMDataTransferPasteboard alloc] init];
331 
332  return DOMDataTransferPasteboard;
333 }
334 
335 - (void)_setDataTransfer:(DataTransfer)aDataTransfer
336 {
337  _dataTransfer = aDataTransfer;
338 }
339 
340 - (void)_setPasteboard:(CPPasteboard)aPasteboard
341 {
342  _dataTransfer.clearData();
343 
344  var types = [aPasteboard types],
345  count = types.length;
346 
347  while (count--)
348  {
349  var type = types[count];
350 
351  if (type === CPStringPboardType)
352  _dataTransfer.setData(type, [aPasteboard stringForType:type]);
353  else
354  _dataTransfer.setData(type, [[aPasteboard dataForType:type] rawString]);
355  }
356 }
357 
358 - (CPArray)types
359 {
360  return Array.prototype.slice.apply(_dataTransfer.types);
361 }
362 
363 - (CPData)dataForType:(CPString)aType
364 {
365  var dataString = _dataTransfer.getData(aType);
366 
367  if (aType === CPStringPboardType)
368  return [CPData dataFromPropertyList:dataString format:kCFPropertyList280NorthFormat_v1_0];
369 
370  return [CPData dataWithRawString:dataString];
371 }
372 
373 - (id)propertyListForType:(CPString)aType
374 {
375  if (aType === CPStringPboardType)
376  return _dataTransfer.getData(aType);
377 
378  return [CPPropertyListSerialization propertyListFromData:[self dataForType:aType] format:CPPropertyListUnknownFormat];
379 }
380 
381 @end
382 
383 #endif
ThemeState prototype toString
Definition: CPTheme.j:413
CPRulerPboard
Definition: CPPasteboard.j:29
UTF8PboardType
Definition: CPPasteboard.j:44
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPImagePboardType
Definition: CPPasteboard.j:47
CPData dataFromPropertyList:format:(id aPlist, [format] CPPropertyListFormat aFormat)
CPStringPboardType
Definition: CPPasteboard.j:37
CPColorPboardType
Definition: CPPasteboard.j:33
var CPPasteboards
Definition: CPPasteboard.j:50
BOOL setData:forType:(CPData aData, [forType] CPString aType)
Definition: CPPasteboard.j:181
A Cappuccino wrapper for any data type.
Definition: CPData.h:2
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
BOOL setPropertyList:forType:(id aPropertyList, [forType] CPString aType)
Definition: CPPasteboard.j:197
CPRTFPboardType
Definition: CPPasteboard.j:41
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPVideosPboardType
Definition: CPPasteboard.j:40
id propertyListFromData:format:(CPData data, [format] CPPropertyListFormat aFormat)
CPImagesPboardType
Definition: CPPasteboard.j:39
An immutable string (collection of characters).
Definition: CPString.h:2
CPArray types()
Definition: CPPasteboard.j:232
CPDragPboard
Definition: CPPasteboard.j:31
CPData dataWithRawString:(CPString aString)
Definition: CPData.j:45
CPFontPboard
Definition: CPPasteboard.j:28
id propertyListForType:(CPString aType)
Definition: CPPasteboard.j:277
CPURLPboardType
Definition: CPPasteboard.j:38
void setVersion:(int aVersion)
Definition: CPObject.j:510
CPFontPboardType
Definition: CPPasteboard.j:35
CPHTMLPboardType
Definition: CPPasteboard.j:36
DataTransfer CPGeneralPboard
Definition: CPPasteboard.j:27
CPFilenamesPboardType
Definition: CPPasteboard.j:34
CPData dataForType:(CPString aType)
Definition: CPPasteboard.j:251
CPFindPboard
Definition: CPPasteboard.j:30
id pasteboardWithName:(CPString aName)
Definition: CPPasteboard.j:93
id alloc()
Definition: CPObject.j:130