XSElementList.cxx

Go to the documentation of this file.
00001 /*
00002  * $Header$
00003  * $Log$
00004  *
00005  * Fills a listbox with the elements sorted, by name, mnemonic, or Z
00006  */
00007 
00008 #include <ctype.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #include <TGFrame.h>
00013 #include <TGLayout.h>
00014 #include <TGWidget.h>
00015 #include <TGWindow.h>
00016 #include <TGPicture.h>
00017 
00018 #include "XSVarious.h"
00019 #include "XSElementList.h"
00020 
00021 //ClassImp(XSElementList)
00022 
00023 /* ----- XSElementList ----- */
00024 XSElementList::XSElementList(TGWindow* p, Int_t sortby)
00025         : TGListBox(p,50,50)
00026 {
00027         UInt_t  i;
00028         int     *index = new int[XSelements->GetSize()];
00029 
00030         sortBy = sortby;
00031 
00032         for (i=0; i<XSelements->GetSize(); i++)
00033                 index[i] = i;
00034 
00035         Sort(index);
00036 
00037         for (i=0; i<XSelements->GetSize(); i++) {
00038                 char    str[100];
00039                 Int_t   Z = ElementString(index[i]+1, str);
00040                 TGString        *tgstr = new TGString(str);
00041                 TGTextLBEntry   *lbe = new TGTextLBEntry(fLbc,tgstr,Z,
00042                                                 fixedGC,fixedFontStruct);
00043 
00044                 TGLayoutHints   *lhints = new TGLayoutHints(
00045                                         kLHintsExpandX | kLHintsTop);
00046 
00047                 fItemVsize = TMath::Max(fItemVsize, lbe->GetDefaultHeight());
00048 
00049                 AddEntry((TGLBEntry*)lbe, lhints);
00050         }
00051 
00052         delete [] index;
00053 } // XSElementList
00054 
00055 /* ----- ~XSElementList ----- */
00056 XSElementList::~XSElementList()
00057 {
00058 } // ~XSElementList
00059 
00060 /* ----- Compare ----- */
00061 Int_t
00062 XSElementList::Compare(int i, int j) const
00063 {
00064         switch (sortBy) {
00065                 case XSEL_SORTBY_NAME:
00066                         return strcmp(XSelements->Name(i+1),
00067                                         XSelements->Name(j+1));
00068 
00069                 case XSEL_SORTBY_MNEMONIC:
00070                         return strcmp(XSelements->Mnemonic(i+1),
00071                                         XSelements->Mnemonic(j+1));
00072 
00073                 case XSEL_SORTBY_Z:
00074                         return  (i<j? -1 : i>j? 1 : 0);
00075 
00076                 default:
00077                         return 0;
00078         }
00079 } // Compare
00080 
00081 /* ----- Sort ----- */
00082 void
00083 XSElementList::Sort(int *index)
00084 {
00085         // --- Perform a bubble sort ---
00086         for (UInt_t i=0; i<XSelements->GetSize(); i++) {
00087                 int change = 0;
00088                 for (UInt_t j=XSelements->GetSize()-1; j>i; j--) {
00089                         if (Compare(index[j],index[j-1]) < 0) {
00090                                 int t      = index[j];
00091                                 index[j]   = index[j-1];
00092                                 index[j-1] = t;
00093                                 change     = 1;
00094                         }
00095                 }
00096                 if (!change)
00097                         return;
00098         }
00099 } // Sort
00100 
00101 /* ----- ElementString ----- */
00102 Int_t
00103 XSElementList::ElementString(Int_t Z, char *buf)
00104 {
00105         switch (sortBy) {
00106                 case XSEL_SORTBY_NAME:
00107                         sprintf(buf,"%-20s %-4s %d",
00108                                 XSelements->Name(Z),
00109                                 XSelements->Mnemonic(Z),
00110                                 Z);
00111                         break;
00112 
00113                 case XSEL_SORTBY_MNEMONIC:
00114                         sprintf(buf,"%-4s %-20s %d",
00115                                 XSelements->Mnemonic(Z),
00116                                 XSelements->Name(Z),
00117                                 Z);
00118                         break;
00119 
00120                 case XSEL_SORTBY_Z:
00121                         sprintf(buf,"%3d   %-4s %-20s",
00122                                 Z,
00123                                 XSelements->Mnemonic(Z),
00124                                 XSelements->Name(Z));
00125                         break;
00126         }
00127         return Z;
00128 } // ElementString
00129 
00130 /* ----- GetZ ------- */
00131 UInt_t
00132 XSElementList::GetZ( TGTextLBEntry *entry )
00133 {
00134         if (entry == NULL) return 0;
00135 
00136         const TGString  *txt = entry->GetText();
00137         if (txt == NULL) return 0;
00138 
00139         const char      *str = txt->GetString();
00140         if (str == NULL) return 0;
00141 
00142         // Search the string for the Z
00143         while (*str && !isdigit(*str))
00144                 str++;
00145         return atol(str);
00146 } // GetZ
00147 
00148 /* ----- SelectZ ----- */
00149 void
00150 XSElementList::SelectZ( UInt_t Z )
00151 {
00152         TGTextLBEntry   *f;
00153         TGFrameElement  *el;
00154 
00155         if (Z<1 || Z>XSelements->GetSize()) return;
00156 
00157         TGLBContainer *ct = (TGLBContainer *) fVport->GetContainer();
00158 
00159         TIter   next(ct->GetList());
00160         while ((el = (TGFrameElement *) next())) {
00161                 f = (TGTextLBEntry *) el->fFrame;
00162                 if (GetZ(f) == Z) {
00163                         f->Activate(kTRUE);
00164                         return;
00165                 }
00166         }
00167         return;
00168 } // SelectZ
00169 
00170 /* ----- CurrentZ ----- */
00171 UInt_t
00172 XSElementList::CurrentZ( )
00173 {
00174         TGTextLBEntry   *entry = (TGTextLBEntry*)(GetSelectedEntry());
00175         return  GetZ(entry);
00176 } // CurrentZ

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