/* * Handles editing of individual entries. */ #include #include "flipMain.h" #include "palmUtil.h" #include "flipEdit.h" #include "resdefs.h" static Boolean EditEventHandler(EventPtr event); static void WordEditFormInit(void); static void WordEditFormDeinit(void); struct FormInfo EditFormInfo = { EventStealer: NULL, EventHandler: EditEventHandler, InitForm: WordEditFormInit, DeinitForm: WordEditFormDeinit, UpdateForm: NULL, }; /* The currently edited record index. */ static UInt16 editIndex; /* The currently edited record handle. */ static MemHandle editRecord; /* The currently edited unpacked record. */ static UnpackedWord editWord = INVALID_UNPACKED_WORD; /* Sets the record to edit and opens the edit form. */ extern void EditRecord(UInt16 recordIndex) { editIndex = recordIndex; editRecord = DmGetRecord(wordDB, editIndex); ErrFatalDisplayIf(!editRecord, "No such record"); FrmGotoForm(WordEditForm); } /* Adds a new empty record and starts editing it. */ extern void AddAndEditWord(void) { UInt16 index; AddWord(wordDB, &index, "", ""); EditRecord(index); } static void SetFieldTextHandles(MemHandle h1, MemHandle h2) { FldSetTextHandle(GetObjectPtr(WordEditWordField1), h1); FldSetTextHandle(GetObjectPtr(WordEditWordField2), h2); } /* Initializes all fields. */ static void WordEditFormInit(void) { UnpackWord(editRecord, &editWord); SetFieldTextHandles(editWord.word1, editWord.word2); CtlSetValue(GetObjectPtr(WordEditKnownBox), editWord.known); } /* Deletes the currently edited record. */ static void EditDeleteRecord(void) { SetFieldTextHandles(NULL, NULL); FreeUnpackedWord(&editWord); DmReleaseRecord(wordDB, editIndex, true); RemoveWord(wordDB, editIndex); FrmGotoForm(FlipForm); } /* Saves all fields. */ static void WordEditFormDeinit(void) { FieldPtr fld1; FieldPtr fld2; UInt16 len1; UInt16 len2; if (!editWord.valid) /* Not editing a valid word, nothing to save */ return; fld1 = GetObjectPtr(WordEditWordField1); fld2 = GetObjectPtr(WordEditWordField2); len1 = FldGetTextLength(fld1); len2 = FldGetTextLength(fld2); if (len1 == 0 && len2 == 0) { /* delete record since it is empty */ EditDeleteRecord(); return; } else if (len1 == 0 || len2 == 0) { /* notify user that there must be something in both fields */ } FldCompactText(fld1); FldCompactText(fld2); FldSetTextHandle(fld1, NULL); FldSetTextHandle(fld2, NULL); editWord.known = CtlGetValue(GetObjectPtr(WordEditKnownBox)); PackWord(editRecord, &editWord); DmReleaseRecord(wordDB, editIndex, true); } static Boolean EditEventHandler(EventPtr event) { FormPtr frm; UInt16 choice; frm = FrmGetActiveForm(); if (event->eType == ctlSelectEvent) { switch (event->data.ctlSelect.controlID) { case WordEditDoneButton: FrmGotoForm(FlipForm); return true; case WordEditDeleteButton: choice = FrmAlert(WordDeleteAlert); if (choice == WordDeleteAlertYes) { EditDeleteRecord(); } return true; } } return false; }