3

I use TEmbeddedWB and TEditDesigner to add HTML to page which is in edit mode. It is as simple as EditDesigner->InsertHTML("1234");.

The problem is because the HTML is not always inserted at the cursor position. If I place this code on a button it works just fine, HTML is inserted at cursor position. But if I place it in CMDialogKey event which intercepts CM_DIALOGKEY message to intercept TAB key it always inserts it at the beginning of the page. If text is selected, then it inserts at selected text position, overwriting it (as it should). But if it is not selected it inserts it at the beginning of HTML.

Here is a function which inserts HTML in TEmbeddedWB:

procedure TEditDesigner.InsertHTML(HTML: string);
var
  Sel: IHTMLSelectionObject;
  Range: IHTMLTxtRange;
  Doc: IHTMLDocument2;
begin
  if FEnable and Assigned(FEmbeddedWB) and (not (csDesigning in ComponentState)) then
  begin
    Doc := FEmbeddedWB.Doc2;
    if Assigned(Doc) then
    begin
      Sel := Doc.selection;
      if Assigned(Sel) then
      begin
        if (Sel.type_ = 'None') or (Sel.type_ = 'Text') then
        begin
          Range := Sel.createRange as IHTMLTxtRange;
          Range.pasteHTML(HTML);
        end;
      end;
    end;
  end;
end;

How can I use it to insert HTML at the cursor position all the time?

Coder12345
  • 3,431
  • 3
  • 33
  • 73
  • 1
    Just speculating here: your CMDialogKey actually makes your Doc loose focus first. Maybe you need to handle `DOM.parentWindow.event` (see also `attachEvent`) – kobik Dec 09 '13 at 14:21
  • @kobik It does lose focus and I have to reset it back to the document. This is because TAB key normally tabs to another control and does not insert anything inside web browser. So far I've managed to insert HTML code, then the Doc loses focus to another control, then I intercept it with CM_DIALOGKEY and return focus back to the control. Dumb but it works. Can you elaborate your parentWindow event? – Coder12345 Dec 09 '13 at 20:51

0 Answers0