1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "gtkwidget.h"
24 #define LIGHTNESS_MULT 1.3
25 #define DARKNESS_MULT 0.7
28 typedef struct _GtkStyleKey GtkStyleKey;
37 GdkPixmap *bg_pixmap[5];
42 GdkColormap *colormap;
47 static void gtk_style_init (GtkStyle *style);
48 static void gtk_styles_init (void);
49 static void gtk_style_remove (GtkStyle *style);
50 static GtkStyle* gtk_style_find (GtkStyle *style,
53 static GtkStyle* gtk_style_new_from_key (GtkStyleKey *key);
54 static GtkStyleKey* gtk_style_key_dup (GtkStyleKey *key);
55 static void gtk_style_destroy (GtkStyle *style);
56 static void gtk_style_key_destroy (GtkStyleKey *key);
57 static guint gtk_style_key_hash (GtkStyleKey *key);
58 static guint gtk_style_value_hash (GtkStyle *style);
59 static gint gtk_style_key_compare (GtkStyleKey *a,
62 static void gtk_default_draw_hline (GtkStyle *style,
64 GtkStateType state_type,
68 static void gtk_default_draw_vline (GtkStyle *style,
70 GtkStateType state_type,
74 static void gtk_default_draw_shadow (GtkStyle *style,
76 GtkStateType state_type,
77 GtkShadowType shadow_type,
82 static void gtk_default_draw_polygon (GtkStyle *style,
84 GtkStateType state_type,
85 GtkShadowType shadow_type,
89 static void gtk_default_draw_arrow (GtkStyle *style,
91 GtkStateType state_type,
92 GtkShadowType shadow_type,
93 GtkArrowType arrow_type,
99 static void gtk_default_draw_diamond (GtkStyle *style,
101 GtkStateType state_type,
102 GtkShadowType shadow_type,
107 static void gtk_default_draw_oval (GtkStyle *style,
109 GtkStateType state_type,
110 GtkShadowType shadow_type,
115 static void gtk_default_draw_string (GtkStyle *style,
117 GtkStateType state_type,
120 const gchar *string);
122 static void gtk_style_shade (GdkColor *a, GdkColor *b, gdouble k);
123 static void rgb_to_hls (gdouble *r, gdouble *g, gdouble *b);
124 static void hls_to_rgb (gdouble *h, gdouble *l, gdouble *s);
127 static GtkStyleClass default_class =
131 gtk_default_draw_hline,
132 gtk_default_draw_vline,
133 gtk_default_draw_shadow,
134 gtk_default_draw_polygon,
135 gtk_default_draw_arrow,
136 gtk_default_draw_diamond,
137 gtk_default_draw_oval,
138 gtk_default_draw_string,
141 static GdkColor gtk_default_normal_fg = { 0, 0, 0, 0 };
142 static GdkColor gtk_default_active_fg = { 0, 0, 0, 0 };
143 static GdkColor gtk_default_prelight_fg = { 0, 0, 0, 0 };
144 static GdkColor gtk_default_selected_fg = { 0, 0xffff, 0xffff, 0xffff };
145 static GdkColor gtk_default_insensitive_fg = { 0, 0x7530, 0x7530, 0x7530 };
147 static GdkColor gtk_default_normal_bg = { 0, 0xd6d6, 0xd6d6, 0xd6d6 };
148 static GdkColor gtk_default_active_bg = { 0, 0xc350, 0xc350, 0xc350 };
149 static GdkColor gtk_default_prelight_bg = { 0, 0xea60, 0xea60, 0xea60 };
150 static GdkColor gtk_default_selected_bg = { 0, 0, 0, 0x9c40 };
151 static GdkColor gtk_default_insensitive_bg = { 0, 0xd6d6, 0xd6d6, 0xd6d6 };
153 static GdkFont *default_font = NULL;
155 static gint initialize = TRUE;
156 static GCache *style_cache = NULL;
157 static GSList *unattached_styles = NULL;
159 static GMemChunk *key_mem_chunk = NULL;
162 gtk_style_copy (GtkStyle *style)
167 g_return_val_if_fail (style != NULL, NULL);
169 new_style = gtk_style_new ();
171 for (i = 0; i < 5; i++)
173 new_style->fg[i] = style->fg[i];
174 new_style->bg[i] = style->bg[i];
175 new_style->text[i] = style->text[i];
176 new_style->base[i] = style->base[i];
178 new_style->bg_pixmap[i] = style->bg_pixmap[i];
181 new_style->font = style->font;
182 gdk_font_ref (new_style->font);
193 style = g_new0 (GtkStyle, 1);
197 gdk_font_load ("-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*");
199 style->font = default_font;
200 gdk_font_ref (style->font);
202 style->ref_count = 1;
203 style->attach_count = 0;
204 style->colormap = NULL;
206 style->klass = &default_class;
208 style->black.red = 0;
209 style->black.green = 0;
210 style->black.blue = 0;
212 style->white.red = 65535;
213 style->white.green = 65535;
214 style->white.blue = 65535;
216 style->black_gc = NULL;
217 style->white_gc = NULL;
219 style->fg[GTK_STATE_NORMAL] = gtk_default_normal_fg;
220 style->fg[GTK_STATE_ACTIVE] = gtk_default_active_fg;
221 style->fg[GTK_STATE_PRELIGHT] = gtk_default_prelight_fg;
222 style->fg[GTK_STATE_SELECTED] = gtk_default_selected_fg;
223 style->fg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
225 style->bg[GTK_STATE_NORMAL] = gtk_default_normal_bg;
226 style->bg[GTK_STATE_ACTIVE] = gtk_default_active_bg;
227 style->bg[GTK_STATE_PRELIGHT] = gtk_default_prelight_bg;
228 style->bg[GTK_STATE_SELECTED] = gtk_default_selected_bg;
229 style->bg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_bg;
231 for (i = 0; i < 5; i++)
233 style->text[i] = style->fg[i];
234 style->base[i] = style->white;
237 for (i = 0; i < 5; i++)
238 style->bg_pixmap[i] = NULL;
240 for (i = 0; i < 5; i++)
242 style->fg_gc[i] = NULL;
243 style->bg_gc[i] = NULL;
244 style->light_gc[i] = NULL;
245 style->dark_gc[i] = NULL;
246 style->mid_gc[i] = NULL;
247 style->text_gc[i] = NULL;
248 style->base_gc[i] = NULL;
251 unattached_styles = g_slist_prepend (unattached_styles, style);
257 gtk_style_attach (GtkStyle *style,
261 GdkColormap *colormap;
264 g_return_val_if_fail (style != NULL, NULL);
265 g_return_val_if_fail (window != NULL, NULL);
267 colormap = gdk_window_get_colormap (window);
268 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
270 new_style = gtk_style_find (style, colormap, depth);
272 if (new_style && (new_style != style))
274 gtk_style_unref (style);
276 gtk_style_ref (style);
279 if (style->attach_count == 0)
280 unattached_styles = g_slist_remove (unattached_styles, style);
282 style->attach_count += 1;
288 gtk_style_detach (GtkStyle *style)
292 g_return_if_fail (style != NULL);
294 style->attach_count -= 1;
295 if (style->attach_count == 0)
297 unattached_styles = g_slist_prepend (unattached_styles, style);
299 gtk_gc_release (style->black_gc);
300 gtk_gc_release (style->white_gc);
302 style->black_gc = NULL;
303 style->white_gc = NULL;
305 for (i = 0; i < 5; i++)
307 gtk_gc_release (style->fg_gc[i]);
308 gtk_gc_release (style->bg_gc[i]);
309 gtk_gc_release (style->light_gc[i]);
310 gtk_gc_release (style->dark_gc[i]);
311 gtk_gc_release (style->mid_gc[i]);
312 gtk_gc_release (style->text_gc[i]);
313 gtk_gc_release (style->base_gc[i]);
315 style->fg_gc[i] = NULL;
316 style->bg_gc[i] = NULL;
317 style->light_gc[i] = NULL;
318 style->dark_gc[i] = NULL;
319 style->mid_gc[i] = NULL;
320 style->text_gc[i] = NULL;
321 style->base_gc[i] = NULL;
325 style->colormap = NULL;
328 gtk_style_remove (style);
332 gtk_style_ref (GtkStyle *style)
334 g_return_val_if_fail (style != NULL, NULL);
336 style->ref_count += 1;
341 gtk_style_unref (GtkStyle *style)
343 g_return_if_fail (style != NULL);
345 style->ref_count -= 1;
346 if (style->ref_count == 0)
347 gtk_style_destroy (style);
351 gtk_style_set_background (GtkStyle *style,
353 GtkStateType state_type)
356 gint parent_relative;
358 g_return_if_fail (style != NULL);
359 g_return_if_fail (window != NULL);
361 if (style->bg_pixmap[state_type])
363 if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
366 parent_relative = TRUE;
370 pixmap = style->bg_pixmap[state_type];
371 parent_relative = FALSE;
374 gdk_window_set_back_pixmap (window, pixmap, parent_relative);
377 gdk_window_set_background (window, &style->bg[state_type]);
382 gtk_draw_hline (GtkStyle *style,
384 GtkStateType state_type,
389 g_return_if_fail (style != NULL);
390 g_return_if_fail (style->klass != NULL);
391 g_return_if_fail (style->klass->draw_hline != NULL);
393 (*style->klass->draw_hline) (style, window, state_type, x1, x2, y);
398 gtk_draw_vline (GtkStyle *style,
400 GtkStateType state_type,
405 g_return_if_fail (style != NULL);
406 g_return_if_fail (style->klass != NULL);
407 g_return_if_fail (style->klass->draw_vline != NULL);
409 (*style->klass->draw_vline) (style, window, state_type, y1, y2, x);
414 gtk_draw_shadow (GtkStyle *style,
416 GtkStateType state_type,
417 GtkShadowType shadow_type,
423 g_return_if_fail (style != NULL);
424 g_return_if_fail (style->klass != NULL);
425 g_return_if_fail (style->klass->draw_shadow != NULL);
427 (*style->klass->draw_shadow) (style, window, state_type, shadow_type, x, y, width, height);
431 gtk_draw_polygon (GtkStyle *style,
433 GtkStateType state_type,
434 GtkShadowType shadow_type,
439 g_return_if_fail (style != NULL);
440 g_return_if_fail (style->klass != NULL);
441 g_return_if_fail (style->klass->draw_shadow != NULL);
443 (*style->klass->draw_polygon) (style, window, state_type, shadow_type, points, npoints, fill);
447 gtk_draw_arrow (GtkStyle *style,
449 GtkStateType state_type,
450 GtkShadowType shadow_type,
451 GtkArrowType arrow_type,
458 g_return_if_fail (style != NULL);
459 g_return_if_fail (style->klass != NULL);
460 g_return_if_fail (style->klass->draw_arrow != NULL);
462 (*style->klass->draw_arrow) (style, window, state_type, shadow_type, arrow_type, fill, x, y, width, height);
467 gtk_draw_diamond (GtkStyle *style,
469 GtkStateType state_type,
470 GtkShadowType shadow_type,
476 g_return_if_fail (style != NULL);
477 g_return_if_fail (style->klass != NULL);
478 g_return_if_fail (style->klass->draw_diamond != NULL);
480 (*style->klass->draw_diamond) (style, window, state_type, shadow_type, x, y, width, height);
485 gtk_draw_oval (GtkStyle *style,
487 GtkStateType state_type,
488 GtkShadowType shadow_type,
494 g_return_if_fail (style != NULL);
495 g_return_if_fail (style->klass != NULL);
496 g_return_if_fail (style->klass->draw_oval != NULL);
498 (*style->klass->draw_oval) (style, window, state_type, shadow_type, x, y, width, height);
502 gtk_draw_string (GtkStyle *style,
504 GtkStateType state_type,
509 g_return_if_fail (style != NULL);
510 g_return_if_fail (style->klass != NULL);
511 g_return_if_fail (style->klass->draw_oval != NULL);
513 (*style->klass->draw_string) (style, window, state_type, x, y, string);
518 gtk_style_init (GtkStyle *style)
520 GdkGCValues gc_values;
521 GdkGCValuesMask gc_values_mask;
522 GdkColormap *colormap;
525 g_return_if_fail (style != NULL);
527 if (style->attach_count == 0)
529 for (i = 0; i < 5; i++)
531 gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
532 gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);
534 style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
535 style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
536 style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
539 colormap = style->colormap;
541 gdk_color_black (colormap, &style->black);
542 gdk_color_white (colormap, &style->white);
544 gc_values_mask = GDK_GC_FOREGROUND | GDK_GC_FONT;
545 if (style->font->type == GDK_FONT_FONT)
547 gc_values.font = style->font;
549 else if (style->font->type == GDK_FONT_FONTSET)
551 gc_values.font = default_font;
554 gc_values.foreground = style->black;
555 style->black_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
557 gc_values.foreground = style->white;
558 style->white_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
560 for (i = 0; i < 5; i++)
562 if (!gdk_color_alloc (colormap, &style->fg[i]))
563 g_warning ("unable to allocate color: ( %d %d %d )",
564 style->fg[i].red, style->fg[i].green, style->fg[i].blue);
565 if (!gdk_color_alloc (colormap, &style->bg[i]))
566 g_warning ("unable to allocate color: ( %d %d %d )",
567 style->bg[i].red, style->bg[i].green, style->bg[i].blue);
568 if (!gdk_color_alloc (colormap, &style->light[i]))
569 g_warning ("unable to allocate color: ( %d %d %d )",
570 style->light[i].red, style->light[i].green, style->light[i].blue);
571 if (!gdk_color_alloc (colormap, &style->dark[i]))
572 g_warning ("unable to allocate color: ( %d %d %d )",
573 style->dark[i].red, style->dark[i].green, style->dark[i].blue);
574 if (!gdk_color_alloc (colormap, &style->mid[i]))
575 g_warning ("unable to allocate color: ( %d %d %d )",
576 style->mid[i].red, style->mid[i].green, style->mid[i].blue);
577 if (!gdk_color_alloc (colormap, &style->text[i]))
578 g_warning ("unable to allocate color: ( %d %d %d )",
579 style->text[i].red, style->text[i].green, style->text[i].blue);
580 if (!gdk_color_alloc (colormap, &style->base[i]))
581 g_warning ("unable to allocate color: ( %d %d %d )",
582 style->base[i].red, style->base[i].green, style->base[i].blue);
584 gc_values.foreground = style->fg[i];
585 style->fg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
587 gc_values.foreground = style->bg[i];
588 style->bg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
590 gc_values.foreground = style->light[i];
591 style->light_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
593 gc_values.foreground = style->dark[i];
594 style->dark_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
596 gc_values.foreground = style->mid[i];
597 style->mid_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
599 gc_values.foreground = style->text[i];
600 style->text_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
602 gc_values.foreground = style->base[i];
603 style->base_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
615 style_cache = g_cache_new ((GCacheNewFunc) gtk_style_new_from_key,
616 (GCacheDestroyFunc) gtk_style_unref,
617 (GCacheDupFunc) gtk_style_key_dup,
618 (GCacheDestroyFunc) gtk_style_key_destroy,
619 (GHashFunc) gtk_style_key_hash,
620 (GHashFunc) gtk_style_value_hash,
621 (GCompareFunc) gtk_style_key_compare);
626 gtk_style_remove (GtkStyle *style)
630 g_cache_remove (style_cache, style);
634 gtk_style_find (GtkStyle *style,
644 for (i = 0; i < 5; i++)
646 key.fg[i] = style->fg[i];
647 key.bg[i] = style->bg[i];
648 key.text[i] = style->text[i];
649 key.base[i] = style->base[i];
650 key.bg_pixmap[i] = style->bg_pixmap[i];
653 key.font = style->font;
654 key.klass = style->klass;
658 style = g_cache_insert (style_cache, &key);
664 gtk_style_new_from_key (GtkStyleKey *key)
671 list = unattached_styles;
678 if ((style->depth != -1) && (style->depth != key->depth))
683 if (style->colormap && (style->colormap != key->colormap))
688 if (style->klass != key->klass)
693 if (!gdk_font_equal (style->font, key->font))
699 for (i = 0; style && (i < 5); i++)
701 if (style->bg_pixmap[i] != key->bg_pixmap[i])
707 if ((style->fg[i].red != key->fg[i].red) ||
708 (style->fg[i].green != key->fg[i].green) ||
709 (style->fg[i].blue != key->fg[i].blue))
715 if ((style->bg[i].red != key->bg[i].red) ||
716 (style->bg[i].green != key->bg[i].green) ||
717 (style->bg[i].blue != key->bg[i].blue))
723 if ((style->text[i].red != key->text[i].red) ||
724 (style->text[i].green != key->text[i].green) ||
725 (style->text[i].blue != key->text[i].blue))
731 if ((style->base[i].red != key->base[i].red) ||
732 (style->base[i].green != key->base[i].green) ||
733 (style->base[i].blue != key->base[i].blue))
742 gtk_style_ref (style);
749 style = g_new0 (GtkStyle, 1);
751 style->ref_count = 1;
752 style->attach_count = 0;
754 style->font = key->font;
755 gdk_font_ref (style->font);
757 style->depth = key->depth;
758 style->colormap = key->colormap;
759 style->klass = key->klass;
761 style->black.red = 0;
762 style->black.green = 0;
763 style->black.blue = 0;
765 style->white.red = 65535;
766 style->white.green = 65535;
767 style->white.blue = 65535;
769 style->black_gc = NULL;
770 style->white_gc = NULL;
772 for (i = 0; i < 5; i++)
774 style->fg[i] = key->fg[i];
775 style->bg[i] = key->bg[i];
776 style->text[i] = key->text[i];
777 style->base[i] = key->base[i];
780 for (i = 0; i < 5; i++)
781 style->bg_pixmap[i] = key->bg_pixmap[i];
783 for (i = 0; i < 5; i++)
785 style->fg_gc[i] = NULL;
786 style->bg_gc[i] = NULL;
787 style->light_gc[i] = NULL;
788 style->dark_gc[i] = NULL;
789 style->mid_gc[i] = NULL;
790 style->text_gc[i] = NULL;
791 style->base_gc[i] = NULL;
795 if (style->depth == -1)
796 style->depth = key->depth;
797 if (!style->colormap)
798 style->colormap = key->colormap;
800 gtk_style_init (style);
806 gtk_style_key_dup (GtkStyleKey *key)
808 GtkStyleKey *new_key;
811 key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkStyleKey),
812 1024, G_ALLOC_AND_FREE);
814 new_key = g_chunk_new (GtkStyleKey, key_mem_chunk);
822 gtk_style_destroy (GtkStyle *style)
826 if (style->attach_count > 0)
828 gtk_gc_release (style->black_gc);
829 gtk_gc_release (style->white_gc);
831 for (i = 0; i < 5; i++)
833 gtk_gc_release (style->fg_gc[i]);
834 gtk_gc_release (style->bg_gc[i]);
835 gtk_gc_release (style->light_gc[i]);
836 gtk_gc_release (style->dark_gc[i]);
837 gtk_gc_release (style->mid_gc[i]);
838 gtk_gc_release (style->text_gc[i]);
839 gtk_gc_release (style->base_gc[i]);
843 unattached_styles = g_slist_remove (unattached_styles, style);
845 gdk_font_unref (style->font);
850 gtk_style_key_destroy (GtkStyleKey *key)
852 g_mem_chunk_free (key_mem_chunk, key);
856 gtk_style_key_hash (GtkStyleKey *key)
863 for (i = 0; i < 5; i++)
865 hash_val += key->fg[i].red + key->fg[i].green + key->fg[i].blue;
866 hash_val += key->bg[i].red + key->bg[i].green + key->bg[i].blue;
867 hash_val += key->text[i].red + key->text[i].green + key->text[i].blue;
868 hash_val += key->base[i].red + key->base[i].green + key->base[i].blue;
871 hash_val += (guint) gdk_font_id (key->font);
872 hash_val += (guint) key->depth;
873 hash_val += (gulong) key->colormap;
874 hash_val += (gulong) key->klass;
880 gtk_style_value_hash (GtkStyle *style)
887 for (i = 0; i < 5; i++)
889 hash_val += style->fg[i].red + style->fg[i].green + style->fg[i].blue;
890 hash_val += style->bg[i].red + style->bg[i].green + style->bg[i].blue;
891 hash_val += style->text[i].red + style->text[i].green + style->text[i].blue;
892 hash_val += style->base[i].red + style->base[i].green + style->base[i].blue;
895 hash_val += (guint) gdk_font_id (style->font);
896 hash_val += (gulong) style->klass;
902 gtk_style_key_compare (GtkStyleKey *a,
907 if (a->depth != b->depth)
909 if (a->colormap != b->colormap)
911 if (a->klass != b->klass)
913 if (!gdk_font_equal (a->font, b->font))
916 for (i = 0; i < 5; i++)
918 if (a->bg_pixmap[i] != b->bg_pixmap[i])
921 if ((a->fg[i].red != b->fg[i].red) ||
922 (a->fg[i].green != b->fg[i].green) ||
923 (a->fg[i].blue != b->fg[i].blue))
925 if ((a->bg[i].red != b->bg[i].red) ||
926 (a->bg[i].green != b->bg[i].green) ||
927 (a->bg[i].blue != b->bg[i].blue))
929 if ((a->text[i].red != b->text[i].red) ||
930 (a->text[i].green != b->text[i].green) ||
931 (a->text[i].blue != b->text[i].blue))
933 if ((a->base[i].red != b->base[i].red) ||
934 (a->base[i].green != b->base[i].green) ||
935 (a->base[i].blue != b->base[i].blue))
944 gtk_default_draw_hline (GtkStyle *style,
946 GtkStateType state_type,
951 gint thickness_light;
955 g_return_if_fail (style != NULL);
956 g_return_if_fail (window != NULL);
958 thickness_light = style->klass->ythickness / 2;
959 thickness_dark = style->klass->ythickness - thickness_light;
961 for (i = 0; i < thickness_dark; i++)
963 gdk_draw_line (window, style->light_gc[state_type], x2 - i - 1, y + i, x2, y + i);
964 gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x2 - i - 1, y + i);
968 for (i = 0; i < thickness_light; i++)
970 gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x1 + thickness_light - i - 1, y + i);
971 gdk_draw_line (window, style->light_gc[state_type], x1 + thickness_light - i - 1, y + i, x2, y + i);
977 gtk_default_draw_vline (GtkStyle *style,
979 GtkStateType state_type,
984 gint thickness_light;
988 g_return_if_fail (style != NULL);
989 g_return_if_fail (window != NULL);
991 thickness_light = style->klass->xthickness / 2;
992 thickness_dark = style->klass->xthickness - thickness_light;
994 for (i = 0; i < thickness_dark; i++)
996 gdk_draw_line (window, style->light_gc[state_type], x + i, y2 - i - 1, x + i, y2);
997 gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y2 - i - 1);
1000 x += thickness_dark;
1001 for (i = 0; i < thickness_light; i++)
1003 gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y1 + thickness_light - i);
1004 gdk_draw_line (window, style->light_gc[state_type], x + i, y1 + thickness_light - i, x + i, y2);
1010 gtk_default_draw_shadow (GtkStyle *style,
1012 GtkStateType state_type,
1013 GtkShadowType shadow_type,
1021 gint thickness_light;
1022 gint thickness_dark;
1025 g_return_if_fail (style != NULL);
1026 g_return_if_fail (window != NULL);
1028 if ((width == -1) && (height == -1))
1029 gdk_window_get_size (window, &width, &height);
1030 else if (width == -1)
1031 gdk_window_get_size (window, &width, NULL);
1032 else if (height == -1)
1033 gdk_window_get_size (window, NULL, &height);
1035 switch (shadow_type)
1037 case GTK_SHADOW_NONE:
1042 case GTK_SHADOW_ETCHED_IN:
1043 gc1 = style->light_gc[state_type];
1044 gc2 = style->dark_gc[state_type];
1046 case GTK_SHADOW_OUT:
1047 case GTK_SHADOW_ETCHED_OUT:
1048 gc1 = style->dark_gc[state_type];
1049 gc2 = style->light_gc[state_type];
1053 switch (shadow_type)
1055 case GTK_SHADOW_NONE:
1059 gdk_draw_line (window, gc1,
1060 x, y + height - 1, x + width - 1, y + height - 1);
1061 gdk_draw_line (window, gc1,
1062 x + width - 1, y, x + width - 1, y + height - 1);
1064 gdk_draw_line (window, style->bg_gc[state_type],
1065 x + 1, y + height - 2, x + width - 2, y + height - 2);
1066 gdk_draw_line (window, style->bg_gc[state_type],
1067 x + width - 2, y + 1, x + width - 2, y + height - 2);
1069 gdk_draw_line (window, style->black_gc,
1070 x + 1, y + 1, x + width - 2, y + 1);
1071 gdk_draw_line (window, style->black_gc,
1072 x + 1, y + 1, x + 1, y + height - 2);
1074 gdk_draw_line (window, gc2,
1075 x, y, x + width - 1, y);
1076 gdk_draw_line (window, gc2,
1077 x, y, x, y + height - 1);
1080 case GTK_SHADOW_OUT:
1081 gdk_draw_line (window, gc1,
1082 x + 1, y + height - 2, x + width - 2, y + height - 2);
1083 gdk_draw_line (window, gc1,
1084 x + width - 2, y + 1, x + width - 2, y + height - 2);
1086 gdk_draw_line (window, gc2,
1087 x, y, x + width - 1, y);
1088 gdk_draw_line (window, gc2,
1089 x, y, x, y + height - 1);
1091 gdk_draw_line (window, style->bg_gc[state_type],
1092 x + 1, y + 1, x + width - 2, y + 1);
1093 gdk_draw_line (window, style->bg_gc[state_type],
1094 x + 1, y + 1, x + 1, y + height - 2);
1096 gdk_draw_line (window, style->black_gc,
1097 x, y + height - 1, x + width - 1, y + height - 1);
1098 gdk_draw_line (window, style->black_gc,
1099 x + width - 1, y, x + width - 1, y + height - 1);
1102 case GTK_SHADOW_ETCHED_IN:
1103 case GTK_SHADOW_ETCHED_OUT:
1104 thickness_light = 1;
1107 for (i = 0; i < thickness_dark; i++)
1109 gdk_draw_line (window, gc1,
1113 y + height - i - 1);
1114 gdk_draw_line (window, gc1,
1118 y + height - i - 1);
1120 gdk_draw_line (window, gc2,
1125 gdk_draw_line (window, gc2,
1129 y + height - i - 2);
1132 for (i = 0; i < thickness_light; i++)
1134 gdk_draw_line (window, gc1,
1135 x + thickness_dark + i,
1136 y + thickness_dark + i,
1137 x + width - thickness_dark - i - 1,
1138 y + thickness_dark + i);
1139 gdk_draw_line (window, gc1,
1140 x + thickness_dark + i,
1141 y + thickness_dark + i,
1142 x + thickness_dark + i,
1143 y + height - thickness_dark - i - 1);
1145 gdk_draw_line (window, gc2,
1146 x + thickness_dark + i,
1147 y + height - thickness_light - i - 1,
1148 x + width - thickness_light - 1,
1149 y + height - thickness_light - i - 1);
1150 gdk_draw_line (window, gc2,
1151 x + width - thickness_light - i - 1,
1152 y + thickness_dark + i,
1153 x + width - thickness_light - i - 1,
1154 y + height - thickness_light - 1);
1162 gtk_default_draw_polygon (GtkStyle *style,
1164 GtkStateType state_type,
1165 GtkShadowType shadow_type,
1171 #define M_PI 3.14159265358979323846
1174 #define M_PI_4 0.78539816339744830962
1177 static const gdouble pi_over_4 = M_PI_4;
1178 static const gdouble pi_3_over_4 = M_PI_4 * 3;
1189 g_return_if_fail (style != NULL);
1190 g_return_if_fail (window != NULL);
1191 g_return_if_fail (points != NULL);
1193 switch (shadow_type)
1196 gc1 = style->bg_gc[state_type];
1197 gc2 = style->dark_gc[state_type];
1198 gc3 = style->light_gc[state_type];
1199 gc4 = style->black_gc;
1201 case GTK_SHADOW_OUT:
1202 gc1 = style->dark_gc[state_type];
1203 gc2 = style->light_gc[state_type];
1204 gc3 = style->black_gc;
1205 gc4 = style->bg_gc[state_type];
1212 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, npoints);
1216 for (i = 0; i < npoints; i++)
1218 if ((points[i].x == points[i+1].x) &&
1219 (points[i].y == points[i+1].y))
1225 angle = atan2 (points[i+1].y - points[i].y,
1226 points[i+1].x - points[i].x);
1229 if ((angle > -pi_3_over_4) && (angle < pi_over_4))
1231 if (angle > -pi_over_4)
1242 gdk_draw_line (window, gc1,
1243 points[i].x-xadjust, points[i].y-yadjust,
1244 points[i+1].x-xadjust, points[i+1].y-yadjust);
1245 gdk_draw_line (window, gc3,
1246 points[i].x, points[i].y,
1247 points[i+1].x, points[i+1].y);
1251 if ((angle < -pi_3_over_4) || (angle > pi_3_over_4))
1262 gdk_draw_line (window, gc4,
1263 points[i].x+xadjust, points[i].y+yadjust,
1264 points[i+1].x+xadjust, points[i+1].y+yadjust);
1265 gdk_draw_line (window, gc2,
1266 points[i].x, points[i].y,
1267 points[i+1].x, points[i+1].y);
1274 gtk_default_draw_arrow (GtkStyle *style,
1276 GtkStateType state_type,
1277 GtkShadowType shadow_type,
1278 GtkArrowType arrow_type,
1293 g_return_if_fail (style != NULL);
1294 g_return_if_fail (window != NULL);
1296 switch (shadow_type)
1299 gc1 = style->bg_gc[state_type];
1300 gc2 = style->dark_gc[state_type];
1301 gc3 = style->light_gc[state_type];
1302 gc4 = style->black_gc;
1304 case GTK_SHADOW_OUT:
1305 gc1 = style->dark_gc[state_type];
1306 gc2 = style->light_gc[state_type];
1307 gc3 = style->black_gc;
1308 gc4 = style->bg_gc[state_type];
1314 if ((width == -1) && (height == -1))
1315 gdk_window_get_size (window, &width, &height);
1316 else if (width == -1)
1317 gdk_window_get_size (window, &width, NULL);
1318 else if (height == -1)
1319 gdk_window_get_size (window, NULL, &height);
1321 half_width = width / 2;
1322 half_height = height / 2;
1329 points[0].x = x + half_width;
1332 points[1].y = y + height - 1;
1333 points[2].x = x + width - 1;
1334 points[2].y = y + height - 1;
1336 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1339 gdk_draw_line (window, gc1,
1340 x + 1, y + height - 2,
1341 x + width - 2, y + height - 2);
1342 gdk_draw_line (window, gc3,
1343 x + 0, y + height - 1,
1344 x + width - 1, y + height - 1);
1346 gdk_draw_line (window, gc1,
1347 x + width - 2, y + height - 1,
1348 x + half_width, y + 1);
1349 gdk_draw_line (window, gc3,
1350 x + width - 1, y + height - 1,
1353 gdk_draw_line (window, gc4,
1354 x + half_width, y + 1,
1355 x + 1, y + height - 1);
1356 gdk_draw_line (window, gc2,
1360 case GTK_ARROW_DOWN:
1363 points[0].x = x + width - 1;
1367 points[2].x = x + half_width;
1368 points[2].y = y + height - 1;
1370 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1373 gdk_draw_line (window, gc4,
1375 y + 1, x + 1, y + 1);
1376 gdk_draw_line (window, gc2,
1380 gdk_draw_line (window, gc4,
1382 x + half_width, y + height - 2);
1383 gdk_draw_line (window, gc2,
1385 x + half_width, y + height - 1);
1387 gdk_draw_line (window, gc1,
1388 x + half_width, y + height - 2,
1390 gdk_draw_line (window, gc3,
1391 x + half_width, y + height - 1,
1394 case GTK_ARROW_LEFT:
1398 points[0].y = y + half_height;
1399 points[1].x = x + width - 1;
1400 points[1].y = y + height - 1;
1401 points[2].x = x + width - 1;
1404 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1407 gdk_draw_line (window, gc1,
1408 x + 1, y + half_height,
1409 x + width - 1, y + height - 1);
1410 gdk_draw_line (window, gc3,
1412 x + width - 1, y + height - 1);
1414 gdk_draw_line (window, gc1,
1415 x + width - 2, y + height - 1,
1416 x + width - 2, y + 1);
1417 gdk_draw_line (window, gc3,
1418 x + width - 1, y + height - 1,
1421 gdk_draw_line (window, gc4,
1422 x + width - 1, y + 1,
1423 x + 1, y + half_width);
1424 gdk_draw_line (window, gc2,
1428 case GTK_ARROW_RIGHT:
1431 points[0].x = x + width - 1;
1432 points[0].y = y + half_height;
1436 points[2].y = y + height - 1;
1438 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1441 gdk_draw_line (window, gc4,
1442 x + width - 1, y + half_height,
1444 gdk_draw_line (window, gc2,
1445 x + width - 1, y + half_height,
1448 gdk_draw_line (window, gc4,
1450 x + 1, y + height - 2);
1451 gdk_draw_line (window, gc2,
1455 gdk_draw_line (window, gc1,
1456 x + 1, y + height - 2,
1457 x + width - 1, y + half_height);
1458 gdk_draw_line (window, gc3,
1460 x + width - 1, y + half_height);
1467 gtk_default_draw_diamond (GtkStyle *style,
1469 GtkStateType state_type,
1470 GtkShadowType shadow_type,
1479 g_return_if_fail (style != NULL);
1480 g_return_if_fail (window != NULL);
1482 if ((width == -1) && (height == -1))
1483 gdk_window_get_size (window, &width, &height);
1484 else if (width == -1)
1485 gdk_window_get_size (window, &width, NULL);
1486 else if (height == -1)
1487 gdk_window_get_size (window, NULL, &height);
1489 half_width = width / 2;
1490 half_height = height / 2;
1492 switch (shadow_type)
1495 gdk_draw_line (window, style->bg_gc[state_type],
1496 x + 2, y + half_height,
1497 x + half_width, y + height - 2);
1498 gdk_draw_line (window, style->bg_gc[state_type],
1499 x + half_width, y + height - 2,
1500 x + width - 2, y + half_height);
1501 gdk_draw_line (window, style->light_gc[state_type],
1502 x + 1, y + half_height,
1503 x + half_width, y + height - 1);
1504 gdk_draw_line (window, style->light_gc[state_type],
1505 x + half_width, y + height - 1,
1506 x + width - 1, y + half_height);
1507 gdk_draw_line (window, style->light_gc[state_type],
1509 x + half_width, y + height);
1510 gdk_draw_line (window, style->light_gc[state_type],
1511 x + half_width, y + height,
1512 x + width, y + half_height);
1514 gdk_draw_line (window, style->black_gc,
1515 x + 2, y + half_height,
1516 x + half_width, y + 2);
1517 gdk_draw_line (window, style->black_gc,
1518 x + half_width, y + 2,
1519 x + width - 2, y + half_height);
1520 gdk_draw_line (window, style->dark_gc[state_type],
1521 x + 1, y + half_height,
1522 x + half_width, y + 1);
1523 gdk_draw_line (window, style->dark_gc[state_type],
1524 x + half_width, y + 1,
1525 x + width - 1, y + half_height);
1526 gdk_draw_line (window, style->dark_gc[state_type],
1529 gdk_draw_line (window, style->dark_gc[state_type],
1531 x + width, y + half_height);
1533 case GTK_SHADOW_OUT:
1534 gdk_draw_line (window, style->dark_gc[state_type],
1535 x + 2, y + half_height,
1536 x + half_width, y + height - 2);
1537 gdk_draw_line (window, style->dark_gc[state_type],
1538 x + half_width, y + height - 2,
1539 x + width - 2, y + half_height);
1540 gdk_draw_line (window, style->dark_gc[state_type],
1541 x + 1, y + half_height,
1542 x + half_width, y + height - 1);
1543 gdk_draw_line (window, style->dark_gc[state_type],
1544 x + half_width, y + height - 1,
1545 x + width - 1, y + half_height);
1546 gdk_draw_line (window, style->black_gc,
1548 x + half_width, y + height);
1549 gdk_draw_line (window, style->black_gc,
1550 x + half_width, y + height,
1551 x + width, y + half_height);
1553 gdk_draw_line (window, style->bg_gc[state_type],
1554 x + 2, y + half_height,
1555 x + half_width, y + 2);
1556 gdk_draw_line (window, style->bg_gc[state_type],
1557 x + half_width, y + 2,
1558 x + width - 2, y + half_height);
1559 gdk_draw_line (window, style->light_gc[state_type],
1560 x + 1, y + half_height,
1561 x + half_width, y + 1);
1562 gdk_draw_line (window, style->light_gc[state_type],
1563 x + half_width, y + 1,
1564 x + width - 1, y + half_height);
1565 gdk_draw_line (window, style->light_gc[state_type],
1568 gdk_draw_line (window, style->light_gc[state_type],
1570 x + width, y + half_height);
1579 gtk_default_draw_oval (GtkStyle *style,
1581 GtkStateType state_type,
1582 GtkShadowType shadow_type,
1588 g_return_if_fail (style != NULL);
1589 g_return_if_fail (window != NULL);
1593 gtk_default_draw_string (GtkStyle *style,
1595 GtkStateType state_type,
1598 const gchar *string)
1600 g_return_if_fail (style != NULL);
1601 g_return_if_fail (window != NULL);
1603 if (state_type == GTK_STATE_INSENSITIVE)
1604 gdk_draw_string (window, style->font, style->white_gc, x + 1, y + 1, string);
1605 gdk_draw_string (window, style->font, style->fg_gc[state_type], x, y, string);
1610 gtk_style_shade (GdkColor *a,
1618 red = (gdouble) a->red / 65535.0;
1619 green = (gdouble) a->green / 65535.0;
1620 blue = (gdouble) a->blue / 65535.0;
1622 rgb_to_hls (&red, &green, &blue);
1627 else if (green < 0.0)
1633 else if (blue < 0.0)
1636 hls_to_rgb (&red, &green, &blue);
1638 b->red = red * 65535.0;
1639 b->green = green * 65535.0;
1640 b->blue = blue * 65535.0;
1644 rgb_to_hls (gdouble *r,
1685 l = (max + min) / 2;
1692 s = (max - min) / (max + min);
1694 s = (max - min) / (2 - max - min);
1698 h = (green - blue) / delta;
1699 else if (green == max)
1700 h = 2 + (blue - red) / delta;
1701 else if (blue == max)
1702 h = 4 + (red - green) / delta;
1715 hls_to_rgb (gdouble *h,
1728 if (lightness <= 0.5)
1729 m2 = lightness * (1 + saturation);
1731 m2 = lightness + saturation - lightness * saturation;
1732 m1 = 2 * lightness - m2;
1734 if (saturation == 0)
1749 r = m1 + (m2 - m1) * hue / 60;
1753 r = m1 + (m2 - m1) * (240 - hue) / 60;
1764 g = m1 + (m2 - m1) * hue / 60;
1768 g = m1 + (m2 - m1) * (240 - hue) / 60;
1779 b = m1 + (m2 - m1) * hue / 60;
1783 b = m1 + (m2 - m1) * (240 - hue) / 60;