home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / classsource.lha / ClassSource / BOOPSI / support / boopsilib.c
Encoding:
C/C++ Source or Header  |  1995-02-12  |  12.7 KB  |  599 lines

  1. #include <classes/BOOPSI/boopsilib.h>
  2.  
  3. #pragma -
  4. #include <pragma/intuition_lib.h>
  5. #include <pragma/graphics_lib.h>
  6. #include <graphics/gfxmacros.h>
  7. #include <clib/alib_protos.h>
  8. #include <pragma/exec_lib.h>
  9. #pragma +
  10.  
  11. #include <string.h>
  12. #include <tools/str.h>
  13. #include <exec/libraries.h>
  14.  
  15. VOID SuperDrawImageState(struct RastPort *rp, struct Image *image,
  16.     struct Image *select, LONG dx, LONG dy, LONG width, LONG height,
  17.     ULONG state, struct DrawInfo *drawinfo)
  18. {
  19.  
  20.     static UWORD ditherData[] = { 0x1111,0x4444 };
  21.  
  22.     if (!image)
  23.         return;
  24.  
  25.     if (image->Depth == CUSTOMIMAGEDEPTH)
  26.     {
  27.         DrawImageState(rp,image,
  28.             dx + ((width - image->Width) >> 1),
  29.             dy + ((height - image->Height) >> 1),
  30.             state,drawinfo);
  31.     }
  32.     else {
  33.         LONG left;
  34.         LONG top;
  35.         switch (state)
  36.         {
  37.             case IDS_NORMAL:
  38.             case IDS_DISABLED:
  39.                 left = dx + ((width - image->Width) >> 1);
  40.                 top = dy + ((height - image->Height) >> 1);
  41.                 DrawImage(rp,image,left,top);
  42.                 break;
  43.             case IDS_SELECTED:
  44.             case IDS_SELECTEDDISABLED:
  45.                 if (select)
  46.                 {
  47.                     left = dx + ((width - select->Width) >> 1);
  48.                     top = dy + ((height - select->Height) >> 1);
  49.                     DrawImage(rp,select,left,top);
  50.                 }
  51.                 else {
  52.                     left = dx + ((width - image->Width) >> 1);
  53.                     top = dy + ((height - image->Height) >> 1);
  54.                     DrawImage(rp,image,left,top);
  55.                     SetDrMd(rp,COMPLEMENT);
  56.                     RectFill(rp,left,top,left+image->Width-1,top+image->Height-1);
  57.                 };
  58.                 break;
  59.         };
  60.         switch (state)
  61.         {
  62.             case IDS_DISABLED:
  63.             case IDS_SELECTEDDISABLED:
  64.                 SetDrMd(rp,JAM1);
  65.                 SetAfPt(rp,ditherData,1);
  66.                 RectFill(rp,dx,dy,dx+width-1,dy+height-1);
  67.                 SetAfPt(rp,NULL,0);
  68.                 break;
  69.         };
  70.     };
  71. }
  72.  
  73. // *************************************************************
  74.  
  75. ULONG SetGInfoAttrs(Object *o, struct GadgetInfo *gi, Tag tag1type, ...)
  76. {
  77.     return DoMethod(o,OM_SET,(struct TagItem *) &tag1type,gi);
  78. }
  79.  
  80. // *************************************************************
  81.  
  82. ULONG SetSuperGInfoAttrs(Class *cl, Object *o, struct GadgetInfo *gi,
  83.     Tag tag1type, ...)
  84. {
  85.     return DoSuperMethod(cl,o,OM_SET,(struct TagItem *) &tag1type,gi);
  86. }
  87.  
  88. // *************************************************************
  89.  
  90. ULONG UpdateGInfoAttrs(Object *o, struct GadgetInfo *gi, ULONG flags,
  91.     Tag tag1type, ...)
  92. {
  93.     return DoMethod(o,OM_UPDATE,(struct TagItem *) &tag1type,gi,flags);
  94. }
  95.  
  96. // *************************************************************
  97.  
  98. ULONG SetGadgetAttrsJB(struct Gadget *g, struct Window *w, struct Requester *r,
  99.     Tag tag1type, ...)
  100. {
  101.     return SetGadgetAttrsA(g,w,r,(struct TagItem *) &tag1type);
  102. }
  103.  
  104. // *************************************************************
  105.  
  106. VOID GetInnerFrame(struct Gadget *g, Object *frame, struct IBox *framebox,
  107.     WORD *frameright, WORD *framebottom)
  108. {
  109.     if (frame)
  110.     {
  111.         struct IBox box;
  112.         SetAttrs(frame,
  113.             IA_Left, 0,
  114.             IA_Top, 0,
  115.             IA_Width, g->Width,
  116.             IA_Height, g->Height,
  117.             TAG_END);
  118.         DoMethod(frame, IM_FRAMEBOX, GADGET_BOX(g), &box, NULL, 0);
  119.         if (framebox)
  120.         {
  121.             framebox->Left = g->LeftEdge - box.Left;
  122.             framebox->Top = g->TopEdge - box.Top;
  123.             framebox->Width = g->Width + g->Width - box.Width;
  124.             framebox->Height = g->Height + g->Height - box.Height;
  125.         };
  126.         if (frameright)
  127.             *frameright = box.Left + box.Width - g->Width - g->LeftEdge;
  128.         if (framebottom)
  129.             *framebottom = box.Top + box.Height - g->Height - g->TopEdge;
  130.     }
  131.     else {
  132.         if (framebox)
  133.         {
  134.             framebox->Left = 0;
  135.             framebox->Top = 0;
  136.             framebox->Width = g->Width;
  137.             framebox->Height = g->Height;
  138.         };
  139.         if (frameright)
  140.             *frameright = 0;
  141.         if (framebottom)
  142.             *framebottom = 0;
  143.     };
  144. }
  145.  
  146. // *************************************************************
  147.  
  148. VOID SuperText(struct RastPort *rp, STRPTR str,
  149.     struct TextFont *font, WORD x, WORD y, WORD width, WORD height,
  150.     WORD adjust, UBYTE mark)
  151. {
  152.     if (!rp || !str)
  153.         return;
  154.     if (font)
  155.         SetFont(rp,font);
  156.  
  157.     String s;
  158.     LONG markpos = -1;
  159.     if (mark != 0)
  160.     {
  161.         ULONG i = 0;
  162.         while (str[i] != 0)
  163.         {
  164.             if (str[i] == mark && markpos == -1)
  165.             {
  166.                 markpos = i;
  167.                 i++;
  168.             };
  169.             s += String(str[i]);
  170.             i++;
  171.         };
  172.     }
  173.     else {
  174.         s = str;
  175.     };
  176.  
  177.     struct TextExtent tex;
  178.     extern struct Library *GfxBase;
  179.     WORD w39 = width;
  180.     if (GfxBase->lib_Version < 39)
  181.         w39++;
  182.     ULONG ll = TextFit(rp,s,s.length(),&tex,NULL,1,w39,height);
  183.  
  184.     Move(rp, x - tex.te_Extent.MinX,
  185.         y - tex.te_Extent.MinY + (height - tex.te_Height + 1) / 2);
  186.     if (markpos == -1)
  187.     {
  188.         Text(rp,(STRPTR) s,ll);
  189.     }
  190.     else {
  191.         if (markpos > 0 && ll >= markpos)
  192.         {
  193.             Text(rp,(STRPTR) s,markpos);
  194.         };
  195.         ULONG enable = AskSoftStyle(rp);
  196.         SetSoftStyle(rp,FSF_UNDERLINED,enable);
  197.         Text(rp,((STRPTR) s) + markpos,1);
  198.         SetSoftStyle(rp,FS_NORMAL,enable);
  199.         if (ll > markpos + 1)
  200.         {
  201.             Text(rp,((STRPTR) s) + markpos + 1, ll - markpos - 1);
  202.         };
  203.     };
  204. }
  205.  
  206. // *************************************************************
  207.  
  208. LONG SuperTextLength(STRPTR str, struct TextAttr *tattr, UBYTE mark)
  209. {
  210.     if (!str)
  211.         return 0;
  212.     struct IntuiText it = { 0,0,0,0,0,NULL,NULL,NULL };
  213.     it.ITextFont = tattr;
  214.  
  215.     if (mark != 0)
  216.     {
  217.         String s;
  218.         LONG markpos = -1;
  219.         ULONG i = 0;
  220.         while (str[i] != 0)
  221.         {
  222.             if (str[i] == mark && markpos == -1)
  223.             {
  224.                 markpos = i;
  225.                 i++;
  226.             };
  227.             s += String(str[i]);
  228.             i++;
  229.         };
  230.         it.IText = (STRPTR) s;
  231.     }
  232.     else {
  233.         it.IText = str;
  234.     };
  235.  
  236.     return IntuiTextLength(&it);
  237. }
  238.  
  239. // *************************************************************
  240.  
  241. static VOID MeasureMark(struct LabelText *lt);
  242.  
  243. struct LabelText *AllocLText(STRPTR str, struct TextAttr *tattr,
  244.     UBYTE apen, UBYTE bpen, UBYTE drmd, UBYTE mark)
  245. {
  246.     if (!str)
  247.         return NULL;
  248.  
  249.     ULONG i = 0;
  250.     LONG markpos = -1;
  251.     while (str[i] != 0)
  252.     {
  253.         if (str[i] == mark && markpos == -1)
  254.         {
  255.             markpos = i;
  256.         };
  257.         i++;
  258.     };
  259.  
  260.     struct LabelText *lt = (struct LabelText *) AllocMem(sizeof(struct LabelText),0);
  261.     if (!lt)
  262.         return NULL;
  263.     lt->itext.FrontPen = apen;
  264.     lt->itext.BackPen = bpen;
  265.     lt->itext.DrawMode = drmd;
  266.     lt->itext.LeftEdge = 0;
  267.     lt->itext.TopEdge = 0;
  268.     lt->itext.ITextFont = tattr;
  269.     lt->itext.NextText = NULL;
  270.     lt->memsize = i+1;
  271.     lt->markpos = markpos;
  272.     lt->itext.IText = (UBYTE *) AllocMem(lt->memsize,0);
  273.     if (!lt->itext.IText)
  274.     {
  275.         FreeLText(lt);
  276.         return NULL;
  277.     };
  278.     ULONG j = 0;
  279.     ULONG k = 0;
  280.     do {
  281.         if (j == markpos)
  282.             j++;
  283.         lt->itext.IText[k] = str[j];
  284.         k++;
  285.     } while (str[j++] != 0);
  286.  
  287.     lt->length = IntuiTextLength(<->itext);
  288.  
  289.     MeasureMark(lt);
  290.  
  291.     return lt;
  292. }
  293.  
  294. VOID FreeLText(struct LabelText *lt)
  295. {
  296.     if (lt)
  297.     {
  298.         if (lt->itext.IText)
  299.             FreeMem(lt->itext.IText,lt->memsize);
  300.         FreeMem(lt,sizeof(struct LabelText));
  301.     };
  302. }
  303.  
  304. WORD MeasureLText(struct LabelText *lt)
  305. {
  306.     if (!lt)
  307.         return 0;
  308.     return lt->length;
  309. }
  310.  
  311. VOID ChangeLTextFont(struct LabelText *lt, struct TextAttr *tattr)
  312. {
  313.     if (!lt)
  314.         return;
  315.     lt->itext.ITextFont = tattr;
  316.     lt->length = IntuiTextLength(<->itext);
  317.     MeasureMark(lt);
  318. }
  319.  
  320. VOID ChangeLTextPens(struct LabelText *lt, UBYTE apen, UBYTE bpen, UBYTE drmd)
  321. {
  322.     if (!lt)
  323.         return;
  324.     lt->itext.FrontPen = apen;
  325.     lt->itext.BackPen = bpen;
  326.     lt->itext.DrawMode = drmd;
  327. }
  328.  
  329. VOID ChangeLText(struct LabelText *lt, STRPTR str, UBYTE mark)
  330. {
  331.     if (!lt || !str)
  332.         return;
  333.  
  334.     if (lt->itext.IText)
  335.     {
  336.         FreeMem(lt->itext.IText,lt->memsize);
  337.         lt->itext.IText = NULL;
  338.     };
  339.  
  340.     ULONG i = 0;
  341.     LONG markpos = -1;
  342.     while (str[i] != 0)
  343.     {
  344.         if (str[i] == mark && markpos == -1)
  345.         {
  346.             markpos = i;
  347.         };
  348.         i++;
  349.     };
  350.  
  351.     lt->memsize = i+1;
  352.     lt->markpos = markpos;
  353.     lt->itext.IText = (UBYTE *) AllocMem(lt->memsize,0);
  354.     if (!lt->itext.IText)
  355.     {
  356.         return;
  357.     };
  358.     ULONG j = 0;
  359.     ULONG k = 0;
  360.     do {
  361.         if (j == markpos)
  362.             j++;
  363.         lt->itext.IText[k] = str[j];
  364.         k++;
  365.     } while (str[j++] != 0);
  366.  
  367.     lt->length = IntuiTextLength(<->itext);
  368.  
  369.     MeasureMark(lt);
  370. }
  371.  
  372. VOID PrintLText(struct RastPort *rp, struct LabelText *lt,
  373.     WORD x, WORD y, WORD width, WORD height, WORD adjust)
  374. {
  375.     if (rp && lt)
  376.     {
  377.         WORD top = y + (height - lt->itext.ITextFont->ta_YSize + 1) / 2;
  378.         switch (adjust)
  379.         {
  380.             case STADJUST_Center:
  381.                 x += (width - lt->length) / 2;
  382.                 break;
  383.             case STADJUST_Right:
  384.                 x += width - lt->length;
  385.                 break;
  386.         };
  387.         PrintIText(rp,<->itext,x,top);
  388.         if (lt->markpos >= 0)
  389.         {
  390.             top += lt->itext.ITextFont->ta_YSize - 1;
  391.             Move(rp,x + lt->markX,top);
  392.             SetAPen(rp,lt->itext.FrontPen);
  393.             SetBPen(rp,lt->itext.BackPen);
  394.             SetDrMd(rp,lt->itext.DrawMode);
  395.             Draw(rp,x + lt->markX + lt->markWidth - 1,top);
  396.         };
  397.     };
  398. }
  399.  
  400. static VOID MeasureMark(struct LabelText *lt)
  401. {
  402.     if (lt->markpos >= 0)
  403.     {
  404.         char c = lt->itext.IText[lt->markpos];
  405.         lt->itext.IText[lt->markpos] = 0;
  406.         lt->markX = IntuiTextLength(<->itext);
  407.         lt->itext.IText[lt->markpos] = c;
  408.         char markbuffer[2];
  409.         markbuffer[0] = c;
  410.         markbuffer[1] = 0;
  411.         struct IntuiText it;
  412.         it.ITextFont = lt->itext.ITextFont;
  413.         it.IText = markbuffer;
  414.         lt->markWidth = IntuiTextLength(&it);
  415.     };
  416. };
  417.  
  418. // *************************************************************
  419.  
  420. WORD MeasureTextLength(STRPTR s, struct TextAttr *t)
  421. {
  422.     if (!s)
  423.         return 0;
  424.     struct IntuiText it;
  425.     it.ITextFont = t;
  426.     it.IText = s;
  427.     return IntuiTextLength(&it);
  428. }
  429.  
  430. VOID PrintClipped(struct RastPort *rp, STRPTR s,
  431.     WORD x, WORD y, WORD width, WORD height, WORD adjust)
  432. {
  433.     if (!s)
  434.         return;
  435.     struct TextExtent tex;
  436.     // ** hier könnte man unter <V39 width+1 übergeben
  437.     extern struct Library *GfxBase;
  438.     WORD w39 = width;
  439.     if (GfxBase->lib_Version < 39)
  440.         w39++;
  441.     ULONG ll = TextFit(rp,s,strlen(s),&tex,NULL,1,w39,height);
  442.     if (ll > 0)
  443.     {
  444.         WORD cy = y - tex.te_Extent.MinY + (height - tex.te_Height + 1) / 2;
  445.         switch (adjust)
  446.         {
  447.             case STADJUST_Left:
  448.                 Move(rp, x - tex.te_Extent.MinX,cy);
  449.                 break;
  450.             case STADJUST_Center:
  451.                 Move(rp, x - tex.te_Extent.MinX + (width - tex.te_Width) / 2,cy);
  452.                 break;
  453.             case STADJUST_Right:
  454.                 Move(rp, x - tex.te_Extent.MinX + width - tex.te_Width,cy);
  455.                 break;
  456.         };
  457.         Text(rp,s,ll);
  458.     };
  459. }
  460.  
  461. // *************************************************************
  462.  
  463. VOID drawOutlineImage(struct RastPort *rp, struct DrawData *v,
  464.     WORD x, WORD y, WORD w, WORD h, struct DrawInfo *di,
  465.     BOOL outline, BOOL plane, BOOL recessed, LONG state)
  466. {
  467. #define AREASIZE 200
  468.     struct TmpRas mytmpras;
  469.     struct AreaInfo myareainfo;
  470.     UWORD areadata[AREASIZE];
  471.     UWORD pens[9];
  472.     if (!rp || !v)
  473.         return;
  474.     PLANEPTR myplane = NULL;
  475.     if (!rp->TmpRas)
  476.     {
  477.         myplane = AllocRaster(rp->BitMap->BytesPerRow*8,rp->BitMap->Rows);
  478.         if (!myplane)
  479.             return;
  480.         InitTmpRas(&mytmpras,myplane,RASSIZE(rp->BitMap->BytesPerRow*8,
  481.             rp->BitMap->Rows));
  482.         rp->TmpRas = &mytmpras;
  483.     };
  484.     BOOL ownareainfo = FALSE;
  485.     if (!rp->AreaInfo)
  486.     {
  487.         InitArea(&myareainfo,areadata,AREASIZE / 5);
  488.         ownareainfo = TRUE;
  489.         rp->AreaInfo = &myareainfo;
  490.     };
  491.     /* register */ UWORD i;
  492.     for (i = 0; i < 9; i++)
  493.         pens[i] = di->dri_Pens[i];
  494.     if (recessed)
  495.     {
  496.         i = pens[SHINEPEN];
  497.         pens[SHINEPEN] = pens[SHADOWPEN];
  498.         pens[SHADOWPEN] = i;
  499.     };
  500.     if (state == IDS_SELECTED || state == IDS_INACTIVESELECTED
  501.         || state == IDS_SELECTEDDISABLED)
  502.     {
  503.         i = pens[SHINEPEN];
  504.         pens[SHINEPEN] = pens[SHADOWPEN];
  505.         pens[SHADOWPEN] = i;
  506.     };
  507.     struct DrawData *firstPoint = v;
  508.     while (v->command != DRAWCMD_End) {
  509.         switch (v->command)
  510.         {
  511.             case DRAWCMD_Move:
  512.                 AreaMove(rp,x+(v->c.move.x*w)/16384,y+(v->c.move.y*h)/16384);
  513.                 break;
  514.             case DRAWCMD_Draw:
  515.                 AreaDraw(rp,x+(v->c.draw.x*w)/16384,y+(v->c.draw.y*h)/16384);
  516.                 break;
  517.             case DRAWCMD_Attr:
  518.                 break;
  519.             case DRAWCMD_Fill:
  520.                 if (plane)
  521.                     SetAPen(rp,pens[v->c.fill.planepen])
  522.                 else
  523.                     SetAPen(rp,pens[v->c.fill.pen]);
  524.                 SetDrMd(rp,JAM2);
  525.                 BNDRYOFF(rp);
  526.                 AreaEnd(rp);
  527.                 if (outline)
  528.                 {
  529.                     BOOL noline = FALSE;
  530.                     BOOL closeLines = FALSE;
  531.                     WORD cx,cy;
  532.                     while (firstPoint != v+1) {
  533.                         switch (firstPoint->command)
  534.                         {
  535.                             case DRAWCMD_Move:
  536.                                 if (closeLines && !noline)
  537.                                     Draw(rp,cx,cy);
  538.                                 cx = x+(firstPoint->c.move.x*w)/16384;
  539.                                 cy = y+(firstPoint->c.move.y*h)/16384;
  540.                                 Move(rp,cx,cy);
  541.                                 closeLines = TRUE;
  542.                                 break;
  543.                             case DRAWCMD_Draw:
  544.                                 if (!closeLines)
  545.                                 {
  546.                                     cx = x+(firstPoint->c.draw.x*w)/16384;
  547.                                     cy = y+(firstPoint->c.draw.y*h)/16384;
  548.                                     closeLines = TRUE;
  549.                                 };
  550.                                 if (noline)
  551.                                     Move(rp,x+(firstPoint->c.draw.x*w)/16384,
  552.                                         y+(firstPoint->c.draw.y*h)/16384);
  553.                                 else
  554.                                     Draw(rp,x+(firstPoint->c.draw.x*w)/16384,
  555.                                         y+(firstPoint->c.draw.y*h)/16384);
  556.                                 break;
  557.                             case DRAWCMD_Attr:
  558.                                 if (firstPoint->c.attr.pen < 0)
  559.                                 {
  560.                                     noline = TRUE;
  561.                                 }
  562.                                 else {
  563.                                     if (plane)
  564.                                         SetAPen(rp,pens[firstPoint->c.attr.planepen])
  565.                                     else
  566.                                         SetAPen(rp,pens[firstPoint->c.attr.pen]);
  567.                                     noline = FALSE;
  568.                                 };
  569.                                 break;
  570.                             case DRAWCMD_Fill:
  571.                                 if (closeLines && !noline)
  572.                                     Draw(rp,cx,cy);
  573.                                 closeLines = FALSE;
  574.                                 break;
  575.                         };
  576.                         firstPoint++;
  577.                     };
  578.                 };
  579.                 firstPoint = v + 1;
  580.                 break;
  581.             case DRAWCMD_Image:
  582.                 break;
  583.         };
  584.         v++;
  585.     };
  586.     if (state == IDS_DISABLED || state == IDS_INACTIVEDISABLED
  587.         || state == IDS_SELECTEDDISABLED)
  588.     {
  589.     };
  590.     if (ownareainfo)
  591.         rp->AreaInfo = NULL;
  592.     if (myplane)
  593.     {
  594.         rp->TmpRas = NULL;
  595.         FreeRaster(myplane,rp->BitMap->BytesPerRow*8,rp->BitMap->Rows);
  596.     };
  597. }
  598.  
  599.