Make GtkToolbar use GtkStyleContext

This commit is contained in:
Carlos Garnacho
2010-12-28 19:03:46 +01:00
parent a3a9c61a5a
commit ac00e77e54

View File

@ -195,8 +195,7 @@ static void gtk_toolbar_get_preferred_height (GtkWidget *widget,
static void gtk_toolbar_size_allocate (GtkWidget *widget, static void gtk_toolbar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation); GtkAllocation *allocation);
static void gtk_toolbar_style_set (GtkWidget *widget, static void gtk_toolbar_style_updated (GtkWidget *widget);
GtkStyle *prev_style);
static gboolean gtk_toolbar_focus (GtkWidget *widget, static gboolean gtk_toolbar_focus (GtkWidget *widget,
GtkDirectionType dir); GtkDirectionType dir);
static void gtk_toolbar_move_focus (GtkWidget *widget, static void gtk_toolbar_move_focus (GtkWidget *widget,
@ -371,7 +370,7 @@ gtk_toolbar_class_init (GtkToolbarClass *klass)
widget_class->get_preferred_width = gtk_toolbar_get_preferred_width; widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
widget_class->get_preferred_height = gtk_toolbar_get_preferred_height; widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
widget_class->size_allocate = gtk_toolbar_size_allocate; widget_class->size_allocate = gtk_toolbar_size_allocate;
widget_class->style_set = gtk_toolbar_style_set; widget_class->style_updated = gtk_toolbar_style_updated;
widget_class->focus = gtk_toolbar_focus; widget_class->focus = gtk_toolbar_focus;
/* need to override the base class function via override_class_handler, /* need to override the base class function via override_class_handler,
@ -646,6 +645,7 @@ static void
gtk_toolbar_init (GtkToolbar *toolbar) gtk_toolbar_init (GtkToolbar *toolbar)
{ {
GtkToolbarPrivate *priv; GtkToolbarPrivate *priv;
GtkStyleContext *context;
toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar, toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
GTK_TYPE_TOOLBAR, GTK_TYPE_TOOLBAR,
@ -685,6 +685,9 @@ gtk_toolbar_init (GtkToolbar *toolbar)
priv->max_homogeneous_pixels = -1; priv->max_homogeneous_pixels = -1;
priv->timer = g_timer_new (); priv->timer = g_timer_new ();
context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
} }
static void static void
@ -813,8 +816,6 @@ gtk_toolbar_realize (GtkWidget *widget)
gtk_widget_set_window (widget, window); gtk_widget_set_window (widget, window);
g_object_ref (window); g_object_ref (window);
gtk_widget_style_attach (widget);
priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget), priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask); &attributes, attributes_mask);
gdk_window_set_user_data (priv->event_window, toolbar); gdk_window_set_user_data (priv->event_window, toolbar);
@ -842,21 +843,25 @@ gtk_toolbar_draw (GtkWidget *widget,
{ {
GtkToolbar *toolbar = GTK_TOOLBAR (widget); GtkToolbar *toolbar = GTK_TOOLBAR (widget);
GtkToolbarPrivate *priv = toolbar->priv; GtkToolbarPrivate *priv = toolbar->priv;
GtkStyleContext *context;
GtkStateFlags state;
GList *list; GList *list;
guint border_width; guint border_width;
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget)); border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_save (context);
gtk_style_context_set_state (context, state);
gtk_render_background (context, cr, border_width, border_width,
gtk_widget_get_allocated_width (widget) - 2 * border_width,
gtk_widget_get_allocated_height (widget) - 2 * border_width);
gtk_render_frame (context, cr, border_width, border_width,
gtk_widget_get_allocated_width (widget) - 2 * border_width,
gtk_widget_get_allocated_height (widget) - 2 * border_width);
gtk_paint_box (gtk_widget_get_style (widget),
cr,
gtk_widget_get_state (widget),
get_shadow_type (toolbar),
widget, "toolbar",
border_width,
border_width,
gtk_widget_get_allocated_width (widget) - 2 * border_width,
gtk_widget_get_allocated_height (widget) - 2 * border_width);
for (list = priv->content; list != NULL; list = list->next) for (list = priv->content; list != NULL; list = list->next)
{ {
ToolbarContent *content = list->data; ToolbarContent *content = list->data;
@ -867,7 +872,9 @@ gtk_toolbar_draw (GtkWidget *widget,
gtk_container_propagate_draw (GTK_CONTAINER (widget), gtk_container_propagate_draw (GTK_CONTAINER (widget),
priv->arrow_button, priv->arrow_button,
cr); cr);
gtk_style_context_restore (context);
return FALSE; return FALSE;
} }
@ -989,11 +996,16 @@ gtk_toolbar_size_request (GtkWidget *widget,
if (get_shadow_type (toolbar) != GTK_SHADOW_NONE) if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
{ {
GtkStyle *style; GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
requisition->width += 2 * style->xthickness; state = gtk_widget_get_state_flags (widget);
requisition->height += 2 * style->ythickness; gtk_style_context_get_padding (context, state, &padding);
requisition->width += padding.left + padding.right;
requisition->height += padding.top + padding.bottom;
} }
priv->button_maxw = max_homogeneous_child_width; priv->button_maxw = max_homogeneous_child_width;
@ -1222,7 +1234,9 @@ gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
GtkAllocation allocation; GtkAllocation allocation;
GtkWidget *widget = GTK_WIDGET (toolbar); GtkWidget *widget = GTK_WIDGET (toolbar);
GtkToolbarPrivate *priv = toolbar->priv; GtkToolbarPrivate *priv = toolbar->priv;
GtkStyle *style; GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
GList *list; GList *list;
gint cur_x; gint cur_x;
gint cur_y; gint cur_y;
@ -1244,7 +1258,9 @@ gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar); priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_get_padding (context, state, &padding);
rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL); rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL); vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
@ -1252,13 +1268,13 @@ gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
if (rtl) if (rtl)
{ {
cur_x = allocation.width - border_width - style->xthickness; cur_x = allocation.width - border_width - padding.right;
cur_y = allocation.height - border_width - style->ythickness; cur_y = allocation.height - border_width - padding.top;
} }
else else
{ {
cur_x = border_width + style->xthickness; cur_x = border_width + padding.left;
cur_y = border_width + style->ythickness; cur_y = border_width + padding.top;
} }
cur_x += allocation.x; cur_x += allocation.x;
@ -1288,14 +1304,16 @@ gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
if (vertical) if (vertical)
{ {
new_start_allocation.width = allocation.width - new_start_allocation.width = allocation.width -
2 * border_width - 2 * style->xthickness; 2 * border_width -
padding.left - padding.right;
new_start_allocation.height = 0; new_start_allocation.height = 0;
} }
else else
{ {
new_start_allocation.width = 0; new_start_allocation.width = 0;
new_start_allocation.height = allocation.height - new_start_allocation.height = allocation.height -
2 * border_width - 2 * style->ythickness; 2 * border_width -
padding.top - padding.bottom;
} }
} }
@ -1454,7 +1472,9 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
GtkAllocation *allocations; GtkAllocation *allocations;
ItemState *new_states; ItemState *new_states;
GtkAllocation arrow_allocation; GtkAllocation arrow_allocation;
GtkStyle *style; GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
gint arrow_size; gint arrow_size;
gint size, pos, short_size; gint size, pos, short_size;
GList *list; GList *list;
@ -1472,7 +1492,9 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
GtkAllocation item_area; GtkAllocation item_area;
GtkShadowType shadow_type; GtkShadowType shadow_type;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_get_padding (context, state, &padding);
gtk_widget_get_allocation (widget, &widget_allocation); gtk_widget_get_allocation (widget, &widget_allocation);
size_changed = FALSE; size_changed = FALSE;
@ -1515,8 +1537,8 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
if (shadow_type != GTK_SHADOW_NONE) if (shadow_type != GTK_SHADOW_NONE)
{ {
available_size -= 2 * style->xthickness; available_size -= padding.left + padding.right;
short_size -= 2 * style->ythickness; short_size -= padding.top + padding.bottom;
} }
} }
else else
@ -1527,8 +1549,8 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
if (shadow_type != GTK_SHADOW_NONE) if (shadow_type != GTK_SHADOW_NONE)
{ {
available_size -= 2 * style->ythickness; available_size -= padding.top + padding.bottom;
short_size -= 2 * style->xthickness; short_size -= padding.left + padding.right;
} }
} }
@ -1704,8 +1726,8 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
if (shadow_type != GTK_SHADOW_NONE) if (shadow_type != GTK_SHADOW_NONE)
{ {
allocations[i].x += style->xthickness; allocations[i].x += padding.left;
allocations[i].y += style->ythickness; allocations[i].y += padding.top;
} }
} }
@ -1716,8 +1738,8 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
if (shadow_type != GTK_SHADOW_NONE) if (shadow_type != GTK_SHADOW_NONE)
{ {
arrow_allocation.x += style->xthickness; arrow_allocation.x += padding.left;
arrow_allocation.y += style->ythickness; arrow_allocation.y += padding.top;
} }
} }
@ -1725,8 +1747,8 @@ gtk_toolbar_size_allocate (GtkWidget *widget,
item_area.y += allocation->y; item_area.y += allocation->y;
if (shadow_type != GTK_SHADOW_NONE) if (shadow_type != GTK_SHADOW_NONE)
{ {
item_area.x += style->xthickness; item_area.x += padding.left;
item_area.y += style->ythickness; item_area.y += padding.top;
} }
/* did anything change? */ /* did anything change? */
@ -1847,8 +1869,7 @@ gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
} }
static void static void
gtk_toolbar_style_set (GtkWidget *widget, gtk_toolbar_style_updated (GtkWidget *widget)
GtkStyle *prev_style)
{ {
GtkToolbar *toolbar = GTK_TOOLBAR (widget); GtkToolbar *toolbar = GTK_TOOLBAR (widget);
GtkToolbarPrivate *priv = toolbar->priv; GtkToolbarPrivate *priv = toolbar->priv;
@ -1856,12 +1877,10 @@ gtk_toolbar_style_set (GtkWidget *widget,
priv->max_homogeneous_pixels = -1; priv->max_homogeneous_pixels = -1;
if (gtk_widget_get_realized (widget)) if (gtk_widget_get_realized (widget))
gtk_style_set_background (gtk_widget_get_style (widget), gtk_style_context_set_background (gtk_widget_get_style_context (widget),
gtk_widget_get_window (widget), gtk_widget_get_window (widget));
gtk_widget_get_state (widget));
if (prev_style) gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
} }
static GList * static GList *
@ -3302,11 +3321,18 @@ calculate_max_homogeneous_pixels (GtkWidget *widget)
{ {
PangoContext *context; PangoContext *context;
PangoFontMetrics *metrics; PangoFontMetrics *metrics;
const PangoFontDescription *font_desc;
GtkStyleContext *style_context;
GtkStateFlags state;
gint char_width; gint char_width;
context = gtk_widget_get_pango_context (widget); context = gtk_widget_get_pango_context (widget);
metrics = pango_context_get_metrics (context, style_context = gtk_widget_get_style_context (widget);
gtk_widget_get_style (widget)->font_desc, state = gtk_widget_get_state_flags (widget);
font_desc = gtk_style_context_get_font (style_context, state);
metrics = pango_context_get_metrics (context, font_desc,
pango_context_get_language (context)); pango_context_get_language (context));
char_width = pango_font_metrics_get_approximate_char_width (metrics); char_width = pango_font_metrics_get_approximate_char_width (metrics);
pango_font_metrics_unref (metrics); pango_font_metrics_unref (metrics);
@ -3648,9 +3674,10 @@ _gtk_toolbar_paint_space_line (GtkWidget *widget,
{ {
GtkToolbarPrivate *priv = toolbar->priv; GtkToolbarPrivate *priv = toolbar->priv;
GtkOrientation orientation; GtkOrientation orientation;
GtkStateType state; GtkStyleContext *context;
GtkStyle *style; GtkStateFlags state;
GdkWindow *window; GdkWindow *window;
GtkBorder padding;
int width, height; int width, height;
const double start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION); const double start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
const double end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION); const double end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
@ -3659,11 +3686,12 @@ _gtk_toolbar_paint_space_line (GtkWidget *widget,
orientation = toolbar? priv->orientation : GTK_ORIENTATION_HORIZONTAL; orientation = toolbar? priv->orientation : GTK_ORIENTATION_HORIZONTAL;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
window = gtk_widget_get_window (widget); window = gtk_widget_get_window (widget);
state = gtk_widget_get_state (widget); state = gtk_widget_get_state_flags (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);
gtk_style_context_get_padding (context, state, &padding);
if (orientation == GTK_ORIENTATION_HORIZONTAL) if (orientation == GTK_ORIENTATION_HORIZONTAL)
{ {
@ -3676,20 +3704,17 @@ _gtk_toolbar_paint_space_line (GtkWidget *widget,
NULL); NULL);
if (wide_separators) if (wide_separators)
gtk_paint_box (style, cr, gtk_render_frame (context, cr,
state, GTK_SHADOW_ETCHED_OUT, (width - separator_width) / 2,
widget, "vseparator", height * start_fraction,
(width - separator_width) / 2, separator_width,
height * start_fraction, height * (end_fraction - start_fraction));
separator_width,
height * (end_fraction - start_fraction));
else else
gtk_paint_vline (style, cr, gtk_render_line (context, cr,
state, widget, (width - padding.left) / 2,
"toolbar",
height * start_fraction, height * start_fraction,
height * end_fraction, (width - padding.left) / 2,
(width - style->xthickness) / 2); height * end_fraction);
} }
else else
{ {
@ -3702,20 +3727,17 @@ _gtk_toolbar_paint_space_line (GtkWidget *widget,
NULL); NULL);
if (wide_separators) if (wide_separators)
gtk_paint_box (style, cr, gtk_render_frame (context, cr,
state, GTK_SHADOW_ETCHED_OUT, width * start_fraction,
widget, "hseparator", (height - separator_height) / 2,
width * start_fraction, width * (end_fraction - start_fraction),
(height - separator_height) / 2, separator_height);
width * (end_fraction - start_fraction),
separator_height);
else else
gtk_paint_hline (style, cr, gtk_render_line (context, cr,
state, widget,
"toolbar",
width * start_fraction, width * start_fraction,
(height - padding.top) / 2,
width * end_fraction, width * end_fraction,
(height - style->ythickness) / 2); (height - padding.top) / 2);
} }
} }