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>
26 #undef GDK_DISABLE_DEPRECATED
29 #include "pixbuf-rc-style.h"
30 #include "pixbuf-style.h"
32 static void pixbuf_style_init (PixbufStyle *style);
33 static void pixbuf_style_class_init (PixbufStyleClass *klass);
35 static GtkStyleClass *parent_class = NULL;
38 match_theme_image (GtkStyle *style,
39 ThemeMatchData *match_data)
43 tmp_list = PIXBUF_RC_STYLE (style->rc_style)->img_list;
48 ThemeImage *image = tmp_list->data;
49 tmp_list = tmp_list->next;
51 if (match_data->function != image->match_data.function)
54 flags = match_data->flags & image->match_data.flags;
56 if (flags != image->match_data.flags) /* Required components not present */
59 if ((flags & THEME_MATCH_STATE) &&
60 match_data->state != image->match_data.state)
63 if ((flags & THEME_MATCH_SHADOW) &&
64 match_data->shadow != image->match_data.shadow)
67 if ((flags & THEME_MATCH_ARROW_DIRECTION) &&
68 match_data->arrow_direction != image->match_data.arrow_direction)
71 if ((flags & THEME_MATCH_ORIENTATION) &&
72 match_data->orientation != image->match_data.orientation)
75 if ((flags & THEME_MATCH_GAP_SIDE) &&
76 match_data->gap_side != image->match_data.gap_side)
79 if (image->match_data.detail &&
80 (!match_data->detail ||
81 strcmp (match_data->detail, image->match_data.detail) != 0))
91 draw_simple_image(GtkStyle *style,
95 ThemeMatchData *match_data,
104 gboolean setbg = FALSE;
106 if ((width == -1) && (height == -1))
108 gdk_drawable_get_size(window, &width, &height);
112 else if (width == -1)
113 gdk_drawable_get_size(window, &width, NULL);
114 else if (height == -1)
115 gdk_drawable_get_size(window, NULL, &height);
117 if (!(match_data->flags & THEME_MATCH_ORIENTATION))
119 match_data->flags |= THEME_MATCH_ORIENTATION;
122 match_data->orientation = GTK_ORIENTATION_VERTICAL;
124 match_data->orientation = GTK_ORIENTATION_HORIZONTAL;
127 image = match_theme_image (style, match_data);
130 if (image->background)
132 theme_pixbuf_render (image->background,
134 draw_center ? COMPONENT_ALL : COMPONENT_ALL | COMPONENT_CENTER,
136 x, y, width, height);
139 if (image->overlay && draw_center)
140 theme_pixbuf_render (image->overlay,
141 window, NULL, area, COMPONENT_ALL,
143 x, y, width, height);
152 draw_gap_image(GtkStyle *style,
156 ThemeMatchData *match_data,
157 gboolean draw_center,
162 GtkPositionType gap_side,
167 gboolean setbg = FALSE;
169 if ((width == -1) && (height == -1))
171 gdk_drawable_get_size(window, &width, &height);
174 else if (width == -1)
175 gdk_drawable_get_size(window, &width, NULL);
176 else if (height == -1)
177 gdk_drawable_get_size(window, NULL, &height);
179 if (!(match_data->flags & THEME_MATCH_ORIENTATION))
181 match_data->flags |= THEME_MATCH_ORIENTATION;
184 match_data->orientation = GTK_ORIENTATION_VERTICAL;
186 match_data->orientation = GTK_ORIENTATION_HORIZONTAL;
189 match_data->flags |= THEME_MATCH_GAP_SIDE;
190 match_data->gap_side = gap_side;
192 image = match_theme_image (style, match_data);
196 GdkRectangle r1, r2, r3;
197 GdkPixbuf *pixbuf = NULL;
198 guint components = COMPONENT_ALL;
201 components |= COMPONENT_CENTER;
203 if (image->gap_start)
204 pixbuf = theme_pixbuf_get_pixbuf (image->gap_start);
210 thickness = gdk_pixbuf_get_height (pixbuf);
212 thickness = style->ythickness;
215 components |= COMPONENT_NORTH_WEST | COMPONENT_NORTH | COMPONENT_NORTH_EAST;
220 r1.height = thickness;
223 r2.width = gap_width;
224 r2.height = thickness;
225 r3.x = x + gap_x + gap_width;
227 r3.width = width - (gap_x + gap_width);
228 r3.height = thickness;
233 thickness = gdk_pixbuf_get_height (pixbuf);
235 thickness = style->ythickness;
238 components |= COMPONENT_SOUTH_WEST | COMPONENT_SOUTH | COMPONENT_SOUTH_EAST;
241 r1.y = y + height - thickness;
243 r1.height = thickness;
245 r2.y = y + height - thickness;
246 r2.width = gap_width;
247 r2.height = thickness;
248 r3.x = x + gap_x + gap_width;
249 r3.y = y + height - thickness;
250 r3.width = width - (gap_x + gap_width);
251 r3.height = thickness;
256 thickness = gdk_pixbuf_get_width (pixbuf);
258 thickness = style->xthickness;
261 components |= COMPONENT_NORTH_WEST | COMPONENT_WEST | COMPONENT_SOUTH_WEST;
265 r1.width = thickness;
269 r2.width = thickness;
270 r2.height = gap_width;
272 r3.y = y + gap_x + gap_width;
273 r3.width = thickness;
274 r3.height = height - (gap_x + gap_width);
279 thickness = gdk_pixbuf_get_width (pixbuf);
281 thickness = style->xthickness;
284 components |= COMPONENT_NORTH_EAST | COMPONENT_EAST | COMPONENT_SOUTH_EAST;
286 r1.x = x + width - thickness;
288 r1.width = thickness;
290 r2.x = x + width - thickness;
292 r2.width = thickness;
293 r2.height = gap_width;
294 r3.x = x + width - thickness;
295 r3.y = y + gap_x + gap_width;
296 r3.width = thickness;
297 r3.height = height - (gap_x + gap_width);
301 g_assert_not_reached ();
304 if (image->background)
305 theme_pixbuf_render (image->background,
306 window, NULL, area, components, FALSE,
307 x, y, width, height);
308 if (image->gap_start)
309 theme_pixbuf_render (image->gap_start,
310 window, NULL, area, COMPONENT_ALL, FALSE,
311 r1.x, r1.y, r1.width, r1.height);
313 theme_pixbuf_render (image->gap,
314 window, NULL, area, COMPONENT_ALL, FALSE,
315 r2.x, r2.y, r2.width, r2.height);
317 theme_pixbuf_render (image->gap_end,
318 window, NULL, area, COMPONENT_ALL, FALSE,
319 r3.x, r3.y, r3.width, r3.height);
328 draw_hline (GtkStyle *style,
339 ThemeMatchData match_data;
341 g_return_if_fail(style != NULL);
342 g_return_if_fail(window != NULL);
344 match_data.function = TOKEN_D_HLINE;
345 match_data.detail = (gchar *)detail;
346 match_data.flags = THEME_MATCH_ORIENTATION | THEME_MATCH_STATE;
347 match_data.state = state;
348 match_data.orientation = GTK_ORIENTATION_HORIZONTAL;
350 image = match_theme_image (style, &match_data);
353 if (image->background)
354 theme_pixbuf_render (image->background,
355 window, NULL, area, COMPONENT_ALL, FALSE,
356 x1, y, (x2 - x1) + 1, 2);
359 parent_class->draw_hline (style, window, state, area, widget, detail,
364 draw_vline (GtkStyle *style,
375 ThemeMatchData match_data;
377 g_return_if_fail (style != NULL);
378 g_return_if_fail (window != NULL);
380 match_data.function = TOKEN_D_VLINE;
381 match_data.detail = (gchar *)detail;
382 match_data.flags = THEME_MATCH_ORIENTATION | THEME_MATCH_STATE;
383 match_data.state = state;
384 match_data.orientation = GTK_ORIENTATION_VERTICAL;
386 image = match_theme_image (style, &match_data);
389 if (image->background)
390 theme_pixbuf_render (image->background,
391 window, NULL, area, COMPONENT_ALL, FALSE,
392 x, y1, 2, (y2 - y1) + 1);
395 parent_class->draw_vline (style, window, state, area, widget, detail,
400 draw_shadow(GtkStyle *style,
403 GtkShadowType shadow,
412 ThemeMatchData match_data;
414 g_return_if_fail(style != NULL);
415 g_return_if_fail(window != NULL);
417 match_data.function = TOKEN_D_SHADOW;
418 match_data.detail = (gchar *)detail;
419 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
420 match_data.shadow = shadow;
421 match_data.state = state;
423 if (!draw_simple_image (style, window, area, widget, &match_data, FALSE, FALSE,
424 x, y, width, height))
425 parent_class->draw_shadow (style, window, state, shadow, area, widget, detail,
426 x, y, width, height);
429 /* This function makes up for some brokeness in gtkrange.c
430 * where we never get the full arrow of the stepper button
431 * and the type of button in a single drawing function.
433 * It doesn't work correctly when the scrollbar is squished
434 * to the point we don't have room for full-sized steppers.
437 reverse_engineer_stepper_box (GtkWidget *range,
438 GtkArrowType arrow_type,
444 gint slider_width = 14, stepper_size = 14;
450 gtk_widget_style_get (range,
451 "slider_width", &slider_width,
452 "stepper_size", &stepper_size,
456 if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
458 box_width = slider_width;
459 box_height = stepper_size;
463 box_width = stepper_size;
464 box_height = slider_width;
467 *x = *x - (box_width - *width) / 2;
468 *y = *y - (box_height - *height) / 2;
470 *height = box_height;
474 draw_arrow (GtkStyle *style,
477 GtkShadowType shadow,
481 GtkArrowType arrow_direction,
488 ThemeMatchData match_data;
490 g_return_if_fail(style != NULL);
491 g_return_if_fail(window != NULL);
494 (strcmp (detail, "hscrollbar") == 0 || strcmp (detail, "vscrollbar") == 0))
496 /* This is a hack to work around the fact that scrollbar steppers are drawn
497 * as a box + arrow, so we never have
499 * The full bounding box of the scrollbar
500 * The arrow direction
502 * At the same time. We simulate an extra paint function, "STEPPER", by doing
503 * nothing for the box, and then here, reverse engineering the box that
504 * was passed to draw box and using that
508 gint box_width = width;
509 gint box_height = height;
511 reverse_engineer_stepper_box (widget, arrow_direction,
512 &box_x, &box_y, &box_width, &box_height);
514 match_data.function = TOKEN_D_STEPPER;
515 match_data.detail = (gchar *)detail;
516 match_data.flags = (THEME_MATCH_SHADOW |
518 THEME_MATCH_ARROW_DIRECTION);
519 match_data.shadow = shadow;
520 match_data.state = state;
521 match_data.arrow_direction = arrow_direction;
523 if (draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
524 box_x, box_y, box_width, box_height))
526 /* The theme included stepper images, we're done */
530 /* Otherwise, draw the full box, and fall through to draw the arrow
532 match_data.function = TOKEN_D_BOX;
533 match_data.detail = (gchar *)detail;
534 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
535 match_data.shadow = shadow;
536 match_data.state = state;
538 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
539 box_x, box_y, box_width, box_height))
540 parent_class->draw_box (style, window, state, shadow, area, widget, detail,
541 box_x, box_y, box_width, box_height);
545 match_data.function = TOKEN_D_ARROW;
546 match_data.detail = (gchar *)detail;
547 match_data.flags = (THEME_MATCH_SHADOW |
549 THEME_MATCH_ARROW_DIRECTION);
550 match_data.shadow = shadow;
551 match_data.state = state;
552 match_data.arrow_direction = arrow_direction;
554 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
555 x, y, width, height))
556 parent_class->draw_arrow (style, window, state, shadow, area, widget, detail,
557 arrow_direction, fill, x, y, width, height);
561 draw_diamond (GtkStyle *style,
564 GtkShadowType shadow,
573 ThemeMatchData match_data;
575 g_return_if_fail(style != NULL);
576 g_return_if_fail(window != NULL);
578 match_data.function = TOKEN_D_DIAMOND;
579 match_data.detail = (gchar *)detail;
580 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
581 match_data.shadow = shadow;
582 match_data.state = state;
584 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
585 x, y, width, height))
586 parent_class->draw_diamond (style, window, state, shadow, area, widget, detail,
587 x, y, width, height);
591 draw_string (GtkStyle * style,
599 const gchar * string)
601 g_return_if_fail(style != NULL);
602 g_return_if_fail(window != NULL);
604 if (state == GTK_STATE_INSENSITIVE)
608 gdk_gc_set_clip_rectangle(style->white_gc, area);
609 gdk_gc_set_clip_rectangle(style->fg_gc[state], area);
612 gdk_draw_string(window, gtk_style_get_font (style), style->fg_gc[state], x, y, string);
616 gdk_gc_set_clip_rectangle(style->white_gc, NULL);
617 gdk_gc_set_clip_rectangle(style->fg_gc[state], NULL);
622 gdk_gc_set_clip_rectangle(style->fg_gc[state], area);
623 gdk_draw_string(window, gtk_style_get_font (style), style->fg_gc[state], x, y, string);
624 gdk_gc_set_clip_rectangle(style->fg_gc[state], NULL);
629 draw_box (GtkStyle *style,
632 GtkShadowType shadow,
641 ThemeMatchData match_data;
643 g_return_if_fail(style != NULL);
644 g_return_if_fail(window != NULL);
647 (strcmp (detail, "hscrollbar") == 0 || strcmp (detail, "vscrollbar") == 0))
649 /* We handle this in draw_arrow */
653 match_data.function = TOKEN_D_BOX;
654 match_data.detail = (gchar *)detail;
655 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
656 match_data.shadow = shadow;
657 match_data.state = state;
659 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
660 x, y, width, height)) {
661 parent_class->draw_box (style, window, state, shadow, area, widget, detail,
662 x, y, width, height);
667 draw_flat_box (GtkStyle *style,
670 GtkShadowType shadow,
679 ThemeMatchData match_data;
681 g_return_if_fail(style != NULL);
682 g_return_if_fail(window != NULL);
684 match_data.function = TOKEN_D_FLAT_BOX;
685 match_data.detail = (gchar *)detail;
686 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
687 match_data.shadow = shadow;
688 match_data.state = state;
690 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
691 x, y, width, height))
692 parent_class->draw_flat_box (style, window, state, shadow, area, widget, detail,
693 x, y, width, height);
697 draw_check (GtkStyle *style,
700 GtkShadowType shadow,
709 ThemeMatchData match_data;
711 g_return_if_fail(style != NULL);
712 g_return_if_fail(window != NULL);
714 match_data.function = TOKEN_D_CHECK;
715 match_data.detail = (gchar *)detail;
716 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
717 match_data.shadow = shadow;
718 match_data.state = state;
720 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
721 x, y, width, height))
722 parent_class->draw_check (style, window, state, shadow, area, widget, detail,
723 x, y, width, height);
727 draw_option (GtkStyle *style,
730 GtkShadowType shadow,
739 ThemeMatchData match_data;
741 g_return_if_fail(style != NULL);
742 g_return_if_fail(window != NULL);
744 match_data.function = TOKEN_D_OPTION;
745 match_data.detail = (gchar *)detail;
746 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
747 match_data.shadow = shadow;
748 match_data.state = state;
750 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
751 x, y, width, height))
752 parent_class->draw_option (style, window, state, shadow, area, widget, detail,
753 x, y, width, height);
757 draw_tab (GtkStyle *style,
760 GtkShadowType shadow,
769 ThemeMatchData match_data;
771 g_return_if_fail(style != NULL);
772 g_return_if_fail(window != NULL);
774 match_data.function = TOKEN_D_TAB;
775 match_data.detail = (gchar *)detail;
776 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
777 match_data.shadow = shadow;
778 match_data.state = state;
780 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
781 x, y, width, height))
782 parent_class->draw_tab (style, window, state, shadow, area, widget, detail,
783 x, y, width, height);
787 draw_shadow_gap (GtkStyle *style,
790 GtkShadowType shadow,
798 GtkPositionType gap_side,
802 ThemeMatchData match_data;
804 match_data.function = TOKEN_D_SHADOW_GAP;
805 match_data.detail = (gchar *)detail;
806 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
807 match_data.flags = (THEME_MATCH_SHADOW |
809 THEME_MATCH_ORIENTATION);
810 match_data.shadow = shadow;
811 match_data.state = state;
813 if (!draw_gap_image (style, window, area, widget, &match_data, FALSE,
814 x, y, width, height, gap_side, gap_x, gap_width))
815 parent_class->draw_shadow_gap (style, window, state, shadow, area, widget, detail,
816 x, y, width, height, gap_side, gap_x, gap_width);
820 draw_box_gap (GtkStyle *style,
823 GtkShadowType shadow,
831 GtkPositionType gap_side,
835 ThemeMatchData match_data;
837 match_data.function = TOKEN_D_BOX_GAP;
838 match_data.detail = (gchar *)detail;
839 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE;
840 match_data.flags = (THEME_MATCH_SHADOW |
842 THEME_MATCH_ORIENTATION);
843 match_data.shadow = shadow;
844 match_data.state = state;
846 if (!draw_gap_image (style, window, area, widget, &match_data, TRUE,
847 x, y, width, height, gap_side, gap_x, gap_width))
848 parent_class->draw_box_gap (style, window, state, shadow, area, widget, detail,
849 x, y, width, height, gap_side, gap_x, gap_width);
853 draw_extension (GtkStyle *style,
856 GtkShadowType shadow,
864 GtkPositionType gap_side)
866 ThemeMatchData match_data;
868 g_return_if_fail(style != NULL);
869 g_return_if_fail(window != NULL);
877 match_data.function = TOKEN_D_EXTENSION;
878 match_data.detail = (gchar *)detail;
879 match_data.flags = THEME_MATCH_SHADOW | THEME_MATCH_STATE | THEME_MATCH_GAP_SIDE;
880 match_data.shadow = shadow;
881 match_data.state = state;
882 match_data.gap_side = gap_side;
884 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
885 x, y, width, height))
886 parent_class->draw_extension (style, window, state, shadow, area, widget, detail,
887 x, y, width, height, gap_side);
891 draw_focus (GtkStyle *style,
893 GtkStateType state_type,
902 ThemeMatchData match_data;
904 g_return_if_fail(style != NULL);
905 g_return_if_fail(window != NULL);
907 match_data.function = TOKEN_D_FOCUS;
908 match_data.detail = (gchar *)detail;
909 match_data.flags = 0;
911 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, FALSE,
912 x, y, width, height))
913 parent_class->draw_focus (style, window, state_type, area, widget, detail,
914 x, y, width, height);
918 draw_slider (GtkStyle *style,
921 GtkShadowType shadow,
929 GtkOrientation orientation)
931 ThemeMatchData match_data;
933 g_return_if_fail(style != NULL);
934 g_return_if_fail(window != NULL);
936 match_data.function = TOKEN_D_SLIDER;
937 match_data.detail = (gchar *)detail;
938 match_data.flags = (THEME_MATCH_SHADOW |
940 THEME_MATCH_ORIENTATION);
941 match_data.shadow = shadow;
942 match_data.state = state;
943 match_data.orientation = orientation;
945 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
946 x, y, width, height))
947 parent_class->draw_slider (style, window, state, shadow, area, widget, detail,
948 x, y, width, height, orientation);
953 draw_handle (GtkStyle *style,
956 GtkShadowType shadow,
964 GtkOrientation orientation)
966 ThemeMatchData match_data;
968 g_return_if_fail (style != NULL);
969 g_return_if_fail (window != NULL);
971 match_data.function = TOKEN_D_HANDLE;
972 match_data.detail = (gchar *)detail;
973 match_data.flags = (THEME_MATCH_SHADOW |
975 THEME_MATCH_ORIENTATION);
976 match_data.shadow = shadow;
977 match_data.state = state;
978 match_data.orientation = orientation;
980 if (!draw_simple_image (style, window, area, widget, &match_data, TRUE, TRUE,
981 x, y, width, height))
982 parent_class->draw_handle (style, window, state, shadow, area, widget, detail,
983 x, y, width, height, orientation);
986 GType pixbuf_type_style = 0;
989 pixbuf_style_register_type (GTypeModule *module)
991 static const GTypeInfo object_info =
993 sizeof (PixbufStyleClass),
994 (GBaseInitFunc) NULL,
995 (GBaseFinalizeFunc) NULL,
996 (GClassInitFunc) pixbuf_style_class_init,
997 NULL, /* class_finalize */
998 NULL, /* class_data */
999 sizeof (PixbufStyle),
1000 0, /* n_preallocs */
1001 (GInstanceInitFunc) pixbuf_style_init,
1004 pixbuf_type_style = g_type_module_register_type (module,
1011 pixbuf_style_init (PixbufStyle *style)
1016 pixbuf_style_class_init (PixbufStyleClass *klass)
1018 GtkStyleClass *style_class = GTK_STYLE_CLASS (klass);
1020 parent_class = g_type_class_peek_parent (klass);
1022 style_class->draw_hline = draw_hline;
1023 style_class->draw_vline = draw_vline;
1024 style_class->draw_shadow = draw_shadow;
1025 style_class->draw_arrow = draw_arrow;
1026 style_class->draw_diamond = draw_diamond;
1027 style_class->draw_string = draw_string;
1028 style_class->draw_box = draw_box;
1029 style_class->draw_flat_box = draw_flat_box;
1030 style_class->draw_check = draw_check;
1031 style_class->draw_option = draw_option;
1032 style_class->draw_tab = draw_tab;
1033 style_class->draw_shadow_gap = draw_shadow_gap;
1034 style_class->draw_box_gap = draw_box_gap;
1035 style_class->draw_extension = draw_extension;
1036 style_class->draw_focus = draw_focus;
1037 style_class->draw_slider = draw_slider;
1038 style_class->draw_handle = draw_handle;