textview: Use a CSS node for selection rendering

Use a CSS node with name selection, like we do for entries
and labels. Unlike those widgets, we currently don't user
gtk_render_background, but just use the background color.
That will require more effort.
This commit is contained in:
Matthias Clasen 2015-11-10 21:18:10 -05:00
parent 735a71da11
commit bb786ac240
2 changed files with 37 additions and 12 deletions

View File

@ -596,6 +596,8 @@ get_selected_clip (GtkTextRenderer *text_renderer,
return clip_region;
}
extern GtkCssNode *gtk_text_view_get_selection_node (GtkTextView *text_view);
static void
render_para (GtkTextRenderer *text_renderer,
GtkTextLineDisplay *line_display,
@ -603,7 +605,6 @@ render_para (GtkTextRenderer *text_renderer,
int selection_end_index)
{
GtkStyleContext *context;
GtkStateFlags state;
PangoLayout *layout = line_display->layout;
int byte_offset = 0;
PangoLayoutIter *iter;
@ -611,6 +612,7 @@ render_para (GtkTextRenderer *text_renderer,
int screen_width;
GdkRGBA selection;
gboolean first = TRUE;
GtkCssNode *selection_node;
iter = pango_layout_get_iter (layout);
@ -624,14 +626,11 @@ render_para (GtkTextRenderer *text_renderer,
screen_width = line_display->total_width;
context = gtk_widget_get_style_context (text_renderer->widget);
gtk_style_context_save (context);
state = gtk_style_context_get_state (context);
state |= GTK_STATE_FLAG_SELECTED;
gtk_style_context_set_state (context, state);
selection_node = gtk_text_view_get_selection_node ((GtkTextView*)text_renderer->widget);
gtk_style_context_save_to_node (context, selection_node);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gtk_style_context_get_background_color (context, state, &selection);
gtk_style_context_get_background_color (context, gtk_style_context_get_state (context), &selection);
G_GNUC_END_IGNORE_DEPRECATIONS
gtk_style_context_restore (context);
@ -825,9 +824,8 @@ G_GNUC_END_IGNORE_DEPRECATIONS
{
GdkRGBA color;
state = gtk_style_context_get_state (context);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gtk_style_context_get_background_color (context, state, &color);
gtk_style_context_get_background_color (context, gtk_style_context_get_state (context), &color);
G_GNUC_END_IGNORE_DEPRECATIONS
gdk_cairo_set_source_rgba (cr, &color);

View File

@ -233,6 +233,8 @@ struct _GtkTextViewPrivate
GtkGesture *multipress_gesture;
GtkGesture *drag_gesture;
GtkCssNode *selection_node;
/* Default style settings */
gint pixels_above_lines;
gint pixels_below_lines;
@ -1737,6 +1739,18 @@ gtk_text_view_init (GtkTextView *text_view)
g_signal_connect (priv->drag_gesture, "drag-end",
G_CALLBACK (gtk_text_view_drag_gesture_end),
widget);
priv->selection_node = gtk_css_node_new ();
gtk_css_node_set_name (priv->selection_node, I_("selection"));
gtk_css_node_set_parent (priv->selection_node, priv->text_window->css_node);
gtk_css_node_set_state (priv->selection_node, gtk_css_node_get_state (priv->text_window->css_node));
g_object_unref (priv->selection_node);
}
GtkCssNode *
gtk_text_view_get_selection_node (GtkTextView *text_view)
{
return text_view->priv->selection_node;
}
static void
@ -4838,6 +4852,7 @@ gtk_text_view_state_flags_changed (GtkWidget *widget,
GtkStateFlags previous_state)
{
GtkTextView *text_view = GTK_TEXT_VIEW (widget);
GtkTextViewPrivate *priv = text_view->priv;
GdkCursor *cursor;
if (gtk_widget_get_realized (widget))
@ -4847,12 +4862,12 @@ gtk_text_view_state_flags_changed (GtkWidget *widget,
else
cursor = NULL;
gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
gdk_window_set_cursor (priv->text_window->bin_window, cursor);
if (cursor)
g_object_unref (cursor);
text_view->priv->mouse_cursor_obscured = FALSE;
priv->mouse_cursor_obscured = FALSE;
}
if (!gtk_widget_is_sensitive (widget))
@ -4860,7 +4875,19 @@ gtk_text_view_state_flags_changed (GtkWidget *widget,
/* Clear any selection */
gtk_text_view_unselect (text_view);
}
gtk_css_node_set_state (priv->text_window->css_node, gtk_widget_get_state (widget));
gtk_css_node_set_state (priv->selection_node, gtk_widget_get_state (widget));
if (priv->left_window)
gtk_css_node_set_state (priv->left_window->css_node, gtk_widget_get_state (widget));
if (priv->right_window)
gtk_css_node_set_state (priv->right_window->css_node, gtk_widget_get_state (widget));
if (priv->top_window)
gtk_css_node_set_state (priv->top_window->css_node, gtk_widget_get_state (widget));
if (priv->bottom_window)
gtk_css_node_set_state (priv->bottom_window->css_node, gtk_widget_get_state (widget));
gtk_widget_queue_draw (widget);
}