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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* tkMacOSXBitmap.c --
*
* This file handles the implementation of native bitmaps.
*
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2006-2009 Daniel A. Steffen <das@users.sourceforge.net>
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkMacOSXPrivate.h"
#include "tkMacOSXConstants.h"
/*
* This structure holds information about native bitmaps.
*/
typedef struct {
const char *name; /* Name of icon. */
OSType iconType; /* OSType of icon. */
} BuiltInIcon;
/*
* This array mapps a string name to the supported builtin icons
* on the Macintosh.
*/
static BuiltInIcon builtInIcons[] = {
{"document", kGenericDocumentIcon},
{"stationery", kGenericStationeryIcon},
{"edition", kGenericEditionFileIcon},
{"application", kGenericApplicationIcon},
{"accessory", kGenericDeskAccessoryIcon},
{"folder", kGenericFolderIcon},
{"pfolder", kPrivateFolderIcon},
{"trash", kTrashIcon},
{"floppy", kGenericFloppyIcon},
{"ramdisk", kGenericRAMDiskIcon},
{"cdrom", kGenericCDROMIcon},
{"preferences", kGenericPreferencesIcon},
{"querydoc", kGenericQueryDocumentIcon},
{"stop", kAlertStopIcon},
{"note", kAlertNoteIcon},
{"caution", kAlertCautionIcon},
{NULL, 0}
};
#define builtInIconSize 32
#define OSTYPE_TO_UTI(x) (NSString *)UTTypeCreatePreferredIdentifierForTag( \
kUTTagClassOSType, UTCreateStringForOSType(x), nil)
static Tcl_HashTable iconBitmapTable = {};
typedef struct {
int kind, width, height;
char *value;
} IconBitmap;
static const char *const iconBitmapOptionStrings[] = {
"-file", "-fileType", "-osType", "-systemType", "-namedImage",
"-imageFile", NULL
};
enum iconBitmapOptions {
ICON_FILE, ICON_FILETYPE, ICON_OSTYPE, ICON_SYSTEMTYPE, ICON_NAMEDIMAGE,
ICON_IMAGEFILE,
};
/*
*----------------------------------------------------------------------
*
* TkpDefineNativeBitmaps --
*
* Add native bitmaps.
*
* Results:
* A standard Tcl result. If an error occurs then TCL_ERROR is
* returned and a message is left in the interp's result.
*
* Side effects:
* "Name" is entered into the bitmap table and may be used from
* here on to refer to the given bitmap.
*
*----------------------------------------------------------------------
*/
void
TkpDefineNativeBitmaps(void)
{
Tcl_HashTable *tablePtr = TkGetBitmapPredefTable();
BuiltInIcon *builtInPtr;
for (builtInPtr = builtInIcons; builtInPtr->name != NULL; builtInPtr++) {
Tcl_HashEntry *predefHashPtr;
Tk_Uid name;
int isNew;
name = Tk_GetUid(builtInPtr->name);
predefHashPtr = Tcl_CreateHashEntry(tablePtr, name, &isNew);
if (isNew) {
TkPredefBitmap *predefPtr = (TkPredefBitmap *)ckalloc(sizeof(TkPredefBitmap));
predefPtr->source = UINT2PTR(builtInPtr->iconType);
predefPtr->width = builtInIconSize;
predefPtr->height = builtInIconSize;
predefPtr->native = 1;
Tcl_SetHashValue(predefHashPtr, predefPtr);
}
}
}
/*
*----------------------------------------------------------------------
*
* PixmapFromImage --
*
* Results:
* Returns a Pixmap with an NSImage drawn into it.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Pixmap
PixmapFromImage(
Display *display,
NSImage* image,
CGSize size)
{
TkMacOSXDrawingContext dc;
Pixmap pixmap;
pixmap = Tk_GetPixmap(display, None, size.width, size.height, 0);
if (TkMacOSXSetupDrawingContext(pixmap, NULL, &dc)) {
if (dc.context) {
CGAffineTransform t = { .a = 1, .b = 0, .c = 0, .d = -1,
.tx = 0, .ty = size.height};
CGContextConcatCTM(dc.context, t);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:GET_NSCONTEXT(dc.context, NO)];
[image drawAtPoint:NSZeroPoint fromRect:NSZeroRect
operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
}
TkMacOSXRestoreDrawingContext(&dc);
}
return pixmap;
}
/*
*----------------------------------------------------------------------
*
* TkpCreateNativeBitmap --
*
* Create native bitmap.
*
* Results:
* Native bitmap.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Pixmap
TkpCreateNativeBitmap(
Display *display,
const void *source) /* Info about the icon to build. */
{
NSString *iconUTI = OSTYPE_TO_UTI(PTR2UINT(source));
NSImage *iconImage = [[NSWorkspace sharedWorkspace]
iconForFileType: iconUTI];
CGSize size = CGSizeMake(builtInIconSize, builtInIconSize);
Pixmap pixmap = PixmapFromImage(display, iconImage, size);
return pixmap;
}
/*
*----------------------------------------------------------------------
*
* OSTypeFromString --
*
* Helper to convert string to OSType.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* t is set to OSType if conversion successful.
*
*----------------------------------------------------------------------
*/
static int
OSTypeFromString(const char *s, OSType *t) {
int result = TCL_ERROR;
Tcl_DString ds;
Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "macRoman");
Tcl_UtfToExternalDString(encoding, s, -1, &ds);
if (Tcl_DStringLength(&ds) <= 4) {
char string[4] = {};
memcpy(string, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
*t = (OSType) string[0] << 24 | (OSType) string[1] << 16 |
(OSType) string[2] << 8 | (OSType) string[3];
result = TCL_OK;
}
Tcl_DStringFree(&ds);
Tcl_FreeEncoding(encoding);
return result;
}
/*
*----------------------------------------------------------------------
*
* TkpGetNativeAppBitmap --
*
* Get a named native bitmap.
*
* Attemps to interpret the given name in order as:
* - name defined by ::tk::mac::iconBitmap
* - NSImage named image name
* - NSImage url string
* - 4-char OSType of IconServices icon
*
* Results:
* Native bitmap or None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Pixmap
TkpGetNativeAppBitmap(
Display *display, /* The display. */
const char *name, /* The name of the bitmap. */
int *width, /* The width & height of the bitmap. */
int *height)
{
Tcl_HashEntry *hPtr;
Pixmap pixmap = None;
NSString *string;
NSImage *image = nil;
NSSize size = { .width = builtInIconSize, .height = builtInIconSize };
if (iconBitmapTable.buckets &&
(hPtr = Tcl_FindHashEntry(&iconBitmapTable, name))) {
OSType type;
IconBitmap *iconBitmap = (IconBitmap *)Tcl_GetHashValue(hPtr);
name = NULL;
size = NSMakeSize(iconBitmap->width, iconBitmap->height);
switch (iconBitmap->kind) {
case ICON_FILE:
string = [[NSString stringWithUTF8String:iconBitmap->value]
stringByExpandingTildeInPath];
image = [[NSWorkspace sharedWorkspace] iconForFile:string];
break;
case ICON_FILETYPE:
string = [NSString stringWithUTF8String:iconBitmap->value];
image = [[NSWorkspace sharedWorkspace] iconForFileType:string];
break;
case ICON_OSTYPE:
if (OSTypeFromString(iconBitmap->value, &type) == TCL_OK) {
string = NSFileTypeForHFSTypeCode(type);
image = [[NSWorkspace sharedWorkspace] iconForFileType:string];
}
break;
case ICON_SYSTEMTYPE:
name = iconBitmap->value;
break;
case ICON_NAMEDIMAGE:
string = [NSString stringWithUTF8String:iconBitmap->value];
image = [NSImage imageNamed:string];
break;
case ICON_IMAGEFILE:
string = [[NSString stringWithUTF8String:iconBitmap->value]
stringByExpandingTildeInPath];
image = [[[NSImage alloc] initWithContentsOfFile:string]
autorelease];
break;
}
if (image) {
[image setSize:size];
}
} else {
string = [NSString stringWithUTF8String:name];
image = [NSImage imageNamed:string];
if (!image) {
NSURL *url = [NSURL fileURLWithPath:string];
if (url) {
image = [[[NSImage alloc] initWithContentsOfURL:url]
autorelease];
}
}
if (image) {
size = [image size];
}
}
if (image) {
*width = size.width;
*height = size.height;
pixmap = PixmapFromImage(display, image, NSSizeToCGSize(size));
} else if (name) {
OSType iconType;
if (OSTypeFromString(name, &iconType) == TCL_OK) {
NSString *iconUTI = OSTYPE_TO_UTI(iconType);
NSImage *iconImage = [[NSWorkspace sharedWorkspace]
iconForFileType: iconUTI];
pixmap = PixmapFromImage(display, iconImage, NSSizeToCGSize(size));
}
}
return pixmap;
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXIconBitmapObjCmd --
*
* Implements the ::tk::mac::iconBitmap command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* none
*
*----------------------------------------------------------------------
*/
int
TkMacOSXIconBitmapObjCmd(
TCL_UNUSED(void *),
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tcl_HashEntry *hPtr;
int i = 1, len, isNew, result = TCL_ERROR;
const char *name, *value;
IconBitmap ib, *iconBitmap;
if (objc != 6) {
Tcl_WrongNumArgs(interp, 1, objv, "name width height "
"-file|-fileType|-osType|-systemType|-namedImage|-imageFile "
"value");
goto end;
}
name = Tcl_GetStringFromObj(objv[i++], &len);
if (!len) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("empty bitmap name", -1));
Tcl_SetErrorCode(interp, "TK", "MACBITMAP", "BAD", NULL);
goto end;
}
if (Tcl_GetIntFromObj(interp, objv[i++], &ib.width) != TCL_OK) {
goto end;
}
if (Tcl_GetIntFromObj(interp, objv[i++], &ib.height) != TCL_OK) {
goto end;
}
if (Tcl_GetIndexFromObjStruct(interp, objv[i++], iconBitmapOptionStrings,
sizeof(char *), "kind", TCL_EXACT, &ib.kind) != TCL_OK) {
goto end;
}
value = Tcl_GetStringFromObj(objv[i++], &len);
if (!len) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("empty bitmap value", -1));
Tcl_SetErrorCode(interp, "TK", "MACBITMAP", "EMPTY", NULL);
goto end;
}
#if 0
if ((kind == ICON_TYPE || kind == ICON_SYSTEM)) {
Tcl_DString ds;
Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "macRoman");
Tcl_UtfToExternalDString(encoding, value, -1, &ds);
len = Tcl_DStringLength(&ds);
Tcl_DStringFree(&ds);
Tcl_FreeEncoding(encoding);
if (len > 4) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"invalid bitmap value", -1));
Tcl_SetErrorCode(interp, "TK", "MACBITMAP", "INVALID", NULL);
goto end;
}
}
#endif
ib.value = (char *)ckalloc(len + 1);
strcpy(ib.value, value);
if (!iconBitmapTable.buckets) {
Tcl_InitHashTable(&iconBitmapTable, TCL_STRING_KEYS);
}
hPtr = Tcl_CreateHashEntry(&iconBitmapTable, name, &isNew);
if (!isNew) {
iconBitmap = (IconBitmap *)Tcl_GetHashValue(hPtr);
ckfree(iconBitmap->value);
} else {
iconBitmap = (IconBitmap *)ckalloc(sizeof(IconBitmap));
Tcl_SetHashValue(hPtr, iconBitmap);
}
*iconBitmap = ib;
result = TCL_OK;
end:
return result;
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|