#include <vcl.h>
#pragma hdrstop

//See https://sourceforge.net/p/predef/wiki/Compilers/ for versions.

#if __BORLANDC__ < 0x0560
// Problem has been observed in C++Builder 4.0.
// This is already fixed in VCL6, provide this for VCL4.

// There is a problem when monitor list is changed, the VCL crash.
// this needs to hack forms.hpp and add public before FMonitors member.
//public:
//	Classes::TList* FMonitors;
//private:
int __stdcall EnumMonitorsProc(HMONITOR hm, HDC dc, tagRECT *r, long Data)
{
  TList *L;
  TMonitor *M;

  L = (TList*)(Data);
  M = new TMonitor;
  M->SetHandle(hm);
  M->SetMonitorNum(L->Count);
  L->Add(M);
  return 1;
}


TMonitor* __fastcall TCustomForm::GetMonitor(void)
{
int i;
HMONITOR HM;

  HM = MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST);

  for(i=0; i<Screen->MonitorCount; i++)
  {
    if(Screen->Monitors[i]->Handle == HM)
      return Screen->Monitors[i];
  }

  // Added from VCL6:
  //if we get here, the Monitors array has changed, so we need to clear and reinitialize it
  Classes::TList* FMonitors = Screen->getMonitors();
  for(i=0; i<Screen->MonitorCount; i++)
  {
    ((TMonitor*)(FMonitors->Items[i]))->Free();
  }
  FMonitors->Clear();
  EnumDisplayMonitors(0, NULL, EnumMonitorsProc, (long)(FMonitors));
  for(i=0; i<Screen->MonitorCount; i++)
  {
    if(Screen->Monitors[i]->Handle == HM)
      return(Screen->Monitors[i]);
  }

  // Additional hack
  if(Screen->MonitorCount>0)
      return Screen->Monitors[0];       // Return anything available.

 return NULL;
}
#endif


#if __BORLANDC__ >= 0x0560 && __BORLANDC__ < 0x660
#include <comctrls.hpp> 
// Problem has been observed in C++Builder 6.0.
// This is already fixed in RAD XE4, provide patch for VCL6.
 
/// Critical defect in C++ builder6 fix, miscalculation of right click position.
void __fastcall TCustomTreeView::WMContextMenu(Messages::TWMContextMenu &Message)
{
  SetFocus();
  if(InvalidPoint(Message.Pos) && (Selected != NULL))
  {
    TPoint CP = ClientToScreen(CenterPoint(Selected->DisplayRect(true)));
    Message.Pos.x = CP.x;
    Message.Pos.y = CP.y;
  }

     // If this line fails to compile please comment it out or apply provided patch.
  Controls::TWinControl::WMContextMenu(Message);        //  inherited;

  MouseUp(mbRight, KeyboardStateToShiftState(),
      Message.Pos.x - ClientOrigin.x,
      Message.Pos.y - ClientOrigin.y);
}
#endif
