#include "TMIOMask.h"
#include "TMAbout.h"
#include "TMControlSPD.h"
#include <TGButton.h>

ClassImp(TMIOMask)

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

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

  fTop = new TGGroupFrame(this, "Choose a directory:", kVerticalFrame);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  TString defaultDir = Form("%s/config/mask",getenv("SPDMOOD_SITE"));
  fF[0] = new TGCompositeFrame(this, 10,10, kVerticalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  fFrame = new TGCompositeFrame(fF[0], 10,10, kHorizontalFrame);
  fText = new TGTextEntry(fFrame, defaultDir.Data(), TEXT_VAL0);
  fText->Associate(this);
  fText->SetWidth(450);
  fFrame->AddFrame(fText, fLayout5);
  fF[0]->AddFrame(fFrame, fLayout5);


  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_CLOSE);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fF[1], "Clear", BUTTON_HELP);
  fTextB[2]->Associate(this);
  fF[1]->AddFrame(fTextB[0], fLayout2);
  fF[1]->AddFrame(fTextB[1], fLayout2);
  fF[1]->AddFrame(fTextB[2], fLayout2);


  fLetUserCloseWindow = kFALSE;
    
  if (bLoad==kTRUE) SetWindowName("Load Pixel Mask");
  else SetWindowName("Save Pixel Mask");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
TMIOMask::~TMIOMask()
{
  // Destructor.
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fF;
  delete fTop;
  delete fText;
  delete[] fTextB;
}

//___________________________________________________________________________________
void TMIOMask::MakeAssignment()
{
  if (bLoad) {
    TMControlSPD::Instance()->LoadMask(fText->GetText());
  }
  else {
    TMControlSPD::Instance()->SaveMask(fText->GetText());
  }
}

//___________________________________________________________________________________
void TMIOMask::CloseWindow()
{
  // Prevents usage of window manager close button.

  if (fLetUserCloseWindow == kFALSE)
    DontCallClose();
  else
    //DestroyWindow();
    UnmapWindow();
}

//___________________________________________________________________________________
Bool_t TMIOMask::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;
	  //	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), DEADNOISY_START, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CLOSE :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "README");
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
