[ Counter ]


Contact Counter 1.0
(public domain code by Dave O'Brien)

Helps you keep track of how many days you've worn your disposable contact lenses. Includes a main screen and a few pop-up forms (using both iziBasic functions and external resources).

 ' Contact Counter.ibas
 {CREATORID "CoCt"}
 {VERSION "1.0"}
 {RESOURCEFILE + "Contact Counter.rsrc"}
 {SECUREFILES ON}
 {PARSER ON}

 'max-day choices
 DIM %M1%,%M2%,%M3%
 DIM %M1$,%M2$,%M3$
 DIM %MaxIndexOld%
 
 'temp strings
 DIM %T1$,%T2$,%T3$
 DIM %Dummy%, %Result%
 
 'centering text
 DIM %CT$   'source string
 DIM %CTF%  'font size
 DIM %CTO%  'resulting offset
 
 'font sizes
 DIM %SmallFont%,%LargeFont%
 
 'colors
 DIM %Black%,%White%,%Red%
 DIM %FgAlertColor%,%BgAlertColor%
 DIM %FgUsageColor%,%BgUsageColor%
 
 'dates
 DIM %LastDate$   'date of last use
 DIM %Mmddyyyy$,%Ddmmyyyy$,%Today$
 DIM %Mm$,%Dd$,%Yyyy$
 
 'counts
 DIM %CurrentCount%,%MaxCount%
 DIM %CC$,%MC$
 
 'misc
 DIM %ScrMode%,%Event%
 DIM %MenuChoice%,%Selection%
 DIM %Y1%,%Y2%

 BEGIN
 %M1%=7 : %M2%=14 : %M3%=30
 'need to keep resource alert synced
 %M1$=STR$(%M1%,0)
 %M2$=STR$(%M2%,0)
 %M3$=STR$(%M3%,0)
 'L$=%M1$+CHR$(182)+%M2$+CHR$(182)+%M3$
 %SmallFont%=0
 %LargeFont%=7
 %ScrMode%=SCREENMODE
 %Red%=COLORRGB(255,150,150)
 IF %ScrMode%<3 THEN  'not color
    %White%=0
    %Black%=1
    %FgAlertColor%=%White%
    %BgAlertColor%=%Black%
 ELSE  'color
    %White%=COLORRGB(255,255,255)
    %Black%=0
    %FgAlertColor%=%Black%
    %BgAlertColor%=%Red%
 ENDIF
 
 MENU 100
 
 GOSUB _loadData
 GOSUB _setupDisplay
 REPEAT
    %Event%=DOEVENTS
    IF %Event%=2 GOSUB _addDay
    IF %Event%=3 GOSUB _startNew
    IF %Event%=4 ADVICEBOX 200
    IF %Event%=6 GOSUB _confirmMaxChange
    IF %Event%=7 GOSUB _updateDateText
    IF %Event%=1001 GOSUB _handleMenu
 UNTIL %Event%<0
 GOSUB _saveData
 END

 _loadData:
    %CurrentCount%=LOADPREF(#1)
    %MaxCount%=LOADPREF(#2)
    IF %MaxCount%<>%M1% THEN
       IF %MaxCount%<>%M2% THEN
          IF %MaxCount%<>%M3% THEN 'first run
             GOSUB _askMaxDays
          ENDIF
       ENDIF
    ENDIF
    %MC$=STR$(%MaxCount%,0)
    %CurrentCount%=MIN(%CurrentCount%,%MaxCount%)
    %LastDate$=LOADPREF$(#3)
 RETURN

 _setupDisplay:
    SCREEN 3  'try 256 colors
    'TITLE "Contact Counter"
    SETFONT %LargeFont%
    BUTTON #2,"Add Day",35,110,90,30
    BUTTON #3,"Start New Pair",35,78,90,30
    HIDE #3
    SETFONT %SmallFont%
    BUTTON #4,"?",148,1,10,10
    'LABEL #5,"Max Days:",5,20
    'POPUPCHOICE #6,%MC$,L$,14,50,20,40,12
    GOSUB _getOldMaxIndex
    GOSUB _updateUsageText
    GOSUB _updateDateText
 RETURN

 _handleMenu:
    %MenuChoice%=MENUITEM
    SELECT CASE %MenuChoice%
       CASE 101
          GOSUB _askMaxDays
       CASE 102
          %Dummy%=NOTICEBOX(301)
    END SELECT
 RETURN

 _askMaxDays:
    %Selection%=NOTICEBOX(300)
    GOSUB _setMaxDays
 RETURN

 _updateUsageText:
    %Y1%=40
    %Y2%=%Y1%+20
    IF %CurrentCount%>=%MaxCount% THEN 'highlight it
       %FgUsageColor%=%FgAlertColor%
       %BgUsageColor%=%BgAlertColor%
       HIDE #2
       SHOW #3
       BEEP 1,2
    ELSE
       %FgUsageColor%=%Black%
       %BgUsageColor%=%White%
       COLOR %White%
       BOXFILLED 0,%Y1% TO 160,%Y2%
    ENDIF
    %CC$=STR$(%CurrentCount%,0)
    %MC$=STR$(%MaxCount%,0)
    %CT$="    "+%CC$+"   days used of   "+%MC$+"    "
    %CTF%=%LargeFont%
    GOSUB _getCenterTextOffset
    SETFONT %LargeFont%
    GPRINT %CT$,%CTO%,%Y1%,%FgUsageColor%,%BgUsageColor%
 RETURN

 _updateDateText:
    %Y1%=147
    %Y2%=%Y1%+20
    GOSUB _getTodaysDate 'gets %Today$
    IF %LastDate$=%Today$ THEN
       U$="Today"  'simplify display
    ELSE
       U$=%LastDate$
    ENDIF
    %CT$="Last added: "+U$
    %CTF%=%SmallFont%
    GOSUB _getCenterTextOffset
    COLOR %White%
    BOXFILLED 0,%Y1% TO 160,%Y2%
    SETFONT %SmallFont%
    COLOR %Black%
    IF %CurrentCount%<>0 THEN 'suppress if new count
       GPRINT %CT$,%CTO%,%Y1%
    ENDIF
 RETURN

 _addDay:
    INC %CurrentCount%
    GOSUB _updateUsageText
    GOSUB _getTodaysDate 'gets %Today$
    %LastDate$=%Today$
    GOSUB _updateDateText
 RETURN

 _startNew:
    %CurrentCount%=0
    GOSUB _updateUsageText
    GOSUB _updateDateText
    HIDE #3
    SHOW #2
 RETURN 

 _confirmMaxChange:
 'obsolete - replaced by dialog
    GOSUB _getOldMaxIndex
    %Selection%=SELECTEDCHOICE
    IF %Selection%<>0 THEN
       IF %MaxIndexOld%<>%Selection% THEN
          %T1$="Really change the max?"
          %T2$="This resets the count."
          %Result%=MESSAGEBOX(%T1$+%T2$,3)
          IF %Result%=1 THEN
             GOSUB _setMaxDays
          ELSE
             UPDATEVALUE #6,%MaxIndexOld%
          ENDIF
       ENDIF
    ENDIF
 RETURN

 _getOldMaxIndex:
 'no longer necessary, but harmless
    IF %MaxCount%=%M1% THEN
       %MaxIndexOld%=1
    ELSE
       IF %MaxCount%=%M2% THEN
          %MaxIndexOld%=2
       ELSE
          IF %MaxCount%=%M3% THEN
             %MaxIndexOld%=3
          ENDIF
       ENDIF
    ELSE
       %T1$="Unknown value: "+%MC$
       %Dummy%=MESSAGEBOX(%T1$,1)
       %MaxIndexOld%=2
    ENDIF
 RETURN

 _setMaxDays:
    SELECT CASE %Selection%
       CASE 1
          %MaxCount%=%M1%
       CASE 2
          %MaxCount%=%M2%
       CASE 3
          %MaxCount%=%M3%
       CASE ELSE
          %MaxCount%=%M2%
    END SELECT
    %MC$=STR$(%MaxCount%,0)
    GOSUB _getOldMaxIndex
    %CurrentCount%=0
    GOSUB _updateUsageText
 RETURN 

 _saveData:
    SAVEPREF #1,%CurrentCount%
    SAVEPREF #2,%MaxCount%
    SAVEPREF #3,%LastDate$
 RETURN

 _getCenterTextOffset:
 'given string, return x to center it
    %CTO%=(160/2)-(FONTWIDTH(%CT$,%CTF%)/2)
 RETURN '%CTO%

 _getTodaysDate:
    %Ddmmyyyy$=DATE$
    GOSUB _toMmddyyyy
    %Today$=%Mmddyyyy$
 RETURN '%Today$

 _toMmddyyyy:
 'convert %Ddmmyyyy$ to %Mmddyyyy$
    %Dd$=MID$(%Ddmmyyyy$,1,2)
    %Mm$=MID$(%Ddmmyyyy$,4,2)
    %Yyyy$=MID$(%Ddmmyyyy$,7,4)
    %Mmddyyyy$=%Mm$+"/"+%Dd$+"/"+%Yyyy$
 RETURN '%Mmddyyyy$
  pages read