week-view: adapt view and widgets to handle RTL

This commit is contained in:
Georges Basile Stavracas Neto
2016-12-08 20:33:34 -02:00
parent 6d4f719829
commit bcfe13bcbc
3 changed files with 72 additions and 12 deletions
+30 -4
View File
@@ -375,6 +375,8 @@ gcal_week_grid_draw (GtkWidget *widget,
GtkBorder padding; GtkBorder padding;
GdkRGBA color; GdkRGBA color;
gboolean ltr;
gdouble x, column_width;
gint i, width, height, today_column; gint i, width, height, today_column;
PangoLayout *layout; PangoLayout *layout;
@@ -383,6 +385,7 @@ gcal_week_grid_draw (GtkWidget *widget,
self = GCAL_WEEK_GRID (widget); self = GCAL_WEEK_GRID (widget);
context = gtk_widget_get_style_context (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget); state = gtk_widget_get_state_flags (widget);
ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
gtk_style_context_add_class (context, "hours"); gtk_style_context_add_class (context, "hours");
gtk_style_context_get_color (context, state, &color); gtk_style_context_get_color (context, state, &color);
@@ -395,6 +398,7 @@ gcal_week_grid_draw (GtkWidget *widget,
width = gtk_widget_get_allocated_width (widget); width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget); height = gtk_widget_get_allocated_height (widget);
column_width = width / 7.0;
cairo_set_line_width (cr, 0.65); cairo_set_line_width (cr, 0.65);
@@ -405,6 +409,7 @@ gcal_week_grid_draw (GtkWidget *widget,
{ {
g_autoptr (GDateTime) now; g_autoptr (GDateTime) now;
GtkBorder margin; GtkBorder margin;
gdouble strip_width;
guint minutes_from_midnight; guint minutes_from_midnight;
gint min_stip_height; gint min_stip_height;
@@ -417,11 +422,18 @@ gcal_week_grid_draw (GtkWidget *widget,
gtk_style_context_get (context, state, "min-height", &min_stip_height, NULL); gtk_style_context_get (context, state, "min-height", &min_stip_height, NULL);
gtk_style_context_get_margin (context, state, &margin); gtk_style_context_get_margin (context, state, &margin);
strip_width = column_width - margin.left - margin.right;
if (ltr)
x = today_column * column_width + margin.left;
else
x = width - (today_column * column_width + margin.right) - strip_width;
gtk_render_background (context, gtk_render_background (context,
cr, cr,
today_column * width / 7.0 + margin.left, x,
round (minutes_from_midnight * ((gdouble) height / MINUTES_PER_DAY) + margin.top), round (minutes_from_midnight * ((gdouble) height / MINUTES_PER_DAY) + margin.top),
width / 7.0 - margin.left - margin.right, strip_width,
MAX (1, min_stip_height - margin.top - margin.bottom)); MAX (1, min_stip_height - margin.top - margin.bottom));
gtk_style_context_restore (context); gtk_style_context_restore (context);
@@ -430,7 +442,12 @@ gcal_week_grid_draw (GtkWidget *widget,
/* Vertical lines */ /* Vertical lines */
for (i = 0; i < 7; i++) for (i = 0; i < 7; i++)
{ {
cairo_move_to (cr, ALIGNED (((width) / 7.0) * i), 0); if (ltr)
x = column_width * i;
else
x = width - column_width * i;
cairo_move_to (cr, ALIGNED (x), 0);
cairo_rel_line_to (cr, 0, height); cairo_rel_line_to (cr, 0, height);
} }
@@ -475,9 +492,11 @@ gcal_week_grid_size_allocate (GtkWidget *widget,
GtkAllocation *allocation) GtkAllocation *allocation)
{ {
GcalWeekGrid *self = GCAL_WEEK_GRID (widget); GcalWeekGrid *self = GCAL_WEEK_GRID (widget);
gboolean ltr;
gdouble minutes_height; gdouble minutes_height;
gdouble column_width; gdouble column_width;
guint i; guint i;
guint x;
/* No need to relayout stuff if nothing changed */ /* No need to relayout stuff if nothing changed */
if (allocation->height == gtk_widget_get_allocated_height (widget) && if (allocation->height == gtk_widget_get_allocated_height (widget) &&
@@ -490,6 +509,8 @@ gcal_week_grid_size_allocate (GtkWidget *widget,
/* Allocate the widget */ /* Allocate the widget */
gtk_widget_set_allocation (widget, allocation); gtk_widget_set_allocation (widget, allocation);
ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
if (gtk_widget_get_realized (widget)) if (gtk_widget_get_realized (widget))
{ {
gdk_window_move_resize (self->event_window, gdk_window_move_resize (self->event_window,
@@ -550,8 +571,13 @@ gcal_week_grid_size_allocate (GtkWidget *widget,
width = column_width / events_at_range - margin.left - margin.right; width = column_width / events_at_range - margin.left - margin.right;
height = MAX ((data->end - data->start) * minutes_height - margin.top - margin.bottom, natural_height); height = MAX ((data->end - data->start) * minutes_height - margin.top - margin.bottom, natural_height);
if (ltr)
x = column_width * i + allocation->x + margin.left + 1;
else
x = allocation->width - width - (column_width * i + allocation->x + margin.left + 1);
/* Setup the child position and size */ /* Setup the child position and size */
child_allocation.x = column_width * i + allocation->x + margin.left + 1; child_allocation.x = x;
child_allocation.y = (data->start % MINUTES_PER_DAY) * minutes_height + margin.top; child_allocation.y = (data->start % MINUTES_PER_DAY) * minutes_height + margin.top;
child_allocation.width = width; child_allocation.width = width;
child_allocation.height = height; child_allocation.height = height;
+26 -4
View File
@@ -1028,19 +1028,25 @@ gcal_week_header_draw (GtkWidget *widget,
gint i, font_height, current_cell, today_column; gint i, font_height, current_cell, today_column;
gint start_x, start_y; gint start_x, start_y;
gboolean ltr;
cairo_save (cr); cairo_save (cr);
/* Fonts and colour selection */ /* Fonts and colour selection */
self = GCAL_WEEK_HEADER (widget); self = GCAL_WEEK_HEADER (widget);
context = gtk_widget_get_style_context (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget); state = gtk_widget_get_state_flags (widget);
ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
start_x = gtk_widget_get_allocated_width (self->expand_button_box); start_x = ltr ? gtk_widget_get_allocated_width (self->expand_button_box) : 0;
start_y = gtk_widget_get_allocated_height (self->header_labels_box); start_y = gtk_widget_get_allocated_height (self->header_labels_box);
gtk_style_context_get_padding (context, state, &padding); gtk_style_context_get_padding (context, state, &padding);
gtk_widget_get_allocation (widget, &alloc); gtk_widget_get_allocation (widget, &alloc);
if (!ltr)
alloc.width -= gtk_widget_get_allocated_width (self->expand_button_box);
gtk_style_context_get_color (context, state, &color); gtk_style_context_get_color (context, state, &color);
gdk_cairo_set_source_rgba (cr, &color); gdk_cairo_set_source_rgba (cr, &color);
@@ -1070,6 +1076,8 @@ gcal_week_header_draw (GtkWidget *widget,
for (i = 0; i < 7; i++) for (i = 0; i < 7; i++)
{ {
gchar *weekday_date, *weekday_abv; gchar *weekday_date, *weekday_abv;
gdouble x;
gint font_width;
gint n_day; gint n_day;
n_day = g_date_time_get_day_of_month (week_start) + i; n_day = g_date_time_get_day_of_month (week_start) + i;
@@ -1090,9 +1098,16 @@ gcal_week_header_draw (GtkWidget *widget,
pango_layout_set_font_description (layout, bold_font); pango_layout_set_font_description (layout, bold_font);
pango_layout_set_text (layout, weekday_date, -1); pango_layout_set_text (layout, weekday_date, -1);
pango_layout_get_pixel_size (layout, &font_width, NULL);
if (ltr)
x = padding.left + cell_width * i + COLUMN_PADDING + start_x;
else
x = alloc.width - (cell_width * i + font_width + COLUMN_PADDING + start_x);
gtk_render_layout (context, gtk_render_layout (context,
cr, cr,
padding.left + cell_width * i + COLUMN_PADDING+ start_x, x,
font_height + padding.bottom + start_y, font_height + padding.bottom + start_y,
layout); layout);
@@ -1111,9 +1126,16 @@ gcal_week_header_draw (GtkWidget *widget,
pango_layout_set_font_description (layout, bold_font); pango_layout_set_font_description (layout, bold_font);
pango_layout_set_text (layout, weekday_abv, -1); pango_layout_set_text (layout, weekday_abv, -1);
pango_layout_get_pixel_size (layout, &font_width, NULL);
if (ltr)
x = padding.left + cell_width * i + COLUMN_PADDING + start_x;
else
x = alloc.width - (cell_width * i + font_width + COLUMN_PADDING + start_x);
gtk_render_layout (context, gtk_render_layout (context,
cr, cr,
padding.left + cell_width * i + COLUMN_PADDING + start_x, x,
start_y, start_y,
layout); layout);
@@ -1129,7 +1151,7 @@ gcal_week_header_draw (GtkWidget *widget,
cairo_set_line_width (cr, 0.25); cairo_set_line_width (cr, 0.25);
cairo_move_to (cr, cairo_move_to (cr,
ALIGNED (cell_width * i + start_x), ALIGNED (ltr ? (cell_width * i + start_x) : (alloc.width - (cell_width * i + start_x))),
font_height + padding.bottom + start_y); font_height + padding.bottom + start_y);
cairo_rel_line_to (cr, cairo_rel_line_to (cr,
+16 -4
View File
@@ -370,13 +370,16 @@ gcal_week_view_draw_hours (GcalWeekView *self,
GtkBorder padding; GtkBorder padding;
GdkRGBA color; GdkRGBA color;
gboolean ltr;
gint i, width, height; gint i, width, height;
gint font_width;
PangoLayout *layout; PangoLayout *layout;
PangoFontDescription *font_desc; PangoFontDescription *font_desc;
context = gtk_widget_get_style_context (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget); state = gtk_widget_get_state_flags (widget);
ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
gtk_style_context_add_class (context, "hours"); gtk_style_context_add_class (context, "hours");
gtk_style_context_get_color (context, state, &color); gtk_style_context_get_color (context, state, &color);
@@ -399,18 +402,21 @@ gcal_week_view_draw_hours (GcalWeekView *self,
if (self->use_24h_format) if (self->use_24h_format)
{ {
hours = g_strdup_printf ("%02d:00", i); hours = g_strdup_printf ("%02d:00", i);
pango_layout_set_text (layout, hours, -1);
} }
else else
{ {
hours = g_strdup_printf ("%d %s", hours = g_strdup_printf ("%d %s",
i % 12 == 0 ? 12 : i % 12, i % 12 == 0 ? 12 : i % 12,
i > 12 ? _("PM") : _("AM")); i > 12 ? _("PM") : _("AM"));
pango_layout_set_text (layout, hours, -1);
} }
cairo_move_to (cr, padding.left, (height / 24) * i + padding.top); pango_layout_set_text (layout, hours, -1);
pango_layout_get_pixel_size (layout, &font_width, NULL);
cairo_move_to (cr,
ltr ? padding.left : width - font_width - padding.right,
(height / 24) * i + padding.top);
pango_cairo_show_layout (cr, layout); pango_cairo_show_layout (cr, layout);
g_free (hours); g_free (hours);
@@ -418,6 +424,12 @@ gcal_week_view_draw_hours (GcalWeekView *self,
cairo_set_line_width (cr, 0.65); cairo_set_line_width (cr, 0.65);
if (!ltr)
{
cairo_move_to (cr, 0.5, 0);
cairo_rel_line_to (cr, 0, height);
}
/* Draws the horizontal complete lines */ /* Draws the horizontal complete lines */
for (i = 1; i < 24; i++) for (i = 1; i < 24; i++)
{ {