Article View: comp.sys.mac
Article #19781Re: Help (List Manager Example)
From: Bruce Florman
Date: Fri, 02 Oct 1987 00:35
Date: Fri, 02 Oct 1987 00:35
298 lines
8714 bytes
8714 bytes
>
>
> I desperately need some source code examples of using the list manager.
> Could someone please either e-mail the code to me or else post it? I
> would prefer the code to be in Lightspeed Pascal, but any other language
> will do. Nirav Patel.
A couple of weeks ago, I desperately needed some too, and I finally
tracked down an example written in C in an old issue of MacTutor.
Unfortunately, I don't remember which issue, and I don't have it handy.
Fortunately, I used the example to write my own routine in LS Pascal, and
I do have that handy.
My code is used to put a regular list of text into a modal dialog.
The interface procedure, LModalSelect1, takes as arguments, the resource
ids of a Dialog (DLOG) and a String List (STR#), the number of rows and
columns in the list, the size of the cells, and the rectangle in which
the list will be viewed. It returns the selected string and that string's
index (in the range 1..n where n is the total number of cells). This
procedure makes certain assumptions about the first three items in the
given dialog's item list (DITL). The first item is the default button.
The second is an enabled userItem that will be used to display the list.
The third is a disabled userItem which will be used to draw the outline
around the default button.
What follows is three files:
1. UModalList.p - the unit that we're interested in.
2. Main.p - a main program to test it with.
3. ModalListOther.r - a Rez format file describing the
resources that I used for my tests.
----------------file UModalList.p----------------
UNIT UModalList;
INTERFACE
USES
ListManager;
PROCEDURE LModalSelect1 (dialogId, stringListId : INTEGER;
nColumns, nRows : INTEGER;
cellWidth, cellHeight : INTEGER;
viewRect : Rect;
VAR aString : Str255;
VAR index : INTEGER);
IMPLEMENTATION
CONST
kOKbutton = 1;
kListItem = 2;
kOutliner = 3;
kScrollBarWidth = 15;
kStdLDEF = 0;
PROCEDURE DrawListItem (aDialog : WindowPtr;
itemNo : INTEGER);
VAR
theList : ListHandle;
theRgn : RgnHandle;
listFrame : Rect;
BEGIN
theList := ListHandle(GetWRefCon(aDialog));
listFrame := theList^^.rView;
InsetRect(listFrame, -1, -1);
FrameRect(listFrame);
theRgn := NewRgn;
RectRgn(theRgn, aDialog^.portRect);
LUpdate(theRgn, theList);
DisposeRgn(theRgn)
END;
PROCEDURE OutlineButton (aDialog : DialogPtr; itemNo : INTEGER);
VAR
iType : INTEGER;
iHandle : Handle;
iBox : Rect;
BEGIN
GetDItem(aDialog, kOKbutton, iType, iHandle, iBox);
InsetRect(iBox, -4, -4);
{ We assume that thePort = aDialog when this code is called. }
{ Just to be safe, we could call SetPort(aDialog), but it isn't }
{ really necessary. }
PenSize(3, 3);
FrameRoundRect(iBox, 16, 16);
PenNormal
END;
PROCEDURE InitListDialog (dialogId, stringListId : INTEGER;
nColumns, nRows : INTEGER;
cellWidth, cellHeight : INTEGER;
viewRect : Rect;
VAR theListDialog : DialogPtr;
VAR theList : ListHandle;
initSelection : INTEGER);
VAR
dataBounds, itemBox : Rect;
doVScroll, doHScroll : BOOLEAN;
cellSize : Point;
aCell : Cell;
i, itemType : INTEGER;
itemHandle : Handle;
aString : Str255;
BEGIN
theListDialog := GetNewDialog(dialogId, NIL, Pointer(-1));
IF EmptyRect(viewRect) THEN
GetDItem(theListDialog, kListItem,
itemType, itemHandle, viewRect);
WITH viewRect DO
BEGIN
doHScroll := (right - left) < (nColumns * cellWidth);
doVScroll := (bottom - top) < (nRows * cellHeight)
END;
SetRect(dataBounds, 0, 0, nColumns, nRows);
SetPt(cellSize, cellWidth, cellHeight);
theList := LNew(viewRect, dataBounds, cellSize, kStdLDEF,
theListDialog, FALSE, FALSE, doHScroll, doVScroll);
SetWRefCon(theListDialog, Ord4(theList));
theList^^.selFlags := lOnlyOne;
FOR i := 0 TO (nColumns * nRows) - 1 DO
BEGIN
SetPt(aCell, i DIV nRows, i MOD nRows);
GetIndString(aString, stringListId, i + 1);
LSetCell(Pointer(Ord4(@aString) + 1), Length(aString),
aCell, theList)
END;
IF initSelection > 0 THEN
BEGIN
i := initSelection - 1;
SetPt(aCell, i DIV nRows, i MOD nRows);
LSetSelect(TRUE, aCell, theList)
END;
LDoDraw(TRUE, theList);
itemBox := viewRect;
WITH itemBox DO
BEGIN
IF doHScroll THEN
bottom := bottom + kScrollBarWidth;
IF doVScroll THEN
right := right + kScrollBarWidth
END;
SetDItem(theListDialog, kListItem, userItem,
Handle(@DrawListItem), itemBox);
SetDItem(theListDialog, kOutliner, userItem + itemDisable,
Handle(@OutlineButton), itemBox);
ShowWindow(theListDialog)
END;
PROCEDURE PoseListDialog (theListDialog : DialogPtr;
theList : ListHandle);
VAR
done : BOOLEAN;
itemHit : INTEGER;
aPoint : Point;
BEGIN
done := FALSE;
WHILE NOT done DO
BEGIN
ModalDialog(NIL, itemHit);
IF itemHit = kOKbutton THEN
done := TRUE
ELSE IF itemHit = kListItem THEN
BEGIN
SetPort(theListDialog);
GetMouse(aPoint);
done := LClick(aPoint, 0, theList)
END
END
END;
PROCEDURE GetListSelection (theList : ListHandle;
VAR aString : Str255;
VAR index : INTEGER);
VAR
someSelect : BOOLEAN;
aCell : Cell;
p : Ptr;
stringLength : INTEGER;
BEGIN
SetPt(aCell, 0, 0);
someSelect := LGetSelect(TRUE, aCell, theList);
IF someSelect THEN
BEGIN
p := @aString;
stringLength := SizeOf(aString);
LGetCell(Pointer(Ord4(p) + 1), stringLength, aCell, theList);
p^ := Byte(stringLength);
WITH theList^^.dataBounds, aCell DO
index := (bottom - top) * h + v + 1
END
ELSE
BEGIN
aString := '';
index := 0
END
END;
PROCEDURE LModalSelect1;
VAR
theList : ListHandle;
theListDialog : DialogPtr;
BEGIN
InitListDialog(dialogId, stringListId,
nColumns, nRows,
cellWidth, cellHeight,
viewRect, theListDialog,
theList, index);
PoseListDialog(theListDialog, theList);
GetListSelection(theList, aString, index);
LDispose(theList);
DisposDialog(theListDialog)
END;
END.
----------------file Main.p----------------
PROGRAM Main;
USES
UModalList;
CONST
kDialogId = 1001;
kStringListId = 1001;
kColumns = 2;
kRows = 13;
kCellWidth = 80;
kCellHeight = 16;
VAR
viewRect : Rect;
aString : Str255;
anItem : INTEGER;
BEGIN
SetRect(viewRect, 8, 8, 168, 88);
LModalSelect1(kDialogId, kStringListId, kColumns, kRows,
kCellWidth, kCellHeight, viewRect, aString, anItem);
ShowText;
WriteLn('item #', anItem : 1, ': ', aString)
END.
-----------------file ModalListOther.r----------------
resource 'DLOG' (1001) {
{120, 154, 262, 345},
documentProc, invisible, noGoAway,
0x0, 1001, "Modal List"
};
resource 'DITL' (1001) {
{ {112, 64, 132, 124}, Button { enabled, "OK" },
{8, 8, 88, 183}, UserItem { enabled },
{0, 0, 0, 0}, UserItem { disabled }
}
};
resource 'STR#' (1001) {
{ "Alpha", "Bravo", "Charlie", "Delta",
"Echo", "Foxtrot", "Golf", "Hotel",
"India", "Juliet", "Kilo", "Lima",
"Mike", "November", "Oscar", "Papa",
"Quebec", "Romeo", "Sierra", "Tango",
"Uniform", "Victor", "Whiskey", "X-ray",
"Yankee", "Zulu"
}
};
----------------End of included files----------------
I don't get a chance for much feedback from other Mac programmers, so
if there are any MacWizards out there who would like to comment on this,
please feel free.
-Bruce Florman
--
========================================================================
florman@rand-unix.ARPA
{decvax,sdcrdcf,trwrb,trwspf,vortex}!rand-unix!gnu!florman
"There is no limit to the amount of good that people can accomplish,
if they don't care who gets the credit."
- Anonymous
Technical Details
Message-ID:
<1151@randvax.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!udel!burdvax!sdcrdcf!randvax!flormanReferences:
<90000001@smu>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!udel!burdvax!sdcrdcf!randvax!florman From: florman@randvax.UUCP (Bruce Florman) Newsgroups: comp.sys.mac Subject: Re: Help (List Manager Example) Message-ID: <1151@randvax.UUCP> Date: Thu, 1-Oct-87 20:35:58 EDT Article-I.D.: randvax.1151 Posted: Thu Oct 1 20:35:58 1987 Date-Received: Tue, 6-Oct-87 04:58:11 EDT References: <90000001@smu> Organization: Rand Corp., Santa Monica Lines: 298