]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/textctrl.cpp
Fix conversion error in tooltips.
[wxWidgets.git] / src / mac / textctrl.cpp
index ad911c398783ed0ab2b4de559f92f4ef80161887..6ebd852390580af98c23ba38761c31d017bfca6b 100644 (file)
@@ -24,6 +24,8 @@
   #include <stat.h>
 #endif
 
+#include "wx/msgdlg.h"
+
 #if wxUSE_STD_IOSTREAM
     #if wxUSE_IOSTREAMH
         #include <fstream.h>
@@ -53,9 +55,9 @@
 #include <Scrap.h>
 #endif
 #include <MacTextEditor.h>
-#include "ATSUnicode.h"
-#include "TextCommon.h"
-#include "TextEncodingConverter.h"
+#include <ATSUnicode.h>
+#include <TextCommon.h>
+#include <TextEncodingConverter.h>
 #include "wx/mac/uma.h"
 
 #define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL
@@ -67,8 +69,6 @@ extern wxControl *wxFindControlFromMacControl(ControlHandle inControl ) ;
 // moment is to avoid setting the true focus on the control, the proper solution at the end would be to have
 // an alternate path for carbon key events that routes automatically into the same wx flow of events
 
-#include "MacTextEditor.h"
-
 /* part codes */
 
 /* kmUPTextPart is the part code we return to indicate the user has clicked
@@ -216,8 +216,17 @@ static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart
                 InvalWindowRect( GetControlOwner( theControl ) , &oldbounds ) ;
             SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
             SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
-            SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
-                bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
+
+            if (!wxFindControlFromMacControl(theControl)->HasFlag(wxNO_BORDER))
+            {
+                SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
+                    bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
+            }
+            else
+            {
+                SetRect(&varsp->fRTextArea, bounds.left , bounds.top ,
+                    bounds.right, bounds.bottom);
+            }
             RectRgn(varsp->fTextBackgroundRgn, &varsp->fRTextOutline);
             if ( IsControlVisible( theControl ) )
                 TXNSetFrameBounds(  varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left,
@@ -237,9 +246,12 @@ static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart
             TXNDraw(varsp->fTXNRec, NULL);
                 /* restore the drawing environment */
                 /* draw the text frame and focus frame (if necessary) */
-            DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
-            if ((**tpvars).fIsActive && varsp->fInFocus) 
-                DrawThemeFocusRect(&varsp->fRFocusOutline, true);
+            if (!wxFindControlFromMacControl(theControl)->HasFlag(wxNO_BORDER))
+            {
+                DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
+                if ((**tpvars).fIsActive && varsp->fInFocus) 
+                    DrawThemeFocusRect(&varsp->fRFocusOutline, true);
+            }
                 /* release our globals */
             HSetState((Handle) tpvars, state);
         }
@@ -419,9 +431,12 @@ static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activati
             /* redraw the frame */
         if ( IsControlVisible( theControl ) )
         {
-            DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
-            if (varsp->fInFocus) 
-                DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive);
+            if (!wxFindControlFromMacControl(theControl)->HasFlag(wxNO_BORDER))
+            {
+                DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
+                if (varsp->fInFocus) 
+                    DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive);
+            }
         }
         HSetState((Handle) tpvars, state);
     }
@@ -482,11 +497,14 @@ static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlF
         new focus state */
         if ( IsControlVisible( theControl ) )
         {
+            if (!wxFindControlFromMacControl(theControl)->HasFlag(wxNO_BORDER))
+            {
            /* save the drawing state */
                SetPort((**tpvars).fDrawingEnvironment);
                wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
-            DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
-            DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus);
+                DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
+                DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus);
+            }
         }
             /* done */
         HSetState((Handle) tpvars, state);
@@ -548,8 +566,17 @@ OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle )
     GetControlBounds(theControl, &bounds);
     SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
     SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
-    SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
-        bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
+    if ((wxStyle & wxNO_BORDER) != wxNO_BORDER)
+    {
+        SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
+                bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
+    }
+    else
+    {
+        SetRect(&varsp->fRTextArea, bounds.left , bounds.top ,
+                bounds.right, bounds.bottom);
+    }
+    
         /* calculate the background region for the text.  In this case, it's kindof
         and irregular region because we're setting the scroll bar a little ways inside
         of the text area. */
@@ -657,6 +684,29 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
 END_EVENT_TABLE()
 #endif
 
+static void SetTXNData( TXNObject txn , const wxString& st , TXNOffset start , TXNOffset end )
+{
+#if wxUSE_UNICODE
+#if SIZEOF_WCHAR_T == 2
+       size_t len = st.Len() ;
+    TXNSetData( txn , kTXNUnicodeTextData,  (void*)st.wc_str(), len * 2,
+      start, end);
+#else
+       wxMBConvUTF16BE converter ;
+       ByteCount byteBufferLen = converter.WC2MB( NULL , st.wc_str() , 0 ) ;
+       UniChar *unibuf = (UniChar*) malloc(byteBufferLen) ;
+       converter.WC2MB( (char*) unibuf , st.wc_str() , byteBufferLen ) ;
+    TXNSetData( txn , kTXNUnicodeTextData,  (void*)unibuf, byteBufferLen ,
+      start, end);
+       free( unibuf ) ;
+#endif
+#else
+       wxCharBuffer text =  st.mb_str(wxConvLocal)  ;
+    TXNSetData( txn , kTXNTextData,  (void*)text.data(), strlen( text ) ,
+      start, end);
+#endif    
+}
+
 // Text item
 void wxTextCtrl::Init()
 {
@@ -708,7 +758,12 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
         return FALSE;
 
     wxSize mySize = size ;
-    if ( m_macUsesTXN )
+    if (style & wxNO_BORDER)
+    {
+        m_macHorizontalBorder = 0 ;
+        m_macVerticalBorder = 0 ;
+    }
+    else if ( m_macUsesTXN )
     {
         m_macHorizontalBorder = 5 ; // additional pixels around the real control
         m_macVerticalBorder = 3 ;
@@ -783,14 +838,7 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
         tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl);
             /* set the text in the record */
         m_macTXN =  (**tpvars).fTXNRec ;
-#if wxUSE_UNICODE
-        TXNSetData( ((TXNObject) m_macTXN) , kTXNUnicodeTextData,  (void*)st.wc_str(), st.Length() * 2,
-          kTXNStartOffset, kTXNEndOffset);
-#else
-       wxCharBuffer text =  st.mb_str(wxConvLocal)  ;
-        TXNSetData( ((TXNObject) m_macTXN) , kTXNTextData,  (void*)text.data(), strlen( text ) ,
-          kTXNStartOffset, kTXNEndOffset);
-#endif
+        SetTXNData( (TXNObject) m_macTXN , st , kTXNStartOffset, kTXNEndOffset ) ;
         m_macTXNvars = tpvars ;
         m_macUsesTXN = true ;
         TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
@@ -834,13 +882,20 @@ wxString wxTextCtrl::GetValue() const
         }
         else
         {
-            actualSize = GetHandleSize( theText ) ;
+            actualSize = GetHandleSize( theText ) / sizeof( UniChar) ;
             if ( actualSize > 0 )
             {
                 wxChar *ptr = result.GetWriteBuf(actualSize*sizeof(wxChar)) ;
+#if SIZEOF_WCHAR_T == 2                                
                 wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ;
+#else
+                               wxMBConvUTF16BE converter ;
+                               HLock( theText ) ;
+                               converter.MB2WC( ptr , (const char*)*theText , actualSize ) ;
+                               HUnlock( theText ) ;
+#endif
                 ptr[actualSize] = 0 ;
-                result.UngetWriteBuf( actualSize ) ;
+                result.UngetWriteBuf( actualSize *sizeof(wxChar) ) ;
             }
             DisposeHandle( theText ) ;
         }
@@ -878,7 +933,7 @@ void wxTextCtrl::GetSelection(long* from, long* to) const
    }
    else
    {
-        TXNGetSelection(  ((TXNObject) m_macTXN) , (TXNOffset*) from , (TXNOffset*) to ) ;
+        TXNGetSelection( (TXNObject) m_macTXN , (TXNOffset*) from , (TXNOffset*) to ) ;
    }
 }
 
@@ -896,14 +951,7 @@ void wxTextCtrl::SetValue(const wxString& str)
         bool formerEditable = m_editable ;
         if ( !formerEditable )
             SetEditable(true) ;
-#if wxUSE_UNICODE
-        TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData,  (void*)st.wc_str(), st.Length() * 2 ,
-          kTXNStartOffset, kTXNEndOffset);
-#else
-       wxCharBuffer text =  st.mb_str(wxConvLocal) ;
-        TXNSetData( ((TXNObject) m_macTXN), kTXNTextData,  (void*)text.data(), strlen( text ) ,
-          kTXNStartOffset, kTXNEndOffset);
-#endif
+        SetTXNData( (TXNObject) m_macTXN , st , kTXNStartOffset, kTXNEndOffset ) ;
         TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
         TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
         if ( !formerEditable )
@@ -1193,13 +1241,7 @@ void wxTextCtrl::Replace(long from, long to, const wxString& str)
             SetEditable(true) ;
         TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ;
         TXNClear( ((TXNObject) m_macTXN) ) ;
-#if wxUSE_UNICODE
-        TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData,  (void*)value.wc_str(), value.Length() * 2 ,
-          kTXNUseCurrentSelection, kTXNUseCurrentSelection);
-#else
-        TXNSetData( ((TXNObject) m_macTXN), kTXNTextData,  (void*)value.c_str(), value.Length(),
-            kTXNUseCurrentSelection, kTXNUseCurrentSelection);
-#endif
+        SetTXNData( (TXNObject) m_macTXN , str , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
         if ( !formerEditable )
             SetEditable( formerEditable ) ;
     }
@@ -1292,14 +1334,7 @@ void wxTextCtrl::WriteText(const wxString& str)
             SetEditable(true) ;
         long start , end , dummy ;
         GetSelection( &start , &dummy ) ;
-#if wxUSE_UNICODE
-        TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData,  (void*)st.wc_str(), st.Length() * 2 ,
-          kTXNUseCurrentSelection, kTXNUseCurrentSelection);
-#else
-       wxCharBuffer text =  st.mb_str(wxConvLocal) ;
-        TXNSetData( ((TXNObject) m_macTXN), kTXNTextData,  (void*)text.data(), strlen( text ) ,
-          kTXNUseCurrentSelection, kTXNUseCurrentSelection);
-#endif
+        SetTXNData( (TXNObject) m_macTXN , st , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
         GetSelection( &dummy , &end ) ;
         SetStyle( start , end , GetDefaultStyle() ) ;
         if ( !formerEditable )
@@ -1442,16 +1477,23 @@ void wxTextCtrl::DiscardEdits()
 
 int wxTextCtrl::GetNumberOfLines() const
 {
-    // TODO change this if possible to reflect real lines
-    wxString content = GetValue() ;
-
-    int count = 1;
-    for (size_t i = 0; i < content.Length() ; i++)
+    if ( m_macUsesTXN ) 
     {
-        if (content[i] == '\r') count++;
+        ItemCount lines ;
+        TXNGetLineCount((TXNObject)m_macTXN, &lines ) ;
+        return lines ;
     }
+    else
+    {          
+        wxString content = GetValue() ;
 
-    return count;
+        int count = 1;
+        for (size_t i = 0; i < content.Length() ; i++)
+        {
+            if (content[i] == '\r') count++;
+        }
+        return count;
+    }
 }
 
 long wxTextCtrl::XYToPosition(long x, long y) const
@@ -1467,7 +1509,27 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
 
 void wxTextCtrl::ShowPosition(long pos)
 {
-    // TODO
+#if TARGET_RT_MAC_MACHO && defined(AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER)
+    if ( m_macUsesTXN ) 
+    {
+        Point current ;
+        Point desired ;
+        TXNOffset selstart , selend ;
+        TXNGetSelection(  (TXNObject) m_macTXN , &selstart , &selend) ;
+        TXNOffsetToPoint( (TXNObject) m_macTXN,  selstart , &current);
+        TXNOffsetToPoint( (TXNObject) m_macTXN,  pos , &desired);
+        //TODO use HIPoints for 10.3 and above
+        if ( (UInt32) TXNScroll != (UInt32) kUnresolvedCFragSymbolAddress )
+        {
+            OSErr theErr = noErr;
+            SInt32 dv = desired.v - current.v ;
+            SInt32 dh = desired.h - current.h ;
+            TXNShowSelection(  (TXNObject) m_macTXN , true ) ;
+            theErr = TXNScroll( (TXNObject) m_macTXN, kTXNScrollUnitsInPixels , kTXNScrollUnitsInPixels , &dv , &dh );          
+            wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
+        }
+    }
+#endif
 }
 
 int wxTextCtrl::GetLineLength(long lineNo) const