home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / Triton / Source / src / text_prim.c < prev    next >
C/C++ Source or Header  |  1998-05-23  |  11KB  |  388 lines

  1. /*
  2.  *  OpenTriton -- A free release of the triton.library source code
  3.  *  Copyright (C) 1993-1998  Stefan Zeiger
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  text_prim.c - Text Primitives
  20.  *
  21.  */
  22.  
  23.  
  24. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////////////////////////////////////// Include our stuff //
  26. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. #define INTUI_V36_NAMES_ONLY
  29. #define TR_NOSUPPORT
  30. #define TR_THIS_IS_TRITON
  31. #define TR_EXTERNAL_ONLY
  32.  
  33. #include "include/libraries/triton.h"
  34. #include "include/clib/triton_protos.h"
  35. #include "prefs/Triton.h"
  36. #include "/internal.h"
  37.  
  38. #include "parts/define_classes.h"
  39.  
  40.  
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. ////////////////////////////////////////////////////////////////////////////////////////// Text Primitives //
  43. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44.  
  45. ULONG __regargs TR_SimplifyTextFlags(struct TR_Project *project, ULONG flags)
  46. {
  47.   if(flags&TRTX_TITLE)
  48.     {
  49.       flags&=(~TRTX_TITLE);
  50.       switch(((struct TR_AppPrefs *)(project->trp_App->tra_Prefs))->frames_title)
  51.     {
  52.     case 1: /* Highlight */
  53.       flags|=TRTX_HIGHLIGHT;
  54.       break;
  55.     case 2: /* Shadow */
  56.       flags|=TRTX_3D;
  57.     }
  58.     }
  59.   return flags;
  60. }
  61.  
  62.  
  63. /****** triton.library/TR_TextWidth ******
  64. *
  65. *   NAME    
  66. *    TR_TextWidth -- Returns text width. (V6)
  67. *
  68. *   SYNOPSIS
  69. *    Width = TR_TextWidth(Project, Text, Flags)
  70. *                         A0       A2    D0
  71. *
  72. *    ULONG TR_TextWidth(struct TR_Project *, STRPTR, ULONG);
  73. *
  74. *   FUNCTION
  75. *    Returns the width of a text string with the specified
  76. *    TRTX_* flags in the given project.
  77. *
  78. *   RESULT
  79. *    Width - The width of the text in pixels
  80. *
  81. *   SEE ALSO
  82. *    TR_TextHeight(), TR_PrintText()
  83. *
  84. ******/
  85.  
  86. ULONG __saveds __asm TR_TextWidth(register __a0 struct TR_Project *project,
  87.                   register __a2 STRPTR text,
  88.                   register __d0 ULONG flags)
  89. {
  90.   struct IntuiText itext;
  91.   ULONG width,linewidth,start,stop;
  92.   UBYTE underscore[2],sep;
  93.  
  94.   if(!text) return 0;
  95.  
  96.   flags=TR_SimplifyTextFlags(project,flags);
  97.   itext.ITextFont=project->trp_PropAttr;
  98.  
  99.   if(flags&TRTX_MULTILINE)
  100.     {
  101.       width=0;
  102.       for(start=0,stop=0;;stop++)
  103.     {
  104.       if(text[stop]=='\n' || text[stop]=='\r' || text[stop]=='\t' || text[stop]==0)
  105.         {
  106.           if(text[start]=='%') start+=2;
  107.           itext.IText=&(text[start]);
  108.           sep=text[stop];
  109.           text[stop]=0;
  110.           linewidth=(ULONG)IntuiTextLength(&itext);
  111.           width=max(width,linewidth);
  112.           text[stop]=sep;
  113.           start=stop+1;
  114.         }
  115.       if(!(text[stop])) break;
  116.     }
  117.     }
  118.   else
  119.     {
  120.       itext.IText=text;
  121.       width=(ULONG)IntuiTextLength(&itext);
  122.  
  123.       if((!(flags&TRTX_NOUNDERSCORE))&&(TR_FirstOccurance(project->trp_Underscore,text)!=-1))
  124.     {
  125.       underscore[0]=project->trp_Underscore;
  126.       underscore[1]=0;
  127.       itext.IText=underscore;
  128.       width-=(ULONG)IntuiTextLength(&itext);
  129.     }
  130.  
  131.       if(flags&(TRTX_3D|TRTX_BOLD)) width++;
  132.     }
  133.  
  134.   return width;
  135. }
  136.  
  137.  
  138. /****** triton.library/TR_TextHeight ******
  139. *
  140. *   NAME    
  141. *    TR_TextWidth -- Returns text height. (V6)
  142. *
  143. *   SYNOPSIS
  144. *    Height = TR_TextHeight(Project, Text, Flags)
  145. *                           A0       A2    D0
  146. *
  147. *    ULONG TR_TextHeight(struct TR_Project *, STRPTR, ULONG);
  148. *
  149. *   FUNCTION
  150. *    Returns the height of a text string with the specified
  151. *    TRTX_* flags in the given project.
  152. *
  153. *   RESULT
  154. *    Height - The height of the text in pixels
  155. *
  156. *   SEE ALSO
  157. *    TR_TextWidth(), TR_PrintText()
  158. *
  159. ******/
  160.  
  161. ULONG __saveds __asm TR_TextHeight(register __a0 struct TR_Project *project,
  162.                    register __a2 STRPTR text,
  163.                    register __d0 ULONG flags)
  164. {
  165.   ULONG height;
  166.  
  167.   if(flags&TRTX_MULTILINE)
  168.     {
  169.       height=project->trp_TotalPropFontHeight
  170.     +TR_NumOccurances('\n',text) /* Small space */
  171.     *(project->trp_TotalPropFontHeight+((project->trp_PropFont->tf_YSize)/4))
  172.     +TR_NumOccurances('\r',text) /* Normal space */
  173.     *(project->trp_TotalPropFontHeight+((project->trp_PropFont->tf_YSize)/2))
  174.     +TR_NumOccurances('\t',text) /* Normal space + separator + normal space */
  175.     *(project->trp_TotalPropFontHeight
  176.       +((project->trp_PropFont->tf_YSize)/2)
  177.       +2
  178.       +((project->trp_PropFont->tf_YSize)/2));
  179.     }
  180.   else
  181.     {
  182.       flags=TR_SimplifyTextFlags(project,flags);
  183.       height=project->trp_TotalPropFontHeight;
  184.       if(flags&TRTX_3D) height++;
  185.     }
  186.  
  187.   return height;
  188. }
  189.  
  190.  
  191. VOID __regargs TR_PrintSimpleText(struct TR_Project *project, struct RastPort *rp,
  192.                   STRPTR text, ULONG textlength,
  193.                   ULONG x, ULONG y, ULONG color,
  194.                   ULONG uscorecolor, LONG underscore_pos)
  195. {
  196.   WORD underx1,underx2;
  197.  
  198.   Move(rp,x,y+project->trp_PropFont->tf_Baseline);
  199.   SetAPen(rp,color);
  200.  
  201.   if(underscore_pos==-1)
  202.   {
  203.     Text(rp,text,textlength);
  204.   }
  205.   else
  206.   {
  207.     Text(rp,text,underscore_pos);
  208.     underx1=rp->cp_x;
  209.     Text(rp,text+underscore_pos+1,1);
  210.     underx2=rp->cp_x-1;
  211.     Text(rp,text+underscore_pos+2,textlength-underscore_pos-2);
  212.     SetAPen(rp,uscorecolor);
  213.     Move(rp,underx1,y+project->trp_PropFont->tf_Baseline+2);
  214.     Draw(rp,underx2,y+project->trp_PropFont->tf_Baseline+2);
  215.   }
  216. }
  217.  
  218.  
  219. /****** triton.library/TR_PrintText ******
  220. *
  221. *   NAME    
  222. *    TR_PrintText -- Prints a line of text. (V6)
  223. *
  224. *   SYNOPSIS
  225. *    TR_PrintText(Project, RastPort, Text, X, Y, Width, Flags)
  226. *                 A0       A1        A2    D1 D2 D3     D0
  227. *
  228. *    VOID TR_PrintText(struct TR_Project *,
  229. *                      struct RastPort *, STRPTR,
  230. *                      ULONG, ULONG, ULONG, ULONG);
  231. *
  232. *   FUNCTION
  233. *    Prints a text into the specified RastPort (or into
  234. *    the project's default RastPort if the supplied RastPort
  235. *    is NULL.
  236. *
  237. *    If you specify TRTX_MULTILINE some formatting sequences
  238. *    may appear in the text:
  239. *    - A <newline> (\n) will start a new line with a small
  240. *      space above it.
  241. *    - A <return> (\r) will add a normal space instead.
  242. *    - A <tab> (\t) will add a normal space, then a 3D separator
  243. *      line and again a normal space.
  244. *
  245. *    The following sequences are allowed at the beginning of
  246. *    a line only:
  247. *    - '%b' switches to boldface,
  248. *    - '%3' and '%s' (V2+) to 3D text,
  249. *    - '%h' to highlight
  250. *    - and '%n' to normal style.
  251. *    - '%%' inserts a '%' character.
  252. *
  253. *   SEE ALSO
  254. *    TR_TextWidth()
  255. *
  256. ******/
  257.  
  258. VOID __asm TR_InternalPrintText(register __a0 struct TR_Project *project,
  259.                 register __a1 struct RastPort *rp,
  260.                 register __a2 STRPTR text,
  261.                 register __d1 ULONG x, register __d2 ULONG y,
  262.                 register __d3 width, register __d0 ULONG flags)
  263. {
  264.   ULONG textlength=TR_FirstOccurance(0,text);
  265.   LONG underscore_pos=-1;
  266.   ULONG color,uscorecolor,start,stop,ynow;
  267.   UBYTE sep;
  268.  
  269.   if(!rp) rp=project->trp_Window->RPort;
  270.  
  271.   flags=TR_SimplifyTextFlags(project,flags);
  272.  
  273.   if(!width) width=TR_TextWidth(project,text,flags);
  274.  
  275.   if(flags&TRTX_MULTILINE)
  276.     {
  277.       for(start=0,stop=0,ynow=y;;stop++)
  278.     {
  279.       if(text[stop]=='\n' || text[stop]=='\r' || text[stop]=='\t' || text[stop]==0)
  280.         {
  281.           if(text[start]=='%')
  282.         {
  283.           flags&=~(TRTX_3D|TRTX_BOLD|TRTX_HIGHLIGHT);
  284.           switch(text[start+1])
  285.             {
  286.             case '3':
  287.             case 's':
  288.               flags|=TRTX_3D;
  289.               break;
  290.             case 'b':
  291.               flags|=TRTX_BOLD;
  292.               break;
  293.             case 'h':
  294.               flags|=TRTX_HIGHLIGHT;
  295.               break;
  296.             case '%':
  297.               start--;
  298.             }
  299.           start+=2;
  300.         }
  301.           sep=text[stop];
  302.           text[stop]=0;
  303.           TR_InternalPrintText(project,rp,&(text[start]),x,ynow,width,flags&(~TRTX_MULTILINE));
  304.           text[stop]=sep;
  305.           start=stop+1;
  306.           ynow+=project->trp_TotalPropFontHeight;
  307.           switch(sep)
  308.         {
  309.         case '\n':
  310.           ynow+=(project->trp_PropFont->tf_YSize)/4;
  311.           break;
  312.         case '\r':
  313.           ynow+=(project->trp_PropFont->tf_YSize)/2;
  314.           break;
  315.         case '\t':
  316.           ynow+=((project->trp_PropFont->tf_YSize)/2);
  317.  
  318.           SetAPen(rp,project->trp_DrawInfo->dri_Pens[SHADOWPEN]);
  319.           Move(rp,x,ynow);
  320.           Draw(rp,x+width-1,ynow);
  321.           SetAPen(rp,project->trp_DrawInfo->dri_Pens[SHINEPEN]);
  322.           Move(rp,x,ynow+1);
  323.           Draw(rp,x+width-1,ynow+1);
  324.  
  325.           Draw(rp,x+width-1,ynow);
  326.           SetAPen(rp,project->trp_DrawInfo->dri_Pens[SHADOWPEN]);
  327.           Move(rp,x,ynow);
  328.           Draw(rp,x,ynow+1);
  329.  
  330.           ynow+=2+((project->trp_PropFont->tf_YSize)/2);
  331.         }
  332.         }
  333.       if(!(text[stop])) break;
  334.     }
  335.     }
  336.   else /* not TRTX_MULTILINE */
  337.     {
  338.       SetFont(rp,project->trp_PropFont);
  339.       SetDrMd(rp,JAM1);
  340.  
  341.       if(flags&TRTX_BOLD) SetSoftStyle(rp,FSF_BOLD,FSF_BOLD);
  342.  
  343.       if(!(flags&TRTX_NOUNDERSCORE)) underscore_pos=TR_FirstOccurance(project->trp_Underscore,text);
  344.  
  345.       if(flags&TRTX_RIGHTALIGN) x=x+width-TR_TextWidth(project,text,flags);
  346.       else if(flags&TRTX_CENTER) x=x+((width-TR_TextWidth(project,text,flags))/2);
  347.  
  348.       if(flags&TRTX_3D)
  349.     {
  350.       TR_PrintSimpleText(project, rp, text, textlength, x+1, y+1,
  351.                  TR_GetPen(project,TRPT_SYSTEMPEN,SHADOWPEN),
  352.                  TR_GetPen(project,TRPT_SYSTEMPEN,SHADOWPEN), underscore_pos);
  353.       TR_PrintSimpleText(project, rp, text, textlength, x, y,
  354.                  TR_GetPen(project,TRPT_SYSTEMPEN,HIGHLIGHTTEXTPEN),
  355.                  TR_GetPen(project,TRPT_SYSTEMPEN,HIGHLIGHTTEXTPEN), underscore_pos);
  356.     }
  357.       else
  358.     {
  359.       if(flags&TRTX_HIGHLIGHT)
  360.         {
  361.           color=TR_GetPen(project,TRPT_SYSTEMPEN,HIGHLIGHTTEXTPEN);
  362.           uscorecolor=TR_GetPen(project,TRPT_TRITONPEN,TRTP_HIGHUSCORE);
  363.         }
  364.       else if(flags&TRTX_SELECTED)
  365.         {
  366.           color=TR_GetPen(project,TRPT_SYSTEMPEN,FILLTEXTPEN);
  367.           uscorecolor=color;
  368.         }
  369.       else
  370.         {
  371.           color=TR_GetPen(project,TRPT_SYSTEMPEN,TEXTPEN);
  372.           uscorecolor=TR_GetPen(project,TRPT_TRITONPEN,TRTP_NORMUSCORE);
  373.         }
  374.       TR_PrintSimpleText(project, rp, text, textlength, x, y, color, uscorecolor, underscore_pos);
  375.     }
  376.     }
  377. }
  378.  
  379.  
  380. VOID __saveds __asm TR_PrintText(register __a0 struct TR_Project *project,
  381.                  register __a1 struct RastPort *rp,
  382.                  register __a2 STRPTR text,
  383.                  register __d1 ULONG x, register __d2 ULONG y,
  384.                  register __d3 ULONG width, register __d0 ULONG flags)
  385. {
  386.   TR_InternalPrintText(project,rp,text,x,y,width,flags);
  387. }
  388.