From 8d760e61cd8db7e299938f061fd0d9425dbbd12c Mon Sep 17 00:00:00 2001 From: foswret Date: Mon, 4 May 2026 15:04:06 -0500 Subject: draw month grid lines --- src/draw.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 98 insertions(+), 24 deletions(-) (limited to 'src/draw.c') diff --git a/src/draw.c b/src/draw.c index 259f995..2489ada 100644 --- a/src/draw.c +++ b/src/draw.c @@ -22,6 +22,36 @@ struct dimensions { double margin; /* margin */ }; +struct month_info { + int first_day; + int num_of_days; +}; + +struct RGB { + double r; + double g; + double b; +}; + +struct RGB hex_to_rgb(char *str) { + double r, g, b; + unsigned int ri, gi, bi; + struct RGB color; + + sscanf(str, "%02x%02x%02x", &ri, &gi, &bi); + + r = ri; + g = gi; + b = bi; + + printf("%d, %d, %d\n", ri, gi, bi); + + color.r = (r / 255.0); + color.g = (g / 255.0); + color.b = (b / 255.0); + + return color; +} // Get the day of the week for a given day // Mostly used for calculating when the start of a month is @@ -34,6 +64,7 @@ int day_of_week (int d, int m, int y) { + // Fill entire background with current color // Function name is changed for clarity int fill_bg(cairo_t *c) { @@ -43,6 +74,7 @@ int fill_bg(cairo_t *c) { + // Draw text at a certain point. // Needn't be a string pointer, can be fixed array of chars // Requires Font Family (string) and Size (int) @@ -151,18 +183,18 @@ int print_dimensions(struct dimensions *d) { // Calculate minimum amount of rows a month can fit in. // Most months can fit within 4 or 5 rows instead of 6. // TBD: This can probably done in a more efficient way -int calculate_minimum_rows(int first_day, int num_of_days, struct dimensions *d) { +int calculate_minimum_rows(struct month_info *m, struct dimensions *d) { int extra_rows = 0; - int days_in_first_week = (d->column_count - first_day); + int days_in_first_week = (d->column_count - m->first_day); - if (first_day == 0) { - days_in_first_week = first_day; + if (m->first_day == 0) { + days_in_first_week = m->first_day; } - int days_in_last_week = (num_of_days - days_in_first_week) % 7; + int days_in_last_week = (m->num_of_days - days_in_first_week) % 7; - int middle_weeks = (num_of_days - days_in_first_week - days_in_last_week) / 7; + int middle_weeks = (m->num_of_days - days_in_first_week - days_in_last_week) / 7; if (days_in_first_week != 0) { extra_rows++; @@ -180,24 +212,51 @@ int calculate_minimum_rows(int first_day, int num_of_days, struct dimensions *d) // Draws the lines for a month //int draw_month_grid (cairo_t *c, int month, int start_day) { -//int draw_month_grid (cairo_t *c, struct dimensions *d) { -// cairo_move_to (c, d->month_top_corner_x, d->month_top_corner_y); -// return 0; -//} +int draw_month_grid (cairo_t *c, struct month_info *m, struct dimensions *d) { + double cursor_x = d->month_top_corner_x; + + int row_offset = d->row_count - calculate_minimum_rows(m, d); + double cursor_y = d->month_top_corner_y + (row_offset * d->day_height); + + double x = cursor_x; + double y = cursor_y; + + cairo_set_source_rgb (c, 0.0, 0.0, 0.0); + cairo_set_line_width (c, 1.0); + + for (int i = 0; i <= d->column_count; i++) { + cairo_move_to(c, cursor_x, cursor_y); + cairo_line_to(c, cursor_x, cursor_y + (d->month_height - (row_offset * d->day_height))); + cursor_x = cursor_x + d->day_width; + } + + // Recenter the cursor + cursor_x = x; + cursor_y = y; + + for (int i = 0; i <= d->row_count; i++) { + cairo_move_to(c, cursor_x, cursor_y); + cairo_line_to(c, cursor_x + d->month_width, cursor_y); + cursor_y = cursor_y + d->day_height; + } + + cairo_stroke(c); + return 0; +} // Draws one month using daybox sub-surface & context, main context, and dimension struct -int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month, int start_day) { +int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, struct dimensions *d) { double cursor_x = d->month_top_corner_x; - if (start_day != 0) cursor_x = cursor_x + (d->day_width * start_day); + if (m->first_day != 0) cursor_x = cursor_x + (d->day_width * m->first_day); // Push bottom of month as far down the page as the margin allows - int row_offset = d->row_count - calculate_minimum_rows(start_day, days_in_month, d); + int row_offset = d->row_count - calculate_minimum_rows(m, d); double cursor_y = d->month_top_corner_y + (row_offset * d->day_height); - int current_day = start_day; + int current_day = m->first_day; int increment = 0; @@ -207,19 +266,21 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions * for (int k = current_day; k < d->column_count; k++) { // When the increment reached the end of the month, end // the function. - if (increment >= days_in_month) return 0; + if (increment >= m->num_of_days) return 0; + cairo_set_source_rgb (cd, 1.0, 1.0, 1.0); - if ((k == 0) || (k == 6)) { - cairo_set_source_rgb (cd, 255.0, 0.0, 0.0); - } else { - cairo_set_source_rgb (cd, 0.0, 0.0, 255.0); - } + //if ((k == 0) || (k == 6)) { + //cairo_set_source_rgb (cd, 0.76862745098, 0.533333333333, 0.18431372549); + //} else { + // cairo_set_source_rgb (cd, 0.5, 0.5, 0.5); + //} fill_bg(cd); // Change text color - cairo_set_source_rgb (cd, 255.0, 255.0, 255.0); + cairo_set_source_rgb (cd, 0.0, 0.0, 0.0); + //cairo_set_source_rgb (cd, 255.0, 255.0, 255.0); // draw_text(), and by extension pango_layout_set_text(), needs a // string as input, so the current day's date is converted. @@ -229,9 +290,9 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions * // Position within cd is arbitrary: (0,0) is too close to edge draw_text(cd, 4.0, 4.0, "Sans", 10, str); - if ((k == 0) || (k == 6)) { - draw_text(cd, d->day_width - 40.0, d->day_height - 40.0, "Sans", 10, "▨"); - } + //if ((k == 0) || (k == 6)) { + //draw_text(cd, d->day_width - 40.0, d->day_height - 40.0, "Sans", 10, "▨"); + //} // Position sub-surface on main surface @@ -264,3 +325,16 @@ int draw_month_title(cairo_t *c, struct dimensions *d, char *name) { draw_text(c, cursor_x, cursor_y, "Sans", 40, name); return 0; } + + +//int draw_weekday_names(cairo_t *c, struct dimensions *d) { +// +//} + + + + + + + + -- cgit v1.2.3