00001 #include "GuiBase.h"
00002 #include "GuiMainWindow.h"
00003
00004 #include "TList.h"
00005 #include "TGFrame.h"
00006
00007 #include <iostream>
00008 #include <algorithm>
00009 using namespace std;
00010
00011 #include <sigc++/sigc++.h>
00012 using namespace SigC;
00013
00014
00015 #ifdef GUI_DEBUG
00016 static int nguis = 0;
00017 #endif
00018
00019 GuiBase::GuiBase()
00020 : SigC::Object()
00021 , fCleanup(0)
00022 , fLayoutHints(kLHintsExpandX|kLHintsExpandY)
00023 , fName("GuiBase")
00024 {
00025 fCleanup = new TList;
00026
00027 #ifdef GUI_DEBUG
00028 ++nguis;
00029 cerr << "GuiBase(), " << nguis << " alive\n";
00030 #endif
00031 }
00032
00033 GuiBase::~GuiBase()
00034 {
00035 if (fCleanup) {
00036 fCleanup->Delete();
00037 delete fCleanup;
00038 }
00039
00040
00041
00042
00043 #ifdef GUI_DEBUG
00044 --nguis;
00045 cerr << "~GuiBase(\""<<this->GetName()<<"\"), " << nguis << " alive\n";
00046 #endif
00047 }
00048
00049 GuiMainWindow* GuiBase::GetMainWindow()
00050 {
00051 TGFrame* as_frame = this->GetFrame();
00052 if (!as_frame) return 0;
00053
00054 TGWindow* parent = const_cast<TGWindow*>(as_frame->GetParent());
00055 if (!parent) {
00056 cerr << "We have no parent?!?\n";
00057 return 0;
00058 }
00059
00060 GuiBase* gb = dynamic_cast<GuiBase*>(parent);
00061 if (!gb) {
00062 cerr << "Parent not a GuiBase\n";
00063 return 0;
00064 }
00065
00066 GuiMainWindow* mw = dynamic_cast<GuiMainWindow*>(gb);
00067 if (!mw) {
00068
00069 return gb->GetMainWindow();
00070 }
00071 return mw;
00072 }
00073
00074 bool GuiBase::AddChild(TGFrame& obj)
00075 {
00076 GuiBase* child = dynamic_cast<GuiBase*>(&obj);
00077 if (!child) {
00078 cerr << "GuiBase::AddChild: failed to cast child to GuiBase\n";
00079 return false;
00080 }
00081 fChildren.push_back(child);
00082 return true;
00083 }
00084
00085 void GuiBase::AddObject(TObject* obj)
00086 {
00087 fCleanup->Add(obj);
00088 }
00089
00090 void GuiBase::LayoutChildren(void)
00091 {
00092 ChildrenList::iterator childit, done = fChildren.end();
00093
00094 for (childit = fChildren.begin(); childit != done; ++childit) {
00095 SigC::Ptr<GuiBase> gptr = *childit;
00096 GuiBase* child = gptr;
00097
00098 TGFrame* frame = child->GetFrame();
00099
00100 child->LayoutChildren();
00101 frame->Layout();
00102 }
00103 }
00104
00105
00106 TGFrame* GuiBase::GetFrame()
00107 {
00108 TGFrame* frame = dynamic_cast<TGFrame*>(this);
00109 if (!frame) cerr << "GuiBase::GetFrame, Can't cast to TGFrame\n";
00110 return frame;
00111 }
00112
00113
00114
00115
00116 void GuiCompositeFrameBase::Add(GuiBase& child)
00117 {
00118 fChildren.push_back(&child);
00119 TGCompositeFrame* cf = dynamic_cast<TGCompositeFrame*>(this->GetFrame());
00120 if (!cf) {
00121 cerr << "GuiCompositeFrameBase: can't cast this to TGCompositeFrame\n";
00122 return;
00123 }
00124
00125 TGLayoutHints* loh = new TGLayoutHints(child.GetLayoutHints());
00126 this->AddObject(loh);
00127 cf->AddFrame(child.GetFrame(),loh);
00128 }
00129
00130 void GuiCompositeFrameBase::Remove(GuiBase& child)
00131 {
00132 ChildrenList::iterator it = find(fChildren.begin(),fChildren.end(),&child);
00133 if (it == fChildren.end()) {
00134 cerr << "GuiCompositeFrameBase: can't remove child I don't have\n";
00135 return;
00136 }
00137
00138
00139
00140 TGFrame* f = dynamic_cast<TGFrame*>(&child);
00141 TGCompositeFrame* cf = dynamic_cast<TGCompositeFrame*>(this);
00142
00143 if (f && cf) {
00144 cf->HideFrame(f);
00145 cf->RemoveFrame(f);
00146 fChildren.erase(it);
00147 }
00148 else {
00149 cerr << "GuiCompositeFrameBase: failed to remove child\n";
00150 }
00151 }
00152 void GuiCompositeFrameBase::Show(GuiBase& child)
00153 {
00154 TGFrame* f = dynamic_cast<TGFrame*>(&child);
00155 TGCompositeFrame* cf = dynamic_cast<TGCompositeFrame*>(this);
00156
00157 if (f && cf) cf->ShowFrame(f);
00158 }
00159 void GuiCompositeFrameBase::Hide(GuiBase& child)
00160 {
00161 TGFrame* f = dynamic_cast<TGFrame*>(&child);
00162 TGCompositeFrame* cf = dynamic_cast<TGCompositeFrame*>(this);
00163
00164 if (f && cf) cf->HideFrame(f);
00165 }