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 GtkCssChange
_gtk_css_change_for_sibling(GtkCssChange match)26 _gtk_css_change_for_sibling (GtkCssChange match)
27 {
28 #define BASE_STATES ( GTK_CSS_CHANGE_CLASS \
29                     | GTK_CSS_CHANGE_NAME \
30                     | GTK_CSS_CHANGE_ID \
31                     | GTK_CSS_CHANGE_FIRST_CHILD \
32                     | GTK_CSS_CHANGE_LAST_CHILD \
33                     | GTK_CSS_CHANGE_NTH_CHILD \
34                     | GTK_CSS_CHANGE_NTH_LAST_CHILD \
35                     | GTK_CSS_CHANGE_STATE \
36                     | GTK_CSS_CHANGE_HOVER \
37                     | GTK_CSS_CHANGE_DISABLED \
38                     | GTK_CSS_CHANGE_SELECTED \
39                     | GTK_CSS_CHANGE_BACKDROP)
40 
41 #define KEEP_STATES ( ~(BASE_STATES|GTK_CSS_CHANGE_SOURCE|GTK_CSS_CHANGE_PARENT_STYLE) \
42                     | GTK_CSS_CHANGE_NTH_CHILD \
43                     | GTK_CSS_CHANGE_NTH_LAST_CHILD)
44 
45   return (match & KEEP_STATES) | ((match & BASE_STATES) << GTK_CSS_CHANGE_SIBLING_SHIFT);
46 
47 #undef BASE_STATES
48 #undef KEEP_STATES
49 }
50 
51 GtkCssChange
_gtk_css_change_for_child(GtkCssChange match)52 _gtk_css_change_for_child (GtkCssChange match)
53 {
54 #define BASE_STATES ( GTK_CSS_CHANGE_CLASS \
55                     | GTK_CSS_CHANGE_NAME \
56                     | GTK_CSS_CHANGE_ID \
57                     | GTK_CSS_CHANGE_FIRST_CHILD \
58                     | GTK_CSS_CHANGE_LAST_CHILD \
59                     | GTK_CSS_CHANGE_NTH_CHILD \
60                     | GTK_CSS_CHANGE_NTH_LAST_CHILD \
61                     | GTK_CSS_CHANGE_STATE \
62                     | GTK_CSS_CHANGE_HOVER \
63                     | GTK_CSS_CHANGE_DISABLED \
64                     | GTK_CSS_CHANGE_BACKDROP \
65                     | GTK_CSS_CHANGE_SELECTED \
66                     | GTK_CSS_CHANGE_SIBLING_CLASS \
67                     | GTK_CSS_CHANGE_SIBLING_NAME \
68                     | GTK_CSS_CHANGE_SIBLING_ID \
69                     | GTK_CSS_CHANGE_SIBLING_FIRST_CHILD \
70                     | GTK_CSS_CHANGE_SIBLING_LAST_CHILD \
71                     | GTK_CSS_CHANGE_SIBLING_NTH_CHILD \
72                     | GTK_CSS_CHANGE_SIBLING_NTH_LAST_CHILD \
73                     | GTK_CSS_CHANGE_SIBLING_STATE \
74                     | GTK_CSS_CHANGE_SIBLING_HOVER \
75                     | GTK_CSS_CHANGE_SIBLING_DISABLED \
76                     | GTK_CSS_CHANGE_SIBLING_BACKDROP \
77                     | GTK_CSS_CHANGE_SIBLING_SELECTED)
78 
79 #define KEEP_STATES (~(BASE_STATES|GTK_CSS_CHANGE_SOURCE|GTK_CSS_CHANGE_PARENT_STYLE))
80 
81   return (match & KEEP_STATES) | ((match & BASE_STATES) << GTK_CSS_CHANGE_PARENT_SHIFT);
82 
83 #undef BASE_STATES
84 #undef KEEP_STATES
85 }
86 
87 void
gtk_css_change_print(GtkCssChange change,GString * string)88 gtk_css_change_print (GtkCssChange  change,
89                       GString      *string)
90 {
91   const struct {
92     GtkCssChange flags;
93     const char *name;
94   } names[] = {
95     { GTK_CSS_CHANGE_CLASS, "class" },
96     { GTK_CSS_CHANGE_NAME, "name" },
97     { GTK_CSS_CHANGE_ID, "id" },
98     { GTK_CSS_CHANGE_FIRST_CHILD, "first-child" },
99     { GTK_CSS_CHANGE_LAST_CHILD, "last-child" },
100     { GTK_CSS_CHANGE_NTH_CHILD, "nth-child" },
101     { GTK_CSS_CHANGE_NTH_LAST_CHILD, "nth-last-child" },
102     { GTK_CSS_CHANGE_STATE, "state" },
103     { GTK_CSS_CHANGE_HOVER, "hover" },
104     { GTK_CSS_CHANGE_DISABLED, "disabled" },
105     { GTK_CSS_CHANGE_BACKDROP, "backdrop" },
106     { GTK_CSS_CHANGE_SELECTED, "selected" },
107 
108     { GTK_CSS_CHANGE_SIBLING_CLASS, "sibling-class" },
109     { GTK_CSS_CHANGE_SIBLING_NAME, "sibling-name" },
110     { GTK_CSS_CHANGE_SIBLING_ID, "sibling-id" },
111     { GTK_CSS_CHANGE_SIBLING_FIRST_CHILD, "sibling-first-child" },
112     { GTK_CSS_CHANGE_SIBLING_LAST_CHILD, "sibling-last-child" },
113     { GTK_CSS_CHANGE_SIBLING_NTH_CHILD, "sibling-nth-child" },
114     { GTK_CSS_CHANGE_SIBLING_NTH_LAST_CHILD, "sibling-nth-last-child" },
115     { GTK_CSS_CHANGE_SIBLING_STATE, "sibling-state" },
116     { GTK_CSS_CHANGE_SIBLING_HOVER, "sibling-hover" },
117     { GTK_CSS_CHANGE_SIBLING_DISABLED, "sibling-disabled" },
118     { GTK_CSS_CHANGE_SIBLING_BACKDROP, "sibling-backdrop" },
119     { GTK_CSS_CHANGE_SIBLING_SELECTED, "sibling-selected" },
120 
121     { GTK_CSS_CHANGE_PARENT_CLASS, "parent-class" },
122     { GTK_CSS_CHANGE_PARENT_NAME, "parent-name" },
123     { GTK_CSS_CHANGE_PARENT_ID, "parent-id" },
124     { GTK_CSS_CHANGE_PARENT_FIRST_CHILD, "parent-first-child" },
125     { GTK_CSS_CHANGE_PARENT_LAST_CHILD, "parent-last-child" },
126     { GTK_CSS_CHANGE_PARENT_NTH_CHILD, "parent-nth-child" },
127     { GTK_CSS_CHANGE_PARENT_NTH_LAST_CHILD, "parent-nth-last-child" },
128     { GTK_CSS_CHANGE_PARENT_STATE, "parent-state" },
129     { GTK_CSS_CHANGE_PARENT_HOVER, "parent-hover" },
130     { GTK_CSS_CHANGE_PARENT_DISABLED, "parent-disabled" },
131     { GTK_CSS_CHANGE_PARENT_BACKDROP, "parent-backdrop" },
132     { GTK_CSS_CHANGE_PARENT_SELECTED, "parent-selected" },
133 
134     { GTK_CSS_CHANGE_PARENT_SIBLING_CLASS, "parent-sibling-class" },
135     { GTK_CSS_CHANGE_PARENT_SIBLING_NAME, "parent-sibling-name" },
136     { GTK_CSS_CHANGE_PARENT_SIBLING_ID, "parent-sibling-id" },
137     { GTK_CSS_CHANGE_PARENT_SIBLING_FIRST_CHILD, "parent-sibling-first-child" },
138     { GTK_CSS_CHANGE_PARENT_SIBLING_LAST_CHILD, "parent-sibling-last-child" },
139     { GTK_CSS_CHANGE_PARENT_SIBLING_NTH_CHILD, "parent-sibling-nth-child" },
140     { GTK_CSS_CHANGE_PARENT_SIBLING_NTH_LAST_CHILD, "parent-sibling-nth-last-child" },
141     { GTK_CSS_CHANGE_PARENT_SIBLING_STATE, "parent-sibling-state" },
142     { GTK_CSS_CHANGE_PARENT_SIBLING_HOVER, "parent-sibling-hover" },
143     { GTK_CSS_CHANGE_PARENT_SIBLING_DISABLED, "parent-sibling-disabled" },
144     { GTK_CSS_CHANGE_PARENT_SIBLING_BACKDROP, "parent-sibling-backdrop" },
145     { GTK_CSS_CHANGE_PARENT_SIBLING_SELECTED, "parent-sibling-selected" },
146 
147     { GTK_CSS_CHANGE_SOURCE, "source" },
148     { GTK_CSS_CHANGE_PARENT_STYLE, "parent-style" },
149     { GTK_CSS_CHANGE_TIMESTAMP, "timestamp" },
150     { GTK_CSS_CHANGE_ANIMATIONS, "animations" },
151   };
152   guint i;
153   gboolean first;
154 
155   first = TRUE;
156 
157   for (i = 0; i < G_N_ELEMENTS (names); i++)
158     {
159       if (change & names[i].flags)
160         {
161           if (first)
162             first = FALSE;
163           else
164             g_string_append (string, "|");
165           g_string_append (string, names[i].name);
166         }
167     }
168 }
169 
170 GtkCssDimension
gtk_css_unit_get_dimension(GtkCssUnit unit)171 gtk_css_unit_get_dimension (GtkCssUnit unit)
172 {
173   switch (unit)
174     {
175     case GTK_CSS_NUMBER:
176       return GTK_CSS_DIMENSION_NUMBER;
177 
178     case GTK_CSS_PERCENT:
179       return GTK_CSS_DIMENSION_PERCENTAGE;
180 
181     case GTK_CSS_PX:
182     case GTK_CSS_PT:
183     case GTK_CSS_EM:
184     case GTK_CSS_EX:
185     case GTK_CSS_REM:
186     case GTK_CSS_PC:
187     case GTK_CSS_IN:
188     case GTK_CSS_CM:
189     case GTK_CSS_MM:
190       return GTK_CSS_DIMENSION_LENGTH;
191 
192     case GTK_CSS_RAD:
193     case GTK_CSS_DEG:
194     case GTK_CSS_GRAD:
195     case GTK_CSS_TURN:
196       return GTK_CSS_DIMENSION_ANGLE;
197 
198     case GTK_CSS_S:
199     case GTK_CSS_MS:
200       return GTK_CSS_DIMENSION_TIME;
201 
202     default:
203       g_assert_not_reached ();
204       return GTK_CSS_DIMENSION_PERCENTAGE;
205     }
206 }
207 
208 char *
gtk_css_change_to_string(GtkCssChange change)209 gtk_css_change_to_string (GtkCssChange change)
210 {
211   GString *string = g_string_new (NULL);
212 
213   gtk_css_change_print (change, string);
214 
215   return g_string_free (string, FALSE);
216 }
217 
218 const char *
gtk_css_pseudoclass_name(GtkStateFlags state)219 gtk_css_pseudoclass_name (GtkStateFlags state)
220 {
221   static const char * state_names[] = {
222     "active",
223     "hover",
224     "selected",
225     "disabled",
226     "indeterminate",
227     "focus",
228     "backdrop",
229     "dir(ltr)",
230     "dir(rtl)",
231     "link",
232     "visited",
233     "checked",
234     "drop(active)",
235     "focus-visible",
236     "focus-within"
237   };
238   guint i;
239 
240   for (i = 0; i < G_N_ELEMENTS (state_names); i++)
241     {
242       if (state == (1 << i))
243         return state_names[i];
244     }
245 
246   return NULL;
247 }
248 
249