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