home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume2 / xbrowser / part03 / ap.c next >
Encoding:
C/C++ Source or Header  |  1989-01-03  |  3.5 KB  |  142 lines

  1. /* Systems Sciences Laboratory, Webster Research Center */
  2.  
  3. static char *PROGRAM_information[] =
  4. {
  5.     "Copyright (c) 1988 Xerox Corporation.  All rights reserved.",
  6.     "$Header$",
  7.     "$Locker$"
  8. }
  9. ;
  10.  
  11. /*
  12.  * Copyright protection claimed includes all forms and matters of copyrightable
  13.  * material and information now allowed by statutory or judicial lay or
  14.  * herinafter granted, including without limitation, material generated from
  15.  * the software programs which are displayed on the screen such as icons,
  16.  * screen display looks, etc.
  17.  */
  18.  
  19.  
  20. #include "xfilebrowser.h"
  21.  
  22. #define chunk 2048
  23.  
  24. typedef struct {
  25.     char *buf;
  26.     int size;
  27.     XtTextPosition pos;
  28.     XtTextSource strSrc;
  29. } ApAsSourceData;
  30.  
  31. /* Private Routines */
  32.  
  33.  
  34. static XtTextPosition ApAsGetLastPos(src)
  35.   XtTextSource src;
  36. {
  37.   ApAsSourceData *data;
  38.     data = (ApAsSourceData *)src->data;
  39.     return (XtTextPosition)(*data->strSrc->GetLastPos)(data->strSrc);
  40. }
  41.  
  42. static ApAsSetLastPos(src, lastPos)
  43.   XtTextSource src;
  44.   XtTextPosition lastPos;
  45. {
  46. }
  47.  
  48. static int ApAsRead(src, pos, text, maxRead)
  49.   XtTextSource src;
  50.   int pos;
  51.   XtTextBlock *text;
  52.   int maxRead;
  53. {
  54.   ApAsSourceData *data;
  55.     data = (ApAsSourceData *)src->data;
  56.     return ((*data->strSrc->Read)(data->strSrc, pos, text, maxRead));
  57. }
  58.  
  59.  
  60. static Arg stringargs[] = {
  61.   {XtNstring, (XtArgVal) NULL},
  62.   {XtNlength, (XtArgVal) NULL},
  63.   {XtNeditType, (XtArgVal) XttextEdit},
  64.   };
  65.  
  66. static int ApAsReplace(src, startPos, endPos, text)
  67.   XtTextSource src;
  68.   XtTextPosition startPos, endPos;
  69.   XtTextBlock *text;
  70. {
  71.   ApAsSourceData *data;
  72.   int i;
  73.  
  74.     if (!allowedit) return 0;
  75.     data = (ApAsSourceData *)src->data;
  76.  
  77.     if((data->pos + text->length) >= data->size){
  78.         while((data->pos + text->length) >= data->size){
  79.             data->size += chunk;
  80.             data->buf = XtRealloc(data->buf, data->size);  /* optimize this!!! */
  81.         } 
  82.         XtStringSourceDestroy(data->strSrc);
  83.     stringargs[0].value = (XtArgVal)data->buf ;
  84.     stringargs[1].value = (XtArgVal)data->size ;
  85.         data->strSrc = (XtTextSource)
  86.           XtStringSourceCreate(toplevel,stringargs,XtNumber(stringargs));
  87.     }
  88.     i = (*data->strSrc->Replace)(data->strSrc, startPos, endPos, text);
  89.     data->pos += text->length;
  90.     return (i);
  91. }
  92.  
  93. static XtTextPosition ApAsScan (src, pos, sType, dir, count, include)
  94.   XtTextSource src;
  95.   XtTextPosition pos;
  96.   XtTextScanType sType;
  97.   XtTextScanDirection dir;
  98.   int count, include;
  99. {
  100.   ApAsSourceData *data;
  101.     data = (ApAsSourceData *)src->data;
  102.     return 
  103.      ((*data->strSrc->Scan)(data->strSrc, pos, sType, dir, count, include));
  104. }
  105.  
  106.  
  107. /* Public routines */
  108.  
  109. XtTextSource TCreateApAsSource ()
  110. {
  111.   XtTextSource src;
  112.   ApAsSourceData *data;
  113.     src = (XtTextSource) XtMalloc(sizeof(XtTextSourceRec));
  114.     src->Read = ApAsRead;
  115.     src->Replace = ApAsReplace; 
  116.     src->GetLastPos = ApAsGetLastPos;
  117.     src->SetLastPos = ApAsSetLastPos;
  118.     src->Scan = ApAsScan;
  119.     src->edit_mode = XttextEdit;
  120.     data = (ApAsSourceData *)(XtMalloc(sizeof(ApAsSourceData)));
  121.     data->buf = XtCalloc(chunk,1);
  122.     data->pos = 0;
  123.     data->size = chunk;
  124.     stringargs[0].value = (XtArgVal)data->buf ;
  125.     stringargs[1].value = (XtArgVal)data->size ;
  126.     data->strSrc = (XtTextSource) XtStringSourceCreate (toplevel,stringargs,
  127.                    XtNumber(stringargs));
  128.     src->data = (caddr_t)data;
  129.     return src;
  130. }
  131.  
  132. TDestroyApAsSource(src)
  133.   XtTextSource src;
  134. {
  135.   ApAsSourceData *data;
  136.     data = (ApAsSourceData *)src->data;
  137.     XtStringSourceDestroy(data->strSrc);
  138.     XtFree(data->buf);
  139.     XtFree(data);
  140.     XtFree(src);
  141. }
  142.