Calendar.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #pragma once
  28. #include <AK/String.h>
  29. #include <LibCore/DateTime.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/Frame.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/Widget.h>
  34. namespace GUI {
  35. class Calendar final : public GUI::Widget {
  36. C_OBJECT(Calendar)
  37. public:
  38. enum Mode {
  39. Month,
  40. Year
  41. };
  42. enum {
  43. ShortNames,
  44. LongNames
  45. };
  46. Calendar(Core::DateTime);
  47. virtual ~Calendar() override;
  48. unsigned int selected_year() const { return m_selected_year; }
  49. unsigned int selected_month() const { return m_selected_month; }
  50. const String selected_calendar_text(bool long_names = ShortNames);
  51. void update_tiles(unsigned int target_year, unsigned int target_month);
  52. void set_selected_calendar(unsigned int year, unsigned int month);
  53. void set_selected_date(Core::DateTime date_time) { m_selected_date = date_time; }
  54. Core::DateTime selected_date() const { return m_selected_date; }
  55. void toggle_mode();
  56. void set_grid(bool grid);
  57. bool has_grid() { return m_grid; }
  58. Mode mode() const { return m_mode; }
  59. Function<void()> on_calendar_tile_click;
  60. Function<void()> on_calendar_tile_doubleclick;
  61. Function<void()> on_month_tile_click;
  62. private:
  63. virtual void resize_event(GUI::ResizeEvent&) override;
  64. class CalendarTile final : public GUI::Frame {
  65. C_OBJECT(CalendarTile)
  66. public:
  67. CalendarTile(int index, Core::DateTime m_date_time);
  68. void update_values(int index, Core::DateTime date_time);
  69. virtual ~CalendarTile() override;
  70. bool is_today() const;
  71. bool is_hovered() const { return m_hovered; }
  72. bool is_selected() const { return m_selected; }
  73. void set_selected(bool b) { m_selected = b; }
  74. bool is_outside_selection() const { return m_outside_selection; }
  75. void set_outside_selection(bool b) { m_outside_selection = b; }
  76. bool has_grid() const { return m_grid; }
  77. void set_grid(bool b) { m_grid = b; }
  78. Core::DateTime get_date_time() { return m_date_time; }
  79. Function<void(int index)> on_doubleclick;
  80. Function<void(int index)> on_click;
  81. private:
  82. virtual void doubleclick_event(GUI::MouseEvent&) override;
  83. virtual void mousedown_event(GUI::MouseEvent&) override;
  84. virtual void enter_event(Core::Event&) override;
  85. virtual void leave_event(Core::Event&) override;
  86. virtual void paint_event(GUI::PaintEvent&) override;
  87. int m_index { 0 };
  88. bool m_outside_selection { false };
  89. bool m_hovered { false };
  90. bool m_selected { false };
  91. bool m_grid { true };
  92. String m_display_date;
  93. Core::DateTime m_date_time;
  94. };
  95. class MonthTile final : public GUI::Button {
  96. C_OBJECT(MonthTile)
  97. public:
  98. MonthTile(int index, Core::DateTime m_date_time);
  99. virtual ~MonthTile() override;
  100. void update_values(Core::DateTime date_time) { m_date_time = date_time; }
  101. Core::DateTime get_date_time() { return m_date_time; }
  102. Function<void(int index)> on_indexed_click;
  103. private:
  104. virtual void mouseup_event(GUI::MouseEvent&) override;
  105. int m_index { 0 };
  106. Core::DateTime m_date_time;
  107. };
  108. RefPtr<MonthTile> m_month_tiles[12];
  109. RefPtr<CalendarTile> m_calendar_tiles[42];
  110. RefPtr<GUI::Label> m_day_names[7];
  111. RefPtr<GUI::Widget> m_week_rows[6];
  112. RefPtr<GUI::Widget> m_month_rows[3];
  113. RefPtr<GUI::Widget> m_month_tile_container;
  114. RefPtr<GUI::Widget> m_calendar_tile_container;
  115. RefPtr<GUI::Widget> m_day_name_container;
  116. Core::DateTime m_selected_date;
  117. Core::DateTime m_previous_selected_date;
  118. unsigned int m_selected_year { 0 };
  119. unsigned int m_selected_month { 0 };
  120. bool m_grid { true };
  121. Mode m_mode { Month };
  122. };
  123. }