+// the rest of the file only deals with the rich edit controls
+#if wxUSE_RICHEDIT
+
+// ----------------------------------------------------------------------------
+// EN_LINK processing
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
+{
+#ifndef __WXWINE__
+    NMHDR *hdr = (NMHDR* )lParam;
+    switch ( hdr->code )
+    {
+       case EN_MSGFILTER:
+            {
+                const MSGFILTER *msgf = (MSGFILTER *)lParam;
+                UINT msg = msgf->msg;
+
+                // this is a bit crazy but richedit 1.0 sends us all mouse
+                // events _except_ WM_LBUTTONUP (don't ask me why) so we have
+                // generate the wxWin events for this message manually
+                //
+                // NB: in fact, this is still not totally correct as it does
+                //     send us WM_LBUTTONUP if the selection was cleared by the
+                //     last click -- so currently we get 2 events in this case,
+                //     but as I don't see any obvious way to check for this I
+                //     leave this code in place because it's still better than
+                //     not getting left up events at all
+                if ( msg == WM_LBUTTONUP )
+                {
+                    WXUINT flags = msgf->wParam;
+                    int x = GET_X_LPARAM(msgf->lParam),
+                        y = GET_Y_LPARAM(msgf->lParam);
+
+                    HandleMouseEvent(msg, x, y, flags);
+                }
+            }
+
+            // return TRUE to process the event (and FALSE to ignore it)
+            return TRUE;
+
+        case EN_LINK:
+            {
+                const ENLINK *enlink = (ENLINK *)hdr;
+
+                switch ( enlink->msg )
+                {
+                    case WM_SETCURSOR:
+                        // ok, so it is hardcoded - do we really nee to
+                        // customize it?
+                        ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
+                        *result = TRUE;
+                        break;
+
+                    case WM_MOUSEMOVE:
+                    case WM_LBUTTONDOWN:
+                    case WM_LBUTTONUP:
+                    case WM_LBUTTONDBLCLK:
+                    case WM_RBUTTONDOWN:
+                    case WM_RBUTTONUP:
+                    case WM_RBUTTONDBLCLK:
+                        // send a mouse event
+                        {
+                            static const wxEventType eventsMouse[] =
+                            {
+                                wxEVT_MOTION,
+                                wxEVT_LEFT_DOWN,
+                                wxEVT_LEFT_UP,
+                                wxEVT_LEFT_DCLICK,
+                                wxEVT_RIGHT_DOWN,
+                                wxEVT_RIGHT_UP,
+                                wxEVT_RIGHT_DCLICK,
+                            };
+
+                            // the event ids are consecutive
+                            wxMouseEvent
+                                evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
+
+                            InitMouseEvent(evtMouse,
+                                           GET_X_LPARAM(enlink->lParam),
+                                           GET_Y_LPARAM(enlink->lParam),
+                                           enlink->wParam);
+
+                            wxTextUrlEvent event(m_windowId, evtMouse,
+                                                 enlink->chrg.cpMin,
+                                                 enlink->chrg.cpMax);
+
+                            InitCommandEvent(event);
+
+                            *result = ProcessCommand(event);
+                        }
+                        break;
+                }
+            }
+            return TRUE;
+    }
+#endif
+    
+    // not processed, leave it to the base class
+    return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result);
+}
+
+// ----------------------------------------------------------------------------
+// colour setting for the rich edit controls
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
+{
+    if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
+    {
+        // colour didn't really change
+        return FALSE;
+    }
+
+    if ( IsRich() )
+    {
+        // rich edit doesn't use WM_CTLCOLOR, hence we need to send
+        // EM_SETBKGNDCOLOR additionally
+        ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
+    }
+
+    return TRUE;
+}
+
+bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
+{
+    if ( !wxTextCtrlBase::SetForegroundColour(colour) )
+    {
+        // colour didn't really change
+        return FALSE;
+    }
+
+    if ( IsRich() )
+    {
+        // change the colour of everything
+        CHARFORMAT cf;
+        wxZeroMemory(cf);
+        cf.cbSize = sizeof(cf);
+        cf.dwMask = CFM_COLOR;
+        cf.crTextColor = wxColourToRGB(colour);
+        ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
+    }
+
+    return TRUE;
+}
+
+// ----------------------------------------------------------------------------
+// styling support for rich edit controls
+// ----------------------------------------------------------------------------
+
+#if wxUSE_RICHEDIT
+
+bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
+{
+#ifdef __WXWINE__
+    return FALSE;
+#else
+    if ( !IsRich() )
+    {
+        // can't do it with normal text control
+        return FALSE;
+    }
+
+    // the richedit 1.0 doesn't handle setting background colour, so don't
+    // even try to do anything if it's the only thing we want to change
+    if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() )
+    {
+        // nothing to do: return TRUE if there was really nothing to do and
+        // FALSE if we failed to set bg colour
+        return !style.HasBackgroundColour();
+    }
+
+    // order the range if needed
+    if ( start > end )
+    {
+        long tmp = start;
+        start = end;
+        end = tmp;
+    }
+
+    // we can only change the format of the selection, so select the range we
+    // want and restore the old selection later
+    long startOld, endOld;
+    GetSelection(&startOld, &endOld);
+
+    // but do we really have to change the selection?
+    bool changeSel = start != startOld || end != endOld;
+
+    if ( changeSel )
+    {
+        DoSetSelection(start, end, FALSE /* don't scroll caret into view */);
+    }
+
+    // initialize CHARFORMAT struct
+#if wxUSE_RICHEDIT2
+    CHARFORMAT2 cf;
+#else
+    CHARFORMAT cf;
+#endif
+
+    wxZeroMemory(cf);
+
+    // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
+    // CHARFORMAT in that case
+#if wxUSE_RICHEDIT2
+    if ( m_verRichEdit == 1 )
+    {
+        // this is the only thing the control is going to grok
+        cf.cbSize = sizeof(CHARFORMAT);
+    }
+    else
+#endif
+    {
+        // CHARFORMAT or CHARFORMAT2
+        cf.cbSize = sizeof(cf);
+    }
+
+    if ( style.HasFont() )
+    {
+        // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
+        //     but using it doesn't seem to hurt neither so leaving it for now
+
+        cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
+                     CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
+
+        // fill in data from LOGFONT but recalculate lfHeight because we need
+        // the real height in twips and not the negative number which
+        // wxFillLogFont() returns (this is correct in general and works with
+        // the Windows font mapper, but not here)
+        LOGFONT lf;
+        wxFillLogFont(&lf, &style.GetFont());
+        cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
+        cf.bCharSet = lf.lfCharSet;
+        cf.bPitchAndFamily = lf.lfPitchAndFamily;
+        wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
+
+        // also deal with underline/italic/bold attributes: note that we must
+        // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
+        // style to allow clearing it
+        if ( lf.lfItalic )
+        {
+            cf.dwEffects |= CFE_ITALIC;
+        }
+
+        if ( lf.lfWeight == FW_BOLD )
+        {
+            cf.dwEffects |= CFE_BOLD;
+        }
+
+        if ( lf.lfUnderline )
+        {
+            cf.dwEffects |= CFE_UNDERLINE;
+        }
+
+        // strikeout fonts are not supported by wxWindows
+    }
+
+    if ( style.HasTextColour() )
+    {
+        cf.dwMask |= CFM_COLOR;
+        cf.crTextColor = wxColourToRGB(style.GetTextColour());
+    }
+
+#if wxUSE_RICHEDIT2
+    if ( m_verRichEdit != 1 && style.HasBackgroundColour() )
+    {
+        cf.dwMask |= CFM_BACKCOLOR;
+        cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
+    }
+#endif // wxUSE_RICHEDIT2
+
+    // do format the selection
+    bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
+                            SCF_SELECTION, (LPARAM)&cf) != 0;
+    if ( !ok )
+    {
+        wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
+    }
+
+    if ( changeSel )
+    {
+        // restore the original selection
+        DoSetSelection(startOld, endOld, FALSE);
+    }
+
+    return ok;
+#endif
+}
+
+bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
+{
+    if ( !wxTextCtrlBase::SetDefaultStyle(style) )
+        return FALSE;
+
+    // we have to do this or the style wouldn't apply for the text typed by the
+    // user
+    long posLast = GetLastPosition();
+    SetStyle(posLast, posLast, m_defaultStyle);
+
+    return TRUE;
+}
+
+#endif
+
+// ----------------------------------------------------------------------------
+// wxRichEditModule
+// ----------------------------------------------------------------------------
+
+bool wxRichEditModule::OnInit()
+{
+    // don't do anything - we will load it when needed
+    return TRUE;
+}
+
+void wxRichEditModule::OnExit()
+{
+    for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ )
+    {
+        if ( ms_hRichEdit[i] )
+        {
+            ::FreeLibrary(ms_hRichEdit[i]);
+        }
+    }
+}
+
+/* static */
+bool wxRichEditModule::Load(int version)
+{
+    // we don't support loading richedit 3.0 as I don't know how to distinguish
+    // it from 2.0 anyhow
+    wxCHECK_MSG( version == 1 || version == 2, FALSE,
+                 _T("incorrect richedit control version requested") );
+
+    // make it the index in the array
+    version--;
+
+    if ( ms_hRichEdit[version] == (HINSTANCE)-1 )
+    {
+        // we had already tried to load it and failed
+        return FALSE;
+    }
+
+    if ( ms_hRichEdit[version] )
+    {
+        // we've already got this one
+        return TRUE;
+    }
+
+    wxString dllname = version ? _T("riched20") : _T("riched32");
+    dllname += _T(".dll");
+
+    ms_hRichEdit[version] = ::LoadLibrary(dllname);
+
+    if ( !ms_hRichEdit[version] )
+    {
+        wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
+
+        ms_hRichEdit[version] = (HINSTANCE)-1;
+
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+#endif // wxUSE_RICHEDIT
+
+#endif // wxUSE_TEXTCTRL