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