TweetFollow Us on Twitter

Expanded Lists
Volume Number:9
Issue Number:8
Column Tag:C Workshop

Related Info: List Manager

Expanding the List Manager

Adding graphics to lists with Custom LDEFs

By Mark W. Batten, Washington, DC

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

The List Manager is not especially well-named. The term “list” suggests one column of text, as in a shopping list or an address list. In fact, the List Manager is much more flexible: it can maintain information in rows and columns, as in a spreadsheet, and can manage data of any kind, including graphics.

The purpose of this article is to demonstrate two simple list definition procedures, or LDEFs, that extend the List Manager’s text-only list to include graphics. First, we’ll create a list of icons with text underneath, sort of like those that appear in Finder windows. Second, we’ll develop a list that puts a small icon next to each entry, the way the standard Open and Save dialog boxes do.

Setting up the application for our LDEFs

When using a custom LDEF, the List Manager leaves the drawing of each cell -- and the interpretation of the data in the cell -- entirely up to you. So to create a list that has both text and graphics in a list, all you have to do is put the graphics data and text in each cell. The LDEF will then interpret the data when it draws the cell. In our example, we just have the application stuff the icon data into the cell, followed by the text (a Pascal string), using List Manager routines:

/* 1 */

 LSetCell(IconPtr,128,theCell,LH);
 LAddToCell(IconName,(int)IconName[0]+1,theCell,LH);

Example 1: The Icon List

The List Manager sends four different kinds of messages to LDEFs: “initialize,” “draw,” “highlight,” and “close.” The initialize and close routines are rarely needed unless your LDEF needs to allocate its own memory for storage; we’ll focus here on the draw and highlight routines to show how they interpret the icon and text data.

Our first example is a list of icons with text underneath. Such a list has lots of uses: showing the user the files in an individual directory, or in an archive file; asking the user to choose from among a list of options; or any time you want to present a list of items in a graphically appealing way. Let’s examine the DoDraw routine in detail.

/* 2 */

doDraw(select,r,offset,LH)
Boolean select;
Rect *r;
int offset;
ListHandle LH;
{

The List Manager sends an LDrawMsg for each cell in the list, so the DoDraw routine’s task is to draw one cell. The cell to draw is passed to the LDEF in two parameters: the Rect r is the rectangle in which to draw the cell, and offset holds the offset into the list’s cell data where the information for this cell begins.

The routine begins with a number of cosmetic effects: it fills the cell with a gray pattern, and then draws a border around the icon. These obviously aren’t essential, but are included here because they help the icon stand out against the background and to give it a three-dimensional appearance:

/* 3 */

 FillRect(r,grey);
 r->top+=(r->bottom-r->top-32)/2;
 r.left+=(r->right-r->left-32)/2;
 r->bottom=r->top+32;
 r->right=r->left+32;
 InsetRect(r,-4,-4); 
 r1=*r;
 FillRect(r,wite); 
 FrameRect(r); 
 InsetRect(r,3,3);
 FrameRect(r); 
 InsetRect(r,1,1); 
 PenPat(dark);
 for(i=1;i<3;i++){
 MoveTo(r->left-(i+1),r->bottom+i);
 LineTo(r->right+i,r->bottom+i);
 MoveTo(r->right+i,r->top-(i+1));
 LineTo(r->right+i,r->bottom+i); 
 }
 PenNormal();

Now we draw the icon. To do so, we have to set up a BitMap that we can pass to CopyBits. Because each cell begins with icon data (which the application put there, remember?), the BitMap’s baseAddr should point to the beginning of the cell’s data. The other BitMap fields are easy, since they’re determined by the size of an icon:

/* 4 */

 HLock((**LH).cells); 
 icon.baseAddr=(char *)(*(**LH).cells)+offset;
 SetRect(&icon.bounds,0,0,32,32);
 icon.rowBytes=4;

Now we’re ready to draw the icon and move on to the text, which the application stored in the cell right after the 128 bytes of icon data. Note that the destination BitMap is supplied by the ListRec, which maintains a GrafPtr for the port the list appears in:

/* 5 */

 src=icon.bounds;
 CopyBits(&icon,&((**LH).port->portBits),&src,r,0,0L); 
 offset+=128;

Now we’re ready to draw the text under the icon. First, let’s copy the text out of the cell data into a local string variable:

/* 6 */

 q=(char *)(*(**LH).cells)+offset; 
 s=p;
 len=*s++=*q++;
 while(--len>=0)*s++=*q++; 
 HUnlock((**LH).cells);

Now we want to center the text in the cell. If the text is too long, though, we don’t want it to run off the edges, so we curtail the string and add a “ ” character to the end. In this example, the LDEF limits text to a maximum of five characters. We also need to erase the text’s rectangle before drawing it, because the text won’t be readable if we just draw the string over the gray pattern we painted into the cell:

/* 7 */

 if(*p>5){ 
 p[5]=0xC9; /* ' ' */
 *p=5;
 }
 c=(r->left+16)-(StringWidth(p)/2);
 GetFontInfo(&inf); 
 i=r->bottom+inf.ascent+inf.descent+inf.leading;
 SetRect(&r1,c,r->bottom+4,c+StringWidth(p),i);
 EraseRect(&r1);
 MoveTo(c,i); 
 DrawString(p);

That’s all there is to it; the final lines just invert the icon if the cell is supposed to be highlighted, first clearing the high bit of the global variable HiliteMode to ensure that the highlighting will use the highlight color on color machines:

/* 8 */

 if(select){
 HiliteMode&=127;
 InvertRect(r);
 }

Example 2: Open and Save Lists

Our second example is the kind of list seen in the Mac’s Open and Save dialog boxes: a list of text items with a small icon (“SICN”) to the left of each entry. This type of list could be handled in much the same way as Example 1, but let’s experiment with a slightly different technique that uses a little less memory.

For our example, we indicate a file’s type with one of three SICNs: a generic application icon for applications, a folder icon for folders, and a generic document icon for all others. With so few icons, there’s no need to store the actual data in each individual cell. Instead, as part of the setup, we have the application read in the appropriate SICN resource and store it in the ListHandle’s userHandle field:

/* 9 */

 SmIcons=Get1Resource('SICN',128); 
 (**(LH)).userHandle=SmIcons;

Then, in storing data in the cells, the application puts in the text, followed by one byte that indicates which SICN in the list to use:

/* 10 */

 PBGetCatInfo(&theParamBlk,false); /* get a file's info */
 LDoDraw(false,LH);
 b=(**(LH)).dataBounds.bottom;
 theCell.v=LAddRow(1,++b,LH); 
 theCell.h=0; 
 if((theParamBlk.ioFlAttrib & 16)!=0)theType=0;
 else{
 if(theParamBlk.ioFlFndrInfo.fdType=='APPL')theType=2; 
 else theType=1;
 }
 LSetCell(n,(int)(*n+1),theCell,LH);
 LAddToCell(&theType,1,theCell,LH);
 LDoDraw(true,LH);

Now let’s look at the Example 2 LDEF’s DoDraw routine to see how to interpret this data. Here we begin by erasing the cell, which is more appropriate for this list than the gray pattern we used in Example 1. Then we set up the destination rectangle to the left edge of the cell, and set up the bounds and rowBytes of the source BitMap:

/* 11 */

 dst=*r; 
 dst.right=dst.left+16;
 SetRect(&icon.bounds,0,0,16,16); 
 icon.rowBytes=2;

To set up the baseAddr field, we first retrieve the byte that we stored at the end of the text. Because there are 32 bytes in each small icon, we multiply the flag byte by 32 to get the proper position in the small icon list, stored in the userHandle. A call to CopyBits completes the drawing:

/* 12 */

 HLock((**LH).cells);
 s=q=(char *)*(**LH).cells;
 q+=off; 
 s+=off;
 q+=(*q+1); 
 icnoff=*q<<5;
 HLock((**LH).userHandle);
 icon.baseAddr=(*(**LH).userHandle)+icnoff;
 CopyBits(&icon,&((**LH).port->portBits),&(icon.bounds), &dst,0,0L);
 HUnlock((**LH).userHandle);

Drawing the text is then similar to Example 1, except that we move the pen to the 
right of the small icon and center it in the cell:

/* 13 */

 GetFontInfo(&inf); 
 len=inf.ascent+inf.descent+inf.leading;
 MoveTo(dst.right+2,(r->bottom+r->top+len)/2-inf.descent);
 DrawString(s);
 HUnlock((**LH).cells);
 if(select)InvertRect(r);

This example uses black-and-white icons, but color icons could be handled the same way; just use a PixMap instead of a BitMap.

Example 1 Source Code
#include "SetUpA4.h"
Pattern grey={ 170,85,170,85,170,85,170,85 },
 wite={ 0,0,0,0,0,0,0,0 },
 dark={ 119,238,221,187,119,238,221,187 };

pascal void main(message,select,r,theCell,offset,len,LH)
int message,offset,len;
ListHandle LH;
Cell theCell;
Boolean select;
Rect *r;
{
 int l;

 RememberA0();
 SetUpA4();
 switch(message){
 case 0: break;
 case 1: 
 case 2: doDraw(select,r,offset,LH); break;
 case 3: break;
 }
 RestoreA4();
}

doDraw(select,r,offset,LH)
Boolean select;
Rect *r;
int offset;
ListHandle LH;
{
 BitMap icon; 
 Rect src,r1; 
 char p[32],*q,*s,c; 
 register int len,i;
 FontInfo inf;
 
 FillRect(r,grey);
 r->top+=(r->bottom-r->top-32)/2;
 r->left+=(r->right-r->left-32)/2;
 r->bottom=r->top+32;
 r->right=r->left+32;
 InsetRect(r,-4,-4); 
 r1=*r;
 FillRect(r,wite); 
 FrameRect(r); 
 InsetRect(r,3,3);
 FrameRect(r); 
 InsetRect(r,1,1); 
 PenPat(dark);
 for(i=1;i<3;i++){
 MoveTo(r->left-(i+1),r->bottom+i);
 LineTo(r->right+i,r->bottom+i);
 MoveTo(r->right+i,r->top-(i+1));
 LineTo(r->right+i,r->bottom+i); 
 }
 PenNormal();

 HLock((**LH).cells);
 icon.baseAddr=(char *)(*(**LH).cells)+offset;
 SetRect(&icon.bounds,0,0,32,32); 
 icon.rowBytes=4;
 src=icon.bounds;
 CopyBits(&icon,&((**LH).port->portBits),&src,r,0,0L);
 offset+=128;
 
 q=(char *)(*(**LH).cells)+offset; 
 s=p;
 len=*s++=*q++;
 while(--len>=0)*s++=*q++; 
 HUnlock((**LH).cells);
 
 if(*p>5){ 
 p[5]=0xC9; 
 *p=5;
 }
 c=(r->left+16)-(StringWidth(p)/2);
 GetFontInfo(&inf); 
 MoveTo(c,r->bottom+inf.ascent+inf.descent+inf.leading); 
 DrawString(p); 
 if(select){
 HiliteMode&=127;
 InvertRect(r);
 }
}

Example 2 Source Code

#include "SetUpA4.h"
Pattern grey={ 170,85,170,85,170,85,170,85 };

pascal void main(message,select,r,theCell,offset,len,LH)
int message,offset,len;
ListHandle LH;
Cell theCell;
Boolean select;
Rect *r;
{
 /* This routine is the same as in Example 1 */
}

doDraw(select,r,offset,LH)
Boolean select;
Rect *r;
int offset;
ListHandle LH;
{
 BitMap icon; 
 Rect dst; 
 char *q,*s,icnoff; 
 int len;
 FontInfo inf;
 
 EraseRect(r); 
 dst=*r; 
 dst.right=dst.left+16;
 SetRect(&icon.bounds,0,0,16,16); 
 icon.rowBytes=2;
 HLock((**LH).cells);
 s=q=(char *)*(**LH).cells;
 q+=off; 
 s+=off;
 q+=(*q+1); 
 icnoff=*q<<5;
 HLock((**LH).userHandle);
 icon.baseAddr=(*(**LH).userHandle)+icnoff;
 CopyBits(&icon,&((**LH).port->portBits),&(icon.bounds),
 &dst,0,0L);
 HUnlock((**LH).userHandle);
 
 GetFontInfo(&inf); 
 len=inf.ascent+inf.descent+inf.leading;
 MoveTo(dst.right+2,(r->bottom+r->top+len)/2-inf.descent);
 DrawString(s);
 HUnlock((**LH).cells);
 if(select){
 HiliteMode&=127;
 InvertRect(r);
 }
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.