Jump to content

_GUICtrlRichEdit_SetText (No Undo)


Go to solution Solved by Andreik,

Recommended Posts

Hi,

Based on this here is modified example from help file:

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $hGui, $iMsg, $idBtnNext, $iStep = 0, $idLblMsg, $hRichEdit
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    $idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60)
    $idBtnNext = GUICtrlCreateButton("Next", 270, 310, 40, 30)
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_SetText($hRichEdit, "First paragraph")
    While True
        $iMsg = GUIGetMsg()
        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                ; GUIDelete()   ; is OK too
                Exit
            Case $iMsg = $idBtnNext
                $iStep += 1
                Switch $iStep
                    Case 1
                        _GUICtrlRichEdit_SetUndoLimit($hRichEdit, 200)
                        GUICtrlSetData($idLblMsg, "Increased the number of actions that can stored in the undo queue from 100 to 200")
                    Case 2
                        _GUICtrlRichEdit_SetUndoLimit($hRichEdit, 0)
                        GUICtrlSetData($idLblMsg, "Undo is disabled")
                        GUICtrlSetState($idBtnNext, $GUI_DISABLE)
                EndSwitch
        EndSelect
    WEnd
EndFunc   ;==>Example

There is also message EM_SETTEXTEX.

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Global $g_idLblMsg

Example()

Func Example()
  Local $hGui = GUICreate("RichEdit Get/Set Text (v" & @AutoItVersion & ")", 340, 350, -1, -1)
  Local $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 320, 220, _
      BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
  $g_idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60)
  GUISetState(@SW_SHOW)

  _GUICtrlRichEdit_SetSel($hRichEdit, 0, -1, True)
  _GUICtrlRichEdit_ReplaceText($hRichEdit, "This is Set text.", True)

  Report("Text: " & @CRLF & @CRLF & _GUICtrlRichEdit_GetText($hRichEdit))

  _GUICtrlRichEdit_Undo($hRichEdit)

  Local $iMsg
  While True
    $iMsg = GUIGetMsg()
    Select
      Case $iMsg = $GUI_EVENT_CLOSE
        _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
        ; GUIDelete()   ; is OK too
        Exit
    EndSelect
  WEnd
EndFunc   ;==>Example

Func Report($sMsg)
  GUICtrlSetData($g_idLblMsg, $sMsg)
EndFunc   ;==>Report

 

Link to comment
Share on other sites

thanks lot for the posts.

the replace-option instead of set-option I know.

This is why I asked if undo can be enabled for the set-option.

BTW the undo limit is already set to a higher value, but even for set-option it would be enough if it was just 1.

So unfortunately I still do not know, how I could get the undo-option activated for _GUICtrlRichEdit_SetText.

cheers mike

 

Link to comment
Share on other sites

Changed a little bit @Nine's example:

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Global $g_idLblMsg

Example()

Func Example()
  Local $hGui = GUICreate("RichEdit Get/Set Text (v" & @AutoItVersion & ")", 340, 350, -1, -1)
  Local $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 320, 220, _
      BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
  $g_idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60)
  GUISetState(@SW_SHOW)

  _GUICtrlRichEdit_SetSel($hRichEdit, 0, -1, True)
  _GUICtrlRichEdit_SetText2($hRichEdit, "This is Set text.", True)

  Report("Text: " & @CRLF & @CRLF & _GUICtrlRichEdit_GetText($hRichEdit))

  _GUICtrlRichEdit_Undo($hRichEdit)
  Local $iMsg
  While True
    $iMsg = GUIGetMsg()
    Select
      Case $iMsg = $GUI_EVENT_CLOSE
        _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
        ; GUIDelete()   ; is OK too
        Exit
    EndSelect
  WEnd
EndFunc   ;==>Example

Func Report($sMsg)
  GUICtrlSetData($g_idLblMsg, $sMsg)
EndFunc   ;==>Report

Func _GUICtrlRichEdit_SetText2($hWnd, $sText, $bCanUndo = False)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False)

    Local $tSetText = DllStructCreate($tagSETTEXTEX)
    DllStructSetData($tSetText, 1, ($bCanUndo ? $ST_KEEPUNDO : $ST_DEFAULT))
    DllStructSetData($tSetText, 2, $CP_ACP)
    Local $iRet
    If StringLeft($sText, 5) <> "{\rtf" And StringLeft($sText, 5) <> "{urtf" Then
        DllStructSetData($tSetText, 2, $CP_UNICODE)
        $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "wstr")
    Else
        $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "STR")
    EndIf
    If Not $iRet Then Return SetError(700, 0, False)
    Return True
EndFunc

Basically the flags member in SETTEXTEX structure is hardcoded with ST_DEFAULT whici means that the undo stack is deleted. Changing ST_DEFAULT with ST_KEEPUNDO the undo stack is preserved. I think there should be a third parameter added to _GUICtrlRichEdit_SetText(), at least for symmetry. Anyway, the help file is wrong stating that the undo stack is preserved, when it's clearly not. Until anything it's changed you can use _GUICtrlRichEdit_SetText2() like in the example above.

Edited by Andreik
Link to comment
Share on other sites

Thanks Andreik,

I have tested your modified _GUICtrlRichEdit_SetText2.

This works fine and enables UNDO. (if selection is done before)

If no selection is done, it works, but no UNDO.

Is there a way to get the UNDO working without selection in window-edit?

We need the selection for other purposes.

Cheers mike

Link to comment
Share on other sites

  • Solution

I'm not sure what are you talking about. Selection has nothing to do with undo, as you can see below:

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Local $hGui = GUICreate("RichEdit Get/Set Text (v" & @AutoItVersion & ")", 340, 350)
Local $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 320, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
GUISetState(@SW_SHOW)

Sleep(5000)
_GUICtrlRichEdit_SetText2($hRichEdit, "This is Set text.", True)

Sleep(5000)
_GUICtrlRichEdit_Undo($hRichEdit)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GUICtrlRichEdit_Destroy($hRichEdit)

Func _GUICtrlRichEdit_SetText2($hWnd, $sText, $bCanUndo = False)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(101, 0, False)
    Local $tSetText = DllStructCreate($tagSETTEXTEX)
    DllStructSetData($tSetText, 1, ($bCanUndo ? $ST_KEEPUNDO : $ST_DEFAULT))
    DllStructSetData($tSetText, 2, $CP_ACP)
    Local $iRet
    If StringLeft($sText, 5) <> "{\rtf" And StringLeft($sText, 5) <> "{urtf" Then
        DllStructSetData($tSetText, 2, $CP_UNICODE)
        $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "wstr")
    Else
        $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "STR")
    EndIf
    If Not $iRet Then Return SetError(700, 0, False)
    Return True
EndFunc

 

Link to comment
Share on other sites

Hi Andreik,

it was my fault.

Your script is OK.

I had made a mistake in updating my Menu-Entry for the Undo.

It asks _GUICtrlRichEdit_CanUndo to enable or disable this menu-entry.

Your Undo was working, but my menu-entry UNDO was not enabled.

Exellent.

I can use now _GUICtrlRichEdit_SetText  with included Undo.

Thanks lot.

Cheers mike

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...