home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d326 / snap.lha / Snap / source / windows.c < prev   
C/C++ Source or Header  |  1990-03-05  |  11KB  |  484 lines

  1. /* Auto: make
  2. */
  3.  
  4. IMPORT struct ArpBase *ArpBase;
  5.  
  6. struct Window *ControlWindow = NULL;
  7. IMPORT struct MsgPort *Sharedport;
  8. IMPORT WORD Sharedrefs;
  9.  
  10. IMPORT LONG TitleBarHeight;
  11. IMPORT LONG ContentsFontHeight;
  12. IMPORT struct RastPort MyRP;
  13.  
  14. #define IDCMPFLAGS (MOUSEMOVE|NEWSIZE|CLOSEWINDOW|GADGETUP|ACTIVEWINDOW|INACTIVEWINDOW|MOUSEBUTTONS)
  15.  
  16. UBYTE *WindowTitle = (UBYTE *)"Snap (c) 1989 Mikael Karlsson";
  17.  
  18. struct Image DiskImage = {
  19.     0, -1,
  20.     16, 8, 1,
  21.     NULL,
  22.     0x01, 0x00,
  23.     NULL
  24. };
  25.  
  26. struct Gadget DiskGad = {
  27.     NULL,
  28.     0, 1,
  29.     16, 8,
  30.     GADGIMAGE|GADGHCOMP|GRELRIGHT,
  31.     RELVERIFY,
  32.     BOOLGADGET,
  33.     (APTR)&DiskImage,
  34.     NULL,
  35.     NULL,
  36.     0L,
  37.     NULL,
  38.     DISKGAD,
  39.     NULL,
  40. };
  41.  
  42. struct Gadget VProp = {
  43.     NULL,
  44.     -15, 10,
  45.     16, -10,
  46.     GADGHCOMP|GRELRIGHT|GRELHEIGHT,
  47.     GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY,
  48.     PROPGADGET,
  49.     NULL,
  50.     NULL,
  51.     NULL,
  52.     0L,
  53.     NULL,
  54.     VPROP,
  55.     NULL
  56. };
  57.  
  58. struct Gadget HProp = {
  59.     NULL,
  60.     1, -8,
  61.     -15, 9,
  62.     GADGHCOMP|GRELBOTTOM|GRELWIDTH,
  63.     GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY,
  64.     PROPGADGET,
  65.     NULL,
  66.     NULL,
  67.     NULL,
  68.     0L,
  69.     NULL,
  70.     HPROP,
  71.     NULL
  72. };
  73.  
  74. struct PropInfo VInfo = {
  75.     AUTOKNOB|FREEVERT,
  76.     0, 0,
  77.     MAXBODY, MAXBODY,
  78.     0, 0, 0, 0, 0, 0
  79. };
  80.  
  81. struct PropInfo HInfo = {
  82.     AUTOKNOB|FREEHORIZ,
  83.     0, 0,
  84.     MAXBODY, MAXBODY,
  85.     0, 0, 0, 0, 0, 0
  86. };
  87.  
  88. struct Image VImage = {
  89.     0, 0, 0, 0, 0, NULL, 0, 0, NULL
  90. };
  91. struct Image HImage = {
  92.     0, 0, 0, 0, 0, NULL, 0, 0, NULL
  93. };
  94.  
  95. /* Window structure for snapped gfx */
  96. struct NewWindow Nw = {
  97.     0, 1,             /* LeftEdge, TopEdge  */
  98.     0, 0,             /* Width, Height */
  99.     -1, -1,           /* DetailPen, BlockPen */
  100.     NULL,             /* IDCMPFlags */
  101.     WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|WINDOWSIZING|SMART_REFRESH|NOCAREREFRESH|RMBTRAP,
  102.     NULL, NULL,       /* FirstGadget, CheckMark */
  103.     NULL,
  104.     NULL, NULL,       /* Screen, BitMap */
  105.     32, 32,           /* MinWidth, MinHeight */
  106.     0, 0,           /* MaxWidth, MaxHeight */
  107.     WBENCHSCREEN      /* Type */
  108. };
  109.  
  110. SHORT Coords3[5][2] = {
  111.     {  0,  0},
  112.     { 74,  0},
  113.     { 74, 10},
  114.     {  0, 10},
  115.     {  0,  0}
  116. };
  117.  
  118. SHORT Coords2[5][2] = {
  119.     {  0,  0},
  120.     { 74,  0},
  121.     { 74, 10},
  122.     {  0, 10},
  123.     {  0,  0}
  124. };
  125.  
  126. SHORT Coords1[5][2] = {
  127.     {  0,  0},
  128.     { 34,  0},
  129.     { 34, 10},
  130.     {  0, 10},
  131.     {  0,  0}
  132. };
  133.  
  134. struct Border Border3 = {
  135.     42, 0,
  136.     1, 0,
  137.     JAM1,
  138.     5,
  139.     (SHORT *)&Coords3,
  140.     NULL
  141. };
  142.  
  143. struct Border Border2 = {
  144.     42, 0,
  145.     1, 0,
  146.     JAM1,
  147.     5,
  148.     (SHORT *)&Coords2,
  149.     &Border3
  150. };
  151.  
  152. struct Border Border1 = {
  153.     4, 0,
  154.     1, 0,
  155.     JAM1,
  156.     5,
  157.     (SHORT *)&Coords1,
  158.     &Border2
  159. };
  160.  
  161. UBYTE TranspBuf[5];
  162.  
  163. struct StringInfo TranspSI = {
  164.     TranspBuf,  /* Buf */
  165.     NULL,       /* UndoBuf */
  166.     0,          /* BufferPos */
  167.     4,          /* MaxChars */
  168.     0,          /* DispPos */
  169.     0,          /* UndoPos */
  170.     0,          /* NumChars */
  171.     0,          /* DispCount */
  172.     0, 0,       /* CLeft, CTop */
  173.     NULL,       /* Layer */
  174.     0L,         /* LongInt */
  175.     NULL        /* AltKeyMap */
  176. };
  177.  
  178. struct Gadget TranspGad = {
  179.     NULL,
  180.     5, 16,
  181.     32, 8,
  182.     LONGINT,
  183.     RELVERIFY,
  184.     STRGADGET,
  185.     NULL,
  186.     NULL,
  187.     NULL,
  188.     0L,
  189.     (APTR)&TranspSI,
  190.     0,
  191.     NULL,
  192. };
  193.  
  194. UBYTE SaveName[256] = "";
  195. UBYTE SaveDirName[256] = "";
  196.  
  197. struct StringInfo NameSI = {
  198.     SaveName,   /* Buf */
  199.     NULL,       /* UndoBuf */
  200.     0,          /* BufferPos */
  201.     255,        /* MaxChars */
  202.     0,          /* DispPos */
  203.     0,          /* UndoPos */
  204.     0,          /* NumChars */
  205.     0,          /* DispCount */
  206.     0, 0,       /* CLeft, CTop */
  207.     NULL,       /* Layer */
  208.     0L,         /* LongInt */
  209.     NULL        /* AltKeyMap */
  210. };
  211.  
  212. struct Gadget NameGad = {
  213.     &TranspGad,
  214.     5, 16,
  215.     32, 8,
  216.     NULL,
  217.     NULL,
  218.     NULL,
  219.     NULL,
  220.     NULL,
  221.     NULL,
  222.     0L,
  223.     NULL,
  224.     NAMEGAD,
  225.     NULL,
  226. };
  227.  
  228. struct Gadget SaveGad = {
  229.     &NameGad,
  230.     5, 16,
  231.     32, 8,
  232.     GADGHCOMP,
  233.     RELVERIFY,
  234.     BOOLGADGET,
  235.     NULL,
  236.     NULL,
  237.     NULL,
  238.     0L,
  239.     NULL,
  240.     SAVEGAD,
  241.     NULL,
  242. };
  243.  
  244. /* Window structure for control window */
  245. struct NewWindow NewCW = {
  246.   10, 10,           /* LeftEdge, TopEdge  */
  247.   320, 40,          /* Width, Height */
  248.   -1, -1,           /* DetailPen, BlockPen */
  249.   NULL,             /* IDCMPFlags */
  250.   WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|SMART_REFRESH|ACTIVATE|NOCAREREFRESH,
  251.   NULL, NULL,       /* FirstGadget, CheckMark */
  252.   (UBYTE *)"Snap Control Window",
  253.   NULL, NULL,       /* Screen, BitMap */
  254.   32, 32,           /* MinWidth, MinHeight */
  255.   -1, -1,           /* MaxWidth, MaxHeight */
  256.   WBENCHSCREEN      /* Type */
  257. };
  258.  
  259. struct FileRequester NameFR = {
  260.     "Save picture as...",
  261.     (BYTE *)SaveName,
  262.     (BYTE *)SaveDirName,
  263.     NULL,
  264.     NULL,
  265.     NULL,
  266.     NULL,
  267.     NULL
  268. };
  269.  
  270. struct Window *opensharedwindow(nw)
  271. struct NewWindow *nw;
  272. {
  273.     struct Window *win;
  274.     struct Screen scr;
  275.  
  276.     if (Sharedport) {
  277.         nw->IDCMPFlags = NULL;
  278.     } else {
  279.         nw->IDCMPFlags = IDCMPFLAGS;
  280.     }
  281.     if (!GetScreenData((char *)&scr, (LONG)sizeof(struct Screen),
  282.       WBENCHSCREEN, NULL)) {
  283.         return NULL;     /* No WB */
  284.     }
  285.     if (nw->TopEdge+nw->Height > scr.Height) {
  286.         nw->TopEdge = scr.Height-nw->Height;
  287.     }
  288.     if (nw->LeftEdge+nw->Width > scr.Width) {
  289.         nw->LeftEdge = scr.Width-nw->Width;
  290.     }
  291.     if (nw->TopEdge < 0) {
  292.         nw->TopEdge = 0;
  293.     }
  294.     if (nw->LeftEdge < 0) {
  295.         nw->LeftEdge = 0;
  296.     }
  297.     if (nw->TopEdge+nw->Height > scr.Height) {
  298.         nw->Height = scr.Height-nw->TopEdge;
  299.     }
  300.     if (nw->LeftEdge+nw->Width > scr.Width) {
  301.         nw->Width = scr.Width-nw->LeftEdge;
  302.     }
  303.  
  304.     if (win = OpenWindow(nw)) {
  305.         if (Sharedport) {
  306.             win->UserPort = Sharedport;
  307.             ModifyIDCMP(win, IDCMPFLAGS);
  308.         } else {
  309.             Sharedport = win->UserPort;
  310.         }
  311.         ++Sharedrefs;
  312.     }
  313.     return(win);
  314. }
  315.  
  316.  
  317. VOID closesharedwindow(win)
  318. struct Window *win;
  319. {
  320.     Forbid();
  321.     Sharedrefs--;
  322.     if (Sharedrefs > 0) {
  323.         REGISTER struct IntuiMessage *im, *succ;
  324.         im = (struct IntuiMessage *)win->UserPort->mp_MsgList.lh_Head;
  325.         while (succ = (struct IntuiMessage *)im->ExecMessage.mn_Node.ln_Succ) {
  326.             if (im->IDCMPWindow == win) {
  327.                 Remove(im);
  328.                 ReplyMsg(im);
  329.             }
  330.             im = succ;
  331.         }
  332.         win->UserPort = NULL;
  333.         ModifyIDCMP(win, MENUVERIFY);     /* NEVER occurs */
  334.     } else
  335.         Sharedport = NULL;
  336.     Permit();
  337.     CloseWindow(win);
  338. }
  339.  
  340. VOID SetUpBorder(Border, Gad)
  341. struct Border *Border;
  342. struct Gadget *Gad;
  343. {
  344.     Border->LeftEdge = Gad->LeftEdge-1;
  345.     Border->TopEdge = Gad->TopEdge-1;
  346.     Border->XY[2] = Border->XY[4] = Gad->Width+1;
  347.     Border->XY[5] = Border->XY[7] = Gad->Height+1;
  348. }
  349.  
  350. VOID AdjustSize(GS)
  351. struct GfxSnap *GS;
  352. {
  353.     ULONG w = GS->window->Width - 18;
  354.     ULONG h = GS->window->Height - GS->VProp.TopEdge - 9;
  355.     ULONG vb, hb;
  356.  
  357.     vb = hb = MAXBODY;
  358.  
  359.     if (w < GS->width) {
  360.         hb = w * (ULONG)MAXBODY / GS->width;
  361.     }
  362.     if (h < GS->height) {
  363.         vb = h * (ULONG)MAXBODY / GS->height;
  364.     }
  365.     NewModifyProp(&GS->VProp, GS->window, NULL, (ULONG)AUTOKNOB|FREEVERT,
  366.       0L, 0L, (ULONG)MAXBODY, vb, 1L);
  367.     NewModifyProp(&GS->HProp, GS->window, NULL, (ULONG)AUTOKNOB|FREEHORIZ,
  368.       0L, 0L, hb, (ULONG)MAXBODY, 1L);
  369.     InitRastPort(&MyRP);
  370.     MyRP.BitMap = &GS->BM;
  371.     ClipBlit(&MyRP, 0L, 0L, GS->window->RPort,
  372.       2L, (LONG)GS->VProp.TopEdge, w, h, 0xC0L);
  373. }
  374.  
  375. VOID SyncGS(GS)
  376. struct GfxSnap *GS;
  377. {
  378.     ULONG w = GS->window->Width - 18;
  379.     ULONG h = GS->window->Height - GS->VProp.TopEdge - 9;
  380.     ULONG vpos, hpos;
  381.     LONG temp;
  382.  
  383.     temp = GS->width - w;
  384.     if (temp <= 0) {
  385.         hpos = 0;
  386.     } else {
  387.         hpos = temp * (LONG)GS->HInfo.HorizPot / MAXBODY;
  388.         if (hpos > temp) {
  389.             hpos = temp;
  390.         }
  391.     }
  392.     temp = GS->height - h;
  393.     if (temp <= 0) {
  394.         vpos = 0;
  395.     } else {
  396.         vpos = temp * (LONG)GS->VInfo.VertPot / MAXBODY;
  397.         if (vpos > temp) {
  398.             vpos = temp;
  399.         }
  400.     }
  401.     InitRastPort(&MyRP);
  402.     MyRP.BitMap = &GS->BM;
  403.     ClipBlit(&MyRP, hpos, vpos, GS->window->RPort,
  404.       2L, (LONG)GS->VProp.TopEdge, w, h, 0xC0L);
  405. }
  406.  
  407. SHORT OpenCW()
  408. {
  409.     WORD temp;
  410.  
  411.     FixHeights();
  412.  
  413.     NewCW.Height = TitleBarHeight + ContentsFontHeight * 2 + 12;
  414.     ControlWindow = opensharedwindow(&NewCW);
  415.     if (!ControlWindow) {
  416.         return 0;
  417.     }
  418.     SaveGad.TopEdge = NameGad.TopEdge = TitleBarHeight + 2;
  419.     SaveGad.Height = NameGad.Height = TranspGad.Height = ContentsFontHeight+2;
  420.     TranspGad.TopEdge = SaveGad.TopEdge + SaveGad.Height + 3;
  421.  
  422.     SaveGad.LeftEdge = 4L;
  423.     SaveGad.Width = TextLength(ControlWindow->RPort, "Save", 4L) + 1;
  424.     if (Sharedrefs == 1) {       /* No snap window, just control window? */
  425.         SaveGad.Flags |= GADGDISABLED;
  426.     } else {
  427.         SaveGad.Flags &= ~GADGDISABLED;
  428.     }
  429.  
  430.     NameGad.LeftEdge = SaveGad.LeftEdge +
  431.       TextLength(ControlWindow->RPort, "Save as ", 8L);
  432.     temp = ControlWindow->Width - 4 - NameGad.LeftEdge;
  433.     NameGad.Width =
  434.       temp - (temp % TextLength(ControlWindow->RPort, " ", 1L)) + 1;
  435.  
  436.     TranspGad.LeftEdge = SaveGad.LeftEdge +
  437.       TextLength(ControlWindow->RPort, "Transparent color ", 18L);
  438.     TranspGad.Width = TextLength(ControlWindow->RPort, "0000", 4L);
  439.  
  440.     SetUpBorder(&Border1, &SaveGad);
  441.     SetUpBorder(&Border2, &NameGad);
  442.     SetUpBorder(&Border3, &TranspGad);
  443.  
  444.     ++TranspGad.LeftEdge;
  445.     ++TranspGad.TopEdge;
  446.  
  447.     if (ArpBase) {
  448.         NameGad.Flags = GADGHCOMP;
  449.         NameGad.Activation = RELVERIFY;
  450.         NameGad.GadgetType = BOOLGADGET;
  451.         NameGad.SpecialInfo = NULL;
  452.     } else {
  453.         NameGad.Flags = SELECTED;
  454.         NameGad.Activation = NULL;
  455.         NameGad.GadgetType = STRGADGET;
  456.         NameGad.SpecialInfo = (APTR)&NameSI;
  457.         ++NameGad.LeftEdge;         /* Adjust string gadget position */
  458.         ++NameGad.TopEdge;          /* Adjust string gadget position */
  459.     }
  460.  
  461.     strcpy(SaveName, "Snap.pic");
  462.     strcpy(TranspBuf, "0");
  463.     TranspSI.LongInt = 0L;
  464.  
  465.     SetAPen(ControlWindow->RPort, 1L);
  466.     SetBPen(ControlWindow->RPort, 0L);
  467.     SetDrMd(ControlWindow->RPort, JAM2);
  468.     Move(ControlWindow->RPort,
  469.       5L, (LONG)SaveGad.TopEdge + 1 + ControlWindow->RPort->Font->tf_Baseline);
  470.     Text(ControlWindow->RPort, "Save as", 7L);
  471.     Move(ControlWindow->RPort,
  472.       5L, (LONG)TranspGad.TopEdge + ControlWindow->RPort->Font->tf_Baseline);
  473.     Text(ControlWindow->RPort, "Transparent color", 17L);
  474.     DrawBorder(ControlWindow->RPort, &Border1, 0L, 0L);
  475.     AddGList(ControlWindow, &SaveGad, -1L, 3L, NULL);
  476.     RefreshGList(&SaveGad, ControlWindow, NULL, 3L);
  477.     if (ArpBase) {
  478.         GadText(&NameGad, SaveName, 8L);
  479.     } else {
  480.         ActivateGadget(&NameGad, ControlWindow, NULL);
  481.     }
  482.     return 1;
  483. }
  484.