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

Global $hRichEdit, $mnu, $mnuUndo, $mnuRedo, $mnuCut, $mnuCopy
Global $mnuPaste, $mnuPasteSpl, $mnuPasteSplRTF, $mnuPasteSplwObjs
Global $iStatusBar, $iCount = 0

Main()

Func Main()
	Local $hGui
	$hGui = GUICreate(StringTrimRight(@ScriptName, 4), 420, 350, -1, -1)
	$iStatusBar = GUICtrlCreateLabel('Строка состояния', 3, 350 - 20, 410, 20)
	$hRichEdit = _GUICtrlRichEdit_Create($hGui, "Это тест.", 10, 10, 400, 220, _
			BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
	GUISetState(@SW_SHOW)

	_GUICtrlRichEdit_AutoDetectURL($hRichEdit, True)
	_GUICtrlRichEdit_AppendText($hRichEdit, @CR & "http://www.autoitscript.com")
	_GUICtrlRichEdit_SetSel($hRichEdit, 10, 10 + 27)
	_GUICtrlRichEdit_SetCharAttributes($hRichEdit, "+li")
	_GUICtrlRichEdit_Deselect($hRichEdit)

	GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

	$mnu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
	$mnuUndo = GUICtrlCreateMenuItem("Отмена", $mnu)
	$mnuRedo = GUICtrlCreateMenuItem("Повтор", $mnu)
	GUICtrlCreateMenuItem("", $mnu)
	$mnuCut = GUICtrlCreateMenuItem("Вырезать", $mnu)
	$mnuCopy = GUICtrlCreateMenuItem("Копировать", $mnu)
	$mnuPaste = GUICtrlCreateMenuItem("Вставить", $mnu)
	$mnuPasteSpl = GUICtrlCreateMenu("Специальная вставка", $mnu)
	$mnuPasteSplRTF = GUICtrlCreateMenuItem("Только RTF", $mnuPasteSpl)
	$mnuPasteSplwObjs = GUICtrlCreateMenuItem("С объектами", $mnuPasteSpl)
	_GUICtrlRichEdit_SetEventMask($hRichEdit, BitOR($ENM_MOUSEEVENTS, $ENM_LINK, $ENM_REQUESTRESIZE))

	While True
		Switch GUIGetMsg()
			Case $GUI_EVENT_CLOSE
				_GUICtrlRichEdit_Destroy($hRichEdit)
				GUIDelete()
				Exit
			Case $mnuUndo
				_GUICtrlRichEdit_Undo($hRichEdit)
			Case $mnuRedo
				_GUICtrlRichEdit_Redo($hRichEdit)
			Case $mnuCut
				_GUICtrlRichEdit_Cut($hRichEdit)
			Case $mnuCopy
				_GUICtrlRichEdit_Copy($hRichEdit)
			Case $mnuPaste
				_GUICtrlRichEdit_Paste($hRichEdit)
			Case $mnuPasteSplRTF
				_GUICtrlRichEdit_PasteSpecial($hRichEdit, False)
			Case $mnuPasteSplwObjs
				_GUICtrlRichEdit_PasteSpecial($hRichEdit, True)
		EndSwitch
	WEnd
EndFunc   ;==>Main

Func WM_NOTIFY($hWnd, $iMsg, $iWparam, $iLparam)
	#forceref $iMsg, $iWparam
	$iCount += 1
	Local $hWndFrom, $iCode, $tNMHDR, $tMsgFilter, $hMenu
	$tNMHDR = DllStructCreate($tagNMHDR, $iLparam)
	$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
	$iCode = DllStructGetData($tNMHDR, "Code")
	Switch $hWndFrom
		Case $hRichEdit
			Switch $iCode
				Case $EN_MSGFILTER
					$tMsgFilter = DllStructCreate($tagMSGFILTER, $iLparam)
					Switch DllStructGetData($tMsgFilter, "msg")
						Case $WM_RBUTTONUP
							$hMenu = GUICtrlGetHandle($mnu)
							_SetMenuTexts($hWndFrom, $hMenu)
							_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd)
							GUICtrlSetData($iStatusBar, '$WM_RBUTTONUP ' & $iCount)
					EndSwitch
				Case $EN_LINK
					GUICtrlSetData($iStatusBar, '$EN_LINK ' & $iCount)
				Case $EN_REQUESTRESIZE
					GUICtrlSetData($iStatusBar, '$EN_REQUESTRESIZE ' & $iCount)
			EndSwitch
	EndSwitch
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _SetMenuTexts($hWnd, $hMenu)
	Local $fState
	If _GUICtrlRichEdit_CanUndo($hWnd) Then
		_GUICtrlMenu_SetItemEnabled($hMenu, $mnuUndo, True, False)
		_GUICtrlMenu_SetItemText($hMenu, $mnuUndo, "Отмена: " & _GUICtrlRichEdit_GetNextUndo($hWnd), False)
	Else
		_GUICtrlMenu_SetItemText($hMenu, $mnuUndo, "Отмена", False)
		_GUICtrlMenu_SetItemEnabled($hMenu, $mnuUndo, False, False)
	EndIf
	If _GUICtrlRichEdit_CanRedo($hWnd) Then
		_GUICtrlMenu_SetItemEnabled($hMenu, $mnuRedo, True, False)
		_GUICtrlMenu_SetItemText($hMenu, $mnuRedo, "Повтор: " & _GUICtrlRichEdit_GetNextRedo($hWnd), False)
	Else
		_GUICtrlMenu_SetItemText($hMenu, $mnuRedo, "Повтор", False)
		_GUICtrlMenu_SetItemEnabled($hMenu, $mnuRedo, False, False)
	EndIf
	$fState = _GUICtrlRichEdit_IsTextSelected($hWnd)
	_GUICtrlMenu_SetItemEnabled($hMenu, $mnuCut, $fState, False)
	_GUICtrlMenu_SetItemEnabled($hMenu, $mnuCopy, $fState, False)

	_GUICtrlMenu_SetItemEnabled($hMenu, $mnuPaste, _GUICtrlRichEdit_CanPaste($hWnd))

	_GUICtrlMenu_SetItemEnabled($hMenu, $mnuPasteSpl, _GUICtrlRichEdit_CanPasteSpecial($hWnd), False)
EndFunc   ;==>_SetMenuTexts