Page 1 of 1

Update a RICHEDIT position without focusing

Posted: Mon May 28, 2018 6:36 pm
by concentra
Hi.

This is a chat like application.
The top control, a RICHEDIT, is the chat "history" and the bottom one, a TMultiGet, is the operator typing control.

If the other party types something before the operator finishes what he is typing, what the other party typed must be presented to the operator, without interfering the typing job.

If the RICHEDIT text grows beyond the RICHEDIT size, it must scrolls, in order to the last typed answer can be seen.

Basically, I need to update the RICHEDIT control position while typing some text.

The only way I found to update the RICHEDIT control is to put focus on it, but, since the operator is typing, the focus needs to go back to the TMultiGet control.

But when the TMultiGet looses focus and gets it again, the typed text becomes marked, and, when the operator keeps typing, it is deleted...

How do I update the RICHEDIT control position without putting focus on it ?

In the sample, if you keep typing while changing focus the text is deleted.

Regards,

Maurício Faria

Code: Select all

#include "fivewin.ch"
#include "richedit.ch"

function Main()

   local oWnd

   DEFINE WINDOW oWnd FROM 1,1 TO 140,110 PIXEL

   @ 014,004 BUTTON "With SetFocus "    SIZE 110,30 PIXEL ACTION Go( .T. )
   @ 054,004 BUTTON "Without SetFocus " SIZE 110,30 PIXEL ACTION Go( .F. )

   ACTIVATE WINDOW oWnd

return nil






Function Go( lSetFocus )

   local oDlg
   local cRichText
   local oRich
   local oTextType
   local cText
   local oTimer
   local hRichDLL := LoadLibrary( "riched20.dll" )

   DEFINE Dialog oDlg FROM 1,1 TO 400,400 PIXEL

   @ 004,004 RichEdit oRich VAR cRichText OF oDlg SIZE 176,66 PIXEL READONLY

   @ 108,004 GET oTextType VAR cText OF oDlg SIZE 174,78 PIXEL MULTILINE

   oTimer := TTimer():New( 1000, { || UpdateRTF( oRich, oTimer, oTextType, lSetFocus ) } )

   ACTIVATE Dialog oDlg ON INIT ( oTimer:Activate(), oTextType:SetFocus() )

   freeLibrary( hRichDLL )

return nil




PROCEDURE UpdateRTF( oRich, oTimer, oTextType, lSetFocus )

   LOCAL cHeader := "{\rtf1\ansi\ansicpg1252\deff0\deflang1046" +;
                    "{\fonttbl{\f0\fnil\fcharset0 Courier New;}}" +;
                    "{\colortbl ;\red0\green0\blue0;\red185\green255\blue86;\red255\green255\blue255;\red255\green255\blue100;}" +;
                    "\viewkind4\uc1\pard\b\fs18"
   LOCAL cRTF := ""
   LOCAL I

   STATIC aMsgs

   oTimer:DEACTIVATE()

   IF EMPTY( aMsgs )
      aMsgs := {"MSGS"}
   ENDIF

   AADD( aMsgs, Time() )

   FOR I:=1 TO LEN( aMsgs )

      IF MOD( I, 2 ) = 0
         cRTF += "\cf1\highlight2 " + aMsgs[I] + "\par "
      ELSE
         cRTF += "\cf1\highlight3 " + aMsgs[I] + "\par "
      ENDIF

   NEXT

   cRTF += "}"

   oRich:SetText( cHeader + cRTF )

   oRich:GoTo(oRich:Len())

   IF lSetFocus
      oRich:SetFocus()
      oTextType:SetFocus()
   ENDIF

   oTimer:ACTIVATE()

RETURN
 

Re: Update a RICHEDIT position without focusing

Posted: Mon May 28, 2018 9:15 pm
by cnavarro
Ok, try with

Code: Select all

oRTF:GoToLine( oRTF:GetLineCount() )
 
end tell me

Re: Update a RICHEDIT position without focusing

Posted: Mon May 28, 2018 9:23 pm
by concentra
cnavarro wrote:Ok, try with

Code: Select all

oRTF:GoToLine( oRTF:GetLineCount() )
 
end tell me
Hi.

I got exactly the same result: without focusing the RICHEDIT doesn't scroll and with focus it scrolls but the TMulitGet deletes what was typed...

Re: Update a RICHEDIT position without focusing

Posted: Mon May 28, 2018 9:50 pm
by cnavarro
Ah, OK, now I understand

Code: Select all

   @ 108,004 GET oTextType VAR cText OF oDlg SIZE 174,78 PIXEL MULTILINE
   oTextType:bGotFocus = { | o | o:SetSel( 0, 0 ), o:GoBottom() }
 

Re: Update a RICHEDIT position without focusing

Posted: Tue May 29, 2018 10:32 am
by concentra
cnavarro wrote:

Code: Select all

   @ 108,004 GET oTextType VAR cText OF oDlg SIZE 174,78 PIXEL MULTILINE
   oTextType:bGotFocus = { | o | o:SetSel( 0, 0 ), o:GoBottom() }
 

Thanks !

It Works.

But it introduces some overhead, it's like going around the block to get to the wall neighbor...

And if the operator moves the cursor to edit the text in other point than the end this solution moves the cursor to the end.

Regards.

Re: Update a RICHEDIT position without focusing

Posted: Tue May 29, 2018 1:43 pm
by cnavarro
Saves the position when the focus is lost ( bLostFocus ), and restores the position when acquiring the focus ( bGotFocus )