#include "TMToolBar.h"
#include "SPDMood.h"

ClassImp(TMToolBar)

//___________________________________________________________________________________
TMToolBar::TMToolBar(TGWindow *handler, TGWindow *p, UInt_t w, UInt_t h)
  :TGCompositeFrame(p, w, h, kVerticalFrame) {

  Int_t nrInFirstRow=4;

  fL[0] = new TGLayoutHints(kLHintsTop | kLHintsLeft,0,0,0,0);
  fL[1] = new TGLayoutHints(kLHintsTop | kLHintsRight,0,0,0,0);
  fL[2] = new TGLayoutHints(kLHintsTop | kLHintsCenterX,0,0,0,0);
  fL[3] = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,0,0,0,0);
  fL[4] = new TGLayoutHints(kLHintsBottom | kLHintsRight | kLHintsExpandX,0,0,0,0);
  fL[5] = new TGLayoutHints(kLHintsExpandX ,0,0,0,0);
  fL[6] = new TGLayoutHints(kLHintsExpandX  | kLHintsExpandY ,0,0,0,0);

  gClient->GetColorByName("green", green);
  gClient->GetColorByName("gray", gray);

  fTopFR = new TGCompositeFrame(this, w, 22, kHorizontalFrame | kFixedSize);
  AddFrame(fTopFR,fL[5]);
  fBottomFR = new TGCompositeFrame(this, w, 22, kHorizontalFrame | kFixedSize);
  AddFrame(fBottomFR,fL[5]);

  // the order of the buttons below must be the same as in SPDMood.h !!!

  TGCompositeFrame *framePtr;
  framePtr = fTopFR;

  Int_t picind=0;
  toolTipText[picind] ="Update displays every X sec.";
  fButton[picind] = new TGTextButton(framePtr, "AUTO-UPDATE ALL", TOOL_BAR_BUTTON_AUTOUPDATE);

  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Reset histograms after each event.";
  fButton[picind] = new TGTextButton(framePtr, "AUTO-RESET ALL", TOOL_BAR_BUTTON_AUTORESET);

  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Skip pixel data parsing.";
  fButton[picind] = new TGTextButton(framePtr, "SKIP PIXEL DATA", TOOL_BAR_BUTTON_SKIPPIXEL);

  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Skip clustering algorithm.";
  fButton[picind] = new TGTextButton(framePtr, "SKIP CLUSTERING", TOOL_BAR_BUTTON_SKIPCLUSTER);





  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Try to show data \neven though the event has inconsistencies.";
  fButton[picind] = new TGTextButton(framePtr, "DISP INCONS EVENTS", TOOL_BAR_BUTTON_TRYDISPERR);

  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Stop Run on Consistency Error.";
  fButton[picind] = new TGTextButton(framePtr, "STOP ON ERROR", TOOL_BAR_BUTTON_STOPONERR);

  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
  toolTipText[picind] ="Skip advanced event counter check. \n(Error ID 15 and 16)\nSee also command line.";
  fButton[picind] = new TGTextButton(framePtr, "SKIP ADV EVCOUNT CHECK", TOOL_BAR_BUTTON_SKIP_ADV_EVC_CHECK);

//!!!  picind++;  if (picind>=nrInFirstRow) framePtr = fBottomFR;
//!!!  toolTipText[picind] ="Check for mismatch in bunch crossing fields. \nSee command line.";
//!!!  fButton[picind] = new TGTextButton(framePtr, "MINI EV ID CHECK", TOOL_BAR_BUTTON_MINIEV_ID_CHECK);

  for (Int_t i=0 ; i<NR_TOOLBAR_BUTTONS ; i++) {
    fButton[i]->SetToolTipText(toolTipText[i], 1000);
    fButton[i]->Associate(this);
    UsableButtons[i] = kTRUE;
    if (i<nrInFirstRow) {
      fTopFR->AddFrame(fButton[i], fL[3]);
    }
    else {
      fBottomFR->AddFrame(fButton[i], fL[3]);
    }
  }

  while ((picind+1)%nrInFirstRow != 0) {
    TGFrame *dummy = new TGFrame(fBottomFR);
    fBottomFR->AddFrame(dummy, fL[3]);
    picind++;
  }


  MapSubwindows();
  Resize();
  MapWindow();

  //default overlap on
  //  ToggleButton(TOOL_BAR_BUTTON_OVERLAP); // NOT ANYMORE!!!
}

//___________________________________________________________________________________
TMToolBar::~TMToolBar() 
{
  // Destructor.

  delete[] fL;
  delete   fTopFR;
  delete   fBottomFR;
  delete[] fButton;
}

void TMToolBar::SetButtonDown(Int_t id) {
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  fButton[index]->AllowStayDown(kTRUE);
  fButton[index]->SetBackgroundColor(green);
  fButton[index]->SetState(kButtonDown);
}
void TMToolBar::SetButtonUp(Int_t id) {
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  fButton[index]->AllowStayDown(kFALSE);
  fButton[index]->SetBackgroundColor(gray);
  fButton[index]->SetState(kButtonUp);
}

//___________________________________________________________________________________
Bool_t TMToolBar::IsToggledDown(Int_t id)
{
  // Returns kTRUE if toggled down.
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  if (kButtonDown == fButton[index]->GetState())
    return kTRUE;
  else 
    return kFALSE;
}

//___________________________________________________________________________________
void TMToolBar::ToggleButton(Int_t id)
{
  // Toggles the button having the id.

  if (IsButtonUsable(id) == kFALSE) return;

  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  if (fButton[index]->GetState() != kButtonUp) {
    SetButtonUp(id);
    return;
  }
	
  if (!IsToggledDown(id)) {
    SetButtonDown(id);
    return;
  }
}

//___________________________________________________________________________________
void TMToolBar::DisableButtonGrayPicture(Int_t id)
{
  //  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE;
//  if (index==0) {
//    ((TGPictureButton*)fButton[index])->SetPicture((const TGPicture*)fPicturePool->GetPicture(d_buttonClosePath));
//  }
  DisableButton(id);
}

//___________________________________________________________________________________
void TMToolBar::DisableButton(Int_t id)
{
  // Disables the button having the id.
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  fButton[index]->SetToolTipText("Disabled for the current session.", 1000);
  UsableButtons[index] = kFALSE; 
  fButton[index]->SetEnabled(kFALSE);
}

//___________________________________________________________________________________
void TMToolBar::EnableButton(Int_t id)
{
  // Enables the button having the id.
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  fButton[index]->SetOn(kTRUE);
  fButton[index]->SetToolTipText(toolTipText[index], 1000);
  fButton[index]->SetEnabled(kTRUE);
  UsableButtons[index] = kTRUE;
}

//___________________________________________________________________________________
Bool_t TMToolBar::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{
  // Default event handler function.

  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_CHECKBUTTON :
    case kCM_BUTTON :
    case kCM_MENU:
      switch (parm1) {
      case TOOL_BAR_BUTTON_AUTOUPDATE :
	ToggleButton(TOOL_BAR_BUTTON_AUTOUPDATE);
	break;
      case TOOL_BAR_BUTTON_AUTORESET :
	ToggleButton(TOOL_BAR_BUTTON_AUTORESET);
	break;
      case TOOL_BAR_BUTTON_SKIPPIXEL :
	ToggleButton(TOOL_BAR_BUTTON_SKIPPIXEL);
	break;
      case TOOL_BAR_BUTTON_SKIPCLUSTER :
	ToggleButton(TOOL_BAR_BUTTON_SKIPCLUSTER);
	break;
      case TOOL_BAR_BUTTON_TRYDISPERR :
	ToggleButton(TOOL_BAR_BUTTON_TRYDISPERR);
	break;
      case TOOL_BAR_BUTTON_STOPONERR :
	ToggleButton(TOOL_BAR_BUTTON_STOPONERR);
	break;
      case TOOL_BAR_BUTTON_SKIP_ADV_EVC_CHECK :
	ToggleButton(TOOL_BAR_BUTTON_SKIP_ADV_EVC_CHECK);
	break;
      case TOOL_BAR_BUTTON_MINIEV_ID_CHECK :
	ToggleButton(TOOL_BAR_BUTTON_MINIEV_ID_CHECK);
	break;
      }
      break;
    }
    break;
  }
  return kTRUE;
}

//___________________________________________________________________________________
void TMToolBar::SetButtonUsable(Int_t id)
{
  // Sets the button having the id as usable.
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  UsableButtons[index] = kTRUE;
}

//___________________________________________________________________________________
Bool_t TMToolBar::IsButtonUsable(Int_t id)
{
  // Returns true if the button having the id is usable.
  UInt_t index = id - TOOL_BAR_BUTTON_AUTOUPDATE; 
  return UsableButtons[index];
}
