]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CallTip.cxx
fixed DLL building bug
[wxWidgets.git] / src / stc / scintilla / src / CallTip.cxx
1 // Scintilla source code edit control
2 /** @file CallTip.cxx
3 ** Code for displaying call tips.
4 **/
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.
7
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "Platform.h"
12
13 #include "Scintilla.h"
14 #include "CallTip.h"
15
16 CallTip::CallTip() {
17 wCallTip = 0;
18 inCallTipMode = false;
19 posStartCallTip = 0;
20 val = 0;
21 xUp = -100;
22 xDown = -100;
23 lineHeight = 1;
24 startHighlight = 0;
25 endHighlight = 0;
26
27 colourBG.desired = ColourDesired(0xff, 0xff, 0xff);
28 colourUnSel.desired = ColourDesired(0x80, 0x80, 0x80);
29 colourSel.desired = ColourDesired(0, 0, 0x80);
30 colourShade.desired = ColourDesired(0, 0, 0);
31 colourLight.desired = ColourDesired(0xc0, 0xc0, 0xc0);
32 }
33
34 CallTip::~CallTip() {
35 font.Release();
36 wCallTip.Destroy();
37 delete []val;
38 val = 0;
39 }
40
41 const int widthArrow = 14;
42
43 void CallTip::RefreshColourPalette(Palette &pal, bool want) {
44 pal.WantFind(colourBG, want);
45 pal.WantFind(colourUnSel, want);
46 pal.WantFind(colourSel, want);
47 pal.WantFind(colourShade, want);
48 pal.WantFind(colourLight, want);
49 }
50
51 void CallTip::DrawChunk(Surface *surface, int &x, const char *s,
52 int posStart, int posEnd, int ytext, PRectangle rcClient,
53 bool highlight, bool draw) {
54 s += posStart;
55 int len = posEnd - posStart;
56 int maxEnd = 0;
57 int ends[10];
58 for (int i=0;i<len;i++) {
59 if (s[i] <= '\002') {
60 if (i > 0)
61 ends[maxEnd++] = i;
62 ends[maxEnd++] = i+1;
63 }
64 }
65 ends[maxEnd++] = len;
66 int startSeg = 0;
67 int xEnd;
68 for (int seg = 0; seg<maxEnd; seg++) {
69 int endSeg = ends[seg];
70 if (endSeg > startSeg) {
71 if (s[startSeg] <= '\002') {
72 xEnd = x + widthArrow;
73 offsetMain = xEnd;
74 if (draw) {
75 const int halfWidth = widthArrow / 2 - 3;
76 const int centreX = x + widthArrow / 2 - 1;
77 const int centreY = (rcClient.top + rcClient.bottom) / 2;
78 rcClient.left = x;
79 rcClient.right = xEnd;
80 surface->FillRectangle(rcClient, colourBG.allocated);
81 PRectangle rcClientInner(rcClient.left+1, rcClient.top+1, rcClient.right-2, rcClient.bottom-1);
82 surface->FillRectangle(rcClientInner, colourUnSel.allocated);
83
84 if (s[startSeg] == '\001') {
85 // Up arrow
86 Point pts[] = {
87 Point(centreX - halfWidth, centreY + halfWidth / 2),
88 Point(centreX + halfWidth, centreY + halfWidth / 2),
89 Point(centreX, centreY - halfWidth + halfWidth / 2),
90 };
91 surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
92 colourBG.allocated, colourBG.allocated);
93 } else {
94 // Down arrow
95 Point pts[] = {
96 Point(centreX - halfWidth, centreY - halfWidth / 2),
97 Point(centreX + halfWidth, centreY - halfWidth / 2),
98 Point(centreX, centreY + halfWidth - halfWidth / 2),
99 };
100 surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
101 colourBG.allocated, colourBG.allocated);
102 }
103 } else {
104 if (s[startSeg] == '\001') {
105 xUp = x+1;
106 } else {
107 xDown = x+1;
108 }
109 }
110 } else {
111 xEnd = x + surface->WidthText(font, s+startSeg, endSeg - startSeg);
112 if (draw) {
113 rcClient.left = x;
114 rcClient.right = xEnd;
115 surface->DrawTextNoClip(rcClient, font, ytext,
116 s+startSeg, endSeg - startSeg,
117 highlight ? colourSel.allocated : colourUnSel.allocated,
118 colourBG.allocated);
119 }
120 }
121 x = xEnd;
122 startSeg = endSeg;
123 }
124 }
125 }
126
127 int CallTip::PaintContents(Surface *surfaceWindow, bool draw) {
128 PRectangle rcClientPos = wCallTip.GetClientPosition();
129 PRectangle rcClientSize(0, 0, rcClientPos.right - rcClientPos.left,
130 rcClientPos.bottom - rcClientPos.top);
131 PRectangle rcClient(1, 1, rcClientSize.right - 1, rcClientSize.bottom - 1);
132
133 // To make a nice small call tip window, it is only sized to fit most normal characters without accents
134 int ascent = surfaceWindow->Ascent(font) - surfaceWindow->InternalLeading(font);
135
136 // For each line...
137 // Draw the definition in three parts: before highlight, highlighted, after highlight
138 int ytext = rcClient.top + ascent + 1;
139 rcClient.bottom = ytext + surfaceWindow->Descent(font) + 1;
140 char *chunkVal = val;
141 bool moreChunks = true;
142 int maxWidth = 0;
143 while (moreChunks) {
144 char *chunkEnd = strchr(chunkVal, '\n');
145 if (chunkEnd == NULL) {
146 chunkEnd = chunkVal + strlen(chunkVal);
147 moreChunks = false;
148 }
149 int chunkOffset = chunkVal - val;
150 int chunkLength = chunkEnd - chunkVal;
151 int chunkEndOffset = chunkOffset + chunkLength;
152 int thisStartHighlight = Platform::Maximum(startHighlight, chunkOffset);
153 thisStartHighlight = Platform::Minimum(thisStartHighlight, chunkEndOffset);
154 thisStartHighlight -= chunkOffset;
155 int thisEndHighlight = Platform::Maximum(endHighlight, chunkOffset);
156 thisEndHighlight = Platform::Minimum(thisEndHighlight, chunkEndOffset);
157 thisEndHighlight -= chunkOffset;
158 rcClient.top = ytext - ascent - 1;
159
160 int x = 5;
161
162 DrawChunk(surfaceWindow, x, chunkVal, 0, thisStartHighlight,
163 ytext, rcClient, false, draw);
164 DrawChunk(surfaceWindow, x, chunkVal, thisStartHighlight, thisEndHighlight,
165 ytext, rcClient, true, draw);
166 DrawChunk(surfaceWindow, x, chunkVal, thisEndHighlight, chunkLength,
167 ytext, rcClient, false, draw);
168
169 chunkVal = chunkEnd + 1;
170 ytext += lineHeight;
171 rcClient.bottom += lineHeight;
172 maxWidth = Platform::Maximum(maxWidth, x);
173 }
174 return maxWidth;
175 }
176
177 void CallTip::PaintCT(Surface *surfaceWindow) {
178 if (!val)
179 return;
180 PRectangle rcClientPos = wCallTip.GetClientPosition();
181 PRectangle rcClientSize(0, 0, rcClientPos.right - rcClientPos.left,
182 rcClientPos.bottom - rcClientPos.top);
183 PRectangle rcClient(1, 1, rcClientSize.right - 1, rcClientSize.bottom - 1);
184
185 surfaceWindow->FillRectangle(rcClient, colourBG.allocated);
186
187 offsetMain = 5;
188 PaintContents(surfaceWindow, true);
189
190 // Draw a raised border around the edges of the window
191 surfaceWindow->MoveTo(0, rcClientSize.bottom - 1);
192 surfaceWindow->PenColour(colourShade.allocated);
193 surfaceWindow->LineTo(rcClientSize.right - 1, rcClientSize.bottom - 1);
194 surfaceWindow->LineTo(rcClientSize.right - 1, 0);
195 surfaceWindow->PenColour(colourLight.allocated);
196 surfaceWindow->LineTo(0, 0);
197 surfaceWindow->LineTo(0, rcClientSize.bottom - 1);
198 }
199
200 void CallTip::MouseClick(Point pt) {
201 clickPlace = 0;
202 if (pt.y < lineHeight) {
203 if ((pt.x > xUp) && (pt.x < xUp + widthArrow - 2)) {
204 clickPlace = 1;
205 } else if ((pt.x > xDown) && (pt.x < xDown + widthArrow - 2)) {
206 clickPlace = 2;
207 }
208 }
209 }
210
211 PRectangle CallTip::CallTipStart(int pos, Point pt, const char *defn,
212 const char *faceName, int size,
213 int codePage_, Window &wParent) {
214 clickPlace = 0;
215 if (val)
216 delete []val;
217 val = new char[strlen(defn) + 1];
218 if (!val)
219 return PRectangle();
220 strcpy(val, defn);
221 codePage = codePage_;
222 Surface *surfaceMeasure = Surface::Allocate();
223 if (!surfaceMeasure)
224 return PRectangle();
225 surfaceMeasure->Init(wParent.GetID());
226 surfaceMeasure->SetUnicodeMode(SC_CP_UTF8 == codePage);
227 surfaceMeasure->SetDBCSMode(codePage);
228 startHighlight = 0;
229 endHighlight = 0;
230 inCallTipMode = true;
231 posStartCallTip = pos;
232 int deviceHeight = surfaceMeasure->DeviceHeightFont(size);
233 font.Create(faceName, SC_CHARSET_DEFAULT, deviceHeight, false, false);
234 // Look for multiple lines in the text
235 // Only support \n here - simply means container must avoid \r!
236 int numLines = 1;
237 const char *newline;
238 const char *look = val;
239 xUp = -100;
240 xDown = -100;
241 offsetMain = 5;
242 int width = PaintContents(surfaceMeasure, false) + 5;
243 while ((newline = strchr(look, '\n')) != NULL) {
244 look = newline + 1;
245 numLines++;
246 }
247 lineHeight = surfaceMeasure->Height(font);
248 // Extra line for border and an empty line at top and bottom
249 int height = lineHeight * numLines - surfaceMeasure->InternalLeading(font) + 2 + 2;
250 delete surfaceMeasure;
251 return PRectangle(pt.x - offsetMain, pt.y + 1, pt.x + width - offsetMain, pt.y + 1 + height);
252 }
253
254 void CallTip::CallTipCancel() {
255 inCallTipMode = false;
256 if (wCallTip.Created()) {
257 wCallTip.Destroy();
258 }
259 }
260
261 void CallTip::SetHighlight(int start, int end) {
262 // Avoid flashing by checking something has really changed
263 if ((start != startHighlight) || (end != endHighlight)) {
264 startHighlight = start;
265 endHighlight = end;
266 if (wCallTip.Created()) {
267 wCallTip.InvalidateAll();
268 }
269 }
270 }