#include "TMDisplayingTextView.h"
#include "TMHistogrammingEventDump.h"
#include <TSystem.h>
#include <TGTextView.h>

ClassImp(TMDisplayingTextView)

//_____________________________________________________________________
TMDisplayingTextView::TMDisplayingTextView(const Char_t *frameName, const TGWindow *p, UInt_t width, UInt_t height, TMHistogramming *hist, UInt_t id, UInt_t plotNr, UInt_t cloneId) :
  TMDisplaying(frameName,p,width,height,hist,id,plotNr,cloneId,kTRUE)
{
  fTheCanvas=NULL;

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

  UpdateWindowName();
  CreateTopFrame();
  CreateDumpOptionFrame();
  CreateBottomFrame();

  SetWMSize(width, height);
  SetWMSizeHints(150, 150, 2000, 1000, 0, 0);
  MapSubwindows();
  Resize();
  MapWindow();

  fHist->Dump(fTheTextView);
  Update();
}
//_____________________________________________________________________
TMDisplayingTextView::~TMDisplayingTextView() {}
//_____________________________________________________________________
void TMDisplayingTextView::CreateTopFrame() {
  fTheTextView = new TGTextView(this, 10,10);
  AddFrame(fTheTextView, new TGLayoutHints(kLHintsExpandX  | kLHintsExpandY,0,0,0,0));
}

//_____________________________________________________________________
void TMDisplayingTextView::CreateDumpOptionFrame() {
  TGCompositeFrame *tf = new TGCompositeFrame(this, 50, 21, kHorizontalFrame | kFixedSize);
  AddFrame(tf, new TGLayoutHints(kLHintsExpandX, 0,0,0,0));

  fRadioHere = new TGRadioButton(tf, "Dump Here", DUMP_HERE);
  tf->AddFrame(fRadioHere,new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 20,0,0,0));
  fRadioHere->Associate(this);
  fRadioHere->SetState(kButtonUp);
  fRadioTerm = new TGRadioButton(tf, "Dump To Terminal", DUMP_TERM);
  tf->AddFrame(fRadioTerm,new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 20,0,0,0));
  fRadioTerm->Associate(this);
  fRadioTerm->SetState(kButtonDown);

}
//_____________________________________________________________________
void TMDisplayingTextView::Update() {
  if (fHist->NeedRedump()) fHist->Dump(fTheTextView);
  
  TString msg;
  msg = Form("Events: %d",fHist->GetNrEvents());
  fStatusBar->SetText(msg.Data(),0);
  msg = Form("Last: %d",fHist->GetLastEventNumber());
  fStatusBar->SetText(msg.Data(),1);
  gSystem->ProcessEvents();
}
//______________________________________________________________________
Bool_t TMDisplayingTextView::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {

    case kCM_RADIOBUTTON :
      if (parm1==DUMP_HERE) {
	//	if (((TMHistogrammingEventDump*)fHist)->GetDumpToTerminal()) {
	fRadioHere->SetState(kButtonDown);
	fRadioTerm->SetState(kButtonUp);
	((TMHistogrammingEventDump*)fHist)->SetDumpToTerminal(kFALSE);
	//	}
	return kTRUE;
      }
      else if (parm1==DUMP_TERM) {
	fRadioTerm->SetState(kButtonDown);
	fRadioHere->SetState(kButtonUp);
	((TMHistogrammingEventDump*)fHist)->SetDumpToTerminal(kTRUE);
	return kTRUE;
      }
      break;

    }
  }
  return TMDisplaying::ProcessMessage(msg,parm1,parm2);
}
