| 1 | # ---------------------------------------------------------------------- | ||
| 2 | # Curses::UI::Calendar | ||
| 3 | # | ||
| 4 | # (c) 2001-2002 by Maurice Makaay. All rights reserved. | ||
| 5 | # This file is part of Curses::UI. Curses::UI is free software. | ||
| 6 | # You can redistribute it and/or modify it under the same terms | ||
| 7 | # as perl itself. | ||
| 8 | # | ||
| 9 | # Currently maintained by Marcus Thiesen | ||
| 10 | # e-mail: marcus@cpan.thiesenweb.de | ||
| 11 | # ---------------------------------------------------------------------- | ||
| 12 | |||
| 13 | #TODO: fix dox | ||
| 14 | |||
| 15 | package Curses::UI::Calendar; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use Curses; | ||
| 19 | use Curses::UI::Common; | ||
| 20 | use Curses::UI::Widget; | ||
| 21 | |||
| 22 | use vars qw( | ||
| 23 | $VERSION | ||
| 24 | @ISA | ||
| 25 | ); | ||
| 26 | |||
| 27 | $VERSION = '1.10'; | ||
| 28 | |||
| 29 | @ISA = qw( | ||
| 30 | Curses::UI::Widget | ||
| 31 | Curses::UI::Common | ||
| 32 | ); | ||
| 33 | |||
| 34 | my @days = (); | ||
| 35 | my @months = (); | ||
| 36 | |||
| 37 | my %routines = ( | ||
| 38 | 'loose-focus' => \&loose_focus, | ||
| 39 | 'date-select' => \&date_select, | ||
| 40 | 'date-selected' => \&date_selected, | ||
| 41 | 'date-nextday' => \&date_nextday, | ||
| 42 | 'date-prevday' => \&date_prevday, | ||
| 43 | 'date-nextweek' => \&date_nextweek, | ||
| 44 | 'date-prevweek' => \&date_prevweek, | ||
| 45 | 'date-nextmonth' => \&date_nextmonth, | ||
| 46 | 'date-prevmonth' => \&date_prevmonth, | ||
| 47 | 'date-nextyear' => \&date_nextyear, | ||
| 48 | 'date-prevyear' => \&date_prevyear, | ||
| 49 | 'date-today' => \&date_today, | ||
| 50 | 'mouse-button' => \&mouse_button, | ||
| 51 | ); | ||
| 52 | |||
| 53 | my %bindings = ( | ||
| 54 | CUI_TAB() => 'loose-focus', | ||
| 55 | KEY_BTAB() => 'loose-focus', | ||
| 56 | KEY_LEFT() => 'date-prevday', | ||
| 57 | "h" => 'date-prevday', | ||
| 58 | KEY_RIGHT() => 'date-nextday', | ||
| 59 | "l" => 'date-nextday', | ||
| 60 | KEY_DOWN() => 'date-nextweek', | ||
| 61 | "j" => 'date-nextweek', | ||
| 62 | KEY_UP() => 'date-prevweek', | ||
| 63 | "k" => 'date-prevweek', | ||
| 64 | KEY_NPAGE() => 'date-nextmonth', | ||
| 65 | "J", => 'date-nextmonth', | ||
| 66 | KEY_PPAGE() => 'date-prevmonth', | ||
| 67 | "K", => 'date-prevmonth', | ||
| 68 | "L", => 'date-nextyear', | ||
| 69 | "H", => 'date-prevyear', | ||
| 70 | "n", => 'date-nextyear', | ||
| 71 | "p", => 'date-prevyear', | ||
| 72 | KEY_HOME() => 'date-selected', | ||
| 73 | "\cA" => 'date-selected', | ||
| 74 | "c" => 'date-selected', | ||
| 75 | "t" => 'date-today', | ||
| 76 | KEY_ENTER() => 'date-select', | ||
| 77 | CUI_SPACE() => 'date-select', | ||
| 78 | ); | ||
| 79 | |||
| 80 | # ---------------------------------------------------------------------- | ||
| 81 | # Constructor | ||
| 82 | # ---------------------------------------------------------------------- | ||
| 83 | |||
| 84 | sub new () | ||
| 85 | { | ||
| 86 | my $class = shift; | ||
| 87 | |||
| 88 | my %userargs = @_; | ||
| 89 | keys_to_lowercase(\%userargs); | ||
| 90 | |||
| 91 | my %args = ( | ||
| 92 | -date => undef, # The date to start width | ||
| 93 | -width => 0, # Widget will fix width itself | ||
| 94 | -height => 0, # Widget will fix height itself | ||
| 95 | -onchange => undef, # Event handler | ||
| 96 | -fg => -1, | ||
| 97 | -bg => -1, | ||
| 98 | -drawline => 1, # Draw a line under the widget? | ||
| 99 | |||
| 100 | %userargs, | ||
| 101 | |||
| 102 | -routines => {%routines}, | ||
| 103 | -bindings => {%bindings}, | ||
| 104 | |||
| 105 | -ipadleft => 1, | ||
| 106 | -ipadright => 1, | ||
| 107 | -ipadbottom => 0, | ||
| 108 | -ipadtop => 0, | ||
| 109 | -focus => 0, | ||
| 110 | -nocursor => 1, | ||
| 111 | ); | ||
| 112 | |||
| 113 | # The widget width should be at least 20. | ||
| 114 | my $min_width = width_by_windowscrwidth(20, %args); | ||
| 115 | $args{-width} = $min_width if $args{-width} != -1 | ||
| 116 | and $args{-width} < $min_width; | ||
| 117 | |||
| 118 | # The widget height should be at least 11. | ||
| 119 | my $min_height = height_by_windowscrheight(11, %args); | ||
| 120 | $args{-height} = $min_height if $args{-height} != -1 | ||
| 121 | and $args{-height} < $min_height; | ||
| 122 | |||
| 123 | my $this = $class->SUPER::new( %args ); | ||
| 124 | |||
| 125 | # Split up and fix the date. | ||
| 126 | $this->setdate($this->{-date}, 1); | ||
| 127 | |||
| 128 | # Set cursor to current date. | ||
| 129 | $this->{-cyear} = $this->{-year}; | ||
| 130 | $this->{-cmonth} = $this->{-month}; | ||
| 131 | $this->{-cday} = $this->{-day}; | ||
| 132 | |||
| 133 | # Load day- and monthnames. | ||
| 134 | @days = $this->root->lang->getarray('days_short'); | ||
| 135 | @months = (undef, $this->root->lang->getarray('months')); | ||
| 136 | |||
| 137 | if ($Curses::UI::ncurses_mouse) { | ||
| 138 | $this->set_mouse_binding( | ||
| 139 | 'mouse-button', BUTTON1_CLICKED(), BUTTON3_CLICKED()); | ||
| 140 | } | ||
| 141 | |||
| 142 | return $this; | ||
| 143 | } | ||
| 144 | |||
| 145 | # ---------------------------------------------------------------------- | ||
| 146 | # Methods | ||
| 147 | # ---------------------------------------------------------------------- | ||
| 148 | |||
| 149 | sub onChange(;$) { shift()->set_event('-onchange', shift()) } | ||
| 150 | sub day($;) { shift()->accessor('-day', shift()) } | ||
| 151 | sub month($;) { shift()->accessor('-month', shift()) } | ||
| 152 | sub year($;) { shift()->accessor('-year', shift()) } | ||
| 153 | |||
| 154 | |||
| 155 | sub layout() | ||
| 156 | { | ||
| 157 | my $this = shift; | ||
| 158 | $this->SUPER::layout() or return; | ||
| 159 | return $this; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub setdate($;$) | ||
| 163 | { | ||
| 164 | my $this = shift; | ||
| 165 | my $date = shift; | ||
| 166 | my $nodraw = shift || 0; | ||
| 167 | |||
| 168 | if (not defined $date) | ||
| 169 | { | ||
| 170 | $this->{-year} = undef; | ||
| 171 | $this->{-month} = undef; | ||
| 172 | $this->{-day} = undef; | ||
| 173 | } | ||
| 174 | elsif ($date =~ /^(\d\d\d\d+)(\d\d)(\d\d)$/) | ||
| 175 | { | ||
| 176 | $this->{-year} = $1; | ||
| 177 | $this->{-month} = $2; | ||
| 178 | $this->{-day} = $3; | ||
| 179 | } | ||
| 180 | elsif ($date =~ /^(\d{1,2})\D(\d{1,2})\D(\d\d\d\d+)$/) | ||
| 181 | { | ||
| 182 | $this->{-year} = $3; | ||
| 183 | $this->{-month} = $2; | ||
| 184 | $this->{-day} = $1; | ||
| 185 | } | ||
| 186 | elsif ($date =~ /^(\d\d\d\d+)\D(\d{1,2})\D(\d{1,2})$/) | ||
| 187 | { | ||
| 188 | $this->{-year} = $1; | ||
| 189 | $this->{-month} = $2; | ||
| 190 | $this->{-day} = $3; | ||
| 191 | } | ||
| 192 | |||
| 193 | $this->make_sane_date; | ||
| 194 | $this->intellidraw unless $nodraw; | ||
| 195 | |||
| 196 | return $this; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub make_sane_date() | ||
| 200 | { | ||
| 201 | my $this = shift; | ||
| 202 | my $cursor = shift; | ||
| 203 | my $c = $cursor ? 'c' : ''; | ||
| 204 | |||
| 205 | # Determine 'today'. | ||
| 206 | my @now = localtime(); $now[4]++; $now[5]+=1900; | ||
| 207 | |||
| 208 | # Use today's values if undefined. | ||
| 209 | $this->{"-${c}day"} = $now[3] | ||
| 210 | unless defined $this->{"-${c}day"}; | ||
| 211 | $this->{"-${c}month"} = $now[4] | ||
| 212 | unless defined $this->{"-${c}month"}; | ||
| 213 | $this->{"-${c}year"} = $now[5] | ||
| 214 | unless defined $this->{"-${c}year"}; | ||
| 215 | |||
| 216 | |||
| 217 | if ($this->{"-${c}year"} < 0) { $this->{"-${c}year"} = 0 } | ||
| 218 | if ($this->{"-${c}year"} > 9999) { $this->{"-${c}year"} = 9999 } | ||
| 219 | if ($this->{"-${c}month"} < 1) { $this->{"-${c}month"} = 1 } | ||
| 220 | if ($this->{"-${c}month"} > 12) { $this->{"-${c}month"} = 12 } | ||
| 221 | |||
| 222 | my $days = days_in_month($this->{"-${c}year"}, $this->{"-${c}month"}); | ||
| 223 | if ($this->{"-${c}day"} < 1) { $this->{"-${c}day"} = 1 } | ||
| 224 | if ($this->{"-${c}day"} > $days) { $this->{"-${c}day"} = $days } | ||
| 225 | |||
| 226 | # undef value? | ||
| 227 | if ($this->{"-${c}year"} == 1752 and $this->{"-${c}month"} == 9) { | ||
| 228 | if ($this->{"-${c}day"} > 2 and $this->{"-${c}day"} < 14) { | ||
| 229 | $this->{"-${c}day"} = ($this->{"-${c}day"} > 8 ? 14 : 2); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | return $this; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub draw(;$) | ||
| 237 | { | ||
| 238 | my $this = shift; | ||
| 239 | my $no_doupdate = shift || 0; | ||
| 240 | |||
| 241 | # Draw the widget | ||
| 242 | $this->SUPER::draw(1) or return $this; | ||
| 243 | |||
| 244 | $this->make_sane_date; | ||
| 245 | $this->make_sane_date(1); | ||
| 246 | |||
| 247 | # Let there be color | ||
| 248 | if ($Curses::UI::color_support) { | ||
| 249 | my $co = $Curses::UI::color_object; | ||
| 250 | my $pair = $co->get_color_pair( | ||
| 251 | $this->{-fg}, | ||
| 252 | $this->{-bg}); | ||
| 253 | |||
| 254 | $this->{-canvasscr}->attron(COLOR_PAIR($pair)); | ||
| 255 | |||
| 256 | } | ||
| 257 | |||
| 258 | # Bold font on if the widget has focus and the selected | ||
| 259 | # date is the active date. | ||
| 260 | $this->{-canvasscr}->attron(A_BOLD) | ||
| 261 | if $this->{-focus} and | ||
| 262 | $this->{-cyear} == $this->{-year} and | ||
| 263 | $this->{-cmonth} == $this->{-month} and | ||
| 264 | $this->{-cday} == $this->{-day}; | ||
| 265 | |||
| 266 | # Draw day, month and year. If the widget has focus, | ||
| 267 | # show the cursor position. Else show the selected position. | ||
| 268 | my $c = $this->{-focus} ? 'c' : ''; | ||
| 269 | $this->{-canvasscr}->addstr(0,0," "x$this->canvaswidth); | ||
| 270 | $this->{-canvasscr}->addstr(0,0, $months[$this->{"-${c}month"}] | ||
| 271 | . " " . $this->{"-${c}day"}); | ||
| 272 | $this->{-canvasscr}->addstr(0,$this->canvaswidth-4,$this->{"-${c}year"}); | ||
| 273 | |||
| 274 | # Draw daynames | ||
| 275 | $this->{-canvasscr}->attron(A_BOLD) if $this->{-focus}; | ||
| 276 | $this->{-canvasscr}->addstr(2,0,join " ", @days); | ||
| 277 | |||
| 278 | # Reset bold font attribute. | ||
| 279 | $this->{-canvasscr}->attroff(A_BOLD) if $this->{-focus}; | ||
| 280 | |||
| 281 | # Draw a line under the date. | ||
| 282 | if ($this->{-drawline}) { | ||
| 283 | $this->{-canvasscr}->move(1,0); | ||
| 284 | $this->{-canvasscr}->hline(ACS_HLINE,$this->canvaswidth); | ||
| 285 | } | ||
| 286 | |||
| 287 | # Create the list of days in the current month. | ||
| 288 | my @month = build_month($this->{"-${c}year"}, $this->{"-${c}month"}); | ||
| 289 | |||
| 290 | # Draw the days. | ||
| 291 | my $month = $this->{"-${c}month"}; | ||
| 292 | my $year = $this->{"-${c}year"}; | ||
| 293 | my $y = 4; | ||
| 294 | my $weekday = 0; | ||
| 295 | foreach my $day (@month) | ||
| 296 | { | ||
| 297 | unless (defined $day) { | ||
| 298 | $weekday++; | ||
| 299 | next; | ||
| 300 | } | ||
| 301 | |||
| 302 | # Make current date bold. | ||
| 303 | $this->{-canvasscr}->attron(A_BOLD) | ||
| 304 | if $this->{-day} == $day and | ||
| 305 | $this->{-month} == $month and | ||
| 306 | $this->{-year} == $year; | ||
| 307 | |||
| 308 | # Make selected date inverse if widget has focus. | ||
| 309 | $this->{-canvasscr}->attron(A_REVERSE) | ||
| 310 | if $this->{-focus} and | ||
| 311 | $this->{-cday} == $day and | ||
| 312 | $this->{-cmonth} == $month and | ||
| 313 | $this->{-cyear} == $year; | ||
| 314 | |||
| 315 | # Draw the day. | ||
| 316 | $this->{-canvasscr}->addstr($y, $weekday*3, sprintf("%2d",$day)); | ||
| 317 | |||
| 318 | # Reset attributes. | ||
| 319 | $this->{-canvasscr}->attroff(A_REVERSE); | ||
| 320 | $this->{-canvasscr}->attroff(A_BOLD); | ||
| 321 | |||
| 322 | $weekday++; | ||
| 323 | if ($weekday == 7) { | ||
| 324 | $weekday = 0; | ||
| 325 | $y++; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | # Move the cursor to the bottomright corner of the widget | ||
| 330 | # (in case the terminal does not support widget hiding). | ||
| 331 | $this->{-canvasscr}->move($this->canvasheight-1, $this->canvaswidth-1); | ||
| 332 | |||
| 333 | $this->{-canvasscr}->noutrefresh(); | ||
| 334 | doupdate() unless $no_doupdate; | ||
| 335 | |||
| 336 | return $this; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub date_selected() | ||
| 340 | { | ||
| 341 | my $this = shift; | ||
| 342 | $this->{-cyear} = $this->{-year}; | ||
| 343 | $this->{-cmonth} = $this->{-month}; | ||
| 344 | $this->{-cday} = $this->{-day}; | ||
| 345 | $this->schedule_draw(1); | ||
| 346 | return $this; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub date_today() | ||
| 350 | { | ||
| 351 | my $this = shift; | ||
| 352 | $this->{-cmonth} = undef; | ||
| 353 | $this->{-cday} = undef; | ||
| 354 | $this->{-cyear} = undef; | ||
| 355 | $this->schedule_draw(1); | ||
| 356 | return $this; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub date_prevyear() | ||
| 360 | { | ||
| 361 | my $this = shift; | ||
| 362 | $this->{-cyear}--; | ||
| 363 | $this->{-cyear} = 0 if $this->{-cyear} < 0; | ||
| 364 | $this->schedule_draw(1); | ||
| 365 | return $this; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub date_nextyear() | ||
| 369 | { | ||
| 370 | my $this = shift; | ||
| 371 | $this->{-cyear}++; | ||
| 372 | $this->{-cyear} = 9999 if $this->{-cyear} > 9999; | ||
| 373 | $this->schedule_draw(1); | ||
| 374 | return $this; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub date_prevmonth() | ||
| 378 | { | ||
| 379 | my $this = shift; | ||
| 380 | $this->{-cmonth}--; | ||
| 381 | if ($this->{-cmonth} < 1 and $this->{-cyear} > 0) { | ||
| 382 | $this->{-cmonth} = 12; | ||
| 383 | $this->{-cyear}--; | ||
| 384 | } | ||
| 385 | $this->schedule_draw(1); | ||
| 386 | return $this; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub date_nextmonth() | ||
| 390 | { | ||
| 391 | my $this = shift; | ||
| 392 | $this->{-cmonth}++; | ||
| 393 | if ($this->{-cmonth} > 12 and $this->{-cyear} < 9999) { | ||
| 394 | $this->{-cmonth} = 1; | ||
| 395 | $this->{-cyear}++; | ||
| 396 | } | ||
| 397 | $this->schedule_draw(1); | ||
| 398 | return $this; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub date_delta_days($;) | ||
| 402 | { | ||
| 403 | my $this = shift; | ||
| 404 | my $delta = shift; | ||
| 405 | |||
| 406 | if ($delta < 0) | ||
| 407 | { | ||
| 408 | my $startday = $this->{-cday}; | ||
| 409 | $this->{-cday} += $delta; | ||
| 410 | if ($this->{-cday} < 1) | ||
| 411 | { | ||
| 412 | if ( ($this->{-cmonth} >= 1 and $this->{-cyear} >= 1) or | ||
| 413 | ($this->{-cmonth} >= 2 and $this->{-cyear} >= 0) ) | ||
| 414 | { | ||
| 415 | $this->date_prevmonth(); | ||
| 416 | my $days = days_in_month($this->{-cyear}, $this->{-cmonth}); | ||
| 417 | $this->{-cday} = $startday + $delta + $days; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | } else { | ||
| 421 | my $days = days_in_month($this->{-cyear}, $this->{-cmonth}); | ||
| 422 | my $newday = $this->{-cday} + $delta - $days; | ||
| 423 | $this->{-cday} += $delta; | ||
| 424 | if ($this->{-cday} > $days and | ||
| 425 | $this->{-cyear} < 9999) | ||
| 426 | { | ||
| 427 | $this->date_nextmonth(); | ||
| 428 | $this->{-cday} = $newday; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | if ($this->{-cyear} == 1752 and $this->{-cmonth} == 9) { | ||
| 433 | if ($this->{-cday} > 2 and $this->{-cday} < 14) { | ||
| 434 | $this->{-cday} = ($delta > 0 ? 14 : 2); | ||
| 435 | } | ||
| 436 | } | ||
| 437 | $this->schedule_draw(1); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub date_prevweek() | ||
| 441 | { | ||
| 442 | my $this = shift; | ||
| 443 | $this->date_delta_days(-7); | ||
| 444 | $this->schedule_draw(1); | ||
| 445 | return $this; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub date_nextweek() | ||
| 449 | { | ||
| 450 | my $this = shift; | ||
| 451 | $this->date_delta_days(+7); | ||
| 452 | $this->schedule_draw(1); | ||
| 453 | return $this; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub date_prevday() | ||
| 457 | { | ||
| 458 | my $this = shift; | ||
| 459 | $this->date_delta_days(-1); | ||
| 460 | $this->schedule_draw(1); | ||
| 461 | return $this; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub date_nextday() | ||
| 465 | { | ||
| 466 | my $this = shift; | ||
| 467 | $this->date_delta_days(+1); | ||
| 468 | $this->schedule_draw(1); | ||
| 469 | return $this; | ||
| 470 | } | ||
| 471 | |||
| 472 | sub date_select() | ||
| 473 | { | ||
| 474 | my $this = shift; | ||
| 475 | $this->{-day} = $this->{-cday}; | ||
| 476 | $this->{-month} = $this->{-cmonth}; | ||
| 477 | $this->{-year} = $this->{-cyear}; | ||
| 478 | $this->schedule_draw(1); | ||
| 479 | $this->run_event('-onchange'); | ||
| 480 | return $this; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub mouse_button($$$$;) | ||
| 484 | { | ||
| 485 | my $this = shift; | ||
| 486 | my $event = shift; | ||
| 487 | my $x = shift; | ||
| 488 | my $y = shift; | ||
| 489 | |||
| 490 | # Click in the day area? | ||
| 491 | if ($y > 3 and $y < 10) | ||
| 492 | { | ||
| 493 | my @month = build_month($this->{-cyear}, $this->{-cmonth}); | ||
| 494 | my $weekday = 0; | ||
| 495 | my $ty = 4; | ||
| 496 | foreach my $day (@month) | ||
| 497 | { | ||
| 498 | unless (defined $day) { $weekday++; next } | ||
| 499 | |||
| 500 | my ($dx, $dy) = ($weekday*3, $ty); | ||
| 501 | |||
| 502 | if ($x >= $dx and $x < $dx+2 and $y == $dy) { | ||
| 503 | $this->{-cday} = $day; | ||
| 504 | $this->date_select(1); | ||
| 505 | $this->focus(); | ||
| 506 | last; | ||
| 507 | } | ||
| 508 | |||
| 509 | $weekday++; | ||
| 510 | if ($weekday == 7) { | ||
| 511 | $weekday = 0; | ||
| 512 | $ty++; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | # Click on the year? | ||
| 518 | elsif ($y == 0 and | ||
| 519 | $x > ($this->canvaswidth-5) and | ||
| 520 | $x < $this->canvaswidth) | ||
| 521 | { | ||
| 522 | # Select year | ||
| 523 | if ( $event->{-bstate} == BUTTON3_CLICKED() ) { | ||
| 524 | $this->date_nextyear; | ||
| 525 | } else { | ||
| 526 | $this->date_prevyear; | ||
| 527 | } | ||
| 528 | $this->focus(); | ||
| 529 | } | ||
| 530 | |||
| 531 | # Click on the month? | ||
| 532 | elsif ( $y == 0 and | ||
| 533 | $x >= 0 and | ||
| 534 | $x < length($months[$this->{-cmonth}]) ) | ||
| 535 | { | ||
| 536 | if ( $event->{-bstate} == BUTTON3_CLICKED() ) { | ||
| 537 | $this->date_nextmonth; | ||
| 538 | } else { | ||
| 539 | $this->date_prevmonth; | ||
| 540 | } | ||
| 541 | $this->focus(); | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | |||
| 546 | return $this; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub get() | ||
| 550 | { | ||
| 551 | my $this = shift; | ||
| 552 | $this->make_sane_date(); | ||
| 553 | return sprintf("%04d-%02d-%02d", | ||
| 554 | $this->{-year}, $this->{-month}, $this->{-day}); | ||
| 555 | } | ||
| 556 | |||
| 557 | # ---------------------------------------------------------------------- | ||
| 558 | # Date calculation | ||
| 559 | # ---------------------------------------------------------------------- | ||
| 560 | |||
| 561 | my @days_in_month = (undef,31,28,31,30,31,30,31,31,30,31,30,31); | ||
| 562 | |||
| 563 | sub is_julian ($$;) | ||
| 564 | { | ||
| 565 | my ($year, $month) = @_; | ||
| 566 | return $year < 1752 or ($year == 1752 and $month <= 9); | ||
| 567 | } | ||
| 568 | |||
| 569 | sub day_of_week($$$;) | ||
| 570 | { | ||
| 571 | my $year = shift; | ||
| 572 | my $month = shift; | ||
| 573 | my $day = shift; | ||
| 574 | |||
| 575 | my $a = int( (14 - $month)/12 ); | ||
| 576 | my $y = $year - $a; | ||
| 577 | my $m = $month + (12 * $a) - 2; | ||
| 578 | my $day_of_week; | ||
| 579 | if (is_julian($year, $month)) | ||
| 580 | { | ||
| 581 | $day_of_week = ( | ||
| 582 | 5 | ||
| 583 | + $day | ||
| 584 | + $y + int($y/4) | ||
| 585 | + int(31*$m/12) | ||
| 586 | ) % 7; | ||
| 587 | } else { | ||
| 588 | $day_of_week = ( | ||
| 589 | $day | ||
| 590 | + $y + int($y/4) | ||
| 591 | - int($y/100) | ||
| 592 | + int($y/400) | ||
| 593 | + int(31*$m/12) | ||
| 594 | ) % 7; | ||
| 595 | } | ||
| 596 | |||
| 597 | return $day_of_week; | ||
| 598 | } | ||
| 599 | |||
| 600 | sub days_in_month($$;) | ||
| 601 | { | ||
| 602 | my $year = shift; | ||
| 603 | my $month = shift; | ||
| 604 | |||
| 605 | if($month == 2 and is_leap_year($year)) { | ||
| 606 | return 29; | ||
| 607 | } else { | ||
| 608 | return $days_in_month[$month]; | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | sub is_leap_year($;) | ||
| 613 | { | ||
| 614 | my $year = shift; | ||
| 615 | |||
| 616 | if (is_julian($year,1)) { | ||
| 617 | return 1 if $year % 4 == 0; | ||
| 618 | } else { | ||
| 619 | return 1 if ($year % 4 == 0 and $year % 100 != 0) | ||
| 620 | or $year % 400 == 0; | ||
| 621 | } | ||
| 622 | return 0; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub build_month ($$;) | ||
| 626 | { | ||
| 627 | my $year = shift; | ||
| 628 | my $month = shift; | ||
| 629 | |||
| 630 | my $first_weekday = day_of_week($year, $month, 1); | ||
| 631 | my $number_of_days = days_in_month($year, $month); | ||
| 632 | |||
| 633 | if ($year == 1752 and $month == 9) { | ||
| 634 | $number_of_days = 19; | ||
| 635 | } | ||
| 636 | |||
| 637 | my @month = (); | ||
| 638 | for (1..$first_weekday) { | ||
| 639 | push @month, undef; | ||
| 640 | } | ||
| 641 | |||
| 642 | my $realday = 1; | ||
| 643 | for( my $day = 1; $day <= $number_of_days; $day++ ) | ||
| 644 | { | ||
| 645 | push @month, $realday; | ||
| 646 | if ($year == 1752 and $month == 9 and $realday == 2) { | ||
| 647 | $realday = 13; | ||
| 648 | } | ||
| 649 | $realday++; | ||
| 650 | } | ||
| 651 | |||
| 652 | return @month; | ||
| 653 | } | ||
| 654 | |||
| 655 | 1; | ||
| 656 | |||
| 657 | |||
| 658 | =pod | ||
| 659 | |||
| 660 | =head1 NAME | ||
| 661 | |||
| 662 | Curses::UI::Calendar - Create and manipulate calendar widgets | ||
| 663 | |||
| 664 | =head1 CLASS HIERARCHY | ||
| 665 | |||
| 666 | Curses::UI::Widget | ||
| 667 | | | ||
| 668 | +----Curses::UI::Calendar | ||
| 669 | |||
| 670 | |||
| 671 | =head1 SYNOPSIS | ||
| 672 | |||
| 673 | use Curses::UI; | ||
| 674 | my $cui = new Curses::UI; | ||
| 675 | my $win = $cui->add('window_id', 'Window'); | ||
| 676 | |||
| 677 | my $calendar = $win->add( | ||
| 678 | 'mycalendar', 'Calendar', | ||
| 679 | -date => '2002-1-14' | ||
| 680 | ); | ||
| 681 | |||
| 682 | $calendar->focus(); | ||
| 683 | my $date = $calendar->get(); | ||
| 684 | |||
| 685 | |||
| 686 | =head1 DESCRIPTION | ||
| 687 | |||
| 688 | Curses::UI::Calendar is a widget that can be used to create | ||
| 689 | a calendar in which the user can select a date. The calendar | ||
| 690 | widget looks like this: | ||
| 691 | |||
| 692 | +----------------------+ | ||
| 693 | | mmm dd yyyy | | ||
| 694 | +----------------------+ | ||
| 695 | | su mo tu we th fr sa | | ||
| 696 | | | | ||
| 697 | | 01 02 03 04 05 | | ||
| 698 | | 06 07 08 09 10 11 12 | | ||
| 699 | | 13 14 15 16 17 18 19 | | ||
| 700 | | 20 21 22 23 24 25 26 | | ||
| 701 | | 27 28 29 30 31 | | ||
| 702 | +----------------------+ | ||
| 703 | |||
| 704 | See exampes/demo-Curses::UI::Calendar in the distribution | ||
| 705 | for a short demo. | ||
| 706 | |||
| 707 | |||
| 708 | |||
| 709 | =head1 STANDARD OPTIONS | ||
| 710 | |||
| 711 | B<-parent>, B<-x>, B<-y>, B<-width>, B<-height>, | ||
| 712 | B<-pad>, B<-padleft>, B<-padright>, B<-padtop>, B<-padbottom>, | ||
| 713 | B<-ipad>, B<-ipadleft>, B<-ipadright>, B<-ipadtop>, B<-ipadbottom>, | ||
| 714 | B<-title>, B<-titlefullwidth>, B<-titlereverse>, B<-onfocus>, | ||
| 715 | B<-onblur> | ||
| 716 | |||
| 717 | For an explanation of these standard options, see | ||
| 718 | L<Curses::UI::Widget|Curses::UI::Widget>. | ||
| 719 | |||
| 720 | B<Remark>: B<-width> and B<-height> can be set, but this widget | ||
| 721 | really want to have its content space at a minimum size. If your | ||
| 722 | B<-width> or B<-height> is not large enough, the widget will | ||
| 723 | automatically fix its value. | ||
| 724 | |||
| 725 | |||
| 726 | |||
| 727 | |||
| 728 | =head1 WIDGET-SPECIFIC OPTIONS | ||
| 729 | |||
| 730 | =over 4 | ||
| 731 | |||
| 732 | =item * B<-date> < DATE > | ||
| 733 | |||
| 734 | This option sets the date to start with. | ||
| 735 | If you do not specify a date, today's | ||
| 736 | date will be used automatically. The format that | ||
| 737 | you can use for this date is one of: | ||
| 738 | |||
| 739 | * B<YYYY-M-D> (e.g. 2002-1-10 or 2002-01-10) | ||
| 740 | |||
| 741 | * B<YYYY/M/D> (e.g. 2002/1/10 or 2002/01/10)) | ||
| 742 | |||
| 743 | * B<YYYYMMDD> (e.g. 20020110) | ||
| 744 | |||
| 745 | * B<D-M-YYYY> (e.g. 10-1-2002 or 10/01/2002) | ||
| 746 | |||
| 747 | * B<D/M/YYYY> (e.g. 10/1/2002 or 10/01/2002) | ||
| 748 | |||
| 749 | =item * B<-onchange> < CODEREF > | ||
| 750 | |||
| 751 | This sets the onChange event handler for the calendar widget. | ||
| 752 | If a new date is selected, the code in CODEREF will be executed. | ||
| 753 | It will get the widget reference as its argument. | ||
| 754 | |||
| 755 | =item * B<-drawline> < CODEREF > | ||
| 756 | |||
| 757 | This option specifies whether or not a line should be drawn under | ||
| 758 | the calendar. | ||
| 759 | |||
| 760 | =back | ||
| 761 | |||
| 762 | |||
| 763 | |||
| 764 | |||
| 765 | =head1 METHODS | ||
| 766 | |||
| 767 | =over 4 | ||
| 768 | |||
| 769 | =item * B<new> ( OPTIONS ) | ||
| 770 | |||
| 771 | =item * B<layout> ( ) | ||
| 772 | |||
| 773 | =item * B<draw> ( BOOLEAN ) | ||
| 774 | |||
| 775 | =item * B<focus> ( ) | ||
| 776 | |||
| 777 | =item * B<onFocus> ( CODEREF ) | ||
| 778 | |||
| 779 | =item * B<onBlur> ( CODEREF ) | ||
| 780 | |||
| 781 | =item * B<intellidraw> ( ) | ||
| 782 | |||
| 783 | These are standard methods. See L<Curses::UI::Widget|Curses::UI::Widget> | ||
| 784 | for an explanation of these. | ||
| 785 | |||
| 786 | =item * B<get> ( ) | ||
| 787 | |||
| 788 | This method will return the currently selected date in the | ||
| 789 | format 'YYYY-MM-DD'. | ||
| 790 | |||
| 791 | =item * B<setdate> ( DATE, [BOOLEAN] ) | ||
| 792 | |||
| 793 | Set the selected date of the widget to DATE. See B<-date> above for | ||
| 794 | the possible formats. The widget will redraw itself, unless BOOLEAN | ||
| 795 | has a true value. | ||
| 796 | |||
| 797 | =item * B<onChange> ( CODEREF ) | ||
| 798 | |||
| 799 | This method can be used to set the B<-onchange> event handler | ||
| 800 | (see above) after initialization of the calendar. | ||
| 801 | |||
| 802 | =back | ||
| 803 | |||
| 804 | |||
| 805 | |||
| 806 | |||
| 807 | =head1 DEFAULT BINDINGS | ||
| 808 | |||
| 809 | =over 4 | ||
| 810 | |||
| 811 | =item * <B<tab>> | ||
| 812 | |||
| 813 | Call the 'loose-focus' routine. This will have the menubar | ||
| 814 | loose its focus and return the value 'LOOSE_FOCUS' to | ||
| 815 | the calling routine. | ||
| 816 | |||
| 817 | =item * <B<enter>>, <B<space>> | ||
| 818 | |||
| 819 | Call the 'date-select' routine. This will select the date on | ||
| 820 | which the cursor is. | ||
| 821 | |||
| 822 | =item * <B<cursor-left>>, <B<h>> | ||
| 823 | |||
| 824 | Call the 'date-prevday' routine. This will have the date | ||
| 825 | cursor go back one day. | ||
| 826 | |||
| 827 | =item * <B<cursor-right>, <B<l>> | ||
| 828 | |||
| 829 | Call the 'date-nextday' routine. This will have the | ||
| 830 | date cursor go forward one day. | ||
| 831 | |||
| 832 | =item * <B<cursor-down>>, <B<j>> | ||
| 833 | |||
| 834 | Call the 'date-nextweek' routine. This will have the | ||
| 835 | date cursor go forward one week. | ||
| 836 | |||
| 837 | =item * <B<cursor-up>>, <B<k>> | ||
| 838 | |||
| 839 | Call the 'date-prevweek' routine. This will have the | ||
| 840 | date cursor go back one week. | ||
| 841 | |||
| 842 | =item * <B<page-up>>, <B<SHIFT+K>> | ||
| 843 | |||
| 844 | Call the 'date-prevmonth' routine. This will have the | ||
| 845 | date cursor go back one month. | ||
| 846 | |||
| 847 | =item * <B<page-down>>, <B<SHIFT+J>> | ||
| 848 | |||
| 849 | Call the 'date-nextmonth' routine. This will have the | ||
| 850 | date cursor go forward one month. | ||
| 851 | |||
| 852 | =item * <B<p>>, <B<SHIFT+H>> | ||
| 853 | |||
| 854 | Call the 'date-prevyear' routine. This will have the | ||
| 855 | date cursor go back one year. | ||
| 856 | |||
| 857 | =item * <B<n>>, <B<SHIFT+L>> | ||
| 858 | |||
| 859 | Call the 'date-nextyear' routine. This will have the | ||
| 860 | date cursor go forward one year. | ||
| 861 | |||
| 862 | =item * <B<home>>, <B<CTRL+A>>, <B<c>> | ||
| 863 | |||
| 864 | Call the 'date-selected' routine. This will have the | ||
| 865 | date cursor go to the current selected date. | ||
| 866 | |||
| 867 | =item * <B<t>> | ||
| 868 | |||
| 869 | Call the 'date-today' routine. This will have the date cursor | ||
| 870 | go to today's date. | ||
| 871 | |||
| 872 | =back | ||
| 873 | |||
| 874 | |||
| 875 | |||
| 876 | |||
| 877 | |||
| 878 | =head1 SEE ALSO | ||
| 879 | |||
| 880 | L<Curses::UI|Curses::UI>, | ||
| 881 | L<Curses::UI::Widget|Curses::UI::Widget>, | ||
| 882 | L<Curses::UI::Common|Curses::UI::Common> | ||
| 883 | |||
| 884 | |||
| 885 | |||
| 886 | |||
| 887 | =head1 AUTHOR | ||
| 888 | |||
| 889 | Copyright (c) 2001-2002 Maurice Makaay. All rights reserved. | ||
| 890 | |||
| 891 | Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de) | ||
| 892 | |||
| 893 | |||
| 894 | This package is free software and is provided "as is" without express | ||
| 895 | or implied warranty. It may be used, redistributed and/or modified | ||
| 896 | under the same terms as perl itself. | ||
| 897 |