]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/private/markupparserattr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/markupparserattr.h
3 // Purpose: Classes mapping markup attributes to wxFont/wxColour.
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.h,v 1.12 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PRIVATE_MARKUPPARSERATTR_H_
12 #define _WX_PRIVATE_MARKUPPARSERATTR_H_
14 #include "wx/private/markupparser.h"
18 #include "wx/colour.h"
21 // ----------------------------------------------------------------------------
22 // wxMarkupParserAttrOutput: simplified wxFont-using version of the above.
23 // ----------------------------------------------------------------------------
25 // This class assumes that wxFont and wxColour are used to perform all the
26 // markup tags and implements the base class virtual functions in terms of
27 // OnAttr{Start,End}() only.
29 // Notice that you still must implement OnText() inherited from the base class
30 // when deriving from this one.
31 class wxMarkupParserAttrOutput
: public wxMarkupParserOutput
34 // A simple container of font and colours.
37 Attr(const wxFont
& font_
,
38 const wxColour
& foreground_
= wxColour(),
39 const wxColour
& background_
= wxColour())
40 : font(font_
), foreground(foreground_
), background(background_
)
50 // This object must be initialized with the font and colours to use
51 // initially, i.e. the ones used before any tags in the string.
52 wxMarkupParserAttrOutput(const wxFont
& font
,
53 const wxColour
& foreground
,
54 const wxColour
& background
)
56 m_attrs
.push(Attr(font
, foreground
, background
));
59 // Indicates the change of the font and/or colours used. Any of the
60 // fields of the argument may be invalid indicating that the corresponding
61 // attribute didn't actually change.
62 virtual void OnAttrStart(const Attr
& attr
) = 0;
64 // Indicates the end of the region affected by the given attributes
65 // (the same ones that were passed to the matching OnAttrStart(), use
66 // GetAttr() to get the ones that will be used from now on).
67 virtual void OnAttrEnd(const Attr
& attr
) = 0;
70 // Implement all pure virtual methods inherited from the base class in
71 // terms of our own ones.
72 virtual void OnBoldStart() { DoChangeFont(&wxFont::Bold
); }
73 virtual void OnBoldEnd() { DoEndAttr(); }
75 virtual void OnItalicStart() { DoChangeFont(&wxFont::Italic
); }
76 virtual void OnItalicEnd() { DoEndAttr(); }
78 virtual void OnUnderlinedStart() { DoChangeFont(&wxFont::Underlined
); }
79 virtual void OnUnderlinedEnd() { DoEndAttr(); }
81 virtual void OnStrikethroughStart() { DoChangeFont(&wxFont::Strikethrough
); }
82 virtual void OnStrikethroughEnd() { DoEndAttr(); }
84 virtual void OnBigStart() { DoChangeFont(&wxFont::Larger
); }
85 virtual void OnBigEnd() { DoEndAttr(); }
87 virtual void OnSmallStart() { DoChangeFont(&wxFont::Smaller
); }
88 virtual void OnSmallEnd() { DoEndAttr(); }
90 virtual void OnTeletypeStart()
92 wxFont
font(GetFont());
93 font
.SetFamily(wxFONTFAMILY_TELETYPE
);
96 virtual void OnTeletypeEnd() { DoEndAttr(); }
98 virtual void OnSpanStart(const wxMarkupSpanAttributes
& spanAttr
)
100 wxFont
font(GetFont());
101 if ( !spanAttr
.m_fontFace
.empty() )
102 font
.SetFaceName(spanAttr
.m_fontFace
);
104 FontModifier
<wxFontWeight
>()(spanAttr
.m_isBold
,
105 font
, &wxFont::SetWeight
,
106 wxFONTWEIGHT_NORMAL
, wxFONTWEIGHT_BOLD
);
108 FontModifier
<wxFontStyle
>()(spanAttr
.m_isItalic
,
109 font
, &wxFont::SetStyle
,
110 wxFONTSTYLE_NORMAL
, wxFONTSTYLE_ITALIC
);
112 FontModifier
<bool>()(spanAttr
.m_isUnderlined
,
113 font
, &wxFont::SetUnderlined
,
116 // TODO: No support for strike-through yet.
118 switch ( spanAttr
.m_sizeKind
)
120 case wxMarkupSpanAttributes::Size_Unspecified
:
123 case wxMarkupSpanAttributes::Size_Relative
:
124 if ( spanAttr
.m_fontSize
> 0 )
130 case wxMarkupSpanAttributes::Size_Symbolic
:
131 // The values of font size intentionally coincide with the
132 // values of wxFontSymbolicSize enum elements so simply cast
134 font
.SetSymbolicSize(
135 static_cast<wxFontSymbolicSize
>(spanAttr
.m_fontSize
)
139 case wxMarkupSpanAttributes::Size_PointParts
:
140 font
.SetPointSize((spanAttr
.m_fontSize
+ 1023)/1024);
145 const Attr
attr(font
, spanAttr
.m_fgCol
, spanAttr
.m_bgCol
);
151 virtual void OnSpanEnd(const wxMarkupSpanAttributes
& WXUNUSED(spanAttr
))
157 // Get the current attributes, i.e. the ones that should be used for
158 // rendering (or measuring or whatever) the text at the current position in
161 // It may be called from OnAttrStart() to get the old attributes used
162 // before and from OnAttrEnd() to get the new attributes that will be used
163 // from now on but is mostly meant to be used from overridden OnText()
165 const Attr
& GetAttr() const { return m_attrs
.top(); }
167 // A shortcut for accessing the font of the current attribute.
168 const wxFont
& GetFont() const { return GetAttr().font
; }
171 // Change only the font to the given one. Call OnAttrStart() to notify
172 // about the change and update the attributes stack.
173 void DoSetFont(const wxFont
& font
)
175 const Attr
attr(font
);
182 // Apply the given function to the font currently on top of the font stack,
183 // push the new font on the stack and call OnAttrStart() with it.
184 void DoChangeFont(wxFont (wxFont::*func
)() const)
186 DoSetFont((GetFont().*func
)());
191 const Attr
attr(m_attrs
.top());
197 // A helper class used to apply the given function to a wxFont object
198 // depending on the value of an OptionalBool.
199 template <typename T
>
204 void operator()(wxMarkupSpanAttributes::OptionalBool isIt
,
206 void (wxFont::*func
)(T
),
212 case wxMarkupSpanAttributes::Unspecified
:
215 case wxMarkupSpanAttributes::No
:
216 (font
.*func
)(noValue
);
219 case wxMarkupSpanAttributes::Yes
:
220 (font
.*func
)(yesValue
);
227 wxStack
<Attr
> m_attrs
;
229 wxDECLARE_NO_COPY_CLASS(wxMarkupParserAttrOutput
);
232 #endif // _WX_PRIVATE_MARKUPPARSERATTR_H_