+void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
+{
+ long from, to;
+ GetSelection(& from, & to);
+ event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
+}
+
+void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
+{
+ event.Enable(GetLastPosition() > 0);
+}
+
+void wxTextCtrl::OnRightClick(wxMouseEvent& event)
+{
+#if wxUSE_RICHEDIT
+ if (IsRich())
+ {
+ if (!m_privateContextMenu)
+ {
+ m_privateContextMenu = new wxMenu;
+ m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
+ m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
+ m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
+ m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
+ m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
+ }
+ PopupMenu(m_privateContextMenu, event.GetPosition());
+ return;
+ }
+ else
+#endif
+ 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
+
+// ----------------------------------------------------------------------------
+// EN_LINK processing
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
+{
+ 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;
+ }
+
+ // 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)
+{
+ 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() &&
+ !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
+ !style.HasTabs() )
+ {
+ // 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"));
+ }
+
+ // now do the paragraph formatting
+ PARAFORMAT2 pf;
+ wxZeroMemory(pf);
+ // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
+ // PARAFORMAT in that case
+#if wxUSE_RICHEDIT2
+ if ( m_verRichEdit == 1 )
+ {
+ // this is the only thing the control is going to grok
+ pf.cbSize = sizeof(PARAFORMAT);
+ }
+ else
+#endif
+ {
+ // PARAFORMAT or PARAFORMAT2
+ pf.cbSize = sizeof(pf);
+ }
+
+ if (style.HasAlignment())
+ {
+ pf.dwMask |= PFM_ALIGNMENT;
+ if (style.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
+ pf.wAlignment = PFA_RIGHT;
+ else if (style.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
+ pf.wAlignment = PFA_CENTER;
+ else if (style.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
+ pf.wAlignment = PFA_JUSTIFY;
+ else
+ pf.wAlignment = PFA_LEFT;
+ }
+
+ if (style.HasLeftIndent())
+ {
+ pf.dwMask |= PFM_STARTINDENT;
+
+ // Convert from 1/10 mm to TWIPS
+ pf.dxStartIndent = (int) (((double) style.GetLeftIndent()) * mm2twips / 10.0) ;
+
+ // TODO: do we need to specify dxOffset?
+ }
+
+ if (style.HasRightIndent())
+ {
+ pf.dwMask |= PFM_RIGHTINDENT;
+
+ // Convert from 1/10 mm to TWIPS
+ pf.dxRightIndent = (int) (((double) style.GetRightIndent()) * mm2twips / 10.0) ;
+ }
+
+ if (style.HasTabs())
+ {
+ pf.dwMask |= PFM_TABSTOPS;
+
+ const wxArrayInt& tabs = style.GetTabs();
+
+ pf.cTabCount = wxMin(tabs.GetCount(), MAX_TAB_STOPS);
+ size_t i;
+ for (i = 0; i < (size_t) pf.cTabCount; i++)
+ {
+ // Convert from 1/10 mm to TWIPS
+ pf.rgxTabs[i] = (int) (((double) tabs[i]) * mm2twips / 10.0) ;
+ }
+ }
+
+ if (pf.dwMask != 0)
+ {
+ // do format the selection
+ bool ok = ::SendMessage(GetHwnd(), EM_SETPARAFORMAT,
+ 0, (LPARAM) &pf) != 0;
+ if ( !ok )
+ {
+ wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
+ }
+ }
+
+ if ( changeSel )
+ {
+ // restore the original selection
+ DoSetSelection(startOld, endOld, FALSE);
+ }
+
+ return ok;
+}
+
+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;
+}
+
+bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
+{
+ if ( !IsRich() )
+ {
+ // can't do it with normal text control
+ return FALSE;
+ }
+
+ // 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);
+ }
+ // 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 = position != startOld || position != endOld;
+
+ if ( changeSel )
+ {
+ DoSetSelection(position, position, FALSE /* don't scroll caret into view */);
+ }
+
+ // get the selection formatting
+ (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT,
+ SCF_SELECTION, (LPARAM)&cf) ;
+
+ LOGFONT lf;
+ lf.lfHeight = cf.yHeight;
+ lf.lfWidth = 0;
+ lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset?
+ lf.lfClipPrecision = 0;
+ lf.lfEscapement = 0;
+ wxStrcpy(lf.lfFaceName, cf.szFaceName);
+ if (cf.dwEffects & CFE_ITALIC)
+ lf.lfItalic = TRUE;
+ lf.lfOrientation = 0;
+ lf.lfPitchAndFamily = cf.bPitchAndFamily;
+ lf.lfQuality = 0;
+ if (cf.dwEffects & CFE_STRIKEOUT)
+ lf.lfStrikeOut = TRUE;
+ if (cf.dwEffects & CFE_UNDERLINE)
+ lf.lfUnderline = TRUE;
+ if (cf.dwEffects & CFE_BOLD)
+ lf.lfWeight = FW_BOLD;
+
+ wxFont font = wxCreateFontFromLogFont(& lf);
+ if (font.Ok())
+ {
+ style.SetFont(font);
+ }
+ style.SetTextColour(wxColour(cf.crTextColor));
+
+#if wxUSE_RICHEDIT2
+ if ( m_verRichEdit != 1 )
+ {
+ // cf.dwMask |= CFM_BACKCOLOR;
+ style.SetBackgroundColour(wxColour(cf.crBackColor));
+ }
+#endif // wxUSE_RICHEDIT2
+
+ // now get the paragraph formatting
+ PARAFORMAT2 pf;
+ wxZeroMemory(pf);
+ // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
+ // PARAFORMAT in that case
+#if wxUSE_RICHEDIT2
+ if ( m_verRichEdit == 1 )
+ {
+ // this is the only thing the control is going to grok
+ pf.cbSize = sizeof(PARAFORMAT);
+ }
+ else
+#endif
+ {
+ // PARAFORMAT or PARAFORMAT2
+ pf.cbSize = sizeof(pf);
+ }
+
+ // do format the selection
+ (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT, 0, (LPARAM) &pf) ;
+
+ style.SetLeftIndent( (int) ((double) pf.dxStartIndent * twips2mm * 10.0) );
+ style.SetRightIndent( (int) ((double) pf.dxRightIndent * twips2mm * 10.0) );
+
+ if (pf.wAlignment == PFA_CENTER)
+ style.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
+ else if (pf.wAlignment == PFA_RIGHT)
+ style.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
+ else if (pf.wAlignment == PFA_JUSTIFY)
+ style.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
+ else
+ style.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
+
+ wxArrayInt tabStops;
+ size_t i;
+ for (i = 0; i < (size_t) pf.cTabCount; i++)
+ {
+ tabStops[i] = (int) ((double) (pf.rgxTabs[i] & 0xFFFF) * twips2mm * 10.0) ;
+ }
+
+ if ( changeSel )
+ {
+ // restore the original selection
+ DoSetSelection(startOld, endOld, FALSE);
+ }
+
+ 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]);
+ ms_hRichEdit[i] = NULL;
+ }
+ }
+}
+
+/* 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