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