Logo ROOT  
Reference Guide
calendar.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// This macro gives an example of how to use html widget to display tabular data.
4 /// To run it do either:
5 /// ~~~
6 /// .x calendar.C
7 /// .x calendar.C++
8 /// ~~~
9 ///
10 /// \macro_code
11 ///
12 /// \author Valeriy Onuchin 24/08/2007
13 
14 
15 #include "TDatime.h"
16 #include "TTimeStamp.h"
17 #include "TGComboBox.h"
18 #include "TGNumberEntry.h"
19 #include "TGLabel.h"
20 #include "TGColorSelect.h"
21 #include "TGHtml.h"
22 #include "TApplication.h"
23 #include "TROOT.h"
24 #include "TColor.h"
25 
26 
27 /////////////////////////// HTML calendar //////////////////////////////////////
28 
29 TString monthNames[12] = {"January", "February", "March", "April",
30  "May", "June", "July", "August", "September",
31  "October", "November", "December"};
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 class HtmlDayName {
35 public: // make them public for shorter code
36  TString fDay; // day name, e.g. "Sunday"
37  TString fAlign; // name align inside table cell
38  TString fBgColor; // cell background color
39  TString fFontSize; // text font size
40  TString fFontColor; // text color
41  TString fHtml; // HTML output code
42 
43 public:
44  HtmlDayName(const char *day);
45  virtual ~HtmlDayName() {}
46 
47  TString Html() const { return fHtml; }
48 
49  ClassDef(HtmlDayName, 0);
50 };
51 
52 //______________________________________________________________________________
53 HtmlDayName::HtmlDayName(const char *day) : fDay(day), fAlign("middle"),
54  fBgColor("#000000"), fFontSize("4"), fFontColor("#FFFFFF")
55 {
56  // ctor.
57 
58  fHtml += "<TH width=14%";
59  fHtml += " align=" + fAlign;
60  fHtml += " bgcolor=" + fBgColor + ">";
61  fHtml += "<font size=" + fFontSize;
62  fHtml += " color=" + fFontColor + ">";
63  fHtml += fDay;
64  fHtml += "</font></TH>\n";
65 }
66 
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 class HtmlMonthTable {
70 public: // make them public for shorter code
71  Int_t fYear; // year
72  Int_t fMonth; // month
73 
74  TString fBorder; // border width
75  TString fBgColor; // background color
76  TString fCellpadding; // cell padding
77  TString fCellFontSize; // cell font size
78  TString fCellBgcolor; // cell background color
79  TString fTodayColor; // background color of cell correspondent today date
80 
81  TDatime fToday; // today's date
82  TString fHtml; // HTML output code
83 
84  void Build();
85  void BuildDayNames();
86  void BuildDays();
87 
88 public:
89  HtmlMonthTable(Int_t year, Int_t month);
90  virtual ~HtmlMonthTable() {}
91 
92  void SetDate(Int_t year, Int_t month);
93  TString Html() const { return fHtml; }
94 
95  ClassDef(HtmlMonthTable, 0);
96 };
97 
98 //______________________________________________________________________________
99 HtmlMonthTable::HtmlMonthTable(Int_t year, Int_t month) : fYear(year),
100  fMonth(month), fBorder("2"), fBgColor("#aaaaaa"), fCellpadding("5"),
101  fCellFontSize("3"), fCellBgcolor("#eeeeee"), fTodayColor("#ffff00")
102 {
103  // Constructor.
104 
105  Build();
106 }
107 
108 //______________________________________________________________________________
109 void HtmlMonthTable::SetDate(Int_t year, Int_t month)
110 {
111  // Set date.
112 
113  fYear = year;
114  fMonth = month;
115  Build();
116 }
117 
118 //______________________________________________________________________________
119 void HtmlMonthTable::Build()
120 {
121  // Build HTML code.
122 
123  fHtml = "<TABLE width=100%";
124  fHtml += " border=" + fBorder;
125  fHtml += " bgcolor=" + fBgColor;
126  fHtml += " cellpadding=" + fCellpadding;
127  fHtml += "><TBODY>";
128 
129  BuildDayNames();
130  BuildDays();
131 
132  fHtml += "</TBODY></TABLE>\n";
133 }
134 
135 //______________________________________________________________________________
136 void HtmlMonthTable::BuildDayNames()
137 {
138  // Build table header with day names.
139 
140  fHtml += "<TR>";
141  fHtml += HtmlDayName("Sunday").Html();
142  fHtml += HtmlDayName("Monday").Html();
143  fHtml += HtmlDayName("Tuesday").Html();
144  fHtml += HtmlDayName("Wednesday").Html();
145  fHtml += HtmlDayName("Thursday").Html();
146  fHtml += HtmlDayName("Friday").Html();
147  fHtml += HtmlDayName("Saturday").Html();
148  fHtml += "</TR>\n";
149 }
150 
151 //______________________________________________________________________________
152 void HtmlMonthTable::BuildDays()
153 {
154  // Build part of table with day numbers.
155 
156  static Int_t maxdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
157 
158  Int_t maxday = maxdays[fMonth-1];
159  if ((fMonth == 2) && TTimeStamp::IsLeapYear(fYear)) maxday = 29;
160 
161  Int_t first = TTimeStamp::GetDayOfWeek(1, fMonth, fYear);
162 
163  // fill html table
164  for (int week = 0; week < 6; week++) {
165  fHtml += "<TR>";
166 
167  for (int weekday = 0; weekday < 7; weekday++) {//
168  Int_t day = week*7 + weekday - first + 1;
169 
170  if ((day > maxday) && !weekday) break; //
171 
172  fHtml += "<TD align=left width=14% ";
173 
174  // hightlight today's cell
175  if ((fToday.GetYear() == fYear) &&
176  (fToday.GetMonth() == fMonth) &&
177  (fToday.GetDay() == day)) {
178  fHtml += " bgcolor=" + fTodayColor;
179  } else {
180  fHtml += " bgcolor=" + fCellBgcolor;
181  }
182  fHtml += ">";
183 
184  //skip week days which are not of this month
185  if ((day <= 0) || (day > maxday)) {
186  fHtml += "&nbsp;</TD>";
187  continue;
188  }
189 
190  fHtml += "<font size=" + fCellFontSize + ">";
191  fHtml += Form("%d", day);
192  fHtml += "</font></TD>\n";
193  }
194  fHtml += "</TR>\n";
195  }
196 }
197 
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 class HtmlCalendar {
201 public: // make them public for shorter code
202  Int_t fYear; // year
203  Int_t fMonth; // month
204  HtmlMonthTable fMonthTable; // HTML table presenting month days
205  TString fHeader; // HTML header
206  TString fFooter; // HTML footer
207  TString fHtml; // output HTML string
208  TString fTitle; // page title
209 
210  void MakeHeader();
211  void MakeFooter();
212 
213 public:
214  HtmlCalendar(Int_t year, Int_t month);
215  virtual ~HtmlCalendar() {}
216 
217  void SetDate(Int_t year, Int_t month);
218  TString Html() const { return fHtml; }
219 
220  ClassDef(HtmlCalendar, 0);
221 };
222 
223 //______________________________________________________________________________
224 HtmlCalendar::HtmlCalendar(Int_t year, Int_t month) : fMonthTable(year, month)
225 {
226  // Constructor.
227 
228  fYear = year;
229  fMonth = month;
230 
231  MakeHeader();
232  MakeFooter();
233 
234  fHtml = fHeader;
235  fHtml += fMonthTable.Html();
236  fHtml += fFooter;
237 }
238 
239 //______________________________________________________________________________
240 void HtmlCalendar::SetDate(Int_t year, Int_t month)
241 {
242  // Create calendar for month/year.
243 
244  fYear = year;
245  fMonth = month;
246 
247  fMonthTable.SetDate(year, month);
248  MakeHeader();
249  MakeFooter();
250  fHtml = fHeader;
251  fHtml += fMonthTable.Html();
252  fHtml += fFooter;
253 }
254 
255 //______________________________________________________________________________
256 void HtmlCalendar::MakeHeader()
257 {
258  // Make HTML header.
259 
260  fTitle = monthNames[fMonth-1] + Form(" %d", fYear);
261  fHeader = "<html><head><title>";
262  fHeader += fTitle;
263  fHeader += "</title></head><body>\n";
264  fHeader += "<center><H2>" + fTitle + "</H2></center>";
265 }
266 
267 //______________________________________________________________________________
268 void HtmlCalendar::MakeFooter()
269 {
270  // Make HTML footer.
271 
272  fFooter = "<br><p><br><center><strong><font size=2 color=#2222ee>";
273  fFooter += "Example of using Html widget to display tabular data.";
274  fFooter += "</font></strong></center></body></html>";
275 }
276 
277 //////////////////////// end of HTML calendar //////////////////////////////////
278 
279 
280 
281 class CalendarWindow {
282 private:
283  TGMainFrame *fMain; // main frame
284  HtmlCalendar *fHtmlText; // calendar HTML table
285  TGHtml *fHtml; // html widget to display HTML calendar
286  TGComboBox *fMonthBox; // month selector
287  TGNumberEntry *fYearEntry; // year selector
288  TGNumberEntry *fFontEntry; // font size selector
289  TGColorSelect *fTableColor; // selector of background color of table
290  TGColorSelect *fCellColor; // selector of background color of table's cells
291 
292 public:
293  CalendarWindow();
294  virtual ~CalendarWindow();
295 
296  void UpdateHTML();
297 
298  ClassDef(CalendarWindow, 0);
299 };
300 
301 
302 //______________________________________________________________________________
303 CalendarWindow::~CalendarWindow()
304 {
305  // Destructor.
306 
307  delete fHtmlText;
308  delete fMain;
309 }
310 
311 //______________________________________________________________________________
312 CalendarWindow::CalendarWindow()
313 {
314  // Main window.
315 
316  fMain = new TGMainFrame(gClient->GetRoot(), 10, 10, kVerticalFrame);
317  fMain->SetCleanup(kDeepCleanup); // delete all subframes on exit
318 
319  // Controls
320  TGHorizontalFrame *controls = new TGHorizontalFrame(fMain);
321  fMain->AddFrame(controls, new TGLayoutHints(kLHintsCenterX, 1, 1, 1, 1));
322 
323  // generate HTML calendar table
324  TDatime today;
325  fHtmlText = new HtmlCalendar(today.GetYear(), today.GetMonth());
326 
327  // create HTML widget
328  fHtml = new TGHtml(fMain, 1, 1);
329  fMain->AddFrame(fHtml, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
330  5, 5, 2, 2));
331 
332  // parse HTML context of HTML calendar table
333  fHtml->ParseText((char*)fHtmlText->Html().Data());
334 
335  TGLabel *dateLabel = new TGLabel(controls, "Date:");
336  controls->AddFrame(dateLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
337  5, 2, 2, 2));
338 
339  //
340  fMonthBox = new TGComboBox(controls);
341  for (int i = 0; i < 12; i++) {
342  fMonthBox->AddEntry(monthNames[i].Data(), i+1);
343  }
344  fMonthBox->Select(today.GetMonth());
345  controls->AddFrame(fMonthBox, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
346 
347  fYearEntry = new TGNumberEntry(controls, today.GetYear(), 5, -1,
351  controls->AddFrame(fYearEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
352 
353  fMonthBox->Resize(100, fYearEntry->GetHeight());
354 
355  TGLabel *fontLabel = new TGLabel(controls, "Font Size:");
356  controls->AddFrame(fontLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
357  30, 2, 2, 2));
358 
359  Int_t fontsize = atoi(fHtmlText->fMonthTable.fCellFontSize.Data());
360  fFontEntry = new TGNumberEntry(controls, fontsize, 2, -1,
364  controls->AddFrame(fFontEntry, new TGLayoutHints(kLHintsLeft, 5, 5, 2, 2));
365 
366  TGLabel *tableLabel = new TGLabel(controls, "Table:");
367  controls->AddFrame(tableLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
368  5, 2, 2, 2));
369 
370  Pixel_t color;
371 
372  gClient->GetColorByName(fHtmlText->fMonthTable.fBgColor.Data(), color);
373  fTableColor = new TGColorSelect(controls, color);
374  controls->AddFrame(fTableColor, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
375  5, 2, 2, 2));
376 
377  TGLabel *cellLabel = new TGLabel(controls, "Cell:");
378  controls->AddFrame(cellLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
379  5, 2, 2, 2));
380 
381  gClient->GetColorByName(fHtmlText->fMonthTable.fCellBgcolor.Data(), color);
382  fCellColor = new TGColorSelect(controls, color);
383  controls->AddFrame(fCellColor, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,
384  5, 2, 2, 2));
385 
386  // connect signals
387  fMonthBox->Connect("Selected(Int_t)", "CalendarWindow", this,
388  "UpdateHTML()");
389  fYearEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
390  this, "UpdateHTML()");
391  fFontEntry->GetNumberEntry()->Connect("TextChanged(char*)", "CalendarWindow",
392  this, "UpdateHTML()");
393  fTableColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
394  "UpdateHTML()");
395  fCellColor->Connect("ColorSelected(Pixel_t)", "CalendarWindow", this,
396  "UpdateHTML()");
397 
398  // terminate ROOT session when window is closed
399  fMain->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
400  fMain->DontCallClose();
401 
402  fMain->MapSubwindows();
403  fMain->Resize(600, 333);
404 
405  // set minimum size of main window
406  fMain->SetWMSizeHints(controls->GetDefaultWidth(), fMain->GetDefaultHeight(),
407  1000, 1000, 0 ,0);
408 
409  TString title = "Calendar for ";
410  title += fHtmlText->fTitle;
411  fMain->SetWindowName(title.Data());
412  fMain->MapRaised();
413 }
414 
415 //______________________________________________________________________________
416 void CalendarWindow::UpdateHTML()
417 {
418  // Update HTML table on user's input.
419 
420  Int_t month = fMonthBox->GetSelected();
421  Int_t year = atoi(fYearEntry->GetNumberEntry()->GetText());
422  fHtmlText->fMonthTable.fCellFontSize = fFontEntry->GetNumberEntry()->GetText();
423 
424  Pixel_t pixel = 0;
425  TColor *color = 0;
426 
427  // table background
428  pixel = fTableColor->GetColor();
429  color = gROOT->GetColor(TColor::GetColor(pixel));
430 
431  if (color) {
432  fHtmlText->fMonthTable.fBgColor = color->AsHexString();
433  }
434 
435  // cell background
436  pixel = fCellColor->GetColor();
437  color = gROOT->GetColor(TColor::GetColor(pixel));
438 
439  if (color) {
440  fHtmlText->fMonthTable.fCellBgcolor = color->AsHexString();
441  }
442 
443  // update HTML context
444  fHtmlText->SetDate(year, month);
445 
446  // parse new HTML context of HTML calendar table
447  fHtml->Clear();
448  fHtml->ParseText((char*)fHtmlText->Html().Data());
449  fHtml->Layout();
450 
451  // update window title
452  TString title = "Calendar for ";
453  title += fHtmlText->fTitle;
454  fMain->SetWindowName(title.Data());
455 }
456 
457 ////////////////////////////////////////////////////////////////////////////////
458 void calendar()
459 {
460  // Main program.
461 
462  new CalendarWindow();
463 }
464 
465 
466 
TGComboBox.h
TGNumberFormat::kNELLimitMax
@ kNELLimitMax
Definition: TGNumberEntry.h:58
first
Definition: first.py:1
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
TGMainFrame
Definition: TGFrame.h:443
TTimeStamp.h
TString::Data
const char * Data() const
Definition: TString.h:369
Pixel_t
ULong_t Pixel_t
Pixel value.
Definition: GuiTypes.h:40
Form
char * Form(const char *fmt,...)
TDatime.h
kVerticalFrame
@ kVerticalFrame
Definition: GuiTypes.h:381
kLHintsLeft
@ kLHintsLeft
Definition: TGLayout.h:31
TGLabel
Definition: TGLabel.h:32
TGNumberFormat::kNEAPositive
@ kNEAPositive
Definition: TGNumberEntry.h:52
Int_t
int Int_t
Definition: RtypesCore.h:45
TColor.h
TGHorizontalFrame
Definition: TGFrame.h:422
TString
Basic string class.
Definition: TString.h:136
TROOT.h
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TGHtml
Definition: TGHtml.h:872
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:35
TColor
The color creation and management class.
Definition: TColor.h:19
TTimeStamp::GetDayOfWeek
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller's formula for calculating the day number.
Definition: TTimeStamp.cxx:418
TGHtml.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
kLHintsExpandY
@ kLHintsExpandY
Definition: TGLayout.h:38
TColor::GetColor
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition: TColor.cxx:1766
TGComboBox
Definition: TGComboBox.h:67
TGLabel.h
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:50
TGNumberFormat::kNELLimitMin
@ kNELLimitMin
Definition: TGNumberEntry.h:57
TGColorSelect
Definition: TGColorSelect.h:127
TGNumberEntry.h
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
TDatime
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
TGColorSelect.h
TDatime::GetMonth
Int_t GetMonth() const
Definition: TDatime.h:66
TGNumberEntry
Definition: TGNumberEntry.h:157
TGCompositeFrame::GetDefaultWidth
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:348
TTimeStamp::IsLeapYear
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
Definition: TTimeStamp.cxx:484
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TGNumberFormat::kNESInteger
@ kNESInteger
Definition: TGNumberEntry.h:34
gROOT
#define gROOT
Definition: TROOT.h:406
TColor::AsHexString
const char * AsHexString() const
Return color as hexadecimal string.
Definition: TColor.cxx:1209
TDatime::GetYear
Int_t GetYear() const
Definition: TDatime.h:65