#include "TMConfigureAdvEvCntDumpAll.h"
#include "TMConfig.h"
#include "TMAbout.h"
#include "SPDMood.h"

ClassImp(TMConfigureAdvEvCntDumpAll);

//___________________________________________________________________________________
TMConfigureAdvEvCntDumpAll::TMConfigureAdvEvCntDumpAll(const TGWindow *p, const TGWindow *main) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {
  // Constructor.
  fSPD = main;

  fLayout  = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,1,1,1,1);
  fLayout2 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,1,1,1,1);
  fLayout3 = new TGLayoutHints(kLHintsCenterY,1,1,1,1);
  fLayout4 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,10,10,10,10);
  fLayout5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX,10,10,10,10);
  fLayout6 = new TGLayoutHints(kLHintsCenterY,1,1,1,1);

  fTop = new TGButtonGroup(this,"Dump Option",kVerticalFrame);
  fRadio[0]=new TGRadioButton(fTop, "Dump all events", CONF_DUMPALLTRUE);
  fRadio[1]=new TGRadioButton(fTop, "Dump erronious events", CONF_DUMPALLFALSE);
  fTop->Show();
  AddFrame(fTop, fLayout);

  fRadio[0]->SetState(kButtonUp);
  fRadio[1]->SetState(kButtonUp);
  if (TMConfig::Instance()->GETadvEvCntDumpAll()) {
    fRadio[0]->SetState(kButtonDown);
  }
  else {
    fRadio[1]->SetState(kButtonDown);
  }

  fBottom = new TGCompositeFrame(this, 100, 100, kHorizontalFrame);
  AddFrame(fBottom, fLayout);
  fTextB[0] = new TGTextButton(fBottom, "Apply", BUTTON_APPLY);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fBottom, "Close", BUTTON_CANCEL);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fBottom, "Help", BUTTON_HELP);
  fTextB[2]->Associate(this);
  fBottom->AddFrame(fTextB[0], fLayout2);
  fBottom->AddFrame(fTextB[1], fLayout2);
  fBottom->AddFrame(fTextB[2], fLayout2);

  fLetUserCloseWindow = kFALSE;
    
  SetWindowName("Adv Event Counter Dump Option");
  MapSubwindows();
  Resize();
  MapWindow();
}
//___________________________________________________________________________________
TMConfigureAdvEvCntDumpAll::~TMConfigureAdvEvCntDumpAll() {
  // Destructor.
  delete fTop;
  delete fBottom;
  delete[] fRadio;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fTextB;
}
//___________________________________________________________________________________
void TMConfigureAdvEvCntDumpAll::MakeAssignment()
{
  // Sets globals.
  if (kButtonDown == fRadio[0]->GetState()) {
    TMConfig::Instance()->SETadvEvCntDumpAll(kTRUE);
  }
  else {
    TMConfig::Instance()->SETadvEvCntDumpAll(kFALSE);
  }
}
//___________________________________________________________________________________
void TMConfigureAdvEvCntDumpAll::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMConfigureAdvEvCntDumpAll::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  // Event handler function.
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_BUTTON :
      switch (parm1) {
      case BUTTON_APPLY :
	{
	  MakeAssignment();
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
