1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #ifndef __COETEXTDRAWER_H__
17 #define __COETEXTDRAWER_H__
28 // forward declarations
29 class CCoeTextDrawerBase;
30 class TCoeTextTypeAdaptor;
33 This class serves as a smart-pointer handle to a CCoeTextDrawerBase-derived object, used for drawing
34 user interface text. Through the use of the CCoeControl's TextDrawer() and GetTextDrawer() methods, a
35 container control can control the text style and color used by its child controls. For example, a button
36 class can override its GetTextDrawer() method to set the text color used by its text label child control
37 depending on the buttons state (e.g. unpressed, pressed, or dimmed).
39 The XCoeTextDrawer object manages the life of the heap allocated CCoeTextDrawerBase (deleting it or
40 resetting it depending on whether the object is reusable or not).
42 XCoeTextDrawer objects shall be allocated on the stack in the control's Draw() method and initialized
43 with a heap allocated CCoeTextDrawerBase object fetched from the control's skin, or from its parent or
44 background through the CCoeControl::TextDrawer() method.
46 Never create a CCoeTextDrawer-derived object inside your CCoeControl::Draw() method, as this is not
47 guaranteed to work in low-memory situations. Non-reusable text drawers must only be created in your
48 control's GetTextDrawer() method, as this provides error handling in case the text drawer could not be
51 Hence, typical use from inside a CCoeControl's Draw() method:
53 XCoeTextDrawer textDrawer(TextDrawer());
54 textDrawer.SetAlignment(EHLeftVCenter);
55 textDrawer.SetMargins(iTextMargins);
56 textDrawer.DrawText(SystemGc(), *iText, iTextRect, ScreenFont(TCoeFont::NormalFont()));
65 IMPORT_C XCoeTextDrawer(CCoeTextDrawerBase& aTextDrawer);
66 IMPORT_C ~XCoeTextDrawer();
68 IMPORT_C void operator=(CCoeTextDrawerBase& aTextDrawer);
70 IMPORT_C void DrawText(CGraphicsContext& aGc, const TBidiText& aText, const TRect& aTextRect, const CFont& aFont) const;
71 IMPORT_C void DrawDisplayOrderedText(CGraphicsContext& aGc, const TDesC& aText, const TRect& aTextRect, const CFont& aFont) const;
73 IMPORT_C void DrawTextVertical(CGraphicsContext& aGc, const TBidiText& aText, const TRect& aTextRect, const CFont& aFont, TBool aUp = ETrue) const;
74 IMPORT_C void DrawDisplayOrderedTextVertical(CGraphicsContext& aGc, const TDesC& aText, const TRect& aTextRect, const CFont& aFont, TBool aUp = ETrue) const;
76 IMPORT_C TRect ClipRect() const;
77 IMPORT_C void SetClipRect(const TRect& aClipRect);
79 inline TRgb TextColor() const;
80 inline void SetTextColor(TRgb aColor);
82 inline TGulAlignment Alignment() const;
83 inline void SetAlignment(const TGulAlignment& aAlignment);
85 inline TMargins8 Margins() const;
86 inline void SetMargins(const TMargins8& aMargins);
88 inline TInt LineGapInPixels() const;
89 inline void SetLineGapInPixels(TInt aLineGapInPixels);
91 IMPORT_C CCoeTextDrawerBase *operator ->(); // deprecated
93 XCoeTextDrawer(const XCoeTextDrawer& aTextDrawer);
95 CCoeTextDrawerBase* iTextDrawer;
100 // forward declaration
101 class CCoeTextDrawerBaseExt;
104 This is the base class for all text drawers implementing different text effects (for example
105 shadow or glowing/outlined text). The object can be created and deleted each time it's used,
106 or Reset() and reused if it IsReusable(). The latter is recommended.
108 Note that the pure virtual DrawText() method is private. This ensures that the object is used
109 through the XCoeTextDrawer class (which manages its life).
111 Note also that the accessor and set methods should be used via the owning XCoeTextDrawer object,
112 and that the MObjectProvider mechanism can be used to identify the actual text drawer implementation.
117 class CCoeTextDrawerBase : public CBase, public MObjectProvider
119 friend class XCoeTextDrawer;
121 IMPORT_C ~CCoeTextDrawerBase();
122 IMPORT_C virtual void Reset();
125 This method returns the main color used by the CCoeTextDrawer to draw text.
126 @return The color used to draw text.
128 virtual TRgb TextColor() const = 0;
130 This method sets the main color to use to draw text.
131 @param aColor The color to use to draw text.
133 virtual void SetTextColor(TRgb aColor) = 0;
135 IMPORT_C TGulAlignment Alignment() const;
136 IMPORT_C void SetAlignment(const TGulAlignment& aAlignment);
138 IMPORT_C TMargins8 Margins() const;
139 IMPORT_C void SetMargins(const TMargins8& aMargins);
141 IMPORT_C TInt LineGapInPixels() const;
142 IMPORT_C void SetLineGapInPixels(TInt aLineGapInPixels);
143 public: // public methods only for use by the owner/creator of the CCoeTextDrawerBase-derived object
144 IMPORT_C TBool IsReusable() const;
145 IMPORT_C void SetReusable(TBool aIsReusable);
147 IMPORT_C void SetAppLanguage(TLanguage aAppLang);
149 IMPORT_C CCoeTextDrawerBase();
150 IMPORT_C TInt Construct();
151 IMPORT_C TGulHAlignment ActualHorizontalAlignment(const TCoeTextTypeAdaptor& aText) const;
154 Any actual text drawer must implement this method to draw the text passed as argument.
155 The implementation must draw the text inside the text rectangle, cropped to the clipping
156 rectangle, and with the given margins and alignment taken into account.
158 Note that the actual horizontal alignment shall depend on the script directionality.
159 Calling ActualHorizontalAlignment() will return the horizontal alignment where left and
160 right has been swapped for right-to-left scripts.
162 virtual void DrawText(CGraphicsContext& aGc, const TCoeTextTypeAdaptor& aText, const CFont& aFont,
163 const TRect& aTextRect, const TRect& aClipRect) const = 0;
165 IMPORT_C virtual TMargins8 EffectMargins();
168 Draws the text vertically inside the text rectangle, cropped to the clipping
169 rectangle, and with the given margins and alignment taken into account.
170 If aUp is ETrue, text is rotated 90 degrees anti-clockwise; EFalse, text is rotated 90 degrees clockwise.
172 Note that the actual horizontal alignment shall depend on the script directionality.
173 Calling ActualHorizontalAlignment() will return the horizontal alignment where left and
174 right has been swapped for right-to-left scripts.
175 Also note that the margines are relative to the orientation of the text.
177 IMPORT_C virtual void DrawTextVertical(CGraphicsContext& aGc, const TCoeTextTypeAdaptor& aText, const CFont& aFont,
178 const TRect& aTextRect, const TRect& aClipRect, TBool aUp) const;
179 IMPORT_C virtual void CCoeTextDrawerBase_Reserved3();
180 IMPORT_C virtual void CCoeTextDrawerBase_Reserved4();
181 IMPORT_C virtual void CCoeTextDrawerBase_Reserved5();
182 IMPORT_C virtual void CCoeTextDrawerBase_Reserved6();
183 IMPORT_C virtual void CCoeTextDrawerBase_Reserved7();
184 IMPORT_C virtual void CCoeTextDrawerBase_Reserved8();
185 IMPORT_C virtual void CCoeTextDrawerBase_Reserved9();
186 IMPORT_C virtual void CCoeTextDrawerBase_Reserved10();
189 TGulAlignment iAlignment;
192 CCoeTextDrawerBaseExt* iExtension;
197 This class allows the XCoeTextDrawer to draw text that is in the form of a TBidiText
198 object as well as pre-reordered new-line separated plain text descriptors. (The descriptor
199 text is especially useful when using the XCoeTextDrawer together with the FORM component).
201 This removes the need to implement two versions of the DrawText() method.
206 class TCoeTextTypeAdaptor
209 IMPORT_C TCoeTextTypeAdaptor(const TDesC& aText); // text lines separated with '\n'
210 IMPORT_C TCoeTextTypeAdaptor(const TBidiText& aText); // TBidiText object
211 IMPORT_C TInt NumberOfLines() const;
212 IMPORT_C TPtrC LineOfText(TInt aLineNumber, TInt& aWidthInPixels, const CFont& aFont) const;
213 IMPORT_C TBool HasRightToLeftDirectionality() const;
226 This is a basic text drawer without any text effects. The default text drawer that can be
227 used if no other (device specific) text drawers has been added to the system.
232 class CCoePlainTextDrawer : public CCoeTextDrawerBase
235 DECLARE_TYPE_ID(0x1020831A)
237 IMPORT_C static CCoePlainTextDrawer* New(TRgb aTextColor);
239 IMPORT_C TRgb TextColor() const;
240 IMPORT_C void SetTextColor(TRgb aTextColor);
241 protected: // from MObjectProvider
242 IMPORT_C virtual TTypeUid::Ptr MopSupplyObject(TTypeUid aId);
244 void DrawText(CGraphicsContext& aGc, const TCoeTextTypeAdaptor& aText, const CFont& aFont,
245 const TRect& aTextRect, const TRect& aClipRect) const;
247 CCoePlainTextDrawer(TRgb aTextColor);
248 TInt Construct(); //lint !e1511 Suppress member hides non-virtual member
258 This method returns the main color used by by DrawText() and DrawDisplayOrderedText().
259 @return The color used to draw text.
261 TRgb XCoeTextDrawer::TextColor() const
262 { return iTextDrawer->TextColor(); }
265 This method sets the main color to use by DrawText() and DrawDisplayOrderedText() to draw text.
266 @param aColor The color to use to draw text.
268 void XCoeTextDrawer::SetTextColor(TRgb aColor)
269 { iTextDrawer->SetTextColor(aColor); }
272 Returns the text alignment that will be used by DrawText() and DrawDisplayOrderedText().
273 Note that left and right alignment will be swapped for right-to-left scripts, unless
274 the alignment has been set to be absolute (see TGulAlignment).
276 @return TGulAlignment value of iAlignment data member
278 TGulAlignment XCoeTextDrawer::Alignment() const
279 { return iTextDrawer->Alignment(); }
282 Set the text alignment that will be used by DrawText() and DrawDisplayOrderedText().
283 Note that left and right alignment will be swapped for right-to-left scripts, unless
284 the alignment has been set to be absolute (see TGulAlignment).
286 @param aAlignment TGulAlignment value.
288 void XCoeTextDrawer::SetAlignment(const TGulAlignment& aAlignment)
289 { iTextDrawer->SetAlignment(aAlignment); }
292 Returns the text margins that will be used by DrawText() and DrawDisplayOrderedText().
293 Note that text effects may intrude on the margin.
295 @return The margins between the text rect and the actual text, in pixels.
297 TMargins8 XCoeTextDrawer::Margins() const
298 { return iTextDrawer->Margins(); }
301 Set the text margins that will be used by DrawText() and DrawDisplayOrderedText().
302 Note that text effects may intrude on the margin, and that margins are always relative to the text orientation.
304 @param aMargins The margins between the text rect and the actual text, in pixels.
306 void XCoeTextDrawer::SetMargins(const TMargins8& aMargins)
307 { iTextDrawer->SetMargins(aMargins); }
310 Returns the gap (in pixels) between lines of text. Default gap is 1 (one) pixel.
311 @return The gap between lines of text, in pixels.
313 TInt XCoeTextDrawer::LineGapInPixels() const
314 { return iTextDrawer->LineGapInPixels(); }
317 Set the gap (in pixels) between lines of text. Default gap is 1 (one) pixel.
318 @param aLineGapInPixels The gap between lines of text, in pixels.
320 void XCoeTextDrawer::SetLineGapInPixels(TInt aLineGapInPixels)
321 { iTextDrawer->SetLineGapInPixels(aLineGapInPixels); }
324 #endif // __COETEXTDRAWER_H__