API  0.9.10
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 
43 UTF8PboardType = @"public.utf8-plain-text";
44 
45 // Deprecated
46 CPImagePboardType = @"CPImagePboardType";
47 
48 
49 var CPPasteboards = nil;
50 
56 @implementation CPPasteboard : CPObject
57 {
58  CPArray _types;
59  CPDictionary _owners;
60  CPDictionary _provided;
61 
62  unsigned _changeCount;
63  CPString _stateUID;
64 }
65 
66 /*
67  @ignore
68 */
69 + (void)initialize
70 {
71  if (self !== [CPPasteboard class])
72  return;
73 
74  [self setVersion:1.0];
75 
76  CPPasteboards = @{};
77 }
78 
83 {
84  return [CPPasteboard pasteboardWithName:CPGeneralPboard];
85 }
86 
92 + (id)pasteboardWithName:(CPString)aName
93 {
94  var pasteboard = [CPPasteboards objectForKey:aName];
95 
96  if (pasteboard)
97  return pasteboard;
98 
99  pasteboard = [[CPPasteboard alloc] _initWithName:aName];
100  [CPPasteboards setObject:pasteboard forKey:aName];
101 
102  return pasteboard;
103 }
104 
105 /* @ignore */
106 - (id)_initWithName:(CPString)aName
107 {
108  self = [super init];
109 
110  if (self)
111  {
112 // _name = aName;
113  _types = [];
114 
115  _owners = @{};
116  _provided = @{};
117 
118  _changeCount = 0;
119  }
120 
121  return self;
122 }
123 
130 - (unsigned)addTypes:(CPArray)types owner:(id)anOwner
131 {
132  var i = 0,
133  count = types.length;
134 
135  for (; i < count; ++i)
136  {
137  var type = types[i];
138 
139  if (![_owners objectForKey:type])
140  {
141  [_types addObject:type];
142  [_provided removeObjectForKey:type];
143  }
144 
145  [_owners setObject:anOwner forKey:type];
146  }
147 
148  return ++_changeCount;
149 }
150 
157 - (unsigned)declareTypes:(CPArray)types owner:(id)anOwner
158 {
159  [_types setArray:types];
160 
161  _owners = @{};
162  _provided = @{};
163 
164  if (anOwner)
165  {
166  var count = _types.length;
167  while (count--)
168  [_owners setObject:anOwner forKey:_types[count]];
169  }
170 
171  return ++_changeCount;
172 }
173 
180 - (BOOL)setData:(CPData)aData forType:(CPString)aType
181 {
182  [_provided setObject:aData forKey:aType];
183 
184  if (aType === CPStringPboardType)
185  [self setData:aData forType:UTF8PboardType];
186 
187  return YES;
188 }
189 
196 - (BOOL)setPropertyList:(id)aPropertyList forType:(CPString)aType
197 {
198  return [self setData:[CPPropertyListSerialization dataFromPropertyList:aPropertyList format:CPPropertyList280NorthFormat_v1_0] forType:aType];
199 }
200 
207 - (void)setString:(CPString)aString forType:(CPString)aType
208 {
209  // Putting a non-string on the string pasteboard can lead to strange crashes.
210  if (aString && aString.isa && ![aString isKindOfClass:CPString])
211  [CPException raise:CPInvalidArgumentException reason:"CPPasteboard setString:forType: must be called with a string."];
212 
213  [self setPropertyList:aString forType:aType];
214 }
215 
216 // Determining Types
223 - (CPString)availableTypeFromArray:(CPArray)anArray
224 {
225  return [anArray firstObjectCommonWithArray:[self types]];
226 }
227 
231 - (CPArray)types
232 {
233  return _types;
234 }
235 
236 // Reading data
240 - (unsigned)changeCount
241 {
242  return _changeCount;
243 }
244 
250 - (CPData)dataForType:(CPString)aType
251 {
252  var data = [_provided objectForKey:aType];
253 
254  if (data)
255  return data;
256 
257  var owner = [_owners objectForKey:aType];
258 
259  if (owner)
260  {
261  [owner pasteboard:self provideDataForType:aType];
262  return [_provided objectForKey:aType];
263  }
264 
265  if (aType === CPStringPboardType)
266  return [self dataForType:UTF8PboardType];
267 
268  return nil;
269 }
270 
276 - (id)propertyListForType:(CPString)aType
277 {
278  var data = [self dataForType:aType];
279 
280  if (data)
281  return [CPPropertyListSerialization propertyListFromData:data format:CPPropertyList280NorthFormat_v1_0];
282 
283  return nil;
284 }
285 
291 - (CPString)stringForType:(CPString)aType
292 {
293  return [self propertyListForType:aType];
294 }
295 
296 /* @ignore */
297 - (CPString)_generateStateUID
298 {
299  var bits = 32;
300 
301  _stateUID = @"";
302 
303  while (bits--)
304  _stateUID += FLOOR(RAND() * 16.0).toString(16).toUpperCase();
305 
306  return _stateUID;
307 }
308 
309 /* @ignore */
310 - (CPString)_stateUID
311 {
312  return _stateUID;
313 }
314 
315 @end
316 
317 #if PLATFORM(DOM)
318 
319 var DOMDataTransferPasteboard = nil;
320 
321 @implementation _CPDOMDataTransferPasteboard : CPPasteboard
322 {
323  DataTransfer _dataTransfer;
324 }
325 
326 + (_CPDOMDataTransferPasteboard)DOMDataTransferPasteboard
327 {
328  if (!DOMDataTransferPasteboard)
329  DOMDataTransferPasteboard = [[_CPDOMDataTransferPasteboard alloc] init];
330 
331  return DOMDataTransferPasteboard;
332 }
333 
334 - (void)_setDataTransfer:(DataTransfer)aDataTransfer
335 {
336  _dataTransfer = aDataTransfer;
337 }
338 
339 - (void)_setPasteboard:(CPPasteboard)aPasteboard
340 {
341  _dataTransfer.clearData();
342 
343  var types = [aPasteboard types],
344  count = types.length;
345 
346  while (count--)
347  {
348  var type = types[count];
349 
350  if (type === CPStringPboardType)
351  _dataTransfer.setData(type, [aPasteboard stringForType:type]);
352  else
353  _dataTransfer.setData(type, [[aPasteboard dataForType:type] rawString]);
354  }
355 }
356 
357 - (CPArray)types
358 {
359  return Array.prototype.slice.apply(_dataTransfer.types);
360 }
361 
362 - (CPData)dataForType:(CPString)aType
363 {
364  var dataString = _dataTransfer.getData(aType);
365 
366  if (aType === CPStringPboardType)
367  return [CPData dataFromPropertyList:dataString format:kCFPropertyList280NorthFormat_v1_0];
368 
369  return [CPData dataWithRawString:dataString];
370 }
371 
372 - (id)propertyListForType:(CPString)aType
373 {
374  if (aType === CPStringPboardType)
375  return _dataTransfer.getData(aType);
376 
377  return [CPPropertyListSerialization propertyListFromData:[self dataForType:aType] format:CPPropertyListUnknownFormat];
378 }
379 
380 @end
381 
382 #endif
CPRulerPboard
Definition: CPPasteboard.j:29
UTF8PboardType
Definition: CPPasteboard.j:43
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPImagePboardType
Definition: CPPasteboard.j:46
CPData dataFromPropertyList:format:(id aPlist, [format] CPPropertyListFormat aFormat)
CPStringPboardType
Definition: CPPasteboard.j:37
CPColorPboardType
Definition: CPPasteboard.j:33
id generalPasteboard()
Definition: CPPasteboard.j:82
var CPPasteboards
Definition: CPPasteboard.j:49
BOOL setData:forType:(CPData aData, [forType] CPString aType)
Definition: CPPasteboard.j:180
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:196
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
void initialize()
Definition: CPPasteboard.j:69
CPArray types()
Definition: CPPasteboard.j:231
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:276
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
id init()
Definition: CPObject.j:145
CPFilenamesPboardType
Definition: CPPasteboard.j:34
unsigned changeCount()
Definition: CPPasteboard.j:240
CPData dataForType:(CPString aType)
Definition: CPPasteboard.j:250
CPFindPboard
Definition: CPPasteboard.j:30
id pasteboardWithName:(CPString aName)
Definition: CPPasteboard.j:92
id alloc()
Definition: CPObject.j:130