2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file CallTip.cxx
|
|
|
|
** Code for displaying call tips.
|
|
|
|
**/
|
|
|
|
// Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-28 00:44:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <string>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
#include "Platform.h"
|
|
|
|
|
|
|
|
#include "Scintilla.h"
|
2015-06-07 21:19:26 +00:00
|
|
|
|
|
|
|
#include "StringCopy.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "CallTip.h"
|
|
|
|
|
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
using namespace Scintilla;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CallTip::CallTip() {
|
|
|
|
wCallTip = 0;
|
|
|
|
inCallTipMode = false;
|
|
|
|
posStartCallTip = 0;
|
|
|
|
rectUp = PRectangle(0,0,0,0);
|
|
|
|
rectDown = PRectangle(0,0,0,0);
|
|
|
|
lineHeight = 1;
|
2011-03-22 00:16:49 +00:00
|
|
|
offsetMain = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
startHighlight = 0;
|
|
|
|
endHighlight = 0;
|
|
|
|
tabSize = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
above = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
useStyleCallTip = false; // for backwards compatibility
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
insetX = 5;
|
|
|
|
widthArrow = 14;
|
|
|
|
borderHeight = 2; // Extra line for border and an empty line at top and bottom.
|
|
|
|
verticalOffset = 1;
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// proper apple colours for the default
|
2013-08-28 00:44:27 +00:00
|
|
|
colourBG = ColourDesired(0xff, 0xff, 0xc6);
|
|
|
|
colourUnSel = ColourDesired(0, 0, 0);
|
2009-04-24 23:35:41 +00:00
|
|
|
#else
|
2013-08-28 00:44:27 +00:00
|
|
|
colourBG = ColourDesired(0xff, 0xff, 0xff);
|
|
|
|
colourUnSel = ColourDesired(0x80, 0x80, 0x80);
|
2009-04-24 23:35:41 +00:00
|
|
|
#endif
|
2013-08-28 00:44:27 +00:00
|
|
|
colourSel = ColourDesired(0, 0, 0x80);
|
|
|
|
colourShade = ColourDesired(0, 0, 0);
|
|
|
|
colourLight = ColourDesired(0xc0, 0xc0, 0xc0);
|
2011-03-22 00:16:49 +00:00
|
|
|
codePage = 0;
|
|
|
|
clickPlace = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallTip::~CallTip() {
|
|
|
|
font.Release();
|
|
|
|
wCallTip.Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Although this test includes 0, we should never see a \0 character.
|
|
|
|
static bool IsArrowCharacter(char ch) {
|
|
|
|
return (ch == 0) || (ch == '\001') || (ch == '\002');
|
|
|
|
}
|
|
|
|
|
|
|
|
// We ignore tabs unless a tab width has been set.
|
2010-07-12 22:19:51 +00:00
|
|
|
bool CallTip::IsTabCharacter(char ch) const {
|
2009-04-24 23:35:41 +00:00
|
|
|
return (tabSize > 0) && (ch == '\t');
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
int CallTip::NextTabPos(int x) const {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (tabSize > 0) { // paranoia... not called unless this is true
|
|
|
|
x -= insetX; // position relative to text
|
|
|
|
x = (x + tabSize) / tabSize; // tab "number"
|
|
|
|
return tabSize*x + insetX; // position of next tab
|
|
|
|
} else {
|
|
|
|
return x + 1; // arbitrary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a section of the call tip that does not include \n in one colour.
|
|
|
|
// The text may include up to numEnds tabs or arrow characters.
|
|
|
|
void CallTip::DrawChunk(Surface *surface, int &x, const char *s,
|
|
|
|
int posStart, int posEnd, int ytext, PRectangle rcClient,
|
|
|
|
bool highlight, bool draw) {
|
|
|
|
s += posStart;
|
|
|
|
int len = posEnd - posStart;
|
|
|
|
|
|
|
|
// Divide the text into sections that are all text, or that are
|
|
|
|
// single arrows or single tab characters (if tabSize > 0).
|
|
|
|
int maxEnd = 0;
|
|
|
|
const int numEnds = 10;
|
|
|
|
int ends[numEnds + 2];
|
2010-07-12 22:19:51 +00:00
|
|
|
for (int i=0; i<len; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if ((maxEnd < numEnds) &&
|
2011-07-17 22:30:49 +00:00
|
|
|
(IsArrowCharacter(s[i]) || IsTabCharacter(s[i]))) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (i > 0)
|
|
|
|
ends[maxEnd++] = i;
|
|
|
|
ends[maxEnd++] = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ends[maxEnd++] = len;
|
|
|
|
int startSeg = 0;
|
|
|
|
int xEnd;
|
|
|
|
for (int seg = 0; seg<maxEnd; seg++) {
|
|
|
|
int endSeg = ends[seg];
|
|
|
|
if (endSeg > startSeg) {
|
|
|
|
if (IsArrowCharacter(s[startSeg])) {
|
2015-06-07 21:19:26 +00:00
|
|
|
xEnd = x + widthArrow;
|
2009-04-24 23:35:41 +00:00
|
|
|
bool upArrow = s[startSeg] == '\001';
|
2015-06-07 21:19:26 +00:00
|
|
|
rcClient.left = static_cast<XYPOSITION>(x);
|
|
|
|
rcClient.right = static_cast<XYPOSITION>(xEnd);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (draw) {
|
|
|
|
const int halfWidth = widthArrow / 2 - 3;
|
2015-06-07 21:19:26 +00:00
|
|
|
const int quarterWidth = halfWidth / 2;
|
|
|
|
const int centreX = x + widthArrow / 2 - 1;
|
|
|
|
const int centreY = static_cast<int>(rcClient.top + rcClient.bottom) / 2;
|
2013-08-28 00:44:27 +00:00
|
|
|
surface->FillRectangle(rcClient, colourBG);
|
2009-04-24 23:35:41 +00:00
|
|
|
PRectangle rcClientInner(rcClient.left + 1, rcClient.top + 1,
|
|
|
|
rcClient.right - 2, rcClient.bottom - 1);
|
2013-08-28 00:44:27 +00:00
|
|
|
surface->FillRectangle(rcClientInner, colourUnSel);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
if (upArrow) { // Up arrow
|
|
|
|
Point pts[] = {
|
2015-06-07 21:19:26 +00:00
|
|
|
Point::FromInts(centreX - halfWidth, centreY + quarterWidth),
|
|
|
|
Point::FromInts(centreX + halfWidth, centreY + quarterWidth),
|
|
|
|
Point::FromInts(centreX, centreY - halfWidth + quarterWidth),
|
2009-04-24 23:35:41 +00:00
|
|
|
};
|
2015-06-07 21:19:26 +00:00
|
|
|
surface->Polygon(pts, ELEMENTS(pts), colourBG, colourBG);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else { // Down arrow
|
|
|
|
Point pts[] = {
|
2015-06-07 21:19:26 +00:00
|
|
|
Point::FromInts(centreX - halfWidth, centreY - quarterWidth),
|
|
|
|
Point::FromInts(centreX + halfWidth, centreY - quarterWidth),
|
|
|
|
Point::FromInts(centreX, centreY + halfWidth - quarterWidth),
|
2009-04-24 23:35:41 +00:00
|
|
|
};
|
2015-06-07 21:19:26 +00:00
|
|
|
surface->Polygon(pts, ELEMENTS(pts), colourBG, colourBG);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
offsetMain = xEnd;
|
|
|
|
if (upArrow) {
|
|
|
|
rectUp = rcClient;
|
|
|
|
} else {
|
|
|
|
rectDown = rcClient;
|
|
|
|
}
|
|
|
|
} else if (IsTabCharacter(s[startSeg])) {
|
|
|
|
xEnd = NextTabPos(x);
|
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
xEnd = x + RoundXYPosition(surface->WidthText(font, s + startSeg, endSeg - startSeg));
|
2009-04-24 23:35:41 +00:00
|
|
|
if (draw) {
|
2015-06-07 21:19:26 +00:00
|
|
|
rcClient.left = static_cast<XYPOSITION>(x);
|
|
|
|
rcClient.right = static_cast<XYPOSITION>(xEnd);
|
|
|
|
surface->DrawTextTransparent(rcClient, font, static_cast<XYPOSITION>(ytext),
|
2009-04-24 23:35:41 +00:00
|
|
|
s+startSeg, endSeg - startSeg,
|
2013-08-28 00:44:27 +00:00
|
|
|
highlight ? colourSel : colourUnSel);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
x = xEnd;
|
|
|
|
startSeg = endSeg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CallTip::PaintContents(Surface *surfaceWindow, bool draw) {
|
|
|
|
PRectangle rcClientPos = wCallTip.GetClientPosition();
|
2015-06-07 21:19:26 +00:00
|
|
|
PRectangle rcClientSize(0.0f, 0.0f, rcClientPos.right - rcClientPos.left,
|
2009-04-24 23:35:41 +00:00
|
|
|
rcClientPos.bottom - rcClientPos.top);
|
2015-06-07 21:19:26 +00:00
|
|
|
PRectangle rcClient(1.0f, 1.0f, rcClientSize.right - 1, rcClientSize.bottom - 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
// To make a nice small call tip window, it is only sized to fit most normal characters without accents
|
2015-06-07 21:19:26 +00:00
|
|
|
int ascent = RoundXYPosition(surfaceWindow->Ascent(font) - surfaceWindow->InternalLeading(font));
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
// For each line...
|
|
|
|
// Draw the definition in three parts: before highlight, highlighted, after highlight
|
2015-06-07 21:19:26 +00:00
|
|
|
int ytext = static_cast<int>(rcClient.top) + ascent + 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
rcClient.bottom = ytext + surfaceWindow->Descent(font) + 1;
|
2013-08-28 00:44:27 +00:00
|
|
|
const char *chunkVal = val.c_str();
|
2009-04-24 23:35:41 +00:00
|
|
|
bool moreChunks = true;
|
|
|
|
int maxWidth = 0;
|
|
|
|
|
|
|
|
while (moreChunks) {
|
2013-08-28 00:44:27 +00:00
|
|
|
const char *chunkEnd = strchr(chunkVal, '\n');
|
2009-04-24 23:35:41 +00:00
|
|
|
if (chunkEnd == NULL) {
|
|
|
|
chunkEnd = chunkVal + strlen(chunkVal);
|
|
|
|
moreChunks = false;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
int chunkOffset = static_cast<int>(chunkVal - val.c_str());
|
|
|
|
int chunkLength = static_cast<int>(chunkEnd - chunkVal);
|
2009-04-24 23:35:41 +00:00
|
|
|
int chunkEndOffset = chunkOffset + chunkLength;
|
|
|
|
int thisStartHighlight = Platform::Maximum(startHighlight, chunkOffset);
|
|
|
|
thisStartHighlight = Platform::Minimum(thisStartHighlight, chunkEndOffset);
|
|
|
|
thisStartHighlight -= chunkOffset;
|
|
|
|
int thisEndHighlight = Platform::Maximum(endHighlight, chunkOffset);
|
|
|
|
thisEndHighlight = Platform::Minimum(thisEndHighlight, chunkEndOffset);
|
|
|
|
thisEndHighlight -= chunkOffset;
|
2015-06-07 21:19:26 +00:00
|
|
|
rcClient.top = static_cast<XYPOSITION>(ytext - ascent - 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
int x = insetX; // start each line at this inset
|
|
|
|
|
|
|
|
DrawChunk(surfaceWindow, x, chunkVal, 0, thisStartHighlight,
|
|
|
|
ytext, rcClient, false, draw);
|
|
|
|
DrawChunk(surfaceWindow, x, chunkVal, thisStartHighlight, thisEndHighlight,
|
|
|
|
ytext, rcClient, true, draw);
|
|
|
|
DrawChunk(surfaceWindow, x, chunkVal, thisEndHighlight, chunkLength,
|
|
|
|
ytext, rcClient, false, draw);
|
|
|
|
|
|
|
|
chunkVal = chunkEnd + 1;
|
|
|
|
ytext += lineHeight;
|
|
|
|
rcClient.bottom += lineHeight;
|
|
|
|
maxWidth = Platform::Maximum(maxWidth, x);
|
|
|
|
}
|
|
|
|
return maxWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallTip::PaintCT(Surface *surfaceWindow) {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (val.empty())
|
2009-04-24 23:35:41 +00:00
|
|
|
return;
|
|
|
|
PRectangle rcClientPos = wCallTip.GetClientPosition();
|
2015-06-07 21:19:26 +00:00
|
|
|
PRectangle rcClientSize(0.0f, 0.0f, rcClientPos.right - rcClientPos.left,
|
2009-04-24 23:35:41 +00:00
|
|
|
rcClientPos.bottom - rcClientPos.top);
|
2015-06-07 21:19:26 +00:00
|
|
|
PRectangle rcClient(1.0f, 1.0f, rcClientSize.right - 1, rcClientSize.bottom - 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
surfaceWindow->FillRectangle(rcClient, colourBG);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
offsetMain = insetX; // initial alignment assuming no arrows
|
|
|
|
PaintContents(surfaceWindow, true);
|
|
|
|
|
|
|
|
#ifndef __APPLE__
|
|
|
|
// OSX doesn't put borders on "help tags"
|
|
|
|
// Draw a raised border around the edges of the window
|
2015-06-07 21:19:26 +00:00
|
|
|
surfaceWindow->MoveTo(0, static_cast<int>(rcClientSize.bottom) - 1);
|
2013-08-28 00:44:27 +00:00
|
|
|
surfaceWindow->PenColour(colourShade);
|
2015-06-07 21:19:26 +00:00
|
|
|
surfaceWindow->LineTo(static_cast<int>(rcClientSize.right) - 1, static_cast<int>(rcClientSize.bottom) - 1);
|
|
|
|
surfaceWindow->LineTo(static_cast<int>(rcClientSize.right) - 1, 0);
|
2013-08-28 00:44:27 +00:00
|
|
|
surfaceWindow->PenColour(colourLight);
|
2009-04-24 23:35:41 +00:00
|
|
|
surfaceWindow->LineTo(0, 0);
|
2015-06-07 21:19:26 +00:00
|
|
|
surfaceWindow->LineTo(0, static_cast<int>(rcClientSize.bottom) - 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallTip::MouseClick(Point pt) {
|
|
|
|
clickPlace = 0;
|
|
|
|
if (rectUp.Contains(pt))
|
|
|
|
clickPlace = 1;
|
|
|
|
if (rectDown.Contains(pt))
|
|
|
|
clickPlace = 2;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
PRectangle CallTip::CallTipStart(int pos, Point pt, int textHeight, const char *defn,
|
2009-04-24 23:35:41 +00:00
|
|
|
const char *faceName, int size,
|
2013-08-28 00:44:27 +00:00
|
|
|
int codePage_, int characterSet,
|
|
|
|
int technology, Window &wParent) {
|
2009-04-24 23:35:41 +00:00
|
|
|
clickPlace = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
val = defn;
|
2009-04-24 23:35:41 +00:00
|
|
|
codePage = codePage_;
|
2013-08-28 00:44:27 +00:00
|
|
|
Surface *surfaceMeasure = Surface::Allocate(technology);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (!surfaceMeasure)
|
|
|
|
return PRectangle();
|
|
|
|
surfaceMeasure->Init(wParent.GetID());
|
|
|
|
surfaceMeasure->SetUnicodeMode(SC_CP_UTF8 == codePage);
|
|
|
|
surfaceMeasure->SetDBCSMode(codePage);
|
|
|
|
startHighlight = 0;
|
|
|
|
endHighlight = 0;
|
|
|
|
inCallTipMode = true;
|
|
|
|
posStartCallTip = pos;
|
2015-06-07 21:19:26 +00:00
|
|
|
XYPOSITION deviceHeight = static_cast<XYPOSITION>(surfaceMeasure->DeviceHeightFont(size));
|
2013-08-28 00:44:27 +00:00
|
|
|
FontParameters fp(faceName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, SC_WEIGHT_NORMAL, false, 0, technology, characterSet);
|
|
|
|
font.Create(fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
// Look for multiple lines in the text
|
|
|
|
// Only support \n here - simply means container must avoid \r!
|
|
|
|
int numLines = 1;
|
|
|
|
const char *newline;
|
2013-08-28 00:44:27 +00:00
|
|
|
const char *look = val.c_str();
|
2009-04-24 23:35:41 +00:00
|
|
|
rectUp = PRectangle(0,0,0,0);
|
|
|
|
rectDown = PRectangle(0,0,0,0);
|
|
|
|
offsetMain = insetX; // changed to right edge of any arrows
|
|
|
|
int width = PaintContents(surfaceMeasure, false) + insetX;
|
|
|
|
while ((newline = strchr(look, '\n')) != NULL) {
|
|
|
|
look = newline + 1;
|
|
|
|
numLines++;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
lineHeight = RoundXYPosition(surfaceMeasure->Height(font));
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// The returned
|
2009-04-24 23:35:41 +00:00
|
|
|
// rectangle is aligned to the right edge of the last arrow encountered in
|
|
|
|
// the tip text, else to the tip text left edge.
|
2015-06-07 21:19:26 +00:00
|
|
|
int height = lineHeight * numLines - static_cast<int>(surfaceMeasure->InternalLeading(font)) + borderHeight * 2;
|
2009-04-24 23:35:41 +00:00
|
|
|
delete surfaceMeasure;
|
2013-08-28 00:44:27 +00:00
|
|
|
if (above) {
|
|
|
|
return PRectangle(pt.x - offsetMain, pt.y - verticalOffset - height, pt.x + width - offsetMain, pt.y - verticalOffset);
|
|
|
|
} else {
|
|
|
|
return PRectangle(pt.x - offsetMain, pt.y + verticalOffset + textHeight, pt.x + width - offsetMain, pt.y + verticalOffset + textHeight + height);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallTip::CallTipCancel() {
|
|
|
|
inCallTipMode = false;
|
|
|
|
if (wCallTip.Created()) {
|
|
|
|
wCallTip.Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallTip::SetHighlight(int start, int end) {
|
|
|
|
// Avoid flashing by checking something has really changed
|
|
|
|
if ((start != startHighlight) || (end != endHighlight)) {
|
|
|
|
startHighlight = start;
|
2013-08-28 00:44:27 +00:00
|
|
|
endHighlight = (end > start) ? end : start;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (wCallTip.Created()) {
|
|
|
|
wCallTip.InvalidateAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the tab size (sizes > 0 enable the use of tabs). This also enables the
|
|
|
|
// use of the STYLE_CALLTIP.
|
|
|
|
void CallTip::SetTabSize(int tabSz) {
|
|
|
|
tabSize = tabSz;
|
|
|
|
useStyleCallTip = true;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// Set the calltip position, below the text by default or if above is false
|
|
|
|
// else above the text.
|
|
|
|
void CallTip::SetPosition(bool aboveText) {
|
|
|
|
above = aboveText;
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
// It might be better to have two access functions for this and to use
|
|
|
|
// them for all settings of colours.
|
2013-08-28 00:44:27 +00:00
|
|
|
void CallTip::SetForeBack(const ColourDesired &fore, const ColourDesired &back) {
|
2009-04-24 23:35:41 +00:00
|
|
|
colourBG = back;
|
|
|
|
colourUnSel = fore;
|
|
|
|
}
|