home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 7.ddi / OWLDEMOS.ZIP / COLORDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.0 KB  |  151 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include "colordlg.h"
  4.  
  5. const unsigned int CN_CLICKED = 0;
  6. const unsigned int CN_DBLCLICKED = 1;
  7.  
  8. #ifdef __DLL__
  9. extern PTModule TheModule;
  10. #else
  11. PTModule TheModule = NULL;
  12. #endif
  13.  
  14.  
  15. TColorControl::TColorControl(PTWindowsObject AParent, int ResourceId,
  16.   COLORREF AColor, PTModule AModule)
  17.   : TControl(AParent, ResourceId, AModule)
  18. {
  19.   Color = AColor;
  20.   DisableTransfer();
  21. }
  22.  
  23. LPSTR TColorControl::GetClassName()
  24. {
  25.   return "ColorControl";
  26. }
  27.  
  28. void TColorControl::GetWindowClass(WNDCLASS& AWndClass)
  29. {
  30.   TControl::GetWindowClass(AWndClass);
  31.   AWndClass.style |= CS_DBLCLKS;
  32. }
  33.  
  34. void TColorControl::Paint(HDC DC, PAINTSTRUCT& PS)
  35. {
  36.   LOGBRUSH ALogBrush;
  37.   HBRUSH NewBrush;
  38.   HBRUSH OldBrush;
  39.  
  40.   ALogBrush.lbStyle = BS_SOLID;
  41.   ALogBrush.lbColor = Color;
  42.   NewBrush = CreateBrushIndirect(&ALogBrush);
  43.   OldBrush = (HBRUSH)SelectObject(DC, NewBrush);
  44.   Rectangle(DC, PS.rcPaint.left, PS.rcPaint.top, PS.rcPaint.right,
  45.     PS.rcPaint.bottom);
  46.   SelectObject(DC, OldBrush);
  47.   DeleteObject(NewBrush);
  48. }
  49.  
  50. void TColorControl::SetColor(COLORREF AColor)
  51. {
  52.   Color = AColor;
  53.   InvalidateRect(HWindow, NULL, TRUE);
  54. }
  55.  
  56. WORD TColorControl::Transfer(void *DataPtr, WORD TransferFlag)
  57. {
  58.   if ( TransferFlag == TF_GETDATA )
  59.   {
  60.     memcpy(DataPtr, &Color, sizeof Color);
  61.   }
  62.   else
  63.     if ( TransferFlag == TF_SETDATA )
  64.       memcpy(&Color, DataPtr, sizeof Color);
  65.   return sizeof(Color);
  66. }
  67.  
  68. void TColorControl::WMLButtonDown(RTMessage)
  69. {
  70.   SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  71.     MAKELONG(HWindow, CN_CLICKED));
  72. }
  73.  
  74. void TColorControl::WMLButtonDblClk(RTMessage)
  75. {
  76.   SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  77.     MAKELONG(HWindow, CN_DBLCLICKED));
  78. }
  79.  
  80. static void DisableChildTransfer(void *P, void *)
  81. {
  82.   ((PTWindowsObject)P)->DisableTransfer();
  83. }
  84.  
  85. TColorDialog::TColorDialog(TWindowsObject *AParent, COLORREF& TheColor)
  86.              : TDialog(AParent, "COLORDIALOG", TheModule)
  87. {
  88.   new TColorControl(this, ID_COLOR1, RGB(0x00, 0x00, 0x00), TheModule);
  89.   new TColorControl(this, ID_COLOR2, RGB(0xFF, 0xFF, 0xFF), TheModule);
  90.   new TColorControl(this, ID_COLOR3, RGB(0xFF, 0x00, 0x00), TheModule);
  91.   new TColorControl(this, ID_COLOR4, RGB(0x00, 0xFF, 0x00), TheModule);
  92.   new TColorControl(this, ID_COLOR5, RGB(0x00, 0x00, 0xFF), TheModule);
  93.   new TColorControl(this, ID_COLOR6, RGB(0x00, 0xFF, 0xFF), TheModule);
  94.   new TColorControl(this, ID_COLOR7, RGB(0xFF, 0x00, 0xFF), TheModule);
  95.   new TColorControl(this, ID_COLOR8, RGB(0xFF, 0xFF, 0x00), TheModule);
  96.   ColorBar1 = new TScrollBar(this, ID_COLORBAR1, TheModule);
  97.   ColorBar2 = new TScrollBar(this, ID_COLORBAR2, TheModule);
  98.   ColorBar3 = new TScrollBar(this, ID_COLORBAR3, TheModule);
  99.   ForEach(DisableChildTransfer, 0);
  100.   SelColor = new TColorControl(this, ID_SELCOLOR, TheColor, TheModule);
  101.   SelColor->EnableTransfer();
  102.   TransferBuffer = &TheColor;
  103. }
  104.  
  105. void TColorDialog::DefChildProc(RTMessage Msg)
  106. {
  107.   TColorControl *TheControl;
  108.  
  109.   if ( Msg.WParam <= ID_COLORBAR3 )
  110.   {
  111.     SelColor->SetColor(RGB((unsigned int)ColorBar1->GetPosition(),
  112.     (unsigned int)ColorBar2->GetPosition(),
  113.     (unsigned int)ColorBar3->GetPosition()));
  114.   }
  115.   else
  116.   {
  117.     TheControl = (TColorControl *)ChildWithId(Msg.WParam);
  118.     SelColor->SetColor(TheControl->Color);
  119.     UpdateBars(TheControl->Color);
  120.     if (Msg.LP.Hi == CN_DBLCLICKED)
  121.     {
  122.       CloseWindow(IDOK);
  123.     }
  124.   }
  125. }
  126.  
  127. void TColorDialog::SetupWindow()
  128. {
  129.   TDialog::SetupWindow();
  130.   ColorBar1->SetRange(0, 255);
  131.   ColorBar2->SetRange(0, 255);
  132.   ColorBar3->SetRange(0, 255);
  133. }
  134.  
  135. void TColorDialog::TransferData(WORD TransferFlag)
  136. {
  137.   TDialog::TransferData(TransferFlag);
  138.   if ( TransferFlag == TF_SETDATA )
  139.   {
  140.     UpdateBars(SelColor->Color);
  141.   }
  142. }
  143.  
  144. void TColorDialog::UpdateBars(COLORREF AColor)
  145. {
  146.   ColorBar1->SetPosition(GetRValue(AColor));
  147.   ColorBar2->SetPosition(GetGValue(AColor));
  148.   ColorBar3->SetPosition(GetBValue(AColor));
  149. }
  150.  
  151.