wxcss 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wxcss/__init__.py +60 -0
- wxcss/internals.py +492 -0
- wxcss/py.typed +1 -0
- wxcss-0.1.0.dist-info/METADATA +156 -0
- wxcss-0.1.0.dist-info/RECORD +6 -0
- wxcss-0.1.0.dist-info/WHEEL +4 -0
wxcss/__init__.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from cssutils.css import (
|
|
2
|
+
CSSComment,
|
|
3
|
+
CSSRuleList,
|
|
4
|
+
CSSStyleRule,
|
|
5
|
+
CSSStyleSheet,
|
|
6
|
+
Selector,
|
|
7
|
+
)
|
|
8
|
+
import wx
|
|
9
|
+
import cssutils
|
|
10
|
+
|
|
11
|
+
from wxcss import internals
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def set_class(window: wx.Window, cls_name: str):
|
|
15
|
+
if not hasattr(window, "__css_classes__"):
|
|
16
|
+
setattr(window, "__css_classes__", [])
|
|
17
|
+
classes = getattr(window, "__css_classes__")
|
|
18
|
+
if not isinstance(classes, list):
|
|
19
|
+
raise TypeError()
|
|
20
|
+
classes.append(cls_name)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def apply_css(css_data: str, window: wx.Window):
|
|
24
|
+
stylesheet: CSSStyleSheet = cssutils.parseString(css_data)
|
|
25
|
+
|
|
26
|
+
def _apply_css_recur(
|
|
27
|
+
window: wx.Window, previously_applied_rules: list[CSSStyleRule]
|
|
28
|
+
):
|
|
29
|
+
window_selector_name = internals.get_css_element_names(window.__class__)
|
|
30
|
+
rules: CSSRuleList = stylesheet.cssRules
|
|
31
|
+
applied_rules = [*previously_applied_rules]
|
|
32
|
+
|
|
33
|
+
for rule in rules:
|
|
34
|
+
match rule:
|
|
35
|
+
case CSSStyleRule():
|
|
36
|
+
selector: Selector
|
|
37
|
+
for selector in rule.selectorList:
|
|
38
|
+
element = selector.element
|
|
39
|
+
should_apply = (
|
|
40
|
+
element is None or element[1] in window_selector_name
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
selector_parts = selector.selectorText.split(".")[1:]
|
|
44
|
+
should_apply = should_apply and internals.all_classes_match(
|
|
45
|
+
selector_parts, window
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if should_apply:
|
|
49
|
+
internals.apply_css_rule(rule, window)
|
|
50
|
+
applied_rules.append(rule)
|
|
51
|
+
|
|
52
|
+
case CSSComment():
|
|
53
|
+
pass
|
|
54
|
+
case _:
|
|
55
|
+
print(rule, type(rule))
|
|
56
|
+
|
|
57
|
+
for child in window.Children:
|
|
58
|
+
_apply_css_recur(child, applied_rules)
|
|
59
|
+
|
|
60
|
+
_apply_css_recur(window, [])
|
wxcss/internals.py
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
from typing import NamedTuple, Protocol
|
|
2
|
+
|
|
3
|
+
from cssutils.css import (
|
|
4
|
+
CSSStyleRule,
|
|
5
|
+
ColorValue,
|
|
6
|
+
DimensionValue,
|
|
7
|
+
Property,
|
|
8
|
+
PropertyValue,
|
|
9
|
+
Value,
|
|
10
|
+
)
|
|
11
|
+
import wx
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CustomizedCSSElementClass(Protocol):
|
|
15
|
+
"""A class with a customized CSS element selector name"""
|
|
16
|
+
|
|
17
|
+
__css_element_name__: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class WidgetWithCSSClasses(Protocol):
|
|
21
|
+
"""A widget containing multiple css classes."""
|
|
22
|
+
|
|
23
|
+
__css_classes__: list[str]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_css_element_name(cls: CustomizedCSSElementClass | type) -> str:
|
|
27
|
+
"""Get the element name used for CSS selectors"""
|
|
28
|
+
if hasattr(cls, "__css_element_name__"):
|
|
29
|
+
return cls.__css_element_name__
|
|
30
|
+
return getattr(cls, "__name__")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_css_element_names(cls: CustomizedCSSElementClass | type) -> list[str]:
|
|
34
|
+
"""Get all element names used for CSS selectors"""
|
|
35
|
+
return [
|
|
36
|
+
get_css_element_name(cls),
|
|
37
|
+
*(get_css_element_name(base) for base in cls.mro()),
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def get_css_classes(window: WidgetWithCSSClasses | wx.Window) -> list[str]:
|
|
42
|
+
"""get all css classes on a widget"""
|
|
43
|
+
if hasattr(window, "__css_classes__"):
|
|
44
|
+
return getattr(window, "__css_classes__")
|
|
45
|
+
return []
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def all_classes_match(
|
|
49
|
+
incoming_classes, window: WidgetWithCSSClasses | wx.Window
|
|
50
|
+
) -> bool:
|
|
51
|
+
"""Checks if all incoming_classes are found on a wx.Window object."""
|
|
52
|
+
return all(incoming in get_css_classes(window) for incoming in incoming_classes)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
_FONT_WEIGHTS_MAP = {
|
|
56
|
+
"normal": wx.FONTWEIGHT_NORMAL,
|
|
57
|
+
"bold": wx.FONTWEIGHT_BOLD,
|
|
58
|
+
"bolder": wx.FONTWEIGHT_EXTRABOLD,
|
|
59
|
+
"extra-bold": wx.FONTWEIGHT_EXTRABOLD,
|
|
60
|
+
"light": wx.FONTWEIGHT_LIGHT,
|
|
61
|
+
"lighter": wx.FONTWEIGHT_EXTRALIGHT,
|
|
62
|
+
"extra-light": wx.FONTWEIGHT_EXTRALIGHT,
|
|
63
|
+
"heavy": wx.FONTWEIGHT_HEAVY,
|
|
64
|
+
"heavier": wx.FONTWEIGHT_EXTRAHEAVY,
|
|
65
|
+
"extra-heavy": wx.FONTWEIGHT_EXTRAHEAVY,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
_FONT_STYLE_MAP = {
|
|
69
|
+
"normal": wx.FONTSTYLE_NORMAL,
|
|
70
|
+
"oblique": wx.FONTSTYLE_SLANT,
|
|
71
|
+
"italic": wx.FONTSTYLE_ITALIC,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class WxCssAutoCreated(NamedTuple):
|
|
76
|
+
created_for: wx.Window
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# TODO: break this out into a dll so we can have a speedup [very slow code :(]
|
|
80
|
+
def create_or_update_spacer(
|
|
81
|
+
sizer: wx.Sizer,
|
|
82
|
+
children: wx.SizerItemList,
|
|
83
|
+
index: int,
|
|
84
|
+
window: wx.Window,
|
|
85
|
+
value: int,
|
|
86
|
+
):
|
|
87
|
+
if (
|
|
88
|
+
(len(children) - 1 > index)
|
|
89
|
+
and (children[index].IsSpacer())
|
|
90
|
+
and (type(children[index].UserData) == WxCssAutoCreated)
|
|
91
|
+
and (children[index].UserData.created_for is window)
|
|
92
|
+
):
|
|
93
|
+
children[index].GetSpacer().Set(value, value)
|
|
94
|
+
else:
|
|
95
|
+
item = sizer.InsertSpacer(index, value)
|
|
96
|
+
item.SetUserData(WxCssAutoCreated(window))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class ApplicationFunctions:
|
|
100
|
+
"""A Namespace containing all functions used for application if properties"""
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def simple_application_spec(*spec: type[Value]):
|
|
104
|
+
def decorator(func):
|
|
105
|
+
def wrapper(window: wx.Window, value: PropertyValue):
|
|
106
|
+
if value.length != len(spec):
|
|
107
|
+
raise ValueError(value)
|
|
108
|
+
for value_item, spec_item in zip(value, spec):
|
|
109
|
+
if not isinstance(value_item, spec_item):
|
|
110
|
+
raise TypeError(value_item, "Expected", spec_item)
|
|
111
|
+
|
|
112
|
+
return func(window, tuple(value))
|
|
113
|
+
|
|
114
|
+
return wrapper
|
|
115
|
+
|
|
116
|
+
return decorator
|
|
117
|
+
|
|
118
|
+
@simple_application_spec(ColorValue)
|
|
119
|
+
@staticmethod
|
|
120
|
+
def apply_background_color(window: wx.Window, value: tuple[ColorValue]):
|
|
121
|
+
window.SetBackgroundColour(
|
|
122
|
+
wx.Colour(
|
|
123
|
+
int(value[0].red),
|
|
124
|
+
int(value[0].green),
|
|
125
|
+
int(value[0].blue),
|
|
126
|
+
alpha=int(value[0].alpha),
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
@simple_application_spec(ColorValue)
|
|
131
|
+
@staticmethod
|
|
132
|
+
def apply_foreground_color(window: wx.Window, value: tuple[ColorValue]):
|
|
133
|
+
window.SetForegroundColour(
|
|
134
|
+
wx.Colour(
|
|
135
|
+
int(value[0].red),
|
|
136
|
+
int(value[0].green),
|
|
137
|
+
int(value[0].blue),
|
|
138
|
+
alpha=int(value[0].alpha),
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
@simple_application_spec(DimensionValue)
|
|
143
|
+
@staticmethod
|
|
144
|
+
def apply_font_size(window: wx.Window, value: tuple[DimensionValue]):
|
|
145
|
+
css_size = value[0]
|
|
146
|
+
|
|
147
|
+
if css_size.dimension == "pt":
|
|
148
|
+
font = window.GetFont()
|
|
149
|
+
font.SetPointSize(int(css_size.value))
|
|
150
|
+
window.SetFont(font)
|
|
151
|
+
elif css_size.dimension == "px":
|
|
152
|
+
font = window.GetFont()
|
|
153
|
+
font.SetPixelSize((0, css_size.value))
|
|
154
|
+
window.SetFont(font)
|
|
155
|
+
else:
|
|
156
|
+
raise TypeError(css_size, "Expected value in pt or px units")
|
|
157
|
+
|
|
158
|
+
@simple_application_spec(Value)
|
|
159
|
+
@staticmethod
|
|
160
|
+
def apply_font_weight(window: wx.Window, value: tuple[Value]):
|
|
161
|
+
css_weight = value[0]
|
|
162
|
+
|
|
163
|
+
if css_weight.value not in _FONT_WEIGHTS_MAP.keys():
|
|
164
|
+
raise ValueError(css_weight)
|
|
165
|
+
|
|
166
|
+
font = window.GetFont()
|
|
167
|
+
font.SetWeight(_FONT_WEIGHTS_MAP[css_weight.value])
|
|
168
|
+
window.SetFont(font)
|
|
169
|
+
|
|
170
|
+
@simple_application_spec(Value)
|
|
171
|
+
@staticmethod
|
|
172
|
+
def apply_font_family(window: wx.Window, value: tuple[Value]):
|
|
173
|
+
css_family = value[0]
|
|
174
|
+
|
|
175
|
+
font = window.GetFont()
|
|
176
|
+
font.SetFaceName(css_family.value)
|
|
177
|
+
window.SetFont(font)
|
|
178
|
+
|
|
179
|
+
@simple_application_spec(Value)
|
|
180
|
+
@staticmethod
|
|
181
|
+
def apply_font_style(window: wx.Window, value: tuple[Value]):
|
|
182
|
+
css_style = value[0]
|
|
183
|
+
|
|
184
|
+
if css_style.value not in _FONT_STYLE_MAP.keys():
|
|
185
|
+
raise ValueError(css_style)
|
|
186
|
+
|
|
187
|
+
font = window.GetFont()
|
|
188
|
+
font.SetStyle(_FONT_STYLE_MAP[css_style.value])
|
|
189
|
+
window.SetFont(font)
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def apply_text_decoration(window: wx.Window, value: PropertyValue):
|
|
193
|
+
font = window.GetFont()
|
|
194
|
+
for deco in value:
|
|
195
|
+
if deco.value == "none":
|
|
196
|
+
font.SetUnderlined(False)
|
|
197
|
+
font.SetStrikethrough(False)
|
|
198
|
+
if deco.value == "underline":
|
|
199
|
+
font.SetUnderlined(True)
|
|
200
|
+
if deco.value == "line-through":
|
|
201
|
+
font.SetStrikethrough(True)
|
|
202
|
+
window.SetFont(font)
|
|
203
|
+
|
|
204
|
+
@simple_application_spec(Value)
|
|
205
|
+
@staticmethod
|
|
206
|
+
def apply_content(window: wx.Window, value: tuple[Value]) -> bool:
|
|
207
|
+
"""Applies "content" to various widgets. Can be hijacked to support more widget types.
|
|
208
|
+
Returns :py:const:`True` if the widget was already handled."""
|
|
209
|
+
match window:
|
|
210
|
+
case wx.StaticText():
|
|
211
|
+
window.SetLabelText(value[0].value)
|
|
212
|
+
case wx.TextCtrl():
|
|
213
|
+
window.SetValue(value[0].value)
|
|
214
|
+
case wx.Button():
|
|
215
|
+
window.SetLabelText(value[0].value)
|
|
216
|
+
case _:
|
|
217
|
+
return False
|
|
218
|
+
return True
|
|
219
|
+
|
|
220
|
+
@simple_application_spec(DimensionValue)
|
|
221
|
+
@staticmethod
|
|
222
|
+
def apply_margin(window: wx.Window, value: tuple[DimensionValue]):
|
|
223
|
+
if value[0].dimension != "px":
|
|
224
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
225
|
+
|
|
226
|
+
if window.GetParent() is None:
|
|
227
|
+
return
|
|
228
|
+
sizer = window.GetParent().GetSizer()
|
|
229
|
+
if sizer is None:
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
children = sizer.GetChildren()
|
|
233
|
+
for c, item in enumerate(children):
|
|
234
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
235
|
+
# apply or update right margin
|
|
236
|
+
create_or_update_spacer(sizer, children, c + 1, window, value[0].value)
|
|
237
|
+
# apply or update left margin
|
|
238
|
+
create_or_update_spacer(sizer, children, c - 1, window, value[0].value)
|
|
239
|
+
break
|
|
240
|
+
|
|
241
|
+
@simple_application_spec(DimensionValue)
|
|
242
|
+
@staticmethod
|
|
243
|
+
def apply_margin_left(window: wx.Window, value: tuple[DimensionValue]):
|
|
244
|
+
if value[0].dimension != "px":
|
|
245
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
246
|
+
|
|
247
|
+
if window.GetParent() is None:
|
|
248
|
+
return
|
|
249
|
+
sizer = window.GetParent().GetSizer()
|
|
250
|
+
if sizer is None:
|
|
251
|
+
return
|
|
252
|
+
|
|
253
|
+
children = sizer.GetChildren()
|
|
254
|
+
for c, item in enumerate(children):
|
|
255
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
256
|
+
create_or_update_spacer(sizer, children, c - 1, window, value[0].value)
|
|
257
|
+
break
|
|
258
|
+
|
|
259
|
+
@simple_application_spec(DimensionValue)
|
|
260
|
+
@staticmethod
|
|
261
|
+
def apply_margin_right(window: wx.Window, value: tuple[DimensionValue]):
|
|
262
|
+
if value[0].dimension != "px":
|
|
263
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
264
|
+
|
|
265
|
+
if window.GetParent() is None:
|
|
266
|
+
return
|
|
267
|
+
sizer = window.GetParent().GetSizer()
|
|
268
|
+
if sizer is None:
|
|
269
|
+
return
|
|
270
|
+
|
|
271
|
+
children = sizer.GetChildren()
|
|
272
|
+
for c, item in enumerate(children):
|
|
273
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
274
|
+
create_or_update_spacer(sizer, children, c + 1, window, value[0].value)
|
|
275
|
+
break
|
|
276
|
+
|
|
277
|
+
@simple_application_spec(DimensionValue)
|
|
278
|
+
@staticmethod
|
|
279
|
+
def apply_padding(window: wx.Window, value: tuple[DimensionValue]):
|
|
280
|
+
if value[0].dimension != "px":
|
|
281
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
282
|
+
sizer = window.GetSizer()
|
|
283
|
+
if sizer is None:
|
|
284
|
+
return
|
|
285
|
+
children = sizer.GetChildren()
|
|
286
|
+
if len(children) > 0:
|
|
287
|
+
# Insert at beginning if entry does not exist
|
|
288
|
+
create_or_update_spacer(sizer, children, 0, window, value[0].value)
|
|
289
|
+
|
|
290
|
+
# insert at end if entry doesnt exist
|
|
291
|
+
create_or_update_spacer(sizer, children, -1, window, value[0].value)
|
|
292
|
+
else:
|
|
293
|
+
item = sizer.AddSpacer(value[0].value)
|
|
294
|
+
item.SetUserData(WxCssAutoCreated(window))
|
|
295
|
+
|
|
296
|
+
@simple_application_spec(DimensionValue)
|
|
297
|
+
@staticmethod
|
|
298
|
+
def apply_max_width(window: wx.Window, value: tuple[DimensionValue]):
|
|
299
|
+
if value[0].dimension != "px":
|
|
300
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
301
|
+
window.SetMaxSize(wx.Size(value[0].value, window.GetMaxHeight()))
|
|
302
|
+
|
|
303
|
+
@simple_application_spec(DimensionValue)
|
|
304
|
+
@staticmethod
|
|
305
|
+
def apply_max_height(window: wx.Window, value: tuple[DimensionValue]):
|
|
306
|
+
if value[0].dimension != "px":
|
|
307
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
308
|
+
window.SetMaxSize(wx.Size(window.GetMaxWidth(), value[0].value))
|
|
309
|
+
|
|
310
|
+
@simple_application_spec(DimensionValue)
|
|
311
|
+
@staticmethod
|
|
312
|
+
def apply_min_width(window: wx.Window, value: tuple[DimensionValue]):
|
|
313
|
+
if value[0].dimension != "px":
|
|
314
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
315
|
+
window.SetMinSize(wx.Size(value[0].value, window.GetMinHeight()))
|
|
316
|
+
|
|
317
|
+
@staticmethod
|
|
318
|
+
def apply_min_height(window: wx.Window, value: tuple[DimensionValue]):
|
|
319
|
+
if value[0].dimension != "px":
|
|
320
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
321
|
+
window.SetMinSize(wx.Size(window.GetMinWidth(), value[0].value))
|
|
322
|
+
|
|
323
|
+
@simple_application_spec(DimensionValue)
|
|
324
|
+
@staticmethod
|
|
325
|
+
def apply_width(window: wx.Window, value: tuple[DimensionValue]):
|
|
326
|
+
if value[0].dimension != "px":
|
|
327
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
328
|
+
x, y = window.GetPositionTuple()
|
|
329
|
+
window.SetDimensions(x, y, value[0].value, window.GetSize().Height)
|
|
330
|
+
|
|
331
|
+
@simple_application_spec(DimensionValue)
|
|
332
|
+
@staticmethod
|
|
333
|
+
def apply_height(window: wx.Window, value: tuple[DimensionValue]):
|
|
334
|
+
if value[0].dimension != "px":
|
|
335
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
336
|
+
x, y = window.GetPositionTuple()
|
|
337
|
+
window.SetDimensions(x, y, window.GetSize().Width, value[0].value)
|
|
338
|
+
|
|
339
|
+
@simple_application_spec(DimensionValue)
|
|
340
|
+
@staticmethod
|
|
341
|
+
def apply_border_width(window: wx.Window, value: tuple[DimensionValue]):
|
|
342
|
+
if value[0].dimension != "px":
|
|
343
|
+
raise TypeError(value[0].value, "Expected value in px units")
|
|
344
|
+
|
|
345
|
+
if window.GetParent() is None:
|
|
346
|
+
return
|
|
347
|
+
sizer = window.GetParent().GetSizer()
|
|
348
|
+
if sizer is None:
|
|
349
|
+
return
|
|
350
|
+
|
|
351
|
+
children = sizer.GetChildren()
|
|
352
|
+
for c, item in enumerate(children):
|
|
353
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
354
|
+
item.SetBorder(value[0].value)
|
|
355
|
+
break
|
|
356
|
+
|
|
357
|
+
@simple_application_spec(Value)
|
|
358
|
+
@staticmethod
|
|
359
|
+
def apply_border_style(window: wx.Window, value: tuple[Value]):
|
|
360
|
+
if window.GetParent() is None:
|
|
361
|
+
return
|
|
362
|
+
sizer = window.GetParent().GetSizer()
|
|
363
|
+
if sizer is None:
|
|
364
|
+
return
|
|
365
|
+
|
|
366
|
+
children = sizer.GetChildren()
|
|
367
|
+
for c, item in enumerate(children):
|
|
368
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
369
|
+
if value[0].value == "none":
|
|
370
|
+
window.SetWindowStyleFlag(window.GetWindowStyleFlag() & ~(wx.ALL))
|
|
371
|
+
else:
|
|
372
|
+
window.SetWindowStyleFlag(
|
|
373
|
+
window.GetWindowStyleFlag() | wx.SIMPLE_BORDER | wx.ALL
|
|
374
|
+
)
|
|
375
|
+
break
|
|
376
|
+
|
|
377
|
+
@simple_application_spec(Value)
|
|
378
|
+
@staticmethod
|
|
379
|
+
def apply_border_top_style(window: wx.Window, value: tuple[Value]):
|
|
380
|
+
if window.GetParent() is None:
|
|
381
|
+
return
|
|
382
|
+
sizer = window.GetParent().GetSizer()
|
|
383
|
+
if sizer is None:
|
|
384
|
+
return
|
|
385
|
+
|
|
386
|
+
children = sizer.GetChildren()
|
|
387
|
+
for c, item in enumerate(children):
|
|
388
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
389
|
+
if value[0].value == "none":
|
|
390
|
+
window.SetWindowStyleFlag(window.GetWindowStyleFlag() & ~(wx.TOP))
|
|
391
|
+
else:
|
|
392
|
+
window.SetWindowStyleFlag(
|
|
393
|
+
window.GetWindowStyleFlag() | wx.SIMPLE_BORDER | wx.TOP
|
|
394
|
+
)
|
|
395
|
+
break
|
|
396
|
+
|
|
397
|
+
@simple_application_spec(Value)
|
|
398
|
+
@staticmethod
|
|
399
|
+
def apply_border_bottom_style(window: wx.Window, value: tuple[Value]):
|
|
400
|
+
if window.GetParent() is None:
|
|
401
|
+
return
|
|
402
|
+
sizer = window.GetParent().GetSizer()
|
|
403
|
+
if sizer is None:
|
|
404
|
+
return
|
|
405
|
+
|
|
406
|
+
children = sizer.GetChildren()
|
|
407
|
+
for c, item in enumerate(children):
|
|
408
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
409
|
+
if value[0].value == "none":
|
|
410
|
+
window.SetWindowStyleFlag(
|
|
411
|
+
window.GetWindowStyleFlag() & ~(wx.BOTTOM)
|
|
412
|
+
)
|
|
413
|
+
else:
|
|
414
|
+
window.SetWindowStyleFlag(
|
|
415
|
+
window.GetWindowStyleFlag() | wx.SIMPLE_BORDER | wx.BOTTOM
|
|
416
|
+
)
|
|
417
|
+
break
|
|
418
|
+
|
|
419
|
+
@simple_application_spec(Value)
|
|
420
|
+
@staticmethod
|
|
421
|
+
def apply_border_left_style(window: wx.Window, value: tuple[Value]):
|
|
422
|
+
if window.GetParent() is None:
|
|
423
|
+
return
|
|
424
|
+
sizer = window.GetParent().GetSizer()
|
|
425
|
+
if sizer is None:
|
|
426
|
+
return
|
|
427
|
+
|
|
428
|
+
children = sizer.GetChildren()
|
|
429
|
+
for c, item in enumerate(children):
|
|
430
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
431
|
+
if value[0].value == "none":
|
|
432
|
+
window.SetWindowStyleFlag(window.GetWindowStyleFlag() & ~(wx.LEFT))
|
|
433
|
+
else:
|
|
434
|
+
window.SetWindowStyleFlag(
|
|
435
|
+
window.GetWindowStyleFlag() | wx.SIMPLE_BORDER | wx.LEFT
|
|
436
|
+
)
|
|
437
|
+
break
|
|
438
|
+
|
|
439
|
+
@simple_application_spec(Value)
|
|
440
|
+
@staticmethod
|
|
441
|
+
def apply_border_right_style(window: wx.Window, value: tuple[Value]):
|
|
442
|
+
if window.GetParent() is None:
|
|
443
|
+
return
|
|
444
|
+
sizer = window.GetParent().GetSizer()
|
|
445
|
+
if sizer is None:
|
|
446
|
+
return
|
|
447
|
+
|
|
448
|
+
children = sizer.GetChildren()
|
|
449
|
+
for c, item in enumerate(children):
|
|
450
|
+
if item.IsWindow() and item.GetWindow().Id == window.Id:
|
|
451
|
+
if value[0].value == "none":
|
|
452
|
+
window.SetWindowStyleFlag(window.GetWindowStyleFlag() & ~(wx.RIGHT))
|
|
453
|
+
else:
|
|
454
|
+
window.SetWindowStyleFlag(
|
|
455
|
+
window.GetWindowStyleFlag() | wx.SIMPLE_BORDER | wx.RIGHT
|
|
456
|
+
)
|
|
457
|
+
break
|
|
458
|
+
|
|
459
|
+
_mapping = {
|
|
460
|
+
"background-color": apply_background_color,
|
|
461
|
+
"color": apply_foreground_color,
|
|
462
|
+
"font-size": apply_font_size,
|
|
463
|
+
"font-weight": apply_font_weight,
|
|
464
|
+
"font-family": apply_font_family,
|
|
465
|
+
"font-style": apply_font_style,
|
|
466
|
+
"text-decoration": apply_text_decoration,
|
|
467
|
+
"content": apply_content,
|
|
468
|
+
"margin": apply_margin,
|
|
469
|
+
"margin-left": apply_margin_left,
|
|
470
|
+
"margin-right": apply_margin_right,
|
|
471
|
+
"padding": apply_padding,
|
|
472
|
+
"max-width": apply_max_width,
|
|
473
|
+
"max-height": apply_max_height,
|
|
474
|
+
"min-width": apply_min_width,
|
|
475
|
+
"min-height": apply_min_height,
|
|
476
|
+
"width": apply_width,
|
|
477
|
+
"height": apply_height,
|
|
478
|
+
"border-width": apply_border_width,
|
|
479
|
+
"border-style": apply_border_style,
|
|
480
|
+
"border-top-style": apply_border_top_style,
|
|
481
|
+
"border-bottom-style": apply_border_bottom_style,
|
|
482
|
+
"border-left-style": apply_border_left_style,
|
|
483
|
+
"border-right-style": apply_border_right_style,
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def apply_css_rule(rule: CSSStyleRule, window: wx.Window):
|
|
488
|
+
property: Property
|
|
489
|
+
for property in rule.style:
|
|
490
|
+
if property.name not in ApplicationFunctions._mapping.keys():
|
|
491
|
+
continue
|
|
492
|
+
ApplicationFunctions._mapping[property.name](window, property.propertyValue)
|
wxcss/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Just declares that this packaged is well-typed for mypy
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: wxcss
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A work-in-progress package to help you apply css stylesheets to WxPython ui.
|
|
5
|
+
Keywords: typed,wxpython,wxwidgets,css,wxcss,stylesheets,wx
|
|
6
|
+
Author: ArachnidAbby, Summersweet-Software
|
|
7
|
+
License: MPL-2.0
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Natural Language :: English
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Dist: cssutils>=2.15.0
|
|
20
|
+
Requires-Dist: wxpython>=4.3.2
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Project-URL: Homepage, https://summersweet.software
|
|
23
|
+
Project-URL: Repository, https://github.com/summersweet-software/wxcss
|
|
24
|
+
Project-URL: Issues, https://github.com/summersweet-software/wxcss/issues
|
|
25
|
+
Project-URL: Documentation, https://wxcss.readthedocs.io/en/latest/
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
[](https://github.com/Summersweet-Software/wxcss?tab=MIT-1-ov-file) [](https://wxcss.readthedocs.io/en/latest/)
|
|
29
|
+
[](https://pypi.org/project/wxcss/) [](https://github.com/sponsors/Summersweet-Software)
|
|
30
|
+
|
|
31
|
+
# wxcss
|
|
32
|
+
|
|
33
|
+
> [!IMPORTANT]
|
|
34
|
+
> wxcss its not affiliated with the developers of WxWidgets or WxPython
|
|
35
|
+
> It simply works _with_ WxPython and is a library to extend it. Thats it.
|
|
36
|
+
> The developers of WxWidgets/WxPython will not assist you with bugs in this library.
|
|
37
|
+
> Additionally:
|
|
38
|
+
> if you view this on pypi, the github is summersweet-software/wxcss. NOT skrulcik/wxcss.
|
|
39
|
+
> the skrulcik repository is extremely old and is not related to this project.
|
|
40
|
+
|
|
41
|
+
Wxcss is a very basic package that adds a handful of helper methods that allow you to style widgets using css.
|
|
42
|
+
|
|
43
|
+
## Why write wxcss?
|
|
44
|
+
|
|
45
|
+
The reason is simple. I hate using wxpython.
|
|
46
|
+
|
|
47
|
+
I know that sounds really weird since I am making a wxpython extension. Genuinely though, one of my biggest annoyances with wxpython was the styling. Its the one hold-out I had in fully embracing wxpython over pyqt/pyside. Its what made me _hate_ it. When, really, its not all that bad of a library.
|
|
48
|
+
|
|
49
|
+
I feel like wxcss at least makes an attempt to solve this problem. It makes me joyful to mess with UI like how I am when working with pyqt/pyside. I hope this package can do the same for you!
|
|
50
|
+
|
|
51
|
+
## Demo
|
|
52
|
+
|
|
53
|
+

|
|
54
|
+
|
|
55
|
+
> Gif showing multiple edits being made to the css and them being applied once the application is reloaded.
|
|
56
|
+
|
|
57
|
+
## Basic API
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
|
|
61
|
+
import wxcss
|
|
62
|
+
# ... wx imports
|
|
63
|
+
|
|
64
|
+
class AppFrame(wx.Frame):
|
|
65
|
+
def __init__(self, *args, **kwargs):
|
|
66
|
+
super().__init__(*args, **kwargs)
|
|
67
|
+
|
|
68
|
+
# * Load the css
|
|
69
|
+
with open("data/default.css", "r") as f:
|
|
70
|
+
css_data = f.read()
|
|
71
|
+
wxcss.apply_css(css_data, self)
|
|
72
|
+
|
|
73
|
+
# css loading needs to happen before this call.
|
|
74
|
+
self.Layout()
|
|
75
|
+
|
|
76
|
+
class MyApp(wx.App):
|
|
77
|
+
def OnInit(self):
|
|
78
|
+
self.frame = AppFrame(None, wx.ID_ANY, "")
|
|
79
|
+
self.SetTopWindow(self.frame)
|
|
80
|
+
self.frame.Show()
|
|
81
|
+
return True
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
app = MyApp(0)
|
|
85
|
+
app.MainLoop()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Apply CSS class to Window/Widget
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import wxcss
|
|
92
|
+
|
|
93
|
+
class MyWidget(wx.Window):
|
|
94
|
+
def __init__(self, *args, **kwargs):
|
|
95
|
+
super().__init__(*args, **kwargs)
|
|
96
|
+
|
|
97
|
+
wxcss.set_class(self, "my-css-class")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**OR**
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import wxcss
|
|
104
|
+
|
|
105
|
+
class MyWidget(wx.Window):
|
|
106
|
+
__css_classes__ = ["my-css-class"]
|
|
107
|
+
|
|
108
|
+
...
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Working CSS Features
|
|
113
|
+
|
|
114
|
+
Below will show you a css example containing all available changeable css properties
|
|
115
|
+
|
|
116
|
+
```css
|
|
117
|
+
/* applied to Frame and Window objects*/
|
|
118
|
+
Frame,
|
|
119
|
+
Window {
|
|
120
|
+
background-color: #ffffff;
|
|
121
|
+
color: #000000; /*modifies "foreground" attribute"*/
|
|
122
|
+
font-family: papyrus;
|
|
123
|
+
font-size: 10px;
|
|
124
|
+
font-weight: heavier;
|
|
125
|
+
font-style: italic;
|
|
126
|
+
text-decoration: underline;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
TextCtrl,
|
|
130
|
+
StaticText {
|
|
131
|
+
content: "some example text";
|
|
132
|
+
|
|
133
|
+
/*The following are very laggy!
|
|
134
|
+
I plan to make these use cython or c for a speedup if possible
|
|
135
|
+
*/
|
|
136
|
+
margin: 10px;
|
|
137
|
+
margin-left: 10px;
|
|
138
|
+
margin-right: 12px;
|
|
139
|
+
|
|
140
|
+
padding: 20px;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/*classes and class element selectors are allowed. Combinators are not supported :( */
|
|
144
|
+
Window.some-class {
|
|
145
|
+
min-width: 50px;
|
|
146
|
+
max-width: 100px;
|
|
147
|
+
width: 80x;
|
|
148
|
+
|
|
149
|
+
min-height: 50px;
|
|
150
|
+
max-height: 100px;
|
|
151
|
+
height: 80px;
|
|
152
|
+
|
|
153
|
+
/* This is still being completed and is buggy/non-functional */
|
|
154
|
+
border-width: 15px;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
wxcss/__init__.py,sha256=V-2MGooqr-DhzMYN0Xmig6lgPTCesnQRLoua7pAPXG0,1958
|
|
2
|
+
wxcss/internals.py,sha256=rX1vqLjIm6PZWSSM4-WFkCcKwv_Wv1ZAltJ0bMDV5-g,17961
|
|
3
|
+
wxcss/py.typed,sha256=6e3Glfsi5LYzU_s4Q2tpqBmF0O0SrjLAK2LTLihxN64,57
|
|
4
|
+
wxcss-0.1.0.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
|
|
5
|
+
wxcss-0.1.0.dist-info/METADATA,sha256=6CwG1C2kUoRUZcFv-j99rd-_cX_pyn3rJgdznw5mm3M,5054
|
|
6
|
+
wxcss-0.1.0.dist-info/RECORD,,
|