home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 371_01 / using.doc < prev    next >
Text File  |  1992-05-09  |  37KB  |  948 lines

  1.     USERS GUIDE TO WINDOSIO
  2.  
  3. WinDosIO is a powerful dynamic link library which allows structured programming, terminal IO and established graphics routines to work under Microsoft Windows.  Yet the full Windows API is always available. Your programs can constitute any combination of the structured and event driven paradigms. Many DOS programs can be ported to Windows without changing a single line of code!
  4.  
  5.  
  6. Getting Started
  7.  
  8. The easiest way to use WinDosIO is to let WinDosIO take care of all  the Windows functions for you. Take a look at EXAMPL2, included on the distribution disk and printed below.
  9.  
  10.  
  11. /***************************************************************************
  12. * EXAMPL2.C
  13. * Hello World
  14. **************************************************************************/
  15.  
  16. #include <WinDosIO.h>
  17.  
  18. main()
  19.  {
  20.     printf("Hello, World\n");
  21.         return 0;
  22.  }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. Look familiar? Its the classic "Hello, World" program for beginning C programmers. Have you ever seen the Window's version of this simple program? It goes on for several pages. Yet the WinDosIO version of this program looks exactly like the DOS or UNIX version with one exception. Instead of including <stdio.h>, <WinDosIO.h> was included. That is only because this program can be compiled under any memory model. However, since printf is located in the WinDosIO DLL, it is always far(a 4 byte address). If you compile with the large memory model then <stdio.h> can be included instead of <WinDosIO.h>.  In that case, "Hello, World" converts from DOS to Windows with no changes at all to the code! It can be resized, moved, and up to 32 instances can be run at once. 
  30.  
  31.  
  32. It Isn't Magic
  33.  
  34. Part of the secret behind the seamless conversion is a source module WinDosIO.C, included on the distribution disk. You compile and link this module with your DOS style program and the WinDosIO import library. A compiler which includes the Window's Software Development Kit is also required. By following the sample compile and link instructions below, you now know enough to begin converting DOS programs to Microsoft Windows.
  35.  
  36.  
  37. WinDosIO.C, The Secret Unfolds
  38.  
  39. Experienced users of WinDosIO will want to take advantage of  Microsoft Windows capabilities to enhance their programs ported over from DOS. Understanding  how to integrate the environments, requires a basic understanding of WinDosIO.C
  40.  
  41. /***************************************************************************
  42. *  WinDosIO WinMain Module - By compiling this module and linking with your
  43. *  DOS application in C or C++, most DOS applications can be run under
  44. *  Microsoft Windows with no changes at all(The header file WinDosIO.h
  45. *  should be included to eliminate warnings and even potential errors
  46. *  if not compiling with the large model!)
  47. *  For WinDOSIO Version 2.0 BETA
  48. *  Copyright Graubart-Cervone Software 1991,1992
  49. *****************************************************************************/
  50. #include <windows.h>
  51. #include <string.h>
  52.  
  53. #include <WinDosIO.h>
  54.  
  55.  
  56. #define MAXARGS 16
  57.  
  58. #define MAX_CMDLINE 128
  59.  
  60. int pascal WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  61.            LPSTR lpszCmdline, int cmdshow)
  62.  
  63.  {
  64.     struct CWS cws;
  65.     struct CWS far *cwsP;
  66.     char cmdLineBuffer[MAX_CMDLINE];
  67.     HWND hwnd;
  68.     MSG msg;
  69.     RECT r;
  70.     char *argv[MAXARGS];
  71.     int argc;
  72.     short i;
  73.  
  74.     /* Adapt to any memory model */
  75.     for (i = 0; i < MAX_CMDLINE; i++)
  76.         if (!(cmdLineBuffer[i] = *lpszCmdline++)) break;
  77.       /* Initialize terminal IO */
  78.     WinDosIO(WD_INIT,hInstance,0);
  79.     WinDosIO(WD_REGISTER_WINDOW,hPrevInstance,0);
  80.     hwnd = WinDosIO(WD_CREATE_MAIN_WINDOW,cmdshow,
  81.                     (long)((LPSTR)"WinDosIO"));
  82.  
  83.     GetClientRect(hwnd,&r);
  84.  
  85.  
  86.     cws.cwsTop = r.top;
  87.     cws.cwsLeft = r.left;
  88.     cws.cwsXSize = r.right - r.left;
  89.     cws.cwsYSize = r.bottom - r.top;
  90.     cws.cwsTitle = "";
  91.     cws.cwsFlags = 0;
  92.     cwsP = &cws;
  93.     WinDosIO(WD_CREATE_CHILD_WINDOW,0,(unsigned long)cwsP);
  94.     argv[0] = "WinDosIO";
  95.     if (argv[argc=1] = strtok(cmdLineBuffer," "))
  96.        for (argc = 2; argc < MAXARGS; argc++)
  97.         if (!(argv[argc] = strtok(NULL," "))) break;
  98.  
  99.     main(argc,argv);
  100.  
  101.  
  102.  
  103.     /* Use WinDosIO(WD_DESTROY,hwnd, 0); to terminate application
  104.        without having to click on system close icon.
  105.     */
  106.  
  107.     while( GetMessage( &msg, 0, 0, 0 ) != 0 )
  108.          {
  109.         TranslateMessage( &msg );
  110.         DispatchMessage( &msg );
  111.          }
  112.  
  113.     return 0;
  114.  }
  115.  
  116.  
  117.  
  118.  
  119. Notice the #include files at the top include both <windows.h> and <WinDosIO.h>. Windows.h is included with the Window's Software Development Kit. The #define's indicate that up to 16 command line arguments can be given(15 plus the program name) in a command line not to exceed 128 characters. 
  120.  
  121.  
  122. The WinMain routine is the high level routine of all Windows programs in C and C++. It is declared as returning an integer and following the Pascal calling convention for its arguments. The function is called with four arguments. The first, hInstance is a unique instance handle for this particular instance of the program. The second argument hPrevInstance is non-zero if at least one previous instance of the program is currently executing. The third argument is the command line used to execute the program. The fourth indicates how to initially display the main window.
  123.  
  124. WinDosIO.C must convert the command line into standard argc, argv format. To parse in a memory model consistent manner, the command line is first moved onto the stack.
  125.  
  126.  
  127. Next the WinDosIO DLL is initialized with the instance handle of the program. Without this initialization call , WinDosIO(WD_INIT, hInstance, 0) , no other WinDosIO functions will operate properly. If you write your own WinMain function, you must initialize WinDosIO before using any of the functions.
  128. Every Windows program has at least one window, often called the main or high level window. Associated with this window is a window procedure which handles messages sent by Windows itself. To create this main window two WinDosIO function calls are required. The first WinDosIO(WD_REGISTER_WINDOW, hPrevInstance, 0) associates certain properties,  including the window procedure with the WinDosIO window class. hPrevInstance is an argument, because this is only done if not previously done by another instance of the program currently running. Having registered the WinDosIO window class, the main window itself is created. This is done with a call to  WinDosIO(WD_CREATE_MAIN_WINDOW, cmdshow, windowTitle). The windowTitle must be a far pointer to a character string cast as a long. The example shows how to do this with a simple string. 
  129.  
  130.  
  131. This is all well and good for many programs, however, as you gain proficiency in Window's programming, you will want to create your own main window and enhance the window procedure for additional functionality. Here is the code which is executed in the DLL when these two WinDosIO functions are executed.
  132.  
  133.  
  134.  
  135.  
  136.  
  137. // Some of the constructs, such as this comment are in C++
  138.  
  139. long far pascal WndProc(HWND, WORD, WORD, LONG);
  140.  
  141. // Called when WinDosIO(WD_REGISTER_WINDOW,...) is called
  142. static void RegisterWindow(HANDLE hPrevInstance)
  143.  {
  144. // Check to insure application has been registered
  145. // If user registers their own window, this is not required 
  146.     AI = GetAppinfo();
  147.     if (!AI)
  148.        Exit(DOSCOM,1);
  149. // If another instance of the program has not already registered
  150. // the window class. 
  151.     if (!hPrevInstance)
  152.      {
  153.         WNDCLASS wc;
  154.         wc.style = 0;
  155.         wc.lpfnWndProc = WndProc; // Window procedure
  156.         wc.cbClsExtra = 0;
  157.         wc.cbWndExtra = 0;
  158.         wc.hInstance = AI->hInst;
  159.         wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
  160.         wc.hCursor = LoadCursor( 0, IDC_ARROW );
  161.         wc.hbrBackground = COLOR_WINDOW + 1;
  162.         wc.lpszMenuName = "WinDosIO";
  163.         wc.lpszClassName = "WinDosIO"; // Class Name
  164.         RegisterClass( &wc );
  165.       }
  166.  }
  167.  
  168. // Called when WinDosIO(WD_CREATE_MAIN_WINDOW,...) is called
  169. static HWND CreateMainWindow(short cmdshow, char *title)
  170.  {
  171.     AI = GetAppinfo();
  172.     if (!AI)
  173.     Exit(DOSCOM,3);
  174.     HWND hwnd = CreateWindow(
  175.    "WinDosIO",  // The window class registered above
  176.        title,
  177.        WS_OVERLAPPEDWINDOW,
  178.         CW_USEDEFAULT,  0,  CW_USEDEFAULT, 0,
  179.          0, 0,  AI->hInst,  NULL
  180.              };
  181.        AI->owner = hwnd;
  182.        ShowWindow(hwnd, cmdshow);
  183.        return hwnd; // Return the handle to the newly created window
  184.  }
  185. // This is the minimum Main Window Procedure required for WinDosIO
  186. long far pascal WndProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  187.  {
  188.   switch( msg )
  189.    {
  190.        case WM_SETFOCUS:
  191.                  WinDosIO(WD_SETFOCUS,0,0);
  192.                   break;
  193.        case WM_CLOSE:
  194.                   WinDosIO(WD_DESTROY,hwnd,0);
  195.                    return FALSE;
  196.        case WM_DESTROY:
  197.                   PostQuitMessage(0);
  198.                    return FALSE;
  199.    }
  200. return DefWindowProc(hwnd,msg,wParam,lParam);
  201. }
  202.  
  203.  
  204.  
  205.  
  206. If you choose to create your own main window, there are three messages that the associated window procedure must respond to. These are WM_SETFOCUS, WM_CLOSE and WM_DESTROY. The window procedure must also be exported in the EXPORTS section of the .def file or with the _export keyword available on some compilers.
  207.  
  208. The WM_SETFOCUS uses WinDosIO(WD_SETFOCUS,0,0) to inform the DLL that the current termIO window(See below), is to get the input (and output) focus. Your program can take other actions here as well.
  209.  
  210. The WM_CLOSE message closes down WinDosIO for your application, by calling WinDosIO(WD_DESTROY,hwnd,0). hwnd is the handle of the main window returned by CreateMainWindow above. This WD_DESTROY function can also be called as the last line of your program, if you do not wish the window to persist beyond the programs termination. If you wish to terminate early, call WinDosIO(WD_DESTROY,...) before calling exit(). 
  211.  
  212. The WM_DESTROY message will be posted by the WD_DESTROY function called out of WM_CLOSE, once it has closed down WinDosIO. Like most standard Windows programs, it posts a WM_QUIT message.
  213.  
  214.  
  215. If you register your own main window class, then you must also write the corresponding window procedure. You can create your own main window of class "WinDosIO"  after calling WinDosIO(WD_REGISTERWINDOW,...) without writing a window procedure.
  216.  
  217. With the main window created, there is still another step required before you can use printf's, gets, gotoxy, arc, etc. You must create at least one termIO window. This is a window of a pre-registered class which serves as a glass TTY for your terminal input and output, as well as graphics. Any given application can create up to 128 of these windows, although usually one is sufficient. The generic WinDosIO.C creates one of these windows . It is created to be the same size as the main window, so the user only sees one window. This is done in WinDosIO.C by getting the size of the main window with GetClientRect, filling the CWS structure with this information and calling WinDosIO(WD_CREATE_CHILD_WINDOW,0,(unsigned long)cwsP) to actually create the window.
  218.  
  219.  You can fill in cws with your own values to control the size, position and other properties of the termIO window, or you can create the termIO window yourself. Here is the code used inside of the DLL to create a termIO window.
  220.  
  221. static HWND CreateChildWindow(short, CWS *cws)
  222. {
  223.     AI = GetAppinfo();
  224.     if (!AI)
  225.         Exit(DOSCOM,5);
  226.     if (!AI->owner)
  227.         Exit(DOSCOM,6);
  228.     HWND idWindow = CreateWindow("termIO",cws->cwsTitle,
  229.               WS_CHILD | cws->cwsFlags,
  230.               cws->cwsLeft,cws->cwsTop,cws->cwsXSize,cws->cwsYSize,
  231.               AI->owner,20,AI->hInst,0);
  232.     if (!idWindow)
  233.         Exit(DOSCOM,7);
  234.     ShowWindow(idWindow,SW_SHOW);
  235.     UpdateWindow(idWindow);
  236.     return idWindow;
  237. }
  238.  
  239.  
  240. The important thing to note is that the window is of type "termIO". AI->owner contains the handle of the parent or main window created above, while AI->hInst contains the instance handle of the application.
  241.  
  242. After creating the termIO window , WinDosIO.C parses the command line into argc, argv format. Once that is complete, it calls main, your program, with argc and argv. With all of the initialization complete, your program can call printf and the over 200 other functions provided in the WinDosIO DLL. When your program has completed and main returns, WinDosIO.C falls into a standard windows message loop, waiting to be terminated.
  243.  
  244.  
  245. Proper Termination
  246.  
  247. With Windows, the user can terminate your program, by clicking on the system close icon long before you planned on having the program terminate. Unless you disallow the close in the WM_CLOSE message handling procedure, WinDosIO will be closed down and the WM_QUIT message will be posted. When this happens, WinDosIO cooperates by returning a negative return code, a NULL pointer or nothing depending on function type. It also sets its internal error to WDE_USERQUIT. This can be obtained with the WinDosIO(WD_ERRNO,0,0) function.
  248.  
  249. Most DOS programs do not bother checking the return codes from printf and other such functions, especially for a negative value. This is OK, as long as the program does not get into a loop waiting for IO and not checking for EOF or NULL pointers, depending on function. The most well behaved programs will sense an error, and return from main. Another option is to call exit() although this could result in a non-graceful termination with the infamous UAE. Another option is to do nothing, falling through to the end, assuming no damage will be done due to lack of IO, and assuming there are no infinite loops waiting on a specific input without checking for error returns. The latter case could hang up Windows itself! If there are such critical periods, the application developer should set a flag which disables the WM_CLOSE action. The flag must be cleared at some point, or the user will not be able to terminate the program.
  250.  
  251. case WM_CLOSE:
  252.     if (!closeDisabled)
  253.         WinDosIO(WD_DESTROY,hwnd,0);
  254.     return FALSE; 
  255.  
  256.  
  257. Memory Models and WinDosIO
  258.  
  259. Programs can be compiled in any of the memory models, however, all functions in the DLL are far and all pointers must also be far. If you compile with the large model, this is not a problem. However, if you compile with any other model, certain precautions should be taken.
  260.  
  261. Make sure that stdio.h, conio.h and graphics.h(graph.h) are not included. All correct function prototypes can be found in WinDosIO.h. This only leaves the problem of  arguments that cannot be prototyped such as the 2nd and subsequent arguments in printf and scanf. MAKE SURE YOU EITHER DECLARE ANY POINTERS USED AS FAR OR CAST THEM AS FAR. There is no way the compiler can warn you about this problem.
  262.  
  263. One method of development is to use the large model until the program is working and then switch to a more efficient memory model. For existing programs, compile first with the large model, to eliminate this potential source of error.
  264.  
  265.  
  266. Getting a Window Handle
  267.  
  268. Even if you did not create your own termIO window(s), you can get the handle of the current termIO window with a call to WinDosIO(WD_GET_HANDLE,0,0). Once you have the handle to the window, there are many things that can be done, such as moving, resizing, intercepting and sending messages.  
  269.  
  270.  
  271. Getting a Device Context Handle
  272.  
  273. The graphics routines in the WinDosIO library give you a great deal of power. However, it would be nice to mix the very powerful Windows GDI drawing functions on the same window termIO window used for the WinDosIO graphics functions. In order to do make these drawings, you must obtain a device context handle for the termIO window.
  274.  
  275.  This is not the device context handle directly associated with the screen, but rather one associated with a bitmap which keeps the termIO window persistent. For that reason, the drawing is done prior to the actual painting of the screen. Here is an example drawing a rounded rectangle on the current termIO window.
  276.  
  277. HANDLE hDev = WinDosIO(WD_GET_HDC,0,0);
  278. HWND hWnd = WinDosIO(WD_GET_HANDLE,0,0);
  279. RECT r;
  280. RoundRect(hDev,r.left = xLeft, r.top = yTop, r.right = xRight,
  281.                  r.bottom =  yBottom,  xCornerEllipse, yCornerEllipse);
  282. InvalidateRect(hWnd,&r,FALSE);
  283. SendMessage(hWnd,WM_PAINT,0,0);
  284.  
  285. This code can be freely mixed with other graphics calls, printf's, getch, etc. 
  286.  
  287.  
  288. Input and Output Focus
  289.  
  290. When a termIO window is created, it is automatically given the input focus and "output focus". Output focus is a WinDosIO term which refers to the current window where any terminal IO or graphics will be displayed.
  291.  
  292. Only after the first termIO window is created  do supported functions become operational. 
  293.  
  294.  
  295. Selecting a Window
  296.  
  297. There can be up to 128 termIO windows per application. By default, the last window created has the input and output focus. If a window is created with the WDS_SELECT style, that window will gain the input and output focus when it is clicked on with the left button of the mouse. Any printing or prompting that is happening in one termIO window will switch over to the window that receives the input and output focus. A window can be given the input and output focus from software with WinDosIO(WD_SELECT,termIOHandle,0);  where termIOHandle is the window handle returned from the CreateWindow or WinDosIO(WD_CREATE_CHILD_WINDOW,...) call.
  298.  
  299.  
  300. Error Codes
  301.  
  302. All error codes are negative numbers. While the WinDosIO calls return their errors directly, the supported functions have well defined return codes. Many of them return EOF or NULL on error. To obtain the actual WDE error code,  errorCode = WinDosIO(WD_GETERRNO,0,0); These error codes are described in the reference section of this manual.
  303.  
  304. Microsoft and Borland graphics routines have their own error conventions and codes. The error is set internally and is returned in the appropriate form by the appropriate function, grstatus() for the Microsoft graphics functions and graphresult() for the Borland Graphics functions. The reference section lists errors for functions in the expected form, although equivalent errors can often be obtained through one of the other two functions.
  305.  
  306.  
  307. Notification Codes
  308.  
  309. The main window receives notification of four events through a WM_COMMAND message. If the termIO window is used in a dialog box, the dialog box procedure receives the notification. As in the notification messages from built in child controls, the ID of the window is found in the wParam. The handle of the child window is stored in the LOWORD of lParam and the notification code itself is stored in the HIWORD.
  310.  
  311.  Notification codes are described in the reference section and are any of the following: WDN_SETFOCUS, WDN_KILLFOCUS, WDN_LBUTTON, WDN_RBUTTON.
  312.  
  313.  
  314. EXAMPL1
  315.  
  316. Exampl1 on the distribution disk, "Counting to a Million" demonstrates a program which creates its own main window and window procedure, creates 11 termIO windows and uses the windows timer function to automatically switch the input and output focus. In addition to the timer, the user can click on one of the buttons and set the output focus to the associated window.
  317.  
  318. Contrast this program with exampl2, the simple "Hello, World" example. Exampl1 is a Windows program which makes use of WinDosIO while exampl2 is a DOS program, converted to windows with WinDosIO.
  319.  
  320.  
  321. /***************************************************************************
  322. * EXAMPL1.C
  323. * This example demonstrates programming in the structured paradigm under
  324. * Windows 3.0. Note the absence of the GetMessage, DispatchMessage loop.
  325. * The program counts to 1 million in any of five different windows. The
  326. * count moves from window to window every 2 seconds. If the user clicks
  327. * on the button associated with any of the windows, the count resumes
  328. * from that window. The user is requested to give an ID and password
  329. * at the start, to demonstrate some additional functionality.
  330. **************************************************************************/
  331.  
  332. #include <Windows.h>
  333. #include "WinDosIO.h"
  334.  
  335.  
  336. long far pascal WndProc(HWND, WORD, WORD, LONG);
  337.  
  338. short ticks;
  339. short currentHandle;
  340. HWND handle[5];
  341. HWND button[5];
  342. HWND idWindow;
  343. HANDLE hInst;
  344.  
  345.  
  346. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  347.            LPSTR lpszCmdline, int cmdshow)
  348.  {
  349.     HWND hwnd;
  350.     WNDCLASS wc;
  351.     short i;
  352.     RECT r;
  353.         struct WINDOSIO w;
  354.         long l;
  355.     MSG msg;
  356.     char buffer[240];
  357.     char far *p;
  358.  
  359.  
  360.     /* Register Main Window Class */
  361.  
  362.     hInst = hInstance;
  363.  
  364.     if (!hPrevInstance)
  365.      {
  366.         wc.style = 0;
  367.         wc.lpfnWndProc = WndProc;
  368.         wc.cbClsExtra = 0;
  369.         wc.cbWndExtra = 0;
  370.         wc.hInstance = hInstance;
  371.         wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
  372.         wc.hCursor = LoadCursor( 0, IDC_ARROW );
  373.         wc.hbrBackground = COLOR_WINDOW + 1;
  374.         wc.lpszMenuName = "Example1";
  375.         wc.lpszClassName = "Example1";
  376.         RegisterClass( &wc );
  377.      }
  378.  
  379.  
  380.     /* Initialize terminal IO */
  381.     WinDosIO(WD_INIT,hInstance,0);
  382.  
  383.     /* Create Main Window */
  384.     hwnd = CreateWindow(
  385.                 "Example1",
  386.                 "Counting to a Million",
  387.                 WS_OVERLAPPEDWINDOW,
  388.                 CW_USEDEFAULT,
  389.                 0,
  390.                 CW_USEDEFAULT,
  391.                 0,
  392.                 0,
  393.                 0,
  394.                 hInstance,
  395.                 NULL
  396.                 );
  397.  
  398.     ShowWindow(hwnd, cmdshow);
  399.  
  400.     /* Wrap width for small windows */
  401.     w.wrapWidth = 12;
  402.  
  403.     /* Create 5 buttons and 5 associated counting windows */
  404.     for (i = 0; i < 5; i++)
  405.      {
  406.       r.top = 130;
  407.       r.left = 10 + (i * 140);
  408.       handle[i] = CreateWindow("termIO",0,
  409.               WS_CHILD | WDS_NOPAGES | WDS_NOTEXTBUFFER,
  410.                r.left,r.top,70,60,
  411.               hwnd,i+50,hInstance,(LPSTR)&w);
  412.       textcolor(i + 10);
  413.       ShowWindow(handle[i],SW_SHOW);
  414.       UpdateWindow(handle[i]);
  415.  
  416.       r.top = 18;
  417.       r.left = 25 + (i * 140);
  418.  
  419.       button[i] =  CreateWindow("termIO",0,
  420.               WS_CHILD | WDS_SELECT | WDS_NOPAGES,
  421.                r.left,r.top,40,38,
  422.               hwnd,i,hInstance,(LPSTR)&w);
  423.       textcolor(i + 10);
  424.       ShowWindow(button[i],SW_SHOW);
  425.       UpdateWindow(button[i]);
  426.       printf("╔═══╗\n");
  427.       printf("║ %d ║\n",i+1);
  428.       printf("╚═══╝");
  429.     }
  430.  
  431.     /* Create ID and Password window */
  432.     r.top = 200;
  433.     r.left = 10;
  434.     idWindow = CreateWindow("termIO","ID and Password",
  435.               WS_CHILD | WS_THICKFRAME | WS_CAPTION |
  436.               WDS_SELECT | WDS_NOPAGES,
  437.               r.left,r.top,290,100,
  438.               hwnd,20,hInstance,0);
  439.     textcolor(WHITE);
  440.     ShowWindow(idWindow,SW_SHOW);
  441.     UpdateWindow(idWindow);
  442.  
  443.     /* Get info from user */
  444.     printf("Enter ID ? ");
  445.     gets(buffer);
  446.  
  447.     _setcursortype(_NOCURSOR);
  448.     p = getpass("Enter password:");
  449.     printf("The password is '%s'\n",p);
  450.  
  451.  
  452.     _setcursortype(_SOLIDCURSOR);
  453.     printf("Hit any key to continue\n");
  454.     getche();
  455.  
  456.     ShowWindow(idWindow,SW_HIDE);
  457.     UpdateWindow(idWindow);
  458.  
  459.     /* Loop through the windows while counting to a million,
  460.        using the timer to change windows.
  461.     */
  462.     currentHandle = 4;
  463.     WinDosIO(WD_SELECT,handle[currentHandle],0);
  464.     SetTimer(hwnd,1,500,0);
  465.     for (l = 0; l < 1000000L; l++)
  466.       if (printf("%ld\n",l) < 0) break;
  467.  
  468.     KillTimer(hwnd,1);
  469.  
  470.     while( GetMessage( &msg, 0, 0, 0 ) != 0 )
  471.          {
  472.         TranslateMessage( &msg );
  473.         DispatchMessage( &msg );
  474.          }
  475.  
  476.     return 0;
  477.  }
  478.  
  479.  
  480. long far pascal WndProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  481.  {
  482.     short i;
  483.     short nCode;
  484.  
  485.     switch( msg )
  486.         {
  487.     case WM_TIMER:
  488.         if (++ticks >= 36)
  489.          {
  490.             WinDosIO(WD_SELECT,handle[currentHandle],0);
  491.             currentHandle = ++currentHandle % 5;
  492.             ticks = 0;
  493.          }
  494.         break;
  495.     case WM_COMMAND:
  496.         nCode = HIWORD(lParam);
  497.         switch (nCode)
  498.          {
  499.             case WDN_LBUTTON:
  500.               for (i = 0; i < 5; i++)
  501.                {
  502.                 if (wParam == i)
  503.                  {
  504.                     currentHandle = i;
  505.                     WinDosIO(WD_SELECT,
  506.                         handle[currentHandle],0);
  507.                     ticks = 0;
  508.                     break;
  509.                  }
  510.                }
  511.          }
  512.         break;
  513.     case WM_SETFOCUS:
  514.         WinDosIO(WD_SETFOCUS,0,0);
  515.         break;
  516.     case WM_CLOSE:
  517.             WinDosIO(WD_DESTROY,hwnd,0);
  518.         return 0;
  519.         case WM_DESTROY:
  520.         PostQuitMessage(0);
  521.             return 0;
  522.         }
  523.  
  524.     return DefWindowProc(hwnd,msg,wParam,lParam);
  525. }
  526.  
  527.  
  528.  
  529.  
  530. Coroutines
  531.  
  532. Event driven code is user driven, while structured code is driven by its own structure. A case can be made for both, in different circumstances. WinDosIO gives you the ability to program both ways, even in the same program. Picture a recursive routine that pops up a different child window at various levels of recursion. This is so different than the paradigm where a message is received, processed and control returned back to windows. It is also different than the structured paradigm, because how do we handle the window processing without reinventing the wheel, when Windows already does it so well.
  533.  
  534. The answer lies in coroutines.  The recursive routine puts the window up and returns control to Windows with the ReturnToWindows function call. The child window is processed in its window procedure like normal. When the user clicks on the button which would cause the recursive routine to move up or down a level of recursion, code in the window procedure calls ReturnToApplication(short returnCode) and the ReturnToWindows call returns back to the recursive routine with the return code. Based on the return code, the recursive routine can decide what to do next.
  535.  
  536. Exampl3 demonstrates coroutines and recursion with a simple and useless application called "The Living Trie" . Click on a letter in the list box and add it to the word displayed. Click on up level and delete a letter.  Notice that a dummy termIO window is created because ReturnToWindows and ReturnToApplication will not function unless there is at least one termIO window. 
  537.  
  538. You can nest ReturnToWindows calls as deep as the stack allows. Each ReturnToApplication will pop back to the most recent ReturnToWindows. Calling ReturnToApplication, with no ReturnToWindows on the stack, will result in no error.
  539.  
  540.  
  541.  
  542. /***************************************************************************
  543. * EXAMPL3.C
  544. * This example demonstrates coroutines. They allow a sequential process
  545. * to use windows when prompting users.
  546. * In this case, the 'living trie' presents the user with the letters
  547. * of the alphabet. It then returns to Windows. When a letter is selected, it is
  548. * concatenated to the word being built up. Again the user is presented with
  549. * the letters of the alphabet and again control returns to Windows. When
  550. * the OK button is pressed, a letter is deleted from the end of the
  551. * word, and control returns back to the application.
  552. * Notice the absence of the run loop.
  553. *****************************************************************************/
  554.  
  555. #include <Windows.h>
  556. #include <WinDosIO.h>
  557.  
  558. #define UP_LEVEL     1
  559. #define SAME_LEVEL     2
  560. #define IDD_LISTBOX    100
  561. #define IDD_BUTTON    101
  562.  
  563. long far pascal WndProc(HWND, WORD, WORD, LONG);
  564. void LivingTrie(void);
  565.  
  566. HANDLE hInst;
  567. HWND hwnd, hwndStatic, hwndListBox, hwndButton, dummyHwnd;
  568.  
  569. char word[256];
  570. short level = 0;
  571.  
  572. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  573.            LPSTR lpszCmdline, int cmdshow)
  574.  {
  575.     WNDCLASS wc;
  576.     short i;
  577.     MSG msg;
  578.     char lBFill[2];
  579.  
  580.     lBFill[1] = 0;
  581.  
  582.     /* Register Main Window Class */
  583.  
  584.     hInst = hInstance;
  585.  
  586.     if (!hPrevInstance)
  587.      {
  588.         wc.style = 0;
  589.         wc.lpfnWndProc = WndProc;
  590.         wc.cbClsExtra = 0;
  591.         wc.cbWndExtra = 0;
  592.         wc.hInstance = hInstance;
  593.         wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
  594.         wc.hCursor = LoadCursor( 0, IDC_ARROW );
  595.         wc.hbrBackground = COLOR_WINDOW + 1;
  596.         wc.lpszMenuName = "Example3";
  597.         wc.lpszClassName = "Example3";
  598.         RegisterClass( &wc );
  599.      }
  600.  
  601.  
  602.     /* Initialize terminal IO */
  603.     WinDosIO(WD_INIT,hInstance,0);
  604.  
  605.     /* Create Main Window */
  606.     hwnd = CreateWindow(
  607.                 "Example3",
  608.                 "The Living Trie",
  609.                 WS_OVERLAPPEDWINDOW,
  610.                 CW_USEDEFAULT,
  611.                 0,
  612.                 CW_USEDEFAULT,
  613.                 0,
  614.                 0,
  615.                 0,
  616.                 hInstance,
  617.                 NULL
  618.                 );
  619.  
  620.     ShowWindow(hwnd, cmdshow);
  621.  
  622.    /* Create a dummy termIO window, which will not even be used. */
  623.    dummyHwnd = CreateWindow("termIO",0,
  624.           WS_CHILD | WDS_NOPAGES | WDS_NOTEXTBUFFER,
  625.           0,0,0,0,hwnd,50,hInstance,0);
  626.    ShowWindow(dummyHwnd,SW_HIDE);
  627.  
  628.    hwndStatic = CreateWindow("static","",WS_CHILD | WS_VISIBLE | WS_BORDER,
  629.                      100,10,300,20,hwnd,0,hInst,0);
  630.  
  631.    hwndListBox = CreateWindow("listbox",0,WS_CHILD|WS_VISIBLE|LBS_NOTIFY |
  632.                        WS_VSCROLL | WS_BORDER,
  633.                       100,60,60,200,hwnd,IDD_LISTBOX,
  634.                       hInst,0);
  635.  
  636.    /* Fill the list box with the letters of the alphabet */
  637.    for (i = 'A'; i <= 'Z'; i++)
  638.     {
  639.       lBFill[0] = i;
  640.       SendMessage(hwndListBox,LB_ADDSTRING,0,(LONG)lBFill);
  641.     }
  642.  
  643.    hwndButton = CreateWindow("button","UP LEVEL",
  644.                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  645.                   100,280,100,40,hwnd,IDD_BUTTON,hInst,0);
  646.  
  647.    LivingTrie();
  648.  
  649.    WinDosIO(WD_DESTROY,hwnd,0);
  650.  
  651.    while( GetMessage( &msg, 0, 0, 0 ) != 0 )
  652.          {
  653.         TranslateMessage( &msg );
  654.         DispatchMessage( &msg );
  655.          }
  656.  
  657.  
  658.    return 0;
  659.  }
  660.  
  661.  
  662.  
  663. void LivingTrie(void)
  664.  {
  665.     short rc;
  666.     for(;;) {
  667.     word[level] = 0;
  668.     SendMessage(hwndStatic,WM_SETTEXT,0,(LONG)word);
  669.     if ((rc = ReturnToWindows()) == UP_LEVEL || rc < 0) break;
  670.      }
  671.  }
  672. /*************************************************************
  673. * 1. If user clicks on letter. Set word[level++] to letter.
  674. *    call Living trie. Then decrement level and ReturnToApplication(SAME_LEVEL)
  675. * 2. If user clicks on OK, call return to application(CLICKED_OK).
  676. **************************************************************/
  677.  
  678.  
  679. long far pascal WndProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  680.  {
  681.  
  682.     WORD notify = HIWORD(lParam);
  683.  
  684.     switch (msg) {
  685.         case WM_COMMAND:
  686.         switch (wParam)
  687.          {
  688.             case IDD_BUTTON:
  689.                  ReturnToApplication(UP_LEVEL);
  690.                  break;
  691.             case IDD_LISTBOX:
  692.                  if (notify == LBN_DBLCLK)
  693.                    {
  694.                      word[level++] = 'A' +
  695.                    SendMessage(hwndListBox,LB_GETCURSEL,0,0);
  696.                      LivingTrie();
  697.                      level--;
  698.                      ReturnToApplication(SAME_LEVEL);
  699.                           }
  700.                  break;
  701.             default:
  702.                 break;
  703.             } /* End switch wParam */
  704.     break;
  705.     case WM_SETFOCUS:
  706.         WinDosIO(WD_SETFOCUS,0,0);
  707.         break;
  708.     case WM_CLOSE:
  709.             WinDosIO(WD_DESTROY,hwnd,0);
  710.         return 0;
  711.         case WM_DESTROY:
  712.         PostQuitMessage(0);
  713.             return 0;
  714.     } /* End switch message */
  715.     return DefWindowProc(hwnd,msg,wParam,lParam);
  716. }
  717.  
  718. Exampl4 and Exampl5
  719.  
  720. These programs demonstrate various text and cursor IO and positioning routines. Exampl4 uses WinDosIO.c, while exampl5 works from the WinMain level.
  721.  
  722.  
  723. Exampl6
  724.  
  725. This example explores many of the functions which emulate the Borland Graphics Interface. It is compiled with WinDosIO.c. Example6 experiments with deferring painting while putting pixels. It also uses a GDI call after obtaining the termIO device context. Unfortunately, due to problems with different graphics cards, functions changing the palette had to be removed from this version.
  726.  
  727. /***************************************************************************
  728. * EXAMPL6.C
  729. * Simple graphics demo
  730. * ****************************************************************************/
  731. #ifdef NOWINDOWS
  732. #include "borgraph.h"
  733. #include <stdio.h>
  734. #include <conio.h>
  735. #else
  736. #include <WinDosIO.h>
  737. #endif
  738. #include <stdlib.h>
  739. #include <memory.h>
  740.  
  741. char userFill[] = {0x18,0x7e,0x42,0xc3,0xc3,0x42,0x7e,0x18};
  742. short currentHandle;
  743. int gdriver = VGA,gmode=VGAMED;
  744. int midx, midy, x, y;
  745. int poly[8];
  746. int i,j;
  747. int size;
  748. char *bitmap;
  749. char msg[80];
  750. int hj, vj;
  751. char *hjust[] = {"LEFT","CENTER","RIGHT"};
  752. char *vjust[] = {"BOTTOM","CENTER","TOP"};
  753. struct arccoordstype ac;
  754.  
  755. main()
  756.   {
  757. #ifndef NOWINDOWS
  758.     WinDosIO(WD_SETTITLE,0,(long)"Graphics Demo");
  759. #endif
  760.     initgraph(&gdriver, &gmode, "");
  761.     if (x = graphresult())
  762.       {
  763.         printf("init graph error %d",x);
  764.         getch();
  765.         exit(1);
  766.          }
  767.     midx = getmaxx()/2;
  768.     midy = getmaxy()/2;
  769.  
  770.     for (i = 0; i < 5; i++)
  771.      {
  772.         for (j = 1; j < 10; j++)
  773.          {
  774.             settextstyle(i,0,j);
  775.             outtextxy(j * 40, i * 40,"A");
  776.          }
  777.      }
  778.     if (getch() < 0) return -1;
  779.     cleardevice();
  780.  
  781.     settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
  782.     moveto(0, midy);
  783.     outtext("Normal ");
  784.     printf("AAA");
  785.     settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
  786.     setusercharsize(1,3,1,1);
  787.     outtext("Short ");
  788.     setusercharsize(3,1,1,1);
  789.     outtext("Wide ");
  790.     if (getch() < 0) return -1;
  791.     cleardevice();
  792.     outtextxy(80,90,"This text should go over the view port");
  793.     setviewport(90,90,180,180,1);
  794.     setcolor(4);
  795.         outtextxy(0, 0, "This is a test, a test, yes a test.");
  796.     setcolor(1);
  797.     if (getch() < 0) return -1;
  798.     clearviewport();
  799.     if (getch() < 0) return -1;
  800.         outtextxy(0, 0, "This is a test, a test, yes a test.");
  801.     if (getch() < 0) return -1;
  802.     setgraphmode(gmode);
  803.     setactivepage(1);
  804.     setcolor(2);
  805.         outtextxy(midx, midy+40, "This is a test, a test, yes a test.");
  806.     setcolor(3);
  807.         outtextxy(midx, midy+60, "This is a test, a test, yes a test.");
  808.     setactivepage(0);
  809.     outtextxy(10,10,"Hit any key to see page #1");
  810.     if (getch() < 0) return -1;
  811.     setvisualpage(1);
  812.     if (getch() < 0) return -1;
  813.     setactivepage(1);
  814.     /* Place grid in upper corner */
  815.     for (x = 0; x < midx; x+= 10)
  816.      {
  817.         moveto(x,0);
  818.         lineto(x,midy);
  819.      }
  820.     setwritemode(XOR_PUT); /* Cause intersections to show */
  821.     setlinestyle(SOLID_LINE,0,THICK_WIDTH);
  822.     for (y = 0; y < midy; y+= 10)
  823.      {
  824.         moveto(0,y);
  825.         lineto(midx,y);
  826.      }
  827.     size = imagesize(10,10,40,50);
  828.     bitmap = malloc(size);
  829.     getimage(10,10,40,50,bitmap); 
  830.  
  831.     setwritemode(COPY_PUT);
  832.     circle(midx+60,60,10);
  833.     circle(midx+60,60,13);
  834.     circle(midx+60,60,16);
  835.     circle(midx+60,60,19);
  836.     circle(midx+60,60,22);
  837.     rectangle(midx+35,35,midx+85,85);
  838.     if (getch() < 0) return -1;
  839.     restorecrtmode();
  840.     gotoxy(2,2);
  841.     cprintf("Now in text mode");
  842.     if (getch() < 0) return -1;
  843.     setgraphmode(gmode);
  844.     outtextxy(50,50,"Back in graphics mode");
  845.     setviewport(130,130,230,230,1);
  846.     circle(10,10,10);
  847.     if (getch() < 0) return -1;
  848. #ifndef NOWINDOWS
  849.     WinDosIO(WD_DEFERPAINT,1,0);
  850. #endif
  851.     for (i = 0; i < 1000; i++)
  852.      {
  853.         putpixel(random(100), random(100), random(16));
  854.      }
  855. #ifndef NOWINDOWS
  856.     WinDosIO(WD_DEFERPAINT,0,0);
  857. #endif
  858.     if (getch() < 0) return -1;
  859.     setgraphmode(gmode);
  860.     setbkcolor(EGA_YELLOW);
  861.     bar3d(20,midy,40,midy+40,10,1);
  862.     setfillstyle(SOLID_FILL,1);
  863.     bar3d(60,midy,80,midy+40,20,0);
  864.     setfillstyle(LINE_FILL,1);
  865.     bar3d(100,midy,120,midy+40,30,1);
  866.     setfillstyle(LTSLASH_FILL,1);
  867.     bar(140,midy,160,midy+40);
  868.     setfillstyle(SLASH_FILL,1);
  869.     bar(180,midy,200,midy+40);
  870.     setfillstyle(BKSLASH_FILL,1);
  871.     bar(220,midy,240,midy+40);
  872.     setfillstyle(LTBKSLASH_FILL,1);
  873.     bar(260,midy,280,midy+40);
  874.     setfillstyle(HATCH_FILL,1);
  875.     bar(300,midy,320,midy+40);
  876.     setfillstyle(XHATCH_FILL,1);
  877.     bar(340,midy,360,midy+40);
  878.     setfillstyle(INTERLEAVE_FILL,1);
  879.     bar(380,midy,400,midy+40);
  880.     setfillstyle(WIDE_DOT_FILL,1);
  881.     bar(420,midy,440,midy+40);
  882.     setfillstyle(CLOSE_DOT_FILL,1);
  883.     bar(460,midy,480,midy+40);
  884.     setfillpattern(userFill,1);
  885.     bar(500,midy,520,midy+40);
  886.     if (getch() < 0) return -1;
  887.     setbkcolor(2);
  888.     arc(midx-5,midy-5,2,45,10);
  889.     arc(midx-5,midy-5,20,80,20);
  890.     arc(midx-5,midy-5,20,120,30);
  891.     getarccoords(&ac);
  892.     moveto(ac.xstart,ac.ystart);
  893.     lineto(200,200);
  894.     moveto(ac.xend,ac.yend);
  895.     lineto(ac.x, ac.y);
  896.     pieslice(midx+20,midy+50,0,30,50);
  897.     ellipse(midx+100,midy+100,45,135,60,30);
  898.     fillellipse(midx-100,midy-100,80,20);
  899.     sector(midx-100,midy+100,135,200,40,80);
  900.     if (getch() < 0) return -1;
  901.     cleardevice();
  902.     poly[0] = 20;
  903.     poly[1] = getmaxy() / 2;
  904.     poly[2] = getmaxx() - 20;
  905.     poly[3] = 20;
  906.     poly[4] = getmaxx() - 50;
  907.     poly[5] = getmaxy() - 20;
  908.     poly[6] = getmaxx() / 2;
  909.     poly[7] = getmaxy() / 2;
  910.     for (i = EMPTY_FILL; i < USER_FILL; i++)
  911.      {
  912.         setfillstyle(i, getmaxcolor());
  913.         fillpoly(4, poly);
  914.         putimage(200,100,bitmap,COPY_PUT);
  915.         if (getch() < 0) return -1;
  916.      }
  917.     circle(50,50,40);
  918.     setfillstyle(SOLID_FILL,4);
  919.     floodfill(50,50,getmaxcolor());
  920.     outtextxy(300,50,getdrivername());
  921.     outtextxy(350,60,getmodename(0));
  922.     outtextxy(350,80,getmodename(1));
  923.     outtextxy(350,100,getmodename(2));
  924.     if (getch() < 0) return -1;
  925.     cleardevice();
  926.     for (hj = LEFT_TEXT; hj <= RIGHT_TEXT; hj++)
  927.       for (vj = BOTTOM_TEXT; vj <= TOP_TEXT; vj++)
  928.        {
  929.         cleardevice();
  930.         settextjustify(hj, vj);
  931.         sprintf(msg,"%s-%s", hjust[hj], vjust[vj]);
  932.         line(midx-4, midy, midx+4, midy);
  933.         line(midx, midy - 4, midx, midy + 4);
  934.         outtextxy(midx, midy, msg);
  935.         if (getch() < 0) return -1;
  936.        }
  937.     closegraph();
  938.     return 0;
  939.  }
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.