1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/cocoa/private/markuptoattr.h
3 // Purpose: Class to convert markup to Cocoa attributed strings.
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_
11 #define _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_
13 #include "wx/private/markupparserattr.h"
15 // ----------------------------------------------------------------------------
16 // wxMarkupToAttrString: create NSAttributedString from markup.
17 // ----------------------------------------------------------------------------
19 class wxMarkupToAttrString
: public wxMarkupParserAttrOutput
22 // We don't care about the original colours because we never use them but
23 // we do need the correct initial font as we apply modifiers (e.g. create a
24 // font larger than it) to it and so it must be valid.
25 wxMarkupToAttrString(wxWindow
*win
, const wxString
& markup
)
26 : wxMarkupParserAttrOutput(win
->GetFont(), wxColour(), wxColour())
29 label(wxControl::RemoveMnemonics(wxMarkupParser::Strip(markup
)));
30 m_attrString
= [[NSMutableAttributedString alloc
]
31 initWithString
: label
.AsNSString()];
35 [m_attrString beginEditing
];
37 // First thing we do is change the default string font: as mentioned in
38 // Apple documentation, attributed strings use "Helvetica 12" font by
39 // default which is different from the system "Lucida Grande" font. So
40 // we need to explicitly change the font for the entire string.
41 [m_attrString addAttribute
:NSFontAttributeName
42 value
:win
->GetFont().OSXGetNSFont()
43 range
:NSMakeRange(0, [m_attrString length
])];
45 // Now translate the markup tags to corresponding attributes.
46 wxMarkupParser
parser(*this);
49 [m_attrString endEditing
];
52 ~wxMarkupToAttrString()
54 [m_attrString release
];
57 // Accessor for the users of this class.
59 // We keep ownership of the returned string.
60 NSMutableAttributedString
*GetNSAttributedString() const
66 // Implement base class pure virtual methods to process markup tags.
67 virtual void OnText(const wxString
& text
)
69 m_pos
+= wxControl::RemoveMnemonics(text
).length();
72 virtual void OnAttrStart(const Attr
& WXUNUSED(attr
))
74 // Just remember the starting position of the range, we can't really
75 // set the attribute until we find the end of it.
76 m_rangeStarts
.push(m_pos
);
79 virtual void OnAttrEnd(const Attr
& attr
)
81 unsigned start
= m_rangeStarts
.top();
84 const NSRange range
= NSMakeRange(start
, m_pos
- start
);
86 [m_attrString addAttribute
:NSFontAttributeName
87 value
:attr
.font
.OSXGetNSFont()
90 if ( attr
.foreground
.IsOk() )
92 [m_attrString addAttribute
:NSForegroundColorAttributeName
93 value
:attr
.foreground
.OSXGetNSColor()
97 if ( attr
.background
.IsOk() )
99 [m_attrString addAttribute
:NSBackgroundColorAttributeName
100 value
:attr
.background
.OSXGetNSColor()
106 // The attributed string we're building.
107 NSMutableAttributedString
*m_attrString
;
109 // The current position in the output string.
112 // The positions of starting ranges.
113 wxStack
<unsigned> m_rangeStarts
;
116 wxDECLARE_NO_COPY_CLASS(wxMarkupToAttrString
);
119 #endif // _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_