calendar.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. -- @author Cédric Le Moigne cedlemo
  2. local helpers = require("blingbling.helpers")
  3. local superproperties = require("blingbling.superproperties")
  4. local grid = require("blingbling.grid")
  5. local text_box = require("blingbling.text_box")
  6. local awful = require("awful")
  7. local util = awful.util
  8. local capi = { screen = screen, mouse = mouse }
  9. local wibox = require("wibox")
  10. local base = require("wibox.layout.base")
  11. local pairs = pairs
  12. local calendar = { mt = {} }
  13. local data = setmetatable({}, { __mode = "k" })
  14. local properties = { "prev_next_widget_style", "current_date_widget_style",
  15. "days_of_week_widget_style", "days_of_month_widget_style",
  16. "weeks_number_widget_style", "corner_widget_style",
  17. "current_day_widget_style", "focus_widget_style",
  18. "info_cell_style", "link_to_external_calendar" }
  19. -- Build properties function
  20. for _, prop in ipairs(properties) do
  21. if not calendar["set_" .. prop] then
  22. calendar["set_" .. prop] = function(cal, value)
  23. data[cal][prop] = value
  24. cal:emit_signal("widget::updated")
  25. return cal
  26. end
  27. end
  28. end
  29. local function generate_header_widgets(calendar)
  30. local props = data[calendar].props
  31. data[calendar].date = text_box(props.current_date_widget_style)
  32. data[calendar].date:set_text(os.date('%a %b %d, %H:%M'))
  33. data[calendar].prev_month = text_box(props.prev_month_widget_style)
  34. data[calendar].prev_month:set_text(util.escape("<<"))
  35. data[calendar].next_month = text_box(props.next_month_widget_style)
  36. data[calendar].next_month:set_text(util.escape(">>"))
  37. end
  38. local function generate_week_day_names()
  39. names = {}
  40. for i=6,12 do
  41. table.insert(names,(os.date("%a",os.time({month=2,day=i,year=2012}))))
  42. end
  43. return names
  44. end
  45. local function generate_week_days(calendar)
  46. names = generate_week_day_names()
  47. data[calendar].week_days = {}
  48. local props = data[calendar].props
  49. for i, n in ipairs(names) do
  50. data[calendar].week_days[i] = text_box(props.days_of_week_style)
  51. data[calendar].week_days[i]:set_text(n)
  52. end
  53. end
  54. local function generate_weeks_numbers(calendar)
  55. local numbers = helpers.get_ISO8601_weeks_number_of_month(data[calendar].month,
  56. data[calendar].year)
  57. local props = data[calendar].props
  58. data[calendar].weeks_numbers = {}
  59. for i=1,6 do
  60. data[calendar].weeks_numbers[i] = text_box(props.weeks_number_style)
  61. data[calendar].weeks_numbers[i]:set_text(numbers[i])
  62. end
  63. end
  64. local function generate_days_of_month(calendar)
  65. -- TODO
  66. end
  67. local function generate_widgets(calendar)
  68. generate_header_widgets(calendar)
  69. generate_week_days(calendar)
  70. generate_weeks_numbers(calendar)
  71. generate_days_of_month(calendar)
  72. end
  73. local function add_header_widgets(calendar)
  74. calendar:add_child(data[calendar].prev_month, 1, 1, 1, 1)
  75. calendar:add_child(data[calendar].date, 3, 1, 4, 1)
  76. calendar:add_child(data[calendar].next_month, 8, 1, 1, 1)
  77. end
  78. local function add_week_days(calendar)
  79. for i,v in ipairs(data[calendar].week_days) do
  80. calendar:add_child(v, i + 1, 2, 1, 1)
  81. end
  82. end
  83. local function add_weeks_numbers(calendar)
  84. for i, w in ipairs(data[calendar].weeks_numbers) do
  85. calendar:add_child(w, 1, 2 + i, 1, 1)
  86. end
  87. end
  88. local function add_days_of_month(calendar)
  89. -- TODO
  90. end
  91. local function fill_grid(calendar)
  92. add_header_widgets(calendar)
  93. add_week_days(calendar)
  94. add_weeks_numbers(calendar)
  95. add_days_of_month(calendar)
  96. end
  97. local function get_current_month_year(calendar)
  98. data[calendar].month = tonumber(os.date("%m"))
  99. data[calendar].year = tonumber(os.date("%Y"))
  100. end
  101. function calendar.new(args)
  102. local args = args or {}
  103. local _calendar = grid()
  104. if args.locale then
  105. os.setlocale(locale)
  106. end
  107. data[_calendar] = {}
  108. data[_calendar].props = helpers.load_properties(properties,
  109. data,
  110. _calendar,
  111. superproperties.calendar)
  112. get_current_month_year(_calendar)
  113. generate_widgets(_calendar)
  114. fill_grid(_calendar)
  115. return _calendar
  116. end
  117. function calendar.mt:__call(...)
  118. return calendar.new(...)
  119. end
  120. return setmetatable(calendar, calendar.mt)