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