]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CallTip.cxx
f4bc5f83c42e0c93e00db9c0ea4ddd4f16e7e788
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"
16 static const int insetX
= 5; // text inset in x from calltip border
17 static const int widthArrow
= 14;
21 inCallTipMode
= false;
24 rectUp
= PRectangle(0,0,0,0);
25 rectDown
= PRectangle(0,0,0,0);
30 useStyleCallTip
= false; // for backwards compatibility
32 colourBG
.desired
= ColourDesired(0xff, 0xff, 0xff);
33 colourUnSel
.desired
= ColourDesired(0x80, 0x80, 0x80);
34 colourSel
.desired
= ColourDesired(0, 0, 0x80);
35 colourShade
.desired
= ColourDesired(0, 0, 0);
36 colourLight
.desired
= ColourDesired(0xc0, 0xc0, 0xc0);
46 void CallTip::RefreshColourPalette(Palette
&pal
, bool want
) {
47 pal
.WantFind(colourBG
, want
);
48 pal
.WantFind(colourUnSel
, want
);
49 pal
.WantFind(colourSel
, want
);
50 pal
.WantFind(colourShade
, want
);
51 pal
.WantFind(colourLight
, want
);
54 // Although this test includes 0, we should never see a \0 character.
55 static bool IsArrowCharacter(char ch
) {
56 return (ch
== 0) || (ch
== '\001') || (ch
== '\002');
59 // We ignore tabs unless a tab width has been set.
60 bool CallTip::IsTabCharacter(char ch
) {
61 return (tabSize
> 0) && (ch
== '\t');
64 int CallTip::NextTabPos(int x
) {
65 if (tabSize
> 0) { // paranoia... not called unless this is true
66 x
-= insetX
; // position relative to text
67 x
= (x
+ tabSize
) / tabSize
; // tab "number"
68 return tabSize
*x
+ insetX
; // position of next tab
70 return x
+ 1; // arbitrary
74 // Draw a section of the call tip that does not include \n in one colour.
75 // The text may include up to numEnds tabs or arrow characters.
76 void CallTip::DrawChunk(Surface
*surface
, int &x
, const char *s
,
77 int posStart
, int posEnd
, int ytext
, PRectangle rcClient
,
78 bool highlight
, bool draw
) {
80 int len
= posEnd
- posStart
;
82 // Divide the text into sections that are all text, or that are
83 // single arrows or single tab characters (if tabSize > 0).
85 const int numEnds
= 10;
86 int ends
[numEnds
+ 2];
87 for (int i
=0;i
<len
;i
++) {
88 if ((maxEnd
< numEnds
) &&
89 (IsArrowCharacter(s
[i
]) || IsTabCharacter(s
[i
])) ) {
98 for (int seg
= 0; seg
<maxEnd
; seg
++) {
99 int endSeg
= ends
[seg
];
100 if (endSeg
> startSeg
) {
101 if (IsArrowCharacter(s
[startSeg
])) {
102 bool upArrow
= s
[startSeg
] == '\001';
104 rcClient
.right
= rcClient
.left
+ widthArrow
;
106 const int halfWidth
= widthArrow
/ 2 - 3;
107 const int centreX
= rcClient
.left
+ widthArrow
/ 2 - 1;
108 const int centreY
= (rcClient
.top
+ rcClient
.bottom
) / 2;
109 surface
->FillRectangle(rcClient
, colourBG
.allocated
);
110 PRectangle
rcClientInner(rcClient
.left
+ 1, rcClient
.top
+ 1,
111 rcClient
.right
- 2, rcClient
.bottom
- 1);
112 surface
->FillRectangle(rcClientInner
, colourUnSel
.allocated
);
114 if (upArrow
) { // Up arrow
116 Point(centreX
- halfWidth
, centreY
+ halfWidth
/ 2),
117 Point(centreX
+ halfWidth
, centreY
+ halfWidth
/ 2),
118 Point(centreX
, centreY
- halfWidth
+ halfWidth
/ 2),
120 surface
->Polygon(pts
, sizeof(pts
) / sizeof(pts
[0]),
121 colourBG
.allocated
, colourBG
.allocated
);
122 } else { // Down arrow
124 Point(centreX
- halfWidth
, centreY
- halfWidth
/ 2),
125 Point(centreX
+ halfWidth
, centreY
- halfWidth
/ 2),
126 Point(centreX
, centreY
+ halfWidth
- halfWidth
/ 2),
128 surface
->Polygon(pts
, sizeof(pts
) / sizeof(pts
[0]),
129 colourBG
.allocated
, colourBG
.allocated
);
132 xEnd
= rcClient
.right
;
139 } else if (IsTabCharacter(s
[startSeg
])) {
140 xEnd
= NextTabPos(x
);
142 xEnd
= x
+ surface
->WidthText(font
, s
+ startSeg
, endSeg
- startSeg
);
145 rcClient
.right
= xEnd
;
146 surface
->DrawTextTransparent(rcClient
, font
, ytext
,
147 s
+startSeg
, endSeg
- startSeg
,
148 highlight
? colourSel
.allocated
: colourUnSel
.allocated
);
157 int CallTip::PaintContents(Surface
*surfaceWindow
, bool draw
) {
158 PRectangle rcClientPos
= wCallTip
.GetClientPosition();
159 PRectangle
rcClientSize(0, 0, rcClientPos
.right
- rcClientPos
.left
,
160 rcClientPos
.bottom
- rcClientPos
.top
);
161 PRectangle
rcClient(1, 1, rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
163 // To make a nice small call tip window, it is only sized to fit most normal characters without accents
164 int ascent
= surfaceWindow
->Ascent(font
) - surfaceWindow
->InternalLeading(font
);
167 // Draw the definition in three parts: before highlight, highlighted, after highlight
168 int ytext
= rcClient
.top
+ ascent
+ 1;
169 rcClient
.bottom
= ytext
+ surfaceWindow
->Descent(font
) + 1;
170 char *chunkVal
= val
;
171 bool moreChunks
= true;
174 char *chunkEnd
= strchr(chunkVal
, '\n');
175 if (chunkEnd
== NULL
) {
176 chunkEnd
= chunkVal
+ strlen(chunkVal
);
179 int chunkOffset
= chunkVal
- val
;
180 int chunkLength
= chunkEnd
- chunkVal
;
181 int chunkEndOffset
= chunkOffset
+ chunkLength
;
182 int thisStartHighlight
= Platform::Maximum(startHighlight
, chunkOffset
);
183 thisStartHighlight
= Platform::Minimum(thisStartHighlight
, chunkEndOffset
);
184 thisStartHighlight
-= chunkOffset
;
185 int thisEndHighlight
= Platform::Maximum(endHighlight
, chunkOffset
);
186 thisEndHighlight
= Platform::Minimum(thisEndHighlight
, chunkEndOffset
);
187 thisEndHighlight
-= chunkOffset
;
188 rcClient
.top
= ytext
- ascent
- 1;
190 int x
= insetX
; // start each line at this inset
192 DrawChunk(surfaceWindow
, x
, chunkVal
, 0, thisStartHighlight
,
193 ytext
, rcClient
, false, draw
);
194 DrawChunk(surfaceWindow
, x
, chunkVal
, thisStartHighlight
, thisEndHighlight
,
195 ytext
, rcClient
, true, draw
);
196 DrawChunk(surfaceWindow
, x
, chunkVal
, thisEndHighlight
, chunkLength
,
197 ytext
, rcClient
, false, draw
);
199 chunkVal
= chunkEnd
+ 1;
201 rcClient
.bottom
+= lineHeight
;
202 maxWidth
= Platform::Maximum(maxWidth
, x
);
207 void CallTip::PaintCT(Surface
*surfaceWindow
) {
210 PRectangle rcClientPos
= wCallTip
.GetClientPosition();
211 PRectangle
rcClientSize(0, 0, rcClientPos
.right
- rcClientPos
.left
,
212 rcClientPos
.bottom
- rcClientPos
.top
);
213 PRectangle
rcClient(1, 1, rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
215 surfaceWindow
->FillRectangle(rcClient
, colourBG
.allocated
);
217 offsetMain
= insetX
; // initial alignment assuming no arrows
218 PaintContents(surfaceWindow
, true);
220 // Draw a raised border around the edges of the window
221 surfaceWindow
->MoveTo(0, rcClientSize
.bottom
- 1);
222 surfaceWindow
->PenColour(colourShade
.allocated
);
223 surfaceWindow
->LineTo(rcClientSize
.right
- 1, rcClientSize
.bottom
- 1);
224 surfaceWindow
->LineTo(rcClientSize
.right
- 1, 0);
225 surfaceWindow
->PenColour(colourLight
.allocated
);
226 surfaceWindow
->LineTo(0, 0);
227 surfaceWindow
->LineTo(0, rcClientSize
.bottom
- 1);
230 void CallTip::MouseClick(Point pt
) {
232 if (rectUp
.Contains(pt
))
234 if (rectDown
.Contains(pt
))
238 PRectangle
CallTip::CallTipStart(int pos
, Point pt
, const char *defn
,
239 const char *faceName
, int size
,
240 int codePage_
, int characterSet
, Window
&wParent
) {
244 val
= new char[strlen(defn
) + 1];
248 codePage
= codePage_
;
249 Surface
*surfaceMeasure
= Surface::Allocate();
252 surfaceMeasure
->Init(wParent
.GetID());
253 surfaceMeasure
->SetUnicodeMode(SC_CP_UTF8
== codePage
);
254 surfaceMeasure
->SetDBCSMode(codePage
);
257 inCallTipMode
= true;
258 posStartCallTip
= pos
;
259 int deviceHeight
= surfaceMeasure
->DeviceHeightFont(size
);
260 font
.Create(faceName
, characterSet
, deviceHeight
, false, false);
261 // Look for multiple lines in the text
262 // Only support \n here - simply means container must avoid \r!
265 const char *look
= val
;
266 rectUp
= PRectangle(0,0,0,0);
267 rectDown
= PRectangle(0,0,0,0);
268 offsetMain
= insetX
; // changed to right edge of any arrows
269 int width
= PaintContents(surfaceMeasure
, false) + insetX
;
270 while ((newline
= strchr(look
, '\n')) != NULL
) {
274 lineHeight
= surfaceMeasure
->Height(font
);
276 // Extra line for border and an empty line at top and bottom. The returned
277 // rectangle is aligned to the right edge of the last arrow encountered in
278 // the tip text, else to the tip text left edge.
279 int height
= lineHeight
* numLines
- surfaceMeasure
->InternalLeading(font
) + 2 + 2;
280 delete surfaceMeasure
;
281 return PRectangle(pt
.x
- offsetMain
, pt
.y
+ 1, pt
.x
+ width
- offsetMain
, pt
.y
+ 1 + height
);
284 void CallTip::CallTipCancel() {
285 inCallTipMode
= false;
286 if (wCallTip
.Created()) {
291 void CallTip::SetHighlight(int start
, int end
) {
292 // Avoid flashing by checking something has really changed
293 if ((start
!= startHighlight
) || (end
!= endHighlight
)) {
294 startHighlight
= start
;
296 if (wCallTip
.Created()) {
297 wCallTip
.InvalidateAll();
302 // Set the tab size (sizes > 0 enable the use of tabs). This also enables the
303 // use of the STYLE_CALLTIP.
304 void CallTip::SetTabSize(int tabSz
) {
306 useStyleCallTip
= true;
309 // It might be better to have two access functions for this and to use
310 // them for all settings of colours.
311 void CallTip::SetForeBack(const ColourPair
&fore
, const ColourPair
&back
) {