00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00052
00053 fPictures = new TList;
00054 fTrash = new TList;
00055 fMapOfButtons = new TMap();
00056
00057 SetWindowName();
00058 }
00059
00060
00061 TGToolBar::~TGToolBar()
00062 {
00063
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
00077
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
00088
00089
00090
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
00127
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
00153
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;
00160 }
00161
00162 return item;
00163 }
00164
00165
00166 void TGToolBar::SetId(TGButton *button, Long_t id)
00167 {
00168
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
00180
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
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
00208
00209
00210
00211
00212
00213 delete fTrash;
00214 fTrash = 0;
00215
00216 TGCompositeFrame::Cleanup();
00217 }
00218
00219
00220 void TGToolBar::ButtonPressed()
00221 {
00222
00223
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
00238
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
00253
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
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
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 }