home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d386 / xlispstat.lha / XLispStat / src1.lzh / Amiga / amireq1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-11  |  17.0 KB  |  460 lines

  1. /* AmiReq1.c - Low Level Requester Objects for Amiga                   */
  2. /* Copyright (c) 1990 by J.K. Lindsey                                  */
  3. /* Additions to XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney     */
  4. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  5. /* You may give out copies of this software; for conditions see the    */
  6. /* file COPYING included with this distribution.                       */
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <libraries/dosextens.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "autil2.h"
  16. #include "xlisp.h"
  17. #include "osdef.h"
  18. #include "xlproto.h"
  19. #include "xlsproto.h"
  20. #include "iviewproto.h"
  21. #include "Stproto.h"
  22. #include "osproto.h"
  23. #include "xlsvar.h"
  24. #include "amivar.h"
  25.  
  26. /* layout definitions */
  27. #define MAX_ENTRY_LENGTH 30
  28. #define SCROLL_MIN 0
  29. #define SCROLL_MAX 100
  30. #define CHOICE_HEIGHT 20
  31.  
  32. void zero_ptr(char *,int),InstallItemList(DialogPtr,LVAL);
  33.  
  34. /***********************************************************************/
  35. /***********************************************************************/
  36. /**                                                                   **/
  37. /**                       Internal Dialog Functions                   **/
  38. /**                                                                   **/
  39. /***********************************************************************/
  40. /***********************************************************************/
  41.  
  42. /***********************************************************************/
  43. /**                                                                   **/
  44. /**                          Utility Functions                        **/
  45. /**                                                                   **/
  46. /***********************************************************************/
  47.  
  48. DialogItemData *GetDialogItemData(DialogPtr theDialog){
  49.    return(GetDialogData(theDialog)->pid);}
  50.  
  51. DialogItemData *FindItemData(DialogPtr theDialog,LVAL item){
  52.    DialogItemData *data,*itemData;
  53.    int n;
  54.    n=DialogItemCount(theDialog);
  55.    data=GetDialogItemData(theDialog);
  56.    for(itemData=0;n&&!itemData;n--,data++)if(data->object==item)itemData=data;
  57.    return(itemData);}
  58.  
  59. /***********************************************************************/
  60. /**                                                                   **/
  61. /**                         DIALOG-PROTO Methods                      **/
  62. /**                                                                   **/
  63. /***********************************************************************/
  64.  
  65. static void MakeDialogItemData(LVAL dialog,struct Gadget **items){
  66.    DialogPtr theDialog;
  67.    DialogData *p;
  68.    int numItems;
  69.    numItems=count_hardware_items(slot_value(dialog,s_items));
  70.    if(!(theDialog=(DialogPtr)GETDIALOGADDRESS(dialog)))xlfail("dialog not allocated");
  71.    p=GetDialogData(theDialog);
  72.    if(!(*items=p->gadl=calloc(numItems,sizeof(struct Gadget))))xlfail("gadgets not allocated");
  73.    if(!(p->pid=calloc(numItems,sizeof(DialogItemData))))xlfail("gadget data not allocated");}
  74.  
  75. static void SetDialogItemData(LVAL dialog){
  76.    DialogPtr theDialog;
  77.    DialogData *dialogData;
  78.    LVAL items=slot_value(dialog,s_items);
  79.    if(!(theDialog=(DialogPtr)GETDIALOGADDRESS(dialog)))xlfail("dialog not allocated");
  80.    SetDialogObject(theDialog,dialog);
  81.    dialogData=GetDialogData(theDialog);
  82.    dialogData->count=0;
  83.    InstallItemList(theDialog,items);}
  84.  
  85. static FindItemType(LVAL item){
  86.    if (consp(item))return(ITEM_LIST);
  87.    else if(button_item_p(item))return(BUTTON_ITEM);
  88.    else if(toggle_item_p(item))return(TOGGLE_ITEM);
  89.    else if(text_item_p(item))return(TEXT_ITEM);
  90.    else if(choice_item_p(item))return(CHOICE_ITEM);
  91.    else if(scroll_item_p(item))return(SCROLL_ITEM);
  92.    else if(list_item_p(item))return(LIST_ITEM);
  93.    else xlerror("item of unknown type",item);}
  94.  
  95. static struct Gadget *install_gadget(DialogPtr theDialog,LVAL item,int *i){
  96.    struct Gadget *gad;
  97.    struct IntuiText *itext;
  98.    DialogData *dialogData;
  99.    Point loc,size;
  100.    dialogData=GetDialogData(theDialog);
  101.    *i=(dialogData->count)++;
  102.    gad=&(dialogData->gadl[*i]);
  103.    loc=ListToPoint(slot_value(item,s_location));
  104.    size=ListToPoint(slot_value(item,s_size));
  105.    if(*i<DialogItemCount(theDialog)-1)gad->NextGadget=gad+1;
  106.    else gad->NextGadget=0;
  107.    gad->TopEdge=loc.v+theDialog->BorderTop;
  108.    gad->LeftEdge=loc.h+theDialog->BorderLeft;
  109.    gad->Height=size.v;
  110.    gad->Width=size.h;
  111.    gad->Flags=GADGHCOMP;
  112.    gad->Activation=RELVERIFY;
  113.    gad->GadgetRender=gad->SelectRender=0;
  114.    if(!(itext=gad->GadgetText=calloc(1,sizeof(struct IntuiText))))
  115.    xlfail("gadget IntuiText allocation failed");
  116.    itext->FrontPen=BLACK;
  117.    itext->BackPen=WHITE;
  118.    itext->DrawMode=JAM2;
  119.    itext->LeftEdge=2;
  120.    itext->TopEdge=(size.v-8)/2;
  121.    gad->GadgetID=*i;
  122.    return(gad);}
  123.    
  124. static void InstallTextItem(DialogPtr theDialog,LVAL item){
  125.    struct Gadget *gad;
  126.    struct StringInfo *spinfo;
  127.    struct Border *bd;
  128.    struct IntuiText *itext,*oitext;
  129.    short *bc;
  130.    static char buf[80],ubuf[80];
  131.    DialogItemData *data;
  132.    char *text;
  133.    int itemIndex;
  134.    gad=install_gadget(theDialog,item,&itemIndex);
  135.    if(!stringp(slot_value(item,s_text)))xlerror("not a string",slot_value(item,s_text));
  136.    text=(char *)getstring(slot_value(item,s_text));
  137.    if(slot_value(item,s_editable)){
  138.       gad->GadgetType=STRGADGET;
  139.       gad->TopEdge+=2;
  140.       if(!(bd=(struct Border *)gad->GadgetRender=calloc(1,sizeof(struct Border))))
  141.       xlfail("text border allocation failed");
  142.       bd->LeftEdge=bd->TopEdge=-2;
  143.       bd->FrontPen=BLACK;
  144.       bd->DrawMode=JAM1;
  145.       bd->Count=5;
  146.       if(!(bc=bd->XY=calloc(1,10*sizeof(short))))xlfail("text point allocationfailed");
  147.       bc[2]=bc[4]=gad->Width;
  148.       bc[5]=bc[7]=12;
  149.       if(!(spinfo=(struct StringInfo *)gad->SpecialInfo=malloc(sizeof(struct StringInfo))))
  150.       xlfail("text SpecialInfo allocation failed");
  151.       strcpy(buf,text);
  152.       spinfo->Buffer=buf;
  153.       spinfo->UndoBuffer=ubuf;
  154.       spinfo->BufferPos=strlen(text);
  155.       spinfo->MaxChars=79;
  156.       spinfo->DispPos=0;
  157.       free(gad->GadgetText);
  158.       gad->GadgetText=0;}
  159.    else {
  160.       gad->Flags=GADGHNONE|GADGDISABLED;
  161.       oitext=gad->GadgetText;
  162.       oitext->IText=text;
  163.       while(*text){
  164.          if(*text==10){
  165.             *text=0;
  166.             if(++text==0)break;
  167.             if(!(itext=calloc(1,sizeof(struct IntuiText))))xlfail("gadget IntuiText allocation failed");
  168.             itext->FrontPen=BLACK;
  169.             itext->BackPen=WHITE;
  170.             itext->DrawMode=JAM2;
  171.             itext->LeftEdge=2;
  172.             itext->TopEdge=oitext->TopEdge+9;
  173.             itext->IText=text;
  174.             oitext->NextText=itext;
  175.             oitext=itext;}
  176.          text++;}
  177.       gad->Width=gad->Height=0;}
  178.    data=GetDialogItemData(theDialog);
  179.    data[itemIndex].type=TEXT_ITEM;
  180.    data[itemIndex].itemNumber=itemIndex;
  181.    data[itemIndex].itemPtr=gad;
  182.    data[itemIndex].object=item;}
  183.  
  184. static short xy1[]={1,1,7,1,7,7,1,7,1,1},xy2[]={0,0,8,0,8,8,0,8,0,0};
  185. static struct Border di={-1,-1,BLACK,WHITE,JAM1,5,xy2,0},
  186.                      si={-1,-1,BLACK,WHITE,JAM1,5,xy1,&di};
  187.  
  188. static void InstallChoiceItem(DialogPtr theDialog,LVAL item){
  189.    LVAL titles,temp;
  190.    DialogItemData *data;
  191.    struct Gadget *gad;
  192.    int i,itemIndex,clusterLeader,clusterSize,initial;
  193.    titles=slot_value(item,s_text);
  194.    if(!listp(titles))xlerror("not a list",titles);
  195.    clusterLeader=GetDialogData(theDialog)->count;
  196.    clusterSize=llength(titles);
  197.    data=GetDialogItemData(theDialog);
  198.    for(i=0;consp(titles);i++,titles=cdr(titles)) {
  199.       if(!stringp(car(titles)))xlerror("not a string",car(titles));
  200.       gad=install_gadget(theDialog,item,&itemIndex);
  201.       gad->GadgetText->IText=(char *)getstring(car(titles));
  202.       gad->GadgetText->TopEdge=0;
  203.       gad->GadgetText->LeftEdge=12;
  204.       gad->Flags=GADGHIMAGE;
  205.       gad->GadgetType=BOOLGADGET;
  206.       gad->Activation=GADGIMMEDIATE;
  207.       gad->Height=gad->Width=7;
  208.       gad->TopEdge+=CHOICE_HEIGHT*i;
  209.       gad->GadgetRender=(APTR)&di;
  210.       gad->SelectRender=(APTR)&si;
  211.       data[itemIndex].type=CHOICE_ITEM;
  212.       data[itemIndex].itemNumber=itemIndex;
  213.       data[itemIndex].itemPtr=gad;
  214.       data[itemIndex].object=item;
  215.       data[itemIndex].clusterLeader=clusterLeader;
  216.       data[itemIndex].clusterSize=clusterSize;}
  217.    temp=slot_value(item,s_value);
  218.    initial=(fixp(temp))?getfixnum(temp):0;
  219.    initial=((initial>=0&&initial<clusterSize)?initial:0)+clusterLeader;
  220.    data[initial].itemPtr->Flags|=SELECTED;}
  221.  
  222. static void InstallScrollItem(DialogPtr theDialog,LVAL item){
  223.    DialogItemData *data;
  224.    struct Gadget *gad;
  225.    struct PropInfo *pinfo;
  226.    int low,high,value,itemIndex,pos;
  227.    LVAL temp;
  228.    gad=install_gadget(theDialog,item,&itemIndex);
  229.    temp=slot_value(item,s_min_value);
  230.    low=fixp(temp)?getfixnum(temp):SCROLL_MIN;
  231.    temp=slot_value(item,s_max_value);
  232.    high=fixp(temp)?getfixnum(temp):SCROLL_MAX;
  233.    temp=slot_value(item,s_value);
  234.    value=(fixp(temp))?getfixnum(temp):low;
  235.    gad->Flags=GADGIMAGE;
  236.    gad->GadgetType=PROPGADGET;
  237.    if(!(gad->GadgetRender=malloc(sizeof(struct Image))))xlfail("slider image allocation failed");
  238.    free(gad->GadgetText);
  239.    gad->GadgetText=0;
  240.    if(!(pinfo=(struct PropInfo *)gad->SpecialInfo=malloc(sizeof(struct PropInfo))))
  241.    xlfail("slider SpecialInfo allocation failed");
  242.    pinfo->Flags=AUTOKNOB;
  243.    pos=(MAXPOT*(value-low))/(high-low);
  244.    if(gad->Height>gad->Width){
  245.       pinfo->Flags|=FREEVERT;
  246.       pinfo->VertPot=pos;
  247.       pinfo->VertBody=MAXBODY/(high-low);
  248.       pinfo->HorizPot=MAXPOT;
  249.       pinfo->HorizBody=MAXBODY;}
  250.    else{
  251.       pinfo->Flags|=FREEHORIZ;
  252.       pinfo->HorizPot=pos;
  253.       pinfo->HorizBody=MAXBODY/(high-low);
  254.       pinfo->VertPot=MAXPOT;
  255.       pinfo->VertBody=MAXBODY;}
  256.    data=GetDialogItemData(theDialog);
  257.    data[itemIndex].type=SCROLL_ITEM;
  258.    data[itemIndex].itemNumber=itemIndex;
  259.    data[itemIndex].itemPtr=gad;
  260.    data[itemIndex].object=item;}
  261.  
  262. static void InstallControlItem(DialogPtr theDialog,LVAL item,int which){
  263.    DialogItemData *data;
  264.    struct Gadget *gad;
  265.    int itemIndex;
  266.    struct Border *border;
  267.    short *bc;
  268.    gad=install_gadget(theDialog,item,&itemIndex);
  269.    if(!(border=(struct Border *)gad->GadgetRender=calloc(1,sizeof(struct Border))))
  270.    xlfail("gadget border allocation failed");
  271.    border->FrontPen=BLACK;
  272.    border->BackPen=WHITE;
  273.    border->DrawMode=JAM1;
  274.    border->Count=9;
  275.    if(!(bc=border->XY=malloc(18*sizeof(short))))xlfail("gadget border points allocation failed");
  276.    bc[0]=bc[5]=bc[10]=bc[15]=bc[16]=2;
  277.    bc[1]=bc[3]=bc[12]=bc[14]=bc[17]=-2;
  278.    bc[2]=bc[8]=gad->Width-2;
  279.    bc[4]=bc[6]=gad->Width+2;
  280.    bc[7]=bc[13]=gad->Height-2;
  281.    bc[9]=bc[11]=gad->Height+2;
  282.    if(which==TOGGLE_ITEM){
  283.       if(slot_value(item,s_value))gad->Flags|=SELECTED;
  284.       gad->Activation|=TOGGLESELECT;}
  285.    if(!stringp(slot_value(item,s_text)))xlerror("not a string",slot_value(item, s_text));
  286.    gad->GadgetText->IText=(char *)getstring(slot_value(item, s_text));
  287.    gad->GadgetText->LeftEdge=(gad->Width-8*strlen(gad->GadgetText->IText))/2;
  288.    gad->GadgetType=BOOLGADGET;
  289.    data=GetDialogItemData(theDialog);
  290.    data[itemIndex].type=which;
  291.    data[itemIndex].itemNumber=itemIndex;
  292.    data[itemIndex].itemPtr=gad;
  293.    data[itemIndex].object=item;}
  294.  
  295. static void InstallButtonItem(DialogPtr theDialog,LVAL item){
  296.   InstallControlItem(theDialog,item,BUTTON_ITEM);}
  297.  
  298. static void InstallToggleItem(DialogPtr theDialog,LVAL item){
  299.   InstallControlItem(theDialog,item,TOGGLE_ITEM);}
  300.  
  301. static void InstallItem(DialogPtr theDialog,LVAL item){
  302.    int type;
  303.    if(!dialog_item_p(item))xlerror("not a dialog item",item);
  304.    type=FindItemType(item);
  305.    switch (type) {
  306.       case BUTTON_ITEM: InstallButtonItem(theDialog,item); break;
  307.       case TOGGLE_ITEM: InstallToggleItem(theDialog,item); break;
  308.       case CHOICE_ITEM: InstallChoiceItem(theDialog,item); break;
  309.       case MESSAGE_ITEM:
  310.       case TEXT_ITEM:   InstallTextItem(theDialog,item);   break;
  311.       case SCROLL_ITEM: InstallScrollItem(theDialog,item); break;
  312. /*      case REAL_SCROLL_ITEM:
  313.       case LIST_ITEM:   InstallListItem(theDialog,item);   break;*/
  314.       default: xlfail("unkown item type");}}
  315.  
  316. static void InstallItemList(DialogPtr theDialog,LVAL items){
  317.    for(;consp(items);items=cdr(items)){
  318.       if(consp(car(items)))InstallItemList(theDialog,car(items));
  319.       else InstallItem(theDialog,car(items));}}
  320.  
  321. static void zero_ptr(char *p,int n){
  322.   while(n-->0)*p++=0;}
  323.  
  324. int count_hardware_items(LVAL items){
  325.    LVAL temp;
  326.    int n;
  327.    if(!consp(items))return(0);
  328.    else for(n=0;consp(items);items=cdr(items))
  329.       switch(FindItemType(car(items))) {
  330.          case CHOICE_ITEM: {
  331.             temp=slot_value(car(items),s_text);
  332.             if(!consp(temp))xlerror("not a list",temp);
  333.             n+=llength(temp);
  334.             break;}
  335.          case ITEM_LIST: n+=count_hardware_items(car(items)); break;
  336.          default: n += 1;}
  337.   return(n);}
  338.  
  339. /***********************************************************************/
  340. /***********************************************************************/
  341. /**                                                                   **/
  342. /**                       Public Dialog Functions                     **/
  343. /**                                                                   **/
  344. /***********************************************************************/
  345. /***********************************************************************/
  346.  
  347. void DialogSetDefaultButton(LVAL dialog,LVAL item){
  348.    if(item&&!button_item_p(item))xlerror("not a button item",item);
  349.    set_slot_value(dialog,s_default_button,item);}
  350.  
  351. void DialogAllocate(LVAL dialog){
  352.    DialogPtr theDialog;
  353.    struct Gadget *items;
  354.    struct NewWindow nw;
  355.    StGWWinInfo *wi;
  356.    Point loc,size;
  357.    char *title;
  358.    unsigned long flags,iflags;
  359.    if(!stringp(slot_value(dialog,s_title)))xlerror("not a string",slot_value(dialog,s_title));
  360.    title=(char *)getstring(slot_value(dialog,s_title));
  361.    loc=ListToPoint(slot_value(dialog,s_location));
  362.    size=ListToPoint(slot_value(dialog,s_size));
  363.    iflags=GADGETUP|GADGETDOWN;
  364.    flags=WINDOWDRAG|SMART_REFRESH|ACTIVATE|WINDOWDEPTH|NOCAREREFRESH;
  365.    if(slot_value(dialog,s_go_away)){
  366.       flags|=WINDOWCLOSE;
  367.       iflags|=CLOSEWINDOW;}
  368.    nw.LeftEdge=loc.h;
  369.    nw.TopEdge=loc.v;
  370.    nw.Width=size.h+2*screen->WBorLeft;
  371.    nw.Height=size.v+screen->WBorLeft+screen->BarHeight+1;
  372.    nw.DetailPen=WHITE;
  373.    nw.BlockPen=BLACK;
  374.    nw.IDCMPFlags=iflags;
  375.    nw.Flags=flags;
  376.    nw.FirstGadget=0;
  377.    nw.CheckMark=0;
  378.    nw.Title=title;
  379.    nw.Screen=screen;
  380.    nw.BitMap=0;
  381.    nw.MinWidth=0;
  382.    nw.MinHeight=0;
  383.    nw.MaxWidth=0;
  384.    nw.MaxHeight=0;
  385.    nw.Type=screentype;
  386.    if(!(theDialog=OpenWindow(&nw)))xlfatal("window allocation failed");
  387.    if(!(wi=(StGWWinInfo *)theDialog->UserData=malloc(sizeof(StGWWinInfo))))
  388.    xlfail("dialog wininfo allocation failed");
  389.    wi->window=theDialog;
  390.    wi->Object=dialog;
  391.    set_dialog_address(theDialog,dialog);
  392.    MakeDialogItemData(dialog,&items);
  393.    SetDialogItemData(dialog);
  394.    AddGList(theDialog,items,0,-1,0);
  395.    RefreshGList(items,theDialog,0,-1);
  396.    DialogSetDefaultButton(dialog,slot_value(dialog,s_default_button));}
  397.  
  398. void DialogRemove(LVAL dialog){
  399.    struct Window *w;
  400.    DialogPtr theDialog;
  401.    int i,which;
  402.    struct Gadget *gad,*gadl;
  403.    struct IntuiText *itext,*oitext;
  404.    DialogItemData *data;
  405.    StGWWinInfo *gwinfo;
  406.    if(check_dialog_address(dialog)){
  407.       theDialog=GETDIALOGADDRESS(dialog);
  408.       w=screen->FirstWindow;
  409.       while(w&&w!=theDialog)w=w->NextWindow;
  410.       if(!w)return;
  411.       CloseWindow(theDialog);
  412.       data=GetDialogItemData(theDialog);
  413.       gadl=GetDialogData(theDialog)->gadl;
  414.       gwinfo=(StGWWinInfo *)theDialog->UserData;
  415.       for(i=0;i<DialogItemCount(theDialog);i++){
  416.          which=data[i].type;
  417.          gad=data[i].itemPtr;
  418.          free(gad->SpecialInfo);
  419.          if(which==BUTTON_ITEM||which==TOGGLE_ITEM||(which==TEXT_ITEM&&gad->GadgetRender))
  420.          free(((struct Border *)gad->GadgetRender)->XY);
  421.          if(which!=CHOICE_ITEM)free(gad->GadgetRender);
  422.          itext=gad->GadgetText;
  423.          while(itext){
  424.             oitext=itext;
  425.             itext=oitext->NextText;
  426.             free(oitext);}}
  427.       free(gadl);
  428.       free(GetDialogItemData(theDialog));
  429.       free(gwinfo);}
  430.    if(objectp(dialog))standard_hardware_clobber(dialog);}
  431.  
  432. static LVAL get_file(char *prompt,char def_name[],char def_dir[]){
  433.    LVAL result;
  434.    struct Process *OurTask;
  435.    APTR prwindowptr;
  436.    char fname[100];
  437.    OurTask=(struct Process *)FindTask(0);
  438.    prwindowptr=(APTR)OurTask->pr_WindowPtr;
  439.    OurTask->pr_WindowPtr=(APTR)window;
  440.    if(!get_fname(window,screen,prompt,def_name,def_dir,""))return(0);
  441.    OurTask->pr_WindowPtr=(APTR)prwindowptr;
  442.    if(!def_name[0])return(0);
  443.    strcpy(fname,def_dir);
  444.    if(fname[strlen(fname)-1]!=':'&&fname[strlen(fname)-1]!='/'&&
  445.    fname[0]!=0)strcat(fname,"/");
  446.    strcat(fname,def_name);
  447.    result=newstring(strlen(fname)+1);
  448.    strcpy(getstring(result),fname);
  449.    return(result);}
  450.  
  451. LVAL xsopenfiledialog(void){
  452.    static unsigned char def_name[50],def_dir[50];
  453.    return(get_file("Choose a file:",def_name,def_dir));}
  454.  
  455. LVAL xssetfiledialog(void){
  456.    char def_name[50],*prompt;
  457.    static unsigned char def_dir[50];
  458.    prompt=(char *)getstring(xlgastring());
  459.    def_name[0]=0;
  460.    return(get_file(prompt,def_name,def_dir));}