]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/textctrl.cpp
restored custom drawing (was accidentally disabled by the last 2 commits)
[wxWidgets.git] / src / msw / textctrl.cpp
index ba253ef85b484126d7aeea2784b8f35581add939..33137b7274e83621f4df52b870c3480e901f302f 100644 (file)
 #include <windowsx.h>
 
 #include "wx/msw/private.h"
+#include "wx/msw/winundef.h"
 
 #include <string.h>
 #include <stdlib.h>
+
+#ifndef __WXWINCE__
 #include <sys/types.h>
+#endif
 
 #if wxUSE_RICHEDIT
 
@@ -140,6 +144,8 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
 #ifdef __WIN16__
     EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
 #endif
+
+    EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
 END_EVENT_TABLE()
 
 // ============================================================================
@@ -158,6 +164,7 @@ void wxTextCtrl::Init()
 
     m_privateContextMenu = NULL;
     m_suppressNextUpdate = FALSE;
+    m_isNativeCaretShown = true;
 }
 
 wxTextCtrl::~wxTextCtrl()
@@ -461,30 +468,32 @@ wxString wxTextCtrl::GetRange(long from, long to) const
         int len = GetWindowTextLength(GetHwnd());
         if ( len > from )
         {
-            // alloc one extra WORD as needed by the control
-            wxChar *p = str.GetWriteBuf(++len);
+            {
+                // alloc one extra WORD as needed by the control
+                wxStringBuffer tmp(str, ++len);
+                wxChar *p = tmp;
 
-            TEXTRANGE textRange;
-            textRange.chrg.cpMin = from;
-            textRange.chrg.cpMax = to == -1 ? len : to;
-            textRange.lpstrText = p;
+                TEXTRANGE textRange;
+                textRange.chrg.cpMin = from;
+                textRange.chrg.cpMax = to == -1 ? len : to;
+                textRange.lpstrText = p;
 
-            (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
+                (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE,
+                                  0, (LPARAM)&textRange);
 
-            if ( m_verRichEdit > 1 )
-            {
-                // RichEdit 2.0 uses just CR ('\r') for the newlines which is
-                // neither Unix nor Windows style - convert it to something
-                // reasonable
-                for ( ; *p; p++ )
+                if ( m_verRichEdit > 1 )
                 {
-                    if ( *p == _T('\r') )
-                        *p = _T('\n');
+                    // RichEdit 2.0 uses just CR ('\r') for the
+                    // newlines which is neither Unix nor Windows
+                    // style - convert it to something reasonable
+                    for ( ; *p; p++ )
+                    {
+                        if ( *p == _T('\r') )
+                            *p = _T('\n');
+                    }
                 }
             }
 
-            str.UngetWriteBuf();
-
             if ( m_verRichEdit == 1 )
             {
                 // convert to the canonical form - see comment below
@@ -1189,13 +1198,16 @@ wxString wxTextCtrl::GetLineText(long lineNo) const
     len += sizeof(WORD);
 
     wxString str;
-    wxChar *buf = str.GetWriteBuf(len);
-
-    *(WORD *)buf = (WORD)len;
-    len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
-    buf[len] = 0;
+    {
+        wxStringBufferLength tmp(str, len);
+        wxChar *buf = tmp;
 
-    str.UngetWriteBuf(len);
+        *(WORD *)buf = (WORD)len;
+        len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE,
+                                    lineNo, (LPARAM)buf);
+        buf[len] = 0;
+        tmp.SetLength(len);
+    }
 
     return str;
 }
@@ -1236,6 +1248,27 @@ bool wxTextCtrl::CanRedo() const
     return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
 }
 
+// ----------------------------------------------------------------------------
+// caret handling (Windows only)
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::ShowNativeCaret(bool show)
+{
+    if ( show != m_isNativeCaretShown )
+    {
+        if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
+        {
+            // not an error, may simply indicate that it's not shown/hidden
+            // yet (i.e. it had been hidden/showh 2 times before)
+            return false;
+        }
+
+        m_isNativeCaretShown = show;
+    }
+
+    return true;
+}
+
 // ----------------------------------------------------------------------------
 // implemenation details
 // ----------------------------------------------------------------------------
@@ -1631,7 +1664,7 @@ void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
     Redo();
 }
 
-void wxTextCtrl::OnDelete(wxCommandEvent& event)
+void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
 {
     long from, to;
     GetSelection(& from, & to);
@@ -1639,7 +1672,7 @@ void wxTextCtrl::OnDelete(wxCommandEvent& event)
         Remove(from, to);
 }
 
-void wxTextCtrl::OnSelectAll(wxCommandEvent& event)
+void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
 {
     SetSelection(-1, -1);
 }
@@ -1707,6 +1740,15 @@ void wxTextCtrl::OnRightClick(wxMouseEvent& event)
     event.Skip();
 }
 
+void wxTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
+{
+    // be sure the caret remains invisible if the user had hidden it
+    if ( !m_isNativeCaretShown )
+    {
+        ::HideCaret(GetHwnd());
+    }
+}
+
 // the rest of the file only deals with the rich edit controls
 #if wxUSE_RICHEDIT
 
@@ -2230,6 +2272,7 @@ void wxRichEditModule::OnExit()
         if ( ms_hRichEdit[i] )
         {
             ::FreeLibrary(ms_hRichEdit[i]);
+            ms_hRichEdit[i] = NULL;
         }
     }
 }