Wtx ~ Wt Extension Library
WtxLib
YearView.cpp
1 
2 #include <Wt/WPushButton.h>
3 #include <Wt/WText.h>
4 #include <Wt/WTable.h>
5 #include <Wt/WCalendar.h>
6 
7 #include "Calendar.h"
8 #include "YearView.h"
9 #include "Widget.h"
10 
11 Wtx::Web::Calendar::YearView::MiniMonth::MiniMonth( int firstDayOfWeek, const Wt::WDate & date )
12 {
13  /*
14  ** The normal WCalendar employs a firstDayOfWeek function that is
15  ** 1-based, meaning, 1=first day of week which is also Monday. Since
16  ** we have daynames() that are zero-based, meaning 0=Sunday, the logic
17  ** works out that 0=Sunday so therefore 1=Monday, but the specification
18  ** for WCalendar is that the possible range is 1..7, not 0..6 so we
19  ** have to make a slight adjustment to the incoming value so that
20  ** 7 becomes 0.
21  **
22  */
23  firstDayOfWeek %= 7;
24 
25  /*
26  ** grab the date but make sure it's properly set to the
27  ** beginning of the month, just in case the caller failed
28  ** to do so.
29  **
30  */
31  m_date = Wt::WDate(date.year(),date.month(),1);
32 
33  /*
34  ** put a header up
35  **
36  */
37  addNew<Wt::WText>
38  ( // Month:{1} {2}:Year
39  Wt::WString("<center>{1} {2}</center>")
40  .arg( Wt::WDate::longMonthName( date.month() ) )
41  .arg( date.year() )
42  );
43 
44  /*
45  ** put in a table, and then put another header
46  ** in that contains the weekday-names.
47  **
48  */
49  auto table = addNew<Wt::WTable>();
50  table-> addStyleClass( "Wtx_MiniMonth" );
51 
52  /*
53  ** put up a short-day-name based on the (normal)
54  ** day names (meaning, sunday is usually the first day)
55  ** in the header row. If a different firstDayOfWeek was
56  ** asked for, then the first-day pasted here will be
57  ** that day. If the firstDayOfWeek=3 then the first
58  ** day shown in the first column should be wednesday.
59  **
60  */
61  for( int weekday = 0; weekday < 7; weekday++ )
62  {
63  auto w = std::make_unique<Wt::WText>( daynames().at((weekday+firstDayOfWeek)%7).substr(0,3) );
64 
65  w-> addStyleClass( "Wtx_MiniMonth_dayname" );
66 
67  table-> elementAt( 0, weekday )-> addWidget( std::move(w) );
68  }
69 
70 // for( int i=0; i<14; i++ )
71 // std::cout << __FILE__ << ":" << __LINE__ << " " << i << " " << i % 7 << std::endl;
72 
73  auto dow = (date.dayOfWeek()-firstDayOfWeek)%7;
74 
75 // std::cout << __FILE__ << ":" << __LINE__
76 // << " dow:" << date.dayOfWeek()
77 // << " dow:" << dow
78 // << " fdw:" << firstDayOfWeek
79 // << " dat:" << date.toString()
80 // << std::endl
81 // ;
82 
83  /*
84  ** put out a row for each week. We are going to
85  ** plop down 6 weeks of days
86  **
87  */
88  for( int week = 0; week < 6; week++ )
89  {
90  /*
91  ** put out a column for each day. There are 7
92  ** days in a week.
93  **
94  */
95  for( int day = 0; day < 7; day++ )
96  {
97  auto d = date.addDays( (dow*-1) + ((week*7)+day) );
98  auto w = std::make_unique<DayWidget>(d);
99  m_dayWidgets.push_back( w.get() );
100 
101  table-> elementAt( week*7, day )-> addWidget( std::move(w) );
102  }
103 
104  } // endfor( int week = 0; week < 6; week++ )
105 
106 } // endMiniCalendar::MiniCalendar( const Wt::WDate & date )
107 
108 void Wtx::Web::Calendar::YearView::MiniMonth::setSelectedDate( const Wt::WDate & date )
109 {
110  for( auto dayWidget : m_dayWidgets )
111  {
112  if( dayWidget-> date() == date )
113  {
114  dayWidget-> setSelected( true );
115  }
116  else
117  {
118  dayWidget-> setSelected( false );
119  }
120 
121  }
122 }
123 
124 Wtx::Web::Calendar::YearView::DayWidget::DayWidget( const Wt::WDate & date )
125 : Wt::WText( Wt::WString("{1}").arg( date.day() ) ),
126  m_date(date)
127 {
128  addStyleClass( "Wtx_MiniMonth_day" );
129 
130  if( date == Wt::WDate::currentDate() )
131  {
132  addStyleClass( "Wtx_MiniMonth_today" );
133  }
134 
135 }
136 
137 const Wt::WDate & Wtx::Web::Calendar::YearView::DayWidget::date() const
138 {
139  return m_date;
140 }
141 
142 void Wtx::Web::Calendar::YearView::DayWidget::setSelected( bool value )
143 {
144  if( value )
145  {
146  addStyleClass( "Wtx_MiniMonth_selected" );
147  }
148  else
149  {
150  removeStyleClass( "Wtx_MiniMonth_selected" );
151  }
152 }
153 
154 Wtx::Web::Calendar::YearView::YearView( int firstDayOfWeek, Wtx::Web::Calendar::Widget * widget )
155 : Wtx::Web::Calendar::BaseView( firstDayOfWeek, widget )
156 {
157  addStyleClass( "divTable" );
158  addStyleClass( "blueTable" );
159 
160  auto currentYear = Wt::WDate( Wt::WDate::currentDate().year(), 1, 1 );
161 
162  for( int row = 0; row < 3; row++ )
163  {
164  auto cwRow = addNew<Wt::WContainerWidget>();
165  cwRow-> addStyleClass( "divTableRow" );
166 
167  for( int col = 0; col < 4; col++ )
168  {
169  auto cwCol = cwRow-> addNew<Wt::WContainerWidget>();
170  cwCol-> addStyleClass( "divTableCell" );
171 
172 // cwCol-> addNew<Wt::WCalendar>(); <--- this does not work because the WCalendar has too many things on it
173 
174  m_months.push_back
175  (
176  cwCol-> addNew<MiniMonth>( firstDayOfWeek, currentYear.addMonths( (row*4) + col ) )
177  );
178 
179 // cwCol-> addNew<Wt::WCalendar>();
180  }
181  }
182 
183 }
184 
185 
186 const std::vector< Wtx::Web::Calendar::YearView::MiniMonth * > & Wtx::Web::Calendar::YearView::months() const
187 {
188  return m_months;
189 }
190 
191 const std::vector< Wtx::Web::Calendar::YearView::DayWidget * > & Wtx::Web::Calendar::YearView::days() const
192 {
193  return m_days;
194 }
195 
196 
197 void Wtx::Web::Calendar::YearView::setSelectedDate( const Wt::WDate & value )
198 {
199  Wtx::Web::Calendar::BaseView::setSelectedDate( value );
200 
201  for( auto month : m_months )
202  {
203  month-> setSelectedDate( value );
204  }
205 }
206 
207 Wt::Signal< Wt::WDate > & Wtx::Web::Calendar::YearView::clicked()
208 {
209  return m_clicked;
210 }
211 
212 
213 
Calendar Widget.
Definition: Widget.h:66
const std::vector< std::string > & daynames()
Long Day Names.
Definition: Calendar.cpp:11
witty extension library
Definition: Activity.h:51