#include "wx/filedlg.h"
#include "MyTextFrame.h"
#include "Ltypes.h" //for debugging messages

BEGIN_EVENT_TABLE(MyTextFrame, wxDialog)
  EVT_BUTTON(TF_GENERATE, MyTextFrame::OnGenerate)
  EVT_BUTTON(TF_REVERT,   MyTextFrame::OnRevert)
  EVT_BUTTON(TF_LOAD,     MyTextFrame::OnLoad)
  EVT_TEXT(  TF_TEXT,     MyTextFrame::OnTextChange) 
END_EVENT_TABLE()


/* Text Frame Methods */

MyTextFrame::MyTextFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
  wxDialog(parent, -1, title, pos, size, wxRESIZE_BORDER | wxDIALOG_MODELESS | wxDEFAULT_DIALOG_STYLE)
{
  LoadButton    =new wxButton(this,TF_LOAD,"Load",wxPoint(20,30) );
  GenerateButton=new wxButton(this,TF_GENERATE,"Generate", wxPoint(50,30) );
  RevertButton  =new wxButton(this,TF_REVERT,"Revert",wxPoint(80,30));
  TheTextBox = new wxTextCtrl(this,TF_TEXT,"", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE | wxHSCROLL  );

  wxSize temp=GenerateButton->GetDefaultSize();

  wxLayoutConstraints *bt1 = new wxLayoutConstraints;
  wxLayoutConstraints *bt2 = new wxLayoutConstraints;
  wxLayoutConstraints *bt3 = new wxLayoutConstraints;
  wxLayoutConstraints *txt = new wxLayoutConstraints;
 
  bt1->width.Absolute      (temp.GetWidth());
  bt1->height.Absolute     (temp.GetHeight());
  bt1->bottom.SameAs       (this, wxBottom);
  bt1->centreX.PercentOf   (this, wxWidth, 25);
  bt2->width.Absolute      (temp.GetWidth());
  bt2->height.Absolute     (temp.GetHeight());
  bt2->bottom.SameAs       (this, wxBottom);
  bt2->centreX.PercentOf   (this, wxWidth, 50);
  bt3->width.Absolute      (temp.GetWidth());
  bt3->height.Absolute     (temp.GetHeight());
  bt3->bottom.SameAs       (this, wxBottom);
  bt3->centreX.PercentOf   (this, wxWidth, 75);
  txt->top.SameAs          (this, wxTop);
  txt->bottom.Above        (LoadButton,-4);
  txt->right.SameAs        (this, wxRight);
  txt->left.SameAs         (this, wxLeft);

  SetAutoLayout(TRUE);
  LoadButton->SetConstraints(bt1);
  GenerateButton->SetConstraints(bt2);
  RevertButton->SetConstraints(bt3);

  TheTextBox->SetConstraints(txt);
  GenerateButton->Show(FALSE);
  RevertButton->Show(FALSE);
  LoadButton->Show(TRUE);
  TheTextBox->SetBackgroundColour(*wxWHITE);
  TheTextBox->Show(TRUE);
  changed=false;
}

void MyTextFrame::SetChanged(bool change)
{
  if (change!=changed) {
    changed=change;
    if (change) {
      TheTextBox->SetBackgroundColour(wxColour(255,180,180));
      TheTextBox->SetForegroundColour(*wxBLACK);
      TheTextBox->Refresh(TRUE);
      GenerateButton->Show(TRUE);
      RevertButton->Show(TRUE);
    } else {
      TheTextBox->SetBackgroundColour(*wxWHITE);
      TheTextBox->SetForegroundColour(*wxBLACK);
      TheTextBox->Refresh(TRUE);
      GenerateButton->Show(FALSE);
      RevertButton->Show(FALSE);
      ThePreviousString=TheTextBox->GetValue();
    };
  };
}

void MyTextFrame::generate()
{
  SetChanged(FALSE);
}

void MyTextFrame::revert()
{
  PutText(ThePreviousString);
}

void MyTextFrame::load()
{
  wxFileDialog open_d(this, "Choose an Lstring file to open", "", "", "*.ls", wxOPEN);
  if (open_d.ShowModal()==wxID_OK)
    {
      TheTextBox->LoadFile(open_d.GetPath());
      //      generate();
    };
}

void MyTextFrame::PutText(wxString TheString)
{
  int pos;
  pos=TheTextBox->GetScrollPos(wxVERTICAL);
  pos=5;
  TheTextBox->Show(FALSE);
  TheTextBox->SetValue( TheString );
  SetChanged(FALSE);
DEBUGGING_MESSAGE2("scroll position should be %i:",pos);
  TheTextBox->SetScrollPos(wxVERTICAL,pos);
  TheTextBox->Show(TRUE);
  TheTextBox->SetScrollPos(wxVERTICAL,pos);
DEBUGGING_MESSAGE2("is %i\n",pos=TheTextBox->GetScrollPos(wxVERTICAL));
}

wxString MyTextFrame::GetText()
{
  return TheTextBox->GetValue();
}

/* event Handlers */

void MyTextFrame::OnGenerate(wxCommandEvent &event)
{
  generate();
  event.Skip();
}

void MyTextFrame::OnRevert(wxCommandEvent &event)
{ 
  revert(); 
}
void MyTextFrame::OnLoad(wxCommandEvent &event)
{ 
  load();
}

void MyTextFrame::OnTextChange(wxCommandEvent &event)
{
  SetChanged(TRUE);
}


