'===========================================================================
' Subject: Digital/Analog Clock w/Sound       Date: 06-15-03 (  :  )       
'  Author: Achilles b. Mina                   Code: RAPIDQ                 
'  Origin: wasaywasay@edsamail.com.ph       Packet: DATETIME.ABC
'===========================================================================
'Alarmed v1.00
'Uploaded 5/19/03
'By Achilles B. Mina

'Alarmed is yet another alarm clock. It demonstrates Rapid-Q's talent 
'for producing highly useful utilities easily and, unfortunately, also 
'shows the author's lack of talent for innovative programming. 
'You can do as you please with this code but kindly acknowledge 
'the talentless author.

'To run the program, provide .wav files of the stated filenames
'in you current directory

$RESOURCE birds AS "birds.wav"
$RESOURCE parrot AS "parrot.wav"
$RESOURCE clicker AS "click.wav"

DECLARE SUB InitAlarm
DECLARE SUB CountdownMode
DECLARE SUB Off
DECLARE SUB Click
DECLARE SUB CheckAMPM
DECLARE SUB Reminded
DECLARE SUB AlarmHow
DECLARE SUB AlarmTime
DECLARE SUB Countdown
DECLARE SUB DigitClock
DECLARE SUB AlarmPaint
DECLARE SUB Sweep
DECLARE SUB About
DECLARE SUB GetMM
DECLARE SUB PlayAlarm
DECLARE SUB Help

DECLARE FUNCTION SetWindowLong Lib "User32" Alias "SetWindowLongA" _
(hwnd as Long, nIndex as Long, dwNewLong as Long) as Long

DECLARE FUNCTION GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA"(ByVal lpszLongPath As String, _
ByVal lpszShortPath As long, ByVal lBuffer As Long) As Long

DECLARE FUNCTION PlayMedia Lib "winmm.dll" _
Alias "mciSendStringA"(ByVal lpstrCommand As String, _
lpstrReturnString As Long, uReturnLength As _
Long, hwndCallback As Long) As Long

DEFINT hour, min, sec, runcount
DEFBYTE clicked, alarmed
DEFSTR ampm$, AlarmFileName$

DIM AlarmEdit(1 TO 3) AS QEDIT
DIM AlarmLabel(1 TO 2) AS QLABEL
DIM AMPM(1 TO 2) AS QRADIOBUTTON
DIM mAudio AS STRING*256

CREATE Clock AS QTIMER
  Interval = 250
  Enabled = 1
  OnTimer = CountdownMode
END CREATE

CREATE AlarmForm AS QFORM
  Height = 203
  Center
  Color = 100050001
  DelBorderIcons(2)
  OnClose = Off
  CREATE Label AS QLABEL
    Align = 1
    Alignment = 2
    LabelStyle = 2
    Font.Name = "Arial"
    Font.Size = 40
  END CREATE
  CREATE Label1 AS QLABEL
    Top = 85
    Left = 45
    Alignment = 2
    LabelStyle = 2
    Font.Name = "Arial"
    Font.Size = 15
    Caption = "Alarm at:"
    Hint = "Click to toggle countdown mode"
    ShowHint = 1
    Cursor = -21
    OnClick = AlarmHow
  END CREATE
  CREATE Remind AS QLABEL
    Top = 112
    Left = 184
    Alignment = 2
    LabelStyle = 1
    Font.Name = "Arial"
    Font.Size = 10
    Font.Color = 390909
    Caption = "Click AM or PM"
  END CREATE
  CREATE GetButt AS QCOOLBTN
    Top = 145
    Left = 35
    Width = 40
    Font.Name = "Arial"
    Font.Size = 10
    Font.Color = &HFFFFFF
    Caption = "Open"
    Flat = 1
    Cursor = -21
    Hint = "Choose another alarm sound"
    ShowHint = 1
    OnClick = GetMM
  END CREATE
  CREATE OffButt AS QCOOLBTN
    Top = 145
    Left = 81
    Width = 40
    Font.Name = "Arial"
    Font.Size = 10
    Font.Color = &HFFFFFF
    Caption = "On/Off"
    Flat = 1
    Cursor = -21
    Hint = "Alarm is enabled"
    ShowHint = 1
    OnClick = Click
  END CREATE
  CREATE AboutButt AS QCOOLBTN
    Top = 145
    Left = 127
    Width = 40
    Font.Name = "Arial"
    Font.Size = 10
    Font.Color = &HFFFFFF
    Caption = "Help"
    Flat = 1
    Cursor = -21
    Hint = "Click to know how to use Alarmed"
    ShowHint = 1
    OnClick = Help
  END CREATE
  CREATE AlarmClock AS QDXSCREEN
    Top = 53
    Left = 178
    Cursor = -21
    Hint = "About"
    Showhint = 1
    OnClick = About
  END CREATE
  CREATE AlarmHelp AS QFORM
    Height = 203
    Width = 150
    Top = (Screen.Height - AlarmHelp.Height)*.5
    Left = Screen.Width*.5 + 160
    BorderStyle = 3
    Caption = "Help"
    Visible = 0
    CREATE AlarmRich AS QRICHEDIT
      Align = 5
      ReadOnly = 1
      ScrollBars = 3
      AddStrings "Welcome!"
      AddStrings ""
      AddStrings "Alarmed is a simple hint-based alarm clock. _
                  To operate it, simply take note of the bubble hints."
      AddStrings ""
      AddStrings "To set the alarm, just type in the hour, _
                  minutes and seconds when you want it to _
                  go off. Seconds is optional."
      AddStrings ""
      AddStrings "There are two ways the alarm can go off: _
                  when the time itself is reached, or after _
                  a certain period has elapsed. Either mode can _
                  be set by clicking Alarm at:"
      AddStrings ""
      AddStrings "You can also change the alarm itself: just click _
                  Open. You can choose all kinds of sounds, including _
                  MP3, midi and even movies!"
      AddStrings ""
      AddStrings "In fact, you don't even have to choose a particular sound. _
                  Alarmed defaults to a parrot call--and that should _
                  wake up even the dead."
      Line(0) = "Welcome!"                
    END CREATE
  END CREATE
END CREATE


SUB InitAlarm
  SetWindowLong(AlarmForm.Handle,-8,0)
  SetWindowLong(Application.Handle,-8,AlarmForm.Handle)
  AlarmEditLeft = -5
  AlarmLabelLeft = 28
  FOR x = 1 TO 3
    AlarmEdit(x).Parent = AlarmForm
    AlarmEdit(x).Top = 110 
    AlarmEdit(x).Left = AlarmEditLeft + 45
    AlarmEditLeft = AlarmEdit(x).Left
    AlarmEdit(x).Width = 30
    IF x = 1 THEN
      AlarmEdit(x).Hint = "Hour"
    ELSEIF x = 2 THEN
      AlarmEdit(x).Hint = "Minutes"
    ELSEIF x = 3 THEN
      AlarmEdit(x).Hint = "Seconds"
    END IF
    AlarmEdit(x).ShowHint = 1
    AlarmEdit(x).Font.Name = "Arial"
    AlarmEdit(x).Font.Size = 15
    IF x < 3 THEN
      AlarmLabel(x).Parent = AlarmForm
      AlarmLabel(x).Top = 105
      AlarmLabel(x).Left = AlarmLabelLeft + 45
      AlarmLabelLeft = AlarmLabel(x).Left 
      AlarmLabel(x).Width = 10
      AlarmLabel(x).Alignment = 2
      AlarmLabel(x).LabelStyle = 2
      AlarmLabel(x).Font.Name = "Arial"
      AlarmLabel(x).Font.Size = 25
      AlarmLabel(x).Caption = ":"
      AMPM(x).Parent = AlarmForm 
      AMPM(x).Top = 148
      AMPM(x).Width = 60
      AMPM(x).Font.Name = "Arial"
      AMPM(x).Font.Size = 15
      IF x = 1 THEN
        AMPM(x).Left = 175
        AMPM(x).Caption = "AM"
      ELSE
        AMPM(x).Left = 230
        AMPM(x).Caption = "PM"
      END IF
      AMPM(x).OnClick = Reminded
    END IF
  NEXT
  AlarmEdit(2).OnChange = CheckAMPM
END SUB

SUB CheckAMPM
  IF Label1.Caption = "Alarm at:" THEN
    IF AMPM(1).Checked = 0 AND AMPM(2).Checked = 0 THEN
      PLAYWAV birds,1
      AlarmClock.Visible = 0
    END IF
  END IF
END SUB

SUB DigitClock
  hour =  VAL(LEFT$(TIME$,2))
  min = VAL(MID$(TIME$,4,2))
  sec = VAL(RIGHT$(TIME$,2))     
  IF hour >= 12 AND hour < 24 THEN
    hour = hour - 12
    IF hour = 0 THEN hour = 12
    ampm$ = " PM"
  ELSEIF hour = 0 THEN 
    hour = 12
    ampm$ = " AM"
  ELSE
    ampm$ = " AM"
  END IF
  thetime$ = STR$(hour) + MID$(TIME$,3,3) + RIGHT$(TIME$,3) + ampm$
  Label.Caption = thetime$
  AlarmForm.Caption = "Alarmed                               " + DATE$
END SUB

SUB AlarmTime
  DigitClock
  IF clicked = 1 THEN EXIT SUB
  IF alarmed = 0 THEN
    IF VAL(AlarmEdit(1).Text) = hour THEN
      IF VAL(AlarmEdit(2).Text) = min THEN
        IF VAL(AlarmEdit(3).Text) = sec THEN
          IF (AMPM(1).Checked = 1 AND ampm$ = " AM") OR _
          (AMPM(2).Checked = 1 AND ampm$ = " PM") THEN 
            PlayAlarm
            alarmed = 1
          END IF
        END IF
      END IF
    END IF
  ELSE
    PlayAlarm
  END IF
END SUB

SUB Countdown
  DigitClock
  IF clicked = 1 THEN EXIT SUB
  IF runcount = 0 THEN
    hcount = VAL(AlarmEdit(1).Text)
    mcount = VAL(AlarmEdit(2).Text)
    scount = VAL(AlarmEdit(3).Text)
    count = hcount*3600 + mcount*60 + scount
  END IF
  IF runcount >= count AND count > 0 THEN 
    PLAYWAV parrot,1
  END IF
  INC runcount
END SUB

SUB Click
  PLAYWAV clicker,1
  IF clicked = 0 THEN
    clicked = 1
    OffButt.Hint = "Alarm is disabled"
  ELSE
    clicked = 0
    alarmed = 0
    runcount = 0
    OffButt.Hint = "Alarm is enabled"
  END IF
END SUB

SUB Reminded
  PLAYWAV clicker,1
  AlarmClock.Visible = 1
END SUB

SUB AlarmHow
  PLAYWAV clicker,1
  IF Label1.Caption = "Alarm at:" THEN
    Label1.Caption = "Alarm after:"
  ELSE
    Label1.Caption = "Alarm at:"
  END IF
END SUB

SUB CountdownMode
  IF Label1.Caption = "Alarm at:" THEN
    AlarmTime
  ELSE
    Countdown
  END IF
  Sweep
END SUB 

SUB Sweep
  AlarmForm.TextOut(90,59,TIME$,0,100050001)
  incsec = sec*6 - 90
  incmin = min*6 - 90
  IF hour = 12 THEN hour = 0 
  inchour = (hour + min/60)*30 - 90 
  anglesec = 0.0175*incsec  '3.14153/180
  anglemin = 0.0175*incmin
  anglehour = 0.0175*inchour
  xsec = 40*cos(anglesec) + 50
  ysec = 40*sin(anglesec) + 48 
  xmin = 40*cos(anglemin) + 50
  ymin = 40*sin(anglemin) + 48
  IF hour < 7 THEN
    xhour = SQR(ABS(625-(25*sin(anglehour))^2)) + 50
  ELSE
    xhour = -SQR(ABS(625-(25*sin(anglehour))^2)) + 50
  END IF
  yhour = 25*sin(anglehour) + 48
  AlarmClock.Circle(6,4,94,92,&HFFFFF,400050004)
  AlarmClock.Circle(48,8,52,12,&HFFFFFF,&HFFFFFF)
  AlarmClock.Circle(48,84,52,88,&HFFFFFF,&HFFFFFF)
  AlarmClock.Circle(10,48,14,52,&HFFFFFF,&HFFFFFF)
  AlarmClock.Circle(86,48,90,52,&HFFFFFF,&HFFFFFF)
  AlarmClock.Line(50,48,xsec,ysec,&HFFFFFF)
  AlarmClock.Line(50,48,xmin,ymin,&HFFFB)
  AlarmClock.Line(50,48,xhour,yhour,&HFFFB)
  AlarmClock.Flip      
END SUB

SUB GetMM
  CREATE OpenMM As QOPENDIALOG
    Filter = "Multimedia files|*.m3u;*.rmi;*.midi;*.mid;*.wav;*.au;*.snd;*.mpa;*.wma;_
      *.mp2;*.mp3;*.asx;*.asf;*.aif;*.iff;*.mpm;*.m1v;*.mpeg;*.mpg;*.avi;*.mov;*.wmv;*.mpv;*.qt;*.dat|_
      Audio files|*.m3u;*.rmi;*.midi;*.mid;*.wav;*.au;*.snd;*.wma;_
      *.mp2;*.mp3;*.aif;*.iff|All files|*.*"
  END CREATE
  IF OpenMM.Execute = 1 THEN
    filePath$ = STRING$(165,0)
    lenFilename = GetShortPathName_
    (OpenMM.FileName,VARPTR(filePath$),164)
    AlarmFileName$ = LEFT$(filePath$,lenFilename)
  END IF
END SUB

SUB PlayAlarm
  IF AlarmFileName$ = "" THEN 
    PLAYWAV parrot,1
  ELSE
    PlayMedia("open " + AlarmFileName$ + " type MpegVideo",0,0,0)
    PlayMedia("play " + AlarmFileName$ + " from 0",0,0,0)
    DO
      DOEVENTS
      PlayMedia("status " + AlarmFileName$ + " mode",VARPTR(mAudio),256,0)
    LOOP UNTIL LEFT$(mAudio,7) = "stopped" OR clicked = 1 
    PlayMedia("close " + AlarmFileName$,0,0,0)
  END IF
END SUB

SUB About
  MESSAGEBOX ("  This is freeware." + CHR$(13) +_
  "Copyright (c) 2003" + CHR$(13) +_ 
  "  Achilles B. Mina" + CHR$(13) + CHR$(13) +_ 
  "     Alatmed is yet" + CHR$(13) +_ 
  "another alarm clock." + CHR$(13) +_ 
  "   It was written in" + CHR$(13) +_ 
  "         Rapid-Q." + CHR$(13)  + CHR$(13) +_ 
  "    It demonstrates" + CHR$(13) +_ 
  "   Rapid-Q's talent" + CHR$(13) +_ 
  "for producing highly" + CHR$(13) +_ 
  " useful apps easily." + CHR$(13) +_ 
  "  It also shows the" + CHR$(13) +_ 
  "   author's lack of" + CHR$(13) +_ 
  "         talent for" + CHR$(13) +_ 
  "  innovative coding.","Alarmed",0)
END SUB

SUB Help
  AlarmHelp.Visible = 1
END SUB

SUB Off
  Application.Terminate
END SUB

InitAlarm
AlarmForm.ShowModal
