// @(#)root/gpad:$Id$
// Author: Rene Brun   01/07/96

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TButton.h"
#include "TCanvas.h"
#include "TLatex.h"

#include <string.h>

ClassImp(TButton)


//______________________________________________________________________________
//  A TButton object is a user interface object.
//  A TButton has a name and an associated action.
//  When the button is clicked with the left mouse button, the corresponding
//  action is executed.
//  A TButton can be created by direct invocation of the constructors
//  or via the graphics editor.
//  The action can be set via TButton::SetMethod.
//  The action can be any command. Examples of actions:
//     "34+78" When the button is clicked, the result of addition is printed.
//     ".x macro.C" . Clicking the button executes the macro macro.C
//  The action can be modified at any time via TButton::SetMethod.
//
//  To modify the layout/size/contents of one or several buttons
//  in a canvas, you must set the canvas editable via TCanvas::SetEditable.
//  By default a TCanvas is editable.
//  By default a TDialogCanvas is not editable.
//  TButtons are in general placed in a TDialogCanvas.
//
//  A TButton being a TPad, one can draw graphics primitives in it
//  when the TCanvas/TDialogCanvas is editable.
//
//       Example of a macro creating a dialogcanvas with buttons
//void but() {
////   example of a dialogcanvas with a few buttons
//
//   TDialogCanvas *dialog = new TDialogCanvas("dialog","",200,300);
//
//// Create first button. Clicking on this button will execute 34+56
//   TButton *but1 = new TButton("button1","34+56",.05,.8,.45,.88);
//   but1->Draw();
//
//// Create second button. Clicking on this button will create a new canvas
//   TButton *but2 = new TButton("canvas","c2 = new TCanvas(\"c2\")",.55,.8,.95,.88);
//   but2->Draw();
//
//// Create third button. Clicking on this button will invoke the browser
//   but3 = new TButton("Browser","br = new TBrowser(\"br\")",0.25,0.54,0.75,0.64);
//   but3->SetFillColor(42);
//   but3->Draw();
//
//// Create last button with no name. Instead a graph is draw inside the button
//// Clicking on this button will invoke the macro $ROOTSYS/tutorials/graphs/graph.C
//   button = new TButton("",".x tutorials/graphs/graph.C",0.15,0.15,0.85,0.38);
//   button->SetFillColor(42);
//   button->Draw();
//   button->SetEditable(kTRUE);
//   button->cd();
//
//   Double_t x[8] = {0.08,0.21,0.34,0.48,0.61,0.7,0.81,0.92};
//   Double_t y[8] = {0.2,0.65,0.4,0.34,0.24,0.43,0.75,0.52};
//   TGraph *graph = new TGraph(8,x,y);
//   graph->SetMarkerColor(4);
//   graph->SetMarkerStyle(21);
//   graph->Draw("lp");
//
//   dialog->cd();
//}
//
//   Executing the macro above produces the following dialogcanvas
//Begin_Html
/*
<img src="gif/dialogbuttons.gif">
*/
//End_Html


//______________________________________________________________________________
TButton::TButton(): TPad()
{
   // Button default constructor.

   fFraming = 0;
   fMethod  = "";
   fLogx    = kFALSE;
   fLogy    = kFALSE;
   SetEditable(kFALSE);
   fFocused = 0;
}


//______________________________________________________________________________
TButton::TButton(const char *title, const char *method, Double_t x1, Double_t y1,Double_t x2, Double_t  y2)
           :TPad("button",title,x1,y1,x2,y2,18,2,1), TAttText(22,0,1,61,0.65)
{
   // Button normal constructor.
   //
   //   Note that the button coordinates x1,y1,x2,y2 are always in the range [0,1]

   fFraming=0;
   SetBit(kCanDelete);
   fModified = kTRUE;
   fMethod = method;
   if (strlen(title)) {
      TLatex *text = new TLatex(0.5*(fX1+fX2),0.5*(fY1+fY2),title);
      fPrimitives->Add(text);
   }
   fLogx    = kFALSE;
   fLogy    = kFALSE;
   SetEditable(kFALSE);
   fFocused = 0;
}


//______________________________________________________________________________
TButton::~TButton()
{
   // Button default destructor.

   if (fPrimitives) fPrimitives->Delete();
}


//______________________________________________________________________________
void TButton::Draw(Option_t *option)
{
   // Draw this button with its current attributes.

   if (fCanvas) AppendPad(option);
}


//______________________________________________________________________________
void TButton::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
   // Execute action corresponding to one event.
   //
   //  This member function is called when a Button object is clicked.

   //check case where pressing a button deletes itself
   if (!TestBit(kNotDeleted)) return;

   if (IsEditable()) {
      TPad::ExecuteEvent(event,px,py);
      return;
   }

   TPad *cdpad = (TPad*)gROOT->GetSelectedPad();
   HideToolTip(event);

   switch (event) {

   case kMouseEnter:
      TPad::ExecuteEvent(event,px,py);
      break;

   case kButton1Down:
      SetBorderMode(-1);
      fFocused=1;
      Modified();
      Update();
      break;

   case kMouseMotion:

      break;

   case kButton1Motion:
      if (px<XtoAbsPixel(1) && px>XtoAbsPixel(0) &&
          py<YtoAbsPixel(0) && py>YtoAbsPixel(1)) {
         if (!fFocused) {
            SetBorderMode(-1);
            fFocused=1;
            Modified();
            GetCanvas()->Modified();
            Update();
         }
      } else if (fFocused) {
         SetBorderMode(1);
         fFocused=0;
         Modified();
         GetCanvas()->Modified();
         Update();
      }
      break;

   case kButton1Up:
      SetCursor(kWatch);
      if (fFocused) {
         if (cdpad) cdpad->cd();
         gROOT->ProcessLine(GetMethod());
      }
      //check case where pressing a button deletes itself
      if (!TestBit(kNotDeleted)) return;
      SetBorderMode(1);
      Modified();
      Update();
      SetCursor(kCross);
      break;
   }
}


//______________________________________________________________________________
void TButton::Paint(Option_t *option)
{
   // Paint this button with its current attributes.

   TPad::Paint(option);  //only called for Postscript print
}


//______________________________________________________________________________
void TButton::PaintModified()
{
   // Paint is modified.

   if (!fCanvas) return;
   if (!fPrimitives) fPrimitives = new TList();
   TObject *obj = GetListOfPrimitives()->First();
   if (obj && obj->InheritsFrom(TText::Class())) {
      TLatex *text = (TLatex*)obj;
      text->SetTitle(GetTitle());
      text->SetTextSize(GetTextSize());
      text->SetTextFont(GetTextFont());
      text->SetTextAlign(GetTextAlign());
      text->SetTextColor(GetTextColor());
      text->SetTextAngle(GetTextAngle());
   }
   SetLogx(0);
   SetLogy(0);
   TPad::PaintModified();
}


//______________________________________________________________________________
void TButton::Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
{
   // Set world coordinate system for the pad.

   TPad::Range(x1,y1,x2,y2);
}


//______________________________________________________________________________
void TButton::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
{
   // Save primitive as a C++ statement(s) on output stream out

   TPad *padsav = (TPad*)gPad;
   char quote = '"';
   if (gROOT->ClassSaved(TButton::Class())) {
      out<<"   ";
   } else {
      out<<"   TButton *";
   }
   char *cm = (char*)GetMethod();
   Int_t nch = strlen(cm);
   char *cmethod = new char[nch+10];
   Int_t i = 0;
   while(*cm) {
      if (*cm == '"') {
         cmethod[i] = '\\';
         i++;
      }
      cmethod[i] = *cm;
      i++;
      cm++;
   }
   cmethod[i] = 0;
   out<<"button = new TButton("<<quote<<GetTitle()
      <<quote<<","<<quote<<cmethod<<quote
      <<","<<fXlowNDC
      <<","<<fYlowNDC
      <<","<<fXlowNDC+fWNDC
      <<","<<fYlowNDC+fHNDC
      <<");"<<std::endl;
   delete [] cmethod;

   SaveFillAttributes(out,"button",0,1001);
   SaveLineAttributes(out,"button",1,1,1);
   SaveTextAttributes(out,"button",22,0,1,61,.65);

   if (GetBorderSize() != 2) {
      out<<"   button->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
   }
   if (GetBorderMode() != 1) {
      out<<"   button->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
   }

   if (GetFraming()) out<<"button->SetFraming();"<<std::endl;
   if (IsEditable()) out<<"button->SetEditable(kTRUE);"<<std::endl;

   out<<"   button->Draw();"<<std::endl;

   TIter next(GetListOfPrimitives());
   TObject *obj = next();  //do not save first primitive

   Int_t nprim = 0;
   while ((obj = next())) {
      if (!nprim) out<<"   button->cd();"<<std::endl;
      nprim++;
      obj->SavePrimitive(out, (Option_t *)next.GetOption());
   }

   if (nprim) out<<"   "<<padsav->GetName()<<"->cd();"<<std::endl;
   padsav->cd();
}


//______________________________________________________________________________
void TButton::SetFraming(Bool_t f)
{
   // if framing is set, button will be highlighted

   fFraming=f;
   if (f) SetBit(kFraming);
   else   ResetBit(kFraming);
}
 TButton.cxx:1
 TButton.cxx:2
 TButton.cxx:3
 TButton.cxx:4
 TButton.cxx:5
 TButton.cxx:6
 TButton.cxx:7
 TButton.cxx:8
 TButton.cxx:9
 TButton.cxx:10
 TButton.cxx:11
 TButton.cxx:12
 TButton.cxx:13
 TButton.cxx:14
 TButton.cxx:15
 TButton.cxx:16
 TButton.cxx:17
 TButton.cxx:18
 TButton.cxx:19
 TButton.cxx:20
 TButton.cxx:21
 TButton.cxx:22
 TButton.cxx:23
 TButton.cxx:24
 TButton.cxx:25
 TButton.cxx:26
 TButton.cxx:27
 TButton.cxx:28
 TButton.cxx:29
 TButton.cxx:30
 TButton.cxx:31
 TButton.cxx:32
 TButton.cxx:33
 TButton.cxx:34
 TButton.cxx:35
 TButton.cxx:36
 TButton.cxx:37
 TButton.cxx:38
 TButton.cxx:39
 TButton.cxx:40
 TButton.cxx:41
 TButton.cxx:42
 TButton.cxx:43
 TButton.cxx:44
 TButton.cxx:45
 TButton.cxx:46
 TButton.cxx:47
 TButton.cxx:48
 TButton.cxx:49
 TButton.cxx:50
 TButton.cxx:51
 TButton.cxx:52
 TButton.cxx:53
 TButton.cxx:54
 TButton.cxx:55
 TButton.cxx:56
 TButton.cxx:57
 TButton.cxx:58
 TButton.cxx:59
 TButton.cxx:60
 TButton.cxx:61
 TButton.cxx:62
 TButton.cxx:63
 TButton.cxx:64
 TButton.cxx:65
 TButton.cxx:66
 TButton.cxx:67
 TButton.cxx:68
 TButton.cxx:69
 TButton.cxx:70
 TButton.cxx:71
 TButton.cxx:72
 TButton.cxx:73
 TButton.cxx:74
 TButton.cxx:75
 TButton.cxx:76
 TButton.cxx:77
 TButton.cxx:78
 TButton.cxx:79
 TButton.cxx:80
 TButton.cxx:81
 TButton.cxx:82
 TButton.cxx:83
 TButton.cxx:84
 TButton.cxx:85
 TButton.cxx:86
 TButton.cxx:87
 TButton.cxx:88
 TButton.cxx:89
 TButton.cxx:90
 TButton.cxx:91
 TButton.cxx:92
 TButton.cxx:93
 TButton.cxx:94
 TButton.cxx:95
 TButton.cxx:96
 TButton.cxx:97
 TButton.cxx:98
 TButton.cxx:99
 TButton.cxx:100
 TButton.cxx:101
 TButton.cxx:102
 TButton.cxx:103
 TButton.cxx:104
 TButton.cxx:105
 TButton.cxx:106
 TButton.cxx:107
 TButton.cxx:108
 TButton.cxx:109
 TButton.cxx:110
 TButton.cxx:111
 TButton.cxx:112
 TButton.cxx:113
 TButton.cxx:114
 TButton.cxx:115
 TButton.cxx:116
 TButton.cxx:117
 TButton.cxx:118
 TButton.cxx:119
 TButton.cxx:120
 TButton.cxx:121
 TButton.cxx:122
 TButton.cxx:123
 TButton.cxx:124
 TButton.cxx:125
 TButton.cxx:126
 TButton.cxx:127
 TButton.cxx:128
 TButton.cxx:129
 TButton.cxx:130
 TButton.cxx:131
 TButton.cxx:132
 TButton.cxx:133
 TButton.cxx:134
 TButton.cxx:135
 TButton.cxx:136
 TButton.cxx:137
 TButton.cxx:138
 TButton.cxx:139
 TButton.cxx:140
 TButton.cxx:141
 TButton.cxx:142
 TButton.cxx:143
 TButton.cxx:144
 TButton.cxx:145
 TButton.cxx:146
 TButton.cxx:147
 TButton.cxx:148
 TButton.cxx:149
 TButton.cxx:150
 TButton.cxx:151
 TButton.cxx:152
 TButton.cxx:153
 TButton.cxx:154
 TButton.cxx:155
 TButton.cxx:156
 TButton.cxx:157
 TButton.cxx:158
 TButton.cxx:159
 TButton.cxx:160
 TButton.cxx:161
 TButton.cxx:162
 TButton.cxx:163
 TButton.cxx:164
 TButton.cxx:165
 TButton.cxx:166
 TButton.cxx:167
 TButton.cxx:168
 TButton.cxx:169
 TButton.cxx:170
 TButton.cxx:171
 TButton.cxx:172
 TButton.cxx:173
 TButton.cxx:174
 TButton.cxx:175
 TButton.cxx:176
 TButton.cxx:177
 TButton.cxx:178
 TButton.cxx:179
 TButton.cxx:180
 TButton.cxx:181
 TButton.cxx:182
 TButton.cxx:183
 TButton.cxx:184
 TButton.cxx:185
 TButton.cxx:186
 TButton.cxx:187
 TButton.cxx:188
 TButton.cxx:189
 TButton.cxx:190
 TButton.cxx:191
 TButton.cxx:192
 TButton.cxx:193
 TButton.cxx:194
 TButton.cxx:195
 TButton.cxx:196
 TButton.cxx:197
 TButton.cxx:198
 TButton.cxx:199
 TButton.cxx:200
 TButton.cxx:201
 TButton.cxx:202
 TButton.cxx:203
 TButton.cxx:204
 TButton.cxx:205
 TButton.cxx:206
 TButton.cxx:207
 TButton.cxx:208
 TButton.cxx:209
 TButton.cxx:210
 TButton.cxx:211
 TButton.cxx:212
 TButton.cxx:213
 TButton.cxx:214
 TButton.cxx:215
 TButton.cxx:216
 TButton.cxx:217
 TButton.cxx:218
 TButton.cxx:219
 TButton.cxx:220
 TButton.cxx:221
 TButton.cxx:222
 TButton.cxx:223
 TButton.cxx:224
 TButton.cxx:225
 TButton.cxx:226
 TButton.cxx:227
 TButton.cxx:228
 TButton.cxx:229
 TButton.cxx:230
 TButton.cxx:231
 TButton.cxx:232
 TButton.cxx:233
 TButton.cxx:234
 TButton.cxx:235
 TButton.cxx:236
 TButton.cxx:237
 TButton.cxx:238
 TButton.cxx:239
 TButton.cxx:240
 TButton.cxx:241
 TButton.cxx:242
 TButton.cxx:243
 TButton.cxx:244
 TButton.cxx:245
 TButton.cxx:246
 TButton.cxx:247
 TButton.cxx:248
 TButton.cxx:249
 TButton.cxx:250
 TButton.cxx:251
 TButton.cxx:252
 TButton.cxx:253
 TButton.cxx:254
 TButton.cxx:255
 TButton.cxx:256
 TButton.cxx:257
 TButton.cxx:258
 TButton.cxx:259
 TButton.cxx:260
 TButton.cxx:261
 TButton.cxx:262
 TButton.cxx:263
 TButton.cxx:264
 TButton.cxx:265
 TButton.cxx:266
 TButton.cxx:267
 TButton.cxx:268
 TButton.cxx:269
 TButton.cxx:270
 TButton.cxx:271
 TButton.cxx:272
 TButton.cxx:273
 TButton.cxx:274
 TButton.cxx:275
 TButton.cxx:276
 TButton.cxx:277
 TButton.cxx:278
 TButton.cxx:279
 TButton.cxx:280
 TButton.cxx:281
 TButton.cxx:282
 TButton.cxx:283
 TButton.cxx:284
 TButton.cxx:285
 TButton.cxx:286
 TButton.cxx:287
 TButton.cxx:288
 TButton.cxx:289
 TButton.cxx:290
 TButton.cxx:291
 TButton.cxx:292
 TButton.cxx:293
 TButton.cxx:294
 TButton.cxx:295
 TButton.cxx:296
 TButton.cxx:297
 TButton.cxx:298
 TButton.cxx:299
 TButton.cxx:300
 TButton.cxx:301
 TButton.cxx:302
 TButton.cxx:303
 TButton.cxx:304
 TButton.cxx:305
 TButton.cxx:306
 TButton.cxx:307
 TButton.cxx:308
 TButton.cxx:309
 TButton.cxx:310
 TButton.cxx:311
 TButton.cxx:312
 TButton.cxx:313
 TButton.cxx:314
 TButton.cxx:315
 TButton.cxx:316
 TButton.cxx:317
 TButton.cxx:318
 TButton.cxx:319
 TButton.cxx:320
 TButton.cxx:321
 TButton.cxx:322
 TButton.cxx:323
 TButton.cxx:324
 TButton.cxx:325
 TButton.cxx:326
 TButton.cxx:327
 TButton.cxx:328
 TButton.cxx:329
 TButton.cxx:330
 TButton.cxx:331