/*****************************************************************************
Open Source MIT License

Copyright (c) 2017 Matthew Ahmed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

// Tray.cpp: implementation of the CTray class.
//
//////////////////////////////////////////////////////////////////////

#include "Headers.h"
#include "Settings.h"
#include "Tray.h"
#include  <shellapi.h>  // Necessary if WIN32_LEAN_AND_MEAN defined

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTray::CTray(HINSTANCE hInst, CSettings * pSet, LPCTSTR sCapt1,
						 LPCTSTR sCapt2, ZWin * pOwner, UINT uIconID, UINT uMenuID)
	: ZWin(hInst, sCapt1, sCapt2, pOwner)
{
	ini = pSet;
	m_hWnd = m_hOwner; // need for fnErr, etc
	m_uIconID = uIconID;
	m_uMenuID = uMenuID;
	strcpy(m_sTip, m_sCapt);
}

CTray::~CTray()
{
//	DeleteTrayIcon();
}

BOOL CTray::AddTrayIcon()
{
	BOOL bRet; 
	NOTIFYICONDATA tnid;
	HICON hIcon;

	tnid.cbSize = sizeof(NOTIFYICONDATA); 
	tnid.hWnd = m_hWnd;  // = m_hOwner
	tnid.uID = U_TRAYICONID;
	tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; 
	tnid.uCallbackMessage = WMU_TRAYMSG;

	hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(m_uIconID));

	if (!hIcon) return FALSE;
	tnid.hIcon = hIcon;

	strcpy(tnid.szTip, m_sTip);

	bRet = Shell_NotifyIcon(NIM_ADD, &tnid); 

	DestroyIcon(hIcon); 

	return bRet; 
}

////- Delete if tip always same
BOOL CTray::SetTip(LPCTSTR sTip, BOOL bUpdate)
{
	strcpy(m_sTip, sTip);
	if (!bUpdate) return TRUE;

	NOTIFYICONDATA tnid;

	tnid.cbSize = sizeof(NOTIFYICONDATA); 
	tnid.hWnd = m_hWnd;
	tnid.uID = U_TRAYICONID;
	strcpy(tnid.szTip, sTip);
	tnid.uFlags = NIF_TIP;

	return Shell_NotifyIcon(NIM_MODIFY, &tnid);
}

BOOL CTray::DeleteTrayIcon()
{
	NOTIFYICONDATA tnid;

	tnid.cbSize = sizeof(NOTIFYICONDATA); 
	tnid.hWnd = m_hWnd;  // = m_hOwner
	tnid.uID = U_TRAYICONID;

	return Shell_NotifyIcon(NIM_DELETE, &tnid);
}

LRESULT CTray::MsgHandler(WPARAM wP, LPARAM lP)
{
	HWND hWnd;
	static TCHAR* sDlgClass = "#32770"; // including fnErr Msg Box 

	if (wP == U_TRAYICONID)	// Should always be true
	{
		hWnd = FindWindow(sDlgClass, m_sCapt); // may be NULL
		////- IF MORE POSSIBLE CAPTIONS, CHECK FOR IF hWnd == NULL
		
		switch (lP)
		{
			case WM_RBUTTONUP :
				if (!hWnd) PostMessage(m_hWnd, WMU_SHOWTRAYMENU, 0, 0);
				else SetForegroundWindow(hWnd);
				break;

			case WM_LBUTTONDBLCLK :
				////-	MAYBE DO SOMETHING, ELSE FALL THROUGH TO ACTIVATE

			case WM_RBUTTONDOWN :
			case WM_LBUTTONDOWN :
				// If dialog running, activate it
				// Otherwise activate self
				if (hWnd) SetForegroundWindow(hWnd);
				else SetForegroundWindow(m_hWnd);
 				break;

			default :	 // e.g. ignore mouse moves; tip already shows
				break;
		}
	}

	return 0;
}

// Don't call directly; do via PostMessage to give SetForegroundWindow time
BOOL CTray::ShowMenu() 
{
	POINT p;
	GetCursorPos(&p);

	HMENU hMenu = LoadMenu(m_hInst, MAKEINTRESOURCE(m_uMenuID));
	if (!hMenu) return fnErr("LoadMenu error");

	HMENU hPopUp = GetSubMenu(hMenu, 0);
	if (!hPopUp) return fnErr("GetSubMenu error");

	// Sometimes takes a few milliseconds for prior SetForegroundWindow cmd on
	// mouse button down to take effect.  Do preliminaries above first and then
	// check.	 If not ForegroundWindow, presumably the Properties dialog was
	// set foreground, or mouse moved over tray	icon with rbutton already down.
	if (GetForegroundWindow() != m_hWnd) return 0;

  if (!TrackPopupMenu(hPopUp, TPM_LEFTALIGN  | TPM_RIGHTBUTTON,
																				p.x, p.y, 0, m_hWnd, NULL))
	{
			return fnErr("TrackPopupMenu error");
	}
	return TRUE;
}
