harmattan version.
[monky] / data / box.lua
1 --[[BOX WIDGET v1.0 by Wlourf 19/12/2010
2 This widget can drawn some boxes, even circles in your conky window
3 http://u-scripts.blogspot.com/ (HowTo coming soon)
4
5 Inspired by Background by londonali1010 (2009)
6
7 The parameters (all optionals) are :
8 x           - x coordinate of top-left corner of the box, default = 0 = (top-left corner of conky window)
9 y           - y coordinate of top-left corner of the box, default = 0 = (top-left corner of conky window)
10 w           - width of the box, default = width of the conky window
11 h           - height of the box, default = width of the conky window
12 radius      - radius of the corner, default = 0 = no radius
13 mode        - mode for drawing the radius, possible values are "circle" or "curve", default ="curve"
14 linear_gradient - table with the coordinates of two points to define a linear gradient,
15                   points are relative to top-left corner of the box, (not the conky window)
16                   {x1,y1,x2,y2}
17 radial_gradient - table with the coordinates of two circle to define a radial gradient,
18              colour={{0,0xCCCCCC,1},{1,0xCCCCCC,0}}     points are relative to top-left corner of the box, (not the conky window)
19                   {x1,y1,r1,x2,y2,r2} (r=radius)
20 colour      - table of colours, default = plain white {{1,0xFFFFFF,1}}
21               this table contains one or more tables with format {P,C,A}
22               P=position of gradient (0 = start of the gradient, 1= end of the gradient)
23               C=hexadecimal colour 
24               A=alpha (opacity) of color (0=invisible,1=opacity 100%)
25               Examples :
26               for a plain color {{1,0x00FF00,0.5}}
27               for a gradient with two colours {{0,0x00FF00,0.5},{1,0x000033,1}}
28               or {{0.5,0x00FF00,1},{1,0x000033,1}} -with this one, gradient will start in the middle
29               for a gradient with three colours {{0,0x00FF00,0.5},{0.5,0x000033,1},{1,0x440033,1}}
30               and so on ...
31
32
33
34 To call this script in Conky, use (assuming you have saved this script to ~/scripts/):
35     lua_load ~/scripts/draw_bg.lua
36     lua_draw_hook_pre main_box
37     
38 And leave one line blank or not after TEXT
39
40 Changelog:
41 + v1.0 -- Original release (19.12.2010)
42 ]]
43
44 -- Change these settings to affect your background.
45
46 table_settings={
47     {   
48         x=5,
49         y=57,
50         h=40,
51         w=650,
52         linear_gradient = {300,200,550,350},
53         colour = {{0,0x000000,0.5},{1,0x000000,0.5}},
54         radius=20,
55     },
56     {   
57         x=315,
58         y=100,
59         h=90,
60         w=485,
61         linear_gradient = {300,200,550,350},
62         colour = {{0,0x000000,0.2},{1,0x000000,0.1}},
63         radius=20,
64     },
65     {   
66         x=5,
67         y=192,
68         h=44,
69         w=795,
70         linear_gradient = {300,200,550,350},
71         colour = {{0,0x000000,0.4},{1,0x000000,0.1}},
72         radius=20,
73     },
74     {   
75         x=5,
76         y=355,
77         h=125,
78         w=795,
79         colour = {{0,0x000000,0.25}},
80         radius=20,
81      },
82     {   
83         x=5,
84         y=240,
85         h=110,
86         w=795,
87         colour = {{0,0x000000,0.4}},
88         radius=20,
89     },
90
91 --[[
92     { --backgound with gradient
93         radius=25,
94         mode="circle",
95         linear_gradient = {300,200,550,350},
96         colour={{0,0xCCCCCC,1},{1,0xCCCCCC,0}}
97     },
98
99
100
101
102     { --pink rounded box
103         x=25,
104         y=150,
105         h=200,
106         w=100,
107         colour = {{O,0xff00ff,0.5}},
108         radius=30,
109         mode="circle"
110      }, 
111     { --border for pink rounded box
112         x=25,
113         y=150,
114         h=200,
115         w=100,
116         radius=30,
117         border=3,
118         mode="circle",
119         colour={
120                 {0,0x0000CC,1},
121                 },    
122          },
123      
124     {  --box with linear gradient
125         x=150,
126         y=150,
127         h=100,
128         w=100,
129         linear_gradient = {50,0,50,100 },
130         colour={
131                     {0,0xffff00,1},
132                     {0.5,0xff0000,1},                
133                     {1,0xff00ff,1},
134         },   
135      },
136      
137      { --box with radial gradient
138         x=150,
139         y=270,
140         h=100,
141         w=100,
142         radius=10,
143         radial_gradient = {20,20,0,20,20,100 },
144         colour={
145                     {0,0xff0000,1},
146                     {1,0xffff00,1},
147                     },   
148         mode="circle",
149         border=0
150      },
151     { --border for above box --gradient are inversed
152         x=150,
153         y=270,
154         h=100,
155         w=100,
156         radius=10,
157         radial_gradient = {20,20,0,20,20,100 },
158         colour={
159                     {1,0xff0000,1},
160                     {0,0xffff00,1},
161                     },   
162         mode="circle",
163         border=5
164      },
165
166
167     { --oh my god, a circle with radial gradient
168         x=300, y=30,
169         w=100,h=100,
170         mode="circle",
171         radius=50,
172         radial_gradient = {50,50,0,50,50,50 },        
173         colour={
174             {0,0xff0000,1},
175             {1,0xffff00,1},
176             },         
177     },
178
179     { --no name for this one ! radius > w or h !
180         x=300, y=250,
181         w=100,h=100,
182         mode="circle",
183         radius=100,
184         radial_gradient = {50,50,0,50,50,50 },        
185         colour={
186             {0,0xff0000,1},
187             {0.5,0x0000ff,1},            
188             {1,0xffff00,1},
189             }, 
190     },
191 ]]
192
193
194   }
195
196 ---------END OF PARAMETERS
197     
198 require 'cairo'
199
200
201     
202 function conky_main_box()
203     if conky_window==nil then return end
204     local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
205     cr=cairo_create(cs)
206     
207     for i in pairs(table_settings) do
208         draw_bg (table_settings[i])
209     end
210     
211     cairo_destroy(cr)
212     cairo_surface_destroy(cs)    
213 end
214     
215 function draw_bg(t)
216     function rgba_to_r_g_b_a(tc)
217         --tc={position,colour,alpha}
218         local colour = tc[2]
219         local alpha = tc[3]
220         return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
221     end
222
223     local PI = math.pi
224
225     --check values and set default values
226     if t.x == nil then t.x = 0 end
227     if t.y == nil then t.y = 0 end
228     if t.w == nil then t.w = conky_window.width end
229     if t.h == nil then t.h = conky_window.height end
230     if t.radius == nil then t.radius = 0 end
231     if t.border == nil then t.border = 0 end
232     if t.colour==nil then t.colour={{1,0xFFFFFF,1}} end
233     if t.linear_gradient ~= nil then 
234         if #t.linear_gradient ~= 4 then
235             t.linear_gradient = {t.x,t.y,t.width,t.height}
236         end
237     end 
238     if t.radial_gradient ~= nil then 
239         if #t.radial_gradient ~= 6 then
240             t.radial_gradient = {t.x,t.y,0, t.x,t.y, t.width}
241         end
242     end 
243     
244     for i=1, #t.colour do    
245         if #t.colour[i]~=3 then 
246             print ("error in color table")
247             t.colour[i]={1,0xFFFFFF,1} 
248         end
249     end
250
251     --for better reading
252     t.linear = t.linear_gradient
253     t.radial = t.radial_gradient
254     t.no_gradient = (t.linear == nil ) and (t.radial == nil )
255
256     cairo_save(cr)
257     cairo_translate(cr, t.x, t.y)
258
259     if t.radius>0 then
260         if t.mode=="circle" then
261             cairo_arc(cr, t.radius, t.radius, t.radius, -PI, -PI/2)
262             cairo_line_to(cr,t.w-t.radius,0)
263             cairo_arc(cr, t.w-t.radius, t.radius, t.radius, -PI/2,0)
264             cairo_line_to(cr,t.w,t.h-t.radius)
265             cairo_arc(cr, t.w-t.radius, t.h-t.radius, t.radius, 0,PI/2)
266             cairo_line_to(cr,t.radius,t.h)
267             cairo_arc(cr, t.radius, t.h-t.radius, t.radius, PI/2,-PI)        
268             cairo_line_to(cr,0,t.radius) 
269         else
270             cairo_move_to(cr,   t.radius,     0)
271             cairo_line_to(cr,   t.w-t.radius, 0)
272             cairo_curve_to(cr,  t.w,    0,  t.w,    0,  t.w,    t.radius)
273             cairo_line_to(cr,   t.w,    t.h-t.radius)
274             cairo_curve_to(cr,  t.w,    t.h,    t.w,    t.h,    t.w -   t.radius, t.h)
275             cairo_line_to(cr,   t.radius, t.h)
276             cairo_curve_to(cr,  0,  t.h,    0,  t.h,    0,  t.h-t.radius)
277             cairo_line_to(cr,   0,  t.radius)
278             cairo_curve_to(cr,  0,  0,  0,  0,  t.radius,0)
279         end
280         cairo_close_path(cr)
281     else
282         cairo_rectangle(cr,0,0,t.w,t.h)
283     end
284     
285     if t.no_gradient then
286         cairo_set_source_rgba(cr,rgba_to_r_g_b_a(t.colour[1]))
287     else
288         if t.linear ~= nil then
289             pat = cairo_pattern_create_linear (t.linear[1],t.linear[2],t.linear[3],t.linear[4])
290         elseif t.radial ~= nil then
291             pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
292         end
293         for i=1, #t.colour do
294             cairo_pattern_add_color_stop_rgba (pat, t.colour[i][1], rgba_to_r_g_b_a(t.colour[i]))
295         end
296         cairo_set_source (cr, pat)
297         cairo_pattern_destroy(pat)
298     end
299     
300     if t.border>0 then
301         cairo_set_line_width(cr,t.border)
302         cairo_stroke(cr)
303     else
304         cairo_fill(cr)
305     end
306     
307     cairo_restore(cr)
308 end
309