Remove "encoding=utf-8" from Vim modelines
[awesome.git] / lib / awful / widget / progressbar.lua.in
blobf2ff6388abe604fcb173d26f778237c168ab92db
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local setmetatable = setmetatable
8 local ipairs = ipairs
9 local math = math
10 local capi = { image = image,
11 widget = widget }
12 local layout = require("awful.widget.layout")
14 --- A progressbar widget.
15 module("awful.widget.progressbar")
17 local data = setmetatable({}, { __mode = "k" })
19 --- Set the progressbar border color.
20 -- If the value is nil, no border will be drawn.
21 -- @name set_border_color
22 -- @class function
23 -- @param progressbar The progressbar.
24 -- @param color The border color to set.
26 --- Set the progressbar foreground color as a gradient.
27 -- @name set_gradient_colors
28 -- @class function
29 -- @param progressbar The progressbar.
30 -- @param gradient_colors A table with gradients colors. The distance between each color
31 -- can also be specified. Example: { "red", "blue" } or { "red", "green",
32 -- "blue", blue = 10 } to specify blue distance from other colors.
34 --- Set the progressbar foreground color.
35 -- @name set_color
36 -- @class function
37 -- @param progressbar The progressbar.
38 -- @param color The progressbar color.
40 --- Set the progressbar background color.
41 -- @name set_background_color
42 -- @class function
43 -- @param progressbar The progressbar.
44 -- @param color The progressbar background color.
46 --- Set the progressbar to draw vertically. Default is false.
47 -- @name set_vertical
48 -- @class function
49 -- @param progressbar The progressbar.
50 -- @param vertical A boolean value.
52 --- Set the progressbar to draw ticks. Default is false.
53 -- @name set_ticks
54 -- @class function
55 -- @param progressbar The progressbar.
56 -- @param ticks A boolean value.
58 --- Set the progressbar ticks gap.
59 -- @name set_ticks_gap
60 -- @class function
61 -- @param progressbar The progressbar.
62 -- @param value The value.
64 --- Set the progressbar ticks size.
65 -- @name set_ticks_size
66 -- @class function
67 -- @param progressbar The progressbar.
68 -- @param value The value.
70 --- Set the maximum value the progressbar should handle.
71 -- @name set_max_value
72 -- @class function
73 -- @param progressbar The progressbar.
74 -- @param value The value.
76 local properties = { "width", "height", "border_color",
77 "gradient_colors", "color", "background_color",
78 "vertical", "value", "max_value",
79 "ticks", "ticks_gap", "ticks_size" }
81 local function update(pbar)
82 local width = data[pbar].width or 100
83 local height = data[pbar].height or 20
84 local ticks_gap = data[pbar].ticks_gap or 1
85 local ticks_size = data[pbar].ticks_size or 4
87 -- Create new empty image
88 local img = capi.image.argb32(width, height, nil)
90 local value = data[pbar].value
91 local max_value = data[pbar].max_value
92 if value >= 0 then
93 value = value / max_value
94 end
96 local over_drawn_width = width
97 local over_drawn_height = height
98 local border_width = 0
99 if data[pbar].border_color then
100 -- Draw border
101 img:draw_rectangle(0, 0, width, height, false, data[pbar].border_color)
102 over_drawn_width = width - 2 -- remove 2 for borders
103 over_drawn_height = height - 2 -- remove 2 for borders
104 border_width = 1
107 local angle = 270
108 if data[pbar].vertical then
109 angle = 180
112 -- Draw full gradient
113 if data[pbar].gradient_colors then
114 img:draw_rectangle_gradient(border_width, border_width,
115 over_drawn_width, over_drawn_height,
116 data[pbar].gradient_colors, angle)
117 else
118 img:draw_rectangle(border_width, border_width,
119 over_drawn_width, over_drawn_height,
120 true, data[pbar].color or "red")
123 -- Cover the part that is not set with a rectangle
124 if data[pbar].vertical then
125 local rel_height = math.floor(over_drawn_height * (1 - value))
126 img:draw_rectangle(border_width,
127 border_width,
128 over_drawn_width,
129 rel_height,
130 true, data[pbar].background_color or "#000000aa")
132 -- Place smaller pieces over the gradient if ticks are enabled
133 if data[pbar].ticks then
134 for i=0, height / (ticks_size+ticks_gap)-border_width do
135 local rel_offset = over_drawn_height / 1 - (ticks_size+ticks_gap) * i
137 if rel_offset >= rel_height then
138 img:draw_rectangle(border_width,
139 rel_offset,
140 over_drawn_width,
141 ticks_gap,
142 true, data[pbar].background_color or "#000000aa")
146 else
147 local rel_x = math.ceil(over_drawn_width * value)
148 img:draw_rectangle(border_width + rel_x,
149 border_width,
150 over_drawn_width - rel_x,
151 over_drawn_height,
152 true, data[pbar].background_color or "#000000aa")
154 if data[pbar].ticks then
155 for i=0, width / (ticks_size+ticks_gap)-border_width do
156 local rel_offset = over_drawn_width / 1 - (ticks_size+ticks_gap) * i
158 if rel_offset <= rel_x then
159 img:draw_rectangle(rel_offset,
160 border_width,
161 ticks_gap,
162 over_drawn_height,
163 true, data[pbar].background_color or "#000000aa")
169 -- Update the image
170 pbar.widget.image = img
173 --- Set the progressbar value.
174 -- @param pbar The progress bar.
175 -- @param value The progress bar value between 0 and 1.
176 function set_value(pbar, value)
177 local value = value or 0
178 local max_value = data[pbar].max_value
179 data[pbar].value = math.min(max_value, math.max(0, value))
180 update(pbar)
181 return pbar
184 --- Set the progressbar height.
185 -- @param progressbar The progressbar.
186 -- @param height The height to set.
187 function set_height(progressbar, height)
188 data[progressbar].height = height
189 update(progressbar)
190 return progressbar
193 --- Set the progressbar width.
194 -- @param progressbar The progressbar.
195 -- @param width The width to set.
196 function set_width(progressbar, width)
197 data[progressbar].width = width
198 update(progressbar)
199 return progressbar
202 -- Build properties function
203 for _, prop in ipairs(properties) do
204 if not _M["set_" .. prop] then
205 _M["set_" .. prop] = function(pbar, value)
206 data[pbar][prop] = value
207 update(pbar)
208 return pbar
213 --- Create a progressbar widget.
214 -- @param args Standard widget() arguments. You should add width and height
215 -- key to set progressbar geometry.
216 -- @return A progressbar widget.
217 function new(args)
218 local args = args or {}
219 local width = args.width or 100
220 local height = args.height or 20
222 args.type = "imagebox"
224 local pbar = {}
226 pbar.widget = capi.widget(args)
227 pbar.widget.resize = false
229 data[pbar] = { width = width, height = height, value = 0, max_value = 1 }
231 -- Set methods
232 for _, prop in ipairs(properties) do
233 pbar["set_" .. prop] = _M["set_" .. prop]
236 pbar.layout = args.layout or layout.horizontal.leftright
238 return pbar
241 setmetatable(_M, { __call = function(_, ...) return new(...) end })
243 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80