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

  1. /*----------------------------------------------------------------------*
  2.    tsel.c Version 2.3 -  © Copyright 1990-91 Jaba Development
  3.  
  4.    Author : Jan van den Baard
  5.    Purpose: Selection requester for gadget & window/requester texts
  6.  *----------------------------------------------------------------------*/
  7.  
  8. static SHORT MainPairs3[] =
  9.  { 0,0,295,0
  10.  };
  11. static struct Border MainBorder3 =
  12.  { 2,13,0,0,JAM1,2,MainPairs3,NULL
  13.  };
  14. static SHORT MainPairs2[] =
  15.  { 0,0,257,0,257,101,0,101,0,0
  16.  };
  17. static struct Border MainBorder2 =
  18.  { 8,17,0,0,JAM1,5,MainPairs2,&MainBorder3
  19.  };
  20. static SHORT MainPairs1[] =
  21.  { 0,0,295,0,295,147,0,147,0,0
  22.  };
  23. static struct Border MainBorder1 =
  24.  { 2,1,0,0,JAM1,5,MainPairs1,&MainBorder2
  25.  };
  26.  
  27. static struct IntuiText GText =
  28.  { 0,0,JAM1,78,4,NULL,(UBYTE *)"Select GadgetText",NULL
  29.  };
  30. static struct IntuiText WText =
  31.  { 0,0,JAM1,78,4,NULL,(UBYTE *)"Select WindowText",NULL
  32.  };
  33. static struct IntuiText RText =
  34.  { 0,0,JAM1,66,4,NULL,(UBYTE *)"Select RequesterText",NULL
  35.  };
  36.  
  37. static SHORT CKPairs[] =
  38.  { 0,0,121,0,121,20,0,20,0,0
  39.  };
  40. static struct Border CKBorder =
  41.  { -1,-1,0,0,JAM1,5,CKPairs,NULL
  42.  };
  43. static struct IntuiText OKText =
  44.  { 0,0,JAM1,50,6,NULL,(UBYTE *)"OK",NULL
  45.  };
  46. static struct Gadget OK =
  47.  { NULL,9,124,120,19,NULL,RELVERIFY,BOOLGADGET,
  48.    (APTR)&CKBorder,NULL,&OKText,NULL,NULL,2,NULL
  49.  };
  50. static struct IntuiText CNCText =
  51.  { 0,0,JAM1,38,6,NULL,(UBYTE *)"CANCEL",NULL
  52.  };
  53. static struct Gadget CNC =
  54.  { &OK,170,124,120,19,NULL,RELVERIFY,BOOLGADGET,
  55.    (APTR)&CKBorder,NULL,&CNCText,NULL,NULL,1,NULL
  56.  };
  57. static struct PropInfo PROPInfo =
  58.  { AUTOKNOB+FREEVERT,-1,0,6553,6553,0,0,0,0,0,0
  59.  };
  60. static struct Image PROPImage;
  61. static struct Gadget PROP =
  62.  { &CNC,269,16,26,104,NULL,RELVERIFY,PROPGADGET,
  63.    (APTR)&PROPImage,NULL,NULL,NULL,(APTR)&PROPInfo,0,NULL
  64.  };
  65. static struct NewWindow sel_req =
  66.  { 10,15,300,150,0,1,GADGETUP|GADGETDOWN,
  67.    NOCAREREFRESH+SMART_REFRESH+ACTIVATE+RMBTRAP,
  68.    NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  69.  };
  70.  
  71. extern struct Window      *MainWindow;
  72. extern struct Screen      *MainScreen;
  73. extern struct Gadget      *Gadget;
  74. extern struct RastPort    *MainRP;
  75. extern struct MemoryChain  Memory;
  76. extern ULONG               Class;
  77. extern BOOL                REQUESTER;
  78. extern USHORT              BackFill;
  79.  
  80. static struct Gadget Gad[10];
  81. static struct Gadget G =
  82.  { NULL,9,18,256,10,GADGHCOMP,TOGGLESELECT+GADGIMMEDIATE,
  83.    BOOLGADGET,NULL,NULL,NULL,NULL,NULL,0,NULL
  84.  };
  85. static struct RastPort *rp;
  86. static struct Window   *tswin;
  87. SHORT           text_num, num_text,selected = 0;
  88.  
  89. /*
  90.  * add the text gadgets to the window
  91.  */
  92. VOID do_gadgets()
  93. {
  94.     register UCOUNT i,top=18;
  95.     LONG            mutex = NULL;
  96.     for(i=0;i<10;i++) mutex += (1 << (i + 3));
  97.     for(i=0;i<10;i++,top+=10)
  98.     {   Gad[i]                   =   G;
  99.         Gad[i].TopEdge           =   top;
  100.         Gad[i].GadgetID          =   i+3;
  101.         Gad[i].MutualExclude     =   mutex;
  102.         Gad[i].NextGadget        =   &Gad[i+1];
  103.     }
  104.     Gad[i].NextGadget = NULL;
  105.     AddGList(tswin,&Gad[0],-1L,10,NULL);
  106. }
  107.  
  108. /*
  109.  * set the proportional gadget according to the number of texts
  110.  */
  111. VOID set_prop(gadget)
  112.     struct Gadget *gadget;
  113. {
  114.     register struct IntuiText *t;
  115.  
  116.     num_text      = 1;
  117.     OK.NextGadget = NULL;
  118.     t             = gadget->GadgetText;
  119.  
  120.     while((t = t->NextText)) num_text++;
  121.  
  122.     PROPInfo.VertPot = 0;
  123.  
  124.     if(num_text <= 10)      PROPInfo.VertBody = MAXBODY;
  125.     else if(num_text == 11) PROPInfo.VertBody = 0x8000;
  126.     else                    PROPInfo.VertBody = MAXBODY / (num_text - 10);
  127. }
  128.  
  129. /*
  130.  * get the pointer to the IntuitionText structure
  131.  * of text number 'num' in gadget 'gadget'
  132.  */
  133. struct IntuiText *GetPtr(gadget,num)
  134.     struct Gadget *gadget;
  135.     SHORT         num;
  136. {
  137.     register COUNT i=0;
  138.     register struct IntuiText *t;
  139.     t = gadget->GadgetText;
  140.     while(i != num)
  141.     {   if(NOT(t = t->NextText)) break;
  142.         i++;
  143.     }
  144.     return(t);
  145. }
  146.  
  147. /*
  148.  * print the text list
  149.  */
  150. VOID do_text(gadget)
  151.     struct Gadget *gadget;
  152. {
  153.     register ULONG             Max = 10, Pos = 0,i,y=25;
  154.     register struct IntuiText *t;
  155.  
  156.     Pos = PROPInfo.VertPot/PROPInfo.VertBody;
  157.     if(num_text - Pos < 10) Max = num_text;
  158.     DeSelectGadget(tswin,&Gad[selected],NULL);
  159.     t = GetPtr(gadget,Pos);
  160.  
  161.     SetDrMd(rp,JAM1);
  162.     SetAPen(rp,1);
  163.     RectFill(rp,9,18,264,117);
  164.  
  165.     for(i=0;i<Max;i++,y+=10)
  166.     {   if(t)
  167.         {   SetAPen(rp,0);
  168.             Move(rp,9,y);
  169.             if(strlen((char *)t->IText) > 32) Text(rp,(char *)t->IText,32);
  170.             else Text(rp,(char *)t->IText,strlen((char *)t->IText));
  171.         }
  172.         t = t->NextText;
  173.     }
  174.     if(i < 10)
  175.     {    OffGList(tswin,&Gad[i],NULL,10-i);
  176.          SetAPen(rp,1);
  177.          RectFill(rp,9,y-8,264,117);
  178.     }
  179.     if(text_num < Pos)
  180.     {   text_num = Pos;
  181.         SelectGadget(tswin,&Gad[0],NULL);
  182.         selected = 0;
  183.     }
  184.     else if(text_num > 9)
  185.     {   text_num = Pos+9;
  186.         SelectGadget(tswin,&Gad[9],NULL);
  187.         selected = 9;
  188.     }
  189.     else
  190.     {   SelectGadget(tswin,&Gad[text_num - Pos],NULL);
  191.         selected = text_num - Pos;
  192.     }
  193. }
  194.  
  195. /*
  196.  * calculate the text number according to the prop position
  197.  */
  198. VOID do_num(num)
  199.     SHORT num;
  200. {
  201.     SHORT Pos;
  202.     Pos = PROPInfo.VertPot/PROPInfo.VertBody;
  203.     text_num = num + Pos;
  204.     selected = num;
  205. }
  206.  
  207. /*
  208.  * delete the text
  209.  */
  210. VOID delete_text(gadget)
  211.     struct Gadget *gadget;
  212. {
  213.     struct IntuiText *t,*succ,*pred;
  214.     LONG Pos;
  215.  
  216.     succ = GetPtr(gadget,text_num+1);
  217.     pred = GetPtr(gadget,text_num-1);
  218.     t    = GetPtr(gadget,text_num);
  219.     if(t)
  220.     {   if(pred) pred->NextText = succ;
  221.         else gadget->GadgetText = succ;
  222.         FreeItem(&Memory,t->IText,80L);
  223.         FreeItem(&Memory,t,(long)sizeof(struct IntuiText));
  224.     }
  225. }
  226.  
  227. /*
  228.  * clear a text from the display
  229.  */
  230. VOID clear_text(g)
  231.     struct Gadget *g;
  232. {
  233.     struct IntuiText *ttc,it;
  234.  
  235.     ttc = GetPtr(g,text_num);
  236.     CopyMem((void *)ttc,(void *)&it,sizeof(struct IntuiText));
  237.     it.FrontPen = it.BackPen = 0;
  238.     if(REQUESTER) it.FrontPen = it.BackPen = BackFill;
  239.     it.DrawMode = JAM2;
  240.     it.NextText = NULL;
  241.     un_grel(MainWindow,g);
  242.     PrintIText(MainRP,&it,g->LeftEdge,g->TopEdge);
  243.     grel(MainWindow,g);
  244. }
  245.  
  246. /*
  247.  * put up the text selector
  248.  */
  249. LONG text_select(gadget,mode,which)
  250.     struct Gadget *gadget;
  251.     LONG           mode;
  252.     USHORT         which;
  253. {
  254.     BOOL   running = TRUE;
  255.     struct IntuiText *MT;
  256.     USHORT gid;
  257.  
  258.     set_prop(gadget);
  259.     sel_req.Screen = MainScreen;
  260.     if(NOT(tswin = OpenWindow(&sel_req))) return;
  261.     disable_window();
  262.     if(which == 0) MT = >ext;
  263.     else if(which == 1) MT = &WText;
  264.     else MT = &RText;
  265.     draw(tswin,&PROP,&MainBorder1,MT);
  266.     do_gadgets();
  267.     rp = tswin->RPort;
  268.     SelectGadget(tswin,&Gad[0],NULL);
  269.     do_num(0);
  270.     do_text(gadget);
  271.     do
  272.     {   Wait(1 << tswin->UserPort->mp_SigBit);
  273.         while(read_msg(tswin))
  274.         {   if((Class == GADGETUP) OR (Class == GADGETDOWN))
  275.             {   gid = Gadget->GadgetID;
  276.                 if(gid > 2)
  277.                 {    do_num(gid-3);
  278.                      MutualExclude(tswin,&Gad[gid-3],&PROP,NULL);
  279.                      SelectGadget(tswin,&Gad[gid-3],NULL);
  280.                 }
  281.                 else if((gid == 0) AND (num_text > 10)) do_text(gadget);
  282.                 else if((gid == 1) OR (gid == 2)) running = FALSE;
  283.             }
  284.         }
  285.     } while(running == TRUE);
  286.     while(read_msg(tswin));
  287.     CloseWindow(tswin);
  288.     enable_window();
  289.     if(gid == 2)
  290.     {   if(mode == 1)      return(text_num);
  291.         else if(mode == 2)
  292.         {   clear_text(gadget);
  293.             edit_text(gadget,mode,text_num,which);
  294.             return(NULL);
  295.         }
  296.         else
  297.         {   clear_text(gadget);
  298.             delete_text(gadget);
  299.             return(NULL);
  300.         }
  301.     }
  302.     return(-1L);
  303. }
  304.