]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/textctrl.mm
fixing native font info support bugs
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
index 308f0b8e9c1e43d7712cfe001d1e69265a629f6b..ca728cf9db4df97fcd8b3a84ae21b579b41ea2c1 100644 (file)
 
 @interface wxNSSecureTextField : NSSecureTextField
 {
-    wxWidgetCocoaImpl* impl;
 }
-
-- (void) setImplementation:(wxWidgetCocoaImpl*) item;
-- (wxWidgetCocoaImpl*) implementation;
 @end
 
 @implementation wxNSSecureTextField 
     }
 }
 
-- (wxWidgetCocoaImpl*) implementation
-{
-    return impl;
-}
-
-- (void) setImplementation:(wxWidgetCocoaImpl*) item
-{
-    impl = item;
-}
-
 - (void)controlTextDidChange:(NSNotification *)aNotification
 {
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
     if ( impl )
     {
         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
     }
 }
 
+- (void)controlTextDidEndEditing:(NSNotification *)aNotification
+{
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+    if ( impl )
+    {
+        impl->DoNotifyFocusEvent( false, NULL );
+    }
+}
+
+@end
+
+@interface wxNSTextScrollView : NSScrollView
+{
+}
 @end
 
-@interface wxNSTextView : NSScrollView
+@interface wxNSTextView : NSTextView
 {
-    wxWidgetCocoaImpl* impl;
+    wxNSTextScrollView* scrollView;
 }
 
-- (void) setImplementation:(wxWidgetCocoaImpl*) item;
-- (wxWidgetCocoaImpl*) implementation;
+- (void)setScrollView: (wxNSTextScrollView *) sv;
+- (wxNSTextScrollView*) scrollView;
+
 @end
 
-@implementation wxNSTextView
+@implementation wxNSTextScrollView
 
 + (void)initialize
 {
     }
 }
 
-- (wxWidgetCocoaImpl*) implementation
-{
-    return impl;
-}
-
-- (void) setImplementation:(wxWidgetCocoaImpl*) item
-{
-    impl = item;
-}
-
-
-- (void)controlTextDidChange:(NSNotification *)aNotification
+- (void)textDidChange:(NSNotification *)aNotification
 {
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
     if ( impl )
     {
         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
         }
     }
 }
+
+- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector
+{
+    wxUnusedVar(aTextView);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+    if ( impl  )
+    {
+        wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
+        if (commandSelector == @selector(insertNewline:))
+        {
+            if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) 
+            {
+                wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
+                event.SetEventObject( wxpeer );
+                event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
+                wxpeer->HandleWindowEvent( event );
+            }
+        }
+    }
+    
+    return NO;
+}
+
+- (void)textDidEndEditing:(NSNotification *)aNotification
+{
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+    if ( impl )
+    {
+        impl->DoNotifyFocusEvent( false, NULL );
+    }
+}
+@end
+
+@implementation wxNSTextView
+
+- (BOOL) becomeFirstResponder
+{
+    BOOL val = [super becomeFirstResponder];
+    
+    if ( val )
+    {
+        wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView );
+        if (impl )
+            impl->DoNotifyFocusEvent( true, NULL );
+
+    }
+    return val;
+}
+
+- (void)setScrollView: (wxNSTextScrollView *) sv
+{
+    scrollView = sv;
+}
+
+- (wxNSTextScrollView*) scrollView
+{
+    return scrollView;
+}
+
 @end
 
 @implementation wxNSTextField
     }
 }
 
-- (wxWidgetCocoaImpl*) implementation
+- (void) setEnabled:(BOOL) flag
 {
-    return impl;
-}
+    [super setEnabled: flag];
 
-- (void) setImplementation:(wxWidgetCocoaImpl*) item
-{
-    impl = item;
+    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)controlTextDidChange:(NSNotification *)aNotification
 {
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
     if ( impl )
     {
         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
     }
 }
 
-/*
+typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
+
+- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
+{
+    wxUnusedVar(textView);
+    wxUnusedVar(control);
+    if (commandSelector == @selector(insertNewline:))
+    {
+        wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+        if ( impl  )
+        {
+            wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
+            if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) 
+            {
+                wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
+                event.SetEventObject( wxpeer );
+                event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
+                wxpeer->HandleWindowEvent( event );
+            }
+        }
+    }
+    
+    return NO;
+}
+
 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
 {
+    wxUnusedVar(aNotification);
+    wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
     if ( impl )
     {
-        wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
-        if ( wxpeer ) {
-            wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
-            event.SetEventObject( wxpeer );
-            event.SetWindow( wxpeer );
-            wxpeer->HandleWindowEvent( event );
-        }
+        impl->DoNotifyFocusEvent( false, NULL );
     }
 }
-*/
 @end
 
 // wxNSTextViewControl
 
 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
 {
-    m_scrollView = (NSScrollView*) w;
-    [w setImplementation: this];
+    wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
+    m_scrollView = sv;
     
     [m_scrollView setHasVerticalScroller:YES];
     [m_scrollView setHasHorizontalScroller:NO];
     [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     NSSize contentSize = [m_scrollView contentSize];
     
-    m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
+    wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
             contentSize.width, contentSize.height)];
-    [m_textView setVerticallyResizable:YES];
-    [m_textView setHorizontallyResizable:NO];
-    [m_textView setAutoresizingMask:NSViewWidthSizable];
+    m_textView = tv;
+    [tv setVerticallyResizable:YES];
+    [tv setHorizontallyResizable:NO];
+    [tv setAutoresizingMask:NSViewWidthSizable];
     
-    [m_scrollView setDocumentView: m_textView];
+    [m_scrollView setDocumentView: tv];
 
-    [m_textView setDelegate: w];
+    [tv setDelegate: w];
+    [tv setScrollView:sv];
 }
 
 wxNSTextViewControl::~wxNSTextViewControl()
@@ -282,7 +365,9 @@ void wxNSTextViewControl::GetSelection( long* from, long* to) const
 
 void wxNSTextViewControl::SetSelection( long from , long to )
 {
-    [m_textView setSelectedRange:NSMakeRange(from, to-from)];
+    NSRange selrange = NSMakeRange(from, to-from);
+    [m_textView setSelectedRange:selrange];
+    [m_textView scrollRangeToVisible:selrange];
 }
 
 void wxNSTextViewControl::WriteText(const wxString& str) 
@@ -290,6 +375,7 @@ void wxNSTextViewControl::WriteText(const wxString& str)
     // temp hack to get logging working early
     wxString former = GetStringValue();
     SetStringValue( former + str );
+    SetSelection(GetStringValue().length(), GetStringValue().length());
 }
 
 // wxNSTextFieldControl
@@ -377,9 +463,11 @@ void wxNSTextFieldControl::WriteText(const wxString& str)
     // temp hack to get logging working early
     wxString former = GetStringValue();
     SetStringValue( former + str );
+    SetSelection(GetStringValue().length(), GetStringValue().length());
 }
 
-void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
+void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), 
+    void* WXUNUSED(_cmd), void *WXUNUSED(sender))
 {
     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
     if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) 
@@ -396,26 +484,27 @@ void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
 //
 
 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, 
-                                    wxWindowMac* parent
-                                    wxWindowID id
+                                    wxWindowMac* WXUNUSED(parent)
+                                    wxWindowID WXUNUSED(id)
                                     const wxString& str,
                                     const wxPoint& pos, 
                                     const wxSize& size,
                                     long style, 
-                                    long extraStyle)
+                                    long WXUNUSED(extraStyle))
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
-    NSTextField* v = nil;
     wxWidgetCocoaImpl* c = NULL;
     
     if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
     {
-        v = [[wxNSTextView alloc] initWithFrame:r];
+        wxNSTextScrollView* v = nil;
+        v = [[wxNSTextScrollView alloc] initWithFrame:r];
         c = new wxNSTextViewControl( wxpeer, v );
         static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
     }
     else 
     {
+        NSTextField* v = nil;
         if ( style & wxTE_PASSWORD )
             v = [[wxNSSecureTextField alloc] initWithFrame:r];
         else
@@ -431,7 +520,6 @@ wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
         [v setBordered:NO];
         
         c = new wxNSTextFieldControl( wxpeer, v );
-        [v setImplementation: c];
         static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
     }