1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        wx/osx/cocoa/private/markuptoattr.h 
   3 // Purpose:     Class to convert markup to Cocoa attributed strings. 
   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_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_ 
  12 #define _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_ 
  14 #include "wx/private/markupparserattr.h" 
  16 // ---------------------------------------------------------------------------- 
  17 // wxMarkupToAttrString: create NSAttributedString from markup. 
  18 // ---------------------------------------------------------------------------- 
  20 class wxMarkupToAttrString 
: public wxMarkupParserAttrOutput
 
  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()) 
  30             label(wxControl::RemoveMnemonics(wxMarkupParser::Strip(markup
))); 
  31         m_attrString 
= [[NSMutableAttributedString alloc
] 
  32                         initWithString
: label
.AsNSString()]; 
  36         [m_attrString beginEditing
]; 
  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
])]; 
  46         // Now translate the markup tags to corresponding attributes. 
  47         wxMarkupParser 
parser(*this); 
  50         [m_attrString endEditing
]; 
  53     ~wxMarkupToAttrString() 
  55         [m_attrString release
]; 
  58     // Accessor for the users of this class. 
  60     // We keep ownership of the returned string. 
  61     NSMutableAttributedString 
*GetNSAttributedString() const 
  67     // Implement base class pure virtual methods to process markup tags. 
  68     virtual void OnText(const wxString
& text
) 
  70         m_pos 
+= wxControl::RemoveMnemonics(text
).length(); 
  73     virtual void OnAttrStart(const Attr
& WXUNUSED(attr
)) 
  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
); 
  80     virtual void OnAttrEnd(const Attr
& attr
) 
  82         unsigned start 
= m_rangeStarts
.top(); 
  85         const NSRange range 
= NSMakeRange(start
, m_pos 
- start
); 
  87         [m_attrString addAttribute
:NSFontAttributeName
 
  88                       value
:attr
.font
.OSXGetNSFont() 
  91         if ( attr
.foreground
.IsOk() ) 
  93             [m_attrString addAttribute
:NSForegroundColorAttributeName
 
  94                           value
:attr
.foreground
.OSXGetNSColor() 
  98         if ( attr
.background
.IsOk() ) 
 100             [m_attrString addAttribute
:NSBackgroundColorAttributeName
 
 101                           value
:attr
.background
.OSXGetNSColor() 
 107     // The attributed string we're building. 
 108     NSMutableAttributedString 
*m_attrString
; 
 110     // The current position in the output string. 
 113     // The positions of starting ranges. 
 114     wxStack
<unsigned> m_rangeStarts
; 
 117     wxDECLARE_NO_COPY_CLASS(wxMarkupToAttrString
); 
 120 #endif // _WX_OSX_COCOA_PRIVATE_MARKUPTOATTR_H_