home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / system-tools / tinymeter / source / tinymeter_main / initclean.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-21  |  9.1 KB  |  363 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/cghooks.h>
  12. #include <intuition/icclass.h>
  13. #include <intuition/classes.h>
  14. #include <intuition/sghooks.h>
  15. #include <intuition/screens.h>
  16. #include <datatypes/datatypesclass.h>
  17. #include <datatypes/datatypes.h>
  18. #include <datatypes/pictureclass.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/text.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <string.h>
  25. #include <clib/macros.h>
  26. #include "gaugeclass.h"
  27. #include "tinymeter.h"
  28.  
  29. extern struct IntuitionBase *IntuitionBase;
  30.  
  31. struct Gadget my_drag=
  32. {
  33.     NULL,
  34.     0,1,
  35.     0,0,
  36.     GFLG_GADGHNONE,
  37.     GACT_IMMEDIATE,
  38.     GTYP_SYSGADGET|GTYP_WDRAGGING,
  39.     0,
  40.     0,
  41.     0,
  42.  
  43.     0,
  44.     0,
  45.     0xFFF2,
  46.     0
  47. };
  48.  
  49. struct Gadget my_close=
  50. {
  51.     NULL,
  52.     0,0,
  53.     16,16,
  54.     NULL,
  55.     GACT_IMMEDIATE,
  56.     GTYP_SYSGADGET|GTYP_CLOSE,
  57.     0,
  58.     0,
  59.     0,
  60.  
  61.     0,
  62.     0,
  63.     0xFFF1,
  64.     0
  65. };
  66.  
  67. struct Gadget my_size=
  68. {
  69.     NULL,
  70.     0,0,
  71.     16,16,
  72.     GFLG_GADGHNONE,
  73.     GACT_IMMEDIATE,
  74.     GTYP_SYSGADGET|GTYP_SIZING,
  75.     0,
  76.     0,
  77.     0,
  78.  
  79.     0,
  80.     0,
  81.     0xFFF3,
  82.     0
  83. };
  84.  
  85. new_window_size(struct tm_sys_set *set,struct tm_data *data)
  86. {
  87.     struct Window   *win=data->win;
  88.     my_drag.Width   =win->Width;
  89.     my_drag.Height  =win->Height-17;
  90.     my_size.TopEdge =win->Height-16;
  91.     my_size.LeftEdge=win->Width -16;
  92. }
  93.  
  94. struct TextFont *loadFont(char *name, UWORD size)
  95. {
  96.     struct  TextFont     *tf;
  97.     struct  TextAttr      my_text_attr;
  98.  
  99.     my_text_attr.ta_Name =(UBYTE *)name;
  100.     my_text_attr.ta_YSize=(UWORD)size;
  101.     my_text_attr.ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;
  102.  
  103.     tf=(struct TextFont *)OpenDiskFont((struct TextAttr *)&my_text_attr);
  104.     if(!tf) if(!(tf=(struct TextFont *)OpenTopaz())) return(0L);
  105.     return(tf);
  106. }
  107.  
  108. struct Window *open_new_window(struct tm_sys_set *set,struct tm_data *data, UWORD ysiz)
  109. {
  110.     struct Window *window;
  111.  
  112.     if((window=(struct Window *)OpenWindowTags(0,
  113.            WA_Left,            set->x_pos,
  114.            WA_Top,             set->y_pos,
  115.            WA_Width,           set->x_siz,
  116.            WA_MaxWidth,        4096,
  117.            WA_MaxHeight,       ysiz,
  118.            WA_MinHeight,       ysiz,
  119.            WA_Height,          ysiz,
  120.            WA_MinWidth,        data->min_x_size,
  121.            WA_Flags,           (set->win_backfront!=win_back ? 0 : WFLG_BACKDROP )|WFLG_BORDERLESS|WFLG_RMBTRAP|WFLG_REPORTMOUSE,
  122.            WA_PubScreen,       data->scr,
  123.            TAG_DONE)))
  124.     {
  125.     if(data->appport=(struct MsgPort *)CreateMsgPort(0L)) data->appwin=(struct AppWindow *)AddAppWindow(0L, 0L, window, data->appport , 0L);
  126.     my_drag.Width=   window->Width;
  127.     my_drag.Height=  window->Height-17;
  128.     my_size.TopEdge= window->Height-16;
  129.     my_size.LeftEdge=window->Width -16;
  130.     if(set->win_move==win_normal)
  131.     {
  132.         AddGadget(window,&my_drag, (UWORD)0);
  133.         AddGadget(window,&my_size, (UWORD)0);
  134.         AddGadget(window,&my_close,(UWORD)0);
  135.     }
  136.     ModifyIDCMP(window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_SIZEVERIFY|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS|IDCMP_GADGETDOWN|IDCMP_GADGETUP);
  137.     return(window);
  138.     }
  139.     return(0L);
  140. }
  141.  
  142. struct Window *snapBackground(struct tm_sys_set *set,struct tm_data *data)
  143. {
  144.     struct Window *win=data->win;
  145.     UWORD x,y,xs,ys;
  146.  
  147.     x=win->LeftEdge;
  148.     y=win->TopEdge;
  149.     xs=win->Width;
  150.     ys=win->Height;
  151.     set->x_pos=x;
  152.     set->y_pos=y;
  153.     set->x_siz=xs;
  154.  
  155.     if(data->bg_bm) FreeBitMap(data->bg_bm); data->bg_bm=0;
  156.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,data->scr->RastPort.BitMap->Depth,0,0))
  157.     {
  158.     RemoveGList(win,win->FirstGadget,-1);
  159.     if(data->appwin)  RemoveAppWindow(data->appwin);
  160.     if(data->appport) DeleteMsgPort  (data->appport);
  161.     CloseWindow(win);
  162.     BltBitMap(data->scr->RastPort.BitMap,x,y,data->bg_bm,0,0,xs,ys,0xc0,0xff,0);
  163.     return((struct Window *)open_new_window(set,data,ys));
  164.     }
  165.     return(0L);
  166. }
  167.  
  168. void CopyTiledBitMap(struct BitMap *Src,WORD SrcSizeX,WORD SrcSizeY,WORD DstSizeX,WORD DstSizeY ,struct BitMap *Dst)
  169. {
  170.     WORD PosX,
  171.      PosY;
  172.     WORD SizeX,
  173.      SizeY;
  174.     for (PosX = 0,SizeX = MIN(SrcSizeX,DstSizeX);PosX<DstSizeX;)
  175.     {
  176.     for (PosY = 0,SizeY = MIN(SrcSizeY,DstSizeY);PosY<DstSizeY;)
  177.     {
  178.         BltBitMap(Src,0,0,Dst,PosX,PosY,SizeX,SizeY,0xC0,0xff,0L);
  179.         PosY += MIN(SizeY,DstSizeY-PosY);
  180.         SizeY = MIN(SizeY,DstSizeY-PosY);
  181.     }
  182.     PosX += MIN(SizeX,DstSizeX-PosX);
  183.     SizeX = MIN(SizeX,DstSizeX-PosX);
  184.     }
  185. }
  186.  
  187. Object *LoadImage(char *file, struct Screen *scr)
  188. {
  189.     Object              *dt_obj;
  190.  
  191.     if (dt_obj = (Object *)NewDTObject(file,
  192.     DTA_SourceType       ,DTST_FILE,
  193.     DTA_GroupID          ,GID_PICTURE,
  194.     PDTA_Remap           ,TRUE,
  195.     PDTA_Screen          ,scr,
  196.     PDTA_FreeSourceBitMap,TRUE,
  197.     OBP_Precision        ,PRECISION_IMAGE,
  198.     TAG_DONE))
  199.     {
  200.     if ( DoDTMethod(dt_obj,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
  201.     {
  202.         return(dt_obj);
  203.     }
  204.     }
  205.     return(NULL);
  206. }
  207.  
  208. fileBackground(struct tm_sys_set *set,struct tm_data *data)
  209. {
  210.     struct Window       *win=data->win;
  211.     struct BitMap       *work;
  212.     struct BitMapHeader *header;
  213.     UWORD xs,ys;
  214.  
  215.     if (data->bg_bm){ FreeBitMap(data->bg_bm); data->bg_bm=0L; }
  216.     if (data->dt_object = LoadImage(&set->bg_picture[0],data->scr))
  217.     {
  218.     GetDTAttrs(data->dt_object,PDTA_BitMapHeader,&header,PDTA_DestBitMap,&work,TAG_DONE);
  219.     if (work==FALSE) GetDTAttrs(data->dt_object,PDTA_BitMap,&work,TAG_DONE);
  220.  
  221.     xs=win->Width;
  222.     ys=win->Height;
  223.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,work->Depth,0,0))
  224.     {
  225.         CopyTiledBitMap(work,header->bmh_Width,header->bmh_Height,xs,ys,data->bg_bm);
  226.     }
  227.     }
  228. }
  229.  
  230. struct Window *openWindow(struct tm_sys_set *set,struct tm_data *data)
  231. {
  232.     UWORD               y_siz,
  233.             line,
  234.             i,
  235.             tmp;
  236.     struct tm_gau_set   *many;
  237.     struct RastPort     *tmpras;
  238.  
  239.     if( data->scr=(struct Screen *)LockPubScreen((char *)&set->pub_name[0]))
  240.     {
  241.     data->on_public=TRUE;
  242.     }
  243.     else
  244.     {
  245.     data->scr=(struct Screen *)IntuitionBase->FirstScreen;
  246.     data->on_public=FALSE;
  247.     }
  248.  
  249.     data->labelpos=0;
  250.     if(tmpras=(struct RastPort *)AllocVec(sizeof(struct RastPort),0L))
  251.     {
  252.     InitRastPort(tmpras);
  253.     for(i=0;i<data->num_of_gaug;i++)
  254.     {
  255.         data->gauge_y_size[i]=0;
  256.         data->gauge_y_size_falling[i]=0;
  257.     }
  258.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  259.     {
  260.         if(many->type!=typ_none)
  261.         {
  262.         line=(i/(set->colums));
  263.         if(data->Font[i]=loadFont(many->font,many->font_size))
  264.             data->gauge_y_size[line]=MAX(data->gauge_y_size[line],data->gauge_y_size_falling[i]=(data->Font[i]->tf_YSize*(many->size_y+100))/100);
  265.         if((many->gauge_type!=typ_histmeter)&&(many->type!=typ_clock_)&&(many->type!=typ_simplelauncher))
  266.         {
  267.             SetFont(tmpras,data->Font[i]);
  268.             tmp=TextLength(tmpras,many->label,my_strlen(many->label));
  269.             if(tmp>data->labelpos)data->labelpos=tmp;
  270.         }
  271.         }
  272.         else data->Font[i]=0;
  273.         many=many->next;
  274.     }
  275.     data->labelpos+=4;
  276.     FreeVec(tmpras);
  277.  
  278.     data->num_of_rows   =((data->num_of_gaug-1)/set->colums)+1;
  279.     data->min_x_size    =((data->labelpos+8)*set->colums)+(set->win_space_x*(set->colums-1))+(set->win_border_x<<1);
  280.     if(data->min_x_size>set->x_siz)set->x_siz=data->min_x_size;
  281.     data->gauge_x_size  =(set->x_siz-(set->win_border_x<<1)-((set->colums-1)*set->win_space_x))/set->colums;
  282.  
  283.     if(set->lay_falling)
  284.     {
  285.         UWORD old_siz,j;
  286.         for(i=0,y_siz=0;i<set->colums;i++)
  287.         {
  288.         old_siz=y_siz;
  289.         for(j=i,y_siz=0;j<data->num_of_gaug;j+=set->colums) y_siz+=data->gauge_y_size_falling[j];
  290.         if(y_siz>old_siz) old_siz=y_siz;
  291.         }
  292.         y_siz=old_siz;
  293.     }
  294.     else for(i=0,y_siz=0;i<data->num_of_rows;i++)y_siz+=data->gauge_y_size[i];
  295.     y_siz+=(set->win_border_y<<1)+(set->win_space_y*(data->num_of_rows-1));
  296.  
  297.     if(data->win=(struct Window *)open_new_window(set,data,y_siz))
  298.     {
  299.         data->bg_color      =obtainPen(data->scr,&set->bg_color);
  300.         data->bright_color  =obtainPen(data->scr,&set->bright_color);
  301.         data->dark_color    =obtainPen(data->scr,&set->dark_color);
  302.         switch (set->bg_type)
  303.         {
  304.         case    bg_file:
  305.             fileBackground(set,data);
  306.             return(data->win);
  307.             break;
  308.         case    bg_snap:
  309.             return(data->win=snapBackground(set,data));
  310.             break;
  311.         default:
  312.             return(data->win);
  313.             break;
  314.         }
  315.     }
  316.     }
  317.     return(0L);
  318. }
  319.  
  320. closeWindow(struct tm_sys_set *set,struct tm_data *data)
  321. {
  322.     int i;
  323.  
  324.     if(!set->bg_color.pen)     ReleasePen(data->scr->ViewPort.ColorMap,data->bg_color);
  325.     if(!set->bright_color.pen) ReleasePen(data->scr->ViewPort.ColorMap,data->bright_color);
  326.     if(!set->dark_color.pen)   ReleasePen(data->scr->ViewPort.ColorMap,data->dark_color);
  327.  
  328.     if(data->appwin)  RemoveAppWindow(data->appwin);
  329.     if(data->appport) DeleteMsgPort  (data->appport);
  330.  
  331.     if(data->win)
  332.     {
  333.     RemoveGList(data->win,data->win->FirstGadget,-1);
  334.     CloseWindow(data->win);
  335.     data->win=0L;
  336.     }
  337.  
  338.     for(i=0;i<data->num_of_gaug;i++)
  339.     if(data->Font[i])
  340.     {
  341.         CloseFont(data->Font[i]);
  342.         data->Font[i]=0L;
  343.     }
  344.  
  345.     if(data->on_public)
  346.     {
  347.     UnlockPubScreen((char *)&set->pub_name[0]);
  348.     data->on_public=FALSE;
  349.     }
  350.  
  351.     if(data->dt_object)
  352.     {
  353.     DisposeDTObject((Object *)data->dt_object);
  354.     data->dt_object=0L;
  355.     }
  356.  
  357.     if(data->bg_bm)
  358.     {
  359.     FreeBitMap(data->bg_bm);
  360.     data->bg_bm=0L;
  361.     }
  362. }
  363.