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

#include "DDQuery.h"
#include "Clipbrd.hpp"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AbNumEdit"
#pragma resource "*.dfm"
// NOTE: this is a public static variable, you can never use
// this when you create multiple instances of a Form
TDropDownQuery *DropDownQuery;
//---------------------------------------------------------------------------
__fastcall TDropDownQuery::TDropDownQuery(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TDropDownQuery::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 TDropDownQuery::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
}
//---------------------------------------------------------------------------

