File: testpb.m

package info (click to toggle)
gnustep-examples 1%3A1.4.0%2Bgit20210703-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,164 kB
  • sloc: objc: 17,202; makefile: 56
file content (82 lines) | stat: -rwxr-xr-x 2,200 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSData.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSGeometry.h>
#include <AppKit/NSPasteboard.h>
#include <AppKit/NSApplication.h>

@interface	pbOwner : NSObject
{
}
- (void) pasteboard: (NSPasteboard*)pb provideDataForType: (NSString*)type;
@end

@implementation	pbOwner
- (void) pasteboard: (NSPasteboard*)pb provideDataForType: (NSString*)type
{
    if ([type isEqual: NSFileContentsPboardType]) {
        NSString*	s = [pb stringForType: NSStringPboardType];

	if (s) {
	    const char*	ptr;
	    int		len;
	    NSData*		d;

	    ptr = [s cString];
	    len = strlen(ptr);
	    d = [NSData dataWithBytes: ptr length: len];
    	    [pb setData: d forType: type];
	}
    }
}
@end

int
main(int argc, char** argv)
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];
  pbOwner	*owner = [pbOwner new];
  NSPasteboard	*pb;
  NSArray	*types;
  NSData	*d;
  NSApplication *theApp;

#if LIB_FOUNDATION_LIBRARY
  [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
#endif

  theApp = [NSApplication sharedApplication];
  [theApp registerServicesMenuSendTypes: [NSArray arrayWithObject: NSStringPboardType]
	  returnTypes: [NSArray arrayWithObject: NSStringPboardType]];
  
  [NSObject enableDoubleReleaseCheck: YES];

  types = [NSArray arrayWithObjects:
	NSStringPboardType, NSFileContentsPboardType, nil];
  pb = [NSPasteboard generalPasteboard];
  [pb declareTypes: types owner: owner];
  [pb setString: @"This is a test" forType: NSStringPboardType];
  d = [pb dataForType: NSFileContentsPboardType];
  printf("%.*s\n", [d length], [d bytes]);

  pb = [NSPasteboard pasteboardWithUniqueName];
  types = [NSArray arrayWithObjects:
	NSStringPboardType, nil];
  [pb declareTypes: types owner: owner];
  [pb setString: @"a lowercase test string" forType: NSStringPboardType];
  if (NSPerformService(@"To upper", pb) == NO)
    {
      printf("Failed to perform 'To upper' service\n");
    }
  else
    {
      NSString	*result = [pb stringForType: NSStringPboardType];

      printf("To upper - result - '%s'\n", [result cString]);
    }
  [pool release];
  exit(0);
}