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