#include "TMConfigureDumpN.h"
#include "TMConfig.h"
#include "TMAbout.h"
#include "SPDMood.h"
#include <TGButton.h>
#include <KeySymbols.h>

ClassImp(TMConfigureDumpN)

//___________________________________________________________________________________
TMConfigureDumpN::TMConfigureDumpN(const TGWindow *p, const TGWindow *main) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {
  fSPD = main;
  fDone = kFALSE;
  
  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);

  fTop = new TGGroupFrame(this, "Nr of events to dump:", kHorizontalFrame);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  fF[0] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  Char_t defaultval[10];
  sprintf(defaultval,"%d",TMConfig::Instance()->GETdumpNr());
  fText = new TGTextEntry(fF[0], defaultval, TEXT_VAL);
  fText->Associate(this);
  fF[0]->AddFrame(fText, fLayout);


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

  BindKey(this, gVirtualX->KeysymToKeycode(kKey_Enter), 0);
  BindKey(this, gVirtualX->KeysymToKeycode(kKey_Return), 0);

  fLetUserCloseWindow = kFALSE;
    
  SetWindowName("Dump N Events");
  MapSubwindows();
  Resize();
  MapWindow();
}
//___________________________________________________________________________________
TMConfigureDumpN::~TMConfigureDumpN() {
  delete[] fF;
  delete fTop;
  delete fText;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete fLabel;
  delete[] fTextB;
}
//___________________________________________________________________________________
Bool_t TMConfigureDumpN::HandleKey(Event_t *event) {
  if (fDone) return kTRUE;
  char   input[10];
  Int_t  n;
  UInt_t keysym;

  if (event->fType == kGKeyPress) {
    gVirtualX->LookupString(event, input, sizeof(input), keysym);
    n = strlen(input);

    switch ((EKeySym)keysym) {
    case kKey_Enter:
    case kKey_Return:
      fDone = kTRUE;
      SendMessage(this, MK_MSG(kC_COMMAND,kCM_BUTTON), BUTTON_APPLY, 0);
      break;
    case kKey_Escape:
      delete this;
      return kFALSE;
    default:
      break;
    }
  }
  return TGTransientFrame::HandleKey(event);
}
//___________________________________________________________________________________
void TMConfigureDumpN::MakeAssignment() {
  TMConfig::Instance()->SETdumpNr(atoi(fText->GetText()));
}
//___________________________________________________________________________________
void TMConfigureDumpN::CloseWindow() {
  if (fLetUserCloseWindow == kFALSE)
    DontCallClose();
  else
    UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMConfigureDumpN::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_BUTTON :
      switch (parm1) {
      case BUTTON_APPLY :
	{
	  MakeAssignment();
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), B_DUMPN_START, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
