+// the rest of the file only deals with the rich edit controls
+#if wxUSE_RICHEDIT
+
+// ----------------------------------------------------------------------------
+// colour setting for the rich edit controls
+// ----------------------------------------------------------------------------
+
+// Watcom C++ doesn't define this
+#ifndef SCF_ALL
+#define SCF_ALL 0x0004
+#endif
+
+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
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
+{
+ if ( !IsRich() )
+ {
+ // can't do it with normal text control
+ return FALSE;
+ }
+
+ // the rich text control doesn't handle setting background colour, so don't
+ // even try if it's the only thing we want to change
+ if ( !style.HasFont() && !style.HasTextColour() )
+ {
+ // nothing to do: return TRUE if there was really nothing to doand
+ // FALSE fi 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 )
+ SendMessage(GetHwnd(), EM_SETSEL, (WPARAM) start, (LPARAM) end);
+
+ // initialize CHARFORMAT struct
+ CHARFORMAT cf;
+ wxZeroMemory(cf);
+ cf.cbSize = sizeof(cf);
+
+ if ( style.HasFont() )
+ {
+ cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET;
+
+ // 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
+ if ( lf.lfItalic )
+ {
+ cf.dwMask |= CFM_ITALIC;
+ cf.dwEffects |= CFE_ITALIC;
+ }
+
+ if ( lf.lfWeight == FW_BOLD )
+ {
+ cf.dwMask |= CFM_BOLD;
+ cf.dwEffects |= CFE_BOLD;
+ }
+
+ if ( lf.lfUnderline )
+ {
+ cf.dwMask |= CFM_UNDERLINE;
+ cf.dwEffects |= CFE_UNDERLINE;
+ }
+
+ // strikeout fonts are not supported by wxWindows
+ }
+
+ if ( style.HasTextColour() )
+ {
+ cf.dwMask |= CFM_COLOR;
+ cf.crTextColor = wxColourToRGB(style.GetTextColour());
+ }
+
+ // 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
+ SendMessage(GetHwnd(), EM_SETSEL, (WPARAM)startOld, (LPARAM)endOld);
+ }
+
+ return ok;
+}
+
+// ----------------------------------------------------------------------------
+// wxRichEditModule
+// ----------------------------------------------------------------------------
+
+bool wxRichEditModule::OnInit()
+{
+ // don't do anything - we will load it when needed
+ return TRUE;
+}
+
+void wxRichEditModule::OnExit()
+{
+ if ( ms_hRichEdit )
+ {
+ FreeLibrary(ms_hRichEdit);
+ }
+}
+
+/* static */
+bool wxRichEditModule::Load(int version)
+{
+ wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
+ _T("incorrect richedit control version requested") );
+
+ if ( version <= ms_verRichEdit )
+ {
+ // we've already got this or better
+ return TRUE;
+ }
+
+ if ( ms_hRichEdit )
+ {
+ ::FreeLibrary(ms_hRichEdit);
+ }
+
+ // always try load riched20.dll first - like this we won't have to reload
+ // it later if we're first asked for RE 1 and then for RE 2 or 3
+ wxString dllname = _T("riched20.dll");
+ ms_hRichEdit = ::LoadLibrary(dllname);
+ ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
+
+ if ( !ms_hRichEdit && (version == 1) )
+ {
+ // fall back to RE 1
+ dllname = _T("riched32.dll");
+ ms_hRichEdit = ::LoadLibrary(dllname);
+ ms_verRichEdit = 1;
+ }
+
+ if ( !ms_hRichEdit )
+ {
+ wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
+
+ ms_verRichEdit = -1;
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+#endif // wxUSE_RICHEDIT
+