home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / WBStartup+ / Source / WBStartup+OS3.5 / ProgressWindow.c < prev    next >
C/C++ Source or Header  |  2000-01-26  |  14KB  |  428 lines

  1. #include <exec/types.h>
  2. #include <graphics/gfx.h>
  3. #include <proto/intuition.h>
  4. #include <proto/gadtools.h>
  5. #include <proto/graphics.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/icon.h>
  9. #include <proto/iffparse.h>
  10. #include <proto/datatypes.h>
  11. #include <proto/layers.h>
  12. #include <prefs/wbpattern.h>
  13. #include <prefs/prefhdr.h>
  14. #include <graphics/gfxmacros.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <exec/memory.h>
  19. #include <datatypes/datatypesclass.h>
  20. #include <datatypes/pictureclass.h>
  21.  
  22. #include "ProgressWindow.h"
  23. #include "WBStartup+.h"
  24.  
  25. #define winwidth 400
  26. #define winheight 80
  27. #define BARHEIGHT 12
  28. #define XOFFSET 75
  29.  
  30. #ifndef min
  31. #define min(x, y)  ((x) < (y) ? (x) : (y))
  32. #endif
  33.  
  34. #ifndef max
  35. #define max(x, y) (! (min (x, y)))
  36. #endif
  37.  
  38. #include <clib/alib_protos.h>
  39. #include <clib/icon_protos.h>
  40. #include <workbench/icon.h>
  41.  
  42. static UWORD BackgroundPen, HighlightTextPen, ShinePen, ShadowPen;
  43. ULONG __chip Crosshatch = 0x5555AAAA;
  44. static struct WBStartup *wbarg=NULL;
  45.  
  46.  
  47. struct ProgressWindowData *CreateProgressWindow(struct WBStartupPrefs *prefs);
  48. void CloseProgressWindow(struct ProgressWindowData *data);
  49. void UpdateProgressBar(struct Window *win, int current, int total);
  50. void ShowIconImage(struct ProgressWindowData *data, struct Image *image);
  51. BOOL LoadBitmap(struct WBStartupPrefs *prefs, struct ProgressWindowData *data, struct Screen *scr);
  52. BOOL InteractiveRunProgram(struct Window *win);
  53. void DisplayProgramName(struct ProgressWindowData *data, char *name, BOOL query);
  54.  
  55.  
  56. struct ProgressWindowData *CreateProgressWindow(struct WBStartupPrefs *prefs)
  57. {
  58.   ULONG scrwidth=640,scrheight=200;
  59.   struct Rectangle rect;
  60.   LONG screen_modeID;
  61.   void *vi;
  62.   char buffer[7],*TitleText="Amiga Workbench \0\0\0\0\0\0\0";
  63.   struct IntuiText iText = { 2,0,0,0,0,NULL,NULL,NULL};
  64.   struct DrawInfo *dri;
  65.   struct Screen *scr;
  66.  
  67.   struct DiskObject *diskobj=NULL;  /* This must be initialized to NULL */
  68.   struct Process *process;
  69.   char buf[200];
  70.   struct Image *image;
  71.   struct ProgressWindowData *windata;
  72.   BOOL success=FALSE;
  73.  
  74.   if (windata = AllocVec(sizeof(struct ProgressWindowData),MEMF_PUBLIC))
  75.   {
  76.     windata->bitmap=NULL;
  77.     windata->iconundobitmap=NULL;
  78.     windata->filenameundobitmap=NULL;
  79.  
  80.     if (scr=LockPubScreen(prefs->PubScreenName))
  81.     {
  82.       if (prefs->BackgroundType!=NONE)
  83.         LoadBitmap(prefs,windata,scr);
  84.  
  85.       /********************************************/
  86.       /* Determine the Size of the visible screen */
  87.       /********************************************/
  88.       screen_modeID = GetVPModeID(&(scr->ViewPort));
  89.       if (screen_modeID != INVALID_ID)
  90.       {
  91.         if (QueryOverscan(screen_modeID, &rect, OSCAN_TEXT))
  92.         {
  93.           scrwidth  = rect.MaxX - rect.MinX + 1;
  94.           scrheight = rect.MaxY - rect.MinY + 1;
  95.  
  96.           scrwidth  = min(scrwidth,scr->Width);
  97.           scrheight = min(scrheight,scr->Height);
  98.         }
  99.       }
  100.  
  101.       /*******************************/
  102.       /* Get WBStartup+'s Icon image */
  103.       /*******************************/
  104.       if (process = (struct Process *)FindTask(NULL))
  105.       {
  106.         NameFromLock(process->pr_HomeDir,buf,199);
  107.         AddPart(buf,process->pr_Task.tc_Node.ln_Name,199);
  108.  
  109.         if (diskobj=(GetIconTags (buf, TAG_DONE)))  // We release the Disk Object down below!
  110.           image = (struct Image *)diskobj->do_Gadget.GadgetRender;
  111.       }
  112.  
  113.       if (windata->win = OpenWindowTags(NULL,
  114.           WA_Left,   (scrwidth-winwidth)/2,
  115.           WA_Top,    (scrheight-winheight)/2,
  116.           WA_Width, winwidth,//(TextLength(&scr->RastPort,"Program Name",12)*2) + (TextLength(&scr->RastPort,"Priority",8)*2) + (2 * OUTSIDEBORDER) + scr->WBorLeft + scr->WBorRight,
  117.           WA_Height, winheight,
  118.           WA_AutoAdjust, TRUE,
  119.           WA_Activate, (prefs->Interactive) ? 1 : 0,
  120.           WA_IDCMP,  (prefs->Interactive) ? IDCMP_VANILLAKEY : 0L,
  121.           WA_Flags,  WFLG_SMART_REFRESH | WFLG_ACTIVATE,
  122.           WA_PubScreen, scr,
  123.           WA_PubScreenFallBack, "Workbench",
  124.           WA_SuperBitMap, windata->bitmap,
  125.           TAG_END))
  126.       {
  127.         /* Set Global Pen Numbers */
  128.         if (dri = GetScreenDrawInfo(scr))
  129.         {
  130.           BackgroundPen = dri->dri_Pens[BACKGROUNDPEN];
  131.           HighlightTextPen = dri->dri_Pens[HIGHLIGHTTEXTPEN];
  132.           ShinePen = dri->dri_Pens[SHINEPEN];
  133.           ShadowPen = dri->dri_Pens[SHADOWPEN];
  134.           FreeScreenDrawInfo( scr, dri );
  135.         }
  136.  
  137.         if (vi=GetVisualInfo(scr,TAG_DONE))
  138.         {
  139.           DrawBevelBox(windata->win->RPort, XOFFSET, 34, windata->win->Width-150, BARHEIGHT, GTBB_Recessed, TRUE, GT_VisualInfo, vi, TAG_DONE);  /* Draw Progress Indicator Box */
  140.           FreeVisualInfo(vi);
  141.         }
  142.  
  143.         /************************/
  144.         /* Write text to window */
  145.         /************************/
  146.         if (GetVar("Workbench",buffer,7,0)>=0)
  147.           strcat(TitleText,buffer);
  148.         iText.IText = TitleText;
  149.         iText.FrontPen = ShadowPen;
  150.         PrintIText(windata->win->RPort, &iText, ((windata->win->Width-IntuiTextLength(&iText))/2)+1, 16);
  151.         iText.FrontPen = HighlightTextPen;
  152.         PrintIText(windata->win->RPort, &iText, (windata->win->Width-IntuiTextLength(&iText))/2, 15);
  153.  
  154.         SetAfPt(windata->win->RPort, NULL, 0);
  155.  
  156.         /********************************/
  157.         /* Make our undo buffer bitmaps */
  158.         /********************************/
  159.         if (windata->bitmap)
  160.         {
  161.           /* icon undo bitmap */
  162.           if (windata->iconundobitmap=AllocBitMap(XOFFSET-windata->win->BorderRight,winheight-(windata->win->BorderTop+windata->win->BorderBottom),GetBitMapAttr(windata->bitmap,BMA_DEPTH),GetBitMapAttr(windata->bitmap,BMA_FLAGS),NULL))
  163.             BltBitMap(windata->bitmap,winwidth-XOFFSET,windata->win->BorderTop,windata->iconundobitmap,0,0,XOFFSET-windata->win->BorderRight,winheight-(windata->win->BorderTop+windata->win->BorderBottom),0x0C0,0xFF,NULL);
  164.           /* filename undo bitmap */
  165.           if (windata->filenameundobitmap=AllocBitMap(winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-windata->win->BorderBottom,GetBitMapAttr(windata->bitmap,BMA_DEPTH),GetBitMapAttr(windata->bitmap,BMA_FLAGS),NULL))
  166.             BltBitMap(windata->bitmap,XOFFSET,34+BARHEIGHT,windata->filenameundobitmap,0,0,winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-windata->win->BorderBottom,0x0C0,0xFF,NULL);
  167.         }
  168.  
  169.         success=TRUE;
  170.       }
  171.  
  172.       if (diskobj)
  173.       {
  174.         DrawImage(windata->win->RPort,image,((XOFFSET-windata->win->BorderLeft-image->Width)/2)+windata->win->BorderLeft,((80-image->Height)/2));
  175.         FreeDiskObject(diskobj);
  176.       }
  177.  
  178.       UnlockPubScreen(NULL,scr);
  179.     }
  180.   }
  181.  
  182.   if (!success)
  183.   {
  184.     FreeVec(windata);
  185.     windata=NULL;
  186.   }
  187.  
  188.   return(windata);
  189. }
  190.  
  191. void CloseProgressWindow(struct ProgressWindowData *data)
  192. {
  193.   CloseWindow(data->win);
  194.   if (data->bitmap)
  195.     FreeBitMap(data->bitmap);
  196.   if (data->iconundobitmap)
  197.     FreeBitMap(data->iconundobitmap);
  198.   if (data->filenameundobitmap)
  199.     FreeBitMap(data->filenameundobitmap);
  200.   FreeVec(data);
  201. }
  202.  
  203. void UpdateProgressBar(struct Window *win, int current, int total)
  204. {
  205.   int x;
  206.   x = ((win->Width-(2*(XOFFSET+2)))*current/total)+(XOFFSET+2);
  207.  
  208.   SetAfPt(win->RPort,(UWORD *)&Crosshatch,1);
  209.   SetBPen(win->RPort,ShinePen);
  210.   SetAPen(win->RPort,3);
  211.   RectFill(win->RPort,(XOFFSET+2),35,x,35+BARHEIGHT-3);
  212. }
  213.  
  214. void ShowIconImage(struct ProgressWindowData *data, struct Image *image)
  215. {
  216.   struct Region *reg;
  217.   struct Region *oldreg;
  218.   struct Rectangle rect;
  219.  
  220.   if (data->iconundobitmap)
  221.     BltBitMapRastPort(data->iconundobitmap,0,0,data->win->RPort,winwidth-XOFFSET,data->win->BorderTop,XOFFSET-data->win->BorderRight,winheight-data->win->BorderTop-data->win->BorderBottom,0x0C0);
  222.   else
  223.   {
  224.     SetAfPt(data->win->RPort,NULL,0);
  225.     SetAPen(data->win->RPort,BackgroundPen);
  226.     RectFill(data->win->RPort,winwidth-XOFFSET,data->win->BorderTop,winwidth-data->win->BorderRight-1,winheight-data->win->BorderBottom-1);
  227.   }
  228.  
  229.   rect.MinX=winwidth-XOFFSET;
  230.   rect.MinY=data->win->BorderTop;
  231.   rect.MaxX=winwidth-data->win->BorderRight-1;
  232.   rect.MaxY=winheight-data->win->BorderBottom-1;
  233.   if (reg=NewRegion())
  234.   {
  235.     OrRectRegion(reg,&rect);
  236.  
  237.     oldreg=InstallClipRegion(data->win->WLayer,reg);
  238.  
  239.     DrawImage(data->win->RPort,image,(400-XOFFSET)+((XOFFSET-image->Width)/2),((80-image->Height)/2));
  240.  
  241.     InstallClipRegion(data->win->WLayer,oldreg);
  242.  
  243.     DisposeRegion(reg);
  244.   }
  245.  
  246. }
  247.  
  248.  
  249.  
  250.  
  251.  
  252. BOOL LoadBitmap(struct WBStartupPrefs *prefs, struct ProgressWindowData *data, struct Screen *scr)
  253. {
  254.   /* Load the picture file with datatypes.library, copy the resulting bitmap and release the file */
  255.  
  256.   struct FrameInfo *o;   /* This is our datatype object */
  257.   struct BitMapHeader *bmhd=NULL;
  258.   struct BitMap       *bitmap;
  259.   struct gpLayout gpl;
  260.   char   pathname[200];
  261.   BOOL   success=FALSE;
  262.  
  263.   struct IFFHandle *iffhandle;
  264.   struct CollectionItem *ci;
  265.   LONG ifferror;
  266.  
  267.   ULONG width, height, xoff, yoff, mapwidth, mapheight;
  268.  
  269.   /***********************************************************************/
  270.   /* Get the filename of the graphics picture to use from WBPattern.prefs*/
  271.   /***********************************************************************/
  272.   if (!prefs->BackgroundFilename[0])
  273.   {
  274.     if (iffhandle = AllocIFF())
  275.     {
  276.       if (iffhandle->iff_Stream = (LONG)Open("ENV:sys/WBPattern.prefs",MODE_OLDFILE))
  277.       {
  278.         InitIFFasDOS(iffhandle);
  279.         if ((ifferror = OpenIFF(iffhandle,IFFF_READ)) == 0)
  280.         {
  281.           CollectionChunk(iffhandle, ID_PREF, ID_PTRN);
  282.  
  283.           for(;;)
  284.           {
  285.  
  286.           ifferror=ParseIFF(iffhandle, IFFPARSE_STEP);
  287.           
  288.           if (ifferror == IFFERR_EOC)
  289.             continue;
  290.           else if (ifferror)
  291.             break;
  292.  
  293.           if (ci = FindCollection(iffhandle, ID_PREF, ID_PTRN))
  294.             do {
  295.               if ((!(((struct WBPatternPrefs *)ci->ci_Data)->wbp_Flags & WBPF_PATTERN)) && (((struct WBPatternPrefs *)ci->ci_Data)->wbp_Which == prefs->BackgroundType-1))
  296.               {
  297.                 strncpy(pathname,(char *)(ci->ci_Data)+sizeof(struct WBPatternPrefs),((struct WBPatternPrefs *)ci->ci_Data)->wbp_DataLength);
  298.                 pathname[((struct WBPatternPrefs *)ci->ci_Data)->wbp_DataLength]=0;
  299.               }
  300.               ci = ci->ci_Next;
  301.             } while (ci);
  302.           }
  303.           CloseIFF(iffhandle);
  304.         }
  305.         Close(iffhandle->iff_Stream);
  306.       }
  307.       FreeIFF(iffhandle);
  308.     }
  309.   }
  310.   else
  311.     strcpy(pathname,prefs->BackgroundFilename);
  312.  
  313.   /*******************************************/
  314.   /* Read the graphics file and get a bitmap */
  315.   /*******************************************/
  316.   if (pathname[0])   /* Do we have a file name to use ? */
  317.   {
  318.     if (o = (struct FrameInfo *)NewDTObject(pathname,
  319.                                             DTA_SourceType,DTST_FILE,
  320.                                             DTA_GroupID,GID_PICTURE,
  321.                                             PDTA_Remap,TRUE,
  322.                                             PDTA_Screen,scr,
  323.                                             TAG_DONE))
  324.     {
  325.       Msg message = (Msg) &gpl;
  326.  
  327.       gpl.MethodID = DTM_PROCLAYOUT;
  328.       gpl.gpl_GInfo = NULL;
  329.       gpl.gpl_Initial = 1;
  330.  
  331.       if (DoMethodA((Object *)o, message))
  332.         GetDTAttrs((Object *)o,
  333.               PDTA_BitMapHeader,&bmhd,
  334.               PDTA_BitMap,&bitmap,
  335.               TAG_DONE);
  336.  
  337.  
  338.       /************************************************************/
  339.       /*   Tile the bitmap into a bitmap the size of our window   */
  340.       /************************************************************/
  341.       if (data->bitmap=AllocBitMap(winwidth,winheight,GetBitMapAttr(bitmap,BMA_DEPTH),GetBitMapAttr(bitmap,BMA_FLAGS),NULL))
  342.       {
  343.         mapwidth = bmhd->bmh_Width;
  344.         mapheight = bmhd->bmh_Height;
  345.         yoff = 0;
  346.         while (yoff < winheight)
  347.         {
  348.           xoff=0;
  349.           height = min(mapheight,winheight-yoff);
  350.           while (xoff < winwidth)
  351.           {
  352.             width = min(mapwidth,winwidth-xoff);
  353.             BltBitMap(bitmap,0,0,data->bitmap,xoff,yoff,width,height,0x0C0,0xFF,NULL);
  354.             xoff += width;
  355.           }
  356.           yoff += height;
  357.         }
  358.         success=TRUE;
  359.       }
  360.  
  361.       DisposeDTObject((Object *)o);
  362.     }
  363.   }
  364.  
  365.   return(success);
  366. }
  367.  
  368.  
  369. BOOL InteractiveRunProgram(struct Window *win)
  370. {
  371.   BOOL runprogram=FALSE;
  372.   struct IntuiMessage *msg;
  373.   BOOL done=FALSE;
  374.  
  375.   while (!done)
  376.   {
  377.     Wait(1L<<win->UserPort->mp_SigBit);
  378.  
  379.     while (msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  380.     {
  381.       if (msg->Class == IDCMP_VANILLAKEY)
  382.         if (msg->Code=='Y' || msg->Code=='y')
  383.         {
  384.           done=TRUE;
  385.           runprogram = TRUE;
  386.         }
  387.         else if (msg->Code=='N' || msg->Code=='n')
  388.         {
  389.           done=TRUE;
  390.           runprogram = FALSE;
  391.         }
  392.       ReplyMsg((struct Message *)msg);
  393.     }
  394.   }
  395.  
  396.   return(runprogram);
  397. }
  398.  
  399. void DisplayProgramName(struct ProgressWindowData *data, char *name, BOOL query)
  400. {
  401.   struct IntuiText iText = { 2,0,0,0,0,NULL,NULL,NULL};
  402.   char *outputbuffer;
  403.  
  404.   /* Fill the text area with our bitmap or blank space */
  405.   if (data->filenameundobitmap)
  406.     BltBitMapRastPort(data->filenameundobitmap,0,0,data->win->RPort,XOFFSET,34+BARHEIGHT,winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-data->win->BorderBottom,0x0C0);
  407.   else
  408.   {
  409.     SetAfPt(data->win->RPort,NULL,0);
  410.     SetAPen(data->win->RPort,BackgroundPen);
  411.     RectFill(data->win->RPort,XOFFSET,34+BARHEIGHT,winwidth-XOFFSET-1,winheight-data->win->BorderBottom-1);
  412.   }
  413.  
  414.   if (outputbuffer = AllocVec(strlen(name)+3,MEMF_PUBLIC))
  415.   {
  416.     strcpy(outputbuffer,name);
  417.     if (query)
  418.       strcat(outputbuffer," ?");
  419.     iText.IText = outputbuffer;
  420.     iText.FrontPen = ShadowPen;
  421.     PrintIText(data->win->RPort, &iText, ((data->win->Width-IntuiTextLength(&iText))/2)+1, 34+BARHEIGHT+((data->win->Height-34-BARHEIGHT-data->win->WScreen->Font->ta_YSize)/2)+2+1);
  422.     iText.FrontPen = HighlightTextPen;
  423.     PrintIText(data->win->RPort, &iText, (data->win->Width-IntuiTextLength(&iText))/2, 34+BARHEIGHT+((data->win->Height-34-BARHEIGHT-data->win->WScreen->Font->ta_YSize)/2)+2);
  424.     FreeVec(outputbuffer);
  425.   }
  426. }
  427.  
  428.