1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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.
8  *
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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include "gtkcsstypesprivate.h"
21 
22 #include "gtkcssnumbervalueprivate.h"
23 #include "gtkstylecontextprivate.h"
24 
25 cairo_operator_t
_gtk_css_blend_mode_get_operator(GtkCssBlendMode mode)26 _gtk_css_blend_mode_get_operator (GtkCssBlendMode mode)
27 {
28   switch (mode)
29     {
30     case GTK_CSS_BLEND_MODE_COLOR:
31       return CAIRO_OPERATOR_HSL_COLOR;
32     case GTK_CSS_BLEND_MODE_COLOR_BURN:
33       return CAIRO_OPERATOR_COLOR_BURN;
34     case GTK_CSS_BLEND_MODE_COLOR_DODGE:
35       return CAIRO_OPERATOR_COLOR_DODGE;
36     case GTK_CSS_BLEND_MODE_DARKEN:
37       return CAIRO_OPERATOR_DARKEN;
38     case GTK_CSS_BLEND_MODE_DIFFERENCE:
39       return CAIRO_OPERATOR_DIFFERENCE;
40     case GTK_CSS_BLEND_MODE_EXCLUSION:
41       return CAIRO_OPERATOR_EXCLUSION;
42     case GTK_CSS_BLEND_MODE_HARD_LIGHT:
43       return CAIRO_OPERATOR_HARD_LIGHT;
44     case GTK_CSS_BLEND_MODE_HUE:
45       return CAIRO_OPERATOR_HSL_HUE;
46     case GTK_CSS_BLEND_MODE_LIGHTEN:
47       return CAIRO_OPERATOR_LIGHTEN;
48     case GTK_CSS_BLEND_MODE_LUMINOSITY:
49       return CAIRO_OPERATOR_HSL_LUMINOSITY;
50     case GTK_CSS_BLEND_MODE_MULTIPLY:
51       return CAIRO_OPERATOR_MULTIPLY;
52     case GTK_CSS_BLEND_MODE_OVERLAY:
53       return CAIRO_OPERATOR_OVERLAY;
54     case GTK_CSS_BLEND_MODE_SATURATE:
55       return CAIRO_OPERATOR_SATURATE;
56     case GTK_CSS_BLEND_MODE_SCREEN:
57       return CAIRO_OPERATOR_SCREEN;
58 
59     case GTK_CSS_BLEND_MODE_NORMAL:
60     default:
61       return CAIRO_OPERATOR_OVER;
62     }
63 }
64 
65 GtkCssChange
_gtk_css_change_for_sibling(GtkCssChange match)66 _gtk_css_change_for_sibling (GtkCssChange match)
67 {
68 #define BASE_STATES ( GTK_CSS_CHANGE_CLASS \
69                     | GTK_CSS_CHANGE_NAME \
70                     | GTK_CSS_CHANGE_ID \
71                     | GTK_CSS_CHANGE_FIRST_CHILD \
72                     | GTK_CSS_CHANGE_LAST_CHILD \
73                     | GTK_CSS_CHANGE_NTH_CHILD \
74                     | GTK_CSS_CHANGE_NTH_LAST_CHILD \
75                     | GTK_CSS_CHANGE_STATE )
76 
77 #define KEEP_STATES ( ~(BASE_STATES|GTK_CSS_CHANGE_SOURCE|GTK_CSS_CHANGE_PARENT_STYLE) \
78                     | GTK_CSS_CHANGE_NTH_CHILD \
79                     | GTK_CSS_CHANGE_NTH_LAST_CHILD)
80 
81 #define SIBLING_SHIFT 8
82 
83   return (match & KEEP_STATES) | ((match & BASE_STATES) << SIBLING_SHIFT);
84 
85 #undef BASE_STATES
86 #undef KEEP_STATES
87 #undef SIBLING_SHIFT
88 }
89 
90 GtkCssChange
_gtk_css_change_for_child(GtkCssChange match)91 _gtk_css_change_for_child (GtkCssChange match)
92 {
93 #define BASE_STATES ( GTK_CSS_CHANGE_CLASS \
94                     | GTK_CSS_CHANGE_NAME \
95                     | GTK_CSS_CHANGE_ID \
96                     | GTK_CSS_CHANGE_FIRST_CHILD \
97                     | GTK_CSS_CHANGE_LAST_CHILD \
98                     | GTK_CSS_CHANGE_NTH_CHILD \
99                     | GTK_CSS_CHANGE_NTH_LAST_CHILD \
100                     | GTK_CSS_CHANGE_STATE \
101                     | GTK_CSS_CHANGE_SIBLING_CLASS \
102                     | GTK_CSS_CHANGE_SIBLING_NAME \
103                     | GTK_CSS_CHANGE_SIBLING_ID \
104                     | GTK_CSS_CHANGE_SIBLING_FIRST_CHILD \
105                     | GTK_CSS_CHANGE_SIBLING_LAST_CHILD \
106                     | GTK_CSS_CHANGE_SIBLING_NTH_CHILD \
107                     | GTK_CSS_CHANGE_SIBLING_NTH_LAST_CHILD \
108                     | GTK_CSS_CHANGE_SIBLING_STATE )
109 
110 #define PARENT_SHIFT 16
111 
112   return (match & ~(BASE_STATES|GTK_CSS_CHANGE_SOURCE|GTK_CSS_CHANGE_PARENT_STYLE)) | ((match & BASE_STATES) << PARENT_SHIFT);
113 
114 #undef BASE_STATES
115 #undef PARENT_SHIFT
116 }
117 
118 void
gtk_css_change_print(GtkCssChange change,GString * string)119 gtk_css_change_print (GtkCssChange  change,
120                       GString      *string)
121 {
122   const struct {
123     GtkCssChange flags;
124     const char *name;
125   } names[] = {
126     { GTK_CSS_CHANGE_CLASS, "class" },
127     { GTK_CSS_CHANGE_NAME, "name" },
128     { GTK_CSS_CHANGE_ID, "id" },
129     { GTK_CSS_CHANGE_FIRST_CHILD, "first-child" },
130     { GTK_CSS_CHANGE_LAST_CHILD, "last-child" },
131     { GTK_CSS_CHANGE_NTH_CHILD, "nth-child" },
132     { GTK_CSS_CHANGE_NTH_LAST_CHILD, "nth-last-child" },
133     { GTK_CSS_CHANGE_STATE, "state" },
134     { GTK_CSS_CHANGE_SIBLING_CLASS, "sibling-class" },
135     { GTK_CSS_CHANGE_SIBLING_NAME, "sibling-name" },
136     { GTK_CSS_CHANGE_SIBLING_ID, "sibling-id" },
137     { GTK_CSS_CHANGE_SIBLING_FIRST_CHILD, "sibling-first-child" },
138     { GTK_CSS_CHANGE_SIBLING_LAST_CHILD, "sibling-last-child" },
139     { GTK_CSS_CHANGE_SIBLING_NTH_CHILD, "sibling-nth-child" },
140     { GTK_CSS_CHANGE_SIBLING_NTH_LAST_CHILD, "sibling-nth-last-child" },
141     { GTK_CSS_CHANGE_SIBLING_STATE, "sibling-state" },
142     { GTK_CSS_CHANGE_PARENT_CLASS, "parent-class" },
143     { GTK_CSS_CHANGE_PARENT_NAME, "parent-name" },
144     { GTK_CSS_CHANGE_PARENT_ID, "parent-id" },
145     { GTK_CSS_CHANGE_PARENT_FIRST_CHILD, "parent-first-child" },
146     { GTK_CSS_CHANGE_PARENT_LAST_CHILD, "parent-last-child" },
147     { GTK_CSS_CHANGE_PARENT_NTH_CHILD, "parent-nth-child" },
148     { GTK_CSS_CHANGE_PARENT_NTH_LAST_CHILD, "parent-nth-last-child" },
149     { GTK_CSS_CHANGE_PARENT_STATE, "parent-state" },
150     { GTK_CSS_CHANGE_PARENT_SIBLING_CLASS, "parent-sibling-" },
151     { GTK_CSS_CHANGE_PARENT_SIBLING_NAME, "parent-sibling-name" },
152     { GTK_CSS_CHANGE_PARENT_SIBLING_ID, "parent-sibling-id" },
153     { GTK_CSS_CHANGE_PARENT_SIBLING_FIRST_CHILD, "parent-sibling-first-child" },
154     { GTK_CSS_CHANGE_PARENT_SIBLING_LAST_CHILD, "parent-sibling-last-child" },
155     { GTK_CSS_CHANGE_PARENT_SIBLING_NTH_CHILD, "parent-sibling-nth-child" },
156     { GTK_CSS_CHANGE_PARENT_SIBLING_NTH_LAST_CHILD, "parent-sibling-nth-last-child" },
157     { GTK_CSS_CHANGE_PARENT_SIBLING_STATE, "parent-sibling-state" },
158     { GTK_CSS_CHANGE_SOURCE, "source" },
159     { GTK_CSS_CHANGE_PARENT_STYLE, "parent-style" },
160     { GTK_CSS_CHANGE_TIMESTAMP, "timestamp" },
161     { GTK_CSS_CHANGE_ANIMATIONS, "animations" },
162   };
163   guint i;
164   gboolean first;
165 
166   first = TRUE;
167 
168   for (i = 0; i < G_N_ELEMENTS (names); i++)
169     {
170       if (change & names[i].flags)
171         {
172           if (first)
173             first = FALSE;
174           else
175             g_string_append (string, "|");
176           g_string_append (string, names[i].name);
177         }
178     }
179 }
180 
181 GtkCssDimension
gtk_css_unit_get_dimension(GtkCssUnit unit)182 gtk_css_unit_get_dimension (GtkCssUnit unit)
183 {
184   switch (unit)
185     {
186     case GTK_CSS_NUMBER:
187       return GTK_CSS_DIMENSION_NUMBER;
188 
189     case GTK_CSS_PERCENT:
190       return GTK_CSS_DIMENSION_PERCENTAGE;
191 
192     case GTK_CSS_PX:
193     case GTK_CSS_PT:
194     case GTK_CSS_EM:
195     case GTK_CSS_EX:
196     case GTK_CSS_REM:
197     case GTK_CSS_PC:
198     case GTK_CSS_IN:
199     case GTK_CSS_CM:
200     case GTK_CSS_MM:
201       return GTK_CSS_DIMENSION_LENGTH;
202 
203     case GTK_CSS_RAD:
204     case GTK_CSS_DEG:
205     case GTK_CSS_GRAD:
206     case GTK_CSS_TURN:
207       return GTK_CSS_DIMENSION_ANGLE;
208 
209     case GTK_CSS_S:
210     case GTK_CSS_MS:
211       return GTK_CSS_DIMENSION_TIME;
212 
213     default:
214       g_assert_not_reached ();
215       return GTK_CSS_DIMENSION_PERCENTAGE;
216     }
217 }
218 
219 char *
gtk_css_change_to_string(GtkCssChange change)220 gtk_css_change_to_string (GtkCssChange change)
221 {
222   GString *string = g_string_new (NULL);
223 
224   gtk_css_change_print (change, string);
225 
226   return g_string_free (string, FALSE);
227 }
228 
229