home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / EXAMPLES / 3 / CSADD.CPP < prev    next >
Text File  |  1997-01-06  |  14KB  |  598 lines

  1. /***********************************************************************
  2.  
  3.  
  4.  
  5.              A demo ADDRESS database.
  6.  
  7.                Version 1.2
  8.  
  9.  
  10.  
  11.                        Copyright(c) 1994-1996
  12.                               ComBits
  13.                           The Netherlands
  14. ***********************************************************************/
  15.  
  16.  
  17. #include "ctype.h"
  18. #include "csa.h"
  19. #include "csmenu.h"
  20. #include "cspanel.h"
  21. #include "csaddio.h"
  22.  
  23. //  Menu Options
  24.  
  25. #define mREINDEX    700
  26. #define mCREATE_DB    701
  27. #define mEXPORT_ASCII    702
  28. #define mEXPORT_DBASE    703
  29. #define mIMPORT_ASCII    704
  30.  
  31. #define mUNSORT     501
  32. #define mSORT_NAME    502
  33. #define mSORT_CITY    503
  34. #define mSORT_BIRTHDAY    504
  35. #define mSORT_RELATION    505
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //////////////////////////// A pop up window for messages /////////////////////
  42. WINDOW w_mess;
  43.  
  44. void message_window_clear(void) { w_mess.remove(); }
  45.  
  46.  
  47. void message_window(char *s)
  48. {
  49.      w_mess.height(10);
  50.      w_mess.width(40);
  51.      w_mess.head(" ** Message ** ");
  52.      w_mess.border(W_BORDER_DOUBLE);
  53.      w_mess.activate();
  54.      gotoxy(max(1,(40-2-(int)strlen(s))/2),4);
  55.      cputs(s);
  56. }
  57.  
  58.  
  59. ///////////////////////////////////////////////////////////////////////////
  60.  
  61.  
  62. class NAM: public NAMio
  63. {
  64.  protected:
  65.  
  66.    int choice;
  67.  
  68.    MENU   m1,m2,m3,m4,m5;
  69.    PANEL  panel;
  70.  
  71.  
  72.  public:
  73.  
  74.  
  75.    void insert(void);
  76.    void def_menu(void);
  77.    int    use_menu(void)    { return (m1.choose(choice)==ENTER) ? choice: -1; }
  78.    int    use_menu(int n) { m1.find_path(n); return use_menu(); }
  79.  
  80.    void edit(void);
  81.    void display(void)    { panel.display(); }
  82.    void recreate(void)    { panel.recreate(); }
  83.    void create(void);
  84.  
  85. };
  86.  
  87.  
  88. /////////////////////////////////////////////////////////////////////////
  89. void NAM::insert(void)
  90. {
  91.  
  92.   append_blank();
  93.  
  94.   _update.now();
  95.  
  96.   recreate();
  97.   display();
  98.   panel.read();
  99.  
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////
  103. void NAM::create(void)
  104. {
  105.  
  106.   panel.remove();
  107.  
  108.   panel.set_dim(3,-1,18,65);               // Size and Position of panel
  109.   panel.head(" Address DataBase ");            // Heading
  110.   panel.border(BORDER_DOUBLE);               // Type of border
  111.   panel.activate();                   // Make panel-window visible
  112.  
  113.   gotoyx(3,10); cprintf("Name: ");
  114.   panel.add_field(4,10,25,NAMio_NAME_LENGTH,static_rec->_name);
  115.  
  116.   gotoyx(3,40); cprintf("City: ");
  117.   panel.add_field(4,40,15,NAMio_CITY_LENGTH,static_rec->_city);
  118.  
  119.   gotoyx(6,10); cprintf("Address: ");
  120.   panel.add_field(7,10,25,NAMio_ADRE_LENGTH,static_rec->_adre);
  121.  
  122.   gotoyx(6,40); cprintf("Telephone: ");
  123.   panel.add_field(7,40,15,NAMio_TEL_LENGTH,static_rec->_tel);
  124.  
  125.   gotoyx(9,10); cprintf("Zip code: ");
  126.   panel.add_field(10,10,NAMio_ZIP_LENGTH,static_rec->_zip);
  127.  
  128.   gotoyx(9,40); cprintf("Country: ");
  129.   panel.add_field(10,40,15,NAMio_COUNT_LENGTH,static_rec->_count);
  130.  
  131.  
  132.   gotoyx(1,43); cprintf("Updated: ");
  133.   panel.add_field(1,53,_update);
  134.   panel.protect(DISPLAY);
  135.  
  136.   gotoyx(12,10); cprintf("Birthday: ");
  137.   panel.add_field(13,10,_birth);
  138.  
  139.   gotoyx(12,40); cprintf("Relation: ");
  140.   panel.add_field(13,40,NAMio_RELATION_LENGTH,static_rec->_relation );
  141.  
  142.   gotoyx(15,10); cprintf("Info: ");
  143.   panel.add_field(16,10,50,NAMio_INFO_LENGTH,static_rec->_info );
  144.  
  145.   panel.exit_key(CTRL_END);
  146.   panel.escape_off();
  147.   panel.display();
  148.  
  149. }
  150.  
  151. /////////////////////////////////////////////////////////////////////////
  152. void NAM::edit(void)
  153. {
  154.  
  155.   WINDOW help;
  156.   help.set_dim( 21,-1,3,40);
  157.   help.border(W_BORDER_DOUBLE);
  158.   help.activate();
  159.   cprintf("    Use CTRL_END to exit editing.");
  160.  
  161.   panel.read();
  162.  
  163.   if(panel.changed())
  164.   {
  165.     dirty=TRUE;
  166.     _update.now();
  167.   }
  168.  
  169. }
  170. /////////////////////////////////////////////////////////////////////////
  171.  
  172. void NAM::def_menu(void)
  173. {
  174.  
  175.  
  176. /////////////////////////// Define the options for menu 1  //////////////
  177.  
  178.  m1.add_option(" e~Xit "        ,ALT_X );
  179.  m1.add_option(" ~Insert "      ,ALT_I );
  180.  m1.add_option(" ~Delete "      ,ALT_D );
  181.  m1.add_option(" ~Edit "        ,ALT_E );
  182.  m1.add_option(" ~Sort order "  ,ALT_S );
  183.  m1.add_option(" ~Output "      ,ALT_O );
  184.  m1.add_option(" ~Utilities "   ,ALT_U );
  185.  m1.add_option(" Se~tup "       ,ALT_T );
  186.  m1.add_option("      ~Help=F1 ",F1 );
  187.  
  188.  
  189. /////////// Define the options for the output submenu ///////////////////////
  190.  
  191.  m2.add_option(" P~ages " , 601 );
  192.  m2.add_option(" P~rinten ",602 );
  193.  
  194.  
  195. /////////// Define the options for the utility submenu //////////////////////
  196.  
  197.  m3.add_option(" ~Pack  "             ,ALT_P );
  198.  m3.add_option(" ~Rebuild Indexes "   ,mREINDEX);
  199.  m3.add_option(" ~Create new database",mCREATE_DB);
  200.  m3.add_option(" ~Export to ASCII "   ,mEXPORT_ASCII);
  201.  m3.add_option(" Export to d~BASE "   ,mEXPORT_DBASE);
  202.  m3.add_option(" ~Import from ASCII " ,mIMPORT_ASCII);
  203.  
  204. /////////// Define the options for the setup submenu ////////////////////////
  205.  
  206.  
  207.  m4.add_option(" Directories ",603 );
  208.  m4.add_option(" Colors "     ,604 );
  209.  m4.add_option(" Save Setup"  ,605 );
  210.  
  211. /////////// Define the options for the 'sort' submenu ///////////////////////
  212.  
  213.  
  214.  m5.add_option(" ~Unsorted "          ,mUNSORT);
  215.  m5.add_option(" Sorted by ~Name "    ,mSORT_NAME);
  216.  m5.add_option(" Sorted by ~City "    ,mSORT_CITY);
  217.  m5.add_option(" Sorted by ~Relation ",mSORT_RELATION);
  218.  m5.add_option(" Sorted by ~Birthday ",mSORT_BIRTHDAY);
  219.  
  220. ///////////////// Make menu 1 'special' (main menu) ////////////////////
  221.  
  222.  
  223.   m1.type(MENU_HOR);             // Display options horizontally
  224.   m1.hold(MENU_HOLD);             // Make it 'always visible'
  225.   m1.border(BORDER_NONE);         // No border
  226.   m1.width(80);              // Set the width to the full screen
  227.  
  228.  
  229. //////////////////////////// Define the colors //////////////////////////
  230.  
  231.  
  232.   int bor_col;
  233.   int scr_col;
  234.   int opt_col;
  235.   int key_col;
  236.  
  237.  
  238.   if(is_color())
  239.   {
  240.     bor_col=make_color(WHITE,CYAN);
  241.     scr_col=make_color(WHITE,CYAN);
  242.     opt_col=make_color(WHITE,RED);
  243.     key_col=make_color(YELLOW,CYAN);
  244.   }
  245.   else
  246.   {
  247.     bor_col=make_color(LIGHTGRAY,BLACK);
  248.     scr_col=bor_col;
  249.     opt_col=make_color(BLACK,WHITE);
  250.     key_col=make_color(WHITE,BLACK);
  251.   }
  252.  
  253.  
  254. /////////////////////////////// Set the colors of each (Sub) menu /////////
  255.  
  256. //  m1.color(bor_col,make_color(YELLOW,RED),key_col,key_col);
  257.  
  258.   m1.color(bor_col,scr_col,opt_col,key_col);
  259.   m2.color(bor_col,scr_col,opt_col,key_col);
  260.   m3.color(bor_col,scr_col,opt_col,key_col);
  261.   m4.color(bor_col,scr_col,opt_col,key_col);
  262.   m5.color(bor_col,scr_col,opt_col,key_col);
  263.  
  264. ///////////////////////// Set the postions of the menus ////////////////////
  265.  
  266.   m1.coord(1,1);
  267.   m2.relative_pos(TO_CURSOR);
  268.   m2.coord(1,1);
  269.   m3.relative_pos(TO_CURSOR);
  270.   m3.coord(1,1);
  271.   m4.relative_pos(TO_CURSOR);
  272.   m4.coord(1,1);
  273.   m5.relative_pos(TO_CURSOR);
  274.   m5.coord(1,1);
  275.  
  276. ////////////// Create the entire menu by connecting the sub-menus //////////
  277.  
  278.   m1.connect(5,m5);
  279.   m1.connect(6,m2);
  280.   m1.connect(7,m3);
  281.   m1.connect(8,m4);
  282.  
  283.   m1.standby();
  284.  
  285. }
  286.  
  287.  
  288. /////////////////////////////////////////////////////////////////////////
  289.  
  290. void main(int argc,char *argv[])
  291. {
  292.  
  293.    if(argc>=2 && !strcmp(argv[1],"/?"))
  294.    {
  295.  
  296.       printf("\n  * * * * * * * An address database.  * * * * * * *    ");
  297.       printf("\n\n\n ADDRESS version 1.0.d ");
  298.       printf("\n\n Compiled at:  %s, %s ",__DATE__,__TIME__);
  299.       printf("\n Copyright (c) ComBits");
  300.       printf("\n\n USAGE:  address [/reindex] [/create] [/?] ");
  301.       printf("\n  /reindex  To rebuild the indexes. ");
  302.       printf("\n  /create   To create a new database. ");
  303.       printf("\n  /?        To display this screen.   ");
  304.       printf("\n\n");
  305.       return;
  306.    }
  307.  
  308.    char inp[50]="";
  309.  
  310.    NAM    nam;
  311.  
  312.  
  313. ////////////////// Program already running? /////////////////////////////
  314.  
  315.    if(nam.lock_file_exist())
  316.    {
  317.       message_window("Lock file exist! Cannot start.\r\n\n\nAny key to continue.");
  318.       cskey();
  319.       message_window_clear();
  320.       return;
  321.    }
  322.  
  323. ////////////////// Create a new database ////////////////////////////////
  324.  
  325.  
  326.    if(argc==2 && !strcmp(argv[1],"/create"))  nam.define();
  327.  
  328.  
  329. ////////////////// Reindex when started with '/reindex' /////////////////
  330.    if(argc==2 && !strcmp(argv[1],"/reindex"))
  331.    {
  332.      nam.open();
  333.      nam.reindex();
  334.      nam.close();
  335.      exit(0);
  336.    }
  337.  
  338.    int    choice=-1;
  339.    int    leng;
  340.  
  341.    clrscr();
  342.  
  343.  
  344. ////////////////// Open database and indexes. ///////////////////////////
  345.  
  346.    nam.open();
  347.    if(nam.numrec()==0) nam.append_blank();
  348.  
  349.  
  350. ////////////////// Setup background /////////////////////////////////////
  351.    WINDOW bckgrnd;
  352.    bckgrnd.border(BORDER_SINGLE);
  353.    bckgrnd.set_dim(2,1,23,80);
  354.    bckgrnd.activate();
  355.  
  356.  
  357. ////////////////// Create input window. /////////////////////////////////
  358.    WINDOW input;
  359.    input.border(BORDER_SINGLE);
  360.    input.set_dim(21,4,3,73);
  361.    input.activate();
  362.  
  363.  
  364. ////////////////// Create menus /////////////////////////////////////////
  365.    nam.def_menu();
  366.  
  367.  
  368.    nam.order(NAMIO_NAME_INDEX);
  369.    nam.top();
  370.  
  371.  
  372.  
  373. ////////////////// Create & display PANEL. //////////////////////////////
  374.    nam.create();
  375.  
  376.  
  377. ////////////////// Display Warning /////////////////////////////////////
  378.  
  379.    WINDOW warning;
  380.  
  381.    warning.border(BORDER_DOUBLE);
  382.    warning.height(14);
  383.    warning.width(46);
  384.    warning.color(make_color(WHITE,RED),make_color(YELLOW,RED));
  385.    warning.head(" ** ALERT! ** ");
  386.    warning.activate();
  387.  
  388.    cputs("\n\n\n\r");
  389.    cputs(    "\n\r         This User-Interface is ");
  390.    cputs(    "\n\r                  NOT" );
  391.    cputs(    "\n\r         part of CSLIB!! ");
  392.    cputs("\n\n\n\r         Please read 'readme.txt' for ");
  393.    cputs(    "\n\r         more info.");
  394.    cputs("\n\n\n\rAny key to continue.");
  395.    cskey();
  396.  
  397.    warning.remove();
  398.  
  399.  
  400. ////////////////// Main processing loop. ////////////////////////////////
  401.     do
  402.     {
  403.       if(choice==-1)
  404.       {
  405.     nam.display();
  406.     input.activate();
  407.     gotoxy(5,1);
  408.     cprintf("Search string: %s",inp); clreol();
  409.     gotoxy(50,1);
  410.     cprintf("Record %ld/%ld",nam.curr_rec(),nam.numrec());
  411.     clreol();
  412.     gotoxy(69,1);
  413.     if(nam.is_delet()) putch('D');
  414.     else           putch(' ');
  415.     gotoxy(20+strlen(inp),1);
  416.     choice=cskey();
  417.       }
  418.  
  419.       switch(choice)
  420.       {
  421.      case mUNSORT:
  422.              nam.order(UNSORTED);
  423.              nam.top();
  424.              choice=-3;
  425.              break;
  426.      case mSORT_NAME:
  427.              nam.order(NAMIO_NAME_INDEX);
  428.              choice=-2;
  429.              break;
  430.      case mSORT_CITY:
  431.              nam.order(NAMIO_CITY_INDEX);
  432.              choice=-2;
  433.              break;
  434.      case mSORT_RELATION:
  435.              nam.order(NAMIO_RELATION_INDEX);
  436.              choice=-2;
  437.              break;
  438.      case mSORT_BIRTHDAY:
  439.              nam.order(NAMIO_BIRTH_INDEX);
  440.              choice=-2;
  441.              break;
  442.      case 601:
  443.      case 602:
  444.      case 603:
  445.      case 604:
  446.      case 605:
  447.              message_window("Sorry, option not implemented.\n\rHit any key...");
  448.              waitkb(3500);
  449.              message_window_clear();
  450.              choice=-1;
  451.              break;
  452.      case mCREATE_DB:
  453.              message_window("Deleting entire database! \n\rARE YOU SURE (y/n).");
  454.              if(cskey()=='y')
  455.              {
  456.             message_window("Creating new database.");
  457.             nam.close();
  458.             nam.define();
  459.             nam.open();
  460.             nam.append_blank();
  461.              }
  462.              message_window_clear();
  463.              choice=-3;
  464.              break;
  465.      case mREINDEX:
  466.              message_window("Indexing... A moment please.");
  467.              nam.reindex();
  468.              message_window_clear();
  469.              choice=-2;
  470.              break;
  471.      case mEXPORT_ASCII:
  472.              message_window("Exporting to 'backup.txt'.");
  473.              nam.export("backup.txt");
  474.              message_window_clear();
  475.              choice=-1;
  476.              break;
  477.      case mEXPORT_DBASE:
  478.              message_window("Exporting to 'DBASE.dbf'.");
  479.              nam.to_DBASE("DBASE.dbf");
  480.              message_window_clear();
  481.              choice=-1;
  482.              break;
  483.      case mIMPORT_ASCII:
  484.              message_window("Importing from 'backup.txt'.");
  485.              nam.import("backup.txt");
  486.              message_window_clear();
  487.              choice=-3;
  488.              break;
  489.      case F1:
  490.      case ALT_H:
  491.              {
  492.             WINDOW help;
  493.             help.set_dim(-1,-1,14,50);
  494.             help.head(" Help Screen ");
  495.             help.browse("address.hlp");
  496.              }
  497.              choice=-1;
  498.              break;
  499.      case ALT_D:
  500.              if(nam.is_delet()) nam.undelet();
  501.              else        nam.delet();
  502.              choice=-1;
  503.              break;
  504.      case ALT_E:
  505.              nam.edit();
  506.              choice=-3;
  507.              break;
  508.      case ALT_I:
  509.              nam.insert();
  510.              choice=-3;
  511.              break;
  512.      case ALT_P:
  513.              message_window("Packing... A moment please.. ");
  514.              nam.pack();
  515.              if(nam.numrec()==0) nam.append_blank();
  516.              message_window_clear();
  517.              nam.top();
  518.              choice=-3;
  519.              break;
  520.      case ALT_X:
  521.              break;
  522.      case ALT_O:
  523.      case ALT_S:
  524.      case ALT_T:
  525.      case ALT_U:
  526.              choice=nam.use_menu(choice);
  527.              break;
  528.      case F10:
  529.              choice=nam.use_menu();
  530.              break;
  531.      case CURSOR_UP:
  532.              nam.skip(-1);
  533.              choice=-3;
  534.              break;
  535.      case CURSOR_DOWN:
  536.              nam.skip(1);
  537.              choice=-3;
  538.              break;
  539.      case PAGE_DOWN:
  540.              nam.skip(10);
  541.              choice=-3;
  542.              break;
  543.      case PAGE_UP:
  544.              nam.skip(-10);
  545.              choice=-3;
  546.              break;
  547.      case HOME:
  548.              nam.top();
  549.              choice=-3;
  550.              break;
  551.      case END:
  552.              nam.bottom();
  553.              choice=-3;
  554.              break;
  555.      case BACKSPACE:
  556.              if(strlen(inp))
  557.              {
  558.                inp[strlen(inp)-1]=0;
  559.                choice=-2;
  560.              }
  561.              else choice=-1;
  562.              break;
  563.      case ESC:
  564.              inp[0]=0;
  565.      case -2:
  566.              nam.search(inp);
  567.      case -3:
  568.              // Adjust panel to new record.
  569.              nam.recreate();
  570.              choice=-1;
  571.              break;
  572.      default:
  573.              if(isprint(choice))
  574.              {
  575.             // Modify search string.
  576.             leng=strlen(inp);
  577.             inp[leng]=choice;
  578.             inp[leng+1]=0;
  579.             choice=-2;
  580.              }
  581.              else choice=-1;
  582.              break;
  583.       }
  584.  
  585.     } while(choice!=ALT_X);
  586.  
  587.  
  588. ////////////////// Remove all windows. //////////////////////////////////
  589.     win_remove_all();
  590.  
  591.  
  592. ////////////////// Close database. //////////////////////////////////////
  593.     nam.close();
  594.  
  595.  
  596. }
  597.  
  598.