TGShutter.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGShutter.cxx 35582 2010-09-22 13:38:27Z bellenot $
00002 // Author: Fons Rademakers   18/9/2000
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 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TGShutter, TGShutterItem                                             //
00015 //                                                                      //
00016 // A shutter widget contains a set of shutter items that can be         //
00017 // open and closed like a shutter.                                      //
00018 // This widget is usefull to group a large number of options in         //
00019 // a number of categories.                                              //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 
00023 #include "TGShutter.h"
00024 #include "TGButton.h"
00025 #include "TList.h"
00026 #include "TTimer.h"
00027 #include "Riostream.h"
00028 
00029 
00030 ClassImp(TGShutterItem)
00031 ClassImp(TGShutter)
00032 
00033 //______________________________________________________________________________
00034 TGShutter::TGShutter(const TGWindow *p, UInt_t options) :
00035    TGCompositeFrame(p, 10, 10, options)
00036 {
00037    // Create shutter frame.
00038 
00039    fSelectedItem        = 0;
00040    fClosingItem         = 0;
00041    fHeightIncrement     = 1;
00042    fClosingHeight       = 0;
00043    fClosingHadScrollbar = kFALSE;
00044    fTimer               = 0;
00045    fTrash               = new TList;
00046 
00047    fDefWidth = fDefHeight = 0;
00048 
00049    // layout manager is not used
00050    delete fLayoutManager;
00051    fLayoutManager = 0;
00052 }
00053 
00054 //______________________________________________________________________________
00055 TGShutter::~TGShutter()
00056 {
00057    // Cleanup shutter widget.
00058 
00059    if (fTimer) delete fTimer;
00060 
00061    if (!MustCleanup()) {
00062       fTrash->Delete();
00063    }
00064    delete fTrash;
00065    fTrash = 0;
00066 }
00067 
00068 //______________________________________________________________________________
00069 void TGShutter::AddItem(TGShutterItem *item)
00070 {
00071    // Add shutter item to shutter frame.
00072 
00073    TGLayoutHints *hints = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY);
00074    AddFrame(item, hints);
00075    fTrash->Add(hints);
00076    if (!fSelectedItem) {
00077       fSelectedItem = item;
00078    }
00079 }
00080 
00081 //______________________________________________________________________________
00082 void TGShutter::RemoveItem(const char *name)
00083 {
00084    // Remove item from shutter
00085 
00086    TGShutterItem *item = GetItem(name);
00087 
00088    if (!item) {
00089       return;
00090    }
00091 
00092    if (fList->GetEntries() <= 1) {
00093       return;
00094    }
00095 
00096    if (item == fSelectedItem) {
00097       TGFrameElement *fe = (TGFrameElement*)fList->FindObject(item->GetFrameElement());
00098       if (fe) {
00099          TGFrameElement *sel = (TGFrameElement*)fList->Before(fe);
00100          if (!sel) {
00101             sel = (TGFrameElement*)fList->After(fe);
00102          }
00103          if (!sel) {
00104             return;
00105          }
00106          SetSelectedItem((TGShutterItem*)sel->fFrame);
00107       }
00108    }
00109    RemoveFrame(item);
00110 
00111    item->DestroyWindow();
00112    delete item;
00113    Layout();
00114 }
00115 
00116 //______________________________________________________________________________
00117 void TGShutter::RemovePage()
00118 {
00119    // Remove selected page
00120 
00121    if (!fSelectedItem) {
00122       return;
00123    }
00124    TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
00125    RemoveItem(btn->GetString().Data());
00126 }
00127 
00128 //______________________________________________________________________________
00129 void TGShutter::RenamePage(const char *name)
00130 {
00131    // Rename selected page
00132 
00133    if (!fSelectedItem) {
00134       return;
00135    }
00136    TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
00137    btn->SetText(name);
00138 }
00139 
00140 //______________________________________________________________________________
00141 TGShutterItem *TGShutter::AddPage(const char *name)
00142 {
00143    // Add new page (shutter item)
00144 
00145    static int id = 1000;
00146    TGShutterItem *item = new TGShutterItem(this, new TGHotString(name), id++);
00147    AddItem(item);
00148    MapSubwindows();
00149    Layout();
00150    return item;
00151 }
00152 
00153 //______________________________________________________________________________
00154 Bool_t TGShutter::ProcessMessage(Long_t /*msg*/, Long_t parm1, Long_t /*parm2*/)
00155 {
00156    // Handle shutter messages.
00157 
00158    if (!fList) return kFALSE;
00159 
00160    TGFrameElement *el;
00161    TGShutterItem  *child, *item = 0;
00162 
00163    TIter next(fList);
00164    while ((el = (TGFrameElement *) next())) {
00165       child = (TGShutterItem *) el->fFrame;
00166       if (parm1 == child->WidgetId()) {
00167          item = child;
00168          break;
00169       }
00170    }
00171 
00172    if (!item) return kFALSE;
00173 
00174    if (!fSelectedItem)
00175       fSelectedItem = (TGShutterItem*) ((TGFrameElement*)fList->First())->fFrame;
00176    if (fSelectedItem == item) return kTRUE;
00177 
00178    fHeightIncrement = 1;
00179    fClosingItem = fSelectedItem;
00180    fClosingHeight = fClosingItem->GetHeight();
00181    fClosingHeight -= fClosingItem->fButton->GetDefaultHeight();
00182    fSelectedItem = item;
00183    Selected(fSelectedItem);
00184    fSelectedItem->Selected();
00185 
00186    if (!fTimer) fTimer = new TTimer(this, 6); //10);
00187    fTimer->Reset();
00188    fTimer->TurnOn();
00189 
00190    return kTRUE;
00191 }
00192 
00193 //______________________________________________________________________________
00194 Bool_t TGShutter::HandleTimer(TTimer *)
00195 {
00196    // Shutter item animation.
00197 
00198    if (!fClosingItem) return kFALSE;
00199    fClosingHeight -= fHeightIncrement;
00200    fHeightIncrement += 5;
00201    if (fClosingHeight > 0) {
00202       fTimer->Reset();
00203    } else {
00204       fClosingItem   = 0;
00205       fClosingHeight = 0;
00206       fTimer->TurnOff();
00207    }
00208    Layout();
00209 
00210    return kTRUE;
00211 }
00212 
00213 //______________________________________________________________________________
00214 void TGShutter::Layout()
00215 {
00216    // Layout shutter items.
00217 
00218    TGFrameElement *el;
00219    TGShutterItem  *child;
00220    Int_t y, bh, exh;
00221 
00222    if (!fList) return;
00223 
00224    if (!fSelectedItem)
00225       fSelectedItem = (TGShutterItem*) ((TGFrameElement*)GetList()->First())->fFrame;
00226 
00227    exh = Int_t(fHeight - (fBorderWidth << 1));
00228    TIter next(fList);
00229    while ((el = (TGFrameElement *) next())) {
00230       child = (TGShutterItem *) el->fFrame;
00231       bh = child->fButton->GetDefaultHeight();
00232       exh -= bh;
00233    }
00234 
00235    y = fBorderWidth;
00236    next.Reset();
00237    while ((el = (TGFrameElement *) next())) {
00238       child = (TGShutterItem *) el->fFrame;
00239       bh = child->fButton->GetDefaultHeight();
00240       if (child == fSelectedItem) {
00241          if (fClosingItem)
00242             child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
00243          else
00244             child->fCanvas->SetScrolling(TGCanvas::kCanvasScrollVertical);
00245          child->ShowFrame(child->fCanvas);
00246          child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
00247                            exh - fClosingHeight + bh);
00248          y += exh - fClosingHeight + bh;
00249       } else if (child == fClosingItem) {
00250          child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
00251          child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
00252                            fClosingHeight + bh);
00253          y += fClosingHeight + bh;
00254       } else {
00255          child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1), bh);
00256          child->HideFrame(child->fCanvas);
00257          y += bh;
00258       }
00259    }
00260 }
00261 
00262 //______________________________________________________________________________
00263 void TGShutter::SetSelectedItem(TGShutterItem *item)
00264 {
00265    // Set item to be the currently open shutter item.
00266 
00267    fSelectedItem = item;
00268    fSelectedItem->Selected(); // emit signal
00269    Layout();
00270 }
00271 
00272 //______________________________________________________________________________
00273 void TGShutter::SetSelectedItem(const char *name)
00274 {
00275    // Set item to be the currently open shutter item.
00276 
00277    TGShutterItem *item = GetItem(name);
00278    if (!item) {
00279       return;
00280    }
00281    SetSelectedItem(item);
00282 }
00283 
00284 //______________________________________________________________________________
00285 void TGShutter::EnableItem(const char *name, Bool_t on)
00286 {
00287    // Disable/enbale shutter item.
00288 
00289    TGShutterItem *item = GetItem(name);
00290    if (!item) {
00291       return;
00292    }
00293 
00294    item->GetButton()->SetEnabled(on);
00295 }
00296 
00297 //______________________________________________________________________________
00298 TGShutterItem *TGShutter::GetItem(const char *name)
00299 {
00300    // returns a shutter item by name (name is hot string of shutter item)
00301 
00302    TGFrameElement *el;
00303    TGShutterItem  *item = 0;
00304 
00305    TIter next(fList);
00306 
00307    while ((el = (TGFrameElement *) next())) {
00308       TGTextButton *btn;
00309       item = (TGShutterItem *)el->fFrame;
00310       btn = (TGTextButton*)item->GetButton();
00311       if (btn->GetString() == name) return item;
00312    }
00313 
00314    return item;
00315 }
00316 
00317 //______________________________________________________________________________
00318 TGDimension TGShutter::GetDefaultSize() const
00319 {
00320    // Return the default / minimal size of the widget.
00321 
00322    UInt_t w = (GetOptions() & kFixedWidth)  || (fDefWidth  == 0) ? fWidth  : fDefWidth;
00323    UInt_t h = (GetOptions() & kFixedHeight) || (fDefHeight == 0) ? fHeight : fDefHeight;
00324    return TGDimension(w, h);
00325 }
00326 
00327 //______________________________________________________________________________
00328 void TGShutter::SetDefaultSize(UInt_t w, UInt_t h)
00329 {
00330    // Set the default / minimal size of the widget.
00331 
00332    fDefWidth  = w;
00333    fDefHeight = h;
00334 }
00335 
00336 
00337 //______________________________________________________________________________
00338 TGShutterItem::TGShutterItem(const TGWindow *p, TGHotString *s, Int_t id,
00339                              UInt_t options) :
00340    TGVerticalFrame (p, 10, 10, options), TGWidget (id)
00341 {
00342    // Create a shutter item.
00343 
00344    if (!p && !s) {
00345       MakeZombie();
00346       return;
00347    }
00348    fButton = new TGTextButton(this, s, id);
00349    fCanvas = new TGCanvas(this, 10, 10, kChildFrame);
00350    fContainer = new TGVerticalFrame(fCanvas->GetViewPort(), 10, 10, kOwnBackground);
00351    fCanvas->SetContainer(fContainer);
00352    fContainer->SetBackgroundColor(fClient->GetShadow(GetDefaultFrameBackground()));
00353 
00354    AddFrame(fButton, fL1 = new TGLayoutHints(kLHintsTop | kLHintsExpandX));
00355    AddFrame(fCanvas, fL2 = new TGLayoutHints(kLHintsExpandY | kLHintsExpandX));
00356 
00357    fButton->Associate((TGFrame *) p);
00358 
00359    fCanvas->SetEditDisabled(kEditDisableGrab | kEditDisableLayout);
00360    fButton->SetEditDisabled(kEditDisableGrab | kEditDisableBtnEnable);
00361    fContainer->SetEditDisabled(kEditDisableGrab);
00362    fEditDisabled = kEditDisableGrab | kEditDisableLayout;
00363 }
00364 
00365 //______________________________________________________________________________
00366 TGShutterItem::~TGShutterItem()
00367 {
00368    // Clan up shutter item.
00369 
00370    if (!IsZombie() && !MustCleanup()) {
00371       delete fL1;
00372       delete fL2;
00373       delete fButton;
00374       delete fContainer;
00375       delete fCanvas;
00376    }
00377 }
00378 
00379 //______________________________________________________________________________
00380 void TGShutterItem::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00381 {
00382    // Save a shutter item widget as a C++ statement(s) on output stream out
00383 
00384    char quote = '"';
00385    TGTextButton *b = (TGTextButton *)fButton;
00386    const char *text = b->GetText()->GetString();
00387    char hotpos = b->GetText()->GetHotPos();
00388    Int_t lentext = b->GetText()->GetLength();
00389    char *outext = new char[lentext+2];       // should be +2 because of \0
00390    Int_t i=0;
00391 
00392    while (lentext) {
00393       if (i == hotpos-1) {
00394          outext[i] = '&';
00395          i++;
00396       }
00397       outext[i] = *text;
00398       i++;
00399       text++;
00400       lentext--;
00401    }
00402    outext[i]=0;
00403 
00404    out << endl;
00405    out << "   // " << quote << outext << quote << " shutter item " << endl;
00406    out << "   TGShutterItem *";
00407    out << GetName() << " = new TGShutterItem(" << fParent->GetName()
00408        << ", new TGHotString(" << quote << outext << quote << "),"
00409        << fButton->WidgetId() << "," << GetOptionString() << ");" << endl;
00410 
00411    delete [] outext;
00412    if (option && strstr(option, "keep_names"))
00413       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00414 
00415    TList *list = ((TGCompositeFrame *)GetContainer())->GetList();
00416 
00417    if (!list) return;
00418 
00419    out << "   TGCompositeFrame *" << GetContainer()->GetName()
00420        << " = (TGCompositeFrame *)" << GetName() << "->GetContainer();" << endl;
00421 
00422    TGFrameElement *el;
00423    TIter next(list);
00424 
00425    while ((el = (TGFrameElement *) next())) {
00426       el->fFrame->SavePrimitive(out, option);
00427       out << "   " << GetContainer()->GetName() <<"->AddFrame(" << el->fFrame->GetName();
00428       el->fLayout->SavePrimitive(out, option);
00429       out << ");"<< endl;
00430    }
00431 }
00432 
00433 //______________________________________________________________________________
00434 void TGShutter::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00435 {
00436    // Save a shutter widget as a C++ statement(s) on output stream out.
00437 
00438    out << endl;
00439    out << "   // shutter" << endl;
00440 
00441    out << "   TGShutter *";
00442    out << GetName() << " = new TGShutter(" << fParent->GetName() << ","
00443        << GetOptionString() << ");" << endl;
00444 
00445    if ((fDefWidth > 0) || (fDefHeight > 0)) {
00446       out << "   " << GetName() << "->SetDefaultSize(";
00447       out << fDefWidth << "," << fDefHeight << ");" << endl;
00448    }
00449    if (option && strstr(option, "keep_names"))
00450       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00451 
00452    if (!fList) return;
00453 
00454    TGFrameElement *el;
00455    TIter next(fList);
00456 
00457    while ((el = (TGFrameElement *) next())) {
00458       el->fFrame->SavePrimitive(out, option);
00459       out << "   " << GetName() <<"->AddItem(" << el->fFrame->GetName();
00460       //el->fLayout->SavePrimitive(out, option);
00461       out << ");"<< endl;
00462    }
00463 
00464    out << "   " << GetName() << "->SetSelectedItem("
00465        << GetSelectedItem()->GetName() << ");" << endl;
00466    out << "   " <<GetName()<< "->Resize("<<GetWidth()<<","<<GetHeight()<<");"<<endl;
00467 }
00468 

Generated on Tue Jul 5 14:22:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1