#include <TObjArray.h>
#include <TGProgressBar.h>
#include <TGWindow.h>
#include <TSystem.h>
#include <TGLabel.h>
#include <TGButton.h>
#include <TApplication.h>
#include <TROOT.h>

class TProgressMeter : public TGTransientFrame {

 private:
  TObjArray fWidgets;
  TGHProgressBar *fProgressBar;
  TGLabel *fLabel;

 public:
  TProgressMeter(const TGWindow *p, const TGWindow *main);
  virtual ~TProgressMeter() {fWidgets.Delete();}

  virtual void CloseWindow() {;} // Do nothing
  void Increment(const Float_t inc) {fProgressBar->Increment(inc);}
  void SetLabel(const Text_t *label) {fLabel->SetText(label); fClient->NeedRedraw(fLabel);}

};

TProgressMeter::TProgressMeter(const TGWindow *p, const TGWindow *main) :
  TGTransientFrame(p, main, 100, 100)
{

  // Layout Hints
  TGLayoutHints *ExpandX = new TGLayoutHints(kLHintsExpandX,5,5,5,5);
  fWidgets.Add(ExpandX);

  // Label
  fLabel = new TGLabel(this,"Default Label");
  fWidgets.Add(fLabel);
  AddFrame(fLabel, ExpandX);

  // Progress Meter Bar
  fProgressBar = new TGHProgressBar(this, TGProgressBar::kFancy, 300);
  fWidgets.Add(fProgressBar);
  fProgressBar->SetRange(0,100);
  fProgressBar->SetBarColor("lightblue");
  fProgressBar->ShowPosition(kTRUE, kTRUE, "%.0f %% complete");

  AddFrame(fProgressBar, ExpandX);

  MapSubwindows();
  Resize(GetDefaultSize());
  MapWindow();

}

class MainWindow : public TGMainFrame {
 private:
  TObjArray fWidgets;

 public:
  MainWindow();
  ~MainWindow() {fWidgets.Delete();gApplication->Terminate(0);}
  void CloseWindow() {delete this;}
  Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);

};

MainWindow::MainWindow() : TGMainFrame(gClient->GetRoot(), 10, 10, kHorizontalFrame)
{


  TGLayoutHints *lo = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,5,5,5,5);
  fWidgets.Add(lo);

  TGTextButton *b = new TGTextButton(this,"Press Here");
  fWidgets.Add(b);
  AddFrame(b,lo);
  b->Associate(this);

  MapSubwindows();
  Resize(GetDefaultSize());
  MapWindow();

}

Bool_t MainWindow::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{

  TProgressMeter *meter = new TProgressMeter(fClient->GetRoot(),fClient->GetRoot());
  meter->SetLabel("Initializing...");
  fClient->ProcessEventsFor(meter);
  gSystem->Sleep(2000);
  meter->SetLabel("Processing...");
  fClient->ProcessEventsFor(meter);
  gSystem->Sleep(2000);
  fClient->ProcessEventsFor(meter);
  for (Int_t i=0; i<100; i++) {
    meter->Increment(1);
    fClient->ProcessEventsFor(meter);
    gSystem->Sleep(40);
  }
  delete meter;

  return kTRUE;

}

int main (int argc, char **argv) {

  TApplication theApp("Progress Meter", &argc, argv);
  MainWindow *MyDisplay = new MainWindow();
  theApp.Run();
  return(0);

}




