Wiley.Symbian.OS.C.plus.plus.for.Mobile.Phones.Aug.2007 (779890), страница 87
Текст из файла (страница 87)
Horizontal justification is specified by the final parameter (here, CGraphicsContext::ECenter, toindicate that the text should be horizontally centered).Vertical JustificationDrawText() does not handle vertical justification because the algorithmis simple and (unlike horizontal justification) vertical justification does notdepend on the text of the string. You have to specify the baseline in pixelsdown from the top of the rectangle, so, as illustrated in Figure 17.2, startwith half the height of the rectangle (measured from its top downwards)and then add half the font’s ascent.486GRAPHICS FOR DISPLAYHalf heightGreetingsAscentBaselineFigure 17.2Half ascentDescenderCalculating the vertical justificationAt this point, we have drawn the text, so we are finished with the fontand must discard it:gc.DiscardFont();It is important to discard the font at this stage, in order to avoid amemory leak.17.2The CGraphicsContext APIAll concrete graphics-context classes are derived from CGraphicsContext, which offers a rich API for device-independent drawing.
Its mainfeatures are shown, in UML, in Figure 17.3.CGraphicsContext contains the main drawing functions and isdefined in gdi.h. All drawing is done using the current pen, brushand font settings, and is clipped to the current clipping region. The pen,brush, font and clipping region settings provide context for the graphicsfunctions – hence the name of the class.You can only set graphics context settings. There is no class for pen,brush and so on, and you cannot interrogate a graphics context to findout its current settings. You can keep a graphics context if you needto keep its settings and you can reset a graphics context with a singlefunction call if you need to throw all the settings away.The drawing functions are illustrated by many examples throughoutthis book. CGraphicsContext is thoroughly documented in the S60and UIQ SDKs. They also contain an example program, FontsShell,that illustrates all the functions covered in this section.THE CGRAPHICSCONTEXT API487CGraphicsContextTPointTSizeSet...()Draw...()iXiYiWidthiHeightpenbrushfontsTRectetc.CFontiTl : TPointiBr : TPointRegionMove()Resize()Shrink()Grow()SetWidth()SetHeight()IsEmpty()Contains(TPoint)Intersects(TRect)Intersection(TRect)BoundingRect(TRect)Center()Figure 17.3 Graphics context API classesCoordinate ClassesGraphics are drawn to a device whose coordinate system is defined inpixels.
Each point on the device has an (x, y) coordinate, measured froman origin at the top left of the device, with x coordinates increasing towardthe right and y coordinates increasing downwards.Later in this chapter, we see how pixel coordinates are related to realworld units such as inches or centimeters. For now, we concentrateon pixels and screen-oriented graphics.Supporting classes for points, rectangles, sizes and regions are definedin e32std.h:• TPoint contains iX and iY coordinates• TRect contains two points, iTl for top left and iBr for bottom right• TSize contains iWidth and iHeight dimensions.These classes are equipped with a large range of constructors, operatorsand functions to manipulate and combine them, but they make no attemptto encapsulate their members.
You do not have to use get and set functions488GRAPHICS FOR DISPLAYto access the coordinates of a point, and so on. In truth, there wouldbe very little point in doing so: the representations of these objects aregenuinely public.The two points that define TRect can be interpreted by specificgraphics implementations in different ways. One common interpretationis to place the top-left point inside the rectangle and the bottom-rightpoint just outside it, as shown in Figure 17.4.iTliBrFigure 17.4An interpretation of TRectThis definition makes some things easier, such as calculating thesize, because you simply subtract the x and y coordinates of the topleft from the bottom-right point.
It makes other things harder, such ascalculations for interactively drawing a rectangle, because you haveto add (1, 1) to the pointer coordinates to include the bottom-rightcorner correctly. If TRect is defined to include its bottom-right corner, itmakes some things easier and others harder.
It is important to rememberthat the definition of the rectangle depends on the specific graphicsinterpretation.Rectangles should be normalized, so that iTl coordinates are nevergreater than corresponding iBr coordinates. If you perform a calculationon a TRect that might violate this condition, call Normalize() to clearthings up by swapping the x and y coordinate values as necessary.Region-related ClassesSeveral region-related classes are also defined in e32std.h.
These definea region of arbitrary shape as the union of several disjoint rectangles. Theregion classes are used extensively by the window server, but only inspecialized application programs.A region can potentially have very many rectangles, so region classesin general can allocate resources on the heap. All the region classesare heavily optimized and are machine-coded on the target. There isalso a region class such that, provided relatively few rectangles areneeded to define the region, no heap-based allocation is necessary.While points, rectangles and sizes are simple T classes that are easy toallocate anywhere and pass around in client–server calls, regions requiremore careful management and, like C classes, need to be deleted orcleaned up when no longer required.THE CGRAPHICSCONTEXT API489Setting up the Graphics ContextCGraphicsContext holds several important items of context for drawing functions.• The pen defines draw modes (color and style).
These are used fordrawing lines, the outlines of filled shapes and text. Draw modeoptions include Boolean operations on the color values of pixels.The most useful Boolean operations are solid (which means ‘use thecolor specified’), null (which means ‘do not draw’) and XOR withwhite (which means ‘invert’ and can be useful for cursor selection,rubber-banding and so on). Style options include solid, dotted, dashedand the pen width.
The BITGDI that draws screen graphics (whichyou saw in Chapter 15) does not support combinations of style andpen width – that is, it cannot do thick dotted lines. Use the SetPenColor(), SetPenStyle() and SetPenSize() member functionsto control the pen. By default, the pen is black, solid, and one pixelthick.• The brush defines the fill and background color or pattern.
Thebrush can be null, solid, a hatching pattern or a bitmap. Forhatching and bitmaps, you can set an offset so that pattern fillson adjacent drawing primitives join without odd edge effects. UseSetBrushStyle(), SetBrushColor(), SetBrushOrigin(),SetBrushPattern() and DiscardBrushPattern() to controlbrush settings; the defaults are null brush, zero origin.• The font defines how text is drawn.
You specify it by passing aCFont* object to CGraphicsContext. The CONE environmenthas one font (iCoeEnv->NormalFont()), while the UIQ or S60environments contain several (iEikonEnv->TitleFont(), LegendFont(), SymbolFont(), AnnotationFont() and DenseFont()). Use UseFont() to set a font, DiscardFont() to say youno longer wish to use that font and SetUnderlineStyle() andSetStrikethroughStyle() to set algorithmic enhancements tothe font in use. By default, no font and no algorithmic enhancementsare in use. Trying to draw text without a font in use results in a panic.• The current position is set by MoveTo() and various DrawTo() member functions; it is moved by MoveBy() and a range of DrawBy()functions. It is also affected by DrawPolyLine(). The MoveBy()and DrawBy() functions support relative moving and drawing.
Bydefault, the current position is (0, 0).• The origin defines the offset, from the device origin, that is used fordrawing. You use SetOrigin() to control it. By default, the originis (0, 0).490GRAPHICS FOR DISPLAY• The clipping region defines the region to which you want yourgraphics to be clipped. You can specify a simple rectangle or a regionthat may be arbitrarily complex. Use SetClippingRect() to set arectangular clipping region and CancelClippingRect() to cancelit.
By default, no clipping region (other than the device limits) applies.• Specialized justification settings for a variant of DrawText() can beset, although it is best not to call these directly from your own code.Instead, use the FORM component in Symbian OS to create text viewsfor you.Use Reset() to set the context to default values.Drawing FunctionsOnce you have set up the graphics context to your liking, there arenumerous ways to draw to the screen.
All graphics-context functions arevirtual, so they can be implemented in derived classes. Furthermore, allgraphics-context functions are designed to succeed, and so do not returnanything (in C++ declarations, they return void). This requirement is sothat multiple graphics-context commands can be batched into a singlemessage and sent to a server for execution: this would not be possible ifany graphics context command had a return value.Points and linesYou can plot a single point or draw an arc, a line, or a polyline. Thesefunctions all use the current pen.