#include "TGShutter.h"
#include "TGButton.h"
#include "TList.h"
#include "TTimer.h"
#include "Riostream.h"
ClassImp(TGShutterItem)
ClassImp(TGShutter)
TGShutter::TGShutter(const TGWindow *p, UInt_t options) :
TGCompositeFrame(p, 10, 10, options)
{
fSelectedItem = 0;
fClosingItem = 0;
fHeightIncrement = 1;
fClosingHeight = 0;
fClosingHadScrollbar = kFALSE;
fTimer = 0;
fTrash = new TList;
delete fLayoutManager;
fLayoutManager = 0;
}
TGShutter::~TGShutter()
{
if (fTimer) delete fTimer;
if (!MustCleanup()) {
fTrash->Delete();
}
delete fTrash;
fTrash = 0;
}
void TGShutter::AddItem(TGShutterItem *item)
{
TGLayoutHints *hints = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY);
AddFrame(item, hints);
fTrash->Add(hints);
if (!fSelectedItem) {
fSelectedItem = item;
}
}
void TGShutter::RemoveItem(const char *name)
{
TGShutterItem *item = GetItem(name);
if (!item) {
return;
}
if (fList->GetEntries() <= 1) {
return;
}
if (item == fSelectedItem) {
TGFrameElement *fe = (TGFrameElement*)fList->FindObject(item->GetFrameElement());
if (fe) {
TGFrameElement *sel = (TGFrameElement*)fList->Before(fe);
if (!sel) {
sel = (TGFrameElement*)fList->After(fe);
}
if (!sel) {
return;
}
SetSelectedItem((TGShutterItem*)sel->fFrame);
}
}
RemoveFrame(item);
item->DestroyWindow();
delete item;
Layout();
}
void TGShutter::RemovePage()
{
if (!fSelectedItem) {
return;
}
TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
RemoveItem(btn->GetString().Data());
}
void TGShutter::RenamePage(const char *name)
{
if (!fSelectedItem) {
return;
}
TGTextButton *btn = (TGTextButton*)fSelectedItem->GetButton();
btn->SetText(name);
}
TGShutterItem *TGShutter::AddPage(const char *name)
{
static int id = 1000;
TGShutterItem *item = new TGShutterItem(this, new TGHotString(name), id++);
AddItem(item);
MapSubwindows();
Layout();
return item;
}
Bool_t TGShutter::ProcessMessage(Long_t , Long_t parm1, Long_t )
{
if (!fList) return kFALSE;
TGFrameElement *el;
TGShutterItem *child, *item = 0;
TIter next(fList);
while ((el = (TGFrameElement *) next())) {
child = (TGShutterItem *) el->fFrame;
if (parm1 == child->WidgetId()) {
item = child;
break;
}
}
if (!item) return kFALSE;
if (!fSelectedItem)
fSelectedItem = (TGShutterItem*) ((TGFrameElement*)fList->First())->fFrame;
if (fSelectedItem == item) return kTRUE;
fHeightIncrement = 1;
fClosingItem = fSelectedItem;
fClosingHeight = fClosingItem->GetHeight();
fClosingHeight -= fClosingItem->fButton->GetDefaultHeight();
fSelectedItem = item;
Selected(fSelectedItem);
fSelectedItem->Selected();
if (!fTimer) fTimer = new TTimer(this, 6);
fTimer->Reset();
fTimer->TurnOn();
return kTRUE;
}
Bool_t TGShutter::HandleTimer(TTimer *)
{
if (!fClosingItem) return kFALSE;
fClosingHeight -= fHeightIncrement;
fHeightIncrement += 5;
if (fClosingHeight > 0) {
fTimer->Reset();
} else {
fClosingItem = 0;
fClosingHeight = 0;
fTimer->TurnOff();
}
Layout();
return kTRUE;
}
void TGShutter::Layout()
{
TGFrameElement *el;
TGShutterItem *child;
Int_t y, bh, exh;
if (!fList) return;
if (!fSelectedItem)
fSelectedItem = (TGShutterItem*) ((TGFrameElement*)GetList()->First())->fFrame;
exh = Int_t(fHeight - (fBorderWidth << 1));
TIter next(fList);
while ((el = (TGFrameElement *) next())) {
child = (TGShutterItem *) el->fFrame;
bh = child->fButton->GetDefaultHeight();
exh -= bh;
}
y = fBorderWidth;
next.Reset();
while ((el = (TGFrameElement *) next())) {
child = (TGShutterItem *) el->fFrame;
bh = child->fButton->GetDefaultHeight();
if (child == fSelectedItem) {
if (fClosingItem)
child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
else
child->fCanvas->SetScrolling(TGCanvas::kCanvasScrollVertical);
child->ShowFrame(child->fCanvas);
child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
exh - fClosingHeight + bh);
y += exh - fClosingHeight + bh;
} else if (child == fClosingItem) {
child->fCanvas->SetScrolling(TGCanvas::kCanvasNoScroll);
child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1),
fClosingHeight + bh);
y += fClosingHeight + bh;
} else {
child->MoveResize(fBorderWidth, y, fWidth - (fBorderWidth << 1), bh);
child->HideFrame(child->fCanvas);
y += bh;
}
}
}
void TGShutter::SetSelectedItem(TGShutterItem *item)
{
fSelectedItem = item;
fSelectedItem->Selected();
Layout();
}
void TGShutter::SetSelectedItem(const char *name)
{
TGShutterItem *item = GetItem(name);
if (!item) {
return;
}
SetSelectedItem(item);
}
void TGShutter::EnableItem(const char *name, Bool_t on)
{
TGShutterItem *item = GetItem(name);
if (!item) {
return;
}
item->GetButton()->SetEnabled(on);
}
TGShutterItem *TGShutter::GetItem(const char *name)
{
TGFrameElement *el;
TGShutterItem *item = 0;
TIter next(fList);
while ((el = (TGFrameElement *) next())) {
TGTextButton *btn;
item = (TGShutterItem *)el->fFrame;
btn = (TGTextButton*)item->GetButton();
if (btn->GetString() == name) return item;
}
return item;
}
TGShutterItem::TGShutterItem(const TGWindow *p, TGHotString *s, Int_t id,
UInt_t options) :
TGVerticalFrame (p, 10, 10, options), TGWidget (id)
{
if (!p && !s) {
MakeZombie();
return;
}
fButton = new TGTextButton(this, s, id);
fCanvas = new TGCanvas(this, 10, 10, kChildFrame);
fContainer = new TGVerticalFrame(fCanvas->GetViewPort(), 10, 10, kOwnBackground);
fCanvas->SetContainer(fContainer);
fContainer->SetBackgroundColor(fClient->GetShadow(GetDefaultFrameBackground()));
AddFrame(fButton, fL1 = new TGLayoutHints(kLHintsTop | kLHintsExpandX));
AddFrame(fCanvas, fL2 = new TGLayoutHints(kLHintsExpandY | kLHintsExpandX));
fButton->Associate((TGFrame *) p);
fCanvas->SetEditDisabled(kEditDisableGrab | kEditDisableLayout);
fButton->SetEditDisabled(kEditDisableGrab | kEditDisableBtnEnable);
fContainer->SetEditDisabled(kEditDisableGrab);
fEditDisabled = kEditDisableGrab | kEditDisableLayout;
}
TGShutterItem::~TGShutterItem()
{
if (!IsZombie() && !MustCleanup()) {
delete fL1;
delete fL2;
delete fButton;
delete fContainer;
delete fCanvas;
}
}
void TGShutterItem::SavePrimitive(ostream &out, Option_t *option )
{
char quote = '"';
TGTextButton *b = (TGTextButton *)fButton;
const char *text = b->GetText()->GetString();
char hotpos = b->GetText()->GetHotPos();
Int_t lentext = b->GetText()->GetLength();
char *outext = new char[lentext+2];
Int_t i=0;
while (lentext) {
if (i == hotpos-1) {
outext[i] = '&';
i++;
}
outext[i] = *text;
i++;
text++;
lentext--;
}
outext[i]=0;
out << endl;
out << " // " << quote << outext << quote << " shutter item " << endl;
out << " TGShutterItem *";
out << GetName() << " = new TGShutterItem(" << fParent->GetName()
<< ", new TGHotString(" << quote << outext << quote << "),"
<< fButton->WidgetId() << "," << GetOptionString() << ");" << endl;
delete [] outext;
TList *list = ((TGCompositeFrame *)GetContainer())->GetList();
if (!list) return;
out << " TGCompositeFrame *" << GetContainer()->GetName()
<< " = (TGCompositeFrame *)" << GetName() << "->GetContainer();" << endl;
TGFrameElement *el;
TIter next(list);
while ((el = (TGFrameElement *) next())) {
el->fFrame->SavePrimitive(out, option);
out << " " << GetContainer()->GetName() <<"->AddFrame(" << el->fFrame->GetName();
el->fLayout->SavePrimitive(out, option);
out << ");"<< endl;
}
}
void TGShutter::SavePrimitive(ostream &out, Option_t *option )
{
out << endl;
out << " // shutter" << endl;
out << " TGShutter *";
out << GetName() << " = new TGShutter(" << fParent->GetName() << ","
<< GetOptionString() << ");" << endl;
if (!fList) return;
TGFrameElement *el;
TIter next(fList);
while ((el = (TGFrameElement *) next())) {
el->fFrame->SavePrimitive(out, option);
out << " " << GetName() <<"->AddItem(" << el->fFrame->GetName();
out << ");"<< endl;
}
out << " " << GetName() << "->SetSelectedItem("
<< GetSelectedItem()->GetName() << ");" << endl;
out << " " <<GetName()<< "->Resize("<<GetWidth()<<","<<GetHeight()<<");"<<endl;
}