]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/textctrl.mm
Workaround for #15404: wxRichTextCtrl: caret does not disappear when focus is lost...
[wxWidgets.git] / src / cocoa / textctrl.mm
index 02114484bab6156a5fe49d0c1dc246bf0dccbae2..07df3cdae0c725c2256394190c0b24f34e45bcbe 100644 (file)
@@ -1,18 +1,20 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        cocoa/textctrl.mm
+// Name:        src/cocoa/textctrl.mm
 // Purpose:     wxTextCtrl
 // Author:      David Elliott
-// Modified by:
+// Modified by: Mark Oxenham
 // Created:     2003/03/16
-// RCS-ID:      $Id:
 // Copyright:   (c) 2003 David Elliott
-// Licence:    wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include "wx/wxprec.h"
+
+#include "wx/textctrl.h"
+
 #ifndef WX_PRECOMP
     #include "wx/app.h"
-    #include "wx/textctrl.h"
+    #include "wx/log.h"
 #endif //WX_PRECOMP
 
 #include "wx/cocoa/string.h"
 
 #import <Foundation/NSString.h>
 #import <AppKit/NSTextField.h>
+#import <AppKit/NSSecureTextField.h>
+#import <AppKit/NSCell.h>
+
+#include <math.h>
 
-IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
-BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
+BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
 END_EVENT_TABLE()
 WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView)
 
@@ -39,9 +44,10 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
     if(!CreateControl(parent,winid,pos,size,style,validator,name))
         return false;
     m_cocoaNSView = NULL;
-    SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
+    SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
     [m_cocoaNSView release];
-    [GetNSTextField() setStringValue:[NSString stringWithCString:value.c_str()]];
+    [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
+
     [GetNSControl() sizeToFit];
     NSRect currentFrame = [m_cocoaNSView frame];
     if(currentFrame.size.width < 70)
@@ -52,23 +58,62 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
     if(m_parent)
         m_parent->CocoaAddChild(this);
     SetInitialFrameRect(pos,size);
+
+    [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
+    [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
+
+    // set the text alignment option
+    NSTextAlignment alignStyle;
+    if (style & wxTE_RIGHT)
+        alignStyle = NSRightTextAlignment;
+    else if (style & wxTE_CENTRE)
+        alignStyle = NSCenterTextAlignment;
+    else // default to wxTE_LEFT because it is 0 and can't be tested
+        alignStyle = NSLeftTextAlignment;
+    [GetNSControl() setAlignment:alignStyle];
+
+    // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE)
+    if (style & wxTE_READONLY)
+    {
+        SetEditable(FALSE);
+    }
+
     return true;
 }
 
 wxTextCtrl::~wxTextCtrl()
 {
-    DisassociateNSTextField(m_cocoaNSView);
+    DisassociateNSTextField(GetNSTextField());
 }
 
 void wxTextCtrl::Cocoa_didChangeText(void)
 {
 }
 
+void wxTextCtrl::CocoaTarget_action(void)
+{
+    SendTextUpdatedEvent();
+}
+
 void wxTextCtrl::AppendText(wxString const&)
 {
 }
 
-void wxTextCtrl::SetEditable(bool)
+void wxTextCtrl::SetEditable(bool editable)
+{
+    // first ensure that the current value is stored (in case the user had not finished editing
+    // before SetEditable(FALSE) was called)
+    DoSetValue(GetValue(),1);
+
+    [GetNSTextField() setEditable: editable];
+
+    // forces the focus on the textctrl to be lost - while focus is still maintained
+    // after SetEditable(FALSE) the user may still edit the control
+    // (might not the best way to do this..)
+    [GetNSTextField() abortEditing];
+}
+
+void wxTextCtrl::MarkDirty()
 {
 }
 
@@ -124,10 +169,13 @@ void wxTextCtrl::Replace(long, long, wxString const&)
 {
 }
 
-void wxTextCtrl::SetValue(wxString const& value)
+void wxTextCtrl::DoSetValue(wxString const& value, int flags)
 {
     wxAutoNSAutoreleasePool pool;
     [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
+
+    if ( flags & SetValue_SendEvent )
+        SendTextUpdatedEvent();
 }
 
 void wxTextCtrl::WriteText(wxString const&)
@@ -136,7 +184,7 @@ void wxTextCtrl::WriteText(wxString const&)
 
 bool wxTextCtrl::IsEditable() const
 {
-    return true;
+    return [GetNSTextField() isEditable];
 }
 
 bool wxTextCtrl::IsModified() const
@@ -168,9 +216,10 @@ int wxTextCtrl::GetLineLength(long) const
     return 0;
 }
 
-long wxTextCtrl::GetLastPosition() const
+wxTextPos wxTextCtrl::GetLastPosition() const
 {
-    return 0;
+    // working - returns the size of the wxString
+    return (long)(GetValue().Len());
 }
 
 int wxTextCtrl::GetNumberOfLines() const
@@ -196,6 +245,18 @@ bool wxTextCtrl::CanUndo() const
 wxString wxTextCtrl::GetValue() const
 {
     wxAutoNSAutoreleasePool pool;
-    return wxString([[GetNSTextField() stringValue] lossyCString]);
+    return wxStringWithNSString([GetNSTextField() stringValue]);
 }
 
+wxSize wxTextCtrl::DoGetBestSize() const
+{
+    wxAutoNSAutoreleasePool pool;
+    wxASSERT(GetNSControl());
+    NSCell *cell = [GetNSControl() cell];
+    wxASSERT(cell);
+    NSSize cellSize = [cell cellSize];
+    wxSize size(100,(int)ceil(cellSize.height));
+
+    wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);
+    return size;
+}