home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 514b.lha / GadgetED_v2.3 / Source / func.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  14KB  |  568 lines

  1. /*----------------------------------------------------------------------*
  2.    func.c Version 2.3 - © Copyright 1990-91 Jaba Development
  3.  
  4.    Author  : Jan van den Baard
  5.    Purpose : Special subroutines used by the program
  6.  *----------------------------------------------------------------------*/
  7.  
  8. extern struct GadgetList Gadgets;
  9. extern struct Gadget     *Gadget, TextGadget;
  10. extern struct Window     *MainWindow;
  11. extern struct Screen     *MainScreen;
  12. extern struct MemoryChain Memory;
  13. extern ULONG             Class, WindowFlags;
  14. extern USHORT            FrontPen, GadgetCount, id, BackFill;
  15. extern USHORT            LightSide, DarkSide;
  16. extern BOOL              Saved;
  17. extern BOOL              REQUESTER;
  18.  
  19. SHORT  LB,TB;
  20.  
  21. /*
  22.  * gadgets, borders and texts for the 'Error' requester
  23.  */
  24. static SHORT MainPairs1[] =
  25.  { 2,1,307,1,307,37,2,37,2,1
  26.  };
  27. static struct Border MainBorder1 =
  28.  { 0,0,0,0,JAM1,5,MainPairs1,NULL
  29.  };
  30. static SHORT MainPairs[] =
  31.  { 2,0,307,0
  32.  };
  33. static struct Border MainBorder =
  34.  { 0,12,0,0,JAM1,2,MainPairs,&MainBorder1
  35.  };
  36. static SHORT CPairs[] =
  37.  { 0,0,111,0,111,10,0,10,0,0
  38.  };
  39. static struct Border CBorder =
  40.  { -1,-1,0,0,JAM1,5,CPairs,NULL
  41.  };
  42. static struct IntuiText CText =
  43.  { 0,0,JAM1,23,1,NULL,(UBYTE *)"CONTINUE",NULL
  44.  };
  45. static struct Gadget C =
  46.  { NULL,101,25,110,9,NULL,RELVERIFY,BOOLGADGET,
  47.    (APTR)&CBorder,NULL,&CText,NULL,NULL,NULL,NULL
  48.  };
  49. static struct IntuiText TText =
  50.  { 0,0,JAM1,63,3,NULL,(UBYTE *)"GadgetEd System Message",NULL
  51.  };
  52. static struct NewWindow err_req =
  53.  { 5,15,310,39,0,1,GADGETUP,NOCAREREFRESH+SMART_REFRESH+ACTIVATE+RMBTRAP,
  54.    NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  55.  };
  56.  
  57. /*
  58.  * gadgets, borders and texts for the 'Ask' Requester
  59.  */
  60. static SHORT MPairs2[] =
  61.  { 0,0,295,0,295,53,0,53,0,0
  62.  };
  63. static struct Border MBorder2 =
  64.  { 2,1,0,0,JAM1,5,MPairs2,NULL
  65.  };
  66. static SHORT MPairs1[] =
  67.  { 0,0,295,0
  68.  };
  69. static struct Border MBorder1 =
  70.  { 2,12,0,0,JAM1,2,MPairs1,&MBorder2
  71.  };
  72. static SHORT YNPairs[] =
  73.  { 0,0,126,0,126,12,0,12,0,0
  74.  };
  75. static struct Border YNBorder =
  76.  { -1,-1,0,0,JAM1,5,YNPairs,NULL
  77.  };
  78. static struct IntuiText YText =
  79.  { 0,0,JAM1,48,2,NULL,(UBYTE *)"YES",NULL
  80.  };
  81. static struct Gadget Y =
  82.  { NULL,11,38,125,11,NULL,RELVERIFY,BOOLGADGET,
  83.    (APTR)&YNBorder,NULL,&YText,NULL,NULL,0,NULL
  84.  };
  85. static struct IntuiText NText =
  86.  { 0,0,JAM1,55,2,NULL,(UBYTE *)"NO",NULL
  87.  };
  88. static struct Gadget N =
  89.  { &Y,162,38,125,11,NULL,RELVERIFY,BOOLGADGET,
  90.    (APTR)&YNBorder,NULL,&NText,NULL,NULL,1,NULL
  91.  };
  92. static struct IntuiText TTText =
  93.  { 0,0,JAM1,100,3,NULL,(UBYTE *)"Pleasy Verify",NULL
  94.  };
  95. static struct NewWindow ask_req =
  96.  { 10,15,300,56,0,1,GADGETUP,NOCAREREFRESH+SMART_REFRESH+ACTIVATE+RMBTRAP,
  97.    NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  98.  };
  99.  
  100. /*
  101.  * gadgets, borders and texts for the 'About' Requester
  102.  */
  103. static SHORT ABPairs2[] =
  104.  { 0,0,295,0
  105.  };
  106. static struct Border ABBorder2 =
  107.  { 2,12,0,0,JAM1,2,ABPairs2,NULL
  108.  };
  109. static SHORT ABPairs1[] =
  110.  { 0,0,295,0,295,67,0,67,0,0
  111.  };
  112. static struct Border ABBorder1 =
  113.  { 2,1,0,0,JAM1,5,ABPairs1,&ABBorder2
  114.  };
  115. static struct IntuiText ABText4 =
  116.  { 0,0,JAM1,78,38,NULL,(UBYTE *)"Jan van den Baard",NULL
  117.  };
  118. static struct IntuiText ABText3 =
  119.  { 0,0,JAM1,40,27,NULL,(UBYTE *)"written in Aztec C V5.0a by",&ABText4
  120.  };
  121. static struct IntuiText ABText2 =
  122.  { 0,0,JAM1,9,17,NULL,(UBYTE *)"(c) Copyright 1991 Jaba Development",&ABText3
  123.  };
  124. static struct IntuiText ABText1 =
  125.  { 0,0,JAM1,54,3,NULL,(UBYTE *)"- GadgetEd Version 2.3 -",&ABText2
  126.  };
  127. static SHORT CONTPairs[] =
  128.  { 0,0,157,0,157,16,0,16,0,0
  129.  };
  130. static struct Border CONTBorder =
  131.  { -1,-1,0,0,JAM1,5,CONTPairs,NULL
  132.  };
  133. static struct IntuiText CONTText =
  134.  { 0,0,JAM1,48,4,NULL,(UBYTE *)"CONTINUE",NULL
  135.  };
  136. static struct Gadget CONT =
  137.  { NULL,68,49,156,15,NULL,RELVERIFY,BOOLGADGET,
  138.    (APTR)&CONTBorder,NULL,&CONTText,NULL,NULL,NULL,NULL
  139.  };
  140. static struct NewWindow ab_req =
  141.  { 10,15,300,70,0,1,GADGETUP,SMART_REFRESH+NOCAREREFRESH+ACTIVATE+RMBTRAP,
  142.    NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  143.  };
  144.  
  145. /*
  146.  * draw the graphics of the window
  147.  */
  148. VOID draw(window,gadget,border,itext)
  149.     struct Window    *window;
  150.     struct Gadget    *gadget;
  151.     struct Border    *border;
  152.     struct IntuiText *itext;
  153. {
  154.     struct RastPort *rp;
  155.     register struct Gadget   *g, *g1;
  156.     register USHORT x,y,x1,y1;
  157.  
  158.     rp = window->RPort;
  159.     SetDrMd(rp,JAM1);
  160.     SetAPen(rp,1);
  161.     RectFill(rp,0,0,window->Width,window->Height);
  162.  
  163.     g = gadget;
  164.     while(1)
  165.     {   if(TestBits((ULONG)g->GadgetType,STRGADGET))
  166.         {   x = g->LeftEdge; y = g->TopEdge;
  167.             x1 = x + g->Width; y1 = y + g->Height;
  168.             RectFill(rp,x,y,x1,y1);
  169.         }
  170.         g1 = g->NextGadget;
  171.         if((TestBits((ULONG)g->GadgetType,BOOLGADGET)) AND
  172.            (NOT TestBits((ULONG)g->GadgetType,GADGET0002)))
  173.                ShadowGadget(window,g,NULL,0);
  174.         if(NOT g1) break;
  175.         g = g1;
  176.     }
  177.     PrintIText(rp,itext,0,0);
  178.     DrawBorder(rp,border,0,0);
  179.     AddGList(window,gadget,-1L,-1L,NULL);
  180.     RefreshGList(gadget,window,NULL,-1L);
  181. }
  182.  
  183. /*
  184.  * put up the error window
  185.  */
  186. VOID Error(message)
  187.     UBYTE         *message;
  188. {
  189.     struct Window *erw;
  190.  
  191.     err_req.Screen = MainScreen;
  192.     if(NOT(erw = OpenWindow(&err_req))) return;
  193.     draw(erw,&C,&MainBorder,&TText);
  194.     ok();
  195.     SetDrMd(erw->RPort,JAM1);
  196.     SetAPen(erw->RPort,2);
  197.     Move(erw->RPort,155-(strlen((char *)message) << 2),21);
  198.     Text(erw->RPort,(char *)message,strlen((char *)message));
  199.     disable_window();
  200.     do
  201.     {   Wait(1 << erw->UserPort->mp_SigBit);
  202.         while(read_msg(erw));
  203.     } while(Class != GADGETUP);
  204.     CloseWindow(erw);
  205.     enable_window();
  206. }
  207.  
  208. /*
  209.  * add a border to gadget 'g'
  210.  */
  211. BOOL add_border(gad)
  212.     struct MyGadget *gad;
  213. {
  214.     register SHORT *XY;
  215.     SHORT           x,y,w,h,xo=0,yo=0;
  216.     struct Border  *border,*border1;
  217.     struct Gadget  *g;
  218.  
  219.     g = &gad->Gadget;
  220.  
  221.     if(NOT TestBits((ULONG)gad->SpecialFlags,OS20BORDER))
  222.     {
  223.         x = g->LeftEdge - 1;
  224.         y = g->TopEdge  - 1;
  225.         w = g->Width;
  226.         h = g->Height;
  227.  
  228.         if(NOT(border = (struct Border *)Alloc(&Memory,sizeof(struct Border))))
  229.             return(FALSE);
  230.         if(NOT(XY = (SHORT *)Alloc(&Memory,(ULONG)20)))
  231.             return(FALSE);
  232.  
  233.         XY[0] = XY[1] = XY[3] = XY[6] = XY[8] = XY[9] = -1;
  234.         XY[2] = XY[4] = w;
  235.         XY[5] = XY[7] = h;
  236.  
  237.         border->FrontPen     = FrontPen;
  238.         border->DrawMode     = JAM1;
  239.         border->Count        = 5;
  240.         border->XY           = XY;
  241.  
  242.         g->GadgetRender = (APTR)border;
  243.         return(TRUE);
  244.     }
  245.     else
  246.     {
  247.         x = g->LeftEdge;
  248.         y = g->TopEdge;
  249.         w = g->Width-1;
  250.         h = g->Height-1;
  251.  
  252.         if(TestBits((ULONG)g->GadgetType,STRGADGET))
  253.         {   x--; y--; w+=2; h++; xo=-2; yo=-1; }
  254.  
  255.         if(NOT(border = (struct Border *)Alloc(&Memory,sizeof(struct Border))))
  256.             return(FALSE);
  257.         if(NOT(XY = (SHORT *)Alloc(&Memory,(ULONG)20)))
  258.             return(FALSE);
  259.  
  260.         XY[0] = XY[2] = xo;
  261.         XY[4] = XY[6] = xo+1;
  262.         XY[1] = XY[7] = XY[9] = yo;
  263.         XY[3] = h;
  264.         XY[5] = h;
  265.         XY[8] = w-1;
  266.  
  267.         border->FrontPen     = LightSide;
  268.         border->DrawMode     = JAM1;
  269.         border->Count        = 5;
  270.         border->XY           = XY;
  271.  
  272.         g->GadgetRender = (APTR)border;
  273.  
  274.         if(NOT(border1 = (struct Border *)Alloc(&Memory,sizeof(struct Border))))
  275.             return(FALSE);
  276.         if(NOT(XY = (SHORT *)Alloc(&Memory,(ULONG)20)))
  277.             return(FALSE);
  278.  
  279.         XY[1] = XY[3] = XY[9] = h;
  280.         XY[2] = XY[4] = w-1;
  281.         XY[6] = XY[8] = w;
  282.         XY[0] = xo+1;
  283.         XY[5] = yo+1;
  284.         XY[7] = yo;
  285.  
  286.         border1->FrontPen     = DarkSide;
  287.         border1->DrawMode     = JAM1;
  288.         border1->Count        = 5;
  289.         border1->XY           = XY;
  290.  
  291.         border->NextBorder    = border1;
  292.         return(TRUE);
  293.     }
  294. }
  295.  
  296. /*
  297.  * add a gadget to the edit window and to the list
  298.  */
  299. VOID add_gadget(w,x,y,x1,y1)
  300.     struct Window *w;
  301.     SHORT          x,y,x1,y1;
  302. {
  303.     SHORT tmp;
  304.     struct MyGadget *gadget;
  305.  
  306.     Saved = FALSE;
  307.  
  308.     if(x > x1) { tmp = x; x = x1; x1 = tmp; }
  309.     if(y > y1) { tmp = y; y = y1; y1 = tmp; }
  310.  
  311.     if(((x1 - x) < 9) OR ((y1 - y) < 9))
  312.     {   draw_box(w,x,y,x1,y1);
  313.         Error("Gadget to small !");
  314.         return;
  315.     }
  316.  
  317.     if(NOT(gadget = (struct MyGadget *)Alloc(&Memory,sizeof(struct MyGadget))))
  318.     {   Error("Out of memory !");
  319.         return;
  320.     }
  321.  
  322.     AddHead((void *)&Gadgets,(void *)gadget);
  323.  
  324.     gadget->Gadget.GadgetType      = BOOLGADGET;
  325.     gadget->Gadget.Flags           = GADGHCOMP;
  326.     gadget->Gadget.Activation      = RELVERIFY;
  327.  
  328.     gadget->Gadget.LeftEdge        = x + 1;
  329.     gadget->Gadget.TopEdge         = y + 1;
  330.     gadget->Gadget.Width           = (x1 - x) - 1;
  331.     gadget->Gadget.Height          = (y1 - y) - 1;
  332.  
  333.     gadget->Gadget.GadgetID        = GadgetCount++;
  334.  
  335.     if(NOT(add_border(gadget)))
  336.     {   RemHead((void *)&Gadgets);
  337.         FreeGadget(gadget);
  338.         Error("Out of memory !");
  339.         return;
  340.     }
  341.  
  342.     AddGList(w,&gadget->Gadget,-1L,1,NULL);
  343.     RefreshGList(&gadget->Gadget,w,NULL,1);
  344.     Format((char *)&gadget->GadgetLabel,"Gadget%ld",id++);
  345. }
  346.  
  347. /*
  348.  * set the absolute dimensions of a 'GREL' gadget
  349.  */
  350. VOID un_grel(wi,g)
  351.     struct Window *wi;
  352.     struct Gadget *g;
  353. {
  354.     SHORT l,t,w,h,ww,wh;
  355.  
  356.     l = g->LeftEdge - 1;
  357.     t = g->TopEdge  - 1;
  358.     w = g->Width    - 1;
  359.     h = g->Height   - 1;
  360.  
  361.     if(TestBits((ULONG)wi->Flags,GIMMEZEROZERO))
  362.     {   ww = wi->GZZWidth;
  363.         wh = wi->GZZHeight;
  364.     }
  365.     else
  366.     {   ww = wi->Width;
  367.         wh = wi->Height;
  368.     }
  369.  
  370.     if(TestBits((ULONG)g->Flags,GRELBOTTOM))
  371.         g->TopEdge  = t + wh;
  372.     if(TestBits((ULONG)g->Flags,GRELRIGHT))
  373.         g->LeftEdge = l + ww;
  374.     if(TestBits((ULONG)g->Flags,GRELWIDTH))
  375.         g->Width    = (w + ww) + 1;
  376.     if(TestBits((ULONG)g->Flags,GRELHEIGHT))
  377.         g->Height   = (h + wh) + 1;
  378. }
  379.  
  380. /*
  381.  * set the relative dimensions of a 'GREL' gadget
  382.  */
  383. VOID grel(wi,g)
  384.     struct Window *wi;
  385.     struct Gadget *g;
  386. {
  387.     SHORT l,t,w,h,ww,wh;
  388.  
  389.     l = g->LeftEdge + 1;
  390.     t = g->TopEdge  + 1;
  391.     w = g->Width    - 1;
  392.     h = g->Height   - 1;
  393.  
  394.     if(TestBits((ULONG)wi->Flags,GIMMEZEROZERO))
  395.     {   ww = wi->GZZWidth;
  396.         wh = wi->GZZHeight;
  397.     }
  398.     else
  399.     {   ww = wi->Width;
  400.         wh = wi->Height;
  401.     }
  402.  
  403.     if(TestBits((ULONG)g->Flags,GRELBOTTOM))
  404.         g->TopEdge  = t - wh;
  405.     if(TestBits((ULONG)g->Flags,GRELRIGHT))
  406.         g->LeftEdge = l - ww;
  407.     if(TestBits((ULONG)g->Flags,GRELWIDTH))
  408.         g->Width    = (w - ww) + 1;
  409.     if(TestBits((ULONG)g->Flags,GRELHEIGHT))
  410.         g->Height   = (h - wh) + 1;
  411. }
  412.  
  413. un_gzz()
  414. {
  415.     struct MyGadget  *gad;
  416.     struct Gadget    *g;
  417.     struct IntuiText *t;
  418.     ULONG             act;
  419.     SHORT             lb,tb;
  420.  
  421.     lb = MainWindow->BorderLeft;
  422.     tb = MainWindow->BorderTop;
  423.  
  424.     if(REQUESTER) return;
  425.  
  426.     if(TestBits(WindowFlags,GIMMEZEROZERO))
  427.     {
  428.         for(gad = Gadgets.Head; gad->Succ; gad = gad->Succ)
  429.         {
  430.             g   = &gad->Gadget;
  431.             act = (ULONG)g->Activation;
  432.  
  433.             un_grel(MainWindow,g);
  434.  
  435.             if(TestBits((ULONG)gad->SpecialFlags,GZZGADGET))
  436.             {
  437.                 if(TestBits(act,LEFTBORDER))
  438.                 {
  439.                     if((g->LeftEdge + g->Width) > lb)
  440.                         lb = g->LeftEdge + g->Width;
  441.                 }
  442.                 else if(TestBits(act,TOPBORDER))
  443.                 {
  444.                     if((g->TopEdge + g->Height) > tb)
  445.                         tb = g->TopEdge + g->Height;
  446.                 }
  447.             }
  448.             grel(MainWindow,g);
  449.         }
  450.         LB = lb; TB = tb;
  451.         for(gad = Gadgets.Head; gad->Succ; gad = gad->Succ)
  452.         {
  453.             g   = &gad->Gadget;
  454.  
  455.             un_grel(MainWindow,g);
  456.  
  457.             if(NOT TestBits((ULONG)gad->SpecialFlags,GZZGADGET))
  458.             {
  459.                 g->LeftEdge -= lb;
  460.                 g->TopEdge  -= tb;
  461.             }
  462.             grel(MainWindow,g);
  463.         }
  464.  
  465.         if((t = TextGadget.GadgetText))
  466.         {
  467.             while(1)
  468.             {
  469.                 t->LeftEdge -= lb;
  470.                 t->TopEdge  -= tb;
  471.                 if(NOT(t = t->NextText)) break;
  472.             }
  473.         }
  474.     }
  475. }
  476.  
  477. do_gzz()
  478. {
  479.     struct MyGadget  *gad;
  480.     struct Gadget    *g;
  481.     struct IntuiText *t;
  482.  
  483.     if(REQUESTER) return;
  484.  
  485.     if(TestBits(WindowFlags,GIMMEZEROZERO))
  486.     {
  487.         for(gad = Gadgets.Head; gad->Succ; gad = gad->Succ)
  488.         {
  489.             g   = &gad->Gadget;
  490.  
  491.             un_grel(MainWindow,g);
  492.  
  493.             if(NOT TestBits((ULONG)gad->SpecialFlags,GZZGADGET))
  494.             {
  495.                 g->LeftEdge += LB;
  496.                 g->TopEdge  += TB;
  497.             }
  498.             grel(MainWindow,g);
  499.         }
  500.  
  501.         if((t = TextGadget.GadgetText))
  502.         {
  503.             while(1)
  504.             {
  505.                 t->LeftEdge += LB;
  506.                 t->TopEdge  += TB;
  507.                 if(NOT(t = t->NextText)) break;
  508.             }
  509.         }
  510.     }
  511. }
  512.  
  513. /*
  514.  * put up the ask window
  515.  */
  516. LONG Ask(text,text1)
  517.     UBYTE         *text, *text1;
  518. {
  519.     USHORT y,id;
  520.     BOOL   ret;
  521.     struct Window *aw;
  522.     struct RastPort *rp;
  523.  
  524.     if(text1) y = 21;
  525.     else      y = 27;
  526.     ask_req.Screen = MainScreen;
  527.     if(NOT(aw = OpenWindow(&ask_req))) return;
  528.     rp = aw->RPort;
  529.     draw(aw,&N,&MBorder1,&TTText);
  530.     SetDrMd(rp,JAM1);
  531.     SetAPen(rp,2);
  532.     Move(rp,150-((strlen((char *)text) << 3) >> 1),y);
  533.     Text(rp,(char *)text,strlen((char *)text));
  534.     if(text1)
  535.     {   Move(rp,150-((strlen((char *)text1) << 3) >> 1),y+10);
  536.         Text(rp,(char *)text1,strlen((char *)text1));
  537.     }
  538.     disable_window();
  539.     do
  540.     {   Wait(1 << aw->UserPort->mp_SigBit);
  541.         while(read_msg(aw));
  542.        id = Gadget->GadgetID;
  543.     } while(Class != GADGETUP);
  544.     CloseWindow(aw);
  545.     if(id == 1) ret = FALSE;
  546.     else ret = TRUE;
  547.     enable_window();
  548.     return(ret);
  549. }
  550.  
  551. /*
  552.  * put up the about window
  553.  */
  554. VOID About()
  555. {
  556.     struct Window *abw;
  557.     ab_req.Screen = MainScreen;
  558.     if(NOT(abw = OpenWindow(&ab_req))) return;
  559.     draw(abw,&CONT,&ABBorder1,&ABText1);
  560.     disable_window();
  561.     do
  562.     {   Wait(1 << abw->UserPort->mp_SigBit);
  563.         while(read_msg(abw));
  564.     } while(Class != GADGETUP);
  565.     CloseWindow(abw);
  566.     enable_window();
  567. }
  568.