home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / color.c < prev    next >
Text File  |  1994-04-20  |  11KB  |  326 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //  Copyright (c) 1988, 1989, 1990  John Stanback
  4. //
  5. //  Project:    OS/2 PM Port of GNU CHESS 4.0 (PmChess)
  6. //
  7. //  Version:    1994-4-17
  8. //
  9. //   Module:    Color.c
  10. //
  11. //   Porter:    Ported to Windows 3.0 by Darly Baker
  12. //
  13. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  14. //
  15. //   Porter:    Revised and ported to OS/2 2.1 by Yibing Fan
  16. //
  17. //   System:    OS2 2.1 using emx0.8g 
  18. //
  19. //  Remarks:    This code does not work well when I got it from KC's post,
  20. //               modified from KC's code (YF)
  21. //
  22. //  Remarks:    This code converted from Windows to PM using a straight port
  23. //              method with some editing improvements.  Based on ideas and
  24. //              code segments of Charles Petzold from artices in Microsoft
  25. //              Systems Journal.
  26. //
  27. //  License:
  28. //
  29. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  30. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  31. //    the consequences of using it or for whether it serves any particular
  32. //    purpose or works at all, unless he says so in writing.  Refer to the
  33. //    CHESS General Public License for full details.
  34. //
  35. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  36. //    but only under the conditions described in the CHESS General Public
  37. //    License.  A copy of this license is supposed to have been given to you
  38. //    along with CHESS so you can know your rights and responsibilities.  It
  39. //    should be in a file named COPYING.  Among other things, the copyright
  40. //    notice and this notice must be preserved on all copies.
  41. //
  42.  
  43. #define INCL_DOS
  44. #define INCL_GPI
  45. #define INCL_PM
  46. #include <os2.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include "PmChess.h"
  50. #include "Resource.h"
  51.  
  52.  
  53. extern ULONG clrBackGround;
  54. extern ULONG clrBlackSquare;
  55. extern ULONG clrWhiteSquare;
  56. extern ULONG clrBlackPiece;
  57. extern ULONG clrWhitePiece;
  58. extern ULONG clrText;
  59. extern SHORT clrcase;
  60.  
  61. extern HWND hComputerColor;
  62. extern HWND hComputerMove;
  63. extern HWND hWhosTurn;
  64. extern HWND hClockComputer;
  65. extern HWND hClockHuman;
  66. extern HWND hMsgComputer;
  67. extern HWND hMsgHuman;
  68.  
  69. //
  70. //  Define dialog procedure's prototypes.
  71. //
  72. MRESULT EXPENTRY ColorProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  73.  
  74.  
  75.  
  76. static char lpChessini[]    = "chess.ini";
  77. static char lpBackGround[]  = "BackGround";
  78. static char lpBlackSquare[] = "BlackSquare";
  79. static char lpWhiteSquare[] = "WhiteSquare";
  80. static char lpBlackPiece[]  = "BlackPiece";
  81. static char lpWhitePiece[]  = "WhitePiece";
  82. static char lpDefault[]     = "Default";
  83. static char lpText[]        = "Text";
  84.  
  85. static char np08lX[] = "%08lX";
  86.  
  87.  
  88. void UpdateChilds()
  89.   {
  90.   WinSetPresParam(hComputerColor, PP_BACKGROUNDCOLORINDEX,
  91.                   sizeof(LONG), &clrBackGround);
  92.   WinSetPresParam(hWhosTurn,      PP_BACKGROUNDCOLORINDEX,
  93.                   sizeof(LONG), &clrBackGround);
  94.   WinSetPresParam(hComputerMove,  PP_BACKGROUNDCOLORINDEX,
  95.                   sizeof(LONG), &clrBackGround);
  96.   WinSetPresParam(hClockComputer,  PP_BACKGROUNDCOLORINDEX,
  97.                   sizeof(LONG), &clrBackGround);
  98.   WinSetPresParam(hClockHuman,  PP_BACKGROUNDCOLORINDEX,
  99.                   sizeof(LONG), &clrBackGround);
  100.   WinSetPresParam(hMsgComputer,  PP_BACKGROUNDCOLORINDEX,
  101.                   sizeof(LONG), &clrBackGround);
  102.   WinSetPresParam(hMsgHuman, PP_BACKGROUNDCOLORINDEX,
  103.                   sizeof(LONG), &clrBackGround);
  104.   }
  105.  
  106.  
  107. void SetStandardColors ( VOID )
  108.   {
  109.   clrBackGround  = CLR_CYAN;
  110.   clrBlackSquare = CLR_DARKGREEN;
  111.   clrWhiteSquare = CLR_WHITE;
  112.   clrBlackPiece  = CLR_DARKRED;
  113.   clrWhitePiece  = CLR_PALEGRAY;
  114.   clrText        = CLR_BLACK;
  115.  
  116.   UpdateChilds();
  117.   }
  118.  
  119.  
  120. void SaveColors(PSZ appname )
  121.   {
  122.   HINI hIni;
  123.   char ostring[30];
  124.  
  125.  
  126.   hIni = PrfOpenProfile(hab, lpChessini);
  127.  
  128.    sprintf ( ostring, np08lX, clrBackGround);
  129.    PrfWriteProfileString (hIni, appname, lpBackGround,ostring);
  130.  
  131.    sprintf ( ostring, np08lX, clrBlackSquare);
  132.    PrfWriteProfileString (hIni, appname, lpBlackSquare,ostring);
  133.    
  134.    sprintf ( ostring, np08lX, clrWhiteSquare);
  135.    PrfWriteProfileString (hIni, appname, lpWhiteSquare,ostring);
  136.    
  137.    sprintf ( ostring, np08lX, clrBlackPiece);
  138.    PrfWriteProfileString (hIni, appname, lpBlackPiece,ostring);
  139.  
  140.    sprintf ( ostring, np08lX, clrWhitePiece);
  141.    PrfWriteProfileString (hIni, appname, lpWhitePiece,ostring);
  142.  
  143.    sprintf ( ostring, np08lX, clrText);
  144.    PrfWriteProfileString (hIni, appname, lpText,ostring);
  145.  
  146.   PrfCloseProfile(hIni);
  147.   }
  148.  
  149. void GetStartupColors ( PSZ appname )
  150. {
  151.   HINI hIni;
  152.   char istring[30];
  153.  
  154.  
  155.   hIni = PrfOpenProfile(hab, lpChessini);
  156.  
  157.    SetStandardColors ();
  158.  
  159.    PrfQueryProfileString (hIni, appname, lpBackGround,lpDefault,istring,
  160.                              sizeof(istring));
  161.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBackGround);
  162.  
  163.    PrfQueryProfileString (hIni, appname, lpBlackSquare,lpDefault,istring,
  164.                              sizeof(istring));
  165.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBlackSquare);
  166.  
  167.    PrfQueryProfileString (hIni, appname, lpWhiteSquare,lpDefault,istring,
  168.                              sizeof(istring));
  169.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrWhiteSquare);
  170.  
  171.    PrfQueryProfileString (hIni, appname, lpBlackPiece,lpDefault,istring,
  172.                              sizeof(istring));
  173.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBlackPiece);
  174.  
  175.    PrfQueryProfileString (hIni, appname, lpWhitePiece,lpDefault,istring,
  176.                              sizeof(istring));
  177.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrWhitePiece);
  178.  
  179.    PrfQueryProfileString (hIni, appname, lpText,lpDefault,istring,
  180.                              sizeof(istring));
  181.    if (strcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrText);
  182.  
  183.   PrfCloseProfile(hIni);
  184.  
  185.  
  186.   UpdateChilds();
  187. }
  188.  
  189.  
  190. int ColorDialog(HWND hWnd, ULONG Param )
  191.   {
  192.   int status;
  193.  
  194.   status = (int)(LONG)WinDlgBox(HWND_DESKTOP, hWnd, ColorProc, 0, IDD_COLORS,
  195.                            (PVOID)Param);
  196.     if (status) UpdateChilds();
  197.   return (status);
  198.   }
  199.  
  200.  
  201. static int ColorToIndex ( ULONG color)
  202. {
  203.    if (color == CLR_BLACK ) return IDC_COLORS_BLACK;
  204.    else if ( color == CLR_BLUE) return IDC_COLORS_BLUE;
  205.    else if ( color == CLR_GREEN) return IDC_COLORS_GREEN;
  206.    else if ( color == CLR_CYAN) return IDC_COLORS_CYAN;
  207.    else if ( color == CLR_RED) return IDC_COLORS_RED;
  208.    else if ( color == CLR_PINK) return IDC_COLORS_PINK;
  209.    else if ( color == CLR_YELLOW) return IDC_COLORS_YELLOW;
  210.    else if ( color == CLR_PALEGRAY) return IDC_COLORS_PALEGRAY;
  211.    else if ( color == CLR_DARKGRAY) return IDC_COLORS_DARKGRAY;
  212.    else if ( color == CLR_DARKBLUE) return IDC_COLORS_DARKBLUE;
  213.    else if ( color == CLR_DARKGREEN) return IDC_COLORS_DARKGREEN;
  214.    else if ( color == CLR_DARKCYAN) return IDC_COLORS_DARKCYAN;
  215.    else if ( color == CLR_DARKRED) return IDC_COLORS_DARKRED;
  216.    else if ( color == CLR_DARKPINK) return IDC_COLORS_DARKPINK;
  217.    else if ( color == CLR_BROWN) return IDC_COLORS_BROWN;
  218.    else if ( color == CLR_WHITE) return IDC_COLORS_WHITE;
  219.    return IDC_COLORS_WHITE;
  220. }
  221.  
  222. static ULONG IndexToColor ( int color)
  223. {
  224.    if (color == IDC_COLORS_BLACK ) return  CLR_BLACK;
  225.    else if ( color == IDC_COLORS_BLUE) return  CLR_BLUE;
  226.    else if ( color == IDC_COLORS_GREEN) return  CLR_GREEN;
  227.    else if ( color == IDC_COLORS_CYAN) return  CLR_CYAN;
  228.    else if ( color == IDC_COLORS_RED) return  CLR_RED;
  229.    else if ( color == IDC_COLORS_PINK) return  CLR_PINK;
  230.    else if ( color == IDC_COLORS_YELLOW) return  CLR_YELLOW;
  231.    else if ( color == IDC_COLORS_PALEGRAY) return  CLR_PALEGRAY;
  232.    else if ( color == IDC_COLORS_DARKGRAY) return  CLR_DARKGRAY;
  233.    else if ( color == IDC_COLORS_DARKBLUE) return  CLR_DARKBLUE;
  234.    else if ( color == IDC_COLORS_DARKGREEN) return  CLR_DARKGREEN;
  235.    else if ( color == IDC_COLORS_DARKCYAN) return  CLR_DARKCYAN;
  236.    else if ( color == IDC_COLORS_DARKRED) return  CLR_DARKRED;
  237.    else if ( color == IDC_COLORS_DARKPINK) return  CLR_DARKPINK;
  238.    else if ( color == IDC_COLORS_BROWN) return  CLR_BROWN;
  239.    else if ( color == IDC_COLORS_WHITE) return  CLR_WHITE;
  240.    return CLR_RED;
  241. }
  242.  
  243. static char lpWBGC[] ="Window background color";
  244. static char lpBS[]   ="Black square color";
  245. static char lpWS[]   ="White square color";
  246. static char lpBP[]   ="Black piece color";
  247. static char lpWP[]   ="White piece color";
  248. static char lpTX[]   ="Text color";
  249.  
  250. static ULONG *pclr;
  251. static int index;
  252.  
  253. MRESULT EXPENTRY ColorProc(HWND hDlg, ULONG msg, MPARAM mp1, MPARAM mp2)
  254.   {
  255.   char *pchHeading;
  256.  
  257.  
  258.   switch (msg)
  259.     {
  260.        case WM_INITDLG:
  261.         switch (clrcase)
  262.         {
  263.         case IDM_COLORS_BACKGROUND:
  264.           pchHeading = (char *) lpWBGC;
  265.           pclr       = &clrBackGround;
  266.           break;
  267.  
  268.         case IDM_COLORS_BSQUARES:
  269.           pchHeading = (char *)lpBS;
  270.           pclr       = &clrBlackSquare;
  271.           break;
  272.  
  273.         case IDM_COLORS_WSQUARES:
  274.           pchHeading = (char *)lpWS;
  275.           pclr       = &clrWhiteSquare;
  276.           break;
  277.  
  278.         case IDM_COLORS_BPIECES:
  279.           pchHeading = (char *) lpBP;
  280.           pclr       = &clrBlackPiece;
  281.           break;
  282.  
  283.         case IDM_COLORS_WPIECES:
  284.           pchHeading = (char *) lpWP;
  285.           pclr       = &clrWhitePiece;
  286.           break;
  287.  
  288.         case IDM_COLORS_TEXT:
  289.           pchHeading = (char *) lpTX;
  290.           pclr       = &clrText;
  291.           break;
  292.  
  293.         }
  294.  
  295.       WinSetDlgItemText(hDlg, IDC_COLORS_HEADER, pchHeading);
  296.       index = ColorToIndex( *pclr);
  297.  
  298.       WinSendDlgItemMsg(hDlg, index, BM_SETCHECK, MPFROMSHORT(TRUE), NULL);
  299.  
  300.       WinSetFocus (HWND_DESKTOP, WinWindowFromID(hDlg, index));
  301.  
  302.       return ((MRESULT)TRUE);
  303.  
  304.     case WM_CONTROL:
  305.       WinSendDlgItemMsg(hDlg, index, BM_SETCHECK, MPFROMSHORT(FALSE), NULL);
  306.       index = SHORT1FROMMP(mp1);
  307.       WinSendDlgItemMsg(hDlg, index, BM_SETCHECK, MPFROMSHORT(TRUE), NULL);
  308.       return 0 ;
  309.  
  310.    case WM_COMMAND:
  311.       switch (SHORT1FROMMP(mp1))
  312.         {
  313.         case IDC_OK:
  314.           *pclr = IndexToColor (index);
  315.           WinDismissDlg(hDlg, TRUE);
  316.           break;
  317.  
  318.         case IDC_CANCEL:
  319.           WinDismissDlg(hDlg, FALSE);
  320.           break;
  321.         }
  322.       return 0 ; 
  323.     }
  324.           return (WinDefDlgProc(hDlg, msg, mp1, mp2));
  325.   }
  326.