]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/stattext.mm
Fix install_name_tool calls in OS X "make install".
[wxWidgets.git] / src / osx / cocoa / stattext.mm
index a30082c99010e6cb8422b77da00cc188b652cff6..832b319017122c01a8a9439e5c4c5adf05b4b0de 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Stefan Csomor
 // Modified by:
 // Created:     04/01/98
-// RCS-ID:      $Id: stattext.cpp 54845 2008-07-30 14:52:41Z SC $
 // Copyright:   (c) Stefan Csomor
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
     #include "wx/settings.h"
 #endif // WX_PRECOMP
 
-#include "wx/notebook.h"
-#include "wx/tabctrl.h"
-
 #include "wx/osx/private.h"
 
+#if wxUSE_MARKUP
+    #include "wx/osx/cocoa/private/markuptoattr.h"
+#endif // wxUSE_MARKUP
+
 #include <stdio.h>
 
-wxSize wxStaticText::DoGetBestSize() const
+@interface wxNSStaticTextView : NSTextField
 {
-    Point bounds;
-#if wxOSX_USE_CARBON
-    Rect bestsize = { 0 , 0 , 0 , 0 } ;
-
-    // try the built-in best size if available
-    Boolean former = m_peer->GetData<Boolean>( kControlStaticTextIsMultilineTag);
-    m_peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
-    m_peer->GetBestRect( &bestsize ) ;
-    m_peer->SetData( kControlStaticTextIsMultilineTag, former );
-    if ( !EmptyRect( &bestsize ) )
+}
+@end
+
+@implementation wxNSStaticTextView
+
++ (void)initialize
+{
+    static BOOL initialized = NO;
+    if (!initialized)
     {
-        bounds.h = bestsize.right - bestsize.left ;
-        bounds.v = bestsize.bottom - bestsize.top ;
+        initialized = YES;
+        wxOSXCocoaClassAddWXMethods( self );
     }
-    else
-#endif
-    {
-#if wxOSX_USE_CARBON
-        ControlFontStyleRec controlFont;
-        OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
-        verify_noerr( err );
-
-        wxCFStringRef str( m_label,  GetFont().GetEncoding() );
-
-#if wxOSX_USE_ATSU_TEXT
-        SInt16 baseline;
-        if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
-        {
-            err = GetThemeTextDimensions(
-                (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
-                m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
-            verify_noerr( err );
-        }
-        else
-#endif
-#endif
-        {
-            wxClientDC dc(const_cast<wxStaticText*>(this));
-            wxCoord width, height ;
-            dc.GetTextExtent( m_label , &width, &height);
-            bounds.h = width;
-            bounds.v = height;
+}
+
+- (void) setEnabled:(BOOL) flag 
+{ 
+    [super setEnabled: flag]; 
+    
+    if (![self drawsBackground]) { 
+        // Static text is drawn incorrectly when disabled. 
+        // For an explanation, see 
+        // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 
+        if (flag)
+        { 
+            [self setTextColor: [NSColor controlTextColor]]; 
         }
+        else 
+        { 
+            [self setTextColor: [NSColor secondarySelectedControlColor]]; 
+        } 
+    } 
+} 
+
+@end
+
+class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
+{
+public:
+    wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
+    {
+        m_lineBreak = lineBreak;
+    }
 
-        if ( m_label.empty() )
-            bounds.h = 0;
+    virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
+    {
+        wxCFStringRef text( title , encoding );
+
+        NSMutableAttributedString *
+            attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
+        DoSetAttrString(attrstring);
+        [attrstring release];
     }
-    bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
-    bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
 
-    return wxSize( bounds.h, bounds.v );
-}
+#if wxUSE_MARKUP
+    virtual void SetLabelMarkup( const wxString& markup)
+    {
+        wxMarkupToAttrString toAttr(GetWXPeer(), markup);
+
+        DoSetAttrString(toAttr.GetNSAttributedString());
+    }
+#endif // wxUSE_MARKUP
 
-// for wxST_ELLIPSIZE_* support:
+private:
+    void DoSetAttrString(NSMutableAttributedString *attrstring)
+    {
+        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
+        [paragraphStyle setLineBreakMode:m_lineBreak];
+        int style = GetWXPeer()->GetWindowStyleFlag();
+        if (style & wxALIGN_CENTER_HORIZONTAL)
+            [paragraphStyle setAlignment: NSCenterTextAlignment];
+        else if (style & wxALIGN_RIGHT)
+            [paragraphStyle setAlignment: NSRightTextAlignment];
+
+        [attrstring addAttribute:NSParagraphStyleAttributeName
+                    value:paragraphStyle
+                    range:NSMakeRange(0, [attrstring length])];
+        NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
+        [cell setAttributedStringValue:attrstring];
+        [paragraphStyle release];
+    }
 
-/*
-   FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
-          to allow correct dynamic ellipsizing of the label
-*/
+    NSLineBreakMode m_lineBreak;
+};
 
-wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, 
-                                    wxWindowMac* parent, 
-                                    wxWindowID id, 
-                                    const wxString& label,
-                                    const wxPoint& pos, 
+wxSize wxStaticText::DoGetBestSize() const
+{
+    return wxWindowMac::DoGetBestSize() ;
+}
+
+wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
+                                    wxWindowMac* WXUNUSED(parent),
+                                    wxWindowID WXUNUSED(id),
+                                    const wxString& WXUNUSED(label),
+                                    const wxPoint& pos,
                                     const wxSize& size,
-                                    long style, 
-                                    long extraStyle) 
+                                    long style,
+                                    long WXUNUSED(extraStyle))
 {
-    NSView* sv = (wxpeer->GetParent()->GetHandle() );
-    
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
-    wxNSTextField* v = [[wxNSTextField alloc] initWithFrame:r];
-    [sv addSubview:v];
+    wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
 
-    [v setBezeled:NO];
     [v setEditable:NO];
     [v setDrawsBackground:NO];
-    
-    wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
-    [v setImplementation:c];
-    return c;
-/*
-    Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
-
-    wxMacControl* peer = new wxMacControl( wxpeer );
-    OSStatus err = CreateStaticTextControl(
-        MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
-        &bounds, NULL, NULL, peer->GetControlRefAddr() );
-    verify_noerr( err );
+    [v setSelectable: NO];
+    [v setBezeled:NO];
+    [v setBordered:NO];
 
-    if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
+    NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
+    if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
     {
-        TruncCode tCode = truncEnd;
         if ( style & wxST_ELLIPSIZE_MIDDLE )
-            tCode = truncMiddle;
-
-        err = peer->SetData( kControlStaticTextTruncTag, tCode );
-        err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
+            linebreak = NSLineBreakByTruncatingMiddle;
+        else if (style & wxST_ELLIPSIZE_END )
+            linebreak = NSLineBreakByTruncatingTail;
+        else if (style & wxST_ELLIPSIZE_START )
+            linebreak = NSLineBreakByTruncatingHead;
     }
-    return peer;
-    */
+    else
+    {
+        [[v cell] setWraps:YES];
+    }
+
+    wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
+    return c;
 }
 
 #endif //if wxUSE_STATTEXT