Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
SimpleList.cp
/* |
File: SimpleList.cp |
Description: A simple illustration of a ListBox Control implementation and its pitfall |
Author: ES |
Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
First release: Jun 21, 2002 |
*/ |
#ifdef __MWERKS__ |
#include <Carbon.h> |
#else |
#include <Carbon/Carbon.h> |
#endif |
// Preferred way is to use CarbonEvents (much shorter listing) |
// But the old way is fine too |
#define USECARBONEVENTS 1 |
Boolean gStop; |
WindowRef gWindow; |
ControlRef gQuit; |
ControlRef gList; |
void CreateTestListWindow(void); |
pascal OSStatus gQuitHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData); |
#if ! USECARBONEVENTS |
void MainEventLoop(); |
#endif |
int main(void) |
{ |
FlushEvents(everyEvent,0); |
InitCursor(); |
gStop = false; |
CreateTestListWindow(); |
#if USECARBONEVENTS |
RunApplicationEventLoop(); |
#else |
MainEventLoop(); |
#endif |
} |
pascal OSStatus gQuitHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) |
{ |
ExitToShell(); |
} |
void CreateTestListWindow(void) |
{ |
// Creating the window |
Rect bounds = {50, 50, 550, 550}; |
WindowAttributes theAttributes = kWindowNoAttributes; |
#if USECARBONEVENTS |
theAttributes |= kWindowStandardHandlerAttribute; |
#endif |
OSStatus theStatus = CreateNewWindow(kDocumentWindowClass, theAttributes, &bounds, &gWindow); |
if ((theStatus != noErr) || (gWindow == NULL)) ExitToShell(); |
ControlRef rootControl; |
// the following line is absolutely necessary to prevent crashes or disfunctions on Mac OS 9 |
// when you click in either of the scroll bars of the list box control. |
CreateRootControl(gWindow, &rootControl); |
// Adding a Quit button |
Rect boundsQuit = {20, 20, 44, 120}; |
theStatus = CreatePushButtonControl(gWindow, &boundsQuit, CFSTR("Quit"), &gQuit); |
if ((theStatus != noErr) || (gQuit == NULL)) ExitToShell(); |
#if USECARBONEVENTS |
EventTypeSpec eventTypeList[] = { {kEventClassControl, kEventControlHit} }; |
theStatus = InstallEventHandler(GetControlEventTarget(gQuit), NewEventHandlerUPP(gQuitHandler), 1, eventTypeList, NULL, NULL); |
#endif |
// Adding our LitsBox Control |
Rect boundsList = {60, 20, 480, 480}; |
ListDefSpec listDef = {kListDefStandardTextType}; |
theStatus = CreateListBoxControl(gWindow, &boundsList, false, 30, 30, true, true, 16, 50, true, &listDef, &gList); |
if ((theStatus != noErr) || (gList == NULL)) ExitToShell(); |
// Populating our List |
ListHandle theList; |
theStatus = GetControlData(gList, kControlNoPart, kControlListBoxListHandleTag, sizeof(ListHandle), (Ptr)&theList, NULL); |
if (theStatus != noErr) ExitToShell(); |
long i, j, theNum = 1; |
Str255 theString; |
Cell whichCell; |
for(i = 0; i < 30; i++) |
{ |
for(j = 0; j < 30; j++) |
{ |
NumToString(theNum, theString); |
theNum++; |
whichCell.h = i; |
whichCell.v = j; |
LSetCell(&theString[1], theString[0], whichCell, theList); |
} |
} |
ShowWindow(gWindow); |
} |
#if ! USECARBONEVENTS |
void MainEventLoop() |
{ |
EventRecord theEvent; |
WindowPtr theWind; |
while (!gStop) |
{ |
if (WaitNextEvent(everyEvent, &theEvent, 4, 0L)) |
switch (theEvent.what) |
{ |
case mouseDown: |
switch (FindWindow(theEvent.where, &theWind)) |
{ |
case inContent: |
if (theWind != FrontNonFloatingWindow()) |
SelectWindow(theWind); |
else if (gWindow == theWind) |
{ |
GrafPtr savePort; |
GetPort(&savePort); |
SetPortWindowPort(gWindow); |
Point thePoint = theEvent.where; |
GlobalToLocal(&thePoint); |
SInt16 partCode; |
ControlRef theControl = FindControlUnderMouse(thePoint, theWind, &partCode); |
if (theControl != NULL) |
{ |
partCode = HandleControlClick(theControl, thePoint, theEvent.modifiers, (ControlActionUPP)0L); |
if (partCode != 0) |
{ |
if (theControl == gQuit) |
gStop = true; |
} |
} |
SetPort(savePort); |
} |
break; |
} |
break; |
case updateEvt: |
if (gWindow == (WindowRef)theEvent.message) |
{ |
BeginUpdate(gWindow); |
DrawControls(gWindow); |
EndUpdate(gWindow); |
} |
break; |
} |
} |
} |
#endif |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-30