Wtx ~ Wt Extension Library
WtxLib
main.cpp
1 #include <Wt/WServer.h>
2 #include <Wt/WApplication.h>
3 #include <Wt/WEnvironment.h>
4 #include <Wt/WContainerWidget.h>
5 #include <Wt/WBootstrapTheme.h>
6 #include <Wt/WTemplate.h>
7 #include <Wt/WText.h>
8 #include <Wt/WBreak.h>
9 #include <Wt/WTable.h>
10 #include <Wt/WTableView.h>
11 #include <Wt/WDate.h>
12 
13 #define COUT_(X) std::cout << __FILE__ << "::" << __LINE__ << " " << X << std::endl;
14 #define COUT_LINE COUT_("");
15 
16 class CalendarActivity
17 {
18  public:
19 
20 
21  protected:
22 
23  private:
24 
25 
26 
27 };
28 
29 class Calendar
30 : public Wt::WContainerWidget
31 {
32  public:
33 
34  protected:
35 
36  private:
37 
38 };
39 
49 class CalendarDay
50 : public Calendar
51 {
52  public:
53 
54  CalendarDay( const Wt::WDate & date );
55 
56  const Wt::WDate & date() const;
57 
58  Wt::WTemplate * templt();
59  Wt::WText * day();
60  Wt::WText * header();
61  Wt::WContainerWidget * body();
62  Wt::WText * footer();
63 
64  protected:
65 
66  private:
67 
68  Wt::WDate m_date;
69  Wt::WTemplate * m_templt = nullptr;
70  Wt::WText * m_day = nullptr;
71  Wt::WText * m_header = nullptr;
72  Wt::WContainerWidget * m_body = nullptr;
73  Wt::WText * m_footer = nullptr;
74 
75 }; // endclass CalendarDay
76 
77 CalendarDay::CalendarDay( const Wt::WDate & d )
78 : m_date( d )
79 {
80  addStyleClass( "divTableCell" );
81 
82  m_templt = addNew<Wt::WTemplate>( Wt::WString::tr("Calendar.day.cell" ) );
83 
84  m_day = m_templt-> bindNew<Wt::WText>( "day", Wt::WString("{1}").arg( date().day() ) );
85  m_day-> addStyleClass( "divDay" );
86 
87  m_header = m_templt-> bindNew<Wt::WText>( "header" );
88  m_header-> addStyleClass( "divHeader" );
89 
90  m_body = m_templt-> bindNew<Wt::WContainerWidget>( "body" );
91  m_body-> addStyleClass( "divBody" );
92 
93  m_footer = m_templt-> bindNew<Wt::WText>( "footer" );
94  m_footer-> addStyleClass( "divFooter" );
95 
96 }
97 
98 const Wt::WDate & CalendarDay::date() const
99 {
100  return m_date;
101 }
102 
103 Wt::WTemplate * CalendarDay::templt()
104 {
105  return m_templt;
106 }
107 
108 Wt::WText * CalendarDay::day()
109 {
110  return m_day;
111 }
112 
113 Wt::WText * CalendarDay::header()
114 {
115  return m_header;
116 }
117 
118 Wt::WContainerWidget * CalendarDay::body()
119 {
120  return m_body;
121 }
122 
123 Wt::WText * CalendarDay::footer()
124 {
125  return m_footer;
126 }
127 
128 
129 
130 class CalendarWeek
131 : public Calendar
132 {
133  public:
134 
135  CalendarWeek( const Wt::WDate & date );
136 
137  protected:
138 
139  private:
140 
141 };
142 
143 CalendarWeek::CalendarWeek( const Wt::WDate & date )
144 {
145  addStyleClass( "divTableRow" );
146 }
147 
148 class CalendarMonth
149 : public Calendar
150 {
151  public:
152 
153  CalendarMonth( const Wt::WDate & selectedDate );
154 
155  const std::vector< CalendarDay * > & days() const;
156 
157  protected:
158 
159  private:
160 
161  std::vector< CalendarDay * > m_days;
162 };
163 
164 CalendarMonth::CalendarMonth( const Wt::WDate & selectedDate )
165 {
166  addStyleClass( "divTable" );
167  addStyleClass( "blueTable" );
168 
169  Wt::WDate first( selectedDate.year(), selectedDate.month(), 1 );
170  Wt::WDate start = first.addDays( first.dayOfWeek() * -1 );
171 
172  {
173  auto cwHeader = addNew<Wt::WContainerWidget>();
174  cwHeader-> addStyleClass( "divTableHeading" );
175 
176  auto cwRow = cwHeader-> addNew<Wt::WContainerWidget>();
177  cwRow-> addStyleClass( "divTableRow" );
178 
179  static const std::vector<std::string> daynames =
180  {
181  "Sun",
182  "Mon",
183  "Tue",
184  "Wed",
185  "Thu",
186  "Fri",
187  "Sat"
188  };
189  for( int weekday = 0; weekday < 7; weekday++ )
190  {
191  auto cwDay = cwRow-> addNew<Wt::WContainerWidget>();
192  cwDay-> addStyleClass( "divTableHead" );
193 
194  cwDay-> addNew<Wt::WText>( daynames.at(weekday) );
195  }
196  }
197 
198  int cellDate = first.dayOfWeek() * -1;
199 
200  /*
201  ** loop through five weeks of calendar
202  **
203  */
204  for( int week = 0; week < 5; week++ )
205  {
206  auto currentWeek = start.addDays( week*7 );
207 
208  auto cwWeek = addNew<CalendarWeek>( currentWeek );
209 
210  /*
211  ** loop through 7 days in a week
212  **
213  */
214  for( int day = 0; day < 7; day++ )
215  {
216  /*
217  ** get this day as current WDate
218  **
219  */
220  auto currentDay = start.addDays( (week*7)+day );
221 
222  /*
223  ** create the container for this day
224  **
225  */
226  auto cwDay = cwWeek-> addNew<CalendarDay>( currentDay );
227 
228  /*
229  ** if this day is outside of this month then
230  ** stick the month abbreviation leader at the
231  ** beginning of the 'day date'
232  **
233  */
234  if( currentDay.month() != selectedDate.month() )
235  {
236  cwDay-> day()-> setText
237  (
238  Wt::WString("{1} {2}")
239  .arg( Wt::WDate::shortMonthName( currentDay.month() ) )
240  .arg( currentDay.day() )
241  );
242  }
243 
244  /*
245  ** if this day is in the previous month, then change
246  ** the background-color
247  **
248  */
249  if( currentDay.month() < selectedDate.month() )
250  {
251  cwDay-> addStyleClass( "divPreviousMonth" );
252  }
253 
254  /*
255  ** if this day is in the next month, then change
256  ** the background-color
257  **
258  */
259  if( currentDay.month() > selectedDate.month() )
260  {
261  cwDay-> addStyleClass( "divNextMonth" );
262  }
263 
264  /*
265  ** if this day is actually 'today' then change the
266  ** background color.
267  **
268  */
269  if( currentDay == selectedDate )
270  {
271  cwDay-> addStyleClass( "divCurrentDay" );
272  }
273 
274  /*
275  ** remember
276  **
277  */
278  m_days.push_back( cwDay );
279 
280  } // endfor( int day = 0; day < 7; day++ )
281 
282  } // endfor( int week = 0; week < 5; week++ )
283 
284 } // endCalendarMonth::CalendarMonth( const Wt::WDate & selectedDate )
285 
286 
287 
288 const std::vector< CalendarDay * > & CalendarMonth::days() const
289 {
290  return m_days;
291 }
292 
293 class CalendarWidget
294 : public Wt::WContainerWidget
295 {
296  public:
297 
298  CalendarWidget();
299 
300  CalendarMonth * monthView() const;
301 
302  private:
303 
304  Wt::WTemplate * m_templt = nullptr;
305  CalendarMonth * m_monthView = nullptr;
306  Wt::WContainerWidget * m_dayPicker = nullptr;
307 };
308 
309 CalendarWidget::CalendarWidget()
310 {
311  m_templt = addNew<Wt::WTemplate>( Wt::WString::tr( "Calendar.widget" ) );
312 
313  m_dayPicker = m_templt-> bindNew<Wt::WContainerWidget>( "dayPicker" );
314  m_monthView = m_templt-> bindNew<CalendarMonth>( "monthView", Wt::WDate::currentDate() );
315 
316  std::map< Wt::WDate, std::string > holidays =
317  {
318  { Wt::WDate( 2018, 5, 5 ), "Cinco de Mayo" },
319  { Wt::WDate( 2018, 5, 10 ), "Ascension Day" },
320  { Wt::WDate( 2018, 5, 13 ), "Mother's Day" },
321  { Wt::WDate( 2018, 5, 21 ), "Victoria Day (Canada)" },
322  { Wt::WDate( 2018, 5, 28 ), "Memorial Day (USA)" }
323  };
324 
325  for( auto calendarDay : m_monthView-> days() )
326  {
327  auto holidayItem = holidays.find( calendarDay-> date() );
328  if( holidayItem != holidays.end() )
329  {
330  calendarDay-> header()-> setText( holidayItem-> second );
331  }
332  }
333 
334  std::map< Wt::WDate, std::string > moon =
335  {
336  { Wt::WDate( 2018, 5, 7 ), "Last Quarter Moon" },
337  { Wt::WDate( 2018, 5, 15 ), "New Moon" },
338  { Wt::WDate( 2018, 5, 21 ), "New Quarter Moon" },
339  { Wt::WDate( 2018, 5, 29 ), "Full Moon" }
340  };
341 
342  for( auto calendarDay : m_monthView-> days() )
343  {
344  auto moonItem = moon.find( calendarDay-> date() );
345  if( moonItem != moon.end() )
346  {
347  calendarDay-> footer()-> setText( moonItem-> second );
348  }
349  }
350 }
351 
352 CalendarMonth * CalendarWidget::monthView() const
353 {
354  return m_monthView;
355 }
356 
357 class DevApp
358 : public Wt::WApplication
359 {
360  public:
361  DevApp(const Wt::WEnvironment &env)
362  : Wt::WApplication( env )
363  {
364  auto bootstrapTheme = std::make_shared<Wt::WBootstrapTheme>();
365  bootstrapTheme-> setVersion( Wt::BootstrapVersion::v3 );
366  bootstrapTheme-> setResponsive(true);
367  setTheme( bootstrapTheme );
368  useStyleSheet( "resources/themes/bootstrap/3/bootstrap-theme.min.css" );
369  useStyleSheet( "styles/calendar.css" );
370  messageResourceBundle().use( "calendar" );
371  setTitle( "Calendar" );
372 
373  auto calendarWidget = root()-> addNew<CalendarWidget>();
374 
375  COUT_( calendarWidget-> monthView()-> days().size() );
376  }
377 
378 };
379 
380 int main(int argc, char** argv)
381 {
382  try
383  {
384  Wt::WServer server( argc, argv );
385 
386  server.addEntryPoint
387  (
388  Wt::EntryPointType::Application,
389  [](const Wt::WEnvironment &env)
390  {
391  return std::make_unique<DevApp>(env);
392  },
393  "/calendar"
394  );
395 
396  server.run();
397  }
398  catch( Wt::WServer::Exception & e )
399  {
400  std::cerr << e.what() << std::endl;
401  return -1;
402  }
403 
404  return 0;
405 
406 }
407 
Calendar Day Widget.
Definition: main.cpp:48
Definition: main.cpp:356
const std::vector< std::string > & daynames()
Long Day Names.
Definition: Calendar.cpp:11