home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TCXOS2.ZIP / DEMO / DEMOMISC.C next >
C/C++ Source or Header  |  1991-11-05  |  19KB  |  497 lines

  1. /*=====[ The TesSeRact(TM) CXL User Interface Development System ]======*
  2.  | Copyright (c) 1987-1991, Innovative Data Concepts, Inc.
  3.  | All Rights Reserved.
  4.  |
  5.  | This Library is part of IDC's TesSeRact Development Tools product
  6.  | line. For information about other IDC products, call  1-215-443-9705.
  7.  *-V--------------------------------------------------------------------*
  8.  | $Header:   D:/beta/version/demomisc.c_v   552.16   29 Sep 1991 14:07:20  $
  9.  |
  10.  | $Log:   D:/beta/version/demomisc.c_v  $
  11.  | 
  12.  |    Rev 552.16   29 Sep 1991 14:07:20
  13.  | use help for TcxlInfo
  14.  | 
  15.  |    Rev 552.15   29 Sep 1991 13:46:30
  16.  | added _heapgrow() call for Watcom
  17.  | 
  18.  |    Rev 552.14   29 Sep 1991 13:21:14
  19.  | added farmalloc #defines for Watcom
  20.  | 
  21.  |    Rev 552.13   22 Jun 1991 14:03:44
  22.  | check correct return value from Wopen
  23.  | 
  24.  |    Rev 552.12   21 Jun 1991 16:24:20
  25.  | code consolidation/reduction
  26.  | 
  27.  |    Rev 552.11   21 Jun 1991 14:06:48
  28.  | change top line
  29.  | 
  30.  |    Rev 552.10   19 Jun 1991 15:41:40
  31.  | added Topspeed header for alloc
  32.  | 
  33.  |    Rev 552.9   13 Jun 1991  4:55:10
  34.  | use NlsYes for GetYn prompts
  35.  | 
  36.  |    Rev 552.8   01 Jun 1991  6:59:36
  37.  | don't bother to home the mouse; clean MoveBarDn
  38.  | 
  39.  |    Rev 552.7   17 May 1991 11:55:48
  40.  | do correct large model conditional
  41.  | 
  42.  |    Rev 552.6   21 Apr 1991 16:06:40
  43.  | Fix Typos
  44.  | 
  45.  |    Rev 552.5   21 Apr 1991 12:01:54
  46.  | add unix compatibility updates
  47.  | 
  48.  |    Rev 552.4   15 Apr 1991  6:40:34
  49.  | zortech header fix
  50.  | 
  51.  |    Rev 552.3   15 Apr 1991  6:17:14
  52.  | Fix Typos
  53.  | 
  54.  |    Rev 552.2   23 Mar 1991  9:50:04
  55.  | account for NlsYes characters in GetYn prompts
  56.  | 
  57.  |    Rev 552.1   17 Mar 1991 20:02:52
  58.  | Version 5.52 (after unlog/build)
  59.  *-D--------------------------------------------------------------------*
  60.  | DemoMisc.C : Miscellaneous common utility functions for the various
  61.  | TCXL demonstration programs.
  62.  *-N-----------------------[ Notes and Caveats ]------------------------*
  63.  | 1) FarVsave() and FarVrestore() will ONLY work correctly in small-data
  64.  |    models (small, medium).  In large-data models (compact, large),
  65.  |    they may fail due to heap collisions.  There's only a far heap in
  66.  |    large-data.
  67.  *======================================================================*/
  68. #include <string.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <TCXL_os.h>
  72. #include "demo\TCXLdemo.h"             /* TCXL demo-specific            */
  73. #if(_TCXL_cc_ == 22)                   /* needed for Zortech            */
  74. #  include <dos.h>
  75. #endif
  76. #if(_TCXL_cc_ == 23)                   /* needed for MicroSoft C        */
  77. #  include  <malloc.h>
  78. #  define     farmalloc(s)   _fmalloc(s)
  79. #  define     farfree(p)     _ffree(p)
  80. #endif
  81. #if(_TCXL_cc_ == 25)                   /* needed for Watcom C           */
  82. #  include  <malloc.h>
  83. #  define     farmalloc(s)   _fmalloc(s)
  84. #  define     farfree(p)     _ffree(p)
  85. #endif
  86. #if(_TCXL_cc_ == 24)                   /* needed for TopSpeed C         */
  87. #  include <alloc.h>
  88. #endif
  89. #include <TCXLmem.h>                   /* TCXL memory system            */
  90. #include <TCXLfar.h>                   /* TCXL far-memory               */
  91. #include <TCXLcod.h>                   /* TCXL key-codes                */
  92. #include <TCXLnls.h>                   /* TCXL NLS sub-system           */
  93.  
  94. /*----------------------------[ Local data ]----------------------------*/
  95.  
  96. LCL   VcelFP   CDC   savescrn;         /* ptr to saved DOS screen       */
  97.  
  98. LCL   ChrP  CDC   error_text[] =       /* global error message table    */
  99. {  NULL,                               /* errnum = 0, no error          */
  100.    NULL,                               /* errnum = 1, windowing error   */
  101.    "Usage : TCXLDEMO [-switches]\n\n"  /* errnum = 2, syntax error      */
  102.    "\t-c = CGA snow elimination\n"
  103.    "\t-b = BIOS screen writing\n"
  104.    "\t-m = force monochrome text attributes",
  105.    "Out of memory!",                   /* errnum = 3, out of memory     */
  106.    "Far Heap Exhausted!"               /* errnum = 4, out of far heap   */
  107. };
  108.  
  109. LCL   ChrP CDC QuitMsg = "\n Quit demo, are you sure? \033A\156Y\b";
  110.  
  111. /*================[ Add a shadow to the current window ]================*/
  112.  
  113. VOID  CTYP  AddShadow(NOARG)
  114. {
  115.    Wshadow(LGREY|_BLACK);              /* simple, ain't it?             */
  116. }  /* AddShadow() */
  117.  
  118. /*===============[ Confirm the user really wants to quit ]==============*/
  119.  
  120. VOID  CTYP  ConfirmQuit(NOARG)
  121. {
  122.    KbndP    kblist;
  123.  
  124.    MouHide();                          /* hide mouse, if visible        */
  125.    kblist = KbndChg(NULL);             /* hide any existing hot keys    */
  126.  
  127.    if(ERR_ARG == Wopen(9, 26, 13, 55, 0, /* pop up a confirmation window*/
  128.          WHITE|_BROWN, WHITE|_BROWN))
  129.       ErrorExit(1);                    /* windowing error!              */
  130.  
  131.    AddShadow();                        /* add shadow, ask user          */
  132.    QuitMsg[30] = NlsYes;
  133.    Wputs(QuitMsg);
  134.    KeyClear();                         /* flush any lurking keys        */
  135.    CurPush(CUR_SHO, -1, -1, -1);       /* show cursor, save state       */
  136.    if(KwGetYn(NlsYes) == 'Y')          /* user said quit                */
  137.       NormalExit();
  138.  
  139.    Wclose();                           /* close window                  */
  140.    CurPop();                           /* restore cursor                */
  141.    KbndChg(kblist);                    /* restore any hidden hot keys   */
  142.    MouShow();                          /* show the mouse, if we hid it  */
  143. }  /* ConfirmQuit() */
  144.  
  145. /*=======================[ Abnormal termination ]=======================*
  146.  | If <e> is 1, it's a windowing system error.  Otherwise look up the
  147.  | error in the error message table.
  148.  *----------------------------------------------------------------------*/
  149.  
  150. VOID  CTYP   ErrorExit(IntT e)
  151. {
  152.    if(e)
  153.    {  if(e == 1)                       /* window-system error           */
  154.       {  if(Wperror(WerrMsg()))        /* use window if possible    jpl */
  155.             Werror();
  156.       }
  157.       else                             /* look up error message         */
  158.       {  if (Wperror(error_text[e]))   /* use window if possible    jpl */
  159.             printf("\n%s\n", error_text[e]);
  160.       }
  161.       CurPop();                        /* restore cursor                */
  162.       exit(e);                         /* _TcxlTerm() will clean up     */
  163.    }
  164. }  /* ErrorExit() */
  165.  
  166. /*===============[ Save the screen image on the far heap ]==============*/
  167.  
  168. #if (_TCXL_dat_ == 0)      /* small data memory models -- use far heap  */
  169.  
  170. VcelFP   CTYP  FarVsave(AtrT Atr)
  171. {
  172.    VcelFP   FarPtr;
  173.    VcelP    NearPtr;
  174. #if   (_TCXL_cc_ == 22)                /* Zortech uses a DwdT           */
  175.    DwdT     sz;
  176. #else                                  /* everybody else uses a WrdT    */
  177.    WrdT     sz;
  178. #endif
  179.  
  180.    if(NULL == (NearPtr = Vsave()))     /* normal Vsave()                */
  181.       ErrorExit(3);                    /* out of memory!                */
  182.  
  183.    sz = (VidDep * VidWid * SIZ(VcelT)) + 1;
  184.    if(NULL == (FarPtr = (VcelFP)farmalloc(sz)))
  185.       ErrorExit(4);                    /* out of far heap!              */
  186.  
  187.    _PutFar((BytFP)FarPtr, (BytP)NearPtr, sz);   /* copy to far heap     */
  188.    free(NearPtr);                      /* free normal buffer            */
  189.    Vclear(Atr);                        /* clear the screen              */
  190.    return(FarPtr);                     /* return far-heap pointer       */
  191. }  /* FarVsave() */
  192.  
  193. #else                      /* large data memory models -- use near heap */
  194.  
  195. VcelFP   CTYP  FarVsave(AtrT Atr)
  196. {
  197.    VcelP    NearPtr;
  198.  
  199.    if(NULL == (NearPtr = Vsave()))     /* normal Vsave()                */
  200.       ErrorExit(3);                    /* out of memory!                */
  201.  
  202.    Vclear(Atr);                        /* clear the screen              */
  203.    return(NearPtr);                    /* return heap pointer           */
  204. }
  205.  
  206. #endif
  207.  
  208. /*===========[ Restore saved screen image from the far heap ]===========*/
  209.  
  210. #if (_TCXL_dat_ == 0)      /* small data memory models -- use far heap  */
  211.  
  212. VOID  CTYP  FarVrestore(VcelFP FarPtr)
  213. {
  214.    VcelP    NearPtr;
  215.    WrdT     sz;
  216.  
  217.    sz = (VidDep * VidWid * SIZ(VcelT)) + 1;
  218.    if(NULL == (NearPtr = (VcelP) malloc(sz)))
  219.       ErrorExit(3);                    /* out of memory!                */
  220.  
  221.    _GetFar((BytP)NearPtr, (BytFP)FarPtr, sz);   /* copy from far heap   */
  222.    farfree(FarPtr);                    /* free far buffer               */
  223.    Vrestore(NearPtr);                  /* restore the screen            */
  224. }  /* FarVrestore() */
  225.  
  226. #else                      /* large data memory models -- use near heap */
  227.  
  228. VOID  CTYP  FarVrestore(VcelFP FarPtr)
  229. {
  230.    Vrestore(FarPtr);
  231. }
  232.  
  233. #endif
  234.  
  235. /*===============[ Initialize TCXL for the demo program ]===============*/
  236.  
  237. VOID  CTYP  Initialize(NOARG)
  238. {
  239. #if(_TCXL_cc_ == 25)                   /* needed for Watcom C           */
  240.    _heapgrow();
  241. #endif
  242.    TcxlInit();                         /* general initialization        */
  243.    KextOff();                          /* no extended key BIOS          */
  244.    CurPush(CUR_HID, CUR_NOR, 0, 0);    /* save the cursor state, hide   */
  245.                                        /*    and home the cursor        */
  246.    savescrn = FarVsave(LGREY|_BLACK);  /* save the screen               */
  247.  
  248.    KbndSet(Key_A_X, ConfirmQuit, 0);   /* bind [Alt-X] to ConfirmQuit() */
  249.  
  250.    HlpDef("TCXLDEMO.HLP", Key_F1,      /* initialize help system,       */
  251.       YELLOW|_RED, YELLOW|_RED,        /*  help key = [F1]              */
  252.       LRED|_RED, WHITE|_RED, RED|_LGREY, BOX_DBL, _VFVCP(PreHelp));
  253.  
  254.    LeftRow = ((VidDep - 1) / 2) - 5;   /* set up display coordinates    */
  255.    RightRow = LeftRow + 6;
  256.    LeftCol = ((VidWid - 1) / 2) - 9;
  257.    RightCol = LeftCol + 19;
  258.  
  259. }  /* Initialize() */
  260.  
  261. /*================[ Define and process a main demo menu ]===============*
  262.  | First, push global help category onto help stack, so that when [F1] is
  263.  | pressed while the selection bar is on an item without help, the global
  264.  | help category will be used. Build the main menu defined by the Mbld
  265.  | object at <mp> and process it
  266.  *----------------------------------------------------------------------*/
  267.  
  268. VOID  CTYP  MainMenu(MbldP mp)
  269. {
  270.    HlpPush(H_GLOBAL);                  /* push global help catagory     */
  271.  
  272.    MnuBuild(mp);                       /* define the main menu          */
  273.    if(MnuGet() == -1)                  /* process the menu              */
  274.       if(TcxlErr > W_ESCPRESS)
  275.          ErrorExit(1);                 /* window error                  */
  276.  
  277.    HlpDrop();                          /* pop help category off stack   */
  278. }  /* MainMenu() */
  279.  
  280. /*===========[ Move the selection bar down in the Main Menu ]===========*
  281.  | Select the next menu item by getting the address of the current menu,
  282.  | then setting the current tag-id to the tag-id of the next menu item.
  283.  | If we had had not numbered the menu item tag-ids sequentially, this
  284.  | would not work.
  285.  *----------------------------------------------------------------------*/
  286.  
  287. VOID  CTYP  MoveBarDn(NOARG)
  288. {
  289.    REG TagT t = IdfTag(ItmCur());
  290.    if(t < 10)                          /* from main menu only           */
  291.       ItmNxt(t + 1);                   /* select next item              */
  292. }  /* MoveBarDn() */
  293.  
  294. /*========================[ Normal termination ]========================*
  295.  | Restore the original screen and cursor before exiting to DOS with
  296.  | ERRORLEVEL 0.
  297.  *----------------------------------------------------------------------*/
  298.  
  299. VOID  CTYP  NormalExit(NOARG)
  300. {
  301.    FarVrestore(savescrn);              /* restore the screen            */
  302.    MouPop();                           /* restore the mouse             */
  303.    CurPop();                           /* restore the cursor            */
  304.    exit(0);                            /* _TcxlTerm() will clean up     */
  305. }  /* NormalExit() */
  306.  
  307.  
  308. /*==[ Main background, program title and system information windows ]===*/
  309.  
  310. ChrT Ins1[] = "INSTALLED";
  311. ChrT Ins2[] = "NOT INSTALLED";
  312.  
  313. VOID  CTYP  OpenBkgrnd(NOARG)
  314. {
  315.    ChrT  str[90];
  316.                                        /* demo background window        */
  317.    if(ERR_ARG == Wopen(0, 0, VidDep - 2, VidWid - 1, 5, 0, LGREEN|_GREEN))
  318.       ErrorExit(1);                    /* windowing error!              */
  319.  
  320.                                        /* system information pop-up     */
  321.    if(!WpopUp(CNT_HOR, VidDep - 8, 0, VidDep - 3, 60, 0, LGREY|_RED,
  322.          LGREY|_RED))
  323.       ErrorExit(1);                    /* windowing error!              */
  324.  
  325.    AddShadow();                        /* add a shadow, display status  */
  326. #if defined(_TCXLos2_)
  327.    sprintf(str, "OS/2 version: %d.%d\n",
  328.       (_SysVer >> 8) & 0xFF, _SysVer & 0xFF);
  329.    WputCen(str);
  330.  
  331.    sprintf(str, "This program is running as a %s application\n",
  332.       _SysFul ? "full-screen" : "VIO-windowed");
  333.    WputCen(str);
  334.  
  335.    sprintf(str, "in screen group %d.\n", _SysSgp);
  336.    WputCen(str);
  337.  
  338.    sprintf(str, "Process ID: %d   Parent's process ID: %d",
  339.       _SysPid, _SysPpd);
  340.    WputCen(str);
  341. #else
  342.    sprintf(str, "DPMI Status: %s   VCPI Status: %s\n",
  343.       (MemDPMI ? Ins1 : Ins2),
  344.       (MemVCPI ? Ins1 : Ins2));
  345.    WputCen(str);
  346.  
  347.    sprintf(str, "Total EMS: %dK   Free EMS: %dK\n",
  348.       MemEtot * 16, MemEfre * 16);
  349.    WputCen(str);
  350.  
  351.    sprintf(str, "Free XMS: %dK   HMA is %s\n", MemXtot,
  352.       (MemHfre ? "AVAILABLE" : "IN USE"));
  353.    WputCen(str);
  354.  
  355.    sprintf(str, "Ultravision is %s   Screen Size is %dx%d",
  356.       (TcxlUv ? Ins1 : Ins2), VidWid, VidDep);
  357.    WputCen(str);
  358. #endif
  359.  
  360.    WsetFil('\xB0');                    /* bottom status bar, fancy fill */
  361.    if(ERR_ARG == Wopen(VidDep - 1, 0, VidDep - 1, VidWid - 1, 5, 0, 
  362.          LGREY|_RED))
  363.       ErrorExit(1);                    /* windowing error!              */
  364.  
  365.    Wprts(0, 1, LGREY|_RED, "[F1]=Help");
  366.    WprtCen(0, LGREY|_RED, "Select demonstration to view.");
  367.    Wprtrj(0, MsByt(VidWid - 2), LGREY|_RED, "[Alt-X]=Quit");
  368.    WsetFil(' ');                       /* blank fill again              */
  369.                                        /* program title pop-up          */
  370.    if(!WpopUp(CNT_HOR, 0, 12, 4, 67, 0, LRED|_MAGENTA, LRED|_MAGENTA))
  371.       ErrorExit(1);                    /* windowing error!              */
  372.  
  373.    AddShadow();                        /* add a shadow, display text    */
  374.    WprtCen(0, WHITE|_MAGENTA,
  375.       "Welcome to the TesSeRact CXL Demonstration Program!");
  376.    WprtCen(2, LCYAN|_MAGENTA,
  377.       "Copyright (c) 1987-1991, Innovative Data Concepts");
  378. }  /* OpenBkgrnd() */
  379.  
  380. /*=======[ Parse the command line for valid invocation switches ]=======*
  381.  | If any valid switches are found, set the appropriate video variable.
  382.  | If any invalid switches are found, display a syntax message. <ac> and
  383.  | <av> are the <argc> and <argv> arguments to main().
  384.  *----------------------------------------------------------------------*/
  385.  
  386. VOID  CTYP  ParseCmdLine(IntT ac, ChrP av[])
  387. {
  388.    REG IntT    i, j;
  389.    ChrP        p;
  390.  
  391.    for(i = 1; i < ac; i++)
  392.    {  p = av[i];
  393.       if(*p == '-' || *p == '/')
  394.       {  for(j = 1; *(p + j); j++)
  395.          {  switch(NlsToLower(*(p + j)))
  396.             {  case 'c':               /* CGA video                     */
  397.                   VidParm(VP_CGA);
  398.                   break;
  399.  
  400.                case 'b':               /* BIOS video                    */
  401.                   VidParm(VP_BIO);
  402.                   break;
  403.  
  404.                case 'm':               /* monochrome video              */
  405.                   VidParm(VP_MON);
  406.                   break;
  407.  
  408.                default:                /* unknown switch                */
  409.                   ErrorExit(2);        /* syntax error!                 */
  410.             }
  411.          }
  412.       }
  413.       else                             /* unknown parameter             */
  414.          ErrorExit(2);                 /* syntax error!                 */
  415.    }
  416. }  /* ParseCmdLine() */
  417.  
  418. /*======[ Parse a MMDDYY date string into separate numeric values ]=====*
  419.  | <b> is the date string, <m>, <d>, and <y> point to integers to receive
  420.  | the month, day and year
  421.  *----------------------------------------------------------------------*/
  422.  
  423. VOID  CTYP  ParseDate(ChrP b, IntP m, IntP d, IntP y)
  424. {
  425.    ChrT  t[7];
  426.  
  427.    strcpy(t, b);
  428.    *y = 1900 + atoi(t + 4);            /* year                          */
  429.    *(t + 4) = EOS;
  430.    *d = atoi(t + 2);                   /* day                           */
  431.    *(t + 2) = EOS;
  432.    *m = atoi(t);                       /* month                         */
  433. }  /* ParseDate() */
  434.  
  435. /*=======================[ Help "open" function ]=======================*
  436.  | Called when the user presses the help key and the help window is
  437.  | opened.
  438.  *----------------------------------------------------------------------*/
  439.  
  440. VOID  CTYP  PreHelp(NOARG)
  441. {
  442.    AddShadow();                        /* add a shadow to window        */
  443.    KbndSet(Key_A_X, ConfirmQuit, 0);   /* bind Alt[X] as exit key       */
  444. }  /* PreHelp() */
  445.  
  446. /*=====================[ Main menu "open" function ]====================*
  447.  | Called when the main demo menu window is opened.
  448.  *----------------------------------------------------------------------*/
  449.  
  450. VOID  CTYP  PreMenu1(NOARG)
  451. {
  452.    AddShadow();                        /* add a shadow                  */
  453. }  /* PreMenu1() */
  454.  
  455. /*======[ Display a pause message, then pause for for a keypress ]======*
  456.  | <r> is the window-row to display on.
  457.  *----------------------------------------------------------------------*/
  458.  
  459. VOID  CTYP  PressAKey(BytT r)
  460. {
  461.    PressAKey1(r, 36);                  /* wait for 2 seconds            */
  462. }  /* PressAKey() */
  463.  
  464. /*======[ Display a pause message, then pause for for a keypress ]======*
  465.  | <r> is the window-row to display on, <w> is the number of clock-ticks
  466.  | to wait.
  467.  *----------------------------------------------------------------------*/
  468.  
  469. VOID  CTYP  PressAKey1(BytT r, IntT w)
  470. {
  471.    AtrT a;
  472.  
  473.    if(DemoFast)                        /* check for window-demo delay   */
  474.       return;
  475.  
  476.    a = MsAtr((_BLINK|YELLOW)|(WdfDat(WctlAct) & (~WHITE)));      /* jpl */
  477.    WprtCen(r, a, "Press a key");       /* display message               */
  478.    if(w == -1)
  479.       KeyWait();                       /* wait for a key                */
  480.    else
  481.    {  if((ChrT)KeyTime(w) == ESC)      /* quit on [Esc]                 */
  482.          ConfirmQuit();
  483.    }
  484.    WprtCen(r, a, "           ");       /* erase message                 */
  485. }
  486.  
  487. /*=========[ Display window of information about TCXL and IDC ]=========*
  488.  | Called from the [I]nformation item in the main demo menu
  489.  *----------------------------------------------------------------------*/
  490.  
  491. VOID  CTYP  TcxlInfo(NOARG)
  492. {
  493.    HlpAdd(H_GLOBAL);                   /* save old, set new help  y     */
  494.    HlpGet();
  495.    HlpDrop();
  496. }  /* TcxlInfo() */
  497.