This macro gives an example of how to use html widget to display tabular data.
TString monthNames[12] = {
"January",
"February",
"March",
"April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
class HtmlDayName {
public:
public:
HtmlDayName(const char *day);
virtual ~HtmlDayName() {}
TString Html()
const {
return fHtml; }
};
HtmlDayName::HtmlDayName(const char *day) : fDay(day), fAlign("middle"),
fBgColor("#000000"), fFontSize("4"), fFontColor("#FFFFFF")
{
fHtml += "<TH width=14%";
fHtml += " align=" + fAlign;
fHtml += " bgcolor=" + fBgColor + ">";
fHtml += "<font size=" + fFontSize;
fHtml += " color=" + fFontColor + ">";
fHtml += fDay;
fHtml += "</font></TH>\n";
}
class HtmlMonthTable {
public:
void Build();
void BuildDayNames();
void BuildDays();
public:
virtual ~HtmlMonthTable() {}
TString Html()
const {
return fHtml; }
};
HtmlMonthTable::HtmlMonthTable(
Int_t year,
Int_t month) : fYear(year),
fMonth(month), fBorder("2"), fBgColor("#aaaaaa"), fCellpadding("5"),
fCellFontSize("3"), fCellBgcolor("#eeeeee"), fTodayColor("#ffff00")
{
Build();
}
void HtmlMonthTable::SetDate(
Int_t year,
Int_t month)
{
fYear = year;
fMonth = month;
Build();
}
void HtmlMonthTable::Build()
{
fHtml = "<TABLE width=100%";
fHtml += " border=" + fBorder;
fHtml += " bgcolor=" + fBgColor;
fHtml += " cellpadding=" + fCellpadding;
fHtml += "><TBODY>";
BuildDayNames();
BuildDays();
fHtml += "</TBODY></TABLE>\n";
}
void HtmlMonthTable::BuildDayNames()
{
fHtml += "<TR>";
fHtml += HtmlDayName("Sunday").Html();
fHtml += HtmlDayName("Monday").Html();
fHtml += HtmlDayName("Tuesday").Html();
fHtml += HtmlDayName("Wednesday").Html();
fHtml += HtmlDayName("Thursday").Html();
fHtml += HtmlDayName("Friday").Html();
fHtml += HtmlDayName("Saturday").Html();
fHtml += "</TR>\n";
}
void HtmlMonthTable::BuildDays()
{
static Int_t maxdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Int_t maxday = maxdays[fMonth-1];
for (int week = 0; week < 6; week++) {
fHtml += "<TR>";
for (int weekday = 0; weekday < 7; weekday++) {
Int_t day = week*7 + weekday - first + 1;
if ((day > maxday) && !weekday) break;
fHtml += "<TD align=left width=14% ";
if ((fToday.GetYear() == fYear) &&
(fToday.GetMonth() == fMonth) &&
(fToday.GetDay() == day)) {
fHtml += " bgcolor=" + fTodayColor;
} else {
fHtml += " bgcolor=" + fCellBgcolor;
}
fHtml += ">";
if ((day <= 0) || (day > maxday)) {
fHtml += " </TD>";
continue;
}
fHtml += "<font size=" + fCellFontSize + ">";
fHtml +=
Form(
"%d", day);
fHtml += "</font></TD>\n";
}
fHtml += "</TR>\n";
}
}
class HtmlCalendar {
public:
HtmlMonthTable fMonthTable;
void MakeHeader();
void MakeFooter();
public:
virtual ~HtmlCalendar() {}
TString Html()
const {
return fHtml; }
};
HtmlCalendar::HtmlCalendar(
Int_t year,
Int_t month) : fMonthTable(year, month)
{
fYear = year;
fMonth = month;
MakeHeader();
MakeFooter();
fHtml = fHeader;
fHtml += fMonthTable.Html();
fHtml += fFooter;
}
void HtmlCalendar::SetDate(
Int_t year,
Int_t month)
{
fYear = year;
fMonth = month;
fMonthTable.SetDate(year, month);
MakeHeader();
MakeFooter();
fHtml = fHeader;
fHtml += fMonthTable.Html();
fHtml += fFooter;
}
void HtmlCalendar::MakeHeader()
{
fTitle = monthNames[fMonth-1] +
Form(
" %d", fYear);
fHeader = "<html><head><title>";
fHeader += fTitle;
fHeader += "</title></head><body>\n";
fHeader += "<center><H2>" + fTitle + "</H2></center>";
}
void HtmlCalendar::MakeFooter()
{
fFooter = "<br><p><br><center><strong><font size=2 color=#2222ee>";
fFooter += "Example of using Html widget to display tabular data.";
fFooter += "</font></strong></center></body></html>";
}
class CalendarWindow {
private:
HtmlCalendar *fHtmlText;
public:
CalendarWindow();
virtual ~CalendarWindow();
void UpdateHTML();
};
CalendarWindow::~CalendarWindow()
{
delete fHtmlText;
delete fMain;
}
CalendarWindow::CalendarWindow()
{
fHtml =
new TGHtml(fMain, 1, 1);
5, 5, 2, 2));
fHtml->ParseText((char*)fHtmlText->Html().Data());
5, 2, 2, 2));
for (int i = 0; i < 12; i++) {
fMonthBox->AddEntry(monthNames[i].
Data(), i+1);
}
fMonthBox->Resize(100, fYearEntry->GetHeight());
30, 2, 2, 2));
Int_t fontsize = atoi(fHtmlText->fMonthTable.fCellFontSize.Data());
TGNumberFormat::kNEAPositive,
5, 2, 2, 2));
gClient->GetColorByName(fHtmlText->fMonthTable.fBgColor.Data(), color);
5, 2, 2, 2));
5, 2, 2, 2));
gClient->GetColorByName(fHtmlText->fMonthTable.fCellBgcolor.Data(), color);
5, 2, 2, 2));
fMonthBox->Connect("Selected(Int_t)", "CalendarWindow", this,
"UpdateHTML()");
fYearEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
this, "UpdateHTML()");
fFontEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
this, "UpdateHTML()");
fTableColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
"UpdateHTML()");
fCellColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
"UpdateHTML()");
fMain->Connect(
"CloseWindow()",
"TApplication",
gApplication,
"Terminate()");
fMain->DontCallClose();
fMain->MapSubwindows();
fMain->Resize(600, 333);
fMain->SetWMSizeHints(controls->
GetDefaultWidth(), fMain->GetDefaultHeight(),
1000, 1000, 0 ,0);
title += fHtmlText->fTitle;
fMain->SetWindowName(title.
Data());
fMain->MapRaised();
}
void CalendarWindow::UpdateHTML()
{
Int_t month = fMonthBox->GetSelected();
Int_t year = atoi(fYearEntry->GetNumberEntry()->GetText());
fHtmlText->fMonthTable.fCellFontSize = fFontEntry->GetNumberEntry()->GetText();
if (color) {
fHtmlText->fMonthTable.fBgColor = color->
AsHexString();
}
pixel = fCellColor->GetColor();
if (color) {
fHtmlText->fMonthTable.fCellBgcolor = color->
AsHexString();
}
fHtmlText->SetDate(year, month);
fHtml->Clear();
fHtml->ParseText((char*)fHtmlText->Html().Data());
fHtml->Layout();
title += fHtmlText->fTitle;
fMain->SetWindowName(title.
Data());
}
void calendar()
{
new CalendarWindow();
}