TwoColLDEF.c

/*
    File:       TwoColLDEF.c
 
    Contains:   Two-column LDEF code resource
    
                looks for a comma in the text of each cell and draws
                the text that follows the comma half-way across the cell
    
                so a cell containing the text
                +--------------+
                | abc,def      |
                +--------------+
                will instead appear as
                +--------------+
                | abc   def    |
                +--------------+
        
                To see this LDEF in action, paste it into the ModalList
                sample program and recompile the program so that the LNew call uses
                the definition procedure 128 rather than 0
 
    Written by: Greg Robbins    
 
    Copyright:  Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include <Lists.h>
#include <ToolUtils.h>
#include <String.h>
#include <LowMem.h>
 
/* constants for spacing */
 
#define kLeftOffset 2
#define kTopOffset  0
 
 
/* main LDEF entry point */
 
pascal void main(short lMessage,Boolean lSelect,Rect *lRect,Cell lCell,
                short lDataOffset,short lDataLen,ListHandle lHandle)
{
    FontInfo fontInfo;                      /* font information (ascent/descent/etc)  */
    ListPtr listPtr;                        /* pointer to store dereferenced list     */
    SignedByte hStateList,hStateCells;      /* state variables for HGetState/SetState */
    Ptr cellData;                           /* points to start of cell data for list  */
    short leftDraw,topDraw;                 /* left/top offsets from topleft of cell  */
    short halfwayLeftDraw;                  /* half way across from left of cell      */
    short substringLen;                     /* number of characters before comma      */
    Ptr commaPtr;                           /* substring beginning at comma           */
    Point currPenPt;                        /* current pen location                   */
    Rect fullCellRect;                      /* unclipped size of cell                 */
    
    /* lock and dereference list mgr handles */
    
    hStateList = HGetState((Handle) lHandle);
    HLock((Handle) lHandle);
    listPtr = *lHandle;
    
    hStateCells = HGetState(listPtr->cells);
    HLock(listPtr->cells);
    
    cellData = *(listPtr->cells);
    
    /* get the size of the cells (since lRect may be clipped) */
    
    LRect(&fullCellRect, lCell, lHandle);
    
    switch (lMessage) {
      case lInitMsg:
        /* we don't need any initialization */
        break;
 
      case lDrawMsg:
        EraseRect(lRect);
        
        if (lDataLen > 0) {
        
            /* determine starting point for drawing */
            
            leftDraw = lRect->left + listPtr->indent.h + kLeftOffset;
            topDraw = lRect->top + listPtr->indent.v + kTopOffset;
            
            /* move to the text location */
            GetFontInfo(&fontInfo);
            MoveTo(leftDraw, topDraw + fontInfo.ascent);
            
            /* if there's a comma, only draw the characters up to the comma
               and then move the data offset and length variables to
               point to the rest of the string following that comma */
             
            /* look for a comma, using memchr from the ANSI libraries (yech) */
            commaPtr = memchr(cellData + lDataOffset, ',', lDataLen);
            
            if (commaPtr != nil) {
            
                /* draw the substring preceding the comma */
                substringLen = (short) (commaPtr - (cellData + lDataOffset));
                DrawText(cellData, lDataOffset, substringLen);
                
                /* move the offset to skip the first substring and comma */
                lDataOffset += substringLen + 1;
                lDataLen -= (substringLen + 1);
                
                /* move to half way across the cell unless the pen
                   is already to the right of that point */
                   
                halfwayLeftDraw = (fullCellRect.right + leftDraw) / 2;
                GetPen(&currPenPt);
                if (currPenPt.h < halfwayLeftDraw)
                    MoveTo(halfwayLeftDraw, topDraw+fontInfo.ascent);
            }
            
            /* draw all remaining characters in the cell */
            DrawText(cellData, lDataOffset, lDataLen);
 
        }
 
        if (!lSelect)
            break;
        
      case lHiliteMsg:
        /* clearing the HiliteMode bit forces the next Invert to really
           be a "hilite" mode inversion */
 
        BitClr((Ptr)LMGetHiliteMode,pHiliteBit);
        InvertRect(lRect);
        break;
 
      case lCloseMsg:
        break;
    }
    
    /* restore the handles to their original states */
    HSetState(listPtr->cells,hStateCells);
    HSetState((Handle) lHandle,hStateList);
}