Calendar.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #pragma once
  8. #include <AK/String.h>
  9. #include <LibConfig/Listener.h>
  10. #include <LibCore/DateTime.h>
  11. #include <LibGUI/Frame.h>
  12. #include <LibGUI/Widget.h>
  13. namespace GUI {
  14. class Calendar final
  15. : public GUI::Frame
  16. , public Config::Listener {
  17. C_OBJECT(Calendar)
  18. public:
  19. enum Mode {
  20. Month,
  21. Year
  22. };
  23. enum Format {
  24. ShortMonthYear,
  25. LongMonthYear,
  26. MonthOnly,
  27. YearOnly
  28. };
  29. void set_selected_date(Core::DateTime date_time) { m_selected_date = date_time; }
  30. Core::DateTime selected_date() const { return m_selected_date; }
  31. void set_view_date(unsigned year, unsigned month)
  32. {
  33. m_view_year = year;
  34. m_view_month = month;
  35. }
  36. unsigned view_year() const { return m_view_year; }
  37. unsigned view_month() const { return m_view_month; }
  38. String formatted_date(Format format = LongMonthYear);
  39. Mode mode() const { return m_mode; }
  40. void toggle_mode();
  41. void update_tiles(unsigned year, unsigned month);
  42. void set_grid(bool);
  43. bool has_grid() const { return m_grid; }
  44. void set_show_year(bool b) { m_show_year = b; }
  45. bool is_showing_year() const { return m_show_year; }
  46. void set_show_month_and_year(bool b) { m_show_month_year = b; }
  47. bool is_showing_month_and_year() const { return m_show_month_year; }
  48. void set_show_days_of_the_week(bool b) { m_show_days = b; }
  49. bool is_showing_days_of_the_week() const { return m_show_days; }
  50. Gfx::IntSize unadjusted_tile_size() const { return m_unadjusted_tile_size; }
  51. void set_unadjusted_tile_size(int width, int height)
  52. {
  53. m_unadjusted_tile_size.set_width(width);
  54. m_unadjusted_tile_size.set_height(height);
  55. }
  56. virtual void config_string_did_change(String const&, String const&, String const&, String const&) override;
  57. Function<void()> on_tile_click;
  58. Function<void()> on_tile_doubleclick;
  59. Function<void()> on_month_click;
  60. private:
  61. Calendar(Core::DateTime date_time = Core::DateTime::now(), Mode mode = Month);
  62. virtual ~Calendar() override = default;
  63. static size_t day_of_week_index(String const&);
  64. virtual void resize_event(GUI::ResizeEvent&) override;
  65. virtual void paint_event(GUI::PaintEvent&) override;
  66. virtual void mousemove_event(GUI::MouseEvent&) override;
  67. virtual void mousedown_event(MouseEvent&) override;
  68. virtual void mouseup_event(MouseEvent&) override;
  69. virtual void doubleclick_event(MouseEvent&) override;
  70. virtual void leave_event(Core::Event&) override;
  71. struct Day {
  72. String name;
  73. int width { 0 };
  74. int height { 16 };
  75. };
  76. Vector<Day> m_days;
  77. struct MonthTile {
  78. String name;
  79. Gfx::IntRect rect;
  80. int width { 0 };
  81. int height { 0 };
  82. bool is_hovered { false };
  83. bool is_being_pressed { false };
  84. };
  85. Vector<MonthTile> m_months;
  86. struct Tile {
  87. unsigned year;
  88. unsigned month;
  89. unsigned day;
  90. Gfx::IntRect rect;
  91. int width { 0 };
  92. int height { 0 };
  93. bool is_today { false };
  94. bool is_selected { false };
  95. bool is_hovered { false };
  96. bool is_outside_selected_month { false };
  97. };
  98. Vector<Tile> m_tiles[12];
  99. bool m_grid { true };
  100. bool m_show_month_year { true };
  101. bool m_show_days { true };
  102. bool m_show_year { false };
  103. bool m_show_month_tiles { false };
  104. int m_currently_pressed_index { -1 };
  105. unsigned m_view_year;
  106. unsigned m_view_month;
  107. Core::DateTime m_selected_date;
  108. Core::DateTime m_previous_selected_date;
  109. Gfx::IntSize m_unadjusted_tile_size;
  110. Gfx::IntSize m_event_size;
  111. Gfx::IntSize m_month_size[12];
  112. Mode m_mode { Month };
  113. enum class DayOfWeek {
  114. Sunday,
  115. Monday,
  116. Tuesday,
  117. Wednesday,
  118. Thursday,
  119. Friday,
  120. Saturday
  121. };
  122. DayOfWeek m_first_day_of_week { DayOfWeek::Sunday };
  123. };
  124. }