home *** CD-ROM | disk | FTP | other *** search
/ Windoware / WINDOWARE_1_6.iso / source / winobj / demo.cpp next >
Text File  |  1991-01-09  |  14KB  |  409 lines

  1. // Object Windows demo program demo.cpp
  2. // Copyright 1991 (c),   Lake View Software
  3. //                       4321 Harborough Rd.
  4. //                       Columbus, OH 43220
  5. // 1/2/91
  6.  
  7. #include "winmenu.hpp"  // Menu class header file
  8. #include "winpick.hpp"  // Pick-List class header file
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <alloc.h>
  12.  
  13. WinObj win1,win2;   // Some global window objects, These will be the random windows
  14.  
  15. // Declare internal functions
  16. int  v_salary(void *num);
  17. int  v_age(void *num);
  18. void rand_open (WinObj &w);
  19. void show_time();
  20. void move_windows(struct time t);
  21. void data_entry();
  22. void moving_windows();
  23. void random_windows();
  24. void menu_demo();
  25. void menu_demo2();
  26. void pick_demo();
  27. void help();
  28. void logo();
  29.  
  30.  
  31. void main(int argc,char **argv)
  32. {
  33.     int arg=0;
  34.     while (++arg < argc) {      // Parse the command line
  35.         if (argv[arg][0]=='-' || argv[arg][0]=='/') {
  36.             switch (argv[arg][1]) {
  37.                 case 'b': Win_bios(W_ON);     // Turn on use of bios writes
  38.                     break;
  39.                 case 's': Win_snow(W_ON);     // Turn on use of snow suppression
  40.                     break;
  41.                 case 'm': Win_mono(W_ON);     // Force black & white only
  42.                     break;
  43.             }
  44.         }
  45.     }
  46.  
  47.     randomize();        // Start random number generator
  48.  
  49.     // Turn clock on
  50.     Win_setbkloop (show_time);       // Set background keyboard loop
  51.                                      // This will run the clock and move win1
  52.                                      // & win2
  53.  
  54.     Win_setonkey (F1,help);         // Set F1 to function help
  55.  
  56.     logo();                         // display logo window
  57.  
  58.     WinMenu m;  // create Main menu
  59.     m.open (3,26,17,52,W_SOLID,_BLUE|LGREEN,_BLUE|LGREY);
  60.     m.title ("[ Demo Menu ]");
  61.     m.prompt (1,4,1,"Data entry");
  62.     m.prompt (3,4,2,"Moving windows");
  63.     m.prompt (5,4,3,"Random windows");
  64.     m.prompt (7,4,4,"Menu demo");
  65.     m.prompt (9,4,5,"Pick-List demo");
  66.     m.prompt (11,4,0,"Quit");
  67.  
  68.     int choice = 1;         // start with menu selection ret_val = 1
  69.     while (choice != 0) {
  70.         choice = m.read(choice);  // get menu selection
  71.         m.hide();                 // hide the menu window
  72.         switch (choice) {
  73.             case 1: data_entry();
  74.                 break;
  75.             case 2: moving_windows();
  76.                 break;
  77.             case 3: random_windows();
  78.                 break;
  79.             case 4: menu_demo();
  80.                 break;
  81.             case 5: pick_demo();
  82.                 break;
  83.         }
  84.     }
  85.     // all windows will close automatically with there destructors
  86. }
  87.  
  88. //*********************Pick-List Demo************************
  89.  
  90. static char *list[] = { "January",   "February",
  91.                         "March",     "April",
  92.                         "May",       "June",
  93.                         "July",      "August",
  94.                         "September", "October",
  95.                         "November",  "December",  0 };
  96.  
  97. void pick_demo()
  98. {
  99.     WinPick pl;
  100.     pl.open (6,33,11,45,W_DOUSIN,_BLUE|WHITE,_BLUE|GREEN);  // open pick window
  101.     pl.shadow();
  102.     pl.title ("Pick-List");
  103.     pl.prompt (list);           // assign a list
  104.     int choice = pl.read();     // get user selection, closes window when finished
  105.  
  106.     // Display the results of the pick.
  107.     if (choice != W_ESCAPE) {
  108.         WinObj w;
  109.         w.open (6,23,11,53,W_DOUSIN,_BLUE|GREEN,_BLUE|WHITE);
  110.         w.shadow();
  111.         w.printf (1,1,"You picked ");
  112.         w.set_attr(_BLUE|YELLOW);
  113.         w.printf ("%s.",list[choice]);
  114.         w.set_attr(_BLUE|LGREY);
  115.         w.printf (3,1,"Press any key to continue.");
  116.         w.get_key();
  117.     }
  118. }
  119.  
  120. //**********************Menu Demo****************************
  121. // First level of a lotus style menu
  122. void menu_demo()
  123. {
  124.     WinMenu m;                                      // Create a menu object
  125.     m.open (1,0,1,79,W_NOBORDER,_BLUE|WHITE); // init the window
  126.     m.message_line(2);                              // Set message line to 2
  127.  
  128.     // Declare some menu selections
  129.     m.prompt (0, 1,1,"Worksheet","Global, Column, Status, Delete");
  130.     m.prompt (0,12,2,"Range","Format, Label, Erase, Name, Justify, Prot");
  131.     m.prompt (0,19,3,"Copy","Copy a cell or range of cells");
  132.     m.prompt (0,25,4,"Move","Move a cell or range of cells");
  133.     m.prompt (0,31,5,"File","Retrieve, Save, Combine, Xtract...");
  134.     m.prompt (0,37,6,"Print","Print a worksheet to a printer or file");
  135.     m.prompt (0,44,0,"Quit","Quit the menu demo");
  136.  
  137.     int choice=1;
  138.     while (choice != 0) {
  139.         choice = m.read(choice);        // Get a menu selection
  140.         switch (choice) {
  141.             case 1:
  142.                 menu_demo2();           // Goto next menu level
  143.                 break;
  144.             case 2:
  145.                 break;
  146.         }
  147.     }
  148.     // Menu object will destruct and close automatically.
  149. }
  150.  
  151. void menu_demo2()       // Second level of a lotus style menu
  152. {
  153.     WinMenu m;
  154.     m.open (1,0,1,79,W_NOBORDER,_BLUE|WHITE);
  155.     m.message_line(2);          // Set message line to 1
  156.     m.prompt (0, 1,1,"Global","Global functions");
  157.     m.prompt (0, 9,2,"Column","Delete, Insert");
  158.     m.prompt (0,17,3,"Status","Display worksheet status");
  159.     m.prompt (0,25,4,"Delete","Delete the entire worksheet");
  160.     m.prompt (0,33,0,"Quit","Quit to the main menu");
  161.     int choice=1;
  162.     while (choice != 0) {
  163.         choice = m.read(choice);
  164.         switch (choice) {
  165.             case 1:
  166.                 break;
  167.             case 2:
  168.                 break;
  169.         }
  170.     }
  171. }
  172.  
  173. //*************************Moving windows Demo*************************
  174. // Open the moving windows
  175. void moving_windows()
  176. {
  177.     // Windows will open or close with each call
  178.     if (!win1.is_open()) {
  179.         win1.open (1,1,10,25,W_DOUBLE,_CYAN|BLACK,_CYAN|BLUE);
  180.         win1.shadow();
  181.         win1.title ("[ Moving Window 1 ]");
  182.         win1.printf("Here is some text in a moving window.");
  183.         win2.open (1,54,10,79,W_SOLID,_GREEN|BLACK,_GREEN|WHITE);
  184.         win2.shadow();
  185.         win2.title ("[ Moving Window 2 ]");
  186.         win2.printf("Here is some text in the second moving window.");
  187.     }
  188.     else {
  189.         win1.close();
  190.         win2.close();
  191.     }
  192. }
  193.  
  194. // Function to move the random windows every second
  195. // This function is called by the clock function with the current time
  196. void move_windows(struct time t)
  197. {
  198.     static int x=1,y=1,sec_hold=0;
  199.     if (!win1.is_open())
  200.         return;
  201.  
  202.     if (sec_hold != t.ti_sec) {      // only move once a second
  203.         sec_hold = t.ti_sec;
  204.         x+=6;  y+=2;
  205.         if (win1.move(y,x) != W_OK) {       // test for error on window move
  206.             x=0;                    // this will tell us when we reach the edge
  207.             y=0;
  208.             win1.move(y,x);
  209.         }
  210.         // randomly move the second window
  211.         win2.move(random(14),random(54));
  212.     }
  213. }
  214.  
  215. //**********************Random Windows Demo***************************
  216. void random_windows()
  217. {
  218.     WinObj win1,win2,win3,win4,win5;    // Create some random windows
  219.     rand_open(win1);
  220.     delay(250);         // Pause for effect
  221.     rand_open(win2);
  222.     delay(250);         // Pause for effect
  223.     rand_open(win3);
  224.     delay(250);         // Pause for effect
  225.     rand_open(win4);
  226.     delay(250);         // Pause for effect
  227.     rand_open(win5);
  228.     delay(250);         // Pause for effect
  229.     win1.clr_win();
  230.     win1.printf("Press any key to continue.");
  231.     win1.get_key();     // Wait for a key press
  232. }
  233.  
  234. // Function to open random windows on the screen
  235. void rand_open (WinObj &w)
  236. {
  237.     static cnt=0;
  238.     int sr,er,sc,ec;
  239.     int color,bcolor,btype;
  240.     sr = random(12);
  241.     sc = random(40);
  242.     er = sr + 10 + random(12-sr);
  243.     ec = sc + 15 + random(63-sc);
  244.     color = random(8)*16 + random(16);
  245.     bcolor = random(8)*16 + random(16);
  246.     btype = random(6);
  247.     w.open (sr,sc,er,ec,btype,color,bcolor);
  248.     w.shadow();
  249.     w.title ("[ Random Window ]");
  250.     w.printf ("This is random window #(%d)",++cnt);
  251. }
  252.  
  253. //************************Data Entry Demo********************************
  254.  
  255. void data_entry()
  256. {
  257.     // Declare data variables.
  258.     static char lastname[16]="",firstname[16]="";
  259.     static char add1[31]="",add2[31]="";
  260.     static char city[16]="",state[3]="",zip[11]="";
  261.     static char phone[15] = "";
  262.     static char start[9]="";
  263.     static double salary=0;
  264.     static long age=0;
  265.  
  266.     WinObj  w(80,80);      // Create the main entry window
  267.     w.open(2,12,12,60,W_DOUBLE,_BLUE|WHITE,_BLUE|YELLOW);
  268.     w.shadow();
  269.     w.title("[ Data Entry ]");
  270.  
  271.     w.printf(1,1,"  Last Name:");
  272.     w.printf(2,1," First Name:");
  273.     w.printf(4,1,"    Address:");
  274.     w.printf(6,1,"City/St/Zip:");
  275.     w.printf(6,29,",");
  276.     w.printf(8,1,"      Phone:");
  277.     w.printf(10,1," Start Date:");
  278.     w.printf(12,1,"       Age:");
  279.     w.printf(14,1,"    Salary:");
  280.  
  281.     str_setsz(lastname,15);    // We won't use a picture for these
  282.     str_setsz(firstname,15);   // variables, so we must give the initial
  283.     str_setsz(add1,30);        // value some size.
  284.     str_setsz(add2,30);
  285.     str_setsz(city,15);
  286.     str_setsz(state,2);
  287.  
  288.     w.get (1,14,lastname,'F');  // force first character of these fields
  289.     w.get (2,14,firstname,'F'); // to be upper case with a 'F'orce style.
  290.     w.get (4,14,add1,'F');
  291.     w.get (5,14,add2,'F');
  292.     w.get (6,14,city,'F');
  293.  
  294.     w.get (6,31,state,'U');        // force state to be uppercase
  295.     w.get (6,35,zip,"99999-9999");  // use a picture for a zip code
  296.  
  297.     w.get (8,14,phone,'P');     // get a phone style
  298.     w.get (10,14,start,'D');    // get a date style
  299.     w.get (12,14,age,3);        // Limit age to 3 places
  300.     w.valid (v_age);            // check age to be over 15
  301.  
  302.     w.get (14,14,salary,9);     // limit salary to hundred thousands
  303.     w.valid (v_salary);         // check to be sure salary is over $10,000
  304.  
  305.     w.read();           // read the gets
  306. }
  307.  
  308. int v_salary(void *num)             // Valid function for salary
  309. {
  310.     if (* (double *)num >= 10000)     // Must first conver num to a double type
  311.         return 1;                   // pointer.  if >= 10,000 return true.
  312.  
  313.     WinObj error;
  314.     error.open (1,12,3,39,W_SINGLE,_RED|WHITE,_RED|YELLOW);
  315.     error.shadow();
  316.     error.title ("[ Error ]");
  317.     error.printf (" Salary must be > 10,000! ");
  318.     Win_beep();
  319.     error.get_key();
  320.     // no need to close the window the distructor will do this for you
  321.     return 0;                       // return false.
  322. }
  323.  
  324. int v_age(void *num)             // Valid function for age
  325. {
  326.     if (* (int *)num > 15)     // Must first conver age to a integer type
  327.         return 1;                   // pointer.  if > 15 return true.
  328.  
  329.     WinObj error;
  330.     error.open (1,12,3,39,W_SINGLE,_RED|WHITE,_RED|YELLOW);
  331.     error.shadow();
  332.     error.title ("[ Error ]");
  333.     error.printf (" Age must be > 15 ");
  334.     Win_beep();
  335.     error.get_key();
  336.     // no need to close the window the distructor will do this for you
  337.     return 0;                       // return false.
  338. }
  339.  
  340. //**************************Clock function*******************************
  341.  
  342. void show_time()        // Show time in upper left corner of screen
  343. {
  344.     static struct time t;
  345.     static WinObj clock_win;    // create a static window object
  346.     static unsigned char hold;
  347.  
  348.     if (!clock_win.is_open())       // open small clock window once
  349.         clock_win.open(0,0,1,7,W_NOBORDER,_BLACK|WHITE);
  350.  
  351.     // First show the clock
  352.     gettime(&t);
  353.     if (hold != t.ti_sec) {
  354.         hold = t.ti_sec;
  355.         clock_win.printf (0,0,"%02d:%02d:%02d",t.ti_hour,t.ti_min,t.ti_sec);
  356.         clock_win.printf (1,0,"%ld",(long )coreleft());  // show free mem
  357.     }
  358.  
  359.     move_windows(t);      // Move the random windows
  360. }
  361.  
  362. //**********************Help Function***********************************
  363. void help()
  364. {
  365.     static int is_active=0;
  366.     int old_x,old_y;
  367.     if (is_active)
  368.         return;
  369.     is_active++;
  370.     Win_getxy(&old_y,&old_x);       // Save current cursor position
  371.  
  372.     WinObj help;
  373.     help.open (1,11,21,67,W_SINGLE,_LGREY|BLACK,_LGREY|WHITE);
  374.     help.title (" Help Window ");
  375.     help.center(1,"Data Entry Keys");
  376.     help.printf(3,1,"/ arrows        Move cursor to next/prev field.");
  377.     help.printf(4,1,"%c/%c arrows        Move cursor to next/prev column.",26,27);
  378.     help.printf(5,1,"Ctrl_%c/%c arrows   Pan window right/left one column.",26,27);
  379.     help.printf(6,1,"Ctrl_end/home     Pan window down/up one row.");
  380.     help.printf(7,1,"PgDn              Exit read mode.");
  381.     help.printf(8,1,"Esc               Quit read mode.");
  382.     int hold_attr = help.set_attr(_LGREY|WHITE);
  383.     help.center(10,"Non Picture Gets Only");
  384.     help.set_attr(hold_attr);
  385.     help.printf(11,1,"Insert            Toggle insert/overtype mode.");
  386.     help.printf(12,1,"Delete            Delete character under cursor.");
  387.     help.printf(13,1,"Backspace         Delete previous character.");
  388.     hold_attr = help.set_attr(_LGREY|RED);
  389.     help.center(17,"Copyright (c) 1991, Lake View Software");
  390.     help.set_attr(hold_attr);
  391.     help.center(18,"Press any key to exit.");
  392.     help.get_key();
  393.  
  394.     Win_gotoxy(old_y,old_x);        // Restore cursor position
  395.     is_active = 0;                  // turn off active flag
  396. }
  397.  
  398.  
  399. //*****************************Logo*****************************************
  400. void logo()
  401. {
  402.     static WinObj l;
  403.     l.open (20,19,24,59,W_SOLID,_BLACK|WHITE,_BLACK|YELLOW);
  404.     l.title (" Object Windows ");
  405.     l.center(0,"The C++ window class system");
  406.     l.center(1,"Copyright (c) 1991");
  407.     l.center(2,"Lake View Software");
  408. }
  409.