-
Notifications
You must be signed in to change notification settings - Fork 9
/
WIN32VSImageRep.m
247 lines (208 loc) · 6.1 KB
/
WIN32VSImageRep.m
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
#include <AppKit/NSImage.h>
#include <AppKit/NSImageRep.h>
#include <AppKit/NSPasteboard.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include "WinUXTheme.h"
#include "WIN32VSImageRep.h"
#define FOURCC_LENGTH 6
#define FOURCC_CODE "W32VSR"
@interface WIN32VSImageRep (Private)
- (void) _updateImageWidthAndHeight;
- (BOOL) _testPartAndStateIndices;
@end
@implementation WIN32VSImageRep
+ (NSArray*) imageUnfilteredFileTypes
{
NSDebugLog(@"WIN32VSR +imageUnfilteredFileTypes.");
return [NSArray arrayWithObject:@"w32vsr"];
}
+ (NSArray*) imageUnfilteredPasteboardTypes
{
NSDebugLog(@"WIN32VSR +imageUnfilteredPasteboardTypes");
return [NSArray arrayWithObjects:NSStringPboardType,
NSFileContentsPboardType,
nil];
}
+ (BOOL) canInitWithData:(NSData*)data
{
NSDebugLog(@"WIN32VSR +canInitWithData:");
if ([data length] >= FOURCC_LENGTH)
{
char fourcc[FOURCC_LENGTH+1];
[data getBytes:(void*)fourcc length:FOURCC_LENGTH];
fourcc[FOURCC_LENGTH] = '\0';
if (strncmp(fourcc, FOURCC_CODE, FOURCC_LENGTH)==0)
return YES;
}
return NO;
}
+ (NSImageRep*) imageRepWithData:(NSData*)data
{
NSDebugLog(@"WIN32VSR +imageRepWithData");
return [[self alloc] initWithData:data];
}
- (id) retain
{
NSDebugLog(@"WIN32VSR -retain");
return [super retain];
}
- (id) init
{
NSDebugLog(@"WIN32VSR -init");
return [super init];
}
- (void) encodeWithCoder:(NSCoder*)coder
{
NSDebugLog(@"WIN32VSR -encodeWithCoder");
[super encodeWithCoder:coder];
if ([coder allowsKeyedCoding])
{
[coder encodeObject:themeClass forKey:@"WIN32ThemeClass"];
[coder encodeInt:part forKey:@"WIN32ThemePart"];
[coder encodeInt:state forKey:@"WIN32ThemeState"];
}
else
{
[coder encodeObject:themeClass];
[coder encodeValueOfObjCType:@encode(int) at:&part];
[coder encodeValueOfObjCType:@encode(int) at:&state];
}
}
- (id) initWithCoder:(NSCoder*)coder
{
NSDebugLog(@"WIN32VSR -initWithCoder");
self = [super initWithCoder:coder];
if (self)
{
if ([coder allowsKeyedCoding])
{
themeClass = RETAIN([coder decodeObjectForKey:@"WIN32ThemeClass"]);
NSAssert([themeClass isKindOfClass:[NSString class]], @"themeClass is not a string in keyed archiver");
part = [coder decodeIntForKey:@"WIN32ThemePart"];
state = [coder decodeIntForKey:@"WIN32ThemeState"];
}
else
{
themeClass = RETAIN([coder decodeObject]);
NSAssert([themeClass isKindOfClass:[NSString class]], @"themeClass is not a string");
[coder decodeValueOfObjCType:@encode(int) at:&part];
[coder decodeValueOfObjCType:@encode(int) at:&state];
}
if ([self _testPartAndStateIndices] == NO)
{
[self dealloc];
return nil;
}
[self _updateImageWidthAndHeight];
}
return self;
}
- (id) initWithData:(NSData*)data
{
NSDebugLog(@"WIN32VSR -initWithData:");
self = [super init];
if (self==nil)
return nil;
if ([data length] > FOURCC_LENGTH)
{
NSMutableString *chars = [[NSMutableString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSDictionary *info;
if (chars==nil)
return nil;
[chars deleteCharactersInRange:NSMakeRange(0, FOURCC_LENGTH)];
info = [chars propertyList];
if (info==nil)
{
RELEASE(chars);
return nil;
}
self->themeClass = [(NSString*)[info objectForKey:@"Class"] retain];
self->part = [[info objectForKey:@"Part"] intValue];
self->state = [[info objectForKey:@"State"] intValue];
// Make sure these part and state indices are valid. This is needed for
// Windows XP, which does not contain the same theme classes, parts and
// states as Windows Vista. This should cause a fallback where the theme
// engine will not load this "image" and use whatever already exists.
if ([self _testPartAndStateIndices] == NO)
{
RELEASE(chars);
DESTROY(self);
return nil;
}
// Change our image width and height based upon what windows tells
// us.
[self _updateImageWidthAndHeight];
RELEASE(chars);
NSDebugLog(@"Successfully loaded WIN32ImageRep object.");
return self;
}
return nil;
}
- (id) copyWithZone:(NSZone*)zone
{
NSDebugLog(@"WIN32VSR -copyWithZone");
WIN32VSImageRep* c = [super copyWithZone:zone];
TEST_RETAIN(c->themeClass);
[self _updateImageWidthAndHeight];
return c;
}
- (BOOL) draw
{
NSDebugLog(@"WIN32VSR -draw: Draw WIN32VSImageRep %@ %d %d", themeClass, part, state);
HTHEME hTheme;
BOOL res;
WinUXTheme *themeEngine;
themeEngine = (WinUXTheme*)[GSTheme theme];
hTheme = [themeEngine themeWithClassName:themeClass];
res = [themeEngine drawThemeBackground:hTheme
inRect:NSMakeRect(0.,0.,[self pixelsWide],[self pixelsHigh])
part:part
state:state
];
[themeEngine releaseTheme:hTheme];
return res;
}
- (void) dealloc
{
NSDebugLog(@"WIN32VSR -dealloc");
[themeClass release];
[super dealloc];
}
@end
@implementation WIN32VSImageRep (Private)
- (void) _updateImageWidthAndHeight
{
if ([[GSTheme theme] class] != [WinUXTheme class])
return;
WinUXTheme* theme = (WinUXTheme*)[GSTheme theme];
HTHEME hTheme;
//int width, height;
NSSize size;
hTheme = [theme themeWithClassName:themeClass];
//size = [theme defaultSizeForTheme:hTheme part:part state:state];
size = [theme sizeForTheme:hTheme part:part state:state type:WIN32ThemeSizeBestFit];
//[theme releaseTheme:hTheme];
//width = [theme intForTheme:hTheme part:part state:state property:TMT_WIDTH];
//height = [theme intForTheme:hTheme part:part state:state property:TMT_HEIGHT];
[self setPixelsHigh:size.height];
[self setPixelsWide:size.width];
[self setSize:size]; // lets assume that pixels == size
[theme releaseTheme:hTheme];
}
- (BOOL) _testPartAndStateIndices
{
HTHEME hTheme;
WinUXTheme *win32Theme = (WinUXTheme*)[GSTheme theme];
BOOL result = NO;
hTheme = [win32Theme themeWithClassName:themeClass];
if (hTheme)
{
result = [win32Theme isTheme:hTheme partDefined:part];
[win32Theme releaseTheme:hTheme];
}
return result;
}
@end