home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Developer Kits / Quark XTLite / CopyDesk XTLite / Sample XTLite Bulb / samplextlite.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-16  |  11.5 KB  |  427 lines  |  [TEXT/KAHL]

  1. /**************************************************************************\
  2. **
  3. **  samplextlite.c
  4. **
  5. **  Sample XTLite bulb.
  6. **
  7. **
  8. **  Copyright © 1993,1994 Quark Incorporated
  9. **  All Rights Reserved
  10. **
  11. \**************************************************************************/
  12.  
  13. #include "XTLite.h"
  14.  
  15. #define DOIMPORTFILTER    1
  16. #define DOEXPORTFILTER    1
  17. #define DOMENUITEM        1
  18. #define DOIDLELOOP        1
  19.  
  20. /* function prototypes */
  21. uchar readchar(int16);
  22.  
  23. #define STRINGRESID    20000
  24. #define IMPORTSTRID    1
  25. #define EXPORTSTRID    2
  26. #define SUFFIXSTRID    3
  27. #define MENUSTRID        4
  28.  
  29. #if DOIMPORTFILTER || DOEXPORTFILTER
  30. #define OURTEXTCREATOR 'ABCX'    /* creator of our text file */
  31. #define OURTEXTTYPE 'MYTX'        /* ResType of our text file */
  32. #define BOLDCHAR '\\'
  33. #define BUFSIZE 1000L            /* size of temp buffer for reading in text file */
  34.  
  35. bool8 boldflag;                    /* flag telling if we're currently handling bold text */
  36. uchar boldchar = BOLDCHAR;
  37. int32 remaining;                    /* num of chars remaining in file buffer */
  38. int32 doffset;                        /* offset of char in buffer */
  39. uchar *filebuffer;                /* holds characters */
  40. filtertxtattrib *mytextattr;    /* text attributes */
  41. filterparaattrib *myparaattr;    /* paragraph attributes */
  42. RGBColor thetextcolor;            /* RGB value for text coloring */
  43. static int16 maxtabs;
  44.  
  45. /**************************************************************************\
  46. **
  47. **  setupfilter
  48. **
  49. **  Setup the filter information.
  50. **
  51. **  Entry: variables to recieve information.
  52. **
  53. **  Exit:  variables filled.
  54. **
  55. \**************************************************************************/
  56. void setupfilter(OSType *fcreator,OSType *ftype,bool8 *importok,bool8 *exportok,
  57.     Str255 getstr,Str255 savestr,Str255 suffixstr)
  58. {
  59.     *fcreator = OURTEXTCREATOR;            /* set up file creator information */
  60.     *ftype = OURTEXTTYPE;                    /* set up file type information */
  61.     *importok = *exportok = TRUE;            /* we can import and export this format */
  62.     GetIndString(getstr,STRINGRESID,IMPORTSTRID);        /* get "Get Text" String */
  63.     GetIndString(savestr,STRINGRESID,EXPORTSTRID);        /* get "Save Text" String */
  64.     GetIndString(suffixstr,STRINGRESID,SUFFIXSTRID);    /* get ".suffix" String (Optional) */
  65.     
  66.     maxtabs = maxnumoftabs();    /* get maximum number of tabs */
  67. }
  68. #else        /* DOIMPORTFILTER || DOEXPORTFILTER */
  69. void setupfilter(OSType *fcreator,OSType *ftype,bool8 *importok,bool8 *exportok,
  70.     Str255 getstr,Str255 savestr,Str255 suffixstr)
  71. {
  72.     *importok = *exportok = FALSE;
  73. }
  74. #endif    /* DOIMPORTFILTER || DOEXPORTFILTER */
  75.  
  76.  
  77.  
  78. #if DOIMPORTFILTER
  79. /**************************************************************************\
  80. **
  81. **  readchar
  82. **
  83. **  Read a character from the file buffer.
  84. **
  85. **  Entry: fnum - the file reference number.
  86. **
  87. **  Exit:  character.
  88. **
  89. \**************************************************************************/
  90. uchar readchar(int16 fnum)
  91. {
  92.     int16 err;
  93.  
  94.     if (remaining == 0) {         /* if buffer is empty then read a buffer full */
  95.         remaining = BUFSIZE;
  96.         err = FSRead(fnum,&remaining,filebuffer);
  97.         if (err && !remaining) {
  98.             if (err != eofErr) return (err);
  99.             return (0xFF);
  100.         }
  101.         doffset = 0;
  102.     }
  103.     remaining--;
  104.     return(filebuffer[doffset++]);
  105. }
  106.  
  107. /**************************************************************************\
  108. **
  109. **  startread
  110. **
  111. **  Initialize variables for start of text import.
  112. **
  113. **  Entry: fnum - the file reference number.
  114. **
  115. **  Exit:  None.
  116. **
  117. \**************************************************************************/
  118. void startread(int16 fnum)
  119. {
  120.     int16 i;
  121.     
  122.     mytextattr = (filtertxtattribptr)NewPtr(sizeof(filtertxtattrib));
  123.     myparaattr = (filterparaattribptr)NewPtr(sizeof(filterparaattrib)+maxtabs*sizeof(filtertabspec));
  124.     
  125.     mytextattr->font = helvetica;        /* set text to 12 point helvetica */
  126.     mytextattr->size = 12L<<16;        /* point size is in fixed notation, so we << 16 */
  127.     mytextattr->kern = 0L;                /* auto kerning */
  128.     mytextattr->shade = 0x00010000;    /* 100% shade color */
  129.     mytextattr->hscale = 0x00010000;    /* 100% horizontal scaled text */
  130.     mytextattr->track = 0L;
  131.     
  132.     myparaattr->relative = FALSE;        /* not relative leading */
  133.     myparaattr->just = LEFT;            /* left justified text */
  134.     myparaattr->leftindent = 0L;        /* no left indent */
  135.     myparaattr->firstindent = 0L;        /* no first line of paragraph indent */
  136.     myparaattr->rightindent = 0L;        /* no right indent */
  137.     myparaattr->leading = 0L;            /* 0 means auto leading */
  138.     myparaattr->spcbefore = 0L;        /* no space before or after paragraphs */
  139.     myparaattr->spcafter = 0L;
  140.  
  141.     for (i = 0; i < maxtabs; i++)    /* tabs every .5 inches (all -1L means default) */
  142.         myparaattr->tabs[i].tabindent = -1L;
  143.         
  144.     thetextcolor.red = 0;                /* text color is black */
  145.     thetextcolor.green = 0;
  146.     thetextcolor.blue = 0;
  147.     
  148.     filebuffer = (uchar *)NewPtr(BUFSIZE);
  149.     remaining = doffset = 0;
  150.     boldflag = FALSE;
  151. }
  152.  
  153. /**************************************************************************\
  154. **
  155. **  readtext
  156. **
  157. **  Read a section of text for text import.
  158. **
  159. **  Entry: variables to recieve information.
  160. **
  161. **  Exit:  variables filled.
  162. **
  163. \**************************************************************************/
  164. void readtext(int16 fnum,Str255 textbuffer,int32 *count,filtertxtattrib *textattribs,
  165.     filterparaattrib *paraattribs,RGBColor *textcolor)
  166. {
  167.     char ch;
  168.     int16 i;
  169.             
  170.     i = 0;
  171.     mytextattr->face = boldflag ? bold : 0;
  172.     while (TRUE) {
  173.         ch = readchar(fnum);
  174.         if (ch == BOLDCHAR) {
  175.             /* handle change of text attributes */
  176.             boldflag = !boldflag;
  177.             /* break at the end of a run of characters */
  178.             if (i) break;
  179.             mytextattr->face = boldflag ? bold : 0;
  180.         }
  181.         if (ch >= ' ' || ch == '\t' || ch == '\r')
  182.             textbuffer[i++] = ch;
  183.         if (ch == '\r' || i == 256) break;
  184.         else if (ch == -1) break;
  185.     }
  186.     /* set the text attributes before returning */    
  187.     textattribs->font = mytextattr->font;
  188.     textattribs->face = mytextattr->face;
  189.     textattribs->size = mytextattr->size;
  190.     textattribs->hscale = mytextattr->hscale;
  191.     textattribs->shade = mytextattr->shade;
  192.     textattribs->kern = mytextattr->kern;
  193.     textattribs->track = mytextattr->track;
  194.  
  195.     paraattribs->relative = myparaattr->relative;
  196.     paraattribs->just = myparaattr->just;
  197.     paraattribs->leftindent = myparaattr->leftindent;
  198.     paraattribs->firstindent = myparaattr->firstindent;
  199.     paraattribs->rightindent = myparaattr->rightindent;
  200.     paraattribs->leading = myparaattr->leading;
  201.     paraattribs->spcbefore = myparaattr->spcbefore;
  202.     paraattribs->spcafter = myparaattr->spcafter;
  203.     BlockMove(paraattribs->tabs,myparaattr->tabs,maxtabs*sizeof(filtertabspec));
  204.  
  205.     textcolor->red = thetextcolor.red;
  206.     textcolor->green = thetextcolor.green;
  207.     textcolor->blue = thetextcolor.blue;
  208.     
  209.     *count = i;    /* tell QuarkXPress how many characters are in the text buffer */
  210. }
  211.  
  212. /**************************************************************************\
  213. **
  214. **  endread
  215. **
  216. **  De-initialize variables for end of text import.
  217. **
  218. **  Entry: fnum - the file reference number.
  219. **
  220. **  Exit:  None.
  221. **
  222. \**************************************************************************/
  223. void endread(int16 fnum)
  224. {
  225.     if (filebuffer) DisposPtr((Ptr)filebuffer);
  226.     DisposePtr((Ptr)mytextattr);
  227.     DisposePtr((Ptr)myparaattr);
  228. }
  229. #else        /* DOIMPORTFILTER */
  230. uchar readchar(int16 fnum) {}
  231. void startread(int16 fnum) {}
  232. void readtext(int16 fnum,Str255 textbuffer,int32 *count,filtertxtattrib *textattribs,
  233.     filterparaattrib *paraattribs,RGBColor *textcolor) {}
  234. void endread(int16 fnum) {}
  235. #endif    /* DOIMPORTFILTER */
  236.  
  237.  
  238.  
  239. #if DOEXPORTFILTER
  240. /**************************************************************************\
  241. **
  242. **  startwrite
  243. **
  244. **  Initialize variables for start of text export.
  245. **
  246. **  Entry: fnum - the file reference number.
  247. **
  248. **  Exit:  None.
  249. **
  250. \**************************************************************************/
  251. void startwrite(int16 fnum)
  252. {
  253.     boldflag = FALSE;
  254. }
  255.  
  256. /**************************************************************************\
  257. **
  258. **  writetext
  259. **
  260. **  Write a section of text for text export.
  261. **
  262. **  Entry: variables to recieve information.
  263. **
  264. **  Exit:  variables filled.
  265. **
  266. \**************************************************************************/
  267. void writetext(int16 fnum,Str255 textbuffer,int32 count,filtertxtattrib *textattribs,
  268.     filterparaattrib *paraattribs,RGBColor *textcolor)
  269. {
  270.     char ch;
  271.     int16 i,j;
  272.     int32 size;
  273.     int16 fserr;
  274.     Str255 tempbuffer;
  275.     
  276.     if (boldflag != ((textattribs->face&bold) != 0)) {
  277.         boldflag = !boldflag;
  278.         size = 1;
  279.         fserr = FSWrite(fnum,&size,&boldchar);
  280.     }
  281.     for (j = i = 0; i < count; i++) {
  282.         ch = textbuffer[i];
  283.         if (ch >= ' ' || ch == TABCHAR || ISSPECIAL(ch)) tempbuffer[j++] = ch;
  284.         else if (ISLINECHAR(ch)) tempbuffer[j++] = RETURNCHAR;
  285.     }
  286.     size = j;
  287.     fserr = FSWrite(fnum,&size,tempbuffer);
  288. }
  289.  
  290. /**************************************************************************\
  291. **
  292. **  endwrite
  293. **
  294. **  De-initialize variables for end of text export.
  295. **
  296. **  Entry: fnum - the file reference number.
  297. **
  298. **  Exit:  None.
  299. **
  300. \**************************************************************************/
  301. void endwrite(int16 fnum)
  302. {
  303.     int32 size;
  304.     int16 fserr;
  305.         
  306.     if (boldflag) {
  307.         size = 1;
  308.         fserr = FSWrite(fnum,&size,&boldchar);
  309.     }
  310. }
  311. #else        /* DOEXPORTFILTER */
  312. void startwrite(int16 fnum) {}
  313. void writetext(int16 fnum,Str255 textbuffer,int32 count,filtertxtattrib *textattribs,
  314.     filterparaattrib *paraattribs,RGBColor *textcolor) {}
  315. void endwrite(int16 fnum) {}
  316. #endif    /* DOEXPORTFILTER */
  317.  
  318.  
  319.  
  320. #if DOMENUITEM
  321. /**************************************************************************\
  322. **
  323. **  setupmenu
  324. **
  325. **  Setup for adding a menu item to the Utilities menu.
  326. **
  327. **  Entry: Storage for the text of the menu item.
  328. **
  329. **  Exit:  TRUE if you want to add your own menu item; FALSE if not.
  330. **
  331. \**************************************************************************/
  332. bool8 setupmenu(Str255 menustr)
  333. {
  334.     GetIndString(menustr,STRINGRESID,MENUSTRID);    /* get "Menu" String */
  335.     return (TRUE);
  336. }
  337.  
  338. void menucall(void)
  339. {
  340.     /* Put you own code here! */
  341.     SysBeep(1);
  342. }
  343. #else        /* DOMENUITEM */
  344. bool8 setupmenu(Str255 menustr)
  345. {
  346.     return (FALSE);
  347. }
  348. void menucall(void) {}
  349. #endif    /* DOMENUITEM */
  350.  
  351.  
  352.  
  353. #if DOIDLELOOP
  354. /**************************************************************************\
  355. **
  356. **  setupidle
  357. **
  358. **  Setup for handling Idle calls.
  359. **
  360. **  Entry: None.
  361. **
  362. **  Exit:  TRUE if you want to handle Idle calls; FALSE if not.
  363. **
  364. \**************************************************************************/
  365. bool8 setupidle(void)
  366. {
  367.     return (TRUE);
  368. }
  369.  
  370. void idlecall(EventRecord *myevent)
  371. {
  372.     register int32 c;
  373.     int32 start,end,amount,textlen;
  374.     Fixed kern;
  375.     int16 character;
  376.     uchar ch;
  377.     bool8 plus;
  378.  
  379. #define PLUSTRACKCHAR (30<<8)        /* the + track value character "}" */
  380. #define MINUSTRACKCHAR (33<<8)    /* the - track value character "{" */
  381.  
  382.     /* Put you own code here! */
  383.     character = (*myevent).message&keyCodeMask;
  384.     if (((*myevent).what == keyDown || (*myevent).what == autoKey)
  385.         && ((*myevent).modifiers&(controlKey+shiftKey+cmdKey)) == controlKey+shiftKey+cmdKey
  386.         && (character == PLUSTRACKCHAR || character == MINUSTRACKCHAR)) {
  387.         if (istextboxcurrent()) {
  388.             gettextinfo(&start,&end,&textlen);
  389.             if (start != end) {
  390.                 plus = character == PLUSTRACKCHAR;
  391.                 amount = ((*myevent).modifiers&optionKey) ? 1L<<16: 10L<<16;
  392.                 turnofftextselection();
  393.                 for (c = start; c < end; c++) {
  394.                     gettext(c,1L,&ch);
  395.                     if (ch == ' ' && c != textlen) {
  396.                         gettextattribute(T_KERN,&kern,c,c+1);
  397.                         if (plus) {
  398.                             kern += amount;
  399.                             if (!settextattribute(T_KERN,kern,c,c+1,FALSE)) {
  400.                                 SysBeep(1);
  401.                                 break;
  402.                             }
  403.                         }
  404.                         else {
  405.                             kern -= amount;
  406.                             if (!settextattribute(T_KERN,kern,c,c+1,FALSE)) {
  407.                                 SysBeep(1);
  408.                                 break;
  409.                             }
  410.                         }
  411.                     }
  412.                 }
  413.                 settextselection(start,end);
  414.             }
  415.             else SysBeep(1);
  416.             (*myevent).what = nullEvent;
  417.         }
  418.     }
  419. }
  420. #else        /* DOIDLELOOP */
  421. bool8 setupidle(void)
  422. {
  423.     return (FALSE);
  424. }
  425. void idlecall(EventRecord *myevent) {}
  426. #endif    /* DOIDLELOOP */
  427.