195 |
|
|
196 |
return maxWidth; |
return maxWidth; |
197 |
} |
} |
198 |
|
|
199 |
|
/** |
200 |
|
* Provides a facility to map screen coordinates into a model location. For a |
201 |
|
* given text fragment and start location within this fragment, this method |
202 |
|
* determines the model location so that the resulting fragment fits best |
203 |
|
* into the span <code>[x0, x]</code>. |
204 |
|
* |
205 |
|
* The parameter <code>round</code> controls which model location is returned |
206 |
|
* if the view coordinates are on a character: If <code>round</code> is |
207 |
|
* <code>true</code>, then the result is rounded up to the next character, so |
208 |
|
* that the resulting fragment is the smallest fragment that is larger than |
209 |
|
* the specified span. If <code>round</code> is <code>false</code>, then the |
210 |
|
* resulting fragment is the largest fragment that is smaller than the |
211 |
|
* specified span. |
212 |
|
* |
213 |
|
* @param s the text segment |
214 |
|
* @param fm the font metrics to use |
215 |
|
* @param x0 the starting screen location |
216 |
|
* @param x the target screen location at which the requested fragment should |
217 |
|
* end |
218 |
|
* @param te the tab expander to use; if this is <code>null</code>, TABs are |
219 |
|
* expanded to one space character |
220 |
|
* @param p0 the starting model location |
221 |
|
* @param round if <code>true</code> round up to the next location, otherwise |
222 |
|
* round down to the current location |
223 |
|
* |
224 |
|
* @return the model location, so that the resulting fragment fits within the |
225 |
|
* specified span |
226 |
|
*/ |
227 |
|
public static final int getTabbedTextOffset(Segment s, FontMetrics fm, int x0, |
228 |
|
int x, TabExpander te, int p0, |
229 |
|
boolean round) |
230 |
|
{ |
231 |
|
// At the end of the for loop, this holds the requested model location |
232 |
|
int pos; |
233 |
|
int currentX = x0; |
234 |
|
for (pos = p0; pos < s.getEndIndex(); pos++) |
235 |
|
{ |
236 |
|
char nextChar = s.array[pos]; |
237 |
|
if (nextChar != '\n') |
238 |
|
currentX += fm.charWidth(nextChar); |
239 |
|
else |
240 |
|
{ |
241 |
|
if (te == null) |
242 |
|
currentX += fm.charWidth(' '); |
243 |
|
else |
244 |
|
currentX = (int) te.nextTabStop(currentX, pos); |
245 |
|
} |
246 |
|
if (currentX >= x) |
247 |
|
{ |
248 |
|
if (! round) |
249 |
|
pos--; |
250 |
|
break; |
251 |
|
} |
252 |
|
} |
253 |
|
return pos; |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
|
* Provides a facility to map screen coordinates into a model location. For a |
258 |
|
* given text fragment and start location within this fragment, this method |
259 |
|
* determines the model location so that the resulting fragment fits best |
260 |
|
* into the span <code>[x0, x]</code>. |
261 |
|
* |
262 |
|
* This method rounds up to the next location, so that the resulting fragment |
263 |
|
* will be the smallest fragment of the text, that is greater than the |
264 |
|
* specified span. |
265 |
|
* |
266 |
|
* @param s the text segment |
267 |
|
* @param fm the font metrics to use |
268 |
|
* @param x0 the starting screen location |
269 |
|
* @param x the target screen location at which the requested fragment should |
270 |
|
* end |
271 |
|
* @param te the tab expander to use; if this is <code>null</code>, TABs are |
272 |
|
* expanded to one space character |
273 |
|
* @param p0 the starting model location |
274 |
|
* |
275 |
|
* @return the model location, so that the resulting fragment fits within the |
276 |
|
* specified span |
277 |
|
*/ |
278 |
|
public static final int getTabbedTextOffset(Segment s, FontMetrics fm, int x0, |
279 |
|
int x, TabExpander te, int p0) |
280 |
|
{ |
281 |
|
return getTabbedTextOffset(s, fm, x0, x, te, p0, true); |
282 |
|
} |
283 |
} |
} |