; ===============================================================================
;
; Program Name:     MsgBoxWizard()
; Description:      Создаёт код функции MessageBox используя GUI
;                   choices
; Requirement(s):   None
; Return Value(s):  None
; Author(s):        Giuseppe Criaco <gcriaco@quipo.it>
;
; ===============================================================================
;
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $g_idOptWarning, $g_idOptInfo, $g_idOptCritical, $g_idOptQuestion
Global $g_idOptSysModal, $g_idOptTaskModal
Global $g_idOptOkCancel, $g_idOptYesNo, $g_idOptYesNoCancel, $g_idOptAbortRetryIgnore
Global $g_idOptRetryCancel, $g_idOptCancelRetryContinue, $g_idOptTopMost
Global $g_idOptRightJust, $g_idOptSecond, $g_idOptThird

_Main()

Func _Main()
	Local $iFlag, $idButton, $sMsgBox, $asMsgText
	Local $idTITLE, $idTEXT, $idTimeout, $idOptOK
	Local $idBTNCOPY, $idBTNEXIT, $idBTNPREVIEW, $iMSG, $sText

	GUICreate("Мастер создания MsgBox v.1.0", 440, 540, 100, 100) ; Создаёт окно

	GUICtrlCreateLabel("Заголовок", 10, 5, 180, 17)
	$idTITLE = GUICtrlCreateInput("", 10, 20, 420, 20)
	GUICtrlSetState(-1, $GUI_FOCUS)
	GUICtrlSetTip(-1, "Заголовок окна сообщения MsgBox.")
	GUICtrlCreateLabel("Текст", 10, 50, 180, 17)
	$idTEXT = GUICtrlCreateEdit("", 10, 65, 420, 100, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_MULTILINE + $ES_WANTRETURN)
	GUICtrlSetTip(-1, "Текст окна сообщения MsgBox.")

	GUICtrlCreateGroup("Иконка", 10, 170, 200, 130)
	$g_idOptWarning = GUICtrlCreateRadio("Предупреждение", 20, 190, 180, 20)
	GUICtrlSetState(-1, $GUI_CHECKED)
	$g_idOptInfo = GUICtrlCreateRadio("Информация", 20, 210, 180, 20)
	$g_idOptCritical = GUICtrlCreateRadio("Стоп", 20, 230, 180, 20)
	$g_idOptQuestion = GUICtrlCreateRadio("Знак вопроса", 20, 250, 180, 20)
	GUICtrlCreateRadio("Ничего", 20, 270, 180, 20)
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	GUICtrlCreateGroup("Модальность", 10, 310, 200, 90)
	GUICtrlCreateRadio("Приложение", 20, 330, 180, 20)
	GUICtrlSetState(-1, $GUI_CHECKED)
	$g_idOptSysModal = GUICtrlCreateRadio("Системная, поверх всех окон", 20, 350, 180, 20)
	$g_idOptTaskModal = GUICtrlCreateRadio("Блокирует родительское окно", 20, 370, 180, 20)
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	GUICtrlCreateGroup("Кнопки", 230, 170, 200, 170)
	$idOptOK = GUICtrlCreateRadio("OK", 240, 190, 185, 20)
	GUICtrlSetState(-1, $GUI_CHECKED)
	$g_idOptOkCancel = GUICtrlCreateRadio("OK, Отмена", 240, 210, 185, 20)
	$g_idOptYesNo = GUICtrlCreateRadio("Да и Нет", 240, 230, 185, 20)
	$g_idOptYesNoCancel = GUICtrlCreateRadio("Да, Нет и Отмена", 240, 250, 185, 20)
	$g_idOptAbortRetryIgnore = GUICtrlCreateRadio("Прервать, Повтор и Пропустить", 240, 270, 185, 20)
	$g_idOptRetryCancel = GUICtrlCreateRadio("Повтор и Отмена", 240, 290, 185, 20)
	$g_idOptCancelRetryContinue = GUICtrlCreateRadio("Отмена, Повторить, Продолжить", 240, 310, 185, 20)
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	GUICtrlCreateGroup("Прочее", 10, 410, 200, 90)
	GUICtrlCreateRadio("Ничего", 20, 430, 180, 20)
	GUICtrlSetState(-1, $GUI_CHECKED)
	$g_idOptTopMost = GUICtrlCreateRadio('Атрибут "поверх всех окон"', 20, 450, 180, 20)
	$g_idOptRightJust = GUICtrlCreateRadio("Выравнивание текстов справа", 20, 470, 180, 20)
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	GUICtrlCreateGroup("Кнопка по умолчанию", 230, 350, 200, 90)
	Local $idOptFirst = GUICtrlCreateRadio("Первая кнопка", 240, 370, 130, 20)
	GUICtrlSetState(-1, $GUI_CHECKED)
	$g_idOptSecond = GUICtrlCreateRadio("Вторая кнопка", 240, 390, 130, 20)
	GUICtrlSetState(-1, $GUI_DISABLE)
	$g_idOptThird = GUICtrlCreateRadio("Третья кнопка", 240, 410, 130, 20)
	GUICtrlSetState(-1, $GUI_DISABLE)
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	GUICtrlCreateGroup("Время ожидания", 230, 450, 200, 50)
	$idTimeout = GUICtrlCreateInput("", 240, 470, 100, 20, $ES_NUMBER)
	GUICtrlSetTip(-1, 'Время ожидания (в секундах), по истечении которого' & @CRLF & 'MsgBox будет автоматически закрыт (по умолчанию 0,' & @CRLF & 'что означает, что время отображения не ограничено).')
	GUICtrlCreateGroup("", -99, -99, 1, 1) ; закрывает группу

	$idBTNPREVIEW = GUICtrlCreateButton("Предпросмотр", 10, 510, 100)
	GUICtrlSetTip(-1, "Показать окно сообщения")
	$idBTNCOPY = GUICtrlCreateButton("Копировать", 120, 510, 100)
	GUICtrlSetTip(-1, "Копирует созданный AutoIt-код в буфер обмена")
	$idBTNEXIT = GUICtrlCreateButton("Выход", 230, 510, 100)
	GUICtrlSetTip(-1, "Выход из программы")

	$idButton = $idOptOK

	GUISetState() ; will display an empty dialog box

	; Run the GUI until the dialog is closed
	While 1
		$iMSG = GUIGetMsg()
		Select
			Case $iMSG = $GUI_EVENT_CLOSE Or $iMSG = $idBTNEXIT
				Exit

			Case $iMSG = $idOptOK
				$idButton = $idOptOK
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_DISABLE)
				GUICtrlSetState($g_idOptThird, $GUI_DISABLE)

			Case $iMSG = $g_idOptOkCancel
				$idButton = $g_idOptOkCancel
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_DISABLE)

			Case $iMSG = $g_idOptYesNo
				$idButton = $g_idOptYesNo
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_DISABLE)

			Case $iMSG = $g_idOptYesNoCancel
				$idButton = $g_idOptYesNoCancel
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_ENABLE)

			Case $iMSG = $g_idOptAbortRetryIgnore
				$idButton = $g_idOptAbortRetryIgnore
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_ENABLE)

			Case $iMSG = $g_idOptRetryCancel
				$idButton = $g_idOptRetryCancel
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_DISABLE)

			Case $iMSG = $g_idOptCancelRetryContinue
				$idButton = $g_idOptCancelRetryContinue
				GUICtrlSetState($idOptFirst, $GUI_CHECKED)
				GUICtrlSetState($idOptFirst, $GUI_ENABLE)
				GUICtrlSetState($g_idOptSecond, $GUI_ENABLE)
				GUICtrlSetState($g_idOptThird, $GUI_ENABLE)

			Case $iMSG = $idBTNPREVIEW
				MsgBox(_SetFlag($iFlag), GUICtrlRead($idTITLE), GUICtrlRead($idTEXT), GUICtrlRead($idTimeout))

			Case $iMSG = $idBTNCOPY
				$asMsgText = StringSplit(GUICtrlRead($idTEXT), @CRLF, 1)
				If $asMsgText[0] = 1 Then
					$sText = GUICtrlRead($idTEXT)
				Else
					$sText = $asMsgText[1]

					For $iCtr = 2 To $asMsgText[0]
						$sText = $sText & Chr(34) & " & @CRLF & " & Chr(34) & $asMsgText[$iCtr]
					Next

				EndIf

				Select
					Case $idButton = $idOptOK
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"   Case Else                ;OK" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptOkCancel
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 1 ;OK" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2 ;Cancel" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 1  ;OK" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2  ;Cancel" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptYesNo
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 6 ;Yes" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 7 ;No" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 6  ;Yes" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 7  ;No" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptYesNoCancel
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 6 ;Yes" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 7 ;No" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2 ;Cancel" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 6  ;Yes" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 7  ;No" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2  ;Cancel" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptAbortRetryIgnore
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 3 ;Abort" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 4 ;Retry" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 5 ;Ignore" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 3  ;Abort" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 4  ;Retry" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 5  ;Ignore" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptRetryCancel
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 4 ;Retry" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2 ;Cancel" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 4  ;Retry" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 2  ;Cancel" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf

					Case $idButton = $g_idOptCancelRetryContinue
						If GUICtrlRead($idTimeout) = "" Then
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 2 ;Cancel" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 10 ;Try Again" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 11 ;Continue" & @CRLF & @CRLF & _
									"EndSelect"
						Else
							$sMsgBox = "Dim $iMsgBoxAnswer" & @CRLF & _
									"$iMsgBoxAnswer = MsgBox(" & _SetFlag($iFlag) & "," & Chr(34) & GUICtrlRead($idTITLE) & Chr(34) & "," _
									 & Chr(34) & $sText & Chr(34) & "," & GUICtrlRead($idTimeout) & ")" & @CRLF & _
									"Select" & @CRLF & _
									"   Case $iMsgBoxAnswer = 2  ;Cancel" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 10 ;Try Again" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = 11 ;Continue" & @CRLF & @CRLF & _
									"   Case $iMsgBoxAnswer = -1 ;Timeout" & @CRLF & @CRLF & _
									"EndSelect"
						EndIf
				EndSelect

				ClipPut($sMsgBox)
		EndSelect

	WEnd
EndFunc   ;==>_Main

; ===============================================================================
;
; Function Name:    _SetFlag()
; Description:      Set the flag that indicates the type of message box and the
;                   possible button combinations.
; Parameter(s):     $iFlag        - Flag
; Requirement(s):   None
; Return Value(s):  On Success - Returns the message box flag
;                   None
; Author(s):        Giuseppe Criaco <gcriaco@quipo.it>
;
; ===============================================================================
;

Func _SetFlag($iFlag)
	$iFlag = 0

	;Icons
	Select
		Case GUICtrlRead($g_idOptWarning) = $GUI_CHECKED
			$iFlag = $iFlag + 48
		Case GUICtrlRead($g_idOptInfo) = $GUI_CHECKED
			$iFlag = $iFlag + 64
		Case GUICtrlRead($g_idOptCritical) = $GUI_CHECKED
			$iFlag = $iFlag + 16
		Case GUICtrlRead($g_idOptQuestion) = $GUI_CHECKED
			$iFlag = $iFlag + 32
	EndSelect

	;Modality
	Select
		Case GUICtrlRead($g_idOptSysModal) = $GUI_CHECKED
			$iFlag = $iFlag + 4096
		Case GUICtrlRead($g_idOptTaskModal) = $GUI_CHECKED
			$iFlag = $iFlag + 8192
	EndSelect

	;Buttons
	Select
		Case GUICtrlRead($g_idOptOkCancel) = $GUI_CHECKED
			$iFlag = $iFlag + 1
		Case GUICtrlRead($g_idOptYesNo) = $GUI_CHECKED
			$iFlag = $iFlag + 4
		Case GUICtrlRead($g_idOptYesNoCancel) = $GUI_CHECKED
			$iFlag = $iFlag + 3
		Case GUICtrlRead($g_idOptAbortRetryIgnore) = $GUI_CHECKED
			$iFlag = $iFlag + 2
		Case GUICtrlRead($g_idOptRetryCancel) = $GUI_CHECKED
			$iFlag = $iFlag + 5
		Case GUICtrlRead($g_idOptCancelRetryContinue) = $GUI_CHECKED
			$iFlag = $iFlag + 6
	EndSelect

	;Miscellaneous
	Select
		Case GUICtrlRead($g_idOptTopMost) = $GUI_CHECKED
			$iFlag = $iFlag + 262144
		Case GUICtrlRead($g_idOptRightJust) = $GUI_CHECKED
			$iFlag = $iFlag + 5244288
	EndSelect

	;Default Buttons
	Select
		Case GUICtrlRead($g_idOptSecond) = $GUI_CHECKED
			$iFlag = $iFlag + 256
		Case GUICtrlRead($g_idOptThird) = $GUI_CHECKED
			$iFlag = $iFlag + 512
	EndSelect

	Return $iFlag
EndFunc   ;==>_SetFlag
