home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_06 / 1n06049a < prev    next >
Text File  |  1990-10-03  |  12KB  |  419 lines

  1. //  CONTACTS    v1.00
  2. //      Borland Turbo C++ 1.01
  3. //      Zinc Interface Library v1.0
  4. //
  5. //      Maintains a simple database of contact information.
  6. //
  7. //      Written by Scott Robert Ladd.
  8.  
  9. #pragma warn -wpar
  10.  
  11. #include "fcntl.h"
  12. #include "sys\stat.h"
  13. #include "io.h"
  14. #include "string.h"
  15. #include "ui_win.hpp"
  16. #include "contacts.hlh"
  17.  
  18. // database record!
  19. struct ContactRec
  20.     {
  21.     char LastName[20];
  22.     char FirstName[20];
  23.     char Company[40];
  24.     char Address1[40];
  25.     char Address2[40];
  26.     char City[16];
  27.     char State[16];
  28.     char ZIP[12];
  29.     char Phone[10];
  30.     };
  31.  
  32. // create a new window type for input of records
  33. class ContactList : public UIW_WINDOW
  34.     {
  35.     private:
  36.         static int  FileHandle;
  37.         static int  IsNew;
  38.         static long RecNo;
  39.  
  40.         static ContactRec Record;
  41.         static ContactRec TempRec;
  42.  
  43.         static void ReadRecord();
  44.         static void WriteRecord();
  45.  
  46.     public:
  47.         ContactList(char *filename, int left, int top);
  48.  
  49.         ~ContactList();
  50.  
  51.         static void AddRecord (void * item, UI_EVENT &event);
  52.         static void NextRecord(void * item, UI_EVENT &event);
  53.         static void PrevRecord(void * item, UI_EVENT &event);
  54.  
  55.         static int GetHandle()
  56.             {
  57.             return FileHandle;
  58.             }
  59.     };
  60.  
  61. // global values
  62.  
  63. UI_DISPLAY *         Display;
  64. UI_EVENT_MANAGER *   EventManager;
  65. UI_WINDOW_MANAGER *  WindowManager;
  66. UI_ERROR_SYSTEM *    ErrorSys;
  67. UI_HELP_SYSTEM *     HelpSys;
  68. UIW_PULL_DOWN_MENU * CtrlMenu;
  69. UIW_POP_UP_ITEM *    OptionClose;
  70. ContactList *        CList = NULL;
  71.  
  72. // constructor
  73. ContactList::ContactList(char *filename, int left, int top) :
  74.     UIW_WINDOW(left - 28,top - 7,56,14,WOF_NO_FLAGS,WOAF_NO_FLAGS,HELP_RECORD)
  75.     {
  76.     IsNew = TRUE;
  77.  
  78.     Record.LastName[0]  = '\000';
  79.     Record.FirstName[0] = '\000';
  80.     Record.Company[0]   = '\000';
  81.     Record.Address1[0]  = '\000';
  82.     Record.Address2[0]  = '\000';
  83.     Record.City[0]      = '\000';
  84.     Record.State[0]     = '\000';
  85.     Record.ZIP[0]       = '\000';
  86.  
  87.     strcpy(Record.Phone, "..........");
  88.  
  89.     TempRec = Record;
  90.  
  91.     // Open the file and read the first Record.
  92.     FileHandle = open(filename, O_RDWR);
  93.  
  94.     if (FileHandle < 0)
  95.         FileHandle = open(filename, O_CREAT, S_IREAD | S_IWRITE);
  96.  
  97.     if (FileHandle < 0)
  98.         return;
  99.  
  100.     ReadRecord();
  101.  
  102.     UIW_PULL_DOWN_MENU *menu = new UIW_PULL_DOWN_MENU(0, WOF_NO_FLAGS, WOAF_NO_FLAGS);
  103.  
  104.     *menu
  105.         + new UIW_PULL_DOWN_ITEM(" ~Previous ", MNIF_NO_FLAGS,
  106.             ContactList::PrevRecord)
  107.         + new UIW_PULL_DOWN_ITEM(" ~Next ", MNIF_NO_FLAGS,
  108.             ContactList::NextRecord)
  109.         + new UIW_PULL_DOWN_ITEM(" ~Add ", MNIF_NO_FLAGS,
  110.             ContactList::AddRecord);
  111.  
  112.     *this
  113.         + new UIW_BORDER
  114.         + new UIW_MAXIMIZE_BUTTON
  115.         + new UIW_MINIMIZE_BUTTON
  116.         + new UIW_SYSTEM_BUTTON
  117.         + new UIW_TITLE(filename, WOF_JUSTIFY_CENTER)
  118.         + menu
  119.  
  120.         + new UIW_PROMPT(38, 0, "#", WOF_NO_FLAGS)
  121.         + new UIW_NUMBER(40, 0, 6, &RecNo, "", NMF_NO_FLAGS,
  122.             WOF_NO_ALLOCATE_DATA | WOF_NON_SELECTABLE)
  123.  
  124.         + new UIW_PROMPT(1,  1, "Last Name...", WOF_NO_FLAGS)
  125.         + new UIW_STRING(13, 1, 20, TempRec.LastName, 20, STF_NO_FLAGS,
  126.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  127.  
  128.         + new UIW_PROMPT(1,  2, "First Name..", WOF_NO_FLAGS)
  129.         + new UIW_STRING(13, 2, 20, TempRec.FirstName, 20, STF_NO_FLAGS,
  130.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  131.  
  132.         + new UIW_PROMPT(1,  3, "Company.....", WOF_NO_FLAGS)
  133.         + new UIW_STRING(13, 3, 40, TempRec.Company, 40, STF_NO_FLAGS,
  134.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  135.  
  136.         + new UIW_PROMPT(1,  4, "Address.....", WOF_NO_FLAGS)
  137.         + new UIW_STRING(13, 4, 40, TempRec.Address1, 40, STF_NO_FLAGS,
  138.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  139.  
  140.         + new UIW_STRING(13, 5, 40, TempRec.Address2, 40, STF_NO_FLAGS,
  141.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  142.  
  143.         + new UIW_PROMPT(1,  6, "City........", WOF_NO_FLAGS)
  144.         + new UIW_STRING(13, 6, 16, TempRec.City, 16, STF_NO_FLAGS,
  145.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  146.  
  147.         + new UIW_PROMPT(1,  7, "State.......", WOF_NO_FLAGS)
  148.         + new UIW_STRING(13, 7, 16, TempRec.State, 16, STF_NO_FLAGS,
  149.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  150.  
  151.         + new UIW_PROMPT(1,  8, "ZIP.........", WOF_NO_FLAGS)
  152.         + new UIW_STRING(13, 8, 12, TempRec.ZIP, 12, STF_NO_FLAGS,
  153.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  154.  
  155.         + new UIW_PROMPT(1,  9, "Phone.....", WOF_NO_FLAGS)
  156.         + new UIW_FORMATTED_STRING(13, 9, 15, TempRec.Phone, "LNNNLLNNNLCCCC",
  157.             "(...) ...-....", WOF_BORDER | WOF_NO_ALLOCATE_DATA);
  158.  
  159.     // Make the ( File | Close ) option active and redisplay menu.
  160.     OptionClose->woFlags &= ~WOF_NON_SELECTABLE;
  161.     }
  162.  
  163. // contact list destructor.
  164. ContactList::~ContactList()
  165.     {
  166.     WriteRecord();
  167.  
  168.     close(FileHandle);
  169.  
  170.     CList = 0;
  171.  
  172.     // Make the ( File | Close ) option inactive and redisplay menu.
  173.     OptionClose->woFlags |= WOF_NON_SELECTABLE;
  174.     }
  175.  
  176. // Read the next Record from the current contact list file.
  177. void ContactList::ReadRecord(void)
  178.     {
  179.     if (read(FileHandle, &TempRec, sizeof(ContactRec)) == sizeof(ContactRec))
  180.         {
  181.         Record = TempRec;
  182.         IsNew = FALSE;
  183.         }
  184.  
  185.     RecNo = lseek(FileHandle, 0L, SEEK_CUR) / sizeof(ContactRec);
  186.  
  187.     if (IsNew)
  188.         RecNo++;
  189.  
  190.     // Update the contact list window if needed.
  191.     if (CList != NULL)
  192.         *WindowManager + CList;
  193.     }
  194.  
  195. // Write the Record to the current contact list file.
  196. void ContactList::WriteRecord(void)
  197.     {
  198.     if (IsNew)
  199.         lseek(FileHandle, 0L, SEEK_END);
  200.     else
  201.         {
  202.         if (lseek(FileHandle, sizeof(ContactRec) * -1L, SEEK_CUR) < 0)
  203.             lseek(FileHandle, 0L, SEEK_SET);
  204.         }
  205.  
  206.     write(FileHandle, &TempRec, sizeof(ContactRec));
  207.  
  208.     IsNew = FALSE;
  209.  
  210.     Record = TempRec;
  211.     }
  212.  
  213. // Control menu ( Edit | Add ) option to add a new Record entry.
  214. void ContactList::AddRecord(void *item, UI_EVENT &event)
  215.     {
  216.     WriteRecord();
  217.  
  218.     if (0L == lseek(FileHandle, 0L, SEEK_END))
  219.         IsNew = TRUE;
  220.  
  221.     TempRec.LastName[0]  = '\000';
  222.     TempRec.FirstName[0] = '\000';
  223.     TempRec.Company[0]   = '\000';
  224.     TempRec.Address1[0]  = '\000';
  225.     TempRec.Address2[0]  = '\000';
  226.     TempRec.City[0]      = '\000';
  227.     TempRec.State[0]     = '\000';
  228.     TempRec.ZIP[0]       = '\000';
  229.  
  230.     strcpy(TempRec.Phone, "..........");
  231.  
  232.     ReadRecord();
  233.  
  234.     Record = TempRec;
  235.     }
  236.  
  237. // Control menu ( Edit | Next ) option to view/modify the next Record.
  238. void ContactList::NextRecord(void *item, UI_EVENT &event)
  239.     {
  240.     WriteRecord();
  241.     ReadRecord();
  242.     }
  243.  
  244. // Control menu ( Edit | Previous ) option to view/modify the previous Record.
  245. void ContactList::PrevRecord(void *item, UI_EVENT &event)
  246.     {
  247.     WriteRecord();
  248.  
  249.     if (lseek(FileHandle, sizeof(ContactRec) * -2L, SEEK_CUR) < 0)
  250.         lseek(FileHandle, 0L, SEEK_SET);
  251.  
  252.     ReadRecord();
  253.     }
  254.  
  255. // Button validate procedure for OK button on "Open CList.." window.
  256. static void OKButton(void *object, UI_EVENT &event)
  257.     {
  258.     // Put an "exit current window" message on the event queue.
  259.     event.type = S_DELETE_LEVEL;
  260.  
  261.     EventManager->Put(event, Q_BEGIN);
  262.     }
  263.  
  264. // Control menu ( File | Close ) option to close the current contact list.
  265. static void CloseCList(void *item, UI_EVENT &event)
  266.     {
  267.     // Remove and delete the current contact list window from the screen.
  268.     *WindowManager - CList;
  269.     delete CList;
  270.     }
  271.  
  272. // Control menu ( File | Open ) option to open a contact list.
  273. static void OpenCList(void *item, UI_EVENT &event)
  274.     {
  275.     char filename[20];
  276.  
  277.     filename[0] = '\0';
  278.  
  279.     if (CList)
  280.         CloseCList(item, event);
  281.  
  282.     // Create a window in the screen center to get contact list name.
  283.     int left = Display->columns / Display->cellWidth / 2;
  284.  
  285.     int top = Display->lines / Display->cellHeight / 2;
  286.  
  287.     *WindowManager
  288.         + &(*new UIW_WINDOW(left - 20, top - 4, 40, 7, WOF_NO_FLAGS,
  289.             WOAF_MODAL | WOAF_NO_SIZE, HELP_OPEN)
  290.         + new UIW_BORDER
  291.         + new UIW_TITLE("Open new contact list...", WOF_JUSTIFY_CENTER)
  292.         + new UIW_PROMPT(2, 1, "File name...", WOF_NO_FLAGS)
  293.         + new UIW_STRING(15, 1, 20, filename, 12, STF_NO_FLAGS,
  294.             WOF_BORDER | WOF_NO_ALLOCATE_DATA)
  295.         + new UIW_BUTTON(18, 3, 5, "OK", BTF_NO_FLAGS,
  296.             WOF_JUSTIFY_CENTER, OKButton));
  297.  
  298.     // Wait for user response.
  299.     int ccode;
  300.  
  301.     do  {
  302.         EventManager->Get(event, Q_NORMAL);
  303.  
  304.         // Send an "exit current window" message if ENTER key.
  305.         if (event.type == E_KEY &&
  306.             (event.rawCode == ENTER || event.rawCode == GRAY_ENTER))
  307.             {
  308.             event.type = S_DELETE_LEVEL;
  309.             }
  310.  
  311.         ccode = WindowManager->Event(event);
  312.         }
  313.     while (ccode != L_EXIT && event.type != S_DELETE_LEVEL);
  314.  
  315.     // Open/create the new contact list if possible.
  316.     if(filename[0] != '\0')
  317.         {
  318.         CList = new ContactList(filename, left, top);
  319.  
  320.         // Add the new contact list to the window manager if no error.
  321.         if (CList->GetHandle() < 0)
  322.             {
  323.             ErrorSys->ReportError(WindowManager, -1, "Error opening file.");
  324.             delete CList;
  325.             }
  326.         else
  327.             *WindowManager + CList;
  328.         }
  329.     }
  330.  
  331. // Control menu ( File | Help ) option to Display general help.
  332. static void Help(void *item, UI_EVENT &event)
  333.     {
  334.     // Call the help system to Display general help.
  335.     HelpSys->DisplayHelp(WindowManager, HELP_GENERAL);
  336.     }
  337.  
  338. // Control menu ( File | Exit ) option to exit the program.
  339. static void Exit(void *item, UI_EVENT &event)
  340.     {
  341.     // Put an EXIT message on the event queue.
  342.     event.type = L_EXIT;
  343.     EventManager->Put(event, Q_BEGIN);
  344.     }
  345.  
  346. static void CreateMenu(void)
  347.     {
  348.     // Create the main control menu.
  349.     CtrlMenu = new UIW_PULL_DOWN_MENU(0, WOF_NO_FLAGS, WOAF_NO_FLAGS);
  350.     CtrlMenu->woAdvancedFlags |= WOAF_LOCKED | WOAF_NON_CURRENT;
  351.  
  352.     // ( File ) option pull down menu (Close is inactive).
  353.     UIW_PULL_DOWN_ITEM *fileOption = new UIW_PULL_DOWN_ITEM(" ~File ",
  354.                                                             MNF_NO_FLAGS, 0);
  355.  
  356.     OptionClose = new UIW_POP_UP_ITEM("~Close contact list...", MNIF_NO_FLAGS,
  357.                                         BTF_NO_TOGGLE, WOF_NO_FLAGS,
  358.                                         CloseCList);
  359.  
  360.     OptionClose->woFlags |= WOF_NON_SELECTABLE;
  361.  
  362.     *fileOption
  363.         + new UIW_POP_UP_ITEM("~Open contact list...", MNIF_NO_FLAGS,
  364.             BTF_NO_TOGGLE, WOF_NO_FLAGS, OpenCList)
  365.         + OptionClose
  366.         + new UIW_POP_UP_ITEM("~Help...", MNIF_NO_FLAGS,
  367.             BTF_NO_TOGGLE, WOF_NO_FLAGS, Help)
  368.         + new UIW_POP_UP_ITEM
  369.         + new UIW_POP_UP_ITEM("E~xit", MNIF_NO_FLAGS, BTF_NO_TOGGLE,
  370.             WOF_NO_FLAGS, Exit);
  371.  
  372.     // Add the option menus to the control menu.
  373.     *CtrlMenu + fileOption;
  374.     *WindowManager + CtrlMenu;
  375.     }
  376.  
  377. main()
  378.     {
  379.     // Initialize the Display, trying for graphics first.
  380.     Display = new UI_DOS_BGI_DISPLAY;
  381.  
  382.     if (!Display->installed)
  383.         {
  384.         delete Display;
  385.         Display = new UI_DOS_TEXT_DISPLAY;
  386.         }
  387.  
  388.     // Initialize the event and window managers.
  389.     EventManager  = new UI_EVENT_MANAGER(100, Display);
  390.  
  391.     *EventManager
  392.         + new UI_BIOS_KEYBOARD
  393.         + new UI_MS_MOUSE
  394.         + new UI_CURSOR;
  395.  
  396.     WindowManager = new UI_WINDOW_MANAGER(Display, EventManager);
  397.  
  398.     HelpSys = new UI_HELP_WINDOW_SYSTEM("contacts.hlp",
  399.                                          WindowManager,HELP_GENERAL);
  400.  
  401.     ErrorSys = new UI_ERROR_WINDOW_SYSTEM;
  402.  
  403.     CreateMenu();
  404.  
  405.     int ccode;
  406.  
  407.     UI_EVENT event;
  408.  
  409.     do  {
  410.         EventManager->Get(event, Q_NORMAL);
  411.         ccode = WindowManager->Event(event);
  412.         }
  413.     while (ccode != L_EXIT);
  414.  
  415.     delete WindowManager;
  416.     delete EventManager;
  417.     delete Display;
  418.     }
  419.