home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel Volume 2 #1
/
carousel.iso
/
mactosh
/
lang
/
listmana.sha
/
test.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-07-10
|
12KB
|
510 lines
/*
* test, an example program which uses the List Manager
* converted from the Pascal program provided on the 12/85 Apple
* supplement 1 disk (a Lisa-only disk) to Aztec C 1.06G by
* Dale Carstensen
* 1504 S Sage
* Los Alamos, NM 87544
* (505)662-3691
* June 16, 1986
*/
#include <quickdraw.h>
#include <desk.h>
#include <dialog.h>
#include <event.h>
#include <font.h>
#include <list.h>
#include <menu.h>
#include <packages.h>
#include <resource.h>
#include <segment.h>
#include <textedit.h>
#include <toolutil.h>
pascal void MacsBug() = 0xa9ff;
#define NIL 0L
#define appleMenu 1
#define fileMenu 256
#define editMenu 257
#define mungeMenu 258
#define selectMenu 259
#define makeNewMenu 260
#define lastMenu 6
#define len255 255
MenuHandle myMenus[lastMenu + 1];
Rect dragRect,r,b,sizeLimits;
Boolean doneFlag,temp,sHoriz,sVert,groBox,drawIt;
EventRecord myEvent;
int code,refNum,i,j;
WindowRecord wRecord;
WindowPtr myWindow,whichWindow;
int theMenu,theItem;
ListHandle myList;
Cell c,cc;
char CellName[len255],cutStr[len255];
int cutStrLen;
Handle h1,h2;
OSErr x;
Point cSize;
int theMark;
int tFlag;
long tempFlags, newSize;
void
CellValue(c, h, v)
Cell *c;
{
c->h = h;
c->v = v;
} /* end of CellValue() */
void
ClrAllCells()
{
Cell c;
CellValue(&c,0,0);
while (LGetSelect(TRUE,&c,myList)) /* get next selected cell */
LSetSelect(FALSE,pass(c),myList); /* and deselect it */
} /* end of ClrAllCells() */
void
SetCell(c)
Cell *c;
{
char iStr[len255],jStr[len255];
NumToString((long)c->h,iStr);
ptoc(iStr);
NumToString((long)c->v,jStr);
ptoc(jStr);
strcpy(CellName, "Cell ");
strcat(CellName, jStr);
strcat(CellName, ",");
strcat(CellName, iStr);
LSetCell(CellName, strlen(CellName), pass(*c), myList);
} /* end of SetCell() */
void
SetCellValues() /* number all the cells in the array*/
{
Cell c;
CellValue(&c,0,0);
do {
SetCell(&c);
} while (LNextCell(TRUE,TRUE,&c,myList)); /* increment v and h */
} /* end of SetCellValues() */
int
CountSelections()
{
Cell c;
int i;
CellValue(&c,0,0);
i = 0;
do {
if (LGetSelect(TRUE,&c,myList))
i++;
} while (LNextCell(TRUE,TRUE,&c,myList));
return(i);
} /* end of CountSelections() */
void
FlipSelections() /* A little razzle-dazzle to test double-click */
{
Cell c;
int i,flipStrLen;
char flipStr[len255];
char a;
Boolean b;
CellValue(&c,0,0);
/* It appears maybe the programmer should choose between the
* while (LGetSelect... or the LLastClick, depending on whether
* the double click should affect all selections, or only one,
* but it didn't say in the Pascal source. The while was
* commented out.
*/
/* while (LGetSelect(TRUE,&c,myList)) */
*((long *)(&c)) = LLastClick(myList);
{
flipStrLen = len255;
LGetCell(flipStr + 1, &flipStrLen, pass(c), myList);
for (i = 1; i <= flipStrLen / 2; i++) {
a = flipStr[i];
flipStr[i] = flipStr[flipStrLen-i+1];
flipStr[flipStrLen-i+1] = a;
};
LSetCell(flipStr + 1, flipStrLen, pass(c), myList);
b = LNextCell(TRUE,TRUE,&c,myList);
};
} /* end of FlipSelections() */
void
SetUpMenus() /* Once-only initialization for menus */
{
int i;
InitMenus(); /* initialize Menu Manager */
myMenus[1] = GetMenu(appleMenu);
AddResMenu(myMenus[1],'DRVR'); /* desk accessories */
myMenus[2] = GetMenu(fileMenu);
myMenus[3] = GetMenu(editMenu);
myMenus[4] = GetMenu(mungeMenu);
myMenus[5] = GetMenu(selectMenu);
myMenus[6] = GetMenu(makeNewMenu);
for (i = 1; i <= lastMenu; i++)
InsertMenu(myMenus[i], 0);
DrawMenuBar();
} /* end of SetUpMenus() */
void
CloseTheList()
{
if (myList != NIL) {
LDispose(myList);
CloseWindow(myWindow);
myList = NIL;
myWindow = NIL;
};
} /* end of closeTheList() */
void
OpenNewList()
{
myWindow = GetNewWindow(256,&wRecord,-1L);
SetPort(myWindow);
BlockMove(&myWindow->portRect, &r, (long)sizeof(Rect));
/* because drawGrowIcon draws lines for both scrollbars, we must
* leave room for both of them if we have a grow box (oh well...)
*/
if (sVert || groBox)
r.right = r.right-15; /* room for vert scroll bar */
if (sHoriz || groBox)
r.bottom = r.bottom-15; /* leave room for horiz scroll bar */
SetRect(&b,0,0,10,50); /* make room for 10*50 items */
cSize.v = (r.bottom-r.top) / 16; /* 16 rows */
cSize.h = (r.right-r.left) / 5; /* and 5 columns */
/* make a list with content r, bounds b, proc 0, and two scrollbars;
* drawing false
*/
myList=LNew(&r,&b,pass(cSize),0,myWindow,FALSE,groBox,sHoriz,sVert);
SetCellValues(); /* number all the cells */
LDoDraw(drawIt,myList); /* set draw state for update */
} /* end of OpenNewList() */
void
DoCommand(mResult)
long mResult;
{
char name[len255];
int i,temp;
Cell c;
Boolean b;
theMenu = (mResult >> 16) & 0xFFFF;
theItem = mResult & 0xFFFF;
switch (theMenu) {
case appleMenu:
GetItem(myMenus[1],theItem,name);
refNum = OpenDeskAcc(name);
break;
case fileMenu:
doneFlag = TRUE; /* Quit */
break;
case editMenu:
if (!SystemEdit(theItem-1)) {
SetPort(myWindow);
CellValue(&cc,0,0);
switch (theItem) {
case 3: /*cut*/
while (LGetSelect(TRUE,&cc,myList)) {
cutStrLen = len255; /*cut up to 255 chars*/
LGetCell(cutStr+1,&cutStrLen,pass(cc),myList);
LClrCell(pass(cc),myList);
b = LNextCell(TRUE,TRUE,&cc,myList);
};
break;
case 4: /*copy*/
if (LGetSelect(TRUE,&cc,myList)) {
cutStrLen = len255; /*copy up to 255 chars*/
LGetCell(cutStr+1,&cutStrLen,pass(cc),myList);
};
break;
case 5: /*paste*/
while (LGetSelect(TRUE,&cc,myList)) {
LSetCell(cutStr+1,cutStrLen,pass(cc),myList);
b = LNextCell(TRUE,TRUE,&cc,myList);
};
break;
case 6: /* find first */
if (LSearch(cutStr+1,cutStrLen,NIL,
&cc,myList)) {
ClrAllCells();
LSetSelect(TRUE,pass(cc),myList);
LAutoScroll(myList);
};
break;
case 7: /* find all */
ClrAllCells();
while (LSearch(cutStr+1,cutStrLen,NIL,&cc,myList)) {
LSetSelect(TRUE,pass(cc),myList);
b = LNextCell(TRUE,TRUE,&cc,myList);
};
break;
}; /* of theItem */
}; /* of editMenu */
break;
case mungeMenu:
CellValue(&c,0,0); /*search for first selected cell*/
if (!LGetSelect(TRUE,&c,myList))
CellValue(&c,(*myList)->dataBounds.right,
(*myList)->dataBounds.bottom);
switch (theItem) {
case 1: /* Add Row */
c.h = CountSelections();
if (c.h == 0)
c.h = 1;
c.v = LAddRow(c.h,c.v,myList);
c.h = 0;
do { /* increment h */
SetCell(&c);
} while (LNextCell(TRUE,FALSE,&c,myList));
break;
case 2: /* Delete Row */
LDelRow(CountSelections(),c.v,myList);
break;
case 3: /* Add Column */
c.v = CountSelections();
if (c.v == 0)
c.v = 1;
c.h = LAddColumn(c.v,c.h,myList);
c.v = 0;
do { /* increment v */
SetCell(&c);
} while (LNextCell(FALSE,TRUE,&c,myList));
break;
case 4: /* Delete Column */
LDelColumn(CountSelections(),c.h,myList);
break;
case 5: /* Clear Selections */
ClrAllCells();
break;
case 6: /* Renumber Cells */
LDoDraw(FALSE,myList);
SetCellValues();
LDoDraw(drawIt,myList);
InvalRect(&myWindow->portRect);
break;
case 7: /* Update List */
InvalRect(&myWindow->portRect);
break;
}; /*of theItem */
break;
case selectMenu:
GetItemMark(myMenus[5],theItem,&theMark);
tempFlags = (*myList)->selFlags;
if (theMark == checkMark) {
theMark = '\0';
/* ClearBit(tempFlags,theItem-1); */
tempFlags &= ~(1 << (theItem-1));
}
else {
theMark = checkMark;
/* SetBit(tempFlags,theItem-1); */
tempFlags |= 1 << (theItem-1);
};
SetItemMark(myMenus[5],theItem,theMark);
tFlag = tempFlags & 0xFFFF;
(*myList)->selFlags = (Byte)tFlag;
break;
case makeNewMenu:
GetItemMark(myMenus[6],theItem,&theMark);
switch (theItem) {
case 1: /* New List */
CloseTheList();
OpenNewList();
break;
case 2: /* Horizontal Scroll */
if (theMark == checkMark) {
theMark = '\0';
sHoriz = FALSE;
}
else {
theMark = checkMark;
sHoriz = TRUE;
};
break;
case 3: /* Vertical Scroll */
if (theMark == checkMark) {
theMark = '\0';
sVert = FALSE;
}
else {
theMark = checkMark;
sVert = TRUE;
};
break;
case 4: /* Grow Box */
if (theMark == checkMark) {
theMark = '\0';
groBox = FALSE;
}
else {
theMark = checkMark;
groBox = TRUE;
};
break;
case 5: /* Drawing On */
if (theMark == checkMark) {
theMark = '\0';
drawIt = FALSE;
LDoDraw(drawIt,myList);
}
else {
theMark = checkMark;
drawIt = TRUE;
LDoDraw(drawIt,myList);
};
break;
}; /* theItem switch*/
SetItemMark(myMenus[6],theItem,theMark);
break;
}; /* of menu switch */
HiliteMenu(0);
} /* end of DoCommand() */
main()
{
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent,0);
InitWindows();
SetUpMenus();
TEInit();
InitDialogs(NIL);
InitCursor();
InitPack(0); /* so list manager package gets initialized */
h1 = GetResource('PACK',0);
/* Note -- if you should want to un-comment the following, Aztec
* C version 1.06G does not support MoveHHi -- it may be trivial
* for the 128K ROMs (just pop the return address to A1, pop the
* argument to A0, trap $A064, JMP (A1) -- maybe). It appears
* the 64K ROMs version of MoveHHi is about 500 bytes of code.
*/
/* MoveHHi(h1); HLock(h1); if you want to move it out of the way */
sizeLimits.left = 60; /* set minimum window size to 60x40*/
sizeLimits.top = 40;
sizeLimits.right = 500; /* and max size to 500x300 */
sizeLimits.bottom = 300;
doneFlag = FALSE;
groBox = FALSE;
sHoriz = FALSE;
sVert = FALSE;
drawIt = FALSE;
cutStr[0] = '\0';
cutStrLen = 0; /* init string for cut/copy/paste */
SetRect(&dragRect,4,24,
screenBits.bounds.right-4,screenBits.bounds.bottom-4);
OpenNewList(); /* make a new window (myWindow)
* and a new list (myList)
*/
do {
SystemTask();
if (GetNextEvent(everyEvent,&myEvent)) {
switch (myEvent.what) {
case mouseDown:
code = FindWindow(pass(myEvent.where),&whichWindow);
switch (code) {
case inMenuBar:
DoCommand(MenuSelect(&myEvent.where));
break;
case inSysWindow:
SystemClick(&myEvent,whichWindow);
break;
case inDrag:
DragWindow(whichWindow,
pass(myEvent.where),&dragRect);
break;
case inGoAway:
if (TrackGoAway(myWindow,pass(myEvent.where)))
CloseTheList();
break;
case inContent:
case inGrow:
if (code == inGrow && groBox == TRUE) {
newSize = GrowWindow(whichWindow,
pass(myEvent.where),&sizeLimits);
if (newSize != 0) {
i = newSize & 0xFFFF;
j = (newSize >> 16) & 0xFFFF;
SizeWindow(whichWindow,i,j,TRUE);
if (sHoriz || groBox)
i = i - 15;
if (sVert || groBox)
j = j - 15;
LSize(i,j,myList);
if (groBox)
DrawGrowIcon(myWindow);
};
}
else {
if (whichWindow != FrontWindow())
SelectWindow(whichWindow);
else {
GlobalToLocal(&myEvent.where);
if (LClick(pass(myEvent.where),
myEvent.modifiers, myList)) {
FlipSelections();
/*and invalidate next dclick */
(*myList)->clikTime -=
GetDblTime();
};
};
};
break;
}; /* of code switch */
break;
case keyDown:
case autoKey:
if (myWindow == FrontWindow())
;
break;
case activateEvt:
LActivate(myEvent.modifiers & 1, myList);
if (groBox)
DrawGrowIcon(myWindow);
break;
case updateEvt:
if (myWindow != NIL) {
SetPort(myWindow);
BeginUpdate(myWindow);
if (groBox)
DrawGrowIcon(myWindow);
LUpdate(thePort->visRgn,myList);
EndUpdate(myWindow);
};
break;
}; /* of event switch */
}; /*
end if(GetNextEvent...) */
} while (!doneFlag);
CloseTheList();
ExitToShell();
} /* end of main() */