From c4d8ea9c08aa9adfa87066900167d449c72c8fe2 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Tue, 6 May 2025 23:21:51 +0100 Subject: [PATCH] cleaned up key handling; added delete entry panel --- app/appui.cls | 205 +++++++++++++++++++++++++++++---------------- db/contacts.sqlite | Bin 9216 -> 10240 bytes 2 files changed, 135 insertions(+), 70 deletions(-) diff --git a/app/appui.cls b/app/appui.cls index 71e282a..878d8c9 100644 --- a/app/appui.cls +++ b/app/appui.cls @@ -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 diff --git a/db/contacts.sqlite b/db/contacts.sqlite index af99a28c891bdfecf5dfc3a152dc11ad3516b7b5..75334250164610bd828816dd3c1cb96727936d5b 100644 GIT binary patch literal 10240 zcmeHN-*4MQ9KW;MbXzH7nz~R`Wrm`4(Yle?iPL&RTH3m8wl*zkHdY3T+{8)E(zvyq zvJ8pQAtA&AFZ>I8fPa8Tgv8S%{stso_QuPe0r9=FW9OW(mC{Y3OKjJ^kMHh#_x)w} zK3;oo$#8U`Vp?08BS@$ZVT`T{0z&9E94EYkhjX8Iz={5F>*6Wr+vwuszJ5}Hv_@zE z|ApVeZ_w-i$Dh8v^}d1Vg$wwB;b z(k|W0`IWqyUC7ZM&IgTRLSCI$^J;EZT@yUm2FWpDB`3_OODYU+HnTRHnNwqX1%4np zF@Yac>D;zA&AMK!H?}r(%Vr9F%)FRehcPQxV^m<7T z6?tZ2p_}LH+Lk^EEn|B(@j!HJ3_rL}`)sMTb>^5}TIfDCQ!y;tDH2hx0%{slELpnd z=;b1WXCb?+t`#!Nt6<0Z%#EdjFnc2pmM#?iJClv=^1rAa8{%X%h#q3}5dDgycm;on zf5LxoJKXpD2>*!xhHj709#WqYuF9zhiVaCpVN(}!*(Jf;_*gGFVq8pJjFVjuMI|LE zsZTA$;q@!(WN`&3*VG2jk@R%emSbB9qYdD*RZngK6Ae|?~E>N0_ppmE~WoqEj z&b(2tH$$~#B_3`_Aeje|WK_ECtU@rD^^SJQbQdVq2Lew^(jvrAOjwl5C`6;~2dN-}5{K$p+JILAGwGfk2a>t?vIm~8hu+L%HREA|*z#n#Fd z%_c{sOs-kcsgBmr?(zs&WX>!(ruD(5v8z=B{kTe;R>Z&&0x0zk=m+L`tzkP_4w6Z` zEO0aM0jUc#1R^p21i{|>^)%4az>(L$E0`a{tyh!;iGT0i{K(I|H^H6;dKzFg(2IXo zAH7mf122;Xdh!1#(j*n4Oa0G~Fv$zynH-g4-q( zYQ>US_JESvs5_Qd@emlGA+Cf@;6a23v5G&#Kj7cF3GQ3&7ycN36}G$lkFfIZZk>t2 zM}9_Ep5yv|z#y<|?vzwWrg0iw8UpsgU)^;123Rof2F<-1K|}9CZI@wy^=+~fwxLHe zJRh2Yzl-80PfF6Qb=E?0Sg~g~6cA6K1Je@9aagi{6IcmHv+`P5lqCb06jo>=tpLC< zQxtm9v;>6CSKZxEl;b-)JM0is2_+Fy6@{RenP-q^&j_9&ZWl#3j5y3qbKmi2_-nxW zwQcB@zxmO=uZt(>LnbIGIg*-EB!;|*D-=RcLLuzdmVuq|6%jyJga|qayFy5igvjmU z{7Fa`qoJN9ppX~{G$RiKqf#*&R>jy22o*QK0B;mbC4ivS`{P+j`ouK1?I!U-y{t8D zU;<^y-wnUq1P&^oNhQMb`Mf7Jod-d;>)8ny_3ezm)6C`OFYhlfP1UkpsaEWAwVmfm zOhYnM+OnrCv>