home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / phigs / ptk.lha / ptk / source / demo / windtest.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-29  |  11.7 KB  |  408 lines

  1. /*--------------------------------------------------------------------------- 
  2.  
  3.  Program name: Windows demo program.
  4.  
  5.  Author: Gareth Williams
  6.  
  7.  Description: demonstrates the windows module by displacing a window
  8.  with the lamp structure.
  9.  
  10.  Modification history : (Version), (Date), (Name), (Description).
  11.  
  12.  1.0, 1st September 1991, G. Williams, First Version.
  13.  
  14.  2.0, June 1992, G. Williams, Converted to ISO PHIGS C.
  15.  
  16.  SunOS requirements: SunPHIGS 2.0, OpenWindows 3.0.
  17.  
  18. ----------------------------------------------------------------------------*/
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <phigs.h>
  23. #include "ptk.h"
  24.  
  25. #define WS1     1
  26. #define DEVICE1 1
  27. #ifdef SUN
  28. #define DEVICE2 4
  29. #endif
  30. #ifndef SUN
  31. #define DEVICE2 2
  32. #endif
  33. #define PI      3.14159
  34.  
  35. /* Operating system dependent code, UNIX/VMS pathnames */
  36. /* UNIX pathnames */
  37. #define SCRIPTNAME "../scripts/lamp.scr"
  38. #define OPENWSNAME "../scripts/openws.scr"
  39.  
  40. /* 
  41. #ifdef VMS
  42. #define SCRIPTNAME "[-.scripts]lamp.scr"
  43. #endif
  44. */
  45.  
  46. static char *colwrd[] = 
  47.      {
  48.          "BLACK", "WHITE", "GREEN", 
  49.          "BLUE", "CYAN", "GREY", "RED" 
  50.      };
  51.  
  52. static Pfloat devx, devy, devz;
  53.  
  54. /*--------------------------------------------------------------------------*/
  55.  
  56. static void init_input(C(void))
  57. /* Initializes four valuators in sample mode, and a choice
  58. ** device in event mode. 
  59. */
  60. {
  61.    Pin_status initstatus = PIN_STATUS_OK;
  62.    Pint initview, err;
  63.    Ppoint initpos;
  64.    Ploc_data locrec;
  65.    Plimit echo;
  66.  
  67.    pset_loc_mode(WS1, DEVICE1, POP_REQ, PSWITCH_ECHO);
  68.    pset_loc_mode(WS1, DEVICE2, POP_REQ, PSWITCH_ECHO);
  69.    echo = ptk_limit(0.0, devx, 0.0, devy); 
  70.    initview = 0;
  71.    initpos = ptk_point(0.5, 0.5);
  72. #ifdef HP
  73.    locrec.pets.pet_r1.loc_colr_ind = 1;
  74. #endif
  75.    pinit_loc(WS1, DEVICE1, initview, &initpos, 1, &echo, &locrec);
  76.    pinit_loc(WS1, DEVICE2, initview, &initpos, 1, &echo, &locrec);
  77.    pset_loc_mode(WS1, DEVICE1, POP_EVENT, PSWITCH_ECHO);
  78.    pset_loc_mode(WS1, DEVICE2, POP_SAMPLE, PSWITCH_ECHO);
  79. }
  80.  
  81. /*--------------------------------------------------------------------------*/
  82.  
  83. static void camerainterface(C(Pint) windid)
  84. PreANSI(Pint windid)
  85. {
  86.   Pint wsid, devnum, viewindex, err;
  87.   Pin_class class;
  88.   Pin_status status;
  89.   Ppoint oldpos, pos;
  90.   Plimit3 lims;
  91.   Ppoint3 defcampos, campos;
  92.   Pfloat zorg;
  93.  
  94.   init_input();                           /* Initialise input devices. */
  95.   oldpos = ptk_point(0.5, 0.5);
  96.   ptk_inqcameralimits(windid, &lims, &err);
  97.   ptk_inqcameraposition(windid, &defcampos, &err);
  98.   zorg = lims.z_min + ((lims.z_max - lims.z_min) / 2.0);
  99.   while (1)
  100.   {
  101.     psample_loc(WS1, DEVICE2, &viewindex, &pos);
  102.     campos = ptk_point3(lims.x_min + (lims.x_max - lims.x_min) * pos.x,
  103.                         lims.y_min + (lims.y_max - lims.y_min) * pos.y,
  104.                         zorg +
  105.                         (sin(PI * pos.x) * sin(PI * pos.y)) *
  106.                         (defcampos.z - zorg));
  107.     ptk_setcameraposition(windid, &campos);
  108.     pupd_ws(WS1, PFLAG_PERFORM);
  109. #ifdef SUN
  110.     pawait_event(0.25, &wsid, &class, &devnum); 
  111. #endif
  112. #ifdef HP
  113.     pawait_event(0.0, &wsid, &class, &devnum); 
  114. #endif
  115. #ifdef PEXSI
  116.     pawait_event(0.5, &wsid, &class, &devnum); 
  117. #endif
  118.     /* See if left button pressed. */
  119.     if (class == PIN_LOC)
  120.       break;
  121.   }
  122.   pset_loc_mode(WS1, DEVICE1, POP_REQ, PSWITCH_ECHO);
  123.   pset_loc_mode(WS1, DEVICE2, POP_REQ, PSWITCH_ECHO);
  124. }  /* camerainterface */
  125.  
  126. /*--------------------------------------------------------------------------*/
  127.  
  128. static void options(C(void))
  129. {
  130.   char commandstr[20], writestr[50];
  131.   Pint lencom, lenstr, err, elem1, elem2, lampid, currentwindow, red, green;
  132.   ptkboolean quit;
  133.   Ppoint pos, size;
  134.   Plimit echoarea;
  135.   Pint lampiconstid;
  136.  
  137.   quit = FALSE;
  138.   currentwindow = 1;
  139.   do
  140.   {
  141.     echoarea = ptk_limit(0.0, devx, 0.0, devy * 0.1);
  142.     ptk_readstring(WS1, "camera", "Input command (default = camera)>", 
  143.                    &echoarea, 20, commandstr, &lencom);
  144.     if (strncmp(commandstr, "camera", lencom) == 0)
  145.     {
  146.       camerainterface(currentwindow);
  147.     }
  148.     else
  149.     if (strncmp(commandstr, "position", lencom) == 0)
  150.     {
  151.       pos.x = ptk_readfloat(WS1, 0.5, "Input position, x (0.5) >", &echoarea);
  152.       pos.y = ptk_readfloat(WS1, 0.5, "Input position, y (0.5) >", &echoarea);
  153.       ptk_setwindowposition(currentwindow, &pos);
  154.     }
  155.     else 
  156.     if (strncmp(commandstr, "size", lencom) == 0)
  157.     {
  158.       Ppoint userpos, usersize;
  159.  
  160.       size.x = ptk_readfloat(WS1, 0.5, "Input size, x (0.5) >", &echoarea);
  161.       size.y = ptk_readfloat(WS1, 0.5, "Input size, y (0.5) >", &echoarea);
  162.       ptk_setwindowsize(currentwindow, &size);
  163.     }
  164.     else
  165.     if (strncmp(commandstr, "iconposition", lencom) == 0)
  166.     {
  167.       pos.x = ptk_readfloat(WS1, 0.5, "Input icon position, x (0.5) >", 
  168.                             &echoarea);
  169.       pos.y = ptk_readfloat(WS1, 0.5, "Input icon position, y (0.5) >", 
  170.                             &echoarea);
  171.       ptk_seticonposition(currentwindow, &pos);
  172.     }
  173.     else 
  174.     if (strncmp(commandstr, "iconsize", lencom) == 0)
  175.     {
  176.       size.x = ptk_readfloat(WS1, 0.1, "Input icon size, x (0.1) >", 
  177.                              &echoarea);
  178.       size.y = ptk_readfloat(WS1, 0.1, "Input icon size, y (0.1) >", 
  179.                              &echoarea);
  180.       ptk_seticonsize(currentwindow, &size);
  181.     }
  182.     else 
  183.     if (strncmp(commandstr, "framesize", lencom) == 0)
  184.     {
  185.       size.x = ptk_readfloat(WS1, 0.01, "Input frame size, x (0.01) >", 
  186.                              &echoarea);
  187.       size.y = ptk_readfloat(WS1, 0.01, "Input frame size, y (0.01) >", 
  188.                              &echoarea);
  189.       ptk_setframesize(currentwindow, &size);
  190.     }
  191.     else 
  192.     if (strncmp(commandstr, "open", lencom) == 0)
  193.     {
  194.       ptk_openwindow(currentwindow);
  195.     }
  196.     else 
  197.     if (strncmp(commandstr, "close", lencom) == 0)
  198.     {
  199.       ptk_closewindow(currentwindow);
  200.     }
  201.     else
  202.     if (strncmp(commandstr, "front", lencom) == 0)
  203.     {
  204.       ptk_frontwindow(currentwindow);
  205.     }
  206.     else 
  207.     if (strncmp(commandstr, "back", lencom) == 0)
  208.     {
  209.       ptk_backwindow(currentwindow);
  210.     }
  211.     else
  212.     if (strncmp(commandstr, "bannerheight", lencom) == 0)
  213.     {
  214.       Pfloat height;
  215.  
  216.       height = ptk_readfloat(WS1, 0.01, "Input banner height >", &echoarea);
  217.       ptk_setbannerheight(currentwindow, height);
  218.     }
  219.     else
  220.     if (strncmp(commandstr, "bannercolours", lencom) == 0)
  221.     {
  222.       Pint bancol, titlecol;
  223.  
  224.       bancol = ptk_readint(WS1, 0, "Input banner colour index >", 
  225.                                   &echoarea);
  226.       titlecol = ptk_readint(WS1, 1, "Input title string colour index >", 
  227.                                   &echoarea);
  228.       ptk_setbannercolours(currentwindow, bancol, titlecol);
  229.     }
  230.     else 
  231.     if (strncmp(commandstr, "phinter", lencom) == 0)
  232.     {
  233.       ptk_callphinter();
  234.     }
  235.     else if (strncmp(commandstr, "hardcopy", lencom) == 0)
  236.     {
  237.       Pint stid, lenname;
  238.       char filename[80];
  239.  
  240.       ptk_readstring(WS1, "window", 
  241.                    "Input filename (default = window) >", &echoarea,
  242.                    80, filename, &lenname);
  243.  
  244.       /* Implementation dependent code, open a hardcopy workstation */
  245. #ifdef SUN
  246.       popen_ws(2, filename, phigs_ws_type_cgm_out);
  247.       ptk_copyviewtable(1, 2);
  248.       ptk_copypostedstruct(1, 2);
  249.       pupd_ws(2, PFLAG_PERFORM);
  250.       pclose_ws(2);
  251. #endif
  252.     }    
  253.     else if (strncmp(commandstr, "help", lencom) == 0)
  254.     {
  255.       printf("windtest options\n");
  256.       printf("----------------\n");
  257.       printf("camera - start up camera interface\n");
  258.       printf("position - set window position (centre)\n");
  259.       printf("size - set window size\n");
  260.       printf("iconposition - set icon position (centre)\n");
  261.       printf("iconsize - set icon size\n");
  262.       printf("framesize - set window frame thickness\n");
  263.       printf("open - open window\n");
  264.       printf("close - close window\n");
  265.       printf("front - bring window/icon to front of display\n");      
  266.       printf("back - send window/icon to back of display\n");      
  267.       printf("bannerheight - set window banner height\n");
  268.       printf("bannercolours - set window banner colours\n");
  269.       printf("phinter - call phinterface\n");
  270.       printf("hardcopy - post window to hardcopy workstation\n");
  271.       printf("quit - exit windtest\n");
  272.     }    
  273.     else 
  274.     if (strncmp(commandstr, "quit", lencom) == 0)
  275.     {
  276.       quit = TRUE;
  277.     }    
  278.     else
  279.     {
  280.       printf("Command unknown\n");    
  281.     }
  282.     pupd_ws(WS1, PFLAG_PERFORM);
  283.   } while (quit == FALSE);
  284. }
  285.  
  286. /*--------------------------------------------------------------------------*/
  287.  
  288. main()
  289. {
  290.   Pint wst, lampid, lampwindow;
  291.   Pint_list lamplist;
  292.   Ppoint pos, size;
  293.   Pint white, black, green, grey, blue, textfont;
  294.   ptkboolean docolour = FALSE;
  295.  
  296.   printf("Demonstrating the PHIGS windows module of the PHIGS Toolkit...\n");
  297.  
  298.   /* Implementation dependent code, open PHIGS and workstation */
  299. #if SUN
  300.   printf("Opening SunPHIGS...\n");
  301.   popen_phigs(PDEF_ERR_FILE, PDEF_MEM_SIZE);
  302.  
  303.   /* Open a workstation. */
  304.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);
  305.  
  306. /*   popen_ws(WS1, (void *)NULL, wst); 
  307.   wst = phigs_ws_type_create( phigs_ws_type_x_tool,
  308.     0);
  309. */
  310. #endif
  311. #ifdef PEXSI
  312.   printf("Opening PEX-SI PHIGS...\n");
  313.   popen_phigs(PDEF_ERR_FILE, PDEF_MEM_SIZE);
  314.  
  315.   /* open the workstation */
  316.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);    
  317. #endif
  318. #ifdef HP
  319.   printf("Opening HP PHIGS...\n");
  320.   popen_phigs(stderr, PDEF_MEM_SIZE);
  321.  
  322.   /* open the workstation */
  323.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);    
  324. #endif
  325.   ptk_inqmaxdevicecoords(WS1, &devx, &devy);
  326.   devz = 0.0;
  327.  
  328. #ifdef SUN
  329. #ifndef SUNMONO
  330.   docolour = TRUE;
  331. #endif
  332. #endif
  333. #ifdef HP
  334. #ifndef HPMONO
  335.   docolour = TRUE;
  336. #endif
  337. #endif
  338. #ifdef PEXSI
  339. #ifndef PEXSIMONO
  340.   docolour = TRUE;
  341. #endif
  342. #endif
  343.  
  344.    pset_disp_upd_st(WS1, PDEFER_WAIT, PMODE_NIVE); 
  345.    
  346.    /* initialise hashtables */
  347.  
  348.    ptk_inithashtables();
  349.    ptk_createhashtable("structureid", 1, 500);
  350.    ptk_createhashtable("label", 1, 50);
  351.    ptk_createhashtable("viewindex", 1, 50);
  352.    ptk_createhashtable("colourindex", 1, 8);
  353.    ptk_createhashtable("windowid", 1, 50);
  354.    ptk_createhashtable("name", 1, 50);
  355.  
  356.    /* set colours */
  357.    if (docolour)
  358.    {
  359.      ptk_setupcolourtable(WS1, 7, colwrd);
  360.      green = ptk_stringtoint("colourindex", "green");
  361.      grey = ptk_stringtoint("colourindex", "grey");
  362.      white = ptk_stringtoint("colourindex", "white");
  363.      black = ptk_stringtoint("colourindex", "black");
  364.      blue = ptk_stringtoint("colourindex", "blue");
  365.    
  366.      /* Implementation dependent code, choose a nice font */
  367. #ifdef SUN
  368.      textfont = PFONT_TRIPLEX;
  369. #endif
  370. #ifndef SUN
  371.      textfont = 1;
  372. #endif
  373.      ptk_setbackgroundcolourind(WS1, grey);
  374.    }
  375.  
  376.    if (ptk_readphinterscript(SCRIPTNAME, NULL, NULL))
  377.    {
  378.      pos = ptk_point(0.5, 0.5);
  379.      size = ptk_point(0.6, 0.6);
  380.      lampwindow = ptk_stringtoint("windowid", "lampwindow");
  381.      ptk_createwindow(WS1, lampwindow, &size, &pos, "lamp window");
  382.      if (docolour)
  383.        ptk_setwindowattrs(lampwindow, textfont, black,
  384.                           green, grey, green, white, black);
  385.      ptk_posttowindow(ptk_stringtoint("windowid", "lampwindow"), 
  386.                       ptk_stringtoint("structureid", "lamp"));
  387.      lamplist.num_ints = 1;
  388.      lamplist.ints = &lampid;
  389.      lampid = ptk_stringtoint("structureid", "lamp");
  390.      ptk_setcameraworld(ptk_stringtoint("windowid", "lampwindow"),
  391.                         &lamplist);
  392.      ptk_postwindow(ptk_stringtoint("windowid", "lampwindow"));
  393.      pupd_ws(WS1, PFLAG_PERFORM);
  394.  
  395.      pos = ptk_point(0.1, 0.9);
  396.      ptk_seticonposition(ptk_stringtoint("windowid", "lampwindow"), &pos);
  397.      options();
  398.      ptk_unpostallfromwindow(ptk_stringtoint("windowid", "lampwindow"));
  399.    }
  400.  
  401.    pclose_ws(WS1);                          /* Close workstation. */
  402.    pclose_phigs();                          /* Close PHIGS. */
  403. }
  404.  
  405. /*--------------------------------------------------------------------------*/
  406.  
  407. /* end of windtest.c */
  408.