home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / blt2rx_o.zip / c_src.zip / bd_mainw.c < prev    next >
C/C++ Source or Header  |  1996-10-13  |  7KB  |  299 lines

  1.  
  2. /* This WinMain() is for Windows apps (Win32s specifically).
  3.  * For console apps see bd_main.c, instead.
  4.  *
  5.  * bd_winmn.c - 12-Oct-1996 Cornel Huth
  6.  *
  7.  * This module calls the bd_main.c module if FOR_WINDOWS==1
  8.  * (and only if ON_WIN32==1) otherwise this is a null module.
  9.  *
  10.  */
  11.  
  12.  
  13. #include "platform.h"         // defines platform, brings in includes
  14.  
  15. #if FOR_WINDOWS == 1          // for ON_WIN32 only
  16.  
  17. // external to this module
  18.  
  19. int main2();             // the regular _main()
  20. void PutMsg(CHAR *strg); // writes text
  21. void GetMsg(CHAR *strg); // gets text
  22.  
  23.  
  24. // public to this module
  25.  
  26. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM uParam,LPARAM lParam);
  27.  
  28. // global to this exe
  29.  
  30. HWND gHwnd=0;
  31. int gCurrStr=0;         // current element in gPutStr[n][0]  (n=1 to 39 strings, init to 0)
  32. CHAR gPutStr[40][82];   // output from bd_*.c modules should be less than this size (PutMsg())
  33.  
  34. int gInputReady=0;      // flag when CR hit
  35. int gCurrChar=0;        // current character pointer, in gGetStr[] (n=0 to 79)
  36. CHAR gGetStr[82];       // input from bd_*.c (GetMsg())
  37.  
  38. BOOL gDie=FALSE;
  39. MSG gMsg;
  40. void DoWinThing(int waitFlag);
  41. void DoWinThing2(void);
  42.  
  43.  
  44. // WinMain program start ////////////////////////////////////////////////////////
  45.  
  46. int APIENTRY WinMain(HINSTANCE hInstance,
  47.                      HINSTANCE hPrevInstance,
  48.                      LPSTR lpCmdLine,
  49.                      int nCmdShow) {
  50.  
  51.  static CHAR appName[] = "bd9wdemo";
  52.  HWND hwnd;
  53.  WNDCLASS wc;
  54.  
  55.  
  56.  if (!hPrevInstance) {
  57.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  58.     wc.lpfnWndProc   = (WNDPROC) WndProc;
  59.     wc.cbClsExtra    = 0;
  60.     wc.cbWndExtra    = 0;
  61.     wc.hInstance     = hInstance;
  62.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  63.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  64.     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  65.     wc.lpszMenuName  = appName;
  66.     wc.lpszClassName = appName;
  67.  
  68.     RegisterClass(&wc);
  69.  }
  70.  else {
  71.     if ((GetVersion() & 0x80000000)==0) return 0; // only play once if win32s in win31
  72.  }
  73.  
  74.  
  75.  hwnd = CreateWindow(appName,"Bullet 2 Demo Starter for Windows", WS_OVERLAPPEDWINDOW,
  76.                      CW_USEDEFAULT,CW_USEDEFAULT,
  77.                      CW_USEDEFAULT,CW_USEDEFAULT,
  78.                      NULL,NULL,hInstance,NULL);
  79.  
  80.  if (!hwnd) return (FALSE);
  81.  gHwnd = hwnd;
  82.  ShowWindow(hwnd, nCmdShow);
  83.  UpdateWindow(hwnd);
  84.  
  85.  // do the hustle
  86.  
  87.  main2();
  88.  
  89.  if (gDie==FALSE) DoWinThing(1);   // wait for death
  90.  return gMsg.wParam;
  91.  
  92.  lpCmdLine;
  93. }
  94.  
  95.  
  96. // Handle message queue here
  97.  
  98. void DoWinThing(int waitFlag) {
  99.  
  100.  if (waitFlag == 0) {
  101.     while (PeekMessage(&gMsg,NULL,0,0,PM_NOREMOVE)) {
  102.        DoWinThing2();
  103.     }
  104.  }
  105.  else {
  106.     DoWinThing2();
  107.  }
  108.  return;
  109. }
  110.  
  111.  
  112. // Handle
  113. void DoWinThing2(void) {
  114.  
  115.  if (GetMessage(&gMsg, NULL, 0, 0)) {
  116.     TranslateMessage(&gMsg);
  117.     DispatchMessage(&gMsg);
  118.  }
  119.  else {
  120.     gDie = TRUE;
  121.  }
  122.  return;
  123. }
  124.  
  125.  
  126. // Windows handler
  127.  
  128. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM uParam,LPARAM lParam) {
  129.  
  130.  TEXTMETRIC tm;
  131.  PAINTSTRUCT ps;
  132.  HDC hdc;
  133.  
  134.  int i,j,k,t;
  135.  
  136.  static int cxChar,cyChar;
  137.  static int outRow,outCol;
  138.  
  139.  CHAR c;
  140.  CHAR lnBuffer[128];
  141.  CHAR *lnPtr;
  142.  int lnLen;
  143.  int outColFlag;
  144.  
  145.  switch (message) {
  146.  case WM_CREATE:
  147.     hdc = GetDC(hwnd);
  148.     SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
  149.     GetTextMetrics(hdc,&tm);
  150.     cxChar = tm.tmAveCharWidth;
  151.     cyChar = tm.tmHeight;
  152.     ReleaseDC(hwnd,hdc);
  153.     outRow = 0;
  154.     outCol = 0;
  155.     break;
  156.  
  157.  case WM_SIZE:
  158.     break;
  159.  
  160.  case WM_SETFOCUS:
  161.     CreateCaret(hwnd,(HBITMAP)1,cxChar,cyChar);
  162.     ShowCaret(hwnd);
  163.     break;
  164.  
  165.  case WM_KILLFOCUS:
  166.     HideCaret(hwnd);
  167.     DestroyCaret();
  168.     break;
  169.  
  170.  case WM_PAINT:
  171.     hdc = BeginPaint(hwnd,&ps);
  172.     SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
  173.  
  174.     outRow=0;
  175.     outCol=0;
  176.     outColFlag = 0;
  177.     lnPtr = lnBuffer;
  178.     lnLen = 0;
  179.  
  180.     for (i=1; i <= gCurrStr; i++) {
  181.        k = strlen(&gPutStr[i][0]);
  182.        for (j=0; j < k; j++) {
  183.           c = gPutStr[i][j];
  184.           switch (c) {
  185.           case '\n':
  186.              if (lnLen) TextOut(hdc,(outCol*cxChar),(outRow*cyChar),lnBuffer,lnLen);
  187.              lnPtr = lnBuffer;
  188.              lnLen = 0;
  189.              outCol = 0;
  190.              outRow++;
  191.              break;
  192.           case '\r':
  193.              outColFlag = 1;
  194.              break;
  195.           case '\x0C':  // form-feed (rest of gPutStr[] line ignored)
  196.              // cls
  197.              gCurrStr=0;
  198.              InvalidateRect(gHwnd,NULL,TRUE);
  199.              break;
  200.  
  201.           default:
  202.              if (c >= 32) {
  203.                 *lnPtr++ = c;
  204.                 lnLen++;
  205.              }
  206.           }
  207.        }
  208.        if (lnLen) {
  209.           TextOut(hdc,(outCol*cxChar),(outRow*cyChar),lnBuffer,lnLen);
  210.           outCol = outCol + lnLen;
  211.           lnPtr = lnBuffer;
  212.           lnLen = 0;
  213.        }
  214.        if (outColFlag) {
  215.           outCol = 0;
  216.           outColFlag = 0;
  217.        }
  218.     }
  219.  
  220.     SetCaretPos(outCol*cxChar,outRow*cyChar);
  221.     EndPaint(hwnd,&ps);
  222.     break;
  223.  
  224.  case WM_CHAR:
  225.     HideCaret(hwnd);
  226.     for (i=0; i < (int)LOWORD(lParam); i++) {
  227.        switch(LOWORD(uParam)) {
  228.        case '\r':
  229.           outRow++;
  230.           outCol=0;
  231.           gGetStr[gCurrChar] = '\x0';
  232.           gInputReady = 1;             // used in GetMsg() in bd_main.c
  233.           break;
  234.        case '\n':
  235.           break;
  236.        case '\b':
  237.           if (gCurrChar > 0) {
  238.              gCurrChar--;
  239.              gGetStr[gCurrChar] = '\x0';
  240.              t = strlen(&gPutStr[gCurrStr][0]);
  241.              if (t > 0) gPutStr[gCurrStr][t-1] = 0;
  242.              if (outCol > 0) outCol--;  // play it safe
  243.              hdc = GetDC(hwnd);
  244.              SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
  245.              TextOut(hdc,(outCol*cxChar),(outRow*cyChar)," ",1);
  246.              ReleaseDC(hwnd, hdc);
  247.           }
  248.           break;
  249.        default:
  250.           if (gCurrChar < sizeof(gGetStr)-1) {
  251.              c = (CHAR)LOWORD(uParam);
  252.              if ((c >= 32) & (c < 127)) {
  253.                 gGetStr[gCurrChar] = c;
  254.                 t = strlen(&gPutStr[gCurrStr][0]);
  255.                 gPutStr[gCurrStr][t] = c;
  256.                 gPutStr[gCurrStr][t+1] = '\x0';
  257.                 hdc = GetDC(hwnd);
  258.                 SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
  259.                 TextOut(hdc,(outCol*cxChar),(outRow*cyChar),&c,1);
  260.                 ReleaseDC(hwnd, hdc);
  261.                 gCurrChar++;
  262.                 outCol++;
  263.              }
  264.           }
  265.           else {
  266.              gGetStr[gCurrChar] = '\x0';
  267.              gInputReady = 1;
  268.           }
  269.           break;
  270.        }
  271.     }
  272.     SetCaretPos(outCol*cxChar,outRow*cyChar);
  273.     ShowCaret(hwnd);
  274.     break;
  275.  
  276.  case WM_DESTROY:
  277.     PostQuitMessage(0);
  278.     break;
  279.  
  280.  default:
  281.     return (DefWindowProc(hwnd, message, uParam, lParam));
  282.  }
  283.  return 0;
  284. }
  285.  
  286. #endif  // FOR_WINDOWS==1
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.