home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 568b.lha / PowerSource_v3.0 / source.lzh / source / Bin.c next >
C/C++ Source or Header  |  1991-09-15  |  34KB  |  1,134 lines

  1. /*----------------------------------------------------------------------*
  2.     binary.c Version 3.0 - © Copyright 1990-91 Jaba Development
  3.  
  4.     Author : Jan van den Baard
  5.     Purpose: The reading and writing of binary PS files.
  6.  *----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *  External data references.
  10.  */
  11. extern struct Screen            *MainScreen;
  12. extern struct Window            *MainWindow;
  13. extern struct FileSelector      *IODir;
  14. extern struct Image             *active;
  15. extern struct MyMenu            *MenuStrip, *ParentMenu;
  16. extern struct ItemList          *MenusList, *ItemsList, *SubsList;
  17. extern struct MyMenuItem        *ParentItem, dummy;
  18. extern struct GadgetList         Gadgets;
  19. extern struct BorderList         SBorders;
  20. extern struct SharedList         Images;
  21. extern struct NewWindow          nw_main;
  22. extern struct Gadget             TextGadget;
  23. extern struct ge_prefs           prefs;
  24. extern struct MemoryChain        Misc, Memory;
  25. extern ULONG                     WindowFlags, IDCMPFlags;
  26. extern UWORD                     GadgetCount, id, BackFill, WDBackFill;
  27. extern UWORD                     FrontPen, BackPen, Colors[], WBColors[];
  28. extern UWORD                     LightSide, DarkSide, ItemCount, SubCount, MenuCount;
  29. extern UWORD                     ItemTotal, SubTotal, MenuTotal, DEPTH;
  30. extern BOOL                      Saved, REQUESTER, WBSCREEN, bo_added;
  31. extern UBYTE                     name[512], wdt[80], wlb[MAXLABEL];
  32. extern UBYTE                     TextLabel[32];
  33.  
  34. extern struct ListItem          *CreateListItem();
  35. extern struct SharedBorder      *CreateSBorder();
  36. extern APTR                      GetBorder(), withdraw();
  37.  
  38. #define NUMLABELS                11
  39.  
  40. /*
  41.  *  Icon data.
  42.  */
  43. USHORT Image0_data[] = {
  44.   0x0000,  0x0000,  0x0000,  0x0800,  0x0000,  0x0000,  0x0000,  0x1800,
  45.   0x0000,  0x0000,  0x0000,  0x1800,  0x0000,  0x0000,  0x0000,  0x1800,
  46.   0x0000,  0x0000,  0x0000,  0x1800,  0x0000,  0x0000,  0x0000,  0x1800,
  47.   0x0020,  0xF800,  0x3E00,  0x1800,  0x0000,  0x0000,  0xC180,  0x1800,
  48.   0x0000,  0x0000,  0x0060,  0x1800,  0x0000,  0x0008,  0x0000,  0x1800,
  49.   0x0000,  0x0006,  0x0000,  0x1800,  0x0000,  0x00C1,  0xE000,  0x1800,
  50.   0x0000,  0xFF00,  0x1800,  0x1800,  0x0000,  0x0000,  0x0600,  0x1800,
  51.   0x0000,  0x0000,  0x0000,  0x1800,  0x0000,  0x0000,  0x0000,  0x1800,
  52.   0x0000,  0x0000,  0x0030,  0x1800,  0x0000,  0x0006,  0x00C0,  0x1800,
  53.   0x003F,  0x8019,  0xFF00,  0x1800,  0x0000,  0x0000,  0x0000,  0x1800,
  54.   0x0000,  0x0000,  0x0000,  0x1800,  0x0000,  0x0000,  0x0000,  0x1800,
  55.   0x7FFF,  0xFFFF,  0xFFFF,  0xF800,  0xFFFF,  0xFFFF,  0xFFFF,  0xF000,
  56.   0xEAAA,  0xAAAA,  0xAAAA,  0xA000,  0xEAAA,  0xAAAA,  0xAAAA,  0xA000,
  57.   0xEAAA,  0xAAAA,  0xAAAA,  0xA000,  0xEAAA,  0xAAAA,  0xAAAA,  0xA000,
  58.   0xEA80,  0x02AA,  0x008A,  0xA000,  0xEA80,  0x00A8,  0x000A,  0xA000,
  59.   0xEAA0,  0xA820,  0x2A0A,  0xA000,  0xEAA0,  0xA820,  0x2A8A,  0xA000,
  60.   0xEAA0,  0xA820,  0x02AA,  0xA000,  0xEAA0,  0xA828,  0x002A,  0xA000,
  61.   0xEAA0,  0x002A,  0x000A,  0xA000,  0xEAA0,  0x00AA,  0xA00A,  0xA000,
  62.   0xEAA0,  0xAAAA,  0xA80A,  0xA000,  0xEAA0,  0xAAA2,  0xAA0A,  0xA000,
  63.   0xEAA0,  0xAAA0,  0xA80A,  0xA000,  0xEAA0,  0xAAA0,  0x280A,  0xA000,
  64.   0xEA80,  0x2AA0,  0x002A,  0xA000,  0xEA80,  0x2AA2,  0x00AA,  0xA000,
  65.   0xEAAA,  0xAAAA,  0xAAAA,  0xA000,  0xEAAA,  0xAAAA,  0xAAAA,  0xA000,
  66.   0xEAAA,  0xAAAA,  0xAAAA,  0xA000,  0x8000,  0x0000,  0x0000,  0x0000
  67. };
  68.  
  69. USHORT Image1_data[] = {
  70.   0xFFFF,  0xFFFF,  0xFFFF,  0xF000,  0xC000,  0x0000,  0x0000,  0x0000,
  71.   0xC000,  0x0000,  0x0000,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  72.   0xC03F,  0xFC01,  0xFF70,  0x0000,  0xC000,  0x0306,  0x0080,  0x0000,
  73.   0xC000,  0x00C8,  0x0000,  0x0000,  0xC000,  0x0000,  0xC000,  0x0000,
  74.   0xC000,  0x0000,  0x3C00,  0x0000,  0xC000,  0x0000,  0x03C0,  0x0000,
  75.   0xC000,  0xF800,  0x0020,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  76.   0xC000,  0x0000,  0x0000,  0x0000,  0xC000,  0x0018,  0x0000,  0x0000,
  77.   0xC000,  0x0006,  0x0000,  0x0000,  0xC000,  0x0001,  0xC400,  0x0000,
  78.   0xC020,  0x8000,  0x3800,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  79.   0xC000,  0x0000,  0x0000,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  80.   0xC000,  0x0000,  0x0000,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  81.   0x8000,  0x0000,  0x0000,  0x0000,  0x0000,  0x0000,  0x0000,  0x0800,
  82.   0x2AAA,  0xAAAA,  0xAAAA,  0xB800,  0x2AAA,  0xAAAA,  0xAAAA,  0xB800,
  83.   0x2AAA,  0xAAAA,  0xAAAA,  0xB800,  0x2A80,  0x02AA,  0x008A,  0xB800,
  84.   0x2A80,  0x00A8,  0x000A,  0xB800,  0x2A80,  0xA820,  0x2A0A,  0xB800,
  85.   0x2AA0,  0xA820,  0x2A8A,  0xB800,  0x2AA0,  0xA820,  0x02AA,  0xB800,
  86.   0x2AA0,  0xA828,  0x002A,  0xB800,  0x2AA0,  0x002A,  0x000A,  0xB800,
  87.   0x2AA0,  0x00AA,  0xA00A,  0xB800,  0x2AA0,  0xAAAA,  0xA80A,  0xB800,
  88.   0x2AA0,  0xAAA2,  0xAA0A,  0xB800,  0x2AA0,  0xAAA0,  0xAA0A,  0xB800,
  89.   0x2AA0,  0xAAA0,  0x280A,  0xB800,  0x2A80,  0x2AA0,  0x002A,  0xB800,
  90.   0x2A80,  0x2AA2,  0x00AA,  0xB800,  0x2AAA,  0xAAAA,  0xAAAA,  0xB800,
  91.   0x2AAA,  0xAAAA,  0xAAAA,  0xB800,  0x2AAA,  0xAAAA,  0xAAAA,  0xB800,
  92.   0x2AAA,  0xAAAA,  0xAAAA,  0xB800,  0x7FFF,  0xFFFF,  0xFFFF,  0xF800
  93. };
  94.  
  95. struct Image Image1_image = {
  96.   0,0,53,23,2,(USHORT *)&Image1_data,0x03,0x00,NULL };
  97. struct Image Image0_image = {
  98.   0,0,53,23,2,(USHORT *)&Image0_data,0x03,0x00,NULL };
  99.  
  100. struct IconBase *IconBase;
  101.  
  102. /*
  103.  * write a 'PS' icon
  104.  */
  105. BOOL WriteIcon(UBYTE *nme)
  106. {
  107.     struct DiskObject  icon;
  108.     struct Gadget      icon_Gadget;
  109.     BOOL               ret;
  110.  
  111.     if((IconBase = OpenLibrary(ICONNAME,0L))) {
  112.         icon_Gadget.NextGadget   =   NULL;
  113.         icon_Gadget.LeftEdge     =   0;
  114.         icon_Gadget.TopEdge      =   0;
  115.         icon_Gadget.Width        =   53;
  116.         icon_Gadget.Height       =   23;
  117.         icon_Gadget.Flags        =   GADGIMAGE+GADGHIMAGE;
  118.         icon_Gadget.Activation   =   RELVERIFY+GADGIMMEDIATE;
  119.         icon_Gadget.GadgetType   =   BOOLGADGET;
  120.         icon_Gadget.GadgetRender =   (APTR)&Image0_image;
  121.         icon_Gadget.SelectRender =   (APTR)&Image1_image;
  122.         icon_Gadget.GadgetText   =   NULL;
  123.         icon_Gadget.MutualExclude=   NULL;
  124.         icon_Gadget.SpecialInfo  =   NULL;
  125.         icon_Gadget.GadgetID     =   NULL;
  126.         icon_Gadget.UserData     =   NULL;
  127.  
  128.         icon.do_Magic            =   WB_DISKMAGIC;
  129.         icon.do_Version          =   WB_DISKVERSION;
  130.         icon.do_Gadget           =   icon_Gadget;
  131.         icon.do_Type             =   WBPROJECT;
  132.         icon.do_DefaultTool      =   (char *)":PowerSource";
  133.         icon.do_ToolTypes        =   NULL;
  134.         icon.do_CurrentX         =   NO_ICON_POSITION;
  135.         icon.do_CurrentY         =   NO_ICON_POSITION;
  136.         icon.do_DrawerData       =   NULL;
  137.         icon.do_ToolWindow       =   NULL;
  138.         icon.do_StackSize        =   8192;
  139.         ret = PutDiskObject((char *)nme,&icon);
  140.  
  141.         CloseLibrary(IconBase);
  142.         if(NOT ret) return(FALSE);
  143.         return(TRUE);
  144.     }
  145.     return(FALSE);
  146. }
  147.  
  148. /*
  149.  * Check for an error.
  150.  */
  151. long CheckErr( void )
  152. {
  153.     char    *err;
  154.  
  155.     if(err = IoErrToStr()) {
  156.         enable_window();
  157.         ok();
  158.         Error(err);
  159.         return(FALSE);
  160.     }
  161.     return(TRUE);
  162. }
  163.  
  164. /*
  165.  * Write the source labels.
  166.  */
  167. long WriteLabels( BPTR file )
  168. {
  169.     ULONG   num = NUMLABELS;
  170.  
  171.     Write(file,(char *)&num,4L);
  172.     Write(file,(char *)&TextLabel[0],NUMLABELS * 32);
  173.  
  174.     return(CheckErr());
  175. }
  176.  
  177. /*
  178.  * Write the gadget shared borders.
  179.  */
  180. long WriteSBord( BPTR file )
  181. {
  182.     register struct SharedBorder    *sbrd;
  183.     register struct Border          *brd;
  184.     ULONG                            ac;
  185.  
  186.     for(sbrd = SBorders.Head, ac = 0L; sbrd->Succ; sbrd = sbrd->Succ, ac++);
  187.  
  188.     Write(file,(char *)&ac,4L);
  189.  
  190.     for(sbrd = SBorders.Head; sbrd->Succ; sbrd = sbrd->Succ) {
  191.         brd = sbrd->Border;
  192.         Write(file,(char *)&sbrd->SpecialFlags,2L);
  193.         while(1) {
  194.             Write(file,(char *)brd,(long)sizeof(struct Border));
  195.             if(NOT TestBits((ULONG)sbrd->SpecialFlags,SB_HIGHBORDER))
  196.                 Write(file,(char *)brd->XY,(long)(brd->Count << 2));
  197.             if(NOT(brd = brd->NextBorder)) break;
  198.         }
  199.     }
  200.  
  201.     return(CheckErr());
  202. }
  203.  
  204. /*
  205.  * Get the account number of each border.
  206.  */
  207. void BuildBAccounts( void )
  208. {
  209.     register struct MyGadget    *mg;
  210.     register struct Gadget      *g;
  211.  
  212.     for(mg = Gadgets.Head; mg->Succ; mg = mg->Succ) {
  213.         g = &mg->Gadget;
  214.         if(NOT TestBits((ULONG)g->GadgetType,PROPGADGET)) {
  215.             if(NOT TestBits((ULONG)mg->SpecialFlags,BORDERONLY)) {
  216.                 if(NOT TestBits((ULONG)g->Flags,GADGIMAGE))
  217.                     mg->BorderAccount = Account(g->GadgetRender);
  218.                 if(TestBits((ULONG)mg->SpecialFlags,OS20HIGHBORDER))
  219.                     mg->HBorderAccount = Account(g->SelectRender);
  220.             }
  221.         }
  222.     }
  223. }
  224.  
  225. /*
  226.  * Write the image bank.
  227.  */
  228. long WriteBank( BPTR file )
  229. {
  230.     register struct SharedImage  *sim;
  231.     register struct Image        *image;
  232.     ULONG                         ac;
  233.  
  234.     for(sim = Images.Head, ac = 0L; sim->Succ; sim = sim->Succ, ac++);
  235.  
  236.     Write(file,(char *)&ac,4L);
  237.  
  238.     for(sim = Images.Head; sim->Succ; sim = sim->Succ) {
  239.         image = sim->Image;
  240.         Write(file,(char *)image,(long)sizeof(struct Image));
  241.         Write(file,(char *)image->ImageData,(long)(RASSIZE(image->Width,image->Height) * image->Depth));
  242.         Write(file,(char *)&sim->Colors[0],64L);
  243.         Write(file,(char *)&sim->CheckSum,4L);
  244.     }
  245.     return(CheckErr());
  246. }
  247.  
  248. /*
  249.  * Get the account number of each image used by a gadget.
  250.  */
  251. void BuildAccounts( void )
  252. {
  253.     register struct MyGadget    *mg;
  254.     register struct Gadget      *g;
  255.  
  256.     for(mg = Gadgets.Head; mg->Succ; mg = mg->Succ) {
  257.         g = &mg->Gadget;
  258.         if(TestBits((ULONG)g->Flags,GADGIMAGE))
  259.             mg->RenderAccount = bank_account(g->GadgetRender);
  260.         if(TestBits((ULONG)g->Flags,GADGHIMAGE)) {
  261.             if(NOT TestBits((ULONG)g->Flags,GADGHBOX)) {
  262.                 if(NOT TestBits((ULONG)mg->SpecialFlags,OS20HIGHBORDER))
  263.                     mg->SelectAccount = bank_account(g->SelectRender);
  264.             }
  265.         }
  266.     }
  267. }
  268.  
  269. /*
  270.  * Write a gadget.
  271.  */
  272. long WriteGadget( BPTR file, struct MyGadget *g )
  273. {
  274.     struct Gadget       *gadget;
  275.     struct IntuiText    *t;
  276.     struct StringInfo   *s;
  277.     struct Border       *b;
  278.  
  279.     gadget = &g->Gadget;
  280.  
  281.     Write(file,(char *)gadget,(long)(sizeof(struct MyGadget) - 8L));
  282.  
  283.     if(t = gadget->GadgetText) {
  284.         while(1) {
  285.             Write(file,(char *)t,(long)sizeof(struct IntuiText));
  286.             Write(file,(char *)t->IText,80L);
  287.             if(NOT (t = t->NextText)) break;
  288.         }
  289.     }
  290.  
  291.     if((TestBits((ULONG)g->SpecialFlags,BORDERONLY)) OR
  292.        (TestBits((ULONG)g->SpecialFlags,NOBORDER))) {
  293.         b = (struct Border *)gadget->GadgetRender;
  294.         while(1) {
  295.             Write(file,(char *)b,(long)sizeof(struct Border));
  296.             Write(file,(char *)b->XY,(long)(b->Count << 2));
  297.             if(NOT(b = b->NextBorder)) break;
  298.         }
  299.     }
  300.  
  301.     if(TestBits((ULONG)gadget->GadgetType,PROPGADGET))
  302.         Write(file,(char *)gadget->SpecialInfo,(long)sizeof(struct PropInfo));
  303.  
  304.     if(TestBits((ULONG)gadget->GadgetType,STRGADGET)) {
  305.         s = (struct StringInfo *)gadget->SpecialInfo;
  306.         Write(file,(char *)s,(long)sizeof(struct StringInfo));
  307.         Write(file,(char *)s->Buffer,(long)s->MaxChars);
  308.     }
  309.     return(CheckErr());
  310. }
  311.  
  312. /*
  313.  * Write the window/requester texts.
  314.  */
  315. long WriteWRTexts( BPTR file )
  316. {
  317.     register struct IntuiText   *t;
  318.     ULONG                        num = 0L;
  319.  
  320.     if(t = TextGadget.GadgetText) {
  321.         num++;
  322.         while(1) {
  323.             if(NOT (t = t->NextText)) break;
  324.             num++;
  325.         }
  326.     }
  327.  
  328.     Write(file,(char *)&num,4L);
  329.  
  330.     if(num) {
  331.         t = TextGadget.GadgetText;
  332.         while(1) {
  333.             Write(file,(char *)t,(long)sizeof(struct IntuiText));
  334.             Write(file,(char *)t->IText,80L);
  335.             if(NOT (t = t->NextText)) break;
  336.         }
  337.     }
  338.     return(CheckErr());
  339. }
  340.  
  341. /*
  342.  * Write a menuitem.
  343.  */
  344. void WriteItem( BPTR file, struct MyMenuItem *item )
  345. {
  346.     struct IntuiText    *itext;
  347.  
  348.     Write(file,(char *)item,(long)sizeof(struct MyMenuItem));
  349.     if(TestBits((ULONG)item->Flags,ITEMTEXT)) {
  350.         itext = (struct IntuiText *)item->ItemFill;
  351.         Write(file,(char *)itext,(long)sizeof(struct IntuiText));
  352.         Write(file,(char *)itext->IText,80L);
  353.     }
  354. }
  355.  
  356. /*
  357.  * Write the menus.
  358.  */
  359. long WriteMenus( BPTR file )
  360. {
  361.     register struct MyMenu      *menu;
  362.     register struct MyMenuItem  *item, *item1, *sub, *sub1;
  363.     ULONG                        menus;
  364.  
  365.     if(MenuStrip) {
  366.         menus = 1L;
  367.         Write(file,(char *)&menus,4L);
  368.  
  369.         for(menu = MenuStrip; menu; menu = menu->NextMenu) {
  370.             if(menu->FirstItem == &dummy) {
  371.                 menu->FirstItem = NULL;
  372.                 Write(file,(char *)menu,(long)sizeof(struct MyMenu));
  373.                 menu->FirstItem = &dummy;
  374.             } else
  375.                 Write(file,(char *)menu,(long)sizeof(struct MyMenu));
  376.  
  377.             if((item = menu->FirstItem) AND (item != &dummy)) {
  378.                 while(1) {
  379.                     item1 = item->NextItem;
  380.                     WriteItem(file,item);
  381.                     if((sub = item->SubItem)) {
  382.                         while(1) {
  383.                             sub1 = sub->NextItem;
  384.                             WriteItem(file,sub);
  385.                             if(NOT(sub = sub1)) break;
  386.                         }
  387.                     }
  388.                     if(NOT(item = item1)) break;
  389.                 }
  390.             }
  391.             if(NOT CheckErr())
  392.                 return(FALSE);
  393.         }
  394.     } else {
  395.         menus = 0L;
  396.         Write(file,(char *)&menus,4L);
  397.     }
  398.     return(CheckErr());
  399. }
  400.  
  401. /*
  402.  * Write the binary file.
  403.  */
  404. void WriteBinGadgets( void )
  405. {
  406.     register struct MyGadget    *g;
  407.     struct ViewPort             *vp;
  408.     struct NewWindow             nw;
  409.     struct BinHeader             head;
  410.     ULONG                        rc;
  411.     BPTR                         file;
  412.     UWORD                        i;
  413.  
  414.     strcpy((char *)IODir->fr_HeadLine,"Save Binary");
  415.  
  416.     IODir->fr_Screen = MainScreen;
  417.     IODir->fr_Caller = MainWindow;
  418.  
  419.     IODir->fr_Flags |= FR_NoInfo;
  420.  
  421.     rc = FileRequest(IODir);
  422.  
  423.     if(rc == FREQ_CANCELED)
  424.         return;
  425.     else if(rc) {
  426.         Error("FileRequester won't open !");
  427.         return;
  428.     }
  429.  
  430.     strcpy((char *)&name[0],(char *)IODir->fr_DirName);
  431.     strcat((char *)&name[0],(char *)IODir->fr_FileName);
  432.  
  433.     CheckName();
  434.  
  435.     disable_window();
  436.     buisy();
  437.  
  438.     SetWindowTitles(MainWindow,(char *)-1L,"Saving Binary......");
  439.  
  440.     vp = &MainScreen->ViewPort;
  441.  
  442.     if(NOT(file = Open((char *)&name[0],MODE_NEWFILE))) {
  443.         enable_window();
  444.         ok();
  445.         Error("Can't open write file !");
  446.         return;
  447.     }
  448.  
  449.     /* write file header */
  450.     head.FileType   =   TYPE;
  451.     head.Version    =   GE_VERSION;
  452.     head.ScrDepth   =   MainScreen->BitMap.Depth;
  453.  
  454.     for(i = 0; i < 32; i++)
  455.         head.Colors[i] = (USHORT)GetRGB4(vp->ColorMap,(long)i);
  456.  
  457.     head.FPen       =   FrontPen;
  458.     head.BPen       =   BackPen;
  459.     head.BackFill   =   BackFill;
  460.     head.WDBackFill =   WDBackFill;
  461.     head.LightSide  =   LightSide;
  462.     head.DarkSide   =   DarkSide;
  463.     head.FlagsBits  =   NULL;
  464.     head.SpareFlags =   NULL;
  465.  
  466.     for(i = 0; i < 6; i++)
  467.         head.Spare[i] = 0;
  468.  
  469.     if(REQUESTER)   head.FlagsBits |= BH_REQUESTER;
  470.     if(WBSCREEN)    head.FlagsBits |= BH_WORKBENCH;
  471.  
  472.     Write(file,(char *)&head,(long)sizeof(struct BinHeader));
  473.  
  474.     /* write preferences */
  475.     Write(file,(char *)&prefs,(long)sizeof(struct ge_prefs));
  476.  
  477.     /* write window specifics */
  478.     nw.LeftEdge     =   MainWindow->LeftEdge;
  479.     nw.TopEdge      =   MainWindow->TopEdge;
  480.     nw.Width        =   MainWindow->Width;
  481.     nw.Height       =   MainWindow->Height;
  482.     nw.DetailPen    =   MainWindow->DetailPen;
  483.     nw.BlockPen     =   MainWindow->BlockPen;
  484.     nw.Flags        =   WindowFlags;
  485.     nw.IDCMPFlags   =   IDCMPFlags;
  486.     nw.FirstGadget  =   NULL;
  487.     nw.CheckMark    =   NULL;
  488.     nw.Title        =   NULL;
  489.     nw.Screen       =   NULL;
  490.     nw.BitMap       =   NULL;
  491.     nw.MinWidth     =   MainWindow->MinWidth;
  492.     nw.MinHeight    =   MainWindow->MinHeight;
  493.     nw.MaxWidth     =   MainWindow->MaxWidth;
  494.     nw.MaxHeight    =   MainWindow->MaxHeight;
  495.     nw.Type         =   CUSTOMSCREEN;
  496.  
  497.     Write(file,(char *)&nw,(long)sizeof(struct NewWindow));
  498.  
  499.     /* write title + label */
  500.     Write(file,(char *)&wdt[0],80L);
  501.     Write(file,(char *)&wlb[0],MAXLABEL);
  502.  
  503.     if(NOT CheckErr())
  504.         goto writeError;
  505.  
  506.     /* write the source labels */
  507.     if(NOT WriteLabels(file))
  508.         goto writeError;
  509.  
  510.     /* write the gadget borders */
  511.     BuildBAccounts();
  512.     if(NOT WriteSBord(file))
  513.         goto writeError;
  514.  
  515.     /* write the image bank */
  516.     BuildAccounts();
  517.     if(NOT WriteBank(file))
  518.         goto writeError;
  519.  
  520.     /* write the window/requester texts */
  521.     if(NOT WriteWRTexts(file))
  522.         goto writeError;
  523.  
  524.     /* write the gadgets */
  525.     rc = (ULONG)GadgetCount;
  526.  
  527.     Write(file,(char *)&rc,4L);
  528.  
  529.     if(rc) {
  530.         for(g = Gadgets.Head; g->Succ; g = g->Succ) {
  531.             if(NOT WriteGadget(file,g))
  532.                 goto writeError;
  533.         }
  534.     }
  535.  
  536.     /* write the menus */
  537.     if(NOT WriteMenus(file))
  538.         goto writeError;
  539.  
  540.     if(NOT CheckErr())
  541.         goto writeError;
  542.  
  543.     Close(file);
  544.     enable_window();
  545.     ok();
  546.  
  547.     if(NOT WriteIcon((UBYTE *)&name[0]))
  548.         Error("Error writing the Icon !");
  549.  
  550.     Saved = TRUE;
  551.  
  552.     refresh();
  553.     return;
  554.  
  555. writeError:
  556.     Close(file);
  557.     DeleteFile((char *)&name[0]);
  558.     refresh();
  559. }
  560.  
  561. /*
  562.  * Read the source labels.
  563.  */
  564. long ReadLabels( BPTR file )
  565. {
  566.     ULONG   num;
  567.  
  568.     Read(file,(char *)&num,4L);
  569.     Read(file,(char *)&TextLabel[0],num * 32);
  570.  
  571.     return(CheckErr());
  572. }
  573.  
  574. /*
  575.  * Read a single border.
  576.  */
  577. struct Border *ReadBorder( BPTR file , ULONG flags )
  578. {
  579.     struct Border   *brd;
  580.  
  581.     if(NOT(brd = (struct Border *)Alloc(&Memory,(long)sizeof(struct Border))))
  582.         return(NULL);
  583.     Read(file,(char *)brd,(long)sizeof(struct Border));
  584.     if(NOT TestBits(flags,SB_HIGHBORDER)) {
  585.         if(NOT(brd->XY = (SHORT *)Alloc(&Memory,(long)(brd->Count << 2))))
  586.             return(NULL);
  587.         Read(file,(char *)brd->XY,(long)(brd->Count << 2));
  588.     }
  589.     return(brd);
  590. }
  591.  
  592. /*
  593.  * Read the gadget shared borders.
  594.  */
  595. long ReadSBord( BPTR file )
  596. {
  597.     register struct SharedBorder    *sbrd;
  598.     register struct Border          *brd, *brd1;
  599.     ULONG                            ac,i;
  600.  
  601.     Read(file,(char *)&ac,4L);
  602.  
  603.     for(i = 0; i < ac; i++) {
  604.         if(NOT(sbrd = CreateSBorder(NULL)))
  605.             goto noMem;
  606.         sbrd->UseCount = NULL;
  607.         Read(file,(char *)&sbrd->SpecialFlags,2L);
  608.         if(NOT(brd = ReadBorder(file,(ULONG)sbrd->SpecialFlags)))
  609.             goto noMem;
  610.         sbrd->Border = brd;
  611.         if(TestBits((ULONG)sbrd->SpecialFlags,SB_HIGHBORDER))
  612.             brd->XY = sbrd->Pred->Border->XY;
  613.         if(brd->NextBorder) {
  614.             while(1) {
  615.                 if(NOT(brd1 = ReadBorder(file,(ULONG)sbrd->SpecialFlags)))
  616.                     goto noMem;
  617.                 if(TestBits((ULONG)sbrd->SpecialFlags,SB_HIGHBORDER))
  618.                     brd1->XY = sbrd->Pred->Border->NextBorder->XY;
  619.                 brd->NextBorder = brd1;
  620.                 if(NOT brd1->NextBorder) break;
  621.                 brd = brd1;
  622.             }
  623.         }
  624.     }
  625.     return(CheckErr());
  626.  
  627. noMem:
  628.     enable_window();
  629.     ok();
  630.     Error("Out of memory !");
  631.     return(FALSE);
  632. }
  633.  
  634. /*
  635.  * Read the image bank.
  636.  */
  637. long ReadBank( BPTR file )
  638. {
  639.     register struct SharedImage *sim;
  640.     register struct Image       *image;
  641.     ULONG                        ac, i, rs;
  642.  
  643.     Read(file,(char *)&ac,4L);
  644.  
  645.     for(i = 0; i < ac; i++) {
  646.         if(NOT(sim = (struct SharedImage *)Alloc(&Memory,(long)sizeof(struct SharedImage))))
  647.             goto noMem;
  648.         AddTail((void *)&Images,(void *)sim);
  649.         if(NOT(image = (struct Image *)Alloc(&Memory,(long)sizeof(struct Image))))
  650.             goto noMem;
  651.         Read(file,(char *)image,(long)sizeof(struct Image));
  652.         rs = (ULONG)(RASSIZE(image->Width,image->Height) * image->Depth);
  653.         if(NOT(image->ImageData = (UWORD *)AllocMem(rs,MEMF_CHIP)))
  654.             goto noMem;
  655.         Read(file,(char *)image->ImageData,rs);
  656.         Read(file,(char *)&sim->Colors[0],64L);
  657.         Read(file,(char *)&sim->CheckSum,4L);
  658.         sim->Image = image;
  659.  
  660.         if(NOT CheckErr())
  661.             return(FALSE);
  662.     }
  663.     active = Images.Head->Image;
  664.     return(CheckErr());
  665.  
  666. noMem:
  667.     enable_window();
  668.     ok();
  669.     Error("Out of memory !");
  670.     return(FALSE);
  671. }
  672.  
  673. /*
  674.  * Read a single IntuiText.
  675.  */
  676. struct IntuiText *ReadIText( BPTR file )
  677. {
  678.     struct IntuiText    *t;
  679.  
  680.     if(NOT(t = (struct IntuiText *)Alloc(&Memory,(long)sizeof(struct IntuiText))))
  681.         return(NULL);
  682.     Read(file,(char *)t,(long)sizeof(struct IntuiText));
  683.     if(NOT(t->IText = (UBYTE *)Alloc(&Memory,80L)))
  684.         return(NULL);
  685.     Read(file,(char *)t->IText,80L);
  686.     return(t);
  687. }
  688.  
  689. /*
  690.  * Read a gadget.
  691.  */
  692. long ReadGadget( BPTR file )
  693. {
  694.     struct MyGadget     *g;
  695.     struct Gadget       *gadget;
  696.     struct IntuiText    *t, *t1;
  697.     struct StringInfo   *s;
  698.     struct Border       *b, *b1;
  699.  
  700.     if(NOT(g = (struct MyGadget *)Alloc(&Memory,(long)sizeof(struct MyGadget))))
  701.         goto noMem;
  702.     AddTail((void *)&Gadgets,(void *)g);
  703.  
  704.     gadget = &g->Gadget;
  705.  
  706.     Read(file,(char *)gadget,(long)(sizeof(struct MyGadget) - 8L));
  707.  
  708.     if(gadget->GadgetText) {
  709.         if(NOT(t = ReadIText(file)))
  710.             goto noMem;
  711.         gadget->GadgetText = t;
  712.         if(t->NextText) {
  713.             while(1) {
  714.                 if(NOT(t1 = ReadIText(file)))
  715.                     goto noMem;
  716.                 t->NextText = t1;
  717.                 if(NOT t1->NextText) break;
  718.                 t = t1;
  719.             }
  720.         }
  721.     }
  722.  
  723.     if((TestBits((ULONG)g->SpecialFlags,BORDERONLY)) OR
  724.        (TestBits((ULONG)g->SpecialFlags,NOBORDER))) {
  725.         if(NOT(b = ReadBorder(file,NULL)))
  726.             goto noMem;
  727.         gadget->GadgetRender = (APTR)b;
  728.  
  729.         if(b->NextBorder) {
  730.             while(1) {
  731.                 if(NOT(b1 = ReadBorder(file,NULL)))
  732.                     goto noMem;
  733.                 b->NextBorder = b1;
  734.                 if(NOT b1->NextBorder) break;
  735.                 b = b1;
  736.             }
  737.         }
  738.     } else if(NOT TestBits((ULONG)gadget->GadgetType,PROPGADGET)) {
  739.         if(NOT TestBits((ULONG)gadget->Flags,GADGIMAGE)) {
  740.             gadget->GadgetRender = GetBorder(g->BorderAccount);
  741.             AddUser(gadget->GadgetRender);
  742.         }
  743.         if(TestBits((ULONG)g->SpecialFlags,OS20HIGHBORDER)) {
  744.             gadget->SelectRender = GetBorder(g->HBorderAccount);
  745.             AddUser(gadget->SelectRender);
  746.         }
  747.     }
  748.  
  749.     if(TestBits((ULONG)gadget->Flags,GADGIMAGE)) {
  750.         gadget->GadgetRender = withdraw(g->RenderAccount);
  751.         add_user(gadget->GadgetRender);
  752.     }
  753.  
  754.     if(TestBits((ULONG)gadget->Flags,GADGHIMAGE)) {
  755.         if(NOT TestBits((ULONG)gadget->Flags,GADGHBOX)) {
  756.             if(NOT TestBits((ULONG)g->SpecialFlags,OS20HIGHBORDER)) {
  757.                 gadget->SelectRender = withdraw(g->SelectAccount);
  758.                 add_user(gadget->SelectRender);
  759.             }
  760.         }
  761.     }
  762.  
  763.     if(TestBits((ULONG)gadget->GadgetType,PROPGADGET)) {
  764.         if(NOT TestBits((ULONG)gadget->Flags,GADGIMAGE)) {
  765.             if(NOT(gadget->GadgetRender = (APTR)Alloc(&Memory,(long)sizeof(struct Image))))
  766.                 goto noMem;
  767.         }
  768.         if(NOT(gadget->SpecialInfo = (APTR)Alloc(&Memory,(long)sizeof(struct PropInfo))))
  769.             goto noMem;
  770.         Read(file,(char *)gadget->SpecialInfo,sizeof(struct PropInfo));
  771.     } else if(TestBits((ULONG)gadget->GadgetType,STRGADGET)) {
  772.         if(NOT(s = (struct StringInfo *)Alloc(&Memory,(long)sizeof(struct StringInfo))))
  773.             goto noMem;
  774.         Read(file,(char *)s,(long)sizeof(struct StringInfo));
  775.         if(NOT(s->Buffer = (UBYTE *)Alloc(&Memory,(long)s->MaxChars)))
  776.             goto noMem;
  777.         Read(file,(char *)s->Buffer,(long)s->MaxChars);
  778.         if(s->UndoBuffer) {
  779.             if(NOT(s->UndoBuffer = (UBYTE *)Alloc(&Memory,(long)s->MaxChars)))
  780.                 goto noMem;
  781.         }
  782.         gadget->SpecialInfo = (APTR)s;
  783.     }
  784.  
  785.     return(CheckErr());
  786.  
  787. noMem:
  788.     enable_window();
  789.     ok();
  790.     Error("Out of memory !");
  791.     return(FALSE);
  792. }
  793.  
  794. /*
  795.  * Read the window/requester texts.
  796.  */
  797. long ReadWRTexts( BPTR file )
  798. {
  799.     register struct IntuiText   *t, *t1;
  800.     ULONG                        num;
  801.  
  802.     Read(file,(char *)&num,4L);
  803.  
  804.     if(num) {
  805.         if(NOT(t = ReadIText(file)))
  806.             goto noMem;
  807.         TextGadget.GadgetText = t;
  808.         if(t->NextText) {
  809.             while(1) {
  810.                 if(NOT(t1 = ReadIText(file)))
  811.                     goto noMem;
  812.                 t->NextText = t1;
  813.                 if(NOT t1->NextText) break;
  814.                 t = t1;
  815.             }
  816.         }
  817.     }
  818.     return(CheckErr());
  819.  
  820. noMem:
  821.     enable_window();
  822.     ok();
  823.     Error("Out of memory !");
  824.     return(FALSE);
  825. }
  826.  
  827. /*
  828.  * Read a single MenuItem.
  829.  */
  830. struct MyMenuItem *ReadItem( BPTR file )
  831. {
  832.     register struct MyMenuItem  *item;
  833.  
  834.     if(NOT(item = (struct MyMenuItem *)Alloc(&Memory,(long)sizeof(struct MyMenuItem))))
  835.         goto noMem;
  836.     Read(file,(char *)item,(long)sizeof(struct MyMenuItem));
  837.     if(TestBits((ULONG)item->Flags,ITEMTEXT)) {
  838.         if(NOT(item->ItemFill = (APTR)ReadIText(file)))
  839.             goto noMem;
  840.     }
  841.     if(NOT CheckErr())
  842.         return(NULL);
  843.     return(item);
  844.  
  845. noMem:
  846.     enable_window();
  847.     ok();
  848.     Error("Out of memory !");
  849.     return(NULL);
  850. }
  851.  
  852. /*
  853.  * Read the menus.
  854.  */
  855. long ReadMenus( BPTR file )
  856. {
  857.     register struct MyMenu      *menu, *menu1;
  858.     register struct MyMenuItem  *item, *item1, *sub, *sub1;
  859.     BOOL                         gotstrip = FALSE, gotitem = FALSE, gotsub = FALSE;
  860.     ULONG                        menus;
  861.  
  862.     Read(file,(char *)&menus,4L);
  863.  
  864.     if(menus) {
  865.         while(1) {
  866.             if(NOT(menu = (struct MyMenu *)Alloc(&Memory,(long)sizeof(struct MyMenu))))
  867.                 goto noMem;
  868.             Read(file,(char *)menu,(long)sizeof(struct MyMenu));
  869.             menu->MenuName = &menu->TheMenuName[0];
  870.             if(NOT(menu->ItsListItem = CreateListItem(menu,MENUED,FALSE,NULL,FALSE)))
  871.                 goto noMem;
  872.             ParentMenu = menu;
  873.             MenuTotal++;
  874.             MenuCount++;
  875.             if(NOT gotstrip) {
  876.                 MenuStrip = menu;
  877.                 gotstrip  = TRUE;
  878.             } else
  879.                 menu1->NextMenu = menu;
  880.  
  881.             if(menu->FirstItem) {
  882.                 while(1) {
  883.                     if(NOT(item = ReadItem(file)))
  884.                         return(FALSE);
  885.                     if(NOT(item->ItsListItem = CreateListItem(item,ITEMED,FALSE,NULL,FALSE)))
  886.                         goto noMem;
  887.                     ParentItem = item;
  888.                     ItemTotal++;
  889.                     ItemCount++;
  890.                     if(NOT gotitem) {
  891.                         menu->FirstItem = item;
  892.                         gotitem         = TRUE;
  893.                     } else
  894.                         item1->NextItem = item;
  895.  
  896.                     if(item->SubItem) {
  897.                         while(1) {
  898.                             if(NOT(sub = ReadItem(file)))
  899.                                 return(FALSE);
  900.                             if(NOT(sub->ItsListItem = CreateListItem(sub,SUBED,FALSE,NULL,FALSE)))
  901.                                 goto noMem;
  902.                             SubTotal++;
  903.                             SubCount++;
  904.                             if(NOT gotsub) {
  905.                                 item->SubItem  = sub;
  906.                                 gotsub         = TRUE;
  907.                             } else
  908.                                 sub1->NextItem = sub;
  909.  
  910.                             if(NOT sub->NextItem) {
  911.                                 gotsub   = FALSE;
  912.                                 SubsList = NULL;
  913.                                 break;
  914.                             }
  915.                             sub1 = sub;
  916.                         }
  917.                     }
  918.                     if(NOT item->NextItem) {
  919.                         gotitem   = FALSE;
  920.                         ItemsList = NULL;
  921.                         break;
  922.                     }
  923.                     item1 = item;
  924.                 }
  925.             } else
  926.                 menu->FirstItem = &dummy;
  927.  
  928.             if(NOT menu->NextMenu) break;
  929.             menu1 = menu;
  930.         }
  931.         if(NOT CheckErr())
  932.             return(FALSE);
  933.     }
  934.     ReformatList(MENUED);
  935.     ReformatList(ITEMED);
  936.     ReformatList(SUBED);
  937.     return(TRUE);
  938.  
  939. noMem:
  940.     enable_window();
  941.     ok();
  942.     Error("Out of memory !");
  943.     return(FALSE);
  944. }
  945.  
  946. /*
  947.  * Read the binary file.
  948.  */
  949. void ReadBinGadgets( long fsu )
  950. {
  951.     register struct MyGadget    *g;
  952.     struct ViewPort             *vp;
  953.     struct BinHeader             head;
  954.     BPTR                         file;
  955.     ULONG                        rc;
  956.     UWORD                        i;
  957.  
  958.     if(NOT fsu) {
  959.         if(Saved == FALSE) {
  960.             if(Ask("Current work isn't saved !","Load a file anyway ?") == FALSE)
  961.                 return;
  962.         }
  963.         strcpy((char *)IODir->fr_HeadLine,"Load Binary");
  964.  
  965.         IODir->fr_Screen = MainScreen;
  966.         IODir->fr_Caller = MainWindow;
  967.  
  968.         IODir->fr_Flags |= FR_NoInfo;
  969.  
  970.         rc = FileRequest(IODir);
  971.  
  972.         if(rc == FREQ_CANCELED)
  973.             return;
  974.         else if(rc) {
  975.             Error("FileRequester won't open !");
  976.             return;
  977.         }
  978.  
  979.         strcpy((char *)&name[0],(char *)IODir->fr_DirName);
  980.         strcat((char *)&name[0],(char *)IODir->fr_FileName);
  981.     }
  982.  
  983.     if(NOT(file = Open((char *)&name[0],MODE_OLDFILE))) {
  984.         CheckName();
  985.         if(NOT(file = Open((char *)&name[0],MODE_OLDFILE))) {
  986.             if(fsu) {
  987.                 WBSCREEN = TRUE;
  988.                 change_depth(2);
  989.             }
  990.             Error("Can't open read file !");
  991.             return;
  992.         }
  993.     }
  994.  
  995.     /* read the file header */
  996.     Read(file,(char *)&head,(long)sizeof(struct BinHeader));
  997.  
  998.     if(head.FileType == TYPE AND head.Version > GE_VERSION) {
  999.         if(fsu) {
  1000.             WBSCREEN = TRUE;
  1001.             change_depth(2);
  1002.         }
  1003.         Close(file);
  1004.         Error("Unknown PowerSource Binary !");
  1005.         return;
  1006.     }
  1007.  
  1008.     if(head.FileType != TYPE) {
  1009.         if(fsu) {
  1010.             WBSCREEN = TRUE;
  1011.             change_depth(2);
  1012.         }
  1013.         Close(file);
  1014.         Error("Not a PowerSource Binary !");
  1015.         return;
  1016.     }
  1017.  
  1018.     FreeGList();
  1019.     free_all();
  1020.     FreeMenuStrip();
  1021.  
  1022.     GadgetCount = id = 0;
  1023.  
  1024.     if(TestBits(head.FlagsBits,BH_REQUESTER))
  1025.         REQUESTER = TRUE;
  1026.     else
  1027.         REQUESTER = FALSE;
  1028.     if(TestBits(head.FlagsBits,BH_WORKBENCH))
  1029.         WBSCREEN  = TRUE;
  1030.     else
  1031.         WBSCREEN  = FALSE;
  1032.  
  1033.     DEPTH           =   head.ScrDepth;
  1034.     FrontPen        =   head.FPen;
  1035.     BackPen         =   head.BPen;
  1036.     BackFill        =   head.BackFill;
  1037.     WDBackFill      =   head.WDBackFill;
  1038.     LightSide       =   head.LightSide;
  1039.     DarkSide        =   head.DarkSide;
  1040.  
  1041.     /* read the preferences */
  1042.     Read(file,(char *)&prefs,(long)sizeof(struct ge_prefs));
  1043.  
  1044.     /* read the window specifics */
  1045.     Read(file,(char *)&nw_main,(long)sizeof(struct NewWindow));
  1046.  
  1047.     WindowFlags = nw_main.Flags;
  1048.     IDCMPFlags  = nw_main.IDCMPFlags;
  1049.  
  1050.     if(REQUESTER)
  1051.         nw_main.Flags   = FLAGS_RQ;
  1052.     else {
  1053.         nw_main.Flags = NULL;
  1054.         if(TestBits(WindowFlags,WINDOWCLOSE))   nw_main.Flags |= WINDOWCLOSE;
  1055.         if(TestBits(WindowFlags,WINDOWDEPTH))   nw_main.Flags |= WINDOWDEPTH;
  1056.         if(TestBits(WindowFlags,WINDOWDRAG))    nw_main.Flags |= WINDOWDRAG;
  1057.         if(TestBits(WindowFlags,WINDOWSIZING))  nw_main.Flags |= WINDOWSIZING;
  1058.         if(TestBits(WindowFlags,SIZEBRIGHT))    nw_main.Flags |= SIZEBRIGHT;
  1059.         if(TestBits(WindowFlags,SIZEBBOTTOM))   nw_main.Flags |= SIZEBBOTTOM;
  1060.         nw_main.Flags |= SMART_REFRESH|NOCAREREFRESH|ACTIVATE;
  1061.     }
  1062.     nw_main.IDCMPFlags = IDCMP_NM;
  1063.  
  1064.     /* read title + label */
  1065.     Read(file,(char *)&wdt[0],80L);
  1066.     Read(file,(char *)&wlb[0],MAXLABEL);
  1067.  
  1068.     /* open main screen and window */
  1069.     change_depth(DEPTH);
  1070.     disable_window();
  1071.     buisy();
  1072.     SetWindowTitles(MainWindow,(char *)-1L,"Reading Binary......");
  1073.  
  1074.     vp = &MainScreen->ViewPort;
  1075.     if(NOT WBSCREEN) LoadRGB4(vp,(void *)&head.Colors[0],(long)(1 << DEPTH));
  1076.     else             LoadRGB4(vp,(void *)&WBColors[0],4L);
  1077.  
  1078.     if(NOT CheckErr())
  1079.         goto readError;
  1080.  
  1081.     /* read the source labels */
  1082.     if(NOT ReadLabels(file))
  1083.         goto readError;
  1084.  
  1085.     /* read the gadget borders */
  1086.     if(NOT ReadSBord(file))
  1087.         goto readError;
  1088.  
  1089.     /* read the image bank */
  1090.     if(NOT ReadBank(file))
  1091.         goto readError;
  1092.  
  1093.     /* read the window/requester texts */
  1094.     if(NOT ReadWRTexts(file))
  1095.         goto readError;
  1096.  
  1097.     /* read the gadgets */
  1098.     Read(file,(char *)&rc,4L);
  1099.     GadgetCount = rc;
  1100.  
  1101.     if(GadgetCount) {
  1102.         for(i = 0; i < GadgetCount; i++) {
  1103.             if(NOT ReadGadget(file))
  1104.                 goto readError;
  1105.         }
  1106.     }
  1107.  
  1108.     /* read the menus */
  1109.     if(NOT ReadMenus(file))
  1110.         goto readError;
  1111.  
  1112.     if(NOT CheckErr())
  1113.         goto readError;
  1114.  
  1115.     Close(file);
  1116.     add_no();
  1117.     bo_added = FALSE;
  1118.     add_bo();
  1119.     bo_added = TRUE;
  1120.     Saved = TRUE;
  1121.     renumber();
  1122.     enable_window();
  1123.     ok();
  1124.     refresh();
  1125.     return;
  1126.  
  1127. readError:
  1128.     Close(file);
  1129.     FreeGList();
  1130.     free_all();
  1131.     FreeMenuStrip();
  1132.     refresh();
  1133. }
  1134.