cleaned up key handling; added delete entry panel

This commit is contained in:
Greg Gauthier 2025-05-06 23:21:51 +01:00
parent beb22585a2
commit c4d8ea9c08
2 changed files with 135 additions and 70 deletions

View File

@ -71,6 +71,12 @@
when clrset = 5 then panel~init_pair(6, panel~COLOR_BLACK, panel~COLOR_GREEN)
/* black text on cyan background */
when clrset = 6 then panel~init_pair(7, panel~COLOR_BLACK, panel~COLOR_CYAN)
/* Yellow text on blue background */
when clrset = 7 then panel~init_pair(8, panel~COLOR_YELLOW, panel~COLOR_BLUE)
/* Cyan text on black background */
when clrset = 8 then panel~init_pair(9, panel~COLOR_CYAN, panel~COLOR_BLACK)
/* blacken everything to give the appearance of disappearance */
when clrset = 9 then panel~init_pair(10, panel~COLOR_BLACK, panel~COLOR_BLACK)
/* Default to white text on black background */
OTHERWISE DO
clrset=0
@ -97,8 +103,8 @@
with_box = .true
menuwin = self~DrawSubPanel(menu_height, menu_width, start_y, start_x, clrset, title, with_box)
menu_items = .array~of("[A]dd Contact", "[R]emove Contact", "[E]dit Contact", "[S]earch", "[L]ist All", "[Q]uit")
menu_keys = .array~of("a", "r", "e", "s", "l", "q")
menu_items = .array~of("[A]dd Contact", "[D]elete Contact", "[E]dit Contact", "[S]earch", "[L]ist All", "[Q]uit")
menu_keys = .array~of("a", "d", "e", "s", "l", "q")
/* Display menu items */
.environment~selected = 1
@ -125,8 +131,23 @@
menuwin~refresh
return
::method dropWindow
expose win menuwin
use arg window
self~SetPanelColor(window, 9)
window~erase()
window~refresh()
window~delwin()
win~refresh
self~setupMainMenu(win)
menuwin~refresh()
RETURN
/**********************
* Add new contact *
**********************/
::method addContactPanel
expose win db menuwin menu_items formwin
expose win db menuwin formwin
/* Create a form panel */
max_y = win~lines
@ -147,58 +168,112 @@
/* Input fields */
firstName = self~getInputField(formwin, 3, 14, 30)
if firstName = .nil then RETURN
if firstName = .nil then do
self~dropWindow(formwin)
RETURN
END
lastName = self~getInputField(formwin, 4, 14, 30)
if lastName = .nil then return
if lastName = .nil then do
self~dropWindow(formwin)
RETURN
END
phone = self~getInputField(formwin, 5, 14, 15)
if phone = .nil then return
if phone = .nil then do
self~dropWindow(formwin)
RETURN
END
email = self~getInputField(formwin, 6, 14, 30)
if email = .nil then return
if email = .nil then do
self~dropWindow(formwin)
RETURN
END
/* Add to database */
contactDict = .Directory~new()
contactDict["FIRST_NAME"] = firstName
contactDict["LAST_NAME"] = lastName
contactDict["PHONE"] = phone
contactDict["EMAIL"] = email
result = db~addContact(contactDict)
if firstname = "" | lastname = "" then do
formwin~mvaddstr(8, 2, "First and Last names are required.")
formwin~refresh()
call SysWait 1.5
END /* don't add contact */
else DO
/* Add to database */
contactDict = .Directory~new()
contactDict["FIRST_NAME"] = firstName
contactDict["LAST_NAME"] = lastName
contactDict["PHONE"] = phone
contactDict["EMAIL"] = email
result = db~addContact(contactDict)
/* Display result message */
if result > 0 then do
formwin~mvaddstr(8, 2, "Contact ID ["result"] added successfully!")
end
else do
formwin~mvaddstr(8, 2, "Failed to add contact.")
end
formwin~refresh()
call SysWait 1
self~dropAddWindow
/* Display result message */
if result > 0 then do /* should be a result id number */
formwin~mvaddstr(8, 2, "Contact ID ["result"] added successfully!")
formwin~refresh()
call SysWait 1.5
end
else do
formwin~mvaddstr(8, 2, "Failed to add contact.")
formwin~refresh()
call SysWait 1.5
end
end /* add contact */
self~dropWindow(formwin)
return
::method dropAddWindow
expose win menuwin formwin
formwin~erase()
formwin~refresh()
self~setupMainMenu(win)
menuwin~refresh()
/****************************
* Delete Contact Panel *
****************************/
::METHOD deleteContactPanel
expose win db menuwin delwin
/* Create a form panel */
max_y = win~lines
max_x = win~cols
form_height = 14
form_width = 35
start_y = (max_y - form_height) % 2
start_x = (max_x - form_width) % 2
delwin = self~DrawSubPanel(form_height, form_width, start_y, start_x, 0, "Delete A Contact", .true)
delwin~mvaddstr(3, 2, "Contact ID: ")
delwin~mvaddstr(form_height-2, 2, "[Enter] to save, [Esc] to cancel")
delwin~refresh()
contactId = self~getInputField(delwin, 3, 14, 10)
if contactId = .nil then do
self~dropWindow(delwin)
RETURN
END
result = db~deleteContact(contactId)
if result = 0 then do /* should be a result id number */
delwin~mvaddstr(8, 2, "Contact deleted successfully!")
delwin~refresh()
call SysWait 1.5
end
else do
delwin~mvaddstr(8, 2, "Failed to add contact.")
delwin~refresh()
call SysWait 1.5
end
self~dropWindow(delwin)
RETURN
/****************************
* List all contacts *
****************************/
::method listAllContactsPanel
expose win db menuwin
/* Create a list panel */
max_y = win~lines
max_x = win~cols
list_height = 20
list_height = max_y - 5
list_width = 70
start_y = (max_y - list_height) % 2
start_x = (max_x - list_width) % 2
listwin = self~DrawSubPanel(list_height, list_width, start_y, start_x, 0, "All Contacts", .true)
listwin = self~DrawSubPanel(list_height, list_width, start_y, start_x, 1, "All Contacts", .true)
listwin~scrollok(.true)
listwin~Setscrreg(4,18)
/* Display column headers */
listwin~attron(listwin~A_BOLD)
@ -224,27 +299,26 @@
listwin~mvaddstr(i+3, 54, contact['email']) */
/* Break if we run out of screen space */
if i > list_height-6 then leave
if i > list_height-7 then LEAVE
end
end
else do
listwin~mvaddstr(5, 5, "No contacts found.")
end
listwin~mvaddstr(list_height-2, 2, "Press any key to return to main menu")
/*
listwin~mvaddstr(list_height-1, 2, "Press any key to return to main menu")
listwin~refresh()
*/
/* Wait for any key */
listwin~getch()
self~dropWindow(listwin)
RETURN
Return
listwin~erase()
listwin~refresh()
/* Redraw the main menu */
win~refresh
self~setupMainMenu(win)
return
/****************************
* Search for contact *
****************************/
::method searchContactPanel
expose win db menuwin menu_items
@ -255,7 +329,7 @@
search_width = 50
start_y = (max_y - search_height) % 2
start_x = (max_x - search_width) % 2
searchwin = self~DrawSubPanel(search_height, search_width, start_y, start_x, 3, "Search Contacts", .true)
searchwin = self~DrawSubPanel(search_height, search_width, start_y, start_x, 7, "Search Contacts", .true)
searchwin~mvaddstr(3, 2, "Search term: ")
searchwin~refresh()
@ -265,10 +339,7 @@
/* If canceled, return to main menu */
if term = .nil then do
searchwin~erase()
searchwin~refresh()
self~setupMainMenu(win)
menuwin~refresh()
self~DropWindow(searchwin)
return
end
@ -277,6 +348,7 @@
searchwin~refresh()
/* Display results in a new window */
self~dropWindow(searchwin)
self~displaySearchResults(term)
return
@ -290,6 +362,9 @@
RETURN
/************************
* Get Input From Field *
************************/
::method getInputField
use arg win, y, x, maxlen
@ -297,16 +372,17 @@
win~curs_set(1) /* Show cursor */
win~keypad(1) /* Enable function keys and arrow keys */
win~echo() /* Show typed characters */
win~raw
/* win~raw */
buffer = ""
do forever
key = win~getch()
select
when key = 27 then do /* ESC key */
win~noecho() /* Stop showing typed characters */
when key = D2C(27) then do /* ESC key */
win~curs_set(0) /* Hide cursor */
win~noecho() /* Stop showing typed characters */
win~mvaddstr(8, 2, "Decimal: "C2D(key) )
return .nil /* Return nil to indicate cancellation */
end
when key = D2C(10) | key = D2C(13) then do /* Enter key */
@ -392,31 +468,20 @@
use arg menuwin, key_char
select
when key_char = 'a' then do
menuwin~mvaddstr(19 - 3, 5, "I would launch the ADD panel ");
menuwin~refresh
call SysWait 0.25
self~addContactPanel()
END
when key_char = 'r' then do
menuwin~mvaddstr(19 - 3, 5, "I would launch the REMOVE panel ");
menuwin~refresh
call SysWait 0.25
when key_char = 'd' then do
self~deleteContactPanel()
END
when key_char = 'e' then do
menuwin~mvaddstr(19 - 3, 5, "I would launch the EDIT panel ");
menuwin~mvaddstr(19 - 3, 5, "TODO: Create an Edit Panel ");
menuwin~refresh
call SysWait 0.25
END
when key_char = 's' then do
menuwin~mvaddstr(19 - 3, 5, "I would launch the SEARCH panel ");
menuwin~refresh
call SysWait 0.25
self~searchContactPanel()
END
when key_char = 'l' then do
menuwin~mvaddstr(19 - 3, 5, "I would launch the LIST panel ")
menuwin~refresh
call SysWait 0.25
self~listAllContactsPanel()
END
when key_char = 'q' then do

Binary file not shown.