home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / varie / charmap / source / acl.c next >
Encoding:
C/C++ Source or Header  |  1992-12-04  |  8.1 KB  |  315 lines

  1. /******************************************************************
  2. ** acl.c: Contient la gestion des requètes ASL, du clipboard, et **
  3. **        support à la locale.library. Écrit par T.Pierron.      **
  4. **        10-Déc-1999                                            **
  5. ******************************************************************/
  6.  
  7.  
  8. #include <Intuition/Intuition.H>
  9. #include <Intuition/IntuitionBase.H>
  10. #include <Libraries/Commodities.H>
  11. #include <Libraries/GadTools.H>
  12. #include <Libraries/ASL.H>
  13. #include <Dos/DosASL.H>
  14. #include <Exec/IO.H>
  15. #include <Devices/Clipboard.H>
  16. #include <Libraries/Locale.H>
  17.  
  18. #define    CATCOMP_STRINGS
  19. #define  CATCOMP_NUMBERS
  20. #include "cmap_strings.h"
  21.  
  22.  
  23. extern struct IntuitionBase *IntuitionBase;
  24. extern struct AslBase *AslBase;
  25. extern struct Window *window;
  26. struct FontRequester    *FR=NULL;
  27.  
  28. /* ASL Font requester tags: */
  29. ULONG FileTags[] = {
  30.     ASLFO_Window,NULL,
  31.     ASLFO_Screen,NULL,
  32.     ASLFO_SleepWindow,TRUE,
  33.     ASLFO_TitleText,(ULONG) MSG_ASLTITLE_STR,
  34.     ASLFO_MaxHeight,255,
  35.     TAG_DONE
  36. };
  37.  
  38. UBYTE *NoASL=MSG_NOASL_STR;
  39.  
  40. /* Try to open a ASL font requester: */
  41. struct TTextAttr *open_asl()
  42. {
  43.     FileTags[1] = (ULONG) window;
  44.     FileTags[3] = (ULONG) IntuitionBase->ActiveScreen;
  45.  
  46.     if(!FR)
  47.         if( !(FR = (void *) AllocAslRequest(ASL_FontRequest,NULL)) ) {
  48.             puts(NoASL);
  49.             return NULL;
  50.         }
  51.  
  52.     if( AslRequest((APTR) FR,FileTags) )
  53.         return &FR->fo_TAttr;
  54.     else
  55.         /* User hit cancel or close gadget! */
  56.         return NULL;
  57. }
  58.  
  59. struct IntuiText MsgTxt={ 0,0,JAM1,0,0,NULL,NULL,0};
  60. struct IntuiText Ack={ 0,0,JAM1,0,0,NULL,MSG_BUTTON1_STR,0};
  61. struct IntuiText Nac={ 0,0,JAM1,0,0,NULL,MSG_BUTTON2_STR,0};
  62.  
  63. BYTE avert(UBYTE *msg)
  64. {
  65.     MsgTxt.IText=msg;
  66.     return AutoRequest( window,&MsgTxt,AslBase?&Ack:NULL,&Nac,NULL,NULL,320,72 );
  67. }
  68.  
  69. /**** Convert a geometry string, describing windows positions: ****/
  70. void Parse_geometry(UBYTE *p)
  71. {
  72.     extern WORD Offset[];
  73.     BYTE sign=FALSE;
  74.     WORD *nb;
  75.  
  76.     for(nb=Offset; ; p++)
  77.     {
  78.         if(*p>='0' && *p<='9')
  79.             for(*nb=0; *p>='0' && *p<='9'; *nb*=10, *nb += *p++ - '0');
  80.  
  81.         switch(*p)
  82.         {
  83.             case '-': sign=TRUE; break;
  84.             case '+': break;
  85.             case 0:
  86.             case ',': if(sign) *nb = - *nb; nb++; sign=FALSE;
  87.                       if(*p && nb-Offset<4) break;
  88.             default:  return;
  89.         }
  90.     }
  91. }
  92.  
  93. /**** Adjust position to what user wants: ****/
  94. WORD Adjust_pos(WORD pos, WORD max, WORD len)
  95. {
  96.     /* if 0x7FFF center between 0 and max: */
  97.     if(pos==0x7FFF) return (max-len>>1);
  98.     /* if neg, align pos to max: */
  99.     if(pos<0) pos+=max-len;
  100.     if(pos<0) return 0;
  101.     if(pos+len>max) return max-len;
  102.     return pos;
  103. }
  104.  
  105. /****************************************************************
  106. ** Procédures d'entrées-sorties avec le Clipboard.device. Tiré **
  107. ** d'un exemple des RKM, modifié et optimisé par l'auteur.     **
  108. ****************************************************************/
  109.  
  110.  
  111. #define MAKE_ID(a,b,c,d) ((a<<24L) | (b<<16L) | (c<<8L) | d)
  112.  
  113. #define ID_FORM MAKE_ID('F','O','R','M')
  114. #define ID_FTXT MAKE_ID('F','T','X','T')
  115. #define ID_CHRS MAKE_ID('C','H','R','S')
  116.  
  117. /* prototypes */
  118. struct IOClipReq    *CBOpen            ( ULONG );
  119. void                    CBClose            (struct IOClipReq *);
  120. int                    CBWriteFTXT        (struct IOClipReq *, UBYTE *);
  121. int                    CBQueryFTXT        (struct IOClipReq *);
  122. void                    CBReadCHRS        (struct IOClipReq *, UBYTE *,WORD Max);
  123. void                    CBReadDone        (struct IOClipReq *);
  124.  
  125.  
  126. /* Try to open the clipboard: */
  127. struct IOClipReq *CBOpen(ULONG unit)
  128. {
  129.     struct MsgPort *mp;
  130.     struct IOStdReq *ior;
  131.  
  132.     if( (mp = (void *) CreatePort(0L,0L)) &&
  133.         (ior=(struct IOStdReq *)CreateExtIO(mp,sizeof(struct IOClipReq))) &&
  134.         (!OpenDevice("clipboard.device",unit,ior,0L)) )
  135.         return (struct IOClipReq *)ior;
  136.  
  137.     DeleteExtIO((struct IOStdReq *)ior);
  138.     DeletePort(mp);
  139.     return NULL;
  140. }
  141.  
  142. /* Try to close it: */
  143. void CBClose(struct IOClipReq *ior)
  144. {
  145.     struct MsgPort *mp = ior->io_Message.mn_ReplyPort;
  146.  
  147.     CloseDevice((struct IOStdReq *)ior);
  148.     DeleteExtIO((struct IOStdReq *)ior);
  149.     DeletePort(mp);
  150. }
  151.  
  152. #define WriteBuf(Buf,Val,Type)    *((Type *)Buf)++=Val
  153. UBYTE Buffer[71];
  154.  
  155. /* Write a string of text to the clipboard.device (max 50 chars!): */
  156. int CBWriteFTXT(struct IOClipReq *ior,UBYTE *string)
  157. {
  158.     UBYTE *p;
  159.     ULONG slen;
  160.     int success;
  161.  
  162.     if( (slen = strlen(string))==0 ) return FALSE;
  163. /*    slen += (slen & 1);                    /* pad if odd */
  164.  
  165.     /* initial set-up for Offset, Error, and ClipID */
  166.     ior->io_Offset = 0;
  167.     ior->io_Error  = 0;
  168.     ior->io_ClipID = 0;
  169.     p = Buffer;
  170.  
  171.     /* Create the IFF header information */
  172.     WriteBuf(p,ID_FORM,ULONG);            /* "FORM"             */
  173.     WriteBuf(p,slen+12,ULONG);            /* + "[size]FTXTCHRS" */
  174.     WriteBuf(p,ID_FTXT,ULONG);            /* "FTXT"             */
  175.     WriteBuf(p,ID_CHRS,ULONG);            /* "CHRS"             */
  176.     WriteBuf(p,slen,ULONG);                /* string length      */
  177.     CopyMem(string,p,slen);
  178.  
  179.     /* Write buffer to clipboard: */
  180.     ior->io_Data    = (STRPTR)Buffer;
  181.     ior->io_Length  = p-Buffer+slen;
  182.     ior->io_Command = CMD_WRITE;
  183.     DoIO( (struct IORequest *) ior);
  184.  
  185.     ior->io_Command=CMD_UPDATE;
  186.     DoIO( (struct IORequest *) ior);
  187.  
  188.     /* Check if io_Error was set by any of the preceding IO requests */
  189.     return ior->io_Error ? FALSE : TRUE;
  190. }
  191.  
  192. #define Tab(Buf,Type,n)        ((Type *)Buf)[n]
  193.  
  194. /* Check if there is TXT in the clipboard: */
  195. int CBQueryFTXT(struct IOClipReq *ior)
  196. {
  197.     /* initial set-up for Offset, Error, and ClipID */
  198.     ior->io_Offset = 0;
  199.     ior->io_Error  = 0;
  200.     ior->io_ClipID = 0;
  201.  
  202.     /* Look for "FORM[size]FTXT" */
  203.     ior->io_Command = CMD_READ;
  204.     ior->io_Data    = (STRPTR)Buffer;
  205.     ior->io_Length  = 12;
  206.     DoIO( (struct IORequest *) ior);
  207.  
  208.     if( (ior->io_Actual == 12L) &&                    /* Do we have at least 12 bytes ? */
  209.         (Tab(Buffer,ULONG,0) == ID_FORM) &&        /* Does it starts with "FORM" ? */
  210.         (Tab(Buffer,ULONG,2) == ID_FTXT) )            /* Is it "FTXT" ? */
  211.         return TRUE;
  212.  
  213.     /* It's not "FORM[size]FTXT", so tell clipboard we are done */
  214.     CBReadDone(ior);
  215.     return FALSE;
  216. }
  217.  
  218.  
  219. /* Reads the next CHRS chunk from clipboard */
  220. void CBReadCHRS(struct IOClipReq *ior,UBYTE *buf, WORD Max)
  221. {
  222.     ULONG size;
  223.  
  224.     /* Find next CHRS chunk */
  225.     ior->io_Command = CMD_READ;
  226.     ior->io_Data    = (STRPTR)buf;
  227.     ior->io_Length  = 8L;
  228.     for(;;)
  229.     {
  230.         /* Read the chunk ID and its length: */
  231.         DoIO( (struct IORequest *) ior);
  232.  
  233.         /* Have get enough data ? */
  234.         if (ior->io_Actual != 8) break;
  235.  
  236.         /* Get buffer size, and pad it if odd: */
  237.         size = Tab(Buffer,ULONG,1);
  238.         if (size & 1) size++;
  239.  
  240.         /* Is it a CHRS chunk ? */
  241.         if(Tab(buf,ULONG,0) == ID_CHRS)
  242.         {
  243.             if(size >= Max) size=Max-1;
  244.             ior->io_Length  = size;
  245.  
  246.             DoIO( (struct IOStdReq *) ior);
  247.             Buffer[size]    = 0;
  248.             break;
  249.         }
  250.         /* If not, skip to next chunk */
  251.         else
  252.             ior->io_Offset += size;
  253.     }
  254.     CBReadDone(ior);
  255. }
  256.  
  257. /* Tell clipboard we are done reading */
  258. void CBReadDone(struct IOClipReq *ior)
  259. {
  260.     ior->io_Command = CMD_READ;
  261.     ior->io_Data    = (STRPTR)Buffer;
  262.     ior->io_Length  = sizeof(Buffer)-2;
  263.  
  264.     /* falls through immediately if io_Actual == 0 */
  265.     while (ior->io_Actual)
  266.         if (DoIO( (struct IORequest *) ior)) break;
  267. }
  268.  
  269. /****************************************************************
  270. ** Locale.library support (50 lines !)                                    **
  271. ****************************************************************/
  272.  
  273.  
  274. /* All strings affected: */
  275. extern UBYTE *GadTxt[],*Errors[],*popmsg[];
  276. extern struct NewMenu newmenu[];
  277. extern struct EasyStruct Request;
  278. extern struct NewBroker newbroker;
  279.  
  280. struct Vars {
  281.     UBYTE **msg;            /* Message to change */
  282.     UBYTE nb;                /* Nb contiguous msg to change */
  283.     WORD  size;                /* Size of contiguous memory */
  284.     WORD    NumStr;            /* Id string from catalog */
  285. } TabVars[]={
  286.     GadTxt+1,5,sizeof(UBYTE *),MSG_NEWFONT,
  287.     Errors,7,sizeof(UBYTE *),MSG_BADOS,
  288.     popmsg,4,sizeof(UBYTE *),MSG_CODE,
  289.     &newmenu->nm_Label,21,sizeof(struct NewMenu),MSG_MENUTITLE,
  290.     &Request.es_Title,3,sizeof(UBYTE *),MSG_ABOUT,
  291.     &newbroker.nb_Name,3,sizeof(UBYTE *),MSG_COMMONAME,
  292.     (UBYTE **)&FileTags[7],1,0,MSG_ASLTITLE,
  293.     &NoASL,1,0,MSG_NOASL,
  294.     &Ack.IText,1,0,MSG_BUTTON1,
  295.     &Nac.IText,1,0,MSG_BUTTON2,
  296. };
  297.  
  298. void *catalog=NULL;
  299.  
  300. /* Translate all messages of the application: */
  301. void Translate_srings()
  302. {
  303.     if(catalog=(void *)OpenCatalogA(NULL,"CharMap.catalog",NULL))
  304.     {
  305.         struct Vars *p;
  306.         WORD n;
  307.         UBYTE **mes;
  308.         /* All necessary information is contained in our table: */
  309.         for(p=TabVars; p<TabVars+sizeof(TabVars)/sizeof(struct Vars); p++)
  310.             for(n=0,mes=p->msg; n<p->nb; n++,((UBYTE *)mes) += p->size )
  311.                 if(*mes!=NM_BARLABEL)    /* Newmenus barlabel string */
  312.                     *mes = (UBYTE *) GetCatalogStr(catalog,p->NumStr++,*mes);
  313.     }
  314. }
  315.