Sign in to follow this  
Followers 0
qwert

A tool for analyzing rich text operations

2 posts in this topic

After a week’s work with RichEdit controls, I’ve made some observations and come to some conclusions that may help others. The main two are:

 

Rich Text is relatively easy when used inside an AutoIt GUI ... as there are ample commands in the _GUICtrlRichText_*** UDF set.

 

Rich Text is fairly difficult when you start to work with multiple fields and with RTF from other sources, largely because of the wide variations in implementations ... but sometimes due to AutoIt-specific support for rich text formats.

 

As a result of my investigation—and with assistance from mLipok—I’ve produced a Rich Text Tool to help me follow the details of the embedded format definitions. I’m offering it here to help others fathom these details.  In the paragraphs that follow the code, I list some of my observations/conclusions in hopes that others might shed additional light.

 

post-29172-0-87411800-1403978439_thumb.p

;========================================
;   Tool for analyzing AutoIt's Rich Text Processing Features
;   (and to demonstrate serveral of the common methods)
;   >> based on the example script developed by mLipok
;   >> with these "tool" enhancements by qwert
;   June, 2014
;========================================
#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Local $hGui, $hRichEdit, $hRichEdit2, $hRichEdit3, $hRichEdit123
Local $iMsg

$hGui = GUICreate("Rich Edit Tools", 1260, 400, -1, 200)

;#Region Create all RichEdit controls
$hRichEdit1 = _GUICtrlRichEdit_Create($hGui, "This is a first test.", 10, 10, 300, 100, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_AppendText($hRichEdit1, @CRLF & "This is more text" & @CRLF)
_GUICtrlRichEdit_SetSel($hRichEdit1, 0, 22)
_GUICtrlRichEdit_SetFont($hRichEdit1, 14, "Arial")
_GUICtrlRichEdit_SetSel($hRichEdit1, -1, -1)

$hRichEdit2 = _GUICtrlRichEdit_Create($hGui, "This is a second test.", 10, 130, 300, 100, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_AppendText($hRichEdit2, @CRLF & "This is more text" & @CRLF)
_GUICtrlRichEdit_SetSel($hRichEdit2, 10, 22)
_GUICtrlRichEdit_SetFont($hRichEdit2, 14, "Arial")
_GUICtrlRichEdit_SetSel($hRichEdit2, -1, -1)

$hRichEdit3 = _GUICtrlRichEdit_Create($hGui, "This is a third test.", 10, 250, 300, 100, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_AppendText($hRichEdit3, @CRLF & "This is more text" & @CRLF)
_GUICtrlRichEdit_SetSel($hRichEdit3, 10, 21)
_GUICtrlRichEdit_SetFont($hRichEdit3, 14, "Arial")
_GUICtrlRichEdit_SetSel($hRichEdit3, -1, -1)

$hRichEdit123 = _GUICtrlRichEdit_Create($hGui, "This is the combined result.", 320, 30, 300, 280, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_AppendText($hRichEdit123, @CRLF & @CRLF)
;#EndRegion Create all RichEdit controls

$hVerbatim = GUICtrlCreateEdit ("This is the resulting Rich Text.", 630, 30, 300, 280, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
$hRichResult = _GUICtrlRichEdit_Create($hGui, "Reserved for process result.", 940, 30, 300, 280, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

GUISetFont(16, 700, 0, "Arial")
GUICtrlCreateLabel("Combined", 324, 6, 120, 24)
GUICtrlCreateLabel("Verbatim", 634, 6, 120, 24)
GUICtrlCreateLabel("Result", 944, 6, 120, 24)
GUICtrlCreateLabel("\", 562, 310, 120, 24)
GUICtrlCreateLabel("\", 872, 310, 120, 24)
GUICtrlCreateLabel("/", 680, 310, 120, 24)
GUICtrlCreateLabel("/", 990, 310, 120, 24)
GUISetFont(12, 400, 0, "Arial")
GUICtrlCreateLabel("These tools use the copy/paste method of combining the rich text fields.", 320, 372, 500, 24)
$Combine = GUICtrlCreateButton("Combine", 104, 364, 120, 24)
GUICtrlSetTip(-1, "Click to redo the combining operation")
$ProcessRich = GUICtrlCreateButton("Process", 564, 330, 120, 24)
GUICtrlSetTip(-1, "Click to produce Verbatim from Combined")
$ProcessVerb = GUICtrlCreateButton("Process", 874, 330, 120, 24)
GUICtrlSetTip(-1, "Click to produce Result from Verbatim")

GUISetState(@SW_SHOW)

Sleep(800)      ; for effect only
_GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit1, $hRichEdit123)  ; Copying $hRichEdit1 to $hRichEdit123
Sleep(800)
_GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit2, $hRichEdit123)  ; Copying $hRichEdit2 to $hRichEdit123
Sleep(800)
_GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit3, $hRichEdit123)  ; Copying $hRichEdit3 to $hRichEdit123
Local $Var = _GUICtrlRichEdit_StreamToVar($hRichEdit123, True)
$Var = StringReplace($Var, "MS Shell Dlg", "Arial", 0)              ; <<<< example of a DIRECT EDIT to change the default font
Sleep(800)
;MsgBox(0, "Result", $Var, 0)                                       ; OPTION to view before storing
_GUICtrlRichEdit_StreamFromVar ($hRichEdit123, $Var)
_GUICtrlRichEdit_StreamToFile($hRichEdit123, @ScriptDir & "\Rich123.rtf", True)
GUICtrlSetData($hVerbatim, $Var)
Sleep(800)      ; again, for effect
MsgBox(0, "", "Done", 1)

While True
    $iMsg = GUIGetMsg()
    Select
    Case $iMsg = $Combine
      _GUICtrlRichEdit_SetSel($hRichEdit123, 0, -1)
      _GUICtrlRichEdit_ReplaceText($hRichEdit123, "New result:" & @CRLF & @CRLF)
      Sleep(800)        ; for effect only
      _GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit1, $hRichEdit123)
      Sleep(800)        ; for effect only
     _GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit2, $hRichEdit123)
      Sleep(800)        ; for effect only
     _GUICtrlRichEdit_CopyAllToRichEdit($hRichEdit3, $hRichEdit123)
      Sleep(800)        ; again, for effect
       MsgBox(0, "", "Done" & @CRLF & "Verbatim is unchanged at this point." & @CRLF & "Click Process to update it.", 2)
    Case $iMsg = $ProcessRich
      Local $Var = _GUICtrlRichEdit_StreamToVar($hRichEdit123, True)
      GUICtrlSetData($hVerbatim, $Var)
      MsgBox(0, "", "Done", 1)
    Case $iMsg = $ProcessVerb
      _GUICtrlRichEdit_StreamFromVar ($hRichResult, GUICtrlRead($hVerbatim))
      MsgBox(0, "", "Done", 1)
    Case $iMsg = $GUI_EVENT_CLOSE
      _GUICtrlRichEdit_Destroy($hRichEdit1) ; needed unless script crashes
      _GUICtrlRichEdit_Destroy($hRichEdit2) ; needed unless script crashes
      _GUICtrlRichEdit_Destroy($hRichEdit3) ; needed unless script crashes
      _GUICtrlRichEdit_Destroy($hRichResult) ; needed unless script crashes
      _GUICtrlRichEdit_Destroy($hRichEdit123) ; needed unless script crashes
        ; GUIDelete()   ; is OK too
      Exit
      EndSelect
WEnd

Func _GUICtrlRichEdit_CopyAllToRichEdit(ByRef $hSourceRichEdit, ByRef $hDestinationRichEdit)
    Local $aSelection = _GUICtrlRichEdit_GetSel($hSourceRichEdit)   ; check for current selection in source control
    _GUICtrlRichEdit_SetSel($hSourceRichEdit, 0, -1)                    ; select all contents in source control
    _GUICtrlRichEdit_Copy($hSourceRichEdit)                     ; copy selected contents from source control
    _GUICtrlRichEdit_Paste($hDestinationRichEdit)                   ; paste content to destination control
    _GUICtrlRichEdit_SetSel($hSourceRichEdit, $aSelection[0], $aSelection[1])       ; revert back selection in source control
EndFunc   ;==>_GUICtrlRichEdit_CopyAllToRichEdit

Observations of AutoIt’s features and limitations:

 

There is no effective way to assign a default font/size for a RichEdit control. You must assign the font/size to existing segments of characters ... or to the entire contents of a control (with Select All).

 

There is no "native method" to directly write more that one control to a file*. StreamToFile handles formatting well, but can only stream out one named control to a named file. There is no option to append to these files (as there is with normal writes).

 

(* as demonstrated by this tool, methods exist to combine RichEdit controls into a single control for use with the StreamToFile operation.)

 

Likewise, copy to clipboard (_GuiCtrlRichEdit_Copy) preserves RichText formatting and can be useful in moving field content. From what I understand, the clipboard maintains both the RTF and text-only versions of the content. Paste and Paste Special determine which version an application receives.

 

AutoIt's normal file and line writes are for text/data only and cannot be used for RichText controls.

 

_GuiCtrlRichEdit_GetText gets only the text content, without character formatting.

 

If you want to work directly with the RTF syntax, you must first to stream the RichEdit to a Variable ... and then use normal search/replace/add string operations to alter the contents of the variable. You must then update the original RichEdit using StreamFromVar in order to display your result or write to a file (StreamToFile).

 

In Summary

 

Think of a RichEdit control as a “processed container” for format-encoded text. The content is displayed according to rules that are embedded with the text. You can’t see the rules. You only observe their effects. And you can only use the RichEdit UDF actions to make changes to the content in a RichEdit control.

 

If you want to see and work with those rules “verbatim”, first stream the RichEdit contents into a variable and then place the contents of that variable into a normal edit control. Once there, you can parse the rich text statements and apply literal edits and exchanges using String operations. Stream the contents of the edit control back into a RichEdit control to see the effects of those changes.

 

The Rich Edit Tools script provides examples of the types of conversions that can be made. The field on the far right is the “interpreted result” of the verbatim text. Note the direct edit of the default font in the script.

 

(A question: note the use of ByRef in the Function. Since it works without them, what is their purpose in this instance?)

 

Everything I’ve outlined here is subject to correction by anyone with more in-depth experience and understanding. Please feel free to contribute.

 

Thanks to mLipok for posting the basis for this example script, in particular, the _GUICtrlRichEdit_CopyAllToRichEdit method for combining field content.

 

 

1 person likes this

Share this post


Link to post
Share on other sites



I will try to go into this script in a few days, please emind me via PM, if I forget.

1 person likes this

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
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
Sign in to follow this  
Followers 0