calendar.C

Go to the documentation of this file.
00001 // Author: Valeriy Onuchin   24/08/2007
00002 //
00003 // This macro gives an example of how to use html widget
00004 // to display tabular data.
00005 //
00006 // To run it do either:
00007 // .x calendar.C
00008 // .x calendar.C++
00009 
00010 
00011 #include "TDatime.h"
00012 #include "TTimeStamp.h"
00013 #include "TGComboBox.h"
00014 #include "TGNumberEntry.h"
00015 #include "TGLabel.h"
00016 #include "TGColorSelect.h"
00017 #include "TGHtml.h"
00018 #include "TApplication.h"
00019 #include "TROOT.h"
00020 #include "TColor.h"
00021 
00022 
00023 /////////////////////////// HTML calendar //////////////////////////////////////
00024 
00025 TString monthNames[12] = {"January", "February", "March", "April", 
00026                           "May", "June", "July", "August", "September",
00027                           "October", "November", "December"};
00028 
00029 ////////////////////////////////////////////////////////////////////////////////
00030 class HtmlDayName {
00031 public:                 // make them public for shorter code
00032    TString fDay;        // day name, e.g. "Sunday"
00033    TString fAlign;      // name align inside table cell 
00034    TString fBgColor;    // cell background color
00035    TString fFontSize;   // text font size
00036    TString fFontColor;  // text color
00037    TString fHtml;       // HTML output code
00038 
00039 public:
00040    HtmlDayName(const char *day);
00041    virtual ~HtmlDayName() {}
00042 
00043    TString Html() const { return fHtml; }
00044 
00045    ClassDef(HtmlDayName, 0);
00046 };
00047 
00048 //______________________________________________________________________________
00049 HtmlDayName::HtmlDayName(const char *day) : fDay(day), fAlign("middle"),
00050    fBgColor("#000000"), fFontSize("4"), fFontColor("#FFFFFF")
00051 {
00052    // ctor.
00053 
00054    fHtml += "<TH  width=14%";
00055    fHtml += " align=" + fAlign;
00056    fHtml += " bgcolor=" + fBgColor + ">";
00057    fHtml += "<font size=" + fFontSize;
00058    fHtml += " color=" + fFontColor + ">";
00059    fHtml += fDay;
00060    fHtml += "</font></TH>\n";
00061 }
00062 
00063 
00064 ////////////////////////////////////////////////////////////////////////////////
00065 class HtmlMonthTable {
00066 public:                    // make them public for shorter code
00067    Int_t    fYear;         // year
00068    Int_t    fMonth;        // month
00069 
00070    TString  fBorder;       // border width
00071    TString  fBgColor;      // background color
00072    TString  fCellpadding;  // cell padding
00073    TString  fCellFontSize; // cell font size
00074    TString  fCellBgcolor;  // cell background color
00075    TString  fTodayColor;   // background color of cell correspondent today date
00076 
00077    TDatime  fToday;        // today's date
00078    TString  fHtml;         // HTML output code
00079 
00080    void Build();
00081    void BuildDayNames();
00082    void BuildDays();
00083 
00084 public:
00085    HtmlMonthTable(Int_t year, Int_t month);
00086    virtual ~HtmlMonthTable() {}
00087 
00088    void SetDate(Int_t year, Int_t month);
00089    TString Html() const { return fHtml; }
00090 
00091    ClassDef(HtmlMonthTable, 0);
00092 };
00093 
00094 //______________________________________________________________________________
00095 HtmlMonthTable::HtmlMonthTable(Int_t year, Int_t month) : fYear(year),
00096    fMonth(month), fBorder("2"), fBgColor("#aaaaaa"), fCellpadding("5"),
00097    fCellFontSize("3"), fCellBgcolor("#eeeeee"), fTodayColor("#ffff00")
00098 {
00099    // Constructor.
00100 
00101    Build();
00102 }
00103 
00104 //______________________________________________________________________________
00105 void HtmlMonthTable::SetDate(Int_t year, Int_t month)
00106 {
00107    // Set date.
00108 
00109    fYear = year;
00110    fMonth = month;
00111    Build();
00112 }
00113 
00114 //______________________________________________________________________________
00115 void HtmlMonthTable::Build()
00116 {
00117    // Build HTML code.
00118 
00119    fHtml = "<TABLE width=100%";
00120    fHtml += " border=" + fBorder;
00121    fHtml += " bgcolor=" + fBgColor;
00122    fHtml += " cellpadding=" + fCellpadding;
00123    fHtml += "><TBODY>";
00124 
00125    BuildDayNames();
00126    BuildDays();
00127 
00128    fHtml += "</TBODY></TABLE>\n";
00129 }
00130 
00131 //______________________________________________________________________________
00132 void HtmlMonthTable::BuildDayNames()
00133 {
00134    // Build table header with day names.
00135 
00136    fHtml += "<TR>";
00137    fHtml += HtmlDayName("Sunday").Html();
00138    fHtml += HtmlDayName("Monday").Html();
00139    fHtml += HtmlDayName("Tuesday").Html();
00140    fHtml += HtmlDayName("Wednesday").Html();
00141    fHtml += HtmlDayName("Thursday").Html();
00142    fHtml += HtmlDayName("Friday").Html();
00143    fHtml += HtmlDayName("Saturday").Html();
00144    fHtml += "</TR>\n";
00145 }
00146 
00147 //______________________________________________________________________________
00148 void HtmlMonthTable::BuildDays()
00149 {
00150    // Build part of table with day numbers.
00151 
00152    static Int_t maxdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00153 
00154    Int_t maxday = maxdays[fMonth-1];
00155    if ((fMonth == 2) && TTimeStamp::IsLeapYear(fYear)) maxday = 29;
00156 
00157    Int_t first = TTimeStamp::GetDayOfWeek(1, fMonth, fYear);
00158 
00159    // fill html table
00160    for (int week = 0; week < 6; week++) {
00161       fHtml += "<TR>";
00162 
00163       for (int weekday = 0; weekday < 7; weekday++) {//
00164          Int_t day = week*7 + weekday - first + 1;
00165 
00166          if ((day > maxday) && !weekday) break; //
00167 
00168          fHtml += "<TD align=left width=14% ";
00169 
00170          // hightlight today's cell
00171          if ((fToday.GetYear() == fYear) && 
00172              (fToday.GetMonth() == fMonth) && 
00173              (fToday.GetDay() == day)) {
00174             fHtml += " bgcolor=" + fTodayColor;
00175          } else {
00176             fHtml += " bgcolor=" + fCellBgcolor;
00177          }
00178          fHtml += ">";
00179 
00180          //skip week days which are not of this month
00181          if ((day <= 0) || (day > maxday)) {
00182             fHtml += "&nbsp;</TD>";
00183             continue;
00184          }
00185 
00186          fHtml += "<font size=" + fCellFontSize + ">";
00187          fHtml += Form("%d", day);
00188          fHtml += "</font></TD>\n";
00189       }
00190       fHtml += "</TR>\n";
00191    }
00192 }
00193 
00194 
00195 ////////////////////////////////////////////////////////////////////////////////
00196 class HtmlCalendar {
00197 public:                          // make them public for shorter code
00198    Int_t          fYear;         // year
00199    Int_t          fMonth;        // month
00200    HtmlMonthTable fMonthTable;   // HTML table presenting month days
00201    TString        fHeader;       // HTML header
00202    TString        fFooter;       // HTML footer
00203    TString        fHtml;         // output HTML string
00204    TString        fTitle;        // page title
00205 
00206    void MakeHeader();
00207    void MakeFooter();
00208 
00209 public:
00210    HtmlCalendar(Int_t year, Int_t month);
00211    virtual ~HtmlCalendar() {}
00212 
00213    void SetDate(Int_t year, Int_t month);
00214    TString Html() const { return fHtml; }
00215 
00216    ClassDef(HtmlCalendar, 0);
00217 };
00218 
00219 //______________________________________________________________________________
00220 HtmlCalendar::HtmlCalendar(Int_t year, Int_t month) : fMonthTable(year, month)
00221 {
00222    // Constructor.
00223    
00224    fYear = year;
00225    fMonth = month;
00226 
00227    MakeHeader();
00228    MakeFooter();
00229 
00230    fHtml = fHeader;
00231    fHtml += fMonthTable.Html();
00232    fHtml += fFooter;
00233 }
00234 
00235 //______________________________________________________________________________
00236 void HtmlCalendar::SetDate(Int_t year, Int_t month)
00237 {
00238    // Create calendar for month/year. 
00239  
00240    fYear = year;
00241    fMonth = month;
00242 
00243    fMonthTable.SetDate(year, month);
00244    MakeHeader();
00245    MakeFooter();
00246    fHtml = fHeader;
00247    fHtml += fMonthTable.Html();
00248    fHtml += fFooter;
00249 }
00250 
00251 //______________________________________________________________________________
00252 void HtmlCalendar::MakeHeader()
00253 {
00254    // Make HTML header.
00255 
00256    fTitle = monthNames[fMonth-1] + Form(" %d", fYear);
00257    fHeader = "<html><head><title>";
00258    fHeader += fTitle;
00259    fHeader += "</title></head><body>\n";
00260    fHeader += "<center><H2>" + fTitle + "</H2></center>";
00261 }
00262 
00263 //______________________________________________________________________________
00264 void HtmlCalendar::MakeFooter()
00265 {
00266    // Make HTML footer.
00267 
00268    fFooter = "<br><p><br><center><strong><font size=2 color=#2222ee>";
00269    fFooter += "Example of using Html widget to display tabular data.";
00270    fFooter += "</font></strong></center></body></html>";
00271 }
00272 
00273 //////////////////////// end of HTML calendar //////////////////////////////////
00274 
00275 
00276 
00277 class CalendarWindow {
00278 private:
00279    TGMainFrame    *fMain;       // main frame
00280    HtmlCalendar   *fHtmlText;   // calendar HTML table
00281    TGHtml         *fHtml;       // html widget to display HTML calendar
00282    TGComboBox     *fMonthBox;   // month selector
00283    TGNumberEntry  *fYearEntry;  // year selector
00284    TGNumberEntry  *fFontEntry;  // font size selector
00285    TGColorSelect  *fTableColor; // selector of background color of table 
00286    TGColorSelect  *fCellColor;  // selector of background color of table's cells
00287 
00288 public:
00289    CalendarWindow();
00290    virtual ~CalendarWindow();
00291 
00292    void UpdateHTML();
00293 
00294    ClassDef(CalendarWindow, 0);
00295 };
00296 
00297 
00298 //______________________________________________________________________________
00299 CalendarWindow::~CalendarWindow()
00300 {
00301    // Destructor.
00302 
00303    delete fHtmlText;
00304    delete fMain;
00305 }
00306 
00307 //______________________________________________________________________________
00308 CalendarWindow::CalendarWindow() 
00309 {
00310    // Main  window.
00311 
00312    fMain = new TGMainFrame(gClient->GetRoot(), 10, 10, kVerticalFrame);
00313    fMain->SetCleanup(kDeepCleanup); // delete all subframes on exit
00314 
00315    // Controls 
00316    TGHorizontalFrame *controls = new TGHorizontalFrame(fMain);
00317    fMain->AddFrame(controls, new TGLayoutHints(kLHintsCenterX, 1, 1, 1, 1));
00318 
00319    // generate HTML calendar table
00320    TDatime today;
00321    fHtmlText = new HtmlCalendar(today.GetYear(), today.GetMonth());
00322 
00323    // create HTML widget
00324    fHtml = new TGHtml(fMain, 1, 1);
00325    fMain->AddFrame(fHtml, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
00326                                             5, 5, 2, 2));
00327 
00328    // parse HTML context of HTML calendar table
00329    fHtml->ParseText((char*)fHtmlText->Html().Data());
00330 
00331    TGLabel *dateLabel = new TGLabel(controls, "Date:");
00332    controls->AddFrame(dateLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00333                                                    5, 2, 2, 2));
00334 
00335    // 
00336    fMonthBox = new TGComboBox(controls);
00337    for (int i = 0; i < 12; i++) {
00338       fMonthBox->AddEntry(monthNames[i].Data(), i+1);
00339    }
00340    fMonthBox->Select(today.GetMonth());
00341    controls->AddFrame(fMonthBox, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
00342 
00343    fYearEntry = new TGNumberEntry(controls, today.GetYear(), 5, -1, 
00344                                   TGNumberFormat::kNESInteger, 
00345                                   TGNumberFormat::kNEAPositive,
00346                                   TGNumberFormat::kNELLimitMin, 1995);
00347    controls->AddFrame(fYearEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
00348 
00349    fMonthBox->Resize(100, fYearEntry->GetHeight());
00350 
00351    TGLabel *fontLabel = new TGLabel(controls, "Font Size:");
00352    controls->AddFrame(fontLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00353                                                    30, 2, 2, 2));  
00354 
00355    Int_t fontsize = atoi(fHtmlText->fMonthTable.fCellFontSize.Data());
00356    fFontEntry = new TGNumberEntry(controls, fontsize, 2, -1,
00357                                   TGNumberFormat::kNESInteger, 
00358                                   TGNumberFormat::kNEAPositive,
00359                                   TGNumberFormat::kNELLimitMax, 0, 7);
00360    controls->AddFrame(fFontEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
00361 
00362    TGLabel *tableLabel = new TGLabel(controls, "Table:");
00363    controls->AddFrame(tableLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00364                                                     5, 2, 2, 2));
00365 
00366    Pixel_t color;
00367 
00368    gClient->GetColorByName(fHtmlText->fMonthTable.fBgColor.Data(), color);
00369    fTableColor = new TGColorSelect(controls, color);
00370    controls->AddFrame(fTableColor, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00371                                                      5, 2, 2, 2));
00372 
00373    TGLabel *cellLabel = new TGLabel(controls, "Cell:");
00374    controls->AddFrame(cellLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00375                                                    5, 2, 2, 2));
00376 
00377    gClient->GetColorByName(fHtmlText->fMonthTable.fCellBgcolor.Data(), color);
00378    fCellColor = new TGColorSelect(controls, color);
00379    controls->AddFrame(fCellColor, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
00380                                                     5, 2, 2, 2));
00381 
00382    // connect signals
00383    fMonthBox->Connect("Selected(Int_t)", "CalendarWindow", this, 
00384                       "UpdateHTML()");
00385    fYearEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
00386                                          this, "UpdateHTML()");
00387    fFontEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
00388                                          this, "UpdateHTML()");
00389    fTableColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
00390                         "UpdateHTML()");
00391    fCellColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
00392                        "UpdateHTML()");
00393 
00394    // terminate ROOT session when window is closed
00395    fMain->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
00396    fMain->DontCallClose();
00397 
00398    fMain->MapSubwindows();
00399    fMain->Resize(600, 333);
00400 
00401    // set  minimum size of main window
00402    fMain->SetWMSizeHints(controls->GetDefaultWidth(), fMain->GetDefaultHeight(),
00403                          1000, 1000, 0 ,0);
00404 
00405    TString title = "Calendar for ";
00406    title += fHtmlText->fTitle;
00407    fMain->SetWindowName(title.Data());
00408    fMain->MapRaised();
00409 }
00410 
00411 //______________________________________________________________________________
00412 void CalendarWindow::UpdateHTML()
00413 {
00414    // Update HTML table on user's input.
00415 
00416    Int_t month = fMonthBox->GetSelected();
00417    Int_t year = atoi(fYearEntry->GetNumberEntry()->GetText());
00418    fHtmlText->fMonthTable.fCellFontSize = fFontEntry->GetNumberEntry()->GetText();
00419 
00420    Pixel_t pixel = 0;
00421    TColor *color = 0;
00422 
00423    // table background
00424    pixel = fTableColor->GetColor();
00425    color = gROOT->GetColor(TColor::GetColor(pixel));
00426 
00427    if (color) {
00428       fHtmlText->fMonthTable.fBgColor = color->AsHexString();
00429    }
00430 
00431    // cell background
00432    pixel = fCellColor->GetColor();
00433    color = gROOT->GetColor(TColor::GetColor(pixel));
00434 
00435    if (color) {
00436       fHtmlText->fMonthTable.fCellBgcolor = color->AsHexString();
00437    }
00438 
00439    // update HTML context
00440    fHtmlText->SetDate(year, month);
00441 
00442    // parse new HTML context of HTML calendar table
00443    fHtml->Clear();
00444    fHtml->ParseText((char*)fHtmlText->Html().Data());
00445    fHtml->Layout();
00446 
00447    // update window title
00448    TString title = "Calendar for ";
00449    title += fHtmlText->fTitle;
00450    fMain->SetWindowName(title.Data());
00451 }
00452 
00453 ////////////////////////////////////////////////////////////////////////////////
00454 void calendar()
00455 {
00456    // Main program.
00457 
00458    new CalendarWindow();
00459 }
00460 
00461 
00462 

Generated on Tue Jul 5 15:44:23 2011 for ROOT_528-00b_version by  doxygen 1.5.1