TweetFollow Us on Twitter

Building PICT 1
Volume Number:10
Issue Number:2
Column Tag:Getting Started

Related Info: Color Quickdraw List Manager

Using The List Manager

Building & using a list of PICT Resources

By Dave Mark, MacTech Magazine Regular Contributing Author

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

At the end of last month’s column, I said that this month’s column would go back to Color Quickdraw. I lied. I was working on a program I was writing for Daniel (it teaches little kids to read) when I got embroiled in a knotty problem involving the List Manager. As I was wrestling with the problem, I came up with the idea for this month’s column. I liked the program so much that, instead of waiting till next month, I decided to go ahead and present it now. Don’t worry, we’ll get back to Color Quickdraw eventually...

PictLister

This month’s program is called PictLister. PictLister lets you create a window listing all the PICT resources available to your program. Note that this includes PICT resources found in your application’s resource fork as well as those found in any resource files opened by the System. Figure 1 shows the PICT resources found by my copy of PictLister.

Figure 1. A PictLister window.

When you double-click on the name of a PICT resource, PictLister creates a new window containing the PICT.

Creating the PictLister Resources

Start by creating a folder named PictLister inside your Development folder. Fire up ResEdit and create a file named PictLister.Π.rsrc inside your PictLister folder.

Now create an ALRT resource (along with a corresponding DITL resource) for our error alert. The ALRT resource should have a Top: of 40, a Left: of 30, a Height: of 116, and a Width: of 292. Be sure to set the DITL ID: to 128.

Next, create a DITL with an id of 128. Use the specifications in Figure 2 to create the OK button and the specifications in Figure 3 to create the error alert’s static text item.

Figure 2. Specifications for the error alert’s OK button.

Figure 3. Specifications for the error alert’s static text item.

Next, create an MBAR resource with an id of 128. The MBAR should list three MENU ids: 128, 129, and 130. Create three MENU resources according to the specifications in Figure 4. Be sure to include a separator line as the third item in the File menu.

Figure 4. The three MENU resources.

Finally, go into the scrapbook (or your favorite graphics application) and use Copy and Paste to create a series of PICT resources in the resource file. Select Get Resource Info from the Resource menu and give each resource a name. It’s important to name your PICT resources, since PictLister uses the PICT’s name to represent the PICT in a PictLister window. Figure 5 shows the Get Resource Info window for my first PICT resource. Notice that the Purgeable check box is checked.

Figure 5. Get Resource Info for my first PICT resource.

Creating the PictLister Project

Save your changes and quit ResEdit. Launch THINK C and create a new project named PictLister.Π in the PictLister folder. Add MacTraps to the project. Next, create a new source code window, save it as PictLister.c and add it to the project.

Type in the following source code:


/* 1 */
#define kMBARResID 128
#define kSleep   60L
#define kMoveToFront (WindowPtr)-1L
#define kNilFilterProc    (ProcPtr)0L
#define kEmptyString "\p"
#define kHasGoAway true
#define kInvisible false

#define kListDefProc 0
#define kDontDrawYet false
#define kHasGrow true
#define kHasHScrolltrue
#define kHasVScrolltrue
#define kFindNexttrue

#define kListWindow0
#define kDAWindow1
#define kUnknownWindow    2
#define kPictWindow3
#define kNilWindow 4

#define kMinWindowWidth   210
#define kMinWindowHeight  63
#define kWindowHeight255
#define kMinPictWinHeight 50
#define kMinPictWinWidth  150

#define mApple   128
#define iAbout   1

#define mFile    129
#define iNewList 1
#define iClose   2
#define iQuit    4

#define kErrorAlertID128


/************************/
/* Typedefs         */
/************************/

typedef struct
{
 WindowRecord  w;
 short  wType;
 ListHandle list;
} ListRecord, *ListPeek;

typedef struct
{
 WindowRecord  w;
 short  wType;
 short  PictResID;
} PictRecord, *PictPeek;


/*************/
/*  Globals  */
/*************/

Boolean gDone;
short   gNewWindowX = 20, gNewWindowY = 50;


/***************/
/*  Functions  */
/***************/

void    ToolboxInit( void );
void    MenuBarInit( void );
void    CreateListWindow( void );
void    DestroyWindow( WindowPtr w );
void    EventLoop( void );
void    DoEvent( EventRecord *eventPtr );
void    DoUpdate( EventRecord *eventPtr );
void    DoActivate( EventRecord *eventPtr );
void    HandleMouseDown( EventRecord *eventPtr );
void    DoContentClick( EventRecord *eventPtr, WindowPtr w );
void    CreatePictWindow( ListHandle list );
void    BumpGlobalXandY( void );
void    DoGrow( EventRecord *eventPtr, WindowPtr w );
void    HandleMenuChoice( long menuChoice );
void    HandleAppleChoice( short item );
void    HandleFileChoice( short item );
void    CenterWindow( WindowPtr w );
void    CenterPict( PicHandle picture, Rect *destRectPtr );
short WindowType( WindowPtr window );
void    DoError( Str255 errorString );


/********************* main ******************/

void    main( void )
{
 ToolboxInit();
 MenuBarInit();
 
 EventLoop();
}


/********************* ToolboxInit ******************/

void    ToolboxInit( void )
{
 InitGraf( &thePort );
 InitFonts();
 InitWindows();
 InitMenus();
 TEInit();
 InitDialogs( nil );
 InitCursor();
}


/********************* MenuBarInit ******************/

void    MenuBarInit( void )
{
 Handle menuBar;
 MenuHandle menu;
 
 menuBar = GetNewMBar( kMBARResID );
 SetMenuBar( menuBar );

 menu = GetMHandle( mApple );
 AddResMenu( menu, 'DRVR' );
 
 DrawMenuBar();
}


/********************* CreateListWindow ******************/

void    CreateListWindow( void )
{
 Rect   r, dataBounds;
 WindowPtrw;
 Point  cSize, cIndex;
 ListHandle list;
 short  i, dummy, numPicts;
 Handle rHandle;
 short  resID;
 ResTypetheResType;
 Str255 rName;
 Ptr    wStorage;
 ListPeek l;
 
 SetRect( &r, gNewWindowX, gNewWindowY, gNewWindowX + 
 kMinWindowWidth, gNewWindowY + kWindowHeight);
 
 BumpGlobalXandY();
 
 wStorage = NewPtr( sizeof( ListRecord ) );
 
 w = NewWindow( wStorage, &r, "\pPicture List", kInvisible,
 documentProc, kMoveToFront, kHasGoAway, 0L );
 
 SetPort( w );
 TextFont( systemFont );
 
 SetRect( &dataBounds, 0, 0, 1, 0 );
 SetPt( &cSize, 0, 0 );
 SetRect (&r, 0, 0, kMinWindowWidth - 15, 
 kWindowHeight - 15);
 
 list = LNew( &r, &dataBounds, cSize, kListDefProc,
 w, kDontDrawYet, kHasGrow, kHasHScroll, kHasVScroll );
 
 (**list).selFlags = lOnlyOne;
 
 l = (ListPeek)w;
 
 l->wType = kListWindow;
 l->list = list;
 
 numPicts = CountResources( 'PICT' );
 
 for ( i = 0; i<numPicts; i++ )
 {
 rHandle = GetIndResource( 'PICT', i + 1 );
 GetResInfo( rHandle, &resID, &theResType, rName );
 
 dummy = LAddRow( 1, i, list );
 SetPt( &cIndex, 0, i );
 
 if ( rName[ 0 ] > 0 )
 LAddToCell( &(rName[1]), rName[0], cIndex, list );
 else
 LAddToCell( "<Unnamed>", 10, cIndex, list );
 }
 
 ShowWindow( w );
 LDoDraw( true, list );
}


/********************* DestroyWindow ******************/

void    DestroyWindow( WindowPtr w )
{
 ListPeek l;
 
 if ( WindowType( w ) == kListWindow )
 {
 HideWindow( w );
 l = (ListPeek)w;
 
 LDispose( l->list );
 
 CloseWindow( w );
 
 DisposePtr( (Ptr)w );
 }
 else if ( WindowType( w ) == kPictWindow )
 {
 CloseWindow( w );
 DisposePtr( (Ptr)w );
 }
}


/********************* EventLoop ******************/

void    EventLoop( void )
{
 EventRecordevent;
 
 gDone = false;
 while ( gDone == false )
 {
 if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
 DoEvent( &event );
 }
}


/********************* DoEvent ******************/

void    DoEvent( EventRecord *eventPtr )
{
 char   theChar;
 
 switch ( eventPtr->what )
 {
 case mouseDown: 
 HandleMouseDown( eventPtr );
 break;
 case keyDown:
 case autoKey:
 theChar = eventPtr->message & charCodeMask;
 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
 HandleMenuChoice( MenuKey( theChar ) );
 break;
 case updateEvt:
 DoUpdate( eventPtr );
 break;
 case activateEvt:
 DoActivate( eventPtr );
 break;
 }
}


/********************* DoUpdate ******************/

void    DoUpdate( EventRecord *eventPtr )
{
 WindowPtrw;
 short  numPicts, i;
 ListPeek l;
 ListHandle list;
 GrafPtroldPort;
 Rect   r;
 Point  cellIndex;
 PicHandlepic;
 PictPeek p;
 
 w = (WindowPtr)(eventPtr->message);
 BeginUpdate( w );
 
 if ( WindowType( w ) == kListWindow )
 {
 GetPort( &oldPort );
 SetPort( w );
 
 l = (ListPeek)w;
 list = l->list;
 
 DrawGrowIcon( w );
 
 LUpdate( (**list).port->visRgn, list );
 
 SetPort( oldPort );
 }
 else if ( WindowType( w ) == kPictWindow )
 {
 GetPort( &oldPort );
 SetPort( w );
 
 r = w->portRect;
 
 p = (PictPeek)w;
 
 pic = GetPicture( p->PictResID );
 
 CenterPict( pic, &r );
 DrawPicture( pic, &r );
 
 SetPort( oldPort );
 }
 EndUpdate( w );
}


/********************* DoActivate ******************/

void    DoActivate( EventRecord *eventPtr )
{
 WindowPtrw;
 ListPeek l;
 ListHandle list;
 
 w = (WindowPtr)(eventPtr->message);
 
 if ( WindowType( w ) == kListWindow )
 {
 l = (ListPeek)w;
 list = l->list;
 
 if ( eventPtr->modifiers & activeFlag )
 LActivate( true, list );
 else
 LActivate( false, list );
 
 DrawGrowIcon( w );
 }
}


/********************* HandleMouseDown ******************/

void    HandleMouseDown( EventRecord *eventPtr )
{
 WindowPtrwindow;
 short  thePart;
 long   menuChoice;
 GrafPtroldPort;
 long   windSize;
 Rect   growRect;
 
 thePart = FindWindow( eventPtr->where, &window );
 
 switch ( thePart )
 {
 case inMenuBar:
 menuChoice = MenuSelect( eventPtr->where );
 HandleMenuChoice( menuChoice );
 break;
 case inSysWindow : 
 SystemClick( eventPtr, window );
 break;
 case inContent:
 DoContentClick( eventPtr, window );
 break;
 case inGrow:
 DoGrow( eventPtr, window );
 break;
 case inDrag : 
 DragWindow( window, eventPtr->where, 
 &screenBits.bounds );
 break;
 case inGoAway:
 if ( TrackGoAway( window, eventPtr->where ) )
 DestroyWindow( window );
 break;
 }
}


/********************* DoContentClick ******************/

void    DoContentClick( EventRecord *eventPtr, WindowPtr w )
{
 GrafPtroldPort;
 ListHandle list;
 ListPeek l;
 
 if ( w != FrontWindow() )
 {
 SelectWindow( w );
 }
 else if ( WindowType( w ) == kListWindow )
 {
 GetPort( &oldPort );
 SetPort( w );
 
 GlobalToLocal( &(eventPtr->where) );
 
 l = (ListPeek)w;
 list = l->list;
 
 if (LClick( eventPtr->where, eventPtr->modifiers, list ))
 CreatePictWindow( list );
 SetPort( oldPort );
 }
}


/********************* CreatePictWindow ******************/

void    CreatePictWindow( ListHandle list )
{
 Cell   cell;
 PicHandlepic;
 Handle rHandle;
 Rect   r;
 short  resID;
 ResTypetheResType;
 Str255 rName;
 PictPeek p;
 Ptr    wStorage;
 WindowPtrw;
 
 SetPt( &cell, 0, 0 );
 
 if ( LGetSelect( kFindNext, &cell, list ) )
 {
 rHandle = GetIndResource( 'PICT', cell.v + 1 );
 pic = (PicHandle)rHandle;
 
 r = (**pic).picFrame;
 
 if ( r.right - r.left < kMinPictWinWidth )
 r.right = r.left + kMinPictWinWidth;
 
 if ( r.bottom - r.top < kMinPictWinHeight )
 r.bottom = r.top + kMinPictWinHeight;
 
 OffsetRect( &r, gNewWindowX - r.left, 
 gNewWindowY - r.top );
 
 BumpGlobalXandY();
 
 wStorage = NewPtr( sizeof( PictRecord ) );
 
 GetResInfo( rHandle, &resID, &theResType, rName );
 
 if ( rName[ 0 ] > 0 )
 {
 w = NewWindow( wStorage, &r, rName, kInvisible,
 noGrowDocProc, kMoveToFront, kHasGoAway, 0L );
 }
 else
 {
 w = NewWindow( wStorage, &r, "\p<Unnamed>", kInvisible,
 noGrowDocProc, kMoveToFront, kHasGoAway, 0L );
 }
 
 ShowWindow( w );
 SetPort( w );
 
 p = (PictPeek)w;
 p->wType = kPictWindow;
 p->PictResID = resID;
 }
}


/********************* BumpGlobalXandY ******************/

void    BumpGlobalXandY( void )
{
 gNewWindowX += 20;
 gNewWindowY += 20;
 
 if ( (gNewWindowX > screenBits.bounds.right - 100) ||
 (gNewWindowY > screenBits.bounds.bottom - 100) )
 {
 gNewWindowX = 20;
 gNewWindowY = 50;
 }
}


/********************* DoGrow  ******************/

void    DoGrow( EventRecord *eventPtr, WindowPtr w )
{
 Rect   r;
 GrafPtroldPort;
 Cell   cSize;
 long   windSize;
 ListHandle list;
 
 if ( WindowType( w ) == kListWindow )
 {
 r.top = kMinWindowHeight;
 r.bottom = 32767;
 r.left = kMinWindowWidth;
 r.right = 32767;
 
 windSize = GrowWindow( w, eventPtr->where, &r );
 if ( windSize )
 {
 GetPort( &oldPort );
 SetPort( w );
 EraseRect( &w->portRect );

 SizeWindow( w, LoWord (windSize), 
 HiWord(windSize), true );
 
 list = ((ListPeek)w)->list;
 LSize( LoWord(windSize) - 15,
 HiWord(windSize) - 15, list );
 
 HLock( (Handle)list );
 cSize = (*list)->cellSize;
 HUnlock( (Handle)list );
 
 cSize.h = LoWord( windSize ) - 15;
 LCellSize( cSize, list );
 InvalRect( &w->portRect );
 SetPort( oldPort );
 }
 }
}


/********************* HandleMenuChoice ******************/

void    HandleMenuChoice( long menuChoice )
{
 short  menu;
 short  item;
 
 if ( menuChoice != 0 )
 {
 menu = HiWord( menuChoice );
 item = LoWord( menuChoice );
 
 switch ( menu )
 {
 case mApple:
 HandleAppleChoice( item );
 break;
 case mFile:
 HandleFileChoice( item );
 break;
 }
 HiliteMenu( 0 );
 }
}


/********************* HandleAppleChoice ******************/

void    HandleAppleChoice( short item )
{
 MenuHandle appleMenu;
 Str255 accName;
 short  accNumber;
 
 switch ( item )
 {
 case iAbout:
 SysBeep( 20 );
 break;
 default:
 appleMenu = GetMHandle( mApple );
 GetItem( appleMenu, item, accName );
 accNumber = OpenDeskAcc( accName );
 break;
 }
}


/********************* HandleFileChoice ******************/

void    HandleFileChoice( short item )
{
 switch ( item )
 {
 case iNewList:
 CreateListWindow();
 break;
 case iClose:
 DestroyWindow( FrontWindow() );
 break;
 case iQuit:
 gDone = true;
 break;
 }
}


/********************* CenterPict ******************/

void    CenterPict( PicHandle picture, Rect *destRectPtr )
{
 Rect   windRect, pictRect;
 
 windRect = *destRectPtr;
 pictRect = (**( picture )).picFrame;
 OffsetRect( &pictRect, windRect.left - pictRect.left,
 windRect.top   - pictRect.top);
 OffsetRect( &pictRect, (windRect.right - pictRect.right)/2,
 (windRect.bottom - pictRect.bottom)/2);
 *destRectPtr = pictRect;
}


/********************* WindowType ******************/

short WindowType( WindowPtr window )
{
 if ( window == nil )
 return( kNilWindow );

 if ( ((WindowPeek)window)->windowKind < 0 )
 return( kDAWindow );
 
 if ( ((ListPeek)window)->wType == kListWindow )
 return( kListWindow );
 
 if ( ((ListPeek)window)->wType == kPictWindow )
 return( kPictWindow );
 
 return( kUnknownWindow );
}


/********************* DoError ******************/

void    DoError( Str255 errorString )
{
 ParamText( errorString, kEmptyString, 
 kEmptyString, kEmptyString );
 
 StopAlert( kErrorAlertID, kNilFilterProc );
 
 ExitToShell();
}

Save your source code, then run PictLister.

Running PictLister

The first thing you’ll see when you run PictLister is the menu bar, featuring the •, File, and Edit menus. Select New List from the File menu. A PictLister window will appear, listing all available PICT resources by name. If the resource doesn’t have a name, the string <Unnamed> will appear.

Play with the window a bit. Resize it. Notice that there is a definite minimum size. Click on an item. Notice that the item highlights. Try scrolling the list. Click on an item and drag it down or up. If the window is small enough to enable scrolling, dragging an item up or down causes the window to auto-scroll to the top or bottom of the list.

Select New List from the File menu again. Notice that the new window is created 20 pixels down and 20 pixels to the right of the previous window. If you create enough windows, eventually the windows will wrap back around to the upper left corner of the main display.

Double-click on an item in the list. A new window will appear containing the named PICT. Click on the close box of a window to close it. You can also close a window by selecting Close from the File menu.

Till Next Month

Next month, we’ll walk through the PictLister source code. If you get a chance to look through the code on your own, you might want to try adding this feature to the program: Instead of using the string <Unnamed> to name unnamed PICT resources, try creating a string containing the resource’s resource id, using the string both in the list window and in the PICT window’s title. See you next month...

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.