home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / colordlg.pak / COLORDLG.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  6KB  |  194 lines

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