TButton.cxx

Go to the documentation of this file.
00001 // @(#)root/gpad:$Id: TButton.cxx 35153 2010-09-06 09:33:36Z couet $
00002 // Author: Rene Brun   01/07/96
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TButton.h"
00015 #include "TCanvas.h"
00016 #include "TLatex.h"
00017 
00018 #include <string.h>
00019 
00020 ClassImp(TButton)
00021 
00022 
00023 //______________________________________________________________________________
00024 //  A TButton object is a user interface object.
00025 //  A TButton has a name and an associated action.
00026 //  When the button is clicked with the left mouse button, the cooresponding
00027 //  action is executed.
00028 //  A Tbutton can be created by direct invokation of the constructors
00029 //  or via the graphics editor.
00030 //  The Action can be set via TButton::SetMethod.
00031 //  The action can be any command. Examples of actions:
00032 //     "34+78" When the button is clicked, the result of addition is printed.
00033 //     ".x macro.C" . Clicking the button executes the macro macro.C
00034 //  The action can be modified at any time via TButton::SetMethod.
00035 //
00036 //  To modify the layout/size/contents of one or several buttons
00037 //  in a canvas, you must set the canvas editable via TCanvas::SetEditable.
00038 //  By default a TCanvas is editable.
00039 //  By default a TDialogCanvas is not editable.
00040 //  TButtons are in general placed in a TDialogCanvas.
00041 //
00042 //  A TButton being a TPad, one can draw graphics primitives in it
00043 //  when the TCanvas/TDialogCanvas is editable.
00044 //
00045 //       Example of a macro creating a dialogcanvas with buttons
00046 //void but() {
00047 ////   example of a dialogcanvas with a few buttons
00048 //
00049 //   TDialogCanvas *dialog = new TDialogCanvas("dialog","",200,300);
00050 //
00051 //// Create first button. Clicking on this button will execute 34+56
00052 //   TButton *but1 = new TButton("button1","34+56",.05,.8,.45,.88);
00053 //   but1->Draw();
00054 //
00055 //// Create second button. Clicking on this button will create a new canvas
00056 //   TButton *but2 = new TButton("canvas","c2 = new TCanvas(\"c2\")",.55,.8,.95,.88);
00057 //   but2->Draw();
00058 //
00059 //// Create third button. Clicking on this button will invoke the browser
00060 //   but3 = new TButton("Browser","br = new TBrowser(\"br\")",0.25,0.54,0.75,0.64);
00061 //   but3->SetFillColor(42);
00062 //   but3->Draw();
00063 //
00064 //// Create last button with no name. Instead a graph is draw inside the button
00065 //// Clicking on this button will invoke the macro $ROOTSYS/tutorials/graphs/graph.C
00066 //   button = new TButton("",".x tutorials/graphs/graph.C",0.15,0.15,0.85,0.38);
00067 //   button->SetFillColor(42);
00068 //   button->Draw();
00069 //   button->SetEditable(kTRUE);
00070 //   button->cd();
00071 //
00072 //   Double_t x[8] = {0.08,0.21,0.34,0.48,0.61,0.7,0.81,0.92};
00073 //   Double_t y[8] = {0.2,0.65,0.4,0.34,0.24,0.43,0.75,0.52};
00074 //   TGraph *graph = new TGraph(8,x,y);
00075 //   graph->SetMarkerColor(4);
00076 //   graph->SetMarkerStyle(21);
00077 //   graph->Draw("lp");
00078 //
00079 //   dialog->cd();
00080 //}
00081 //
00082 //   Executing the macro above produces the following dialogcanvas
00083 //Begin_Html
00084 /*
00085 <img src="gif/dialogbuttons.gif">
00086 */
00087 //End_Html
00088 
00089 
00090 //______________________________________________________________________________
00091 TButton::TButton(): TPad()
00092 {
00093    // Button default constructor.
00094 
00095    fFraming = 0;
00096    fMethod  = "";
00097    fLogx    = kFALSE;
00098    fLogy    = kFALSE;
00099    SetEditable(kFALSE);
00100    fFocused = 0;
00101 }
00102 
00103 
00104 //______________________________________________________________________________
00105 TButton::TButton(const char *title, const char *method, Double_t x1, Double_t y1,Double_t x2, Double_t  y2)
00106            :TPad("button",title,x1,y1,x2,y2,18,2,1), TAttText(22,0,1,61,0.65)
00107 {
00108    // Button normal constructor.
00109    //
00110    //   Note that the button coordinates x1,y1,x2,y2 are always in the range [0,1]
00111 
00112    fFraming=0;
00113    SetBit(kCanDelete);
00114    fModified = kTRUE;
00115    fMethod = method;
00116    if (strlen(title)) {
00117       TLatex *text = new TLatex(0.5*(fX1+fX2),0.5*(fY1+fY2),title);
00118       fPrimitives->Add(text);
00119    }
00120    fLogx    = kFALSE;
00121    fLogy    = kFALSE;
00122    SetEditable(kFALSE);
00123    fFocused = 0;
00124 }
00125 
00126 
00127 //______________________________________________________________________________
00128 TButton::~TButton()
00129 {
00130    // Button default destructor.
00131 
00132    if (fPrimitives) fPrimitives->Delete();
00133 }
00134 
00135 
00136 //______________________________________________________________________________
00137 void TButton::Draw(Option_t *option)
00138 {
00139    // Draw this button with its current attributes.
00140 
00141    if (fCanvas) AppendPad(option);
00142 }
00143 
00144 
00145 //______________________________________________________________________________
00146 void TButton::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00147 {
00148    // Execute action corresponding to one event.
00149    //
00150    //  This member function is called when a Button object is clicked.
00151 
00152    //check case where pressing a button deletes itself
00153    if (!TestBit(kNotDeleted)) return;
00154 
00155    if (IsEditable()) {
00156       TPad::ExecuteEvent(event,px,py);
00157       return;
00158    }
00159 
00160    TPad *cdpad = (TPad*)gROOT->GetSelectedPad();
00161    HideToolTip(event);
00162 
00163    switch (event) {
00164 
00165    case kMouseEnter:
00166       TPad::ExecuteEvent(event,px,py);
00167       break;
00168 
00169    case kButton1Down:
00170       SetBorderMode(-1);
00171       fFocused=1;
00172       Modified();
00173       Update();
00174       break;
00175 
00176    case kMouseMotion:
00177 
00178       break;
00179 
00180    case kButton1Motion:
00181       if (px<XtoAbsPixel(1) && px>XtoAbsPixel(0) &&
00182           py<YtoAbsPixel(0) && py>YtoAbsPixel(1)) {
00183          if (!fFocused) {
00184             SetBorderMode(-1);
00185             fFocused=1;
00186             Modified();
00187             GetCanvas()->Modified();
00188             Update();
00189          }
00190       } else if (fFocused) {
00191          SetBorderMode(1);
00192          fFocused=0;
00193          Modified();
00194          GetCanvas()->Modified();
00195          Update();
00196       }
00197       break;
00198 
00199    case kButton1Up:
00200       SetCursor(kWatch);
00201       if (fFocused) {
00202          if (cdpad) cdpad->cd();
00203          gROOT->ProcessLine(GetMethod());
00204       }
00205       //check case where pressing a button deletes itself
00206       if (!TestBit(kNotDeleted)) return;
00207       SetBorderMode(1);
00208       Modified();
00209       Update();
00210       SetCursor(kCross);
00211       break;
00212    }
00213 }
00214 
00215 
00216 //______________________________________________________________________________
00217 void TButton::Paint(Option_t *option)
00218 {
00219    // Paint this button with its current attributes.
00220 
00221    TPad::Paint(option);  //only called for Postscript print
00222 }
00223 
00224 
00225 //______________________________________________________________________________
00226 void TButton::PaintModified()
00227 {
00228    // Paint is modified.
00229 
00230    if (!fCanvas) return;
00231    if (!fPrimitives) fPrimitives = new TList();
00232    TObject *obj = GetListOfPrimitives()->First();
00233    if (obj && obj->InheritsFrom(TText::Class())) {
00234       TLatex *text = (TLatex*)obj;
00235       text->SetTitle(GetTitle());
00236       text->SetTextSize(GetTextSize());
00237       text->SetTextFont(GetTextFont());
00238       text->SetTextAlign(GetTextAlign());
00239       text->SetTextColor(GetTextColor());
00240       text->SetTextAngle(GetTextAngle());
00241    }
00242    SetLogx(0);
00243    SetLogy(0);
00244    TPad::PaintModified();
00245 }
00246 
00247 
00248 //______________________________________________________________________________
00249 void TButton::Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
00250 {
00251    // Set world coordinate system for the pad.
00252 
00253    TPad::Range(x1,y1,x2,y2);
00254 }
00255 
00256 
00257 //______________________________________________________________________________
00258 void TButton::SavePrimitive(ostream &out, Option_t * /*= ""*/)
00259 {
00260    // Save primitive as a C++ statement(s) on output stream out
00261 
00262    TPad *padsav = (TPad*)gPad;
00263    char quote = '"';
00264    if (gROOT->ClassSaved(TButton::Class())) {
00265       out<<"   ";
00266    } else {
00267       out<<"   TButton *";
00268    }
00269    char *cm = (char*)GetMethod();
00270    Int_t nch = strlen(cm);
00271    char *cmethod = new char[nch+10];
00272    Int_t i = 0;
00273    while(*cm) {
00274       if (*cm == '"') {
00275          cmethod[i] = '\\';
00276          i++;
00277       }
00278       cmethod[i] = *cm;
00279       i++;
00280       cm++;
00281    }
00282    cmethod[i] = 0;
00283    out<<"button = new TButton("<<quote<<GetTitle()
00284       <<quote<<","<<quote<<cmethod<<quote
00285       <<","<<fXlowNDC
00286       <<","<<fYlowNDC
00287       <<","<<fXlowNDC+fWNDC
00288       <<","<<fYlowNDC+fHNDC
00289       <<");"<<endl;
00290    delete [] cmethod;
00291 
00292    SaveFillAttributes(out,"button",0,1001);
00293    SaveLineAttributes(out,"button",1,1,1);
00294    SaveTextAttributes(out,"button",22,0,1,61,.65);
00295 
00296    if (GetBorderSize() != 2) {
00297       out<<"   button->SetBorderSize("<<GetBorderSize()<<");"<<endl;
00298    }
00299    if (GetBorderMode() != 1) {
00300       out<<"   button->SetBorderMode("<<GetBorderMode()<<");"<<endl;
00301    }
00302 
00303    if (GetFraming()) out<<"button->SetFraming();"<<endl;
00304    if (IsEditable()) out<<"button->SetEditable(kTRUE);"<<endl;
00305 
00306    out<<"   button->Draw();"<<endl;
00307 
00308    TIter next(GetListOfPrimitives());
00309    TObject *obj = next();  //do not save first primitive
00310 
00311    Int_t nprim = 0;
00312    while ((obj = next())) {
00313       if (!nprim) out<<"   button->cd();"<<endl;
00314       nprim++;
00315       obj->SavePrimitive(out, (Option_t *)next.GetOption());
00316    }
00317 
00318    if (nprim) out<<"   "<<padsav->GetName()<<"->cd();"<<endl;
00319    padsav->cd();
00320 }
00321 
00322 
00323 //______________________________________________________________________________
00324 void TButton::SetFraming(Bool_t f)
00325 {
00326    // if framing is set, button will be highlighted
00327 
00328    fFraming=f;
00329    if (f) SetBit(kFraming);
00330    else   ResetBit(kFraming);
00331 }

Generated on Tue Jul 5 14:14:06 2011 for ROOT_528-00b_version by  doxygen 1.5.1