home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / PalFrame.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  7.5 KB  |  287 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PalFrame.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef PALFRAME_H
  15. #include "PalFrame.h"
  16. #endif
  17.  
  18. #ifndef DRAWPART_H
  19. #include "DrawPart.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. // ----- Part Layer -----
  27.  
  28. #ifndef FWUTIL_H
  29. #include "FWUtil.h"
  30. #endif
  31.  
  32. // ----- OS Layer -----
  33.  
  34. #ifndef FWWINDOW_H
  35. #include "FWWindow.h"
  36. #endif
  37.  
  38. #ifndef FWEVENT_H
  39. #include "FWEvent.h"
  40. #endif
  41.  
  42. #ifndef FWLINSHP_H
  43. #include "FWLinShp.h"
  44. #endif
  45.  
  46. #ifndef FWRECT_H
  47. #include "FWRect.h"
  48. #endif
  49.  
  50. #ifndef FWRECSHP_H
  51. #include "FWRecShp.h"
  52. #endif
  53.  
  54. #ifndef FWMEMMGR_H
  55. #include "FWMemMgr.h"
  56. #endif
  57.  
  58. //========================================================================================
  59. // Runtime Information
  60. //========================================================================================
  61.  
  62. #ifdef FW_BUILD_MAC
  63. #pragma segment odfdrawframes
  64. #endif
  65.  
  66. //========================================================================================
  67. // CLASS CPalette
  68. //========================================================================================
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // CPalette::CPalette
  72. //----------------------------------------------------------------------------------------
  73.  
  74. CPalette::CPalette() :
  75.     fColorTable(NULL)
  76. {
  77. #ifdef FW_BUILD_MAC
  78.     fColorTable = ::GetCTable(8);
  79.     fNumbersOfColors = 256;
  80. #endif
  81.  
  82. #ifdef FW_BUILD_WIN
  83.     HDC hdc = ::GetDC(NULL);
  84. /*    if (::GetDeviceCaps(hdc,  RASTERCAPS) & RC_PALETTE)
  85.     {
  86.         fNumbersOfColors = ::GetDeviceCaps(hdc, SIZEPALETTE);
  87.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  88.         ::GetSystemPaletteEntries(hdc, 0, fNumbersOfColors, fColorTable);
  89.     }
  90.     else*/
  91.     {
  92.         fNumbersOfColors = 1 << (::GetDeviceCaps(hdc, PLANES) * ::GetDeviceCaps(hdc, BITSPIXEL));
  93.         if (fNumbersOfColors < 16)
  94.             fNumbersOfColors = 16;
  95.         if (fNumbersOfColors > 256)
  96.             fNumbersOfColors = 256;
  97.         
  98.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  99.         BYTE red, green, blue;
  100.         red = green = blue = 0;
  101.         
  102.         BYTE redInc, greenInc, blueInc;
  103.         if (fNumbersOfColors == 16)
  104.         {
  105.             redInc = 64;
  106.             greenInc = 128;
  107.             blueInc = 128;        
  108.         }
  109.         else
  110.         {
  111.             redInc = 32;
  112.             greenInc = 32;
  113.             blueInc = 64;        
  114.         }
  115.         
  116.         for (short i = 0; i < fNumbersOfColors; i++)
  117.         {
  118.             fColorTable[i].peRed = red;
  119.             fColorTable[i].peGreen = green;
  120.             fColorTable[i].peBlue = blue;            
  121.             fColorTable[i].peFlags = 0;
  122.             if (!(blue += blueInc))
  123.                 if (!(red += redInc))
  124.                     green += greenInc;
  125. /*
  126.             if (!(red += redInc))
  127.                 if (!(green += greenInc))
  128.                     blue += blueInc;
  129. */
  130.         }
  131.         fColorTable[fNumbersOfColors-1].peRed = 255;
  132.         fColorTable[fNumbersOfColors-1].peGreen = 255;
  133.         fColorTable[fNumbersOfColors-1].peBlue = 255;            
  134.     }
  135.     ::ReleaseDC(NULL, hdc);
  136. #endif
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // CPalette::~CPalette
  141. //----------------------------------------------------------------------------------------
  142.  
  143. CPalette::~CPalette()
  144. {
  145.     if (fColorTable)
  146. #ifdef FW_BUILD_MAC
  147.         ::DisposeCTable(fColorTable);
  148. #endif
  149. #ifdef FW_BUILD_WIN
  150.         delete fColorTable;
  151. #endif    
  152.     fColorTable = NULL;
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // CPalette::GetColor
  157. //----------------------------------------------------------------------------------------
  158.  
  159. void CPalette::GetColor(short colorIndex, FW_CColor* color) const
  160. {
  161. #ifdef FW_BUILD_MAC
  162.     *color = (*fColorTable)->ctTable[colorIndex].rgb;
  163. #endif
  164. #ifdef FW_BUILD_WIN
  165.     if (colorIndex < fNumbersOfColors)
  166.         (*color).SetRGB(fColorTable[colorIndex].peRed,
  167.                         fColorTable[colorIndex].peGreen,
  168.                         fColorTable[colorIndex].peBlue);
  169.     else
  170.         (*color).SetRGB(128, 128, 128);
  171. #endif
  172. }
  173.  
  174. //========================================================================================
  175. // CLASS CPaletteFrame
  176. //========================================================================================
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // CPaletteFrame::CPaletteFrame
  180. //----------------------------------------------------------------------------------------
  181.  
  182. CPaletteFrame::CPaletteFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, CDrawPart *drawPart) :
  183.     CFloatingWindowFrame(ev, odFrame, presentation, drawPart),
  184.     fPalette(NULL),
  185.     fGrid(NULL)
  186. {
  187.     fPalette = new CPalette;
  188.     if (fPalette->NumberOfColors() == 256)
  189.     {
  190.         fGrid = new CGrid(8, 32,                     // 8 rows 32 columns
  191.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  192.                         FW_CPoint(FW_IntToFixed(8), FW_IntToFixed(8)),
  193.                         FW_IntToFixed(1));
  194.     }
  195.     else
  196.     {
  197.         fGrid = new CGrid(2, 8,                     // 2 rows 8 columns
  198.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  199.                         FW_CPoint(FW_IntToFixed(16), FW_IntToFixed(16)),
  200.                         FW_IntToFixed(1));
  201.     }
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // CPaletteFrame::~CPaletteFrame
  206. //----------------------------------------------------------------------------------------
  207.  
  208. CPaletteFrame::~CPaletteFrame()
  209. {
  210.     delete fPalette;
  211.     fPalette = NULL;
  212.     
  213.     delete fGrid;
  214.     fGrid = NULL;
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------
  218. // CPaletteFrame::Draw
  219. //----------------------------------------------------------------------------------------
  220.  
  221. void CPaletteFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  222. {    
  223.     FW_CFacetContext fc(ev, odFacet, invalidShape);
  224.     fc.SetMapping(fMapping);
  225.  
  226.     EraseBackground(ev, fc);
  227.     
  228.     // ----- Draw the grid -----
  229.     fGrid->DrawGridBorders(fc);
  230.     
  231.     // ----- Draw each color -----
  232.     FW_CColor color;
  233.     FW_CRect rect;
  234.     FW_CRectShape rectShape(FW_kZeroRect, FW_kFill);
  235.     for (unsigned long index = 0; index < fPalette->NumberOfColors(); index++)
  236.     {
  237.         fGrid->GetCellInterior(index, rect);
  238.         rectShape.SetRectangle(rect);
  239.         fPalette->GetColor(index, &color);
  240.         rectShape.GetInk()->SetForeColor(color);
  241.         rectShape.Render(fc);
  242.     }
  243. }
  244.  
  245. //----------------------------------------------------------------------------------------
  246. // CPaletteFrame::FacetAdded
  247. //----------------------------------------------------------------------------------------
  248. //    When ODWindow moves back to ODFrame we will be able to move this code
  249. //    back to the constructor
  250.  
  251. void CPaletteFrame::FacetAdded(Environment* ev, ODFacet* facet)
  252. {
  253.     // ----- Call inherited first -----
  254.     CFloatingWindowFrame::FacetAdded(ev, facet);
  255.     
  256.     // ----- Resize my Window -----
  257.     FW_CRect gridRect;
  258.     fGrid->GetExteriorGridRect(gridRect);
  259.     gridRect.Inset(-FW_IntToFixed(2), -FW_IntToFixed(2));
  260.     FW_CPoint windowSize(gridRect.Width(), gridRect.Height());
  261.     GetWindow(ev)->SetWindowSize(ev, windowSize);
  262. }
  263.  
  264. //----------------------------------------------------------------------------------------
  265. // CPaletteFrame::DoMouseDown
  266. //----------------------------------------------------------------------------------------
  267.  
  268. FW_Boolean CPaletteFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  269. {
  270.     FW_CPoint where = theMouseEvent.GetLogicalMousePosition(ev, fMapping);
  271.     
  272.     unsigned long colorIndex;    
  273.     if (fGrid->FindCell(where, colorIndex))
  274.     {
  275.         FW_CColor color;
  276.         fPalette->GetColor(colorIndex, &color);
  277.         
  278.         if (theMouseEvent.IsCopyModifier(ev))
  279.             fDrawPart->SetFrameColor(ev, color);
  280.         else
  281.             fDrawPart->SetFillColor(ev, color);    
  282.     }
  283.  
  284.     return TRUE;
  285. }
  286.  
  287.