2 * Copyright (C) 1998-2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Written by Owen Taylor <otaylor@redhat.com>, based on code by
20 * Carsten Haitzler <raster@rasterman.com>
27 #include "pixbuf-rc-style.h"
28 #include "pixbuf-style.h"
30 static void pixbuf_style_init (PixbufStyle *style);
31 static void pixbuf_style_class_init (PixbufStyleClass *klass);
33 static GtkStyleClass *parent_class = NULL;
36 match_theme_image (GtkStyle *style,
37 ThemeMatchData *match_data)
41 tmp_list = PIXBUF_RC_STYLE (style->rc_style)->img_list;
46 ThemeImage *image = tmp_list->data;
47 tmp_list = tmp_list->next;
49 if (match_data->function != image->match_data.function)
52 flags = match_data->flags & image->match_data.flags;
54 if (flags != image->match_data.flags) /* Required components not present */
57 if ((flags & THEME_MATCH_STATE) &&
58 match_data->state != image->match_data.state)
61 if ((flags & THEME_MATCH_SHADOW) &&
62 match_data->shadow != image->match_data.shadow)
65 if ((flags & THEME_MATCH_ARROW_DIRECTION) &&
66 match_data->arrow_direction != image->match_data.arrow_direction)
69 if ((flags & THEME_MATCH_ORIENTATION) &&
70 match_data->orientation != image->match_data.orientation)
73 if ((flags & THEME_MATCH_GAP_SIDE) &&
74 match_data->gap_side != image->match_data.gap_side)
77 if ((flags & THEME_MATCH_EXPANDER_STYLE) &&
78 match_data->expander_style != image->match_data.expander_style)
81 if ((flags & THEME_MATCH_WINDOW_EDGE) &&
82 match_data->window_edge != image->match_data.window_edge)
85 if (image->match_data.detail &&
86 (!match_data->detail ||
87 strcmp (match_data->detail, image->match_data.detail) != 0))
97 draw_simple_image(GtkStyle *style,
100 ThemeMatchData *match_data,
101 gboolean draw_center,
102 gboolean allow_setbg,
111 if (!(match_data->flags & THEME_MATCH_ORIENTATION))
113 match_data->flags |= THEME_MATCH_ORIENTATION;
116 match_data->orientation = GTK_ORIENTATION_VERTICAL;
118 match_data->orientation = GTK_ORIENTATION_HORIZONTAL;
121 image = match_theme_image (style, match_data);
124 if (image->background)
126 theme_pixbuf_render (image->background, cr,
127 draw_center ? COMPONENT_ALL : COMPONENT_ALL | COMPONENT_CENTER,
129 x, y, width, height);
132 if (image->overlay && draw_center)
133 theme_pixbuf_render (image->overlay, cr, COMPONENT_ALL,
135 x, y, width, height);
144 draw_simple_image_no_cairo(GtkStyle *style,
148 ThemeMatchData *match_data,
149 gboolean draw_center,
150 gboolean allow_setbg,
159 if ((width == -1) && (height == -1))
160 gdk_drawable_get_size(window, &width, &height);
161 else if (width == -1)
162 gdk_drawable_get_size(window, &width, NULL);
163 else if (height == -1)
164 gdk_drawable_get_size(window, NULL, &height);
166 cr = gdk_cairo_create (window);
169 gdk_cairo_rectangle (cr, area);
173 result = draw_simple_image (style, cr, widget, match_data,
174 draw_center, allow_setbg,
175 x, y, width, height);
183 draw_gap_image(GtkStyle *style,
186 ThemeMatchData *match_data,
187 gboolean draw_center,
192 GtkPositionType gap_side,
198 if (!(match_data->flags & THEME_MATCH_ORIENTATION))
200 match_data->flags |= THEME_MATCH_ORIENTATION;
203 match_data->orientation = GTK_ORIENTATION_VERTICAL;
205 match_data->orientation = GTK_ORIENTATION_HORIZONTAL;
208 match_data->flags |= THEME_MATCH_GAP_SIDE;
209 match_data->gap_side = gap_side;
211 image = match_theme_image (style, match_data);
215 GdkRectangle r1, r2, r3;
216 GdkPixbuf *pixbuf = NULL;
217 guint components = COMPONENT_ALL;
220 components |= COMPONENT_CENTER;
222 if (image->gap_start)
223 pixbuf = theme_pixbuf_get_pixbuf (image->gap_start);
229 thickness = gdk_pixbuf_get_height (pixbuf);
231 thickness = style->ythickness;
234 components |= COMPONENT_NORTH_WEST | COMPONENT_NORTH | COMPONENT_NORTH_EAST;
239 r1.height = thickness;
242 r2.width = gap_width;
243 r2.height = thickness;
244 r3.x = x + gap_x + gap_width;
246 r3.width = width - (gap_x + gap_width);
247 r3.height = thickness;
252 thickness = gdk_pixbuf_get_height (pixbuf);
254 thickness = style->ythickness;
257 components |= COMPONENT_SOUTH_WEST | COMPONENT_SOUTH | COMPONENT_SOUTH_EAST;
260 r1.y = y + height - thickness;
262 r1.height = thickness;
264 r2.y = y + height - thickness;
265 r2.width = gap_width;
266 r2.height = thickness;
267 r3.x = x + gap_x + gap_width;
268 r3.y = y + height - thickness;
269 r3.width = width - (gap_x + gap_width);
270 r3.height = thickness;
275 thickness = gdk_pixbuf_get_width (pixbuf);
277 thickness = style->xthickness;
280 components |= COMPONENT_NORTH_WEST | COMPONENT_WEST | COMPONENT_SOUTH_WEST;
284 r1.width = thickness;
288 r2.width = thickness;
289 r2.height = gap_width;
291 r3.y = y + gap_x + gap_width;
292 r3.width = thickness;
293 r3.height = height - (gap_x + gap_width);
298 thickness = gdk_pixbuf_get_width (pixbuf);
300 thickness = style->xthickness;
303 components |= COMPONENT_NORTH_EAST | COMPONENT_EAST | COMPONENT_SOUTH_EAST;
305 r1.x = x + width - thickness;
307 r1.width = thickness;
309 r2.x = x + width - thickness;
311 r2.width = thickness;
312 r2.height = gap_width;
313 r3.x = x + width - thickness;
314 r3.y = y + gap_x + gap_width;
315 r3.width = thickness;
316 r3.height = height - (gap_x + gap_width);
320 g_assert_not_reached ();
323 if (image->background)
324 theme_pixbuf_render (image->background,
325 cr, components, FALSE,
326 x, y, width, height);
327 if (image->gap_start)
328 theme_pixbuf_render (image->gap_start,
329 cr, COMPONENT_ALL, FALSE,
330 r1.x, r1.y, r1.width, r1.height);
332 theme_pixbuf_render (image->gap,
333 cr, COMPONENT_ALL, FALSE,
334 r2.x, r2.y, r2.width, r2.height);
336 theme_pixbuf_render (image->gap_end,
337 cr, COMPONENT_ALL, FALSE,
338 r3.x, r3.y, r3.width, r3.height);
347 draw_hline (GtkStyle *style,
357 ThemeMatchData match_data;
359 match_data.function = TOKEN_D_HLINE;
360 match_data.detail = (gchar *)detail;
361 match_data.flags = THEME_MATCH_ORIENTATION | THEME_MATCH_STATE;
362 match_data.state = state;
363 match_data.orientation = GTK_ORIENTATION_HORIZONTAL;
365 image = match_theme_image (style, &match_data);
368 if (image->background)
369 theme_pixbuf_render (image->background,
370 cr, COMPONENT_ALL, FALSE,
371 x1, y, (x2 - x1) + 1, 2);
374 parent_class->draw_hline (style, cr, state, widget, detail,
379 draw_vline (GtkStyle *style,
389 ThemeMatchData match_data;
391 match_data.function = TOKEN_D_VLINE;
392 match_data.detail = (gchar *)detail;
393 match_data.flags = THEME_MATCH_ORIENTATION | THEME_MATCH_STATE;
394 match_data.state = state;
395 match_data.orientation = GTK_ORIENTATION_VERTICAL;
397 image = match_theme_image (style, &match_data);
400 if (image->background)
401 theme_pixbuf_render (image->background,
402 cr, COMPONENT_ALL, FALSE,
403 x, y1, 2, (y2 - y1) + 1);
406 parent_class->draw_vline (style, cr, state, widget, detail,
411 draw_shadow(GtkStyle *style,
414 GtkShadowType shadow,
422 ThemeMatchData match_data;
424 match_data.function = TOKEN_D_SHADOW;
425 match_data.detail = (gchar *)detail;
426 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
427 match_data.shadow = shadow;
428 match_data.state = state;
430 if (!draw_simple_image (style, cr, widget, &match_data, FALSE, FALSE,
431 x, y, width, height))
432 parent_class->draw_shadow (style, cr, state, shadow, widget, detail,
433 x, y, width, height);
436 /* This function makes up for some brokeness in gtkrange.c
437 * where we never get the full arrow of the stepper button
438 * and the type of button in a single drawing function.
440 * It doesn't work correctly when the scrollbar is squished
441 * to the point we don't have room for full-sized steppers.
444 reverse_engineer_stepper_box (GtkWidget *range,
445 GtkArrowType arrow_type,
451 gint slider_width = 14, stepper_size = 14;
455 if (range && GTK_IS_RANGE (range))
457 gtk_widget_style_get (range,
458 "slider_width", &slider_width,
459 "stepper_size", &stepper_size,
463 if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
465 box_width = slider_width;
466 box_height = stepper_size;
470 box_width = stepper_size;
471 box_height = slider_width;
474 *x = *x - (box_width - *width) / 2;
475 *y = *y - (box_height - *height) / 2;
477 *height = box_height;
481 draw_arrow (GtkStyle *style,
484 GtkShadowType shadow,
487 GtkArrowType arrow_direction,
494 ThemeMatchData match_data;
497 (strcmp (detail, "hscrollbar") == 0 || strcmp (detail, "vscrollbar") == 0))
499 /* This is a hack to work around the fact that scrollbar steppers are drawn
500 * as a box + arrow, so we never have
502 * The full bounding box of the scrollbar
503 * The arrow direction
505 * At the same time. We simulate an extra paint function, "STEPPER", by doing
506 * nothing for the box, and then here, reverse engineering the box that
507 * was passed to draw box and using that
511 gint box_width = width;
512 gint box_height = height;
514 reverse_engineer_stepper_box (widget, arrow_direction,
515 &box_x, &box_y, &box_width, &box_height);
517 match_data.function = TOKEN_D_STEPPER;
518 match_data.detail = (gchar *)detail;
519 match_data.flags = (THEME_MATCH_SHADOW |
521 THEME_MATCH_ARROW_DIRECTION);
522 match_data.shadow = shadow;
523 match_data.state = state;
524 match_data.arrow_direction = arrow_direction;
526 if (draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
527 box_x, box_y, box_width, box_height))
529 /* The theme included stepper images, we're done */
533 /* Otherwise, draw the full box, and fall through to draw the arrow
535 match_data.function = TOKEN_D_BOX;
536 match_data.detail = (gchar *)detail;
537 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
538 match_data.shadow = shadow;
539 match_data.state = state;
541 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
542 box_x, box_y, box_width, box_height))
543 parent_class->draw_box (style, cr, state, shadow, widget, detail,
544 box_x, box_y, box_width, box_height);
548 match_data.function = TOKEN_D_ARROW;
549 match_data.detail = (gchar *)detail;
550 match_data.flags = (THEME_MATCH_SHADOW |
552 THEME_MATCH_ARROW_DIRECTION);
553 match_data.shadow = shadow;
554 match_data.state = state;
555 match_data.arrow_direction = arrow_direction;
557 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
558 x, y, width, height))
559 parent_class->draw_arrow (style, cr, state, shadow, widget, detail,
560 arrow_direction, fill, x, y, width, height);
564 draw_diamond (GtkStyle *style,
567 GtkShadowType shadow,
575 ThemeMatchData match_data;
577 match_data.function = TOKEN_D_DIAMOND;
578 match_data.detail = (gchar *)detail;
579 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
580 match_data.shadow = shadow;
581 match_data.state = state;
583 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
584 x, y, width, height))
585 parent_class->draw_diamond (style, cr, state, shadow, widget, detail,
586 x, y, width, height);
590 draw_box (GtkStyle *style,
593 GtkShadowType shadow,
601 ThemeMatchData match_data;
604 (strcmp (detail, "hscrollbar") == 0 || strcmp (detail, "vscrollbar") == 0))
606 /* We handle this in draw_arrow */
610 match_data.function = TOKEN_D_BOX;
611 match_data.detail = (gchar *)detail;
612 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
613 match_data.shadow = shadow;
614 match_data.state = state;
616 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
617 x, y, width, height)) {
618 parent_class->draw_box (style, cr, state, shadow, widget, detail,
619 x, y, width, height);
624 draw_flat_box (GtkStyle *style,
627 GtkShadowType shadow,
635 ThemeMatchData match_data;
637 match_data.function = TOKEN_D_FLAT_BOX;
638 match_data.detail = (gchar *)detail;
639 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
640 match_data.shadow = shadow;
641 match_data.state = state;
643 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
644 x, y, width, height))
645 parent_class->draw_flat_box (style, cr, state, shadow, widget, detail,
646 x, y, width, height);
650 draw_check (GtkStyle *style,
653 GtkShadowType shadow,
661 ThemeMatchData match_data;
663 match_data.function = TOKEN_D_CHECK;
664 match_data.detail = (gchar *)detail;
665 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
666 match_data.shadow = shadow;
667 match_data.state = state;
669 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
670 x, y, width, height))
671 parent_class->draw_check (style, cr, state, shadow, widget, detail,
672 x, y, width, height);
676 draw_option (GtkStyle *style,
679 GtkShadowType shadow,
687 ThemeMatchData match_data;
689 match_data.function = TOKEN_D_OPTION;
690 match_data.detail = (gchar *)detail;
691 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
692 match_data.shadow = shadow;
693 match_data.state = state;
695 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
696 x, y, width, height))
697 parent_class->draw_option (style, cr, state, shadow, widget, detail,
698 x, y, width, height);
702 draw_tab (GtkStyle *style,
705 GtkShadowType shadow,
713 ThemeMatchData match_data;
715 match_data.function = TOKEN_D_TAB;
716 match_data.detail = (gchar *)detail;
717 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
718 match_data.shadow = shadow;
719 match_data.state = state;
721 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
722 x, y, width, height))
723 parent_class->draw_tab (style, cr, state, shadow, widget, detail,
724 x, y, width, height);
728 draw_shadow_gap (GtkStyle *style,
731 GtkShadowType shadow,
738 GtkPositionType gap_side,
742 ThemeMatchData match_data;
744 match_data.function = TOKEN_D_SHADOW_GAP;
745 match_data.detail = (gchar *)detail;
746 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
747 match_data.flags = (THEME_MATCH_SHADOW |
749 THEME_MATCH_ORIENTATION);
750 match_data.shadow = shadow;
751 match_data.state = state;
753 if (!draw_gap_image (style, cr, widget, &match_data, FALSE,
754 x, y, width, height, gap_side, gap_x, gap_width))
755 parent_class->draw_shadow_gap (style, cr, state, shadow, widget, detail,
756 x, y, width, height, gap_side, gap_x, gap_width);
760 draw_box_gap (GtkStyle *style,
763 GtkShadowType shadow,
770 GtkPositionType gap_side,
774 ThemeMatchData match_data;
776 match_data.function = TOKEN_D_BOX_GAP;
777 match_data.detail = (gchar *)detail;
778 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
779 match_data.flags = (THEME_MATCH_SHADOW |
781 THEME_MATCH_ORIENTATION);
782 match_data.shadow = shadow;
783 match_data.state = state;
785 if (!draw_gap_image (style, cr, widget, &match_data, TRUE,
786 x, y, width, height, gap_side, gap_x, gap_width))
787 parent_class->draw_box_gap (style, cr, state, shadow, widget, detail,
788 x, y, width, height, gap_side, gap_x, gap_width);
792 draw_extension (GtkStyle *style,
795 GtkShadowType shadow,
802 GtkPositionType gap_side)
804 ThemeMatchData match_data;
806 match_data.function = TOKEN_D_EXTENSION;
807 match_data.detail = (gchar *)detail;
808 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE | THEME_MATCH_GAP_SIDE;
809 match_data.shadow = shadow;
810 match_data.state = state;
811 match_data.gap_side = gap_side;
813 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
814 x, y, width, height))
815 parent_class->draw_extension (style, cr, state, shadow, widget, detail,
816 x, y, width, height, gap_side);
820 draw_focus (GtkStyle *style,
822 GtkStateType state_type,
830 ThemeMatchData match_data;
832 match_data.function = TOKEN_D_FOCUS;
833 match_data.detail = (gchar *)detail;
834 match_data.flags = 0;
836 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, FALSE,
837 x, y, width, height))
838 parent_class->draw_focus (style, cr, state_type, widget, detail,
839 x, y, width, height);
843 draw_slider (GtkStyle *style,
846 GtkShadowType shadow,
853 GtkOrientation orientation)
855 ThemeMatchData match_data;
857 match_data.function = TOKEN_D_SLIDER;
858 match_data.detail = (gchar *)detail;
859 match_data.flags = (THEME_MATCH_SHADOW |
861 THEME_MATCH_ORIENTATION);
862 match_data.shadow = shadow;
863 match_data.state = state;
864 match_data.orientation = orientation;
866 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
867 x, y, width, height))
868 parent_class->draw_slider (style, cr, state, shadow, widget, detail,
869 x, y, width, height, orientation);
874 draw_handle (GtkStyle *style,
877 GtkShadowType shadow,
884 GtkOrientation orientation)
886 ThemeMatchData match_data;
888 match_data.function = TOKEN_D_HANDLE;
889 match_data.detail = (gchar *)detail;
890 match_data.flags = (THEME_MATCH_SHADOW |
892 THEME_MATCH_ORIENTATION);
893 match_data.shadow = shadow;
894 match_data.state = state;
895 match_data.orientation = orientation;
897 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
898 x, y, width, height))
899 parent_class->draw_handle (style, cr, state, shadow, widget, detail,
900 x, y, width, height, orientation);
904 draw_expander (GtkStyle *style,
911 GtkExpanderStyle expander_style)
913 #define DEFAULT_EXPANDER_SIZE 12
915 ThemeMatchData match_data;
920 gtk_widget_class_find_style_property (GTK_WIDGET_GET_CLASS (widget),
923 gtk_widget_style_get (widget,
924 "expander-size", &expander_size,
928 expander_size = DEFAULT_EXPANDER_SIZE;
930 radius = expander_size/2;
932 match_data.function = TOKEN_D_EXPANDER;
933 match_data.detail = (gchar *)detail;
934 match_data.flags = (THEME_MATCH_STATE |
935 THEME_MATCH_EXPANDER_STYLE);
936 match_data.state = state;
937 match_data.expander_style = expander_style;
939 if (!draw_simple_image (style, cr, widget, &match_data, TRUE, TRUE,
940 x - radius, y - radius, expander_size, expander_size))
941 parent_class->draw_expander (style, cr, state, widget, detail,
942 x, y, expander_style);
946 draw_resize_grip (GtkStyle *style,
958 ThemeMatchData match_data;
960 g_return_if_fail (style != NULL);
961 g_return_if_fail (window != NULL);
963 match_data.function = TOKEN_D_RESIZE_GRIP;
964 match_data.detail = (gchar *)detail;
965 match_data.flags = (THEME_MATCH_STATE |
966 THEME_MATCH_WINDOW_EDGE);
967 match_data.state = state;
968 match_data.window_edge = edge;
970 if (!draw_simple_image_no_cairo (style, window, area, widget, &match_data, TRUE, TRUE,
971 x, y, width, height))
972 parent_class->draw_resize_grip (style, window, state, area, widget, detail,
973 edge, x, y, width, height);
976 GType pixbuf_type_style = 0;
979 pixbuf_style_register_type (GTypeModule *module)
981 const GTypeInfo object_info =
983 sizeof (PixbufStyleClass),
984 (GBaseInitFunc) NULL,
985 (GBaseFinalizeFunc) NULL,
986 (GClassInitFunc) pixbuf_style_class_init,
987 NULL, /* class_finalize */
988 NULL, /* class_data */
989 sizeof (PixbufStyle),
991 (GInstanceInitFunc) pixbuf_style_init,
994 pixbuf_type_style = g_type_module_register_type (module,
1001 pixbuf_style_init (PixbufStyle *style)
1006 pixbuf_style_class_init (PixbufStyleClass *klass)
1008 GtkStyleClass *style_class = GTK_STYLE_CLASS (klass);
1010 parent_class = g_type_class_peek_parent (klass);
1012 style_class->draw_hline = draw_hline;
1013 style_class->draw_vline = draw_vline;
1014 style_class->draw_shadow = draw_shadow;
1015 style_class->draw_arrow = draw_arrow;
1016 style_class->draw_diamond = draw_diamond;
1017 style_class->draw_box = draw_box;
1018 style_class->draw_flat_box = draw_flat_box;
1019 style_class->draw_check = draw_check;
1020 style_class->draw_option = draw_option;
1021 style_class->draw_tab = draw_tab;
1022 style_class->draw_shadow_gap = draw_shadow_gap;
1023 style_class->draw_box_gap = draw_box_gap;
1024 style_class->draw_extension = draw_extension;
1025 style_class->draw_focus = draw_focus;
1026 style_class->draw_slider = draw_slider;
1027 style_class->draw_handle = draw_handle;
1028 style_class->draw_expander = draw_expander;
1029 style_class->draw_resize_grip = draw_resize_grip;