home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / msj / v06n06 / intnat.exe / KEY.EXE / KEYMAP.C next >
Encoding:
C/C++ Source or Header  |  1991-11-01  |  5.0 KB  |  194 lines

  1. /* 
  2.  * Resident segment for KEYMAP
  3.  * Copyright (c) 1991 by William S. Hall and Peter Belew
  4.  */
  5.  
  6. #define NOCOMM
  7. #define NOKANJI
  8. #define NOATOM
  9. #define NOSOUND
  10. #include <windows.h>
  11. #include <string.h>
  12. #include "winutils.h"
  13. /* all global variables are declared in this module */
  14. #define EXTERN
  15. #include "keymap.h"
  16.  
  17. /* local functions */
  18. static void NEAR ResetKeys(HWND hWnd);
  19.  
  20. /* Entry point for program */
  21. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  22.            LPSTR lpszCmdLine, int cmdShow)
  23. {
  24.  
  25.     MSG msg;
  26.  
  27.   /* If initialization is not successful then exit */
  28.     if (!InitProgram(hInstance,hPrevInstance,lpszCmdLine,cmdShow))
  29.     return FALSE;
  30.  
  31.   /* Retrieve messages from Windows */
  32.     while (GetMessage(&msg, NULL, 0, 0)) {
  33.     TranslateMessage(&msg);
  34.     DispatchMessage(&msg);
  35.     }
  36.     return msg.wParam;        /* exit program */
  37. }
  38.  
  39. /* All messages are processed here */
  40. long FAR PASCAL MainWndProc(register HWND hWnd, unsigned message,
  41.                 register WORD wParam,LONG lParam)
  42. {
  43.  
  44.     switch(message) {
  45.  
  46.     case WM_WININICHANGE:
  47.         if ((lstrcmpi((LPSTR)lParam, "intl") == 0))
  48.         ResetKeys(hWnd);
  49.         break;
  50.  
  51.     case WM_COMMAND:
  52.         switch(wParam) {
  53.         case IDM_ABOUT:        /* show ego box */
  54.             OpenDlgBox(hWnd, AboutDlg, DT_ABOUT);
  55.             break;
  56.         case IDM_CLEAR:        /* clear practice window */
  57.             SetWindowText(hEdit, "");
  58.             break;
  59.         case IDM_GRAY:        /* switch key color */
  60.         case IDM_WHITE:
  61.             if (KeyColor != wParam) {
  62.                 HMENU hMenu = GetMenu(hWnd);
  63.                 CheckMenuItem(hMenu,KeyColor,MF_UNCHECKED);
  64.             KeyColor = wParam;
  65.                 CheckMenuItem(hMenu,wParam,MF_CHECKED);
  66.                 ResetKeys(hWnd);
  67.             }
  68.             break;
  69.         }
  70.         break;
  71.  
  72.     case WM_SETFOCUS:    /* keep the focus on the edit box */
  73.         if (IsWindow(hEdit))
  74.             SetFocus(hEdit);
  75.         break;
  76.  
  77.     case WM_CREATE:        /* create the key windows */
  78.         MainWndCreate(hWnd);
  79.         break;
  80.         
  81.     case WM_DESTROY:    /* leaving */
  82.         PostQuitMessage(0);
  83.         break;
  84.  
  85.     case WM_NCDESTROY:    /* final cleanup, fall thru */
  86.         if (fpEditWndProc)
  87.             FreeProcInstance(fpEditWndProc);
  88.         if (fpLocalEditWndProc)
  89.             FreeProcInstance(fpLocalEditWndProc);
  90.     
  91.     default:
  92.         return (long)DefWindowProc(hWnd,message,wParam,lParam);
  93.         break;
  94.     }
  95.     return(0L);
  96. }
  97.  
  98. /* reset the keys */
  99. static void NEAR ResetKeys(HWND hWnd)
  100. {
  101.     register int i;
  102.     register hctl;
  103.     char buf[80];
  104.     HCURSOR hCursor;
  105.  
  106.     SetCapture(hWnd);    /* we now own the mouse cursor */
  107.     hCursor = SetCursor(LoadCursor(NULL,IDC_WAIT));    /* hourglass */
  108.     for (i = 0; i < MAXKEYS; i++) {      /* redraw the keys */
  109.     hctl = GetDlgItem(hWnd, kdata[i].scancode);
  110.     ChildCreate(hctl);
  111.     InvalidateRect(hctl, NULL, TRUE);
  112.     }
  113.     LoadString(hInst,IDS_TITLE,buf,sizeof(buf));  /* redraw title */
  114.     SetWindowText(hWnd, buf);
  115.     ModifyWindowsTitle(hWnd);    /* show the current keyboard */
  116.     SetCursor(hCursor);        /* reset mouse cursor */
  117.     ReleaseCapture();        /* release control */
  118.  
  119. }
  120.  
  121. /* Read the keyboard's name from Setup.inf.  Lot's of embedded
  122.    strings here, but they are universal in Windows and are
  123.    not language dependent.
  124. */
  125. #define MAXPATHLEN    144
  126. #define MAXFILENAMELEN    12
  127. void FAR ModifyWindowsTitle(HWND hWnd)
  128. {
  129.     char kbdll[20];
  130.     char keystr[512];
  131.     char winpath[MAXPATHLEN + MAXFILENAMELEN + 2];
  132.     char *keyptr;
  133.     register int len;
  134.     char kbstring[80];
  135.     char title[256];
  136.     char *result;
  137.  
  138.     /* find out where SETUP.INF is located and add in its name */
  139.     len = GetSystemDirectory(winpath, sizeof(winpath));
  140.     if ((len == 0) || (len > MAXPATHLEN))
  141.     return;        /* error */
  142.     if (winpath[len - 1] != '\\') /* are we in a subdirectory? */
  143.     lstrcat(winpath, "\\");
  144.     lstrcat(winpath, "SETUP.INF");
  145.  
  146.     /* now read in the keyboard dll from system.ini */
  147.     len = GetPrivateProfileString("keyboard", "keyboard.dll",
  148.                   "", kbdll, sizeof(kbdll), 
  149.                   "system.ini");
  150.     if (len == 0)
  151.     strcpy(keystr, "nodll");    /* none in use */
  152.     else    /* found one, so enumerate all possibilities */
  153.     GetPrivateProfileString("keyboard.tables", NULL, "",
  154.                      keystr, sizeof(keystr), winpath);
  155.     keyptr = keystr;
  156.     while (len = strlen(keyptr)) {  /* loop thru strings until target found */
  157.     GetPrivateProfileString("keyboard.tables", keyptr, "",
  158.                      kbstring,sizeof(kbstring),winpath);
  159.     if (result = strstr(kbstring, kbdll)) {
  160.         strtok(kbstring, "\"");    /* found one, parse and add to tile */
  161.         GetWindowText(hWnd, title, sizeof(title));
  162.         strcat(title, strtok(NULL, "\""));
  163.         SetWindowText(hWnd, title);
  164.         break;
  165.     }
  166.     keyptr += len + 1;
  167.     }
  168. }
  169.  
  170. /* show ego box */
  171. BOOL FAR PASCAL AboutDlg(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  172. {
  173.     switch(message) {
  174.  
  175.     case WM_INITDIALOG:
  176.         break;
  177.  
  178.     case WM_COMMAND:
  179.         switch(wParam) {
  180.         case IDOK:
  181.         case IDCANCEL:
  182.             EndDialog(hDlg, TRUE);
  183.             break;
  184.         default:
  185.             return FALSE;
  186.         }
  187.         break;
  188.  
  189.     default:
  190.         return FALSE;
  191.     }
  192.     return TRUE;
  193. }
  194.