19
by segphault
Merged the rewrite branch into trunk |
1 |
#!/usr/bin/env python
|
2 |
||
3 |
import gtk, pango, gobject, cairo |
|
4 |
||
5 |
def draw_round_rect(c, r, x, y, w, h): |
|
6 |
c.move_to(x+r,y) |
|
7 |
c.line_to(x+w-r,y); c.curve_to(x+w,y,x+w,y,x+w,y+r) |
|
8 |
c.line_to(x+w,y+h-r); c.curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h) |
|
9 |
c.line_to(x+r,y+h); c.curve_to(x,y+h,x,y+h,x,y+h-r) |
|
10 |
c.line_to(x,y+r); c.curve_to(x,y,x,y,x+r,y) |
|
11 |
c.close_path() |
|
12 |
||
13 |
def color_to_cairo_rgba(c, a = 1): |
|
14 |
return c.red/65535.0, c.green/65535.0, c.blue/65535.0, a |
|
15 |
||
16 |
class Frame(gtk.Frame): |
|
17 |
def __init__(self, gradient = None, bgcolor = "white", transparency = 1, rounded=15, gradient_position=2, show_gradient=True): |
|
18 |
self.set_bgcolor(bgcolor) |
|
19 |
self.transparency = transparency |
|
20 |
self.rounded = rounded |
|
21 |
self.gradient = gradient |
|
22 |
self.show_gradient = show_gradient |
|
23 |
self.gradient_position = gradient_position |
|
24 |
gtk.Frame.__init__(self) |
|
25 |
||
26 |
def get_cairo_gradient(self): |
|
27 |
if self.gradient: return gradient |
|
28 |
elif self.bgcolor: |
|
29 |
x,y,w,h = self.allocation |
|
30 |
g = cairo.LinearGradient(0.0, y, 0.0, y + h * self.gradient_position) |
|
31 |
g.add_color_stop_rgba(0, *color_to_cairo_rgba(self.bgcolor, self.transparency)) |
|
32 |
if self.show_gradient: g.add_color_stop_rgba(1, 0, 0, 0, 1) |
|
33 |
return g |
|
34 |
||
35 |
def set_bgcolor(self, color): |
|
36 |
if isinstance(color, gtk.gdk.Color): self.bgcolor = color |
|
37 |
elif isinstance(color, str): self.bgcolor = gtk.gdk.color_parse(color) |
|
38 |
||
39 |
def do_expose_event(self, event): |
|
40 |
self.set_shadow_type(gtk.SHADOW_NONE) |
|
41 |
x,y,w,h = self.allocation |
|
42 |
||
43 |
c = self.window.cairo_create() |
|
44 |
c.set_source(self.get_cairo_gradient()) |
|
45 |
||
46 |
draw_round_rect(c, self.rounded, *self.allocation) |
|
47 |
c.fill() |
|
48 |
||
49 |
gtk.Frame.do_expose_event(self, event) |
|
50 |
||
51 |
gobject.type_register(Frame) |
|
52 |
||
53 |
class WrapLabel(gtk.Frame): |
|
24
by segphault
Added support for configurable text shadows |
54 |
def __init__(self, markup = None, text = None, shadow = None): |
19
by segphault
Merged the rewrite branch into trunk |
55 |
gtk.Frame.__init__(self) |
56 |
self.set_shadow_type(gtk.SHADOW_NONE) |
|
24
by segphault
Added support for configurable text shadows |
57 |
self.shadow = shadow |
19
by segphault
Merged the rewrite branch into trunk |
58 |
|
59 |
self.pango_layout = self.create_pango_layout(text or "") |
|
60 |
if markup: self.pango_layout.set_markup(markup) |
|
61 |
self.pango_layout.set_wrap(pango.WRAP_WORD_CHAR) |
|
62 |
||
37
by segphault
Change cursor to hand when the user hovers over a link (bug #193943) |
63 |
self.ev = gtk.EventBox() |
64 |
self.ev.set_visible_window(False) |
|
65 |
self.add(self.ev) |
|
19
by segphault
Merged the rewrite branch into trunk |
66 |
|
67 |
def do_expose_event(self, event): |
|
68 |
gtk.Frame.do_expose_event(self, event) |
|
69 |
self.set_size_request(-1, (self.pango_layout.get_size()[1] // pango.SCALE + 10)) |
|
70 |
||
71 |
x,y,w,h = self.allocation |
|
72 |
||
73 |
gc = self.window.new_gc() |
|
74 |
self.pango_layout.set_width(w * pango.SCALE) |
|
24
by segphault
Added support for configurable text shadows |
75 |
|
76 |
if self.shadow: |
|
77 |
shadow = self.pango_layout.copy() |
|
78 |
attrs = shadow.get_attributes() |
|
79 |
attrs.change(pango.AttrForeground(self.shadow.red, self.shadow.green, self.shadow.blue, 0, -1)) |
|
80 |
shadow.set_attributes(attrs) |
|
81 |
self.window.draw_layout(gc, x + 1, y + 1, shadow) |
|
82 |
||
19
by segphault
Merged the rewrite branch into trunk |
83 |
self.window.draw_layout(gc, x, y, self.pango_layout) |
84 |
||
85 |
gobject.type_register(WrapLabel) |
|
86 |
||
87 |
class RoundImage(gtk.Image): |
|
88 |
def __init__(self, radius = 15): |
|
89 |
self.radius = 15 |
|
90 |
gtk.Image.__init__(self) |
|
91 |
||
92 |
def do_expose_event(self, event): |
|
93 |
x, y, w, h = self.allocation |
|
94 |
i = self.get_pixbuf() |
|
95 |
c = self.window.cairo_create() |
|
96 |
||
97 |
#c.set_source_rgba(0, 0, 0, 0.5)
|
|
98 |
#draw_round_rect(c, 15, x + 3, y + 3, i.get_width(), i.get_height())
|
|
99 |
#c.fill()
|
|
100 |
||
101 |
c.set_source_pixbuf(i, x, y) |
|
102 |
draw_round_rect(c, self.radius, x, y, i.get_width(), i.get_height()) |
|
103 |
c.fill() |
|
104 |
||
105 |
gobject.type_register(RoundImage) |
|
106 |
||
48
by segphault
Cleanup and compositing window support in Glitter |
107 |
class CompositedWindow(gtk.Window): |
108 |
def __init__(self, *a): |
|
109 |
gtk.Window.__init__(self, *a) |
|
110 |
||
111 |
self.connect("expose-event", self.on_expose) |
|
112 |
self.connect("screen-changed", self.screen_changed) |
|
113 |
self.set_app_paintable(True) |
|
114 |
self.screen_changed(self) |
|
115 |
||
116 |
def screen_changed(self, w, old_screen=None): |
|
117 |
screen = w.get_screen() |
|
118 |
cma = screen.get_rgba_colormap() |
|
119 |
w.set_colormap(cma) |
|
120 |
||
121 |
def on_expose(self, w, e): |
|
122 |
cr = w.window.cairo_create() |
|
123 |
cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) |
|
124 |
cr.set_operator(cairo.OPERATOR_SOURCE) |
|
125 |
cr.paint() |
|
126 |
||
19
by segphault
Merged the rewrite branch into trunk |
127 |
if __name__ == "__main__": |
48
by segphault
Cleanup and compositing window support in Glitter |
128 |
w = CompositedWindow() |
19
by segphault
Merged the rewrite branch into trunk |
129 |
w.connect("destroy", gtk.main_quit) |
130 |
f = Frame(bgcolor="red", transparency=0.8) |
|
131 |
l = WrapLabel(markup="<b>This is a test</b>") |
|
132 |
f.add(l) |
|
133 |
w.add(f) |
|
134 |
w.show_all() |
|
135 |
gtk.main() |
|
136 |