home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / chess / color.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  10KB  |  296 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30 Daryl Baker
  5.  
  6.   Based on Ideas and code segments of Charles Petzold from artices in
  7.   MicroSoft Systems Journal January 1990 Vol. 5 Number 1.
  8.  
  9.   This file is part of CHESS.
  10.  
  11.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  12.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  13.   the consequences of using it or for whether it serves any particular
  14.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  15.   General Public License for full details.
  16.  
  17.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  18.   only under the conditions described in the CHESS General Public License.
  19.   A copy of this license is supposed to have been given to you along with
  20.   CHESS so you can know your rights and responsibilities.  It should be in a
  21.   file named COPYING.  Among other things, the copyright notice and this
  22.   notice must be preserved on all copies.
  23. */
  24.  
  25. #define NOATOM 
  26. #define NOCLIPBOARD
  27. #define NOCREATESTRUCT
  28. #define NOFONT
  29. #define NOREGION
  30. #define NOSOUND
  31. #define NOWH
  32. #define NOWINOFFSETS
  33. #define NOCOMM
  34. #define NOKANJI
  35.  
  36. #include <windows.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. #include "chess.h"
  41. #include "color.h"
  42.  
  43. #define CBLACK     RGB(0,0,0)
  44. #define BLUE      RGB(0,0,255)
  45. #define GREEN     RGB(0,255,0)
  46. #define CYAN      RGB(128,255,255)
  47. #define RED       RGB(255,0,0)
  48. #define PINK      RGB(255,0,255)
  49. #define YELLOW    RGB(255,255,0)
  50. #define PALEGRAY  RGB(192,192,192)
  51. #define DARKGRAY  RGB(127,127,127)
  52. #define DARKBLUE  RGB(0,0,128)
  53. #define DARKGREEN RGB(0,128,0)
  54. #define DARKCYAN  RGB(0,255,255)
  55. #define DARKRED   RGB(128,0,0)
  56. #define DARKPINK  RGB(255,0,128)
  57. #define BROWN     RGB(128,128,64)
  58. #define CWHITE     RGB(255,255,255)
  59.  
  60. extern DWORD clrBackGround;
  61. extern DWORD clrBlackSquare;
  62. extern DWORD clrWhiteSquare;
  63. extern DWORD clrBlackPiece;
  64. extern DWORD clrWhitePiece;
  65. extern DWORD clrText;
  66.  
  67. BOOL FAR PASCAL ColorDlgProc ( HWND hDlg, unsigned message,
  68.                                WORD wParam, LONG lParam);
  69.  
  70.  
  71. void SetStandardColors ( VOID )
  72. {
  73.    clrBackGround =  BROWN;
  74.    clrBlackSquare = DARKGREEN;
  75.    clrWhiteSquare = PALEGRAY;
  76.    clrBlackPiece  = RED;
  77.    clrWhitePiece  = CWHITE;
  78.    clrText        = CBLACK;
  79. }
  80.  
  81.  
  82. static char _based(_segname("_CODE")) lpChessini[]    = "chess.ini";
  83. static char _based(_segname("_CODE")) lpBackGround[]  = "BackGround";
  84. static char _based(_segname("_CODE")) lpBlackSquare[] = "BlackSquare";
  85. static char _based(_segname("_CODE")) lpWhiteSquare[] = "WhiteSquare";
  86. static char _based(_segname("_CODE")) lpBlackPiece[]  = "BlackPiece";
  87. static char _based(_segname("_CODE")) lpWhitePiece[]  = "WhitePiece";
  88. static char _based(_segname("_CODE")) lpDefault[]     = "Default";
  89. static char _based(_segname("_CODE")) lpText[]        = "Text";
  90.  
  91. static char np08lX[] = "%08lX";
  92.  
  93. void SaveColors ( LPSTR appname )
  94. {
  95.    char ostring[30];
  96.  
  97.    wsprintf ( ostring, np08lX, clrBackGround);
  98.    WritePrivateProfileString ( appname, lpBackGround,ostring,lpChessini);
  99.  
  100.    wsprintf ( ostring, np08lX, clrBlackSquare);
  101.    WritePrivateProfileString ( appname, lpBlackSquare,ostring,lpChessini);
  102.    
  103.    wsprintf ( ostring, np08lX, clrWhiteSquare);
  104.    WritePrivateProfileString ( appname, lpWhiteSquare,ostring,lpChessini);
  105.    
  106.    wsprintf ( ostring, np08lX, clrBlackPiece);
  107.    WritePrivateProfileString ( appname, lpBlackPiece,ostring,lpChessini);
  108.    
  109.    wsprintf ( ostring, np08lX, clrWhitePiece);
  110.    WritePrivateProfileString ( appname, lpWhitePiece,ostring,lpChessini);
  111.  
  112.    wsprintf ( ostring, np08lX, clrText);
  113.    WritePrivateProfileString ( appname, lpText,ostring,lpChessini);
  114. }
  115.  
  116. void GetStartupColors ( LPSTR appname )
  117. {
  118.    char istring[30];
  119.  
  120.    SetStandardColors ();
  121.  
  122.    GetPrivateProfileString ( appname, lpBackGround,lpDefault,istring,
  123.                              sizeof(istring), lpChessini);
  124.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBackGround);
  125.  
  126.    GetPrivateProfileString ( appname, lpBlackSquare,lpDefault,istring,
  127.                              sizeof(istring), lpChessini);
  128.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBlackSquare);
  129.  
  130.    GetPrivateProfileString ( appname, lpWhiteSquare,lpDefault,istring,
  131.                              sizeof(istring), lpChessini);
  132.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrWhiteSquare);
  133.  
  134.    GetPrivateProfileString ( appname, lpBlackPiece,lpDefault,istring,
  135.                              sizeof(istring), lpChessini);
  136.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrBlackPiece);
  137.  
  138.    GetPrivateProfileString ( appname, lpWhitePiece,lpDefault,istring,
  139.                              sizeof(istring), lpChessini);
  140.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrWhitePiece);
  141.  
  142.    GetPrivateProfileString ( appname, lpText,lpDefault,istring,
  143.                              sizeof(istring), lpChessini);
  144.    if (_fstrcmp ( istring, lpDefault) != 0)  sscanf ( istring, np08lX, &clrText);
  145. }
  146.  
  147.  
  148. int ColorDialog ( HWND hWnd, HANDLE hInst, DWORD Param )
  149. {
  150.    FARPROC lpProcColor;
  151.    int status;
  152.  
  153.    lpProcColor = MakeProcInstance(ColorDlgProc, hInst);
  154.     status = DialogBoxParam (hInst, MAKEINTRESOURCE(COLOR),   hWnd,  lpProcColor, Param);
  155.    FreeProcInstance(lpProcColor);
  156.    return status;
  157. }
  158.  
  159. static int ColorToIndex ( DWORD color)
  160. {
  161.    if (color == CBLACK ) return CNT_BLACK;
  162.    else if ( color == BLUE) return CNT_BLUE;
  163.    else if ( color == GREEN) return CNT_GREEN;
  164.    else if ( color == CYAN) return CNT_CYAN;
  165.    else if ( color == RED) return CNT_RED;
  166.    else if ( color == PINK) return CNT_PINK;
  167.    else if ( color == YELLOW) return CNT_YELLOW;
  168.    else if ( color == PALEGRAY) return CNT_PALEGRAY;
  169.    else if ( color == DARKGRAY) return CNT_DARKGRAY;
  170.    else if ( color == DARKBLUE) return CNT_DARKBLUE;
  171.    else if ( color == DARKGREEN) return CNT_DARKGREEN;
  172.    else if ( color == DARKCYAN) return CNT_DARKCYAN;
  173.    else if ( color == DARKRED) return CNT_DARKRED;
  174.    else if ( color == DARKPINK) return CNT_DARKPINK;
  175.    else if ( color == BROWN) return CNT_BROWN;
  176.    else if ( color == CWHITE) return CNT_WHITE;
  177.    return CNT_WHITE;
  178. }
  179.  
  180. static DWORD IndexToColor ( int color)
  181. {
  182.    if (color == CNT_BLACK ) return  CBLACK;
  183.    else if ( color == CNT_BLUE) return  BLUE;
  184.    else if ( color == CNT_GREEN) return  GREEN;
  185.    else if ( color == CNT_CYAN) return  CYAN;
  186.    else if ( color == CNT_RED) return  RED;
  187.    else if ( color == CNT_PINK) return  PINK;
  188.    else if ( color == CNT_YELLOW) return  YELLOW;
  189.    else if ( color == CNT_PALEGRAY) return  PALEGRAY;
  190.    else if ( color == CNT_DARKGRAY) return  DARKGRAY;
  191.    else if ( color == CNT_DARKBLUE) return  DARKBLUE;
  192.    else if ( color == CNT_DARKGREEN) return  DARKGREEN;
  193.    else if ( color == CNT_DARKCYAN) return  DARKCYAN;
  194.    else if ( color == CNT_DARKRED) return  DARKRED;
  195.    else if ( color == CNT_DARKPINK) return  DARKPINK;
  196.    else if ( color == CNT_BROWN) return  BROWN;
  197.    else if ( color == CNT_WHITE) return  CWHITE;
  198.    return RGB(255,0,0);
  199. }
  200.  
  201. static char _based(_segname("_CODE")) lpWBGC[] ="Window background color";
  202. static char _based(_segname("_CODE")) lpBS[]   ="Black square color";
  203. static char _based(_segname("_CODE")) lpWS[]   ="White square color";
  204. static char _based(_segname("_CODE")) lpBP[]   ="Black piece color";
  205. static char _based(_segname("_CODE")) lpWP[]   ="White piece color";
  206. static char _based(_segname("_CODE")) lpTX[]   ="Text color";
  207.  
  208. static DWORD *pclr;
  209. static int index;
  210.  
  211. BOOL FAR PASCAL ColorDlgProc ( HWND hDlg, unsigned message,
  212.                                WORD wParam, LONG lParam)
  213. {
  214.    char far *pchHeading;
  215.  
  216.    switch (message) {
  217.        case WM_INITDIALOG:           /* message: initialize dialog box */
  218.  
  219.          switch (lParam){
  220.             default:
  221.             case IDM_BACKGROUND:
  222.                pchHeading = (char far *) lpWBGC; 
  223.                pclr       = &clrBackGround;
  224.                break;
  225.  
  226.             case IDM_BLACKSQUARE:
  227.                pchHeading = (char far *)lpBS;
  228.                pclr       = &clrBlackSquare;
  229.                break;
  230.  
  231.             case IDM_WHITESQUARE:
  232.                pchHeading = (char far *)lpWS;
  233.                pclr       = &clrWhiteSquare;
  234.                break;
  235.  
  236.             case IDM_BLACKPIECE:
  237.                pchHeading = (char far *) lpBP;
  238.                pclr       = &clrBlackPiece;
  239.                break;
  240.  
  241.             case IDM_WHITEPIECE:
  242.                pchHeading = (char far *) lpWP;
  243.                pclr       = &clrWhitePiece;
  244.                break;
  245.  
  246.             case IDM_TEXT:
  247.                pchHeading = (char far *) lpTX;
  248.                pclr       = &clrText;
  249.                break;
  250.          }
  251.  
  252.          SetDlgItemText ( hDlg, IDD_HEADING, pchHeading);
  253.          index = ColorToIndex ( *pclr);
  254.          CheckRadioButton ( hDlg, CNT_BLACK, CNT_WHITE, index);
  255.          return (TRUE);
  256.  
  257.        case WM_COMMAND:              /* message: received a command */
  258.          switch (wParam) {
  259.          
  260.             case IDD_OK:
  261.                  EndDialog(hDlg, 1);
  262.                *pclr = IndexToColor (index);
  263.                  return TRUE;
  264.  
  265.             case IDD_CANCEL:
  266.                  EndDialog(hDlg, NULL);
  267.                  return TRUE;
  268.  
  269.             case CNT_BLACK:
  270.             case CNT_BLUE:     
  271.             case CNT_GREEN:    
  272.             case CNT_CYAN:     
  273.             case CNT_RED:      
  274.             case CNT_PINK:     
  275.             case CNT_YELLOW:   
  276.             case CNT_PALEGRAY: 
  277.             case CNT_DARKGRAY: 
  278.             case CNT_DARKBLUE: 
  279.             case CNT_DARKGREEN:   
  280.             case CNT_DARKCYAN:    
  281.             case CNT_DARKRED:     
  282.             case CNT_DARKPINK:    
  283.             case CNT_BROWN:
  284.             case CNT_WHITE:
  285.  
  286.                index = wParam;       
  287.                CheckRadioButton ( hDlg, CNT_BLACK, CNT_WHITE, index);
  288.                break;
  289.          }
  290.           break;
  291.     }
  292.  
  293.     return (FALSE);                  /* Didn't process a message    */
  294. }
  295.  
  296.