Guest User

.box.lua

a guest
Aug 14th, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.45 KB | None | 0 0
  1. --[[BOX WIDGET v1.1 by Wlourf 27/01/2011
  2. This widget can drawn some boxes, even circles in your conky window
  3. http://u-scripts.blogspot.com/2011/01/box-widget.html)
  4.  
  5. Inspired by Background by londonali1010 (2009), thanks ;-)
  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 = height of the conky window
  12. corners     - corners is a table for the four corners in this order : top-left, top-right,bottom-right, bottom-left
  13.               each corner is defined in a table with a shape and a radius, available shapes are : "curve","circle","line"
  14.               example for the same shapes for all corners:
  15.               { {"circle",10} }
  16.               example for first corner different from the three others
  17.               { {"circle",10}, {"circle",5}  }              
  18.               example for top corners differents from bottom corners
  19.               { {"circle",10}, {"circle",10}, {"line",0}  }  
  20.               default = { {"line",0} } i.e=no corner
  21. operator    - set the compositing operator (needs in the conkyrc : own_window_argb_visual yes)                          
  22.               see http://cairographics.org/operators/
  23.               available operators are :
  24.               "clear","source","over","in","out","atop","dest","dest_over","dest_in","dest_out","dest_atop","xor","add","saturate"
  25.               default = "over"
  26. border      - if border>0, the script draws only the border, like a frame, default=0
  27. dash        - if border>0 and dash>0, the border is draw with dashes, default=0
  28. skew_x      - skew box around x axis, default = 0
  29. skew_y      - skew box around y axis, default = 0
  30. scale_x     - rescale the x axis, default=1, useful for drawing elipses ...
  31. scale_y     - rescale the x axis, default=1
  32. angle       - angle of rotation of the box in degrees, default = 0
  33.               i.e. a horizontal graph
  34. rot_x       - x point of rotation's axis, default = 0,
  35.               relative to top-left corner of the box, (not the conky window)
  36. rot_y       - y point of rotation's axis, default = 0              
  37.               relative to top-left corner of the box, (not the conky window)
  38. draw_me     - if set to false, box is not drawn (default = true or 1)
  39.               it can be used with a conky string, if the string returns 1, the box is drawn :
  40.               example : "${if_empty ${wireless_essid wlan0}}${else}1$endif",              
  41.              
  42. linear_gradient - table with the coordinates of two points to define a linear gradient,
  43.                   points are relative to top-left corner of the box, (not the conky window)
  44.                   {x1,y1,x2,y2}
  45. radial_gradient - table with the coordinates of two circle to define a radial gradient,
  46.                   points are relative to top-left corner of the box, (not the conky window)
  47.                   {x1,y1,r1,x2,y2,r2} (r=radius)
  48. colour      - table of colours, default = plain white {{1,0xFFFFFF,0.5}}
  49.               this table contains one or more tables with format {P,C,A}
  50.               P=position of gradient (0 = start of the gradient, 1= end of the gradient)
  51.               C=hexadecimal colour
  52.               A=alpha (opacity) of color (0=invisible,1=opacity 100%)
  53.               Examples :
  54.               for a plain color {{1,0x00FF00,0.5}}
  55.               for a gradient with two colours {{0,0x00FF00,0.5},{1,0x000033,1}}        {x=80,y=150,w=20,h=20,
  56.         radial_gradient={20,20,0,20,20,20},
  57.         colour={{0.5,0xFFFFFF,1},{1,0x000000,0}},
  58.               or {{0.5,0x00FF00,1},{1,0x000033,1}} -with this one, gradient will start in the middle
  59.               for a gradient with three colours {{0,0x00FF00,0.5},{0.5,0x000033,1},{1,0x440033,1}}
  60.               and so on ...
  61.  
  62.  
  63.  
  64. To call this script in Conky, use (assuming you have saved this script to ~/scripts/):
  65.     lua_load ~/scripts/box.lua
  66.     lua_draw_hook_pre main_box
  67.    
  68. And leave one line blank or not after TEXT
  69.  
  70. Changelog:
  71. + v1.0 -- Original release (19.12.2010)
  72. + v1.1 -- Adding parameters: operator, dash, angle, skew_x, skew_y, draw_me
  73.           corners are described in a table
  74.  
  75. --      This program is free software; you can redistribute it and/or modify
  76. --      it under the terms of the GNU General Public License as published by
  77. --      the Free Software Foundation version 3 (GPLv3)
  78. --    
  79. --      This program is distributed in the hope that it will be useful,
  80. --      but WITHOUT ANY WARRANTY; without even the implied warranty of
  81. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  82. --      GNU General Public License for more details.
  83. --    
  84. --      You should have received a copy of the GNU General Public License
  85. --      along with this program; if not, write to the Free Software
  86. --      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  87. --      MA 02110-1301, USA.    
  88.  
  89.  
  90. ]]
  91.  
  92.  
  93. require 'cairo'
  94.    
  95. function conky_main_box()
  96.  
  97.     if conky_window==nil then return end
  98.    
  99.     ---------------------- PARAMETERS BEGIN HERE
  100.     local boxes_settings={
  101.         {
  102.             x=0,
  103.             y=0,
  104.             w=450,
  105.             h=750,
  106.             corners={{"circle",20}},
  107.             colour={{0,0x000000,0.5}},
  108.         }
  109.  
  110.     }
  111.  
  112.  
  113.     ---------------------------- PARAMETERS END HERE
  114.    
  115.     local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  116.     local cr=cairo_create(cs)
  117.    
  118.     if tonumber(conky_parse("$updates"))<5 then return end
  119.     for i in pairs(boxes_settings) do
  120.         draw_box (cr,boxes_settings[i])
  121.     end
  122.     cairo_destroy(cr)
  123.     cairo_surface_destroy(cs)    
  124. end
  125.  
  126.    
  127. function draw_box(cr,t)
  128.  
  129.     if t.draw_me == true then t.draw_me = nil end
  130.     if t.draw_me ~= nil and conky_parse(tostring(t.draw_me)) ~= "1" then return end
  131.  
  132.     local table_corners={"circle","curve","line"}
  133.  
  134.     local t_operators={
  135.         clear   = CAIRO_OPERATOR_CLEAR,
  136.         source  = CAIRO_OPERATOR_SOURCE,
  137.         over    = CAIRO_OPERATOR_OVER,
  138.         ["in"]      = CAIRO_OPERATOR_IN,
  139.         out     = CAIRO_OPERATOR_OUT,
  140.         atop    = CAIRO_OPERATOR_ATOP,
  141.         dest    = CAIRO_OPERATOR_DEST,
  142.         dest_over   = CAIRO_OPERATOR_DEST_OVER,
  143.         dest_in = CAIRO_OPERATOR_DEST_IN,
  144.         dest_out = CAIRO_OPERATOR_DEST_OUT,
  145.         dest_atop = CAIRO_OPERATOR_DEST_ATOP,
  146.         xor = CAIRO_OPERATOR_XOR,
  147.         add = CAIRO_OPERATOR_ADD,
  148.         saturate =  CAIRO_OPERATOR_SATURATE,
  149.     }
  150.        
  151.     function rgba_to_r_g_b_a(tc)
  152.         --tc={position,colour,alpha}
  153.         local colour = tc[2]
  154.         local alpha = tc[3]
  155.         return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  156.     end
  157.  
  158.     function table.copy(t)
  159.       local t2 = {}
  160.       for k,v in pairs(t) do
  161.        t2[k] = {v[1],v[2]}
  162.       end
  163.       return t2
  164.     end
  165.  
  166.     function draw_corner(num,t)
  167.         local shape=t[1]
  168.         local radius=t[2]
  169.         local x,y = t[3],t[4]
  170.         if shape=="line" then
  171.             if num == 1 then cairo_line_to(cr,radius,0)
  172.                 elseif num == 2 then cairo_line_to(cr,x,radius)
  173.                 elseif num == 3 then cairo_line_to(cr,x-radius,y)
  174.                 elseif num == 4 then cairo_line_to(cr,0,y-radius)
  175.             end
  176.         end
  177.         if shape=="circle" then
  178.             local PI = math.pi
  179.            if num == 1 then cairo_arc(cr,radius,radius,radius,-PI,-PI/2)
  180.                 elseif num == 2 then cairo_arc(cr,x-radius,y+radius,radius,-PI/2,0)
  181.                 elseif num == 3 then cairo_arc(cr,x-radius,y-radius,radius,0,PI/2)
  182.                 elseif num == 4 then cairo_arc(cr,radius,y-radius,radius,PI/2,-PI)
  183.             end
  184.         end
  185.         if shape=="curve" then
  186.             if num == 1 then cairo_curve_to(cr,0,radius ,0,0 ,radius,0)
  187.                 elseif num == 2 then cairo_curve_to(cr,x-radius,0, x,y, x,radius)
  188.                 elseif num == 3 then cairo_curve_to(cr,x,y-radius, x,y, x-radius,y)
  189.                 elseif num == 4 then cairo_curve_to(cr,radius,y, x,y, 0,y-radius)
  190.             end
  191.         end        
  192.     end  
  193.  
  194.     --check values and set default values
  195.     if t.x == nil then t.x = 0 end
  196.     if t.y == nil then t.y = 0 end
  197.     if t.w == nil then t.w = conky_window.width end
  198.     if t.h == nil then t.h = conky_window.height end
  199.     if t.radius == nil then t.radius = 0 end
  200.     if t.border == nil then t.border = 0 end
  201.     if t.colour==nil then t.colour={{1,0xFFFFFF,0.5}} end
  202.     if t.linear_gradient ~= nil then
  203.         if #t.linear_gradient ~= 4 then
  204.             t.linear_gradient = {t.x,t.y,t.width,t.height}
  205.         end
  206.     end
  207.     if t.angle==nil then t.angle = 0 end
  208.  
  209.     if t.skew_x == nil then t.skew_x=0  end
  210.     if t.skew_y == nil then  t.skew_y=0 end
  211.     if t.scale_x==nil then t.scale_x=1 end
  212.     if t.scale_y==nil then t.scale_y=1 end 
  213.     if t.rot_x == nil then t.rot_x=0  end
  214.     if t.rot_y == nil then  t.rot_y=0 end
  215.    
  216.     if t.operator == nil then t.operator = "over" end
  217.     if (t_operators[t.operator]) == nil then
  218.         print ("wrong operator :",t.operator)
  219.         t.operator = "over"
  220.     end
  221.    
  222.     if t.radial_gradient ~= nil then
  223.         if #t.radial_gradient ~= 6 then
  224.             t.radial_gradient = {t.x,t.y,0, t.x,t.y, t.width}
  225.         end
  226.     end
  227.    
  228.     for i=1, #t.colour do    
  229.         if #t.colour[i]~=3 then
  230.             print ("error in color table")
  231.             t.colour[i]={1,0xFFFFFF,1}
  232.         end
  233.     end
  234.  
  235.     if t.corners == nil then t.corners={ {"line",0} } end
  236.     local t_corners = {}
  237.     local t_corners = table.copy(t.corners)
  238.     --don't use t_corners=t.corners otherwise t.corners is altered
  239.  
  240.     --complete the t_corners table if needed
  241.     for i=#t_corners+1,4 do    
  242.         t_corners[i]=t_corners[#t_corners]
  243.         local flag=false
  244.         for j,v in pairs(table_corners) do flag=flag or (t_corners[i][1]==v) end
  245.         if not flag then print ("error in corners table :",t_corners[i][1]);t_corners[i][1]="curve"  end
  246.     end
  247.  
  248.     --this way :    
  249.     --    t_corners[1][4]=x    
  250.     --    t_corners[2][3]=y
  251.     --doesn't work
  252.     t_corners[1]={t_corners[1][1],t_corners[1][2],0,0}
  253.     t_corners[2]={t_corners[2][1],t_corners[2][2],t.w,0}
  254.     t_corners[3]={t_corners[3][1],t_corners[3][2],t.w,t.h}    
  255.     t_corners[4]={t_corners[4][1],t_corners[4][2],0,t.h}        
  256.  
  257.     t.no_gradient = (t.linear_gradient == nil ) and (t.radial_gradient == nil )
  258.  
  259.     cairo_save(cr)
  260.     cairo_translate(cr, t.x, t.y)
  261.     if t.rot_x~=0 or t.rot_y~=0 or t.angle~=0 then
  262.         cairo_translate(cr,t.rot_x,t.rot_y)
  263.         cairo_rotate(cr,t.angle*math.pi/180)
  264.         cairo_translate(cr,-t.rot_x,-t.rot_y)
  265.     end
  266.     if t.scale_x~=1 or t.scale_y~=1 or t.skew_x~=0 or t.skew_y~=0 then
  267.         local matrix0 = cairo_matrix_t:create()
  268.         tolua.takeownership(matrix0)
  269.         cairo_matrix_init (matrix0, t.scale_x,math.pi*t.skew_y/180  , math.pi*t.skew_x/180  ,t.scale_y,0,0)
  270.         cairo_transform(cr,matrix0)    
  271.     end
  272.    
  273.     local tc=t_corners
  274.     cairo_move_to(cr,tc[1][2],0)
  275.     cairo_line_to(cr,t.w-tc[2][2],0)
  276.     draw_corner(2,tc[2])
  277.     cairo_line_to(cr,t.w,t.h-tc[3][2])
  278.     draw_corner(3,tc[3])
  279.     cairo_line_to(cr,tc[4][2],t.h)
  280.     draw_corner(4,tc[4])
  281.     cairo_line_to(cr,0,tc[1][2])
  282.     draw_corner(1,tc[1])
  283.    
  284.     if t.no_gradient then
  285.         cairo_set_source_rgba(cr,rgba_to_r_g_b_a(t.colour[1]))
  286.     else
  287.         if t.linear_gradient ~= nil then
  288.             pat = cairo_pattern_create_linear (t.linear_gradient[1],t.linear_gradient[2],t.linear_gradient[3],t.linear_gradient[4])
  289.         elseif t.radial_gradient ~= nil then
  290.             pat = cairo_pattern_create_radial (t.radial_gradient[1],t.radial_gradient[2],t.radial_gradient[3],
  291.                 t.radial_gradient[4],t.radial_gradient[5],t.radial_gradient[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.     cairo_set_operator(cr,t_operators[t.operator])
  301.  
  302.     if t.border>0 then
  303.         cairo_close_path(cr)
  304.         if t.dash ~= nil then cairo_set_dash(cr, t.dash, 1, 0.0) end
  305.         cairo_set_line_width(cr,t.border)
  306.         cairo_stroke(cr)
  307.     else
  308.         cairo_fill(cr)
  309.     end
  310.  
  311.     cairo_restore(cr)
  312. end
Advertisement
Add Comment
Please, Sign In to add comment