Debian Codegraph @

flaw/calendar.lua

raw symbols SCIP
1 -- flaw, a Lua OO management framework for Awesome WM widgets.
2 -- Copyright (C) 2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
3
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
8
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
13
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 -- Grab environment.
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 --- A Calendar.
46 --
47 -- <p>This module contains a text gadget and a provider to display a
48 -- calendar. It was written thanks to the original calendar module
49 -- proposed at <a
50 -- href="http://awesome.naquadah.org/wiki/Calendar_widget">http://awesome.naquadah.org/wiki/Calendar_widget</a>.</p>
51 --
52 -- <h2>Gadget</h2>
53 --
54 -- <p>The calendar gadget can be instantiated by indexing the gadget
55 -- module with <code>text.calendar</code>. The ID parameter has no
56 -- meaning for the calendar gadget, and it takes no particular
57 -- parameters. See the <a href="flaw.gadget.html">gadget</a>
58 -- module documentation to learn about standard gadgets
59 -- parameters.</p>
60 --
61 -- <div class='example'>
62 -- g = flaw.gadget.text.calendar('')
63 -- </div>
64 --
65 -- <p>When the mouse gets over the gadget, a floating
66 -- <code>wibox</code> is displayed to present a month view. The
67 -- buttons 4 and 5 of the mouse - which should normally be bound to
68 -- the wheel up and down movements - allow you to change the displayed
69 -- month. With the help of the <code>shift</code> key, the year is
70 -- changed.</p>
71 --
72 -- <h2>Provider</h2>
73 --
74 -- <p>The provider makes no hardware or software access whatsoever. It
75 -- simply builds a textual month view for the calendar gadget. This
76 -- also means the <code>delay</code> parameter has no meaning for
77 -- it.</p>
78 --
79 --
80 -- @author David Soulayrol &lt;david.soulayrol AT gmail DOT com&gt;
81 -- @copyright 2010,2011, David Soulayrol
82 module('flaw.calendar')
83
84 --- The calendar provider prototype.
85 --
86 -- <p>The calendar provider type is set to
87 -- <code>calendar._NAME</code>.</p>
88 --
89 -- @class table
90 -- @name CalendarProvider
91 CalendarProvider = flaw.provider.Provider:new{ type = _NAME }
92
93 function CalendarProvider:subscribe(g)
94 if flaw.provider.Provider.subscribe(self, g) then
95 -- Initial update.
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 --- A factory for calendar providers.
151 --
152 -- <p>Only one provider is built.</p>
153 --
154 -- @return a brand new calendar provider, or the existing one if the
155 -- found in the providers cache.
156 function provider_factory()
157 local p = flaw.provider.get(_NAME, '')
158 -- Create the provider if necessary.
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 --- A Gadget prototype for calendar display.
171 --
172 -- @class table
173 -- @name CalendarGadget
174 CalendarGadget = flaw.gadget.Gadget:new{}
175
176 -- Create the wrapped gadget.
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)