Calendar.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
  3. * Copyright (c) 2020-2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/DateTime.h>
  8. #include <LibGUI/Calendar.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Window.h>
  11. #include <LibGfx/FontDatabase.h>
  12. #include <LibGfx/Palette.h>
  13. REGISTER_WIDGET(GUI, Calendar);
  14. namespace GUI {
  15. static const char* long_day_names[] = {
  16. "Sunday", "Monday", "Tuesday", "Wednesday",
  17. "Thursday", "Friday", "Saturday"
  18. };
  19. static const char* short_day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  20. static const char* mini_day_names[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
  21. static const char* micro_day_names[] = { "S", "M", "T", "W", "T", "F", "S" };
  22. static const char* long_month_names[] = {
  23. "January", "February", "March", "April", "May", "June",
  24. "July", "August", "September", "October", "November", "December"
  25. };
  26. static const char* short_month_names[] = {
  27. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  28. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  29. };
  30. static const auto extra_large_font = Gfx::BitmapFont::load_from_file("/res/fonts/MarietaRegular36.font");
  31. static const auto large_font = Gfx::BitmapFont::load_from_file("/res/fonts/MarietaRegular24.font");
  32. static const auto medium_font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonRegular14.font");
  33. static const auto small_font = Gfx::BitmapFont::load_from_file("/res/fonts/KaticaRegular10.font");
  34. Calendar::Calendar(Core::DateTime date_time, Mode mode)
  35. : m_selected_date(date_time)
  36. , m_mode(mode)
  37. {
  38. set_fill_with_background_color(true);
  39. for (int i = 0; i < 7; i++) {
  40. Day day;
  41. m_days.append(move(day));
  42. }
  43. for (int i = 0; i < 12; i++) {
  44. MonthTile month;
  45. m_months.append(move(month));
  46. for (int j = 0; j < 42; j++) {
  47. Tile tile;
  48. m_tiles[i].append(move(tile));
  49. }
  50. }
  51. update_tiles(m_selected_date.year(), m_selected_date.month());
  52. }
  53. void Calendar::set_grid(bool show)
  54. {
  55. if (m_grid == show)
  56. return;
  57. m_grid = show;
  58. }
  59. void Calendar::toggle_mode()
  60. {
  61. m_mode == Month ? m_mode = Year : m_mode = Month;
  62. set_show_days_of_the_week(!m_show_days);
  63. set_show_year(!m_show_year);
  64. set_show_month_and_year(!m_show_month_year);
  65. update_tiles(this->view_year(), this->view_month());
  66. this->resize(this->height(), this->width());
  67. invalidate_layout();
  68. }
  69. void Calendar::resize_event(GUI::ResizeEvent& event)
  70. {
  71. m_event_size.set_width(event.size().width() - (frame_thickness() * 2));
  72. m_event_size.set_height(event.size().height() - (frame_thickness() * 2));
  73. if (mode() == Month) {
  74. if (m_event_size.width() < 160 || m_event_size.height() < 130)
  75. set_show_month_and_year(false);
  76. else if (m_event_size.width() >= 160 && m_event_size.height() >= 130)
  77. set_show_month_and_year(true);
  78. set_show_year(false);
  79. const int GRID_LINES = 6;
  80. int tile_width = (m_event_size.width() - GRID_LINES) / 7;
  81. int width_remainder = (m_event_size.width() - GRID_LINES) % 7;
  82. int y_offset = is_showing_days_of_the_week() ? 16 : 0;
  83. y_offset += is_showing_month_and_year() ? 24 : 0;
  84. int tile_height = (m_event_size.height() - y_offset - GRID_LINES) / 6;
  85. int height_remainder = (m_event_size.height() - y_offset - GRID_LINES) % 6;
  86. set_unadjusted_tile_size(tile_width, tile_height);
  87. tile_width < 30 || tile_height < 30 ? set_grid(false) : set_grid(true);
  88. for (int i = 0; i < 42; i++) {
  89. m_tiles[0][i].width = tile_width;
  90. m_tiles[0][i].height = tile_height;
  91. }
  92. for (auto& day : m_days)
  93. day.width = tile_width;
  94. for (int i = 0; i < width_remainder; i++) {
  95. m_days[i].width = (tile_width + 1);
  96. for (int j = i; j < i + 36; j += 7) {
  97. m_tiles[0][j].width = tile_width + 1;
  98. }
  99. }
  100. for (int j = 0; j < height_remainder * 7; j++)
  101. m_tiles[0][j].height = tile_height + 1;
  102. if (is_showing_days_of_the_week()) {
  103. for (int i = 0; i < 7; i++) {
  104. if (m_event_size.width() < 138)
  105. m_days[i].name = micro_day_names[i];
  106. else if (m_event_size.width() < 200)
  107. m_days[i].name = mini_day_names[i];
  108. else if (m_event_size.width() < 480)
  109. m_days[i].name = short_day_names[i];
  110. else
  111. m_days[i].name = long_day_names[i];
  112. }
  113. }
  114. } else {
  115. if (m_event_size.width() < 140 && m_event_size.height() < 120)
  116. set_show_year(false);
  117. else if (m_event_size.width() >= 140 && m_event_size.height() >= 120)
  118. set_show_year(true);
  119. set_show_month_and_year(false);
  120. const int VERT_GRID_LINES = 27;
  121. const int HORI_GRID_LINES = 15;
  122. const int THREADING = 3;
  123. const int MONTH_TITLE = 19;
  124. int tile_width = (m_event_size.width() - VERT_GRID_LINES) / 28;
  125. int width_remainder = (m_event_size.width() - VERT_GRID_LINES) % 28;
  126. int y_offset = is_showing_year() ? 22 : 0;
  127. y_offset += (MONTH_TITLE * 3) + (THREADING * 3);
  128. int tile_height = (m_event_size.height() - y_offset - HORI_GRID_LINES) / 18;
  129. int height_remainder = (m_event_size.height() - y_offset - HORI_GRID_LINES) % 18;
  130. set_grid(false);
  131. set_unadjusted_tile_size(tile_width, tile_height);
  132. if (unadjusted_tile_size().width() < 17 || unadjusted_tile_size().height() < 13)
  133. m_show_month_tiles = true;
  134. else
  135. m_show_month_tiles = false;
  136. if (m_show_month_tiles) {
  137. int month_tile_width = m_event_size.width() / 4;
  138. int width_remainder = m_event_size.width() % 4;
  139. int y_offset = is_showing_year() ? 23 : 0;
  140. int month_tile_height = (m_event_size.height() - y_offset) / 3;
  141. int height_remainder = (m_event_size.height() - y_offset) % 3;
  142. for (int i = 0; i < 12; i++) {
  143. m_months[i].width = month_tile_width;
  144. m_months[i].height = month_tile_height;
  145. if (m_event_size.width() < 250)
  146. m_months[i].name = short_month_names[i];
  147. else
  148. m_months[i].name = long_month_names[i];
  149. }
  150. if (width_remainder) {
  151. for (int i = 0; i < width_remainder; i++) {
  152. for (int j = i; j < 12; j += 4) {
  153. m_months[j].width = month_tile_width + 1;
  154. }
  155. }
  156. }
  157. if (height_remainder) {
  158. for (int i = 0; i < height_remainder * 4; i++) {
  159. m_months[i].height = month_tile_height + 1;
  160. }
  161. }
  162. return;
  163. }
  164. for (int i = 0; i < 12; i++) {
  165. int remainder = 0;
  166. if (i == 0 || i == 4 || i == 8)
  167. remainder = min(width_remainder, 7);
  168. if (i == 1 || i == 5 || i == 9)
  169. width_remainder > 7 ? remainder = min(width_remainder - 7, 7) : remainder = 0;
  170. if (i == 2 || i == 6 || i == 10)
  171. width_remainder > 14 ? remainder = min(width_remainder - 14, 7) : remainder = 0;
  172. if (i == 3 || i == 7 || i == 11)
  173. width_remainder > 21 ? remainder = width_remainder - 21 : remainder = 0;
  174. m_month_size[i].set_width(remainder + 6 + tile_width * 7);
  175. if (i >= 0 && i <= 3)
  176. remainder = min(height_remainder, 6);
  177. if (i >= 4 && i <= 7)
  178. height_remainder > 6 ? remainder = min(height_remainder - 6, 6) : remainder = 0;
  179. if (i >= 8 && i <= 12)
  180. height_remainder > 12 ? remainder = height_remainder - 12 : remainder = 0;
  181. m_month_size[i].set_height(remainder + 5 + tile_height * 6);
  182. for (int j = 0; j < 42; j++) {
  183. m_tiles[i][j].width = tile_width;
  184. m_tiles[i][j].height = tile_height;
  185. }
  186. }
  187. if (width_remainder) {
  188. for (int i = 0; i < 12; i += 4) {
  189. for (int j = 0; j < min(width_remainder, 7); j++) {
  190. for (int k = j; k < j + 36; k += 7) {
  191. m_tiles[i][k].width = tile_width + 1;
  192. }
  193. }
  194. }
  195. }
  196. if (width_remainder > 7) {
  197. for (int i = 1; i < 12; i += 4) {
  198. for (int j = 0; j < min(width_remainder - 7, 7); j++) {
  199. for (int k = j; k < j + 36; k += 7) {
  200. m_tiles[i][k].width = tile_width + 1;
  201. }
  202. }
  203. }
  204. }
  205. if (width_remainder > 14) {
  206. for (int i = 2; i < 12; i += 4) {
  207. for (int j = 0; j < min(width_remainder - 14, 7); j++) {
  208. for (int k = j; k < j + 36; k += 7) {
  209. m_tiles[i][k].width = tile_width + 1;
  210. }
  211. }
  212. }
  213. }
  214. if (width_remainder > 21) {
  215. for (int i = 3; i < 12; i += 4) {
  216. for (int j = 0; j < width_remainder - 21; j++) {
  217. for (int k = j; k < j + 36; k += 7) {
  218. m_tiles[i][k].width = tile_width + 1;
  219. }
  220. }
  221. }
  222. }
  223. if (height_remainder) {
  224. for (int i = 0; i < 4; i++) {
  225. for (int j = 0; j < min(height_remainder, 6) * 7; j++) {
  226. m_tiles[i][j].height = tile_height + 1;
  227. }
  228. }
  229. }
  230. if (height_remainder > 6) {
  231. for (int i = 4; i < 8; i++) {
  232. for (int j = 0; j < min(height_remainder - 6, 6) * 7; j++) {
  233. m_tiles[i][j].height = tile_height + 1;
  234. }
  235. }
  236. }
  237. if (height_remainder > 12) {
  238. for (int i = 8; i < 12; i++) {
  239. for (int j = 0; j < (height_remainder - 12) * 7; j++) {
  240. m_tiles[i][j].height = tile_height + 1;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. void Calendar::update_tiles(unsigned view_year, unsigned view_month)
  247. {
  248. set_view_date(view_year, view_month);
  249. unsigned months;
  250. mode() == Month ? months = 1 : months = 12;
  251. for (unsigned i = 0; i < months; i++) {
  252. if (mode() == Year)
  253. view_month = i + 1;
  254. for (unsigned j = 0; j < 42; j++) {
  255. auto date_time = Core::DateTime::create(view_year, view_month, 1);
  256. unsigned start_of_month = date_time.weekday();
  257. unsigned year;
  258. unsigned month;
  259. unsigned day;
  260. if (start_of_month == 0 && mode() != Year) {
  261. month = (view_month - 1 == 0) ? 12 : view_month - 1;
  262. year = (month == 12) ? view_year - 1 : view_year;
  263. date_time.set_time(year, month, 1);
  264. day = (date_time.days_in_month() - 6 + j);
  265. } else if (start_of_month > j) {
  266. month = (view_month - 1 == 0) ? 12 : view_month - 1;
  267. year = (month == 12) ? view_year - 1 : view_year;
  268. date_time.set_time(year, month, 1);
  269. day = (date_time.days_in_month() - (start_of_month) + j) + 1;
  270. } else if ((j - start_of_month) + 1 > date_time.days_in_month()) {
  271. month = (view_month + 1) > 12 ? 1 : view_month + 1;
  272. year = (month == 1) ? view_year + 1 : view_year;
  273. day = ((j - start_of_month) + 1) - date_time.days_in_month();
  274. } else {
  275. month = view_month;
  276. year = view_year;
  277. day = (j - start_of_month) + 1;
  278. }
  279. date_time.set_time(year, month, day);
  280. m_tiles[i][j].date_time = date_time;
  281. m_tiles[i][j].is_outside_selected_month = (date_time.month() != view_month
  282. || date_time.year() != view_year);
  283. m_tiles[i][j].is_selected = (date_time.year() == m_selected_date.year()
  284. && date_time.month() == m_selected_date.month()
  285. && date_time.day() == m_selected_date.day()
  286. && (mode() == Year ? !m_tiles[i][j].is_outside_selected_month : true));
  287. m_tiles[i][j].is_today = (date_time.day() == Core::DateTime::now().day()
  288. && date_time.month() == Core::DateTime::now().month()
  289. && date_time.year() == Core::DateTime::now().year());
  290. }
  291. }
  292. update();
  293. }
  294. String Calendar::formatted_date(Format format)
  295. {
  296. switch (format) {
  297. case ShortMonthYear:
  298. return String::formatted("{} {}", short_month_names[view_month() - 1], view_year());
  299. case LongMonthYear:
  300. return String::formatted("{} {}", long_month_names[view_month() - 1], view_year());
  301. case MonthOnly:
  302. return String::formatted("{}", long_month_names[view_month() - 1]);
  303. case YearOnly:
  304. return String::number(view_year());
  305. default:
  306. VERIFY_NOT_REACHED();
  307. }
  308. }
  309. void Calendar::paint_event(GUI::PaintEvent& event)
  310. {
  311. GUI::Frame::paint_event(event);
  312. GUI::Painter painter(*this);
  313. painter.add_clip_rect(frame_inner_rect());
  314. painter.add_clip_rect(event.rect());
  315. if (has_grid())
  316. painter.fill_rect(frame_inner_rect(), palette().threed_shadow2());
  317. else
  318. painter.fill_rect(frame_inner_rect(), palette().base());
  319. painter.translate(frame_thickness(), frame_thickness());
  320. int width = unadjusted_tile_size().width();
  321. int height = unadjusted_tile_size().height();
  322. int x_offset = 0;
  323. int y_offset = 0;
  324. if (is_showing_year()) {
  325. auto year_only_rect = Gfx::IntRect(
  326. 0,
  327. 0,
  328. frame_inner_rect().width(),
  329. 22);
  330. y_offset += year_only_rect.height();
  331. painter.fill_rect(year_only_rect, palette().hover_highlight());
  332. painter.draw_text(year_only_rect, formatted_date(YearOnly), medium_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  333. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, (!m_show_month_tiles ? palette().threed_shadow1() : palette().threed_shadow2()), 1);
  334. y_offset += 1;
  335. if (!m_show_month_tiles) {
  336. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, palette().threed_highlight(), 1);
  337. y_offset += 1;
  338. }
  339. } else if (is_showing_month_and_year()) {
  340. auto month_year_rect = Gfx::IntRect(
  341. 0,
  342. 0,
  343. frame_inner_rect().width(),
  344. 22);
  345. painter.fill_rect(month_year_rect, palette().hover_highlight());
  346. month_year_rect.set_width(frame_inner_rect().width() / 2);
  347. painter.draw_text(month_year_rect, formatted_date(MonthOnly), medium_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  348. month_year_rect.set_x(month_year_rect.width() + (frame_inner_rect().width() % 2 ? 1 : 0));
  349. painter.draw_text(month_year_rect, formatted_date(YearOnly), medium_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  350. y_offset += 22;
  351. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, palette().threed_shadow1(), 1);
  352. y_offset += 1;
  353. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, palette().threed_highlight(), 1);
  354. y_offset += 1;
  355. }
  356. if (mode() == Year && m_show_month_tiles) {
  357. int i = 0;
  358. for (int j = 0; j < 3; j++) {
  359. x_offset = 0;
  360. for (int k = 0; k < 4; k++) {
  361. if (k > 0)
  362. x_offset += m_months[i - 1].width;
  363. auto month_tile_rect = Gfx::IntRect(
  364. x_offset,
  365. y_offset,
  366. m_months[i].width,
  367. m_months[i].height);
  368. m_months[i].rect = month_tile_rect.translated(frame_thickness(), frame_thickness());
  369. Gfx::StylePainter::paint_button(
  370. painter, month_tile_rect, palette(),
  371. Gfx::ButtonStyle::Normal,
  372. m_months[i].is_being_pressed,
  373. m_months[i].is_hovered,
  374. false, true, false);
  375. set_font(small_font);
  376. painter.draw_text(month_tile_rect, m_months[i].name, font(), Gfx::TextAlignment::Center, palette().base_text());
  377. i++;
  378. }
  379. y_offset += m_months[i - 1].height;
  380. }
  381. return;
  382. }
  383. if (is_showing_days_of_the_week()) {
  384. auto days_of_the_week_rect = Gfx::IntRect(
  385. 0,
  386. y_offset,
  387. frame_inner_rect().width(),
  388. 16);
  389. painter.fill_rect(days_of_the_week_rect, palette().hover_highlight());
  390. for (int i = 0; i < 7; i++) {
  391. if (i > 0)
  392. x_offset += m_days[i - 1].width + 1;
  393. Gfx::IntRect day_rect = Gfx::IntRect(
  394. x_offset,
  395. y_offset,
  396. m_days[i].width,
  397. 16);
  398. painter.draw_text(day_rect, m_days[i].name, small_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  399. }
  400. y_offset += days_of_the_week_rect.height();
  401. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, palette().threed_shadow2(), 1);
  402. y_offset += 1;
  403. }
  404. if (mode() == Month) {
  405. int i = 0;
  406. for (int j = 0; j < 6; j++) {
  407. x_offset = 0;
  408. if (j > 0)
  409. y_offset += m_tiles[0][(j - 1) * 7].height + 1;
  410. for (int k = 0; k < 7; k++) {
  411. if (k > 0)
  412. x_offset += m_tiles[0][k - 1].width + 1;
  413. auto tile_rect = Gfx::IntRect(
  414. x_offset,
  415. y_offset,
  416. m_tiles[0][i].width,
  417. m_tiles[0][i].height);
  418. m_tiles[0][i].rect = tile_rect.translated(frame_thickness(), frame_thickness());
  419. if (m_tiles[0][i].is_hovered || m_tiles[0][i].is_selected)
  420. painter.fill_rect(tile_rect, palette().hover_highlight());
  421. else
  422. painter.fill_rect(tile_rect, palette().base());
  423. auto text_alignment = Gfx::TextAlignment::TopRight;
  424. auto text_rect = Gfx::IntRect(
  425. x_offset,
  426. y_offset + 4,
  427. m_tiles[0][i].width - 4,
  428. font().glyph_height() + 4);
  429. if (width > 150 && height > 150) {
  430. set_font(extra_large_font);
  431. } else if (width > 100 && height > 100) {
  432. set_font(large_font);
  433. } else if (width > 50 && height > 50) {
  434. set_font(medium_font);
  435. } else if (width >= 30 && height >= 30) {
  436. set_font(small_font);
  437. } else {
  438. set_font(small_font);
  439. text_alignment = Gfx::TextAlignment::Center;
  440. text_rect = Gfx::IntRect(tile_rect);
  441. }
  442. auto display_date = String::number(m_tiles[0][i].date_time.day());
  443. if (m_tiles[0][i].is_selected && (width < 30 || height < 30))
  444. painter.draw_rect(tile_rect, palette().base_text());
  445. if (m_tiles[0][i].is_today && !m_tiles[0][i].is_outside_selected_month) {
  446. painter.draw_text(text_rect, display_date, font().bold_variant(), text_alignment, palette().base_text());
  447. } else if (m_tiles[0][i].is_outside_selected_month) {
  448. painter.draw_text(text_rect, display_date, m_tiles[0][i].is_today ? font().bold_variant() : font(), text_alignment, Color::LightGray);
  449. } else {
  450. painter.draw_text(text_rect, display_date, font(), text_alignment, palette().base_text());
  451. }
  452. i++;
  453. }
  454. }
  455. } else {
  456. for (int i = 0; i < 4; i++) {
  457. static int x_month_offset;
  458. x_month_offset += (i > 0 ? m_month_size[i - 1].width() + 1 : 0);
  459. auto month_rect = Gfx::IntRect(
  460. x_month_offset,
  461. y_offset,
  462. m_month_size[i].width(),
  463. 19);
  464. painter.fill_rect(month_rect, palette().hover_highlight());
  465. painter.draw_text(month_rect, long_month_names[i], medium_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  466. if (i > 0 && i < 4) {
  467. painter.draw_line({ x_month_offset - 1, y_offset - 1 }, { x_month_offset - 1, y_offset + 18 }, palette().threed_shadow2(), 1);
  468. painter.draw_line({ x_month_offset, y_offset - 1 }, { x_month_offset, y_offset + 18 }, palette().threed_highlight(), 1);
  469. }
  470. if (i == 3)
  471. x_month_offset = 0;
  472. }
  473. y_offset += 19;
  474. painter.draw_line({ 0, y_offset }, { frame_inner_rect().width(), y_offset }, palette().threed_shadow2(), 1);
  475. y_offset += 1;
  476. int x_translation = 0;
  477. int y_translation = y_offset;
  478. for (int l = 0; l < 12; l++) {
  479. if ((l > 0 && l < 4) || (l > 4 && l < 8) || (l > 8)) {
  480. x_translation += m_month_size[l - 1].width() + 1;
  481. } else if (l % 4 == 0) {
  482. x_translation = 0;
  483. }
  484. if (l < 4 || (l > 4 && l < 8) || l > 8) {
  485. y_offset = y_translation;
  486. } else if (l == 4 || l == 8) {
  487. y_translation += m_month_size[l - 1].height();
  488. painter.draw_line({ 0, y_translation }, { frame_inner_rect().width(), y_translation }, palette().threed_shadow1(), 1);
  489. y_translation += 1;
  490. painter.draw_line({ 0, y_translation }, { frame_inner_rect().width(), y_translation }, palette().threed_highlight(), 1);
  491. y_translation += 1;
  492. y_offset = y_translation;
  493. for (int i = l; i < (l == 4 ? 8 : 12); i++) {
  494. static int x_month_offset;
  495. x_month_offset += (i > (l == 4 ? 4 : 8) ? m_month_size[i - 1].width() + 1 : 0);
  496. auto month_rect = Gfx::IntRect(
  497. x_month_offset,
  498. y_offset,
  499. m_month_size[i].width(),
  500. 19);
  501. painter.fill_rect(month_rect, palette().hover_highlight());
  502. painter.draw_text(month_rect, long_month_names[i], medium_font->bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  503. if (i > (l == 4 ? 4 : 8) && i < (l == 4 ? 8 : 12)) {
  504. painter.draw_line({ x_month_offset - 1, y_offset - 1 }, { x_month_offset - 1, y_offset + 18 }, palette().threed_shadow2(), 1);
  505. painter.draw_line({ x_month_offset, y_offset - 1 }, { x_month_offset, y_offset + 18 }, palette().threed_highlight(), 1);
  506. }
  507. if (i == 7 || i == 11)
  508. x_month_offset = 0;
  509. }
  510. y_translation += 19;
  511. painter.draw_line({ 0, y_translation }, { frame_inner_rect().width(), y_translation }, palette().threed_shadow2(), 1);
  512. y_translation += 1;
  513. y_offset = y_translation;
  514. }
  515. int i = 0;
  516. for (int j = 0; j < 6; j++) {
  517. x_offset = 0;
  518. if (j > 0)
  519. y_offset += m_tiles[l][(j - 1) * 7].height + (j < 6 ? 1 : 0);
  520. if (j == 0 && l != 3 && l != 7 && l != 11) {
  521. painter.draw_line(
  522. { m_month_size[l].width() + x_translation, y_offset },
  523. { m_month_size[l].width() + x_translation, y_offset + m_month_size[l].height() },
  524. palette().threed_shadow2(),
  525. 1);
  526. }
  527. for (int k = 0; k < 7; k++) {
  528. if (k > 0)
  529. x_offset += m_tiles[l][k - 1].width + 1;
  530. auto tile_rect = Gfx::IntRect(
  531. x_offset + x_translation,
  532. y_offset,
  533. m_tiles[l][i].width,
  534. m_tiles[l][i].height);
  535. m_tiles[l][i].rect = tile_rect.translated(frame_thickness(), frame_thickness());
  536. if (m_tiles[l][i].is_hovered || m_tiles[l][i].is_selected)
  537. painter.fill_rect(tile_rect, palette().hover_highlight());
  538. else
  539. painter.fill_rect(tile_rect, palette().base());
  540. if (width > 50 && height > 50) {
  541. set_font(medium_font);
  542. } else {
  543. set_font(small_font);
  544. }
  545. auto display_date = String::number(m_tiles[l][i].date_time.day());
  546. if (m_tiles[l][i].is_selected)
  547. painter.draw_rect(tile_rect, palette().base_text());
  548. if (m_tiles[l][i].is_today && !m_tiles[l][i].is_outside_selected_month) {
  549. painter.draw_text(tile_rect, display_date, font().bold_variant(), Gfx::TextAlignment::Center, palette().base_text());
  550. } else if (!m_tiles[l][i].is_outside_selected_month) {
  551. painter.draw_text(tile_rect, display_date, font(), Gfx::TextAlignment::Center, palette().base_text());
  552. }
  553. i++;
  554. }
  555. }
  556. }
  557. }
  558. }
  559. void Calendar::leave_event(Core::Event&)
  560. {
  561. int months;
  562. mode() == Month ? months = 1 : months = 12;
  563. for (int i = 0; i < months; i++) {
  564. if (mode() == Year && m_show_month_tiles) {
  565. m_months[i].is_hovered = false;
  566. continue;
  567. } else {
  568. for (int j = 0; j < 42; j++) {
  569. m_tiles[i][j].is_hovered = false;
  570. }
  571. }
  572. }
  573. update();
  574. }
  575. void Calendar::mousemove_event(GUI::MouseEvent& event)
  576. {
  577. static int last_index_i;
  578. static int last_index_j;
  579. if (mode() == Year && m_show_month_tiles) {
  580. if (m_months[last_index_i].rect.contains(event.position()) && (m_months[last_index_i].is_hovered || m_months[last_index_i].is_being_pressed)) {
  581. return;
  582. } else {
  583. m_months[last_index_i].is_hovered = false;
  584. m_months[last_index_i].is_being_pressed = false;
  585. update(m_months[last_index_i].rect);
  586. }
  587. } else {
  588. if (m_tiles[last_index_i][last_index_j].rect.contains(event.position()) && m_tiles[last_index_i][last_index_j].is_hovered) {
  589. return;
  590. } else {
  591. m_tiles[last_index_i][last_index_j].is_hovered = false;
  592. update(m_tiles[last_index_i][last_index_j].rect);
  593. }
  594. }
  595. int months;
  596. mode() == Month ? months = 1 : months = 12;
  597. for (int i = 0; i < months; i++) {
  598. if (mode() == Year && m_show_month_tiles) {
  599. if (m_months[i].rect.contains(event.position())) {
  600. if (m_currently_pressed_index == -1 || m_currently_pressed_index == i)
  601. m_months[i].is_hovered = true;
  602. if (m_currently_pressed_index == i)
  603. m_months[i].is_being_pressed = true;
  604. update(m_months[last_index_i].rect);
  605. if (m_months[i].is_being_pressed == true)
  606. m_currently_pressed_index = i;
  607. last_index_i = i;
  608. update(m_months[i].rect);
  609. break;
  610. }
  611. } else {
  612. for (int j = 0; j < 42; j++) {
  613. if (mode() == Year && m_tiles[i][j].is_outside_selected_month)
  614. continue;
  615. if (m_tiles[i][j].rect.contains(event.position())) {
  616. m_tiles[i][j].is_hovered = true;
  617. update(m_tiles[last_index_i][last_index_j].rect);
  618. last_index_i = i;
  619. last_index_j = j;
  620. update(m_tiles[i][j].rect);
  621. break;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. void Calendar::mouseup_event(GUI::MouseEvent& event)
  628. {
  629. int months;
  630. mode() == Month ? months = 1 : months = 12;
  631. for (int i = 0; i < months; i++) {
  632. if (mode() == Year && m_show_month_tiles) {
  633. if (m_months[i].rect.contains(event.position()) && m_months[i].is_being_pressed) {
  634. set_view_date(view_year(), (unsigned)i + 1);
  635. toggle_mode();
  636. if (on_month_click)
  637. on_month_click();
  638. }
  639. } else {
  640. for (int j = 0; j < 42; j++) {
  641. if (mode() == Year && m_tiles[i][j].is_outside_selected_month)
  642. continue;
  643. if (m_tiles[i][j].rect.contains(event.position())) {
  644. m_previous_selected_date = m_selected_date;
  645. m_selected_date = m_tiles[i][j].date_time;
  646. update_tiles(m_selected_date.year(), m_selected_date.month());
  647. if (on_tile_click)
  648. on_tile_click();
  649. }
  650. }
  651. }
  652. if (months == 12) {
  653. m_months[i].is_being_pressed = false;
  654. m_months[i].is_hovered = false;
  655. }
  656. }
  657. m_currently_pressed_index = -1;
  658. update();
  659. }
  660. void Calendar::mousedown_event(GUI::MouseEvent& event)
  661. {
  662. if (mode() == Year && m_show_month_tiles) {
  663. for (int i = 0; i < 12; i++) {
  664. if (m_months[i].rect.contains(event.position())) {
  665. m_months[i].is_being_pressed = true;
  666. m_currently_pressed_index = i;
  667. update(m_months[i].rect);
  668. break;
  669. }
  670. }
  671. }
  672. }
  673. void Calendar::doubleclick_event(GUI::MouseEvent& event)
  674. {
  675. int months;
  676. mode() == Month ? months = 1 : months = 12;
  677. for (int i = 0; i < months; i++) {
  678. for (int j = 0; j < 42; j++) {
  679. if (m_tiles[i][j].date_time.day() != m_previous_selected_date.day())
  680. continue;
  681. if (mode() == Year && m_tiles[i][j].is_outside_selected_month)
  682. continue;
  683. if (m_tiles[i][j].rect.contains(event.position())) {
  684. if (on_tile_doubleclick)
  685. on_tile_doubleclick();
  686. }
  687. }
  688. }
  689. }
  690. }