]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/textctrl.cpp
added spinctrl implementation
[wxWidgets.git] / src / os2 / textctrl.cpp
index 720e6082be0ddc57b38b5d63999634d7d0435d1c..acbe4bca0a37a43ce4a573efe66f01c15c9d8d9f 100644 (file)
@@ -1,40 +1,59 @@
 /////////////////////////////////////////////////////////////////////////////
 // Name:        textctrl.cpp
 // Purpose:     wxTextCtrl
 /////////////////////////////////////////////////////////////////////////////
 // Name:        textctrl.cpp
 // Purpose:     wxTextCtrl
-// Author:      AUTHOR
+// Author:      David Webster
 // Modified by:
 // Modified by:
-// Created:     ??/??/98
+// Created:     10/17/99
 // RCS-ID:      $Id$
 // RCS-ID:      $Id$
-// Copyright:   (c) AUTHOR
-// Licence:    wxWindows licence
+// Copyright:   (c) David Webster
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation "textctrl.h"
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/textctrl.h"
+    #include "wx/settings.h"
+    #include "wx/brush.h"
+    #include "wx/utils.h"
+    #include "wx/log.h"
 #endif
 
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fstream.h>
+#if wxUSE_CLIPBOARD
+    #include "wx/app.h"
+    #include "wx/clipbrd.h"
+#endif
+
+#include "wx/textfile.h"
 
 
-#include "wx/textctrl.h"
-#include "wx/settings.h"
-#include "wx/filefn.h"
-#include "wx/utils.h"
+#include "wx/os2/private.h"
 
 
-#if defined(__BORLANDC__) && !defined(__WIN32__)
-#include <alloc.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#if wxUSE_IOSTREAMH
+#   include <fstream.h>
 #else
 #else
-#ifndef __GNUWIN32__
-#include <malloc.h>
-#endif
+#   include <fstream>
 #endif
 
 #endif
 
-#if !USE_SHARED_LIBRARY
+
+// ----------------------------------------------------------------------------
+// event tables and other macros
+// ----------------------------------------------------------------------------
+
 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
 
 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
 
 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
-       EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
+    EVT_CHAR(wxTextCtrl::OnChar)
+    EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
+
     EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
     EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
     EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
     EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
     EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
     EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
@@ -47,299 +66,662 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
     EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
     EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
 END_EVENT_TABLE()
     EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
     EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
 END_EVENT_TABLE()
-#endif
 
 
-// Text item
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// creation
+// ----------------------------------------------------------------------------
+
 wxTextCtrl::wxTextCtrl()
 wxTextCtrl::wxTextCtrl()
-#ifndef NO_TEXT_WINDOW_STREAM
- :streambuf()
-#endif
 {
 {
-    m_fileName = "";
 }
 
 }
 
-bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
-                  const wxString& value,
-           const wxPoint& pos,
-           const wxSize& size, long style,
-           const wxValidator& validator,
-           const wxString& name)
-{
-    m_fileName = "";
-    SetName(name);
-    SetValidator(validator);
-    if (parent) parent->AddChild(this);
+bool wxTextCtrl::Create(
+  wxWindow*                         pParent
+, wxWindowID                        vId
+, const wxString&                   rsValue
+, const wxPoint&                    rPos
+, const wxSize&                     rSize
+, long                              lStyle
+#if wxUSE_VALIDATORS
+, const wxValidator&                rValidator
+#endif
+, const wxString&                   rsName
+)
+{
+    //
+    // Base initialization
+    //
+    if ( !CreateBase( pParent
+                     ,vId
+                     ,rPos
+                     ,rSize
+                     ,lStyle
+#if wxUSE_VALIDATORS
+                     ,rValidator
+#endif
+                     ,rsName
+                    ))
+        return FALSE;
+
+    if (pParent )
+        pParent->AddChild(this);
 
 
-    m_windowStyle = style;
+    m_windowStyle = lStyle;
 
 
-    if ( id == -1 )
-           m_windowId = (int)NewControlId();
+    long                            lSstyle = WS_VISIBLE | WS_TABSTOP;
+
+    //
+    // Single and multiline edit fields are two different controls in PM
+    //
+    if ( m_windowStyle & wxTE_MULTILINE )
+    {
+        m_bIsMLE = TRUE;
+        m_windowStyle |= wxTE_PROCESS_ENTER;
+
+        if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
+            lSstyle |= MLS_VSCROLL;
+        if (m_windowStyle & wxHSCROLL)
+            lSstyle |= MLS_HSCROLL;
+        if (m_windowStyle & wxTE_READONLY)
+            lSstyle |= MLS_READONLY;
+    }
+    else
+    {
+        lSstyle |= ES_LEFT;
+
+        if (m_windowStyle & wxHSCROLL)
+            lSstyle |=  ES_AUTOSCROLL;
+        if (m_windowStyle & wxTE_READONLY)
+            lSstyle |= ES_READONLY;
+        if (m_windowStyle & wxTE_PASSWORD) // hidden input
+            lSstyle |= ES_UNREADABLE;
+    }
+    if (m_bIsMLE)
+    {
+        m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
+                                           ,WC_MLE                   // Window class
+                                           ,(PSZ)rsValue.c_str()     // Initial Text
+                                           ,(ULONG)lSstyle           // Style flags
+                                           ,(LONG)rPos.x             // X pos of origin
+                                           ,(LONG)rPos.y             // Y pos of origin
+                                           ,(LONG)rSize.x            // field width
+                                           ,(LONG)rSize.y            // field height
+                                           ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
+                                           ,HWND_TOP                 // initial z position
+                                           ,(ULONG)vId               // Window identifier
+                                           ,NULL                     // no control data
+                                           ,NULL                     // no Presentation parameters
+                                          );
+    }
     else
     else
-           m_windowId = id;
+    {
+        m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
+                                           ,WC_ENTRYFIELD            // Window class
+                                           ,(PSZ)rsValue.c_str()     // Initial Text
+                                           ,(ULONG)lSstyle           // Style flags
+                                           ,(LONG)rPos.x             // X pos of origin
+                                           ,(LONG)rPos.y             // Y pos of origin
+                                           ,(LONG)rSize.x            // field width
+                                           ,(LONG)rSize.y            // field height
+                                           ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
+                                           ,HWND_TOP                 // initial z position
+                                           ,(ULONG)vId               // Window identifier
+                                           ,NULL                     // no control data
+                                           ,NULL                     // no Presentation parameters
+                                          );
+    }
 
 
+    if (m_hWnd == 0)
+    {
+        return FALSE;
+    }
+
+    SubclassWin(GetHWND());
+
+    //
+    // Set font, position, size and initial value
+    //
+    wxFont&                         vFontParent = pParent->GetFont();
+
+    if (vFontParent.Ok())
+    {
+        SetFont(vFontParent);
+    }
+    else
+    {
+        SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
+    }
+    if (!rsValue.IsEmpty())
+    {
+        SetValue(rsValue);
+    }
+    SetupColours();
+    SetSize( rPos.x
+            ,rPos.y
+            ,rSize.x
+            ,rSize.y
+           );
     return TRUE;
     return TRUE;
+} // end of wxTextCtrl::Create
+
+//
+// Make sure the window style (etc.) reflects the HWND style (roughly)
+//
+void wxTextCtrl::AdoptAttributesFromHWND()
+{
+    HWND                            hWnd = GetHwnd();
+    LONG                            lStyle = ::WinQueryWindowULong(hWnd, QWL_STYLE);
+
+    wxWindow::AdoptAttributesFromHWND();
+
+    if (m_bIsMLE)
+    {
+        m_windowStyle |= wxTE_MULTILINE;
+        if (lStyle & MLS_READONLY)
+            m_windowStyle |= wxTE_READONLY;
+    }
+    else
+    {
+        if (lStyle & ES_UNREADABLE)
+            m_windowStyle |= wxTE_PASSWORD;
+        if (lStyle & ES_READONLY)
+            m_windowStyle |= wxTE_READONLY;
+    }
 }
 
 }
 
+void wxTextCtrl::SetupColours()
+{
+    // FIXME why is bg colour not inherited from parent?
+    SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
+    SetForegroundColour(GetParent()->GetForegroundColour());
+}
+
+// ----------------------------------------------------------------------------
+// set/get the controls text
+// ----------------------------------------------------------------------------
+
 wxString wxTextCtrl::GetValue() const
 {
 wxString wxTextCtrl::GetValue() const
 {
-    // TODO
-    return wxString("");
+    return wxGetWindowText(GetHWND());
 }
 
 void wxTextCtrl::SetValue(const wxString& value)
 {
 }
 
 void wxTextCtrl::SetValue(const wxString& value)
 {
-    // TODO
+// TODO:
+/*
+    wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
+
+    SetWindowText(GetHwnd(), valueDos);
+
+    AdjustSpaceLimit();
+*/
 }
 
 }
 
-void wxTextCtrl::SetSize(int x, int y, int width, int height, int sizeFlags)
+void wxTextCtrl::WriteText(const wxString& value)
 {
 {
-    // TODO
+// TODO:
+/*
+    wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
+
+    SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
+
+    AdjustSpaceLimit();
+*/
 }
 
 }
 
-// Clipboard operations
-void wxTextCtrl::Copy()
+void wxTextCtrl::AppendText(const wxString& text)
 {
 {
-    // TODO
+// TODO:
+/*
+    SetInsertionPointEnd();
+    WriteText(text);
+*/
 }
 
 }
 
-void wxTextCtrl::Cut()
+void wxTextCtrl::Clear()
 {
 {
-    // TODO
+//    SetWindowText(GetHwnd(), wxT(""));
 }
 
 }
 
-void wxTextCtrl::Paste()
+// ----------------------------------------------------------------------------
+// Clipboard operations
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::Copy()
 {
 {
-    // TODO
-}
+    if (CanCopy())
+    {
+        HWND hWnd = GetHwnd();
+        if (m_bIsMLE)
+            ::WinSendMsg(hWnd, MLM_COPY, 0, 0);
+        else
+            ::WinSendMsg(hWnd, EM_COPY, 0, 0);
+    }
+} // end of wxTextCtrl::Copy
 
 
-void wxTextCtrl::SetEditable(bool editable)
+void wxTextCtrl::Cut()
 {
 {
-    // TODO
+    if (CanCut())
+    {
+        HWND hWnd = GetHwnd();
+
+        if (m_bIsMLE)
+            ::WinSendMsg(hWnd, MLM_CUT, 0, 0);
+        else
+            ::WinSendMsg(hWnd, EM_CUT, 0, 0);
+    }
 }
 
 }
 
-void wxTextCtrl::SetInsertionPoint(long pos)
+void wxTextCtrl::Paste()
 {
 {
-    // TODO
+    if (CanPaste())
+    {
+        HWND hWnd = GetHwnd();
+//        SendMessage(hWnd, WM_PASTE, 0, 0L);
+    }
 }
 
 }
 
-void wxTextCtrl::SetInsertionPointEnd()
+bool wxTextCtrl::CanCopy() const
 {
 {
-    long pos = GetLastPosition();
-    SetInsertionPoint(pos);
+    // Can copy if there's a selection
+    long from = 0L;
+    long to = 0L;
+//    GetSelection(& from, & to);
+    return (from != to);
 }
 
 }
 
-long wxTextCtrl::GetInsertionPoint() const
+bool wxTextCtrl::CanCut() const
 {
 {
-    // TODO
-    return 0;
+    // Can cut if there's a selection
+    long from = 0L;
+    long to = 0L;
+//    GetSelection(& from, & to);
+    return (from != to);
 }
 
 }
 
-long wxTextCtrl::GetLastPosition() const
+bool wxTextCtrl::CanPaste() const
 {
 {
-    // TODO
-    return 0;
-}
+    bool                            bIsTextAvailable = FALSE;
 
 
-void wxTextCtrl::Replace(long from, long to, const wxString& value)
+    if (!IsEditable())
+        return FALSE;
+
+    //
+    // Check for straight text on clipboard
+    //
+    if (::WinOpenClipbrd(vHabmain))
+    {
+        bIsTextAvailable = (::WinQueryClipbrdData(vHabmain, CF_TEXT) != 0);
+        ::WinCloseClipbrd(vHabmain);
+    }
+    return bIsTextAvailable;
+} // end of wxTextCtrl::CanPaste
+
+// ----------------------------------------------------------------------------
+// Accessors
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::SetEditable(bool editable)
 {
 {
-    // TODO
+    HWND hWnd = GetHwnd();
+//    SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
 }
 
 }
 
-void wxTextCtrl::Remove(long from, long to)
+void wxTextCtrl::SetInsertionPoint(long pos)
 {
 {
-    // TODO
+// TODO:
+/*
+    HWND hWnd = GetHwnd();
+    {
+        SendMessage(hWnd, EM_SETSEL, pos, pos);
+        SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
+    }
+    static const char *nothing = "";
+    SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
+*/
 }
 
 }
 
-void wxTextCtrl::SetSelection(long from, long to)
+void wxTextCtrl::SetInsertionPointEnd()
 {
 {
-    // TODO
 }
 
 }
 
-bool wxTextCtrl::LoadFile(const wxString& file)
+long wxTextCtrl::GetInsertionPoint() const
 {
 {
-    if (!wxFileExists(file))
-        return FALSE;
+    WXDWORD                         dwPos = 0L;
 
 
-    m_fileName = file;
+    if (m_bIsMLE)
+        dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), MLM_QUERYSEL, (MPARAM)MLFQS_MINSEL, 0);
+    else
+    {
+        dwPos = (WXDWORD)::WinSendMsg(GetHwnd(), EM_QUERYSEL, 0, 0);
+        dwPos = SHORT1FROMMP((MPARAM)dwPos);  // the first 16 bit value is the min pos
+    }
+    return (dwPos & 0xFFFF);
+} // end of wxTextCtrl::GetInsertionPoint
 
 
-    Clear();
+long wxTextCtrl::GetLastPosition() const
+{
+    HWND                            hWnd = GetHwnd();
+    long                            lCharIndex;
+    long                            lLineLength;
 
 
-    ifstream input((char*) (const char*) file, ios::nocreate | ios::in);
+    if (m_bIsMLE)
+    {
+        lCharIndex = 0;
 
 
-    if (!input.bad())
+        //
+        // This just gets the total text length.  The last will be this value
+        //
+        lLineLength = (long)::WinSendMsg(hWnd, MLM_QUERYTEXTLENGTH, 0, 0);
+    }
+    else
     {
     {
-        struct stat stat_buf;
-        if (stat(file, &stat_buf) < 0)
-            return FALSE;
-        // This may need to be a bigger buffer than the file size suggests,
-        // if it's a UNIX file. Give it an extra 1000 just in case.
-        char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000));
-        long no_lines = 0;
-        long pos = 0;
-        while (!input.eof() && input.peek() != EOF)
+        WNDPARAMS                   vParams;
+
+        lCharIndex = 0;
+        vParams.fsStatus = WPM_CCHTEXT;
+        if (::WinSendMsg( GetHwnd()
+                         ,WM_QUERYWINDOWPARAMS
+                         ,&vParams
+                         ,0
+                        ))
         {
         {
-            input.getline(wxBuffer, 500);
-               int len = strlen(wxBuffer);
-               wxBuffer[len] = 13;
-               wxBuffer[len+1] = 10;
-               wxBuffer[len+2] = 0;
-               strcpy(tmp_buffer+pos, wxBuffer);
-               pos += strlen(wxBuffer);
-               no_lines++;
-         }
+            lLineLength = (long)vParams.cchText;
+        }
+        else
+            lLineLength = 0;
+    }
+    return(lCharIndex + lLineLength);
+} // end of wxTextCtrl::GetLastPosition
 
 
-         // TODO add line
+// If the return values from and to are the same, there is no
+// selection.
+void wxTextCtrl::GetSelection(long* from, long* to) const
+{
+    DWORD dwStart, dwEnd;
+    MPARAM wParam = (MPARAM) (DWORD*) & dwStart; // receives starting position
+    MPARAM lParam = (MPARAM) (DWORD*) & dwEnd;   // receives ending position
 
 
-         free(tmp_buffer);
+//    ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
 
 
-         return TRUE;
-    }
+    *from = dwStart;
+    *to = dwEnd;
+}
+
+bool wxTextCtrl::IsEditable() const
+{
+// TODO:
+/*
+    long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
+
+    return ((style & ES_READONLY) == 0);
+*/
     return FALSE;
 }
 
     return FALSE;
 }
 
-// If file is null, try saved file name first
-// Returns TRUE if succeeds.
-bool wxTextCtrl::SaveFile(const wxString& file)
+// ----------------------------------------------------------------------------
+// Editing
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::Replace(long from, long to, const wxString& value)
 {
 {
-    wxString theFile(file);
-    if (theFile == "")
-        theFile = m_fileName;
-    if (theFile == "")
-        return FALSE;
-    m_fileName = theFile;
+#if wxUSE_CLIPBOARD
+    HWND hWnd = GetHwnd();
+    long fromChar = from;
+    long toChar = to;
 
 
-    ofstream output((char*) (const char*) theFile);
-    if (output.bad())
-           return FALSE;
+    // Set selection and remove it
+//    SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
+//    SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
 
 
-    // TODO get and save text
+    // Now replace with 'value', by pasting.
+    wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
 
 
-    return FALSE;
+    // Paste into edit control
+//    SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
+#else
+    wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
+#endif
 }
 
 }
 
-void wxTextCtrl::WriteText(const wxString& text)
+void wxTextCtrl::Remove(long from, long to)
 {
 {
-    // TODO write text to control
+    HWND hWnd = GetHwnd();
+    long fromChar = from;
+    long toChar = to;
+
+    // Cut all selected text
+//    SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
+//    SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
 }
 
 }
 
-void wxTextCtrl::AppendText(const wxString& text)
+void wxTextCtrl::SetSelection(long from, long to)
 {
 {
-    // TODO append text to control
+    HWND hWnd = GetHwnd();
+    long fromChar = from;
+    long toChar = to;
+
+    // if from and to are both -1, it means (in wxWindows) that all text should
+    // be selected. Translate into Windows convention
+    if ((from == -1) && (to == -1))
+    {
+      fromChar = 0;
+      toChar = -1;
+    }
+
+//    SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
+//    SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
 }
 
 }
 
-void wxTextCtrl::Clear()
+bool wxTextCtrl::LoadFile(const wxString& file)
 {
 {
-    // TODO
+// TODO:
+/*
+    if ( wxTextCtrlBase::LoadFile(file) )
+    {
+        // update the size limit if needed
+        AdjustSpaceLimit();
+
+        return TRUE;
+    }
+*/
+    return FALSE;
 }
 
 bool wxTextCtrl::IsModified() const
 {
 }
 
 bool wxTextCtrl::IsModified() const
 {
-    // TODO
+//    return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
     return FALSE;
 }
 
     return FALSE;
 }
 
+//
 // Makes 'unmodified'
 // Makes 'unmodified'
+//
 void wxTextCtrl::DiscardEdits()
 {
 void wxTextCtrl::DiscardEdits()
 {
-    // TODO
-}
+    if (m_bIsMLE)
+        ::WinSendMsg(GetHwnd(), MLM_SETCHANGED, MPFROMLONG(FALSE), 0);
+    else
+        //
+        // EM controls do not have a SETCHANGED but issuing a query should reset it
+        //
+        ::WinSendMsg(GetHwnd(), EM_QUERYCHANGED, 0, 0);
+} // end of wxTextCtrl::DiscardEdits
 
 int wxTextCtrl::GetNumberOfLines() const
 {
 
 int wxTextCtrl::GetNumberOfLines() const
 {
-    // TODO
+//    return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
     return 0;
 }
 
 long wxTextCtrl::XYToPosition(long x, long y) const
 {
     return 0;
 }
 
 long wxTextCtrl::XYToPosition(long x, long y) const
 {
-    // TODO
-    return 0;
+    HWND hWnd = GetHwnd();
+
+    // This gets the char index for the _beginning_ of this line
+// TODO:
+/*
+    int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
+    return (long)(x + charIndex);
+*/
+    return 0L;
 }
 
 }
 
-void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
+bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
 {
 {
-    // TODO
+    HWND hWnd = GetHwnd();
+
+    // This gets the line number containing the character
+    int lineNo = -1;
+//    lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
+
+    if ( lineNo == -1 )
+    {
+        // no such line
+        return FALSE;
+    }
+
+    // This gets the char index for the _beginning_ of this line
+    int charIndex = 0; // TODO: (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
+    if ( charIndex == -1 )
+    {
+        return FALSE;
+    }
+
+    // The X position must therefore be the different between pos and charIndex
+    if ( x )
+        *x = (long)(pos - charIndex);
+    if ( y )
+        *y = (long)lineNo;
+
+    return TRUE;
 }
 
 void wxTextCtrl::ShowPosition(long pos)
 {
 }
 
 void wxTextCtrl::ShowPosition(long pos)
 {
-    // TODO
+    HWND hWnd = GetHwnd();
+
+    // To scroll to a position, we pass the number of lines and characters
+    // to scroll *by*. This means that we need to:
+    // (1) Find the line position of the current line.
+    // (2) Find the line position of pos.
+    // (3) Scroll by (pos - current).
+    // For now, ignore the horizontal scrolling.
+
+    // Is this where scrolling is relative to - the line containing the caret?
+    // Or is the first visible line??? Try first visible line.
+//    int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
+
+// TODO:
+/*
+    int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
+
+    int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
+
+    int linesToScroll = specifiedLineLineNo - currentLineLineNo;
+
+    if (linesToScroll != 0)
+      (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
+*/
 }
 
 }
 
-int wxTextCtrl::GetLineLength(long lineNo) const
+int wxTextCtrl::GetLineLength(
+  long                              lLineNo
+) const
 {
 {
-    // TODO
-    return 0;
-}
+    long                            lLen = 0L;
+
+    if (m_bIsMLE)
+        lLen = (long)::WinSendMsg(GetHwnd(), MLM_QUERYLINELENGTH, 0, 0);
+    else
+    {
+        WNDPARAMS                   vParams;
+
+        vParams.fsStatus = WPM_CCHTEXT;
+        if (::WinSendMsg( GetHwnd()
+                         ,WM_QUERYWINDOWPARAMS
+                         ,&vParams
+                         ,0
+                        ))
+        {
+            lLen = vParams.cchText;
+        }
+        else
+            lLen = 32;
+    }
+    return lLen;
+} // end of
 
 wxString wxTextCtrl::GetLineText(long lineNo) const
 {
 
 wxString wxTextCtrl::GetLineText(long lineNo) const
 {
-    // TODO
-    return wxString("");
-}
+    size_t len = (size_t)GetLineLength(lineNo) + 1;
+    char *buf = (char *)malloc(len);
+    *(WORD *)buf = len;
+    int noChars = 0; // TODO:(int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
+    buf[noChars] = 0;
 
 
-bool wxTextCtrl::CanCopy() const
-{
-    // Can copy if there's a selection
-    long from, to;
-    GetSelection(& from, & to);
-    return (from != to) ;
-}
+    wxString str(buf);
 
 
-bool wxTextCtrl::CanCut() const
-{
-    // Can cut if there's a selection
-    long from, to;
-    GetSelection(& from, & to);
-    return (from != to) ;
-}
+    free(buf);
 
 
-bool wxTextCtrl::CanPaste() const
-{
-    return IsEditable() ;
+    return str;
 }
 
 }
 
+// ----------------------------------------------------------------------------
 // Undo/redo
 // Undo/redo
+// ----------------------------------------------------------------------------
+
 void wxTextCtrl::Undo()
 {
 void wxTextCtrl::Undo()
 {
-    // TODO
+    if (CanUndo())
+    {
+//        ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
+    }
 }
 
 void wxTextCtrl::Redo()
 {
 }
 
 void wxTextCtrl::Redo()
 {
-    // TODO
+    if (CanRedo())
+    {
+        // Same as Undo, since Undo undoes the undo, i.e. a redo.
+//        ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
+    }
 }
 
 bool wxTextCtrl::CanUndo() const
 {
 }
 
 bool wxTextCtrl::CanUndo() const
 {
-    // TODO
-    return FALSE;
-}
+    bool                            bOk;
+
+    if (m_bIsMLE)
+        bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
+    else
+        bOk = FALSE; // can't undo regular edit fields in PM
+    return bOk;
+} // end of wxTextCtrl::CanUndo
 
 bool wxTextCtrl::CanRedo() const
 {
 
 bool wxTextCtrl::CanRedo() const
 {
-    // TODO
-    return FALSE;
-}
+    bool                            bOk;
 
 
-// If the return values from and to are the same, there is no
-// selection.
-void wxTextCtrl::GetSelection(long* from, long* to) const
-{
-    // TODO
-    *from = 0;
-    *to = 0;
-}
+    if (m_bIsMLE)
+        bOk = (::WinSendMsg(GetHwnd(), MLM_QUERYUNDO, 0, 0) != 0);
+    else
+        bOk = FALSE; // can't undo regular edit fields in PM
+    return bOk;
+} // end of wxTextCtrl::CanRedo
 
 
-bool wxTextCtrl::IsEditable() const
-{
-    // TODO
-    return FALSE;
-}
+// ----------------------------------------------------------------------------
+// implemenation details
+// ----------------------------------------------------------------------------
 
 void wxTextCtrl::Command(wxCommandEvent & event)
 {
 
 void wxTextCtrl::Command(wxCommandEvent & event)
 {
-    SetValue (event.GetString());
+    SetValue(event.GetString());
     ProcessCommand (event);
 }
 
     ProcessCommand (event);
 }
 
@@ -352,161 +734,196 @@ void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
     }
 }
 
     }
 }
 
-// The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
-// AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
-
-//=========================================================================
-// Called then the buffer is full (gcc 2.6.3) 
-// or when "endl" is output (Borland 4.5)
-//=========================================================================
-// Class declaration using multiple inheritance doesn't work properly for
-// Borland. See note in wb_text.h.
-#ifndef NO_TEXT_WINDOW_STREAM
-int wxTextCtrl::overflow(int c)
-{
-  // Make sure there is a holding area
-  if ( allocate()==EOF )
-  {
-    wxError("Streambuf allocation failed","Internal error");
-    return EOF;
-  }
-  
-  // Verify that there are no characters in get area
-  if ( gptr() && gptr() < egptr() )
-  {
-     wxError("Who's trespassing my get area?","Internal error");
-     return EOF;
-  }
-
-  // Reset get area
-  setg(0,0,0);
-
-  // Make sure there is a put area
-  if ( ! pptr() )
-  {
-/* This doesn't seem to be fatal so comment out error message */
-//    wxError("Put area not opened","Internal error");
-    setp( base(), base() );
-  }
-
-  // Determine how many characters have been inserted but no consumed
-  int plen = pptr() - pbase();
-
-  // Now Jerry relies on the fact that the buffer is at least 2 chars
-  // long, but the holding area "may be as small as 1" ???
-  // And we need an additional \0, so let's keep this inefficient but
-  // safe copy.
-
-  // If c!=EOF, it is a character that must also be comsumed
-  int xtra = c==EOF? 0 : 1;
-
-  // Write temporary C-string to wxTextWindow
-  {
-  char *txt = new char[plen+xtra+1];
-  memcpy(txt, pbase(), plen);
-  txt[plen] = (char)c;     // append c
-  txt[plen+xtra] = '\0';   // append '\0' or overwrite c
-    // If the put area already contained \0, output will be truncated there
-  AppendText(txt);
-    delete[] txt;
-  }
-
-  // Reset put area
-  setp(pbase(), epptr());
-
-#if defined(__WATCOMC__)
-  return __NOT_EOF;
-#elif defined(zapeof)     // HP-UX (all cfront based?)
-  return zapeof(c);
-#else
-  return c!=EOF ? c : 0;  // this should make everybody happy
-#endif
-}
+WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
+                                WXUINT message, WXWPARAM wParam,
+                                WXLPARAM lParam)
+{
+    HDC hdc = (HDC)pDC;
+// TODO:
+/*
+    SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT
+                                                           : OPAQUE);
 
 
-//=========================================================================
-// called then "endl" is output (gcc) or then explicit sync is done (Borland)
-//=========================================================================
-int wxTextCtrl::sync()
-{
-  // Verify that there are no characters in get area
-  if ( gptr() && gptr() < egptr() )
-  {
-     wxError("Who's trespassing my get area?","Internal error");
-     return EOF;
-  }
-
-  if ( pptr() && pptr() > pbase() ) return overflow(EOF);
-
-  return 0;
-/* OLD CODE
-  int len = pptr() - pbase();
-  char *txt = new char[len+1];
-  strncpy(txt, pbase(), len);
-  txt[len] = '\0';
-  (*this) << txt;
-  setp(pbase(), epptr());
-  delete[] txt;
-  return 0;
+    ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
+    ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
 */
 */
-}
+    wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
 
 
-//=========================================================================
-// Should not be called by a "ostream". Used by a "istream"
-//=========================================================================
-int wxTextCtrl::underflow()
-{
-  return EOF;
+    return (WXHBRUSH) backgroundBrush->GetResourceHandle();
 }
 }
-#endif
 
 
-wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
+void wxTextCtrl::OnChar(wxKeyEvent& event)
 {
 {
-    AppendText(s);
-    return *this;
-}
+    switch ( event.KeyCode() )
+    {
+// TODO:
+/*
+        case WXK_RETURN:
+            if ( !(m_windowStyle & wxTE_MULTILINE) )
+            {
+                wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+                event.SetEventObject( this );
+                if ( GetEventHandler()->ProcessEvent(event) )
+                    return;
+            }
+            //else: multiline controls need Enter for themselves
+
+            break;
+
+        case WXK_TAB:
+            // always produce navigation event - even if we process TAB
+            // ourselves the fact that we got here means that the user code
+            // decided to skip processing of this TAB - probably to let it
+            // do its default job.
+            //
+            // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
+            //     handled by Windows
+            {
+                wxNavigationKeyEvent eventNav;
+                eventNav.SetDirection(!event.ShiftDown());
+                eventNav.SetWindowChange(FALSE);
+                eventNav.SetEventObject(this);
+
+                if ( GetEventHandler()->ProcessEvent(eventNav) )
+                    return;
+            }
+            break;
+*/
+        default:
+            event.Skip();
+            return;
+    }
 
 
-wxTextCtrl& wxTextCtrl::operator<<(float f)
-{
-    wxString str;
-    str.Printf("%.2f", f);
-    AppendText(str);
-    return *this;
+    // don't just call event.Skip() because this will cause TABs and ENTERs
+    // be passed upwards and we don't always want this - instead process it
+    // right here
+
+    // FIXME
+    event.Skip();
 }
 
 }
 
-wxTextCtrl& wxTextCtrl::operator<<(double d)
+bool wxTextCtrl::OS2Command(WXUINT param, WXWORD WXUNUSED(id))
 {
 {
-    wxString str;
-    str.Printf("%.2f", d);
-    AppendText(str);
-    return *this;
+    switch (param)
+    {
+// TODO:
+/*
+        case EN_SETFOCUS:
+        case EN_KILLFOCUS:
+            {
+                wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
+                        : wxEVT_SET_FOCUS,
+                        m_windowId);
+                event.SetEventObject( this );
+                GetEventHandler()->ProcessEvent(event);
+            }
+            break;
+
+        case EN_CHANGE:
+            {
+                wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
+                wxString val(GetValue());
+                if ( !val.IsNull() )
+                    event.m_commandString = WXSTRINGCAST val;
+                event.SetEventObject( this );
+                ProcessCommand(event);
+            }
+            break;
+
+        case EN_ERRSPACE:
+            // the text size limit has been hit - increase it
+            AdjustSpaceLimit();
+            break;
+
+            // the other notification messages are not processed
+        case EN_UPDATE:
+        case EN_MAXTEXT:
+        case EN_HSCROLL:
+        case EN_VSCROLL:
+*/
+        default:
+            return FALSE;
+    }
+
+    // processed
+    return TRUE;
 }
 
 }
 
-wxTextCtrl& wxTextCtrl::operator<<(int i)
+void wxTextCtrl::AdjustSpaceLimit()
 {
 {
-    wxString str;
-    str.Printf("%d", i);
-    AppendText(str);
-    return *this;
-}
+    unsigned int                    uLen = 0;
+    unsigned int                    uLimit = 0;
+
+    uLen   = ::WinQueryWindowTextLength(GetHwnd());
+    if (m_bIsMLE)
+    {
+        uLimit = (unsigned int)::WinSendMsg( GetHwnd()
+                                            ,MLM_QUERYTEXTLIMIT
+                                            ,0
+                                            ,0
+                                           );
+    }
+    else
+    {
+        ENTRYFDATA*                 pEfd;
+        WNDPARAMS                   vParams;
+
+        vParams.fsStatus = WPM_CBCTLDATA;
+        vParams.cbCtlData = sizeof(ENTRYFDATA);
+
+        if (::WinSendMsg( GetHwnd()
+                         ,WM_QUERYWINDOWPARAMS
+                         ,&vParams
+                         ,0
+                        ))
+        {
+            pEfd = (ENTRYFDATA*)vParams.pCtlData;
+            uLimit = (unsigned int)pEfd->cchEditLimit;
+        }
+        else
+            uLimit = 32; //PM's default
+    }
+    if (uLen >= uLimit)
+    {
+        uLimit = uLen + 0x8000;    // 32Kb
+        if (uLimit > 0xffff)
+        {
+            uLimit = 0L;
+        }
+        if (m_bIsMLE)
+            ::WinSendMsg(GetHwnd(), MLM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
+        else
+            ::WinSendMsg(GetHwnd(), EM_SETTEXTLIMIT, MPFROMLONG(uLimit), 0);
+    }
+} // end of wxTextCtrl::AdjustSpaceLimit
 
 
-wxTextCtrl& wxTextCtrl::operator<<(long i)
+bool wxTextCtrl::AcceptsFocus() const
 {
 {
-    wxString str;
-    str.Printf("%ld", i);
-    AppendText(str);
-    return *this;
+    // we don't want focus if we can't be edited
+    return IsEditable() && wxControl::AcceptsFocus();
 }
 
 }
 
-wxTextCtrl& wxTextCtrl::operator<<(const char c)
+wxSize wxTextCtrl::DoGetBestSize() const
 {
 {
-    char buf[2];
+    int cx, cy;
+    wxGetCharSize(GetHWND(), &cx, &cy, (wxFont*)&GetFont());
+
+    int wText = DEFAULT_ITEM_WIDTH;
+
+    int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
+    if ( m_windowStyle & wxTE_MULTILINE )
+    {
+        hText *= wxMin(GetNumberOfLines(), 5);
+    }
+    //else: for single line control everything is ok
 
 
-    buf[0] = c;
-    buf[1] = 0;
-    AppendText(buf);
-    return *this;
+    return wxSize(wText, hText);
 }
 
 }
 
+// ----------------------------------------------------------------------------
+// standard handlers for standard edit menu events
+// ----------------------------------------------------------------------------
+
 void wxTextCtrl::OnCut(wxCommandEvent& event)
 {
     Cut();
 void wxTextCtrl::OnCut(wxCommandEvent& event)
 {
     Cut();
@@ -556,3 +973,4 @@ void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
 {
     event.Enable( CanRedo() );
 }
 {
     event.Enable( CanRedo() );
 }
+