#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include-once
#include <Clipboard.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <ScrollBarConstants.au3>
#include <UDFGlobalID.au3>
#include <WindowsConstants.au3>
; #INDEX# =========================================================================================
; Title .........: Rich Edit
; AutoIt Version:  3.3.0++
; Language:        English
; Description:     Programmer-friendly Rich Edit control
; =================================================================================================
; #CURRENT# =======================================================================================
;_GUICtrlRichEdit_AppendText
;_GUICtrlRichEdit_AutoDetectURL
;_GUICtrlRichEdit_CanPaste
;_GuiCtrlRichEdit_CanPasteSpecial
;_GUICtrlRichEdit_CanRedo
;_GUICtrlRichEdit_CanUndo
;_GuiCtrlRichEdit_ChangeFontSize
;_GUICtrlRichEdit_Copy
;_GUICtrlRichEdit_Create
;_GUICtrlRichEdit_Cut
;_GuiCtrlRichEdit_Deselect
;_GUICtrlRichEdit_Destroy
;_GuiCtrlRichEdit_EmptyUndoBuffer
;_GUICtrlRichEdit_FindText
;_GUICtrlRichEdit_FindTextInRange
;_GuiCtrlRichEdit_GetCharAttributes
;_GUICtrlRichEdit_GetCharPosFromXY
;_GuiCtrlRichEdit_GetCharPosOfNextWord
;_GuiCtrlRichEdit_GetCharPosOfPreviousWord
;_GuiCtrlRichEdit_GetCharWordBreakInfo
;_GUICtrlRichEdit_GetCtrlText
;_GUICtrlRichEdit_GetCtrlTextLength
;_GUICtrlRichEdit_GetCtrlZoom
;_GUICtrlRichEdit_GetFirstCharPosOnLine
;_GUICtrlRichEdit_GetFont
;_GUICtrlRichEdit_GetFormattingRect
;_GUICtrlRichEdit_GetLineCount
;_GUICtrlRichEdit_GetLineLength
;_GuiCtrlRichEdit_GetLineNUmberFromCharPos
;_GUICtrlRichEdit_GetNextRedo
;_GUICtrlRichEdit_GetNextUndo
;_GUICtrlRichEdit_GetNumberOfFirstVisibleLine
;_GUICtrlRichEdit_GetParaAlignment
;_GuiCtrlRichEdit_GetParaAttributes
;_GuiCtrlRichEdit_GetParaBorder
;_GuiCtrlRichEdit_GetParaIndents
;_GUICtrlRichEdit_GetParaNumbering
;_GuiCtrlRichEdit_GetParaShading
;_GUICtrlRichEdit_GetParaSpacing
;_GUICtrlRichEdit_GetParaTabStops
;_GUICtrlRichEdit_GetPasswordChar
;_GUICtrlRichEdit_GetScrollPos
;_GUICtrlRichEdit_GetSel
;_GuiCtrlRichEdit_GetSelAA
;_GUICtrlRichEdit_GetSelText
;_GuiCtrlRichEdit_GetSpaceUnit
;_GUICtrlRichEdit_GetTextinLine
;_GUICtrlRichEdit_GetTextInRange
;_GuiCtrlRichEdit_GetVersion
;_GuiCtrlRichEdit_GetXYFromCharPos
;_GuiCtrlRichEdit_GotoCharPos
;_GUICtrlRichEdit_HideSelection
;_GUICtrlRichEdit_InsertText
;_GuiCtrlRichEdit_IsModified
;_GUICtrlRichEdit_IsTextSelected
;_GUICtrlRichEdit_Paste
;_GuiCtrlRichEdit_PasteSpecial
;_GUICtrlRichEdit_PauseRedraw
;_GUICtrlRichEdit_Redo
;_GuiCtrlRichEdit_ReplaceText
;_GUICtrlRichEdit_ResumeRedraw
;_GUICtrlRichEdit_ScrollLineOrPage
;_GUICtrlRichEdit_ScrollLines
;_GuiCtrlRichEdit_ScrollToCaret
;_GuiCtrlRichEdit_SetCharAttributes
;_GUICtrlRichEdit_SetCtrlBkColor
;_GuiCtrlRichEdit_SetCtrlLimitOnText
;_GUICtrlRichEdit_SetCtrlTabStops
;_GUICtrlRichEdit_SetCtrlZoom
;_GUICtrlRichEdit_SetEventMask
;_GUICtrlRichEdit_SetFont
;_GUICtrlRichEdit_SetModified
;_GUICtrlRichEdit_SetParaAlignment
;_GuiCtrlRichEdit_SetParaAttributes
;_GuiCtrlRichEdit_SetParaBorder
;_GuiCtrlRichEdit_SetParaIndents
;_GUICtrlRichEdit_SetParaNumbering
;_GuiCtrlRichEdit_SetParaShading
;_GUICtrlRichEdit_SetParaSpacing
;_GUICtrlRichEdit_SetParaTabStops
;_GUICtrlRichEdit_SetPasswordChar
;_GUICtrlRichEdit_SetScrollPos
;_GUICtrlRichEdit_SetSel
;_GuiCtrlRichEdit_SetSpaceUnit
;_GUICtrlRichEdit_SetText
;_GUICtrlRichEdit_SetUndoLimit
;_GUICtrlRichEdit_StreamFromFile
;_GUICtrlRichEdit_StreamFromVar
;_GUICtrlRichEdit_StreamToFile
;_GUICtrlRichEdit_StreamToVar
;_GUICtrlRichEdit_Undo
;
; =================================================================================================
; #INTERNAL_USE_ONLY#==============================================================================
;__GCR_ConvertColorRefToRGBString
;__GCR_ConvertRomanToNumber
;__GCR_ConvertTwipsToSpaceUnit
;__GCR_GetParaScopeChar
;__GCR_Init
;__GCR_InvalidateRect
;__GCR_IsNumeric
;__GCR_ParseParaNumberingStyle
;__GCR_SendGetCharFormatMessage
;__GCR_SendGetParaFormatMessage
;__GCR_SendMessage
;__GCR_SetOLECallback
;__GCR_StreamFromFileCallback
;__GCR_StreamFromVarCallback
;__GCR_StreamToFileCallback
;__GCR_StreamToVarCallback
;__RichCom_Object_AddRef
;__RichCom_Object_ContextSensitiveHelp
;__RichCom_Object_DeleteObject
;__RichCom_Object_GetClipboardData
;__RichCom_Object_GetContextMenu
;__RichCom_Object_GetDragDropEffect
;__RichCom_Object_GetInPlaceContext
;__RichCom_Object_GetNewStorage
;__RichCom_Object_QueryAcceptData
;__RichCom_Object_QueryInsertObject
;__RichCom_Object_QueryInterface
;__RichCom_Object_Release
;__RichCom_Object_ShowContainerUI
;
; =================================================================================================
Global $_GRE_oldMustDeclareVars = Opt("MustDeclareVars", 1)
; Author(s): GaryFrost, grham, Prog@ndy, KIP
; OLE stuff: example from http://www.powerbasic.com/support/pbforums/showpost.php?p=294112&postcount=7
Global $__GCR_DebugIt = 0
;~ Global Const $WM_NOTIFY = 0x4E
;~ Global Const $WM_USER = 0x400
Global $RICHEDIT_OLE_CALLBACKDLL = "EMPTY"
;Global Const $RICHEDIT_CLASS10A = "RICHEDIT"
;Global Const $RICHEDIT_CLASS = $RICHEDIT_CLASS10A
;Global Const $RICHEDIT_CLASSA = "RichEdit20A"
;Global Const $RICHEDIT_CLASSW = "RichEdit20W"
;Global Const $RICHEDIT_CLASS50W = "RichEdit50W"
Global $_GRE_sRTFClassName, $h_GUICtrlRTF_lib, $_GRE_Version, $_GRE_TwipsPeSpaceUnit = 1440 ; inches
Global $_GRE_hUser32dll, $_GRE_CF_RTF, $_GRE_CF_RETEXTOBJ

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_LoadDLL
; Description ...: Sets global variables $_GRE_sRTFClassName, $h_GUICtrlRTF_lib, $_GRE_Version, $_GRE_CF_RTF and $_GRE_CF_RETEXTOBJ
; Syntax.........: __GCR_LoadDLL{}
; Parameters ....: none
; Return values .: none
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
;
; =================================================================================================
Func __GCR_Init()
	$h_GUICtrlRTF_lib = DllCall("kernel32.dll", "hwnd", "LoadLibraryA", "str", "MSFTEDIT.DLL")
	If $h_GUICtrlRTF_lib[0] <> 0 Then
		$_GRE_sRTFClassName = "RichEdit50W"
		$_GRE_Version = 4.1
	Else
		;RICHED20.DLL
		$h_GUICtrlRTF_lib = DllCall("kernel32.dll", "hwnd", "LoadLibraryA", "str", "RICHED20.DLL")
		$_GRE_Version = FileGetVersion(@SystemDir & "\riched20.dll", "ProductVersion")
		Switch $_GRE_Version
			Case 3.0
				$_GRE_sRTFClassName = "RichEdit20W"
			Case 5.0
				$_GRE_sRTFClassName = "RichEdit50W"
			Case 6.0
				$_GRE_sRTFClassName = "RichEdit60W"
		EndSwitch
	EndIf
	$_GRE_CF_RTF = _ClipBoard_RegisterFormat("Rich Text Format")
	$_GRE_CF_RETEXTOBJ = _ClipBoard_RegisterFormat("Rich Text Format with Objects")
EndFunc   ;==>__GCR_Init
;--------------------------------------------------------------------------------------------------

;---------------------------- Constants -----------------------------------------------------------
Global Const $ICC_STANDARD_CLASSES = 0x4000
Global Const $ST_DEFAULT = 0
Global Const $ST_KEEPUNDO = 1
Global Const $ST_SELECTION = 2
Global Const $GT_DEFAULT = 0
Global Const $GT_SELECTION = 2
Global Const $GT_USECRLF = 1
Global Const $GTL_CLOSE = 4
Global Const $GTL_DEFAULT = 0
Global Const $GTL_NUMBYTES = 16
Global Const $GTL_NUMCHARS = 8
Global Const $GTL_PRECISE = 2
Global Const $GTL_USECRLF = 1
; pitch and family
;~ If Not IsDeclared("DEFAULT_PITCH") Then Global Const $DEFAULT_PITCH	= 0
;~ If Not IsDeclared("FF_DONTCARE") Then Global Const $FF_DONTCARE		= 0
;~ Global Const $FF_ROMAN = 16
;~ Global Const $FF_SWISS = 32
;~ Global Const $FF_MODERN = 48
;~ Global Const $FF_SCRIPT = 64
;~ Global Const $FW_DONTCARE = 0
;~ Global Const $FW_THIN = 100
;~ Global Const $FW_EXTRALIGHT = 200
;~ Global Const $FW_ULTRALIGHT = 200
;~ Global Const $FW_LIGHT = 300
;~ Global Const $FW_NORMAL = 400
;~ Global Const $FW_REGULAR = 400
;~ Global Const $FW_MEDIUM = 500
;~ Global Const $FW_SEMIBOLD = 600
;~ Global Const $FW_DEMIBOLD = 600
;~ Global Const $FW_BOLD = 700
;~ Global Const $FW_EXTRABOLD = 800
;~ Global Const $FW_ULTRABOLD = 800
;~ Global Const $FW_HEAVY = 900
;~ Global Const $FW_BLACK = 900
; char sets
;~ Global Const $ANSI_CHARSET = 0
;~ Global Const $DEFAULT_CHARSET = 1
;~ Global Const $SYMBOL_CHARSET = 2
;~ Global Const $MAC_CHARSET = 77
;~ Global Const $SHIFTJIS_CHARSET = 128
;~ Global Const $HANGEUL_CHARSET = 129
;~ Global Const $GB2312_CHARSET = 134
;~ Global Const $CHINESEBIG5_CHARSET = 136
;~ Global Const $GREEK_CHARSET = 161
;~ Global Const $TURKISH_CHARSET = 162
;~ Global Const $VIETNAMESE_CHARSET = 163
;~ Global Const $BALTIC_CHARSET = 186
;~ Global Const $RUSSIAN_CHARSET = 204
;~ Global Const $OEM_CHARSET = 255
Global Const $CFU_UNDERLINENONE = 0
Global Const $CFU_UNDERLINE = 1
Global Const $CFU_UNDERLINEWORD = 2
Global Const $CFU_UNDERLINEDOUBLE = 3
Global Const $CFU_UNDERLINEDOTTED = 4
; code pages
Global Const $CP_ACP = 0 ; use system default
Global Const $CP_UNICODE = 1200
Global Const $CP_37 = 37
Global Const $CP_273 = 273
Global Const $CP_277 = 277
Global Const $CP_278 = 278
Global Const $CP_280 = 280
Global Const $CP_284 = 284
Global Const $CP_285 = 285
Global Const $CP_290 = 290
Global Const $CP_297 = 297
Global Const $CP_423 = 423
Global Const $CP_500 = 500
Global Const $CP_875 = 875
Global Const $CP_930 = 930
Global Const $CP_931 = 931
Global Const $CP_932 = 932
Global Const $CP_933 = 933
Global Const $CP_935 = 935
Global Const $CP_936 = 936
Global Const $CP_937 = 937
Global Const $CP_939 = 939
Global Const $CP_949 = 949
Global Const $CP_950 = 950
Global Const $CP_1027 = 1027
Global Const $CP_5026 = 5026
Global Const $CP_5035 = 5035
Global Const $CFM_ALLCAPS = 0x80
Global Const $CFM_ANIMATION = 0x40000
Global Const $CFM_BACKCOLOR = 0x4000000
Global Const $CFM_BOLD = 0x1
Global Const $CFM_CHARSET = 0x8000000
Global Const $CFM_COLOR = 0x40000000
Global Const $CFM_DISABLED = 0x2000
Global Const $CFM_EMBOSS = 0x800
Global Const $CFM_FACE = 0x20000000
Global Const $CFM_HIDDEN = 0x100
Global Const $CFM_IMPRINT = 0x1000
Global Const $CFM_ITALIC = 0x2
Global Const $CFM_KERNING = 0x100000
Global Const $CFM_LCID = 0x2000000
Global Const $CFM_LINK = 0x20
Global Const $CFM_OFFSET = 0x10000000
Global Const $CFM_OUTLINE = 0x200
Global Const $CFM_PROTECTED = 0x10
Global Const $CFM_REVAUTHOR = 0x8000
Global Const $CFM_REVISED = 0x4000
Global Const $CFM_SHADOW = 0x400
Global Const $CFM_SIZE = 0x80000000
Global Const $CFM_SMALLCAPS = 0x40
Global Const $CFM_SPACING = 0x200000
Global Const $CFM_STRIKEOUT = 0x8
Global Const $CFM_STYLE = 0x80000
Global Const $CFM_SUBSCRIPT = BitOR(0x10000, 0x20000)
Global Const $CFM_SUPERSCRIPT = $CFM_SUBSCRIPT
Global Const $CFM_UNDERLINE = 0x4
Global Const $CFM_UNDERLINETYPE = 0x800000
Global Const $CFM_WEIGHT = 0x400000
Global Const $CFE_ALLCAPS = $CFM_ALLCAPS
Global Const $CFE_AUTOBACKCOLOR = $CFM_BACKCOLOR
Global Const $CFE_AUTOCOLOR = 0x40000000
Global Const $CFE_BOLD = $CFM_BOLD
Global Const $CFE_DISABLED = $CFM_DISABLED
Global Const $CFE_EMBOSS = $CFM_EMBOSS
Global Const $CFE_HIDDEN = $CFM_HIDDEN
Global Const $CFE_IMPRINT = $CFM_IMPRINT
Global Const $CFE_ITALIC = $CFM_ITALIC
Global Const $CFE_LINK = $CFM_LINK
Global Const $CFE_OUTLINE = $CFM_OUTLINE
Global Const $CFE_PROTECTED = $CFM_PROTECTED
Global Const $CFE_REVISED = $CFM_REVISED
Global Const $CFE_SHADOW = $CFM_SHADOW
Global Const $CFE_SMALLCAPS = $CFM_SMALLCAPS
Global Const $CFE_STRIKEOUT = 0x8
Global Const $CFE_SUBSCRIPT = 0x10000
Global Const $CFE_SUPERSCRIPT = 0x20000
Global Const $CFE_UNDERLINE = 0x4
Global Const $FR_DOWN = 0x1
Global Const $FR_MATCHALEFHAMZA = 0x80000000
Global Const $FR_MATCHCASE = 0x4
Global Const $FR_MATCHDIAC = 0x20000000
Global Const $FR_MATCHKASHIDA = 0x40000000
Global Const $FR_WHOLEWORD = 0x2
;~ Global Const $CFM_EFFECTS = BitOR($CFM_BOLD, $CFM_ITALIC, $CFM_UNDERLINE, $CFM_COLOR, $CFM_STRIKEOUT, $CFE_PROTECTED, $CFM_LINK)
;~ Global Const $CFM_ALL = BitOR($CFM_EFFECTS, $CFM_SIZE, $CFM_FACE, $CFM_OFFSET, $CFM_CHARSET)
Global Const $SCF_DEFAULT = 0x0
Global Const $SCF_SELECTION = 0x1
Global Const $SCF_WORD = 0x2
Global Const $SCF_ALL = 0x4
Global Const $SCF_USEUIRULES = 0x8
Global Const $SCF_ASSOCIATEFONT = 0x10
Global Const $SCF_NOKBUPDATE = 0x20
Global Enum $UID_UNKNOWN = 0, _
		$UID_TYPING = 1, _
		$UID_DELETE = 2, _
		$UID_DRAGDROP = 3, _
		$UID_CUT = 4, _
		$UID_PASTE = 5
;--------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------
;---------------------- Messages ------------------------------------------------------------------
; RichEdit Messages
Global Const $EM_AUTOURLDETECT = ($WM_USER + 91)
Global Const $EM_CANPASTE = ($WM_USER + 50)
Global Const $EM_CANREDO = ($WM_USER + 85)
Global Const $EM_DISPLAYBAND = ($WM_USER + 51)
Global Const $EM_EXGETSEL = ($WM_USER + 52)
Global Const $EM_EXLIMITTEXT = ($WM_USER + 53)
Global Const $EM_EXLINEFROMCHAR = ($WM_USER + 54)
Global Const $EM_EXSETSEL = ($WM_USER + 55)
Global Const $EM_FINDTEXT = ($WM_USER + 56)
Global Const $EM_FINDTEXTEX = ($WM_USER + 79)
Global Const $EM_FINDTEXTEXW = ($WM_USER + 124)
Global Const $EM_FINDTEXTW = ($WM_USER + 123)
Global Const $EM_FINDWORDBREAK = ($WM_USER + 76)
Global Const $EM_FORMATRANGE = ($WM_USER + 57)
Global Const $EM_GETAUTOURLDETECT = ($WM_USER + 92)
Global Const $EM_GETBIDIOPTIONS = ($WM_USER + 201)
Global Const $EM_GETCHARFORMAT = ($WM_USER + 58)
Global Const $EM_GETEDITSTYLE = ($WM_USER + 205)
Global Const $EM_GETEVENTMASK = ($WM_USER + 59)
Global Const $EM_GETIMECOLOR = ($WM_USER + 105)
Global Const $EM_GETIMECOMPMODE = ($WM_USER + 122)
Global Const $EM_GETIMEMODEBIAS = ($WM_USER + 127)
Global Const $EM_GETIMEOPTIONS = ($WM_USER + 107)
Global Const $EM_GETLANGOPTIONS = ($WM_USER + 121)
Global Const $EM_GETOPTIONS = ($WM_USER + 78)
Global Const $EM_GETPARAFORMAT = ($WM_USER + 61)
Global Const $EM_GETPUNCTUATION = ($WM_USER + 101)
Global Const $EM_GETREDONAME = ($WM_USER + 87)
Global Const $EM_GETSCROLLPOS = ($WM_USER + 221)
Global Const $EM_GETSELTEXT = ($WM_USER + 62)
Global Const $EM_GETTEXTEX = ($WM_USER + 94)
Global Const $EM_GETTEXTLENGTHEX = ($WM_USER + 95)
Global Const $EM_GETTEXTMODE = ($WM_USER + 90)
Global Const $EM_GETTEXTRANGE = ($WM_USER + 75)
Global Const $EM_GETTYPOGRAPHYOPTIONS = ($WM_USER + 203)
Global Const $EM_GETUNDONAME = ($WM_USER + 86)
Global Const $EM_GETWORDBREAKPROCEX = ($WM_USER + 80)
Global Const $EM_GETWORDWRAPMODE = ($WM_USER + 103)
Global Const $EM_GETZOOM = ($WM_USER + 224)
Global Const $EM_HIDESELECTION = ($WM_USER + 63)
Global Const $EM_PASTESPECIAL = ($WM_USER + 64)
Global Const $EM_RECONVERSION = ($WM_USER + 125)
Global Const $EM_REDO = ($WM_USER + 84)
Global Const $EM_REQUESTRESIZE = ($WM_USER + 65)
Global Const $EM_SELECTIONTYPE = ($WM_USER + 66)
Global Const $EM_SETBIDIOPTIONS = ($WM_USER + 200)
Global Const $EM_SETBKGNDCOLOR = ($WM_USER + 67)
Global Const $EM_SETCHARFORMAT = ($WM_USER + 68)
Global Const $EM_SETEDITSTYLE = ($WM_USER + 204)
Global Const $EM_SETEVENTMASK = ($WM_USER + 69)
Global Const $EM_SETFONTSIZE = ($WM_USER + 223)
Global Const $EM_SETIMECOLOR = ($WM_USER + 104)
Global Const $EM_SETIMEMODEBIAS = ($WM_USER + 126)
Global Const $EM_SETIMEOPTIONS = ($WM_USER + 106)
Global Const $EM_SETLANGOPTIONS = ($WM_USER + 120)
Global Const $EM_SETOLECALLBACK = ($WM_USER + 70)
Global Const $EM_SETOPTIONS = ($WM_USER + 77)
Global Const $EM_SETPALETTE = ($WM_USER + 93)
Global Const $EM_SETPARAFORMAT = ($WM_USER + 71)
Global Const $EM_SETPUNCTUATION = ($WM_USER + 100)
Global Const $EM_SETSCROLLPOS = ($WM_USER + 222)
Global Const $EM_SETTARGETDEVICE = ($WM_USER + 72)
Global Const $EM_SETTEXTEX = ($WM_USER + 97)
Global Const $EM_SETTEXTMODE = ($WM_USER + 89)
Global Const $EM_SETTYPOGRAPHYOPTIONS = ($WM_USER + 202)
Global Const $EM_SETUNDOLIMIT = ($WM_USER + 82)
Global Const $EM_SETWORDBREAKPROCEX = ($WM_USER + 81)
Global Const $EM_SETWORDWRAPMODE = ($WM_USER + 102)
Global Const $EM_SETZOOM = ($WM_USER + 225)
Global Const $EM_SHOWSCROLLBAR = ($WM_USER + 96)
Global Const $EM_STOPGROUPTYPING = ($WM_USER + 88)
Global Const $EM_STREAMIN = ($WM_USER + 73)
Global Const $EM_STREAMOUT = ($WM_USER + 74)
; picked up from EditConstants.au3
;Global Const $EM_CANUNDO = 0xC6
;Global Const $EM_CHARFROMPOS = 0xD7
;Global Const $EM_EMPTYUNDOBUFFER = 0xCD
;Global Const $EM_GETFIRSTVISIBLELINE = 0xCE
;Global Const $EM_GETLINE = 0xC4
;Global Const $EM_GETLINECOUNT = 0xBA
;Global Const $EM_GETPASSWORDCHAR = 0xD2
;Global Const $EM_GETRECT = 0xB2
;Global Const $EM_LINEINDEX = 0xBB
;Global Const $EM_LINELENGTH = 0xC1
;Global Const $EM_LINESCROLL = 0xB6
;Global Const $EM_GETMODIFY = 0xB8
;Global Const $EM_POSFROMCHAR = 0xD6
;Global Const $EM_REPLACESEL = 0xC2
;Global Const $EM_SETMODIFY = 0xB9
;Global Const $EM_SETPASSWORDCHAR = 0xCC
;Global Const $EM_SETSEL = 0xB1
;Global Const $EM_SETTABSTOPS = 0xCB
;Global Const $EM_UNDO = 0xC7

;Global Const $ES_AUTOHSCROLL = 128
;Global Const $ES_AUTOVSCROLL = 64
;Global Const $ES_CENTER = 1
;Global Const $ES_LEFT = 0
;Global Const $ES_MULTILINE = 4
;Global Const $ES_NOHIDESEL = 256

Global Const $EN_ALIGNLTR = 0X710
Global Const $EN_ALIGNRTL = 0X711
Global Const $EN_CORRECTTEXT = 0X705
Global Const $EN_DRAGDROPDONE = 0X70c
Global Const $EN_DROPFILES = 0X703
Global Const $EN_IMECHANGE = 0X707
Global Const $EN_LINK = 0X70b
Global Const $EN_MSGFILTER = 0X700
Global Const $EN_OBJECTPOSITIONS = 0X70a
Global Const $EN_OLEOPFAILED = 0X709
Global Const $EN_PROTECTED = 0X704
Global Const $EN_REQUESTRESIZE = 0X701
Global Const $EN_SAVECLIPBOARD = 0X708
Global Const $EN_SELCHANGE = 0X702
Global Const $EN_STOPNOUNDO = 0X706
Global Const $ENM_CHANGE = 0x1
Global Const $ENM_CORRECTTEXT = 0x400000
Global Const $ENM_DRAGDROPDONE = 0x10
Global Const $ENM_DROPFILES = 0x100000
Global Const $ENM_IMECHANGE = 0x800000
Global Const $ENM_KEYEVENTS = 0x10000
Global Const $ENM_LINK = 0x4000000
Global Const $ENM_MOUSEEVENTS = 0x20000
Global Const $ENM_OBJECTPOSITIONS = 0x2000000
Global Const $ENM_PROTECTED = 0x200000
Global Const $ENM_REQUESTRESIZE = 0x40000
Global Const $ENM_SCROLL = 0x4
Global Const $ENM_SCROLLEVENTS = 0x8
Global Const $ENM_SELCHANGE = 0x80000
Global Const $ENM_UPDATE = 0x2
Global Const $ES_DISABLENOSCROLL = 0x2000
Global Const $ES_EX_NOCALLOLEINIT = 0x1000000
Global Const $ES_NOIME = 0x80000
Global Const $ES_SELFIME = 0x40000
Global Const $ES_SUNKEN = 0x4000
;Global Const $ES_NUMBER = 0x2000
;~ Global Const $ES_PASSWORD				= 0x20
;Global Const $ES_READONLY = 0x800
;~ Global Const $ES_RIGHT					= 0x2
;Global Const $ES_WANTRETURN = 0x1000
If Not IsDeclared("WM_LBUTTONDBLCLK") Then Global Const $WM_LBUTTONDBLCLK = 0x203
If Not IsDeclared("WM_LBUTTONDOWN") Then Global Const $WM_LBUTTONDOWN = 0x201
;~ Global Const $WM_LBUTTONUP = 0x202
;~ Global Const $WM_MOUSEMOVE = 0x200
If Not IsDeclared("WM_RBUTTONDBLCLK") Then Global Const $WM_RBUTTONDBLCLK = 0x206
If Not IsDeclared("WM_RBUTTONDOWN") Then Global Const $WM_RBUTTONDOWN = 0x204
If Not IsDeclared("WM_RBUTTONUP") Then Global Const $WM_RBUTTONUP = 0x205
;~ Global Const $WM_SETCURSOR = 0x20
; structure formats
Global Const $LF_FACESIZE = 32
Global Const $MAX_TAB_STOPS = 32
Global Const $CFM_RichEdit_SET = 0x08000000
Global Const $PFA_LEFT = 0x1
Global Const $PFA_RIGHT = 0x2
Global Const $PFA_CENTER = 0x3
Global Const $PFA_JUSTIFY = 4
Global Const $PFA_FULL_INTERWORD = 4
Global Const $PFE_TABLE = 0x4000
Global Const $PFM_NUMBERING = 0x20
Global Const $PFM_ALIGNMENT = 0x8
Global Const $PFM_SPACEBEFORE = 0x40
Global Const $PFM_NUMBERINGSTYLE = 0x2000
Global Const $PFM_NUMBERINGSTART = 0x8000
Global Const $PFM_BORDER = 0x800
Global Const $PFM_RIGHTINDENT = 0x2
Global Const $PFM_STARTINDENT = 0x1
Global Const $PFM_OFFSET = 0x4
Global Const $PFM_OFFSETINDENT = 0x80000000
Global Const $PFM_LINESPACING = 0x100
Global Const $PFM_SPACEAFTER = 0x80
Global Const $PFM_NUMBERINGTAB = 0x4000
Global Const $PFM_TABLE = 0x40000000
Global Const $PFM_TABSTOPS = 0x10
Global Const $PFN_BULLET = 0x1
; Source: http://cvs.sourceforge.jp/cgi-bin/viewcvs.cgi/ablib/ver5lib/Include/api_richedit.sbp?revision=1.2
Global Const $PFM_RTLPARA = 0x10000
Global Const $PFM_KEEP = 0x20000
Global Const $PFM_KEEPNEXT = 0x40000
Global Const $PFM_PAGEBREAKBEFORE = 0x80000
Global Const $PFM_NOLINENUMBER = 0x100000
Global Const $PFM_NOWIDOWCONTROL = 0x200000
Global Const $PFM_DONOTHYPHEN = 0x400000
Global Const $PFM_SIDEBYSIDE = 0x800000
;Global Const $PFM_TABLE =           	  0xc0000000		; differs from value given above
Global Const $PFE_RTLPARA = BitShift($PFM_RTLPARA, 16)
Global Const $PFE_KEEP = BitShift($PFM_KEEP, 16)
Global Const $PFE_KEEPNEXT = BitShift($PFM_KEEPNEXT, 16)
Global Const $PFE_PAGEBREAKBEFORE = BitShift($PFM_PAGEBREAKBEFORE, 16)
Global Const $PFE_NOLINENUMBER = BitShift($PFM_NOLINENUMBER, 16)
Global Const $PFE_NOWIDOWCONTROL = BitShift($PFM_NOWIDOWCONTROL, 16)
Global Const $PFE_DONOTHYPHEN = BitShift($PFM_DONOTHYPHEN, 16)
Global Const $PFE_SIDEBYSIDE = BitShift($PFM_SIDEBYSIDE, 16)
; end of source sourceforge
Global Const $PFM_SHADING = 0x1000
Global Const $WB_CLASSIFY = 3
Global Const $WB_ISDELIMITER = 2
Global Const $WB_LEFT = 0
Global Const $WB_LEFTBREAK = 6
Global Const $WB_MOVEWORDLEFT = 4
Global Const $WB_MOVEWORDNEXT = 5
Global Const $WB_MOVEWORDPREV = 4
Global Const $WB_MOVEWORDRIGHT = 5
Global Const $WB_NEXTBREAK = 7
Global Const $WB_PREVBREAK = 6
Global Const $WB_RIGHT = 1
Global Const $WB_RIGHTBREAK = 7
Global Const $WBF_ISWHITE = 0x10
Global Const $WBF_BREAKLINE = 0x20
Global Const $WBF_BREAKAFTER = 0x40
; For Stream Callbacks :)
Global Const $SF_TEXT = 0x1
Global Const $SF_RTF = 0x2
Global Const $SF_RTFNOOBJS = 0x3
Global Const $SF_TEXTIZED = 0x4
Global Const $SF_UNICODE = 0x0010
Global Const $SF_USECODEPAGE = 0x20
Global Const $SFF_PLAINRTF = 0x4000
Global Const $SFF_SELECTION = 0x8000
;--------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------
;---------------- DLL Structures ------------------------------------------------------------------
Global Const $tagEDITSTREAM = "long_PTR dwCookie; DWORD dwError; ptr pfnCallback"
;~ Global Const $tagNMHDR = "int;int;int"
;~ HWND hwndFrom;
;~ UINT idFrom;
;~ UINT code;
;~ Global Const $tagRECT = "int;int;int;int"
Global Const $tagBIDIOPTIONS = "uint;int;int"
;~ UINT cbSize;
;~ WORD wMask;
;~ WORD wEffects
Global Const $tagCHARFORMAT = "uint;dword;dword;int;int;int;byte;byte;char[" & $LF_FACESIZE & "]"
;~ UINT cbSize;
;~ DWORD dwMask;
;~ DWORD dwEffects;
;~ LONG yHeight;
;~ LONG yOffset;
;~ COLORREF crCharColor;
;~ BYTE bCharSet;
;~ BYTE bPitchAndFamily;
;~ TCHAR szFaceName[LF_FACESIZE];
;~ Global Const $tagCHARFORMAT2 = "uint;dword;dword;int;int;int;byte;byte;char[" & $LF_FACESIZE & "];int;short;int;byte;byte;byte;byte"
Global Const $tagCHARFORMAT2 = "UINT cbSize;DWORD dwMask;DWORD dwEffects;LONG yHeight;LONG yOffset;long crCharColor;BYTE bCharSet;BYTE bPitchAndFamily;" & _
		"CHAR szFaceName[" & $LF_FACESIZE & "];ushort wWeight;SHORT sSpacing;long crBackColor;dword lcid;DWORD dwReserved;SHORT sStyle;ushort wKerning;BYTE bUnderlineType;BYTE bAnimation;BYTE bRevAuthor;BYTE bReserved1"
;~ UINT cbSize;
;~ DWORD dwMask;
;~ DWORD dwEffects;
;~ LONG yHeight;
;~ LONG yOffset;
;~ COLORREF crCharColor;
;~ BYTE bCharSet;
;~ BYTE bPitchAndFamily;
;~ TCHAR szFaceName[LF_FACESIZE];
;~ WORD wWeight;
;~ SHORT sSpacing;
;~ COLORREF crBackColor;
;~ LCID lcid;
;~ DWORD dwReserved;
;~ SHORT sStyle;
;~ WORD wKerning;
;~ BYTE bUnderlineType;
;~ BYTE bAnimation;
;~ BYTE bRevAuthor;
;~ BYTE bReserved1;
Global Const $tagCHARRANGE = "int cpMin;int cpMax"
;~ LONG cpMin;
;~ LONG cpMax;
Global Const $tagCOMPCOLOR = "int;int;dword"
;~ COLORREF crText;
;~ COLORREF crBackground;
;~ DWORD dwEffects
;~ editstream {
;~     DWORD_PTR dwCookie;
;~     DWORD dwError;
;~     EDITSTREAMCALLBACK pfnCallback
Global Const $tagENCORRECTTEXT = $tagNMHDR & ";" & $tagCHARRANGE & ";int"
;~ NMHDR nmhdr;
;~ CHARRANGE chrg;
;~ WORD seltyp;
Global Const $tagENDROPFILES = $tagNMHDR & ";int;int;int"
;~ NMHDR nmhdr;
;~ HANDLE hDrop;
;~ LONG cp;
;~ BOOL fProtected
Global Const $tagENLINK = $tagNMHDR & ";uint msg;int wParam;int lParam;" & $tagCHARRANGE
;~ NMHDR nmhdr;
;~ UINT msg;
;~ WPARAM wParam;
;~ LPARAM lParam;
;~ CHARRANGE chrg
Global Const $tagENLOWFIRTF = $tagNMHDR & ";ptr"
;~ NMHDR nmhdr;
;~ CHAR *szControl
Global Const $tagENOLEOPFAILED = $tagNMHDR & ";int;int;int"
;~ NMHDR nmhdr;
;~ LONG iob;
;~ LONG lOper;
;~ HRESULT hr;
Global Const $tagENPROTECTED = $tagNMHDR & ";uint msg;int wParam;int lParam;" & $tagCHARRANGE
;~ NMHDR nmhdr;
;~ UINT msg;
;~ WPARAM wParam;
;~ LPARAM lParam;
;~ CHARRANGE chrg
Global Const $tagENSAVECLIPBOARD = $tagNMHDR & ";int;int"
;~ NMHDR nmhdr;
;~ LONG cObjectCount;
;~ LONG cch;
Global Const $tagFINDTEXT = $tagCHARRANGE & ";ptr lpstrText"
;~ Global Const $tagFINDTEXT = $tagCHARRANGE & ";char[128]"
;~ CHARRANGE chrg;
;~ LPCTSTR lpstrText;
Global Const $tagFINDTEXTEX = $tagCHARRANGE & ";ptr lpstrText;" & $tagCHARRANGE
;~ CHARRANGE chrg;
;~ LPCTSTR lpstrText;
;~ CHARRANGE chrgText
Global Const $tagFORMATRANGE = "int;int;" & $tagRECT & ";" & $tagRECT & ";" & $tagCHARRANGE
;~ HDC hdc;
;~ HDC hdcTarget;
;~ RECT rc;
;~ RECT rcPage;
;~ CHARRANGE chrg
Global Const $tagGETTEXTEX = "dword;dword;uint;char;int"
;~ DWORD cb;
;~ DWORD flags;
;~ UINT codepage;
;~ LPCSTR lpDefaultChar;
;~ LPBOOL lpUsedDefChar
Global Const $tagGETTEXTLENGTHEX = "dword;uint"
;~ DWORD flags;
;~ UINT codepage;
;~ tagHyphenateInfo {
;~     SHORT cbSize;
;~     SHORT dxHyphenateZone;
;~     PFNHYPHENATEPROC pfnHyphenate
Global Const $tagKHYPH = "int;int;int;int;int;int;int"
;~ khyphNil,
;~ khyphNormal,
;~ khyphAddBefore,
;~ khyphChangeBefore,
;~ khyphDeleteBefore,
;~ khyphChangeAfter,
;~ khyphDelAndChange
Global Const $tagHYPHRESULT = $tagKHYPH & ";int;char"
;~ KHYPH khyph;
;~ LONG ichHyph;
;~ WCHAR chHyph
Global Const $tagIMECOMPTEXT = "int;dword"
;~ LONG cb;
;~ DWORD flags;
Global Const $tagEN_MSGFILTER = $tagNMHDR & ";uint msg;int wParam;int lParam"
;~ NMHDR nmhdr;
;~ UINT msg;
;~ WPARAM wParam;
;~ LPARAM lParam
Global Const $tagOBJECTPOSITIONS = $tagNMHDR & ";int;int"
;~ NMHDR nmhdr;
;~ LONG cObjectCount;
;~ LONG *pcpPositions
Global Const $tagPARAFORMAT = "uint cbSize;uint dwMask;short wNumbering;short wEffects;int dxStartIndent;" _
		 & "int dxRightIndent;int dxOffset;short wAlignment;short cTabCount;int rgxTabs[" & $MAX_TAB_STOPS & "]"
;~ UINT cbSize;
;~ DWORD dwMask;
;~ WORD wNumbering;
;~ WORD wReserved;
;~ LONG dxStartIndent;
;~ LONG dxRightIndent;
;~ LONG dxOffset;
;~ WORD wAlignment;
;~ SHORT cTabCount;
;~ LONG rgxTabs[MAX_TAB_STOPS];
Global Const $tagPARAFORMAT2 = "uint cbSize;uint dwMask;short wNumbering;short wEffects;int dxStartIndent;" _
		 & "int dxRightIndent;int dxOffset;short wAlignment;short cTabCount;int rgxTabs[" & $MAX_TAB_STOPS & "];" _
		 & "int dySpaceBefore;int dySpaceAfter;int dyLineSpacing;ushort sStyle;byte bLineSpacingRule;" _
		 & "byte bOutlineLevel;short wShadingWeight;short wShadingStyle;short wNumberingStart;short wNumberingStyle;" _
		 & "short wNumberingTab;short wBorderSpace;short wBorderWidth;short wBorders"
;~ UINT cbSize;
;~ DWORD dwMask;
;~ WORD  wNumbering;
;~ WORD  wEffects;
;~ LONG  dxStartIndent;
;~ LONG  dxRightIndent;
;~ LONG  dxOffset;
;~ WORD  wAlignment;
;~ SHORT cTabCount;
;~ LONG  rgxTabs[MAX_TAB_STOPS];
;~ LONG  dySpaceBefore;
;~ LONG  dySpaceAfter;
;~ LONG  dyLineSpacing;
;~ SHORT sStyle;
;~ BYTE  bLineSpacingRule;
;~ BYTE  bOutlineLevel;
;~ WORD  wShadingWeight;
;~ WORD  wShadingStyle;
;~ WORD  wNumberingStart;
;~ WORD  wNumberingStyle;
;~ WORD  wNumberingTab;
;~ WORD  wBorderSpace;
;~ WORD  wBorderWidth;
;~ WORD  wBorders;
Global Const $tagPUNCTUATION = "uint;ptr"
;~ UINT iSize;
;~ LPSTR szPunctuation
;~ Global $reobject_fmt = "dword;int;int; {
;~     DWORD cbStruct;
;~     LONG cp;
;~     CLSID clsid;
;~     LPOLEOBJECT poleobj;
;~     LPSTORAGE pstg;
;~     LPOLECLIENTSITE polesite;
;~     SIZEL sizel;
;~     DWORD dvaspect;
;~     DWORD dwFlags;
;~     DWORD dwUser
Global Const $tagREPASTESPECIAL = "dword;dword"
;~ DWORD dwAspect;
;~ DWORD_PTR dwParam
Global Const $tagREQRESIZE = $tagNMHDR & ";" & $tagRECT
;~ NMHDR nmhdr;
;~ RECT rc;
Global Const $tagSELCHANGE = $tagNMHDR & ";" & $tagCHARRANGE & ";long seltyp"
;~ NMHDR nmhdr;
;~ CHARRANGE chrg;
;~ WORD seltyp;
Global Const $tagSETTEXTEX = "dword;uint"
;~ DWORD flags;
;~ UINT codepage
Global Const $tagTEXTRANGE = $tagCHARRANGE & ";ptr"
;~ CHARRANGE chrg;
;~ LPSTR lpstrText
;~ Global Const $tagLOGFONT = "int;int;int;int;int;byte;byte;byte;byte;byte;byte;byte;byte;char[" & $LF_FACESIZE & "]"
;~ LONG lfHeight;
;~ LONG lfWidth;
;~ LONG lfEscapement;
;~ LONG lfOrientation;
;~ LONG lfWeight;
;~ BYTE lfItalic;
;~ BYTE lfUnderline;
;~ BYTE lfStrikeOut;
;~ BYTE lfCharSet;
;~ BYTE lfOutPrecision;
;~ BYTE lfClipPrecision;
;~ BYTE lfQuality;
;~ BYTE lfPitchAndFamily;
;~ TCHAR lfFaceName[LF_FACESIZE];
;--------------------------------------------------------------------------------------------------

Global $_GRC_StreamFromFileCallback = DllCallbackRegister("__GCR_StreamFromFileCallback", "dword", "long_ptr;ptr;long;ptr")
Global $_GRC_StreamFromVarCallback = DllCallbackRegister("__GCR_StreamFromVarCallback", "dword", "long_ptr;ptr;long;ptr")
Global $_GRC_StreamToFileCallback = DllCallbackRegister("__GCR_StreamToFileCallback", "dword", "long_ptr;ptr;long;ptr")
Global $_GRC_StreamToVarCallback = DllCallbackRegister("__GCR_StreamToVarCallback", "dword", "long_ptr;ptr;long;ptr")
Global $_GRC_tEditStream = DllStructCreate($tagEDITSTREAM)
Global $_GRC_sStreamVar
Global $_ghRichEditLastWnd

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_StreamFromFileCallback
; Description ...: Callback function for streaming in from a file
; Syntax.........: __GCR_StreamFromFileCallback($hFile, $pBuf, $iBuflen, $ptrQbytes)
; Parameters ....: $hFile - Handle to the file
;                  $pBuf - pointer to a buffer in the control
;                  $iBuflen - length of this buffer
;                  $ptrQbytes - pointer to number of bytes set in buffer
; Return values .: More bytes to "return"  - 0
;                  All bytes have been "returned" - 1
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........; @@MsdnLink@@ EditStreamCallback Function
; Example .......; No
;
; =================================================================================================
Func __GCR_StreamFromFileCallback($hFile, $pBuf, $iBuflen, $ptrQbytes)
	Local $tQbytes, $tBuf, $buf
	$tQbytes = DllStructCreate("long", $ptrQbytes)
	DllStructSetData($tQbytes, 1, 0)
	;	If @AutoItUnicode Then
	;		$tBuf = DllStructCreate("wchar[" & ($iBuflen+1) & "]", $pBuf)
	;  Else
	$tBuf = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
	;  EndIf
	$buf = FileRead($hFile, $iBuflen - 1)
	If @error <> 0 Then Return 1
	DllStructSetData($tBuf, 1, $buf)
	DllStructSetData($tQbytes, 1, StringLen($buf))
	Return 0
EndFunc   ;==>__GCR_StreamFromFileCallback

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_StreamFromVarCallback
; Description ...: Callback function for streaming in from a variable
; Syntax.........: __GCR_StreamFromVarCallback($dwCookie, $pBuf, $iBufLen, $ptrQbytes)
; Parameters ....: $dwCookie - not used
;                  $pBuf - pointer to a buffer in the control
;                  $iBuflen - length of this buffer
;                  $ptrQbytes - pointer to number of bytes set in buffer
; Return values .: More bytes to "return"  - 0
;                  All bytes have been "returned" - 1
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; @@MsdnLink@@ EditStreamCallback Function
; Example .......; No
;
; =================================================================================================
Func __GCR_StreamFromVarCallback($dwCookie, $pBuf, $iBuflen, $ptrQbytes)
	Local $tQbytes, $tCtl, $sCtl, $iLen

	$dwCookie = $dwCookie ; to satisfy AutoItWrapper
	$tQbytes = DllStructCreate("long", $ptrQbytes)
	DllStructSetData($tQbytes, 1, 0)

	$tCtl = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
	$sCtl = StringLeft($_GRC_sStreamVar, $iBuflen - 1)
	If $sCtl = "" Then Return 1
	DllStructSetData($tCtl, 1, $sCtl)

	$iLen = StringLen($sCtl)
	DllStructSetData($tQbytes, 1, $iLen)
	$_GRC_sStreamVar = StringMid($_GRC_sStreamVar, $iLen + 1)
	Return 0
EndFunc   ;==>__GCR_StreamFromVarCallback

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_StreamFToFileCallback
; Description ...: Callback function for streaming out to a file
; Syntax.........: __GCR_StreamToFileCallback($hFile, $pBuf, $iBuflen, $ptrQbytes
; Parameters ....: $hFile - Handle to the file
;                  $pBuf - pointer to a buffer in the control
;                  $iBuflen - length of this buffer
;                  $ptrQbytes - pointer to number of bytes set in buffer
; Return values .: 0
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........; @@MsdnLink@@ EditStreamCallback Function
; Example .......; No
;
; =================================================================================================

Func __GCR_StreamToFileCallback($hFile, $pBuf, $iBuflen, $ptrQbytes)
	Local $tQbytes, $tBuf, $s
	$tQbytes = DllStructCreate("long", $ptrQbytes)
	DllStructSetData($tQbytes, 1, 0)
	$tBuf = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
	$s = DllStructGetData($tBuf, 1)
	FileWrite($hFile, $s)
	DllStructSetData($tQbytes, 1, StringLen($s))
	Return 0
EndFunc   ;==>__GCR_StreamToFileCallback

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_StreamFToVarCallback
; Description ...: Callback function for streaming out to a variable
; Syntax.........: __GCR_StreamToVarCallback($dwCookie, $pBuf, $iBufLen, $ptrQbytes)
; Parameters ....: $dwCookie - not used
;                  $pBuf - pointer to a buffer in the control
;                  $iBuflen - length of this buffer
;                  $ptrQbytes - pointer to number of bytes set in buffer
; Return values .: 0
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; @@MsdnLink@@ EditStreamCallback Function
; Example .......; No
;
; =================================================================================================
Func __GCR_StreamToVarCallback($dwCookie, $pBuf, $iBuflen, $ptrQbytes)
	Local $tQbytes, $tBuf, $s
	$dwCookie = $dwCookie ; to satisfy AutoItWrapper
	$tQbytes = DllStructCreate("long", $ptrQbytes)
	DllStructSetData($tQbytes, 1, 0)
	;	If @AutoItUnicode Then
	;		$tBuf = DllStructCreate("wchar[" & ($iBuflen+1) & "]", $pBuf)
	;  Else
	$tBuf = DllStructCreate("char[" & $iBuflen & "]", $pBuf)
	;  EndIf
	$s = DllStructGetData($tBuf, 1)
	$_GRC_sStreamVar &= $s
	Return 0
EndFunc   ;==>__GCR_StreamToVarCallback

;------------------------------------------------------------------------------------------
; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_AppendText
; Description....: Appends text at the end of the client area
; Syntax ........: _GUICtrlRichEdit_AppendText($hWnd, $sText)
; Parameters.....: hWnd		- Handle to the control
;                  $sText    - Text to be appended
; Return values..: Succcess - True
;                  |Failure - False, and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Gary Frost (gafrost (custompcs@charter.net))
; Modified ......: Prog@ndy, Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_InsertText, _GUICtrlRichEdit_ReplaceSel, _GuiCtrlRichEdit_SetText
; Link ..........: @@MsdnLink@@ EM_SETTEXTEX
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_AppendText($hWnd, $sText)
	Local $iLength, $tSetText
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$iLength = _GUICtrlRichEdit_GetCtrlTextLength($hWnd)
	_GUICtrlRichEdit_SetSel($hWnd, $iLength, $iLength) ; go to end of text
	$tSetText = DllStructCreate($tagSETTEXTEX)
	DllStructSetData($tSetText, 1, $ST_SELECTION)
	If StringLeft($sText, 5) <> "{\rtf" And StringLeft($sText, 5) <> "{urtf" Then
		DllStructSetData($tSetText, 2, $CP_UNICODE)
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "wstr")
	Else
		DllStructSetData($tSetText, 2, $CP_ACP)
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "str")
	EndIf
	If @error Then Return SetError(@error, 0, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_AppendText

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_AutoDetectURL
; Description....: Enables or disables automatic detection of URLS
; Syntax ........: _GUICtrlRichEdit_AutoDetectURL($hWnd, $fState)
; Parameters.....: hWnd		- Handle to the control
;                  $fState - True to detect URLs in text, False not to
; Return values..: Succcess - True
;                  |Failure - False, and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fState is neither True nor False
;                  |700 - internal error, e.g. insufficient memory
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If enabled, any modified text is scanned for text that matches the format of a URL. It recognizes strings
;                  starting with the following as URLs: http:, file:, mailto:, ftp:, https:, gopher:, nntp:, prospero:,
;                  telnet:, news:, wais:. When a URL is detected, Windows sets the link attribute for all characters in the
;                  URL string, and highlights the string.
;+
;                  When automatic URL detection is on and a URL is detected, Windows removes the link attribute of all
;                  characters that are not URLs.
;+
;                  For notification to happen, call _GuiCtrlRichEdit_SetEventMask with $ENM_LINK
; Related .......: _GuiCtrlRichEdit_SetEventMask
; Link ..........: @@MsdnLink@@ EM_AUTOURLDETECT, @@MsdnLink@@ EN_LINK notification
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_AutoDetectURL($hWnd, $fState)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fState) Then Return SetError(102, 0, False)

	If __GCR_SendMessage($hWnd, $EM_AUTOURLDETECT, $fState) = 0 Then
		Return True
	Else
		Return SetError(700, 0, False)
	EndIf
EndFunc   ;==>_GUICtrlRichEdit_AutoDetectURL

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_CanPaste
; Description....: Can the contents of the clipboard be pasted into the control?
; Syntax ........: _GUICtrlRichEdit_CanPaste($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Succcess - True or False
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: Data in two clipboard formats can be pasted: RTF and RTF with Objects.
;                  This function determines whether data in either format is on the clipboard.
; Related .......: _GuiCtrlRichEdit_Paste
; Link ..........: @@MsdnLink@@ EM_CANPASTE
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_CanPaste($hWnd)
	Local $iResult
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$iResult = __GCR_SendMessage($hWnd, $EM_CANPASTE, 0, 0)
	If @error Then Return SetError(@error, 0, 0)
	Return $iResult <> 0
EndFunc   ;==>_GUICtrlRichEdit_CanPaste


; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_CanPasteSpecial
; Description....: Can the contents of the clipboard be pasted into the control in both formats?
; Syntax ........: _GUICtrlRichEdit_CanPasteSpecial($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Succcess - True or False
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Data in two clipboard formats can be pasted: RTF and RTF with Objects.
;                  This function determines whether data in both formats is on the clipboard.
; Related .......: _GuiCtrlRichEdit_PasteSpecial
; Link ..........: @@MsdnLink@@ EM_CANPASTE
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_CanPasteSpecial($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_CANPASTE, $_GRE_CF_RTF, 0) <> 0 _
			And __GCR_SendMessage($hWnd, $EM_CANPASTE, $_GRE_CF_RETEXTOBJ, 0) <> 0
EndFunc   ;==>_GuiCtrlRichEdit_CanPasteSpecial

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_CanRedo
; Description....: Can an undone action be redone?
; Syntax ........: _GUICtrlRichEdit_CanRedo($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Succcess - True or False
;                  |Failure - False and sets @error
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GuiCtrlRichEdit_Redo
; Link ..........: @@MsdnLink@@ EM_CANREDO
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_CanRedo($hWnd)
	Local $iResult
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$iResult = __GCR_SendMessage($hWnd, $EM_CANREDO, 0, 0)
	If @error Then Return SetError(@error, 0, 0)
	Return $iResult <> 0
EndFunc   ;==>_GUICtrlRichEdit_CanRedo

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_CanUndo
; Description....: Can an action be undone?
; Syntax ........: _GUICtrlRichEdit_CanUndo($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Succcess - True - there are action(s) in the undo queue
;                  Failure - False and may set @error
;                  |0 - there are no actions in the undo queue
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GuiCtrlRichEdit_Undo
; Link ..........: @@MsdnLink@@ EM_CANUNDO
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_CanUndo($hWnd)
	Local $iResult
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$iResult = __GCR_SendMessage($hWnd, $EM_CANUNDO, 0, 0)
	If @error Then Return SetError(@error, 0, 0)
	Return $iResult <> 0
EndFunc   ;==>_GUICtrlRichEdit_CanUndo

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_ChangeFontSize
; Description....: Increment or decrement font size(s) of selected text
; Syntax ........: _GuiCtrlRichEdit_ChangeFontSize($hWnd, $iIncrement)
; Parameters.....: $hWnd		 - Handle to the control
;                  $iIncrement - Positive to increase, negative to decrease
; Return values..: Succcess - True - Font sizes were changed
;                  Failure - False and may set @error
;                  |101  - $hWnd is not a handle
;                  |102  - $iIncrement is not a number
;                  |-1 - no text selected
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: If there are several font sizes in the selected text, all are incremented/decremented
;+
;                  For $iIncrement positive, font sizes are rounded up; for $iIncrement negative, they are rounded down.
;+
;                  Rich Edit first adds $iIncrement to the existing font size. It then rounds up (or down) as follows:
;                  <= 12 points: 1 e.g. 7 + 1 => 8 points, 14 - 3 => 10 points
;                  12.05 to 28 points: 20 + 2.25 => 24 points
;                  28.05 to 80 points: rounded to next of 28, 36, 48, 72 or 80, e.g. 28 + 1 => 36 points, 80 - 1 => 72 points
;                  > 80 points: 10, e.g. 80 + 1 => 90
; Related .......: _GuiCtrlRichEdit_SetFont
; Link ..........: @@MsdnLink@@ EM_SETFONTSIZE
; Example .......: Yes
;
;===============================================================================

Func _GuiCtrlRichEdit_ChangeFontSize($hWnd, $iIncrement)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iIncrement) Then SetError(1021, 0, False)

	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then Return SetError(-1, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_SETFONTSIZE, $iIncrement, 0)
EndFunc   ;==>_GuiCtrlRichEdit_ChangeFontSize

;#FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_Copy
; Description....: Copy text to clipboard
; Syntax ........: _GUICtrlRichEdit_Copy($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_CanCopy,_GuiCtrlRichEdit__Cut, _GuiCtrlRichEdit_Paste
; Link ..........: @@MsdnLink@@ WM_COPY
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_Copy($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $WM_COPY, 0, 0)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_Copy

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_Create
; Description ...: Create a Edit control
; Syntax.........: _GUICtrlEdit_Create($hWnd, $sText, $iX, $iY, $iWidth = 150[, $iHeight = 150[, $iStyle = -1[, $iExStyle = -1]]])
; Parameters ....: $hWnd        - Handle to parent or owner window
;                  $sText       - Text to be displayed in the control
;                  $iLeft       - Horizontal position of the control
;                  $iTop        - Vertical position of the control
;                  $iWidth      - Control width
;                  $iHeight     - Control height
;                  $iStyle      - Control styles:
;                  |$ES_AUTOHSCROLL - Automatically scrolls text to the right by 10 characters when the user types a character at the end of the line.
;                  |$ES_AUTOVSCROLL - Automatically scrolls text up one page when the user presses the ENTER key on the last line.
;                  |$WS_HSCROLL     - Control has horizontal scroll bar
;                  |$WS_VSCROLL     - Control has vertical scroll bar
;                  |$ES_CENTER      - Centers text in a edit control.
;                  |$ES_LEFT        - Aligns text with the left margin.
;                  |$ES_MULTILINE   - Generates a multi-line control (Default)
;                  |$ES_NOHIDESEL   - The selected text is inverted, even if the control does not have the focus.
;                  |$ES_NUMBER      - Allows only digits to be entered into the edit control.
;                  |$ES_READONLY    - Prevents the user from typing or editing text in the edit control.
;                  |$ES_RIGHT       - Right-aligns text edit control.
;                  |$ES_WANTRETURN  - Specifies that a carriage return be inserted when the user presses the ENTER key. (Default)
;                  |$ES_PASSWORD    - Displays an asterisk (*) for each character that is typed into the edit control
;                  -
;                  |Default: 0
;                  |Forced : WS_CHILD, $WS_VISIBLE, $WS_TABSTOP unless $ES_READONLY
;                  $iExStyle    - Control extended style. These correspond to the standard $WS_EX_ constants.
; Return values .: Success      - Handle to the Rich Edit control
;                  Failure      - False and sets @error:
;                  |103 - $iLeft is neither a positive number nor zero
;                  |104 - $iTop is is neither a positive number nor zero
;                  |105 - $iWidth is neither a positive number nor -1
;                  |106 - $iHeight is neither a positive number nor -1
;                  |107 - $iStyle is is neither a positive number nor zero nor -1
;                  |108 - $iExStyle is is neither a positive number nor zero nor -1
; Author ........: Gary Frost
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_Destroy
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_Create($hWnd, $sText, $iLeft, $iTop, $iWidth = 150, $iHeight = 150, $iStyle = -1, $iExStyle = -1)
	Local $hRichEdit, $nCtrlID

	If Not IsHWnd($hWnd) Then _WinAPI_ShowError("Invalid Window handle for _GUICtrlRichEdit_Create 1st parameter")
	If Not IsString($sText) Then _WinAPI_ShowError("2nd parameter not a string for _GUICtrlRichEdit_Create")
	If Not __GCR_IsNumeric($iLeft, ">=0") Then Return SetError(103, 0, False)
	If Not __GCR_IsNumeric($iTop, ">=0") Then Return SetError(104, 0, False)
	If Not __GCR_IsNumeric($iWidth, ">0,-1") Then Return SetError(105, 0, False)
	If Not __GCR_IsNumeric($iHeight, ">0,-1") Then Return SetError(106, 0, False)
	If Not __GCR_IsNumeric($iStyle, ">=0,-1") Then Return SetError(107, 0, False)
	If Not __GCR_IsNumeric($iExStyle, ">=0,-1") Then Return SetError(108, 0, False)

	If $iWidth = -1 Then $iWidth = 150
	If $iHeight = -1 Then $iHeight = 150
	If $iStyle = -1 Then $iStyle = BitOR($ES_WANTRETURN, $ES_MULTILINE)

	If BitAND($iStyle, $ES_MULTILINE) <> 0 Then $iStyle = BitOR($iStyle, $ES_WANTRETURN)
	If $iExStyle = -1 Then $iExStyle = 0x200 ;	$DS_FOREGROUND

	$iStyle = BitOR($iStyle, $WS_CHILD, $WS_VISIBLE)
	If BitAND($iStyle, $ES_READONLY) = 0 Then $iStyle = BitOR($iStyle, $WS_TABSTOP)
	;=========================================================================================================

	$nCtrlID = _UDF_GetNextGlobalID($hWnd)
	If @error Then Return SetError(@error, @extended, 0)

	__GCR_Init()

	$hRichEdit = _WinAPI_CreateWindowEx($iExStyle, $_GRE_sRTFClassName, "", $iStyle, $iLeft, $iTop, $iWidth, _
			$iHeight, $hWnd, $nCtrlID)
	If $hRichEdit = 0 Then Return SetError(700, 0, False)

	__GCR_SetOLECallback($hRichEdit)
	__GCR_SendMessage($hRichEdit, $WM_SETFONT, _WinAPI_GetStockObject($DEFAULT_GUI_FONT), True)
	;	_GuiCtrlRichEdit_SetFont($hRichEdit,20,"Arial")
	;	_GuiCtrlRichEdit_SetCharAttributes($hRichEdit,"-bo")
	_GUICtrlRichEdit_AppendText($hRichEdit, $sText)
	Return $hRichEdit
EndFunc   ;==>_GUICtrlRichEdit_Create

;#FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_Cut
; Description....: Cut text to clipboard
; Syntax ........: _GUICtrlRichEdit_Cut($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_CanCopy,_GuiCtrlRichEdit__Copy, _GuiCtrlRichEdit_Paste
; Link ..........: @@MsdnLink@@ WM_CUT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_Cut($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $WM_CUT, 0, 0)

	Return True
EndFunc   ;==>_GUICtrlRichEdit_Cut

;#FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_Deselect
; Description....: Deselects text, leaving none selected
; Syntax ........: _GUICtrlRichEdit_Cut($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Leaves the insertion point at the anchor point of the selection
; Related .......: _GuiCtrlRichEdit_SetSel, _GuiCtrlRichEdit_GetSel, _GuiCtrlRichEdit_IsTextSelected
; Link ..........: @@MsdnLink@@ EM_SETSEL
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_Deselect($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $EM_SETSEL, -1, 0)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_Deselect

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_Destroy
; Description ...: Delete the Rich Edit control
; Syntax.........: _GUICtrlRichEdit_Destroy(ByRef $hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - True, Handle is set to 0
;                  Failure      - False and sets @error:
;                  |1 - attempt to destroy a control belonging to another application
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: Only be used on Rich Edit controls created with _GUICtrlEdit_Create
; Related .......: _GUICtrlEdit_Create
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_Destroy(ByRef $hWnd)
	Local $Destroyed, $iResult

	If _WinAPI_IsClassName($hWnd, $_GRE_sRTFClassName) Then
		If IsHWnd($hWnd) Then
			If _WinAPI_InProcess($hWnd, $_ghRichEditLastWnd) Then
				Local $nCtrlID = _WinAPI_GetDlgCtrlID($hWnd)
				Local $hParent = _WinAPI_GetParent($hWnd)
				$Destroyed = _WinAPI_DestroyWindow($hWnd)
				$iResult = _UDF_FreeGlobalID($hParent, $nCtrlID)
				If Not $iResult Then
					; can check for errors here if needed, for debug
				EndIf
			Else
				_WinAPI_ShowMsg("Not Allowed to Destroy Other Applications Control(s)")
				Return SetError(1, 1, False)
			EndIf
		Else
			$Destroyed = GUICtrlDelete($hWnd)
		EndIf
		If $Destroyed Then $hWnd = 0
		Return $Destroyed <> 0
	EndIf
	Return SetError(2, 2, False)
EndFunc   ;==>_GUICtrlRichEdit_Destroy

; #FUNCTION# ====================================================================================================================
; Name...........: _GuiCtrlRichEdit_EmptyUndoBuffer
; Description ...: Resets the undo flag of the control
; Syntax.........: _GuiCtrlRichEdit_EmptyUndoBuffer($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - True, Handle is set to 0
;                  Failure      - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......: _GUICtrlEdit_Undo, _GuiCtrlRichEdit_Redo
; Link ..........; @@MsdnLink@@ EM_SETEMPTYUNDOBUFFER
; Example .......; Yes
; ===============================================================================================================================
Func _GuiCtrlRichEdit_EmptyUndoBuffer($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $EM_EMPTYUNDOBUFFER, 0, 0)
EndFunc   ;==>_GuiCtrlRichEdit_EmptyUndoBuffer


; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_FindText
; Description ...: Search for a text starting at insertion point or at anchor point of selection
; Syntax ........: _GUICtrlRichEdit_FindText($hWnd, $sText[, $fForward = True[, $fMatchCase = False[, $fWholeWord = False[, $iBehavior = 0]]]])
; Parameters ....: $hWnd        - Handle to the control
;                  $s_Text      - Text to find
;                  $fForward    - Search direction (Optional)
;                  !Default: forward
;                  |(Win 95: search is always forward)
;                  $fMatchCase  - Search is case-sensitive (Optional)
;                  |Default: case-insensitive
;                  $fWholeWord  - Search only for text as a whole word (Optional)
;                  |Default: partial or full word
;                  $behavior    - Any BitOr combination of $FR_MATCHALEFHAMZA, $FR_MATCHDIAC and $FR_MATCHKASHIDA
;                  |Default: 0
; Return Values. : Success - If found, inter-character position before start of matching text, else -1
;						 |Failure - -1 and sets @error:
;                  |101  - $hWnd is not a handle
;                  |102  - $sText = ""
;                  |103  - $fForward is neither True nor False
;                  |104  - $fMatchCase is neither True nor False
;                  |105  - $fWholeWord is neither True nor False
;                  |1061 - $iBehavior is not a number
;                  |1062 - $iBehavior is invalid
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_FindTextInRange
; Link ..........: @@MsdnLink@@ EM_FINDTEXT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_FindText($hWnd, $sText, $fForward = True, $fMatchCase = False, $fWholeWord = False, _
		$iBehavior = 0)
	Local $iLen, $tText, $tFindtext, $iWparam, $iResult, $aiAnchorActive
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, -1)
	If $sText = "" Then Return SetError(102, 0, -1)
	If Not IsBool($fForward) Then Return SetError(103, 0, -1)
	If Not IsBool($fMatchCase) Then Return SetError(104, 0, -1)
	If Not IsBool($fWholeWord) Then Return SetError(105, 0, -1)
	If Not __GCR_IsNumeric($iBehavior) Then Return SetError(1061, 0, -1)
	If BitAND($iBehavior, BitNOT(BitOR($FR_MATCHALEFHAMZA, $FR_MATCHDIAC, $FR_MATCHKASHIDA))) <> 0 Then _
			Return SetError(1062, 0, -1)

	$iLen = StringLen($sText) + 3
	$tText = DllStructCreate("wchar[" & $iLen & "]")
	DllStructSetData($tText, 1, $sText)
	$tFindtext = DllStructCreate($tagFINDTEXT)
	$aiAnchorActive = StringSplit(_GuiCtrlRichEdit_GetSelAA($hWnd), ";")
	DllStructSetData($tFindtext, 1, $aiAnchorActive[1])
	DllStructSetData($tFindtext, 2, _Iif($fForward, -1, 0)) ; to end else to start
	DllStructSetData($tFindtext, 3, DllStructGetPtr($tText))
	$iWparam = 0
	If $fForward Then $iWparam = $FR_DOWN
	If $fWholeWord Then $iWparam = BitOR($iWparam, $FR_WHOLEWORD)
	If $fMatchCase Then $iWparam = BitOR($iWparam, $FR_MATCHCASE)
	$iWparam = BitOR($iWparam, $iBehavior)
	;	If @AutoItUnicode Then
	$iResult = __GCR_SendMessage($hWnd, $EM_FINDTEXTW, $iWparam, DllStructGetPtr($tFindtext), "wparam", "ptr")
	;	Else
	;		$iResult = __GCR_SendMessage($hWnd, $EM_FINDTEXT, $iWparam, DllStructGetPtr($tFindtext), "wparam", "ptr")
	;	EndIf
	If @error Then Return SetError(@error, 0, -1)
	Return $iResult
EndFunc   ;==>_GUICtrlRichEdit_FindText

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_FindTextInRange
; Description ...: Search for a text in a range of inter-character positions
; Syntax ........: _GUICtrlRichEdit_FindTextInRange($hWnd, $sText[, $iStart = 0[, $iEnd = -1[, $fMatchCase = False[, $fwholeWord = False[, $iBehavior = 0]]]]])
; Parameters ....: $hWnd        - Handle to the control
;                  $s_Text      - Text to find
;                  $iStart      - Starting inter-character position of search (Optional)
;                  |Default: beginning of control
;                  $iEnd        - Ending inter-character position of search (Optional)
;                  |Default: end of control
;                  $fMatchCase  - Search is case-sensitive (Optional)
;                  |Default: case-insensitive
;                  $fWholeWord  - Search only for text as a whole word (Optional)
;                  |Default: partial or full word
;                  $behavior    - Any BitOr combination of $FR_MATCHALEFHAMZA, $FR_MATCHDIAC and $FR_MATCHKASHIDA
;                  |Default: 0
; Return Values. : Success - a string containing values separated by a semicolon (;)
;                  |If target string found, range of inter-character positions containing the matching text, e.g. "45;52"
;                  If not found, "-1;-1"
;						 Failure - False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |102  - $sText = ""
;                  |103  - $iStart is neither a positive number nor zero nor -1
;                  |104  - $iEnd is neither a positive number nor zero nor -1
;                  |105  - $fMatchCase must be True or False
;                  |106  - $fwholeWord must be True or False
;                  |1071 - $iBehavior is not a number
;                  |1072 - $iBehavior is invalid
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks:         The inter-character position at the beginning of the control is 0.
;                  The default character range, 0 to -1, searches the whole text downwwards.
;                  Setting $iEnd to -1 searches down to the end of the control
;                  Setting $iStart to -1 searches up to the start of the control
; Related .......: _GUICtrlRichEdit_FindText
; Link ..........: @@MsdnLink@@ EM_FINDTextInRange
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_FindTextInRange($hWnd, $sText, $iStart = 0, $iEnd = -1, $fMatchCase = False, $fWholeWord = False, _
		$iBehavior = 0)
	Local $iLen, $tText, $tFindtext, $iWparam
	If $sText = "" Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iStart, ">=0,-1") Then Return SetError(103, 0, False)
	If Not __GCR_IsNumeric($iEnd, ">=0,-1") Then Return SetError(104, 0, False)
	If Not IsBool($fMatchCase) Then Return SetError(105, 0, False)
	If Not IsBool($fWholeWord) Then Return SetError(106, 0, False)
	If Not __GCR_IsNumeric($iBehavior) Then Return SetError(1071, 0, -1)
	If BitAND($iBehavior, BitNOT(BitOR($FR_MATCHALEFHAMZA, $FR_MATCHDIAC, $FR_MATCHKASHIDA))) <> 0 Then _
			Return SetError(1072, 0, -1)

	$iLen = StringLen($sText) + 3
	$tText = DllStructCreate("wchar[" & $iLen & "]")
	DllStructSetData($tText, 1, $sText)
	$tFindtext = DllStructCreate($tagFINDTEXTEX)
	DllStructSetData($tFindtext, 1, $iStart)
	DllStructSetData($tFindtext, 2, $iEnd)
	DllStructSetData($tFindtext, 3, DllStructGetPtr($tText))
	$iWparam = 0
	If $iEnd >= $iStart Or $iEnd = -1 Then
		$iWparam = $FR_DOWN
	EndIf
	If $fWholeWord Then $iWparam = BitOR($iWparam, $FR_WHOLEWORD)
	If $fMatchCase Then $iWparam = BitOR($iWparam, $FR_MATCHCASE)
	$iWparam = BitOR($iWparam, $iBehavior)
	;	If @AutoItUnicode Then
	__GCR_SendMessage($hWnd, $EM_FINDTEXTEXW, $iWparam, DllStructGetPtr($tFindtext), "iWparam", "ptr")
	;	Else
	__GCR_SendMessage($hWnd, $EM_FINDTEXTEX, $iWparam, DllStructGetPtr($tFindtext), "iWparam", "ptr")
	;	EndIf
	If @error Then Return SetError(700, @error, False)
	Return DllStructGetData($tFindtext, 4) & ";" & DllStructGetData($tFindtext, 5)
EndFunc   ;==>_GUICtrlRichEdit_FindTextInRange

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_GetCharAttributes
; Description....: Returns attributes that are on either in a selection or, if no selection, at the insertion point
; Description....: Returns whether an attribute on throughout a selected text or on for part of it
; Syntax ........: _GuiCtrlRichEdit_GetCharattributes($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success -  a string consisting of three-character groups. Each group consists of:
;                  |   first and second character: any of:
;                  |      bo - bolded
;                  |      di - disabled - characters displayed with a shadow [nd]
;                  |      em - embossed [nd]
;                  |      hi - hidden, i.e. not displayed
;                  |      im - imprinted [nd]
;                  |      it - italcized
;                  |      li - EN_LINK messages are sent when mouse is over text with this attribute
;                  |      ou - outlined [nd]
;                  |      pr - EN_PROTECT sent when user attempts to modify
;                  |      re - marked as revised [nd]
;                  |      sh - shadowed [nd]
;                  |      sm - small capital letters [nd]
;                  |      st - struck out
;                  |      sb - subscript [nd]
;                  |      sp - superscript [nd]
;                  |      un - underlined
;                  |   third character: + for on, ~ for mixed
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
;                  |-1  - no text is selected
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Some attributes do not display in a Rich Edit control; they are marked with [nd] above.
;+
;                  Returns "" if no attributes are on
; Related .......: _GuiCtrlRichEdit_SetCharAttributes
; Link ..........: @@MsdnLink@@ EM_GETCHARFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetCharAttributes($hWnd)
	Local $iMask, $iEffects, $tCharFormat, $sState, $sStatesAndAtts, $fSel, $fM, $fE
	Local Const $av[16][3] = [ _
			["bo", $CFM_BOLD, $CFE_BOLD],["di", $CFM_DISABLED, $CFE_DISABLED], _
			["em", $CFM_EMBOSS, $CFE_EMBOSS],["hi", $CFM_HIDDEN, $CFE_HIDDEN], _
			["im", $CFM_IMPRINT, $CFE_IMPRINT],["it", $CFM_ITALIC, $CFE_ITALIC], _
			["li", $CFM_LINK, $CFE_LINK],["ou", $CFM_OUTLINE, $CFE_OUTLINE], _
			["pr", $CFM_PROTECTED, $CFE_PROTECTED],["re", $CFM_REVISED, $CFE_REVISED], _
			["sh", $CFM_SHADOW, $CFE_SHADOW],["sm", $CFM_SMALLCAPS, $CFE_SMALLCAPS], _
			["st", $CFM_STRIKEOUT, $CFE_STRIKEOUT],["sb", $CFM_SUBSCRIPT, $CFE_SUBSCRIPT], _
			["sp", $CFM_SUPERSCRIPT, $CFE_SUPERSCRIPT],["un", $CFM_UNDERLINE, $CFE_UNDERLINE]]

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then Return SetError(-1, 0, False)
	$tCharFormat = DllStructCreate($tagCHARFORMAT2)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	;	$iWparam = _Iif($fDefault,$SCF_DEFAULT,$SCF_SELECTION)	; SCF_DEFAULT doesn't work
	__GCR_SendMessage($hWnd, $EM_GETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($tCharFormat), 0, "wparam", "ptr")
	If @error Then Return SetError(@error, 0, False)

	$iMask = DllStructGetData($tCharFormat, 2)
	$iEffects = DllStructGetData($tCharFormat, 3)
	$fSel = _GUICtrlRichEdit_IsTextSelected($hWnd)

	$sStatesAndAtts = ""
	For $i = 0 To UBound($av, 1) - 1
		$fM = BitAND($iMask, $av[$i][1]) = $av[$i][1]
		$fE = BitAND($iEffects, $av[$i][2]) = $av[$i][2]
		If $fSel Then
			If $fM Then
				If $fE Then
					$sState = "+"
				Else
					$sState = "-"
				EndIf
			Else
				$sState = "~"
			EndIf
		Else
			If $fM Then
				$sState = "+"
			Else
				$sState = "-"
			EndIf
		EndIf
		If $sState <> "-" Then $sStatesAndAtts &= $av[$i][0] & $sState
	Next
	Return $sStatesAndAtts
EndFunc   ;==>_GuiCtrlRichEdit_GetCharAttributes

#cs
	; #FUNCTION# ;===============================================================================
	;
	; Name ..........: _GUICtrlRichEdit_GetCharBkColor
	; Description ...: Retrieves the background color of the selected text or, if none selected, of the character to the right of the insertion point
	; Syntax ........: _GUICtrlRichEdit_GetCharBkColor($hWnd)
	; Parameters ....: $hWnd 			- Handle to control
	; Return Values. : Success -	a string of values separated by semicolons (;)
	;                  |value 1 - COLORREF value
	;                  |value 2 - red component (percentage)
	;                  |value 3 - green component (percentage)
	;                  |value 4 - blue component (percentage)
	;                  |value 5 -
	;                  | +  - color is the same throughtout the selection
	;                  | ~  - color is that of the first selected character; there are other colors in the selection
	;                  Failure - "" and sets @error to:
	;                  |101 - $hWnd is not a handle
	; Authors........: grham
	; Modified ......: Chris Haslam (c.haslam)
	; Remarks .......:
	; Related .......: _GuiCtrlRichEdit_SetCharBkColor
	; Link ..........: @@MsdnLink@@ EM_CHARFORMAT
	; Example .......: Yes
	;
	;===============================================================================
	Func _GUICtrlRichEdit_GetCharBkColor($hWnd)
	; Commented out because, while it does round-trip with _GUICtrlRichEdit_SetCharBkColor, coloring doesn't show
	Local $tCharFormat, $sRet, $iMask, $iEffects, $iColor

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tCharFormat = DllStructCreate($tagCHARFORMAT2)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	__GCR_SendGetCharFormatMessage($hWnd, $tCharFormat)
	$iMask = DllStructGetData($tCharFormat, 2)
	$iEffects = DllStructGetData($tCharFormat, 3)
	If BitAND($iEffects, $CFE_AUTOBACKCOLOR) = $CFE_AUTOBACKCOLOR Then
	$iColor = _WinAPI_GetSysColor($COLOR_WINDOWTEXT)
	Else
	$iColor = DllStructGetData($tCharFormat, 12)
	EndIf
	$sRet &= $iColor & ";" & __GCR_ConvertColorRefToRGBString($iColor) & ";"
	$sRet &= _Iif(BitAND($iMask, $CFM_COLOR) <> 0, "+", "~")
	Return $sRet
	EndFunc   ;==>_GUICtrlRichEdit_GetCharBkColor
#ce

#cs
	; #FUNCTION# ;===============================================================================
	; Name ..........: _GUICtrlRichEdit_GetCharColor
	; Description ...: Retrieves the color of the selected text or, if none selected, of the character to the right of the insertion point
	; Syntax ........: _GUICtrlRichEdit_GetCharBkColor($hWnd)
	; Parameters ....: $hWnd 			- Handle to control
	; Return Values. : Success -	a string of values separated by semicolons (;)
	;                  |value 1 - COLORREF value
	;                  |value 2 - red component (percentage)
	;                  |value 3 - green component (percentage)
	;                  |value 4 - blue component (percentage)
	;                  |value 5 -
	;                  | +  - color is the same throughtout the selection
	;                  | ~  - color is that of the first selected character; there are other colors in the selection
	;						 Failure - "" and sets @error to:
	;                  |101 - $hWnd is not a handle
	; Authors........: grham
	; Modified ......: Chris Haslam (c.haslam)
	; Remarks .......:
	; Related .......: _GuiCtrlRichEdit_SetCharBkColor
	; Link ..........: @@MsdnLink@@ EM_CHARFORMAT
	; Example .......: Yes
	;===============================================================================
	Func _GUICtrlRichEdit_GetCharColor($hWnd)
	; Commented out because, while it does round-trip with _GUICtrlRichEdit_SetCharColor, coloring doesn't show
	Local $tCharFormat, $sRet, $iMask, $iEffects, $iColor

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$tCharFormat = DllStructCreate($tagCHARFORMAT)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	__GCR_SendGetCharFormatMessage($hWnd, $tCharFormat)
	$iMask = DllStructGetData($tCharFormat, 2)
	$iEffects = DllStructGetData($tCharFormat, 3)
	If BitAND($iEffects, $CFE_AUTOCOLOR) = $CFE_AUTOCOLOR Then
	$iColor = _WinAPI_GetSysColor($COLOR_WINDOWTEXT)
	Else
	$iColor = DllStructGetData($tCharFormat, 6)
	EndIf
	$sRet &= $iColor & ";" & __GCR_ConvertColorRefToRGBString($iColor) & ";"
	consdebug("Get:$iColor",$iColor)
	$sRet &= _Iif(BitAND($iMask, $CFM_COLOR) <> 0, "+", "~")
	Return $sRet
	EndFunc   ;==>_GUICtrlRichEdit_GetCharColor
#ce

; #FUNCTION# ;===============================================================================
;
; Name...........:  _GUICtrlRichEdit_GetCharPosFromXY
; Description ...:  Gets inter-character position closest to a specified point in the client area
; Syntax.........: _GUICtrlRichEdit_CGetharFromPos($hWnd, $iX, $iY)
; Parameters ....: $hWnd        - Handle to the control
;                  |$iX         - horizontal screen coordinate relative to left side of control
;                  |$iY         - vertical screen coordinate relative to top of control
; Return values .: Success      - one-based character index of the character nearest the specified point
;                  |(index of last character in the control if the specified point is beyond text)
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iX is not a number
;                  |103 - $iY is not a number
;                  |-1 - ($iX,$iY) outside client area
; Author ........: Gary Frost (gafrost)
; Modified.......: Prog@ndy, Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GuiCtrlRichEdit_GetXYFromCharPos
; Link ..........; @@MsdnLink@@ EM_CHARFROMPOS
; Example .......: Yes
;
;===============================================================================================================================
Func _GUICtrlRichEdit_GetCharPosFromXY($hWnd, $iX, $iY)
	Local $tPointL, $iResult, $aiRect
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iX) Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iY) Then Return SetError(103, 0, False)

	$aiRect = StringSplit(_GUICtrlRichEdit_GetFormattingRect($hWnd), ";")
	If $iX < $aiRect[1] Or $iX > $aiRect[3] Or $iY < $aiRect[2] Or $iY > $aiRect[4] Then Return -1
	$tPointL = DllStructCreate("LONG x; LONG y;")
	DllStructSetData($tPointL, 1, $iX)
	DllStructSetData($tPointL, 2, $iY)
	$iResult = __GCR_SendMessage($hWnd, $EM_CHARFROMPOS, 0, DllStructGetPtr($tPointL), 0, "wparam", "ptr")
	If @error Then Return SetError(@error, -1, "")
	Return $iResult
EndFunc   ;==>_GUICtrlRichEdit_GetCharPosFromXY

; #FUNCTION# ;===============================================================================
;
; Name...........:  _GuiCtrlRichEdit_GetCharPosOfNextWord
; Description ...:  Gets inter-character position before the next word
; Syntax.........: _GuiCtrlRichEdit_GetCharPosOfNextWord($hWnd, $iCpStart)
; Parameters ....: $hWnd      - Handle to the control
;                  |$iCPStart - inter-character position to start from
; Return values .: Success      - inter-character position before next word
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iCpStart is not a number
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: "Word" is defined broadly; it includes punctuation, parentheses and hyphens.
; Related .......: _GuiCtrlRichEdit_GetCharPosofPreviousWord
; Link ..........; @@MsdnLink@@ EM_FINDWORDBREAK
; Example .......: Yes
;
;===============================================================================================================================
Func _GuiCtrlRichEdit_GetCharPosOfNextWord($hWnd, $iCpStart)
	; WB_RIGHT, WB_LEFT, WB_RIGHTBREAK, WB_LEFTBREAK and WB_ISDELIMITER don't work properly or at all
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iCpStart) Then Return SetError(102, 0, False)

	Return __GCR_SendMessage($hWnd, $EM_FINDWORDBREAK, $WB_MOVEWORDRIGHT, $iCpStart)
EndFunc   ;==>_GuiCtrlRichEdit_GetCharPosOfNextWord

; #FUNCTION# ;===============================================================================
;
; Name...........:  _GuiCtrlRichEdit_GetCharPosOfPreviousWord
; Description ...:  Gets inter-character position before the Previous word
; Syntax.........: _GuiCtrlRichEdit_GetCharPosOfPreviousWord($hWnd, $iCpStart)
; Parameters ....: $hWnd      - Handle to the control
;                  |$iCPStart - inter-character position to start from
; Return values .: Success      - inter-character position before Previous word
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iCpStart is not a number
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: "Word" is defined broadly; it includes punctuation, parentheses and hyphens.
; Related .......: _GuiCtrlRichEdit_GetCharPosofNextWord
; Link ..........; @@MsdnLink@@ EM_FINDWORDBREAK
; Example .......: Yes
;
;===============================================================================================================================
Func _GuiCtrlRichEdit_GetCharPosOfPreviousWord($hWnd, $iCpStart)
	; WB_RIGHT, WB_LEFT, WB_RIGHTBREAK, WB_LEFTBREAK and WB_ISDELIMITER don't work properly or at all
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iCpStart) Then Return SetError(102, 0, False)

	Return __GCR_SendMessage($hWnd, $EM_FINDWORDBREAK, $WB_MOVEWORDLEFT, $iCpStart)
EndFunc   ;==>_GuiCtrlRichEdit_GetCharPosOfPreviousWord

; #FUNCTION# ;===============================================================================
;
; Name...........:  _GuiCtrlRichEdit_GetCharWordBreakInfo
; Description ...:  Gets inter-character position before the Previous word
; Syntax.........: __GuiCtrlRichEdit_GetCharWordBreakInfo($hWnd, $iCp)
; Parameters ....: $hWnd - Handle to the control
;                  |$iCP - inter-character position to left of character of interest
; Return values .: Success      - string consisting of comma-separated values:
;                  |value 1 - word-break flag(s):
;                  |  c - line may be broken after this character
;                  |  d - character is an end-of-word delimiter. Lines may be broken after delimiters
;                  |  w - character is white-space. (Trailing white-spaces are not included in line length.)
;                  |value 2 - character class: a number
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iCp is not a number
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: Lines may be broken at delimiters or between characters od different classes.
;+
;                  Character classes are defined in word-break procedures. The classes in the default
;                  procedure are: 0 = alphanumeric character, 1 = other printing character (except hyphen),
;                  2 = space, 3 = tab, 4 = hyphen or end-of-paragraph.
;
; Related .......: _GuiCtrlRichEdit_GetCharPosofNextWord
; Link ..........; @@MsdnLink@@ EM_FINDWORDBREAK
; Example .......: Yes
;

Func _GuiCtrlRichEdit_GetCharWordBreakInfo($hWnd, $iCp)
	Local $iResult, $sRet, $iClass
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iCp) Then Return SetError(102, 0, False)

	$iResult = __GCR_SendMessage($hWnd, $EM_FINDWORDBREAK, $WB_CLASSIFY, $iCp)
	$sRet = ""
	$iClass = BitAND($iResult, 0xF0)
	If BitAND($iClass, $WBF_BREAKAFTER) Then $sRet &= "c"
	If BitAND($iClass, $WBF_BREAKLINE) Then $sRet &= "d"
	If BitAND($iClass, $WBF_ISWHITE) Then $sRet &= "w"
	$sRet &= ";" & BitAND($iResult, 0xF)
	Return $sRet
EndFunc   ;==>_GuiCtrlRichEdit_GetCharWordBreakInfo

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetCtrlText
; Description ...: Get all of the text in the control
; Syntax ........: _GUICtrlRichEdit_GetCtrlText($hWnd[, $fCrToCrLf = False[, $iCodePage = 0[, $sReplChar = ""]]])
; Parameters ....: $hWnd 			- Handle to control
;                  $fCrToCrLf - Convert each CR to a CrLf (Optional)
;                  |True - do it
;                  | don't (Default)
;                  $iCodePage - code page used in translation (Optional)
;                  |Default: use system default
;                  |CP_ACP for ANSI, 1200 for Unicode
;                  $sReplaChar - Character used if $iCodePage is not 1200 and a wide character cannot be represented in
;                  +specified code page (Optional)
; Return Values. : Success -	the text
;						 Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fCrToCrLf must be True or False
;                  |103 - $iCodePage is not a number
;                  |700 - internal error
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: On success, if $sReplChar set, @extended contains whether this character was used
; Related .......: _GuiCtrlRichEdit_SetText, _GuiCtrlRichEdit_AppendText, _GuiCtrlRichEdit_InsertText
; Link ..........: @@MsdnLink@@ EEM_GETTEXTEX
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetCtrlText($hWnd, $fCrToCrLf = False, $iCodePage = 0, $sReplChar = "")
	Local $iLen, $tText, $tGetTextEx, $iResult, $tDefaultChar, $tUsedDefChar
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fCrToCrLf) Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iCodePage) Then Return SetError(103, 0, False)

	$iLen = _GUICtrlRichEdit_GetCtrlTextLength($hWnd) + 1
	$tText = DllStructCreate("wchar[" & $iLen & "]")

	$tGetTextEx = DllStructCreate($tagGETTEXTEX)
	DllStructSetData($tGetTextEx, 1, $iLen)
	If $fCrToCrLf Then DllStructSetData($tGetTextEx, 2, $GT_USECRLF)
	If $iCodePage <> 0 Then DllStructSetData($tGetTextEx, 3, $iCodePage)
	If $sReplChar <> "" Then
		$tDefaultChar = DllStructCreate("wchar [2]")
		DllStructSetData($tGetTextEx, 4, $tDefaultChar)
		$tUsedDefChar = DllStructCreate("int")
	EndIf
	DllStructSetData($tGetTextEx, 3, $CP_UNICODE)
	$iResult = __GCR_SendMessage($hWnd, $EM_GETTEXTEX, DllStructGetPtr($tGetTextEx), DllStructGetPtr($tText))
	If $iResult = 0 Then Return SetError(700, 0, "")
	If $sReplChar <> "" Then SetExtended(DllStructGetData($tUsedDefChar, 1) <> 0)
	Return DllStructGetData($tText, 1)
EndFunc   ;==>_GUICtrlRichEdit_GetCtrlText

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetCtrlTextLength
; Description ...: Get the length of the whole text in the control
; Syntax ........: _GUICtrlRichEdit_GetCtrlTextLength($hWnd[, $fExact = True[, $fChars = False]])
; Parameters ....: $hWnd 			- Handle to control
;                  $fExact -   = True Return the exact length (Optional)
;                  |       -  = False return at least the number of characters in the control (faster)
;                  |Default: exact length
;                  $fChars - = True - return length in characters
;                  |          = False - return length in bytes
;                  |Default: bytes
; Return Values. : Success -	length, in bytes or characters
;						 Failure - False and sets @error to:
;                  |101 - $hWnd is not a handle
;                  |102 - $fExact must be True or False
;                  |103 - $fChars must be True or False
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ EM_GETTEXTLENGTHEX
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetCtrlTextLength($hWnd, $fExact = True, $fChars = False)
	Local $tGetTextLen, $iResult, $iFlags

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fExact) Then Return SetError(102, 0, False)
	If Not IsBool($fChars) Then Return SetError(103, 0, False)

	$tGetTextLen = DllStructCreate($tagGETTEXTLENGTHEX)
	$iFlags = BitOR($GTL_USECRLF, _Iif($fExact, $GTL_PRECISE, $GTL_CLOSE))
	$iFlags = BitOR($iFlags, _Iif($fChars, $GTL_DEFAULT, $GTL_NUMBYTES))
	DllStructSetData($tGetTextLen, 1, $iFlags)
	DllStructSetData($tGetTextLen, 2, _Iif($fChars, $CP_ACP, $CP_UNICODE))
	$iResult = __GCR_SendMessage($hWnd, $EM_GETTEXTLENGTHEX, DllStructGetPtr($tGetTextLen), 0)
	Return $iResult
EndFunc   ;==>_GUICtrlRichEdit_GetCtrlTextLength

; #FUNCTION# ====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetCtrlZoom
; Description ...: Gets the zoom level of the control
; Syntax.........: _GUICtrlRichEdit_GetCtrlZoom($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success: zoom level, in percent
;                  False: False and sets @error:
;                  |700 - internal error
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_SetCtrlZoom
; Link ..........; @@MsdnLink@@ EM_GETZOOM
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetCtrlZoom($hWnd)
	Local $wParam, $lparam, $iRet, $ai
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$ai = __GCR_SendMessage($hWnd, $EM_GETZOOM, $wParam, $lparam, -1, "int*", "int*")
	If @error Or $ai[0] = False Then Return SetError(700, 0, False)
	If $ai[3] = 0 And $ai[4] = 0 Then ; if a control that has not been zoomed
		$iRet = 100
	Else
		$iRet = $ai[3] / $ai[4] * 100
	EndIf
	Return StringFormat("%.2f", $iRet)
EndFunc   ;==>_GUICtrlRichEdit_GetCtrlZoom

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetFirstCharPosOnLine
; Description ...: Retrieves the inter-character position preceding the first character of a line
; Syntax ........: _GUICtrlRichEdit_GetFirstCharPosOnLine($h_RichEdit[, $iLine = -1])
; Parameters ....: $hWnd 			- Handle to control
;                  $iLine - Line number (Optional)
;                  |Default: current line
; Return Values. : Success - Character position of the first character of the line
;						 Failure - 0  and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - $iLine is neither positive nor -1
;                  |1022 - $iLine greater than number of lines of text
; Authors........: Gary Frost (custompcs at charter dot net)
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: A control that contains no text has one line.
;+
;                  The first line is line 1. The first character position in the client area is 0.
; Related .......:
; Link ..........: @@MsdnLink@@ EM_GETLINEINDEX
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetFirstCharPosOnLine($hWnd, $iLine = -1)
	Local $iResult
	If Not IsHWnd($hWnd) Then Return SetError(1012, 0, False)
	If Not __GCR_IsNumeric($iLine, ">0,-1") Then Return SetError(1021, 0, False)

	If $iLine <> -1 Then $iLine -= 1
	$iResult = __GCR_SendMessage($hWnd, $EM_LINEINDEX, $iLine)
	If $iResult = -1 Then
		Return SetError(1022, 0, False)
	Else
		Return $iResult
	EndIf
EndFunc   ;==>_GUICtrlRichEdit_GetFirstCharPosOnLine

; #FUNCTION# ;===============================================================================
;
; Name...........: _GUICtrlRichEdit_GetFont
; Description ...: Gets the font attributes of a selection or, if no selection, at the insertion point
; Syntax.........: _GUICtrlRichEdit_GetFont($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success - a string containing values separated by semicolons (;):
;                  |value 1 - point size
;                  |   0 if sizes are mixed in selection
;                  |value 2 - the name of the font
;                  |   "" if fonts are mixed in selection
;                  |value 3 - the character set
;                  |   $ANSI_CHARSET        - 0
;                  |   $BALTIC_CHARSET      - 186
;                  |   $CHINESEBIG5_CHARSET - 136
;                  |   $EASTEUROPE_CHARSET  - 238
;                  |   $GB2312_CHARSET      - 134
;                  |   $GREEK_CHARSET       - 161
;                  |   $HANGEUL_CHARSET     - 129
;                  |   $MAC_CHARSET         - 77
;                  |   $OEM_CHARSET         - 255
;                  |   $RUSSIAN_CHARSET     - 204
;                  |   $SHIFTJIS_CHARSET    - 128
;                  |   $SYMBOL_CHARSET      - 2
;                  |   $TURKISH_CHARSET     - 162
;                  |   $VIETNAMESE_CHARSET  - 163
;                  |$iLcid - Locale ID
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_SetFont
; Link ..........; @@MsdnLink@@ EM_GETCHARFORMAT, @@MsdnLink@@ LOGFONT, http://www.hep.wisc.edu/~pinghc/books/apirefeng/l/logfont.html
; Example .......: Yes
;
;===============================================================================================================================
Func _GUICtrlRichEdit_GetFont($hWnd)
	; MSDN does not give a mask (CFM) for bPitchAndFamily so it appears that there is no way of knwoing when it is valid => omitted here
	Local $sName = "", $iPoints = 0, $iCharset = 0, $iLcid = 1033, $tCharFormat
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tCharFormat = DllStructCreate($tagCHARFORMAT)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))

	__GCR_SendGetCharFormatMessage($hWnd, $tCharFormat)

	If BitAND(DllStructGetData($tCharFormat, 2), $CFM_FACE) = $CFM_FACE Then _
			$sName = DllStructGetData($tCharFormat, 9)

	If BitAND(DllStructGetData($tCharFormat, 2), $CFM_SIZE) = $CFM_SIZE Then _
			$iPoints = DllStructGetData($tCharFormat, 4) / 20

	If BitAND(DllStructGetData($tCharFormat, 2), $CFM_CHARSET) = $CFM_CHARSET Then _
			$iCharset = DllStructGetData($tCharFormat, 7)

	If BitAND(DllStructGetData($tCharFormat, 2), $CFM_LCID) = $CFM_LCID Then _
			$iLcid = DllStructGetData($tCharFormat, 13)

	Return $iPoints & ";" & $sName & ";" & ";" & $iCharset & ";" & $iLcid
EndFunc   ;==>_GUICtrlRichEdit_GetFont

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetFormattingRect
; Description ...: Retrieves the formatting rectangle of a control
; Syntax.........: _GUICtrlRichEdit_GetFormattingRect($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - a string consisting of x and y coordinates - "<left>;<top>;<right>;<bottom>"
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The formatting rectangle is the area in which text is drawn, part of which may not be visible.
;+
;                  All returned values are in dialog units referenced to the control
;+
;                  Per MSDN, the values returned by this function may not be exactly what may be set by _GUICtrlRichEdit_SetFormattingRect
; Related .......:
; Link ..........; @@MsdnLink@@ EM_GETRECT
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetFormattingRect($hWnd)
	Local $aiRect[4], $tRect

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$tRect = DllStructCreate($tagRECT)
	__GCR_SendMessage($hWnd, $EM_GETRECT, 0, DllStructGetPtr($tRect), 0, "wparam", "ptr")
	$aiRect[0] = DllStructGetData($tRect, "Left")
	$aiRect[1] = DllStructGetData($tRect, "Top")
	$aiRect[2] = DllStructGetData($tRect, "Right")
	$aiRect[3] = DllStructGetData($tRect, "Bottom")
	Return $aiRect[0] & ";" & $aiRect[1] & ";" & $aiRect[2] & ";" & $aiRect[3]
EndFunc   ;==>_GUICtrlRichEdit_GetFormattingRect

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetLineCount
; Description ...: Retrieves the number of lines in a multi-line edit control
; Syntax ........: _GUICtrlRichEdit_GetItalic($hWnd)
; Parameters ....: $hWnd 			- Handle to control
; Return Values. : Success - Total number of text lines
;						 Failure - False  and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Gary Frost (custompcs at charter dot net)
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: A control that contains no text has one line
;+
;                  Lines that are not currently visible are included in the count
;+
;                  If Wordwrap is enabled, the number of lines can change when the dimensions of the editing window change.
; Related .......:
; Link ..........: @@MsdnLink@@ EM_GETLINECOUNT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetLineCount($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_GETLINECOUNT)
EndFunc   ;==>_GUICtrlRichEdit_GetLineCount

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetLineLength
; Description ...: Retrieves the length of a line
; Syntax ........: _GUICtrlRichEdit_GetLineLength($hWnd, $iLine)
; Parameters ....: $hWnd 			- Handle to control
;                  $iLine - line number
;                  |Special value: -1 - return number of unselected characters on lines containing selected characters
; Return Values..: Success - Multi-line control - Length of the line (in characters)
;                  |Single-line control - number of characters in the control
;						 Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iLine is neither positive nor -1
;                  |1022 - $iLine is greater than number of characters in the control
; Authors........: Gary Frost (custompcs at charter dot net)
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: A control that contains no text has one line
;                  The first inter-character position in a control is 0.
;+
;                  The result does not include carriage-return characters at the end of the line.
; Related .......:
; Link ..........: @@MsdnLink@@ EM_GETLINELENGTH
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetLineLength($hWnd, $iLine)
	Local $iResult, $iCharPos
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iLine, ">0,-1") Then Return SetError(102, 0, False)

	$iCharPos = _GUICtrlRichEdit_GetFirstCharPosOnLine($hWnd, $iLine)
	$iResult = __GCR_SendMessage($hWnd, $EM_LINELENGTH, $iCharPos)
	Return $iResult
EndFunc   ;==>_GUICtrlRichEdit_GetLineLength


; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_GetLineNUmberFromCharPos
; Description ...: Retrieves the line number on which an inter-character position is found
; Syntax ........: _GuiCtrlRichEdit_GetLineNUmberFromCharPos($hWnd, $iCharPos)
; Parameters ....: $hWnd 			- Handle to control
;                  $iCharPos - Inter-character position
; Return Values..: Success - Line number
;						 Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iCharPos is not a positive number
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: A control that contains no text has one line
;                  The first inter-character position in a control is 0.
;+
;                  The first line is line 1.
;+
;                  If $iCharPos is negative or more than the number of characters in the control,
;                  returns the number of lines in the control
; Related .......:
; Link ..........: @@MsdnLink@@ EM_EXLINEFROMCHAR
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetLineNUmberFromCharPos($hWnd, $iCharPos)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iCharPos, ">=0") Then Return SetError(102, 0, False)

	Return __GCR_SendMessage($hWnd, $EM_EXLINEFROMCHAR, 0, $iCharPos) + 1
EndFunc   ;==>_GuiCtrlRichEdit_GetLineNUmberFromCharPos

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetNextRedo
; Description ...: Retrieves the name or type ID of the next possible redo action
; Syntax.........: _GUICtrlRichEdit_GetNextRedo($hWnd, $fName = True)
; Parameters ....: $hWnd        - Handle to the control
;                  $fName       - True (return name, default) or False (return ID number)
; Return values .: Success - depends on value of $fName:
;                  |If $fName is True: "Unknown", "Typing", "Delete", "Drag and drop", "Cut" or "Paste"
;                  |If $Name is False: the corresponding number (0 to 5)
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fName is neither True nor False
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: Note that EM_GETREDONAME does not distinguish between Unknown and redo queue empty
; Related .......: _GUICtrlRichEdit_Redo, _GuiCtrlRichEdit_Undo
; Link ..........; @@MsdnLink@@ EM_GETREDONAME
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetNextRedo($hWnd, $fName = True)
	Local Const $as[6] = ["Unknown", "Typing", "Delete", "Drag and drop", "Cut", "Paste"]
	Local $iUid
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fName) Then Return SetError(102, 0, False)

	$iUid = __GCR_SendMessage($hWnd, $EM_GETREDONAME, 0, 0)
	If $fName Then
		Return $as[$iUid]
	Else
		Return $iUid
	EndIf
EndFunc   ;==>_GUICtrlRichEdit_GetNextRedo

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetNextUndo
; Description ...: Retrieves the name or type ID of the next possible Undo action
; Syntax.........: _GUICtrlRichEdit_GetNextUndo($hWnd, $fName = True)
; Parameters ....: $hWnd        - Handle to the control
;                  $fName       - True (return name, default) or False (return ID number)
; Return values .: Success - depends on value of $fName:
;                  |If $fName is True: "Unknown", "Typing", "Delete", "Drag and drop", "Cut" or "Paste"
;                  |If $Name is False: the corresponding number (0 to 5)
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fName is neither True nor False
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: Note that EM_GETUndoNAME does not distinguish between Unknown and Undo queue empty
; Related .......: _GUICtrlRichEdit_Undo, _GuiCtrlRichEdit_Undo
; Link ..........; @@MsdnLink@@ EM_GETUndoNAME
;
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetNextUndo($hWnd, $fName = True)
	Local Const $as[6] = ["Unknown", "Typing", "Delete", "Drag and drop", "Cut", "Paste"]
	Local $iUid
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fName) Then Return SetError(102, 0, False)

	$iUid = __GCR_SendMessage($hWnd, $EM_GETUNDONAME, 0, 0)
	If $fName Then
		Return $as[$iUid]
	Else
		Return $iUid
	EndIf
EndFunc   ;==>_GUICtrlRichEdit_GetNextUndo

; #FUNCTION# ;===============================================================================
;
; Name...........: _GUICtrlRichEdit_GetNumberOfFirstVisibleLine
; Description ...: Gets number of the first line which is visible in the control
; Syntax.........: _GUICtrlRichEdit_GetNumberOfFirstVisibleLine($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - line number
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Gary Frost (gafrost)
; Modified.......:
; Remarks .......: The first line is numbered 1
; Related .......:
; Link ..........; @@MsdnLink@@ EM_GETFIRSTVISIBLELINE
; Example .......: Yes
;
;===============================================================================================================================
Func _GUICtrlRichEdit_GetNumberOfFirstVisibleLine($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_GETFIRSTVISIBLELINE) + 1
EndFunc   ;==>_GUICtrlRichEdit_GetNumberOfFirstVisibleLine

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetParaAlignment
; Description....: Gets the alignment of selected paragraph(s), or (if no selection) of the current paragraph
; Syntax ........: _GUICtrlRichEdit_GetParaAlignment($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - a string consisting of a value, a semicolon (;), and a scope character:
;                  |alignment:
;                  |   l - aligned with the left margin.
;                  |   r - aligned with the right margin.
;                  |   c - centered between margins
;                  |   j - justified between margins
;                  |   f - justified between margins
;                  |   w - justified between margins by only expanding spaces
;                  |scope:
;                  |   a - all (or only) selected paragraphs have this alignment
;                  |   f - the first selected paragraph has this alignment, but other(s) don't
;                  |   c - the current paragraph has this alignment
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: In Richedit 2.0, justify does not display
; Related .......: _GUICtrlRichEdit_SetParaAlignment
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetParaAlignment($hWnd)
	Local $tParaFormat, $iAlignment, $sRet, $iMask
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, "")

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))

	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	If @error Then Return SetError(@error, 0, False)
	$iMask = DllStructGetData($tParaFormat, 2)
	$iAlignment = DllStructGetData($tParaFormat, 8)
	Switch ($iAlignment)
		Case $PFA_LEFT
			$sRet = "l"
		Case $PFA_CENTER
			$sRet = "c"
		Case $PFA_RIGHT
			$sRet = "r"
		Case $PFA_JUSTIFY
			$sRet = "j"
		Case $PFA_FULL_INTERWORD
			$sRet = "w"
	EndSwitch
	$sRet &= ";" & __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_ALIGNMENT)
	Return $sRet
EndFunc   ;==>_GUICtrlRichEdit_GetParaAlignment

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetParaAttributes
; Description....: Gets the attributes of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GuiCtrlRichEdit_SetParaAtttributes($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - a string consisting of values separated by semicolons (;}.
;                  |Values 1 to 10: a group consisting of:
;                  |   Characters 1 to 3 - attribute
;                  |      fpg  -  force this/these paragraphs on to new page(s) (Initially off)
;                  |      hyp  -  automatic hypthenation (Initially on)
;                  |      kpt  -  keep this/these paragraph(s) together on a page (Initially off}
;                  |      kpn  -  keep this/these paragraph(s) and the next together on a page (Initially off)
;                  |      pwo  -  prevent widows and orphans, i.e. avoid a single line of this/these paragraph(s)
;                  +on a page (Initially off)
;                  |      r2l  -  display text using right-to-left reading order (Initially off)
;                  |      sbs  -  display paragraphs side by side (Initially off)
;                  |      sln  -  suppress line numbers in documents or sections with line numbers (Initially off)
;                  |      tbl  -  paragraph(s) is/are table row(s) (Initially off)
;                  |   Character 4 - state:
;                  |      +  -  attribute is on
;                  |      -  -  attribute is off
;                  |Value 11 - scope:
;                  |   f - the first selected paragraph has these attributes
;                  |   c - the current paragraph has these attributes
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_SetParaAttributes
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetParaAttributes($hWnd)
	; dwMask is always bitor of all PFMs
	Local $iEffects, $tParaFormat, $sStatesAndAtts = "", $sState
	Local Enum $kAbbrev = 0, $kEffect, $kInverted
	; MS seems to mean LINENUMBER and WIDOWCONTROL, not NOLINENUMBER and NOWIDOWCONTROL
	Local Const $av[9][3] = [ _	; abbrev, mask, effect, inverted
			["fpg", $PFE_PAGEBREAKBEFORE, False], _
			["hyp", $PFE_DONOTHYPHEN, True], _
			["kpt", $PFE_KEEP, False], _
			["kpn", $PFE_KEEPNEXT, False], _
			["pwo", $PFE_NOWIDOWCONTROL, False], _
			["r2l", $PFE_RTLPARA, False], _
			["row", $PFE_TABLE, False], _
			["sbs", $PFE_SIDEBYSIDE, False], _
			["sln", $PFE_NOLINENUMBER, False]]
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	If @error Then Return SetError(@error, 0, -1)

	$iEffects = DllStructGetData($tParaFormat, "wEffects")

	$sStatesAndAtts = ""
	For $i = 0 To UBound($av, 1) - 1
		$sStatesAndAtts &= $av[$i][$kAbbrev]
		If BitAND($iEffects, $av[$i][$kEffect]) = $av[$i][$kEffect] Then
			$sState = _Iif($av[$i][$kInverted], "-", "+")
		Else
			$sState = _Iif($av[$i][$kInverted], "+", "-")
		EndIf
		$sStatesAndAtts &= $sState & ";"
	Next
	$sStatesAndAtts &= _Iif(_GUICtrlRichEdit_IsTextSelected($hWnd), "f", "c")
	Return $sStatesAndAtts
EndFunc   ;==>_GuiCtrlRichEdit_GetParaAttributes

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_GetParaBorder
; Description....: Gets the border settings of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GuiCtrlRichEdit_GetParaSAtttributes($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success -  settings of first selected paragraph - a string consisting of values separated by semicolons (:):
;                  Value 1 - one or more of:
;                  |   l - left border
;                  |   r - right border
;                  |   t - top border
;                  |   b - bottom border
;                  |   i - inside border
;                  |   o - outside border
;                  |   or empty - no border
;                  |Value 2 -  line style - one of:
;                  |   none - no line
;                  |   .75   -  3/4 point
;                  |   1.5   -  1 1/2 points
;                  |   2.25  -  2 1/4 points
;                  |   3     -  3 points
;                  |   4.5   -  4 1/2 points
;                  |   6     -  6 points
;                  |   .75d  -  1/2 points, double
;                  |   1.5d  -  1 1/2 points, double
;                  |   2.25d -  2 1/4 points, double
;                  |   .75g  -  3/4 point grey
;                  |   .75gd - 3/4 point grey dashed
;                  |Value 3 - one of:
;                  |   aut   - autocolor
;                  |   blk   - black
;                  |   blu   - blue
;                  |   cyn   - cyan
;                  |   grn   - green
;                  |   mag   - magenta
;                  |   red   - red
;                  |   yel   - yellow
;                  |   whi   - white
;                  |   dbl   - dark blue
;                  |   dgn   - dark green
;                  |   dmg   - dark magenta
;                  |   drd   - dark red
;                  |   dyl   - dark yellow
;                  |   dgy   - dark grey
;                  |   lgy   - light grey
;                  |Value 4 - space between the border and the text (in space units)
;                  |Value 5 - scope:
;                  |   a - all (or only) selected paragraphs have these settings
;                  |   f - the first selected paragraph has these settings, but other(s) don't
;                  |   c - the current paragraph has these settings
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Borders do not show in Rich Edit, but borders created here will show in Word
; Related .......: _GUICtrlRichEdit_SetParaBorder
; Link ..........: @@MsdnLink@@ EM_GETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetParaBorder($hWnd)
	Local $tParaFormat, $iSpace, $iBorders, $sRet, $iMask
	Local Const $avLocs[6][2] = [["l", 1],["r", 2],["t", 4],["b", 8],["i", 16],["o", 32]]
	Local Const $avLS[12] = ["none", .75, 1.5, 2.25, 3, 4.5, 6, ".75d", "1.5d", "2.25d", ".75g", ".75gd"]
	Local Const $sClrs = "blk;blu;cyn;grn;mag;red;yel;whi;dbl;dgn;dmg;drd;dyl;dgy;lgy;"
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	If @error Then Return SetError(@error, 0, -1)

	$iMask = DllStructGetData($tParaFormat, 2)
	$iSpace = DllStructGetData($tParaFormat, 22)
	;	$iWidth = DllStructGetData($tParaFormat, 23)	; wBorderWidth does not round-trip in Rich Edit 3.0
	$iBorders = DllStructGetData($tParaFormat, 24)

	$sRet = ""
	For $i = 0 To UBound($avLocs, 1) - 1
		If BitAND($iBorders, $avLocs[$i][1]) Then $sRet &= $avLocs[$i][0]
	Next
	$sRet &= ";"
	$sRet &= $avLS[BitShift(BitAND($iBorders, 0xF00), 8)]
	$sRet &= ";"
	If BitAND($iBorders, 64) Then
		$sRet &= "aut"
	Else
		$sRet &= StringMid($sClrs, BitShift(BitAND($iBorders, 0xF000), 12) * 4 + 1, 3)
	EndIf
	$sRet &= ";"
	$sRet &= __GCR_ConvertTwipsToSpaceUnit($iSpace) & ";" ; & __GCR_ConvertTwipsToSpaceUnit($iWidth) & ";"
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_BORDER)
	Return $sRet
EndFunc   ;==>_GuiCtrlRichEdit_GetParaBorder

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetParaIndents
; Description....: Gets the border indent settings of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GuiCtrlRichEdit_GetParaIndents($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success -  settings - a string consisting of values separated by semicolons (:):
;                  |Value 1 - Left - indentation of left side of the body of the paragraph (of lines after the first) (in space units)
;                  |Value 2 - Right - indentation of  right side of the paragraph (in space units)
;                  |Value 3 - FirstLine - indentation of the first line relative to other lines (in space units)
;                  |Value 4 - scope:
;                  |   a - all (or only) selected paragraphs have these settings
;                  |   f - the first selected paragraph has these settings, but other(s) don't
;                  |   c - the current paragraph has these settings
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Postive values of $iLeft, $iRight and $iFirstLine indent towards the center of the paragraph
;+To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
; Related .......: _GUICtrlRichEdit_SetParaIndents, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_GETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetParaIndents($hWnd)
	Local $tParaFormat, $idxSI, $iDxOfs, $iDxRI, $ret, $iLeft, $iFirstLine, $iRight, $iMask

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, "dwMask", BitOR($PFM_STARTINDENT, $PFM_OFFSET))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	If @error Then Return SetError(@error, 0, "")

	$iMask = DllStructGetData($tParaFormat, "dwMask")
	$idxSI = DllStructGetData($tParaFormat, "dxStartIndent") ; absolute
	$iDxOfs = DllStructGetData($tParaFormat, "dxOffset")
	$iDxRI = DllStructGetData($tParaFormat, "dxRightIndent")

	$iLeft = __GCR_ConvertTwipsToSpaceUnit($idxSI + $iDxOfs)
	$iFirstLine = __GCR_ConvertTwipsToSpaceUnit(-$iDxOfs)
	$iRight = __GCR_ConvertTwipsToSpaceUnit($iDxRI)

	$ret = $iLeft & ";" & $iRight & ";" & $iFirstLine & ";" & __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_STARTINDENT)
	Return $ret
EndFunc   ;==>_GuiCtrlRichEdit_GetParaIndents

; #FUNCTION# ;===============================================================================
;
; Name ..........: GUICtrlRichEdit_GetParaNumbering
; Description....: Gets the numbering style of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GUICtrlRichEdit_SetParaNumbering($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - values separated by semicolons (;):
;                  |value 1 - a string showing the style and starting "number"
;                  +e.g. "." (bullet), "1)","(b)", "C.", "iv", "V)"
;                  |   Trailing spaces indicate the minimum spaces between the number and the paragraph
;                  |   Special cases:
;                  |      "=" - This paragraph is an unnumbered paragraph within the preceding list element
;                  |       "" - removed the numbering from the selected paragraph(s)
;                  |value 2 - If Roman numbers, "Roman" else ""
;                  |value 3 - space between number/bullet and paragraph (in space units)
;                  |Value 4 - scope:
;                  |   a - all (or only) selected paragraphs have these settings
;                  |   f - the first selected paragraph has these settings, but other(s) don't
;                  |   c - the current paragraph has these settings

;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
; Related .......: _GUICtrlRichEdit_SetParaNumbering, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_GETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetParaNumbering($hWnd)
	Local Const $avRoman[7][2] = [[1000, "m"],[500, "d"],[100, "c"],[50, "l"],[10, "x"],[5, "v"],[1, "i"]]
	Local $tParaFormat, $iChar, $iStart, $iStyle, $iTab, $sRet, $iQspaces, $iPoints, $av, $iMask

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, 2, BitOR($PFM_NUMBERING, $PFM_NUMBERINGSTART, $PFM_NUMBERINGSTYLE))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)

	$iMask = DllStructGetData($tParaFormat, "dwMask")
	$iChar = DllStructGetData($tParaFormat, "wNumbering")
	$iStart = DllStructGetData($tParaFormat, "wNumberingStart")
	$iStyle = DllStructGetData($tParaFormat, "wNumberingStyle")
	$iTab = DllStructGetData($tParaFormat, "wNumberingTab")
	Switch $iChar
		Case 0 ; no numbering
			$sRet = ""
		Case 1 ; bullet
			$sRet = "."
		Case 2 ; Arabic
			$sRet = $iStart
		Case 3
			$sRet = Chr(Asc("a") + $iStart - 1)
		Case 4
			$sRet = Chr(Asc("a") + $iStart - 1)
		Case 5, 6 ; lower case Roman
			$sRet = ""
			For $i = 0 To UBound($avRoman, 1) - 2 Step 2
				For $j = $i To $i + 1
					While $iStart >= $avRoman[$j][0]
						$sRet &= $avRoman[$j][1]
						$iStart -= $avRoman[$j][0]
					WEnd
					If $iStart = $avRoman[$j][0] - 1 Then
						$sRet &= $avRoman[$i + 2][1] & $avRoman[$j][1]
						$iStart -= $avRoman[$j][0] - $avRoman[$i + 2][0]
					EndIf
				Next
			Next
			While $iStart > 0
				$sRet &= "i"
				$iStart -= 1
			WEnd
			If $iChar = 6 Then $sRet = StringUpper($sRet)
	EndSwitch
	If $iChar > 1 Then
		Switch $iStyle
			Case 0
				$sRet &= ")"
			Case 0x100
				$sRet = "(" & $sRet & ")"
			Case 0x200
				$sRet &= "."
			Case 0x300 ; display only number
				; do nothing
		EndSwitch
	EndIf
	; set number-to-text spacing based on font at anchor
	$av = StringSplit(_GUICtrlRichEdit_GetFont($hWnd), ";")
	$iPoints = $av[1]
	$iQspaces = Round($iTab / ($iPoints * 20), 0)
	For $i = 1 To $iQspaces
		$sRet &= " "
	Next
	$sRet &= ";"
	$sRet &= _Iif($iChar = 5 Or $iChar = 6, "Roman;", ";")
	$sRet &= __GCR_ConvertTwipsToSpaceUnit($iTab) & ";"
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, BitOR($PFM_NUMBERING, $PFM_NUMBERINGSTART, $PFM_NUMBERINGSTYLE))
	Return $sRet
EndFunc   ;==>_GUICtrlRichEdit_GetParaNumbering

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_GetParaShading
; Description....: Gets the shading settings of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GuiCtrlRichEdit_GetParaShading($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success -  settings of first selected paragraph - a string consisting of values separated by semicolons (:):
;                  |value 1 - Weight - percent of foreground color, the rest being background color
;                  |value 2 - style  - a string containing one of the following:
;                  |   non - none
;                  |   dhz - dark horizontal
;                  |   dvt - dark vertical
;                  |   ddd - dark down diagonal
;                  |   dud - dark up diagonal
;                  |   dgr - dark grid
;                  |   dtr - dark trellis
;                  |   lhz - light horizontal
;                  |   lvt - light vertical
;                  |   ldd - light down diagonal
;                  |   lud - light up diagonal
;                  |   lgr - light grid
;                  |   ltr - light trellis
;                  |value 3 - Foreground color - one of the following:
;                  |   "blk"   - black
;                  |   "blu"   - blue
;                  |   "cyn"   - cyan
;                  |   "grn"   - green
;                  |   "mag"   - magenta
;                  |   "red"   - red
;                  |   "yel"   - yellow
;                  |   "whi"   - white
;                  |   "dbl"   - dark blue
;                  |   "dgn"   - dark green
;                  |   "dmg"   - dark magenta
;                  |   "drd"   - dark red
;                  |   "dyl"   - dark yellow
;                  |   "dgy"   - dark grey
;                  |   "lgy"   - light grey
;                  |value 4 - Background color - same values as for Foreground color
;                  |Value 5 - scope:
;                  |   a - all (or only) selected paragraphs have these settings
;                  |   f - the first selected paragraph has these settings, but other(s) don't
;                  |   c - the current paragraph has these settings
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Shading does not show in Rich Edit, but shading created here will show in Word
; Related .......: _GUICtrlRichEdit_SetParaShading
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_GetParaShading($hWnd)
	Local Const $asStyles[13] = ["non", "dhz", "dvt", "ddd", "dud", "dgr", "dtr", "lhz", "lrt", "ldd", "lud", _
			"lgr", "ltr"]
	Local Const $asClrs[16] = ["blk", "blu", "cyn", "grn", "mag", "red", "yel", "whi", "dbl", "dgn", "dmg", _
			"drd", "dyl", "dgy", "lgy"]
	Local $iMask, $iWeight, $iS, $iN, $sRet, $tParaFormat

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, "")

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	If @error Then Return SetError(@error, 0, -1)

	$iMask = DllStructGetData($tParaFormat, "dwMask")
	$iWeight = DllStructGetData($tParaFormat, "wShadingWeight")
	$iS = DllStructGetData($tParaFormat, "wShadingStyle")

	$sRet = $iWeight & ";"
	$iN = BitAND($iS, 0xF)
	$sRet &= $asStyles[$iN] & ";"
	$iN = BitShift(BitAND($iS, 0xF0), 4)
	$sRet &= $asClrs[$iN] & ";"
	$iN = BitShift(BitAND($iS, 0xF00), 8)
	$sRet &= $asClrs[$iN] & ";"
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_SHADING)
	Return $sRet
EndFunc   ;==>_GuiCtrlRichEdit_GetParaShading

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetParaSpacing
; Description....: Gets the spacing settings of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GUICtrlRichEdit_GetParaSpacing($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - a string consisting of the settings separated by semicolons (;):
;                  |value 1 - inter-line spacing:
;                  |   either a number - in space units
;                  |   or "<number> lines" - in lines
;                  |Value 2 - scope of value 1:
;                  |   a - all (or only) selected paragraph(s) have the above setting
;                  |   f - the first selected paragraph has this setting, but other(s) don't
;                  |   c - the current paragraph has this setting
;                  |value 3 - spacing before paragraphs (in space units)
;                  |value 4 - scope of value 3 - see above
;                  |value 5 - spacing after paragraphs (in space units)
;                  |value 6 - scope of value 5 - see above
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
; Related .......: _GUICtrlRichEdit_SetParaSpacing, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetParaSpacing($hWnd)
	Local $tParaFormat, $iInter, $iRule, $iAfter, $iBefore, $sRet, $iMask

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	$iInter = DllStructGetData($tParaFormat, "dyLineSpacing")
	$iRule = DllStructGetData($tParaFormat, "bLineSpacingRule")
	Switch $iRule
		Case 0
			$sRet = "1 line;"
		Case 1
			$sRet = "1.5 lines;"
		Case 2
			$sRet = "2 lines;"
		Case 3, 4
			$sRet = __GCR_ConvertTwipsToSpaceUnit($iInter) & ";"
		Case 5
			$sRet = StringFormat("%.2f", $iInter / 20) & " lines;"
	EndSwitch
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_LINESPACING) & ";"

	$iBefore = DllStructGetData($tParaFormat, "dySpaceBefore")
	$sRet &= __GCR_ConvertTwipsToSpaceUnit($iBefore) & ";"
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_SPACEBEFORE) & ";"

	$iAfter &= DllStructGetData($tParaFormat, "dySPaceAfter")
	$sRet &= __GCR_ConvertTwipsToSpaceUnit($iAfter) & ";"
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_SPACEAFTER)
	Return $sRet
EndFunc   ;==>_GUICtrlRichEdit_GetParaSpacing

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetParaTabStops
; Description....: Gets the tabstops of (first) selected paragraph or (if no selection) of the current paragraph
; Syntax ........: _GUICtrlRichEdit_GetParaTabStops($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - A string consisting of values separated by ; (semicolons).
;                  |value 1 - number of tabstops
;                  |values 2 to <value 1> + 1 - description of a tabstop:
;                  | absolute position of a tab stop (in space units)
;                  | kind of tab
;                  |   l - left tab
;                  |   c - center tab
;                  |   r - decimal tab
;                  |   b - bar tab (a vertical bar, as in Word)
;                  | kind of dot leader
;                  |   . - dotted leader
;                  |   - - dashed leader
;                  |   _ - underline leader
;                  |   = - double line leader
;                  |   t - thick-line leader
;                  |   a space - no leader
;                  |Value <value 1> + 2 - scope:
;                  |   a - all (or only) selected paragraphs have these settings
;                  |   f - the first selected paragraph has these settings, but other(s) don't
;                  |   c - the current paragraph has these settings
;                  Failure - "" and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
;+
;                  To enter a tab into a control, press Ctrl_Tab
; Related .......:  _GUICtrlRichEdit_SetTabStops, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_GETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetParaTabStops($hWnd)
	Local $iQtabs, $sRet = "", $iMask, $iN, $iM, $tParaFormat
	Local Const $asKind[5] = ["l", "c", "r", "d", "b"], $asLeader[6] = [" ", ".", "-", "_", "t", "="]
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))
	__GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)

	$iMask = DllStructGetData($tParaFormat, "dwMask")
	$iQtabs = DllStructGetData($tParaFormat, "cTabCount")
	$sRet = $iQtabs & ";"
	For $i = 1 To $iQtabs
		$iN = DllStructGetData($tParaFormat, "rgxTabs", $i)
		$sRet &= __GCR_ConvertTwipsToSpaceUnit(BitAND($iN, 0xFFFFF))
		$iM = BitAND(BitShift($iN, 24), 0xF)
		$sRet &= $asKind[$iM]
		$iM = BitAND(BitShift($iN, 28), 0xF)
		$sRet &= $asLeader[$iM] & ";"
	Next
	$sRet &= __GCR_GetParaScopeChar($hWnd, $iMask, $PFM_TABSTOPS)
	Return $sRet
EndFunc   ;==>_GUICtrlRichEdit_GetParaTabStops

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_GetPasswordChar
; Description ...: Gets the password character that a rich edit control displays when the user enters text
; Syntax.........: _GUICtrlEdit_GetPasswordChar($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - The character to be displayed in place of any characters typed by the user
;                  |Special case: 0 - there is no password character, so the control displays the characters typed by the user
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Gary Frost
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_SetPasswordChar
; Link ..........; @@MsdnLink@@ EM_GETPASSWORDCHAR
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetPasswordChar($hWnd)
	Local $n
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$n = __GCR_SendMessage($hWnd, $EM_GETPASSWORDCHAR)
	Return _Iif($n = 0, "", Chr($n))
EndFunc   ;==>_GUICtrlRichEdit_GetPasswordChar

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetScrollPos
; Description ...: Gets the Scrolling position of the control
; Syntax.........: __GUICtrlRichEdit_GetScrollPos($hWnd, ByRef $iX, ByRef $iY)
; Parameters ....: $hWnd        - Handle to the control
;                  $iX - x coordinate of scrolling position (Returned)
;                  $iY - x coordinate of scrolling position (Returned)
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: unknown
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The scrolling position is the upper left corner of the control
; Related .......: _GUICtrlRichEdit_SetScrollingPos
; Link ..........; @@MsdnLink@@ EM_GETSCROLLPOS
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetScrollPos($hWnd)
	Local $tPoint
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tPoint = DllStructCreate($tagPOINT)
	__GCR_SendMessage($hWnd, $EM_GETSCROLLPOS, 0, DllStructGetPtr($tPoint))
	Return DllStructGetData($tPoint, "x") & ";" & DllStructGetData($tPoint, "y")
EndFunc   ;==>_GUICtrlRichEdit_GetScrollPos

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetSel
; Description ...: Gets the low and high inter-character positions of a selection
; Syntax.........: _GUICtrlRichEdit_GetSel($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success - a string in the format "<low>;<high>"
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The first character of the text is after inter-character position 0.
;                  If high = low, no text is selected
; Related .......: _GUICtrlRichEdit_GetSel, _GUICtrlRichEdit_GetSelAA
; Link ..........; @@MsdnLink@@ EM_EXGETSEL
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetSel($hWnd)
	Local $tCharRange
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$tCharRange = DllStructCreate($tagCHARRANGE)
	__GCR_SendMessage($hWnd, $EM_EXGETSEL, 0, DllStructGetPtr($tCharRange))
	Return DllStructGetData($tCharRange, 1) & ";" & DllStructGetData($tCharRange, 2)
EndFunc   ;==>_GUICtrlRichEdit_GetSel

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetSelAA
; Description ...: Gets the anchor and active inter-character positions of a selection, in that order
; Syntax.........: _GUICtrlRichEdit_GetSelAA($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success - a string in the format "<anchor>;<active>"
;                  Failure - False and sets @error:
;                  |-1  - no text is selected
;                  |101 - $hWnd is not a handle
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The first character of the text is after inter-character position 0.
; Related .......: _GUICtrlRichEdit_GetSel
; Link ..........; @@MsdnLink@@ EM_EXGETSEL
; Example .......: Yes
;
; ===============================================================================================================================
Func _GuiCtrlRichEdit_GetSelAA($hWnd)
	Local $aiLowHigh[3], $aiNoSel[3], $iAnchor, $iActive, $tCharRange, $tCharRange2
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tCharRange = DllStructCreate($tagCHARRANGE)
	__GCR_SendMessage($hWnd, $EM_EXGETSEL, 0, DllStructGetPtr($tCharRange))
	$aiLowHigh[1] = DllStructGetData($tCharRange, 1)
	$aiLowHigh[2] = DllStructGetData($tCharRange, 2)

	If $aiLowHigh[2] = $aiLowHigh[1] Then Return SetError(-1, 0, False)

	__GCR_SendMessage($hWnd, $EM_SETSEL, -1, 0) ; deselect

	$tCharRange2 = DllStructCreate($tagCHARRANGE)
	__GCR_SendMessage($hWnd, $EM_EXGETSEL, 0, DllStructGetPtr($tCharRange2))
	$aiNoSel[1] = DllStructGetData($tCharRange2, 1)
	$aiNoSel[2] = DllStructGetData($tCharRange2, 2)

	If $aiLowHigh[1] = $aiNoSel[1] Then ; if active < anchor
		$iAnchor = $aiLowHigh[2]
		$iActive = $aiLowHigh[1]
	Else
		$iAnchor = $aiLowHigh[1]
		$iActive = $aiLowHigh[2]
	EndIf
	__GCR_SendMessage($hWnd, $EM_SETSEL, $aiLowHigh[1], $aiLowHigh[2])
	Return $iAnchor & ";" & $iActive
EndFunc   ;==>_GuiCtrlRichEdit_GetSelAA

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_GetSelText
; Description ...: Retrieves the currently selected text
; Syntax.........: _GUICtrlRichEdit_GetSelText($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success - the selected text
;                  Failure - False and sets @error:
;                  |-1  - no text is selected
;                  |101 - $hWnd is not a handle
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_GetSel
; Link ..........; @@MsdnLink@@ EM_EXGETSELTEXT
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_GetSelText($hWnd)
	Local $tText, $aiLowHigh

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then Return SetError(-1, 0, -1)

	$aiLowHigh = StringSplit(_GUICtrlRichEdit_GetSel($hWnd), ";")
	$tText = DllStructCreate("wchar[" & $aiLowHigh[2] - $aiLowHigh[1] + 1 & "]")
	__GCR_SendMessage($hWnd, $EM_GETSELTEXT, 0, DllStructGetPtr($tText))
	Return DllStructGetData($tText, 1)
EndFunc   ;==>_GUICtrlRichEdit_GetSelText

;#FUNCTION# ;===============================================================================
;
; Name...........: _GuiCtrlRichEdit_GetSpaceUnit
; Description ...: Gets the unit of measure of horizontal and vertical space used in parameters of various _GuiCtrlRichEdit functions
; Syntax.........: _GuiCtrlRichEdit_GetSpaceUnit()
; Parameters ....: none
; Return values .: Success - "in", "cm", "mm", "pt" (points), or "tw" (twips, 1/1440 inches, 1/567 centimetres)
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: Initially, space is measured in inches
; Related .......: _GuiCtrlRichEdit_SetSpaceUnit
; Link ..........;
; Example .......: Yes
;
;===============================================================================================================================
Func _GuiCtrlRichEdit_GetSpaceUnit()
	Switch $_GRE_TwipsPeSpaceUnit
		Case 1440
			Return "in"
		Case 567
			Return "cm"
		Case 56.7
			Return "mm"
		Case 20
			Return "pt"
		Case 1
			Return "tw"
	EndSwitch
EndFunc   ;==>_GuiCtrlRichEdit_GetSpaceUnit

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetTextInLine
; Description....: Gets a line of text
; Syntax ........: _GUICtrlRichEdit_GetTextInLine($hWnd, $iLine)
; Parameters.....: $hWnd		- Handle to the control
;                  $iLine    - Line number
; Return values..: Success - the text
;                  |Failure - False, and sets @error:
;                  |101  - $hWnd is not a handle
;                  |1021 - $iLine is not a positive number
;                  |1022 - $iLine exceeds number of lines in control
;                  |700  - internal error
; Authors........: Gary Frost (gafrost (custompcs@charter.net))
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: The first line in a control  is 1
; Related .......: _GuiCtrlRichEdit_GetSel
; Link ..........: @@MsdnLink@@ EM_GETLINE
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetTextinLine($hWnd, $iLine)
	Local $iLen, $tBuffer, $iResult, $tString
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iLine, ">0") Then Return SetError(1021, 0, False)
	If $iLine > _GUICtrlRichEdit_GetLineCount($hWnd) Then Return SetError(1022, 0, False)

	$iLen = _GUICtrlRichEdit_GetLineLength($hWnd, $iLine)
	$tBuffer = DllStructCreate("short Len;wchar Text[" & $iLen + 2 & "]")
	DllStructSetData($tBuffer, "Len", $iLen + 2)
	If $iLine <> -1 Then $iLine -= 1
	$iResult = __GCR_SendMessage($hWnd, $EM_GETLINE, $iLine, DllStructGetPtr($tBuffer), 10, "wparam", "ptr")
	If $iResult = 0 Then Return SetError(700, 0, False)
	$tString = DllStructCreate("char Text[" & $iLen + 1 & "]", DllStructGetPtr($tBuffer))
	Return StringLeft(DllStructGetData($tString, "Text"), $iLen)
EndFunc   ;==>_GUICtrlRichEdit_GetTextinLine

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_GetTextInRange()
; Description....: Gets the text from from one inter-character position to another
; Syntax ........: _GUICtrlRichEdit_GetTextRange($hWnd, $iStart, $iEnd)
; Parameters.....: $hWnd		- Handle to the control
;                  $iStart    - Inter-character position before the text
;                  $iEnd       - Inter-character position after the text
;                  |Special value: -1 - end of text
; Return values..: Success - the text
;                  |Failure - False, and sets @error:
;                  |101  - $hWnd is not a handle
;                  |102 - $iStart is neither positive nor zero
;                  |1031 - $iEnd is neither positive nor zero nor -1
;                  |1032 - $iStart < $iEnd and $iEnd <> -1
; Authors........: Gary Frost (gafrost (custompcs@charter.net))
; Modified ......: Prog@ndy, Chris Haslam (c.haslam)
; Remarks .......: The first character position in the control is 0
; Related .......: _GuiCtrlRichEdit_GetInLine
; Link ..........: @@MsdnLink@@ EM_GETTEXTRANGE
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_GetTextInRange($hWnd, $iStart, $iEnd)
	Local $iLen, $tText, $tTextRange, $sUnicode = ""

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iStart, ">=0") Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iEnd, ">=0,-1") Then Return SetError(1031, 0, False)
	If Not ($iEnd > $iStart Or $iEnd = -1) Then Return SetError(1032, 0, False)

	$sUnicode = "w"
	;	If @AutoItUnicode Then $sUnicode = "w"
	$iLen = _GUICtrlRichEdit_GetCtrlTextLength($hWnd) ; can't use $iEnd - $iStart + 1 because of Unicode
	$tText = DllStructCreate($sUnicode & "char[" & ($iLen + 4) & "]")
	$tTextRange = DllStructCreate($tagTEXTRANGE)
	DllStructSetData($tTextRange, 1, $iStart)
	DllStructSetData($tTextRange, 2, $iEnd)
	DllStructSetData($tTextRange, 3, DllStructGetPtr($tText))
	__GCR_SendMessage($hWnd, $EM_GETTEXTRANGE, 0, DllStructGetPtr($tTextRange), 0, "wparam", "ptr")
	Return DllStructGetData($tText, 1)
EndFunc   ;==>_GUICtrlRichEdit_GetTextInRange

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GuiCtrlRichEdit_GetVersion
; Description ...: Retrieves the version of Rich Edit
; Syntax.........: _GuiCtrlRichEdit_GetVersion()
; Parameters ....: none
; Return values .: Version of Rich Edit
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: The versions of Rich Edit included in versions of Windows are:
;                  3.0 - Windows 2000, XP
;                  3.1 - Windows Vista
;                  4.0 - Windows XP SP1
;                  4.1 - Windows Vista
; Related .......:
; Link ..........;
; Example .......: Yes
;
; ===============================================================================================================================
Func _GuiCtrlRichEdit_GetVersion()
	Return $_GRE_Version
EndFunc   ;==>_GuiCtrlRichEdit_GetVersion

; #FUNCTION# ;====================================================================================================================
;
; Name...........: _GuiCtrlRichEdit_GetXYFromCharPos
; Description ...: Retrieves the XY coordinates of an inter-character position
; Syntax.........: _GuiCtrlRichEdit_GetXYFromCharPos($hWnd, $iCharPos)
; Parameters ....: $hWnd        - Handle to the control
;                  $iCharPos    - Inter-character position
; Return values .: Success _ "<x>;<y>" - coordinates of the inter-character position, relative to the top-left corner of the client area
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - $iCharPos is neither a positive number nor zero
;                  |1022 - $iCharPos exceeds the number of characters in the control
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: The first inter-character position is numbered 0
;+
;                  With a multi-line control, coordinates are returned even for inter-character positions
;                  that are not visible.
; Related .......:
; Link ..........; @@MsdnLink@@ EM_POSFROMCHAR
; Example .......: Yes
;
; ===============================================================================================================================
Func _GuiCtrlRichEdit_GetXYFromCharPos($hWnd, $iCharPos)
	Local $tPoint
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iCharPos, ">=0") Then Return SetError(1021, 0, False)
	If $iCharPos > _GUICtrlRichEdit_GetCtrlTextLength($hWnd) Then Return SetError(1022, 0, False)

	$tPoint = DllStructCreate($tagPOINT)
	__GCR_SendMessage($hWnd, $EM_POSFROMCHAR, DllStructGetPtr($tPoint), $iCharPos, 0, "ptr", "lparam")
	Return DllStructGetData($tPoint, "X") & ";" & DllStructGetData($tPoint, "Y")
EndFunc   ;==>_GuiCtrlRichEdit_GetXYFromCharPos

; #FUNCTION# ;=====================================================================================
;
; Name...........: _GuiCtrlRichEdit_GotoCharPos
; Description ...: Moves the insertion point to an inter-character position
; Syntax.........: _GuiCtrlRichEdit_GotoCharPos($hWnd, $iCharPos)
; Parameters ....: $hWnd        - Handle to the control
;                  $iCharPos  the inter-character position
;                  |Special value: -1 - end of text
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iCharPos is neither a positive number nor zero nor -1
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: The first character of the text in a control is at character position 1
;+
;                  Cancels text selection (if any)
; Related .......: _GUICtrlRichEdit_GetSel, _GuiCtrlRichEdit_Deselect, _GuiCtrlRichEdit_IsTextSelected, _GUICtrlRichEdit_SetSel
; Link ..........; @@MsdnLink@@ EM_EXSETSEL
; Example .......: Yes
;
; =================================================================================================
Func _GuiCtrlRichEdit_GotoCharPos($hWnd, $iCharPos)
	_GUICtrlRichEdit_SetSel($hWnd, $iCharPos, $iCharPos)
	If @error Then Return SetError(@error, 0, False)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_GotoCharPos

; #FUNCTION# ====================================================================================================================
;
; Name...........: _GUICtrlRichEdit_HideSelection
; Description ...: Hides (or shows) a selection
; Syntax.........: _GUICtrlRichEdit_HideSelection($hWnd[, $fHide = True])
; Parameters ....: $hWnd        - Handle to the control
;                  $fHide - = True - hide
;                  | = False - show
;                  | Default: hide
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fHide must be True or False
; Author ........: Prog@ndy
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_GetSel, _GuiCtrlRichEdit_SetSel
; Link ..........; @@MsdnLink@@ EM_HIDESELECTION
; Example .......: Yes
;
; ===============================================================================================================================
Func _GUICtrlRichEdit_HideSelection($hWnd, $fHide = True)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fHide) Then Return SetError(102, 0, False)

	__GCR_SendMessage($hWnd, $EM_HIDESELECTION, $fHide, 0)
EndFunc   ;==>_GUICtrlRichEdit_HideSelection

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_InsertText()
; Description....: Inserts text at insertion point or anchor point of selection
; Syntax ........: _GUICtrlRichEdit_InsertText($hWnd, $sText)
; Parameters.....: hWnd		- Handle to the control
;                  $sText    - Text to be inserted
; Return values..: Succcess - True
;                  Failure - False, and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $sText = ""
; Authors........: Gary Frost (gafrost (custompcs@charter.net)
; Modified ......: Prog@ndy, Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_AppendText, _GUICtrlRichEdit_ReplaceSel, _GUICtrlRichEdit_SetText
; Link ..........: @@MsdnLink@@ EM_SETTEXTEX
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_InsertText($hWnd, $sText)
	Local $tSetText

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If $sText = "" Then Return SetError(102, 0, False)

	$tSetText = DllStructCreate($tagSETTEXTEX)
	DllStructSetData($tSetText, 1, $ST_SELECTION)
	_GuiCtrlRichEdit_Deselect($hWnd)
	If StringLeft($sText, 5) <> "{\rtf" And StringLeft($sText, 5) <> "{urtf" Then
		DllStructSetData($tSetText, 2, $CP_UNICODE)
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "wstr")
	Else
		DllStructSetData($tSetText, 2, $CP_ACP)
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "str")
	EndIf
	If @error Then Return SetError(@error, 0, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_InsertText

; #FUNCTION# ====================================================================================================================
; Name...........: _GuiCtrlRichEdit_IsModified
; Description ...: Retrieves the state of a rich edit control's modification flag
; Syntax.........: _GuiCtrlRichEdit_IsModified($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      - True or False
;                  Failure - Sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The system automatically sets the modification flag to False when the control is created.
;                  If the control's text is changed, either by the user or programmatically, the system sets the flag to True
;                  You can call _GUICtrlEdit_SetModify to set or clear the flag
; Related .......: _GUICtrlRichEdit_SetModified
; Link ..........; @@MsdnLink@@ EM_GETMODIFY
; Example .......; Yes
; ===============================================================================================================================

Func _GuiCtrlRichEdit_IsModified($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	Return __GCR_SendMessage($hWnd, $EM_GETMODIFY) <> 0
EndFunc   ;==>_GuiCtrlRichEdit_IsModified

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_IsTextSelected
; Description....: Is text selected?
; Syntax ........: _GUICtrlRichEdit_IsTextSelected($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Succcess - True or False
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_GetSel, _GUICtrlRichEdit_SetSel
; Link ..........: @@MsdnLink@@ EM_EXGETSEL
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_IsTextSelected($hWnd)
	Local $tCharRange
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$tCharRange = DllStructCreate($tagCHARRANGE)
	__GCR_SendMessage($hWnd, $EM_EXGETSEL, 0, DllStructGetPtr($tCharRange))
	Return DllStructGetData($tCharRange, 2) <> DllStructGetData($tCharRange, 1)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_IsTextSelected

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_Paste
; Description....: Paste RTF or RTF with Objects from clipboard
; Syntax ........: _GUICtrlRichEdit_Paste($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: Pastes with objects if available, else without
; Related .......: _GUICtrlRichEdit_CanPaste, _GuiCtrlRichEdit__PasteSpecial, _GuiCtrlRichEdit__Cut, __GuiCtrlRichEdit_Copy
; Link ..........: @@MsdnLink@@ WM_PASTE
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_Paste($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $WM_PASTE, 0, 0)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_Paste

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_PasteSpecial
; Description....: Paste RTF or RTF and Objects from clipboard
; Syntax ........: _GUICtrlRichEdit_PasteSpecial($hWnd, $fAndObjects = True)
; Parameters.....: hWnd		- Handle to the control
;                  $fAndObjects - Paste objects as well as RTF - True (default) or False
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fAndObjects is neither True nor False
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_CanPasteSpecial, _GuiCtrlRichEdit__Paste , _GuiCtrlRichEdit__Cut, __GuiCtrlRichEdit_Copy
; Link ..........: @@MsdnLink@@ WM_PASTESPECIAL
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_PasteSpecial($hWnd, $fAndObjects = True)
	Local $iN
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	$iN = _Iif($fAndObjects, $_GRE_CF_RETEXTOBJ, $_GRE_CF_RTF)
	__GCR_SendMessage($hWnd, $EM_PASTESPECIAL, $iN, 0)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_PasteSpecial

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_PauseRedraw
; Description....: Pauses redrawing of the control
; Syntax ........: _GUICtrlRichEdit_PauseRedraw($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: unknown
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_ResumeRedraw
; Link ..........: @@MsdnLink@@ WM_SETREDRAW
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_PauseRedraw($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $WM_SETREDRAW, False, 0)
EndFunc   ;==>_GUICtrlRichEdit_PauseRedraw

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_Redo
; Description....: Redoes last undone action
; Syntax ........: _GUICtrlRichEdit_PauseRedraw($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_Undo, _GUICtrlRichEdit_CanRedo, _GuiCtrlRichEdit_GetNextRedo
; Link ..........: @@MsdnLink@@ EM_REDO
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_Redo($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_REDO, 0, 0) <> 0
EndFunc   ;==>_GUICtrlRichEdit_Redo

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_ReplaceText
; Description....: Replaces selected text
; Syntax ........: _GuiCtrlRichEdit_ReplaceText($hWnd, $sText[, $fCanUndo = True])
; Parameters.....: $hWnd		- Handle to the control
;                  $sText     - Replacement text
;                  $fCanUndo  - Can operation can be undone? True (Default) or False
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |103 - $fCanUndo must be True or False
;                  |-1  - no text is selected
; Authors........: Gary Frost (gafrost)
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_InsertText, _GUICtrlRichEdit_SetText, _GuiCtrlRichEdit_Undo
; Link ..........: @@MsdnLink@@ EM_REPLACESEL
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_ReplaceText($hWnd, $sText, $fCanUndo = True)
	Local $tMemMap, $tText, $ptrBuf
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fCanUndo) Then Return SetError(103, 0, False)
	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then Return SetError(-1, 0, False)

	$tText = DllStructCreate("char Text[" & StringLen($sText) + 1 & "]")
	DllStructSetData($tText, "Text", $sText)
	$ptrBuf = DllStructGetPtr($tText)
	_MemInit($hWnd, StringLen($sText) + 1, $tMemMap)
	_MemWrite($tMemMap, $ptrBuf)
	__GCR_SendMessage($hWnd, $EM_REPLACESEL, $fCanUndo, $ptrBuf, 0, "wparam", "ptr")
	_MemFree($tMemMap)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_ReplaceText

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_ResumeRedraw
; Description....: Resumes redrawing of the control
; Syntax ........: _GUICtrlRichEdit_ResumeRedraw($hWnd)
; Parameters.....: hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: unknown
; Modified ......:
; Remarks .......:
; Related .......: _GUICtrlRichEdit_PauseRedraw
; Link ..........: @@MsdnLink@@ WM_SETREDRAW
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_ResumeRedraw($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $WM_SETREDRAW, True, 0)
	Return __GCR_InvalidateRect($hWnd)
EndFunc   ;==>_GUICtrlRichEdit_ResumeRedraw

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_ScrollLineOrPage
; Description ...: Scrolls the text down or up a line or a page
; Syntax.........: _GUICtrlRichEdit_ScrollLineOrPage($hWnd, $sAction)
; Parameters ....: $hWnd        - Handle to the control
;                  $sAction     - one of the following:
;                  |"ld" - line down
;                  |"lu" - line up
;                  |"pd" - page down
;                  |"pu" - page up
; Return values .: Success      -  the number of lines actually scrolled (positive if down)
;                  Failure      -  False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |1021 - $sAction is not two characters
;                  |1022 - first character must be l or p
;                  |1023 - second character must be d or u
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: This function is well behaved: it never scrolls such that lines beyond the last one are shown.
; Related .......: _GUICtrlEdit_ScrollLines, _GuiCtrlRichEdit_ScrollToCaret
; Link ..........; @@MsdnLink@@ EM_SCROLL
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_ScrollLineOrPage($hWnd, $sAction)
	Local $sCh, $iWparam, $iRet
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If StringLen($sAction) <> 2 Then Return SetError(1021, 0, False)
	$sCh = StringLeft($sAction, 1)
	If Not ($sCh = "l" Or $sCh = "p") Then Return SetError(1022, 0, False)
	$sCh = StringRight($sAction, 1)
	If Not ($sCh = "d" Or $sCh = "u") Then Return SetError(1023, 0, False)

	Switch $sAction
		Case "ld"
			$iWparam = $SB_LINEDOWN
		Case "lu"
			$iWparam = $SB_LINEUP
		Case "pd"
			$iWparam = $SB_PAGEDOWN
		Case "pu"
			$iWparam = $SB_PAGEUP
	EndSwitch
	$iRet = __GCR_SendMessage($hWnd, $EM_SCROLL, $iWparam, 0)
	$iRet = BitAND($iRet, 0xFFFF) ; low word
	If BitAND($iRet, 0x8000) <> 0 Then $iRet = BitOR($iRet, 0xFFFF0000) ; extend sign bit
	Return $iRet
EndFunc   ;==>_GUICtrlRichEdit_ScrollLineOrPage

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_ScrollLines
; Description ...: Scrolls the text down or up a number of lines
; Syntax.........: _GUICtrlRichEdit_ScrollLines($hWnd, $iQlines)
; Parameters ....: $hWnd        - Handle to the control
;                  $iQlines     - number of lines to scroll
; Return values .: Success      -  True
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iQlines is not numeric
;                  |700 - attempt to scroll a single-line control
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: To scroll down, set $iQlines to a positive value; to scroll up, a negative value.
;+
;                  _GUICtrlRichEdit_ScrollLines is well behaved: if $iQlines is more than the number of lines below
;                  the current line, it scrolls to show only the last line; if $iQlines is negative and specifies
;                  a line before the first one, it scrolls to show the first line at the top of the control window.
; Related .......: _GUICtrlEdit_ScrollLineOrPage, _GuiCtrlRichEdit_ScrollToCaret
; Link ..........; @@MsdnLink@@ EM_LINESCROLL
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_ScrollLines($hWnd, $iQlines)
	Local $fRet
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iQlines) Then SetError(102, 0, False)

	$fRet = __GCR_SendMessage($hWnd, $EM_LINESCROLL, 0, $iQlines) <> 0
	If Not $fRet Then Return SetError(700, 0, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_ScrollLines

; #FUNCTION# ====================================================================================================================
; Name...........: _GuiCtrlRichEdit_ScrollToCaret
; Description ...: Scrolls to show line on which caret (insertion point) is
; Syntax.........: _GuiCtrlRichEdit_ScrollToCaret($hWnd)
; Parameters ....: $hWnd        - Handle to the control
; Return values .: Success      -  True
;                  Failure      -  False and sets @error:
;                  |101 - $hWnd is not a handle
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......:
; Related .......: _GUICtrlEdit_ScrollLineOrPage, _GUICtrlRichEdit_ScrollLines
; Link ..........; @@MsdnLink@@ EM_SCROLLCARET
; Example .......; Yes
; ===============================================================================================================================

Func _GuiCtrlRichEdit_ScrollToCaret($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	__GCR_SendMessage($hWnd, $EM_SCROLLCARET, 0, 0)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_ScrollToCaret

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_SetCharAttributes
; Description....: Turns an attribute on or off for selected text or, if none selected, for text inserted at the insertion point
; Syntax ........: _GuiCtrlRichEdit_SetCharEffects($hWnd, $sStatesAndEffects[, $fWord = False])
; Parameters.....: $hWnd		- Handle to the control
;                  $sStatesAndAtts -  a string consisting of three character groups: + (or -) for the state, and a two-letter abbreviation for the attribute
;                  |   first character: + for on, - for off
;                  |   second and third character: any of:
;                  |      bo - bold
;                  |      di - disable - displays characters with a shadow [nd]
;                  |      em - emboss [nd]
;                  |      hi - hide, i.e. don't display
;                  |      im - imprint [nd]
;                  |      it - italcize
;                  |      li - send EN_LINK messages when mouse is over text with this attribute
;                  |      ou - outline [nd]
;                  |      pr - send EN_PROTECT when user attempts to modify
;                  |      re - mark as revised [nd]
;                  |      sh - shadow [nd]
;                  |      sm - small capital letters [nd]
;                  |      st - strike out
;                  |      sb - subscript [nd]
;                  |      sp - superscript [nd]
;                  |      un - underline
;                  $fWord (Optional)
;                  | True
;                  |   If text is selected, apply the attribute to whole words in the selected text
;                  |   If not:
;                  |      If the insertion point is in a word, or at the end of it, apply the attribute to the word
;                  |      If not, apply the attribute to text inserted at the insertion point
;                  | False (Default)
;                  |   If text is selected, apply the attribute to the selected text
;                  |   If not, apply the attribute to text inserted at the insertion point
; Return values..: Success - True
;                  |101 - $hWnd is not a handle
;                  Failure - False and sets @error:
;                  |1021 - length of $sStatesAndAtts is not  multiple of 3
;                  |1022 - first character of group not + or -. The character is in @extended
;                  |1023 - an abbreviation for an attribute is invalid. It is in @extended
;                  |103  - $fWord must be True or False
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Some attributes do not display; they are marked with [nd] above.
; Related .......: _GuiCtrlRichEdit_GetCharAttributes
; Link ..........: @@MsdnLink@@ EM_SETCHARFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetCharAttributes($hWnd, $sStatesAndAtts, $fWord = False)
	Local $iMask = 0, $iEffects = 0, $n, $s, $tCharFormat, $iWparam, $fResult
	Local Const $av[16][3] = [ _
			["bo", $CFM_BOLD, $CFE_BOLD],["di", $CFM_DISABLED, $CFE_DISABLED], _
			["em", $CFM_EMBOSS, $CFE_EMBOSS],["hi", $CFM_HIDDEN, $CFE_HIDDEN], _
			["im", $CFM_IMPRINT, $CFE_IMPRINT],["it", $CFM_ITALIC, $CFE_ITALIC], _
			["li", $CFM_LINK, $CFE_LINK],["ou", $CFM_OUTLINE, $CFE_OUTLINE], _
			["pr", $CFM_PROTECTED, $CFE_PROTECTED],["re", $CFM_REVISED, $CFE_REVISED], _
			["sh", $CFM_SHADOW, $CFE_SHADOW],["sm", $CFM_SMALLCAPS, $CFE_SMALLCAPS], _
			["st", $CFM_STRIKEOUT, $CFE_STRIKEOUT],["sb", $CFM_SUBSCRIPT, $CFE_SUBSCRIPT], _
			["sp", $CFM_SUPERSCRIPT, $CFE_SUPERSCRIPT],["un", $CFM_UNDERLINE, $CFE_UNDERLINE]]

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fWord) Then Return SetError(103, 0, False)

	For $i = 1 To StringLen($sStatesAndAtts) Step 3
		$s = StringMid($sStatesAndAtts, $i + 1, 2)
		$n = -1
		For $j = 0 To UBound($av) - 1
			If $av[$j][0] = $s Then
				$n = $j
				ExitLoop
			EndIf
		Next
		If $n = -1 Then Return SetError(1023, $s, False) ; not found
		$iMask = BitOR($iMask, $av[$n][1])
		$s = StringMid($sStatesAndAtts, $i, 1)
		Switch $s
			Case "+"
				$iEffects = BitOR($iEffects, $av[$n][2])
			Case "-"
				; do nothing
			Case Else
				Return SetError(1022, $s, False)
		EndSwitch
	Next
	$tCharFormat = DllStructCreate($tagCHARFORMAT)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	DllStructSetData($tCharFormat, 2, $iMask)
	DllStructSetData($tCharFormat, 3, $iEffects)
	$iWparam = _Iif($fWord, BitOR($SCF_WORD, $SCF_SELECTION), $SCF_SELECTION)
	$fResult = __GCR_SendMessage($hWnd, $EM_SETCHARFORMAT, $iWparam, DllStructGetPtr($tCharFormat)) <> 0
	If @error Then Return SetError(@error, 0, "")
	Return $fResult
EndFunc   ;==>_GuiCtrlRichEdit_SetCharAttributes

#cs
	; #FUNCTION# ;===============================================================================
	;
	; Name ..........: _GUICtrlRichEdit_SetCharBkColor
	; Description....: Sets the background color of selected text or, if none selected, sets the background color of text inserted at the insertion point
	; Syntax ........: _GUICtrlRichEdit_SetCharBkColor($hWnd[, $iColor])
	; Parameters.....: $hWnd		- Handle to the control
	;                  $vColor - one of the following: (Optional)
	;                  |a number - a COLORREF value
	;                  |"sys" - the system color (default)
	;                  |RGB percentages in the format "<red>;<green>;<blue>"
	;                  |  If a value is omitted, the previous value is used
	; Return values..: Success - True
	;                  Failure - False and sets @error
	;                  |101 - $hWnd is not a handle
	;                  |1021 - $vColor: not 2 semicolons
	;                  |1022 - $vColor: value not 0 to 100
	;                  |1023 - $vColor: invalid character
	; Authors........: Chris Haslam (c.haslam)
	; Modified ......:
	; Remarks .......:
	; Related .......: _GUICtrlRichEdit_GetCharBkColor
	; Link ..........: @@MsdnLink@@ EM_SETCHARFORMAT
	; Example .......: Yes
	;
	;===============================================================================
	Func _GUICtrlRichEdit_SetCharBkColor($hWnd, $vColor = "sys")
	; Commented out because, while it does round-trip with _GUICtrlRichEdit_GetCharBkColor, coloring doesn't show
	Local $tCharFormat, $iColor = 0, $av, $avOld

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tCharFormat = DllStructCreate($tagCHARFORMAT2)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	Switch True
	Case StringIsDigit($vColor)
	$iColor = $vColor
	Case $vColor = "Sys"
	DllStructSetData($tCharFormat, 3, $CFE_AUTOBACKCOLOR)
	Case Else
	$av = StringSplit($vColor, ";")
	If $av[0] <> 3 Then Return SetError(1021, 0, False)
	$avOld = StringSplit(_GUICtrlRichEdit_GetCharBkColor($hWnd), ";")
	For $i = 1 To $av[0]
	If StringIsDigit($av[$i]) Then
	If $av[$i] < 0 Or $av[$i] > 100 Then Return SetError(1022, 0, False)
	ElseIf $av[$i] = "" Then
	$av[$i] = $avOld[$i + 1]
	Else
	Return SetError(1023, 0, False)
	EndIf
	$iColor = BitOR($iColor, BitShift($av[$i] * 255 / 100 + 0.5, -8 * ($i - 1)))
	Next
	EndSwitch
	DllStructSetData($tCharFormat, 2, $CFM_BACKCOLOR)
	DllStructSetData($tCharFormat, 12, $iColor)
	Return __GCR_SendMessage($hWnd, $EM_SETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($tCharFormat)) <> 0
	EndFunc   ;==>_GUICtrlRichEdit_SetCharBkColor
#ce

#cs
	; #FUNCTION# ;===============================================================================
	;
	; Name ..........: _GUICtrlRichEdit_SetCharColor
	; Description....: Sets the color of selected text or, if none selected, sets the background color of text inserted at the insertion point
	; Syntax ........: _GUICtrlRichEdit_SetCharColor($hWnd[, $iColor])
	; Parameters.....: $hWnd		- Handle to the control
	;                  $vCOlor - one of the following: (Optional)
	;                  |a number - a COLORREF value
	;                  |"sys" - the system color (default)
	;                  |RGB percentages in the format "<red>;<green>;<blue>"
	;                  |  If a value is omitted, the previous value is used
	; Return values..: Success - True
	;                  Failure - False and sets @error:
	;                  |101 - $hWnd is not a handle
	;                  |1021 - $vColor: not 3 semicolons
	;                  |1022 - $vColor: value not 0 to 100
	;                  |1023 - $vColor: invalid character
	; Authors........: Chris Haslam (c.haslam)
	; Modified ......:
	; Remarks .......:
	; Related .......: _GUICtrlRichEdit_GetCharColor
	; Link ..........: @@MsdnLink@@ EM_SETCHARFORMAT
	; Example .......: Yes
	;
	;===============================================================================
	Func _GUICtrlRichEdit_SetCharColor($hWnd, $vColor = "sys")
	; Commented out because, while it does round-trip with _GUICtrlRichEdit_GetCharColor, coloring doesn't show
	Local $tCharFormat, $iColor = 0, $av, $avOld

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tCharFormat = DllStructCreate($tagCHARFORMAT)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))
	Switch True
	Case StringIsDigit($vColor)
	$iColor = $vColor
	Case $vColor = "Sys"
	DllStructSetData($tCharFormat, 3, $CFE_AUTOCOLOR)
	Case Else
	$av = StringSplit($vColor, ";")
	If $av[0] <> 3 Then Return SetError(1021, 0, False)
	$avOld = StringSplit(_GUICtrlRichEdit_GetCharColor($hWnd), ";")
	For $i = 1 To $av[0]
	If StringIsDigit($av[$i]) Then
	If $av[$i] < 0 Or $av[$i] > 100 Then Return SetError(1022, 0, False)
	ElseIf $av[$i] = "" Then
	$av[$i] = $avOld[$i + 1]
	Else
	Return SetError(1023, 0, False)
	EndIf
	$iColor += BitShift($av[$i] * 255 / 100 + 0.5, -8 * ($i - 1))
	Next
	EndSwitch
	consdebug("Set:$iColor",$iColor)
	DllStructSetData($tCharFormat, 2, $CFM_COLOR)
	DllStructSetData($tCharFormat, 6, $iColor)
	Return __GCR_SendMessage($hWnd, $EM_SETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($tCharFormat))
	EndFunc   ;==>_GUICtrlRichEdit_SetCharColor
#ce

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetCtrlBkColor
; Description....: Sets the background color of the control
; Syntax ........: _GUICtrlRichEdit_SetCtrlBkColor($hWnd, $vColor = "sys")
; Parameters.....: $hWnd		- Handle to the control
;                  $vColor - one of the following: (Optional)
;                  |a number - a COLORREF value
;                  |"sys" - the system color (default)
;                  |RGB percentages in the format "<red>;<green>;<blue>"
;                  |  If a value is omitted, the previous value is used
; Return values..: Success - True
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
;                  |1021 - $vColor: not 3 semicolons
;                  |1022 - $vColor: value not 0 to 100
;                  |1023 - $vColor: invalid character
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ EM_SETBKGNDCOLOR
; Example .......:
;
;===============================================================================
Func _GUICtrlRichEdit_SetCtrlBkColor($hWnd, $vColor = "sys")
	Local $iColor = 0, $fSysColor = False, $av
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Switch True
		Case StringIsDigit($vColor)
			$iColor = $vColor
		Case $vColor = "Sys"
			$fSysColor = True
		Case Else
			$av = StringSplit($vColor, ";")
			If $av[0] <> 3 Then Return SetError(1021, 0, False)
			For $i = 1 To $av[0]
				If StringIsDigit($av[$i]) Then
					If $av[$i] < 0 Or $av[$i] > 100 Then Return SetError(1022, 0, False)
					$iColor += BitShift($av[$i] * 255 / 100 + 0.5, -8 * ($i - 1))
				Else
					Return SetError(1023, 0, False)
				EndIf
			Next
	EndSwitch
	__GCR_SendMessage($hWnd, $EM_SETBKGNDCOLOR, $fSysColor, $iColor)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetCtrlBkColor

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_SetCtrlLimitOnText
; Description....: Change number of characters that can be typed, pasted or streamed in as Rich Text Format
; Syntax ........: _GuiCtrlRichEdit_SetCtrlLimitOnText($hWnd, $iNewLimit)
; Parameters.....: hWnd		- Handle to the control
;                  $iNewLimit - new limit
;                  Special value: 0 - 65,535 characters
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iNewLimit is neither a positive number nor zero
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: This function sets the limit on the number of characters a user can type in or paste into a control.
;+
;                  It also limits the number of characters of RTF text that can be streamed in using
;                  _GuiCtrlRichEdit_StreamFromFile and _GuiCtrlRichEdit_StreamFromVar.
;+
;                  It does not limit the amount of plain text that can be streamed in.
;+
;                  The initial limit is 32,767 characters.
; Related .......: _GUICtrlRichEdit_StreamInFile, _GUICtrlRichEdit_StreamInVar
; Link ..........: @@MsdnLink@@ EM_EXLIMITTEXT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetCtrlLimitOnText($hWnd, $iNewLimit)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iNewLimit, ">=0") Then Return SetError(102, 0, False)


	If $iNewLimit < 65535 Then $iNewLimit = 0 ; default max is 64K
	__GCR_SendMessage($hWnd, $EM_EXLIMITTEXT, 0, $iNewLimit)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_SetCtrlLimitOnText

; #FUNCTION# ;===============================================================================
;
; Name...........: _GuiCtrlRichEdit_SetCtrlTabStops
; Description ...: Sets tab stops for the control
; Syntax.........: _GUICtrlRichEdit_SetCtrlTabStops($hWnd, $vTabStops[, $fRedraw = True])
; Parameters ....: $hWnd - handle of control
;                  $VTabStops - tab stop(s) to set in space units:
;                  |If a string, semicolon-separated tab stop positions
;                  |If numeric: set a tab stop every <n> space units
;                  $fRedraw - whether to redraw the control - True (default) or False
; Return values .: Success - True
;                : Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - $vTabStops is neither a string nor a number
;                  |1022 - $vTabStops is a string but a tab stop in it is not a positive number
;                  |1023 - $vTabStops is an empty string
;                  |1024 - $vTabStops is a number but it is zero or negative
;                  |103  - $fRedraw must be True or False
; Author ........: KIP
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: Space units are initially inches
;                   To enter a tab into a control, press Ctrl_Tab
; Related .......: _GuiCtrlRichEdit_SetParaTabStops, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........; @@MsdnLink@@ EM_SETTABSTOPS
; Example .......: Yes
;
;===============================================================================================================================

Func _GUICtrlRichEdit_SetCtrlTabStops($hWnd, $vTabStops, $fRedraw = True)
	; Should take tabstops in space units (like EM_SETPARAFORMAT PFM_TABSTOPS, but how to convert inches, etc.
	; to dialog units? For now, a kludge based on experimentation
	Local Const $kTwipsPerDU = 18.75
	Local $iNumTabStops, $tTabStops, $tagTabStops, $fResult, $iWparam, $as

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fRedraw) Then Return SetError(103, 0, False)

	If IsString($vTabStops) Then ; Set every tabstop manually
		If $vTabStops = "" Then Return SetError(103, 0, False)
		$as = StringSplit($vTabStops, ";")
		$iNumTabStops = $as[0]
		For $i = 1 To $iNumTabStops
			If Not __GCR_IsNumeric($as[$i], ">") Then Return SetError(1022, 0, False)
			$tagTabStops &= "int;"
		Next
		$tagTabStops = StringTrimRight($tagTabStops, 1)
		$tTabStops = DllStructCreate($tagTabStops)
		For $i = 1 To $iNumTabStops
			DllStructSetData($tTabStops, $i, $as[$i] * $_GRE_TwipsPeSpaceUnit / $kTwipsPerDU)
		Next
		$iWparam = $iNumTabStops
	ElseIf IsNumber($vTabStops) Then
		If __GCR_IsNumeric($vTabStops, ">") Then
			$tTabStops = DllStructCreate("int")
			DllStructSetData($tTabStops, 1, $vTabStops * $_GRE_TwipsPeSpaceUnit / $kTwipsPerDU)
			$iWparam = 1
		Else
			Return SetError(1024, 9, False)
		EndIf
	Else
		Return SetError(1021, 0, False)
	EndIf
	$fResult = __GCR_SendMessage($hWnd, $EM_SETTABSTOPS, $iWparam, DllStructGetPtr($tTabStops), 0, "wparam", "ptr") <> 0
	If $fRedraw Then __GCR_InvalidateRect($hWnd) ; redraw the control
	Return $fResult
EndFunc   ;==>_GUICtrlRichEdit_SetCtrlTabStops

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetCtrlZoom
; Description....: Sets zoom level of the control
; Syntax ........: _GUICtrlRichEdit_SetCtrlZoom($hWnd, $iPercent)
; Parameters.....: $hWnd		- Handle to the control
;                  $iPercent - percentage zoom
;                  |values: 100 and 200 to 6400
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |1021 - $iPercent is not a positive number
;                  |1022 - $iPercent neither 100 nor in the range 200 to 6400
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: MSDN claims that EM_SETZOOM works from 1.56% (1/64) to 6400$ (64/1) but testing shows that
;                  it only works reliably for the values shown above
; Related .......: _GuiCtrlRichEdit_GetZoom
; Link ..........: @@MsdnLink@@ EM_SETZOOM
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetCtrlZoom($hWnd, $iPercent)
	Local $iNumerator, $iDenominator

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iPercent, ">0") Then Return SetError(1021, 0, False)

	Select
		Case Not ($iPercent = 100 Or ($iPercent >= 200 And $iPercent < 6400))
			Return SetError(1022, 0, False)
		Case $iPercent >= 100
			$iNumerator = 10000
			$iDenominator = 10000 / ($iPercent / 100)
		Case Else
			$iNumerator = 10000 * ($iPercent / 100)
			$iDenominator = 10000
	EndSelect
	Return __GCR_SendMessage($hWnd, $EM_SETZOOM, $iNumerator, $iDenominator) <> 0
EndFunc   ;==>_GUICtrlRichEdit_SetCtrlZoom

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetEventMask
; Description....: Specifies which notification messages are sent to the parent window
; Syntax ........: _GUICtrlRichEdit_SetEventMask($hWnd, $iFlags)
; Parameters.....: $hWnd		- Handle to the control
;                  $iEventMask - BitOr combination of :
;                  |$ENM_CHANGE - Sends $EN_CHANGE  notifications (user may have altered text)
;                  |$ENM_CORRECTTEXT - Sends $EN_CORRECTTEXT notifications (parent window can cancel correction of text)
;                  |$ENM_DRAGDROPDONE - Sends $EN_DRAGDROPDONE notifications (drag and drop operation completed)
;                  |$ENM_DROPFILES - Sends $EN_DROPFILES notifications (user is attempting to drop files into the control)
;                  |$ENM_KEYEVENTS - Sends $EN_MSGFILTER notifications for keyboard events
;                  |$ENM_LINK - Sends $EN_LINK notifications when the mouse pointer is over text having the link character
;                  +attribute set and when user clicks the mouse [2.0+]
;                  |$ENM_MOUSEEVENTS - Sends $EN_MSGFILTER notifications for mouse events to parent window
;                  |$ENM_OBJECTPOSITIONS - Sends $EN_OBJECTPOSITIONS notifications when the control reads in objects
;                  |$ENM_PROTECTED - Sends $EN_PROTECTED notifications when the user attempts to change characters having
;                  +the protected attribute set
;                  |$ENM_REQUESTRESIZE - Sends $EN_REQUESTRESIZE notifications that the control's contents are either smaller or
;                  +larger than the control's window size
;                  |$ENM_SCROLL - Sends $EN_HSCROLL and $EN_VSCROLL notifications when the user clicks the horizontal/vertical
;                  +scroll bar
;                  |$ENM_SCROLLEVENTS - Sends EN_MSGFILTER notifications for mouse wheel events
;                  |$ENM_SELCHANGE - Sends EN_SELCHANGE notifications when the current selection changes
;                  |$ENM_UPDATE - Sends EN_UPDATE notifications when a control is about to redraw itself
;                  |or
;                  |$ENM_NONE - Disables sending of notification messages to the parent window
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iEventMask is not a number
; Authors........: Yoan Roblet (Arcker)
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: The number in parentheses indicate which versions mask settings apply to.
;+
;                  $ENM_IMECHANGE only applies to Asian-language versions of Windows
;+
;                  $EN_UPDATE notifications are always sent except for when 4.0 is emulating 1.0
; Related .......: _GuiCtrlRichEdit_SetCharAttributes, _GuiCtrlRichEdit_GetCharAttributes
; Link ..........: @@MsdnLink@@ EM_SETEVENTMASK
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetEventMask($hWnd, $iEventMask)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iEventMask) Then Return SetError(102, 0, False)


	Return __GCR_SendMessage($hWnd, $EM_SETEVENTMASK, 0, $iEventMask)
EndFunc   ;==>_GUICtrlRichEdit_SetEventMask

; #FUNCTION# ;===============================================================================
;
; Name...........: _GUICtrlRichEdit_SetFont
; Description ...: Sets the font attributes of selected text or, if none selected, sets those of text inserted at the insertion point
; Syntax.........: _GUICtrlRichEdit_SetFont($hWnd,  $iPoints = Default[, $sName = Default[, $iCharset = Default[, $iLcid = Default]]])
; Parameters ....: $hWnd        - Handle to the control
;                  $iPoints - point size (Optional)
;                  $sName - the name of the font face, e.g. "Courier" not "Courier Bold" (Optional)
;                  $iCharSet -  the character set (Optional) - one of:
;                  |$ANSI_CHARSET        - 0
;                  |$BALTIC_CHARSET      - 186
;                  |$CHINESEBIG5_CHARSET - 136
;                  |$DEFAULT_CHARSET     - 1
;                  |$EASTEUROPE_CHARSET  - 238
;                  |$GB2312_CHARSET      - 134
;                  |$GREEK_CHARSET       - 161
;                  |$HANGEUL_CHARSET     - 129
;                  |$MAC_CHARSET         - 77
;                  |$OEM_CHARSET         - 255
;                  |$RUSSIAN_CHARSET     - 204
;                  |$SHIFTJIS_CHARSET    - 128
;                  |$SYMBOL_CHARSET      - 2
;                  |$TURKISH_CHARSET     - 162
;                  |$VIETNAMESE_CHARSET  - 163
;                  $iLcid - see http://www.microsoft.com/globaldev/reference/lcid-all.mspx (Optional)
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iPoints is not a positive number
;                  |103 - $sName is not alphabetic
;                  |104 - $iLcid is not a number
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: If a parameter is omitted (or is Default), the value is unchanged
; Related .......: _GUICtrlRichEdit_GetFont
; Link ..........; @@MsdnLink@@ EM_SETCHARFORMAT, @@MsdnLink@@ LOGFONT, http://www.hep.wisc.edu/~pinghc/books/apirefeng/l/logfont.html
; Example .......: Yes
;
;===============================================================================================================================
Func _GUICtrlRichEdit_SetFont($hWnd, $iPoints = Default, $sName = Default, $iCharset = Default, $iLcid = Default)
	; MSDN does not give a mask (CFM) for bPitchAndFamily so it appears that it cannot be set => omitted here
	Local $tCharFormat, $iDwMask = 0, $as

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($iPoints = Default Or __GCR_IsNumeric($iPoints, ">0")) Then Return SetError(102, 0, False)
	If $sName <> Default Then
		$as = StringSplit($sName, " ")
		For $i = 1 To UBound($as) - 1
			If Not StringIsAlpha($as[$i]) Then Return SetError(103, 0, False)
		Next
	EndIf
	If Not ($iCharset = Default Or __GCR_IsNumeric($iCharset)) Then Return SetError(104, 0, False)
	If Not ($iLcid = Default Or __GCR_IsNumeric($iLcid)) Then Return SetError(105, 0, False)

	$tCharFormat = DllStructCreate($tagCHARFORMAT2)
	DllStructSetData($tCharFormat, 1, DllStructGetSize($tCharFormat))

	If $iPoints <> Default Then
		$iDwMask = $CFM_SIZE
		DllStructSetData($tCharFormat, 4, Int($iPoints * 20))
	EndIf
	If $sName <> Default Then
		If StringLen($sName) > $LF_FACESIZE - 1 Then SetError(-1, 0, False)
		$iDwMask = BitOR($iDwMask, $CFM_FACE)
		DllStructSetData($tCharFormat, 9, $sName)
	EndIf
	If $iCharset <> Default Then
		$iDwMask = BitOR($iDwMask, $CFM_CHARSET)
		DllStructSetData($tCharFormat, 7, $iCharset)
	EndIf
	If $iLcid <> Default Then
		$iDwMask = BitOR($iDwMask, $CFM_LCID)
		DllStructSetData($tCharFormat, 13, $iLcid)
	EndIf
	DllStructSetData($tCharFormat, 2, $iDwMask)

	__GCR_SendMessage($hWnd, $EM_SETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($tCharFormat), 0, "wparam", "ptr")
	If @error Then Return SetError(@error + 200, 0, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetFont

#cs
	; #FUNCTION# ;====================================================================================================================
	;
	; Name...........: _GUICtrlRichEdit_SetFormattingRect
	; Description ...: Sets the formatting rectangle of a control
	; Syntax.........: _GUICtrlRichEdit_SetFormattingRect($hWnd, $vLeft = Default[, $vTop = Default[, $vRight = Default[, $vBottom = Default]]])
	; Parameters ....: $hWnd        - Handle to the control
	;                  $vLeft   - a number, "+ <number>", or "<- <number>"
	;                  |Special value - "-0" - reset rectnagle to default values
	;                  $vTop    - a number, "+ <number>", or "<- <number>"
	;                  $vRight  - a number, "+ <number>", or "<- <number>"
	;                  $vBottom - a number, "+ <number>", or "<- <number>"
	; Return values .: Success - True
	;                  Failure - False and sets @error:
	;                  |101 - $hWnd is not a handle
	;                  |1021 - $vLeft is not a positive number
	;                  |1022 - left >= right
	;                  |1031 - $vTop is not a positive number
	;                  |1032 - top >= bottom
	;                  |1041 - $vRight is not a positive number
	;                  |1042 - right <= left
	;                  |1051 - $vBottom is not a positive number
	;                  |1052 - bottom <= top
	; Author ........: Chris Haslam (c.haslam)
	; Modified.......:
	; Remarks .......: The formatting rectangle is the area in which text is drawn, part of which may not be visible.
	;+
	;                  Parameters default to previous values
	; Related .......: _GUICtrlRichEdit_GetFormattingRect
	; Link ..........; @@MsdnLink@@ EM_SETRECT
	; Example .......:
	;
	; ===============================================================================================================================
	Func _GUICtrlRichEdit_SetFormattingRect($hWnd, $vLeft = Default, $vTop = Default, $vRight = Default, $vBottom = Default, $fRedraw = True)
	; Commentsed out because EM_SETRECT doesn't work
	Local $as, $tRect, $s, $iMsg
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($vLeft = Default Or __GCR_IsNumeric($vLeft)) Then Return SetError(1021, 0, False)
	If Not ($vTop = Default Or __GCR_IsNumeric($vTop)) Then Return SetError(1031, 0, False)
	If Not ($vRight = Default Or __GCR_IsNumeric($vRight)) Then Return SetError(1041, 0, False)
	If Not ($vBottom = Default Or __GCR_IsNumeric($vBottom)) Then Return SetError(1051, 0, False)
	If $vLeft="-0" Then
	__GCR_SendMessage($hWnd, $iMsg, 0, 0, 0, "wparam", "int")	; reset to default
	Else
	$s = _GUICtrlRichEdit_GetFormattingRect($hWnd)
	$as = StringSplit($s, ";")
	If $vLeft = Default Then
	$vLeft = $as[1]
	ElseIf StringInStr("+-", StringLeft($vLeft, 1)) <> 0 Then
	$vLeft = $as[1] + $vLeft
	EndIf
	If $vTop = Default Then
	$vTop = $as[2]
	ElseIf StringInStr("+-", StringLeft($vTop, 1)) <> 0 Then
	$vTop = $as[2] + $vTop
	EndIf
	If $vRight = Default Then
	$vRight = $as[3]
	ElseIf StringInStr("+-", StringLeft($vRight, 1)) <> 0 Then
	$vRight = $as[3] + $vRight
	EndIf
	If $vBottom = Default Then
	$vBottom = $as[4]
	ElseIf StringInStr("+-", StringLeft($vBottom, 1)) <> 0 Then
	$vBottom = $as[4] + $vBottom
	EndIf
	If $vLeft >= $vRight Then Return SetError(1022, 0, False)
	If $vTop >= $vBottom Then Return SetError(1032, 0, False)
	If $vRight <= $vLeft Then Return SetError(1042, 0, False)
	If $vBottom < -$vTop Then Return SetError(1052, 0, False)
	$tRect = DllStructCreate($tagRECT)
	DllStructSetData($tRect, "Left", Number($vLeft))
	DllStructSetData($tRect, "Top", Number($vTop))
	DllStructSetData($tRect, "Right", Number($vRight))
	DllStructSetData($tRect, "Bottom", Number($vBottom))
	$iMsg = _Iif($fRedraw, $EM_SETRECT, $EM_SETRECTNP)
	__GCR_SendMessage($hWnd, $iMsg, 0, DllStructGetPtr($tRect), 0, "wparam", "ptr")
	EndIf
	Return True
	EndFunc   ;==>_GUICtrlRichEdit_SetFormattingRect
#ce

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_SetModified
; Description ...: Sets or clears the modification flag
; Syntax.........: _GUICtrlRichEdit_SetModified($hWnd, $fState = True)
; Parameters ....: $hWnd        - Handle to the control
;                  $fState   - Specifies the new value for the modification flag:
;                  | True       - Indicates that the text has been modified (default)
;                  |False       - Indicates it has not been modified.
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fState must be True or False
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......: The system automatically sets the modification flag to False when the control is created.
;                  If the control's text is changed, either by the user or programmatically, the system sets the flag to True
;                  You can use the _GUICtrlEdit_IsModified to retrieve the current state of the flag.
; Related .......: _GUICtrlRichEdit_IsModified
; Link ..........; @@MsdnLink@@ $EM_SETMODIFY
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_SetModified($hWnd, $fState = True)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fState) Then Return SetError(102, 0, False)

	__GCR_SendMessage($hWnd, $EM_SETMODIFY, $fState)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetModified

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaAlignment
; Description....: Sets alignment of paragraph(s) in the current selection or, if no selection, of paragraphs inserted at the insertion point
; Syntax ........: _GUICtrlRichEdit_SetParaAlignment($hWnd, $iAlignment)
; Parameters.....: $hWnd		- Handle to the control
;                  $sAlignment - values:
;                  |"l" - align with the left margin.
;                  |"r" - align with the right margin.
;                  |"c" - center between margins
;                  |"j" - justify between margins
;                  |"f" - justify between margins by only expanding spaces
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - invalid $sAlignment
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: In Richedit 2.0, justify does not display
; Related .......: _GUICtrlRichEdit_GetParaAlignment
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetParaAlignment($hWnd, $sAlignment)
	Local $tParaFormat, $iAlignment

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	Switch $sAlignment
		Case "l"
			$iAlignment = $PFA_LEFT
		Case "c"
			$iAlignment = $PFA_CENTER
		Case "r"
			$iAlignment = $PFA_RIGHT
		Case "j"
			$iAlignment = $PFA_JUSTIFY
		Case "w"
			$iAlignment = $PFA_FULL_INTERWORD
		Case Else
			Return SetError(101, 0, False)
	EndSwitch
	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, 2, $PFM_ALIGNMENT)
	DllStructSetData($tParaFormat, 8, $iAlignment)
	Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
EndFunc   ;==>_GUICtrlRichEdit_SetParaAlignment

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaAttributes
; Description....: Sets attributes of paragraph(s) in the current selection or, if no selection, of paragraphs inserted at the insertion point
; Syntax ........: _GuiCtrlRichEdit_SetParaAtttributes($hWnd, $sStatesAndAtts)
; Parameters.....: $hWnd		- Handle to the control
;                  $sStatesAndAtts -  a string consisting of groups separated by semicolons (;}.
;                  + Each group consists of:
;                  |First character - state:
;                  |   +  -  turn attribute on
;                  |   -  -  turn attribute off
;                  Characters 2 to 4 - attribute
;                  |   "fpg"  -  force this/these paragraphs on to new page(s) (Initially off)
;                  |   "hyp"  -  automatic hypthenation (Initially on)
;                  |   "kpt"  -  keep this/these paragraph(s) together on a page (Initially off}
;                  |   "kpn"  -  keep this/these paragraph(s) and the next together on a page (Initially off)
;                  |   "pwo"  -  prevent widows and orphans, i.e. avoid a single line of this/these paragraphs
;                  +on a page (Initially off)
;                  |   "r2l"  -  display text using right-to-left reading order (Initially off)
;                  |   "row"  -  paragraph(s) is/are table row(s) (Initially off)
;                  |   "sbs"  -  display paragraphs side by side (Initially off)
;                  |   "sln"  -  suppress line numbers in documents or sections with line numbers (Initially off)
; Return values..: Success - True
;                  Failure - False and sets @error
;                  |101  - $hWnd is not a handle
;                  |1021 - a state character in $sStatesAndAtts is invalid. It is in @extended
;                  |1022 - an attribute abbreviation in $sStatesAndAtts is invalid. It is in @extended
;                  |1023 - length of $sStatesAndAtts is invalid
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Displays results in Word but not in Rich Edit
; Related .......: _GUICtrlRichEdit_GetParaAttributes
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetParaAttributes($hWnd, $sStatesAndAtts)
	Local $iMask = 0, $iEffects = 0, $s, $n, $tParaFormat, $as
	Local Enum $kAbbrev = 0, $kMask, $kEffect, $kInverted
	; MS seems to mean LINENUMBER and WIDOWCONTROL, not NOLINENUMBER and NOWIDOWCONTROL
	Local Const $av[9][4] = [ _	; abbrev, mask, effect, inverted
			["fpg", $PFM_PAGEBREAKBEFORE, $PFE_PAGEBREAKBEFORE, False], _
			["hyp", $PFM_DONOTHYPHEN, $PFE_DONOTHYPHEN, True], _
			["kpt", $PFM_KEEP, $PFE_KEEP, False], _
			["kpn", $PFM_KEEPNEXT, $PFE_KEEPNEXT, False], _
			["pwo", $PFM_NOWIDOWCONTROL, $PFE_NOWIDOWCONTROL, False], _
			["r2l", $PFM_RTLPARA, $PFE_RTLPARA, False], _
			["row", $PFM_TABLE, $PFE_TABLE, False], _
			["sbs", $PFM_SIDEBYSIDE, $PFE_SIDEBYSIDE, False], _
			["sln", $PFM_NOLINENUMBER, $PFE_NOLINENUMBER, False]]

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	If Mod(StringLen($sStatesAndAtts) + 1, 5) <> 0 Then Return SetError(1023, 0, False)
	$as = StringSplit($sStatesAndAtts, ";")
	For $i = 1 To UBound($as, 1) - 1
		$s = StringMid($as[$i], 2)
		$n = -1
		For $j = 0 To UBound($av, 1) - 1
			If $av[$j][$kAbbrev] = $s Then
				$n = $j
				ExitLoop
			EndIf
		Next
		If $n = -1 Then Return SetError(1022, $s, False)
		$iMask = BitOR($iMask, $av[$n][$kMask])
		$s = StringLeft($as[$i], 1)
		Switch $s
			Case "+"
				If Not $av[$n][$kInverted] Then ; if normal sense
					$iEffects = BitOR($iEffects, $av[$n][$kEffect])
				EndIf
			Case "-"
				If $av[$n][$kInverted] Then ; if inverted sense
					$iEffects = BitOR($iEffects, $av[$n][$kEffect])
				EndIf
			Case Else
				Return SetError(1021, $s, False)
		EndSwitch
	Next
	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, 2, $iMask)
	DllStructSetData($tParaFormat, 4, $iEffects)
	Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat), 0, "wparam", "ptr") <> 0
EndFunc   ;==>_GuiCtrlRichEdit_SetParaAttributes

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaBorder
; Description....: Sets the border  of paragraph(s) in the current selection or, if no selection, of paragraphs inserted at the insertion point
; Syntax ........: _GuiCtrlRichEdit_SetParaBorder($hWnd[, $sLocation[, $vLineStyle[, $sColor[, $iSpace]]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $sLocation (Optional) - a string consisting of any logical combination of:
;                  |   l - left border
;                  |   r - right border
;                  |   t - top border
;                  |   b - bottom border
;                  |   i - inside border
;                  |   o - outside border
;                  |or  ""  - no border  (initial value)
;                  $vLineStyle {Optional) - line style - one of:
;                  |   "none" - no line  (initial value)
;                  |   .75    -  3/4 point
;                  |   1.5    -  1 1/2 points
;                  |   2.25   -  2 1/4 points
;                  |   3      -  3 points
;                  |   4.5    -  4 1/2 points
;                  |   6      -  6 points
;                  |   ".75d" -  1/2 points, double
;                  |   "1.5d" -  1 1/2 points, double
;                  |   "2.25d" - 2 1/4 points, double
;                  |   ".75g"  - 3/4 point grey
;                  |   ".75gd" - 3/4 point grey dashed
;                  $sColor {Optional) - one of:
;                  |   "aut"   - autocolor
;                  |   "blk"   - black  (initial value)
;                  |   "blu"   - blue
;                  |   "cyn"   - cyan
;                  |   "grn"   - green
;                  |   "mag"   - magenta
;                  |   "red"   - red
;                  |   "yel"   - yellow
;                  |   "whi"   - white
;                  |   "dbl"   - dark blue
;                  |   "dgn"   - dark green
;                  |   "dmg"   - dark magenta
;                  |   "drd"   - dark red
;                  |   "dyl"   - dark yellow
;                  |   "dgy"   - dark grey
;                  |   "lgy"   - light grey
;                  $iSpace (Optional) - space between the border and the text (in space units) ( (initial value): 0)
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102  -  value of $sLocation is invalid
;                  |103 -  value of $ivLineStyle is invalid
;                  |104 -  value of $sColor is invalid
;                  |106 - $iSpace is neither a positive number nor zero
;                  |106  -  $iWidth is neither a positive number nor zero
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
;+
;                  If text is selected, the defaults are the values of the first paragraph with text selected.
;                  If none is selected, the defaults are the values of the current paragraph.
;+
;                  To remove a border, call with two parameters: ($hWnd, "")
;+
;                  Borders do not show in Rich Edit, but ones created here should show in Word
; Related .......: _GUICtrlRichEdit_GetParaBorder, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetParaBorder($hWnd, $sLocation = Default, $vLineStyle = Default, $sColor = Default, $iSpace = Default)
	; wBorderWidth doesn't appear to work
	Local $iBorders, $n, $tParaFormat, $s, $iLoc, $iLineStyle, $iColor, $as
	;	Local $tOldParaFormat,$iOldLoc, $iOldSpace, $iOldLineStyle, $iOldColor, $iN
	Local Const $avLocs[6][2] = [["l", 1],["r", 2],["t", 4],["b", 8],["i", 16],["o", 32]]
	Local Const $avLS[12] = ["none", .75, 1.5, 2.25, 3, 4.5, 6, ".75d", "1.5d", "2.25d", ".75g", ".75gd"]
	Local Const $sClrs = ";blk;blu;cyn;grn;mag;red;yel;whi;dbl;dgn;dmg;drd;dyl;dgy;lgy;aut;"

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($iSpace = Default Or __GCR_IsNumeric($iSpace, ">=0")) Then Return SetError(105, 0, False)
	;	If Not ($iWidth = Default Or __GCR_IsNumeric($iWidth, ">=0")) Then  Return SetError(106, 0, False)	; wBorderWidth does not round-trip

	If $sLocation = "" Then
		$iBorders = 0
		$iSpace = 0
		;		$iWidth = 0
	Else
		If $sLocation = Default Or $vLineStyle = Default Or $sColor = Default Or $iSpace = Default Then
			$as = StringSplit(_GuiCtrlRichEdit_GetParaBorder($hWnd), ";")
			If $sLocation = Default Then $sLocation = $as[1]
			If $vLineStyle = Default Then $vLineStyle = $as[2]
			If $sColor = Default Then $sColor = $as[3]
			If $iSpace = Default Then $iSpace = $as[4]
		EndIf
		$iLoc = 0
		For $i = 1 To StringLen($sLocation)
			$s = StringMid($sLocation, $i, 1)
			$n = -1
			For $j = 0 To UBound($avLocs, 1) - 1
				If $avLocs[$j][0] = $s Then
					$n = $j
					ExitLoop
				EndIf
			Next
			If $n = -1 Then Return SetError(102, $s, False)
			$iLoc = BitOR($iLoc, $avLocs[$n][1])
		Next
		$n = -1
		For $i = 0 To UBound($avLS, 1) - 1
			If $vLineStyle = $avLS[$i] Then
				$n = $i
				ExitLoop
			EndIf
		Next
		If $n = -1 Then Return SetError(103, 0, False)
		$iLineStyle = $n
		$n = StringInStr($sClrs, ";" & $sColor & ";")
		If $n = 0 Then Return SetError(104, 0, False)
		$iColor = Int($n / 4)
		If $iColor = 16 Then ; if autocolor
			$iLoc = BitOR($iLoc, 64)
			$iColor = 0
		EndIf
		$iBorders = $iLoc + BitShift($iLineStyle, -8) + BitShift($iColor, -12)
		;		If $iWidth = Default Then $iWidth = $iOldWidth
	EndIf
	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, "wBorderSpace", $iSpace * $_GRE_TwipsPeSpaceUnit)
	;	DllStructGetData($tParaFormat, 23, $iWidth * $_GRE_TwipsPeSpaceUnit)
	DllStructSetData($tParaFormat, "wBorders", $iBorders)
	DllStructSetData($tParaFormat, "dwMask", $PFM_BORDER)
	Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat), 0, "wparam", "ptr") <> 0
EndFunc   ;==>_GuiCtrlRichEdit_SetParaBorder

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaIndents
; Description....: Sets indents of paragraph(s) in the current selection or, if no selection, of paragraphs inserted at the insertion point
; Syntax ........: _GuiCtrlRichEdit_SetParaIndents($hWnd, $vLeft[ = Default, $iRight[ = Default, $iFirstLine[ = Default]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $vLeft - indentation of left side of the body of the paragraph (in space units) (Optional)
;                  | absolute - a number
;                  | relative to previous - a string - "+<number>" or "-<number>"
;                  $iRght - indentation of  right side of the paragraph (in space units) (Optional)
;                  $iFirstLine - indentation of the first line relative to other lines (in space units) (Optional)
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - $vLeft is neither a number nor a string consisting of a number
;                  |1022 - $vLeft would start body of paragrpah to left of client area
;                  |103 - $iRight is not a number
;                  |105 - $iFirstLine is not a number
;                  |200 - First line would be outdented beyond the client area
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Postive values of $iLeft, $iRight and $iFirstLine indent towards the center of the paragraph
;+
;                  All three values are initially zero.
;+
;                  To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
;+
;                  If text is selected, the defaults are the values of the first paragraph with text selected.
;                  If none is selected, the defaults are the values of the current paragraph.
; Related .......: _GUICtrlRichEdit_GetParaIndents, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetParaIndents($hWnd, $vLeft = Default, $iRight = Default, $iFirstLine = Default)
	Local $tParaFormat, $fResult, $idxSI, $iDxOfs, $as, $s

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($vLeft = Default Or __GCR_IsNumeric($vLeft)) Then Return SetError(1021, 0, False)
	If Not ($iRight = Default Or __GCR_IsNumeric($iRight, ">=0")) Then Return SetError(103, 0, False)
	If Not ($iFirstLine = Default Or __GCR_IsNumeric($iFirstLine)) Then Return SetError(104, 0, False)

	$s = _GuiCtrlRichEdit_GetParaIndents($hWnd)
	$as = StringSplit($s, ";")
	If $vLeft = Default Then $vLeft = $as[1]
	If $iRight = Default Then $iRight = $as[2]
	If $iFirstLine = Default Then $iFirstLine = $as[3]
	If $vLeft < 0 Then Return SetError(1022, 0, False)
	If $vLeft + $iFirstLine < 0 Then Return SetError(200, 0, False)

	If StringInStr("+-", StringLeft($vLeft, 1)) <> 0 Then $vLeft = $as[1] + $vLeft

	$idxSI = $vLeft + $iFirstLine
	$iDxOfs = -$iFirstLine

	$tParaFormat = DllStructCreate($tagPARAFORMAT)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, "dxStartIndent", $idxSI * $_GRE_TwipsPeSpaceUnit)
	DllStructSetData($tParaFormat, "dxOffset", $iDxOfs * $_GRE_TwipsPeSpaceUnit)
	DllStructSetData($tParaFormat, "dxRightIndent", $iRight * $_GRE_TwipsPeSpaceUnit)
	DllStructSetData($tParaFormat, 2, BitOR($PFM_STARTINDENT, $PFM_OFFSET, $PFM_RIGHTINDENT)) ; absolute
	$fResult = __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
	If @error Then Return SetError(@error, 0, False)
	Return $fResult
EndFunc   ;==>_GuiCtrlRichEdit_SetParaIndents

; #FUNCTION# ;===============================================================================
;
; Name ..........: GUICtrlRichEdit_SetParaNumbering
; Description....: Sets numbering of paragraph(s) in the current selection or, if no selection, of paragraph(s) inserted at the insertion point
; Syntax ........: _GUICtrlRichEdit_SetParaNumbering($hWnd, $sStyle, $iTextToNbrSpace = Default,$fForceRoman=False)
; Parameters.....: $hWnd		- Handle to the control
;                  $sStyle - a string specifying style and starting "number": e.g. "." (bullet), "1)","(b)", "C.", "iv", "V)"
;                  |   This is the "numbering" that will display for the first paragraph.
;                  |   Trailing spaces indicate the minimum spaces between the number and the paragraph unless iTextToNbrSpace is entered
;                  |   Special cases:
;                  |      "=" - This paragraph is an unnumbered paragraph within the preceding list element
;                  |       "" - removed the numbering from the selected paragraph(s)
;                  $iTextToNbrSpace - space between number/bullet and paragraph (in space units) (Optional)
;                  |Default: number of trailing spaces times point size
;                  $fForceRoman (Optional)
;                   |True - i, v, x ... in $sStyle is Roman numeral 1, 5, 10 ...
;                  |False - i, v, x ... in $sStyle is letter i, v, x ... {Default)
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |102 - $sStyle is invalid
;                  |103 - $iTextToNbrSpace is not a postive number
;                  |104 - $fForceRoman must be True or False
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
; Related .......: _GUICtrlRichEdit_GetParaNumbering, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetParaNumbering($hWnd, $sStyle, $iTextToNbrSpace = Default, $fForceRoman = False)
	Local $iPFM, $iWNumbering, $iWnumStart, $iWnumStyle, $iQspaces, $tParaFormat
	Local $iPoints, $iTwips, $av

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($iTextToNbrSpace = Default Or __GCR_IsNumeric($iTextToNbrSpace, ">0")) Then Return SetError(103, 0, False)
	If Not IsBool($fForceRoman) Then Return SetError(104, 0, False)

	__GCR_ParseParaNumberingStyle($sStyle, $fForceRoman, $iPFM, $iWNumbering, $iWnumStart, $iWnumStyle, $iQspaces)
	If @error Then Return SetError(@error, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, 1, DllStructGetSize($tParaFormat))
	If BitAND($iPFM, $PFM_NUMBERING) Then DllStructSetData($tParaFormat, 3, $iWNumbering)
	If BitAND($iPFM, $PFM_NUMBERINGSTART) Then DllStructSetData($tParaFormat, 19, $iWnumStart)
	If BitAND($iPFM, $PFM_NUMBERINGSTYLE) Then DllStructSetData($tParaFormat, 20, $iWnumStyle)
	If BitAND($iPFM, $PFM_NUMBERINGTAB) Then
		If $iTextToNbrSpace = Default Then
			; set number-to-text spacing based on font at anchor or onsertion point
			$av = StringSplit(_GUICtrlRichEdit_GetFont($hWnd), ";")
			$iPoints = $av[1]
			$iTwips = $iQspaces * $iPoints * 20
		Else
			$iTwips = $iTextToNbrSpace * $_GRE_TwipsPeSpaceUnit
		EndIf
		DllStructSetData($tParaFormat, 21, $iTwips)
	EndIf
	DllStructSetData($tParaFormat, 2, $iPFM)
	Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
EndFunc   ;==>_GUICtrlRichEdit_SetParaNumbering

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GuiCtrlRichEdit_SetParaShading
; Description....: Sets the shading of paragraph(s) in the current selection or, if no selection, of paragraphs inserted at the insertion point
; Syntax ........: _GuiCtrlRichEdit_SetParaShading($hWnd, $iWeight[ = Default, $sStyle[ = Default, $sForeColor[ = Default, $sBackColor[ = Default]]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $iWeight (Optional) - percent of foreground color, the rest being background color
;                  $sStyle {Optional) - shading style - a string containing one of the following:
;                  |   non - none
;                  |   dhz - dark horizontal
;                  |   dvt - dark vertical
;                  |   ddd - dark down diagonal
;                  |   dud - dark up diagonal
;                  |   dgr - dark grid
;                  |   dtr - dark trellis
;                  |   lhz - light horizontal
;                  |   lvt - light vertical
;                  |   ldd - light down diagonal
;                  |   lud - light up diagonal
;                  |   lgr - light grid
;                  |   ltr - light trellis
;                  $sForeColour (Optional) - one of the following:
;                  |   "blk"   - black  (initial value)
;                  |   "blu"   - blue
;                  |   "cyn"   - cyan
;                  |   "grn"   - green
;                  |   "mag"   - magenta
;                  |   "red"   - red
;                  |   "yel"   - yellow
;                  |   "whi"   - white
;                  |   "dbl"   - dark blue
;                  |   "dgn"   - dark green
;                  |   "dmg"   - dark magenta
;                  |   "drd"   - dark red
;                  |   "dyl"   - dark yellow
;                  |   "dgy"   - dark grey
;                  |   "lgy"   - light grey
;                  $sBackColor (Optional) - same values as for $sForeColor
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |1021 - $iWeight is not a positive number
;                  |1022 -  value of $iWeight is invalid
;                  |103  -  value of $sStyle is invalid
;                  |104  -  value of $sForeColor is invalid
;                  |105  -  value of $sBackColor is invalid
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If text is selected, the defaults are the values of the first paragraph with text selected.
;                  If none is selected, the defaults are the values of the current paragraph.
;+
;                  Shading does not show in Rich Edit, but shading created here will show in Word
; Related .......: _GUICtrlRichEdit_GetParaShading
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GuiCtrlRichEdit_SetParaShading($hWnd, $iWeight = Default, $sStyle = Default, $sForeColor = Default, $sBackColor = Default)
	Local $tParaFormat, $iN, $iStyle, $iFore, $iBack, $fResult, $iS, $as
	Local Const $sStyles = ";non;dhz;dvt;ddd;dud;dgr;dtr;lhz;lrt;ldd;lud;lgr;ltr;"
	Local Const $sClrs = ";blk;blu;cyn;grn;mag;red;yel;whi;dbl;dgn;dmg;drd;dyl;dgy;lgy;"

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($iWeight = Default Or __GCR_IsNumeric($iWeight, ">=0")) Then Return SetError(1021, 0, False)

	If $iWeight <> Default Or $sStyle <> Default Or $sForeColor <> Default Or $sBackColor <> Default Then
		$as = StringSplit(_GuiCtrlRichEdit_GetParaShading($hWnd), ";")
		If $iWeight = Default Then $iWeight = $as[1]
		If $sStyle = Default Then $sStyle = $as[2]
		If $sForeColor = Default Then $sForeColor = $as[3]
		If $sBackColor = Default Then $sBackColor = $as[4]
		#cs
			$tOldParaFormat = DllStructCreate($tagPARAFORMAT2)
			DllStructSetData($tOldParaFormat, 1, DllStructGetSize($tOldParaFormat))
			__GCR_SendMessage($hWnd, $EM_GETPARAFORMAT, 0, DllStructGetPtr($tOldParaFormat))
		#ce
	EndIf
	;	If $iWeight = Default Then
	;		$iWeight = DllStructGetData($tOldParaFormat, 17)
	;	Else
	If $iWeight < 0 Or $iWeight > 100 Then Return SetError(1022, 0, False)
	;	EndIf
	;	If $sStyle = Default Or $sForeColor = Default Or $sBackColor = Default Then
	;		$iS = DllStructGetData($tOldParaFormat, 18)
	;	EndIf
	;	If $sStyle = Default Then
	;		$iStyle = BitAND($iS, 0xF)
	;	Else
	$iN = StringInStr($sStyles, ";" & $sStyle & ";")
	If $iN = 0 Then Return SetError(103, 0, False)
	$iStyle = Int($iN / 4)
	;	EndIf
	;	If $sForeColor = Default Then
	$iFore = BitShift(BitAND($iS, 0xF0), 4)
	;	Else
	$iN = StringInStr($sClrs, ";" & $sForeColor & ";")
	If $iN = 0 Then Return SetError(104, 0, False)
	$iFore = Int($iN / 4)
	;	EndIf
	;	If $sBackColor = Default Then
	;		$iBack = BitShift(BitAND($iS, 0xF00), 8)
	;	Else
	$iN = StringInStr($sClrs, ";" & $sBackColor & ";")
	If $iN = 0 Then Return SetError(105, 0, False)
	$iBack = Int($iN / 4)
	;	EndIf
	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))
	DllStructSetData($tParaFormat, "wShadingWeight", $iWeight)
	$iN = $iStyle + BitShift($iFore, -4) + BitShift($iBack, -8)
	DllStructSetData($tParaFormat, "wShadingStyle", $iN)
	DllStructSetData($tParaFormat, "dwMask", $PFM_SHADING)
	$fResult = __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
	If @error Then Return SetError(@error, 0, False)
	Return $fResult
EndFunc   ;==>_GuiCtrlRichEdit_SetParaShading

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaSpacing
; Description....: Sets paragraph spacing of paragraphs having selected text or, if none selected, sets it for text inserted at the insertion point
; Syntax ........: _GUICtrlRichEdit_SetParaSpacing($hWnd, [$vInter=Default[, $iBefore=Default[, $iAfter=Default]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $vInter - spacing between lines: (Optional)
;                  |either: a number - in space units
;                  |or: "<number> lines" - in lines
;                  $iBefore - spacing before paragraph(s) (in space units) (Optional)
;                  $iAfter  - spacing after paragraph(s) (in space units) (Optional)
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |1021 - $vInter is invalid
;                  |1022 - Only 1, 1.5 and 2 line spacing can be set via "<n> lines"
;                  |103  - $iBefore is neither a positive number nor zero
;                  |104  - $iAfter is neither a positive number nor zero
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: Only settings which are not defaulted are set
;+
;                  To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
; Related .......: _GUICtrlRichEdit_GetParaSpacing, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETPARAFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetParaSpacing($hWnd, $vInter = Default, $iBefore = Default, $iAfter = Default)
	Local $tParaFormat, $iMask = 0, $iP, $iRule, $iLnSp = 0

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not ($iBefore = Default Or __GCR_IsNumeric($iBefore, ">=0")) Then Return SetError(103, 0, False)
	If Not ($iAfter = Default Or __GCR_IsNumeric($iAfter, ">=0")) Then Return SetError(104, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))
	If $vInter <> Default Then
		$vInter = StringStripWS($vInter, 8) ; strip all spaces
		$iP = StringInStr($vInter, "line", 2) ; case-insensitive, faster
		If $iP <> 0 Then
			$vInter = StringLeft($vInter, $iP - 1)
		EndIf
		If Not __GCR_IsNumeric($vInter, ">=0") Then Return SetError(1021, 0, False)
		If $iP <> 0 Then ; if in lines
			Switch $vInter
				Case 1
					$iRule = 0
				Case 1.5
					$iRule = 1
				Case 2
					$iRule = 2
				Case Else
					If $vInter < 1 Then Return SetError(1022, 0, False)
					$iRule = 5 ; spacing in lines
					$iLnSp = $vInter * 20
			EndSwitch
		Else
			$iRule = 4 ; spacing in twips
			$iLnSp = $vInter * $_GRE_TwipsPeSpaceUnit
		EndIf
		$iMask = $PFM_LINESPACING
		DllStructSetData($tParaFormat, "bLineSpacingRule", $iRule)
		If $iLnSp <> 0 Then DllStructSetData($tParaFormat, 13, $iLnSp)
	EndIf
	If $iBefore <> Default Then
		$iMask = BitOR($iMask, $PFM_SPACEBEFORE)
		DllStructSetData($tParaFormat, "dySpaceBefore", $iBefore * $_GRE_TwipsPeSpaceUnit)
	EndIf
	If $iAfter <> Default Then
		$iMask = BitOR($iMask, $PFM_SPACEAFTER)
		DllStructSetData($tParaFormat, "dySpaceAfter", $iAfter * $_GRE_TwipsPeSpaceUnit)
	EndIf
	If $iMask <> 0 Then
		DllStructSetData($tParaFormat, "dwMask", $iMask)
		Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
	Else
		Return True
	EndIf
EndFunc   ;==>_GUICtrlRichEdit_SetParaSpacing

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetParaTabStops
; Description....: Sets tab stops  of paragraphs having selected text or, if none selected, sets it for text inserted at the insertion point
; Syntax ........: _GUICtrlRichEdit_SetParaTabStops($hWnd, $sTabStops)
; Parameters.....: $hWnd		- Handle to the control
;                  $sTabStops - A string consisting of groups separated by ; (semicolon). Format of a group:
;                  | absolute position of a tab stop (in space units)
;                  | kind of tab
;                  |   l - left tab
;                  |   c - center tab
;                  |   r - decimal tab
;                  |   b - bar tab
;                  | kind of dot leader
;                  |   . - dotted leader
;                  |   - - dashed leader
;                  |   _ - underline leader
;                  |   = - double line leader
;                  |   t - thick-line leader
;                  |   a space - no leader
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - absolute position of a tab stop missing or invalid
;                  |1022 - kind of tab missing or invalid
;                  |1023 - kind of tab leader missing or invalid
;                  |1024 - attempt to set too many tab stops
;                  |   @extended contains the tab number (ref 1) in $sTabStops where the error occurred.
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To reset the tab stops, do _GUICtrlRichEdit_SetParaTabStops($hWnd, "")
;+
;                  To set "space units", call _GuiCtrlRichEdit_SetSpaceUnits. Initially inches
;+
;                  To enter a tab into a control, press Ctrl_Tab
; Related .......: _GUICtrlRichEdit_GetParaTabStops, _GuiCtrlRichEdit_SetSpaceUnits
; Link ..........: @@MsdnLink@@ EM_SETCHARFORMAT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetParaTabStops($hWnd, $sTabStops)
	Local $asTabs, $asAtab, $i, $s, $iN, $iP
	Local $tParaFormat

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tParaFormat = DllStructCreate($tagPARAFORMAT2)
	DllStructSetData($tParaFormat, "cbSize", DllStructGetSize($tParaFormat))

	If $sTabStops = "" Then
		DllStructSetData($tParaFormat, "cTabCount", 0)
	Else
		$asTabs = StringSplit($sTabStops, ";")
		If $asTabs[0] > $MAX_TAB_STOPS Then Return SetError(1021, 0, False)
		For $iTab = 1 To $asTabs[0]
			$asAtab = StringSplit($asTabs[$iTab], "") ; split into characters
			$i = 1
			While $i <= $asAtab[0] And StringInStr("01234567890.", $asAtab[$i]) <> 0
				$i += 1
			WEnd
			If $i = 1 Then Return SetError(1021, $iTab, False)
			$s = StringLeft($asTabs[$iTab], $i - 1)
			If Not __GCR_IsNumeric($s, ">=0") Then Return SetError(1021, $iTab, False)
			$iN = $s * $_GRE_TwipsPeSpaceUnit
			If $i <= $asAtab[0] Then
				$iP = StringInStr("lcrdb", $asAtab[$i])
				If $iP = 0 Then Return SetError(1022, $iTab, False)
				$iN = BitOR($iN, BitShift($iP - 1, -24))
			EndIf
			$i += 1
			If $i <= $asAtab[0] Then
				$iP = StringInStr(" .-_t=", $asAtab[$i])
				If $iP = 0 Then Return SetError(1023, $iTab, False)
				$iN = BitOR($iN, BitShift($iP - 1, -28))
			EndIf
			DllStructSetData($tParaFormat, "rgxTabs", $iN, $iTab)
		Next
		DllStructSetData($tParaFormat, "cTabCount", $asTabs[0])
	EndIf
	DllStructSetData($tParaFormat, "dwMask", $PFM_TABSTOPS)
	Return __GCR_SendMessage($hWnd, $EM_SETPARAFORMAT, 0, DllStructGetPtr($tParaFormat)) <> 0
EndFunc   ;==>_GUICtrlRichEdit_SetParaTabStops

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlRichEdit_SetPasswordChar
; Description ...: Sets the characters to be displayed instead of those typed, or causes typed characters to show
; Syntax.........: _GUICtrlRichEdit_SetPasswordChar($hWnd[, $cDisplayChar = "0"])
; Parameters ....: $hWnd         - Handle to the control
;                  $cDisplayChar - The character to be displayed in place of the characters typed by the user.
;                  |Special value: "" - characters typed are displayed
; Return values .: Succcess - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $cDisplayChar is not a character
; Author ........: Gary Frost
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......: _GUICtrlRichEdit_GetPasswordChar
; Link ..........; @@MsdnLink@@ EM_SETPASSWORDCHAR
; Example .......; Yes
; ===============================================================================================================================
Func _GUICtrlRichEdit_SetPasswordChar($hWnd, $cDisplayChar)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsString($cDisplayChar) Then SetError(102, 0, False)

	If $cDisplayChar = "" Then
		__GCR_SendMessage($hWnd, $EM_SETPASSWORDCHAR)
	Else
		__GCR_SendMessage($hWnd, $EM_SETPASSWORDCHAR, Asc($cDisplayChar))
	EndIf
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetPasswordChar

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlEdit_SetReadOnly
; Description ...: Sets or removes the read-only state
; Syntax.........: _GuiCtrlRichEdit_SetReadOnly($hWnd[, $fState = True])
; Parameters ....: $hWnd        - Handle to the control
;                  $fState   - one of the following values:
;                  |True       - Sets control to read-only (default)
;                  |False      - Sets control to read-write
; Return values .: Success      - True
;                  Failure      - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $fState is neither true nor false
;                  |700 - operation failed
; Author ........: Gary Frost (gafrost)
; Modified.......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........; @@MsdnLink@@ EM_SETREADONLY
; Example .......; Yes
; ===============================================================================================================================
Func _GuiCtrlRichEdit_SetReadOnly($hWnd, $fState = True)
	Local $iRet
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not IsBool($fState) Then Return SetError(102, 0, False)

	$iRet = __GCR_SendMessage($hWnd, $EM_SETREADONLY, $fState)
	If $iRet = 0 Then Return SetError(700, 0, False)
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_SetReadOnly
; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetScrollPos
; Description....: Scrolls the display such that ($ix,$iY) is in the upper left corner of the control
; Syntax ........: _GUICtrlRichEdit_SetScrollPos($hWnd, $iX, $iY)
; Parameters.....: $hWnd		- Handle to the control
;                  $iX - x coorindate (in pixels)
;                  $iY - y coorindate (in pixels)
; Return values..: Success - True
;                  Failure - False and sets @error
;                  |101 - $hWnd is not a handle
;                  |102 - $iX is neither a positive number nor zero
;                  |103 - $iY is neither a positive number nor zero
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: Never scrolls the text completely off the view rectangle
; Related .......:
; Link ..........: @@MsdnLink@@ EM_SETSCROLLPOS
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetScrollPos($hWnd, $iX, $iY)
	Local $tPoint

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iX, ">=0") Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iY, ">=0") Then Return SetError(103, 0, False)

	$tPoint = DllStructCreate($tagPOINT)
	DllStructSetData($tPoint, 1, $iX)
	DllStructSetData($tPoint, 2, $iY)
	Return __GCR_SendMessage($hWnd, $EM_SETSCROLLPOS, 0, DllStructGetPtr($tPoint), 0) <> 0
EndFunc   ;==>_GUICtrlRichEdit_SetScrollPos

; #FUNCTION# ;=====================================================================================
;
; Name...........: _GUICtrlRichEdit_SetSel
; Description ...: Sets the low and high character position of a selection
; Syntax.........: _GUICtrlRichEdit_SetSel($hWnd, $iAnchor, $iActive[, $fHideSel = False])
; Parameters ....: $hWnd        - Handle to the control
;                  $iAnchor -  the character position of the first character to select
;                  |Special value: -1 - end of text
;                  $iActive -  the character position of the last character to select
;                  |Special value: -1 - end of text
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iAnchor is neither a positive number nor zero nor -1
;                  |103 - $iActive is neither a positive number nor zero nor -1
;                  |104 - $fHideSel must be True or False
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: The first character of the text in a control is at character position 1
;+
;                  $iActive can be less than $iAnchor
; Related .......: _GUICtrlRichEdit_GetSel, _GuiCtrlRichEdit_Deselect, _GuiCtrlRichEdit_IsTextSelected, _GuiCtrlRichEdit_GotoCharPos
; Link ..........; @@MsdnLink@@ EM_EXSETSEL
; Example .......: Yes
;
; =================================================================================================
Func _GUICtrlRichEdit_SetSel($hWnd, $iAnchor, $iActive, $fHideSel = False)
	;Local $tCharRange
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iAnchor, ">=0,-1") Then Return SetError(102, 0, False)
	If Not __GCR_IsNumeric($iActive, ">=0,-1") Then Return SetError(103, 0, False)
	If Not IsBool($fHideSel) Then Return SetError(104, 0, False)
	#cs
		$tCharRange = DllStructCreate($tagCHARRANGE)
		DllStructSetData($tCharRange,"cpMin",$iAnchor)
		DllStructSetData($tCharRange,"cpMax",$iActive)
		__GCR_SendMessage($hWnd, $EM_EXSETSEL, 0, DllStructGetPtr($tCharRange),0,"wparam","ptr")
	#ce
	__GCR_SendMessage($hWnd, $EM_SETSEL, $iAnchor, $iActive)
	If @error Then Return SetError(@error, 0, False)
	If $fHideSel Then __GCR_SendMessage($hWnd, $EM_HIDESELECTION, $fHideSel)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetSel

;#FUNCTION# ;===============================================================================
;
; Name...........: _GuiCtrlRichEdit_SetSpaceUnit
; Description ...: Gets the unit of measure of horizontal and vertical space used in parameters of various _GuiCtrlRichEdit functions
; Syntax.........: _GuiCtrlRichEdit_SetSpaceUnit()
; Parameters ....: $sUnit - "in", "cm", "mm", "pt" (points), or "tw" (twips, 1/1440 inches, 1/567 centimetres
; Return values .: Success - True
;                  Failure - False and sets @error to 1
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: Initially, space is measured in inches
; Related .......:
; Link ..........;
; Example .......: Yes
;
;==================================================================================================
Func _GuiCtrlRichEdit_SetSpaceUnit($sUnit)
	Switch StringLower($sUnit)
		Case "in"
			$_GRE_TwipsPeSpaceUnit = 1440
		Case "cm"
			$_GRE_TwipsPeSpaceUnit = 567
		Case "mm"
			$_GRE_TwipsPeSpaceUnit = 56.7
		Case "pt"
			$_GRE_TwipsPeSpaceUnit = 20
		Case "tw"
			$_GRE_TwipsPeSpaceUnit = 1
		Case Else
			Return SetError(1, 0, False)
	EndSwitch
	Return True
EndFunc   ;==>_GuiCtrlRichEdit_SetSpaceUnit

;#FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetText
; Description....: Sets the text of a control
; Syntax ........: _GUICtrlRichEdit_SetText($hWnd, $sText)
; Parameters.....: hWnd		- Handle to the control
;                  $sText         - Plain or RTF text to put into the control
; Return values..: Succcess - True
;                  |Failure - False, and sets @error:
;                  |101 - $hWnd is not a handle
; Authors........: Gary Frost (gafrost (custompcs@charter.net))
; Modified ......: Prog@ndy, Chris Haslam (c.haslam)
; Remarks .......: Sets all of the text
;                  |Text can be plain or RTF text
;                  |Keeps the undo stack
; Related .......: DllCall, _GUICtrlRichEdit_GetCtrlText, _GUICtrlRichEdit_AppendText, _GUICtrlRichEdit_InsertText,
;                    _GUICtrlRichEdit_ReplaceSel, _GUICtrlRichEdit_EmptyUndoBuffer
; Link ..........:
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetText($hWnd, $sText)
	Local $tSetText

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	$tSetText = DllStructCreate($tagSETTEXTEX)
	;	DllStructSetData($tSetText, 1, $ST_KEEPUNDO)
	DllStructSetData($tSetText, 1, $ST_DEFAULT)
	DllStructSetData($tSetText, 2, $CP_ACP)
	If StringLeft($sText, 5) <> "{\rtf" And StringLeft($sText, 5) <> "{urtf" Then
		DllStructSetData($tSetText, 2, $CP_UNICODE)
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "wstr")
	Else
		__GCR_SendMessage($hWnd, $EM_SETTEXTEX, DllStructGetPtr($tSetText), $sText, 0, "ptr", "str")
	EndIf
	If @error Then Return SetError(@error, 0, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_SetText

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_SetUndoLimit
; Description....: Sets the maximum number of actions that can stored in the undo queue
; Syntax ........: _GUICtrlRichEdit_SetUndoLimit($hWnd, $iLimit)
; Parameters.....: $hWnd		- Handle to the control
;                  $iLimit - the maximum number of actions that can be stored in the undo queue
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |102 - $iLimit is neither a positive number nor zero
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: To disable the Undo feature, set $iLimit to zero
;+
;                  The initial value of $iLimit is 100.
; Related .......: _GUICtrlRichEdit_Undo, _GUICtrlRichEdit_CanRedo, _GUICtrlRichEdit_CanUndo, _GUICtrlRichEdit_GetNextRedo, _GUICtrlRichEdit_Redo
; Link ..........: @@MsdnLink@@ EM_SETUNDOLIMIT
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_SetUndoLimit($hWnd, $iLimit)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	If Not __GCR_IsNumeric($iLimit, ">=0") Then Return SetError(102, 0, False)

	Return __GCR_SendMessage($hWnd, $EM_SETUNDOLIMIT, $iLimit) <> 0 Or $iLimit = 0
EndFunc   ;==>_GUICtrlRichEdit_SetUndoLimit

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_StreamFromFile
; Description....: Sets text in a control from a file
; Syntax ........: _GUICtrlRichEdit_StreamFromFile($hWnd, $sFilespec)
; Parameters.....: $hWnd		- Handle to the control
;                  $sFileSpec - file specification
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1021 - unable to open $sFilespec
;                  |1022 - file is empty
;                  |700  - attempt to stream in too many characters
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If text is selected, replaces selection, else replaces all text in the control
;+
;                  Call _GuiCtrlRichEdit_SetLimitonText to increase the number of characters the control can contain
; Related .......: _GuiCtrlRichEdit_SetLimitonText, _GUICtrlRichEdit_StreamFromVar, _GUICtrlRichEdit_StreamToFile
; Link ..........: @@MsdnLink@@ EM_STREAMIN
; Example .......: Yes
;
;===============================================================================

Func _GUICtrlRichEdit_StreamFromFile($hWnd, $sFilespec)
	Local $hFile, $iQchs, $buf, $iError, $iWparam

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	DllStructSetData($_GRC_tEditStream, 3, DllCallbackGetPtr($_GRC_StreamFromFileCallback))
	$hFile = FileOpen($sFilespec, 0) ; read
	If $hFile = -1 Then Return SetError(1021, 0, False)
	$buf = FileRead($hFile, 5)
	FileClose($hFile)
	$hFile = FileOpen($sFilespec, 0) ; read           reopen it at the start
	DllStructSetData($_GRC_tEditStream, 1, $hFile) ; -> Send handle to CallbackFunc
	$iWparam = _Iif($buf == "{\rtf" Or $buf == "{urtf", $SF_RTF, $SF_TEXT)
	$iWparam = BitOR($iWparam, $SFF_SELECTION)
	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then
		_GUICtrlRichEdit_SetText($hWnd, "")
	EndIf
	$iQchs = __GCR_SendMessage($hWnd, $EM_STREAMIN, $iWparam, DllStructGetPtr($_GRC_tEditStream))
	FileClose($hFile)
	$iError = DllStructGetData($_GRC_tEditStream, 2)
	If $iError <> 1 Then SetError(700, $iError, False)
	If $iQchs = 0 Then
		If FileGetSize($sFilespec) = 0 Then Return SetError(1022, 0, False)
		Return SetError(700, $iError, False)
	EndIf
	Return True
EndFunc   ;==>_GUICtrlRichEdit_StreamFromFile

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_StreamFromVar
; Description....: Sets text in a control from a variable
; Syntax ........: _GUICtrlRichEdit_StreamFromVar($hWnd, $sVar)
; Parameters.....: $hWnd		- Handle to the control
;                  $sVar - a string
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |700  - attempt to stream in too many characters
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If text is selected, replaces selection, else replaces all text in the control
;+
;                  Call _GuiCtrlRichEdit_SetLimitonText to increase the number of characters the control can contain
; Related .......: _GuiCtrlRichEdit_SetLimitonText, _GUICtrlRichEdit_StreamFromFile, _GUICtrlRichEdit_StreamToVar
; Link ..........: @@MsdnLink@@ EM_STREAMIN
; Example .......: Yes
;
;===============================================================================

Func _GUICtrlRichEdit_StreamFromVar($hWnd, $sVar)
	Local $iWparam, $iError, $s

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	DllStructSetData($_GRC_tEditStream, 3, DllCallbackGetPtr($_GRC_StreamFromVarCallback))
	$_GRC_sStreamVar = $sVar
	$s = StringLeft($sVar, 5)
	$iWparam = _Iif($s == "{\rtf" Or $s == "{urtf", $SF_RTF, $SF_TEXT)
	$iWparam = BitOR($iWparam, $SFF_SELECTION)
	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then
		_GUICtrlRichEdit_SetText($hWnd, "")
	EndIf
	__GCR_SendMessage($hWnd, $EM_STREAMIN, $iWparam, DllStructGetPtr($_GRC_tEditStream))
	$iError = DllStructGetData($_GRC_tEditStream, 2)
	If $iError <> 1 Then Return SetError(700, $iError, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_StreamFromVar

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_StreamToFile
; Description....: Writes contens of a control to a file
; Syntax ........: _GUICtrlRichEdit_StreamToFile($hWnd, $sFilespec[, $fIncludeCOM=True[, $iOpts=0[, $iCodePage = 0]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $sFileSpec - file specification
;                  $fIncludeCOM (Optional)
;                  |True (default):
;                  |    If writing to a .rtf file, includes any COM objects (space consuming)
;                  |    If writing to any other file, writes a text represntation of COM objects
;                  |False: Writes spaces instead of COM objects
;                  $iOpts - additional options: (Optional) (default: 0)
;                  |$SFF_PLAINTRTF - write only rich text keywords common to all languages
;                  |$SF_UNICODE    - write Unicode
;                  $iCodePage - Generate UTF-8 and text using this code page (Optional)
;                  |Default: do not
; Return values..: Success - True
;                  Failure - False and sets @error:
;                  |101  - $hWnd is not a handle
;                  |102  - Can't create $sFilespec
;                  |1041 - $SFF_PLAINRTF is invalid for a text file
;                  |1042 - $opts: invalid option
;                  |1043 - $SF_UNICODE is only valid for a text file
;                  |700  - internal error
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If text is selected, writes only the selection, else writes all text in the control
;+
;                  If the extension in $sFileSpec is .rtf, RTF is written, else text
; Related .......: _GuiCtrlRichEdit_SetLimitonText, _GUICtrlRichEdit_StreamFromVar, _GUICtrlRichEdit_StreamToFile
; Link ..........: @@MsdnLink@@ EM_STREAMIN
; Example .......: Yes
;
;===============================================================================

Func _GUICtrlRichEdit_StreamToFile($hWnd, $sFilespec, $fIncludeCOM = True, $iOpts = 0, $iCodePage = 0)
	Local $iWparam, $hFile, $iError

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	If StringRight($sFilespec, 4) = ".rtf" Then
		$iWparam = _Iif($fIncludeCOM, $SF_RTF, $SF_RTFNOOBJS)
	Else
		$iWparam = _Iif($fIncludeCOM, $SF_TEXTIZED, $SF_TEXT)
		If BitAND($iOpts, $SFF_PLAINRTF) Then Return SetError(1041, 0, False)
	EndIf
	; only opts are $SFF_PLAINRTF and $SF_UNICODE
	If BitAND($iOpts, BitNOT(BitOR($SFF_PLAINRTF, $SF_UNICODE))) Then Return SetError(1042, 0, False)
	If BitAND($iOpts, $SF_UNICODE) Then
		If Not BitAND($iWparam, $SF_TEXT) Then Return SetError(1043, 0, False)
	EndIf

	If _GUICtrlRichEdit_IsTextSelected($hWnd) Then $iWparam = BitOR($iWparam, $SFF_SELECTION)

	$iWparam = BitOR($iWparam, $iOpts)
	If $iCodePage <> 0 Then
		$iWparam = BitOR($iWparam, $SF_USECODEPAGE, BitShift($iCodePage, -16))
	EndIf
	DllStructSetData($_GRC_tEditStream, 3, DllCallbackGetPtr($_GRC_StreamToFileCallback))
	$hFile = FileOpen($sFilespec, 2) ; overwrite
	If $hFile - 1 Then Return SetError(102, 0, False)

	DllStructSetData($_GRC_tEditStream, 1, $hFile) ; -> Send handle to CallbackFunc
	__GCR_SendMessage($hWnd, $EM_STREAMOUT, $iWparam, DllStructGetPtr($_GRC_tEditStream))
	FileClose($hFile)
	$iError = DllStructGetData($_GRC_tEditStream, 2)
	If $iError <> 0 Then SetError(700, $iError, False)
	Return True
EndFunc   ;==>_GUICtrlRichEdit_StreamToFile

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_StreamToVar
; Description....: Writes contents of a control to a variable
; Syntax ........: _GUICtrlRichEdit_StreamToVar($hWnd, $fRtf = True[, $fIncludeCOM=True[, $iOpts=0[, $iCodePage = 0]]])
; Parameters.....: $hWnd		- Handle to the control
;                  $fRtf  (Optional)
;                  |True  - write Rich Text Format (RTF) (Default)
;                  |False - write only text
;                  $fIncludeCOM (Optional)
;                  |True (default):
;                  |    If writing RTF, include any COM objects (space consuming)
;                  |    If writing only text, write a text represntation of COM objects
;                  |False: Write spaces instead of COM objects
;                  $iOpts - additional options:
;                  |$SFF_PLAINTRTF - write only rich text keywords common to all languages
;                  |$SF_UNICODE    - write Unicode
;                  $iCodePage - Generate UTF-8 and text using this code page (Optional)
;                  |Default: do not
; Return values..: Success - the RTF or text
;                  Failure - False and sets @error:
;                  |101 - $hWnd is not a handle
;                  |1041 - $SFF_PLAINRTF is invalid for a text file
;                  |1042 - $opts: invalid option
;                  |1043 - $SF_UNICODE is only valid for a text file
;                  |700  - internal error
; Authors........: Chris Haslam (c.haslam)
; Modified ......:
; Remarks .......: If text is selected, writes only the selection, else writes all text in the control
;+
;                  If the extension in $sFileSpec is .rtf, RTF is written, else text
; Related .......: _GuiCtrlRichEdit_SetLimitonText, _GUICtrlRichEdit_StreamFromVar, _GUICtrlRichEdit_StreamToFile
; Link ..........: @@MsdnLink@@ EM_STREAMIN
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_StreamToVar($hWnd, $fRtf = True, $fIncludeCOM = True, $iOpts = 0, $iCodePage = 0)
	Local $iWparam, $iError

	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

	If $fRtf Then
		$iWparam = _Iif($fIncludeCOM, $SF_RTF, $SF_RTFNOOBJS)
	Else
		$iWparam = _Iif($fIncludeCOM, $SF_TEXTIZED, $SF_TEXT)
		If BitAND($iOpts, $SFF_PLAINRTF) Then Return SetError(1041, 0, False)
	EndIf
	; only opts are $SFF_PLAINRTF and $SF_UNICODE
	If BitAND($iOpts, BitNOT(BitOR($SFF_PLAINRTF, $SF_UNICODE))) Then Return SetError(1042, 0, False)
	If BitAND($iOpts, $SF_UNICODE) Then
		If Not BitAND($iWparam, $SF_TEXT) Then Return SetError(1043, 0, False)
	EndIf
	If _GUICtrlRichEdit_IsTextSelected($hWnd) Then $iWparam = BitOR($iWparam, $SFF_SELECTION)

	$iWparam = BitOR($iWparam, $iOpts)
	If $iCodePage <> 0 Then
		$iWparam = BitOR($iWparam, $SF_USECODEPAGE, BitShift($iCodePage, -16))
	EndIf

	DllStructSetData($_GRC_tEditStream, 3, DllCallbackGetPtr($_GRC_StreamToVarCallback))

	$_GRC_sStreamVar = ""
	__GCR_SendMessage($hWnd, $EM_STREAMOUT, $iWparam, DllStructGetPtr($_GRC_tEditStream))
	$iError = DllStructGetData($_GRC_tEditStream, 2)
	If $iError <> 0 Then SetError(700, $iError, False)
	Return $_GRC_sStreamVar
EndFunc   ;==>_GUICtrlRichEdit_StreamToVar

; #FUNCTION# ;===============================================================================
;
; Name ..........: _GUICtrlRichEdit_Undo
; Description....: Undoes the last edit control operation in the control's undo queue
; Syntax ........: _GUICtrlRichEdit_Undo($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - True - undo operation succeeded
;                  Failure - False - undo operation failed. May set @error:
;                  |101 - $hWnd is not a handle
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......: For a single-line edit control, the return value is always True
; Related .......: _GuiCtrlRichEdit_CanUndo, _GuiCtrlRichEdit_GetNextUndo, _GUICtrlRichEdit_Redo
; Link ..........: @@MsdnLink@@ EM_UNDO
; Example .......: Yes
;
;===============================================================================
Func _GUICtrlRichEdit_Undo($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	Return __GCR_SendMessage($hWnd, $EM_UNDO, 0, 0)
EndFunc   ;==>_GUICtrlRichEdit_Undo

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_ConvertTwipsToSpaceUnit
; Description ...: Converts Twips (1/1440 inch) to user space units
; Syntax.........: __GCR_ConvertTwipsToSpaceUnit
; Parameters ....: $nIn - space in twips
; Return values .: Success - value in space units (inches, cm, mm, points or twips)
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......;
; Related .......:
; Link ..........;
; Example .......; No
;
; =================================================================================================
Func __GCR_ConvertTwipsToSpaceUnit($nIn)
	Local $ret
	Switch $_GRE_TwipsPeSpaceUnit
		Case 1440, 567 ; inches, cm
			$ret = StringFormat("%.2f", $nIn / $_GRE_TwipsPeSpaceUnit)
			If $ret = "-0.00" Then $ret = "0.00"
		Case 56.7, 72 ; mm, points
			$ret = StringFormat("%.1f", $nIn / $_GRE_TwipsPeSpaceUnit)
			If $ret = "-0.0" Then $ret = "0.0"
		Case Else
			$ret = $nIn
	EndSwitch
	Return $ret
EndFunc   ;==>__GCR_ConvertTwipsToSpaceUnit

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_ConvertColorRefToRGBString
; Description ...: Converts a COLORREF value to RGB
; Syntax.........: __GCR_ConvertTwipsToSpaceUnit($iColor)
; Parameters ....: $iColor - COLORREF value
; Return values .: Success - a string of color component values, as percentages, separated by semicolons (:):
;                  |value 1 - red
;                  |value 2 - green
;                  |value 3 - blue
;                  Failure - does not fail
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......;
; Related .......:
; Link ..........; @@MsdnLink@@ COLORREF
; Example .......; No
;
; =================================================================================================
Func __GCR_ConvertColorRefToRGBString($iColor)
	Local $sRet
	$sRet = Round(BitAND($iColor, 0xFF) * 100 / 255, 0) & ";"
	$sRet &= Round(BitShift(BitAND($iColor, 0xFF00), 8) * 100 / 255, 0) & ";"
	$sRet &= Round(BitShift(BitAND($iColor, 0xFF0000), 16) * 100 / 255, 0)
	Return $sRet
EndFunc   ;==>__GCR_ConvertColorRefToRGBString

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_IsNumeric
; Description ...: Does a variable contain a numeric value?
; Syntax.........: __GCR_IsNumeric($vN)
; Parameters ....: $VN - the variable
;                  $SRange - ">0", ">=0", ">0,-1", ">=0,-1"
; Return values .: Success - True or False
;                  Failure - can't fail
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......;
; Related .......:
; Link ..........;
; Example .......; No
;
; =================================================================================================
Func __GCR_IsNumeric($vN, $sRange = "")
	If Not (IsNumber($vN) Or StringIsInt($vN) Or StringIsFloat($vN)) Then Return False
	Switch $sRange
		Case ">0"
			If $vN <= 0 Then Return False
		Case ">=0"
			If $vN < 0 Then Return False
		Case ">0,-1"
			If Not ($vN > 0 Or $vN = -1) Then Return False
		Case ">=0,-1"
			If Not ($vN >= 0 Or $vN = -1) Then Return False
	EndSwitch
	Return True
EndFunc   ;==>__GCR_IsNumeric

; #INTERNAL_USE_ONLY# =============================================================================
;
; Name...........: __GCR_GetParaScopeChar
; Description ...: Gets the scope to which paragraph format settings apply
; Syntax.........:  __GCR_GetParaScopeChar($hWnd, $iMask, $iPFM)
; Parameters ....: $hWnd - handle to control
;                  $iMask - mask returned by __GCR_SendMessage
; Return values .: Success - the scope character
;                  Failure - can't fail
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......; Takes advantage of an undocumented feature of EM_GETPARAFORMAT
; Related .......:
; Link ..........;
; Example .......; No
;
; =================================================================================================
Func __GCR_GetParaScopeChar($hWnd, $iMask, $iPFM)
	If Not _GUICtrlRichEdit_IsTextSelected($hWnd) Then
		Return "c"
	ElseIf BitAND($iMask, $iPFM) = $iPFM Then
		Return "a"
	Else
		Return "f"
	EndIf
EndFunc   ;==>__GCR_GetParaScopeChar

; #INTERNAL_USE_ONLY# =============================================================================
; Name...........: __GCR_ParseParaNumberingStyle
; Description ...: For _GuiCtrlRichEdit_SetParaNumbering(), parses $vStyle
; Syntax.........: __GCR_ParseParaNumberingStyle($sIn, $fForceRoman, ByRef $iPFM, ByRef $iWNumbering, ByRef $iWnumStart, ByRef $iWnumStyle, ByRef $iQspaces)
; Parameters ....: $sIn - style string: see _GuiCtrlRichEdit_SetParaNumbering()
;                  $ForceRoman - If $vStyle contains numner i, interpret as Roman one else as letter i
;                  $iPFM - BitOr combination of $PFM_ constants (Returned)
;                  $iWNumbering - wNumbering member of PARAFORMAT2 structure
;                  $iWnumStart - wNumbering Start  member of PARAFORMAT2 structure (Returned)
;                  $iWNumStyle - wNumberingStyle  member of PARAFORMAT2 structure  (Returned)
;                  $iQspaces - for wNumberingTab  member of PARAFORMAT2 structure  (Returned)
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |102 - $sIn is invalid
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......;
; Related .......: GuiCtrlRichEdit_SetParaNumbering()
; Link ..........; @@MsdnLink@@ EM_PARAMFORMAT
; Example .......; No
; =====================================================
Func __GCR_ParseParaNumberingStyle($sIn, $fForceRoman, ByRef $iPFM, ByRef $iWNumbering, ByRef $iWnumStart, ByRef $iWnumStyle, ByRef $iQspaces)
	Local Const $sRoman = "mdclxviMDCLXVI", $kRpar = 0, $k2par = 0x100, $kPeriod = 0x200, $kNbrOnly = 0x300
	Local $fMayBeRoman, $fIsRoman, $iN, $s
	If $sIn = "" Then
		$iWNumbering = 0
		$iPFM = $PFM_NUMBERING
	Else
		$s = StringStripWS($sIn, 2) ; trialing whitespace
		$iQspaces = StringLen($sIn) - StringLen($s)
		$sIn = $s
		$iPFM = $PFM_NUMBERINGTAB
		If $sIn = "." Then
			$iWNumbering = $PFN_BULLET
			$iPFM = BitOR($iPFM, $PFM_NUMBERING)
		ElseIf $sIn = "=" Then
			$iWnumStyle = 0x400
			$iPFM = BitOR($iPFM, $PFM_NUMBERINGSTYLE)
		Else
			Switch StringRight($sIn, 1)
				Case ")"
					If StringLeft($sIn, 1) = "(" Then
						$iWnumStyle = $k2par
						$sIn = StringTrimLeft($sIn, 1)
					Else
						$iWnumStyle = $kRpar
					EndIf
				Case "."
					$iWnumStyle = $kPeriod
				Case Else ; display only number
					$iWnumStyle = $kNbrOnly
			EndSwitch
			$iPFM = BitOR($iPFM, $PFM_NUMBERINGSTYLE)
			If $iWnumStyle <> 0x300 Then $sIn = StringTrimRight($sIn, 1)
			If StringIsDigit($sIn) Then
				$iWnumStart = Number($sIn)
				$iWNumbering = 2
				$iPFM = BitOR($iPFM, $PFM_NUMBERINGSTART, $PFM_NUMBERING)
			Else
				$fMayBeRoman = True
				For $i = 1 To StringLen($sIn)
					If Not StringInStr($sRoman, StringMid($sIn, $i, 1)) Then
						$fMayBeRoman = False
						ExitLoop
					EndIf
				Next
				If $fMayBeRoman Then
					$fIsRoman = $fForceRoman
				Else
					$fIsRoman = False
				EndIf
				Switch True
					Case $fIsRoman
						$iWnumStart = __GCR_ConvertRomanToNumber($sIn)
						If $iWnumStart = -1 Then Return SetError(102, 0, False)
						$iWNumbering = _Iif(StringIsLower($sIn), 5, 6)
						$iPFM = BitOR($iPFM, $PFM_NUMBERINGSTART, $PFM_NUMBERING)
					Case StringIsAlpha($sIn)
						If StringIsLower($sIn) Then
							$iWNumbering = 3
						Else
							$iWNumbering = 4
							$sIn = StringLower($sIn)
						EndIf
						$iWnumStart = 0
						For $iP = 1 To StringLen($sIn)
							$iN = Asc(StringMid($sIn, $i))
							If $iN >= Asc("a") And $iN <= Asc("z") Then
								$iWnumStart = $iWnumStart * 26 + ($iN - Asc("a") + 1)
							EndIf
						Next
						$iPFM = BitOR($iPFM, $PFM_NUMBERINGSTART, $PFM_NUMBERING)
					Case Else
						Return SetError(102, 0, False)
				EndSwitch
			EndIf
		EndIf
	EndIf
	Return True
EndFunc   ;==>__GCR_ParseParaNumberingStyle

; #INTERNAL_USE_ONLY# =============================================================================
; Name...........: __GCR_ConvertRomanToNumber
; Description ...: Converts a Roman number to a number
; Syntax.........: __GCR_ConvertRomanToNumber($sRnum)
; Parameters ....: $sRnum - string containing Roman number
; Return values .: Success - the (Arabic) number
;                  Failure - -1
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......; Is case-insensitive
; Related .......:
; Link ..........;
; Example .......; No
; =====================================================
Func __GCR_ConvertRomanToNumber($sRnum)
	Local $i = 1, $iRet, $iDigit, $iQ1s
	Local Enum $k9, $k5, $k4, $k1, $kMult, $kHigher
	Local Const $av[3][6] = [["cm", "d", "cd", "c", 100, "m"],["xc", "l", "xl", "x", 10, "mdc"],["ix", "v", "iv", "i", 1, "mdclx"]]
	$sRnum = StringLower($sRnum)
	While StringMid($sRnum, $i, 1) = "m"
		$i += 1
	WEnd
	$iRet = ($i - 1) * 1000
	For $j = 0 To 2
		$iDigit = 0
		If StringMid($sRnum, $i, 2) = $av[$j][$k9] Then
			$iDigit = 9
			$i += 2
		ElseIf StringMid($sRnum, $i, 1) = $av[$j][$k5] Then
			$iDigit = 5
			$i += 1
		ElseIf StringMid($sRnum, $i, 2) = $av[$j][$k4] Then
			$iDigit = 4
			$i += 2
		ElseIf StringInStr($av[$j][$kHigher], StringMid($sRnum, $i, 1)) Then
			Return -1
		EndIf
		If $iDigit = 0 Or $iDigit = 5 Then
			$iQ1s = 0
			While StringMid($sRnum, $i, 1) = $av[$j][$k1]
				$iQ1s += 1
				If $iQ1s > 3 Then Return 0
				$i += 1
			WEnd
			$iDigit += $iQ1s
		EndIf
		$iRet += $iDigit * $av[$j][$kMult]
	Next
	If $i <= StringLen($sRnum) Then Return -1
	Return $iRet
EndFunc   ;==>__GCR_ConvertRomanToNumber

; #INTERNAL_USE_ONLY# =============================================================================
; Name...........: __GCR_SendGetCharFormatMessage
; Description ...: Gets character format of character just after the anchor point (if text is selected) or after the inserton point
; Syntax.........: __GCR_SendGetCharFormatMessage($hWnd, $tCharFormat)
; Parameters ....: $hWnd - handle of control
;                : $tCharFormat - CHARFORMAT or CHARFORMAT2 structure
; Return values .: Success - True
;                  Failure - error of _SendMessage EM_CHARFORMAT SCF_SELECTION
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......; If there is a selection, restores it before returning, with the anchor and actove positions correct,
;                  even if active < anchor
; Related .......:
; Link ..........; @@MsdnLink@@ EM_CHARFORMAT
; Example .......; No
; =====================================================
Func __GCR_SendGetCharFormatMessage($hWnd, $tCharFormat)
	Local $fIsSel, $aiAnchAct, $iError, $iResult
	$fIsSel = _GUICtrlRichEdit_IsTextSelected($hWnd)
	If $fIsSel Then
		$aiAnchAct = StringSplit(_GuiCtrlRichEdit_GetSelAA($hWnd), ";")
		_GUICtrlRichEdit_SetSel($hWnd, $aiAnchAct[1], $aiAnchAct[1] + 1, True) ; select first char, hiding selection
	EndIf
	$iResult = __GCR_SendMessage($hWnd, $EM_GETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($tCharFormat))
	$iError = @error
	If $fIsSel Then
		_GUICtrlRichEdit_SetSel($hWnd, $aiAnchAct[1], $aiAnchAct[2])
	EndIf
	If $iError Then Return SetError($iError, 0, False)
	Return $iResult
EndFunc   ;==>__GCR_SendGetCharFormatMessage

; #INTERNAL_USE_ONLY# =============================================================================
; Name...........: __GCR_SendGetParaFormatMessage
; Description ...: Gets format of (first) selected paragraph or, if no selection, of the paragraph containing the insertion point
; Syntax.........: __GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
; Parameters ....: $hWnd - handle of control
;                : $tParaFormat - PARAFORMAT or PARAFORMAT2 structure
; Return values .: Success - True
;                  Failure - error of _SendMessage EM_PARAFORMAT
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......;
; Related .......:
; Link ..........; @@MsdnLink@@ EM_PARAFORMAT
; Example .......; No
; =====================================================
Func __GCR_SendGetParaFormatMessage($hWnd, $tParaFormat)
	Local $fIsSel, $iError, $as, $iN, $iInsPt
	$fIsSel = _GUICtrlRichEdit_IsTextSelected($hWnd)
	If Not $fIsSel Then
		$as = StringSplit(_GUICtrlRichEdit_GetSel($hWnd), ";")
		$iInsPt = $as[1]
		$iN = _GUICtrlRichEdit_GetFirstCharPosOnLine($hWnd)
		_GUICtrlRichEdit_SetSel($hWnd, $iN, $iN + 1, True)
	EndIf

	__GCR_SendMessage($hWnd, $EM_GETPARAFORMAT, 0, DllStructGetPtr($tParaFormat), 0, "wparam", "ptr")
	$iError = @error

	If Not $fIsSel Then
		_GUICtrlRichEdit_SetSel($hWnd, $iInsPt, $iInsPt)
	EndIf
	If $iError Then Return SetError($iError, 0, False)
	Return True
EndFunc   ;==>__GCR_SendGetParaFormatMessage

; Author ........: Paul Campbell (PaulIA)
Func __GCR_InvalidateRect($hWnd, $tRect = 0, $fErase = True)
	Local $pRect, $aResult
	If $tRect <> 0 Then $pRect = DllStructGetPtr($tRect)
	$aResult = DllCall("User32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", $pRect, "int", $fErase)
	Return $aResult[0] <> 0
EndFunc   ;==>__GCR_InvalidateRect
#include <SendMessage.au3>
; Functions translated from http://www.powerbasic.com/support/pbforums/showpost.php?p=294112&postcount=7
; by Prog@ndy
#include-once
Global $pObj_RichComObject = DllStructCreate("ptr pIntf; dword  Refcount")
Global $pCall_RichCom, $pObj_RichCom
Global $hLib_RichCom_OLE32 = DllOpen("OLE32.DLL")
Global Const $_GCR_S_OK = 0
Global Const $_GCR_E_NOTIMPL = 0x80004001
Global Const $_GCR_E_INVALIDARG = 0x80070057
Global $__RichCom_Object_QueryInterface = DllCallbackRegister("__RichCom_Object_QueryInterface", "long", "ptr;dword;dword")
Global $__RichCom_Object_AddRef = DllCallbackRegister("__RichCom_Object_AddRef", "long", "ptr")
Global $__RichCom_Object_Release = DllCallbackRegister("__RichCom_Object_Release", "long", "ptr")
Global $__RichCom_Object_GetNewStorage = DllCallbackRegister("__RichCom_Object_GetNewStorage", "long", "ptr;ptr")
Global $__RichCom_Object_GetInPlaceContext = DllCallbackRegister("__RichCom_Object_GetInPlaceContext", "long", "ptr;dword;dword;dword")
Global $__RichCom_Object_ShowContainerUI = DllCallbackRegister("__RichCom_Object_ShowContainerUI", "long", "ptr;long")
Global $__RichCom_Object_QueryInsertObject = DllCallbackRegister("__RichCom_Object_QueryInsertObject", "long", "ptr;dword;ptr;long")
Global $__RichCom_Object_DeleteObject = DllCallbackRegister("__RichCom_Object_DeleteObject", "long", "ptr;ptr")
Global $__RichCom_Object_QueryAcceptData = DllCallbackRegister("__RichCom_Object_QueryAcceptData", "long", "ptr;ptr;dword;dword;dword;ptr")
Global $__RichCom_Object_ContextSensitiveHelp = DllCallbackRegister("__RichCom_Object_ContextSensitiveHelp", "long", "ptr;long")
Global $__RichCom_Object_GetClipboardData = DllCallbackRegister("__RichCom_Object_GetClipboardData", "long", "ptr;ptr;dword;ptr")
Global $__RichCom_Object_GetDragDropEffect = DllCallbackRegister("__RichCom_Object_GetDragDropEffect", "long", "ptr;dword;dword;dword")
Global $__RichCom_Object_GetContextMenu = DllCallbackRegister("__RichCom_Object_GetContextMenu", "long", "ptr;short;ptr;ptr;ptr")

; #INTERNAL USE ONLY# ;===============================================================================
;
; Name ..........: __GCR_SetOLECallback
; Description....: Enables OLE-relationed functionality
; Syntax ........: _GUICtrlRichEdit_SetOLECallback($hWnd)
; Parameters.....: $hWnd		- Handle to the control
; Return values..: Success - True
;                  Failure - False and set @error:
;                  |101 - $hWnd is not a handle
;                  |700 - internal error
; Authors........: Prog@ndy
; Modified ......: Chris Haslam (c.haslam)
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ EM_SETOLECALLBACK
; Example .......:
;
;===============================================================================
Func __GCR_SetOLECallback($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)
	;'// Initialize the OLE part.
	If Not $pObj_RichCom Then
		$pCall_RichCom = DllStructCreate("ptr[20]");  '(With some extra space for the future)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_QueryInterface), 1)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_AddRef), 2)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_Release), 3)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_GetNewStorage), 4)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_GetInPlaceContext), 5)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_ShowContainerUI), 6)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_QueryInsertObject), 7)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_DeleteObject), 8)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_QueryAcceptData), 9)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_ContextSensitiveHelp), 10)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_GetClipboardData), 11)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_GetDragDropEffect), 12)
		DllStructSetData($pCall_RichCom, 1, DllCallbackGetPtr($__RichCom_Object_GetContextMenu), 13)
		DllStructSetData($pObj_RichComObject, 1, DllStructGetPtr($pCall_RichCom))
		DllStructSetData($pObj_RichComObject, 2, 1)
		$pObj_RichCom = DllStructGetPtr($pObj_RichComObject)
	EndIf
	Local $EM_SETOLECALLBACK = 0x400 + 70
	If __GCR_SendMessage($hWnd, $EM_SETOLECALLBACK, 0, $pObj_RichCom) = 0 Then Return SetError(700, 0, False)
	Return True
EndFunc   ;==>__GCR_SetOLECallback
;~ '/////////////////////////////////////
;~ '// OLE stuff, don't use yourself..
;~ '/////////////////////////////////////
;~ '// Useless procedure, never called..
; INTERNAL USE ONLY
; __RichCom_Object_QueryInterface( $pObject As Dword, $REFIID As Dword, $ppvObj As Dword ) As Dword
Func __RichCom_Object_QueryInterface($pObject, $REFIID, $ppvObj)
	$pObject = $pObject ; to satisfy Au3Check
	$REFIID = $REFIID ; to satisfy Au3Check
	$ppvObj = $ppvObj ; to satisfy Au3Check
	Return $_GCR_S_OK
EndFunc   ;==>__RichCom_Object_QueryInterface
; INTERNAL USE ONLY
; __RichCom_Object_AddRef( $pObject As Dword Ptr ) As Dword
Func __RichCom_Object_AddRef($pObject)
;~ Exit Function
	Local $data = DllStructCreate("ptr;dword", $pObject)
	DllStructSetData($data, 2, DllStructGetData($data, 2) + 1)
	Return DllStructGetData($data, 2)
EndFunc   ;==>__RichCom_Object_AddRef
; INTERNAL USE ONLY
; __RichCom_Object_Release( ByVal pObject As Dword Ptr ) As Dword
Func __RichCom_Object_Release($pObject)
;~ Exit Function
	Local $data = DllStructCreate("ptr;dword", $pObject)
	If DllStructGetData($data, 2) > 0 Then
		DllStructSetData($data, 2, DllStructGetData($data, 2) - 1)
		Return DllStructGetData($data, 2)
	EndIf
;~     If @pObject[1] > 0 Then
;~         Decr @pObject[1]
;~         Func = @pObject[1]
;~     Else
;~         pObject = 0
;~     End If
EndFunc   ;==>__RichCom_Object_Release
; INTERNAL USE ONLY
; __RichCom_Object_GetInPlaceContext( ByVal pObject As Dword Ptr, lplpFrame As Dword, lplpDoc As Dword, lpFrameInfo As Dword ) As Dword
Func __RichCom_Object_GetInPlaceContext($pObject, $lplpFrame, $lplpDoc, $lpFrameInfo)
	$pObject = $pObject ; to satisfy Au3Check
	$lplpFrame = $lplpFrame ; to satisfy Au3Check
	$lplpDoc = $lplpDoc ; to satisfy Au3Check
	$lpFrameInfo = $lpFrameInfo ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_GetInPlaceContext
; INTERNAL USE ONLY
; __RichCom_Object_ShowContainerUI( ByVal pObject As Dword Ptr, fShow As Long ) As Dword
Func __RichCom_Object_ShowContainerUI($pObject, $fShow)
	$pObject = $pObject ; to satisfy Au3Check
	$fShow = $fShow ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_ShowContainerUI
; INTERNAL USE ONLY
; ~ Func __RichCom_Object_QueryInsertObject( ByVal pObject As Dword Ptr, lpclsid As Dword, ByVal lpstg As Dword Ptr, cp As Long ) As Dword
Func __RichCom_Object_QueryInsertObject($pObject, $lpclsid, $lpstg, $cp)
	$pObject = $pObject ; to satisfy Au3Check
	$lpclsid = $lpclsid ; to satisfy Au3Check
	$lpstg = $lpstg ; to satisfy Au3Check
	$cp = $cp ; to satisfy Au3Check
	Return $_GCR_S_OK
EndFunc   ;==>__RichCom_Object_QueryInsertObject
; INTERNAL USE ONLY
; __RichCom_Object_DeleteObject( ByVal pObject As Dword Ptr, lpoleobj As Dword ) As Dword
Func __RichCom_Object_DeleteObject($pObject, $lpoleobj)
	$pObject = $pObject ; to satisfy Au3Check
	$lpoleobj = $lpoleobj ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_DeleteObject
; INTERNAL USE ONLY
; __RichCom_Object_QueryAcceptData( ByVal pObject As Dword Ptr, lpdataobj As Dword, lpcfFormat As Dword, reco As Dword, fReally As Long, hMetaPict As Dword ) As Dword
Func __RichCom_Object_QueryAcceptData($pObject, $lpdataobj, $lpcfFormat, $reco, $fReally, $hMetaPict)
	$pObject = $pObject ; to satisfy Au3Check
	$lpdataobj = $lpdataobj ; to satisfy Au3Check
	$lpcfFormat = $lpcfFormat ; to satisfy Au3Check
	$reco = $reco ; to satisfy Au3Check
	$fReally = $fReally ; to satisfy Au3Check
	$hMetaPict = $hMetaPict ; to satisfy Au3Check
	Return $_GCR_S_OK
EndFunc   ;==>__RichCom_Object_QueryAcceptData
; INTERNAL USE ONLY
; __RichCom_Object_ContextSensitiveHelp( ByVal pObject As Dword Ptr, fEnterMode As Long ) As Dword
Func __RichCom_Object_ContextSensitiveHelp($pObject, $fEnterMode)
	$pObject = $pObject ; to satisfy Au3Check
	$fEnterMode = $fEnterMode ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_ContextSensitiveHelp
; INTERNAL USE ONLY
; __RichCom_Object_GetClipboardData( ByVal pObject As Dword Ptr, lpchrg As Dword, reco As Dword, lplpdataobj As Dword ) As Dword
Func __RichCom_Object_GetClipboardData($pObject, $lpchrg, $reco, $lplpdataobj)
	$pObject = $pObject ; to satisfy Au3Check
	$lpchrg = $lpchrg ; to satisfy Au3Check
	$reco = $reco ; to satisfy Au3Check
	$lplpdataobj = $lplpdataobj ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_GetClipboardData
; INTERNAL USE ONLY
; __RichCom_Object_GetDragDropEffect( ByVal pObject As Dword Ptr, fDrag As Long, grfKeyState As Dword, pdwEffect As Dword ) As Dword
Func __RichCom_Object_GetDragDropEffect($pObject, $fDrag, $grfKeyState, $pdwEffect)
	$pObject = $pObject ; to satisfy Au3Check
	$fDrag = $fDrag ; to satisfy Au3Check
	$grfKeyState = $grfKeyState ; to satisfy Au3Check
	$pdwEffect = $pdwEffect ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_GetDragDropEffect
; INTERNAL USE ONLY
; __RichCom_Object_GetContextMenu( ByVal pObject As Dword Ptr, seltype As Word, lpoleobj As Dword, lpchrg As Dword, lphmenu As Dword ) As Dword
Func __RichCom_Object_GetContextMenu($pObject, $seltype, $lpoleobj, $lpchrg, $lphmenu)
	$pObject = $pObject ; to satisfy Au3Check
	$seltype = $seltype ; to satisfy Au3Check
	$lpoleobj = $lpoleobj ; to satisfy Au3Check
	$lpchrg = $lpchrg ; to satisfy Au3Check
	$lphmenu = $lphmenu ; to satisfy Au3Check
	Return $_GCR_E_NOTIMPL
EndFunc   ;==>__RichCom_Object_GetContextMenu
; INTERNAL USE ONLY
; __RichCom_Object_GetNewStorage( ByVal pObject As Dword Ptr, lplpstg As Dword ) As Dword
Func __RichCom_Object_GetNewStorage($pObject, $lplpstg)
	Local $sc ; As Dword
	Local $lpLockBytes; As Dword Ptr
	$pObject = $pObject ; to satisfy Au3Check
;~     If pCall_RichCom_CreateILockBytesOnHGlobal = 0 Or pCall_RichCom_StgCreateDocfileOnILockBytes = 0 Then Exit Function
	$sc = DllCall($hLib_RichCom_OLE32, "dword", "CreateILockBytesOnHGlobal", "hwnd", 0, "int", 1, "ptr*", 0)
	$lpLockBytes = $sc[3]
	$sc = $sc[0]
;~     Call Dword pCall_RichCom_CreateILockBytesOnHGlobal Using _
;~         RichCom_CreateILockBytesOnHGlobal( ByVal 0&, ByVal 1&, lpLockBytes ) To sc
	If $sc Then Return $sc
	$sc = DllCall($hLib_RichCom_OLE32, "dword", "StgCreateDocfileOnILockBytes", "ptr", $lpLockBytes, "dword", BitOR(0x10, 2, 0x1000), "dword", 0, "ptr*", 0)
	Local $lpstg = DllStructCreate("ptr", $lplpstg)
	DllStructSetData($lpstg, 1, $sc[4])
	$sc = $sc[0]
;~     Call Dword pCall_RichCom_StgCreateDocfileOnILockBytes Using _
;~         RichCom_StgCreateDocfileOnILockBytes( _
;~           @lpLockBytes _
;~         , ByVal %STGM_SHARE_EXCLUSIVE Or %STGM_READWRITE Or %STGM_CREATE _
;~         , ByVal 0& _
;~         , lplpstg _
;~         ) To sc
	If $sc Then
		Local $obj = DllStructCreate("ptr", $lpLockBytes)
		Local $iUnknownFuncTable = DllStructCreate("ptr[3]", DllStructGetData($obj, 1))
		Local $lpReleaseFunc = DllStructGetData($iUnknownFuncTable, 3)
		Call("MemoryFuncCall" & "", "long", $lpReleaseFunc, "ptr", $lpLockBytes)
		If @error = 1 Then ConsoleWrite("!> Needs MemoryDLL.au3 for correct release of ILockBytes" & @CRLF)
	EndIf
;~ '   If sc Then Call Dword @@lpLockBytes[2] Using __RichCom_Object_Release( @lpLockBytes )
	Return $sc
EndFunc   ;==>__RichCom_Object_GetNewStorage

; #INTERNAL_USE_ONLY# =============================================================================
; Name...........: __GCR__GCR_SendMessage
; Description ...: Wrapper for commonly used Dll Call
; Syntax.........: __GCR_SendMessage($hWnd, $iMsg[, $wParam = 0[, $lParam = 0[, $iReturn = 0[, $wParamType = "wparam"[, $lParamType = "lparam"[, $sReturnType = "lparam"]]]]]])
; Parameters ....: $hWnd       - Window/control handle
;                  $iMsg       - Message to send to control (number)
;                  $wParam     - Specifies additional message-specific information
;                  $lParam     - Specifies additional message-specific information
;                  $iReturn    - What to return:
;                  |0 - Return value from dll call
;                  |1 - $ihWnd
;                  |2 - $iMsg
;                  |3 - $wParam
;                  |4 - $lParam
;                  |<0 or > 4 - array same as dllcall
;                  $wParamType - See DllCall in Related
;                  $lParamType - See DllCall in Related
;                  $sReturnType - See DllCall in Related
; Return values .: Success      - User selected value from the DllCall() result
;                  Failure      - @error is set
; Author ........: Chris Haslam (c.haslam)
; Modified.......:
; Remarks .......: Fast version of __GCR_SendMessage (Valik, Gary Frost (GaryFrost) aka gafrost) for Rich Edit
; Related .......:
; Link ..........;
; Example .......;
; ===============================================================================================================================
Func __GCR_SendMessage($hWnd, $iMsg, $wParam = 0, $lparam = 0, $iReturn = 0, $wParamType = "wparam", $lParamType = "lparam", $sReturnType = "lparam")
	If $_GRE_hUser32dll = 0 Then $_GRE_hUser32dll = DllOpen("user32.dll")
	Local $aResult = DllCall($_GRE_hUser32dll, $sReturnType, "SendMessage", "hwnd", $hWnd, "int", $iMsg, $wParamType, $wParam, $lParamType, $lparam)
	If @error Then Return SetError(@error, @extended, "")
	If $iReturn >= 0 And $iReturn <= 4 Then Return $aResult[$iReturn]
	Return $aResult
EndFunc   ;==>__GCR_SendMessage


Opt("MustDeclareVars", $_GRE_oldMustDeclareVars)
