1 2 /** 3 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 4 * This file is dual licensed under LGPL v2.1 and the Scintilla license (http://www.scintilla.org/License.txt). 5 * @file PlatCocoa.h 6 */ 7 8 #ifndef PLATCOCOA_H 9 #define PLATCOCOA_H 10 11 #include <cstdlib> 12 #include <cassert> 13 #include <cstring> 14 #include <cstdio> 15 16 #include <sys/time.h> 17 18 #include <Cocoa/Cocoa.h> 19 20 #include "Platform.h" 21 #include "Scintilla.h" 22 23 #include "QuartzTextLayout.h" 24 25 NSRect PRectangleToNSRect(const Scintilla::PRectangle &rc); 26 Scintilla::PRectangle NSRectToPRectangle(NSRect &rc); 27 CFStringEncoding EncodingFromCharacterSet(bool unicode, int characterSet); 28 29 @interface ScintillaContextMenu : NSMenu { 30 Scintilla::ScintillaCocoa *owner; 31 } 32 - (void) handleCommand: (NSMenuItem *) sender; 33 - (void) setOwner: (Scintilla::ScintillaCocoa *) newOwner; 34 35 @end 36 37 namespace Scintilla { 38 39 // A class to do the actual text rendering for us using Quartz 2D. 40 class SurfaceImpl : public Surface { 41 private: 42 bool unicodeMode; 43 float x; 44 float y; 45 46 CGContextRef gc; 47 48 /** The text layout instance */ 49 std::unique_ptr<QuartzTextLayout> textLayout; 50 int codePage; 51 int verticalDeviceResolution; 52 53 /** If the surface is a bitmap context, contains a reference to the bitmap data. */ 54 std::unique_ptr<uint8_t[]> bitmapData; 55 /** If the surface is a bitmap context, stores the dimensions of the bitmap. */ 56 int bitmapWidth; 57 int bitmapHeight; 58 59 /** Set the CGContext's fill colour to the specified desired colour. */ 60 void FillColour(const ColourDesired &back); 61 62 63 // 24-bit RGB+A bitmap data constants 64 static const int BITS_PER_COMPONENT = 8; 65 static const int BITS_PER_PIXEL = BITS_PER_COMPONENT * 4; 66 static const int BYTES_PER_PIXEL = BITS_PER_PIXEL / 8; 67 68 void Clear(); 69 70 public: 71 SurfaceImpl(); 72 ~SurfaceImpl() override; 73 74 void Init(WindowID wid) override; 75 void Init(SurfaceID sid, WindowID wid) override; 76 void InitPixMap(int width, int height, Surface *surface_, WindowID wid) override; GetContext()77 CGContextRef GetContext() { return gc; } 78 79 void Release() override; 80 bool Initialised() override; 81 void PenColour(ColourDesired fore) override; 82 83 /** Returns a CGImageRef that represents the surface. Returns NULL if this is not possible. */ 84 CGImageRef CreateImage(); 85 void CopyImageRectangle(Surface &surfaceSource, PRectangle srcRect, PRectangle dstRect); 86 87 int LogPixelsY() override; 88 int DeviceHeightFont(int points) override; 89 void MoveTo(int x_, int y_) override; 90 void LineTo(int x_, int y_) override; 91 void Polygon(Scintilla::Point *pts, size_t npts, ColourDesired fore, ColourDesired back) override; 92 void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) override; 93 void FillRectangle(PRectangle rc, ColourDesired back) override; 94 void FillRectangle(PRectangle rc, Surface &surfacePattern) override; 95 void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) override; 96 void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill, 97 ColourDesired outline, int alphaOutline, int flags) override; 98 void GradientRectangle(PRectangle rc, const std::vector<ColourStop> &stops, GradientOptions options) override; 99 void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) override; 100 void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override; 101 void Copy(PRectangle rc, Scintilla::Point from, Surface &surfaceSource) override; 102 std::unique_ptr<IScreenLineLayout> Layout(const IScreenLine *screenLine) override; 103 void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, 104 ColourDesired back) override; 105 void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, 106 ColourDesired back) override; 107 void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) override; 108 void MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) override; 109 XYPOSITION WidthText(Font &font_, std::string_view text) override; 110 XYPOSITION Ascent(Font &font_) override; 111 XYPOSITION Descent(Font &font_) override; 112 XYPOSITION InternalLeading(Font &font_) override; 113 XYPOSITION Height(Font &font_) override; 114 XYPOSITION AverageCharWidth(Font &font_) override; 115 116 void SetClip(PRectangle rc) override; 117 void FlushCachedState() override; 118 119 void SetUnicodeMode(bool unicodeMode_) override; 120 void SetDBCSMode(int codePage_) override; 121 void SetBidiR2L(bool bidiR2L_) override; 122 }; // SurfaceImpl class 123 124 } // Scintilla namespace 125 126 #endif 127