#include "TEveGedEditor.h"
#include "TEveElement.h"
#include "TEveManager.h"
#include "TGButton.h"
#include "TGLabel.h"
#include "TGToolTip.h"
#include "TGDNDManager.h"
#include "TGMsgBox.h"
#include "TClass.h"
#include "TContextMenu.h"
ClassImp(TEveGedEditor);
Int_t TEveGedEditor::fgMaxExtraEditors = 10;
TList *TEveGedEditor::fgExtraEditors = new TList;
TContextMenu *TEveGedEditor::fgContextMenu = 0;
TEveGedEditor::TEveGedEditor(TCanvas* canvas, UInt_t width, UInt_t height) :
TGedEditor(canvas, width, height),
fElement (0),
fObject (0)
{
fTabContainer->RemoveAll();
TGedFrame* nf = CreateNameFrame(fTabContainer, "Style");
nf->SetGedEditor(this);
nf->SetModelClass(0);
fTabContainer->AddFrame(nf, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 2, 2, 2));
TClass* amClass = TClass::GetClass("TAttMarker");
TClass* edClass = TClass::GetClass("TAttMarkerEditor");
TGWindow *exroot = (TGWindow*) fClient->GetRoot();
fClient->SetRoot(fTabContainer);
SetFrameCreator(this);
TGedFrame *frame = reinterpret_cast<TGedFrame*>(edClass->New());
frame->SetModelClass(amClass);
{
Int_t off = edClass->GetDataMemberOffset("fPriority");
if (off == 0)
Warning("TEveGedEditor::TEveGedEditor", "Can't fix priority for TAttMarkerEditor.\n");
else
* (Int_t*) (((char*)frame) + off) = 1;
}
SetFrameCreator(0);
fClient->SetRoot(exroot);
fFrameMap.Add(amClass, frame);
}
TEveGedEditor::~TEveGedEditor()
{
if (gDebug > 0)
Info("TEveGedEditor::~TEveGedEditor", "%p going down.", this);
}
void TEveGedEditor::CloseWindow()
{
if (gDebug > 0)
Info("TEveGedEditor::CloseWindow", "%p closing.", this);
fgExtraEditors->Remove(this);
DeleteWindow();
}
void TEveGedEditor::DeleteWindow()
{
if (gDebug > 0)
Info("TEveGedEditor::DeleteWindow", "%p shooting timer.", this);
DisplayElement(0);
if (gDNDManager) {
if (gDNDManager->GetMainFrame() == this)
gDNDManager->SetMainFrame(0);
}
if (!TestBit(kDeleteWindowCalled))
TTimer::SingleShot(150, IsA()->GetName(), this, "ReallyDelete()");
SetBit(kDeleteWindowCalled);
}
TGedFrame* TEveGedEditor::CreateNameFrame(const TGWindow* parent, const char* )
{
return new TEveGedNameFrame(parent);
}
TEveElement* TEveGedEditor::GetEveElement() const
{
return (fModel == fObject) ? fElement : 0;
}
void TEveGedEditor::DisplayElement(TEveElement* re)
{
static const TEveException eh("TEveGedEditor::DisplayElement ");
fElement = re;
fObject = fElement ? fElement->GetEditorObject(eh) : 0;
TGedEditor::SetModel(fPad, fObject, kButton1Down);
}
void TEveGedEditor::DisplayObject(TObject* obj)
{
fElement = dynamic_cast<TEveElement*>(obj);
fObject = obj;
TGedEditor::SetModel(fPad, obj, kButton1Down);
}
void TEveGedEditor::SetModel(TVirtualPad* pad, TObject* obj, Int_t event)
{
fElement = dynamic_cast<TEveElement*>(obj);
fObject = obj;
TGedEditor::SetModel(pad, obj, event);
}
void TEveGedEditor::Update(TGedFrame* )
{
if (fElement)
{
fElement->ElementChanged();
fElement->PropagateVizParamsToProjecteds();
}
gEve->Redraw3D();
}
void TEveGedEditor::SpawnNewEditor(TObject* obj)
{
if (fgExtraEditors->GetSize() >= fgMaxExtraEditors)
{
new TGMsgBox(gClient->GetDefaultRoot(), gEve->GetMainWindow(),
"Clutter warning",
"Maximum number of extra editors reached.",
kMBIconStop, kMBOk);
}
if (obj)
{
TEveGedEditor *ed = new TEveGedEditor();
ed->DisplayObject(obj);
ed->SetWindowName(Form("GED %s", obj->GetName()));
fgExtraEditors->Add(ed);
}
}
void TEveGedEditor::ElementChanged(TEveElement* el)
{
TObject *eobj = el->GetEditorObject("TEveGedEditor::ElementChanged ");
TObjLink *lnk = fgExtraEditors->FirstLink();
while (lnk)
{
TEveGedEditor *ed = (TEveGedEditor*) lnk->GetObject();
if (ed->GetModel() == eobj)
ed->DisplayElement(el);
lnk = lnk->Next();
}
}
void TEveGedEditor::ElementDeleted(TEveElement* el)
{
TObject *eobj = el->GetEditorObject("TEveGedEditor::ElementChanged ");
TObjLink *lnk = fgExtraEditors->FirstLink();
while (lnk)
{
TEveGedEditor *ed = (TEveGedEditor*) lnk->GetObject();
if (ed->GetModel() == eobj)
{
TObjLink *next = lnk->Next();
ed->DeleteWindow();
fgExtraEditors->Remove(lnk);
lnk = next;
}
else
{
lnk = lnk->Next();
}
}
}
void TEveGedEditor::DestroyEditors()
{
while ( ! fgExtraEditors->IsEmpty())
{
TEveGedEditor *ed = (TEveGedEditor*) fgExtraEditors->First();
ed->DeleteWindow();
fgExtraEditors->RemoveFirst();
}
}
TContextMenu* TEveGedEditor::GetContextMenu()
{
if (fgContextMenu == 0)
fgContextMenu = new TContextMenu("", "");
return fgContextMenu;
}
ClassImp(TEveGedNameFrame);
TEveGedNameFrame::TEveGedNameFrame(const TGWindow *p, Int_t width, Int_t height,
UInt_t options) :
TGedFrame(p, width, height, options),
fNCButton(0)
{
fNCButton = new TEveGedNameTextButton(this);
fNCButton->SetTextColor(0x0020a0);
AddFrame(fNCButton, new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
fNCButton->Connect("Clicked()", "TEveGedNameFrame", this, "SpawnEditorClone()");
}
TEveGedNameFrame::~TEveGedNameFrame()
{
}
void TEveGedNameFrame::SetModel(TObject* obj)
{
if (obj)
{
fNCButton->SetText(Form("%s [%s]", obj->GetName(), obj->ClassName()));
fNCButton->SetToolTipText(obj->GetTitle());
fNCButton->SetEnabled(kTRUE);
}
else
{
fNCButton->SetText("No object selected.");
fNCButton->SetToolTipText(0);
fNCButton->SetEnabled(kFALSE);
}
}
void TEveGedNameFrame::SpawnEditorClone()
{
TEveGedEditor::SpawnNewEditor(fGedEditor->GetModel());
}
ClassImp(TEveGedNameTextButton);
TEveGedNameTextButton::TEveGedNameTextButton(TEveGedNameFrame* p) :
TGTextButton(p, ""),
fFrame(p)
{
gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
kButtonPressMask | kButtonReleaseMask,
kNone, kNone);
}
TEveGedNameTextButton::~TEveGedNameTextButton()
{
}
Bool_t TEveGedNameTextButton::HandleButton(Event_t* event)
{
static const TEveException eh("TEveGedNameTextButton::HandleButton ");
if (fTip) fTip->Hide();
if (fState == kButtonDisabled) return kTRUE;
if (event->fCode == kButton3 && event->fType == kButtonPress)
{
TEveGedEditor *eged = (TEveGedEditor*) fFrame->GetGedEditor();
TEveElement *el = eged->GetEveElement();
if (el)
TEveGedEditor::GetContextMenu()->Popup(event->fXRoot, event->fYRoot,
el->GetObject(eh));
return 1;
}
else if (event->fCode == kButton1)
{
return TGTextButton::HandleButton(event);
}
else
{
return 0;
}
}