#Region ### includes ###
#include-once
#NoTrayIcon
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Ribbonsbar.au3>
#include <GuiListView.au3>
#include <EditConstants.au3>
#EndRegion ### includes ###

Opt("GUIResizeMode", 802)
Global $appname = 'Example App', $appver = '1.0', $additemsgui_W = 400, $additemsgui_H = 150, $hidesidepanel = False, $fActive = True

#Region ### $maingui ###
$maingui = GUICreate($appname & ' ' & $appver, @DesktopWidth, 600, -1, -1, BitOr($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
_WinAPI_SetClassLongEx($maingui, -26, BitAND(_WinAPI_GetClassLongEx($maingui, -26), BitNOT(1), BitNOT(2)))
$hBar = _RibbonsBar_Create($maingui, 0, 0, 0, 0, 'blue')
$hTab_Home = _RibbonsBar_Create_Tab($hBar, 'Home')
$hItem_FileOps = _RibbonsBar_Create_TabItem($hTab_Home, 'File operations' , 225 )
$hGrandButton_New = _RibbonsBar_Create_GrandButton($hItem_FileOps,'',412,'New',52, True )
_RibbonsBar_GrandButtons_Enable($hGrandButton_New, 0)
$hDropDownNew = _RibbonsBar_Create_ContextMenu($hGrandButton_New)
$hDropDownNew_1 = _GUICtrlCreateODMenuItem("Standard ...", $hDropDownNew, "smallIcons.dll", 261)
$hDropDownNew_2 = _GUICtrlCreateODMenuItem("Manual ...", $hDropDownNew, "smallIcons.dll", 382)
$hGrandButton_Open = _RibbonsBar_Create_GrandButton($hItem_FileOps,'',453,'Open',52, False )
_RibbonsBar_GrandButtons_Enable($hGrandButton_Open, 0)
$hGrandButton_Save = _RibbonsBar_Create_GrandButton($hItem_FileOps,'',514,'Save',52, True )
_RibbonsBar_GrandButtons_Enable($hGrandButton_Save, 0)
$hDropDownSave = _RibbonsBar_Create_ContextMenu($hGrandButton_Save)
$hDropDownSave_1 = _GUICtrlCreateODMenuItem("Save", $hDropDownSave, "smallIcons.dll", 287)
$hDropDownSave_2 = _GUICtrlCreateODMenuItem("Save As ...", $hDropDownSave, "smallIcons.dll", 286)
$hGrandButton_Export = _RibbonsBar_Create_GrandButton($hItem_FileOps,'',337,'Export',62, True )
_RibbonsBar_GrandButtons_Enable($hGrandButton_Export, 0)
$hDropDownExport = _RibbonsBar_Create_ContextMenu($hGrandButton_Export)
$hDropDownExport_1 = _GUICtrlCreateODMenuItem("To Excel", $hDropDownExport, "smallIcons.dll", 1243)
$hItem_ItemOps = _RibbonsBar_Create_TabItem ( $hTab_Home, 'Item operations' , 393 )
$hGrandButton_AddItems = _RibbonsBar_Create_GrandButton($hItem_ItemOps,'',301,'Add item(s)',72, True )
$hDropDownAddItems = _RibbonsBar_Create_ContextMenu($hGrandButton_AddItems)
_RibbonsBar_GrandButtons_Enable($hGrandButton_AddItems, 0)
$hDropDownAddItems_1 = _GUICtrlCreateODMenuItem("From Catalogue", $hDropDownAddItems, "smallIcons.dll", 173)
$hDropDownAddItems_2 = _GUICtrlCreateODMenuItem("From Quote", $hDropDownAddItems, "smallIcons.dll", 143)
$hDropDownAddItems_3 = _GUICtrlCreateODMenuItem("Paste", $hDropDownAddItems, "smallIcons.dll", 9)
$hGrandButton_DelItems = _RibbonsBar_Create_GrandButton($hItem_ItemOps,'',615,'Delete item(s)',82, False )
_RibbonsBar_GrandButtons_Enable($hGrandButton_DelItems, 0)
$SmallButton_Undo = _RibbonsBar_Create_SmallButton($hItem_ItemOps,'',728,1,'Undo (Test: click to enable grand buttons)',230)
$SmallButton_Redo = _RibbonsBar_Create_SmallButton($hItem_ItemOps,'',194,2,'Redo (Test: click to disable grand buttons)',230)
_RibbonsBar_SmallButtons_Enable($SmallButton_Redo, 0)
$hTab_Help = _RibbonsBar_Create_Tab( $hBar, 'Help' )
$hItem_Information = _RibbonsBar_Create_TabItem ( $hTab_Help, 'Information' , 120 )
$hGrandButton_About = _RibbonsBar_Create_GrandButton($hItem_Information,'',631,'Help','',False)
$hGrandButton_About = _RibbonsBar_Create_GrandButton($hItem_Information,'',644,'About','',False)
_RibbonsBar_Tab_SetState($hTab_Home)
GUISwitch($maingui)
WinMove($maingui, "", Default, Default, 800, Default)
$wPos = WinGetPos($maingui)
WinMove($maingui, "", @DesktopWidth/2 - $wPos[2]/2, @DesktopHeight/2 - $wPos[3]/2, Default, Default)
$wPos = WinGetPos($maingui)
$mainlistview = GUICtrlCreateListView('Col 1|Col 2|Col 3|Col 4|Col 5|Col 6', -2, 120, _
				$wPos[2], $wPos[3], Default, 0x200)
_GUICtrlListView_SetExtendedListViewStyle($mainlistview, BitOR($LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_FLATSB))
_GUICtrlListView_SetColumnWidth($mainlistview, 0, 120)
_GUICtrlListView_SetColumnWidth($mainlistview, 1, 250)
_GUICtrlListView_SetColumnWidth($mainlistview, 2, 50)
_GUICtrlListView_SetColumnWidth($mainlistview, 3, 100)
_GUICtrlListView_SetColumnWidth($mainlistview, 4, 100)
_GUICtrlListView_SetColumnWidth($mainlistview, 5, 50)
$hidepanellbl = GUICtrlCreateLabel('|', $wPos[2] - 300, 121, 10, $wPos[3] - 120, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont(-1, Default, Default, Default, "Wingdings 3")
GUICtrlSetBkColor(-1, 0xf2f8ff)
GUICtrlSetCursor(-1, 0)
#EndRegion ### $additemsgui ###

#Region ### $sidepanel ###
$sidepanel = GUICreate("", 290, 715, $wPos[2] - 312, 116, $WS_POPUP, BitOR($WS_EX_MDICHILD, 0x08000000), $maingui);$WS_POPUP, $WS_EX_MDICHILD, $maingui)
GUISetBkColor(0xf2f8ff)
GUICtrlCreateLabel("This is an example sidepanel", 0, 8, 290, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
#EndRegion ### $sidepanel ###

#Region ### $additemsgui ###
$additemsgui = GUICreate('Add item(s) from Catalogue', $additemsgui_W, $additemsgui_H, 0, 0, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_EX_MDICHILD, $maingui)
GUISetBkColor(0xf2f8ff)
GUISetFont(10, 400)
$additems_addlabel = GUICtrlCreateLabel('Start typing below to add item(s) from catalogue (min 2 chars):', 10, 15, $additemsgui_W - 20, 21)
$additemsinput = GUICtrlCreateInput('', 10, 45, $additemsgui_W - 20, Default)
GUICtrlSendMsg(-1, $EM_SETCUEBANNER, False, 'Enter an item number here')
$additemsbn_add = GUICtrlCreateButton('Add', 10, 85, 110, 40)
#EndRegion ### $additemsgui ###

GUIRegisterMsg($WM_GETMINMAXINFO, "WM_GETMINMAXINFO")
GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUIRegisterMsg($WM_NCACTIVATE, "_WM_NCACTIVATE")
GUIRegisterMsg($WM_ACTIVATEAPP, "_WM_ACTIVATEAPP")
GUISetState(@SW_SHOW, $maingui)
GUISetState(@SW_SHOWNOACTIVATE, $sidepanel)

While 1
	$msg = GUIGetMsg(1)
	Switch $msg[1]
		Case $maingui
			Switch $msg[0]
				Case $GUI_EVENT_CLOSE
					_GDIPlus_Shutdown()
					Exit
				Case $hDropDownAddItems_1
					GUISetState(@SW_DISABLE, $maingui)
					$mPos = WinGetPos($maingui)
					WinMove($additemsgui, '', $mPos[0] + (($mPos[2]/2) - ($additemsgui_W/2)), $mPos[1] + (($mPos[3]/2) - ($additemsgui_H/2)))
					GUICtrlSetData($additemsinput, '')
					GUISetState(@SW_SHOW, $additemsgui)
					_WinAPI_SetFocus(ControlGetHandle('', '', $additems_addlabel))
				Case $hDropDownNew_1
					ConsoleWrite('Clicked "New" menu item 1'&@CRLF)
				Case $hDropDownNew_2
					ConsoleWrite('Clicked "New" menu item 2'&@CRLF)
				Case $hidepanellbl
					If $hidesidepanel = False Then ;hide
						HideSidePanel()
					Else ;unhide
						ShowSidePanel()
					EndIf
			EndSwitch
		Case $additemsgui
			Switch $msg[0]
				Case $GUI_EVENT_CLOSE
					GUISetState(@SW_ENABLE, $maingui)
					GUISetState(@SW_HIDE, $additemsgui)
			EndSwitch
	EndSwitch
	Switch _RibbonsBar_GetMsg()
		Case $hGrandButton_Open
			ConsoleWrite('open...'&@CRLF)
		Case $SmallButton_Undo
			_RibbonsBar_GrandButtons_Enable($hGrandButton_New, 1)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Save, 1)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Export, 1)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_AddItems, 1)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_DelItems, 1)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Open, 1)
			If _RibbonsBar_SmallButtons_GetState($SmallButton_Undo) = 1 Then
				ConsoleWrite('undo...'&@CRLF)
			EndIf
		Case $SmallButton_Redo
			_RibbonsBar_GrandButtons_Enable($hGrandButton_New, 0)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Save, 0)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Export, 0)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_AddItems, 0)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_DelItems, 0)
			_RibbonsBar_GrandButtons_Enable($hGrandButton_Open, 0)
			If _RibbonsBar_SmallButtons_GetState($SmallButton_Redo) = 1 Then
				ConsoleWrite('redo...'&@CRLF)
			EndIf
		Case $hTab_Home
			_RibbonsBar_Tab_SetState($hTab_Home)
		Case $hGrandButton_Export
			_RibbonsBar_ContextMenu_SetState($hDropDownExport)
		Case $hGrandButton_Save
			_RibbonsBar_ContextMenu_SetState($hDropDownSave)
		Case $hGrandButton_AddItems
			_RibbonsBar_ContextMenu_SetState($hDropDownAddItems)
		Case $hGrandButton_New
			_RibbonsBar_ContextMenu_SetState($hDropDownNew)
		Case $hTab_Help
			_RibbonsBar_Tab_SetState($hTab_Help)
	EndSwitch
WEnd

Func HideSidePanel()
	$wPos = WinGetPos($maingui)
	GUISetState(@SW_HIDE, $sidepanel)
	GUICtrlSetData($hidepanellbl, '|')
	GUICtrlSetPos($hidepanellbl, $wPos[2] - 28, Default, Default, $wPos[3] - 158)
	_WinAPI_SetWindowPos(GUICtrlGetHandle($mainlistview), 0, 0, 0, $wPos[2] - 27, $wPos[3] - 120, BitOR($SWP_NOMOVE, $SWP_NOZORDER))
	$hidesidepanel = True
EndFunc

Func ShowSidePanel()
	GUICtrlSetData($hidepanellbl, '}')
	$wPos = WinGetPos($maingui)
	WinMove($sidepanel, '', $wPos[0] + $wPos[2] - 300, $wPos[1] + 152, 290, $wPos[3] - 162)
	_WinAPI_SetWindowPos(GUICtrlGetHandle($mainlistview), 0, 0, 0, $wPos[2] - 316, $wPos[3] - 120, BitOR($SWP_NOMOVE, $SWP_NOZORDER))
	GUICtrlSetPos($hidepanellbl, $wPos[2] - 317, Default, Default, $wPos[3] - 158)
	GUISetState(@SW_SHOWNOACTIVATE, $sidepanel)
	$hidesidepanel = False
EndFunc

Func _WM_NCACTIVATE($hWnd, $Msg, $wParam, $lParam)
	#forceref $hWnd, $Msg, $wParam, $lParam
	If $fActive Then Return 1
	Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NCACTIVATE

Func _WM_ACTIVATEAPP($hWnd, $Msg, $wParam, $lParam)
	#forceref $hWnd, $Msg, $wParam, $lParam
	Local $iFlags = BitOR($SWP_NOMOVE, $SWP_NOSIZE, $SWP_FRAMECHANGED)
	$fActive = $wParam
	If $wParam Then
		_WinAPI_SetWindowPos($hWnd, $HWND_TOP, 0, 0, 0, 0, $iFlags)
		If IsHWnd($sidepanel) Then _WinAPI_SetWindowPos($sidepanel, $HWND_TOP, 0, 0, 0, 0, $iFlags)
	Else
		If IsHWnd($sidepanel) Then _WinAPI_PostMessage($sidepanel, $WM_NCACTIVATE, 0, 0)
		_WinAPI_PostMessage($hWnd, $WM_NCACTIVATE, 0, 0)
	EndIf
	Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NCACTIVATE

Func WM_GETMINMAXINFO($hwnd, $Msg, $wParam, $lParam)
    If $hWnd = $maingui Then
        $tagMaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $lParam)
        DllStructSetData($tagMaxinfo, 7,  800) ; W (min size)
        DllStructSetData($tagMaxinfo, 8,  600) ; H (min size)
        Return 0
    EndIf
EndFunc   ;==>WM_GETMINMAXINFO

Func WM_SIZE($hwnd, $uMsg, $wParam, $lParam)
	If $hidesidepanel = False Then
		$wPos = WinGetPos($maingui)
		_WinAPI_SetWindowPos(GUICtrlGetHandle($mainlistview), 0, 0, 0, _WinAPI_LoWord($lParam) - 300, _WinAPI_HiWord($lParam) - 119, BitOR($SWP_NOMOVE, $SWP_NOZORDER))
		GUICtrlSetPos($hidepanellbl, _WinAPI_LoWord($lParam) - 301, Default, Default, $wPos[3] - 158)
		WinMove($sidepanel, '', $wPos[0] + $wPos[2] - 300, $wPos[1] + 152, Default, $wPos[3] - 160)
	Else
		$wPos = WinGetPos($maingui)
		GUICtrlSetPos($hidepanellbl, _WinAPI_LoWord($lParam) - 11, Default, Default, $wPos[3] - 158)
		_WinAPI_SetWindowPos(GUICtrlGetHandle($mainlistview), 0, 0, 0, _WinAPI_LoWord($lParam) - 10, _WinAPI_HiWord($lParam) - 119, BitOR($SWP_NOMOVE, $SWP_NOZORDER))
	EndIf
    Return 1
EndFunc