Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "CalendarWidget.h"
28#include "AddEventDialog.h"
29#include "Calendar.h"
30#include <LibCore/DateTime.h>
31#include <LibGUI/BoxLayout.h>
32#include <LibGUI/Button.h>
33#include <LibGUI/Painter.h>
34#include <LibGUI/Window.h>
35#include <LibGfx/Font.h>
36#include <LibGfx/Palette.h>
37
38CalendarWidget::CalendarWidget()
39{
40 m_calendar = make<Calendar>(Core::DateTime::now());
41
42 set_fill_with_background_color(true);
43 set_layout<GUI::VerticalBoxLayout>();
44
45 m_top_container = add<Widget>();
46 m_top_container->set_layout<GUI::HorizontalBoxLayout>();
47 m_top_container->layout()->set_margins({ 4, 4, 4, 4 });
48 m_top_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
49 m_top_container->set_preferred_size(0, 45);
50
51 auto& top_left_container = m_top_container->add<Widget>();
52 top_left_container.set_layout<GUI::HorizontalBoxLayout>();
53 top_left_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
54 top_left_container.set_preferred_size(0, 45);
55 m_selected_date_label = top_left_container.add<GUI::Label>(m_calendar->selected_date_text());
56 m_selected_date_label->set_font(Gfx::Font::default_bold_font());
57 m_selected_date_label->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
58 m_selected_date_label->set_preferred_size(80, 14);
59 m_selected_date_label->set_text_alignment(Gfx::TextAlignment::Center);
60
61 m_bottom_container = add<Widget>();
62
63 m_prev_month_button = top_left_container.add<GUI::Button>("<");
64 m_prev_month_button->set_font(Gfx::Font::default_bold_font());
65 m_prev_month_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
66 m_prev_month_button->set_preferred_size(40, 40);
67 m_prev_month_button->on_click = [this] {
68 int m_target_month = m_calendar->selected_month() - 1;
69 int m_target_year = m_calendar->selected_year();
70
71 if (m_calendar->selected_month() <= 1) {
72 m_target_month = 12;
73 m_target_year--;
74 }
75 update_calendar_tiles(m_target_year, m_target_month);
76 };
77
78 m_next_month_button = top_left_container.add<GUI::Button>(">");
79 m_next_month_button->set_font(Gfx::Font::default_bold_font());
80 m_next_month_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
81 m_next_month_button->set_preferred_size(40, 40);
82 m_next_month_button->on_click = [this] {
83 int m_target_month = m_calendar->selected_month() + 1;
84 int m_target_year = m_calendar->selected_year();
85
86 if (m_calendar->selected_month() >= 12) {
87 m_target_month = 1;
88 m_target_year++;
89 }
90 update_calendar_tiles(m_target_year, m_target_month);
91 };
92
93 auto& top_right_container = m_top_container->add<Widget>();
94 top_right_container.set_layout<GUI::HorizontalBoxLayout>();
95 top_right_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
96 top_right_container.set_preferred_size(0, 45);
97
98 top_right_container.layout()->add_spacer();
99
100 m_add_event_button = top_right_container.add<GUI::Button>("Add Event");
101 m_add_event_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
102 m_add_event_button->set_preferred_size(100, 25);
103 m_add_event_button->on_click = [this] {
104 AddEventDialog::show(m_calendar, window());
105 };
106
107 update_calendar_tiles(m_calendar->selected_year(), m_calendar->selected_month());
108}
109
110CalendarWidget::~CalendarWidget()
111{
112}
113void CalendarWidget::resize_event(GUI::ResizeEvent& event)
114{
115 if (event.size().width() < 350) {
116 if (m_next_month_button->size_policy(Orientation::Horizontal) == GUI::SizePolicy::Fixed)
117 m_next_month_button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
118 if (m_prev_month_button->size_policy(Orientation::Horizontal) == GUI::SizePolicy::Fixed)
119 m_prev_month_button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
120 } else {
121 if (m_next_month_button->size_policy(Orientation::Horizontal) == GUI::SizePolicy::Fill)
122 m_next_month_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
123 if (m_prev_month_button->size_policy(Orientation::Horizontal) == GUI::SizePolicy::Fill)
124 m_prev_month_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
125 }
126
127 if (m_top_container->height() > event.size().height() / 3) {
128 if (m_top_container->is_visible())
129 m_top_container->set_visible(false);
130 } else if (!m_top_container->is_visible())
131 m_top_container->set_visible(true);
132
133 int top_container_height = (m_top_container->is_visible()) ? 47 : 0;
134 m_tile_width = event.size().width() / 7;
135 m_tile_height = (event.size().height() - top_container_height) / 5;
136
137 int i = 0;
138 for (int y = 0; y < 5; y++)
139 for (int x = 0; x < 7; x++) {
140 int x_offset = x * m_tile_width;
141 int y_offset = (y * m_tile_height);
142 m_calendar_tiles[i]->set_relative_rect(x_offset, y_offset, m_tile_width, m_tile_height);
143 i++;
144 }
145}
146
147void CalendarWidget::update_calendar_tiles(int target_year, int target_month)
148{
149 unsigned int i = 0;
150 //TODO: Modify m_tile_height if the end of the month doesn't fit onto the current tile array
151 for (int y = 0; y < 5; y++)
152 for (int x = 0; x < 7; x++) {
153 auto date_time = Core::DateTime::create(target_year, target_month, 1);
154 int x_offset = x * m_tile_width;
155 int y_offset = (y * m_tile_height);
156
157 unsigned int start_of_month = date_time.weekday();
158 unsigned int year;
159 unsigned int month;
160 unsigned int day;
161
162 if (start_of_month > i) {
163 month = (target_month - 1 == 0) ? 12 : target_month - 1;
164 year = (month == 12) ? target_year - 1 : target_year;
165 date_time.set_time(year, month, 1);
166 day = (date_time.days_in_month() - (start_of_month) + i) + 1;
167 date_time.set_time(year, month, day);
168
169 } else if ((i - start_of_month) + 1 > date_time.days_in_month()) {
170 month = (target_month + 1) > 12 ? 1 : target_month + 1;
171 year = (month == 1) ? target_year + 1 : target_year;
172 day = ((i - start_of_month) + 1) - date_time.days_in_month();
173 date_time.set_time(year, month, day);
174 } else {
175 month = target_month;
176 year = target_year;
177 day = (i - start_of_month) + 1;
178 date_time.set_time(year, month, day);
179 }
180
181 if (!m_calendar_tiles[i]) {
182 m_calendar_tiles[i] = m_bottom_container->add<CalendarTile>(*m_calendar, i, date_time);
183 m_calendar_tiles[i]->set_frame_thickness(0);
184 m_calendar_tiles[i]->set_relative_rect(x_offset, y_offset, 85, 85);
185 } else {
186 m_calendar_tiles[i]->update_values(*m_calendar, i, date_time);
187 m_calendar_tiles[i]->update();
188 }
189 i++;
190 }
191
192 m_calendar->set_selected_date(target_year, target_month);
193 m_selected_date_label->set_text(m_calendar->selected_date_text());
194}
195
196CalendarWidget::CalendarTile::CalendarTile(Calendar& calendar, int index, Core::DateTime date_time)
197 : m_index(index)
198 , m_date_time(date_time)
199 , m_calendar(calendar)
200{
201 update_values(calendar, index, date_time);
202}
203
204void CalendarWidget::CalendarTile::update_values(Calendar& calendar, int index, Core::DateTime date_time)
205{
206 m_calendar = calendar;
207 m_index = index;
208 m_date_time = date_time;
209 m_display_weekday_name = index < 7;
210
211 if (m_display_weekday_name) {
212 static const String m_day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
213 m_weekday_name = m_day_names[index];
214 }
215
216 m_display_date = (m_date_time.day() == 1) ? String::format("%s %d", name_of_month(m_date_time.month()).characters(), m_date_time.day()) : String::number(m_date_time.day());
217}
218
219CalendarWidget::CalendarTile::~CalendarTile()
220{
221}
222
223void CalendarWidget::CalendarTile::paint_event(GUI::PaintEvent& event)
224{
225 GUI::Frame::paint_event(event);
226
227 GUI::Painter painter(*this);
228 painter.add_clip_rect(frame_inner_rect());
229 painter.fill_rect(frame_inner_rect(), palette().base());
230
231 painter.draw_line(frame_inner_rect().top_right(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
232 if (m_index == 0 || m_index % 7 == 0)
233 painter.draw_line(frame_inner_rect().top_left(), frame_inner_rect().bottom_left(), Color::NamedColor::Black);
234
235 if (m_index < 7)
236 painter.draw_line(frame_inner_rect().top_left(), frame_inner_rect().top_right(), Color::NamedColor::Black);
237 painter.draw_line(frame_inner_rect().bottom_left(), frame_inner_rect().bottom_right(), Color::NamedColor::Black);
238
239 Gfx::Rect day_rect = Gfx::Rect(frame_inner_rect().x(), frame_inner_rect().y(), frame_inner_rect().width(), font().glyph_height() + 4);
240
241 int weekday_characters_width = (font().glyph_width('0') * (m_weekday_name.length() + 1)) + 4;
242 if (m_display_weekday_name && (frame_inner_rect().height() > (font().glyph_height() + 4) * 2) && (frame_inner_rect().width() > weekday_characters_width)) {
243 auto weekday_rect = Gfx::Rect(frame_inner_rect().x(), frame_inner_rect().y(), frame_inner_rect().width(), font().glyph_height() + 4);
244 weekday_rect.set_top(frame_inner_rect().y() + 2);
245 painter.draw_text(weekday_rect, m_weekday_name, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().base_text());
246 day_rect.set_y(frame_inner_rect().y() + 15);
247 } else {
248 day_rect = Gfx::Rect(frame_inner_rect().x(), frame_inner_rect().y(), frame_inner_rect().width(), font().glyph_height() + 4);
249 day_rect.set_y(frame_inner_rect().y() + 4);
250 }
251
252 int highlight_rect_width = (font().glyph_width('0') * (m_display_date.length() + 1)) + 2;
253 auto display_date = (m_date_time.day() == 1 && frame_inner_rect().width() > highlight_rect_width) ? m_display_date : String::number(m_date_time.day());
254
255 if (m_calendar.is_today(m_date_time)) {
256 auto highlight_rect = Gfx::Rect(day_rect.width() / 2 - (highlight_rect_width / 2), day_rect.y(), highlight_rect_width, font().glyph_height() + 4);
257 painter.draw_rect(highlight_rect, palette().base_text());
258 painter.draw_text(day_rect, display_date, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().base_text());
259 } else
260 painter.draw_text(day_rect, display_date, Gfx::TextAlignment::Center, palette().base_text());
261}