summaryrefslogtreecommitdiffstats
path: root/macosx/tkMacOSXServices.c
blob: 6ecb12dd5d7f5b232da4bfe736704d1209897058 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * tkMacOSXServices.c --
 *\
 *	This file allows the integration of Tk and the Cocoa NSServices API.
 *
 * Copyright (c) 2010-2019 Kevin Walzer/WordTech Communications LLC.
 * Copyright (c) 2010 Adrian Robert.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include <CoreServices/CoreServices.h>
#include <tkInt.h>
#include <tkMacOSXInt.h>

static Tcl_Interp *ServicesInterp;

/*
 * Event proc which calls the PerformService procedure
 */

static int
ServicesEventProc(
    Tcl_Event *event,
    int flags)
{
    Tcl_GlobalEval(ServicesInterp, "::tk::mac::PerformService");
    return 1;
}

/*
 * Class declarations for TkService class.
 */

@interface TkService : NSView {

}

+ (void) initialize;
- (void)provideService:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error;
- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType;
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard  types:(NSArray *)types;

@end

/*
 * Class methods.
 */

@implementation TkService

+ (void) initialize {
    NSArray *sendTypes = [NSArray arrayWithObjects:@"NSStringPboardType",
				  @"NSPasteboardTypeString", nil];
    [NSApp registerServicesMenuSendTypes:sendTypes returnTypes:sendTypes];
    return;
}


- (id)validRequestorForSendType:(NSString *)sendType
		     returnType:(NSString *)returnType
{
    if ([sendType isEqualToString:@"NSStringPboardType"] ||
	[sendType isEqualToString:@"NSPasteboardTypeString"]) {
	return self;
    }
    return [super validRequestorForSendType:sendType returnType:returnType];
}

/*
 * Make sure the view accepts events.
 */

- (BOOL)acceptsFirstResponder
{
    return YES;
}
- (BOOL)becomeFirstResponder
{
    return YES;
}

/*
 * Get selected text, copy to pasteboard.
 */

- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard
			     types:(NSArray *)types
{
    NSArray *typesDeclared = nil;
    NSString *pboardType = nil;

    for (NSString *typeString in types) {
	if ([typeString isEqualToString:@"NSStringPboardType"] ||
	    [typeString isEqualToString:@"NSPasteboardTypeString"]) {
	    typesDeclared = [NSArray arrayWithObject:typeString];
	    pboardType = typeString;
	    break;
	}
    }
    if (!typesDeclared) {
	return NO;
    }
    Tcl_Eval(ServicesInterp, "selection get");

    char *copystring = Tcl_GetString(Tcl_GetObjResult(ServicesInterp));
    NSString *writestring = [NSString stringWithUTF8String:copystring];

    [pboard declareTypes:typesDeclared owner:nil];
    return [pboard setString:writestring forType:pboardType];
}

/*
 * This is the method that actually calls the Tk service; this is the method
 * that must be defined in info.plist.
 */

- (void)provideService:(NSPasteboard *)pboard
	      userData:(NSString *)data
		 error:(NSString **)error
{
    NSString *pboardString, *pboardType;
    NSArray *types = [pboard types];
    Tcl_Event *event;

    /*
     * Get string from private pasteboard, write to general pasteboard to make
     * available to Tcl service.
     */

    for (NSString *typeString in types) {
	if ([typeString isEqualToString:@"NSStringPboardType"] ||
	    [typeString isEqualToString:@"NSPasteboardTypeString"]) {
	    pboardString = [pboard stringForType:typeString];
	    pboardType = typeString;
	    break;
	}
    }
    if (pboardString) {
	NSPasteboard *generalpasteboard = [NSPasteboard generalPasteboard];
	[generalpasteboard declareTypes:[NSArray arrayWithObjects:pboardType, nil]
				  owner:nil];
	[generalpasteboard setString:pboardString forType:pboardType];
	event = ckalloc(sizeof(Tcl_Event));
	event->proc = ServicesEventProc;
	Tcl_QueueEvent((Tcl_Event *)event, TCL_QUEUE_TAIL);
    }
}
@end

/*
 * Register a specific widget to access the Services menu.
 */

int
TkMacOSXRegisterServiceWidgetObjCmd(
    ClientData cd,
    Tcl_Interp *ip,
    int objc,
    Tcl_Obj *CONST objv[])
{
    /*
     * Need proper number of args.
     */

    if (objc != 2) {
	Tcl_WrongNumArgs(ip, 1, objv, "path?");
	return TCL_ERROR;
    }

    /*
     * Get the object that holds this Tk Window...
     */

    Rect bounds;
    NSRect frame;
    Tk_Window path =
	    Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip));

    if (path == NULL) {
	return TCL_ERROR;
    }

    Tk_MakeWindowExist(path);
    Tk_MapWindow(path);
    Drawable d = Tk_WindowId(path);

    /*
     * Get NSView from Tk window and add subview.
     */

    TkService *serviceview = [[TkService alloc] init];
    NSView *view = TkMacOSXGetRootControl(d);

    if ([serviceview superview] != view) {
	[view addSubview:serviceview];
    }
    TkMacOSXWinBounds((TkWindow*)path, &bounds);

    /*
     * Hack to make sure subview is set to take up entire geometry of window.
     */

    frame = NSMakeRect(bounds.left, bounds.top, 100000, 100000);
    frame.origin.y = 0;
    if (!NSEqualRects(frame, [serviceview frame])) {
    	[serviceview setFrame:frame];
    }
    [serviceview release];
    return TCL_OK;
}

/*
 * Initalize the package in the Tcl interpreter, create Tcl commands.
 */

int
TkMacOSXServices_Init(
    Tcl_Interp *interp)
{
    /*
     * Initialize instance of TclServices to provide service functionality.
     */

    TkService *service = [[TkService alloc] init];

    ServicesInterp = interp;
    [NSApp setServicesProvider:service];
    return TCL_OK;
}

/*
 * Local Variables:
 * mode: objc
 * c-basic-offset: 4
 * fill-column: 79
 * coding: utf-8
 * End:
 */