Commit 2a9d130e authored by Matthias Clasen's avatar Matthias Clasen

GtkScale: Normalize mark positions

This is in preparation for making scale marks styling better.
As a side-effect, TOP and LEFT position are treated the same now,
as well as BOTTOM and RIGHT.

Based on a patch by Bastien Nocera,
https://bugzilla.gnome.org/show_bug.cgi?id=643685
parent a47a7abe
...@@ -96,7 +96,7 @@ struct _GtkScaleMark ...@@ -96,7 +96,7 @@ struct _GtkScaleMark
{ {
gdouble value; gdouble value;
gchar *markup; gchar *markup;
GtkPositionType position; GtkPositionType position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */
}; };
enum { enum {
...@@ -792,9 +792,10 @@ gtk_scale_get_range_border (GtkRange *range, ...@@ -792,9 +792,10 @@ gtk_scale_get_range_border (GtkRange *range,
NULL); NULL);
gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL) if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL)
{ {
gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
if (n1 > 0) if (n1 > 0)
border->top += h1 + value_spacing + slider_width / 2; border->top += h1 + value_spacing + slider_width / 2;
if (n2 > 0) if (n2 > 0)
...@@ -802,7 +803,6 @@ gtk_scale_get_range_border (GtkRange *range, ...@@ -802,7 +803,6 @@ gtk_scale_get_range_border (GtkRange *range,
} }
else else
{ {
gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2);
if (n1 > 0) if (n1 > 0)
border->left += w1 + value_spacing + slider_width / 2; border->left += w1 + value_spacing + slider_width / 2;
if (n2 > 0) if (n2 > 0)
...@@ -991,7 +991,7 @@ gtk_scale_get_preferred_height (GtkWidget *widget, ...@@ -991,7 +991,7 @@ gtk_scale_get_preferred_height (GtkWidget *widget,
gtk_widget_style_get (widget, "slider-length", &slider_length, NULL); gtk_widget_style_get (widget, "slider-length", &slider_length, NULL);
gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2); gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2);
h1 = (n1 - 1) * h1 + MAX (h1, slider_length); h1 = (n1 - 1) * h1 + MAX (h1, slider_length);
h2 = (n2 - 1) * h1 + MAX (h2, slider_length); h2 = (n2 - 1) * h1 + MAX (h2, slider_length);
h = MAX (h1, h2); h = MAX (h1, h2);
...@@ -1021,7 +1021,7 @@ find_next_pos (GtkWidget *widget, ...@@ -1021,7 +1021,7 @@ find_next_pos (GtkWidget *widget,
} }
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM) if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
return allocation.width; return allocation.width;
else else
return allocation.height; return allocation.height;
...@@ -1095,15 +1095,14 @@ gtk_scale_draw (GtkWidget *widget, ...@@ -1095,15 +1095,14 @@ gtk_scale_draw (GtkWidget *widget,
y1 = range_rect.y + range_rect.height; y1 = range_rect.y + range_rect.height;
y2 = y1 + slider_width / 2; y2 = y1 + slider_width / 2;
min_pos = min_pos_after; min_pos = min_pos_after;
max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep; max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
} }
gtk_style_context_save (context); gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK); gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
gtk_style_context_set_state (context, state); gtk_style_context_set_state (context, state);
gtk_render_line (context, cr, gtk_render_line (context, cr, x1, y1, x1, y2);
x1, y1, x1, y2);
if (mark->markup) if (mark->markup)
{ {
...@@ -1128,27 +1127,26 @@ gtk_scale_draw (GtkWidget *widget, ...@@ -1128,27 +1127,26 @@ gtk_scale_draw (GtkWidget *widget,
min_pos_after = x3 + logical_rect.width + min_sep; min_pos_after = x3 + logical_rect.width + min_sep;
} }
gtk_render_layout (context, cr, gtk_render_layout (context, cr, x3, y3, layout);
x3, y3, layout);
} }
gtk_style_context_restore (context); gtk_style_context_restore (context);
} }
else else
{ {
if (mark->position == GTK_POS_LEFT) if (mark->position == GTK_POS_TOP)
{ {
x1 = range_rect.x; x1 = range_rect.x;
x2 = range_rect.x - slider_width / 2; x2 = range_rect.x - slider_width / 2;
min_pos = min_pos_before; min_pos = min_pos_before;
max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep; max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep;
} }
else else
{ {
x1 = range_rect.x + range_rect.width; x1 = range_rect.x + range_rect.width;
x2 = range_rect.x + range_rect.width + slider_width / 2; x2 = range_rect.x + range_rect.width + slider_width / 2;
min_pos = min_pos_after; min_pos = min_pos_after;
max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep; max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM, 1) - min_sep;
} }
y1 = marks[i]; y1 = marks[i];
...@@ -1156,8 +1154,7 @@ gtk_scale_draw (GtkWidget *widget, ...@@ -1156,8 +1154,7 @@ gtk_scale_draw (GtkWidget *widget,
gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK); gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK);
gtk_style_context_set_state (context, state); gtk_style_context_set_state (context, state);
gtk_render_line (context, cr, gtk_render_line (context, cr, x1, y1, x2, y1);
x1, y1, x2, y1);
if (mark->markup) if (mark->markup)
{ {
...@@ -1171,7 +1168,7 @@ gtk_scale_draw (GtkWidget *widget, ...@@ -1171,7 +1168,7 @@ gtk_scale_draw (GtkWidget *widget,
y3 = max_pos - logical_rect.height; y3 = max_pos - logical_rect.height;
if (y3 < 0) if (y3 < 0)
y3 = 0; y3 = 0;
if (mark->position == GTK_POS_LEFT) if (mark->position == GTK_POS_TOP)
{ {
x3 = x2 - value_spacing - logical_rect.width; x3 = x2 - value_spacing - logical_rect.width;
min_pos_before = y3 + logical_rect.height + min_sep; min_pos_before = y3 + logical_rect.height + min_sep;
...@@ -1182,8 +1179,7 @@ gtk_scale_draw (GtkWidget *widget, ...@@ -1182,8 +1179,7 @@ gtk_scale_draw (GtkWidget *widget,
min_pos_after = y3 + logical_rect.height + min_sep; min_pos_after = y3 + logical_rect.height + min_sep;
} }
gtk_render_layout (context, cr, gtk_render_layout (context, cr, x3, y3, layout);
x3, y3, layout);
} }
gtk_style_context_restore (context); gtk_style_context_restore (context);
...@@ -1493,9 +1489,9 @@ compare_marks (gpointer a, gpointer b) ...@@ -1493,9 +1489,9 @@ compare_marks (gpointer a, gpointer b)
* @value: the value at which the mark is placed, must be between * @value: the value at which the mark is placed, must be between
* the lower and upper limits of the scales' adjustment * the lower and upper limits of the scales' adjustment
* @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP
* is drawn above the scale, anything else below. For a vertical scale, * and %GTK_POS_LEFT are drawn above the scale, anything else below.
* #GTK_POS_LEFT is drawn to the left of the scale, anything else to the * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
* right. * the left of the scale, anything else to the right.
* @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL * @markup: (allow-none): Text to be shown at the mark, using <link linkend="PangoMarkupFormat">Pango markup</link>, or %NULL
* *
* *
...@@ -1530,7 +1526,11 @@ gtk_scale_add_mark (GtkScale *scale, ...@@ -1530,7 +1526,11 @@ gtk_scale_add_mark (GtkScale *scale,
mark = g_new (GtkScaleMark, 1); mark = g_new (GtkScaleMark, 1);
mark->value = value; mark->value = value;
mark->markup = g_strdup (markup); mark->markup = g_strdup (markup);
mark->position = position; if (position == GTK_POS_LEFT ||
position == GTK_POS_TOP)
mark->position = GTK_POS_TOP;
else
mark->position = GTK_POS_BOTTOM;
priv->marks = g_slist_insert_sorted (priv->marks, mark, priv->marks = g_slist_insert_sorted (priv->marks, mark,
(GCompareFunc) compare_marks); (GCompareFunc) compare_marks);
...@@ -1671,7 +1671,11 @@ marks_start_element (GMarkupParseContext *context, ...@@ -1671,7 +1671,11 @@ marks_start_element (GMarkupParseContext *context,
mark = g_slice_new (MarkData); mark = g_slice_new (MarkData);
mark->value = value; mark->value = value;
mark->position = position; if (position == GTK_POS_LEFT ||
position == GTK_POS_TOP)
mark->position = GTK_POS_TOP;
else
mark->position = GTK_POS_BOTTOM;
mark->markup = g_string_new (""); mark->markup = g_string_new ("");
mark->context = g_strdup (msg_context); mark->context = g_strdup (msg_context);
mark->translatable = translatable; mark->translatable = translatable;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment