//ignore above symbols
#include "DocArranger.h"
// Globals
Char **gDocListChoices = NULL;
Int16 gDocListNumItems = 0;
UInt32 searchType = 'TEXt';
Boolean fUpdateScreen = true;
/*
* cleanUpList()
* Free up the memory blocks held by listItems.
*/
static void cleanUpList(UInt16 listID)
{
FormType * form = FrmGetActiveForm();
ListType * list = FrmGetObjectPtr(form, FrmGetObjectIndex(form, listID));
UInt16 i = 0;
if (gDocListNumItems) {
for (i = 0; i < gDocListNumItems; i++) {
MemPtrFree((MemPtr) gDocListChoices[i]);
}
MemPtrFree((MemPtr) gDocListChoices);
gDocListChoices = 0;
}
}
/*
* updateList()
* Highlight the item selected by the user
* TODO - Case insensitive search.
*/
static void updateList(UInt16 listID, UInt16 fieldID)
{
UInt16 i; // general-purpose index
Char* input; // Text entered on the text field.
Char* filename; // String on the list item.
Char buf1[32], buf2[32];
FormType * form = FrmGetActiveForm();
ListType * list = FrmGetObjectPtr(form, FrmGetObjectIndex(form, listID));
FieldType * field = FrmGetObjectPtr(form, FrmGetObjectIndex(form, fieldID));
// If nothing on the field, bail out.
if ( FldGetTextLength(field) == 0 ) {
return;
}
// Retrieve the string on the field.
input = FldGetTextPtr(field);
StrToLower(buf1, input);
// Navigate each item on the list and look for the string
for (i = 0; i < LstGetNumberOfItems(list); i++) {
filename = LstGetSelectionText(list, i);
StrToLower(buf2, filename);
// Found. Set the entry to be on the top of the box and highlight.
if (StrStr(buf2, buf1) != NULL) {
LstSetTopItem(list, i);
LstDrawList(list);
LstSetSelection(list, i);
break;
}
}
}
/*
* drawMenu()
*/
static Boolean drawMenu(UInt16 command)
{
Boolean handled = false;
switch (command)
{
case OptionsMenuAbout:
FrmCustomAlert(AlertAboutListBox, "동백2호", NULL, NULL);
handled = true;
break;
default:
break;
}
return handled;
}
/*
* FillListWithFileNames()
* Fill the list specified by 'list', with the files whose type is 'type'.
*
* TODO: Need to check MemPtrNew's returning NULL on listItems, etc.
*/
static void FillListWithFileNames(ListType *list, UInt32 type)
{
Char** listItems; // dreaded char **
Int16 i; // general-purpose index
UInt16 card;
LocalID dbID;
DmSearchStateType searchState;
Err error;
UInt16 counter = 0; // total number of files
Boolean boolean = true;
// Search the DB for 'TEXt' file
error = DmGetNextDatabaseByTypeCreator(true, &searchState, type, NULL, false, &card, &dbID);
if (error == errNone)
{
do {
// Found one; increment the counter;
counter++;
// then look for the next one, until there's no more
error = DmGetNextDatabaseByTypeCreator(false, &searchState, type, NULL, false, &card, &dbID);
} while (error == errNone);
}
// Allocate memory for char **. We need 'counter' number of elements in char **,
// each of which will be a pointer to Char type.
listItems = (Char **) MemPtrNew(counter * sizeof (Char *));
// Save the allocated block, as well as the total number, so that we can free them later.
// Otherwise memory leak won't be avoided.
gDocListChoices = listItems;
gDocListNumItems = counter;
// Now for each element in listItems, allocate memory for holding char *.
for (i = 0; i < counter; i++) {
// Since most filenames are less than 32 charaters long, let's allocate 32 bytes.
listItems[i] = (Char *) MemPtrNew(32);
// Now we need to actually know what the name of file is.
error = DmGetNextDatabaseByTypeCreator (boolean, &searchState, type, NULL,false, &card, &dbID);
// If not found, let's get outta here.
if (error != errNone) {
break;
}
error = DmDatabaseInfo(card, dbID, listItems[i], NULL,NULL, NULL, NULL, NULL, NULL, NULL,NULL,NULL, NULL);
// We're setting the variable 'boolean' to false, so that we won't start
// a brand new search on the next iteration.
boolean = false;
}
// Done. Fill the list with listItems.
LstSetListChoices(list, listItems, counter);
}
/*
* mainFormInit()
* Initialize the form by displaying the list box.
*/
static void mainFormInit(FormType * form)
{
// Find the list refered to as 'MainList' in the form.
ListType * list = (ListType *) FrmGetObjectPtr(form, FrmGetObjectIndex (form, MainFormList));
// Fill up the list with file names whose type is 'TEXt';
// searchType has been defined globally (see ListBox.h)
FillListWithFileNames(list, searchType);
// Draw the list, and set the highlight on the first item of the list.
LstDrawList(list);
LstSetSelection(list, 0);
}
/*
* mainFormHandleEvent
*
*/
static Boolean mainFormHandleEvent(EventType *event)
{
UInt16 fieldIndex;
Int len;
static int lastLen;
Char *pStr;
Boolean handled = false;
FormType *form = NULL;
FieldType *field = NULL;
switch (event->eType)
{
case frmOpenEvent:
// Retrieve the main form, draw it, and initialize it
// (which actually draws the listbox)
form = FrmGetActiveForm();
FrmDrawForm(form);
mainFormInit(form);
// For focusing on the main text field
// FldFreeMemory() is unnecessary with CodeWarriori-compiled code.
fieldIndex = FrmGetObjectIndex(form, MainFormTextField);
field = FrmGetObjectPtr(form, fieldIndex);
FldFreeMemory(field);
FrmSetFocus(form, fieldIndex);
handled = true;
break;
case frmCloseEvent:
// Free up the memory
cleanUpList(MainFormList);
break; //remember to leave handled as false
case ctlSelectEvent:
switch (event->data.ctlSelect.controlID)
{
// The button has been tapped.
case MainFormOKButton:
updateList(MainFormList, MainFormTextField);
handled = true;
break;
default:
break;
}
// When 'Enter' key has been entered.
case keyDownEvent:
/*
if (event->data.keyDown.chr == linefeedChr) {
updateList(MainFormList, MainFormTextField);
handled = true;
break;
}
*/
fUpdateScreen = true;
break;
case nilEvent:
form = FrmGetActiveForm();
fieldIndex = FrmGetObjectIndex(form, MainFormTextField);
field = FrmGetObjectPtr(form, fieldIndex);
pStr = FldGetTextPtr(field);
len = 0;
if (pStr) { len = StrLen(pStr); }
if (len != lastLen) {
updateList(MainFormList, MainFormTextField);
lastLen=len;
fUpdateScreen = false;
}
handled = true;
break;
// For menu bar
case menuEvent:
handled = drawMenu(event->data.menu.itemID);
break;
default:
break;
}
return handled;
}
/*
* AppStart()
*/
static Err AppStart(void)
{
return errNone;
}
/*
* AppStop()
*/
static void AppStop(void)
{
FrmCloseAllForms();
}
/*
* AppHandleEvent
*/
static Boolean AppHandleEvent(EventType *event)
{
FormType *form;
UInt16 formID;
Boolean handled = false;
if (event -> eType == frmLoadEvent)
{
formID = event -> data.frmLoad.formID;
form = FrmInitForm(formID);
FrmSetActiveForm(form);
switch (formID)
{
case MainForm:
FrmSetEventHandler(form, mainFormHandleEvent);
break;
default:
break;
}
handled = true;
}
return handled;
}
/*
* AppEventLoop()
*/
static void AppEventLoop(void)
{
EventType event;
Err error;
do
{
EvtGetEvent(&event, (fUpdateScreen ? 0 : evtWaitForever));
if (! SysHandleEvent(&event))
if (! MenuHandleEvent(0, &event, &error))
if (! AppHandleEvent(&event))
FrmDispatchEvent(&event);
} while (event.eType != appStopEvent);
}
/*
* PilotMain()
*/
UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
Err error;
switch (cmd)
{
case sysAppLaunchCmdNormalLaunch:
error = AppStart();
if (error) return error;
FrmGotoForm(MainForm);
AppEventLoop();
AppStop();
break;
default:
break;
}
return error;
}
// ignore below symbols