/platform/osx/objc/view.m

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