Hello.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: Hello.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Valeriy Onuchin & Fons Rademakers   04/10/98
00003 
00004 ///////////////////////////////////////////////////////////////////
00005 //  Animated Text with cool wave effect.
00006 //
00007 //  ROOT implementation of the hello world example borrowed
00008 //  from the Qt hello world example.
00009 //
00010 //  To run this example do the following:
00011 //  $ root
00012 //  root [0] gSystem.Load("Hello")
00013 //  root [1] Hello h
00014 //  <enjoy>
00015 //  root [2] .q
00016 //
00017 //  Other ROOT fun examples: Tetris, Aclock ...
00018 //
00019 //  Begin_Html
00020 // <img src="http://emcal06.rhic.bnl.gov/~onuchin/root/gif/hello_clock.gif">
00021 //  End_Html
00022 //
00023 ///////////////////////////////////////////////////////////////////
00024 
00025 #include <TROOT.h>
00026 #include <TSystem.h>
00027 #include "Hello.h"
00028 #include "TList.h"
00029 
00030 ClassImp(Hello)
00031 
00032 
00033 TChar::TChar(char ch, Coord_t x, Coord_t y) : TText(x, y, "")
00034 {
00035    // Create single character text.
00036 
00037    SetTitle(Form("%c",ch));
00038 }
00039 
00040 Float_t TChar::GetWidth()
00041 {
00042    // Return character width
00043 
00044    UInt_t w,h;
00045 
00046    if (!TVirtualPad::Pad()) return 0;
00047 
00048    Float_t wh = (Float_t)gPad->XtoAbsPixel(gPad->GetX2());
00049    GetTextExtent(w, h, (char*)GetTitle());
00050    return  w/wh;
00051 }
00052 
00053 
00054 Hello::Hello(const char *text) : TTimer(40, kTRUE)
00055 {
00056    // Hello constructor
00057 
00058    TChar *ch;
00059    fI = 0;
00060    fList = new TList();
00061 
00062    if (!TVirtualPad::Pad())
00063       fPad = new TCanvas("Hello:canvas","ROOT says Hello!",200,200,400,200);
00064    else
00065       fPad = (TPad*)gPad;
00066 
00067    Connect(fPad, "Closed()", "TTimer", this, "TurnOff()");
00068 
00069    while(text[fI]) {               // create  list of characters
00070       ch = new TChar(text[fI]);
00071       ch->SetTextFont(72);         // times-bold-r-normal
00072       ch->SetTextSize(0.3);
00073       ch->Modify();
00074       fI++;
00075       fList->Add(ch);
00076    }
00077 
00078    Draw();                         // append this to current pad
00079    Paint();                        // calculate new coordinates of chars and paint it
00080    gSystem->AddTimer(this);        // start timer= start animation
00081 }
00082 
00083 Hello::~Hello()
00084 {
00085    // Clean up hello.
00086 
00087    fList->Delete();
00088    delete fList;
00089 }
00090 
00091 void Hello::ExecuteEvent(Int_t event, Int_t, Int_t)
00092 {
00093    // Actions when mouse clicked.
00094 
00095    if (event == kButton1Up) {
00096       TTimer::Remove();
00097    }
00098 }
00099 
00100 Float_t Hello::GetWidth()
00101 {
00102    // Return text width
00103 
00104    TChar *ch;
00105    TIter nextin(fList);
00106    Float_t width = 0;
00107 
00108    while ((ch = (TChar*)nextin()))
00109       width = width + ch->GetWidth();
00110 
00111    return width;
00112 }
00113 
00114 void Hello::Paint(Option_t *)
00115 {
00116    // Paint text
00117 
00118    static int sin_tbl[16] = {
00119         0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
00120    };
00121 
00122    TChar *ch;
00123    TIter nextin(fList);
00124    Float_t width = GetWidth();
00125 
00126    Coord_t xnext = (1-width)/2.;    // draw text in the center of the pad
00127    Coord_t y;
00128    Coord_t y0 = 0.5;
00129 
00130    fI = (fI+1) & 15;
00131    int i = 0;
00132 
00133    while ((ch = (TChar*)nextin())) {
00134       int i16 = (fI+i) & 15;
00135       y = y0 - (Coord_t)sin_tbl[i16]/1500.;   // sin-wave coordinates
00136       ch->SetY(y);
00137       ch->SetX(xnext);
00138       ch->SetTextColor(i16+200);              // let's use x3d colors
00139       i++;
00140       xnext = xnext + ch->GetWidth();         // x position of next character
00141       ch->Paint("");                          // paint character
00142    }
00143 }
00144 
00145 void Hello::Print(Option_t * opt) const 
00146 {
00147    fList->Print(opt);
00148 }
00149 
00150 void Hello::ls(Option_t *opt) const 
00151 {
00152    fList->ls(opt);
00153 }
00154 
00155 Bool_t Hello::Notify()
00156 {
00157    // Actions after timer time-out
00158 
00159    fPad->Modified();
00160    fPad->Update();
00161    TTimer::Reset();
00162    return kFALSE;
00163 }

Generated on Tue Jul 5 15:15:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1