home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 124 / af124a.adf / af124a.lzx / WBStartup+V2.8 / Source / WBStartup+Prefs / CheckBoxListView.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  6KB  |  244 lines

  1. /* Thanks go out to David Swasbrook, who gave me an example  of how to use GTLZ_CallBack */
  2.  
  3. #include <exec/types.h>
  4. #include <intuition/intuition.h>
  5. #include <graphics/text.h>
  6. #include <clib/intuition_protos.h>
  7. #include <string.h>
  8. #include <intuition/imageclass.h>
  9. #include <libraries/gadtools.h>
  10. #include <clib/graphics_protos.h>
  11. #include <proto/intuition.h>
  12. #include <proto/graphics.h>
  13. #include <stdio.h>
  14. #include <graphics/gfxmacros.h>
  15.  
  16. #include "CheckBoxListView.h"
  17.  
  18. #include "WBStartup+Prefs.h"
  19.  
  20.  
  21. #define    LVRASTPORT    l->lvdm_RastPort
  22. #define    LVPEN        l->lvdm_DrawInfo->dri_Pens
  23. #define    XOFF        2
  24. WORD     xoff;        /* Offset to start printing text at */
  25. WORD    yoff;        /* Vertical offset */
  26. APTR    CheckMarkObj = NULL;
  27. BYTE    FillPen,FillTextPen;
  28. BYTE    BgPen,FgPen;
  29. struct IntuiText CheckBoxList_it = { 1,0,0,0,0,NULL, NULL, NULL};
  30. struct TextAttr ChecklistFont = { NULL,0,0 };
  31.  
  32. ULONG __chip Crosshatch = 0x5555AAAA;
  33. ULONG __chip SolidLine = 0xFFFFFFFF;
  34.  
  35.  
  36. void FillIntuiTextLen(struct IntuiText *it, char *source, char *buffer, ULONG len)
  37. {
  38.   /* Puts a string in it->IText which is less than or equal to len pixels long. */
  39.  
  40.   /* IntuitText *it  Must be preloaded (except IText) to determine the string pixel length  */
  41.   /* char *source    The string to start from.  */
  42.   /* char *buffer    The final string.  */
  43.   /* ULONG len       The # of pixels which the source string is cropped to.  */
  44.  
  45.   WORD lb, ub, mid, l;
  46.  
  47.   it->IText = source;
  48.   if (IntuiTextLength(it) <= len)
  49.     return;
  50.  
  51.   strcpy(buffer, source);
  52.  
  53.   lb = 0;
  54.   ub = strlen(buffer);
  55.  
  56.   it->IText = buffer;
  57.  
  58.   /* Is a binary search method for efficiency */
  59.     while (lb < ub)
  60.   {
  61.       mid = (ub-lb) / 2 + lb;
  62.       strncpy(buffer,source,mid);
  63.       strcpy(&buffer[mid],">");  /* Put a '>' and a NULL in the right place */
  64.       l = IntuiTextLength(it);
  65.  
  66.       if (l < len)
  67.     {
  68.           lb = mid;
  69.           if (lb == ub-1)
  70.         lb = ub;
  71.       }
  72.     else if (l > len)
  73.     {
  74.           ub = mid;
  75.       }
  76.     else
  77.       lb = ub;
  78.   }
  79. }
  80.  
  81.  
  82. BOOL CheckBoxList_CallBack_SetUp(WORD h, struct DrawInfo *di, struct IntuiText *it)
  83. {
  84.   /* Sets the global pen values, and creates/allocates the checkbox image to use in */
  85.   /* the list view. */
  86.   /* WORD h         The height if the list view cell */
  87.   /* DrawInfo *di   DrawInfo to use in the checkbox creation */
  88.   /* IntuiText *it  IntuiText defining the font which is used is the listview */
  89.   /* RETURNS  TRUE if the checkmark box was created, false otherwise */
  90.  
  91.     CheckBoxList_it.ITextFont = &ChecklistFont;
  92.     xoff = CheckBoxList_it.ITextFont->ta_YSize+6;            /* Width of checkbox */
  93.     yoff = (h-CheckBoxList_it.ITextFont->ta_YSize+1)/2;   /* Height of checkbox */
  94.  
  95.   /* Set global pen values */
  96.     FillTextPen = di->dri_Pens[FILLTEXTPEN];
  97.     FillPen = di->dri_Pens[FILLPEN];
  98.     FgPen = di->dri_Pens[TEXTPEN];
  99.     BgPen = di->dri_Pens[BACKGROUNDPEN];
  100.  
  101.   /* Create the check box image to use in the list view */
  102.     CheckMarkObj = NewObject(NULL, SYSICLASS,
  103.       IA_Width,xoff-3,
  104.       IA_Height,h-1,
  105.       SYSIA_Which,CHECKIMAGE,
  106.       SYSIA_DrawInfo,di,
  107.       TAG_DONE);
  108.  
  109.     if (CheckMarkObj)
  110.     return(TRUE);
  111.  
  112.     return(FALSE);
  113. }
  114.  
  115. void CheckBoxList_CallBack_CleanUp(void)
  116. {
  117.   /* Deallocate the checkbox image */
  118.  
  119.     if (CheckMarkObj)
  120.   {
  121.     DisposeObject(CheckMarkObj);
  122.     CheckMarkObj = NULL;
  123.   }
  124. }
  125.  
  126.  
  127. ULONG __saveds __asm CheckBoxList_CallBack(register __a0 struct Hook *hook, register __a1 struct LVDrawMsg *l, register __a2 struct Node *n)
  128. {
  129.   /* Render a cell in the listview in the cells current state */
  130.   /* Hook *hook   Hook structure */
  131.   /* LVDrawMsg *l  Listview message explaining cell state */
  132.   /* Node *n  Pointer to the WBSP_Node for the cell to draw */
  133.   /* RETURNS  LVCB_OK if the lv draw method is LV_DRAW, LVCB_UNKNOWN otherwise */
  134.  
  135.     ULONG res = LVCB_UNKNOWN;
  136.     BYTE pen;
  137.     UBYTE temp[256];
  138.     WORD x,y,w,h,ids;
  139.   UBYTE oldstyle;
  140.     struct IntuiText *it = &CheckBoxList_it;
  141.   char prioritystring[10];
  142.   char *ptr;
  143.  
  144.  
  145.     x = l->lvdm_Bounds.MinX;
  146.     y = l->lvdm_Bounds.MinY;
  147.     w = l->lvdm_Bounds.MaxX - x;
  148.     h = l->lvdm_Bounds.MaxY - y;
  149.  
  150.     if(l->lvdm_MethodID==LV_DRAW)
  151.   {
  152.         if(!(CheckMarkObj))
  153.       CheckBoxList_CallBack_SetUp(h,l->lvdm_DrawInfo,it);
  154.  
  155.     /* Clear The box with the back ground color */
  156.     SetAPen(LVRASTPORT,BgPen);
  157.     SetAfPt(LVRASTPORT, NULL, 0);
  158.     RectFill(LVRASTPORT,x,y,x+w,y+h);
  159.  
  160.         switch(l->lvdm_State)
  161.     {
  162.             case LVR_NORMAL:
  163.         break;
  164.             case LVR_SELECTED:
  165.         SetAPen(LVRASTPORT,FillPen);
  166.         SetAfPt(LVRASTPORT,(UWORD *)&Crosshatch,1);
  167.         RectFill(LVRASTPORT,x,y,x+w,y+h);
  168.         break;
  169.         }
  170.  
  171.         switch(l->lvdm_State)
  172.     {
  173.             case LVR_NORMAL: pen = FgPen; break;
  174.             case LVR_SELECTED: pen = FillTextPen; break;
  175.         }
  176.         it->FrontPen = pen;
  177.  
  178.     /* Draw vertical line before priority */
  179.     SetAPen(LVRASTPORT,LVPEN[SHINEPEN]);
  180.     Move(LVRASTPORT,x+PRIORITYEDGE,y-1); Draw(LVRASTPORT,x+PRIORITYEDGE,y+h);
  181.     SetAPen(LVRASTPORT,LVPEN[SHADOWPEN]);
  182.     Move(LVRASTPORT,x+PRIORITYEDGE+1,y); Draw(LVRASTPORT,x+PRIORITYEDGE+1,y+h+1);
  183.  
  184.     if (n->ln_Type == TITLE)
  185.     {
  186.           /* Draw horizontal line under title */
  187.           SetAPen(LVRASTPORT,LVPEN[SHINEPEN]);
  188.           Move(LVRASTPORT,x,y+h-1); Draw(LVRASTPORT,x+w,y+h-1);
  189.           SetAPen(LVRASTPORT,LVPEN[SHADOWPEN]);
  190.           Move(LVRASTPORT,x,y+h); Draw(LVRASTPORT,x+w,y+h);
  191.  
  192.       oldstyle = it->ITextFont->ta_Style;
  193.       it->ITextFont->ta_Style = FSF_BOLD;
  194.  
  195.       if (ptr=strchr(n->ln_Name,','))
  196.         *ptr=NULL;
  197.  
  198.       FillIntuiTextLen(it, n->ln_Name, &*temp, w-4-(w-PRIORITYEDGE));
  199.           PrintIText(LVRASTPORT, it, x+4, y+yoff);
  200.  
  201.       if (ptr)
  202.       {
  203.         FillIntuiTextLen(it, ptr+1, &*temp, w-6-PRIORITYEDGE);
  204.             PrintIText(LVRASTPORT, it, x+6+PRIORITYEDGE, y+yoff);
  205.         *ptr=',';
  206.       }
  207.  
  208.       it->ITextFont->ta_Style = oldstyle;
  209.     }
  210.     else  /* not a title */
  211.     {
  212.           FillIntuiTextLen(it,n->ln_Name,&*temp,w-xoff-4-(w-PRIORITYEDGE));
  213.           PrintIText(LVRASTPORT, it, x+xoff+4, y+yoff);
  214.  
  215.           if (n->ln_Type)
  216.         ids = IDS_SELECTED;
  217.           else
  218.         ids = IDS_NORMAL;
  219.  
  220.           DrawImageState(LVRASTPORT,CheckMarkObj,x+2,y+1,ids,l->lvdm_DrawInfo);
  221.  
  222.       /* Draw priority text */
  223.       stci_d(prioritystring,(int)n->ln_Pri);
  224.           FillIntuiTextLen(it,prioritystring,&*temp,w-6-PRIORITYEDGE);
  225.       PrintIText(LVRASTPORT, it, x+6+PRIORITYEDGE, y+yoff);
  226.     }
  227.  
  228.         res = LVCB_OK;
  229.     }
  230.  
  231.     return(res);
  232. }
  233.  
  234.  
  235.  
  236. void InitHook(struct Hook *hook, ULONG (*c_function)(), APTR userdata)
  237. {
  238.     hook->h_Entry    = c_function;
  239.     hook->h_SubEntry = NULL;
  240.     hook->h_Data    = userdata;
  241. }
  242.  
  243.  
  244.