#include "TDatime.h"
#include "TTimeStamp.h"
#include "TGComboBox.h"
#include "TGNumberEntry.h"
#include "TGLabel.h"
#include "TGColorSelect.h"
#include "TGHtml.h"
#include "TApplication.h"
#include "TROOT.h"
#include "TColor.h"
TString monthNames[12] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
class HtmlDayName {
public:
TString fDay;
TString fAlign;
TString fBgColor;
TString fFontSize;
TString fFontColor;
TString fHtml;
public:
HtmlDayName(const char *day);
virtual ~HtmlDayName() {}
TString Html() const { return fHtml; }
ClassDef(HtmlDayName, 0);
};
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:
Int_t fYear;
Int_t fMonth;
TString fBorder;
TString fBgColor;
TString fCellpadding;
TString fCellFontSize;
TString fCellBgcolor;
TString fTodayColor;
TDatime fToday;
TString fHtml;
void Build();
void BuildDayNames();
void BuildDays();
public:
HtmlMonthTable(Int_t year, Int_t month);
virtual ~HtmlMonthTable() {}
void SetDate(Int_t year, Int_t month);
TString Html() const { return fHtml; }
ClassDef(HtmlMonthTable, 0);
};
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];
if ((fMonth == 2) && TTimeStamp::IsLeapYear(fYear)) maxday = 29;
Int_t first = TTimeStamp::GetDayOfWeek(1, fMonth, fYear);
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:
Int_t fYear;
Int_t fMonth;
HtmlMonthTable fMonthTable;
TString fHeader;
TString fFooter;
TString fHtml;
TString fTitle;
void MakeHeader();
void MakeFooter();
public:
HtmlCalendar(Int_t year, Int_t month);
virtual ~HtmlCalendar() {}
void SetDate(Int_t year, Int_t month);
TString Html() const { return fHtml; }
ClassDef(HtmlCalendar, 0);
};
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>Example of using ";
fFooter += "Html widget to display tabular data.</font></strong></center></body></html>";
}
class CalendarWindow {
private:
TGMainFrame *fMain;
HtmlCalendar *fHtmlText;
TGHtml *fHtml;
TGComboBox *fMonthBox;
TGNumberEntry *fYearEntry;
TGNumberEntry *fFontEntry;
TGColorSelect *fTableColor;
TGColorSelect *fCellColor;
public:
CalendarWindow();
virtual ~CalendarWindow();
void UpdateHTML();
ClassDef(CalendarWindow, 0);
};
CalendarWindow::~CalendarWindow()
{
delete fHtmlText;
delete fMain;
}
CalendarWindow::CalendarWindow()
{
fMain = new TGMainFrame(gClient->GetRoot(), 10, 10, kVerticalFrame);
fMain->SetCleanup(kDeepCleanup);
TGHorizontalFrame *controls = new TGHorizontalFrame(fMain);
fMain->AddFrame(controls, new TGLayoutHints(kLHintsCenterX, 1, 1, 1, 1));
TDatime today;
fHtmlText = new HtmlCalendar(today.GetYear(), today.GetMonth());
fHtml = new TGHtml(fMain, 1, 1);
fMain->AddFrame(fHtml, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 5, 5, 2, 2));
fHtml->ParseText((char*)fHtmlText->Html().Data());
TGLabel *dateLabel = new TGLabel(controls, "Date:");
controls->AddFrame(dateLabel, new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 5, 2, 2, 2));
fMonthBox = new TGComboBox(controls);
for (int i = 0; i < 12; i++) {
fMonthBox->AddEntry(monthNames[i].Data(), i+1);
}
fMonthBox->Select(today.GetMonth());
controls->AddFrame(fMonthBox, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
fYearEntry = new TGNumberEntry(controls, today.GetYear(), 5, -1,
TGNumberFormat::kNESInteger,
TGNumberFormat::kNEAPositive,
TGNumberFormat::kNELLimitMin, 1995);
controls->AddFrame(fYearEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
fMonthBox->Resize(100, fYearEntry->GetHeight());
TGLabel *fontLabel = new TGLabel(controls, "Font Size:");
controls->AddFrame(fontLabel, new TGLayoutHints(kLHintsLeft| kLHintsCenterY, 30, 2, 2, 2));
Int_t fontsize = atoi(fHtmlText->fMonthTable.fCellFontSize.Data());
fFontEntry = new TGNumberEntry(controls, fontsize, 2, -1,
TGNumberFormat::kNESInteger,
TGNumberFormat::kNEAPositive,
TGNumberFormat::kNELLimitMax, 0, 7);
controls->AddFrame(fFontEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
TGLabel *tableLabel = new TGLabel(controls, "Table:");
controls->AddFrame(tableLabel, new TGLayoutHints(kLHintsLeft| kLHintsCenterY, 5, 2, 2, 2));
Pixel_t color;
gClient->GetColorByName(fHtmlText->fMonthTable.fBgColor.Data(), color);
fTableColor = new TGColorSelect(controls, color);
controls->AddFrame(fTableColor, new TGLayoutHints(kLHintsLeft| kLHintsCenterY, 5, 2, 2, 2));
TGLabel *cellLabel = new TGLabel(controls, "Cell:");
controls->AddFrame(cellLabel, new TGLayoutHints(kLHintsLeft| kLHintsCenterY, 5, 2, 2, 2));
gClient->GetColorByName(fHtmlText->fMonthTable.fCellBgcolor.Data(), color);
fCellColor = new TGColorSelect(controls, color);
controls->AddFrame(fCellColor, new TGLayoutHints(kLHintsLeft| kLHintsCenterY, 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);
TString title = "Calendar for ";
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();
Pixel_t pixel = 0;
TColor *color = 0;
pixel = fTableColor->GetColor();
color = gROOT->GetColor(TColor::GetColor(pixel));
if (color) {
fHtmlText->fMonthTable.fBgColor = color->AsHexString();
}
pixel = fCellColor->GetColor();
color = gROOT->GetColor(TColor::GetColor(pixel));
if (color) {
fHtmlText->fMonthTable.fCellBgcolor = color->AsHexString();
}
fHtmlText->SetDate(year, month);
fHtml->Clear();
fHtml->ParseText((char*)fHtmlText->Html().Data());
fHtml->Layout();
TString title = "Calendar for ";
title += fHtmlText->fTitle;
fMain->SetWindowName(title.Data());
}
void calendar()
{
new CalendarWindow();
}
|
|