home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / fonts / cw_src / altexe / lsupp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  9.7 KB  |  455 lines

  1. #define        no_Trace_
  2.  
  3. #define        NOVIRTUALKEYCODES
  4. #include    "c:\windev\include\windows.h"
  5. #include    "hello.h"
  6.  
  7. #define        KTABLESIZE (72*4)
  8. extern int    shiftstate;
  9. extern char    kTable[KTABLESIZE];
  10. extern char    buff[];
  11. extern char    vk_t[];
  12. extern char    vk_names[];
  13. extern HANDLE    hInst;
  14. extern int    bNoTrs;
  15.  
  16. int    GetScanCode (int);
  17.  
  18. /*
  19. static    char spc_keys []=
  20.     {
  21.     VK_0,        ')',
  22.     VK_1,        '!',
  23.     VK_2,        '@',
  24.     VK_3,        '#',
  25.     VK_4,        '$',
  26.     VK_5,        '%',
  27.     VK_6,        '^',
  28.     VK_7,        '&',
  29.     VK_8,        '*',
  30.     VK_9,        '(',
  31.     VK_OEM_1,    ':',
  32.     VK_OEM_2,    '?',
  33.     VK_OEM_3,    '~',
  34.     VK_OEM_4,    '{',
  35.     VK_OEM_5,    '|',
  36.     VK_OEM_6,    '}',
  37.     VK_OEM_7,    '"',
  38.     VK_OEM_PLUS,    '+',
  39.     VK_OEM_MINUS,    '_',
  40.     VK_OEM_COMMA,    '<',
  41.     VK_OEM_PERIOD,    '>',
  42.     0
  43.     };
  44. */
  45. //
  46. //    Returns ascii 'key name' - from alternative layout, or from
  47. //    current language layout
  48. //
  49. //    NOTE:    All key translation must be performed from keys ScanCodes,
  50. //        not from VK_ codes !!! (little BUG!)
  51. //
  52.  
  53. int    GetKeyName (int vkey)
  54.     {
  55.     unsigned i;
  56.     unsigned j, scan;
  57.     DWORD    oem;
  58.     int    retval;
  59.     char    tbu [132];
  60.  
  61.     for (i= 0; vk_t [i]; i++)
  62.         {
  63.         if ((char)vkey == vk_t [i])
  64.             break;
  65.         }
  66.  
  67.     if (!vk_t[i])
  68.         {
  69.         return '?';
  70.         }
  71.  
  72.     if (bNoTrs)
  73.         {
  74.         shiftstate= (shiftstate) ? 1 : 0;
  75.         if ((scan= GetScanCode ((int)vkey)) != 0xff)
  76.             {
  77.             for (j= 0x20; j< '~'; j++)
  78.                 {
  79.                 oem= OemKeyScan ((char) j);
  80.                 if (((HIWORD (oem)>> 1) & 1) == shiftstate)
  81.                     {
  82.                     if ((char)(LOWORD (oem)) == (char) scan)
  83.                         {
  84.                         return j;
  85.                         }
  86.                     }
  87.                 }
  88.                 j= MapVirtualKey (i= MapVirtualKey (scan, 1), 2);
  89.                 if (shiftstate)
  90.                     {
  91. //                    AnsiUpper ((LPSTR) &j);
  92.                     return j;
  93.                     }
  94.                 /*
  95.                 i= VK_ key code;
  96.                 */
  97.                 for (j= 0x20; j< 0xff; j++)
  98.                     {
  99.                     scan= VkKeyScan (j);
  100.                     if (LOWORD (scan) == i)
  101.                         {
  102.                         if (!HIWORD (scan))
  103.                             return j;
  104.                         }
  105.                     }
  106.                 return ' ';
  107.             }
  108.         else
  109.             {
  110.             return '?';
  111.             }
  112.         }
  113.     else
  114.         {
  115.         retval= (char) CyrTrs (vk_t [i], shiftstate);
  116.         }
  117.     return retval;
  118.     }
  119.  
  120. void    SetKeys (HWND hDlg)
  121.     {
  122.     int i;
  123.     int j;
  124.  
  125.     buff [1]= 0;
  126.     for (i= 0; vk_t [i] != 0; i++)
  127.         {
  128.          buff [0]= GetKeyName (vk_t [i]);
  129.         if (buff [0] == '&')
  130.             {
  131.             buff [1]= '&';
  132.             buff [2]= 0;
  133.             }
  134.         SetDlgItemText (hDlg, vk_t [i], (LPSTR) buff);
  135.         buff [1]= 0;
  136.         }
  137.     }
  138.  
  139. void
  140. SetFonts    (hDlg, hFont)
  141. HWND        hDlg;
  142. HFONT        hFont;
  143. {
  144.     int    i;
  145.  
  146.     for (i= 0; vk_t[i] != 0; i++) {
  147.         SendDlgItemMessage (hDlg, vk_t[i], WM_SETFONT, hFont, 1L);
  148.     }
  149.     SendDlgItemMessage (hDlg, IDL_VEDIT, WM_SETFONT, hFont, 1L);
  150. }
  151.  
  152. AddExt(name, ext)
  153. char name[], ext[];
  154. {
  155.     PSTR pTptr;
  156.  
  157.     pTptr= name;
  158.     while (*pTptr && *pTptr != '.') pTptr++;
  159.     if (*pTptr != '.')
  160.         strcat(name, ext);
  161. }
  162.  
  163. mystrchr(char *s, int vl)
  164. {
  165.     int i;
  166.  
  167.     for (i= 0; s[i]; i++)
  168.         if (s[i] == vl) return i;
  169.     return -1;
  170. }
  171.  
  172. extern HWND    hMainWnd;
  173. extern HWND    hMainDlg;
  174. extern char    szAppName[];
  175. extern char    l_name[];
  176.  
  177. SetText()
  178. {
  179.     char    str[132];
  180.  
  181.     lstrcpy(str, szAppName);
  182.     lstrcat(str, " - ");
  183.     lstrcat(str, l_name);
  184.     SetWindowText(hMainWnd, str);
  185. }
  186.  
  187. int
  188. filopen        (path, mode)
  189. PSTR        path;
  190. {
  191.     short    flabel;
  192.     int    i;
  193.     char    message[30];
  194.     char    msg [50];
  195.  
  196. retry:
  197.     flabel= _lopen(path, mode);
  198.     if (flabel < 0) {
  199.         LoadString(hInst, ERR_CREATE, message, sizeof message);
  200.         sprintf (msg, "cannot open file %s, create?", path);
  201.         if (MessageBox(hMainDlg, msg, szAppName, MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL) == IDYES) {
  202.             _lclose(_lcreat(path, 0));
  203.             flabel= _lopen(path, OF_WRITE);
  204. //            for (i= 0; i< KTABLESIZE; i++)
  205. //                if (i%4) kTable[i]= '?';
  206.             if (filwrite(flabel))
  207.                 return -1;
  208.             _lclose(flabel);
  209.             goto retry;
  210.         }
  211.     }
  212.     return flabel;
  213. }
  214.  
  215. int
  216. filwrite    (flabel)
  217. short        flabel;
  218. {
  219.     char    message[30];
  220.  
  221.     if (_lwrite(flabel, kTable, KTABLESIZE) != KTABLESIZE) {
  222.         LoadString(hInst, ERR_WRITE, message, sizeof message);
  223.         MessageBox(hMainDlg, message, szAppName, MB_OK);
  224.         _lclose(flabel);
  225.         return -1;
  226.     }
  227.     return 0;
  228. }
  229.  
  230. int
  231. filread        (flabel)
  232. short        flabel;
  233. {
  234.     char    message[30];
  235.  
  236.     if (_lread(flabel, kTable, KTABLESIZE) != KTABLESIZE) {
  237.         LoadString(hInst, ERR_READ, message, sizeof message);
  238.         MessageBox(hMainDlg, message, szAppName, MB_OK);
  239.         _lclose(flabel);
  240.         return -1;
  241.     }
  242.     return 0;
  243. }
  244.  
  245. void
  246. demomsg        ()
  247. {
  248.     char    dmsg[256];
  249.     LoadString(hInst, ERR_DEMO, dmsg, 256);
  250.     MessageBox(NULL, dmsg, szAppName, MB_OK);
  251. }
  252.  
  253.  
  254. //
  255. //    fonts
  256. //
  257.  
  258. #define    MAXSIZE        20
  259. #define    MAXFONT        20
  260.  
  261. int    FontIndex= 0;
  262. int    SizeIndex= 0;
  263. int    CurrentFont= 0;
  264. int    CurrentSize= 0;
  265.  
  266. char    FontList [MAXFONT][32];
  267. char    SizeList [MAXSIZE];
  268. BYTE    CharSet[MAXFONT];
  269. BYTE    PitchAndFamily[MAXFONT];
  270.  
  271. FARPROC    lpEnumFunc;
  272.  
  273. int    FAR PASCAL EnumFunc (LPLOGFONT, LPTEXTMETRIC, short, LPSTR);
  274. BOOL    FAR PASCAL SelectFont (HWND, unsigned, WORD, LONG);
  275. int    _lstrlen (LPSTR);
  276. void    _lstrncpy (LPSTR, LPSTR, int);
  277. void    _lstrcpy (LPSTR, LPSTR);
  278.  
  279. void GetSizes(hWnd, _CurrentFont)
  280. HWND hWnd;
  281. int _CurrentFont;
  282. {
  283.     HDC hDC;
  284.  
  285.     SizeIndex = 0;
  286.     hDC = GetDC(hWnd);
  287.     lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
  288.     EnumFonts(hDC, FontList[_CurrentFont], lpEnumFunc, (LPSTR) 1L);
  289.     FreeProcInstance(lpEnumFunc);
  290.     ReleaseDC(hWnd, hDC);
  291. }
  292.  
  293. void GetFonts(hWnd)
  294. HWND hWnd;
  295. {
  296.  
  297.     HDC hDC;
  298.  
  299.     FontIndex = 0;
  300.     SizeIndex = 0;
  301.     hDC = GetDC(hWnd);
  302.     lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
  303.     EnumFonts(hDC, (LPSTR) NULL, lpEnumFunc, (LPSTR) NULL);
  304.     FreeProcInstance(lpEnumFunc);
  305.     ReleaseDC(hWnd, hDC);
  306. }
  307.  
  308. BOOL FAR PASCAL SelectFont(hDlg, message, wParam, lParam)
  309. HWND hDlg;
  310. unsigned message;
  311. WORD wParam;
  312. LONG lParam;
  313. {
  314.  
  315.     int i;
  316.     int index;
  317.     char buf[LF_FACESIZE];
  318.  
  319.     switch (message) {
  320.         case WM_INITDIALOG:
  321.         GetFonts (hDlg);
  322.             for (i=0; i<FontIndex; i++) {        /* displays available fonts */
  323.                 SendDlgItemMessage(hDlg, ID_SF_LIST, LB_ADDSTRING,
  324.                     NULL, (LONG) (LPSTR) FontList[i]);
  325.                 SendDlgItemMessage(hDlg, ID_SF_LIST, LB_SETCURSEL,
  326.                     0, 0L);
  327.             }
  328.             SendDlgItemMessage(hDlg, ID_SF_LIST, LB_SETCURSEL,
  329.                 CurrentFont, 0L);
  330.             GetSizes(hDlg, 0);
  331.             for (i=0; i<SizeIndex; i++) {        /* displays font sizes      */
  332.                 sprintf(buf, "%d", SizeList[i]);
  333.                 SendDlgItemMessage(hDlg, ID_SF_SIZE, LB_ADDSTRING,
  334.                     0, (LONG) (LPSTR) buf);
  335.                 SendDlgItemMessage(hDlg, ID_SF_SIZE, LB_SETCURSEL,
  336.                     0, 0L);
  337.             }
  338.             return (TRUE);
  339.             break;
  340.  
  341.         case WM_COMMAND:
  342.             switch (wParam) {
  343.                 case ID_SF_OK:
  344. okay:
  345.                     index=SendDlgItemMessage(hDlg, ID_SF_LIST,
  346.                         LB_GETCURSEL, 0, 0L);
  347.                     if (index == LB_ERR) {
  348.                         MessageBox(hDlg, "No font selected",
  349.                             "Select Font", MB_OK | MB_ICONEXCLAMATION);
  350.                     break;
  351.             }
  352.             CurrentFont = index;
  353.             index = SendDlgItemMessage(hDlg, ID_SF_SIZE,
  354.                 LB_GETCURSEL, 0, 0L);
  355.             if (index == LB_ERR) {
  356.                 MessageBox(hDlg, "No size selected",
  357.                     "Select Font", MB_OK | MB_ICONEXCLAMATION);
  358.                 break;
  359.             }
  360.             CurrentSize = index;
  361.             EndDialog(hDlg, 1);
  362.             break;
  363.  
  364.         case ID_SF_CANCEL:
  365.             EndDialog(hDlg, 0);
  366.             break;
  367.  
  368.         case ID_SF_LIST:
  369.             switch (HIWORD(lParam)) {
  370.                 case LBN_SELCHANGE:
  371.                     index = SendDlgItemMessage(hDlg, ID_SF_LIST,
  372.                         LB_GETCURSEL, 0, 0L);
  373.                     if (index == LB_ERR)
  374.                         break;
  375.                     SendDlgItemMessage(hDlg, ID_SF_SIZE, LB_RESETCONTENT, 0, 0L);
  376.                     GetSizes(hDlg, index);
  377.                     for (i = 0; i < SizeIndex; i++) {
  378.                         sprintf(buf, "%d", SizeList[i]);
  379.                         SendDlgItemMessage(hDlg, ID_SF_SIZE,
  380.                             LB_ADDSTRING, 0, (LONG) (LPSTR) buf);
  381.                         SendDlgItemMessage(hDlg, ID_SF_SIZE, LB_SETCURSEL, 0, 0L);
  382.             }
  383.             break;
  384.  
  385.                 case LBN_DBLCLK:
  386.                 goto okay;
  387.                 break;
  388.             }
  389.             break;
  390.  
  391.         case ID_SF_SIZE:
  392.             if(HIWORD(lParam) == LBN_DBLCLK)
  393.                 goto okay;
  394.             break;
  395.         }
  396.         break;
  397.     }
  398.     return (FALSE);
  399. }
  400.  
  401. int FAR PASCAL EnumFunc(lpLogFont, lpTextMetric, FontType, lpData)
  402. LPLOGFONT lpLogFont;
  403. LPTEXTMETRIC lpTextMetric;
  404. short FontType;
  405. LPSTR lpData;
  406. {
  407.     switch (LOWORD(lpData)) {
  408.         case 0:
  409.             if (FontIndex >= MAXFONT)
  410.                 return (0);
  411.             _lstrcpy((LPSTR) FontList[FontIndex],
  412.                 (LPSTR) (lpLogFont->lfFaceName));
  413.             CharSet[FontIndex] = lpLogFont->lfCharSet;
  414.             PitchAndFamily[FontIndex] = lpLogFont->lfPitchAndFamily;
  415.             return (++FontIndex);
  416.  
  417.         case 1:
  418.             if (SizeIndex >= MAXSIZE)
  419.                 return (0);
  420.             SizeList[SizeIndex] = lpLogFont->lfHeight;
  421.             return (++SizeIndex);
  422.     }
  423. }
  424.  
  425. int _lstrlen(lpStr)
  426. LPSTR lpStr;
  427. {
  428.     int i;
  429.     for (i=0; *lpStr++; i++);
  430.     return (i);
  431. }
  432.  
  433. void _lstrncpy(lpDest, lpSrc, n)
  434. LPSTR lpDest, lpSrc;
  435. int n;
  436. {
  437.     while (n--)
  438.         if(!(*lpDest++ = *lpSrc++))
  439.             return;
  440. }
  441.  
  442. void _lstrcpy(lpDest, lpSrc)
  443. LPSTR lpDest, lpSrc;
  444. {
  445.     while(*lpDest++ = *lpSrc++);
  446. }
  447.  
  448. int    ask_save (char *s)
  449.     {
  450.     char    smsg [100];
  451.  
  452.         sprintf (smsg, "Keyboard layout %s has changed.\nDo you want to save it?", s);
  453.         return (MessageBox (GetFocus (), smsg, szAppName, MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL) == IDYES);
  454.     }
  455.