TGToolBar.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGToolBar.cxx 35582 2010-09-22 13:38:27Z bellenot $
00002 // Author: Fons Rademakers   25/02/98
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 
00013     This source is based on Xclass95, a Win95-looking GUI toolkit.
00014     Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
00015 
00016     Xclass95 is free software; you can redistribute it and/or
00017     modify it under the terms of the GNU Library General Public
00018     License as published by the Free Software Foundation; either
00019     version 2 of the License, or (at your option) any later version.
00020 
00021 **************************************************************************/
00022 
00023 //////////////////////////////////////////////////////////////////////////
00024 //                                                                      //
00025 // TGToolBar                                                            //
00026 //                                                                      //
00027 // A toolbar is a composite frame that contains TGPictureButtons.       //
00028 // Often used in combination with a TGHorizontal3DLine.                 //
00029 //                                                                      //
00030 //////////////////////////////////////////////////////////////////////////
00031 
00032 #include "TGToolBar.h"
00033 #include "TList.h"
00034 #include "TGButton.h"
00035 #include "TGPicture.h"
00036 #include "TGToolTip.h"
00037 #include "TSystem.h"
00038 #include "TROOT.h"
00039 #include "Riostream.h"
00040 #include "TMap.h"
00041 
00042 
00043 ClassImp(TGToolBar)
00044 
00045 //______________________________________________________________________________
00046 TGToolBar::TGToolBar(const TGWindow *p, UInt_t w, UInt_t h,
00047                      UInt_t options, ULong_t back) :
00048                      TGCompositeFrame(p, w, h, options, back)
00049 
00050 {
00051    // Create toolbar widget.
00052 
00053    fPictures = new TList;
00054    fTrash    = new TList;
00055    fMapOfButtons = new TMap();  // map of button/id pairs
00056 
00057    SetWindowName();
00058 }
00059 
00060 //______________________________________________________________________________
00061 TGToolBar::~TGToolBar()
00062 {
00063    // Delete toolbar and its buttons and layout hints.
00064 
00065    if (!MustCleanup()) {
00066       if (fTrash) fTrash->Clear("nodelete");
00067    }
00068    delete fTrash;
00069    fTrash = 0;
00070 
00071    TIter next(fPictures);
00072    const TGPicture *p;
00073    while ((p = (const TGPicture *) next()))
00074       fClient->FreePicture(p);
00075 
00076    // pictures might already have been deleted above, so avoid access
00077    // to these objects
00078    fPictures->Clear("nodelete");
00079 
00080    delete fPictures;
00081    delete fMapOfButtons;
00082 }
00083 
00084 //______________________________________________________________________________
00085 TGButton *TGToolBar::AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing)
00086 {
00087    // Add button to toolbar. All buttons added via this method will be
00088    // deleted by the toolbar. On return the TGButton field of the
00089    // ToolBarData_t struct is filled in (if fPixmap was valid).
00090    // Window w is the window to which the button messages will be send.
00091 
00092    const TGPicture *pic = fClient->GetPicture(button->fPixmap);
00093    if (!pic) {
00094       Error("AddButton", "pixmap not found: %s", button->fPixmap);
00095       return 0;
00096    }
00097    fPictures->Add((TObject*)pic);
00098 
00099    TGPictureButton *pbut;
00100    TGLayoutHints   *layout;
00101 
00102    pbut = new TGPictureButton(this, pic, button->fId);
00103    pbut->SetToolTipText(button->fTipText);
00104 
00105    layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
00106    AddFrame(pbut, layout);
00107    pbut->AllowStayDown(button->fStayDown);
00108    pbut->Associate(w);
00109    button->fButton = pbut;
00110 
00111    fTrash->Add(pbut);
00112    fTrash->Add(layout);
00113 
00114    fMapOfButtons->Add(pbut, (TObject*)((Long_t)button->fId));
00115 
00116    Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
00117    Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
00118    Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
00119 
00120    return pbut;
00121 }
00122 
00123 //______________________________________________________________________________
00124 TGButton *TGToolBar::AddButton(const TGWindow *w, TGPictureButton *pbut, Int_t spacing)
00125 {
00126    // Add button to toolbar. All buttons added via this method will be deleted
00127    // by the toolbar, w is the window to which the button messages will be send.
00128 
00129    const TGPicture *pic = pbut->GetPicture();
00130    fPictures->Add((TObject*)pic);
00131 
00132    TGLayoutHints   *layout;
00133    layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
00134    AddFrame(pbut, layout);
00135    pbut->Associate(w);
00136 
00137    fTrash->Add(pbut);
00138    fTrash->Add(layout);
00139 
00140    fMapOfButtons->Add(pbut, (TObject*)((Long_t)pbut->WidgetId()));
00141 
00142    Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
00143    Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
00144    Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
00145 
00146    return pbut;
00147 }
00148 
00149 //______________________________________________________________________________
00150 TGButton *TGToolBar::GetButton(Int_t id) const
00151 {
00152    // Finds and returns a pointer to the button with the specified
00153    // identifier id. Returns null if the button was not found.
00154 
00155    TIter next(fMapOfButtons);
00156    register TGButton *item = 0;
00157 
00158    while ((item = (TGButton*)next())) {
00159       if ((Long_t)fMapOfButtons->GetValue(item) == id) break;   // found
00160    }
00161 
00162    return item;
00163 }
00164 
00165 //______________________________________________________________________________
00166 void TGToolBar::SetId(TGButton *button, Long_t id)
00167 {
00168    // changes id for button.
00169 
00170    TPair *a = (TPair*) fMapOfButtons->FindObject(button);
00171    if (a) {
00172       a->SetValue((TObject*)id);
00173    }
00174 }
00175 
00176 //______________________________________________________________________________
00177 Long_t TGToolBar::GetId(TGButton *button) const
00178 {
00179    // Finds and returns the id of the button.
00180    // Returns -1 if the button is not a member of this group.
00181 
00182    TPair *a = (TPair*) fMapOfButtons->FindObject(button);
00183    if (a)
00184       return Long_t(a->Value());
00185    else
00186       return Long_t(-1);
00187 }
00188 
00189 //______________________________________________________________________________
00190 void TGToolBar::ChangeIcon(ToolBarData_t *button, const char *new_icon)
00191 {
00192    // Change the icon of a toolbar button.
00193 
00194    const TGPicture *pic = fClient->GetPicture(new_icon);
00195    if (!pic) {
00196       Error("ChangeIcon", "pixmap not found: %s", new_icon);
00197       return;
00198    }
00199    fPictures->Add((TObject*)pic);
00200 
00201    ((TGPictureButton *)button->fButton)->SetPicture(pic);
00202 }
00203 
00204 //______________________________________________________________________________
00205 void TGToolBar::Cleanup()
00206 {
00207    // Cleanup and delete all objects contained in this composite frame.
00208    // This will delete all objects added via AddFrame().
00209    // CAUTION: all objects (frames and layout hints) must be unique, i.e.
00210    // cannot be shared.
00211 
00212    // avoid double deletion of objects in trash
00213    delete fTrash;
00214    fTrash = 0;
00215 
00216    TGCompositeFrame::Cleanup();
00217 }
00218 
00219 //______________________________________________________________________________
00220 void TGToolBar::ButtonPressed()
00221 {
00222    // This slot is activated when one of the buttons in the group emits the
00223    // Pressed() signal.
00224 
00225    TGButton *btn = (TGButton*)gTQSender;
00226 
00227    TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
00228    if (a) {
00229       Int_t id = (Int_t)Long_t(a->Value());
00230       Pressed(id);
00231    }
00232 }
00233 
00234 //______________________________________________________________________________
00235 void TGToolBar::ButtonReleased()
00236 {
00237    // This slot is activated when one of the buttons in the group emits the
00238    // Released() signal.
00239 
00240    TGButton *btn = (TGButton*)gTQSender;
00241 
00242    TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
00243    if (a) {
00244       Int_t id = (Int_t)Long_t(a->Value());
00245       Released(id);
00246    }
00247 }
00248 
00249 //______________________________________________________________________________
00250 void TGToolBar::ButtonClicked()
00251 {
00252    // This slot is activated when one of the buttons in the group emits the
00253    // Clicked() signal.
00254 
00255    TGButton *btn = (TGButton*)gTQSender;
00256 
00257    TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
00258    if (a) {
00259       Int_t id = (Int_t)Long_t(a->Value());
00260       Clicked(id);
00261    }
00262 }
00263 
00264 //______________________________________________________________________________
00265 void TGToolBar::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00266 {
00267    // Save an horizontal slider as a C++ statement(s) on output stream out.
00268 
00269    if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00270 
00271    out << endl;
00272    out << "   // tool bar" << endl;
00273 
00274    out << "   TGToolBar *";
00275    out << GetName() << " = new TGToolBar(" << fParent->GetName()
00276        << "," << GetWidth() << "," << GetHeight();
00277 
00278    if (fBackground == GetDefaultFrameBackground()) {
00279       if (!GetOptions()) {
00280          out <<");" << endl;
00281       } else {
00282          out << "," << GetOptionString() <<");" << endl;
00283       }
00284    } else {
00285       out << "," << GetOptionString() << ",ucolor);" << endl;
00286    }
00287    if (option && strstr(option, "keep_names"))
00288       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00289 
00290    char quote = '"';
00291 
00292    int i = 0;
00293    const char *picname;
00294 
00295    TGFrameElement *f;
00296    TIter next(GetList());
00297 
00298    while ((f = (TGFrameElement *) next())) {
00299       if (f->fFrame->InheritsFrom(TGPictureButton::Class())) {
00300          if (!gROOT->ClassSaved(TGPictureButton::Class())) {
00301             //  declare a structure used for pictute buttons
00302             out << endl << "   ToolBarData_t t;" << endl;
00303          }
00304 
00305          TGPictureButton *pb = (TGPictureButton *)f->fFrame;
00306          picname = pb->GetPicture()->GetName();
00307 
00308          out << "   t.fPixmap = " << quote
00309              << gSystem->ExpandPathName(gSystem->UnixPathName(picname))
00310              << quote << ";" << endl;
00311          out << "   t.fTipText = " << quote
00312              << pb->GetToolTip()->GetText()->GetString() << quote << ";" << endl;
00313          if (pb->GetState() == kButtonDown) {
00314             out << "   t.fStayDown = kTRUE;" << endl;
00315          } else {
00316             out << "   t.fStayDown = kFALSE;" << endl;
00317          }
00318          out << "   t.fId = " << i+1 << ";" << endl;
00319          out << "   t.fButton = 0;" << endl;
00320          out << "   " << GetName() << "->AddButton(" << GetParent()->GetName()
00321              << ",&t," << f->fLayout->GetPadLeft() << ");" << endl;
00322          if (pb->GetState() == kButtonDown) {
00323             out << "   TGButton *" << pb->GetName() <<  " = t.fButton;" << endl;
00324             out << "   " << pb->GetName() << "->SetState(kButtonDown);"  << endl;
00325          }
00326          if (pb->GetState() == kButtonDisabled) {
00327             out << "   TGButton *" << pb->GetName() <<  " = t.fButton;" << endl;
00328             out << "   " << pb->GetName() << "->SetState(kButtonDisabled);" << endl;
00329          }
00330          if (pb->GetState() == kButtonEngaged) {
00331             out << "   TGButton *" << pb->GetName() <<  " = t.fButton;" << endl;
00332             out << "   " << pb->GetName() << "->SetState(kButtonEngaged);"  << endl;
00333          }
00334          i++;
00335       } else {
00336          f->fFrame->SavePrimitive(out, option);
00337          out << "   " << GetName()<<"->AddFrame(" << f->fFrame->GetName();
00338          f->fLayout->SavePrimitive(out, option);
00339          out << ");"<< endl;
00340       }
00341    }
00342 }

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