TTVLVContainer.cxx

Go to the documentation of this file.
00001 // @(#)root/treeviewer:$Id: TTVLVContainer.cxx 35908 2010-09-30 11:07:53Z brun $
00002 //Author : Andrei Gheata   16/08/00
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 #include "TTVLVContainer.h"
00013 #include "TTreeViewer.h"
00014 #include "TGPicture.h"
00015 #include "TGLabel.h"
00016 #include "TGButton.h"
00017 #include "TGTextEntry.h"
00018 #include "TGToolTip.h"
00019 #include "TList.h"
00020 
00021 
00022 ClassImp(TGItemContext)
00023 
00024 //////////////////////////////////////////////////////////////////////////
00025 // TGItemContext  -  empty object used as context menu support for      //
00026 // TGLVTreeEntries                                                      //
00027 //////////////////////////////////////////////////////////////////////////
00028 
00029 //______________________________________________________________________________
00030 TGItemContext::TGItemContext()
00031 {
00032    // Constructor
00033 
00034    fItem = 0;
00035 }
00036 //______________________________________________________________________________
00037 void TGItemContext::Draw(Option_t *)
00038 {
00039    // Draw item
00040 
00041    fItem->GetContainer()->GetViewer()->ProcessMessage(MK_MSG(kC_CONTAINER, kCT_ITEMDBLCLICK), kButton1, 0);
00042 }
00043 //______________________________________________________________________________
00044 void TGItemContext::EditExpression()
00045 {
00046    // Edit expression
00047 
00048    fItem->GetContainer()->GetViewer()->EditExpression();
00049 }
00050 //______________________________________________________________________________
00051 void TGItemContext::Empty()
00052 {
00053    // Empty item
00054 
00055    fItem->Empty();
00056 }
00057 //______________________________________________________________________________
00058 void TGItemContext::RemoveItem()
00059 {
00060    // Remove item
00061 
00062    fItem->GetContainer()->GetViewer()->RemoveItem();
00063 }
00064 //______________________________________________________________________________
00065 void TGItemContext::Scan()
00066 {
00067    // Scan item
00068 
00069    fItem->GetContainer()->GetViewer()->SetScanMode();
00070    fItem->GetContainer()->GetViewer()->ProcessMessage(MK_MSG(kC_CONTAINER, kCT_ITEMDBLCLICK), kButton1, 0);
00071 }
00072 //______________________________________________________________________________
00073 void TGItemContext::SetExpression(const char *name, const char *alias, Bool_t cut)
00074 {
00075    // Set item expression
00076 
00077    fItem->SetExpression(name, alias, cut);
00078 }
00079 
00080 ClassImp(TTVLVEntry)
00081 
00082 
00083 //////////////////////////////////////////////////////////////////////////
00084 //                                                                      //
00085 // TGTreeLVEntry is a TGLVEntry that has a name of a variable to be     //
00086 // draw by the TTreeViewer GUI, and an alias for it.                    //
00087 //                                                                      //
00088 //////////////////////////////////////////////////////////////////////////
00089 
00090 //______________________________________________________________________________
00091 TTVLVEntry::TTVLVEntry(const TGWindow *p,
00092                              const TGPicture *bigpic, const TGPicture *smallpic,
00093                              TGString *name, TGString **subnames,
00094                              EListViewMode ViewMode)
00095               :TGLVEntry(p, bigpic, smallpic, name, subnames, ViewMode)
00096 {
00097    // TGTreeLVEntry constructor.
00098 
00099    // both alias and true name are initialized to name
00100    fTrueName.InitialCapacity(1000);
00101    fContainer = (TTVLVContainer *) p;
00102 
00103    fTip = 0;
00104    fIsCut = kFALSE;
00105    fTrueName = name->GetString();
00106    fContext = new TGItemContext();
00107    fContext->Associate(this);
00108 
00109    AddInput(kEnterWindowMask | kLeaveWindowMask);
00110 }
00111 //______________________________________________________________________________
00112 TTVLVEntry::~TTVLVEntry()
00113 {
00114    // TGTreeLVEntry destructor
00115 
00116    if (fTip) delete fTip;
00117    delete fContext;
00118 }
00119 //______________________________________________________________________________
00120 const char *TTVLVEntry::ConvertAliases()
00121 {
00122    // Convert all aliases into true names
00123 
00124    TList *list = GetContainer()->GetViewer()->ExpressionList();
00125    fConvName = fTrueName;
00126    TString start(fConvName);
00127    TIter next(list);
00128    TTVLVEntry* item;
00129    while (!FullConverted()) {
00130       next.Reset();
00131       start = fConvName;
00132       while ((item=(TTVLVEntry*)next())) {
00133          if (item != this)
00134             fConvName.ReplaceAll(item->GetAlias(), item->GetTrueName());
00135       }
00136       if (fConvName == start) {
00137          //the following line is deadcode reported by coverity because item=0
00138          //if (item) Warning(item->GetAlias(), "Cannot convert aliases for this expression.");
00139          return(fConvName.Data());
00140       }
00141    }
00142    return(fConvName.Data());
00143 }
00144 //______________________________________________________________________________
00145 Bool_t TTVLVEntry::FullConverted()
00146 {
00147    // Return true if converted name is alias free
00148 
00149    TList *list = GetContainer()->GetViewer()->ExpressionList();
00150    TIter next(list);
00151    TTVLVEntry* item;
00152    while ((item=(TTVLVEntry*)next())) {
00153       if (item != this) {
00154          if (fConvName.Contains(item->GetAlias())) return kFALSE;
00155       }
00156    }
00157    return kTRUE;
00158 }
00159 //______________________________________________________________________________
00160 void TTVLVEntry::CopyItem(TTVLVEntry *dest)
00161 {
00162    // Copy this item's name and alias to an other.
00163 
00164    if (!dest) return;
00165    dest->SetExpression(fTrueName.Data(), fAlias.Data(), fIsCut);
00166    TString alias = dest->GetAlias();
00167    if (!alias.BeginsWith("~") && !alias.Contains("empty")) dest->PrependTilde();
00168 }
00169 //______________________________________________________________________________
00170 Bool_t TTVLVEntry::HandleCrossing(Event_t *event)
00171 {
00172    // Handle mouse crossing event.
00173 
00174    if (fTip) {
00175       if (event->fType == kEnterNotify)
00176          fTip->Reset();
00177       else
00178          fTip->Hide();
00179    }
00180    return kTRUE;
00181 }
00182 //______________________________________________________________________________
00183 Bool_t TTVLVEntry::HasAlias()
00184 {
00185    // Check if alias name is not empty.
00186 
00187    if (fAlias.Length()) return kTRUE;
00188    return kFALSE;
00189 }
00190 //______________________________________________________________________________
00191 void TTVLVEntry::PrependTilde()
00192 {
00193    // Prepend a ~ to item alias
00194 
00195    fAlias = "~" + fAlias;
00196    SetItemName(fAlias.Data());
00197 }
00198 //______________________________________________________________________________
00199 void TTVLVEntry::SetItemName(const char* name)
00200 {
00201    // redraw this entry with new name
00202 
00203    if (fItemName) delete fItemName;
00204    fItemName = new TGString(name);
00205    Int_t max_ascent, max_descent;
00206    fTWidth = gVirtualX->TextWidth(fFontStruct, fItemName->GetString(), fItemName->GetLength());
00207    gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
00208    fTHeight = max_ascent + max_descent;
00209    gVirtualX->ClearWindow(fId);
00210    Resize(GetDefaultSize());
00211    fClient->NeedRedraw(this);
00212 }
00213 //______________________________________________________________________________
00214 void TTVLVEntry::SetCutType(Bool_t type)
00215 {
00216    // Set cut type
00217 
00218    if (fIsCut && type) return;
00219    if (!fIsCut && !type) return;
00220    if (type) {
00221       SetSmallPic(fClient->GetPicture("selection_t.xpm"));
00222       SetToolTipText("Selection expression. Drag to scissors to activate");
00223    } else
00224       SetSmallPic(fClient->GetPicture("expression_t.xpm"));
00225    fIsCut = type;
00226 }
00227 //______________________________________________________________________________
00228 void TTVLVEntry::SetExpression(const char* name, const char* alias, Bool_t cutType)
00229 {
00230    // Set the true name, alias and type of the expression, then refresh it
00231 
00232    SetItemName(alias);
00233    SetAlias(alias);
00234    SetTrueName(name);
00235    ULong_t *itemType = (ULong_t *) GetUserData();
00236    if (*itemType & TTreeViewer::kLTPackType) {
00237       if (strlen(name))
00238          SetSmallPic(fClient->GetPicture("pack_t.xpm"));
00239       else
00240          SetSmallPic(fClient->GetPicture("pack-empty_t.xpm"));
00241    }
00242    if ((*itemType & TTreeViewer::kLTDragType) && strlen(name) && !fIsCut)
00243       SetToolTipText("Double-click to draw. Drag and drop. Use Edit/Expression or context menu to edit.");
00244    if (*itemType & TTreeViewer::kLTDragType) SetCutType(cutType);
00245 }
00246 //______________________________________________________________________________
00247 void TTVLVEntry::Empty()
00248 {
00249    // clear all names and alias
00250 
00251    SetExpression("","-empty-");
00252    ULong_t *itemType = (ULong_t *) GetUserData();
00253    if (itemType && (*itemType & TTreeViewer::kLTDragType))
00254       SetToolTipText("User-defined expression/cut. Double-click to edit");
00255 }
00256 //______________________________________________________________________________
00257 void TTVLVEntry::SetToolTipText(const char *text, Long_t delayms)
00258 {
00259    // Set tool tip text associated with this item. The delay is in
00260    // milliseconds (minimum 250). To remove tool tip call method with text = 0
00261 
00262    if (fTip) {
00263       delete fTip;
00264       fTip = 0;
00265    }
00266 
00267    if (text && strlen(text))
00268       fTip = new TGToolTip(fClient->GetRoot(), this, text, delayms);
00269 }
00270 //______________________________________________________________________________
00271 void TTVLVEntry::SetSmallPic(const TGPicture *spic)
00272 {
00273    // Set small picture
00274 
00275    const TGPicture *cspic = fSmallPic;
00276    fSmallPic = spic;
00277    fCurrent = fSmallPic;
00278    if (fSelPic) delete fSelPic;
00279    fSelPic = 0;
00280    if (fActive) {
00281       fSelPic = new TGSelectedPicture(fClient, fCurrent);
00282    }
00283    DoRedraw();
00284    fClient->FreePicture(cspic);
00285 }
00286 
00287 ClassImp(TTVLVContainer)
00288 
00289 //////////////////////////////////////////////////////////////////////////
00290 //                                                                      //
00291 //   TTVLVContainer                                                     //
00292 //                                                                      //
00293 // This class represent the list view container for the.                //
00294 // TreeView class. It is a TGLVContainer with item dragging             //
00295 // capabilities for the TTVLVEntry objects inside.                      //
00296 //                                                                      //
00297 //////////////////////////////////////////////////////////////////////////
00298 
00299 //______________________________________________________________________________
00300 TTVLVContainer::TTVLVContainer(const TGWindow *p, UInt_t w, UInt_t h, UInt_t options)
00301           :TGLVContainer(p, w, h,options | kSunkenFrame)
00302 {
00303    // TGLVContainer constructor
00304 
00305    fListView = 0;
00306    fViewer = 0;
00307    fExpressionList = new TList;
00308    fCursor = gVirtualX->CreateCursor(kMove);
00309    fDefaultCursor = gVirtualX->CreateCursor(kPointer);
00310    fMapSubwindows = kTRUE;
00311 }
00312 
00313 //______________________________________________________________________________
00314 TTVLVContainer::~TTVLVContainer()
00315 {
00316    // TGLVContainer destructor
00317 
00318    delete fExpressionList;
00319 }
00320 
00321 //______________________________________________________________________________
00322 const char* TTVLVContainer::Cut()
00323 {
00324    // return the cut entry
00325 
00326    TGFrameElement *el = (TGFrameElement *) fList->At(3);
00327    if (el) {
00328       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00329       if (f) return f->ConvertAliases();
00330       return 0;
00331    }
00332    return 0;
00333 }
00334 
00335 //______________________________________________________________________________
00336 TTVLVEntry * TTVLVContainer::ExpressionItem(Int_t index)
00337 {
00338    // return the expression item at specific position
00339 
00340    TGFrameElement *el = (TGFrameElement *) fList->At(index);
00341    if (el) {
00342       TTVLVEntry *item = (TTVLVEntry *) el->fFrame;
00343       return item;
00344    }
00345    return 0;
00346 }
00347 
00348 //______________________________________________________________________________
00349 TList* TTVLVContainer::ExpressionList()
00350 {
00351    // return the list of user-defined expressions
00352 
00353    fExpressionList->Clear();
00354    TIter next(fList);
00355    TGFrameElement *el;
00356    while ((el = (TGFrameElement*)next())) {
00357       TTVLVEntry *item = (TTVLVEntry *)el->fFrame;
00358       if (item) {
00359          ULong_t *itemType = (ULong_t *) item->GetUserData();
00360          if ((*itemType & TTreeViewer::kLTExpressionType) &&
00361             (*itemType & TTreeViewer::kLTDragType)) fExpressionList->Add(item);
00362       }
00363    }
00364    return fExpressionList;
00365 }
00366 
00367 //______________________________________________________________________________
00368 const char* TTVLVContainer::Ex()
00369 {
00370    // return the expression on X
00371 
00372    TGFrameElement *el = (TGFrameElement *) fList->At(0);
00373    if (el) {
00374       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00375       if (f) return f->ConvertAliases();
00376       return 0;
00377    }
00378    return 0;
00379 }
00380 
00381 //______________________________________________________________________________
00382 const char* TTVLVContainer::Ey()
00383 {
00384    // return the expression on Y
00385 
00386    TGFrameElement *el = (TGFrameElement *) fList->At(1);
00387    if (el) {
00388       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00389       if (f) return f->ConvertAliases();
00390       return 0;
00391    }
00392    return 0;
00393 }
00394 
00395 //______________________________________________________________________________
00396 const char* TTVLVContainer::Ez()
00397 {
00398    // return the expression on Z
00399 
00400    TGFrameElement *el = (TGFrameElement *) fList->At(2);
00401    if (el) {
00402       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00403       if (f) return f->ConvertAliases();
00404       return 0;
00405    }
00406    return 0;
00407 }
00408 
00409 //______________________________________________________________________________
00410 const char* TTVLVContainer::ScanList()
00411 {
00412    // return the cut entry
00413 
00414    TGFrameElement *el = (TGFrameElement *) fList->At(4);
00415    if (el) {
00416       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00417       if (f) return f->GetTrueName();
00418       return 0;
00419    }
00420    return 0;
00421 }
00422 
00423 //______________________________________________________________________________
00424 Bool_t TTVLVContainer::HandleButton(Event_t *event)
00425 {
00426    // Handle mouse button event in container.
00427 
00428    int total, selected;
00429 
00430    if (event->fType == kButtonPress) {
00431       fXp = event->fX;
00432       fYp = event->fY;
00433       if (fLastActive) {
00434          fLastActive->Activate(kFALSE);
00435          fLastActive = 0;
00436       }
00437       total = selected = 0;
00438 
00439       TGFrameElement *el;
00440       TIter next(fList);
00441       while ((el = (TGFrameElement *) next())) {
00442          TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00443          ++total;
00444          if (f->GetId() == (Window_t)event->fUser[0]) {  // fUser[0] = subwindow
00445             f->Activate(kTRUE);
00446             if (f->GetTip()) (f->GetTip())->Hide();
00447             fX0 = f->GetX();
00448             fY0 = f->GetY();
00449             ++selected;
00450             fLastActive = f;
00451          } else {
00452             f->Activate(kFALSE);
00453          }
00454       }
00455 
00456       if (fTotal != total || fSelected != selected) {
00457          fTotal = total;
00458          fSelected = selected;
00459          SendMessage(fMsgWindow, MK_MSG(kC_CONTAINER, kCT_SELCHANGED),
00460                      fTotal, fSelected);
00461       }
00462 
00463       if (selected == 1 && event->fCode == 1) {
00464          ULong_t *itemType = (ULong_t *) fLastActive->GetUserData();
00465          if (*itemType & TTreeViewer::kLTDragType) {
00466             fDragging = kTRUE;
00467             gVirtualX->SetCursor(fId,fCursor);
00468             fXp = event->fX;
00469             fYp = event->fY;
00470          }
00471       }
00472    }
00473 
00474    if (event->fType == kButtonRelease) {
00475       if (fDragging) {
00476          fDragging = kFALSE;
00477          gVirtualX->SetCursor(fId,fDefaultCursor);
00478          fLastActive->Move(fX0,fY0);
00479          TGFrameElement *el;
00480          TIter next(fList);
00481          while ((el = (TGFrameElement *) next())) {
00482             TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00483             if ((f == fLastActive) || !f->IsActive()) continue;
00484             ULong_t *itemType = (ULong_t *) f->GetUserData();
00485             fLastActive->Activate(kFALSE);
00486             if (!(*itemType & TTreeViewer::kLTPackType)) {
00487                // dragging items to expressions
00488                ((TTVLVEntry *) fLastActive)->CopyItem(f);
00489                if (*itemType & TTreeViewer::kLTDragType)
00490                   f->SetToolTipText("Double-click to draw. Drag and drop. Use Edit/Expression or context menu to edit.");
00491             } else {
00492                if (strlen(((TTVLVEntry *) fLastActive)->GetTrueName())) {
00493                   // dragging to scan box
00494                   if (!strlen(f->GetTrueName())) {
00495                      f->SetTrueName(((TTVLVEntry *)fLastActive)->GetTrueName());
00496                      f->SetSmallPic(fClient->GetPicture("pack_t.xpm"));
00497                   } else {
00498                      TString name(2000);
00499                      TString dragged = ((TTVLVEntry *)fLastActive)->ConvertAliases();
00500                      name  = f->GetTrueName();
00501                      if ((name.Length()+dragged.Length()) < 228) {
00502                         name += ":";
00503                         name += dragged;
00504                         f->SetTrueName(name.Data());
00505                      } else {
00506                         Warning("HandleButton",
00507                                 "Name too long. Can not add any more items to scan box.");
00508                      }
00509                   }
00510                }
00511             }
00512             fLastActive = f;
00513             if (fViewer) {
00514                char msg[2000];
00515                msg[0] = 0;
00516                snprintf(msg,2000, "Content : %s", f->GetTrueName());
00517                fViewer->Message(msg);
00518             }
00519          }
00520          if ((TMath::Abs(event->fX - fXp) < 2) && (TMath::Abs(event->fY - fYp) < 2)) {
00521             SendMessage(fMsgWindow, MK_MSG(kC_CONTAINER, kCT_ITEMCLICK),
00522                         event->fCode, (event->fYRoot << 16) | event->fXRoot);
00523          }
00524       } else {
00525          SendMessage(fMsgWindow, MK_MSG(kC_CONTAINER, kCT_ITEMCLICK),
00526                      event->fCode, (event->fYRoot << 16) | event->fXRoot);
00527       }
00528    }
00529    return kTRUE;
00530 }
00531 
00532 //______________________________________________________________________________
00533 Bool_t TTVLVContainer::HandleMotion(Event_t *event)
00534 {
00535    // Handle mouse motion events.
00536 
00537    Int_t xf0, xff, yf0, yff;
00538    Int_t xpos = event->fX - (fXp-fX0);
00539    Int_t ypos = event->fY - (fYp-fY0);
00540 
00541    if (fDragging) {
00542       TGFrameElement *el;
00543       ULong_t *itemType;
00544       TIter next(fList);
00545       while ((el = (TGFrameElement *) next())) {
00546          TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00547          if (f == fLastActive) {
00548             if (f->GetTip()) (f->GetTip())->Hide();
00549             continue;
00550          }
00551          xf0 = f->GetX();
00552          yf0 = f->GetY();
00553          xff = f->GetX() + f->GetWidth();
00554          yff = f->GetY() + f->GetHeight();
00555          itemType = (ULong_t *) f->GetUserData();
00556          if (*itemType & TTreeViewer::kLTExpressionType) {
00557             if (xpos>xf0 && xpos<xff && ypos>yf0 && ypos<yff) {
00558                f->Activate(kTRUE);
00559             } else {
00560                f->Activate(kFALSE);
00561             }
00562          }
00563       }
00564       if ((fXp - event->fX) > 10) {
00565          fListView->SetHsbPosition(0);
00566          fListView->SetVsbPosition(0);
00567       }
00568       fLastActive->Move(xpos, ypos);
00569       gVirtualX->RaiseWindow(fLastActive->GetId());
00570       SendMessage(fMsgWindow, MK_MSG(kC_CONTAINER,(EWidgetMessageTypes)4),event->fX, event->fY);
00571    }
00572    return kTRUE;
00573 }
00574 
00575 //______________________________________________________________________________
00576 void TTVLVContainer::EmptyAll()
00577 {
00578    // Clear all names and aliases for expression type items
00579 
00580    TGFrameElement *el;
00581    TIter next(fList);
00582    while ((el = (TGFrameElement *) next())) {
00583       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00584       UInt_t *userData = (UInt_t *) f->GetUserData();
00585       if (*userData & TTreeViewer::kLTExpressionType) {
00586          if (*userData & TTreeViewer::kLTPackType) {
00587             f->SetSmallPic(fClient->GetPicture("pack-empty_t.xpm"));
00588             f->SetTrueName("");
00589          } else {
00590             f->Empty();
00591          }
00592       }
00593    }
00594 }
00595 
00596 //______________________________________________________________________________
00597 void TTVLVContainer::RemoveNonStatic()
00598 {
00599    // remove all non-static items from the list view, except expressions
00600 
00601    TGFrameElement *el;
00602    TIter next(fList);
00603    while ((el = (TGFrameElement *) next())) {
00604       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00605       UInt_t *userData = (UInt_t *) f->GetUserData();
00606       if (!((*userData) & TTreeViewer::kLTExpressionType)) {
00607          RemoveItem(f);
00608       }
00609    }
00610    fLastActive = 0;
00611 }
00612 
00613 //______________________________________________________________________________
00614 void TTVLVContainer::SelectItem(const char* name)
00615 {
00616    // select an item
00617 
00618    if (fLastActive) {
00619       fLastActive->Activate(kFALSE);
00620       fLastActive = 0;
00621    }
00622    TGFrameElement *el;
00623    fSelected = 0;
00624    TIter next(fList);
00625    while ((el = (TGFrameElement *) next())) {
00626       TTVLVEntry *f = (TTVLVEntry *) el->fFrame;
00627       if (!strcmp(f->GetItemName()->GetString(),name)) {
00628          f->Activate(kTRUE);
00629          fLastActive = (TGLVEntry *) f;
00630          fSelected++;
00631       } else {
00632          f->Activate(kFALSE);
00633       }
00634    }
00635 }
00636 
00637 ClassImp(TGSelectBox)
00638 
00639 //////////////////////////////////////////////////////////////////////////
00640 //                                                                      //
00641 //   TGSelectBox                                                        //
00642 //                                                                      //
00643 // This class represent a specialized expression editor for             //
00644 // TTVLVEntry 'true name' and 'alias' data members.                     //
00645 // It is a singleton in order to be able to use it for several          //
00646 // expressions.                                                         //
00647 //                                                                      //
00648 //////////////////////////////////////////////////////////////////////////
00649 
00650 enum ETransientFrameCommands {
00651    kTFDone,
00652    kTFCancel
00653 };
00654 
00655 TGSelectBox* TGSelectBox::fgInstance = 0;
00656 
00657 //______________________________________________________________________________
00658 TGSelectBox::TGSelectBox(const TGWindow *p, const TGWindow *main,
00659                          UInt_t w, UInt_t h)
00660             :TGTransientFrame(p, main, w, h)
00661 {
00662    // TGSelectBox constructor
00663 
00664    if (!fgInstance) {
00665       fgInstance = this;
00666       fViewer = (TTreeViewer *)fMain;
00667       if (!fViewer) Error("TGSelectBox", "Must be started from viewer");
00668       fEntry = 0;
00669       fLayout = new TGLayoutHints(kLHintsTop | kLHintsCenterY | kLHintsExpandX, 0, 0, 0, 2);
00670       fBLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 2, 2, 2);
00671       fBLayout1= new TGLayoutHints(kLHintsTop | kLHintsRight, 2, 0, 2, 2);
00672 
00673       fLabel = new TGLabel(this, "");
00674       AddFrame(fLabel,fLayout);
00675 
00676       fTe = new TGTextEntry(this, new TGTextBuffer(2000));
00677       fTe->SetToolTipText("Type an expression using C++ syntax. Click other expression/leaves to paste them here.");
00678       AddFrame(fTe, fLayout);
00679 
00680       fLabelAlias = new TGLabel(this, "Alias");
00681       AddFrame(fLabelAlias,fLayout);
00682 
00683       fTeAlias = new TGTextEntry(this, new TGTextBuffer(100));
00684       fTeAlias->SetToolTipText("Define an alias for this expression. Do NOT use leading strings of other aliases.");
00685       AddFrame(fTeAlias, fLayout);
00686 
00687       fBf = new TGHorizontalFrame(this, 10, 10);
00688 
00689       fCANCEL = new TGTextButton(fBf, "&Cancel", kTFCancel);
00690       fCANCEL->Associate(this);
00691       fBf->AddFrame(fCANCEL, fBLayout);
00692 
00693       fDONE = new TGTextButton(fBf, "&Done", kTFDone);
00694       fDONE->Associate(this);
00695       fBf->AddFrame(fDONE, fBLayout1);
00696 
00697       AddFrame(fBf, fLayout);
00698 
00699       MapSubwindows();
00700       Resize(GetDefaultSize());
00701 
00702 //      SetBackgroundColor(color);
00703       Window_t wdum;
00704       Int_t ax, ay;
00705       gVirtualX->TranslateCoordinates(main->GetId(), GetParent()->GetId(), 25,
00706                         (Int_t)(((TGFrame *) main)->GetHeight() - fHeight) >> 1,
00707                         ax, ay, wdum);
00708       MoveResize(ax, ay, w, GetDefaultHeight());
00709       MapWindow();
00710    }
00711 }
00712 
00713 //______________________________________________________________________________
00714 TGSelectBox::~TGSelectBox()
00715 {
00716    // TGSelectBox destructor
00717 
00718    fgInstance = 0;
00719    delete fLabel;
00720    delete fTe;
00721    delete fLabelAlias;
00722    delete fTeAlias;
00723    delete fDONE;
00724    delete fCANCEL;
00725    delete fBf;
00726    delete fLayout;
00727    delete fBLayout;
00728    delete fBLayout1;
00729 }
00730 
00731 //______________________________________________________________________________
00732 void TGSelectBox::CloseWindow()
00733 {
00734    // close the select box
00735 
00736    gVirtualX->UnmapWindow(GetId());
00737    delete this;
00738 }
00739 
00740 //______________________________________________________________________________
00741 TGSelectBox * TGSelectBox::GetInstance()
00742 {
00743    // return the pointer to the instantiated singleton
00744 
00745    return fgInstance;
00746 }
00747 
00748 //______________________________________________________________________________
00749 void TGSelectBox::GrabPointer()
00750 {
00751    // just focus the cursor inside
00752 
00753    Event_t event;
00754    event.fType = kButtonPress;
00755    event.fCode = kButton1;
00756    Int_t position = fTe->GetCursorPosition();
00757    fTe->HandleButton(&event);
00758    fTe->SetCursorPosition(position);
00759 }
00760 
00761 //______________________________________________________________________________
00762 void TGSelectBox::SetLabel(const char* title)
00763 {
00764    // Set label of selection box
00765 
00766    fLabel->SetText(new TGString(title));
00767 }
00768 
00769 //______________________________________________________________________________
00770 void TGSelectBox::SaveText()
00771 {
00772    // save the edited entry true name and alias
00773 
00774    if (fEntry) {
00775 
00776       Bool_t cutType;
00777       TString name(fTe->GetText());
00778       if (name.Length())
00779          fEntry->SetToolTipText("Double-click to draw. Drag and drop. Use Edit/Expression or context menu to edit.");
00780       else
00781          fEntry->SetToolTipText("User-defined expression/cut. Double-click to edit");
00782       // Set type of item to "cut" if containing boolean operators
00783       cutType = name.Contains("<") || name.Contains(">") || name.Contains("=") ||
00784                 name.Contains("!") || name.Contains("&") || name.Contains("|");
00785       TString alias(fTeAlias->GetText());
00786       if (!alias.BeginsWith("~") && !alias.Contains("empty")) fTeAlias->InsertText("~", 0);
00787       fEntry->SetExpression(fTe->GetText(), fTeAlias->GetText(), cutType);
00788 
00789       if (fOldAlias.Contains("empty")) {
00790          fOldAlias = fTeAlias->GetText();
00791          return;
00792       }
00793       TList *list = fViewer->ExpressionList();
00794       TIter next(list);
00795       TTVLVEntry* item;
00796       while ((item=(TTVLVEntry*)next())) {
00797          if (item != fEntry) {
00798             name = item->GetTrueName();
00799             name.ReplaceAll(fOldAlias.Data(), fTeAlias->GetText());
00800             item->SetTrueName(name.Data());
00801          }
00802       }
00803    }
00804 }
00805 
00806 //______________________________________________________________________________
00807 void TGSelectBox::SetEntry(TTVLVEntry *entry)
00808 {
00809    // connect one entry
00810 
00811    fEntry = entry;
00812    fTe->SetText(entry->GetTrueName());
00813    fTeAlias->SetText(entry->GetAlias());
00814    fOldAlias = entry->GetAlias();
00815 }
00816 
00817 //______________________________________________________________________________
00818 void TGSelectBox::InsertText(const char* text)
00819 {
00820    // Insert text in text entry
00821 
00822    Int_t start = fTe->GetCursorPosition();
00823    fTe->InsertText(text, fTe->GetCursorPosition());
00824    fTe->SetCursorPosition(start+strlen(text));
00825 }
00826 
00827 //______________________________________________________________________________
00828 Bool_t TGSelectBox::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00829 {
00830    // Message interpreter
00831 
00832    switch (GET_MSG(msg)) {
00833       case kC_TEXTENTRY:
00834          switch (GET_SUBMSG(msg)) {
00835             case kTE_ENTER:
00836                if (ValidateAlias()) SaveText();
00837                break;
00838             default:
00839                break;
00840          }
00841          break;
00842       case kC_COMMAND:
00843          switch (GET_SUBMSG(msg)) {
00844             case kCM_BUTTON:
00845                switch (parm1) {
00846                   case kTFDone:
00847                      if (!ValidateAlias()) break;
00848                      SaveText();
00849                      CloseWindow();
00850                      break;
00851                   case kTFCancel:
00852                      CloseWindow();
00853                      break;
00854                   default:
00855                      break;
00856                }
00857                break;
00858             default:
00859                break;
00860          }
00861          break;
00862       default:
00863          if (parm2) break;       // just to avoid warning on CC compiler
00864          break;
00865    }
00866    return kTRUE;
00867 }
00868 
00869 //______________________________________________________________________________
00870 Bool_t TGSelectBox::ValidateAlias()
00871 {
00872    // return true if edited alias is not a leading string of other expression aliases
00873 
00874    if (!strcmp(fTeAlias->GetText(), "-empty-") || !strlen(fTeAlias->GetText())) {
00875       fViewer->Warning("ValidateAlias", "You should define the alias first.");
00876       return kFALSE;
00877    }
00878    TList *list = fViewer->ExpressionList();
00879    TIter next(list);
00880    TTVLVEntry* item;
00881    while ((item=(TTVLVEntry*)next())) {
00882       if (item != fEntry) {
00883          TString itemalias(item->GetAlias());
00884          if (itemalias.Contains(fTeAlias->GetText())) {
00885             fViewer->Warning("ValidAlias", "Alias can not be the leading string of other alias.");
00886             return kFALSE;
00887          }
00888       }
00889    }
00890    return kTRUE;
00891 }

Generated on Tue Jul 5 15:43:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1