#include "TGToolBar.h"
#include "TList.h"
#include "TGButton.h"
#include "TGPicture.h"
#include "TGToolTip.h"
#include "TSystem.h"
#include "TROOT.h"
#include "Riostream.h"
#include "TMap.h"
ClassImp(TGToolBar)
TGToolBar::TGToolBar(const TGWindow *p, UInt_t w, UInt_t h,
UInt_t options, ULong_t back) :
TGCompositeFrame(p, w, h, options, back)
{
fPictures = new TList;
fTrash = new TList;
fMapOfButtons = new TMap();
SetWindowName();
}
TGToolBar::~TGToolBar()
{
if (!MustCleanup()) {
if (fTrash) fTrash->Clear("nodelete");
}
delete fTrash;
fTrash = 0;
TIter next(fPictures);
const TGPicture *p;
while ((p = (const TGPicture *) next()))
fClient->FreePicture(p);
fPictures->Clear("nodelete");
delete fPictures;
delete fMapOfButtons;
}
TGButton *TGToolBar::AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing)
{
const TGPicture *pic = fClient->GetPicture(button->fPixmap);
if (!pic) {
Error("AddButton", "pixmap not found: %s", button->fPixmap);
return 0;
}
fPictures->Add((TObject*)pic);
TGPictureButton *pbut;
TGLayoutHints *layout;
pbut = new TGPictureButton(this, pic, button->fId);
pbut->SetStyle(gClient->GetStyle());
pbut->SetToolTipText(button->fTipText);
layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
AddFrame(pbut, layout);
pbut->AllowStayDown(button->fStayDown);
pbut->Associate(w);
button->fButton = pbut;
fTrash->Add(pbut);
fTrash->Add(layout);
fMapOfButtons->Add(pbut, (TObject*)((Long_t)button->fId));
Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
return pbut;
}
TGButton *TGToolBar::AddButton(const TGWindow *w, TGPictureButton *pbut, Int_t spacing)
{
const TGPicture *pic = pbut->GetPicture();
fPictures->Add((TObject*)pic);
TGLayoutHints *layout;
layout = new TGLayoutHints(kLHintsTop | kLHintsLeft, spacing, 0, 2, 2);
pbut->SetStyle(gClient->GetStyle());
AddFrame(pbut, layout);
pbut->Associate(w);
fTrash->Add(pbut);
fTrash->Add(layout);
fMapOfButtons->Add(pbut, (TObject*)((Long_t)pbut->WidgetId()));
Connect(pbut, "Pressed()" , "TGToolBar", this, "ButtonPressed()");
Connect(pbut, "Released()", "TGToolBar", this, "ButtonReleased()");
Connect(pbut, "Clicked()" , "TGToolBar", this, "ButtonClicked()");
return pbut;
}
TGButton *TGToolBar::GetButton(Int_t id) const
{
TIter next(fMapOfButtons);
TGButton *item = 0;
while ((item = (TGButton*)next())) {
if ((Long_t)fMapOfButtons->GetValue(item) == id) break;
}
return item;
}
void TGToolBar::SetId(TGButton *button, Long_t id)
{
TPair *a = (TPair*) fMapOfButtons->FindObject(button);
if (a) {
a->SetValue((TObject*)id);
}
}
Long_t TGToolBar::GetId(TGButton *button) const
{
TPair *a = (TPair*) fMapOfButtons->FindObject(button);
if (a)
return Long_t(a->Value());
else
return Long_t(-1);
}
void TGToolBar::ChangeIcon(ToolBarData_t *button, const char *new_icon)
{
const TGPicture *pic = fClient->GetPicture(new_icon);
if (!pic) {
Error("ChangeIcon", "pixmap not found: %s", new_icon);
return;
}
fPictures->Add((TObject*)pic);
((TGPictureButton *)button->fButton)->SetPicture(pic);
}
void TGToolBar::Cleanup()
{
delete fTrash;
fTrash = 0;
TGCompositeFrame::Cleanup();
}
void TGToolBar::ButtonPressed()
{
TGButton *btn = (TGButton*)gTQSender;
TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
if (a) {
Int_t id = (Int_t)Long_t(a->Value());
Pressed(id);
}
}
void TGToolBar::ButtonReleased()
{
TGButton *btn = (TGButton*)gTQSender;
TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
if (a) {
Int_t id = (Int_t)Long_t(a->Value());
Released(id);
}
}
void TGToolBar::ButtonClicked()
{
TGButton *btn = (TGButton*)gTQSender;
TPair *a = (TPair*) fMapOfButtons->FindObject(btn);
if (a) {
Int_t id = (Int_t)Long_t(a->Value());
Clicked(id);
}
}
void TGToolBar::SavePrimitive(std::ostream &out, Option_t *option )
{
if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
out << std::endl;
out << " // tool bar" << std::endl;
out << " TGToolBar *";
out << GetName() << " = new TGToolBar(" << fParent->GetName()
<< "," << GetWidth() << "," << GetHeight();
if (fBackground == GetDefaultFrameBackground()) {
if (!GetOptions()) {
out <<");" << std::endl;
} else {
out << "," << GetOptionString() <<");" << std::endl;
}
} else {
out << "," << GetOptionString() << ",ucolor);" << std::endl;
}
if (option && strstr(option, "keep_names"))
out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
char quote = '"';
int i = 0;
const char *picname;
TGFrameElement *f;
TIter next(GetList());
while ((f = (TGFrameElement *) next())) {
if (f->fFrame->InheritsFrom(TGPictureButton::Class())) {
if (!gROOT->ClassSaved(TGPictureButton::Class())) {
out << std::endl << " ToolBarData_t t;" << std::endl;
}
TGPictureButton *pb = (TGPictureButton *)f->fFrame;
picname = pb->GetPicture()->GetName();
out << " t.fPixmap = " << quote
<< gSystem->ExpandPathName(gSystem->UnixPathName(picname))
<< quote << ";" << std::endl;
out << " t.fTipText = " << quote
<< pb->GetToolTip()->GetText()->GetString() << quote << ";" << std::endl;
if (pb->GetState() == kButtonDown) {
out << " t.fStayDown = kTRUE;" << std::endl;
} else {
out << " t.fStayDown = kFALSE;" << std::endl;
}
out << " t.fId = " << i+1 << ";" << std::endl;
out << " t.fButton = 0;" << std::endl;
out << " " << GetName() << "->AddButton(" << GetParent()->GetName()
<< ",&t," << f->fLayout->GetPadLeft() << ");" << std::endl;
if (pb->GetState() == kButtonDown) {
out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
out << " " << pb->GetName() << "->SetState(kButtonDown);" << std::endl;
}
if (pb->GetState() == kButtonDisabled) {
out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
out << " " << pb->GetName() << "->SetState(kButtonDisabled);" << std::endl;
}
if (pb->GetState() == kButtonEngaged) {
out << " TGButton *" << pb->GetName() << " = t.fButton;" << std::endl;
out << " " << pb->GetName() << "->SetState(kButtonEngaged);" << std::endl;
}
i++;
} else {
f->fFrame->SavePrimitive(out, option);
out << " " << GetName()<<"->AddFrame(" << f->fFrame->GetName();
f->fLayout->SavePrimitive(out, option);
out << ");"<< std::endl;
}
}
}