home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CHESS.PAK / COLORS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.4 KB  |  309 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl/pch.h>
  5. #include <owl/static.h>
  6. #include <owl/inputdia.h>
  7. #include <bwcc.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include "wcdefs.h"
  11. #include "info.h"
  12. #include "wchess.h"
  13. #include "edit.h"
  14. #include "colors.h"
  15. #include "externs.h"
  16.  
  17. #define min(x, y)   (((x) < (y)) ? (x) : (y))
  18. #define max(x, y)   (((x) > (y)) ? (x) : (y))
  19.  
  20. DEFINE_RESPONSE_TABLE1(TColorsDialog, TDialog)
  21.   EV_WM_CTLCOLOR,
  22.   EV_WM_VSCROLL,
  23.   EV_WM_DRAWITEM,
  24.   EV_COMMAND(IDOK, CmOk),
  25.   EV_MESSAGE(CL_KILLFOCUS, EvKillFocus),
  26. END_RESPONSE_TABLE;
  27.  
  28. TColorsDialog::TColorsDialog(TWindow* parent, const char* name)
  29.   : TWindow(parent),
  30.     TDialog(parent, name)
  31. {
  32. }
  33.  
  34. TColorsDialog::~TColorsDialog()
  35. {
  36.   DeleteObject(hSBBrush[Red]);
  37.   DeleteObject(hSBBrush[Green]);
  38.   DeleteObject(hSBBrush[Blue]);
  39. }
  40.  
  41. inline void
  42. TColorsDialog::CLSetFocus(HWND hWnd)
  43. {
  44.   SendMessage(WM_NEXTDLGCTL, WPARAM(hWnd), 1);
  45.   ::SendMessage(hWnd, EM_SETSEL, 0, MAKELONG(0, 32767));
  46. }
  47.  
  48. BOOL
  49. TColorsDialog::GetColorValue(WORD id)
  50. {
  51.   int  newVal;
  52.   bool ok;
  53.   BYTE *curVal;
  54.   HWND hScroller, hButton;
  55.  
  56.   newVal = GetDlgItemInt(id, &ok, TRUE);
  57.  
  58.   switch (id) {
  59.     case IDD_WEDITRED:
  60.       curVal = &WSqColors[0];
  61.       hScroller = GetDlgItem(IDD_WRED);
  62.       hButton = WhiteSq;
  63.       break;
  64.     case IDD_WEDITGREEN:
  65.       curVal = &WSqColors[1];
  66.       hScroller = GetDlgItem(IDD_WGREEN);
  67.       hButton = WhiteSq;
  68.       break;
  69.     case IDD_WEDITBLUE:
  70.       curVal = &WSqColors[2];
  71.       hScroller = GetDlgItem(IDD_WBLUE);
  72.       hButton = WhiteSq;
  73.       break;
  74.     case IDD_BEDITRED:
  75.       curVal = &BSqColors[0];
  76.       hScroller = GetDlgItem(IDD_BRED);
  77.       hButton = BlackSq;
  78.       break;
  79.     case IDD_BEDITGREEN:
  80.       curVal = &BSqColors[1];
  81.       hScroller = GetDlgItem(IDD_BGREEN);
  82.       hButton = BlackSq;
  83.       break;
  84.     case IDD_BEDITBLUE:
  85.       curVal = &BSqColors[2];
  86.       hScroller = GetDlgItem(IDD_BBLUE);
  87.       hButton = BlackSq;
  88.       break;
  89.     }
  90.  
  91.   if (ok)
  92.     ok = (newVal > 255) ? FALSE : ((newVal < 0 ) ? FALSE : TRUE);
  93.   else
  94.     newVal = *curVal;
  95.  
  96.   if (!ok) {
  97.     Error("Please enter a valid number between 0 and 255.");
  98.     CLSetFocus(GetDlgItem(id));
  99.  
  100.   } else if (newVal != *curVal) {
  101.     *curVal = (BYTE)newVal;
  102.     ::SetScrollPos(hScroller, SB_CTL, *curVal, TRUE);
  103.     ::InvalidateRect(hButton, 0, FALSE);
  104.   }
  105.   return ok;
  106. }
  107.  
  108. void
  109. TColorsDialog::CmOk()
  110. {
  111.   MSG msg;
  112.   while (PeekMessage(&msg, HWindow, CL_KILLFOCUS, CL_KILLFOCUS, PM_NOREMOVE))
  113.     continue;
  114.  
  115.   if (GetColorValue(IDD_WEDITRED) && GetColorValue(IDD_WEDITGREEN) &&
  116.       GetColorValue(IDD_WEDITBLUE) && GetColorValue(IDD_BEDITRED) &&
  117.       GetColorValue(IDD_BEDITGREEN) && GetColorValue(IDD_BEDITBLUE)) {
  118.  
  119.     WhiteSquareColor = TColor(WSqColors[0], WSqColors[1], WSqColors[2]);
  120.     BlackSquareColor = TColor(BSqColors[0], BSqColors[1], BSqColors[2]);
  121.     CloseWindow(IDOK);
  122.   }
  123. }
  124.  
  125. void
  126. TColorsDialog::SetupWindow()
  127. {
  128.   TDialog::SetupWindow();
  129.  
  130.   WSqColors[0] = WhiteSquareColor.Red();
  131.   WSqColors[1] = WhiteSquareColor.Green();
  132.   WSqColors[2] = WhiteSquareColor.Blue();
  133.   BSqColors[0] = BlackSquareColor.Red();
  134.   BSqColors[1] = BlackSquareColor.Green();
  135.   BSqColors[2] = BlackSquareColor.Blue();
  136.  
  137.   SetDlgItemInt(IDD_WEDITRED, WSqColors[0], FALSE);
  138.   SetDlgItemInt(IDD_WEDITGREEN, WSqColors[1], FALSE);
  139.   SetDlgItemInt(IDD_WEDITBLUE, WSqColors[2], FALSE);
  140.  
  141.   ::SetScrollRange(GetDlgItem(IDD_WRED), SB_CTL, 0, 255, FALSE);
  142.   ::SetScrollPos(GetDlgItem(IDD_WRED), SB_CTL, WSqColors[0], FALSE);
  143.   ::SetScrollRange(GetDlgItem(IDD_WGREEN), SB_CTL, 0, 255, FALSE);
  144.   ::SetScrollPos(GetDlgItem(IDD_WGREEN), SB_CTL, WSqColors[1], FALSE);
  145.   ::SetScrollRange(GetDlgItem(IDD_WBLUE), SB_CTL, 0, 255, FALSE);
  146.   ::SetScrollPos(GetDlgItem(IDD_WBLUE), SB_CTL, WSqColors[2], FALSE);
  147.  
  148.   SetDlgItemInt(IDD_BEDITRED, BSqColors[0], FALSE);
  149.   SetDlgItemInt(IDD_BEDITGREEN, BSqColors[1], FALSE);
  150.   SetDlgItemInt(IDD_BEDITBLUE, BSqColors[2], FALSE);
  151.  
  152.   ::SetScrollRange(GetDlgItem(IDD_BRED), SB_CTL, 0, 255, FALSE);
  153.   ::SetScrollPos(GetDlgItem(IDD_BRED), SB_CTL, BSqColors[0], FALSE);
  154.   ::SetScrollRange(GetDlgItem(IDD_BGREEN), SB_CTL, 0, 255, FALSE);
  155.   ::SetScrollPos(GetDlgItem(IDD_BGREEN), SB_CTL, BSqColors[1], FALSE);
  156.   ::SetScrollRange(GetDlgItem(IDD_BBLUE), SB_CTL, 0, 255, FALSE);
  157.   ::SetScrollPos(GetDlgItem(IDD_BBLUE), SB_CTL, BSqColors[2], FALSE);
  158.  
  159.   RWID = ::GetDlgCtrlID(GetDlgItem(IDD_WRED));
  160.   GWID = ::GetDlgCtrlID(GetDlgItem(IDD_WGREEN));
  161.   BWID = ::GetDlgCtrlID(GetDlgItem(IDD_WBLUE));
  162.  
  163.   RBID = ::GetDlgCtrlID(GetDlgItem(IDD_BRED));
  164.   GBID = ::GetDlgCtrlID(GetDlgItem(IDD_BGREEN));
  165.   BBID = ::GetDlgCtrlID(GetDlgItem(IDD_BBLUE));
  166.  
  167.   WStatic = ::GetDlgCtrlID(WhiteSq = GetDlgItem(IDD_WHITECOLOR));
  168.   BStatic = ::GetDlgCtrlID(BlackSq = GetDlgItem(IDD_BLACKCOLOR));
  169.  
  170.   hSBBrush[Red] = CreateSolidBrush(RGB(255, 0, 0));
  171.   hSBBrush[Green] = CreateSolidBrush(RGB(0, 255, 0));
  172.   hSBBrush[Blue] = CreateSolidBrush(RGB(0, 0, 255));
  173. }
  174.  
  175. HBRUSH
  176. TColorsDialog::EvCtlColor(HDC hDC, HWND hWndChild, UINT ctlType)
  177. {
  178.   int id = ::GetDlgCtrlID(hWndChild);
  179.   if (ctlType == CTLCOLOR_SCROLLBAR) {
  180.     if (id == RWID || id == RBID)
  181.       return hSBBrush[Red];
  182.     if (id == GWID || id == GBID)
  183.       return hSBBrush[Green];
  184.     if (id == BWID || id == BBID)
  185.       return hSBBrush[Blue];
  186.     }
  187.   return TDialog::EvCtlColor(hDC, hWndChild, ctlType);
  188. }
  189.  
  190. void
  191. TColorsDialog::EvVScroll(UINT scrollCode, UINT thumbPos, HWND hWndCtl)
  192. {
  193.   int   id = ::GetDlgCtrlID(hWndCtl);
  194.   BYTE* color;
  195.   HWND  hScroller, hButton;
  196.   int   dlgitem;
  197.  
  198.   if (id == RWID) {
  199.     color = &WSqColors[0];
  200.     hScroller = GetDlgItem(IDD_WRED);
  201.     dlgitem = IDD_WEDITRED;
  202.     hButton = WhiteSq;
  203.  
  204.   } else if (id == RBID) {
  205.     color = &BSqColors[0];
  206.     hScroller = GetDlgItem(IDD_BRED);
  207.     dlgitem = IDD_BEDITRED;
  208.     hButton = BlackSq;
  209.  
  210.   } else if (id == BBID) {
  211.     color = &BSqColors[2];
  212.     hScroller = GetDlgItem(IDD_BBLUE);
  213.     dlgitem = IDD_BEDITBLUE;
  214.     hButton = BlackSq;
  215.  
  216.   } else if (id == BWID) {
  217.     color = &WSqColors[2];
  218.     hScroller = GetDlgItem(IDD_WBLUE);
  219.     dlgitem = IDD_WEDITBLUE;
  220.     hButton = WhiteSq;
  221.  
  222.   } else if (id == GBID) {
  223.     color = &BSqColors[1];
  224.     hScroller = GetDlgItem(IDD_BGREEN);
  225.     dlgitem = IDD_BEDITGREEN;
  226.     hButton = BlackSq;
  227.  
  228.   } else if (id == GWID) {
  229.     color = &WSqColors[1];
  230.     hScroller = GetDlgItem(IDD_WGREEN);
  231.     dlgitem = IDD_WEDITGREEN;
  232.     hButton = WhiteSq;
  233.  
  234.   } else
  235.       return;
  236.  
  237.   switch (scrollCode) {
  238.     case SB_PAGEDOWN:
  239.       *color += (BYTE)15;
  240.     case SB_LINEDOWN:
  241.       *color = (BYTE)min(255, *color + 1);
  242.       break;
  243.     case SB_PAGEUP:
  244.       *color -= (BYTE)15;
  245.     case SB_LINEUP:
  246.       *color = (BYTE)max(0, *color - 1);
  247.       break;
  248.     case SB_TOP:
  249.       *color = 0;
  250.       break;
  251.     case SB_BOTTOM:
  252.       *color = 255;
  253.       break;
  254.     case SB_THUMBPOSITION:
  255.     case SB_THUMBTRACK:
  256.       *color = (BYTE)thumbPos;
  257.   }
  258.   ::SetScrollPos(hScroller, SB_CTL, *color, TRUE);
  259.   SetDlgItemInt(dlgitem, *color, FALSE);
  260.   ::InvalidateRect(hButton, 0, FALSE);
  261. }
  262.  
  263. void
  264. TColorsDialog::EvDrawItem(UINT ctlID, DRAWITEMSTRUCT far& drawInfo)
  265. {
  266.   if (drawInfo.itemAction != ODA_DRAWENTIRE)
  267.     return;
  268.  
  269.   BYTE   *colors;
  270.   if (ctlID == WStatic)
  271.     colors = WSqColors;
  272.   else if (ctlID == BStatic)
  273.     colors = BSqColors;
  274.   else
  275.     return;
  276.  
  277.   HANDLE hOldBrush = SelectObject(drawInfo.hDC,
  278.     CreateSolidBrush(RGB(colors[0], colors[1], colors[2])));
  279.  
  280.   TRect* r = (TRect*)&drawInfo.rcItem;
  281.  
  282.   ::Rectangle(drawInfo.hDC, r->left, r->top, r->right, r->bottom);
  283.   DeleteObject(SelectObject(drawInfo.hDC, hOldBrush));
  284. }
  285.  
  286. LRESULT
  287. TColorsDialog::EvKillFocus(WPARAM wParam, LPARAM)
  288. {
  289.   WORD id = (WORD)wParam;
  290.   HWND focus = GetFocus();
  291.  
  292.   if (GetDlgItem(id) == focus || GetDlgItem(IDCANCEL) == focus ||
  293.       GetActiveWindow() != HWindow)
  294.     return 0;
  295.   GetColorValue(id);
  296.   return 1;
  297. }
  298.  
  299. LRESULT
  300. TColorsDialog::EvCommand(UINT id, HWND hWndCtl, UINT notifyCode)
  301. {
  302.   if (hWndCtl && notifyCode == EN_KILLFOCUS) {
  303.     if (::SendMessage(hWndCtl, EM_GETMODIFY, 0, 0))
  304.       PostMessage(CL_KILLFOCUS, id);
  305.     return 1;
  306.   }
  307.   return TDialog::EvCommand(id, hWndCtl, notifyCode);
  308. }
  309.