#include "TMInterDialog.h"
#include <TGButton.h>

ClassImp(TMInterDialog);


TMInterDialog::TMInterDialog(const TGWindow *p, const TGWindow *main, const Char_t *choice1, const Char_t *choice2, TGText *text, UInt_t proceedCommand, UInt_t cancelCommand)
  :TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {

  fProceedCommand = proceedCommand;
  fCancelCommand = cancelCommand;
  fMain = main;

  fL = new TGLayoutHints(kLHintsExpandY, 10, 10, 10, 10);
  fL2 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,1,1,1,1);

  Long_t nrRows = text->RowCount();
  Long_t longestLen = text->GetLineLength(text->GetLongestLine());

  fTextView = new TGTextView(this, longestLen*8+8, nrRows*25+25);
  AddFrame(fTextView, fL);
  fTextView->AddText(text);

  fBottom = new TGCompositeFrame(this, 100, 30, kHorizontalFrame | kFixedSize);
  //  fBottom->SetHeight(60);
  fTextB[0] = new TGTextButton(fBottom, choice1, BUTTON_PROCEED);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fBottom, choice2, BUTTON_CANCEL);
  fTextB[1]->Associate(this);
  fBottom->AddFrame(fTextB[0], fL2);
  fBottom->AddFrame(fTextB[1], fL2);
  AddFrame(fBottom, fL2);


  fLetUserCloseWindow = kFALSE;
    
  MapSubwindows();
  Resize();
  MapWindow();
    
  //  SetWMSizeHints(750, 540, 750, 540, 0, 0);
}

TMInterDialog::~TMInterDialog()
{
  // Destructor.
    
  delete   fTextView;
  delete   fL;
  delete   fL2;
  delete   fBottom;
  delete[] fTextB;
}

void TMInterDialog::CloseWindow()
{
  // Prevents window manager close window function.

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

Bool_t TMInterDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{
  // Event handler.

  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_BUTTON :
      switch (parm1) {
      case BUTTON_PROCEED :
	{
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fMain, MK_MSG(kC_COMMAND,kCM_BUTTON), fProceedCommand, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fMain, MK_MSG(kC_COMMAND,kCM_BUTTON), fCancelCommand, 0);
	  CloseWindow();
	  break;
	}
      }
    }
  }
  return kTRUE;    
}
