home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / bus_pers / xcale_1_.lha / XCale / xcale.c < prev    next >
C/C++ Source or Header  |  1992-12-05  |  10KB  |  431 lines

  1. ;/* Little NeXT style calender by Juha Tuominen, 1992
  2. ;    v1.1 22-Jun-92 little smaller now
  3. ;    v1.2 30-Jul-92 supports and needs locale.library
  4. ;    v1.3 03-Dec-92 autoscaling depending on screen font height
  5. ;    v1.3b 05-Dec-92 ARGH! there was a small bug in 1.3.... sorry
  6.  
  7. lc -j73 -v -y -ba xcale.c
  8. blink lib:cback.o+xcale.o to xcale lib lib:lc.lib+lib:amiga.lib
  9. quit
  10.  
  11. */
  12.  
  13. #include <exec/types.h>
  14. #include <libraries/diskfont.h>
  15. #include <libraries/dos.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <graphics/gfx.h>
  18. #include <graphics/gfxmacros.h>
  19. #include <libraries/locale.h>
  20. #include <utility/hooks.h>
  21. #include <proto/intuition.h>
  22. #include <proto/graphics.h>
  23. #include <proto/exec.h>
  24. #include <stdlib.h>
  25.  
  26. #define VERSTAG        "\0$VER: XCale 1.3b (05-Dec-92)"
  27. #define VSTRING        "XCale"
  28.  
  29. long _stack=16384;
  30. char *_procname=VSTRING;
  31. long _priority=5;
  32. long _BackGroundIO=FALSE;
  33.  
  34. void MemCleanup() {}
  35.  
  36. UBYTE    versiontag[]=VERSTAG;
  37. BOOL    keepgoing=TRUE,mode=FALSE,lastside=TRUE;
  38. char    datestring[20],aika[10];
  39. int        dsp=0,offset;
  40. struct    LocaleBase *LocaleBase=NULL;
  41. struct    Locale *localepointer=NULL;
  42. struct    IntuitionBase *IntuitionBase=NULL;
  43. struct    Window *window=NULL;
  44. struct    Library *DiskfontBase=NULL;
  45. struct    GfxBase *GfxBase=NULL;
  46. struct    Hook    hook;
  47. struct    NewWindow mywindow = 
  48. {    100,
  49.     100,
  50.     70,
  51.     76,
  52.     0,1,
  53.     CLOSEWINDOW | MOUSEBUTTONS,
  54.     SMART_REFRESH | WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH | RMBTRAP,
  55.     NULL,
  56.     NULL,
  57.     NULL,
  58.     NULL,
  59.     NULL,
  60.     NULL,
  61.     NULL,
  62.     NULL,
  63.     NULL,
  64.     WBENCHSCREEN
  65.  
  66. };
  67.  
  68. /* Iff2Src conversion: src:c/xcale.iff */
  69.  
  70. UWORD chip data[488] =
  71. {
  72.     /* Plane 0 */
  73.     0x0000,0x0000,0x0000,0x0000,
  74.     0x7FFF,0xFFFF,0xFFFF,0xFF00,
  75.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  76.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  77.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  78.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  79.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  80.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  81.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  82.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  83.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  84.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  85.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  86.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  87.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  88.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  89.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  90.     0x7FFF,0xFFFF,0xFFFF,0xFE00,
  91.     0x4000,0x0000,0x0000,0x0000,
  92.     0x0000,0x0000,0x0000,0x0000,
  93.     0x0000,0x0000,0x0000,0x0000,
  94.     0x0000,0x0000,0x0000,0x0000,
  95.     0x0000,0x0000,0x0010,0x0000,
  96.     0x0000,0x0000,0x0018,0x0000,
  97.     0x0000,0x0000,0x001C,0x0000,
  98.     0x0000,0x0000,0x001E,0x0000,
  99.     0x0000,0x0000,0x001F,0x0000,
  100.     0x0000,0x0000,0x001F,0x8000,
  101.     0x0000,0x0000,0x001F,0xC000,
  102.     0x0000,0x0000,0x001F,0xC000,
  103.     0x0000,0x0000,0x001F,0xC000,
  104.     0x0000,0x0000,0x001F,0xC000,
  105.     0x0000,0x0000,0x001F,0xC000,
  106.     0x0000,0x0000,0x001F,0xC000,
  107.     0x0000,0x0000,0x001F,0xC000,
  108.     0x0000,0x0000,0x001F,0xC000,
  109.     0x0000,0x0000,0x001F,0xC000,
  110.     0x0000,0x0000,0x001F,0xC000,
  111.     0x0000,0x0000,0x001F,0xC000,
  112.     0x0000,0x0000,0x001F,0xC000,
  113.     0x0000,0x0000,0x001F,0xC000,
  114.     0x0000,0x0000,0x001F,0xC000,
  115.     0x0000,0x0000,0x001F,0xC000,
  116.     0x0000,0x0000,0x001F,0xC000,
  117.     0x0000,0x0000,0x001F,0xC000,
  118.     0x0000,0x0000,0x001F,0xC000,
  119.     0x0000,0x0000,0x001F,0xC000,
  120.     0x0000,0x0000,0x001F,0xC000,
  121.     0x0000,0x0000,0x001F,0xC000,
  122.     0x0000,0x0000,0x001F,0xC000,
  123.     0x0000,0x0000,0x003F,0xC000,
  124.     0x0000,0x0000,0x003F,0xC000,
  125.     0x0000,0x0000,0x007F,0xC000,
  126.     0x003F,0xFFFF,0xFFFF,0xC000,
  127.     0x0000,0x0000,0x001F,0xC000,
  128.     0x003F,0xFFFF,0xFFFF,0xC000,
  129.     0x001F,0xFFFF,0xFFFF,0xC000,
  130.     0x000F,0xFFFF,0xFFFF,0xC000,
  131.     0x0007,0xFFFF,0xFFFF,0xC000,
  132.     0x0003,0xFFFF,0xFFFF,0xC000,
  133.     0x0000,0x0000,0x0000,0x0000,
  134.     /* Plane 1 */
  135.     0x0000,0x0000,0x0000,0x0000,
  136.     0x0000,0x0000,0x0000,0x0100,
  137.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  138.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  139.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  140.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  141.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  142.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  143.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  144.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  145.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  146.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  147.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  148.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  149.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  150.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  151.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  152.     0x3FFF,0xFFFF,0xFFFF,0xFF00,
  153.     0x7FFF,0xFFFF,0xFFFF,0xFF00,
  154.     0x0000,0x0000,0x0000,0x0000,
  155.     0x0000,0x0000,0x0000,0x0000,
  156.     0x003F,0xFFFF,0xFFE0,0x0000,
  157.     0x003F,0xFFFF,0xFFE0,0x0000,
  158.     0x003F,0xFFFF,0xFFE0,0x0000,
  159.     0x003F,0xFFFF,0xFFE0,0x0000,
  160.     0x003F,0xFFFF,0xFFE2,0x0000,
  161.     0x003F,0xFFFF,0xFFE3,0x0000,
  162.     0x003F,0xFFFF,0xFFE3,0x8000,
  163.     0x003F,0xFFFF,0xFFE3,0xC000,
  164.     0x003F,0xFFFF,0xFFE3,0xC000,
  165.     0x003F,0xFFFF,0xFFE3,0xC000,
  166.     0x003F,0xFFFF,0xFFE3,0xC000,
  167.     0x003F,0xFFFF,0xFFE3,0xC000,
  168.     0x003F,0xFFFF,0xFFE3,0xC000,
  169.     0x003F,0xFFFF,0xFFE3,0xC000,
  170.     0x003F,0xFFFF,0xFFE3,0xC000,
  171.     0x003F,0xFFFF,0xFFE3,0xC000,
  172.     0x003F,0xFFFF,0xFFE3,0xC000,
  173.     0x003F,0xFFFF,0xFFE3,0xC000,
  174.     0x003F,0xFFFF,0xFFE3,0xC000,
  175.     0x003F,0xFFFF,0xFFE3,0xC000,
  176.     0x003F,0xFFFF,0xFFE3,0xC000,
  177.     0x003F,0xFFFF,0xFFE3,0xC000,
  178.     0x003F,0xFFFF,0xFFE3,0xC000,
  179.     0x003F,0xFFFF,0xFFE3,0xC000,
  180.     0x003F,0xFFFF,0xFFE3,0xC000,
  181.     0x003F,0xFFFF,0xFFE3,0xC000,
  182.     0x003F,0xFFFF,0xFFE3,0xC000,
  183.     0x007F,0xFFFF,0xFFE3,0xC000,
  184.     0x007F,0xFFFF,0xFFE3,0xC000,
  185.     0x007F,0xFFFF,0xFFE3,0xC000,
  186.     0x00FF,0xFFFF,0xFFE3,0xC000,
  187.     0x01FF,0xFFFF,0xFFE3,0xC000,
  188.     0x003F,0xFFFF,0xFFE3,0xC000,
  189.     0x0000,0x0000,0x0003,0xC000,
  190.     0x0000,0x0000,0x0003,0xC000,
  191.     0x0000,0x0000,0x0003,0xC000,
  192.     0x000F,0xFFFF,0xFFFF,0xC000,
  193.     0x0007,0xFFFF,0xFFFF,0xC000,
  194.     0x0003,0xFFFF,0xFFFF,0xC000,
  195.     0x0000,0x0000,0x0000,0x0000
  196. };
  197.  
  198.  
  199. struct Image im =
  200. {
  201.     0,0,
  202.     57,61,
  203.     2,
  204.     &data[0],
  205.     0xff,0x0,
  206.     NULL
  207. };
  208.  
  209. void __saveds __asm addchr(register __a1 APTR);
  210. void cleanexit(int);
  211. void openthings(void);
  212. void changefont(char *,int);
  213. void convert_datestamp(struct DateStamp);
  214. void initgraphics(char *,char *,int);
  215. void inittime(char *);
  216. void flickersecs(BOOL,BOOL);
  217.  
  218.  
  219. void __saveds __asm addchr(register __a1 APTR c)
  220. {    if(dsp<20)
  221.         datestring[dsp++]=(char)c;
  222. }
  223.  
  224. void cleanexit(int error)
  225. {
  226.     if(window) CloseWindow(window);
  227.     if(DiskfontBase) CloseLibrary(DiskfontBase);
  228.     if(localepointer) CloseLocale(localepointer);
  229.     if(LocaleBase) CloseLibrary((struct Library *)LocaleBase);
  230.     if(GfxBase) CloseLibrary(GfxBase);
  231.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  232.     XCEXIT(error);
  233. }
  234.  
  235.  
  236. void openthings(void)
  237. {
  238.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",33L)))
  239.         cleanexit(30);
  240.     if(!(IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",33L)))
  241.         cleanexit(30);
  242.     if(!(LocaleBase=(struct LocaleBase *)OpenLibrary("locale.library",38L)))
  243.         cleanexit(25);
  244.     if(!(localepointer=(struct Locale *)OpenLocale("ENV:Sys/locale.prefs")))
  245.         cleanexit(21);
  246.     if(!(DiskfontBase=OpenLibrary("diskfont.library", 33L)))
  247.         cleanexit(30);
  248.     if(!(window=OpenWindow(&mywindow)))
  249.         cleanexit(27);
  250.  
  251.     if(offset=window->WScreen->BarHeight-8)
  252.         SizeWindow(window,0,offset);
  253. }
  254.  
  255.  
  256.  
  257. void changefont(char *fontname, int fontsize)
  258. {    struct    TextAttr    textattr;
  259.     struct    TextFont    *textfont;
  260.  
  261.     textattr.ta_Name=fontname;
  262.     textattr.ta_YSize=fontsize;
  263.     textattr.ta_Style=FS_NORMAL;
  264.     textattr.ta_Flags=FPF_ROMFONT | FPF_DISKFONT;
  265.  
  266.     if(textfont=(struct TextFont *)OpenDiskFont(&textattr))
  267.     {    SetFont(window->RPort,textfont);
  268.         if(window->RPort->TxHeight!=fontsize)
  269.             cleanexit(20);
  270.     }
  271.     else
  272.         cleanexit(10);
  273. }
  274.  
  275. void convert_datestamp(struct DateStamp v)
  276. {    char     time[10];
  277.     unsigned int year,day;
  278.     unsigned char month[4];
  279.     static unsigned int oldyear, oldday;
  280.     static unsigned char oldmonth[4];
  281.  
  282.     dsp=0;
  283.     FormatDate(localepointer,"%H",&v,&hook);
  284.     sprintf(time,"%s ",datestring);
  285.     dsp=0;
  286.     FormatDate(localepointer,"%M",&v,&hook);
  287.     strcat(time,datestring);
  288.     dsp=0;
  289.     FormatDate(localepointer,"%y",&v,&hook);
  290.     year=atoi(datestring);
  291.     dsp=0;
  292.     FormatDate(localepointer,"%b",&v,&hook);
  293.     strcpy(month,datestring);
  294.     dsp=0;
  295.     FormatDate(localepointer,"%e",&v,&hook);
  296.     day=atoi(datestring);
  297.     dsp=0;
  298.     FormatDate(localepointer,"%A",&v,&hook);
  299.  
  300.  
  301.     if(oldday!=day || strcmp(month,oldmonth) || oldyear!=year)
  302.     {    initgraphics(month,datestring,day);
  303.         oldday=day;
  304.         strcpy(oldmonth,month);
  305.         oldyear=year;
  306.         strcpy(aika," ");
  307.     }
  308.  
  309.     if(strcmp(time,aika))
  310.     {    inittime(time);
  311.         strcpy(aika,time);
  312.     }
  313.     if(v.ds_Tick/50<30)
  314.     {    if(!lastside)
  315.         {    flickersecs(FALSE,TRUE);
  316.             lastside=TRUE;
  317.         }
  318.         flickersecs(TRUE,mode);
  319.     }
  320.     else
  321.     {    if(lastside)
  322.         {    flickersecs(TRUE,TRUE);
  323.             lastside=FALSE;
  324.         }
  325.         flickersecs(FALSE,mode);
  326.     }
  327.     mode=!mode;
  328. }
  329.  
  330.  
  331. void flickersecs(BOOL upper,BOOL on)
  332. {    int ypos=22+offset;
  333.  
  334.     if(upper)
  335.         ypos-=4;
  336.  
  337.     if(on)
  338.     {    SetAPen(window->RPort,1);
  339.         RectFill(window->RPort,34,ypos+1,35,ypos+2);
  340.         SetAPen(window->RPort,2);
  341.         RectFill(window->RPort,33,ypos,34,ypos+1);
  342.     }
  343.     else
  344.     {    SetAPen(window->RPort,3);
  345.         RectFill(window->RPort,33,ypos,35,ypos+2);
  346.     }
  347. }
  348.  
  349. void inittime(char *time)
  350. {
  351.     changefont("sunsmall.font",14);
  352.     SetDrMd(window->RPort,JAM2);
  353.     SetAPen(window->RPort,3);
  354.     RectFill(window->RPort,10,15+offset,60,26+offset);
  355.     Move(window->RPort,15,26+offset);
  356.     SetAPen(window->RPort,1);
  357.     SetBPen(window->RPort,3);
  358.     Text(window->RPort,time,5);
  359.     SetDrMd(window->RPort,JAM1);
  360.     Move(window->RPort,14,25+offset);
  361.     SetAPen(window->RPort,2);
  362.     Text(window->RPort,time,5);
  363.     SetAPen(window->RPort,2);
  364.     Move(window->RPort,14,30+offset);
  365.     Draw(window->RPort,55,30+offset);
  366.     flickersecs(TRUE,TRUE);
  367.     flickersecs(FALSE,TRUE);
  368. }
  369.  
  370. void initgraphics(char *month, char *dayofweek, int day)
  371. {    char temp[4];
  372.  
  373.     SetDrMd(window->RPort,JAM1);
  374.     Delay(25);    /* if we don't wait, Intuition screws up, don't know why.. */
  375.     DrawImage(window->RPort,&im,6,12+offset);
  376.     changefont("sunfont.font",16);
  377.     Move(window->RPort,21,54+offset);
  378.     SetAPen(window->RPort,1);
  379.     SetBPen(window->RPort,2);
  380.     sprintf(temp,"%02d",day);
  381.     Text(window->RPort,temp,2);
  382.     changefont("blob.font",8);
  383.     Move(window->RPort,24,40+offset);
  384.     Text(window->RPort,dayofweek,3);
  385.     Move(window->RPort,23,63+offset);
  386.     Text(window->RPort,month,3);
  387.     ScrollRaster(window->RPort,1,0,21,61+offset,45,64+offset);
  388. }
  389.  
  390. void _main(char *arguments)
  391. {
  392.     struct    DateStamp v;
  393.     struct    IntuiMessage *msg=NULL;
  394.     int        i,j=0;
  395.     char    temp1[20];
  396.  
  397.     hook.h_Entry=(APTR)addchr;
  398.  
  399.     for(i=0;i<strlen(arguments);i++)
  400.     {    if(arguments[i]==32 || arguments[i]==13 || arguments[i]==10)
  401.         {    if(!strncmp(temp1,"-x",2))
  402.                 mywindow.LeftEdge=atoi(temp1+2);
  403.             if(!strncmp(temp1,"-y",2))
  404.                 mywindow.TopEdge=atoi(temp1+2);
  405.             j=0;
  406.         }
  407.         else
  408.         {    temp1[j++]=arguments[i];
  409.             temp1[j]=0;
  410.         }
  411.     }
  412.  
  413.     openthings();
  414.  
  415.     while(keepgoing)
  416.     {    if(msg=(struct IntuiMessage *)GetMsg(window->UserPort))
  417.         {    ReplyMsg((struct Message *)msg);
  418.             if(msg->Class==CLOSEWINDOW)
  419.                 keepgoing=FALSE;
  420.         }
  421.         else
  422.         {    DateStamp(&v);
  423.             convert_datestamp(v);
  424.             Delay(49);
  425.         }
  426.         if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  427.             break;
  428.     }
  429.     cleanexit(0);
  430. }
  431.