[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Clock



#!perl -w

use Mac::Events;
use Mac::Windows;
use Mac::QuickDraw;
use Mac::Menus;
use Time::Local;

@divs = (.4, .75, .90);
$showtext = 1;
$showtitle = 1;
$pi = 3.14159;
$pi2 = $pi * 2;
$wrect = new Rect (100, 100, 300, 250);

$lgray  = new RGBColor ((50000) x 3);
$dgray  = new RGBColor ((10000) x 3);
$black = new RGBColor ((0) x 3);
$white = new RGBColor ((65535) x 3);

sub InitWin {
  my ($rect, $title) = @_;

  $win->dispose if $win;
  $win = new MacColorWindow (
    $rect,
    'Clock',
    1,
    $title ? documentProc : plainDBox,
    1,                  # Anybody know how to write a round WDEF?
  );

  $win->sethook(layout => sub {
    my $wRect = $win->window->contRgn->rgnBBox;
    SetPort($win->window);
    my $sPt = GlobalToLocal(new Point ($wRect->right, $wRect->bottom));

    $midh = $sPt->h * .50;
    $midv = ($sPt->v - ($showtext ? 20 : 0)) * .50;
    $mdv = $midv - 10;
    $mdh = $mdv;
    if ($mdh > $sPt->h / 2 - 10) {
      $mdh = $sPt->h / 2 - 10;
      $mdv = $mdh;
    }
    $tpos = $sPt->v - 10;

    for (0..2) {
      $hands[$_]{len}[0] = $mdh * $divs[$_]; 
      $hands[$_]{len}[1] = $mdv * $divs[$_];
      $hands[$_]{ang} = rand() * $pi;
      $hands[$_]{begin} = [0, 0];
      $hands[$_]{end} = [0, 0];
    }
    # Indices are $hands[ which hand ]{ len, tip }[ x or y coord ].

    calc_hands();
    InvalRect new Rect (0, 0, $sPt->h, $sPt->v);
  });
  $win->layout;
  $win->sethook(drawgrowicon => sub {});

  RGBBackColor($lgray);
  $win->sethook(redraw => sub {
    RGBForeColor($white);
    FrameOval(new Rect($midh - $mdh + 1, $midv - $mdv + 1, $midh + $mdh + 1,
$midv + $mdv + 1));
    RGBForeColor($dgray);
    FrameOval(new Rect($midh - $mdh, $midv - $mdv, $midh + $mdh, $midv + $mdv));
    RGBForeColor($black);
    foreach (@hands) {
      MoveTo($midh, $midv);
      LineTo(@{$_->{tip}});
    }
    if ($showtext) {
      MoveTo($midh - StringWidth($tstr) / 2, $tpos);
      DrawString $tstr;
    }
  });

  $win->sethook(click => sub {
  if ($Mac::Events::CurrentEvent->modifiers & controlKey) {
      my ($mw, $pt) = @_;
      $m->insert;
      my $mp = LocalToGlobal(GetMouse);
      PopUpMenuSelect $m->{menu}, $mp->v, $mp->h, 1;
      $m->delete;
    } else {
      DragWindow($win->window, $Mac::Events::CurrentEvent->where);
    }
  });
}
InitWin($wrect, $showtitle);

sub calc_hands {
  foreach (@hands) {
    $_->{tip}[0] = sin($_->{ang})*$_->{len}[0]+$midh;
    $_->{tip}[1] = -cos($_->{ang})*$_->{len}[1]+$midv;
    @box = map {($$_[0] < $$_[1]) ? $_ : [$$_[1], $$_[0]]} ([$_->{tip}[0],
$midh], [$_->{tip}[1], $midv]);
    $_->{begin} = [$box[0][0], $box[1][0]];
    $_->{end} = [$box[0][1], $box[1][1]];
  }
}

$m = new MacHierMenu 2000, '', (
  ['Text Time' => sub {
    $showtext = !$showtext;
    $win->layout;
    SetItemMark $m->{menu}, 1, $showtext ? "\cR" : '';
  }],
  ['Title Bar' => sub {
    $showtitle = !$showtitle;
    InitWin($win->window->contRgn->rgnBBox, $showtitle);
    SetItemMark $m->{menu}, 2, $showtitle ? "\cR" : '';
  }],
);
SetItemMark $m->{menu}, 1, $showtext ? "\cR" : '';
SetItemMark $m->{menu}, 2, $showtitle ? "\cR" : '';

$t = $tt = 0;
while ($win->window) {
  if ($t != ($tt = time) and $t = $tt) {
    SetPort($win->window);
    foreach (@hands) {
      InvalRect new Rect (@{$_->{begin}}, map $_+1, @{$_->{end}});
    }
    #($sec, $min, $hr) = localtime;
    #$hands[0]{ang} = $hr % 12 / 12 * $pi2;
    #$hands[1]{ang} = $min     / 60 * $pi2;
    #$hands[2]{ang} = $sec     / 60 * $pi2;

    $tstr = scalar localtime;
    $secs_today = timegm(localtime) % 86400;
    $hands[0]{ang} = $secs_today / 21600 * $pi2;
    $hands[1]{ang} = $secs_today / 3600 * $pi2;
    $hands[2]{ang} = $secs_today / 60 * $pi2;

    calc_hands();
    foreach (@hands) {
      InvalRect new Rect (@{$_->{begin}}, map $_+1, @{$_->{end}});
    }
    InvalRect new Rect ($midh - StringWidth($tstr) / 2, $tpos - 10, $midh +
StringWidth($tstr) / 2, $tpos + 10)
      if $showtext;
  }
  WaitNextEvent;
}

END {$win->dispose}

__END__

-- 
  Kevin Reid.      |         Macintosh.
   "I'm me."       |      Think different.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch