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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //
  5. //  This file implements a custom control (TColorControl) and a
  6. //  dialog box (TColorDialog) that uses them.
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #include <owl/applicat.h>
  10. #include <owl/scrollba.h>
  11. #include <owl/dc.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <mem.h>
  16. #include "colordlg.rh"
  17. #include "colordlg.h"
  18.  
  19. const unsigned int CN_CLICKED = 1;      // color control notifications
  20.  
  21. //
  22. //
  23. //
  24. DEFINE_RESPONSE_TABLE1(TColorControl, TControl)
  25.   EV_WM_LBUTTONDOWN,
  26.   EV_WM_PAINT,
  27. END_RESPONSE_TABLE;
  28.  
  29. //
  30. // This is only included if we're building the DLL version of colordlg
  31. //
  32. #if defined(BUILDEXAMPLEDLL)
  33.  
  34.   // This exported function provides C (non-OWL) access to the dialog
  35.   //
  36.   extern "C" bool FAR PASCAL __export
  37.   GetColor(HWND parentHWnd, COLORREF& colorBuffer)
  38.   {
  39.     // Create an alias OWL application object when called from a non-OWL
  40.     // program so that TDialog is happy
  41.     //
  42.     TModule* appModule;
  43.     TApplication dummyApp("color dialog app", appModule);
  44.     TWindow parentAlias(parentHWnd, ::Module);
  45.     return TColorDialog(&parentAlias, (TColor&)colorBuffer).Execute() == IDOK;
  46.   }
  47. #endif
  48.  
  49. //
  50. //
  51. //
  52. TColorControl::TColorControl(TWindow* parent, int resId, TColor color)
  53. :
  54.   TControl(parent, resId, ::Module),
  55.   Color(color)
  56. {
  57.   DisableTransfer();
  58. }
  59.  
  60. //
  61. // Must use EvPaint since TControl assumes a pre-defined class control has a
  62. // window's class that paints it. (see source\owl/control.cpp)
  63. //
  64. void
  65. TColorControl::EvPaint()
  66. {
  67.   TPaintDC dc(*this);
  68.   dc.FillRect(GetClientRect(), TBrush(Color));
  69. }
  70.  
  71.  
  72. //
  73. //
  74. //
  75. void
  76. TColorControl::SetColor(TColor color)
  77. {
  78.   Color = color;
  79.   Invalidate();
  80. }
  81.  
  82. //
  83. //
  84. //
  85. uint
  86. TColorControl::Transfer(void* buffer, TTransferDirection direction)
  87. {
  88.   if (direction == tdGetData)
  89.     memcpy(buffer, &Color, sizeof Color);
  90.  
  91.   else
  92.     if (direction == tdSetData)
  93.       memcpy(&Color, buffer, sizeof Color);
  94.  
  95.   return sizeof Color;
  96. }
  97.  
  98. //
  99. // Notify parent of a CN_CLICKED event by sending a WM_COMMAND message
  100. //
  101. void
  102. TColorControl::EvLButtonDown(uint, TPoint&)
  103. {
  104.   Parent->SendNotification(Attr.Id, CN_CLICKED, HWindow);
  105. }
  106.  
  107. //
  108. //
  109. //
  110. DEFINE_RESPONSE_TABLE1(TColorDialog, TDialog)
  111.   EV_CHILD_NOTIFY(ID_COLOR1,CN_CLICKED,ClickFmControl1),
  112.   EV_CHILD_NOTIFY(ID_COLOR2,CN_CLICKED,ClickFmControl2),
  113.   EV_CHILD_NOTIFY(ID_COLOR3,CN_CLICKED,ClickFmControl3),
  114.   EV_CHILD_NOTIFY(ID_COLOR4,CN_CLICKED,ClickFmControl4),
  115.   EV_CHILD_NOTIFY(ID_COLOR5,CN_CLICKED,ClickFmControl5),
  116.   EV_CHILD_NOTIFY(ID_COLOR6,CN_CLICKED,ClickFmControl6),
  117.   EV_CHILD_NOTIFY(ID_COLOR7,CN_CLICKED,ClickFmControl7),
  118.   EV_CHILD_NOTIFY(ID_COLOR8,CN_CLICKED,ClickFmControl8),
  119.   EV_SB_ENDSCROLL(ID_COLORBAR1,SetColorFmSlider),
  120.   EV_SB_ENDSCROLL(ID_COLORBAR2,SetColorFmSlider),
  121.   EV_SB_ENDSCROLL(ID_COLORBAR3,SetColorFmSlider),
  122. END_RESPONSE_TABLE;
  123.  
  124. //
  125. //
  126. //
  127. static void
  128. DisableChildTransfer(TWindow* w, void*)
  129. {
  130.   w->DisableTransfer();
  131. }
  132.  
  133. //
  134. //
  135. //
  136. TColorDialog::TColorDialog(TWindow* parent, TColor& color)
  137. :
  138.   TDialog(parent, IDD_COLORDLG, ::Module)
  139. {
  140.   new TColorControl(this, ID_COLOR1, TColor(000, 000, 000));
  141.   new TColorControl(this, ID_COLOR2, TColor(255, 255, 255));
  142.   new TColorControl(this, ID_COLOR3, TColor(255, 000, 000));
  143.   new TColorControl(this, ID_COLOR4, TColor(000, 255, 000));
  144.   new TColorControl(this, ID_COLOR5, TColor(000, 000, 255));
  145.   new TColorControl(this, ID_COLOR6, TColor(000, 255, 255));
  146.   new TColorControl(this, ID_COLOR7, TColor(255, 000, 255));
  147.   new TColorControl(this, ID_COLOR8, TColor(255, 255, 000));
  148.  
  149.   ColorBar1 = new TScrollBar(this, ID_COLORBAR1, ::Module);
  150.   ColorBar2 = new TScrollBar(this, ID_COLORBAR2, ::Module);
  151.   ColorBar3 = new TScrollBar(this, ID_COLORBAR3, ::Module);
  152.  
  153.   ForEach(DisableChildTransfer);
  154.  
  155.   SelColor = new TColorControl(this, ID_SELCOLOR, color);
  156.   SelColor->EnableTransfer();
  157.  
  158.   TransferBuffer = &color;
  159. }
  160.  
  161. //
  162. // Handlers for each custom control
  163. //
  164. void TColorDialog::ClickFmControl1() {SetColorFmControl(ID_COLOR1);}
  165. void TColorDialog::ClickFmControl2() {SetColorFmControl(ID_COLOR2);}
  166. void TColorDialog::ClickFmControl3() {SetColorFmControl(ID_COLOR3);}
  167. void TColorDialog::ClickFmControl4() {SetColorFmControl(ID_COLOR4);}
  168. void TColorDialog::ClickFmControl5() {SetColorFmControl(ID_COLOR5);}
  169. void TColorDialog::ClickFmControl6() {SetColorFmControl(ID_COLOR6);}
  170. void TColorDialog::ClickFmControl7() {SetColorFmControl(ID_COLOR7);}
  171. void TColorDialog::ClickFmControl8() {SetColorFmControl(ID_COLOR8);}
  172.  
  173. //
  174. // Update the selected color control and bars with the chosen color
  175. //
  176. void
  177. TColorDialog::SetColorFmControl(uint id)
  178. {
  179.   TColorControl* control = TYPESAFE_DOWNCAST(ChildWithId(id), TColorControl);
  180.   if (control) {
  181.     TColor color = control->GetColor();
  182.     SelColor->SetColor(color);
  183.     UpdateBars(color);
  184.   }
  185. }
  186.  
  187. //
  188. // Update the selected color control with the current slider values
  189. //
  190. void
  191. TColorDialog::SetColorFmSlider()
  192. {
  193.   SelColor->SetColor(TColor(ColorBar1->GetPosition(),
  194.   ColorBar2->GetPosition(), ColorBar3->GetPosition()));
  195. }
  196.  
  197. //
  198. //
  199. //
  200. void
  201. TColorDialog::SetupWindow()
  202. {
  203.   TDialog::SetupWindow();
  204.   ColorBar1->SetRange(0, 255);
  205.   ColorBar2->SetRange(0, 255);
  206.   ColorBar3->SetRange(0, 255);
  207.  
  208.   UpdateBars(SelColor->GetColor());
  209. }
  210.  
  211.  
  212. //
  213. //
  214. //
  215. void
  216. TColorDialog::TransferData(TTransferDirection transferFlag)
  217. {
  218.   TDialog::TransferData(transferFlag);
  219.   if (transferFlag == tdSetData)
  220.     UpdateBars(SelColor->GetColor());
  221. }
  222.  
  223. //
  224. //
  225. //
  226. void
  227. TColorDialog::UpdateBars(TColor color)
  228. {
  229.   ColorBar1->SetPosition(color.Red());
  230.   ColorBar2->SetPosition(color.Green());
  231.   ColorBar3->SetPosition(color.Blue());
  232. }
  233.