Calendar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_fixed_height(16);
  62. m_day_name_container->layout()->set_spacing(0);
  63. m_day_name_container->set_fill_with_background_color(true);
  64. m_day_name_container->set_background_role(Gfx::ColorRole::HoverHighlight);
  65. for (auto& day : m_day_names) {
  66. day = m_day_name_container->add<GUI::Label>();
  67. day->set_font(Gfx::Font::default_bold_font());
  68. }
  69. m_calendar_tile_container = add<GUI::Widget>();
  70. m_calendar_tile_container->set_layout<GUI::VerticalBoxLayout>();
  71. m_calendar_tile_container->layout()->set_spacing(0);
  72. for (auto& row : m_week_rows) {
  73. row = m_calendar_tile_container->add<GUI::Widget>();
  74. row->set_layout<GUI::HorizontalBoxLayout>();
  75. row->layout()->set_spacing(0);
  76. }
  77. int i = 0;
  78. for (int j = 0; j < 6; j++)
  79. for (int k = 0; k < 7; k++) {
  80. m_calendar_tiles[i] = m_week_rows[j]->add<CalendarTile>(i, date_time);
  81. m_calendar_tiles[i]->on_click = [this](int index) {
  82. m_previous_selected_date = m_selected_date;
  83. m_selected_date = m_calendar_tiles[index]->get_date_time();
  84. update_tiles(m_selected_date.year(), m_selected_date.month());
  85. if (on_calendar_tile_click)
  86. on_calendar_tile_click();
  87. };
  88. m_calendar_tiles[i]->on_doubleclick = [this](int index) {
  89. if (m_calendar_tiles[index]->get_date_time().day() != m_previous_selected_date.day())
  90. return;
  91. if (on_calendar_tile_doubleclick)
  92. on_calendar_tile_doubleclick();
  93. };
  94. i++;
  95. }
  96. m_month_tile_container = add<GUI::Widget>();
  97. m_month_tile_container->set_visible(false);
  98. m_month_tile_container->set_layout<GUI::VerticalBoxLayout>();
  99. m_month_tile_container->set_fill_with_background_color(true);
  100. m_month_tile_container->set_background_role(Gfx::ColorRole::HoverHighlight);
  101. m_month_tile_container->layout()->set_spacing(0);
  102. for (auto& row : m_month_rows) {
  103. row = m_month_tile_container->add<GUI::Widget>();
  104. row->set_layout<GUI::HorizontalBoxLayout>();
  105. row->layout()->set_spacing(0);
  106. }
  107. i = 0;
  108. for (int j = 0; j < 3; j++)
  109. for (int k = 0; k < 4; k++) {
  110. m_month_tiles[i] = m_month_rows[j]->add<MonthTile>(i, date_time);
  111. m_month_tiles[i]->set_button_style(Gfx::ButtonStyle::CoolBar);
  112. m_month_tiles[i]->on_indexed_click = [this](int index) {
  113. toggle_mode();
  114. update_tiles(m_month_tiles[index]->get_date_time().year(), m_month_tiles[index]->get_date_time().month());
  115. if (on_month_tile_click)
  116. on_month_tile_click();
  117. };
  118. i++;
  119. }
  120. update_tiles(selected_year(), selected_month());
  121. }
  122. Calendar::~Calendar()
  123. {
  124. }
  125. void Calendar::toggle_mode()
  126. {
  127. m_mode == Month ? m_mode = Year : m_mode = Month;
  128. if (mode() == Month) {
  129. m_day_name_container->set_visible(true);
  130. m_calendar_tile_container->set_visible(true);
  131. m_month_tile_container->set_visible(false);
  132. } else {
  133. m_day_name_container->set_visible(false);
  134. m_calendar_tile_container->set_visible(false);
  135. m_month_tile_container->set_visible(true);
  136. }
  137. this->resize(this->height(), this->width());
  138. update_tiles(selected_year(), selected_month());
  139. }
  140. void Calendar::set_grid(bool grid)
  141. {
  142. if (m_grid == grid)
  143. return;
  144. m_grid = grid;
  145. for (int i = 0; i < 42; i++) {
  146. m_calendar_tiles[i]->set_grid(grid);
  147. m_calendar_tiles[i]->update();
  148. }
  149. }
  150. void Calendar::resize_event(GUI::ResizeEvent& event)
  151. {
  152. if (m_day_name_container->is_visible()) {
  153. for (int i = 0; i < 7; i++) {
  154. if (event.size().width() < 120)
  155. m_day_names[i]->set_text(micro_day_names[i]);
  156. else if (event.size().width() < 200)
  157. m_day_names[i]->set_text(mini_day_names[i]);
  158. else if (event.size().width() < 480)
  159. m_day_names[i]->set_text(short_day_names[i]);
  160. else
  161. m_day_names[i]->set_text(long_day_names[i]);
  162. }
  163. }
  164. if (m_month_tile_container->is_visible()) {
  165. for (int i = 0; i < 12; i++) {
  166. if (event.size().width() < 250)
  167. m_month_tiles[i]->set_text(short_month_names[i]);
  168. else
  169. m_month_tiles[i]->set_text(long_month_names[i]);
  170. }
  171. }
  172. (event.size().width() < 200) ? set_grid(false) : set_grid(true);
  173. }
  174. void Calendar::update_tiles(unsigned int target_year, unsigned int target_month)
  175. {
  176. set_selected_calendar(target_year, target_month);
  177. if (mode() == Month) {
  178. unsigned int i = 0;
  179. for (int y = 0; y < 6; y++)
  180. for (int x = 0; x < 7; x++) {
  181. auto date_time = Core::DateTime::create(target_year, target_month, 1);
  182. unsigned int start_of_month = date_time.weekday();
  183. unsigned int year;
  184. unsigned int month;
  185. unsigned int day;
  186. if (start_of_month > i) {
  187. month = (target_month - 1 == 0) ? 12 : target_month - 1;
  188. year = (month == 12) ? target_year - 1 : target_year;
  189. date_time.set_time(year, month, 1);
  190. day = (date_time.days_in_month() - (start_of_month) + i) + 1;
  191. date_time.set_time(year, month, day);
  192. } else if ((i - start_of_month) + 1 > date_time.days_in_month()) {
  193. month = (target_month + 1) > 12 ? 1 : target_month + 1;
  194. year = (month == 1) ? target_year + 1 : target_year;
  195. day = ((i - start_of_month) + 1) - date_time.days_in_month();
  196. date_time.set_time(year, month, day);
  197. } else {
  198. month = target_month;
  199. year = target_year;
  200. day = (i - start_of_month) + 1;
  201. date_time.set_time(year, month, day);
  202. }
  203. m_calendar_tiles[i]->update_values(i, date_time);
  204. 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());
  205. m_calendar_tiles[i]->set_outside_selection(date_time.month() != selected_month() || date_time.year() != selected_year());
  206. m_calendar_tiles[i]->update();
  207. i++;
  208. }
  209. } else {
  210. for (int i = 0; i < 12; i++) {
  211. auto date_time = Core::DateTime::create(target_year, i + 1, 1);
  212. m_month_tiles[i]->update_values(date_time);
  213. }
  214. }
  215. }
  216. const String Calendar::selected_calendar_text(bool long_names)
  217. {
  218. if (mode() == Month)
  219. return String::format("%s %u", long_names ? long_month_names[m_selected_month - 1] : short_month_names[m_selected_month - 1], m_selected_year);
  220. else
  221. return String::format("%u", m_selected_year);
  222. }
  223. void Calendar::set_selected_calendar(unsigned int year, unsigned int month)
  224. {
  225. m_selected_year = year;
  226. m_selected_month = month;
  227. }
  228. Calendar::MonthTile::MonthTile(int index, Core::DateTime date_time)
  229. : m_index(index)
  230. , m_date_time(date_time)
  231. {
  232. }
  233. Calendar::MonthTile::~MonthTile()
  234. {
  235. }
  236. void Calendar::MonthTile::mouseup_event(GUI::MouseEvent& event)
  237. {
  238. if (on_indexed_click)
  239. on_indexed_click(m_index);
  240. GUI::Button::mouseup_event(event);
  241. }
  242. Calendar::CalendarTile::CalendarTile(int index, Core::DateTime date_time)
  243. {
  244. set_frame_thickness(0);
  245. update_values(index, date_time);
  246. }
  247. void Calendar::CalendarTile::update_values(int index, Core::DateTime date_time)
  248. {
  249. m_index = index;
  250. m_date_time = date_time;
  251. 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());
  252. }
  253. Calendar::CalendarTile::~CalendarTile()
  254. {
  255. }
  256. void Calendar::CalendarTile::doubleclick_event(GUI::MouseEvent&)
  257. {
  258. if (on_doubleclick)
  259. on_doubleclick(m_index);
  260. }
  261. void Calendar::CalendarTile::mousedown_event(GUI::MouseEvent&)
  262. {
  263. if (on_click)
  264. on_click(m_index);
  265. }
  266. void Calendar::CalendarTile::enter_event(Core::Event&)
  267. {
  268. m_hovered = true;
  269. update();
  270. }
  271. void Calendar::CalendarTile::leave_event(Core::Event&)
  272. {
  273. m_hovered = false;
  274. update();
  275. }
  276. bool Calendar::CalendarTile::is_today() const
  277. {
  278. auto current_date_time = Core::DateTime::now();
  279. 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();
  280. }
  281. void Calendar::CalendarTile::paint_event(GUI::PaintEvent& event)
  282. {
  283. GUI::Frame::paint_event(event);
  284. GUI::Painter painter(*this);
  285. painter.add_clip_rect(frame_inner_rect());
  286. if (is_hovered() || is_selected())
  287. painter.fill_rect(frame_inner_rect(), palette().hover_highlight());
  288. else
  289. painter.fill_rect(frame_inner_rect(), palette().base());
  290. if (m_index < 7)
  291. painter.draw_line(frame_inner_rect().top_left(), frame_inner_rect().top_right(), Color::NamedColor::Black);
  292. if (!((m_index + 1) % 7 == 0) && has_grid())
  293. painter.draw_line(frame_inner_rect().top_right(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
  294. if (m_index < 35 && has_grid())
  295. painter.draw_line(frame_inner_rect().bottom_left(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
  296. Gfx::IntRect day_rect;
  297. if (has_grid()) {
  298. day_rect = Gfx::IntRect(frame_inner_rect().x(), frame_inner_rect().y(), frame_inner_rect().width(), font().glyph_height() + 4);
  299. day_rect.set_y(frame_inner_rect().y() + 4);
  300. } else {
  301. day_rect = Gfx::IntRect(frame_inner_rect());
  302. }
  303. int highlight_rect_width = (font().glyph_width('0') * (m_display_date.length() + 1)) + 2;
  304. auto display_date = (m_date_time.day() == 1 && frame_inner_rect().width() > highlight_rect_width) ? m_display_date : String::number(m_date_time.day());
  305. if (is_today()) {
  306. if (has_grid()) {
  307. auto highlight_rect = Gfx::IntRect(day_rect.width() / 2 - (highlight_rect_width / 2), day_rect.y(), highlight_rect_width, font().glyph_height() + 4);
  308. painter.draw_rect(highlight_rect, palette().base_text());
  309. } else if (is_selected()) {
  310. painter.draw_rect(frame_inner_rect(), palette().base_text());
  311. }
  312. painter.draw_text(day_rect, display_date, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().base_text());
  313. } else if (is_outside_selection()) {
  314. painter.draw_text(day_rect, display_date, Gfx::Font::default_font(), Gfx::TextAlignment::Center, Color::LightGray);
  315. } else {
  316. if (!has_grid() && is_selected())
  317. painter.draw_rect(frame_inner_rect(), palette().base_text());
  318. painter.draw_text(day_rect, display_date, Gfx::Font::default_font(), Gfx::TextAlignment::Center, palette().base_text());
  319. }
  320. }
  321. }