PageRenderTime 78ms CodeModel.GetById 2ms app.highlight 72ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/osx/objc/view.m

http://github.com/wilkie/djehuty
Objective C | 269 lines | 209 code | 57 blank | 3 comment | 37 complexity | f343864ed676e78ff523a22a31229de2 MD5 | raw file
  1#include <Cocoa/Cocoa.h>
  2#include <Foundation/Foundation.h>
  3
  4#include "OSXView.h"
  5
  6void _OSXViewCreate(struct _OSXViewPlatformVars** viewVars, int width, int height) {
  7	(*viewVars) = malloc (sizeof(struct _OSXViewPlatformVars));
  8
  9	(*viewVars)->dib_image = [ [ [ NSImage alloc ] initWithSize:NSMakeSize(width, height) ] retain ];
 10}
 11
 12void _OSXViewCreateDIB(struct _OSXViewPlatformVars** viewVars, int width, int height) {
 13	(*viewVars) = malloc (sizeof(struct _OSXViewPlatformVars));
 14
 15	(*viewVars)->dib_image_rep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes:nil
 16		pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4
 17		hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace
 18		bitmapFormat:NSAlphaNonpremultipliedBitmapFormat bytesPerRow:(width*4) bitsPerPixel:32 ] retain ];
 19
 20	(*viewVars)->dib_image = [ [ [ NSImage alloc ] initWithSize:NSMakeSize(width, height) ] retain ];
 21	[ (*viewVars)->dib_image addRepresentation: (*viewVars)->dib_image_rep ];
 22
 23	[ (*viewVars)->dib_image setFlipped:YES ];
 24}
 25
 26void _OSXViewDestroy(struct _OSXViewPlatformVars* viewVars, int isDIB, int isWindow) {
 27	if (isDIB) {
 28		[ viewVars->dib_image_rep release ];
 29		[ viewVars->dib_image release ];
 30	}
 31	else if (!isWindow) {
 32		[ viewVars->dib_image release ];
 33	}
 34
 35	free(viewVars);
 36}
 37
 38void* _OSXGetBytes(struct _OSXViewPlatformVars* viewVars) {
 39	return [ viewVars->dib_image_rep bitmapData ];
 40}
 41
 42// --- Graphics --- //
 43
 44void _OSXUsePen(struct _OSXViewPlatformVars* viewVars, int fromWindow, void* pen) {
 45	if (!fromWindow) {
 46		[ viewVars->dib_image lockFocus ];
 47	}
 48
 49	[ ((NSColor*)pen) setStroke ];
 50
 51	if (!fromWindow) {
 52		[ viewVars->dib_image unlockFocus ];
 53	}
 54}
 55
 56void _OSXCreatePen(void** pen, double r, double g, double b, double a) {
 57	NSColor* nsclr = [ [ NSColor colorWithDeviceRed:r green:g blue:b alpha:a ] retain ];
 58
 59	(*pen) = (void*)nsclr;
 60}
 61
 62void _OSXDestroyPen(void* pen) {
 63	[ (NSColor*)pen release ];
 64}
 65
 66void _OSXUseBrush(struct _OSXViewPlatformVars* viewVars, int fromWindow, void* brush) {
 67	if (!fromWindow) {
 68		[ viewVars->dib_image lockFocus ];
 69	}
 70
 71	[ ((NSColor*)brush) setFill ];
 72
 73	if (!fromWindow) {
 74		[ viewVars->dib_image unlockFocus ];
 75	}
 76}
 77
 78void _OSXCreateBrush(void** brush, double r, double g, double b, double a) {
 79	NSColor* nsclr = [ [ NSColor colorWithDeviceRed:r green:g blue:b alpha:a ] retain ];
 80
 81	(*brush) = (void*)nsclr;
 82}
 83
 84void _OSXDestroyBrush(void* brush) {
 85	[ (NSColor*)brush release ];
 86}
 87
 88//  Shapes  //
 89
 90void _OSXDrawLine(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
 91	if (!fromWindow) {
 92		[ viewVars->dib_image lockFocus ];
 93		[ NSBezierPath strokeLineFromPoint:NSMakePoint((x),(y)) toPoint:NSMakePoint((x2),(y2)) ];
 94		[ viewVars->dib_image unlockFocus ];
 95	}
 96	else {
 97		[ NSBezierPath strokeLineFromPoint:NSMakePoint((x),(y)) toPoint:NSMakePoint((x2),(y2)) ];
 98	}
 99}
100
101void _OSXDrawRect(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
102	if (!fromWindow) {
103		[ viewVars->dib_image lockFocus ];
104		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
105		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
106		[ viewVars->dib_image unlockFocus ];
107	}
108	else {
109		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
110		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
111	}
112}
113
114void _OSXFillRect(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
115	if (!fromWindow) {
116		[ viewVars->dib_image lockFocus ];
117		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
118		[ viewVars->dib_image unlockFocus ];
119	}
120	else {
121		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
122	}
123}
124
125void _OSXStrokeRect(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
126	if (!fromWindow) {
127		[ viewVars->dib_image lockFocus ];
128		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
129		[ viewVars->dib_image unlockFocus ];
130	}
131	else {
132		NSRectFill(NSMakeRect((x),(y),(x2)-(x),(y2)-(y)));
133		[ [ NSBezierPath bezierPathWithRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
134	}
135}
136
137void _OSXDrawOval(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
138	if (!fromWindow) {
139		[ viewVars->dib_image lockFocus ];
140		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
141		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
142		[ viewVars->dib_image unlockFocus ];
143	}
144	else {
145		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
146		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
147	}
148}
149
150void _OSXFillOval(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
151	if (!fromWindow) {
152		[ viewVars->dib_image lockFocus ];
153		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
154		[ viewVars->dib_image unlockFocus ];
155	}
156	else {
157		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] fill ];
158	}
159}
160
161void _OSXStrokeOval(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, int x2, int y2) {
162	if (!fromWindow) {
163		[ viewVars->dib_image lockFocus ];
164		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
165		[ viewVars->dib_image unlockFocus ];
166	}
167	else {
168		[ [ NSBezierPath bezierPathWithOvalInRect:NSMakeRect((x),(y),(x2)-(x),(y2)-(y)) ] stroke ];
169	}
170}
171
172// Text //
173
174void _OSXDrawText(struct _OSXViewPlatformVars* viewVars, int fromWindow, int x, int y, char* str) {
175	if (!fromWindow) {
176		[ viewVars->dib_image lockFocus ];
177	}
178
179	NSString* nss = [ [ NSString alloc ] initWithUTF8String:str ];
180
181	[ viewVars->txtstore setAttributedString: [ [ NSAttributedString alloc ] initWithString: nss attributes:viewVars->cur_font ] ] ;
182
183	NSRange glyphs = [ viewVars->layout glyphRangeForTextContainer: viewVars->container ];
184
185	[ [ NSGraphicsContext currentContext ] setShouldAntialias:YES ];
186	[ viewVars->layout drawGlyphsForGlyphRange:glyphs atPoint: NSMakePoint(x-5,y) ];
187	[ [ NSGraphicsContext currentContext ] setShouldAntialias:NO ];
188
189	if (!fromWindow) {
190		[ viewVars->dib_image unlockFocus ];
191	}
192}
193
194void _OSXMeasureText(struct _OSXViewPlatformVars* viewVars, int fromWindow, char* str, unsigned int* w, unsigned int* h) {
195	if (!fromWindow) {
196		[ viewVars->dib_image lockFocus ];
197	}
198
199	NSString* nss = [ [ NSString alloc ] initWithUTF8String:str ];
200
201	[ viewVars->txtstore setAttributedString: [ [ NSAttributedString alloc ] initWithString: nss attributes:viewVars->cur_font ] ] ;
202
203	NSRange glyphs = [ viewVars->layout glyphRangeForTextContainer: viewVars->container ];
204
205	NSRect nrect = [ viewVars->layout boundingRectForGlyphRange:glyphs inTextContainer: viewVars->container ];
206
207	(*w) = (unsigned int)nrect.size.width;
208	(*h) = (unsigned int)nrect.size.height;
209
210	if (!fromWindow) {
211		[ viewVars->dib_image unlockFocus ];
212	}
213}
214
215void _OSXDrawViewXY(struct _OSXViewPlatformVars* viewVars, int fromWindow, struct _OSXViewPlatformVars* srcViewVars, int srcFromWindow, int x, int y, int srcX, int srcY) {
216	if (!fromWindow) {
217		[ viewVars->dib_image lockFocus ];
218	}
219
220	if (!srcFromWindow) {
221		[ srcViewVars->dib_image drawAtPoint:NSMakePoint(x, y) fromRect:NSZeroRect operation:NSCompositeSourceOver  fraction:1.0f ];
222	}
223
224	if (!fromWindow) {
225		[ viewVars->dib_image unlockFocus ];
226	}
227}
228
229void _OSXDrawViewXYXYWH(struct _OSXViewPlatformVars* viewVars, int fromWindow, struct _OSXViewPlatformVars* srcViewVars, int srcFromWindow, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) {
230	if (!fromWindow) {
231		[ viewVars->dib_image lockFocus ];
232	}
233
234	if (!srcFromWindow) {
235		[ viewVars->dib_image drawInRect:NSMakeRect(x, y, srcWidth, srcHeight) fromRect:NSMakeRect(srcX, srcY, srcWidth, srcHeight) operation:NSCompositeSourceOver fraction:1.0f ];
236	}
237
238	if (!fromWindow) {
239		[ viewVars->dib_image unlockFocus ];
240	}
241}
242
243void _OSXDrawViewXYA(struct _OSXViewPlatformVars* viewVars, int fromWindow, struct _OSXViewPlatformVars* srcViewVars, int srcFromWindow, int x, int y, int srcX, int srcY, float opacity) {
244	if (!fromWindow) {
245		[ viewVars->dib_image lockFocus ];
246	}
247
248	if (!srcFromWindow) {
249		[ srcViewVars->dib_image drawAtPoint:NSMakePoint(x, y) fromRect:NSZeroRect operation:NSCompositeSourceOver  fraction: opacity ];
250	}
251
252	if (!fromWindow) {
253		[ viewVars->dib_image unlockFocus ];
254	}
255}
256
257void _OSXDrawViewXYXYWHA(struct _OSXViewPlatformVars* viewVars, int fromWindow, struct _OSXViewPlatformVars* srcViewVars, int srcFromWindow, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight, float opacity) {
258	if (!fromWindow) {
259		[ viewVars->dib_image lockFocus ];
260	}
261
262	if (!srcFromWindow) {
263		[ viewVars->dib_image drawInRect:NSMakeRect(x, y, srcWidth, srcHeight) fromRect:NSMakeRect(srcX, srcY, srcWidth, srcHeight) operation:NSCompositeSourceOver fraction: opacity ];
264	}
265
266	if (!fromWindow) {
267		[ viewVars->dib_image unlockFocus ];
268	}
269}