1 // Scintilla source code edit control
3 ** Code for displaying call tips.
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
13 #include "Scintilla.h"
18 using namespace Scintilla
;
21 static const int insetX
= 5; // text inset in x from calltip border
22 static const int widthArrow
= 14;
26 inCallTipMode
= false;
29 rectUp
= PRectangle(0,0,0,0);
30 rectDown
= PRectangle(0,0,0,0);
37 useStyleCallTip
= false; // for backwards compatibility
40 // proper apple colours for the default
41 colourBG
= ColourDesired(0xff, 0xff, 0xc6);
42 colourUnSel
= ColourDesired(0, 0, 0);
44 colourBG
= ColourDesired(0xff, 0xff, 0xff);
45 colourUnSel
= ColourDesired(0x80, 0x80, 0x80);
47 colourSel
= ColourDesired(0, 0, 0x80);
48 colourShade
= ColourDesired(0, 0, 0);
49 colourLight
= ColourDesired(0xc0, 0xc0, 0xc0);
61 // Although this test includes 0, we should never see a \0 character.
62 static bool IsArrowCharacter(char ch
) {
63 return (ch
== 0) || (ch
== '\001') || (ch
== '\002');
66 // We ignore tabs unless a tab width has been set.
67 bool CallTip::IsTabCharacter(char ch
) const {
68 return (tabSize
> 0) && (ch
== '\t');
71 int CallTip::NextTabPos(int x
) {
72 if (tabSize
> 0) { // paranoia... not called unless this is true
73 x
-= insetX
; // position relative to text
74 x
= (x
+ tabSize
) / tabSize
; // tab "number"
75 return tabSize
*x
+ insetX
; // position of next tab
77 return x
+ 1; // arbitrary
81 // Draw a section of the call tip that does not include \n in one colour.
82 // The text may include up to numEnds tabs or arrow characters.
83 void CallTip::DrawChunk(Surface
*surface
, int &x
, const char *s
,
84 int posStart
, int posEnd
, int ytext
, PRectangle rcClient
,
85 bool highlight
, bool draw
) {
87 int len
= posEnd
- posStart
;
89 // Divide the text into sections that are all text, or that are
90 // single arrows or single tab characters (if tabSize > 0).
92 const int numEnds
= 10;
93 int ends
[numEnds
+ 2];
94 for (int i
=0; i
<len
; i
++) {
95 if ((maxEnd
< numEnds
) &&
96 (IsArrowCharacter(s
[i
]) || IsTabCharacter(s
[i
]))) {
102 ends
[maxEnd
++] = len
;
105 for (int seg
= 0; seg
<maxEnd
; seg
++) {
106 int endSeg
= ends
[seg
];
107 if (endSeg
> startSeg
) {
108 if (IsArrowCharacter(s
[startSeg
])) {
109 bool upArrow
= s
[startSeg
] == '\001';
111 rcClient
.right
= rcClient
.left
+ widthArrow
;
113 const int halfWidth
= widthArrow
/ 2 - 3;
114 const int centreX
= rcClient
.left
+ widthArrow
/ 2 - 1;
115 const int centreY
= (rcClient
.top
+ rcClient
.bottom
) / 2;
116 surface
->FillRectangle(rcClient
, colourBG
);
117 PRectangle
rcClientInner(rcClient
.left
+ 1, rcClient
.top
+ 1,
118 rcClient
.right
- 2, rcClient
.bottom
- 1);
119 surface
->FillRectangle(rcClientInner
, colourUnSel
);
121 if (upArrow
) { // Up arrow
123 Point(centreX
- halfWidth
, centreY
+ halfWidth
/ 2),
124 Point(centreX
+ halfWidth
, centreY
+ halfWidth
/ 2),
125 Point(centreX
, centreY
- halfWidth
+ halfWidth
/ 2),
127 surface
->Polygon(pts
, sizeof(pts
) / sizeof(pts
[0]),
129 } else { // Down arrow
131 Point(centreX
- halfWidth
, centreY
- halfWidth
/ 2),
132 Point(centreX
+ halfWidth
, centreY
- halfWidth
/ 2),
133 Point(centreX
, centreY
+ halfWidth
- halfWidth
/ 2),
135 surface
->Polygon(pts
, sizeof(pts
) / sizeof(pts
[0]),
139 xEnd
= rcClient
.right
;
146 } else if (IsTabCharacter(s
[startSeg
])) {
147 xEnd
= NextTabPos(x
);
149 xEnd
= x
+ surface
->WidthText(font
, s
+ startSeg
, endSeg
- startSeg
);
152 rcClient
.right
= xEnd
;
153 surface
->DrawTextTransparent(rcClient
, font
, ytext
,
154 s
+startSeg
, endSeg
- startSeg
,
155 highlight
? colourSel
: colourUnSel
);
164 int CallTip::PaintContents(Surface
*surfaceWindow
, bool draw
) {
165 PRectangle rcClientPos
= wCallTip
.GetClientPosition();
166 PRectangle
rcClientSize(0, 0, rcClientPos
.right
- rcClientPos
.left
,
167 rcClientPos
.bottom
- rcClientPos
.top
);
168 PRectangle
rcClient(1, 1, rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
170 // To make a nice small call tip window, it is only sized to fit most normal characters without accents
171 int ascent
= surfaceWindow
->Ascent(font
) - surfaceWindow
->InternalLeading(font
);
174 // Draw the definition in three parts: before highlight, highlighted, after highlight
175 int ytext
= rcClient
.top
+ ascent
+ 1;
176 rcClient
.bottom
= ytext
+ surfaceWindow
->Descent(font
) + 1;
177 char *chunkVal
= val
;
178 bool moreChunks
= true;
182 char *chunkEnd
= strchr(chunkVal
, '\n');
183 if (chunkEnd
== NULL
) {
184 chunkEnd
= chunkVal
+ strlen(chunkVal
);
187 int chunkOffset
= chunkVal
- val
;
188 int chunkLength
= chunkEnd
- chunkVal
;
189 int chunkEndOffset
= chunkOffset
+ chunkLength
;
190 int thisStartHighlight
= Platform::Maximum(startHighlight
, chunkOffset
);
191 thisStartHighlight
= Platform::Minimum(thisStartHighlight
, chunkEndOffset
);
192 thisStartHighlight
-= chunkOffset
;
193 int thisEndHighlight
= Platform::Maximum(endHighlight
, chunkOffset
);
194 thisEndHighlight
= Platform::Minimum(thisEndHighlight
, chunkEndOffset
);
195 thisEndHighlight
-= chunkOffset
;
196 rcClient
.top
= ytext
- ascent
- 1;
198 int x
= insetX
; // start each line at this inset
200 DrawChunk(surfaceWindow
, x
, chunkVal
, 0, thisStartHighlight
,
201 ytext
, rcClient
, false, draw
);
202 DrawChunk(surfaceWindow
, x
, chunkVal
, thisStartHighlight
, thisEndHighlight
,
203 ytext
, rcClient
, true, draw
);
204 DrawChunk(surfaceWindow
, x
, chunkVal
, thisEndHighlight
, chunkLength
,
205 ytext
, rcClient
, false, draw
);
207 chunkVal
= chunkEnd
+ 1;
209 rcClient
.bottom
+= lineHeight
;
210 maxWidth
= Platform::Maximum(maxWidth
, x
);
215 void CallTip::PaintCT(Surface
*surfaceWindow
) {
218 PRectangle rcClientPos
= wCallTip
.GetClientPosition();
219 PRectangle
rcClientSize(0, 0, rcClientPos
.right
- rcClientPos
.left
,
220 rcClientPos
.bottom
- rcClientPos
.top
);
221 PRectangle
rcClient(1, 1, rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
223 surfaceWindow
->FillRectangle(rcClient
, colourBG
);
225 offsetMain
= insetX
; // initial alignment assuming no arrows
226 PaintContents(surfaceWindow
, true);
229 // OSX doesn't put borders on "help tags"
230 // Draw a raised border around the edges of the window
231 surfaceWindow
->MoveTo(0, rcClientSize
.bottom
- 1);
232 surfaceWindow
->PenColour(colourShade
);
233 surfaceWindow
->LineTo(rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
234 surfaceWindow
->LineTo(rcClientSize
.right
- 1, 0);
235 surfaceWindow
->PenColour(colourLight
);
236 surfaceWindow
->LineTo(0, 0);
237 surfaceWindow
->LineTo(0, rcClientSize
.bottom
- 1);
241 void CallTip::MouseClick(Point pt
) {
243 if (rectUp
.Contains(pt
))
245 if (rectDown
.Contains(pt
))
249 PRectangle
CallTip::CallTipStart(int pos
, Point pt
, int textHeight
, const char *defn
,
250 const char *faceName
, int size
,
251 int codePage_
, int characterSet
,
252 int technology
, Window
&wParent
) {
256 val
= new char[strlen(defn
) + 1];
258 codePage
= codePage_
;
259 Surface
*surfaceMeasure
= Surface::Allocate(technology
);
262 surfaceMeasure
->Init(wParent
.GetID());
263 surfaceMeasure
->SetUnicodeMode(SC_CP_UTF8
== codePage
);
264 surfaceMeasure
->SetDBCSMode(codePage
);
267 inCallTipMode
= true;
268 posStartCallTip
= pos
;
269 int deviceHeight
= surfaceMeasure
->DeviceHeightFont(size
);
270 FontParameters
fp(faceName
, deviceHeight
/ SC_FONT_SIZE_MULTIPLIER
, SC_WEIGHT_NORMAL
, false, 0, technology
, characterSet
);
272 // Look for multiple lines in the text
273 // Only support \n here - simply means container must avoid \r!
276 const char *look
= val
;
277 rectUp
= PRectangle(0,0,0,0);
278 rectDown
= PRectangle(0,0,0,0);
279 offsetMain
= insetX
; // changed to right edge of any arrows
280 int width
= PaintContents(surfaceMeasure
, false) + insetX
;
281 while ((newline
= strchr(look
, '\n')) != NULL
) {
285 lineHeight
= surfaceMeasure
->Height(font
);
287 // Extra line for border and an empty line at top and bottom. The returned
288 // rectangle is aligned to the right edge of the last arrow encountered in
289 // the tip text, else to the tip text left edge.
290 int height
= lineHeight
* numLines
- surfaceMeasure
->InternalLeading(font
) + 2 + 2;
291 delete surfaceMeasure
;
293 return PRectangle(pt
.x
- offsetMain
, pt
.y
- 1 - height
, pt
.x
+ width
- offsetMain
, pt
.y
- 1);
295 return PRectangle(pt
.x
- offsetMain
, pt
.y
+ 1 + textHeight
, pt
.x
+ width
- offsetMain
, pt
.y
+ 1 + textHeight
+ height
);
299 void CallTip::CallTipCancel() {
300 inCallTipMode
= false;
301 if (wCallTip
.Created()) {
306 void CallTip::SetHighlight(int start
, int end
) {
307 // Avoid flashing by checking something has really changed
308 if ((start
!= startHighlight
) || (end
!= endHighlight
)) {
309 startHighlight
= start
;
311 if (wCallTip
.Created()) {
312 wCallTip
.InvalidateAll();
317 // Set the tab size (sizes > 0 enable the use of tabs). This also enables the
318 // use of the STYLE_CALLTIP.
319 void CallTip::SetTabSize(int tabSz
) {
321 useStyleCallTip
= true;
324 // Set the calltip position, below the text by default or if above is false
325 // else above the text.
326 void CallTip::SetPosition(bool aboveText
) {
330 // It might be better to have two access functions for this and to use
331 // them for all settings of colours.
332 void CallTip::SetForeBack(const ColourDesired
&fore
, const ColourDesired
&back
) {