//----------------------------------------------------------------------------
//Borland C++Builder
//Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#include <shellapi.h>
#include <registry.hpp>
#pragma hdrstop

#include "traymain.h"
#include "TPropiedades.h"
#include "ListInit.h"
//---------------------------------------------------------------------------
#pragma link "LM2000"
#pragma resource "*.dfm"
TTrayMainForm *TrayMainForm;
//---------------------------------------------------------------------------
__fastcall TTrayMainForm::TTrayMainForm(TComponent* Owner)
  : TForm(Owner)
{
    Running = true;
  	TrayMessage(NIM_ADD);
    TrayMessage(NIM_MODIFY);

    TAcercaDe *AcercaDe;
    AcercaDe = new TAcercaDe(this);
    AcercaDe->Show();
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::DrawItem(TMessage& Msg)
{
     IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
     TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::MyNotify(TMessage& Msg)
{
    POINT MousePos;

    switch(Msg.LParam)
    {
        case WM_RBUTTONUP:
            if ( GetCursorPos(&MousePos) )
            {
                PopupMenu1->PopupComponent = Propiedades;
                SetForegroundWindow(Handle);
                PopupMenu1->Popup(MousePos.x, MousePos.y);
                PopupMenu1->PopupComponent = 0;
            }
            else
                Show();
            break;
        case WM_LBUTTONDBLCLK:
              Properties1Click(0);
            break;
/*
        case WM_LBUTTONUP:
              Properties1Click(0);
            break;
*/            
        default:
            break;
    }
    TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::FormDestroy(TObject *Sender)
{
	TrayMessage(NIM_DELETE);
}
//---------------------------------------------------------------------------
bool __fastcall TTrayMainForm::TrayMessage(DWORD dwMessage)
{
   NOTIFYICONDATA tnd;
   PSTR pszTip;

   pszTip = TipText();

   tnd.cbSize          = sizeof(NOTIFYICONDATA);
   tnd.hWnd            = Handle;
   tnd.uID             = IDC_MYICON;
   tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage	= MYWM_NOTIFY;

   if (dwMessage == NIM_MODIFY)
    {
        tnd.hIcon		= IconHandle();
        if (pszTip)
           lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
	    else
        tnd.szTip[0] = '\0';
    }
   else
    {
        tnd.hIcon = NULL;
        tnd.szTip[0] = '\0';
    }

   return (Shell_NotifyIcon(dwMessage, &tnd));
}
//---------------------------------------------------------------------------
HANDLE __fastcall TTrayMainForm::IconHandle(void)
{
    if (Running)
        return imagenOn -> Picture->Icon->Handle;
    else
        return imagenOFF -> Picture->Icon->Handle;
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::ToggleState(void)
{
    Running = !Running;
    TrayMessage(NIM_MODIFY);
    if ( Running )
    {
        ToggleState1->Caption = "Desactivar";
        // Reanudamos el hilo
        Propiedades->LEGO_COMM -> Resume();
    } else {
        ToggleState1->Caption = "Activar";
        // Paramos al hilo
        Propiedades->LEGO_COMM -> Suspend();
    }
}
//---------------------------------------------------------------------------
PSTR __fastcall TTrayMainForm::TipText(void)
{
    if (Running)
        return ("CAP-c esta ACTIVADO");
    else
        return ("-- DESACTIVADO --");
}
//---------------------------------------------------------------------------
LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
{
	HICON hIcon;

	hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
		16, 16, 0);
	if (!hIcon)
		return(FALSE);

	DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
		16, 16, 0, NULL, DI_NORMAL);

	return(TRUE);
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::Properties1Click(TObject *Sender)
{
//    Show();
 Propiedades -> Visible = !(Propiedades->Visible);
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::ToggleState1Click(TObject *Sender)
{
    ToggleState();
}
//---------------------------------------------------------------------------
void __fastcall TTrayMainForm::Shutdown1Click(TObject *Sender)
{
  // Desactivamos el HILO de ejecución y esperamos hasta que acabe
  Propiedades->LEGO_COMM->Terminate();
  Propiedades->LEGO_COMM->WaitFor();

  Close();
}
//---------------------------------------------------------------------------


void __fastcall TTrayMainForm::BSalirClick(TObject *Sender)
{
 Visible = false;    
}
//---------------------------------------------------------------------------


void __fastcall TTrayMainForm::FormCreate(TObject *Sender)
{
    Visible = false;
}
//---------------------------------------------------------------------------


void __fastcall TTrayMainForm::Acercade1Click(TObject *Sender)
{
    TAcercaDe *AcercaDe;
    AcercaDe = new TAcercaDe(this);
    AcercaDe->ShowModal();
}
//---------------------------------------------------------------------------



