Remove all traces of awsetbg and wallpaper setters
[awesome.git] / lib / beautiful.lua.in
blobe8a800f5e3d91bf46db99c107ab46221f6ea864b
1 ----------------------------------------------------------------------------
2 -- @author Damien Leone <damien.leone@gmail.com>
3 -- @author Julien Danjou <julien@danjou.info>
4 -- @copyright 2008-2009 Damien Leone, Julien Danjou
5 -- @release @AWESOME_VERSION@
6 ----------------------------------------------------------------------------
8 -- Grab environment
9 local os = os
10 local print = print
11 local pcall = pcall
12 local pairs = pairs
13 local type = type
14 local dofile = dofile
15 local setmetatable = setmetatable
16 local util = require("awful.util")
17 local lgi = require("lgi")
18 local cairo = lgi.cairo
19 local Pango = lgi.Pango
20 local PangoCairo = lgi.PangoCairo
21 local capi =
23 screen = screen,
24 awesome = awesome
27 --- Theme library.
28 local beautiful = { mt = {} }
30 -- Local data
31 local theme
32 local descs = setmetatable({}, { __mode = 'k' })
33 local fonts = setmetatable({}, { __mode = 'v' })
34 local active_font
36 local function load_font(name)
37 name = name or active_font
38 if name and type(name) ~= "string" and descs[name] then
39 name = descs[name]
40 end
41 if fonts[name] then
42 return fonts[name]
43 end
44 -- load new font
45 local desc = Pango.FontDescription.from_string(name)
47 local ctx = PangoCairo.font_map_get_default():create_context()
48 local layout = Pango.Layout.new(ctx)
49 layout:set_font_description(desc)
51 local width, height = layout:get_pixel_size()
52 local font = { name = name, description = desc, height = height }
53 fonts[name] = font
54 descs[desc] = name
55 return font
56 end
58 local function set_font(name)
59 active_font = load_font(name).name
60 end
62 function beautiful.get_font(name)
63 return load_font(name).description
64 end
66 function beautiful.get_font_height(name)
67 return load_font(name).height
68 end
70 --- Init function, should be runned at the beginning of configuration file.
71 -- @param path The theme file path.
72 function beautiful.init(path)
73 if path then
74 local success
75 success, theme = pcall(function() return dofile(path) end)
77 if not success then
78 return print("E: beautiful: error loading theme file " .. theme)
79 elseif theme then
80 -- try and grab user's $HOME directory
81 local homedir = os.getenv("HOME")
82 -- expand '~'
83 if homedir then
84 for k, v in pairs(theme) do
85 if type(v) == "string" then theme[k] = v:gsub("~", homedir) end
86 end
87 end
89 if theme.font then set_font(theme.font) end
90 if theme.fg_normal then capi.awesome.fg = theme.fg_normal end
91 if theme.bg_normal then capi.awesome.bg = theme.bg_normal end
92 else
93 return print("E: beautiful: error loading theme file " .. path)
94 end
95 else
96 return print("E: beautiful: error loading theme: no path specified")
97 end
98 end
100 --- Get the current theme.
101 -- @return The current theme table.
102 function beautiful.get()
103 return theme
106 function beautiful.mt:__index(k)
107 return theme[k]
110 -- Set the default font
111 set_font("sans 8")
113 return setmetatable(beautiful, beautiful.mt)
115 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80