rndk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/demos/appointment.rb +40 -25
  3. data/demos/clock.rb +22 -11
  4. data/demos/fileview.rb +141 -0
  5. data/examples/01-hello-world.rb +1 -2
  6. data/examples/02-colors.rb +58 -0
  7. data/examples/03-markup.rb +70 -0
  8. data/examples/04-quick-widgets.rb +72 -0
  9. data/examples/05-position-widget.rb +3 -6
  10. data/examples/calendar.rb +104 -0
  11. data/examples/scroll.rb +106 -0
  12. data/lib/rndk/alphalist.rb +14 -14
  13. data/lib/rndk/button.rb +21 -21
  14. data/lib/rndk/buttonbox.rb +18 -18
  15. data/lib/rndk/calendar.rb +333 -240
  16. data/lib/rndk/core/color.rb +136 -0
  17. data/lib/rndk/core/display.rb +23 -12
  18. data/lib/rndk/core/draw.rb +32 -26
  19. data/lib/rndk/core/markup.rb +561 -0
  20. data/lib/rndk/core/quick_widgets.rb +232 -12
  21. data/lib/rndk/core/screen.rb +16 -17
  22. data/lib/rndk/core/utils.rb +143 -0
  23. data/lib/rndk/core/widget.rb +133 -92
  24. data/lib/rndk/dialog.rb +17 -17
  25. data/lib/rndk/entry.rb +21 -21
  26. data/lib/rndk/fselect.rb +16 -16
  27. data/lib/rndk/graph.rb +10 -10
  28. data/lib/rndk/histogram.rb +10 -10
  29. data/lib/rndk/itemlist.rb +20 -20
  30. data/lib/rndk/label.rb +66 -45
  31. data/lib/rndk/marquee.rb +10 -10
  32. data/lib/rndk/matrix.rb +27 -27
  33. data/lib/rndk/mentry.rb +22 -22
  34. data/lib/rndk/menu.rb +14 -14
  35. data/lib/rndk/radio.rb +19 -19
  36. data/lib/rndk/scale.rb +21 -21
  37. data/lib/rndk/scroll.rb +19 -19
  38. data/lib/rndk/scroller.rb +2 -0
  39. data/lib/rndk/selection.rb +20 -20
  40. data/lib/rndk/slider.rb +21 -21
  41. data/lib/rndk/swindow.rb +20 -20
  42. data/lib/rndk/template.rb +21 -21
  43. data/lib/rndk/version.rb +1 -1
  44. data/lib/rndk/viewer.rb +18 -18
  45. data/lib/rndk.rb +99 -777
  46. data/rndk.gemspec +1 -1
  47. metadata +12 -3
data/lib/rndk/calendar.rb CHANGED
@@ -1,10 +1,48 @@
1
1
  require 'rndk'
2
2
 
3
3
  module RNDK
4
- class CALENDAR < RNDK::Widget
5
- attr_accessor :week_base
4
+
5
+ # Pop-up calendar Widget.
6
+ #
7
+ # The Calendar Widget allows the user to traverse through
8
+ # months/years using the cursor keys.
9
+ #
10
+ # ## Keybindings
11
+ #
12
+ # Left Arrow:: Moves the cursor to the previous day.
13
+ # Right Arrow:: Moves the cursor to the next day.
14
+ # Up Arrow:: Moves the cursor to the next week.
15
+ # Down Arrow:: Moves the cursor to the previous week.
16
+ # t:: Sets the calendar to the current date.
17
+ # T:: Sets the calendar to the current date.
18
+ # n:: Advances the calendar one month ahead.
19
+ # N:: Advances the calendar six months ahead.
20
+ # p:: Advances the calendar one month back.
21
+ # P:: Advances the calendar six months back.
22
+ # -:: Advances the calendar one year ahead.
23
+ # +:: Advances the calendar one year back.
24
+ # Enter:: Exits the widget and returns a value of
25
+ # time_t which represents the day
26
+ # selected at 1 second after midnight.
27
+ # This also sets the widget data exitType
28
+ # to vNORMAL.
29
+ # Tab:: Exits the widget and returns a value of
30
+ # time_t which represents the day
31
+ # selected at 1 second after midnight.
32
+ # This also sets the widget data exitType
33
+ # to vNORMAL.
34
+ # Escape:: Exits the widget and returns (time_)-1
35
+ # This also sets the widget data exitType
36
+ # to vESCAPE_HIT.
37
+ # Ctrl-L:: Refreshes the screen.
38
+ #
39
+ class Calendar < RNDK::Widget
40
+
6
41
  attr_reader :day, :month, :year
7
42
 
43
+ # First day of the week - Sunday is 0, Monday is 1, etc.
44
+ attr_accessor :week_base
45
+
8
46
  MONTHS_OF_THE_YEAR = [
9
47
  'NULL',
10
48
  'January',
@@ -37,47 +75,96 @@ module RNDK
37
75
  31,
38
76
  ]
39
77
 
40
- MAX_DAYS = 32
78
+ MAX_DAYS = 32
41
79
  MAX_MONTHS = 13
42
- MAX_YEARS = 140
80
+ MAX_YEARS = 140
43
81
 
44
- CALENDAR_LIMIT = MAX_DAYS * MAX_MONTHS * MAX_YEARS
82
+ CALENDAR_LIMIT = (MAX_DAYS * MAX_MONTHS * MAX_YEARS)
45
83
 
46
- def self.CALENDAR_INDEX(d, m, y)
47
- (y * RNDK::CALENDAR::MAX_MONTHS + m) * RNDK::CALENDAR::MAX_DAYS + d
84
+ # Tells what day of the week the `month` starts on.
85
+ def self.month_starting_weekday(year, month)
86
+ return Time.mktime(year, month, 1, 10, 0, 0).wday
48
87
  end
49
88
 
50
- def setCalendarCell(d, m, y, value)
51
- @marker[RNDK::CALENDAR.CALENDAR_INDEX(d, m, y)] = value
89
+ # Tells if `year` is a leap year.
90
+ def self.leap_year? year
91
+ result = false
92
+ if year % 4 == 0
93
+ if year % 100 == 0
94
+ if year % 400 == 0
95
+ result = true
96
+ end
97
+ else
98
+ result = true
99
+ end
100
+ end
101
+ result
52
102
  end
53
103
 
54
- def getCalendarCell(d, m, y)
55
- @marker[RNDK::CALENDAR.CALENDAR_INDEX(d, m, y)]
56
- end
104
+ # Returns how many days the `month`/`year` has.
105
+ def self.days_in_month(year, month)
106
+ month_length = DAYS_OF_THE_MONTH[month]
107
+
108
+ if month == 2
109
+ month_length += if Calendar.leap_year?(year)
110
+ then 1
111
+ else 0
112
+ end
113
+ end
57
114
 
58
- def initialize(rndkscreen, xplace, yplace, title, day, month, year,
59
- day_attrib, month_attrib, year_attrib, highlight, box, shadow)
115
+ month_length
116
+ end
117
+
118
+ # Creates a Calendar Widget.
119
+ #
120
+ # * `day`, `month` and `year` are integers. I suggest
121
+ # you to use Ruby's `Time.now.gmtime`.
122
+ # * `title` can be more than one line - just split them
123
+ # with `\n`s.
124
+ # * `*_attrib` are specific colors.
125
+ #
126
+ # @note If `day`, `month` or `year` are zero, we'll use
127
+ # the current date for it.
128
+ # If all of them are 0, will use the complete date
129
+ # of today.
130
+ def initialize(rndkscreen,
131
+ xplace,
132
+ yplace,
133
+ title,
134
+ day,
135
+ month,
136
+ year,
137
+ day_attrib,
138
+ month_attrib,
139
+ year_attrib,
140
+ highlight,
141
+ box,
142
+ shadow)
60
143
  super()
61
- parent_width = Ncurses.getmaxx(rndkscreen.window)
144
+ self.set_date(day, month, year)
145
+ self.set_box box
146
+
147
+ parent_width = Ncurses.getmaxx(rndkscreen.window)
62
148
  parent_height = Ncurses.getmaxy(rndkscreen.window)
63
- box_width = 24
149
+
150
+ box_width = 24
64
151
  box_height = 11
152
+
65
153
  dayname = 'Su Mo Tu We Th Fr Sa '
66
154
  bindings = {
67
- 'T' => Ncurses::KEY_HOME,
68
- 't' => Ncurses::KEY_HOME,
69
- 'n' => Ncurses::KEY_NPAGE,
155
+ 'T' => Ncurses::KEY_HOME,
156
+ 't' => Ncurses::KEY_HOME,
157
+ 'n' => Ncurses::KEY_NPAGE,
70
158
  RNDK::FORCHAR => Ncurses::KEY_NPAGE,
71
- 'p' => Ncurses::KEY_PPAGE,
159
+ 'p' => Ncurses::KEY_PPAGE,
72
160
  RNDK::BACKCHAR => Ncurses::KEY_PPAGE,
73
161
  }
74
162
 
75
- self.setBox(box)
76
-
77
- box_width = self.setTitle(title, box_width)
163
+ box_width = self.set_title(title, box_width)
78
164
  box_height += @title_lines
79
165
 
80
- # Make sure we didn't extend beyond the dimensions of the window.
166
+ # Make sure we didn't extend beyond the dimensions
167
+ # of the window.
81
168
  box_width = [box_width, parent_width].min
82
169
  box_height = [box_height, parent_height].min
83
170
 
@@ -103,131 +190,158 @@ module RNDK
103
190
  @field_width = box_width - 2 * (1 + @border_size)
104
191
 
105
192
  # Set months and day names
106
- @month_name = RNDK::CALENDAR::MONTHS_OF_THE_YEAR.clone
193
+ @month_name = Calendar::MONTHS_OF_THE_YEAR.clone
107
194
  @day_name = dayname
108
195
 
109
196
  # Set the rest of the widget values.
110
197
  @screen = rndkscreen
111
198
  @parent = rndkscreen.window
112
- @shadow_win = nil
199
+
113
200
  @xpos = xpos
114
201
  @ypos = ypos
115
- @box_width = box_width
202
+
203
+ @width = box_width
204
+ @box_width = box_width
116
205
  @box_height = box_height
117
- @day = day
118
- @month = month
119
- @year = year
120
- @day_attrib = day_attrib
206
+
207
+ @day_attrib = day_attrib
121
208
  @month_attrib = month_attrib
122
- @year_attrib = year_attrib
123
- @highlight = highlight
124
- @width = box_width
209
+ @year_attrib = year_attrib
210
+ @highlight = highlight
211
+
125
212
  @accepts_focus = true
126
- @input_window = @win
213
+ @input_window = @win
214
+
127
215
  @week_base = 0
128
- @shadow = shadow
129
- @label_win = Ncurses.subwin(@win, 1, @field_width,
130
- ypos + @title_lines + 1, xpos + 1 + @border_size)
216
+
217
+ @shadow = shadow
218
+ @shadow_win = nil
219
+
220
+ @label_win = Ncurses.subwin(@win,
221
+ 1,
222
+ @field_width,
223
+ ypos + @title_lines + 1,
224
+ xpos + 1 + @border_size)
131
225
  if @label_win.nil?
132
226
  self.destroy
133
227
  return nil
134
228
  end
135
229
 
136
- @field_win = Ncurses.subwin(@win, 7, 20,
137
- ypos + @title_lines + 3, xpos + @x_offset)
230
+ @field_win = Ncurses.subwin(@win,
231
+ 7,
232
+ 20,
233
+ ypos + @title_lines + 3,
234
+ xpos + @x_offset)
138
235
  if @field_win.nil?
139
236
  self.destroy
140
237
  return nil
141
238
  end
142
- self.setBox(box)
143
-
144
- @marker = [0] * RNDK::CALENDAR::CALENDAR_LIMIT
145
239
 
146
- # If the day/month/year values were 0, then use today's date.
147
- if @day == 0 && @month == 0 && @year == 0
148
- date_info = Time.new.gmtime
149
- @day = date_info.day
150
- @month = date_info.month
151
- @year = date_info
152
- end
240
+ # Mon Nov 11 18:54:40
241
+ # another `set_box box` was here
242
+ # apparently nothing fucked up, see if I can delete this
153
243
 
154
- # Verify the dates provided.
155
- self.verifyCalendarDate
156
-
157
- # Determine which day the month starts on.
158
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
244
+ @marker = [0] * Calendar::CALENDAR_LIMIT
159
245
 
160
246
  # If a shadow was requested, then create the shadow window.
161
247
  if shadow
162
- @shadow_win = Ncurses.newwin(box_height, box_width,
163
- ypos + 1, xpos + 1)
248
+ @shadow_win = Ncurses.newwin(box_height,
249
+ box_width,
250
+ ypos + 1,
251
+ xpos + 1)
164
252
  end
165
253
 
166
254
  # Setup the key bindings.
167
255
  bindings.each do |from, to|
168
- self.bind(:CALENDAR, from, :getc, to)
256
+ self.bind(:calendar, from, :getc, to)
169
257
  end
170
258
 
171
- rndkscreen.register(:CALENDAR, self)
259
+ rndkscreen.register(:calendar, self)
172
260
  end
173
261
 
174
- # This function lets the user play with this widget.
175
- def activate(actions)
176
- ret = -1
177
- self.draw(@box)
262
+ # Returns the specific internal index of `d`/`m`/`y`.
263
+ def self.calendar_index(d, m, y)
264
+ (y * Calendar::MAX_MONTHS + m) * Calendar::MAX_DAYS + d
265
+ end
266
+
267
+ # Sets `d`/`m`/`y` cell to have `value`.
268
+ def setCalendarCell(d, m, y, value)
269
+ @marker[Calendar.calendar_index(d, m, y)] = value
270
+ end
271
+
272
+ # Returns current value on cell `d`/`m`/`y`.
273
+ def getCalendarCell(d, m, y)
274
+ @marker[Calendar.calendar_index(d, m, y)]
275
+ end
276
+
277
+ # Activates the Calendar Widget, letting the user interact with it.
278
+ #
279
+ # `actions` is an Array of characters. If it's non-null,
280
+ # will #inject each char on it into the Widget.
281
+ #
282
+ # @return The date or `nil` if something bad happened.
283
+ def activate(actions=[])
284
+ ret = nil
285
+ self.draw @box
178
286
 
179
287
  if actions.nil? || actions.size == 0
180
- while true
288
+ # Interacting with the user
289
+ loop do
181
290
  input = self.getch([])
182
291
 
183
292
  # Inject the character into the widget.
184
- ret = self.inject(input)
185
- if @exit_type != :EARLY_EXIT
186
- return ret
187
- end
293
+ ret = self.inject input
294
+
295
+ return ret if @exit_type != :EARLY_EXIT
188
296
  end
297
+
189
298
  else
190
- # Inject each character one at a time.
299
+ # Executing `actions`, one char at a time.
191
300
  actions.each do |action|
192
- ret = self.inject(action)
193
- if @exit_type != :EARLY_EXIT
194
- return ret
195
- end
301
+ ret = self.inject action
302
+
303
+ return ret if @exit_type != :EARLY_EXIT
196
304
  end
197
305
  end
198
- return ret
306
+ ret
199
307
  end
200
308
 
201
- # This injects a single character into the widget.
202
- def inject(input)
203
- # Declare local variables
309
+ # Makes the Calendar react to `char` just as if the user
310
+ # had pressed it.
311
+ #
312
+ # Nice to simulate batch actions on a Widget.
313
+ #
314
+ # Besides normal keybindings (arrow keys and such), see
315
+ # Widget#set_exit_type to see how the Widget exits.
316
+ #
317
+ def inject char
204
318
  pp_return = 1
205
- ret = -1
206
- complete = false
319
+ ret = nil
320
+ complete = false
207
321
 
208
322
  # Set the exit type
209
- self.setExitType(0)
323
+ self.set_exit_type(0)
210
324
 
211
325
  # Refresh the widget field.
212
326
  self.drawField
213
327
 
214
328
  # Check if there is a pre-process function to be called.
215
329
  unless @pre_process_func.nil?
216
- pp_return = @pre_process_func.call(:CALENDAR, self,
217
- @pre_process_data, input)
330
+ pp_return = @pre_process_func.call(:calendar, self, @pre_process_data, char)
218
331
  end
219
332
 
220
333
  # Should we continue?
221
334
  if pp_return != 0
222
335
  # Check a predefined binding
223
- if self.checkBind(:CALENDAR, input)
336
+ if self.checkBind(:calendar, char)
224
337
 
225
338
  ## FIXME What the heck? Missing method?
226
339
  #self.checkEarlyExit
227
340
 
228
341
  complete = true
342
+
229
343
  else
230
- case input
344
+ case char
231
345
  when Ncurses::KEY_UP then self.decrementCalendarDay 7
232
346
  when Ncurses::KEY_DOWN then self.incrementCalendarDay 7
233
347
  when Ncurses::KEY_LEFT then self.decrementCalendarDay 1
@@ -239,16 +353,17 @@ module RNDK
239
353
  when '-'.ord then self.decrementCalendarYear 1
240
354
  when '+'.ord then self.incrementCalendarYear 1
241
355
 
242
- when Ncurses::KEY_HOME then self.setDate(-1, -1, -1)
356
+ when Ncurses::KEY_HOME
357
+ self.set_date(0, 0, 0)
243
358
 
244
359
  when RNDK::KEY_ESC
245
- self.setExitType(input)
360
+ self.set_exit_type char
246
361
  complete = true
247
362
  when Ncurses::ERR
248
- self.setExitType(input)
363
+ self.set_exit_type char
249
364
  complete = true
250
365
  when RNDK::KEY_TAB, RNDK::KEY_RETURN, Ncurses::KEY_ENTER
251
- self.setExitType(input)
366
+ self.set_exit_type char
252
367
  ret = self.getCurrentTime
253
368
  complete = true
254
369
  when RNDK::REFRESH
@@ -259,12 +374,12 @@ module RNDK
259
374
 
260
375
  # Should we do a post-process?
261
376
  if !complete && !(@post_process_func.nil?)
262
- @post_process_func.call(:CALENDAR, self, @post_process_data, input)
377
+ @post_process_func.call(:calendar, self, @post_process_data, char)
263
378
  end
264
379
  end
265
380
 
266
381
  if !complete
267
- self.setExitType(0)
382
+ self.set_exit_type(0)
268
383
  end
269
384
 
270
385
  @result_data = ret
@@ -278,22 +393,21 @@ module RNDK
278
393
  self.move_specific(xplace, yplace, relative, refresh_flag, windows, [])
279
394
  end
280
395
 
281
- # This draws the calendar widget.
282
- def draw(box)
396
+ # Draws the Calendar Widget on the Screen.
397
+ #
398
+ # If `box` is true, it is drawn with a box.
399
+ def draw(box=false)
400
+
283
401
  header_len = @day_name.size
284
402
  col_len = (6 + header_len) / 7
285
403
 
286
404
  # Is there a shadow?
287
- unless @shadow_win.nil?
288
- Draw.drawShadow(@shadow_win)
289
- end
405
+ Draw.drawShadow(@shadow_win) unless @shadow_win.nil?
290
406
 
291
407
  # Box the widget if asked.
292
- if box
293
- Draw.drawObjBox(@win, self)
294
- end
408
+ Draw.drawObjBox(@win, self) if box
295
409
 
296
- self.drawTitle(@win)
410
+ self.drawTitle @win
297
411
 
298
412
  # Draw in the day-of-the-week header.
299
413
  (0...7).each do |col|
@@ -312,11 +426,11 @@ module RNDK
312
426
  self.drawField
313
427
  end
314
428
 
315
- # This draws the month field.
429
+ # Draws the month field.
316
430
  def drawField
317
431
  month_name = @month_name[@month]
318
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
319
- year_index = RNDK::CALENDAR.YEAR2INDEX(@year)
432
+ month_length = Calendar.days_in_month(@year, @month)
433
+ year_index = Calendar.global_year_index(@year)
320
434
  year_len = 0
321
435
  save_y = -1
322
436
  save_x = -1
@@ -370,70 +484,76 @@ module RNDK
370
484
  end
371
485
  end
372
486
 
373
- # This sets multiple attributes of the widget
487
+ # Sets multiple attributes of the Widget.
488
+ #
489
+ # See Calendar#initialize.
374
490
  def set(day, month, year, day_attrib, month_attrib, year_attrib, highlight, box)
375
- self.setDate(day, month, yar)
376
- self.setDayAttribute(day_attrib)
377
- self.setMonthAttribute(month_attrib)
378
- self.setYearAttribute(year_attrib)
379
- self.setHighlight(highlight)
380
- self.setBox(box)
381
- end
491
+ self.set_date(day, month, yar)
492
+ self.set_day_attrib(day_attrib)
493
+ self.set_month_attrib(month_attrib)
494
+ self.set_year_attrib(year_attrib)
495
+ self.set_highlight(highlight)
496
+ self.set_box(box)
497
+ end
498
+
499
+ # Sets the current date.
500
+ #
501
+ # @note If `day`, `month` or `year` are zero, we'll use
502
+ # the current date for it.
503
+ # If all of them are 0, will use the complete date
504
+ # of today.
505
+ def set_date(day, month, year)
382
506
 
383
- # This sets the date and some attributes.
384
- def setDate(day, month, year)
385
507
  # Get the current dates and set the default values for the
386
508
  # day/month/year values for the calendar
387
509
  date_info = Time.new.gmtime
388
510
 
389
- # Set the date elements if we need to.
390
- @day = if day == -1 then date_info.day else day end
391
- @month = if month == -1 then date_info.month else month end
392
- @year = if year == -1 then date_info.year else year end
511
+ @day = if day == 0 then date_info.day else day end
512
+ @month = if month == 0 then date_info.month else month end
513
+ @year = if year == 0 then date_info.year else year end
393
514
 
394
- # Verify the date information.
395
- self.verifyCalendarDate
515
+ self.normalize_date
396
516
 
397
517
  # Get the start of the current month.
398
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
518
+ @week_day = Calendar.month_starting_weekday(@year, @month)
399
519
  end
400
520
 
401
- # This returns the current date on the calendar.
402
- def getDate(day, month, year)
403
- day << @day
404
- month << @month
405
- year << @year
521
+ # Returns the current date the calendar is displaying.
522
+ #
523
+ # @return An array with `[day, month, year]` numbers.
524
+ def get_date
525
+ [@day, @month, @year]
406
526
  end
407
527
 
408
- # This sets the attribute of the days in the calendar.
409
- def setDayAttribute(attribute)
528
+ # Sets the appearance/color of the days.
529
+ def set_day_attrib attribute
410
530
  @day_attrib = attribute
411
531
  end
412
532
 
413
- def getDayAttribute
533
+ def get_day_attrib
414
534
  return @day_attrib
415
535
  end
416
536
 
417
- # This sets the attribute of the month names in the calendar.
418
- def setMonthAttribute(attribute)
537
+ # Sets the appearance/color of the month name.
538
+ def set_month_attrib attribute
419
539
  @month_attrib = attribute
420
540
  end
421
541
 
422
- def getMonthAttribute
542
+ def get_month_attrib
423
543
  return @month_attrib
424
544
  end
425
545
 
426
- # This sets the attribute of the year in the calendar.
427
- def setYearAttribute(attribute)
546
+ # Sets the appearance/color of the year number.
547
+ def set_year_attrib attribute
428
548
  @year_attrib = attribute
429
549
  end
430
550
 
431
- def getYearAttribute
551
+ def get_year_attrib
432
552
  return @year_attrib
433
553
  end
434
554
 
435
- # This sets the attribute of the highlight box.
436
- def setHighlight(highlight)
555
+ # Sets the attribute/color of the highlight bar of the scrolling list.
556
+ def set_highlight highlight
437
557
  @highlight = highlight
438
558
  end
439
559
 
@@ -441,128 +561,110 @@ module RNDK
441
561
  return @highlight
442
562
  end
443
563
 
444
- # This sets the background attribute of the widget.
445
- def setBKattr(attrib)
564
+ # Sets the background attribute/color of the widget.
565
+ def set_bg_attrib attrib
446
566
  Ncurses.wbkgd(@win, attrib)
447
567
  Ncurses.wbkgd(@field_win, attrib)
448
568
  Ncurses.wbkgd(@label_win, attrib) unless @label_win.nil?
449
569
  end
450
570
 
451
- # This erases the calendar widget.
571
+ # Erases the Calendar from the Screen.
572
+ # @note It does not destroy the widget.
452
573
  def erase
453
- if self.validRNDKObject
454
- RNDK.eraseCursesWindow @label_win
455
- RNDK.eraseCursesWindow @field_win
456
- RNDK.eraseCursesWindow @win
457
- RNDK.eraseCursesWindow @shadow_win
458
- end
574
+ return unless self.valid_widget?
575
+
576
+ RNDK.window_erase @label_win
577
+ RNDK.window_erase @field_win
578
+ RNDK.window_erase @win
579
+ RNDK.window_erase @shadow_win
459
580
  end
460
581
 
461
- # This destroys the calendar
582
+ # Destroys all windows inside the Widget and
583
+ # removes it from the Screen.
462
584
  def destroy
463
585
  self.cleanTitle
464
586
 
465
- RNDK.deleteCursesWindow @label_win
466
- RNDK.deleteCursesWindow @field_win
467
- RNDK.deleteCursesWindow @shadow_win
468
- RNDK.deleteCursesWindow @win
587
+ RNDK.window_delete @label_win
588
+ RNDK.window_delete @field_win
589
+ RNDK.window_delete @shadow_win
590
+ RNDK.window_delete @win
469
591
 
470
- # Clean the key bindings.
471
- self.cleanBindings(:CALENDAR)
592
+ self.clean_bindings :calendar
472
593
 
473
- # Unregister the object.
474
- RNDK::Screen.unregister(:CALENDAR, self)
594
+ RNDK::Screen.unregister(:calendar, self)
475
595
  end
476
596
 
477
- # This sets a marker on the calendar.
597
+ # Sets a marker on a specific date.
478
598
  def setMarker(day, month, year, marker)
479
- year_index = RNDK::CALENDAR.YEAR2INDEX(year)
599
+ year_index = Calendar.global_year_index(year)
480
600
  oldmarker = self.getMarker(day, month, year)
481
601
 
482
602
  # Check to see if a marker has not already been set
483
603
  if oldmarker != 0
484
- self.setCalendarCell(day, month, year_index,
485
- oldmarker | Ncurses::A_BLINK)
604
+ self.setCalendarCell(day, month, year_index, oldmarker | Ncurses::A_BLINK)
486
605
  else
487
606
  self.setCalendarCell(day, month, year_index, marker)
488
607
  end
489
608
  end
490
609
 
610
+ # Returns the marker on a specific date.
491
611
  def getMarker(day, month, year)
492
612
  result = 0
493
- year = RNDK::CALENDAR.YEAR2INDEX(year)
613
+ year = Calendar.global_year_index(year)
494
614
  if @marker != 0
495
615
  result = self.getCalendarCell(day, month, year)
496
616
  end
497
617
  return result
498
618
  end
499
619
 
500
- # This sets a marker on the calendar.
620
+ # Removes a marker from the Calendar.
501
621
  def removeMarker(day, month, year)
502
- year_index = RNDK::CALENDAR.YEAR2INDEX(year)
622
+ year_index = Calendar.global_year_index(year)
503
623
  self.setCalendarCell(day, month, year_index, 0)
504
624
  end
505
625
 
506
- # THis function sets the month name.
626
+ # Sets the month name.
507
627
  def setMonthNames(months)
508
628
  (1...[months.size, @month_name.size].min).each do |x|
509
629
  @month_name[x] = months[x]
510
630
  end
511
631
  end
512
632
 
513
- # This function sets the day's name
514
- def setDaysNames(days)
633
+
634
+ # Sets the names of the days of the week.
635
+ #
636
+ # `days` is a String listing the 2-character
637
+ # abbreviations for the days.
638
+ #
639
+ # The default value is `"Su Mo Tu We Th Fr Sa"`
640
+ #
641
+ # "Su" (Sunday) is numbered zero internally, making it by default
642
+ # the first day of the week. Set the `week_base` member of the
643
+ # widget to select a different day.
644
+ #
645
+ # For example, Monday would be 1, Tuesday 2, etc.
646
+ #
647
+ def set_days_names days
515
648
  @day_name = days.clone
516
649
  end
517
650
 
518
- # This makes sure that the dates provided exist.
519
- def verifyCalendarDate
520
- # Make sure the given year is not less than 1900.
521
- if @year < 1900
522
- @year = 1900
523
- end
524
-
525
- # Make sure the month is within range.
526
- if @month > 12
527
- @month = 12
528
- end
529
- if @month < 1
530
- @month = 1
531
- end
651
+ # Makes sure that the internal dates exist, capping
652
+ # the values if too big/small.
653
+ def normalize_date
654
+ @year = 1900 if @year < 1900
655
+ @month = 12 if @month > 12
656
+ @month = 1 if @month < 1
657
+ @day = 1 if @day < 1
532
658
 
533
659
  # Make sure the day given is within range of the month.
534
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
535
- if @day < 1
536
- @day = 1
537
- end
538
- if @day > month_length
539
- @day = month_length
540
- end
541
- end
660
+ month_length = Calendar.days_in_month(@year, @month)
542
661
 
543
- # This returns what day of the week the month starts on.
544
- def self.getMonthStartWeekday(year, month)
545
- return Time.mktime(year, month, 1, 10, 0, 0).wday
546
- end
547
-
548
- # This function returns a 1 if it's a leap year and 0 if not.
549
- def self.isLeapYear(year)
550
- result = false
551
- if year % 4 == 0
552
- if year % 100 == 0
553
- if year % 400 == 0
554
- result = true
555
- end
556
- else
557
- result = true
558
- end
559
- end
560
- return result
662
+ @day = month_length if @day > month_length
561
663
  end
562
664
 
563
665
  # This increments the current day by the given value.
564
666
  def incrementCalendarDay(adjust)
565
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
667
+ month_length = Calendar.days_in_month(@year, @month)
566
668
 
567
669
  # Make sure we adjust the day correctly.
568
670
  if adjust + @day > month_length
@@ -588,12 +690,12 @@ module RNDK
588
690
  'Can not go past the year 1900'
589
691
  ]
590
692
  RNDK.beep
591
- @screen.popupLabel(mesg, 2)
693
+ @screen.popup_label(mesg, 2)
592
694
  return
593
695
  end
594
- month_length = RNDK::CALENDAR.getMonthLength(@year - 1, 12)
696
+ month_length = Calendar.days_in_month(@year - 1, 12)
595
697
  else
596
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month - 1)
698
+ month_length = Calendar.days_in_month(@year, @month - 1)
597
699
  end
598
700
 
599
701
  @day = month_length - (adjust - @day)
@@ -617,13 +719,13 @@ module RNDK
617
719
  end
618
720
 
619
721
  # Get the length of the current month.
620
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
722
+ month_length = Calendar.days_in_month(@year, @month)
621
723
  if @day > month_length
622
724
  @day = month_length
623
725
  end
624
726
 
625
727
  # Get the start of the current month.
626
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
728
+ @week_day = Calendar.month_starting_weekday(@year, @month)
627
729
 
628
730
  # Redraw the calendar.
629
731
  self.erase
@@ -640,7 +742,7 @@ module RNDK
640
742
  'Can not go past the year 1900',
641
743
  ]
642
744
  RNDK.beep
643
- @screen.popupLabel(mesg, 2)
745
+ @screen.popup_label(mesg, 2)
644
746
  return
645
747
  else
646
748
  @month = 13 - adjust
@@ -651,13 +753,13 @@ module RNDK
651
753
  end
652
754
 
653
755
  # Get the length of the current month.
654
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
756
+ month_length = Calendar.days_in_month(@year, @month)
655
757
  if @day > month_length
656
758
  @day = month_length
657
759
  end
658
760
 
659
761
  # Get the start o the current month.
660
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
762
+ @week_day = Calendar.month_starting_weekday(@year, @month)
661
763
 
662
764
  # Redraw the calendar.
663
765
  self.erase
@@ -671,14 +773,14 @@ module RNDK
671
773
 
672
774
  # If we are in Feb make sure we don't trip into voidness.
673
775
  if @month == 2
674
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
776
+ month_length = Calendar.days_in_month(@year, @month)
675
777
  if @day > month_length
676
778
  @day = month_length
677
779
  end
678
780
  end
679
781
 
680
782
  # Get the start of the current month.
681
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
783
+ @week_day = Calendar.month_starting_weekday(@year, @month)
682
784
 
683
785
  # Redraw the calendar.
684
786
  self.erase
@@ -694,7 +796,7 @@ module RNDK
694
796
  'Can not go past the year 1900',
695
797
  ]
696
798
  RNDK.beep
697
- @screen.popupLabel(mesg, 2)
799
+ @screen.popup_label(mesg, 2)
698
800
  return
699
801
  end
700
802
 
@@ -703,34 +805,20 @@ module RNDK
703
805
 
704
806
  # If we are in Feb make sure we don't trip into voidness.
705
807
  if @month == 2
706
- month_length = RNDK::CALENDAR.getMonthLength(@year, @month)
808
+ month_length = Calendar.days_in_month(@year, @month)
707
809
  if @day > month_length
708
810
  @day = month_length
709
811
  end
710
812
  end
711
813
 
712
814
  # Get the start of the current month.
713
- @week_day = RNDK::CALENDAR.getMonthStartWeekday(@year, @month)
815
+ @week_day = Calendar.month_starting_weekday(@year, @month)
714
816
 
715
817
  # Redraw the calendar.
716
818
  self.erase
717
819
  self.draw(@box)
718
820
  end
719
821
 
720
- # This returns the length of the current month.
721
- def self.getMonthLength(year, month)
722
- month_length = DAYS_OF_THE_MONTH[month]
723
-
724
- if month == 2
725
- month_length += if RNDK::CALENDAR.isLeapYear(year)
726
- then 1
727
- else 0
728
- end
729
- end
730
-
731
- return month_length
732
- end
733
-
734
822
  # This returns what day of the week the month starts on.
735
823
  def getCurrentTime
736
824
  # Determine the current time and determine if we are in DST.
@@ -747,20 +835,25 @@ module RNDK
747
835
  self.draw(@box)
748
836
  end
749
837
 
750
- def self.YEAR2INDEX(year)
751
- if year >= 1900
752
- year - 1900
753
- else
754
- year
755
- end
756
- end
757
-
838
+ # @see Widget#position
758
839
  def position
759
840
  super(@win)
760
841
  end
761
842
 
762
843
  def object_type
763
- :CALENDAR
844
+ :calendar
764
845
  end
846
+
847
+ private
848
+
849
+ # Returns the internal widget `year` index.
850
+ # Minimum year is 1900.
851
+ def self.global_year_index year
852
+ return (year - 1900) if year >= 1900
853
+
854
+ year
855
+ end
856
+
765
857
  end
766
858
  end
859
+