#include <stdlib.h>
#include "TGListTree.h"
#include "TGPicture.h"
#include "TGCanvas.h"
#include "TGScrollBar.h"
#include "TGToolTip.h"
#include "KeySymbols.h"
#include "TGTextEditDialogs.h"
#include "TGResourcePool.h"
#include "TGMsgBox.h"
#include "TError.h"
#include "TColor.h"
#include "TSystem.h"
#include "TString.h"
#include "Riostream.h"
Pixel_t        TGListTree::fgGrayPixel = 0;
const TGFont  *TGListTree::fgDefaultFont = 0;
TGGC          *TGListTree::fgDrawGC = 0;
TGGC          *TGListTree::fgLineGC = 0;
TGGC          *TGListTree::fgHighlightGC = 0;
TGGC          *TGListTree::fgColorGC = 0;
ClassImp(TGListTreeItem)
ClassImp(TGListTree)
static Int_t FontHeight(FontStruct_t f)
{
   int max_ascent, max_descent;
   gVirtualX->GetFontProperties(f, max_ascent, max_descent);
   return max_ascent + max_descent;
}
static Int_t FontAscent(FontStruct_t f)
{
   int max_ascent, max_descent;
   gVirtualX->GetFontProperties(f, max_ascent, max_descent);
   return max_ascent;
}
static Int_t FontTextWidth(FontStruct_t f, const char *c)
{
   return gVirtualX->TextWidth(f, c, strlen(c));
}
TGListTreeItem::TGListTreeItem(TGClient *client, const char *name,
                               const TGPicture *opened,
                               const TGPicture *closed,
                               Bool_t checkbox)
{
   
   fClient = client;
   fText = name;
   fCheckBox = checkbox;
   fChecked = kTRUE;
   if (!opened)
      opened = fClient->GetPicture("ofolder_t.xpm");
   else
      ((TGPicture *)opened)->AddReference();
   if (!closed)
      closed = fClient->GetPicture("folder_t.xpm");
   else
      ((TGPicture *)closed)->AddReference();
   fOpenPic   = opened;
   fClosedPic = closed;
   fCheckedPic   = fClient->GetPicture("checked_t.xpm");
   fUncheckedPic = fClient->GetPicture("unchecked_t.xpm");
   if (fCheckBox)
      fPicWidth  = TMath::Max(fCheckedPic->GetWidth() + fOpenPic->GetWidth(),
                              fCheckedPic->GetWidth() + fClosedPic->GetWidth());
   else
      fPicWidth  = TMath::Max(fOpenPic->GetWidth(), fClosedPic->GetWidth());
   fOpen = fActive = kFALSE;
   fParent =
   fFirstchild =
   fPrevsibling =
   fNextsibling = 0;
   fY =
   fHeight = 0;
   fUserData = 0;
   fHasColor = kFALSE;
   fColor = 0;
}
TGListTreeItem::~TGListTreeItem()
{
   
   fClient->FreePicture(fOpenPic);
   fClient->FreePicture(fClosedPic);
   fClient->FreePicture(fCheckedPic);
   fClient->FreePicture(fUncheckedPic);
}
void TGListTreeItem::Rename(const char *new_name)
{
   
   fText = new_name;
}
void TGListTreeItem::SetCheckBox(Bool_t on)
{
   
   fCheckBox = on;
   if (fCheckBox)
      fPicWidth  = TMath::Max(fCheckedPic->GetWidth() + fOpenPic->GetWidth(),
                              fCheckedPic->GetWidth() + fClosedPic->GetWidth());
   else
      fPicWidth  = TMath::Max(fOpenPic->GetWidth(), fClosedPic->GetWidth());
}
void TGListTreeItem::SetCheckBoxPictures(const TGPicture *checked,
                                         const TGPicture *unchecked)
{
   
   fClient->FreePicture(fCheckedPic);
   fClient->FreePicture(fUncheckedPic);
   if (!checked) {
         ::Warning("TGListTreeItem::SetCheckBoxPictures", "checked picture not specified, defaulting to checked_t");
         checked = fClient->GetPicture("checked_t.xpm");
   } else
      ((TGPicture *)checked)->AddReference();
   if (!unchecked) {
         ::Warning("TGListTreeItem::SetCheckBoxPictures", "unchecked picture not specified, defaulting to unchecked_t");
         unchecked = fClient->GetPicture("unchecked_t.xpm");
   } else
      ((TGPicture *)unchecked)->AddReference();
   fCheckedPic   = checked;
   fUncheckedPic = unchecked;
}
void TGListTreeItem::SetPictures(const TGPicture *opened, const TGPicture *closed)
{
   
   fClient->FreePicture(fOpenPic);
   fClient->FreePicture(fClosedPic);
   if (!opened) {
      ::Warning("TGListTreeItem::SetPictures", "opened picture not specified, defaulting to ofolder_t");
      opened = fClient->GetPicture("ofolder_t.xpm");
   } else
      ((TGPicture *)opened)->AddReference();
   if (!closed) {
      ::Warning("TGListTreeItem::SetPictures", "closed picture not specified, defaulting to folder_t");
      closed = fClient->GetPicture("folder_t.xpm");
   } else
      ((TGPicture *)closed)->AddReference();
   fOpenPic   = opened;
   fClosedPic = closed;
}
TGListTree::TGListTree(TGWindow *p, UInt_t w, UInt_t h, UInt_t options,
                       ULong_t back) :
   TGContainer(p, w, h, options, back)
{
   
   fMsgWindow   = p;
   fCanvas      = 0;
   fTip         = 0;
   fTipItem     = 0;
   fAutoTips    = kFALSE;
   fAutoCheckBoxPic = kTRUE;
   fDisableOpen = kFALSE;
   fGrayPixel   = GetGrayPixel();
   fFont        = GetDefaultFontStruct();
   fDrawGC      = GetDrawGC()();
   fLineGC      = GetLineGC()();
   fHighlightGC = GetHighlightGC()();
   fColorGC     = GetColorGC()();
   fFirst = fSelected = 0;
   fDefw = fDefh = 1;
   fHspacing = 2;
   fVspacing = 2;  
   fIndent   = 3;  
   fMargin   = 2;
   fColorMode = kDefault;
   gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
                         kButtonPressMask | kButtonReleaseMask,
                         kNone, kNone);
   AddInput(kPointerMotionMask | kEnterWindowMask |
            kLeaveWindowMask | kKeyPressMask);
   SetWindowName();
   fEditDisabled = kEditDisable | kEditDisableGrab | kEditDisableBtnEnable;
}
TGListTree::TGListTree(TGCanvas *p,UInt_t options,ULong_t back) :
   TGContainer(p, options, back)
{
   
   fMsgWindow   = p;
   fTip         = 0;
   fTipItem     = 0;
   fAutoTips    = kFALSE;
   fAutoCheckBoxPic = kTRUE;
   fDisableOpen = kFALSE;
   fGrayPixel   = GetGrayPixel();
   fFont        = GetDefaultFontStruct();
   fDrawGC      = GetDrawGC()();
   fLineGC      = GetLineGC()();
   fHighlightGC = GetHighlightGC()();
   fColorGC     = GetColorGC()();
   fFirst = fSelected = 0;
   fDefw = fDefh = 1;
   fHspacing = 2;
   fVspacing = 2;  
   fIndent   = 3;  
   fMargin   = 2;
   fColorMode = kDefault;
   gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
                         kButtonPressMask | kButtonReleaseMask,
                         kNone, kNone);
   AddInput(kPointerMotionMask | kEnterWindowMask |
            kLeaveWindowMask | kKeyPressMask);
   SetWindowName();
   fEditDisabled = kEditDisable | kEditDisableGrab | kEditDisableBtnEnable;
}
TGListTree::~TGListTree()
{
   
   TGListTreeItem *item, *sibling;
   delete fTip;
   item = fFirst;
   while (item) {
      if (item->fFirstchild)
         PDeleteChildren(item->fFirstchild);
      sibling = item->fNextsibling;
      delete item;
      item = sibling;
   }
}
void TGListTree::HighlightItem(TGListTreeItem *item, Bool_t state, Bool_t draw)
{
   
   if (item) {
      if ((item == fSelected) && !state) {
         fSelected = 0;
         if (draw) DrawItemName(item);
      } else if (state != item->fActive) {
         item->fActive = state;
         if (draw) DrawItemName(item);
      }
   }
}
void TGListTree::HighlightChildren(TGListTreeItem *item, Bool_t state, Bool_t draw)
{
   
   while (item) {
      HighlightItem(item, state, draw);
      if (item->fFirstchild)
         HighlightChildren(item->fFirstchild, state, (item->fOpen) ? draw : kFALSE);
      item = item->fNextsibling;
   }
}
void TGListTree::UnselectAll(Bool_t draw)
{
   
   HighlightChildren(fFirst, kFALSE, draw);
}
Bool_t TGListTree::HandleButton(Event_t *event)
{
   
   TGListTreeItem *item;
   if (fTip) fTip->Hide();
   Int_t page = 0;
   if (event->fCode == kButton4 || event->fCode == kButton5) {
      if (!fCanvas) return kTRUE;
      if (fCanvas->GetContainer()->GetHeight())
         page = Int_t(Float_t(fCanvas->GetViewPort()->GetHeight() *
                              fCanvas->GetViewPort()->GetHeight()) /
                              fCanvas->GetContainer()->GetHeight());
   }
   if (event->fCode == kButton4) {
      
      Int_t newpos = fCanvas->GetVsbPosition() - page;
      if (newpos < 0) newpos = 0;
      fCanvas->SetVsbPosition(newpos);
      return kTRUE;
   }
   if (event->fCode == kButton5) {
      
      Int_t newpos = fCanvas->GetVsbPosition() + page;
      fCanvas->SetVsbPosition(newpos);
      return kTRUE;
   }
   if (event->fType == kButtonPress) {
      if ((item = FindItem(event->fY)) != 0) {
         if (event->fCode == kButton1) {
            Int_t minx, maxx;
            Int_t minxchk = 0, maxxchk = 0;
            if (item->HasCheckBox()) {
               minxchk = (item->fXtext - Int_t(item->fPicWidth));
               maxxchk = (item->fXtext - Int_t(item->fPicWidth)) +
                       Int_t(item->fCheckedPic->GetWidth()) - 4;
               maxx = maxxchk - 8;
               minx = minxchk - 16;
            }
            else {
               maxx = (item->fXtext - Int_t(item->fPicWidth)) - 8;
               minx = (item->fXtext - Int_t(item->fPicWidth)) - 16;
            }
            if ((item->HasCheckBox()) && (event->fX < maxxchk) &&
               (event->fX > minxchk)) {
               fLastY = event->fY;
               ToggleItem(item);
               UpdateChecked(item, kTRUE);
               Checked((TObject *)item->GetUserData(), item->IsChecked());
               return kTRUE;
            }
            if ((event->fX < maxx) && (event->fX > minx)) {
               ClearViewPort();
               item->fOpen = !item->fOpen;
               fClient->NeedRedraw(this);
               return kTRUE;
            }
         }
         if (fSelected) fSelected->fActive = kFALSE;
         fLastY = event->fY;
         UnselectAll(kTRUE);
         fSelected = item;
         
         HighlightItem(item, kTRUE, kTRUE);
         SendMessage(fMsgWindow, MK_MSG(kC_LISTTREE, kCT_ITEMCLICK),
                     event->fCode, (event->fYRoot << 16) | event->fXRoot);
         Clicked(item, event->fCode);
         Clicked(item, event->fCode, event->fXRoot, event->fYRoot);
      }
   }
   if (event->fType == kButtonRelease) gVirtualX->SetInputFocus(fId);
   return kTRUE;
}
Bool_t TGListTree::HandleDoubleClick(Event_t *event)
{
   
   TGListTreeItem *item;
   
   
   if (fDisableOpen && event->fCode == kButton1 && (item = FindItem(event->fY)) != 0) {
      SendMessage(fMsgWindow, MK_MSG(kC_LISTTREE, kCT_ITEMDBLCLICK),
                  event->fCode, (event->fYRoot << 16) | event->fXRoot);
      DoubleClicked(item, event->fCode);
      DoubleClicked(item, event->fCode, event->fXRoot, event->fYRoot);
      return kTRUE;
   }
   
   if (event->fCode == kButton1 && (item = FindItem(event->fY)) != 0) {
      ClearViewPort();
      item->fOpen = !item->fOpen;
      if (item != fSelected) { 
         if (fSelected) fSelected->fActive = kFALSE;
         UnselectAll(kTRUE);
         fSelected = item;
         
         HighlightItem(item, kTRUE, kTRUE);
      }
      SendMessage(fMsgWindow, MK_MSG(kC_LISTTREE, kCT_ITEMDBLCLICK),
                  event->fCode, (event->fYRoot << 16) | event->fXRoot);
      DoubleClicked(item, event->fCode);
      DoubleClicked(item, event->fCode, event->fXRoot, event->fYRoot);
      fClient->NeedRedraw(this);
   }
   return kTRUE;
}
Bool_t TGListTree::HandleExpose(Event_t * )
{
   
   Draw(0, (Int_t)fHeight);
   return kTRUE;
}
Bool_t TGListTree::HandleCrossing(Event_t *event)
{
   
   if (fTip) {
      if (event->fType == kLeaveNotify) {
         fTip->Hide();
         fTipItem = 0;
      }
   }
   return kTRUE;
}
Bool_t TGListTree::HandleMotion(Event_t *event)
{
   
   TGListTreeItem *item;
   fOnMouseOver = kFALSE;
   TGPosition pos = GetPagePosition();
   if ((item = FindItem(event->fY)) != 0) {
      if (item->HasCheckBox()) {
         if ((event->fX < (item->fXtext - Int_t(item->fPicWidth)) +
                           Int_t(item->fCheckedPic->GetWidth()) - 4) &&
             (event->fX > (item->fXtext - Int_t(item->fPicWidth)))) {
            gVirtualX->SetCursor(fId, gVirtualX->CreateCursor(kPointer));
            return kTRUE;
         }
         else {
            gVirtualX->SetCursor(fId, gVirtualX->CreateCursor(kHand));
         }
      }
      if (fTipItem == item) return kTRUE;
      OnMouseOver(item);
      gVirtualX->SetCursor(fId, gVirtualX->CreateCursor(kHand));
      if (fTip)
         fTip->Hide();
      if (item->fTipText.Length() > 0) {
         SetToolTipText(item->fTipText.Data(), item->fXtext,
                        item->fY -pos.fY +item->fHeight -4, 1000);
      } else if (fAutoTips && item->GetUserData()) {
         
         
         TObject *obj = (TObject *)item->GetUserData();
         if (obj->InheritsFrom(TObject::Class())) {
            SetToolTipText(obj->GetTitle(), item->fXtext,
                           item->fY -pos.fY +item->fHeight -4, 1000);
         }
      }
      fTipItem = item;
   } else {
      gVirtualX->SetCursor(fId, gVirtualX->CreateCursor(kPointer));
   }
   return kTRUE;
}
Bool_t TGListTree::HandleKey(Event_t *event)
{
   
   
   char   input[10];
   Int_t  n;
   UInt_t keysym;
   if (event->fType == kGKeyPress) {
      gVirtualX->LookupString(event, input, sizeof(input), keysym);
      n = strlen(input);
      KeyPressed(fSelected, keysym, event->fState);
      switch ((EKeySym)keysym) {
         case kKey_Enter:
         case kKey_Return:
            
            if (fSelected) fSelected->fOpen = !fSelected->fOpen;
            SendMessage(fMsgWindow, MK_MSG(kC_LISTTREE, kCT_ITEMDBLCLICK),
                        kButton1, (event->fYRoot << 16) | event->fXRoot);
            if (fSelected) ReturnPressed(fSelected);
            break;
         default:
         break;
      }
      if (event->fState & kKeyControlMask) {   
         switch((EKeySym)keysym & ~0x20) {   
            case kKey_A:
               break;
            case kKey_B:
               break;
            case kKey_C:
               return kTRUE;
            case kKey_D:
               break;
            case kKey_E:
               End();
               break;
            case kKey_F:
               Search();
               break;
            case kKey_G:
               break;
            case kKey_H:
               break;
            case kKey_K:
               End();
               break;
            case kKey_U:
               Home();
               break;
            case kKey_V:
            case kKey_Y:
               return kTRUE;
            case kKey_X:
               return kTRUE;
            default:
               return kTRUE;
         }
      }
      if (n && keysym >= 32 && keysym < 127 &&     
          !(event->fState & kKeyControlMask) &&
          (EKeySym)keysym != kKey_Delete &&
          (EKeySym)keysym != kKey_Backspace) {
      } else {
         switch ((EKeySym)keysym) {
            case kKey_F3:
               break;
            case kKey_F5:
               Layout();
               break;
            case kKey_F7:
               Search();
               break;
            case kKey_Left:
               break;
            case kKey_Right:
               break;
            case kKey_Up:
               LineUp(event->fState & kKeyShiftMask);
               break;
            case kKey_Down:
               LineDown(event->fState & kKeyShiftMask);
               break;
            case kKey_PageUp:
               PageUp(event->fState & kKeyShiftMask);
               break;
            case kKey_PageDown:
               PageDown(event->fState & kKeyShiftMask);
               break;
            case kKey_Home:
               Home(event->fState & kKeyShiftMask);
               break;
            case kKey_End:
               End(event->fState & kKeyShiftMask);
               break;
            default:
               break;
         }
      }
   }
   return kTRUE;
}
void TGListTree::OnMouseOver(TGListTreeItem *entry)
{
   
   if (!fOnMouseOver) Emit("OnMouseOver(TGListTreeItem*)", (Long_t)entry);
   fOnMouseOver = kTRUE;
}
void TGListTree::KeyPressed(TGListTreeItem *entry, UInt_t keysym, UInt_t mask)
{
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   Long_t args[3];
   args[0] = (Long_t)entry;
   args[1] = (Long_t)keysym;
   args[2] = (Long_t)mask;
   Emit("KeyPressed(TGFame*,ULong_t,ULong_t)", args);
   SendMessage(fMsgWindow, MK_MSG(kC_LISTTREE, kCT_KEY), keysym, mask);
}
void TGListTree::ReturnPressed(TGListTreeItem *entry)
{
   
   Emit("ReturnPressed(TGListTreeItem*)", (Long_t)entry);
}
void TGListTree::Clicked(TGListTreeItem *entry, Int_t btn)
{
   
   Long_t args[2];
   args[0] = (Long_t)entry;
   args[1] = btn;
   Emit("Clicked(TGListTreeItem*,Int_t)", args);
}
void TGListTree::Checked(TObject *entry, Bool_t on)
{
   
   Long_t args[2];
   args[0] = (Long_t)entry;
   args[1] = on;
   Emit("Checked(TObject*,Bool_t)", args);
}
void TGListTree::Clicked(TGListTreeItem *entry, Int_t btn, Int_t x, Int_t y)
{
   
   Long_t args[4];
   args[0] = (Long_t)entry;
   args[1] = btn;
   args[2] = x;
   args[3] = y;
   Emit("Clicked(TGListTreeItem*,Int_t,Int_t,Int_t)", args);
}
void TGListTree::DoubleClicked(TGListTreeItem *entry, Int_t btn)
{
   
   Long_t args[2];
   args[0] = (Long_t)entry;
   args[1] = btn;
   Emit("DoubleClicked(TGListTreeItem*,Int_t)", args);
}
void TGListTree::DoubleClicked(TGListTreeItem *entry, Int_t btn, Int_t x, Int_t y)
{
   
   Long_t args[4];
   args[0] = (Long_t)entry;
   args[1] = btn;
   args[2] = x;
   args[3] = y;
   Emit("DoubleClicked(TGListTreeItem*,Int_t,Int_t,Int_t)", args);
}
void TGListTree::Home(Bool_t )
{
   
   if (fCanvas) fCanvas->SetVsbPosition(0);
}
void TGListTree::End(Bool_t )
{
   
   if (fCanvas) fCanvas->SetVsbPosition((Int_t)fHeight);
}
void TGListTree::PageUp(Bool_t )
{
   
   if (!fCanvas || !fSelected) return;
   TGDimension dim = GetPageDimension();
   Int_t newpos = fCanvas->GetVsbPosition() - dim.fHeight;
   if (newpos<0) newpos = 0;
   fCanvas->SetVsbPosition(newpos);
}
void TGListTree::PageDown(Bool_t )
{
   
   if (!fCanvas || !fSelected) return;
   TGDimension dim = GetPageDimension();
   Int_t newpos = fCanvas->GetVsbPosition() + dim.fHeight;
   fCanvas->SetVsbPosition(newpos);
}
void TGListTree::LineUp(Bool_t )
{
   
   if (!fCanvas || !fSelected) return;
   const TGPicture *pic1, *pic2;
   Int_t height;
   if (fSelected->fOpen)
      pic2 = fSelected->fOpenPic;
   else
      pic2 = fSelected->fClosedPic;
   if (fSelected->HasCheckBox()){
      if (fSelected->IsChecked())
         pic1 = fSelected->fOpenPic;
      else
         pic1 = fSelected->fClosedPic;
      height  = TMath::Max(pic2->GetHeight() + fVspacing, pic1->GetHeight() + fVspacing);
   }
   else {
      height = pic2->GetHeight() + fVspacing;
   }
   Int_t newpos = fCanvas->GetVsbPosition() - height;
   if (newpos<0) newpos = 0;
   fCanvas->SetVsbPosition(newpos);
}
void TGListTree::LineDown(Bool_t )
{
   
   if (!fCanvas || !fSelected) return;
   const TGPicture *pic1, *pic2;
   Int_t height;
   if (fSelected->fOpen)
      pic2 = fSelected->fOpenPic;
   else
      pic2 = fSelected->fClosedPic;
   if (fSelected->HasCheckBox()){
      if (fSelected->IsChecked())
         pic1 = fSelected->fOpenPic;
      else
         pic1 = fSelected->fClosedPic;
      height  = TMath::Max(pic2->GetHeight() + fVspacing, pic1->GetHeight() + fVspacing);
   }
   else {
      height = pic2->GetHeight() + fVspacing;
   }
   Int_t newpos = fCanvas->GetVsbPosition() + height;
   if (newpos<0) newpos = 0;
   fCanvas->SetVsbPosition(newpos);
}
void TGListTree::AdjustPosition(TGListTreeItem *item)
{
   
   
   TGListTreeItem *it = item;
   if (!it) it = fSelected;
   if (!it) {
      HighlightItem(fFirst); 
      return;
   }
   Int_t y = 0;
   Int_t yparent = 0;
   Int_t vh = 0;
   Int_t v = 0;
   if (it) {
      y = it->fY;
      if (it->GetParent()) yparent = it->GetParent()->fY;
   }
   if (y==0) y = yparent; 
   if (fCanvas->GetVScrollbar()->IsMapped()) {
      vh = fCanvas->GetVScrollbar()->GetPosition()+(Int_t)fViewPort->GetHeight();
      if (y<fCanvas->GetVScrollbar()->GetPosition()) {
         v = TMath::Max(0,y-(Int_t)fViewPort->GetHeight()/2);
         fCanvas->SetVsbPosition(v);
      } else if (y+(Int_t)it->fHeight>vh) {
         v = TMath::Min((Int_t)GetHeight()-(Int_t)fViewPort->GetHeight(),
                        y+(Int_t)it->fHeight-(Int_t)fViewPort->GetHeight()/2);
         if (v<0) v = 0;
         fCanvas->SetVsbPosition(v);
      }
   }
}
void TGListTree::Search(Bool_t )
{
   
   Int_t ret = 0;
   char msg[256];
   static TString buf;
   TGSearchType *srch = new TGSearchType;
   srch->fBuffer = (char*)buf.Data();
   TGListTreeItem *item;
   new TGSearchDialog(fClient->GetDefaultRoot(), fCanvas, 400, 150, srch, &ret);
   if (ret) {
      item = FindItemByPathname(srch->fBuffer);
      if (!item) {
         sprintf(msg, "Couldn't find \"%s\"", srch->fBuffer);
         gVirtualX->Bell(20);
         new TGMsgBox(fClient->GetDefaultRoot(), fCanvas, "Container", msg,
                      kMBIconExclamation, kMBOk, 0);
      } else {
         ClearHighlighted();
         HighlightItem(item);
      }
   }
   buf = srch->fBuffer;
   delete srch;
}
void TGListTree::DrawRegion(Int_t , Int_t , UInt_t , UInt_t )
{
   
   Draw(0, (Int_t)fHeight);
}
void TGListTree::DoRedraw()
{
   
   ClearViewPort();
   Draw(0, (Int_t)fHeight);
}
void TGListTree::Draw(Int_t yevent, Int_t hevent)
{
   
   TGListTreeItem *item;
   Int_t  x, y, xbranch;
   UInt_t width, height, old_width, old_height;
   
   
   fExposeTop = yevent - FontHeight(fFont);
   fExposeBottom = yevent + hevent + FontHeight(fFont);
   old_width  = fDefw;
   old_height = fDefh;
   fDefw = fDefh = 1;
   TGPosition pos = GetPagePosition();
   x = -pos.fX;
   y = fMargin + yevent;
   item = fFirst;
   while (item) {
      xbranch = -1;
      DrawItem(item, x, y, &xbranch, &width, &height);
      width += pos.fX + x + fHspacing + fMargin;
      if (width > fDefw) fDefw = width;
      y += height + fVspacing;
      if (item->fFirstchild && item->fOpen)
         y = DrawChildren(item->fFirstchild, x, y, xbranch);
      item = item->fNextsibling;
   }
   fDefh = y + fMargin;
   if ((old_width != fDefw) || (old_height != fDefh)) {
      fCanvas->Layout();
   }
}
Int_t TGListTree::DrawChildren(TGListTreeItem *item, Int_t x, Int_t y, Int_t xroot)
{
   
   UInt_t width, height;
   Int_t  xbranch;
   TGPosition pos = GetPagePosition();
   x += fIndent + (Int_t)item->fParent->fPicWidth;
   while (item) {
      xbranch = xroot;
      DrawItem(item, x, y, &xbranch, &width, &height);
      width += pos.fX + x + fHspacing + fMargin;
      if (width > fDefw) fDefw = width;
      y += height + fVspacing;
      if ((item->fFirstchild) && (item->fOpen))
         y = DrawChildren(item->fFirstchild, x, y, xbranch);
      item = item->fNextsibling;
   }
   return y;
}
void TGListTree::DrawItem(TGListTreeItem *item, Int_t x, Int_t y, Int_t *xroot,
                          UInt_t *retwidth, UInt_t *retheight)
{
   
   Int_t  xpic2, ypic2, xbranch, ybranch, xtext, ytext, xline, yline, xc;
   Int_t  xpic1 = 0, ypic1 = 0;
   UInt_t height;
   const TGPicture *pic1 = 0, *pic2;
   TGPosition pos = GetPagePosition();
   TGDimension dim = GetPageDimension();
   
   if (item->fOpen)
      pic2 = item->fOpenPic;
   else
      pic2 = item->fClosedPic;
   if (item->HasCheckBox()) {
      if (item->IsChecked())
         pic1 = item->fCheckedPic;
      else
         pic1 = item->fUncheckedPic;
   }
   
   height = FontHeight(fFont);
   xpic2 = x;
   xtext = x + fHspacing + (Int_t)item->fPicWidth;
   if ((item->HasCheckBox()) && (pic1)) {
      if (pic1->GetHeight() > height) {
         ytext = y + (Int_t)((pic1->GetHeight() - height) >> 1);
         height = pic1->GetHeight();
         ypic1 = y;
      } else {
         ytext = y;
         ypic1 = y + (Int_t)((height - pic1->GetHeight()) >> 1);
      }
      xpic1 = x;
      xpic2 = xpic1 + pic1->GetWidth();
      xline = xpic1;
   } else {
      xpic2 = x;
      ypic2 = y;
      xline = 0;
   }
   if (pic2) {
      if (pic2->GetHeight() > height) {
         ytext = y + (Int_t)((pic2->GetHeight() - height) >> 1);
         height = pic2->GetHeight();
         ypic2 = y;
      } else {
         ytext = y;
         ypic2 = y + (Int_t)((height - pic2->GetHeight()) >> 1);
      }
      xbranch = xpic2 + (Int_t)(pic2->GetWidth() >> 1);
      ybranch = ypic2 + (Int_t)pic2->GetHeight();
      yline = ypic2 + (Int_t)(pic2->GetHeight() >> 1);
      if (xline == 0) xline = xpic2;
   } else {
      if (xline == 0) xline = xpic2;
      ypic2 = ytext = y;
      xbranch = xpic2 + (Int_t)(item->fPicWidth >> 1);
      yline = ybranch = ypic2 + (Int_t)(height >> 1);
      yline = ypic2 + (Int_t)(height >> 1);
   }
   
   ++height; height &= ~1;
   
   item->fY      = y;
   item->fXtext  = xtext;
   item->fYtext  = ytext;
   item->fHeight = height;
   
   Int_t yp = y - pos.fY;
   Int_t ylinep = yline - pos.fY;
   Int_t ybranchp = ybranch - pos.fY;
   Int_t ypicp = ypic2 - pos.fY;
   if ((yp >= fExposeTop) && (yp <= (Int_t)dim.fHeight)) {
      if (*xroot >= 0) {
         xc = *xroot;
         if (item->fNextsibling)
            gVirtualX->DrawLine(fId, fLineGC, xc, yp, xc, yp+height);
         else
            gVirtualX->DrawLine(fId, fLineGC, xc, yp, xc, ylinep);
         TGListTreeItem *p = item->fParent;
         while (p) {
            xc -= (fIndent + (Int_t)item->fPicWidth);
            if (p->fNextsibling)
               gVirtualX->DrawLine(fId, fLineGC, xc, yp, xc, yp+height);
            p = p->fParent;
         }
         gVirtualX->DrawLine(fId, fLineGC, *xroot, ylinep, xline  , ylinep);
         DrawNode(item, *xroot, yline);
      }
      if (item->fOpen && item->fFirstchild)
         gVirtualX->DrawLine(fId, fLineGC, xbranch, ybranchp,
                        xbranch, yp+height);
      
      
      if (item->HasCheckBox()) {
         if (item->IsChecked())
            item->fCheckedPic->Draw(fId, fDrawGC, xpic1, ypicp);
         else
            item->fUncheckedPic->Draw(fId, fDrawGC, xpic1, ypicp);
      }
      if (item->fActive || item == fSelected)
         item->fOpenPic->Draw(fId, fDrawGC, xpic2, ypicp);
      else
         item->fClosedPic->Draw(fId, fDrawGC, xpic2, ypicp);
      DrawItemName(item);
   }
   *xroot = xbranch;
   *retwidth = FontTextWidth(fFont, item->fText.Data()) + item->fPicWidth;
   *retheight = height;
}
void TGListTree::DrawItemName(TGListTreeItem *item)
{
   
   UInt_t width;
   TGPosition pos = GetPagePosition();
   width = FontTextWidth(fFont, item->fText.Data());
   if (item->fActive || item == fSelected) {
      gVirtualX->SetForeground(fDrawGC, fgDefaultSelectedBackground);
      gVirtualX->FillRectangle(fId, fDrawGC,
                          item->fXtext, item->fYtext-pos.fY, width, FontHeight(fFont));
      gVirtualX->SetForeground(fDrawGC, fgBlackPixel);
      gVirtualX->DrawString(fId, fHighlightGC,
                       item->fXtext, item->fYtext - pos.fY + FontAscent(fFont),
                       item->fText.Data(), item->fText.Length());
   } else {
      gVirtualX->FillRectangle(fId, fHighlightGC,
                          item->fXtext, item->fYtext-pos.fY, width, FontHeight(fFont));
      gVirtualX->DrawString(fId, fDrawGC,
                       item->fXtext, item->fYtext-pos.fY + FontAscent(fFont),
                       item->fText.Data(), item->fText.Length());
   }
   if (fColorMode != 0 && item->fHasColor) {
      gVirtualX->SetForeground(fColorGC, TColor::Number2Pixel(item->fColor));
      if (fColorMode & kColorUnderline) {
         Int_t y = item->fYtext-pos.fY + FontAscent(fFont) + 2;
         gVirtualX->DrawLine(fId, fColorGC, item->fXtext, y, item->fXtext + width, y);
      }
      if (fColorMode & kColorBox) {
         Int_t x = item->fXtext + width + 4;
         Int_t y = item->fYtext-pos.fY  + 3;
         Int_t h = FontAscent(fFont)    - 4;
         gVirtualX->FillRectangle(fId, fColorGC, x, y, h, h);
         gVirtualX->DrawRectangle(fId, fDrawGC,  x, y, h, h);
      }
   }
}
void TGListTree::DrawNode(TGListTreeItem *item, Int_t x, Int_t y)
{
   
   TGPosition pos = GetPagePosition();
   Int_t yp = y - pos.fY;
   if (item->fFirstchild) {
      gVirtualX->DrawLine(fId, fHighlightGC, x, yp-2, x, yp+2);
      gVirtualX->SetForeground(fHighlightGC, fgBlackPixel);
      gVirtualX->DrawLine(fId, fHighlightGC, x-2, yp, x+2, yp);
      if (!item->fOpen)
         gVirtualX->DrawLine(fId, fHighlightGC, x, yp-2, x, yp+2);
      gVirtualX->SetForeground(fHighlightGC, fGrayPixel);
      gVirtualX->DrawLine(fId, fHighlightGC, x-4, yp-4, x+4, yp-4);
      gVirtualX->DrawLine(fId, fHighlightGC, x+4, yp-4, x+4, yp+4);
      gVirtualX->DrawLine(fId, fHighlightGC, x-4, yp+4, x+4, yp+4);
      gVirtualX->DrawLine(fId, fHighlightGC, x-4, yp-4, x-4, yp+4);
      gVirtualX->SetForeground(fHighlightGC, fgWhitePixel);
   }
}
void TGListTree::SetToolTipText(const char *text, Int_t x, Int_t y, Long_t delayms)
{
   
   
   
   
   if (delayms == 0) {
      delete fTip;
      fTip = 0;
      return;
   }
   if (text && strlen(text)) {
      if (!fTip)
         fTip = new TGToolTip(fClient->GetDefaultRoot(), this, text, delayms);
      else
         fTip->SetText(text);
      fTip->SetPosition(x, y);
      fTip->Reset();
   }
}
void TGListTree::RemoveReference(TGListTreeItem *item)
{
   
   
   
   if (item->fPrevsibling) {
      item->fPrevsibling->fNextsibling = item->fNextsibling;
      if (item->fNextsibling)
         item->fNextsibling->fPrevsibling = item->fPrevsibling;
   } else {
      
      if (item->fParent)
         item->fParent->fFirstchild = item->fNextsibling;
      else
         fFirst = item->fNextsibling;
      if (item->fNextsibling)
         item->fNextsibling->fPrevsibling = 0;
   }
}
void TGListTree::PDeleteChildren(TGListTreeItem *item)
{
   
   TGListTreeItem *sibling;
   while (item) {
      if (item->fFirstchild) {
         PDeleteChildren(item->fFirstchild);
         item->fFirstchild = 0;
      }
      sibling = item->fNextsibling;
      if (fSelected == item)
         fSelected = 0;
      delete item;
      item = sibling;
   }
}
void TGListTree::InsertChild(TGListTreeItem *parent, TGListTreeItem *item)
{
   
   TGListTreeItem *i;
   item->fParent = parent;
   item->fNextsibling = item->fPrevsibling = 0;
   if (parent) {
      if (parent->fFirstchild) {
         i = parent->fFirstchild;
         while (i->fNextsibling) i = i->fNextsibling;
         i->fNextsibling = item;
         item->fPrevsibling = i;
      } else {
         parent->fFirstchild = item;
      }
   } else {  
      if (fFirst) {
         i = fFirst;
         while (i->fNextsibling) i = i->fNextsibling;
         i->fNextsibling = item;
         item->fPrevsibling = i;
      } else {
         fFirst = item;
      }
   }
   UpdateChecked(item);
}
void TGListTree::InsertChildren(TGListTreeItem *parent, TGListTreeItem *item)
{
   
   TGListTreeItem *next, *newnext;
   
   
   
   
   
   
   
   next = item->fNextsibling;
   
   InsertChild(parent, item);
   
   
   newnext = item->fNextsibling;
   
   item->fNextsibling = next;
   
   
   
   while (item->fNextsibling) {
      item->fParent = parent;
      item = item->fNextsibling;
   }
   
   item->fNextsibling = newnext;
   if (newnext)
      newnext->fPrevsibling = item;
}
Int_t TGListTree::SearchChildren(TGListTreeItem *item, Int_t y, Int_t findy,
                                 TGListTreeItem **finditem)
{
   
   UInt_t height;
   const TGPicture *pic;
   while (item) {
      
      if (item->fOpen)
         pic = item->fOpenPic;
      else
         pic = item->fClosedPic;
      
      height = FontHeight(fFont);
      if (pic && (pic->GetHeight() > height))
         height = pic->GetHeight();
      if ((findy >= y) && (findy <= y + (Int_t)height)) {
         *finditem = item;
         return -1;
      }
      y += (Int_t)height + fVspacing;
      if ((item->fFirstchild) && (item->fOpen)) {
         y = SearchChildren(item->fFirstchild, y, findy, finditem);
         if (*finditem) return -1;
      }
      item = item->fNextsibling;
   }
   return y;
}
TGListTreeItem *TGListTree::FindItem(Int_t findy)
{
   
   Int_t  y;
   UInt_t height;
   TGListTreeItem *item, *finditem;
   const TGPicture *pic;
   TGPosition pos = GetPagePosition();
   y = fMargin - pos.fY;
   item = fFirst;
   finditem = 0;
   while (item && !finditem) {
      
      if (item->fOpen)
         pic = item->fOpenPic;
      else
         pic = item->fClosedPic;
      
      height = FontHeight(fFont);
      if (pic && (pic->GetHeight() > height))
         height = pic->GetHeight();
      if ((findy >= y) && (findy <= y + (Int_t)height))
         return item;
      y += (Int_t)height + fVspacing;
      if ((item->fFirstchild) && (item->fOpen)) {
         y = SearchChildren(item->fFirstchild, y, findy, &finditem);
         
      }
      item = item->fNextsibling;
   }
   return finditem;
}
TGListTreeItem *TGListTree::AddItem(TGListTreeItem *parent, const char *string,
                                    const TGPicture *open, const TGPicture *closed,
                                    Bool_t checkbox)
{
   
   TGListTreeItem *item;
   item = new TGListTreeItem(fClient, string, open, closed, checkbox);
   InsertChild(parent, item);
   if (fClient->IsEditable()) {
      if (parent)
         OpenItem(parent);
      fClient->NeedRedraw(this);
   }
   return item;
}
TGListTreeItem *TGListTree::AddItem(TGListTreeItem *parent, const char *string,
                                    void *userData, const TGPicture *open,
                                    const TGPicture *closed,
                                    Bool_t checkbox)
{
   
   
   TGListTreeItem *item = FindChildByData(parent, userData);
   if (!item) {
      item = AddItem(parent, string, open, closed, checkbox);
      if (item) item->SetUserData(userData);
   }
   return item;
}
void TGListTree::RenameItem(TGListTreeItem *item, const char *string)
{
   
   if (item)
      item->Rename(string);
   if (fClient->IsEditable()) {
      fClient->NeedRedraw(this);
   }
}
Int_t TGListTree::DeleteItem(TGListTreeItem *item)
{
   
   if (item->fFirstchild)
      PDeleteChildren(item->fFirstchild);
   item->fFirstchild = 0;
   RemoveReference(item);
   if (fSelected == item)
      fSelected = 0;
   delete item;
   if (fClient->IsEditable()) {
      fClient->NeedRedraw(this);
   }
   return 1;
}
void TGListTree::OpenItem(TGListTreeItem *item)
{
   
   if (item) {
      item->fOpen = kTRUE;
      AdjustPosition(item);
   }
}
void TGListTree::CloseItem(TGListTreeItem *item)
{
   
   if (item) {
      item->fOpen = kFALSE;
      AdjustPosition(item);
   }
}
Int_t TGListTree::RecursiveDeleteItem(TGListTreeItem *item, void *ptr)
{
   
   
   if (item && ptr) {
      if (item->fUserData == ptr)
         DeleteItem(item);
      else {
         if (item->fOpen && item->fFirstchild)
            RecursiveDeleteItem(item->fFirstchild,  ptr);
         RecursiveDeleteItem(item->fNextsibling, ptr);
      }
   }
   return 1;
}
void TGListTree::SetToolTipItem(TGListTreeItem *item, const char *string)
{
   
   
   
   if (item)
      item->fTipText = string;
}
Int_t TGListTree::DeleteChildren(TGListTreeItem *item)
{
   
   if (item->fFirstchild)
      PDeleteChildren(item->fFirstchild);
   item->fFirstchild = 0;
   
   return 1;
}
Int_t TGListTree::Reparent(TGListTreeItem *item, TGListTreeItem *newparent)
{
   
   
   RemoveReference(item);
   
   InsertChild(newparent, item);
   
   return 1;
}
Int_t TGListTree::ReparentChildren(TGListTreeItem *item,
                                 TGListTreeItem *newparent)
{
   
   TGListTreeItem *first;
   if (item->fFirstchild) {
      first = item->fFirstchild;
      item->fFirstchild = 0;
      InsertChildren(newparent, first);
      
      return 1;
   }
   return 0;
}
static Int_t Compare(const void *item1, const void *item2)
{
   return strcmp((*((TGListTreeItem **) item1))->GetText(),
                 (*((TGListTreeItem **) item2))->GetText());
}
Int_t TGListTree::Sort(TGListTreeItem *item)
{
   
   TGListTreeItem *first, *parent, **list;
   size_t i, count;
   
   while (item->fPrevsibling) item = item->fPrevsibling;
   first = item;
   parent = first->fParent;
   
   count = 1;
   while (item->fNextsibling) item = item->fNextsibling, count++;
   if (count <= 1) return 1;
   list = new TGListTreeItem* [count];
   list[0] = first;
   count = 1;
   while (first->fNextsibling) {
      list[count] = first->fNextsibling;
      count++;
      first = first->fNextsibling;
   }
   ::qsort(list, count, sizeof(TGListTreeItem*), ::Compare);
   list[0]->fPrevsibling = 0;
   for (i = 0; i < count; i++) {
      if (i < count - 1)
         list[i]->fNextsibling = list[i + 1];
      if (i > 0)
         list[i]->fPrevsibling = list[i - 1];
   }
   list[count - 1]->fNextsibling = 0;
   if (parent)
      parent->fFirstchild = list[0];
   else
      fFirst = list[0];
   delete [] list;
   
   return 1;
}
Int_t TGListTree::SortSiblings(TGListTreeItem *item)
{
   
   return Sort(item);
}
Int_t TGListTree::SortChildren(TGListTreeItem *item)
{
   
   TGListTreeItem *first;
   if (item) {
      first = item->fFirstchild;
      if (first)
         SortSiblings(first);
   } else {
      if (fFirst) {
         first = fFirst->fFirstchild;
         if (first)
            SortSiblings(first);
      }
   }
   return 1;
}
TGListTreeItem *TGListTree::FindSiblingByName(TGListTreeItem *item, const char *name)
{
   
   
   if (item) {
      while (item->fPrevsibling)
         item = item->fPrevsibling;
      while (item) {
         if (item->fText == name) {
            return item;
         }
         item = item->fNextsibling;
      }
      return item;
   }
   return 0;
}
TGListTreeItem *TGListTree::FindSiblingByData(TGListTreeItem *item, void *userData)
{
   
   
   if (item) {
      while (item->fPrevsibling)
         item = item->fPrevsibling;
      while (item) {
         if (item->fUserData == userData) {
            return item;
         }
         item = item->fNextsibling;
      }
      return item;
   }
   return 0;
}
TGListTreeItem *TGListTree::FindChildByName(TGListTreeItem *item, const char *name)
{
   
   
   if (item && item->fFirstchild) {
      item = item->fFirstchild;
   } else if (!item && fFirst) {
      item = fFirst;
   } else {
      item = 0;
   }
   while (item) {
      if (item->fText == name) {
         return item;
      }
      item = item->fNextsibling;
   }
   return 0;
}
TGListTreeItem *TGListTree::FindChildByData(TGListTreeItem *item, void *userData)
{
   
   
   if (item && item->fFirstchild) {
      item = item->fFirstchild;
   } else if (!item && fFirst) {
      item = fFirst;
   } else {
      item = 0;
   }
   while (item) {
      if (item->fUserData == userData) {
         return item;
      }
      item = item->fNextsibling;
   }
   return 0;
}
TGListTreeItem *TGListTree::FindItemByPathname(const char *path)
{
   
   
   if (!path || !*path) return 0;
   const char *p = path, *s;
   char dirname[256];
   TGListTreeItem *item = 0;
   item = FindChildByName(item, "/");
   TGListTreeItem *diritem = 0;
   TString fulldir;
start:
   while (1) {
      while (*p && *p == '/') p++;
      if (!*p) break;
      s = strchr(p, '/');
      if (!s) {
         strcpy(dirname, p);
      } else {
         strncpy(dirname, p, s-p);
         dirname[s-p] = 0;
      }
      item = FindChildByName(item, dirname);
      if (!diritem && dirname) {
         fulldir += "/";
         fulldir += dirname;
         if ((diritem=FindChildByName(0, fulldir.Data()))) {
            if (!s || !strlen(s)) return diritem;
            p = ++s;
            item = diritem;
            goto start;
         }
      }
      if (!s || !strlen(s)) return item;
      p = ++s;
   }
   return 0;
}
void TGListTree::HighlightItem(TGListTreeItem *item)
{
   
   UnselectAll(kFALSE);
   HighlightItem(item, kTRUE, kFALSE);
   AdjustPosition(item);
   
}
void TGListTree::ClearHighlighted()
{
   
   UnselectAll(kFALSE);
   
}
void TGListTree::GetPathnameFromItem(TGListTreeItem *item, char *path, Int_t depth)
{
   
   
   char tmppath[1024];
   *path = '\0';
   while (item) {
      sprintf(tmppath, "/%s%s", item->fText.Data(), path);
      strcpy(path, tmppath);
      item = item->fParent;
      if (--depth == 0 && item) {
         sprintf(tmppath, "...%s", path);
         strcpy(path, tmppath);
         return;
      }
   }
}
Pixel_t TGListTree::GetGrayPixel()
{
   
   static Bool_t init = kFALSE;
   if (!init) {
      if (!gClient->GetColorByName("#808080", fgGrayPixel))
         fgGrayPixel = fgBlackPixel;
      init = kTRUE;
   }
   return fgGrayPixel;
}
FontStruct_t TGListTree::GetDefaultFontStruct()
{
   
   if (!fgDefaultFont)
      fgDefaultFont = gClient->GetResourcePool()->GetIconFont();
   return fgDefaultFont->GetFontStruct();
}
const TGGC &TGListTree::GetDrawGC()
{
   
   if (!fgDrawGC) {
      GCValues_t gcv;
      gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
                  kGCForeground | kGCBackground | kGCFont;
      gcv.fLineStyle  = kLineSolid;
      gcv.fLineWidth  = 0;
      gcv.fFillStyle  = kFillSolid;
      gcv.fFont       = fgDefaultFont->GetFontHandle();
      gcv.fBackground = fgWhitePixel;
      gcv.fForeground = fgBlackPixel;
      fgDrawGC = gClient->GetGC(&gcv, kTRUE);
   }
   return *fgDrawGC;
}
const TGGC &TGListTree::GetLineGC()
{
   
   if (!fgLineGC) {
      GCValues_t gcv;
      gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
                  kGCForeground | kGCBackground | kGCFont;
      gcv.fLineStyle  = kLineOnOffDash;
      gcv.fLineWidth  = 0;
      gcv.fFillStyle  = kFillSolid;
      gcv.fFont       = fgDefaultFont->GetFontHandle();
      gcv.fBackground = fgWhitePixel;
      gcv.fForeground = GetGrayPixel();
      fgLineGC = gClient->GetGC(&gcv, kTRUE);
      fgLineGC->SetDashOffset(0);
      fgLineGC->SetDashList("\x1\x1", 2);
   }
   return *fgLineGC;
}
const TGGC &TGListTree::GetHighlightGC()
{
   
   if (!fgHighlightGC) {
      GCValues_t gcv;
      gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
                  kGCForeground | kGCBackground | kGCFont;
      gcv.fLineStyle  = kLineSolid;
      gcv.fLineWidth  = 0;
      gcv.fFillStyle  = kFillSolid;
      gcv.fFont       = fgDefaultFont->GetFontHandle();
      gcv.fBackground = fgDefaultSelectedBackground;
      gcv.fForeground = fgWhitePixel;
      fgHighlightGC = gClient->GetGC(&gcv, kTRUE);
   }
   return *fgHighlightGC;
}
const TGGC &TGListTree::GetColorGC()
{
   
   if (!fgColorGC) {
      GCValues_t gcv;
      gcv.fMask = kGCLineStyle  | kGCLineWidth  | kGCFillStyle |
                  kGCForeground | kGCBackground;
      gcv.fLineStyle  = kLineSolid;
      gcv.fLineWidth  = 1;
      gcv.fFillStyle  = kFillSolid;
      gcv.fBackground = fgDefaultSelectedBackground;
      gcv.fForeground = fgWhitePixel;
      fgColorGC = gClient->GetGC(&gcv, kTRUE);
   }
   return *fgColorGC;
}
void TGListTree::SavePrimitive(ostream &out, Option_t *option )
{
   
   if (fBackground != GetWhitePixel()) SaveUserColor(out, option);
   out << endl << "   // list tree" << endl;
   out << "   TGListTree *";
   if ((fParent->GetParent())->InheritsFrom(TGCanvas::Class())) {
      out << GetName() << " = new TGListTree(" << GetCanvas()->GetName();
   } else {
      out << GetName() << " = new TGListTree(" << fParent->GetName();
      out << "," << GetWidth() << "," << GetHeight();
   }
   if (fBackground == GetWhitePixel()) {
      if (GetOptions() == kSunkenFrame) {
         out <<");" << endl;
      } else {
         out << "," << GetOptionString() <<");" << endl;
      }
   } else {
      out << "," << GetOptionString() << ",ucolor);" << endl;
   }
   out << endl;
   static Int_t n = 0;
   TGListTreeItem *current;
   current = GetFirstItem();
   if (current->fOpenPic)
      out << "   const TGPicture *popen;       //used for list tree items" << endl;
   if (current->fClosedPic)
      out << "   const TGPicture *pclose;      //used for list tree items" << endl;
   out << endl;
   while (current) {
      out << "   TGListTreeItem *item" << n << " = " << GetName() << "->AddItem(";
      current->SavePrimitive(out, Form("%d",n), n);
      if (current->IsOpen())
         out << "   " << GetName() << "->OpenItem(item" << n << ");" << endl;
      else
         out << "   " << GetName() << "->CloseItem(item" << n << ");" << endl;
      if (current == fSelected)
         out << "   " << GetName() << "->SetSelected(item" << n << ");" << endl;
      n++;
      if (current->fFirstchild) {
         SaveChildren(out, current->fFirstchild, n);
      }
      current = current->fNextsibling;
   }
   out << endl;
}
void TGListTree::SaveChildren(ostream &out, TGListTreeItem *item, Int_t &n)
{
   
   Int_t p = n-1;
   while (item) {
      out << "   TGListTreeItem *item" << n << " = " << GetName() << "->AddItem(";
      item->SavePrimitive(out, Form("%d",p),n);
      n++;
      if (item->fFirstchild) {
         SaveChildren(out, item->fFirstchild, n);
      }
      item = item->fNextsibling;
   }
}
void TGListTreeItem::SavePrimitive(ostream &out, Option_t *option, Int_t n)
{
   
   static const TGPicture *oldopen=0;
   static const TGPicture *oldclose=0;
   static const TGPicture *oldcheck=0;
   static const TGPicture *olduncheck=0;
   static Bool_t makecheck = kTRUE;
   static Bool_t makeuncheck = kTRUE;
   static Color_t oldcolor = -1;
   char quote = '"';
   TString s = Form("%d",n);
   if (!fParent)
      out << "NULL,";
   else
      out << "item" << option << ",";
   out << quote << GetText() << quote;
   out << ");" << endl;
   if (oldopen != fOpenPic) {
      oldopen = fOpenPic;
      out << "   popen = gClient->GetPicture(" << quote
          << gSystem->ExpandPathName(gSystem->UnixPathName(fOpenPic->GetName()))
          << quote << ");" << endl;
   }
   if (oldclose != fClosedPic) {
      oldclose = fClosedPic;
      out << "   pclose = gClient->GetPicture(" << quote
          << gSystem->ExpandPathName(gSystem->UnixPathName(fClosedPic->GetName()))
          << quote << ");" << endl;
   }
   out << "   item" << s.Data() << "->SetPictures(popen, pclose);" << endl;
   if (HasCheckBox()) {
      if (fCheckedPic && makecheck) {
         out << "   const TGPicture *pcheck;        //used for checked items" << endl;
         makecheck = kFALSE;
      }
      if (fUncheckedPic && makeuncheck) {
         out << "   const TGPicture *puncheck;      //used for unchecked items" << endl;
         makeuncheck = kFALSE;
      }
      out << "   item" << s.Data() << "->CheckItem();" << endl;
      if (oldcheck != fCheckedPic) {
         oldcheck = fCheckedPic;
         out << "   pcheck = gClient->GetPicture(" << quote
             << gSystem->ExpandPathName(gSystem->UnixPathName(fCheckedPic->GetName()))
             << quote << ");" << endl;
      }
      if (olduncheck != fUncheckedPic) {
         olduncheck = fUncheckedPic;
         out << "   puncheck = gClient->GetPicture(" << quote
             << gSystem->ExpandPathName(gSystem->UnixPathName(fUncheckedPic->GetName()))
             << quote << ");" << endl;
      }
      out << "   item" << s.Data() << "->SetCheckBoxPictures(pcheck, puncheck);" << endl;
      out << "   item" << s.Data() << "->SetCheckBox(kTRUE);" << endl;
   }
   if (fHasColor) {
      if (oldcolor != fColor) {
         oldcolor = fColor;
         out << "   item" << s.Data() << "->SetColor(" << fColor << ");" << endl;
      }
   }
   if (fTipText.Length() > 0) {
      out << "   item" << s.Data() << "->SetTipText(" << quote
          << GetTipText() << quote << ");" << endl;
   }
}
void TGListTree::CheckItem(TGListTreeItem *item, Bool_t check)
{
   
   item->CheckItem(check);
}
void TGListTree::SetCheckBox(TGListTreeItem *item, Bool_t on)
{
   
   item->SetCheckBox(on);
}
void TGListTree::ToggleItem(TGListTreeItem *item)
{
   
   item->Toggle();
}
void TGListTree::UpdateChecked(TGListTreeItem *item, Bool_t redraw)
{
   
   
   if (fAutoCheckBoxPic == kFALSE) return;
   Bool_t diff = kFALSE;
   TGListTreeItem *current = item;
   if (item->GetParent()) {
      current = item->GetParent()->GetFirstChild();
   }
   while (current) {
      TGListTreeItem *parent = current->GetParent();
      if ((parent) && (parent->HasCheckBox())) {
         if ( ((parent->IsChecked()) && (!current->IsChecked())) ||
              ((!parent->IsChecked()) && (current->IsChecked())) ) {
            diff = kTRUE;
            break;
         }
      }
      current = current->fNextsibling;
   }
   if ((item->GetParent()) && (item->GetParent()->HasCheckBox())) {
      if (diff) {
         item->GetParent()->SetCheckBoxPictures(fClient->GetPicture("checked_dis_t.xpm"),
                                                fClient->GetPicture("unchecked_dis_t.xpm"));
      }
      else {
         item->GetParent()->SetCheckBoxPictures(fClient->GetPicture("checked_t.xpm"),
                                                fClient->GetPicture("unchecked_t.xpm"));
      }
   }
   diff = kFALSE;
   current = item->GetFirstChild();
   while (current) {
      if (current->HasCheckBox()) {
         if ( ((current->IsChecked()) && (!item->IsChecked())) ||
              ((!current->IsChecked()) && (item->IsChecked())) ) {
            diff = kTRUE;
            break;
         }
      }
      current = current->GetNextSibling();
   }
   if (diff) {
      item->SetCheckBoxPictures(fClient->GetPicture("checked_dis_t.xpm"),
                                fClient->GetPicture("unchecked_dis_t.xpm"));
   }
   else {
      item->SetCheckBoxPictures(fClient->GetPicture("checked_t.xpm"),
                                fClient->GetPicture("unchecked_t.xpm"));
   }
   if (redraw)
      fClient->NeedRedraw(this);
}
TGListTreeItem *TGListTree::FindItemByObj(TGListTreeItem *item, void *ptr)
{
   
   
   TGListTreeItem *fitem;
   if (item && ptr) {
      if (item->fUserData == ptr)
         return item;
      else {
         if (item->fFirstchild) {
            fitem = FindItemByObj(item->fFirstchild,  ptr);
            if (fitem) return fitem;
         }
         return FindItemByObj(item->fNextsibling, ptr);
      }
   }
   return 0;
}
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.