]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/stattext.mm
notify the event loop that synthesized events are on the queue, wait for them to...
[wxWidgets.git] / src / osx / cocoa / stattext.mm
index 74e3aad2f6806e92ac2608b04fae3108b9b74aaa..591a97a4764a1e9fb6475d751d498891409745dd 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Stefan Csomor
 // Modified by:
 // Created:     04/01/98
-// RCS-ID:      $Id: stattext.cpp 54845 2008-07-30 14:52:41Z SC $
+// RCS-ID:      $Id$
 // Copyright:   (c) Stefan Csomor
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include "wx/osx/private.h"
 
+#if wxUSE_MARKUP
+    #include "wx/osx/cocoa/private/markuptoattr.h"
+#endif // wxUSE_MARKUP
+
 #include <stdio.h>
 
-@interface wxNSStaticTextView : NSTextView
+@interface wxNSStaticTextView : NSTextField
 {
-    wxWidgetCocoaImpl* impl;
 }
-
-- (void) setImplementation:(wxWidgetCocoaImpl*) item;
-- (wxWidgetCocoaImpl*) implementation;
 @end
+
 @implementation wxNSStaticTextView
 
 + (void)initialize
 {
     static BOOL initialized = NO;
-    if (!initialized) 
-    {    
+    if (!initialized)
+    {
         initialized = YES;
         wxOSXCocoaClassAddWXMethods( self );
     }
 }
 
-- (wxWidgetCocoaImpl*) implementation
-{
-    return impl;
-}
+- (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]]; 
+        } 
+    } 
+} 
 
-- (void) setImplementation:(wxWidgetCocoaImpl*) item
-{
-    impl = item;
-}
 @end
 
-
-wxSize wxStaticText::DoGetBestSize() const
+class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
 {
-Point bounds;
-
-#if wxOSX_USE_ATSU_TEXT
-    OSStatus err = noErr;
-    wxCFStringRef str( m_label,  GetFont().GetEncoding() );
-
-    SInt16 baseline;
-    if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
+public:
+    wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
     {
-        err = GetThemeTextDimensions(
-            (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
-            m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
-        verify_noerr( err );
+        m_lineBreak = lineBreak;
     }
-    else
-#endif
+
+    virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
     {
-        wxClientDC dc(const_cast<wxStaticText*>(this));
-        wxCoord width, height ;
-        dc.GetMultiLineTextExtent( m_label , &width, &height);
-        // FIXME: Some labels seem to have their last characters
-        // stripped out.  Adding 12 pixels seems to be enough to fix this.
-        // Perhaps m_label is not being synced properly...
-        bounds.h = width+12;
-        bounds.v = height;
+        wxCFStringRef text( title , encoding );
+
+        NSMutableAttributedString *
+            attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
+        DoSetAttrString(attrstring);
+        [attrstring release];
     }
 
-    if ( m_label.empty() )
-        bounds.h = 0;
+#if wxUSE_MARKUP
+    virtual void SetLabelMarkup( const wxString& markup)
+    {
+        wxMarkupToAttrString toAttr(GetWXPeer(), markup);
 
-    bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
-    bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
+        DoSetAttrString(toAttr.GetNSAttributedString());
+    }
+#endif // wxUSE_MARKUP
 
-    return wxSize( bounds.h, bounds.v );
-}
+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];
+    }
 
-// for wxST_ELLIPSIZE_* support:
+    NSLineBreakMode m_lineBreak;
+};
 
-/*
-   FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
-          to allow correct dynamic ellipsizing of the label
-*/
+wxSize wxStaticText::DoGetBestSize() const
+{
+    return wxWindowMac::DoGetBestSize() ;
+}
 
 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
-                                    wxWindowMac* parent,
-                                    wxWindowID id,
-                                    const wxString& label,
+                                    wxWindowMac* WXUNUSED(parent),
+                                    wxWindowID WXUNUSED(id),
+                                    const wxString& WXUNUSED(label),
                                     const wxPoint& pos,
                                     const wxSize& size,
                                     long style,
-                                    long extraStyle)
+                                    long WXUNUSED(extraStyle))
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
     wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
 
     [v setEditable:NO];
     [v setDrawsBackground:NO];
-    [v setString: wxCFStringRef( label , wxpeer->GetFont().GetEncoding() ).AsNSString()];
-    
-    NSRange allText = NSMakeRange(0, label.length());
-    if (style & wxALIGN_CENTER)
-        [v setAlignment: NSCenterTextAlignment range: allText];
-    else if (style & wxALIGN_RIGHT)
-        [v setAlignment: NSRightTextAlignment range: allText];
+    [v setSelectable: NO];
+    [v setBezeled:NO];
+    [v setBordered:NO];
 
-    wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
-    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 );
-
-    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;
+    }
+    else
+    {
+        [[v cell] setWraps:YES];
     }
-    return peer;
-    */
+
+    wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
+    return c;
 }
 
 #endif //if wxUSE_STATTEXT