Calendar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
  3. * Copyright (c) 2020, the SerenityOS developers.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <LibCore/DateTime.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Calendar.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/Window.h>
  33. #include <LibGfx/Font.h>
  34. #include <LibGfx/Palette.h>
  35. namespace GUI {
  36. static const char* long_day_names[] = {
  37. "Sunday", "Monday", "Tuesday", "Wednesday",
  38. "Thursday", "Friday", "Saturday"
  39. };
  40. static const char* short_day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  41. static const char* mini_day_names[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
  42. static const char* micro_day_names[] = { "S", "M", "T", "W", "T", "F", "S" };
  43. static const char* long_month_names[] = {
  44. "January", "February", "March", "April", "May", "June",
  45. "July", "August", "September", "October", "November", "December"
  46. };
  47. static const char* short_month_names[] = {
  48. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  49. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  50. };
  51. Calendar::Calendar(Core::DateTime date_time)
  52. : m_selected_date(date_time)
  53. , m_selected_year(date_time.year())
  54. , m_selected_month(date_time.month())
  55. {
  56. set_fill_with_background_color(true);
  57. set_layout<GUI::VerticalBoxLayout>();
  58. layout()->set_spacing(0);
  59. m_day_name_container = add<GUI::Widget>();
  60. m_day_name_container->set_layout<GUI::HorizontalBoxLayout>();
  61. m_day_name_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  62. m_day_name_container->set_preferred_size(0, 16);
  63. m_day_name_container->layout()->set_spacing(0);
  64. m_day_name_container->set_fill_with_background_color(true);
  65. m_day_name_container->set_background_role(Gfx::ColorRole::HoverHighlight);
  66. for (auto& day : m_day_names) {
  67. day = m_day_name_container->add<GUI::Label>();
  68. day->set_font(Gfx::Font::default_bold_font());
  69. }
  70. m_calendar_tile_container = add<GUI::Widget>();
  71. m_calendar_tile_container->set_layout<GUI::VerticalBoxLayout>();
  72. m_calendar_tile_container->layout()->set_spacing(0);
  73. for (auto& row : m_week_rows) {
  74. row = m_calendar_tile_container->add<GUI::Widget>();
  75. row->set_layout<GUI::HorizontalBoxLayout>();
  76. row->layout()->set_spacing(0);
  77. }
  78. int i = 0;
  79. for (int j = 0; j < 6; j++)
  80. for (int k = 0; k < 7; k++) {
  81. m_calendar_tiles[i] = m_week_rows[j]->add<CalendarTile>(i, date_time);
  82. m_calendar_tiles[i]->on_click = [this](int index) {
  83. m_previous_selected_date = m_selected_date;
  84. m_selected_date = m_calendar_tiles[index]->get_date_time();
  85. update_tiles(m_selected_date.year(), m_selected_date.month());
  86. if (on_calendar_tile_click)
  87. on_calendar_tile_click();
  88. };
  89. m_calendar_tiles[i]->on_doubleclick = [this](int index) {
  90. if (m_calendar_tiles[index]->get_date_time().day() != m_previous_selected_date.day())
  91. return;
  92. if (on_calendar_tile_doubleclick)
  93. on_calendar_tile_doubleclick();
  94. };
  95. i++;
  96. }
  97. m_month_tile_container = add<GUI::Widget>();
  98. m_month_tile_container->set_visible(false);
  99. m_month_tile_container->set_layout<GUI::VerticalBoxLayout>();
  100. m_month_tile_container->set_fill_with_background_color(true);
  101. m_month_tile_container->set_background_role(Gfx::ColorRole::HoverHighlight);
  102. m_month_tile_container->layout()->set_spacing(0);
  103. for (auto& row : m_month_rows) {
  104. row = m_month_tile_container->add<GUI::Widget>();
  105. row->set_layout<GUI::HorizontalBoxLayout>();
  106. row->layout()->set_spacing(0);
  107. }
  108. i = 0;
  109. for (int j = 0; j < 3; j++)
  110. for (int k = 0; k < 4; k++) {
  111. m_month_tiles[i] = m_month_rows[j]->add<MonthTile>(i, date_time);
  112. m_month_tiles[i]->set_button_style(Gfx::ButtonStyle::CoolBar);
  113. m_month_tiles[i]->on_indexed_click = [this](int index) {
  114. toggle_mode();
  115. update_tiles(m_month_tiles[index]->get_date_time().year(), m_month_tiles[index]->get_date_time().month());
  116. if (on_month_tile_click)
  117. on_month_tile_click();
  118. };
  119. i++;
  120. }
  121. update_tiles(selected_year(), selected_month());
  122. }
  123. Calendar::~Calendar()
  124. {
  125. }
  126. void Calendar::toggle_mode()
  127. {
  128. m_mode == Month ? m_mode = Year : m_mode = Month;
  129. if (mode() == Month) {
  130. m_day_name_container->set_visible(true);
  131. m_calendar_tile_container->set_visible(true);
  132. m_month_tile_container->set_visible(false);
  133. } else {
  134. m_day_name_container->set_visible(false);
  135. m_calendar_tile_container->set_visible(false);
  136. m_month_tile_container->set_visible(true);
  137. }
  138. this->resize(this->height(), this->width());
  139. update_tiles(selected_year(), selected_month());
  140. }
  141. void Calendar::set_grid(bool grid)
  142. {
  143. if (m_grid == grid)
  144. return;
  145. m_grid = grid;
  146. for (int i = 0; i < 42; i++) {
  147. m_calendar_tiles[i]->set_grid(grid);
  148. m_calendar_tiles[i]->update();
  149. }
  150. }
  151. void Calendar::resize_event(GUI::ResizeEvent& event)
  152. {
  153. if (m_day_name_container->is_visible()) {
  154. for (int i = 0; i < 7; i++) {
  155. if (event.size().width() < 120)
  156. m_day_names[i]->set_text(micro_day_names[i]);
  157. else if (event.size().width() < 200)
  158. m_day_names[i]->set_text(mini_day_names[i]);
  159. else if (event.size().width() < 480)
  160. m_day_names[i]->set_text(short_day_names[i]);
  161. else
  162. m_day_names[i]->set_text(long_day_names[i]);
  163. }
  164. }
  165. if (m_month_tile_container->is_visible()) {
  166. for (int i = 0; i < 12; i++) {
  167. if (event.size().width() < 250)
  168. m_month_tiles[i]->set_text(short_month_names[i]);
  169. else
  170. m_month_tiles[i]->set_text(long_month_names[i]);
  171. }
  172. }
  173. (event.size().width() < 200) ? set_grid(false) : set_grid(true);
  174. }
  175. void Calendar::update_tiles(unsigned int target_year, unsigned int target_month)
  176. {
  177. set_selected_calendar(target_year, target_month);
  178. if (mode() == Month) {
  179. unsigned int i = 0;
  180. for (int y = 0; y < 6; y++)
  181. for (int x = 0; x < 7; x++) {
  182. auto date_time = Core::DateTime::create(target_year, target_month, 1);
  183. unsigned int start_of_month = date_time.weekday();
  184. unsigned int year;
  185. unsigned int month;
  186. unsigned int day;
  187. if (start_of_month > i) {
  188. month = (target_month - 1 == 0) ? 12 : target_month - 1;
  189. year = (month == 12) ? target_year - 1 : target_year;
  190. date_time.set_time(year, month, 1);
  191. day = (date_time.days_in_month() - (start_of_month) + i) + 1;
  192. date_time.set_time(year, month, day);
  193. } else if ((i - start_of_month) + 1 > date_time.days_in_month()) {
  194. month = (target_month + 1) > 12 ? 1 : target_month + 1;
  195. year = (month == 1) ? target_year + 1 : target_year;
  196. day = ((i - start_of_month) + 1) - date_time.days_in_month();
  197. date_time.set_time(year, month, day);
  198. } else {
  199. month = target_month;
  200. year = target_year;
  201. day = (i - start_of_month) + 1;
  202. date_time.set_time(year, month, day);
  203. }
  204. m_calendar_tiles[i]->update_values(i, date_time);
  205. m_calendar_tiles[i]->set_selected(date_time.year() == m_selected_date.year() && date_time.month() == m_selected_date.month() && date_time.day() == m_selected_date.day());
  206. m_calendar_tiles[i]->set_outside_selection(date_time.month() != selected_month() || date_time.year() != selected_year());
  207. m_calendar_tiles[i]->update();
  208. i++;
  209. }
  210. } else {
  211. for (int i = 0; i < 12; i++) {
  212. auto date_time = Core::DateTime::create(target_year, i + 1, 1);
  213. m_month_tiles[i]->update_values(date_time);
  214. }
  215. }
  216. }
  217. const String Calendar::selected_calendar_text(bool long_names)
  218. {
  219. if (mode() == Month)
  220. return String::format("%s %u", long_names ? long_month_names[m_selected_month - 1] : short_month_names[m_selected_month - 1], m_selected_year);
  221. else
  222. return String::format("%u", m_selected_year);
  223. }
  224. void Calendar::set_selected_calendar(unsigned int year, unsigned int month)
  225. {
  226. m_selected_year = year;
  227. m_selected_month = month;
  228. }
  229. Calendar::MonthTile::MonthTile(int index, Core::DateTime date_time)
  230. : m_index(index)
  231. , m_date_time(date_time)
  232. {
  233. }
  234. Calendar::MonthTile::~MonthTile()
  235. {
  236. }
  237. void Calendar::MonthTile::mouseup_event(GUI::MouseEvent& event)
  238. {
  239. if (on_indexed_click)
  240. on_indexed_click(m_index);
  241. GUI::Button::mouseup_event(event);
  242. }
  243. Calendar::CalendarTile::CalendarTile(int index, Core::DateTime date_time)
  244. {
  245. set_frame_thickness(0);
  246. update_values(index, date_time);
  247. }
  248. void Calendar::CalendarTile::update_values(int index, Core::DateTime date_time)
  249. {
  250. m_index = index;
  251. m_date_time = date_time;
  252. m_display_date = (m_date_time.day() == 1) ? String::format("%s %u", short_month_names[m_date_time.month() - 1], m_date_time.day()) : String::number(m_date_time.day());
  253. }
  254. Calendar::CalendarTile::~CalendarTile()
  255. {
  256. }
  257. void Calendar::CalendarTile::doubleclick_event(GUI::MouseEvent&)
  258. {
  259. if (on_doubleclick)
  260. on_doubleclick(m_index);
  261. }
  262. void Calendar::CalendarTile::mousedown_event(GUI::MouseEvent&)
  263. {
  264. if (on_click)
  265. on_click(m_index);
  266. }
  267. void Calendar::CalendarTile::enter_event(Core::Event&)
  268. {
  269. m_hovered = true;
  270. update();
  271. }
  272. void Calendar::CalendarTile::leave_event(Core::Event&)
  273. {
  274. m_hovered = false;
  275. update();
  276. }
  277. bool Calendar::CalendarTile::is_today() const
  278. {
  279. auto current_date_time = Core::DateTime::now();
  280. return m_date_time.day() == current_date_time.day() && m_date_time.month() == current_date_time.month() && m_date_time.year() == current_date_time.year();
  281. }
  282. void Calendar::CalendarTile::paint_event(GUI::PaintEvent& event)
  283. {
  284. GUI::Frame::paint_event(event);
  285. GUI::Painter painter(*this);
  286. painter.add_clip_rect(frame_inner_rect());
  287. if (is_hovered() || is_selected())
  288. painter.fill_rect(frame_inner_rect(), palette().hover_highlight());
  289. else
  290. painter.fill_rect(frame_inner_rect(), palette().base());
  291. if (m_index < 7)
  292. painter.draw_line(frame_inner_rect().top_left(), frame_inner_rect().top_right(), Color::NamedColor::Black);
  293. if (!((m_index + 1) % 7 == 0) && has_grid())
  294. painter.draw_line(frame_inner_rect().top_right(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
  295. if (m_index < 35 && has_grid())
  296. painter.draw_line(frame_inner_rect().bottom_left(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
  297. Gfx::IntRect day_rect;
  298. if (has_grid()) {
  299. day_rect = Gfx::IntRect(frame_inner_rect().x(), frame_inner_rect().y(), frame_inner_rect().width(), font().glyph_height() + 4);
  300. day_rect.set_y(frame_inner_rect().y() + 4);
  301. } else {
  302. day_rect = Gfx::IntRect(frame_inner_rect());
  303. }
  304. int highlight_rect_width = (font().glyph_width('0') * (m_display_date.length() + 1)) + 2;
  305. auto display_date = (m_date_time.day() == 1 && frame_inner_rect().width() > highlight_rect_width) ? m_display_date : String::number(m_date_time.day());
  306. if (is_today()) {
  307. if (has_grid()) {
  308. auto highlight_rect = Gfx::IntRect(day_rect.width() / 2 - (highlight_rect_width / 2), day_rect.y(), highlight_rect_width, font().glyph_height() + 4);
  309. painter.draw_rect(highlight_rect, palette().base_text());
  310. } else if (is_selected()) {
  311. painter.draw_rect(frame_inner_rect(), palette().base_text());
  312. }
  313. painter.draw_text(day_rect, display_date, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().base_text());
  314. } else if (is_outside_selection()) {
  315. painter.draw_text(day_rect, display_date, Gfx::Font::default_font(), Gfx::TextAlignment::Center, Color::LightGray);
  316. } else {
  317. if (!has_grid() && is_selected())
  318. painter.draw_rect(frame_inner_rect(), palette().base_text());
  319. painter.draw_text(day_rect, display_date, Gfx::Font::default_font(), Gfx::TextAlignment::Center, palette().base_text());
  320. }
  321. }
  322. }