The MASM Forum

Projects => MasmBasic & the RichMasm IDE => Topic started by: jj2007 on March 03, 2023, 03:01:17 AM

Title: Uncle Tom, the RichEdit control's little helper
Post by: jj2007 on March 03, 2023, 03:01:17 AM
Our buggy friend (http://masm32.com/board/index.php?topic=5383.msg117970#msg117970), the RichEdit control, has lots of more or less useful messages. And it has a second option for communicating with you: the Text Object Model alias TOM.

Now, if you think "that smells suspiciously of COM", you are absolutely right. For example, a proc that inserts a string into a given range might look like this (actually a workaround for the infamous EM_EXSETSEL bug (https://learn.microsoft.com/en-us/answers/questions/1123169/em-exsetsel-bug-in-msftedit-dll-richedit50w)):

_TomRepSel proc uses esi edi pChrg, _Insert$ ; ptr to CHARRANGE, string to insert
  call TomInit ; load ITextDocument interface into edi
  mov ecx, pChrg
  mov eax, [ecx.CHARRANGE.cpMax]
  push eax ; cpMax
  push eax ; cpMin=cpMax for EXSETSEL below
  CoInvoke pInterface, ITextDocument.Range, [ecx.CHARRANGE.cpMin], eax, addr ReTom.mbtRange
  lea edi, ReTom.mbtRange
  CoInvoke pInterface, ITextRange.SetFormattedText, ReTom.mbtRange ; set the range to formatted (otherwise shortcuts fail)
  CoInvoke pInterface, ITextRange.SetText, Ole$(_Insert$)
  invoke SendMessage, _hRE, EM_EXSETSEL, 0, esp ; no bug for end of selection
  pop edx
  pop ecx
  ret
_TomRepSel endp


That looks evil indeed, and it needs the latest MasmBasic version (http://masm32.com/board/index.php?topic=94.0). For the time being, I have used TOM only to get and set text (see attachment), but it looks promising. There are a wealth of new functions (https://learn.microsoft.com/en-us/windows/win32/api/tom/) to study (and some crispy new bugs, pardon: features).

\Masm32\MasmBasic\MasmBasic.inc has implemented the most important interfaces; look below MbTOM. I'll continue playing if I find the time, and maybe keep you posted ;-)