// @(#)root/gui:$Id$
// Author: Fons Rademakers   25/02/98

/*************************************************************************
 * 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.             *
 *************************************************************************/
/**************************************************************************

    This source is based on Xclass95, a Win95-looking GUI toolkit.
    Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.

    Xclass95 is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

**************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGToolBar                                                            //
//                                                                      //
// A toolbar is a composite frame that contains TGPictureButtons.       //
// Often used in combination with a TGHorizontal3DLine.                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#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)

{
   // Create toolbar widget.

   fPictures = new TList;
   fTrash    = new TList;
   fMapOfButtons = new TMap();  // map of button/id pairs

   SetWindowName();
}

//______________________________________________________________________________
TGToolBar::~TGToolBar()
{
   // Delete toolbar and its buttons and layout hints.

   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);

   // pictures might already have been deleted above, so avoid access
   // to these objects
   fPictures->Clear("nodelete");

   delete fPictures;
   delete fMapOfButtons;
}

//______________________________________________________________________________
TGButton *TGToolBar::AddButton(const TGWindow *w, ToolBarData_t *button, Int_t spacing)
{
   // Add button to toolbar. All buttons added via this method will be
   // deleted by the toolbar. On return the TGButton field of the
   // ToolBarData_t struct is filled in (if fPixmap was valid).
   // Window w is the window to which the button messages will be send.

   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)
{
   // Add button to toolbar. All buttons added via this method will be deleted
   // by the toolbar, w is the window to which the button messages will be send.

   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
{
   // Finds and returns a pointer to the button with the specified
   // identifier id. Returns null if the button was not found.

   TIter next(fMapOfButtons);
   TGButton *item = 0;

   while ((item = (TGButton*)next())) {
      if ((Long_t)fMapOfButtons->GetValue(item) == id) break;   // found
   }

   return item;
}

//______________________________________________________________________________
void TGToolBar::SetId(TGButton *button, Long_t id)
{
   // changes id for button.

   TPair *a = (TPair*) fMapOfButtons->FindObject(button);
   if (a) {
      a->SetValue((TObject*)id);
   }
}

//______________________________________________________________________________
Long_t TGToolBar::GetId(TGButton *button) const
{
   // Finds and returns the id of the button.
   // Returns -1 if the button is not a member of this group.

   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)
{
   // Change the icon of a toolbar button.

   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()
{
   // Cleanup and delete all objects contained in this composite frame.
   // This will delete all objects added via AddFrame().
   // CAUTION: all objects (frames and layout hints) must be unique, i.e.
   // cannot be shared.

   // avoid double deletion of objects in trash
   delete fTrash;
   fTrash = 0;

   TGCompositeFrame::Cleanup();
}

//______________________________________________________________________________
void TGToolBar::ButtonPressed()
{
   // This slot is activated when one of the buttons in the group emits the
   // Pressed() signal.

   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()
{
   // This slot is activated when one of the buttons in the group emits the
   // Released() signal.

   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()
{
   // This slot is activated when one of the buttons in the group emits the
   // Clicked() signal.

   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 /*= ""*/)
{
   // Save an horizontal slider as a C++ statement(s) on output stream out.

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