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

#include "BlockName_Query.h"
#include "Clipbrd.hpp"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "_GClass"
#pragma link "AbLED"
#pragma resource "*.dfm"
TBlockNameQuery *BlockNameQuery;
//---------------------------------------------------------------------------
__fastcall TBlockNameQuery::TBlockNameQuery(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TBlockNameQuery::PosRightClick(TObject *Sender)
{
Ordinal = 1;
PosTop->Font->Color = clRed;
PosRight->Font->Color = clGreen;
PosBottom->Font->Color = clRed;
PosLeft->Font->Color = clRed;
DisplayFlagName->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TBlockNameQuery::PosBottomClick(TObject *Sender)
{
Ordinal = 2;
PosTop->Font->Color = clRed;
PosRight->Font->Color = clRed;
PosBottom->Font->Color = clGreen;
PosLeft->Font->Color = clRed;
DisplayFlagName->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TBlockNameQuery::PosLeftClick(TObject *Sender)
{
Ordinal = 3;
PosTop->Font->Color = clRed;
PosRight->Font->Color = clRed;
PosBottom->Font->Color = clRed;
PosLeft->Font->Color = clGreen;
DisplayFlagName->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TBlockNameQuery::PosTopClick(TObject *Sender)
{
Ordinal = 4;
PosTop->Font->Color = clGreen;
PosRight->Font->Color = clRed;
PosBottom->Font->Color = clRed;
PosLeft->Font->Color = clRed;
DisplayFlagName->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TBlockNameQuery::SetOrdinal(int Ord)
{
DisplayFlagName->Checked = true;
switch(Ord) {
	case 0:
    default:
		DisplayFlagName->Checked = false;
        Ordinal = 0;	// don't display the Block Name
    	break;
	case 1:
		PosRightClick(NULL);
    	break;
    case 2:
        PosBottomClick(NULL);
    	break;
    case 3:
        PosLeftClick(NULL);
    	break;
    case 4:
        PosTopClick(NULL);
    	break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TBlockNameQuery::DisplayFlagNameClick(TObject *Sender)
{
if (DisplayFlagName->Checked) {
	PosRightClick(NULL);
	} else {
	PosTop->Font->Color = clRed;
	PosRight->Font->Color = clRed;
	PosBottom->Font->Color = clRed;
	PosLeft->Font->Color = clRed;
	Ordinal = 0;	// don't display the Block Name
	}
}
//---------------------------------------------------------------------------

void __fastcall TBlockNameQuery::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 TBlockNameQuery::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.
// Call this form with this sequence
/*
#if __BORLANDC__ >= 0x720
// Defined for CB10.1 C++ programs that use the VCL.
DropDownQuery->Position    = poDesigned;
DropDownQuery->PopupMode = pmExplicit;
DropDownQuery->PopupParent = ptrPC;
#endif
retv = DropDownQuery->ShowModal();
*/
//
// 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     
}
//---------------------------------------------------------------------------

