//---------------------------------------------------------------------------
/*
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

    This file is part of Remote Train Control.

    Remote Train Control is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Remote Train Control is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#pragma hdrstop

#include "Message_Popup.h"
#include "Train_Control.h"
#include "Setup_Control.h"
#include "Clipbrd.hpp"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

// Never use the global variable MessagePopup, this form should always be created with "new"
//TMessagePopup *MessagePopup;
//---------------------------------------------------------------------------
__fastcall TMessagePopup::TMessagePopup(TComponent* Owner)
	: TForm(Owner)
{
assert(false);	// never called since I used the overloaded constructor below
}
//---------------------------------------------------------------------------

__fastcall TMessagePopup::TMessagePopup(TComponent* Owner, HWND Parent, WPARAM wp, LPARAM lp)
	: TForm(Owner)
{
// Calling sequence required since there are no strings passed to this component  is demonstrated
// in TMessageDisplay Thread in Message_Thread.cpp
//
// Component displays a message and waits for a [Cancel] button push. Used by Playback
// and previously by PFA to cancel.
//

// grab the Parent's Handle
ParentHandle = Parent;
// grab the message params
WParam = wp;
LParam = lp;
Tag = 0;		// simple popup message unless changed, if Tag == 1, it's a stop playback window
				// if Tag == 2, it's a stop PFA window
}
//---------------------------------------------------------------------------

void __fastcall TMessagePopup::CancelButtonClick(TObject *Sender)
{
CancelButton->Visible = false;
// Tag == 1 means this is a playback window and button is a cancel button
if (CancelButton->Tag == 1) {
	// Send a stop Playback message to Parent
    if (ParentHandle) {
    	(void) PostMessage(ParentHandle, CM_STOPPLAYBACK, WParam, LParam);
        }
	}
//
// NOTE: no longer used by PFA
//
// Tag == 2 means this is a PFA window and button is a cancel button
if (CancelButton->Tag == 2) {
	// Send a stop PFA message to Parent
    if (ParentHandle) {
    	(void) PostMessage(ParentHandle, CM_STOPPFA, WParam, LParam);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TMessagePopup::CopyrightClick(TObject *Sender)
{
// Copy the Form to the clipboard.
// In BCB4.0, there is no SetSize method associated with TBitmap
// so we have to use Width and Height separately.
//
// see:
// https://stackoverflow.com/questions/3190777/how-do-i-copy-a-form-as-an-image-to-the-clipboard
//
Graphics::TBitmap * bm;
bm = new Graphics::TBitmap;
try
	{
    #if __BORLANDC__ >= 0x720
	// Defined for CB10.1 C++ programs that use the VCL.
	bm->SetSize(ClientWidth, ClientHeight);
    #else
    bm->Width = ClientWidth;
    bm->Height = ClientHeight;
    #endif
	BitBlt(bm->Canvas->Handle, 0, 0, ClientWidth, ClientHeight, Canvas->Handle, 0, 0, SRCCOPY);
    Clipboard()->Assign(bm);
    }
__finally
	{
	delete bm;
    }    
}
//---------------------------------------------------------------------------

void __fastcall TMessagePopup::FormShow(TObject *Sender)
{
#if __BORLANDC__ >= 0x720
// Defined for CB10.1 C++ programs that use the VCL.
//
// See PC_Thread.cpp -- CallInputDropDown(void) for calling
// sequence for this code to correctly place the form when
// monitor 2 is requested.
//
// Code developed with the help of Grok:
// https://grok.com/share/c2hhcmQtMg_7cadeded-7ad7-4353-88bd-ff9ae464b145
// https://grok.com/c/aac589ba-ab79-40aa-8e5a-82d400df210b?rid=954e999c-e1c3-4169-8faf-972a3e81e5c8
//
if (PopupParent)   // or pass the owner form another way
    {
        TForm* OwnerForm = dynamic_cast<TForm*>(PopupParent);

        if (OwnerForm)
        {
            HandleNeeded();

            Left = OwnerForm->Left + (OwnerForm->Width  - Width)  / 2;
            Top  = OwnerForm->Top  + (OwnerForm->Height - Height) / 2;
        }
    }

    MakeFullyVisible();   // Important for monitor 2
#endif     
}
//---------------------------------------------------------------------------

