Method: CDK::CALENDAR#initialize

Defined in:
lib/cdk/components/calendar.rb

#initialize(cdkscreen, xplace, yplace, title, day, month, year, day_attrib, month_attrib, year_attrib, highlight, box, shadow) ⇒ CALENDAR

Returns a new instance of CALENDAR.



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cdk/components/calendar.rb', line 58

def initialize(cdkscreen, xplace, yplace, title, day, month, year,
    day_attrib, month_attrib, year_attrib, highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = 24
  box_height = 11
  dayname = 'Su Mo Tu We Th Fr Sa '
  bindings = {
      'T'           => Ncurses::KEY_HOME,
      't'           => Ncurses::KEY_HOME,
      'n'           => Ncurses::KEY_NPAGE,
      CDK::FORCHAR  => Ncurses::KEY_NPAGE,
      'p'           => Ncurses::KEY_PPAGE,
      CDK::BACKCHAR => Ncurses::KEY_PPAGE,
  }

  self.setBox(box)

  box_width = self.setTitle(title, box_width)
  box_height += @title_lines

  # Make sure we didn't extend beyond the dimensions of the window.
  box_width = [box_width, parent_width].min
  box_height = [box_height, parent_height].min

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Create the calendar window.
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

  # Is the window nil?
  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

  # Set some variables.
  @x_offset = (box_width - 20) / 2
  @field_width = box_width - 2 * (1 + @border_size)

  # Set months and day names
  @month_name = CDK::CALENDAR::MONTHS_OF_THE_YEAR.clone
  @day_name = dayname

  # Set the rest of the widget values.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @shadow_win = nil
  @xpos = xpos
  @ypos = ypos
  @box_width = box_width
  @box_height = box_height
  @day = day
  @month = month
  @year = year
  @day_attrib = day_attrib
  @month_attrib = month_attrib
  @year_attrib = year_attrib
  @highlight = highlight
  @width = box_width
  @accepts_focus = true
  @input_window = @win
  @week_base = 0
  @shadow = shadow
  @label_win = @win.subwin(1, @field_width,
      ypos + @title_lines + 1, xpos + 1 + @border_size)
  if @label_win.nil?
    self.destroy
    return nil
  end

  @field_win = @win.subwin(7, 20,
      ypos + @title_lines + 3, xpos + @x_offset)
  if @field_win.nil?
    self.destroy
    return nil
  end
  self.setBox(box)

  @marker = [0] * CDK::CALENDAR::CALENDAR_LIMIT

  # If the day/month/year values were 0, then use today's date.
  if @day == 0 && @month == 0 && @year == 0
    date_info = Time.new.gmtime
    @day = date_info.day
    @month = date_info.month
    @year = date_info
  end

  # Verify the dates provided.
  self.verifyCalendarDate

  # Determine which day the month starts on.
  @week_day = CDK::CALENDAR.getMonthStartWeekday(@year, @month)

  # If a shadow was requested, then create the shadow window.
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  # Setup the key bindings.
  bindings.each do |from, to|
    self.bind(:CALENDAR, from, :getc, to)
  end

  cdkscreen.register(:CALENDAR, self)
end