📄 platform.cpp
字号:
/* porting scintilla to qt module*//* platform of qt gui*/#include <stdio.h>#include <stdarg.h>#include <string.h>#include <qapplication.h>#include <qwidget.h>#include <qfont.h>#include <qpixmap.h>#include <qimage.h>#include <qstring.h>#include <qdatetime.h>#include <qpainter.h>#include <qcursor.h>#include <qlibrary.h>#include <qpolygon.h>#include "Platform.h"#include "XPM.h"#include "qsteclasses.h"#include "qstescintillabase.h"//type convertorsstatic QFont *PFont(FontID id){ return reinterpret_cast<QFont*>(id);}static QWidget *PWindow(WindowID id){ return reinterpret_cast<QWidget *>(id);}static QStePopup *PMenu(MenuID id){ return reinterpret_cast<QStePopup *>(id);}//create a point instance from a long valuePoint Point::FromLong(long lpoint){ return Point(Platform::LowShortFromLong(lpoint),Platform::HighShortFromLong(lpoint));}//color palette managementPalette::Palette(){ used = 0; allowRealization = false;}Palette::~Palette(){ Release();}void Palette::Release(){ used = 0;}void Palette::WantFind(ColourPair &cp,bool want){ if(!want) cp.allocated.Set(cp.desired.AsLong());}void Palette::Allocate(Window &wid){}//font managementFont::Font():id(0){}Font::~Font(){}void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic, bool extraFontFlag){ Release(); QFont *f = new QFont();; // If name of the font begins with a '-', assume, that it is // a full fontspec. if(faceName[0] == '-'){ f->setRawName(faceName); }else{ f->setFamily(faceName); f->setPointSize(size); f->setBold(bold); f->setItalic(italic); } id = f;}void Font::Release(){ if(id){ delete PFont(id); id = 0; }}//a surface abstracts a place to drawclass SurfaceImpl : public Surface{public: SurfaceImpl(); virtual ~SurfaceImpl(); void Init(WindowID wid); void Init(SurfaceID sid,WindowID wid); void Init(QPainter *qp); void InitPixMap(int width,int height,Surface *,WindowID wid); void Release(); bool Initialised(){ return painter; } void PenColour(ColourAllocated fore); int LogPixelsY(){ return 72; } int DeviceHeightFont(int points){ return points; } void MoveTo(int x_,int y_); void LineTo(int x_,int y_); void Polygon(Point *pts,int npts,ColourAllocated fore,ColourAllocated back); void RectangleDraw(PRectangle rc,ColourAllocated fore,ColourAllocated back); void FillRectangle(PRectangle rc,ColourAllocated back); void FillRectangle(PRectangle rc,Surface &surfacePattern); void RoundedRectangle(PRectangle rc,ColourAllocated fore,ColourAllocated back); void AlphaRectangle(PRectangle rc,int cornerSize,ColourAllocated fill,int alphaFill,ColourAllocated outline,int alphaOutline,int flags); void Ellipse(PRectangle rc,ColourAllocated fore,ColourAllocated back); void Copy(PRectangle rc,Point from,Surface &surfaceSource); void DrawTextNoClip(PRectangle rc,Font &font_,int ybase,const char *s,int len,ColourAllocated fore,ColourAllocated back); void DrawTextClipped(PRectangle rc,Font &font_,int ybase,const char *s,int len,ColourAllocated fore,ColourAllocated back); void DrawTextTransparent(PRectangle rc,Font &font_,int ybase,const char *s,int len,ColourAllocated fore); void MeasureWidths(Font &font_,const char *s,int len,int *positions); int WidthText(Font &font_,const char *s,int len); int WidthChar(Font &font_,char ch); int Ascent(Font &font_); int Descent(Font &font_); int InternalLeading(Font &font_){ return 0; } int ExternalLeading(Font &font_); int Height(Font &font_); int AverageCharWidth(Font &font_){ return WidthChar(font_,'n'); } int SetPalette(Palette *,bool){ return 0; } void SetClip(PRectangle rc); void FlushCachedState(); void SetUnicodeMode(bool unicodeMode_){ unicodeMode = unicodeMode_; } void SetDBCSMode(int codePage){ } void DrawXPM(PRectangle rc,const XPM *xpm);private: QFontMetrics metrics(Font &font_); QString convertText(const char *s,int len); static QRgb convertQRgb(const ColourAllocated &col,unsigned alpha); static QColor convertQColor(const ColourAllocated &col,unsigned alpha = 0xff); bool unicodeMode; QPaintDevice *pd; QPainter *painter; bool my_resources; int pen_x,pen_y;};Surface *Surface::Allocate(){ return new SurfaceImpl;}SurfaceImpl::SurfaceImpl():unicodeMode(false),pd(0),painter(0),my_resources(false),pen_x(0),pen_y(0){}SurfaceImpl::~SurfaceImpl(){ Release();}void SurfaceImpl::Init(WindowID wid){ Release(); pd = reinterpret_cast<QWidget *>(wid);}void SurfaceImpl::Init(SurfaceID sid,WindowID wid){ Release(); // This method, and the SurfaceID type, is only used when printing. As it // is actually a void * we pass (when using SCI_FORMATRANGE) a pointer to a // QPainter rather than a pointer to a SurfaceImpl as might be expected. QPainter *p = reinterpret_cast<QPainter *>(sid); pd = p->device(); painter = p;}void SurfaceImpl::Init(QPainter *p){ Release(); pd = p->device(); painter = p;}void SurfaceImpl::InitPixMap(int width,int height,Surface *ss,WindowID wid){ Release(); pd = new QPixmap(width,height); painter = new QPainter(pd); my_resources = true;}void SurfaceImpl::Release(){ if(my_resources){ if(painter) delete painter; if(pd) delete pd; my_resources = false; } painter = 0; pd = 0;}void SurfaceImpl::MoveTo(int x_,int y_){ Q_ASSERT(painter); pen_x = x_; pen_y = y_;}void SurfaceImpl::LineTo(int x_,int y_){ Q_ASSERT(painter); painter->drawLine(pen_x,pen_y,x_,y_); pen_x = x_; pen_y = y_;}void SurfaceImpl::PenColour(ColourAllocated fore){ Q_ASSERT(painter); painter->setPen(convertQColor(fore));}void SurfaceImpl::Polygon(Point *pts,int npts,ColourAllocated fore,ColourAllocated back){ Q_ASSERT(painter); QPolygon qpts(npts); for(int i = 0;i < npts;i++){ qpts.setPoint(i,pts[i].x,pts[i].y); } painter->setPen(convertQColor(fore)); painter->setBrush(convertQColor(back)); painter->drawPolygon(qpts);}void SurfaceImpl::RectangleDraw(PRectangle rc,ColourAllocated fore,ColourAllocated back){ Q_ASSERT(painter); painter->setPen(convertQColor(fore)); painter->setBrush(convertQColor(back)); painter->drawRect(rc.left,rc.top,rc.right - rc.left - 1,rc.bottom - rc.top - 1);}void SurfaceImpl::FillRectangle(PRectangle rc,ColourAllocated back){ Q_ASSERT(painter); painter->setPen(Qt::NoPen); painter->setBrush(convertQColor(back)); painter->drawRect(rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top);}void SurfaceImpl::FillRectangle(PRectangle rc,Surface &surfacePattern){ Q_ASSERT(painter); SurfaceImpl &si = static_cast<SurfaceImpl &>(surfacePattern); QPixmap *pm = static_cast<QPixmap *>(si.pd); if(pm){ QBrush brsh(Qt::black,*pm); painter->setPen(Qt::NoPen); painter->setBrush(brsh); painter->drawRect(rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top); }else{ FillRectangle(rc,ColourAllocated(0)); }}void SurfaceImpl::RoundedRectangle(PRectangle rc,ColourAllocated fore,ColourAllocated back){ Q_ASSERT(painter); painter->setPen(convertQColor(fore)); painter->setBrush(convertQColor(back)); painter->drawRoundRect(rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top);}void SurfaceImpl::AlphaRectangle(PRectangle rc,int cornerSize,ColourAllocated fill,int alphaFill,ColourAllocated outline,int alphaOutline,int){ Q_ASSERT(painter); int w = rc.right - rc.left; int h = rc.bottom - rc.top; // Assume that "cornerSize" means outline width. if(cornerSize > 0) painter->setPen(QPen(QColor(convertQRgb(outline,alphaOutline)),cornerSize)); else painter->setPen(Qt::NoPen); painter->setBrush(QColor(convertQRgb(fill,alphaFill))); painter->drawRect(rc.left,rc.top,w,h);}void SurfaceImpl::Ellipse(PRectangle rc,ColourAllocated fore,ColourAllocated back){ Q_ASSERT(painter); painter->setPen(convertQColor(fore)); painter->setBrush(convertQColor(back)); painter->drawEllipse(rc.left,rc.top,rc.right - rc.left,rc.bottom - rc.top);}void SurfaceImpl::Copy(PRectangle rc,Point from,Surface &surfaceSource){ Q_ASSERT(painter); SurfaceImpl &si = static_cast<SurfaceImpl &>(surfaceSource); if(si.pd){ QPixmap *pm = static_cast<QPixmap *>(si.pd); painter->drawPixmap(rc.left,rc.top,*pm,from.x,from.y,rc.right - rc.left,rc.bottom - rc.top); }}void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, int ybase,const char *s, int len,ColourAllocated fore, ColourAllocated back){ Q_ASSERT(painter); FillRectangle(rc, back); DrawTextTransparent(rc, font_, ybase, s, len, fore);}void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, int ybase,const char *s, int len, ColourAllocated fore, ColourAllocated back){ Q_ASSERT(painter); SetClip(rc); DrawTextNoClip(rc, font_, ybase, s, len, fore, back); painter->setClipping(false);}void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, int ybase,const char *s, int len, ColourAllocated fore){ Q_ASSERT(painter); QString qs = convertText(s, len); QFont *f = PFont(font_.GetID()); if (f) painter->setFont(*f); painter->setPen(convertQColor(fore)); painter->drawText(rc.left, ybase, qs);}void SurfaceImpl::DrawXPM(PRectangle rc, const XPM *xpm){ Q_ASSERT(painter); int x, y; const QPixmap &qpm = xpm->Pixmap(); x = rc.left + (rc.Width() - qpm.width()) / 2; y = rc.top + (rc.Height() - qpm.height()) / 2; painter->drawPixmap(x, y, qpm);}void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len,int *positions){ QFontMetrics fm = metrics(font_); QString qs = convertText(s, len); int totalWidth = 0, ui = 0; for (int i = 0; i < qs.length(); ++i){ totalWidth += fm.width(qs[i]); int l = (unicodeMode ? QString(qs[i]).toUtf8().length() : 1); while (l--) positions[ui++] = totalWidth; }}int SurfaceImpl::WidthText(Font &font_, const char *s, int len){ QString qs = convertText(s, len); return metrics(font_).width(qs, qs.length());}int SurfaceImpl::WidthChar(Font &font_, char ch){ return metrics(font_).width(ch);}int SurfaceImpl::Ascent(Font &font_){ return metrics(font_).ascent();}int SurfaceImpl::Descent(Font &font_){ // Qt doesn't include the baseline in the descent, so add it. Note that // a descent from Qt4 always seems to be 2 pixels larger (irrespective of // font or size) than the same descent from Qt3. This means that text is a // little more spaced out with Qt4 - and will be more noticeable with // smaller fonts. return metrics(font_).descent() + 1;}int SurfaceImpl::ExternalLeading(Font &font_){ // Scintilla doesn't use this at the moment, which is good because Qt4 can // return a negative value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -