#include <sstream>
#include <algorithm>
#include "TGLabel.h"
#include "TGComboBox.h"
#include "TGNumberEntry.h"
#include "TGButton.h"
#include "TCanvas.h"
#include "TRootEmbeddedCanvas.h"
#include "TMemStat.h"
#include "TMemStatDrawDlg.h"
#include "TMemStatResource.h"
using namespace std;
struct SFill_t : public binary_function<string, TGComboBox*, bool>
{
bool operator()(const string &val, TGComboBox* box) const
{
if (!box)
return false;
box->AddEntry(val.c_str(), box->GetNumberOfEntries());
return true;
}
};
TMemStatDrawDlg::TMemStatDrawDlg(TGCompositeFrame *parent, TMemStat *MemStat):
fMemStat(MemStat),
fboxOrder(NULL),
fboxSortStat(NULL),
fboxSortStamp(NULL),
fNmbStackDeep(NULL),
fNmbSortDeep(NULL),
fNmbMaxLength(NULL),
fEc(NULL)
{
PlaceCtrls(parent);
}
TMemStatDrawDlg::~TMemStatDrawDlg()
{
}
void TMemStatDrawDlg::PlaceCtrls(TGCompositeFrame *frame)
{
TGCompositeFrame *cont = new TGCompositeFrame(frame, 800, 400, kHorizontalFrame | kFitWidth | kFitHeight);
frame->AddFrame(cont, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY | kLHintsExpandX, 2, 2, 2, 2));
TGCompositeFrame *contR = new TGCompositeFrame(cont, 200, 200, kVerticalFrame | kFitWidth | kFitHeight);
cont->AddFrame(contR, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY, 2, 2, 2, 2));
StringVector_t values;
values.push_back("increasing");
values.push_back("decreasing");
PlaceLBoxCtrl(contR, &fboxOrder, "Order: ", values, resCBoxOrder);
values.clear();
values.push_back("TotalAllocCount");
values.push_back("TotalAlocSize");
values.push_back("AllocCount");
values.push_back("AllocSize");
PlaceLBoxCtrl(contR, &fboxSortStat, "Sort stat: ", values, resCBoxSortStat);
values.clear();
values.push_back("Current");
values.push_back("MaxSize");
values.push_back("MaxCount");
PlaceLBoxCtrl(contR, &fboxSortStamp, "Sort stamp: ", values, resCBoxSortStamp);
PlaceDeepCtrl(contR);
TGTextButton *btnDraw = new TGTextButton(contR);
btnDraw->Connect("Clicked()", "TMemStatDrawDlg", this, "HandleDrawMemStat()");
btnDraw->SetText("Draw");
contR->AddFrame(btnDraw,
new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 10, 10, 10, 10));
PlaceEmbeddedCanvas(cont);
}
void TMemStatDrawDlg::PlaceLBoxCtrl(TGCompositeFrame *frame, TGComboBox **box,
const string &Label, const StringVector_t &Vealues, Int_t resource)
{
TGHorizontalFrame *horz = new TGHorizontalFrame(frame);
frame->AddFrame(horz, new TGLayoutHints(kLHintsExpandX));
TGLabel *lbl = new TGLabel(horz, Label.c_str());
horz->AddFrame(lbl,
new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 2, 2, 2, 2));
*box = new TGComboBox( horz, resource );
(*box)->Resize(120, 20);
horz->AddFrame((*box), new TGLayoutHints(kLHintsRight, 2, 2, 2, 2));
for_each(Vealues.begin(), Vealues.end(), bind2nd(SFill_t(), (*box)));
(*box)->Select(0);
}
void TMemStatDrawDlg::PlaceDeepCtrl(TGCompositeFrame *frame)
{
TGGroupFrame *contDeep = new TGGroupFrame(frame, "Deepnes", kVerticalFrame | kFitWidth | kFitHeight);
frame->AddFrame(contDeep, new TGLayoutHints(kLHintsExpandX));
TGHorizontalFrame *horz0 = new TGHorizontalFrame(contDeep);
contDeep->AddFrame(horz0, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY));
TGLabel *lblStackDeep = new TGLabel(horz0, "Stack deep:");
horz0->AddFrame(lblStackDeep, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
fNmbStackDeep = new TGNumberEntry(horz0, fMemStat->GetStackDeep(), 1, resNmbStackDeep, TGNumberFormat::kNESInteger,
TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 1, 50);
horz0->AddFrame(fNmbStackDeep, new TGLayoutHints( kLHintsRight, 2, 2, 2, 2));
fNmbStackDeep->Resize(100, 20);
TGHorizontalFrame *horz1 = new TGHorizontalFrame(contDeep);
contDeep->AddFrame(horz1, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY));
TGLabel *lSortDeep = new TGLabel(horz1, "Sort deep:");
horz1->AddFrame(lSortDeep, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
fNmbSortDeep = new TGNumberEntry(horz1, fMemStat->GetSortDeep(), 1, resNmbSortDeep, TGNumberFormat::kNESInteger,
TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 1, 50);
horz1->AddFrame(fNmbSortDeep, new TGLayoutHints( kLHintsRight, 2, 2, 2, 2));
fNmbSortDeep->Resize(100, 20);
TGHorizontalFrame *horz2 = new TGHorizontalFrame(contDeep);
contDeep->AddFrame(horz2, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY));
TGLabel *lbl = new TGLabel(horz2, "Max length:");
horz2->AddFrame(lbl, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
fNmbMaxLength = new TGNumberEntry(horz2, fMemStat->GetMaxStringLength(), 1, resNmbMaxLength, TGNumberFormat::kNESInteger,
TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 1, 500);
horz2->AddFrame(fNmbMaxLength, new TGLayoutHints(kLHintsRight, 2, 2, 2, 2));
fNmbMaxLength->Resize(100, 20);
}
void TMemStatDrawDlg::ReDraw()
{
if (!fMemStat)
return;
ostringstream ss;
if (fboxOrder)
ss << "order " << fboxOrder->GetSelected();
if (fboxSortStat)
ss << " sortstat " << fboxSortStat->GetSelected();
if (fboxSortStamp)
ss << " sortstamp " << fboxSortStamp->GetSelected();
if (fNmbStackDeep)
ss << " sortdeep " << fNmbStackDeep->GetIntNumber();
if (fNmbSortDeep)
ss << " stackdeep " << fNmbSortDeep->GetIntNumber();
if (fNmbMaxLength)
ss << " maxlength " << fNmbMaxLength->GetIntNumber();
fMemStat->Draw(ss.str().c_str());
fEc->GetCanvas()->Modified();
fEc->GetCanvas()->Update();
}
void TMemStatDrawDlg::HandleDrawMemStat()
{
ReDraw();
}
void TMemStatDrawDlg::PlaceEmbeddedCanvas(TGCompositeFrame *frame)
{
if (fEc)
return;
fEc = new TRootEmbeddedCanvas("ec", frame, 200, 200);
frame->AddFrame(fEc, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
fEc->GetCanvas()->SetBorderMode(0);
}