]>
Commit | Line | Data |
---|---|---|
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 | // 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 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_ | |
12 | #define _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_ | |
13 | ||
14 | #include "wx/private/markupparserattr.h" | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // wxMarkupToAttrString: create NSAttributedString from markup. | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | class wxMarkupToAttrString : public wxMarkupParserAttrOutput | |
21 | { | |
22 | public: | |
23 | // We don't care about the original colours because we never use them but | |
24 | // we do need the correct initial font as we apply modifiers (e.g. create a | |
25 | // font larger than it) to it and so it must be valid. | |
26 | wxMarkupToAttrString(wxWindow *win, const wxString& markup) | |
27 | : wxMarkupParserAttrOutput(win->GetFont(), wxColour(), wxColour()) | |
28 | { | |
29 | const wxCFStringRef | |
30 | label(wxControl::RemoveMnemonics(wxMarkupParser::Strip(markup))); | |
31 | m_attrString = [[NSMutableAttributedString alloc] | |
32 | initWithString: label.AsNSString()]; | |
33 | ||
34 | m_pos = 0; | |
35 | ||
36 | [m_attrString beginEditing]; | |
37 | ||
38 | // First thing we do is change the default string font: as mentioned in | |
39 | // Apple documentation, attributed strings use "Helvetica 12" font by | |
40 | // default which is different from the system "Lucida Grande" font. So | |
41 | // we need to explicitly change the font for the entire string. | |
42 | [m_attrString addAttribute:NSFontAttributeName | |
43 | value:win->GetFont().OSXGetNSFont() | |
44 | range:NSMakeRange(0, [m_attrString length])]; | |
45 | ||
46 | // Now translate the markup tags to corresponding attributes. | |
47 | wxMarkupParser parser(*this); | |
48 | parser.Parse(markup); | |
49 | ||
50 | [m_attrString endEditing]; | |
51 | } | |
52 | ||
53 | ~wxMarkupToAttrString() | |
54 | { | |
55 | [m_attrString release]; | |
56 | } | |
57 | ||
58 | // Accessor for the users of this class. | |
59 | // | |
60 | // We keep ownership of the returned string. | |
61 | NSMutableAttributedString *GetNSAttributedString() const | |
62 | { | |
63 | return m_attrString; | |
64 | } | |
65 | ||
66 | ||
67 | // Implement base class pure virtual methods to process markup tags. | |
68 | virtual void OnText(const wxString& text) | |
69 | { | |
70 | m_pos += wxControl::RemoveMnemonics(text).length(); | |
71 | } | |
72 | ||
73 | virtual void OnAttrStart(const Attr& WXUNUSED(attr)) | |
74 | { | |
75 | // Just remember the starting position of the range, we can't really | |
76 | // set the attribute until we find the end of it. | |
77 | m_rangeStarts.push(m_pos); | |
78 | } | |
79 | ||
80 | virtual void OnAttrEnd(const Attr& attr) | |
81 | { | |
82 | unsigned start = m_rangeStarts.top(); | |
83 | m_rangeStarts.pop(); | |
84 | ||
85 | const NSRange range = NSMakeRange(start, m_pos - start); | |
86 | ||
87 | [m_attrString addAttribute:NSFontAttributeName | |
88 | value:attr.font.OSXGetNSFont() | |
89 | range:range]; | |
90 | ||
91 | if ( attr.foreground.IsOk() ) | |
92 | { | |
93 | [m_attrString addAttribute:NSForegroundColorAttributeName | |
94 | value:attr.foreground.OSXGetNSColor() | |
95 | range:range]; | |
96 | } | |
97 | ||
98 | if ( attr.background.IsOk() ) | |
99 | { | |
100 | [m_attrString addAttribute:NSBackgroundColorAttributeName | |
101 | value:attr.background.OSXGetNSColor() | |
102 | range:range]; | |
103 | } | |
104 | } | |
105 | ||
106 | private: | |
107 | // The attributed string we're building. | |
108 | NSMutableAttributedString *m_attrString; | |
109 | ||
110 | // The current position in the output string. | |
111 | unsigned m_pos; | |
112 | ||
113 | // The positions of starting ranges. | |
114 | wxStack<unsigned> m_rangeStarts; | |
115 | ||
116 | ||
117 | wxDECLARE_NO_COPY_CLASS(wxMarkupToAttrString); | |
118 | }; | |
119 | ||
120 | #endif // _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_ |