| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
local math = math |
| 20 |
local os = os |
| 21 |
local string = string |
| 22 |
local table = table |
| 23 |
local tostring = tostring |
| 24 |
|
| 25 |
local capi = { |
| 26 |
client = client, |
| 27 |
mouse = mouse, |
| 28 |
wibox = wibox, |
| 29 |
widget = widget, |
| 30 |
} |
| 31 |
|
| 32 |
local awful = { |
| 33 |
button = require("awful.button"), |
| 34 |
placement = require("awful.placement"), |
| 35 |
util = require("awful.util"), |
| 36 |
widget = require("awful.widget"), |
| 37 |
} |
| 38 |
|
| 39 |
local flaw = { |
| 40 |
gadget = require('flaw.gadget'), |
| 41 |
provider = require('flaw.provider'), |
| 42 |
helper = require('flaw.helper'), |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
module('flaw.calendar') |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
CalendarProvider = flaw.provider.Provider:new{ type = _NAME } |
| 92 |
|
| 93 |
function CalendarProvider:subscribe(g) |
| 94 |
if flaw.provider.Provider.subscribe(self, g) then |
| 95 |
|
| 96 |
g:update() |
| 97 |
return true |
| 98 |
end |
| 99 |
return false |
| 100 |
end |
| 101 |
|
| 102 |
function CalendarProvider:set_month(m) |
| 103 |
self.data.month = self.data.month + (m or 1) |
| 104 |
self:build_simple_view() |
| 105 |
end |
| 106 |
|
| 107 |
function CalendarProvider:build_simple_view() |
| 108 |
local t = os.time{ year = self.data.year, month = self.data.month + 1, day = 0 } |
| 109 |
local d = os.date('*t', t) |
| 110 |
local mthDays, stDay= d.day, (d.wday - d.day) % 7 |
| 111 |
local lines = {} |
| 112 |
|
| 113 |
for x = 0, 6 do |
| 114 |
lines[x+1] = os.date( |
| 115 |
"%a ", os.time{ year = 2006, month = 1, day = x + 1 }) |
| 116 |
end |
| 117 |
lines[8] = " " |
| 118 |
|
| 119 |
local writeLine = 1 |
| 120 |
while writeLine < (stDay + 1) do |
| 121 |
lines[writeLine] = lines[writeLine] .. " " |
| 122 |
writeLine = writeLine + 1 |
| 123 |
end |
| 124 |
|
| 125 |
for x = 1, mthDays do |
| 126 |
if writeLine == 8 then |
| 127 |
writeLine = 1 |
| 128 |
end |
| 129 |
if writeLine == 1 or x == 1 then |
| 130 |
lines[8] = lines[8] .. os.date( |
| 131 |
" %V",os.time{ year = self.data.year, month = self.data.month, day = x }) |
| 132 |
end |
| 133 |
if (#(tostring(x)) == 1) then |
| 134 |
x = " " .. x |
| 135 |
end |
| 136 |
lines[writeLine] = lines[writeLine] .. " " .. x |
| 137 |
writeLine = writeLine + 1 |
| 138 |
end |
| 139 |
local header = os.date( |
| 140 |
"%B %Y\n", os.time{ year = self.data.year, month = self.data.month, day = 1 }) |
| 141 |
header = string.rep( |
| 142 |
" ", math.floor((#(lines[1]) - #header) / 2 )) .. header |
| 143 |
|
| 144 |
self.data.v_simple = string.format( |
| 145 |
'<span font_desc="%s">%s</span>', 'monospace', |
| 146 |
header .. table.concat(lines, '\n')) |
| 147 |
self:refresh_gadgets() |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
function provider_factory() |
| 157 |
local p = flaw.provider.get(_NAME, '') |
| 158 |
|
| 159 |
if p == nil then |
| 160 |
p = CalendarProvider:new{ |
| 161 |
id = '', data = { month = os.date('%m'), year = os.date('%Y'), |
| 162 |
v_simple = '', v_remind = '', v_events = '' } } |
| 163 |
p:build_simple_view() |
| 164 |
flaw.provider.add(p) |
| 165 |
end |
| 166 |
return p |
| 167 |
end |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
CalendarGadget = flaw.gadget.Gadget:new{} |
| 175 |
|
| 176 |
|
| 177 |
function CalendarGadget:create(wopt) |
| 178 |
self.widget = awful.widget.textclock( |
| 179 |
awful.util.table.join(wopt, { name = self.id }), self.clock_format) |
| 180 |
|
| 181 |
self.wibox = capi.wibox{} |
| 182 |
self.wibox.visible = false |
| 183 |
self.wibox.ontop = true |
| 184 |
self.wibox.widgets = { capi.widget{ type = "textbox", name = 'calendar' } } |
| 185 |
|
| 186 |
self.widget:add_signal("mouse::enter", function() self:popup() end) |
| 187 |
self.widget:add_signal("mouse::leave", function() self:collapse() end) |
| 188 |
self.wibox:add_signal("mouse::enter", function() self:collapse() end) |
| 189 |
|
| 190 |
self.widget:buttons( |
| 191 |
awful.util.table.join( |
| 192 |
awful.button({}, 4, function() self.provider:set_month(-1) end), |
| 193 |
awful.button({}, 5, function() self.provider:set_month(1) end), |
| 194 |
awful.button({ 'Shift' }, 4, function() self.provider:set_month(-12) end), |
| 195 |
awful.button({ 'Shift' }, 5, function() self.provider:set_month(12) end))) |
| 196 |
end |
| 197 |
|
| 198 |
function CalendarGadget:redraw() |
| 199 |
if self.provider ~= nil and self.provider.data ~= nil then |
| 200 |
self.wibox.widgets[1].text = self.provider.data.v_simple |
| 201 |
|
| 202 |
local size = self.wibox.widgets[1]:extents() |
| 203 |
self.wibox.width = size.width + 10 |
| 204 |
self.wibox.height = size.height + 10 |
| 205 |
end |
| 206 |
end |
| 207 |
|
| 208 |
function CalendarGadget:popup() |
| 209 |
if self.wibox.visible then return end |
| 210 |
|
| 211 |
self.wibox.screen = capi.mouse.screen |
| 212 |
awful.placement.under_mouse(self.wibox) |
| 213 |
awful.placement.no_offscreen(self.wibox) |
| 214 |
|
| 215 |
self.wibox.visible = true |
| 216 |
end |
| 217 |
|
| 218 |
function CalendarGadget:collapse() |
| 219 |
self.wibox.visible = false |
| 220 |
end |
| 221 |
|
| 222 |
flaw.gadget.register.text(_M, {}, {}, CalendarGadget) |