Make GtkTextView use GtkStyleContext

GtkTextAppearance still uses GdkColors, even though it could
switch to GdkRGBA with little hassle as it seems sheldomly used
out there.
This commit is contained in:
Carlos Garnacho 2010-12-14 20:26:52 +01:00
parent b0560107e9
commit 66800aa212
2 changed files with 90 additions and 56 deletions

View File

@ -330,6 +330,13 @@ struct _GtkStyleContextClass
*/ */
#define GTK_STYLE_CLASS_NOTEBOOK "notebook" #define GTK_STYLE_CLASS_NOTEBOOK "notebook"
/**
* GTK_STYLE_CLASS_VIEW:
*
* A widget class defining a view, such as iconviews or treeviews
*/
#define GTK_STYLE_CLASS_VIEW "view"
/** /**
* GTK_STYLE_CLASS_INFO: * GTK_STYLE_CLASS_INFO:
* *

View File

@ -304,14 +304,13 @@ static void gtk_text_view_size_allocate (GtkWidget *widget,
GtkAllocation *allocation); GtkAllocation *allocation);
static void gtk_text_view_realize (GtkWidget *widget); static void gtk_text_view_realize (GtkWidget *widget);
static void gtk_text_view_unrealize (GtkWidget *widget); static void gtk_text_view_unrealize (GtkWidget *widget);
static void gtk_text_view_style_set (GtkWidget *widget, static void gtk_text_view_style_updated (GtkWidget *widget);
GtkStyle *previous_style);
static void gtk_text_view_direction_changed (GtkWidget *widget, static void gtk_text_view_direction_changed (GtkWidget *widget,
GtkTextDirection previous_direction); GtkTextDirection previous_direction);
static void gtk_text_view_grab_notify (GtkWidget *widget, static void gtk_text_view_grab_notify (GtkWidget *widget,
gboolean was_grabbed); gboolean was_grabbed);
static void gtk_text_view_state_changed (GtkWidget *widget, static void gtk_text_view_state_flags_changed (GtkWidget *widget,
GtkStateType previous_state); GtkStateFlags previous_state);
static gint gtk_text_view_event (GtkWidget *widget, static gint gtk_text_view_event (GtkWidget *widget,
GdkEvent *event); GdkEvent *event);
@ -409,8 +408,7 @@ static void gtk_text_view_get_first_para_iter (GtkTextView *text_vi
GtkTextIter *iter); GtkTextIter *iter);
static void gtk_text_view_update_layout_width (GtkTextView *text_view); static void gtk_text_view_update_layout_width (GtkTextView *text_view);
static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view, static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
GtkTextAttributes *values, GtkTextAttributes *values);
GtkStyle *style);
static void gtk_text_view_ensure_layout (GtkTextView *text_view); static void gtk_text_view_ensure_layout (GtkTextView *text_view);
static void gtk_text_view_destroy_layout (GtkTextView *text_view); static void gtk_text_view_destroy_layout (GtkTextView *text_view);
static void gtk_text_view_check_keymap_direction (GtkTextView *text_view); static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
@ -603,10 +601,10 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
widget_class->destroy = gtk_text_view_destroy; widget_class->destroy = gtk_text_view_destroy;
widget_class->realize = gtk_text_view_realize; widget_class->realize = gtk_text_view_realize;
widget_class->unrealize = gtk_text_view_unrealize; widget_class->unrealize = gtk_text_view_unrealize;
widget_class->style_set = gtk_text_view_style_set; widget_class->style_updated = gtk_text_view_style_updated;
widget_class->direction_changed = gtk_text_view_direction_changed; widget_class->direction_changed = gtk_text_view_direction_changed;
widget_class->grab_notify = gtk_text_view_grab_notify; widget_class->grab_notify = gtk_text_view_grab_notify;
widget_class->state_changed = gtk_text_view_state_changed; widget_class->state_flags_changed = gtk_text_view_state_flags_changed;
widget_class->get_preferred_width = gtk_text_view_get_preferred_width; widget_class->get_preferred_width = gtk_text_view_get_preferred_width;
widget_class->get_preferred_height = gtk_text_view_get_preferred_height; widget_class->get_preferred_height = gtk_text_view_get_preferred_height;
widget_class->size_allocate = gtk_text_view_size_allocate; widget_class->size_allocate = gtk_text_view_size_allocate;
@ -3949,11 +3947,14 @@ gtk_text_view_realize (GtkWidget *widget)
GtkAllocation allocation; GtkAllocation allocation;
GtkTextView *text_view; GtkTextView *text_view;
GtkTextViewPrivate *priv; GtkTextViewPrivate *priv;
GtkStyleContext *context;
GtkStateFlags state;
GdkWindow *window; GdkWindow *window;
GdkWindowAttr attributes; GdkWindowAttr attributes;
gint attributes_mask; gint attributes_mask;
GSList *tmp_list; GSList *tmp_list;
GdkRGBA color;
text_view = GTK_TEXT_VIEW (widget); text_view = GTK_TEXT_VIEW (widget);
priv = text_view->priv; priv = text_view->priv;
@ -3977,11 +3978,11 @@ gtk_text_view_realize (GtkWidget *widget)
gtk_widget_set_window (widget, window); gtk_widget_set_window (widget, window);
gdk_window_set_user_data (window, widget); gdk_window_set_user_data (window, widget);
/* must come before text_window_realize calls */ context = gtk_widget_get_style_context (widget);
gtk_widget_style_attach (widget); state = gtk_widget_get_state_flags (widget);
gdk_window_set_background (window, gtk_style_context_get_background_color (context, state, &color);
&gtk_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]); gdk_window_set_background_rgba (window, &color);
text_window_realize (priv->text_window, widget); text_window_realize (priv->text_window, widget);
@ -4066,42 +4067,47 @@ gtk_text_view_unrealize (GtkWidget *widget)
static void static void
gtk_text_view_set_background (GtkTextView *text_view) gtk_text_view_set_background (GtkTextView *text_view)
{ {
GtkStyle *style; GtkStyleContext *context;
GtkStateType state; GtkStateFlags state;
GtkWidget *widget; GtkWidget *widget;
GtkTextViewPrivate *priv; GtkTextViewPrivate *priv;
GdkRGBA color;
widget = GTK_WIDGET (text_view); widget = GTK_WIDGET (text_view);
priv = text_view->priv; priv = text_view->priv;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state (widget); state = gtk_widget_get_state_flags (widget);
gdk_window_set_background (gtk_widget_get_window (widget), /* Set bin window background */
&style->bg[state]); gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
gdk_window_set_background (priv->text_window->bin_window, gtk_style_context_get_background_color (context, state, &color);
&style->base[state]); gdk_window_set_background_rgba (priv->text_window->bin_window, &color);
gtk_style_context_restore (context);
/* Set lateral panes background */
gtk_style_context_get_background_color (context, state, &color);
gdk_window_set_background_rgba (gtk_widget_get_window (widget), &color);
if (priv->left_window) if (priv->left_window)
gdk_window_set_background (priv->left_window->bin_window, gdk_window_set_background_rgba (priv->left_window->bin_window, &color);
&style->bg[state]);
if (priv->right_window) if (priv->right_window)
gdk_window_set_background (priv->right_window->bin_window, gdk_window_set_background_rgba (priv->right_window->bin_window, &color);
&style->bg[state]);
if (priv->top_window) if (priv->top_window)
gdk_window_set_background (priv->top_window->bin_window, gdk_window_set_background_rgba (priv->top_window->bin_window, &color);
&style->bg[state]);
if (priv->bottom_window) if (priv->bottom_window)
gdk_window_set_background (priv->bottom_window->bin_window, gdk_window_set_background_rgba (priv->bottom_window->bin_window, &color);
&style->bg[state]);
} }
static void static void
gtk_text_view_style_set (GtkWidget *widget, gtk_text_view_style_updated (GtkWidget *widget)
GtkStyle *previous_style)
{ {
GtkTextView *text_view; GtkTextView *text_view;
GtkTextViewPrivate *priv; GtkTextViewPrivate *priv;
@ -4115,11 +4121,10 @@ gtk_text_view_style_set (GtkWidget *widget,
gtk_text_view_set_background (text_view); gtk_text_view_set_background (text_view);
} }
if (priv->layout && previous_style) if (priv->layout && priv->layout->default_style)
{ {
gtk_text_view_set_attributes_from_style (text_view, gtk_text_view_set_attributes_from_style (text_view,
priv->layout->default_style, priv->layout->default_style);
gtk_widget_get_style (widget));
ltr_context = gtk_widget_create_pango_context (widget); ltr_context = gtk_widget_create_pango_context (widget);
pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR); pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
@ -4148,8 +4153,8 @@ gtk_text_view_direction_changed (GtkWidget *widget,
} }
static void static void
gtk_text_view_state_changed (GtkWidget *widget, gtk_text_view_state_flags_changed (GtkWidget *widget,
GtkStateType previous_state) GtkStateFlags previous_state)
{ {
GtkTextView *text_view = GTK_TEXT_VIEW (widget); GtkTextView *text_view = GTK_TEXT_VIEW (widget);
GdkCursor *cursor; GdkCursor *cursor;
@ -4870,13 +4875,14 @@ gtk_text_view_draw_focus (GtkWidget *widget,
NULL); NULL);
if (gtk_widget_has_focus (widget) && !interior_focus) if (gtk_widget_has_focus (widget) && !interior_focus)
{ {
gtk_paint_focus (gtk_widget_get_style (widget), cr, GtkStyleContext *context;
gtk_widget_get_state (widget),
widget, "textview", context = gtk_widget_get_style_context (widget);
0, 0,
gtk_widget_get_allocated_width (widget), gtk_render_focus (context, cr, 0, 0,
gtk_widget_get_allocated_height (widget)); gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
} }
} }
@ -6535,16 +6541,30 @@ gtk_text_view_end_selection_drag (GtkTextView *text_view)
static void static void
gtk_text_view_set_attributes_from_style (GtkTextView *text_view, gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
GtkTextAttributes *values, GtkTextAttributes *values)
GtkStyle *style)
{ {
values->appearance.bg_color = style->base[GTK_STATE_NORMAL]; GtkStyleContext *context;
values->appearance.fg_color = style->text[GTK_STATE_NORMAL]; GdkRGBA bg_color, fg_color;
GtkStateFlags state;
context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
state = gtk_widget_get_state_flags (GTK_WIDGET (text_view));
gtk_style_context_get_background_color (context, state, &bg_color);
gtk_style_context_get_color (context, state, &fg_color);
values->appearance.bg_color.red = CLAMP (bg_color.red * 65535. + 0.5, 0, 65535);
values->appearance.bg_color.green = CLAMP (bg_color.green * 65535. + 0.5, 0, 65535);
values->appearance.bg_color.blue = CLAMP (bg_color.blue * 65535. + 0.5, 0, 65535);
values->appearance.fg_color.red = CLAMP (fg_color.red * 65535. + 0.5, 0, 65535);
values->appearance.fg_color.green = CLAMP (fg_color.green * 65535. + 0.5, 0, 65535);
values->appearance.fg_color.blue = CLAMP (fg_color.blue * 65535. + 0.5, 0, 65535);
if (values->font) if (values->font)
pango_font_description_free (values->font); pango_font_description_free (values->font);
values->font = pango_font_description_copy (style->font_desc); values->font = pango_font_description_copy (gtk_style_context_get_font (context, state));
} }
static void static void
@ -6638,10 +6658,7 @@ gtk_text_view_ensure_layout (GtkTextView *text_view)
style = gtk_text_attributes_new (); style = gtk_text_attributes_new ();
gtk_widget_ensure_style (widget); gtk_text_view_set_attributes_from_style (text_view, style);
gtk_text_view_set_attributes_from_style (text_view,
style,
gtk_widget_get_style (widget));
style->pixels_above_lines = priv->pixels_above_lines; style->pixels_above_lines = priv->pixels_above_lines;
style->pixels_below_lines = priv->pixels_below_lines; style->pixels_below_lines = priv->pixels_below_lines;
@ -8352,10 +8369,13 @@ static void
text_window_realize (GtkTextWindow *win, text_window_realize (GtkTextWindow *win,
GtkWidget *widget) GtkWidget *widget)
{ {
GtkStyleContext *context;
GtkStateFlags state;
GdkWindow *window; GdkWindow *window;
GdkWindowAttr attributes; GdkWindowAttr attributes;
gint attributes_mask; gint attributes_mask;
GdkCursor *cursor; GdkCursor *cursor;
GdkRGBA color;
attributes.window_type = GDK_WINDOW_CHILD; attributes.window_type = GDK_WINDOW_CHILD;
attributes.x = win->allocation.x; attributes.x = win->allocation.x;
@ -8397,6 +8417,9 @@ text_window_realize (GtkTextWindow *win,
gdk_window_show (win->bin_window); gdk_window_show (win->bin_window);
gdk_window_set_user_data (win->bin_window, win->widget); gdk_window_set_user_data (win->bin_window, win->widget);
context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
if (win->type == GTK_TEXT_WINDOW_TEXT) if (win->type == GTK_TEXT_WINDOW_TEXT)
{ {
if (gtk_widget_is_sensitive (widget)) if (gtk_widget_is_sensitive (widget))
@ -8411,14 +8434,18 @@ text_window_realize (GtkTextWindow *win,
gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context, gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context,
win->window); win->window);
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
gdk_window_set_background (win->bin_window, gtk_style_context_get_background_color (context, state, &color);
&gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]); gdk_window_set_background_rgba (win->bin_window, &color);
gtk_style_context_restore (context);
} }
else else
{ {
gdk_window_set_background (win->bin_window, gtk_style_context_get_background_color (context, state, &color);
&gtk_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]); gdk_window_set_background_rgba (win->bin_window, &color);
} }
g_object_set_qdata (G_OBJECT (win->window), g_object_set_qdata (G_OBJECT (win->window),