15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

editing a Text Document: The Capseditor example ❘ OC39<br />

the required coordinates of the top-left corner of the item. The returned coordinates are world coordinates,<br />

but that is fine because you have already called TranslateTransform() on the Graphics object so that you<br />

need to pass it as a world coordinate, rather than a page, when asking it to display items.<br />

Coordinate Transforms<br />

This section examines the implementation of the helper methods that are written in the CapsEditor<br />

sample to help you with coordinate transforms. These are the WorldYCoordinateToLineIndex() <strong>and</strong><br />

LineIndexToWorldCoordinates() methods referred to in the previous section, as well as a couple of other<br />

methods.<br />

First, LineIndexToWorldCoordinates() takes a given line index, <strong>and</strong> works out the world coordinates of<br />

the top-left corner of that line, using the known margin <strong>and</strong> line height:<br />

private Point LineIndexToWorldCoordinates(int index)<br />

{<br />

Point TopLeftCorner = new Point(<br />

(int)margin, (int)(lineHeight*index + margin));<br />

return TopLeftCorner;<br />

}<br />

code download CapsEditor.sln<br />

You also use a method that roughly does the reverse transform in OnPaint().<br />

WorldYCoordinateToLineIndex() works out the line index, but it takes into account only a vertical world<br />

coordinate because it is used to work out the line index corresponding to the top <strong>and</strong> bottom of the clip<br />

region:<br />

private int WorldYCoordinateToLineIndex(int y)<br />

{<br />

if (y < margin)<br />

{<br />

return -1;<br />

}<br />

return (int)((y-margin)/lineHeight);<br />

}<br />

There are three more methods, which will be called from the h<strong>and</strong>ler routine that responds to the user<br />

double-clicking the mouse. First, you have a method that works out the index of the line being displayed at<br />

given world coordinates. Unlike WorldYCoordinateToLineIndex(), this method takes into account the x<br />

<strong>and</strong> y positions of the coordinates. It returns -1 if there is no line of text covering the coordinates passed in:<br />

private int WorldCoordinatesToLineIndex(Point position)<br />

{<br />

if (!documentHasData)<br />

{<br />

return -1;<br />

}<br />

if (position.Y < margin || position.X < margin)<br />

{<br />

return -1;<br />

}<br />

int index = (int)(position.Y-margin)/(int)this.lineHeight;<br />

// check position is not below document<br />

if (index >= documentLines.Count)<br />

{<br />

return -1;<br />

}<br />

// now check that horizontal position is within this line<br />

TextLineInformation theLine =<br />

(TextLineInformation)documentLines[index];<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!