1// @file PlatQt.h
2// Copyright (c) 1990-2011, Scientific Toolworks, Inc.
3//
4// The License.txt file describes the conditions under which this software may be distributed.
5//
6// Author: Jason Haslam
7//
8// Additions Copyright (c) 2011 Archaeopteryx Software, Inc. d/b/a Wingware
9// Scintilla platform layer for Qt
10
11#ifndef PLATQT_H
12#define PLATQT_H
13
14#include <cstddef>
15
16#include <string_view>
17#include <vector>
18#include <optional>
19#include <memory>
20
21#include "Debugging.h"
22#include "Geometry.h"
23#include "ScintillaTypes.h"
24#include "ScintillaMessages.h"
25#include "Platform.h"
26
27#include <QUrl>
28#include <QPaintDevice>
29#include <QPainter>
30#include <QHash>
31
32namespace Scintilla::Internal {
33
34const char *CharacterSetID(Scintilla::CharacterSet characterSet);
35
36inline QColor QColorFromColourRGBA(ColourRGBA ca)
37{
38 return QColor(ca.GetRed(), ca.GetGreen(), ca.GetBlue(), ca.GetAlpha());
39}
40
41inline QRect QRectFromPRect(PRectangle pr)
42{
43 return QRect(pr.left, pr.top, pr.Width(), pr.Height());
44}
45
46inline QRectF QRectFFromPRect(PRectangle pr)
47{
48 return QRectF(pr.left, pr.top, pr.Width(), pr.Height());
49}
50
51inline PRectangle PRectFromQRect(QRect qr)
52{
53 return PRectangle(qr.x(), qr.y(), qr.x() + qr.width(), qr.y() + qr.height());
54}
55
56inline Point PointFromQPoint(QPoint qp)
57{
58 return Point(qp.x(), qp.y());
59}
60
61inline QPointF QPointFFromPoint(Point qp)
62{
63 return QPointF(qp.x, qp.y);
64}
65
66constexpr PRectangle RectangleInset(PRectangle rc, XYPOSITION delta) noexcept {
67 return PRectangle(rc.left + delta, rc.top + delta, rc.right - delta, rc.bottom - delta);
68}
69
70class SurfaceImpl : public Surface {
71private:
72 QPaintDevice *device = nullptr;
73 QPainter *painter = nullptr;
74 bool deviceOwned = false;
75 bool painterOwned = false;
76 SurfaceMode mode;
77 const char *codecName = nullptr;
78 QTextCodec *codec = nullptr;
79
80 void Clear();
81
82public:
83 SurfaceImpl();
84 SurfaceImpl(int width, int height, SurfaceMode mode_);
85 virtual ~SurfaceImpl();
86
87 void Init(WindowID wid) override;
88 void Init(SurfaceID sid, WindowID wid) override;
89 std::unique_ptr<Surface> AllocatePixMap(int width, int height) override;
90
91 void SetMode(SurfaceMode mode) override;
92
93 void Release() noexcept override;
94 int SupportsFeature(Scintilla::Supports feature) noexcept override;
95 bool Initialised() override;
96 void PenColour(ColourRGBA fore);
97 void PenColourWidth(ColourRGBA fore, XYPOSITION strokeWidth);
98 int LogPixelsY() override;
99 int PixelDivisions() override;
100 int DeviceHeightFont(int points) override;
101 void LineDraw(Point start, Point end, Stroke stroke) override;
102 void PolyLine(const Point *pts, size_t npts, Stroke stroke) override;
103 void Polygon(const Point *pts, size_t npts, FillStroke fillStroke) override;
104 void RectangleDraw(PRectangle rc, FillStroke fillStroke) override;
105 void RectangleFrame(PRectangle rc, Stroke stroke) override;
106 void FillRectangle(PRectangle rc, Fill fill) override;
107 void FillRectangleAligned(PRectangle rc, Fill fill) override;
108 void FillRectangle(PRectangle rc, Surface &surfacePattern) override;
109 void RoundedRectangle(PRectangle rc, FillStroke fillStroke) override;
110 void AlphaRectangle(PRectangle rc, XYPOSITION cornerSize, FillStroke fillStroke) override;
111 void GradientRectangle(PRectangle rc, const std::vector<ColourStop> &stops, GradientOptions options) override;
112 void DrawRGBAImage(PRectangle rc, int width, int height,
113 const unsigned char *pixelsImage) override;
114 void Ellipse(PRectangle rc, FillStroke fillStroke) override;
115 void Stadium(PRectangle rc, FillStroke fillStroke, Ends ends) override;
116 void Copy(PRectangle rc, Point from, Surface &surfaceSource) override;
117
118 std::unique_ptr<IScreenLineLayout> Layout(const IScreenLine *screenLine) override;
119
120 void DrawTextNoClip(PRectangle rc, const Font *font, XYPOSITION ybase,
121 std::string_view text, ColourRGBA fore, ColourRGBA back) override;
122 void DrawTextClipped(PRectangle rc, const Font *font, XYPOSITION ybase,
123 std::string_view text, ColourRGBA fore, ColourRGBA back) override;
124 void DrawTextTransparent(PRectangle rc, const Font *font, XYPOSITION ybase,
125 std::string_view text, ColourRGBA fore) override;
126 void MeasureWidths(const Font *font, std::string_view text,
127 XYPOSITION *positions) override;
128 XYPOSITION WidthText(const Font *font, std::string_view text) override;
129
130 void DrawTextNoClipUTF8(PRectangle rc, const Font *font_, XYPOSITION ybase,
131 std::string_view text, ColourRGBA fore, ColourRGBA back) override;
132 void DrawTextClippedUTF8(PRectangle rc, const Font *font_, XYPOSITION ybase,
133 std::string_view text, ColourRGBA fore, ColourRGBA back) override;
134 void DrawTextTransparentUTF8(PRectangle rc, const Font *font_, XYPOSITION ybase,
135 std::string_view text, ColourRGBA fore) override;
136 void MeasureWidthsUTF8(const Font *font_, std::string_view text,
137 XYPOSITION *positions) override;
138 XYPOSITION WidthTextUTF8(const Font *font_, std::string_view text) override;
139
140 XYPOSITION Ascent(const Font *font) override;
141 XYPOSITION Descent(const Font *font) override;
142 XYPOSITION InternalLeading(const Font *font) override;
143 XYPOSITION Height(const Font *font) override;
144 XYPOSITION AverageCharWidth(const Font *font) override;
145
146 void SetClip(PRectangle rc) override;
147 void PopClip() override;
148 void FlushCachedState() override;
149 void FlushDrawing() override;
150
151 void BrushColour(ColourRGBA back);
152 void SetCodec(const Font *font);
153 void SetFont(const Font *font);
154
155 QPaintDevice *GetPaintDevice();
156 void SetPainter(QPainter *painter);
157 QPainter *GetPainter();
158};
159
160}
161
162#endif
163