Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / include / wx / osx / cocoa / private / markuptoattr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/cocoa/private/markuptoattr.h
3 // Purpose: Class to convert markup to Cocoa attributed strings.
4 // Author: Vadim Zeitlin
5 // Created: 2011-02-22
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_
11 #define _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_
12
13 #include "wx/private/markupparserattr.h"
14
15 // ----------------------------------------------------------------------------
16 // wxMarkupToAttrString: create NSAttributedString from markup.
17 // ----------------------------------------------------------------------------
18
19 class wxMarkupToAttrString : public wxMarkupParserAttrOutput
20 {
21 public:
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())
27 {
28 const wxCFStringRef
29 label(wxControl::RemoveMnemonics(wxMarkupParser::Strip(markup)));
30 m_attrString = [[NSMutableAttributedString alloc]
31 initWithString: label.AsNSString()];
32
33 m_pos = 0;
34
35 [m_attrString beginEditing];
36
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])];
44
45 // Now translate the markup tags to corresponding attributes.
46 wxMarkupParser parser(*this);
47 parser.Parse(markup);
48
49 [m_attrString endEditing];
50 }
51
52 ~wxMarkupToAttrString()
53 {
54 [m_attrString release];
55 }
56
57 // Accessor for the users of this class.
58 //
59 // We keep ownership of the returned string.
60 NSMutableAttributedString *GetNSAttributedString() const
61 {
62 return m_attrString;
63 }
64
65
66 // Implement base class pure virtual methods to process markup tags.
67 virtual void OnText(const wxString& text)
68 {
69 m_pos += wxControl::RemoveMnemonics(text).length();
70 }
71
72 virtual void OnAttrStart(const Attr& WXUNUSED(attr))
73 {
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);
77 }
78
79 virtual void OnAttrEnd(const Attr& attr)
80 {
81 unsigned start = m_rangeStarts.top();
82 m_rangeStarts.pop();
83
84 const NSRange range = NSMakeRange(start, m_pos - start);
85
86 [m_attrString addAttribute:NSFontAttributeName
87 value:attr.font.OSXGetNSFont()
88 range:range];
89
90 if ( attr.foreground.IsOk() )
91 {
92 [m_attrString addAttribute:NSForegroundColorAttributeName
93 value:attr.foreground.OSXGetNSColor()
94 range:range];
95 }
96
97 if ( attr.background.IsOk() )
98 {
99 [m_attrString addAttribute:NSBackgroundColorAttributeName
100 value:attr.background.OSXGetNSColor()
101 range:range];
102 }
103 }
104
105 private:
106 // The attributed string we're building.
107 NSMutableAttributedString *m_attrString;
108
109 // The current position in the output string.
110 unsigned m_pos;
111
112 // The positions of starting ranges.
113 wxStack<unsigned> m_rangeStarts;
114
115
116 wxDECLARE_NO_COPY_CLASS(wxMarkupToAttrString);
117 };
118
119 #endif // _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_