//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------

/*
  Constructor.
*/
__fastcall TfrmMain::TfrmMain(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------

/*
  Event for the form's creation. (OnCreate)
*/
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
    // The usual stuff
  pgeMain->Align = alClient;
  pnlMain->Align = alClient;

    // Make sure that the first tab is showing. The default behavior is to
    // show the tab that was active during design.
  pgeMain->ActivePageIndex = 0;
}
//---------------------------------------------------------------------------

/*
  Event handler for the OnChange event of any edit box.
*/
void __fastcall TfrmMain::ColorChange(TObject *Sender)
{
    // Read the values from the edit boxes. (The text is AnsiString)
  int red = StrToInt(ledtRed->Text);
  int green = StrToInt(ledtGreen->Text);
  int blue = StrToInt(ledtBlue->Text);

    // Set the panel's color to the values of the edit boxes
  pnlMain->Color = TColor(RGB(red, green, blue));

    // Synchronize the sliders with the edit box values
  barRed->Position = red;
  barGreen->Position = green;
  barBlue->Position = blue;

  pnlMain->Caption = Format("%.2X%.2X%.2X", ARRAYOFCONST((red, green, blue)));
}
//---------------------------------------------------------------------------

/*
  Event handler when any of the sliders moves (OnChange event)
*/
void __fastcall TfrmMain::ColorSlides(TObject *Sender)
{
  spnRed->Position = short(barRed->Position);
  spnGreen->Position = short(barGreen->Position);
  spnBlue->Position = short(barBlue->Position);
}
//---------------------------------------------------------------------------

/*
  Event handler for the OnChange event of the alpha blending slider
*/
void __fastcall TfrmMain::barAlphaBlendChange(TObject *Sender)
{
    // Update the form's AlphaBlend property. (Transparency)
  AlphaBlendValue = (unsigned char)barAlphaBlend->Position;
}
//---------------------------------------------------------------------------

/*
  Redraw the label with the count of maps in combo box
*/
void TfrmMain::UpdateMapCount(void)
{
  int count = lstMaps2->Items->Count;
  lblAvailableMaps->Caption = "&Available maps (" + IntToStr(count) + ")";

    // Make sure there is no text shown if there are no items
  if (!count)
    lstMaps2->Clear();
}

/*
  Handler for *all* check boxes since the code is the same.
*/
void __fastcall TfrmMain::chkMapsClick(TObject *Sender)
{
    /*
      In a robust application, you may want to make sure that the Sender really
      is a TCheckBox. (dynamic_cast)
    */
  TCheckBox *cb = (TCheckBox *)Sender;
  String s = cb->Caption;
  if (cb->Checked)
  {
    int index = lstMaps2->Items->Add(s);
    lstMaps2->ItemIndex = index;
  }
  else
  {
    int index = lstMaps2->Items->IndexOf(s);
    if (index >= 0)
      lstMaps2->Items->Delete(index);
  }
  UpdateMapCount();
}
//---------------------------------------------------------------------------

/*
  Called from the event handlers to check/uncheck all. (Code reuse)
*/
void TfrmMain::SetChecks(bool state)
{
  int count = grpMaps->ControlCount;
  for (int i = 0; i < count; i++)
  {
    TCheckBox *cb = (TCheckBox *)grpMaps->Controls[i];
    cb->Checked = state;
  }
}

/*
  Handles the click on "Select all"
*/
void __fastcall TfrmMain::btnSelectAllClick(TObject *Sender)
{
  SetChecks(true);  // Check all
  UpdateMapCount(); // Update label
}
//---------------------------------------------------------------------------

/*
  Handles the click on "Select none"
*/
void __fastcall TfrmMain::btnSelectNoneClick(TObject *Sender)
{
  SetChecks(false); // Uncheck all
  UpdateMapCount(); // Update label
}
//---------------------------------------------------------------------------