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.
23 #define LIGHTNESS_MULT 1.3
24 #define DARKNESS_MULT 0.7
27 typedef struct _GtkStyleKey GtkStyleKey;
36 GdkPixmap *bg_pixmap[5];
41 GdkColormap *colormap;
46 static void gtk_style_init (GtkStyle *style);
47 static void gtk_styles_init (void);
48 static void gtk_style_remove (GtkStyle *style);
49 static GtkStyle* gtk_style_find (GtkStyle *style,
52 static GtkStyle* gtk_style_new_from_key (GtkStyleKey *key);
53 static GtkStyleKey* gtk_style_key_dup (GtkStyleKey *key);
54 static void gtk_style_destroy (GtkStyle *style);
55 static void gtk_style_key_destroy (GtkStyleKey *key);
56 static guint gtk_style_key_hash (GtkStyleKey *key);
57 static guint gtk_style_value_hash (GtkStyle *style);
58 static gint gtk_style_key_compare (GtkStyleKey *a,
61 static void gtk_default_draw_hline (GtkStyle *style,
63 GtkStateType state_type,
67 static void gtk_default_draw_vline (GtkStyle *style,
69 GtkStateType state_type,
73 static void gtk_default_draw_shadow (GtkStyle *style,
75 GtkStateType state_type,
76 GtkShadowType shadow_type,
81 static void gtk_default_draw_polygon (GtkStyle *style,
83 GtkStateType state_type,
84 GtkShadowType shadow_type,
88 static void gtk_default_draw_arrow (GtkStyle *style,
90 GtkStateType state_type,
91 GtkShadowType shadow_type,
92 GtkArrowType arrow_type,
98 static void gtk_default_draw_diamond (GtkStyle *style,
100 GtkStateType state_type,
101 GtkShadowType shadow_type,
106 static void gtk_default_draw_oval (GtkStyle *style,
108 GtkStateType state_type,
109 GtkShadowType shadow_type,
114 static void gtk_default_draw_string (GtkStyle *style,
116 GtkStateType state_type,
119 const gchar *string);
121 static void gtk_style_shade (GdkColor *a, GdkColor *b, gdouble k);
122 static void rgb_to_hls (gdouble *r, gdouble *g, gdouble *b);
123 static void hls_to_rgb (gdouble *h, gdouble *l, gdouble *s);
126 static GtkStyleClass default_class =
130 gtk_default_draw_hline,
131 gtk_default_draw_vline,
132 gtk_default_draw_shadow,
133 gtk_default_draw_polygon,
134 gtk_default_draw_arrow,
135 gtk_default_draw_diamond,
136 gtk_default_draw_oval,
137 gtk_default_draw_string,
140 static GdkColor gtk_default_normal_fg = { 0, 0, 0, 0 };
141 static GdkColor gtk_default_active_fg = { 0, 0, 0, 0 };
142 static GdkColor gtk_default_prelight_fg = { 0, 0, 0, 0 };
143 static GdkColor gtk_default_selected_fg = { 0, 0xffff, 0xffff, 0xffff };
144 static GdkColor gtk_default_insensitive_fg = { 0, 0x7530, 0x7530, 0x7530 };
146 static GdkColor gtk_default_normal_bg = { 0, 0xd6d6, 0xd6d6, 0xd6d6 };
147 static GdkColor gtk_default_active_bg = { 0, 0xc350, 0xc350, 0xc350 };
148 static GdkColor gtk_default_prelight_bg = { 0, 0xea60, 0xea60, 0xea60 };
149 static GdkColor gtk_default_selected_bg = { 0, 0, 0, 0x9c40 };
150 static GdkColor gtk_default_insensitive_bg = { 0, 0xd6d6, 0xd6d6, 0xd6d6 };
152 static GdkFont *default_font = NULL;
154 static gint initialize = TRUE;
155 static GCache *style_cache = NULL;
156 static GSList *unattached_styles = NULL;
158 static GMemChunk *key_mem_chunk = NULL;
167 style = g_new (GtkStyle, 1);
170 default_font = gdk_font_load ("-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*");
172 style->font = default_font;
173 gdk_font_ref (style->font);
175 style->ref_count = 0;
176 style->attach_count = 0;
177 style->colormap = NULL;
179 style->klass = &default_class;
181 style->black.red = 0;
182 style->black.green = 0;
183 style->black.blue = 0;
185 style->white.red = 65535;
186 style->white.green = 65535;
187 style->white.blue = 65535;
189 style->black_gc = NULL;
190 style->white_gc = NULL;
192 style->fg[GTK_STATE_NORMAL] = gtk_default_normal_fg;
193 style->fg[GTK_STATE_ACTIVE] = gtk_default_active_fg;
194 style->fg[GTK_STATE_PRELIGHT] = gtk_default_prelight_fg;
195 style->fg[GTK_STATE_SELECTED] = gtk_default_selected_fg;
196 style->fg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
198 style->bg[GTK_STATE_NORMAL] = gtk_default_normal_bg;
199 style->bg[GTK_STATE_ACTIVE] = gtk_default_active_bg;
200 style->bg[GTK_STATE_PRELIGHT] = gtk_default_prelight_bg;
201 style->bg[GTK_STATE_SELECTED] = gtk_default_selected_bg;
202 style->bg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_bg;
204 for (i = 0; i < 5; i++)
206 style->text[i] = style->fg[i];
207 style->base[i] = style->white;
210 for (i = 0; i < 5; i++)
211 style->bg_pixmap[i] = NULL;
213 for (i = 0; i < 5; i++)
215 style->fg_gc[i] = NULL;
216 style->bg_gc[i] = NULL;
217 style->light_gc[i] = NULL;
218 style->dark_gc[i] = NULL;
219 style->mid_gc[i] = NULL;
220 style->text_gc[i] = NULL;
221 style->base_gc[i] = NULL;
224 unattached_styles = g_slist_prepend (unattached_styles, style);
230 gtk_style_attach (GtkStyle *style,
234 GdkColormap *colormap;
237 g_return_val_if_fail (style != NULL, NULL);
238 g_return_val_if_fail (window != NULL, NULL);
240 colormap = gdk_window_get_colormap (window);
241 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
243 new_style = gtk_style_find (style, colormap, depth);
245 if (new_style && (new_style != style))
247 gtk_style_unref (style);
249 gtk_style_ref (style);
252 if (style->attach_count == 0)
253 unattached_styles = g_slist_remove (unattached_styles, style);
255 style->attach_count += 1;
261 gtk_style_detach (GtkStyle *style)
265 g_return_if_fail (style != NULL);
267 style->attach_count -= 1;
268 if (style->attach_count == 0)
270 unattached_styles = g_slist_prepend (unattached_styles, style);
272 gtk_gc_release (style->black_gc);
273 gtk_gc_release (style->white_gc);
275 style->black_gc = NULL;
276 style->white_gc = NULL;
278 for (i = 0; i < 5; i++)
280 gtk_gc_release (style->fg_gc[i]);
281 gtk_gc_release (style->bg_gc[i]);
282 gtk_gc_release (style->light_gc[i]);
283 gtk_gc_release (style->dark_gc[i]);
284 gtk_gc_release (style->mid_gc[i]);
285 gtk_gc_release (style->text_gc[i]);
286 gtk_gc_release (style->base_gc[i]);
288 style->fg_gc[i] = NULL;
289 style->bg_gc[i] = NULL;
290 style->light_gc[i] = NULL;
291 style->dark_gc[i] = NULL;
292 style->mid_gc[i] = NULL;
293 style->text_gc[i] = NULL;
294 style->base_gc[i] = NULL;
298 style->colormap = NULL;
301 gtk_style_remove (style);
305 gtk_style_ref (GtkStyle *style)
307 g_return_val_if_fail (style != NULL, NULL);
309 style->ref_count += 1;
314 gtk_style_unref (GtkStyle *style)
316 g_return_if_fail (style != NULL);
318 style->ref_count -= 1;
319 if (style->ref_count == 0)
320 gtk_style_destroy (style);
324 gtk_style_set_background (GtkStyle *style,
326 GtkStateType state_type)
329 gint parent_relative;
331 g_return_if_fail (style != NULL);
332 g_return_if_fail (window != NULL);
334 if (style->bg_pixmap[state_type])
336 if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
339 parent_relative = TRUE;
343 pixmap = style->bg_pixmap[state_type];
344 parent_relative = FALSE;
347 gdk_window_set_back_pixmap (window, pixmap, parent_relative);
350 gdk_window_set_background (window, &style->bg[state_type]);
355 gtk_draw_hline (GtkStyle *style,
357 GtkStateType state_type,
362 g_return_if_fail (style != NULL);
363 g_return_if_fail (style->klass != NULL);
364 g_return_if_fail (style->klass->draw_hline != NULL);
366 (*style->klass->draw_hline) (style, window, state_type, x1, x2, y);
371 gtk_draw_vline (GtkStyle *style,
373 GtkStateType state_type,
378 g_return_if_fail (style != NULL);
379 g_return_if_fail (style->klass != NULL);
380 g_return_if_fail (style->klass->draw_vline != NULL);
382 (*style->klass->draw_vline) (style, window, state_type, y1, y2, x);
387 gtk_draw_shadow (GtkStyle *style,
389 GtkStateType state_type,
390 GtkShadowType shadow_type,
396 g_return_if_fail (style != NULL);
397 g_return_if_fail (style->klass != NULL);
398 g_return_if_fail (style->klass->draw_shadow != NULL);
400 (*style->klass->draw_shadow) (style, window, state_type, shadow_type, x, y, width, height);
404 gtk_draw_polygon (GtkStyle *style,
406 GtkStateType state_type,
407 GtkShadowType shadow_type,
412 g_return_if_fail (style != NULL);
413 g_return_if_fail (style->klass != NULL);
414 g_return_if_fail (style->klass->draw_shadow != NULL);
416 (*style->klass->draw_polygon) (style, window, state_type, shadow_type, points, npoints, fill);
420 gtk_draw_arrow (GtkStyle *style,
422 GtkStateType state_type,
423 GtkShadowType shadow_type,
424 GtkArrowType arrow_type,
431 g_return_if_fail (style != NULL);
432 g_return_if_fail (style->klass != NULL);
433 g_return_if_fail (style->klass->draw_arrow != NULL);
435 (*style->klass->draw_arrow) (style, window, state_type, shadow_type, arrow_type, fill, x, y, width, height);
440 gtk_draw_diamond (GtkStyle *style,
442 GtkStateType state_type,
443 GtkShadowType shadow_type,
449 g_return_if_fail (style != NULL);
450 g_return_if_fail (style->klass != NULL);
451 g_return_if_fail (style->klass->draw_diamond != NULL);
453 (*style->klass->draw_diamond) (style, window, state_type, shadow_type, x, y, width, height);
458 gtk_draw_oval (GtkStyle *style,
460 GtkStateType state_type,
461 GtkShadowType shadow_type,
467 g_return_if_fail (style != NULL);
468 g_return_if_fail (style->klass != NULL);
469 g_return_if_fail (style->klass->draw_oval != NULL);
471 (*style->klass->draw_oval) (style, window, state_type, shadow_type, x, y, width, height);
475 gtk_draw_string (GtkStyle *style,
477 GtkStateType state_type,
482 g_return_if_fail (style != NULL);
483 g_return_if_fail (style->klass != NULL);
484 g_return_if_fail (style->klass->draw_oval != NULL);
486 (*style->klass->draw_string) (style, window, state_type, x, y, string);
491 gtk_style_init (GtkStyle *style)
493 GdkGCValues gc_values;
494 GdkGCValuesMask gc_values_mask;
495 GdkColormap *colormap;
498 g_return_if_fail (style != NULL);
500 if (style->attach_count == 0)
502 for (i = 0; i < 5; i++)
504 gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
505 gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);
507 style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
508 style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
509 style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
512 colormap = style->colormap;
514 gdk_color_black (colormap, &style->black);
515 gdk_color_white (colormap, &style->white);
517 gc_values_mask = GDK_GC_FOREGROUND | GDK_GC_FONT;
518 if (style->font->type == GDK_FONT_FONT)
520 gc_values.font = style->font;
522 else if (style->font->type == GDK_FONT_FONTSET)
524 gc_values.font = default_font;
527 gc_values.foreground = style->black;
528 style->black_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
530 gc_values.foreground = style->white;
531 style->white_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
533 for (i = 0; i < 5; i++)
535 if (!gdk_color_alloc (colormap, &style->fg[i]))
536 g_warning ("unable to allocate color: ( %d %d %d )",
537 style->fg[i].red, style->fg[i].green, style->fg[i].blue);
538 if (!gdk_color_alloc (colormap, &style->bg[i]))
539 g_warning ("unable to allocate color: ( %d %d %d )",
540 style->bg[i].red, style->bg[i].green, style->bg[i].blue);
541 if (!gdk_color_alloc (colormap, &style->light[i]))
542 g_warning ("unable to allocate color: ( %d %d %d )",
543 style->light[i].red, style->light[i].green, style->light[i].blue);
544 if (!gdk_color_alloc (colormap, &style->dark[i]))
545 g_warning ("unable to allocate color: ( %d %d %d )",
546 style->dark[i].red, style->dark[i].green, style->dark[i].blue);
547 if (!gdk_color_alloc (colormap, &style->mid[i]))
548 g_warning ("unable to allocate color: ( %d %d %d )",
549 style->mid[i].red, style->mid[i].green, style->mid[i].blue);
550 if (!gdk_color_alloc (colormap, &style->text[i]))
551 g_warning ("unable to allocate color: ( %d %d %d )",
552 style->text[i].red, style->text[i].green, style->text[i].blue);
553 if (!gdk_color_alloc (colormap, &style->base[i]))
554 g_warning ("unable to allocate color: ( %d %d %d )",
555 style->base[i].red, style->base[i].green, style->base[i].blue);
557 gc_values.foreground = style->fg[i];
558 style->fg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
560 gc_values.foreground = style->bg[i];
561 style->bg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
563 gc_values.foreground = style->light[i];
564 style->light_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
566 gc_values.foreground = style->dark[i];
567 style->dark_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
569 gc_values.foreground = style->mid[i];
570 style->mid_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
572 gc_values.foreground = style->text[i];
573 style->text_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
575 gc_values.foreground = style->base[i];
576 style->base_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
588 style_cache = g_cache_new ((GCacheNewFunc) gtk_style_new_from_key,
589 (GCacheDestroyFunc) gtk_style_destroy,
590 (GCacheDupFunc) gtk_style_key_dup,
591 (GCacheDestroyFunc) gtk_style_key_destroy,
592 (GHashFunc) gtk_style_key_hash,
593 (GHashFunc) gtk_style_value_hash,
594 (GCompareFunc) gtk_style_key_compare);
599 gtk_style_remove (GtkStyle *style)
604 g_cache_remove (style_cache, style);
608 gtk_style_find (GtkStyle *style,
618 for (i = 0; i < 5; i++)
620 key.fg[i] = style->fg[i];
621 key.bg[i] = style->bg[i];
622 key.text[i] = style->text[i];
623 key.base[i] = style->base[i];
624 key.bg_pixmap[i] = style->bg_pixmap[i];
627 key.font = style->font;
628 key.klass = style->klass;
632 style = g_cache_insert (style_cache, &key);
638 gtk_style_new_from_key (GtkStyleKey *key)
645 list = unattached_styles;
652 if ((style->depth != -1) && (style->depth != key->depth))
657 if (style->colormap && (style->colormap != key->colormap))
662 if (style->klass != key->klass)
667 if (!gdk_font_equal (style->font, key->font))
673 for (i = 0; style && (i < 5); i++)
675 if (style->bg_pixmap[i] != key->bg_pixmap[i])
681 if ((style->fg[i].red != key->fg[i].red) ||
682 (style->fg[i].green != key->fg[i].green) ||
683 (style->fg[i].blue != key->fg[i].blue))
689 if ((style->bg[i].red != key->bg[i].red) ||
690 (style->bg[i].green != key->bg[i].green) ||
691 (style->bg[i].blue != key->bg[i].blue))
697 if ((style->text[i].red != key->text[i].red) ||
698 (style->text[i].green != key->text[i].green) ||
699 (style->text[i].blue != key->text[i].blue))
705 if ((style->base[i].red != key->base[i].red) ||
706 (style->base[i].green != key->base[i].green) ||
707 (style->base[i].blue != key->base[i].blue))
720 style = g_new (GtkStyle, 1);
722 style->ref_count = 0;
723 style->attach_count = 0;
725 style->font = key->font;
726 gdk_font_ref (style->font);
728 style->depth = key->depth;
729 style->colormap = key->colormap;
730 style->klass = key->klass;
732 style->black.red = 0;
733 style->black.green = 0;
734 style->black.blue = 0;
736 style->white.red = 65535;
737 style->white.green = 65535;
738 style->white.blue = 65535;
740 style->black_gc = NULL;
741 style->white_gc = NULL;
743 for (i = 0; i < 5; i++)
745 style->fg[i] = key->fg[i];
746 style->bg[i] = key->bg[i];
747 style->text[i] = key->text[i];
748 style->base[i] = key->base[i];
751 for (i = 0; i < 5; i++)
752 style->bg_pixmap[i] = key->bg_pixmap[i];
754 for (i = 0; i < 5; i++)
756 style->fg_gc[i] = NULL;
757 style->bg_gc[i] = NULL;
758 style->light_gc[i] = NULL;
759 style->dark_gc[i] = NULL;
760 style->mid_gc[i] = NULL;
761 style->text_gc[i] = NULL;
762 style->base_gc[i] = NULL;
766 if (style->depth == -1)
767 style->depth = key->depth;
768 if (!style->colormap)
769 style->colormap = key->colormap;
771 gtk_style_init (style);
777 gtk_style_key_dup (GtkStyleKey *key)
779 GtkStyleKey *new_key;
782 key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkStyleKey),
783 1024, G_ALLOC_AND_FREE);
785 new_key = g_chunk_new (GtkStyleKey, key_mem_chunk);
793 gtk_style_destroy (GtkStyle *style)
797 if (style->ref_count != 0)
800 if (style->attach_count > 0)
802 gtk_gc_release (style->black_gc);
803 gtk_gc_release (style->white_gc);
805 for (i = 0; i < 5; i++)
807 gtk_gc_release (style->fg_gc[i]);
808 gtk_gc_release (style->bg_gc[i]);
809 gtk_gc_release (style->light_gc[i]);
810 gtk_gc_release (style->dark_gc[i]);
811 gtk_gc_release (style->mid_gc[i]);
812 gtk_gc_release (style->text_gc[i]);
813 gtk_gc_release (style->base_gc[i]);
817 unattached_styles = g_slist_remove (unattached_styles, style);
819 if (style->font->type == GDK_FONT_FONT)
820 gdk_font_free (style->font);
821 else if (style->font->type == GDK_FONT_FONTSET)
822 gdk_fontset_free (style->font);
824 g_error("undefined font type\n");
830 gtk_style_key_destroy (GtkStyleKey *key)
832 g_mem_chunk_free (key_mem_chunk, key);
836 gtk_style_key_hash (GtkStyleKey *key)
843 for (i = 0; i < 5; i++)
845 hash_val += key->fg[i].red + key->fg[i].green + key->fg[i].blue;
846 hash_val += key->bg[i].red + key->bg[i].green + key->bg[i].blue;
847 hash_val += key->text[i].red + key->text[i].green + key->text[i].blue;
848 hash_val += key->base[i].red + key->base[i].green + key->base[i].blue;
851 hash_val += (guint) gdk_font_id (key->font);
852 hash_val += (guint) key->depth;
853 hash_val += (gulong) key->colormap;
854 hash_val += (gulong) key->klass;
860 gtk_style_value_hash (GtkStyle *style)
867 for (i = 0; i < 5; i++)
869 hash_val += style->fg[i].red + style->fg[i].green + style->fg[i].blue;
870 hash_val += style->bg[i].red + style->bg[i].green + style->bg[i].blue;
871 hash_val += style->text[i].red + style->text[i].green + style->text[i].blue;
872 hash_val += style->base[i].red + style->base[i].green + style->base[i].blue;
875 hash_val += (guint) gdk_font_id (style->font);
876 hash_val += (gulong) style->klass;
882 gtk_style_key_compare (GtkStyleKey *a,
887 if (a->depth != b->depth)
889 if (a->colormap != b->colormap)
891 if (a->klass != b->klass)
893 if (!gdk_font_equal (a->font, b->font))
896 for (i = 0; i < 5; i++)
898 if (a->bg_pixmap[i] != b->bg_pixmap[i])
901 if ((a->fg[i].red != b->fg[i].red) ||
902 (a->fg[i].green != b->fg[i].green) ||
903 (a->fg[i].blue != b->fg[i].blue))
905 if ((a->bg[i].red != b->bg[i].red) ||
906 (a->bg[i].green != b->bg[i].green) ||
907 (a->bg[i].blue != b->bg[i].blue))
909 if ((a->text[i].red != b->text[i].red) ||
910 (a->text[i].green != b->text[i].green) ||
911 (a->text[i].blue != b->text[i].blue))
913 if ((a->base[i].red != b->base[i].red) ||
914 (a->base[i].green != b->base[i].green) ||
915 (a->base[i].blue != b->base[i].blue))
924 gtk_default_draw_hline (GtkStyle *style,
926 GtkStateType state_type,
931 gint thickness_light;
935 g_return_if_fail (style != NULL);
936 g_return_if_fail (window != NULL);
938 thickness_light = style->klass->ythickness / 2;
939 thickness_dark = style->klass->ythickness - thickness_light;
941 for (i = 0; i < thickness_dark; i++)
943 gdk_draw_line (window, style->light_gc[state_type], x2 - i - 1, y + i, x2, y + i);
944 gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x2 - i - 1, y + i);
948 for (i = 0; i < thickness_light; i++)
950 gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x1 + thickness_light - i - 1, y + i);
951 gdk_draw_line (window, style->light_gc[state_type], x1 + thickness_light - i - 1, y + i, x2, y + i);
957 gtk_default_draw_vline (GtkStyle *style,
959 GtkStateType state_type,
964 gint thickness_light;
968 g_return_if_fail (style != NULL);
969 g_return_if_fail (window != NULL);
971 thickness_light = style->klass->xthickness / 2;
972 thickness_dark = style->klass->xthickness - thickness_light;
974 for (i = 0; i < thickness_dark; i++)
976 gdk_draw_line (window, style->light_gc[state_type], x + i, y2 - i - 1, x + i, y2);
977 gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y2 - i - 1);
981 for (i = 0; i < thickness_light; i++)
983 gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y1 + thickness_light - i);
984 gdk_draw_line (window, style->light_gc[state_type], x + i, y1 + thickness_light - i, x + i, y2);
990 gtk_default_draw_shadow (GtkStyle *style,
992 GtkStateType state_type,
993 GtkShadowType shadow_type,
1001 gint thickness_light;
1002 gint thickness_dark;
1005 g_return_if_fail (style != NULL);
1006 g_return_if_fail (window != NULL);
1008 if ((width == -1) && (height == -1))
1009 gdk_window_get_size (window, &width, &height);
1010 else if (width == -1)
1011 gdk_window_get_size (window, &width, NULL);
1012 else if (height == -1)
1013 gdk_window_get_size (window, NULL, &height);
1015 switch (shadow_type)
1017 case GTK_SHADOW_NONE:
1022 case GTK_SHADOW_ETCHED_IN:
1023 gc1 = style->light_gc[state_type];
1024 gc2 = style->dark_gc[state_type];
1026 case GTK_SHADOW_OUT:
1027 case GTK_SHADOW_ETCHED_OUT:
1028 gc1 = style->dark_gc[state_type];
1029 gc2 = style->light_gc[state_type];
1033 switch (shadow_type)
1035 case GTK_SHADOW_NONE:
1039 gdk_draw_line (window, gc1,
1040 x, y + height - 1, x + width - 1, y + height - 1);
1041 gdk_draw_line (window, gc1,
1042 x + width - 1, y, x + width - 1, y + height - 1);
1044 gdk_draw_line (window, style->bg_gc[state_type],
1045 x + 1, y + height - 2, x + width - 2, y + height - 2);
1046 gdk_draw_line (window, style->bg_gc[state_type],
1047 x + width - 2, y + 1, x + width - 2, y + height - 2);
1049 gdk_draw_line (window, style->black_gc,
1050 x + 1, y + 1, x + width - 2, y + 1);
1051 gdk_draw_line (window, style->black_gc,
1052 x + 1, y + 1, x + 1, y + height - 2);
1054 gdk_draw_line (window, gc2,
1055 x, y, x + width - 1, y);
1056 gdk_draw_line (window, gc2,
1057 x, y, x, y + height - 1);
1060 case GTK_SHADOW_OUT:
1061 gdk_draw_line (window, gc1,
1062 x + 1, y + height - 2, x + width - 2, y + height - 2);
1063 gdk_draw_line (window, gc1,
1064 x + width - 2, y + 1, x + width - 2, y + height - 2);
1066 gdk_draw_line (window, gc2,
1067 x, y, x + width - 1, y);
1068 gdk_draw_line (window, gc2,
1069 x, y, x, y + height - 1);
1071 gdk_draw_line (window, style->bg_gc[state_type],
1072 x + 1, y + 1, x + width - 2, y + 1);
1073 gdk_draw_line (window, style->bg_gc[state_type],
1074 x + 1, y + 1, x + 1, y + height - 2);
1076 gdk_draw_line (window, style->black_gc,
1077 x, y + height - 1, x + width - 1, y + height - 1);
1078 gdk_draw_line (window, style->black_gc,
1079 x + width - 1, y, x + width - 1, y + height - 1);
1082 case GTK_SHADOW_ETCHED_IN:
1083 case GTK_SHADOW_ETCHED_OUT:
1084 thickness_light = 1;
1087 for (i = 0; i < thickness_dark; i++)
1089 gdk_draw_line (window, gc1,
1093 y + height - i - 1);
1094 gdk_draw_line (window, gc1,
1098 y + height - i - 1);
1100 gdk_draw_line (window, gc2,
1105 gdk_draw_line (window, gc2,
1109 y + height - i - 2);
1112 for (i = 0; i < thickness_light; i++)
1114 gdk_draw_line (window, gc1,
1115 x + thickness_dark + i,
1116 y + thickness_dark + i,
1117 x + width - thickness_dark - i - 1,
1118 y + thickness_dark + i);
1119 gdk_draw_line (window, gc1,
1120 x + thickness_dark + i,
1121 y + thickness_dark + i,
1122 x + thickness_dark + i,
1123 y + height - thickness_dark - i - 1);
1125 gdk_draw_line (window, gc2,
1126 x + thickness_dark + i,
1127 y + height - thickness_light - i - 1,
1128 x + width - thickness_light - 1,
1129 y + height - thickness_light - i - 1);
1130 gdk_draw_line (window, gc2,
1131 x + width - thickness_light - i - 1,
1132 y + thickness_dark + i,
1133 x + width - thickness_light - i - 1,
1134 y + height - thickness_light - 1);
1142 gtk_default_draw_polygon (GtkStyle *style,
1144 GtkStateType state_type,
1145 GtkShadowType shadow_type,
1151 #define M_PI 3.14159265358979323846
1154 #define M_PI_4 0.78539816339744830962
1157 static const gdouble pi_over_4 = M_PI_4;
1158 static const gdouble pi_3_over_4 = M_PI_4 * 3;
1169 g_return_if_fail (style != NULL);
1170 g_return_if_fail (window != NULL);
1171 g_return_if_fail (points != NULL);
1173 switch (shadow_type)
1176 gc1 = style->bg_gc[state_type];
1177 gc2 = style->dark_gc[state_type];
1178 gc3 = style->light_gc[state_type];
1179 gc4 = style->black_gc;
1181 case GTK_SHADOW_OUT:
1182 gc1 = style->dark_gc[state_type];
1183 gc2 = style->light_gc[state_type];
1184 gc3 = style->black_gc;
1185 gc4 = style->bg_gc[state_type];
1192 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, npoints);
1195 for (i = 0; i < npoints; i++)
1197 if ((points[i].x == points[i+1].x) &&
1198 (points[i].y == points[i+1].y))
1204 angle = atan2 (points[i+1].y - points[i].y,
1205 points[i+1].x - points[i].x);
1208 if ((angle > -pi_3_over_4) && (angle < pi_over_4))
1212 while (angle > M_PI)
1215 if ((angle > pi_3_over_4) || (angle < pi_over_4))
1226 gdk_draw_line (window, gc1,
1227 points[i].x-xadjust, points[i].y-yadjust,
1228 points[i+1].x-xadjust, points[i+1].y-yadjust);
1229 gdk_draw_line (window, gc3,
1230 points[i].x, points[i].y,
1231 points[i+1].x, points[i+1].y);
1235 for (i = 0; i < npoints; i++)
1237 if ((points[i].x == points[i+1].x) &&
1238 (points[i].y == points[i+1].y))
1244 angle = atan2 (points[i+1].y - points[i].y,
1245 points[i+1].x - points[i].x);
1248 if ((angle <= -pi_3_over_4) || (angle >= pi_over_4))
1252 while (angle > M_PI)
1255 if ((angle > pi_3_over_4) || (angle < pi_over_4))
1266 gdk_draw_line (window, gc4,
1267 points[i].x+xadjust, points[i].y+yadjust,
1268 points[i+1].x+xadjust, points[i+1].y+yadjust);
1269 gdk_draw_line (window, gc2,
1270 points[i].x, points[i].y,
1271 points[i+1].x, points[i+1].y);
1278 gtk_default_draw_arrow (GtkStyle *style,
1280 GtkStateType state_type,
1281 GtkShadowType shadow_type,
1282 GtkArrowType arrow_type,
1297 g_return_if_fail (style != NULL);
1298 g_return_if_fail (window != NULL);
1300 switch (shadow_type)
1303 gc1 = style->bg_gc[state_type];
1304 gc2 = style->dark_gc[state_type];
1305 gc3 = style->light_gc[state_type];
1306 gc4 = style->black_gc;
1308 case GTK_SHADOW_OUT:
1309 gc1 = style->dark_gc[state_type];
1310 gc2 = style->light_gc[state_type];
1311 gc3 = style->black_gc;
1312 gc4 = style->bg_gc[state_type];
1318 if ((width == -1) && (height == -1))
1319 gdk_window_get_size (window, &width, &height);
1320 else if (width == -1)
1321 gdk_window_get_size (window, &width, NULL);
1322 else if (height == -1)
1323 gdk_window_get_size (window, NULL, &height);
1325 half_width = width / 2;
1326 half_height = height / 2;
1333 points[0].x = x + half_width;
1336 points[1].y = y + height - 1;
1337 points[2].x = x + width - 1;
1338 points[2].y = y + height - 1;
1340 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1343 gdk_draw_line (window, gc1,
1344 x + 1, y + height - 2,
1345 x + width - 2, y + height - 2);
1346 gdk_draw_line (window, gc3,
1347 x + 0, y + height - 1,
1348 x + width - 1, y + height - 1);
1350 gdk_draw_line (window, gc1,
1351 x + width - 2, y + height - 1,
1352 x + half_width, y + 1);
1353 gdk_draw_line (window, gc3,
1354 x + width - 1, y + height - 1,
1357 gdk_draw_line (window, gc4,
1358 x + half_width, y + 1,
1359 x + 1, y + height - 1);
1360 gdk_draw_line (window, gc2,
1364 case GTK_ARROW_DOWN:
1367 points[0].x = x + width - 1;
1371 points[2].x = x + half_width;
1372 points[2].y = y + height - 1;
1374 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1377 gdk_draw_line (window, gc4,
1379 y + 1, x + 1, y + 1);
1380 gdk_draw_line (window, gc2,
1384 gdk_draw_line (window, gc4,
1386 x + half_width, y + height - 2);
1387 gdk_draw_line (window, gc2,
1389 x + half_width, y + height - 1);
1391 gdk_draw_line (window, gc1,
1392 x + half_width, y + height - 2,
1394 gdk_draw_line (window, gc3,
1395 x + half_width, y + height - 1,
1398 case GTK_ARROW_LEFT:
1402 points[0].y = y + half_height;
1403 points[1].x = x + width - 1;
1404 points[1].y = y + height - 1;
1405 points[2].x = x + width - 1;
1408 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1411 gdk_draw_line (window, gc1,
1412 x + 1, y + half_height,
1413 x + width - 1, y + height - 1);
1414 gdk_draw_line (window, gc3,
1416 x + width - 1, y + height - 1);
1418 gdk_draw_line (window, gc1,
1419 x + width - 2, y + height - 1,
1420 x + width - 2, y + 1);
1421 gdk_draw_line (window, gc3,
1422 x + width - 1, y + height - 1,
1425 gdk_draw_line (window, gc4,
1426 x + width - 1, y + 1,
1427 x + 1, y + half_width);
1428 gdk_draw_line (window, gc2,
1432 case GTK_ARROW_RIGHT:
1435 points[0].x = x + width - 1;
1436 points[0].y = y + half_height;
1440 points[2].y = y + height - 1;
1442 gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, 3);
1445 gdk_draw_line (window, gc4,
1446 x + width - 1, y + half_height,
1448 gdk_draw_line (window, gc2,
1449 x + width - 1, y + half_height,
1452 gdk_draw_line (window, gc4,
1454 x + 1, y + height - 2);
1455 gdk_draw_line (window, gc2,
1459 gdk_draw_line (window, gc1,
1460 x + 1, y + height - 2,
1461 x + width - 1, y + half_height);
1462 gdk_draw_line (window, gc3,
1464 x + width - 1, y + half_height);
1471 gtk_default_draw_diamond (GtkStyle *style,
1473 GtkStateType state_type,
1474 GtkShadowType shadow_type,
1483 g_return_if_fail (style != NULL);
1484 g_return_if_fail (window != NULL);
1486 if ((width == -1) && (height == -1))
1487 gdk_window_get_size (window, &width, &height);
1488 else if (width == -1)
1489 gdk_window_get_size (window, &width, NULL);
1490 else if (height == -1)
1491 gdk_window_get_size (window, NULL, &height);
1493 half_width = width / 2;
1494 half_height = height / 2;
1496 switch (shadow_type)
1499 gdk_draw_line (window, style->bg_gc[state_type],
1500 x + 2, y + half_height,
1501 x + half_width, y + height - 2);
1502 gdk_draw_line (window, style->bg_gc[state_type],
1503 x + half_width, y + height - 2,
1504 x + width - 2, y + half_height);
1505 gdk_draw_line (window, style->light_gc[state_type],
1506 x + 1, y + half_height,
1507 x + half_width, y + height - 1);
1508 gdk_draw_line (window, style->light_gc[state_type],
1509 x + half_width, y + height - 1,
1510 x + width - 1, y + half_height);
1511 gdk_draw_line (window, style->light_gc[state_type],
1513 x + half_width, y + height);
1514 gdk_draw_line (window, style->light_gc[state_type],
1515 x + half_width, y + height,
1516 x + width, y + half_height);
1518 gdk_draw_line (window, style->black_gc,
1519 x + 2, y + half_height,
1520 x + half_width, y + 2);
1521 gdk_draw_line (window, style->black_gc,
1522 x + half_width, y + 2,
1523 x + width - 2, y + half_height);
1524 gdk_draw_line (window, style->dark_gc[state_type],
1525 x + 1, y + half_height,
1526 x + half_width, y + 1);
1527 gdk_draw_line (window, style->dark_gc[state_type],
1528 x + half_width, y + 1,
1529 x + width - 1, y + half_height);
1530 gdk_draw_line (window, style->dark_gc[state_type],
1533 gdk_draw_line (window, style->dark_gc[state_type],
1535 x + width, y + half_height);
1537 case GTK_SHADOW_OUT:
1538 gdk_draw_line (window, style->dark_gc[state_type],
1539 x + 2, y + half_height,
1540 x + half_width, y + height - 2);
1541 gdk_draw_line (window, style->dark_gc[state_type],
1542 x + half_width, y + height - 2,
1543 x + width - 2, y + half_height);
1544 gdk_draw_line (window, style->dark_gc[state_type],
1545 x + 1, y + half_height,
1546 x + half_width, y + height - 1);
1547 gdk_draw_line (window, style->dark_gc[state_type],
1548 x + half_width, y + height - 1,
1549 x + width - 1, y + half_height);
1550 gdk_draw_line (window, style->black_gc,
1552 x + half_width, y + height);
1553 gdk_draw_line (window, style->black_gc,
1554 x + half_width, y + height,
1555 x + width, y + half_height);
1557 gdk_draw_line (window, style->bg_gc[state_type],
1558 x + 2, y + half_height,
1559 x + half_width, y + 2);
1560 gdk_draw_line (window, style->bg_gc[state_type],
1561 x + half_width, y + 2,
1562 x + width - 2, y + half_height);
1563 gdk_draw_line (window, style->light_gc[state_type],
1564 x + 1, y + half_height,
1565 x + half_width, y + 1);
1566 gdk_draw_line (window, style->light_gc[state_type],
1567 x + half_width, y + 1,
1568 x + width - 1, y + half_height);
1569 gdk_draw_line (window, style->light_gc[state_type],
1572 gdk_draw_line (window, style->light_gc[state_type],
1574 x + width, y + half_height);
1583 gtk_default_draw_oval (GtkStyle *style,
1585 GtkStateType state_type,
1586 GtkShadowType shadow_type,
1592 g_return_if_fail (style != NULL);
1593 g_return_if_fail (window != NULL);
1597 gtk_default_draw_string (GtkStyle *style,
1599 GtkStateType state_type,
1602 const gchar *string)
1604 g_return_if_fail (style != NULL);
1605 g_return_if_fail (window != NULL);
1607 if (state_type == GTK_STATE_INSENSITIVE)
1608 gdk_draw_string (window, style->font, style->white_gc, x + 1, y + 1, string);
1609 gdk_draw_string (window, style->font, style->fg_gc[state_type], x, y, string);
1614 gtk_style_shade (GdkColor *a,
1622 red = (gdouble) a->red / 65535.0;
1623 green = (gdouble) a->green / 65535.0;
1624 blue = (gdouble) a->blue / 65535.0;
1626 rgb_to_hls (&red, &green, &blue);
1631 else if (green < 0.0)
1637 else if (blue < 0.0)
1640 hls_to_rgb (&red, &green, &blue);
1642 b->red = red * 65535.0;
1643 b->green = green * 65535.0;
1644 b->blue = blue * 65535.0;
1648 rgb_to_hls (gdouble *r,
1689 l = (max + min) / 2;
1696 s = (max - min) / (max + min);
1698 s = (max - min) / (2 - max - min);
1702 h = (green - blue) / delta;
1703 else if (green == max)
1704 h = 2 + (blue - red) / delta;
1705 else if (blue == max)
1706 h = 4 + (red - green) / delta;
1719 hls_to_rgb (gdouble *h,
1732 if (lightness <= 0.5)
1733 m2 = lightness * (1 + saturation);
1735 m2 = lightness + saturation - lightness * saturation;
1736 m1 = 2 * lightness - m2;
1738 if (saturation == 0)
1753 r = m1 + (m2 - m1) * hue / 60;
1757 r = m1 + (m2 - m1) * (240 - hue) / 60;
1768 g = m1 + (m2 - m1) * hue / 60;
1772 g = m1 + (m2 - m1) * (240 - hue) / 60;
1783 b = m1 + (m2 - m1) * hue / 60;
1787 b = m1 + (m2 - m1) * (240 - hue) / 60;