dd95c274f097c4da8b26746851ac9417a9e9c623
[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=610,
58         y=100,
59         h=86,
60         w=240,
61         linear_gradient = {300,200,550,350},
62         colour = {{0,0x000000,0.2},{1,0x000000,0.1}},
63         radius=20,
64     },
65     {   
66         x=350,
67         y=100,
68         h=86,
69         w=250,
70         linear_gradient = {300,200,550,350},
71         colour = {{0,0x000000,0.2},{1,0x000000,0.1}},
72         radius=20,
73     },
74     {   
75         x=5,
76         y=191,
77         h=44,
78         w=845,
79         linear_gradient = {300,200,550,350},
80         colour = {{0,0x000000,0.4},{1,0x000000,0.1}},
81         radius=20,
82     },
83     {   
84         x=5,
85         y=357,
86         h=123,
87         w=845,
88         colour = {{0,0x000000,0.25}},
89         radius=20,
90      },
91     {   
92         x=5,
93         y=241,
94         h=112,
95         w=845,
96         colour = {{0,0x000000,0.4}},
97         radius=20,
98     },
99
100 --[[
101     { --backgound with gradient
102         radius=25,
103         mode="circle",
104         linear_gradient = {300,200,550,350},
105         colour={{0,0xCCCCCC,1},{1,0xCCCCCC,0}}
106     },
107
108
109
110
111     { --pink rounded box
112         x=25,
113         y=150,
114         h=200,
115         w=100,
116         colour = {{O,0xff00ff,0.5}},
117         radius=30,
118         mode="circle"
119      }, 
120     { --border for pink rounded box
121         x=25,
122         y=150,
123         h=200,
124         w=100,
125         radius=30,
126         border=3,
127         mode="circle",
128         colour={
129                 {0,0x0000CC,1},
130                 },    
131          },
132      
133     {  --box with linear gradient
134         x=150,
135         y=150,
136         h=100,
137         w=100,
138         linear_gradient = {50,0,50,100 },
139         colour={
140                     {0,0xffff00,1},
141                     {0.5,0xff0000,1},                
142                     {1,0xff00ff,1},
143         },   
144      },
145      
146      { --box with radial gradient
147         x=150,
148         y=270,
149         h=100,
150         w=100,
151         radius=10,
152         radial_gradient = {20,20,0,20,20,100 },
153         colour={
154                     {0,0xff0000,1},
155                     {1,0xffff00,1},
156                     },   
157         mode="circle",
158         border=0
159      },
160     { --border for above box --gradient are inversed
161         x=150,
162         y=270,
163         h=100,
164         w=100,
165         radius=10,
166         radial_gradient = {20,20,0,20,20,100 },
167         colour={
168                     {1,0xff0000,1},
169                     {0,0xffff00,1},
170                     },   
171         mode="circle",
172         border=5
173      },
174
175
176     { --oh my god, a circle with radial gradient
177         x=300, y=30,
178         w=100,h=100,
179         mode="circle",
180         radius=50,
181         radial_gradient = {50,50,0,50,50,50 },        
182         colour={
183             {0,0xff0000,1},
184             {1,0xffff00,1},
185             },         
186     },
187
188     { --no name for this one ! radius > w or h !
189         x=300, y=250,
190         w=100,h=100,
191         mode="circle",
192         radius=100,
193         radial_gradient = {50,50,0,50,50,50 },        
194         colour={
195             {0,0xff0000,1},
196             {0.5,0x0000ff,1},            
197             {1,0xffff00,1},
198             }, 
199     },
200 ]]
201
202
203   }
204
205 ---------END OF PARAMETERS
206     
207 require 'cairo'
208
209
210     
211 function conky_main_box()
212     if conky_window==nil then return end
213     local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
214     cr=cairo_create(cs)
215     
216     for i in pairs(table_settings) do
217         draw_bg (table_settings[i])
218     end
219     
220     cairo_destroy(cr)
221     cairo_surface_destroy(cs)    
222 end
223     
224 function draw_bg(t)
225     function rgba_to_r_g_b_a(tc)
226         --tc={position,colour,alpha}
227         local colour = tc[2]
228         local alpha = tc[3]
229         return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
230     end
231
232     local PI = math.pi
233
234     --check values and set default values
235     if t.x == nil then t.x = 0 end
236     if t.y == nil then t.y = 0 end
237     if t.w == nil then t.w = conky_window.width end
238     if t.h == nil then t.h = conky_window.height end
239     if t.radius == nil then t.radius = 0 end
240     if t.border == nil then t.border = 0 end
241     if t.colour==nil then t.colour={{1,0xFFFFFF,1}} end
242     if t.linear_gradient ~= nil then 
243         if #t.linear_gradient ~= 4 then
244             t.linear_gradient = {t.x,t.y,t.width,t.height}
245         end
246     end 
247     if t.radial_gradient ~= nil then 
248         if #t.radial_gradient ~= 6 then
249             t.radial_gradient = {t.x,t.y,0, t.x,t.y, t.width}
250         end
251     end 
252     
253     for i=1, #t.colour do    
254         if #t.colour[i]~=3 then 
255             print ("error in color table")
256             t.colour[i]={1,0xFFFFFF,1} 
257         end
258     end
259
260     --for better reading
261     t.linear = t.linear_gradient
262     t.radial = t.radial_gradient
263     t.no_gradient = (t.linear == nil ) and (t.radial == nil )
264
265     cairo_save(cr)
266     cairo_translate(cr, t.x, t.y)
267
268     if t.radius>0 then
269         if t.mode=="circle" then
270             cairo_arc(cr, t.radius, t.radius, t.radius, -PI, -PI/2)
271             cairo_line_to(cr,t.w-t.radius,0)
272             cairo_arc(cr, t.w-t.radius, t.radius, t.radius, -PI/2,0)
273             cairo_line_to(cr,t.w,t.h-t.radius)
274             cairo_arc(cr, t.w-t.radius, t.h-t.radius, t.radius, 0,PI/2)
275             cairo_line_to(cr,t.radius,t.h)
276             cairo_arc(cr, t.radius, t.h-t.radius, t.radius, PI/2,-PI)        
277             cairo_line_to(cr,0,t.radius) 
278         else
279             cairo_move_to(cr,   t.radius,     0)
280             cairo_line_to(cr,   t.w-t.radius, 0)
281             cairo_curve_to(cr,  t.w,    0,  t.w,    0,  t.w,    t.radius)
282             cairo_line_to(cr,   t.w,    t.h-t.radius)
283             cairo_curve_to(cr,  t.w,    t.h,    t.w,    t.h,    t.w -   t.radius, t.h)
284             cairo_line_to(cr,   t.radius, t.h)
285             cairo_curve_to(cr,  0,  t.h,    0,  t.h,    0,  t.h-t.radius)
286             cairo_line_to(cr,   0,  t.radius)
287             cairo_curve_to(cr,  0,  0,  0,  0,  t.radius,0)
288         end
289         cairo_close_path(cr)
290     else
291         cairo_rectangle(cr,0,0,t.w,t.h)
292     end
293     
294     if t.no_gradient then
295         cairo_set_source_rgba(cr,rgba_to_r_g_b_a(t.colour[1]))
296     else
297         if t.linear ~= nil then
298             pat = cairo_pattern_create_linear (t.linear[1],t.linear[2],t.linear[3],t.linear[4])
299         elseif t.radial ~= nil then
300             pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
301         end
302         for i=1, #t.colour do
303             cairo_pattern_add_color_stop_rgba (pat, t.colour[i][1], rgba_to_r_g_b_a(t.colour[i]))
304         end
305         cairo_set_source (cr, pat)
306         cairo_pattern_destroy(pat)
307     end
308     
309     if t.border>0 then
310         cairo_set_line_width(cr,t.border)
311         cairo_stroke(cr)
312     else
313         cairo_fill(cr)
314     end
315     
316     cairo_restore(cr)
317 end
318