// @(#)root/gui:$Id$
// Author: Bertrand Bellenot   26/09/2007

#include "TROOT.h"
#include "TSystem.h"
#include "TRint.h"
#include "TApplication.h"
#include "TGClient.h"
#include "TGLabel.h"
#include "TGFrame.h"
#include "TGLayout.h"
#include "TGComboBox.h"
#include "TGTextView.h"
#include "TGTextEntry.h"
#include "TGTextEdit.h"
#include "TInterpreter.h"
#include "Getline.h"

#include "TGCommandPlugin.h"

//_____________________________________________________________________________
//
// TGCommandPlugin
//
// Class used to redirect command line input/output.
//_____________________________________________________________________________

ClassImp(TGCommandPlugin)

//______________________________________________________________________________
TGCommandPlugin::TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h) :
      TGMainFrame(p, w, h)
{
   // TGCommandPlugin Constructor.

   SetCleanup(kDeepCleanup);
   fHf = new TGHorizontalFrame(this, 100, 20);
   fComboCmd   = new TGComboBox(fHf, "", 1);
   fCommand    = fComboCmd->GetTextEntry();
   fCommandBuf = fCommand->GetBuffer();
   fComboCmd->Resize(200, fCommand->GetDefaultHeight());
   fHf->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY |
                 kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
   fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
                 new TGLayoutHints(kLHintsCenterY | kLHintsRight, 
                 5, 5, 1, 1));
   AddFrame(fHf, new TGLayoutHints(kLHintsLeft | kLHintsTop | 
            kLHintsExpandX, 3, 3, 3, 3));
   fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
                     "HandleCommand()");

   Pixel_t pxl;
   gClient->GetColorByName("#3399ff", pxl);
   fStatus = new TGTextView(this, 10, 100, 1);
   fStatus->SetSelectBack(pxl);
   fStatus->SetSelectFore(TGFrame::GetWhitePixel());
   AddFrame(fStatus, new TGLayoutHints(kLHintsLeft | kLHintsTop | 
            kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
   fPid = gSystem->GetPid();
   TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
                        gSystem->HomeDirectory())));
   FILE *lunin = fopen(defhist.Data(), "rt");
   if (lunin) {
      char histline[256];
      while (fgets(histline, 256, lunin)) {
         histline[strlen(histline)-1] = 0; // remove trailing "\n"
         fComboCmd->InsertEntry(histline, 0, -1);
      }
      fclose(lunin);
   }
   fTimer = new TTimer(this, 1000);
   fTimer->Reset();
   fTimer->TurnOn();
   MapSubwindows();
   Resize(GetDefaultSize());
   MapWindow();
}

//______________________________________________________________________________
TGCommandPlugin::~TGCommandPlugin()
{
   // Destructor.

   TString pathtmp = TString::Format("%s/command.%d.log", 
                                     gSystem->TempDirectory(), fPid);
   gSystem->Unlink(pathtmp);
   delete fTimer;
   Cleanup();
}

//______________________________________________________________________________
void TGCommandPlugin::CheckRemote(const char * /*str*/)
{
   // Check if actual ROOT session is a remote one or a local one.

   Pixel_t pxl;
   TApplication *app = gROOT->GetApplication();
   if (!app->InheritsFrom("TRint"))
      return;
   TString sPrompt = ((TRint*)app)->GetPrompt();
   Int_t end = sPrompt.Index(":root [", 0);
   if (end > 0 && end != kNPOS) {
      // remote session
      sPrompt.Remove(end);
      gClient->GetColorByName("#ff0000", pxl);
      fLabel->SetTextColor(pxl);
      fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
   }
   else {
      // local session
      gClient->GetColorByName("#000000", pxl);
      fLabel->SetTextColor(pxl);
      fLabel->SetText("Command (local):");
   }
   fHf->Layout();
}

//______________________________________________________________________________
void TGCommandPlugin::HandleCommand()
{
   // Handle command line from the "command" combo box.

   const char *string = fCommandBuf->GetString();
   if (strlen(string) > 1) {
      // form temporary file path
      TString sPrompt = "root []";
      TString pathtmp = TString::Format("%s/command.%d.log", 
                                        gSystem->TempDirectory(), fPid);
      TApplication *app = gROOT->GetApplication();
      if (app->InheritsFrom("TRint"))
         sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
      FILE *lunout = fopen(pathtmp.Data(), "a+t");
      if (lunout) {
         fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
         fclose(lunout);
      }
      gSystem->RedirectOutput(pathtmp.Data(), "a");
      gApplication->SetBit(TApplication::kProcessRemotely);
      gROOT->ProcessLine(string);
      fComboCmd->InsertEntry(string, 0, -1);
      if (app->InheritsFrom("TRint"))
         Gl_histadd((char *)string);
      gSystem->RedirectOutput(0);
      fStatus->LoadFile(pathtmp.Data());
      fStatus->ShowBottom();
      CheckRemote(string);
      fCommand->Clear();
   }
}

//______________________________________________________________________________
Bool_t TGCommandPlugin::HandleTimer(TTimer *t)
{
   // Handle timer event.

   if (t != fTimer) return kTRUE;
   CheckRemote("");
   return kTRUE;
}
 TGCommandPlugin.cxx:1
 TGCommandPlugin.cxx:2
 TGCommandPlugin.cxx:3
 TGCommandPlugin.cxx:4
 TGCommandPlugin.cxx:5
 TGCommandPlugin.cxx:6
 TGCommandPlugin.cxx:7
 TGCommandPlugin.cxx:8
 TGCommandPlugin.cxx:9
 TGCommandPlugin.cxx:10
 TGCommandPlugin.cxx:11
 TGCommandPlugin.cxx:12
 TGCommandPlugin.cxx:13
 TGCommandPlugin.cxx:14
 TGCommandPlugin.cxx:15
 TGCommandPlugin.cxx:16
 TGCommandPlugin.cxx:17
 TGCommandPlugin.cxx:18
 TGCommandPlugin.cxx:19
 TGCommandPlugin.cxx:20
 TGCommandPlugin.cxx:21
 TGCommandPlugin.cxx:22
 TGCommandPlugin.cxx:23
 TGCommandPlugin.cxx:24
 TGCommandPlugin.cxx:25
 TGCommandPlugin.cxx:26
 TGCommandPlugin.cxx:27
 TGCommandPlugin.cxx:28
 TGCommandPlugin.cxx:29
 TGCommandPlugin.cxx:30
 TGCommandPlugin.cxx:31
 TGCommandPlugin.cxx:32
 TGCommandPlugin.cxx:33
 TGCommandPlugin.cxx:34
 TGCommandPlugin.cxx:35
 TGCommandPlugin.cxx:36
 TGCommandPlugin.cxx:37
 TGCommandPlugin.cxx:38
 TGCommandPlugin.cxx:39
 TGCommandPlugin.cxx:40
 TGCommandPlugin.cxx:41
 TGCommandPlugin.cxx:42
 TGCommandPlugin.cxx:43
 TGCommandPlugin.cxx:44
 TGCommandPlugin.cxx:45
 TGCommandPlugin.cxx:46
 TGCommandPlugin.cxx:47
 TGCommandPlugin.cxx:48
 TGCommandPlugin.cxx:49
 TGCommandPlugin.cxx:50
 TGCommandPlugin.cxx:51
 TGCommandPlugin.cxx:52
 TGCommandPlugin.cxx:53
 TGCommandPlugin.cxx:54
 TGCommandPlugin.cxx:55
 TGCommandPlugin.cxx:56
 TGCommandPlugin.cxx:57
 TGCommandPlugin.cxx:58
 TGCommandPlugin.cxx:59
 TGCommandPlugin.cxx:60
 TGCommandPlugin.cxx:61
 TGCommandPlugin.cxx:62
 TGCommandPlugin.cxx:63
 TGCommandPlugin.cxx:64
 TGCommandPlugin.cxx:65
 TGCommandPlugin.cxx:66
 TGCommandPlugin.cxx:67
 TGCommandPlugin.cxx:68
 TGCommandPlugin.cxx:69
 TGCommandPlugin.cxx:70
 TGCommandPlugin.cxx:71
 TGCommandPlugin.cxx:72
 TGCommandPlugin.cxx:73
 TGCommandPlugin.cxx:74
 TGCommandPlugin.cxx:75
 TGCommandPlugin.cxx:76
 TGCommandPlugin.cxx:77
 TGCommandPlugin.cxx:78
 TGCommandPlugin.cxx:79
 TGCommandPlugin.cxx:80
 TGCommandPlugin.cxx:81
 TGCommandPlugin.cxx:82
 TGCommandPlugin.cxx:83
 TGCommandPlugin.cxx:84
 TGCommandPlugin.cxx:85
 TGCommandPlugin.cxx:86
 TGCommandPlugin.cxx:87
 TGCommandPlugin.cxx:88
 TGCommandPlugin.cxx:89
 TGCommandPlugin.cxx:90
 TGCommandPlugin.cxx:91
 TGCommandPlugin.cxx:92
 TGCommandPlugin.cxx:93
 TGCommandPlugin.cxx:94
 TGCommandPlugin.cxx:95
 TGCommandPlugin.cxx:96
 TGCommandPlugin.cxx:97
 TGCommandPlugin.cxx:98
 TGCommandPlugin.cxx:99
 TGCommandPlugin.cxx:100
 TGCommandPlugin.cxx:101
 TGCommandPlugin.cxx:102
 TGCommandPlugin.cxx:103
 TGCommandPlugin.cxx:104
 TGCommandPlugin.cxx:105
 TGCommandPlugin.cxx:106
 TGCommandPlugin.cxx:107
 TGCommandPlugin.cxx:108
 TGCommandPlugin.cxx:109
 TGCommandPlugin.cxx:110
 TGCommandPlugin.cxx:111
 TGCommandPlugin.cxx:112
 TGCommandPlugin.cxx:113
 TGCommandPlugin.cxx:114
 TGCommandPlugin.cxx:115
 TGCommandPlugin.cxx:116
 TGCommandPlugin.cxx:117
 TGCommandPlugin.cxx:118
 TGCommandPlugin.cxx:119
 TGCommandPlugin.cxx:120
 TGCommandPlugin.cxx:121
 TGCommandPlugin.cxx:122
 TGCommandPlugin.cxx:123
 TGCommandPlugin.cxx:124
 TGCommandPlugin.cxx:125
 TGCommandPlugin.cxx:126
 TGCommandPlugin.cxx:127
 TGCommandPlugin.cxx:128
 TGCommandPlugin.cxx:129
 TGCommandPlugin.cxx:130
 TGCommandPlugin.cxx:131
 TGCommandPlugin.cxx:132
 TGCommandPlugin.cxx:133
 TGCommandPlugin.cxx:134
 TGCommandPlugin.cxx:135
 TGCommandPlugin.cxx:136
 TGCommandPlugin.cxx:137
 TGCommandPlugin.cxx:138
 TGCommandPlugin.cxx:139
 TGCommandPlugin.cxx:140
 TGCommandPlugin.cxx:141
 TGCommandPlugin.cxx:142
 TGCommandPlugin.cxx:143
 TGCommandPlugin.cxx:144
 TGCommandPlugin.cxx:145
 TGCommandPlugin.cxx:146
 TGCommandPlugin.cxx:147
 TGCommandPlugin.cxx:148
 TGCommandPlugin.cxx:149
 TGCommandPlugin.cxx:150
 TGCommandPlugin.cxx:151
 TGCommandPlugin.cxx:152
 TGCommandPlugin.cxx:153
 TGCommandPlugin.cxx:154
 TGCommandPlugin.cxx:155
 TGCommandPlugin.cxx:156
 TGCommandPlugin.cxx:157
 TGCommandPlugin.cxx:158
 TGCommandPlugin.cxx:159