home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / palette / palview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-16  |  9.2 KB  |  375 lines

  1. // palview.cpp : implementation of the CPalView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pal.h"
  6.  
  7. #include "paldoc.h"
  8. #include "palview.h"
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. //Allow for a system palette with 256 colors, just like Palette.c
  16. #define PALSIZE 256
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CPalView
  21.  
  22. IMPLEMENT_DYNCREATE(CPalView, CView)
  23.  
  24. BEGIN_MESSAGE_MAP(CPalView, CView)
  25.     //{{AFX_MSG_MAP(CPalView)
  26.     ON_WM_CREATE()
  27.     ON_WM_SIZE()
  28.     ON_MESSAGE(WM_PALETTECHANGED, OnPalNotify)
  29.     ON_WM_LBUTTONDOWN()
  30.     ON_WM_MOUSEMOVE()
  31.     ON_WM_LBUTTONUP()
  32.     ON_WM_DESTROY()
  33.     //}}AFX_MSG_MAP
  34.     // Standard printing commands
  35.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  36.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CPalView construction/destruction
  41.  
  42. CPalView::CPalView()
  43. {
  44.   //intialize value to FALSE since we are not ready to capture mouse input
  45.     bCaptured=FALSE;
  46.     
  47. }
  48.  
  49. CPalView::~CPalView()
  50. {
  51.  //dynamically created object must be deleted 
  52.  delete m_pPal;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CPalView drawing
  62.  
  63. void CPalView::OnDraw(CDC* pDC)
  64. {
  65.     CPalDoc* pDoc = GetDocument();
  66.  
  67.     //pointer to later save the original brush
  68.     CBrush* pOldBrush;
  69.     // embedded object for a new brush
  70.     CBrush Brush;
  71.     //embedded object for a new pen
  72.     CPen Pen;
  73.    
  74.  
  75.     int i, j, top, left, bottom, right;
  76.         //every time you redraw you should select the palette anew
  77.         //the palette won't be put into effect until you realize it,
  78.         //so always pair the following two functions
  79.         pDC->SelectPalette(m_pPal, 0);
  80.         pDC->RealizePalette();
  81.         //blank out the pen
  82.         pDC->SelectStockObject(NULL_PEN);
  83.     
  84.  
  85.       //the following code is taken directly out of Palette.c
  86.       //with minor change to do thing the cpp way.
  87.     /*
  88.         note: the use of (long) casts here is to avoid overrange
  89.         values for large windows
  90.     */
  91.  
  92.     for (j=0, top=0; j<iYcells; j++, top=bottom) {
  93.         bottom = (int)((long)(j+1)*(long)iHeight/(long)iYcells) + 1;
  94.         for (i=0, left=0; i<iXcells; i++, left=right) {
  95.             right = (int)((long)(i+1)*(long)iWidth/(long)iXcells) + 1;
  96.             //embedded Brush object returns BOOL
  97.             Brush.CreateSolidBrush(PALETTEINDEX(j * iXcells + i));
  98.             //select the new brush into the DC 
  99.             //function returns the old brush 
  100.             pOldBrush = pDC->SelectObject(&Brush);
  101.             //make a rectangle
  102.             pDC->Rectangle(left,top, right,bottom);
  103.             //we are done with this palette color brush 
  104.             //so put the old brush back into the DC
  105.             pDC->SelectObject(pOldBrush);
  106.             //you MUST call DeleteObject() before you can 
  107.             //use the Brush object again with another palette color
  108.             Brush.DeleteObject();
  109.            }
  110.    
  111.     }
  112.     //restore the pen 
  113.     pDC->SelectStockObject(BLACK_PEN);
  114.  
  115. }
  116.  
  117. /////////////////////////////////////////////////////////////////////////////
  118. // CPalView printing
  119.  
  120. BOOL CPalView::OnPreparePrinting(CPrintInfo* pInfo)
  121. {
  122.     // default preparation
  123.     return DoPreparePrinting(pInfo);
  124. }
  125.  
  126. void CPalView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  127. {
  128.     // TODO: add extra initialization before printing
  129. }
  130.  
  131. void CPalView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  132. {
  133.     // TODO: add cleanup after printing
  134. }
  135.  
  136.  
  137.  
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CPalView diagnostics
  141.  
  142. #ifdef _DEBUG
  143. void CPalView::AssertValid() const
  144. {
  145.     CView::AssertValid();
  146. }
  147.  
  148. void CPalView::Dump(CDumpContext& dc) const
  149. {
  150.     CView::Dump(dc);
  151. }
  152.  
  153. CPalDoc* CPalView::GetDocument() // non-debug version is inline
  154. {
  155.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPalDoc)));
  156.     return (CPalDoc*) m_pDocument;
  157. }
  158.  
  159. #endif //_DEBUG
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162. // CPalView message handlers
  163.  
  164. BOOL CPalView::PreCreateWindow(CREATESTRUCT& cs)
  165. {
  166.     ASSERT(cs.lpszClass == NULL);      
  167.      //PreCreateWindow is called after the constructor but before 
  168.      //the window is created based on the framework's default window
  169.      //registration.
  170.      
  171.     // AfxRegisterWndClass returns a null terminated string containing
  172.     // the class name (not a c++ class but a WINDOW 3.1 variety) 
  173.     // which is the WNDCLASS structure you know and love.
  174.     // by calling AfxRegisterWndClass function before the window is 
  175.     // created, you can customize the window (most) any way you like!
  176.     // This is where we can load our custom cursor.
  177.     // AfxGetApp() returns a pointer that can access application specific 
  178.     // information as a pointer to class CWndApp LoadCursor() and LoadIcon()
  179.     // are members of CWndApp.    
  180.     cs.lpszClass = AfxRegisterWndClass(CS_VREDRAW/CS_HREDRAW,
  181.         AfxGetApp()->LoadCursor("Cursor"), (HBRUSH)(COLOR_WINDOW+1),
  182.         AfxGetApp()->LoadIcon(IDR_MAINFRAME));
  183.     return TRUE;
  184. }
  185.  
  186.  
  187.  
  188. int CPalView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  189. {
  190.  
  191.     
  192.     if (CView::OnCreate(lpCreateStruct) == -1)
  193.         return -1;
  194.         
  195.     
  196.  
  197.  
  198.     return 0;
  199. }
  200.  
  201. void CPalView::OnSize(UINT nType, int cx, int cy)
  202. {
  203.     //the window's coordinates have been passed to OnSize() 
  204.     //so we don't have to deal with LPARAM 
  205.     CView::OnSize(nType, cx, cy);
  206.         
  207.           
  208.        /* compute the number of cells on each axis to keep aspect good */
  209.         iXcells = get_xcells(cx, cy);
  210.         iYcells = PALSIZE / iXcells;
  211.         iWidth = cx;
  212.         iHeight = cy;
  213.  
  214.     
  215. }
  216.  
  217.  
  218.  
  219.  
  220. LRESULT CPalView::OnPalNotify(WPARAM wParam, LPARAM)
  221. {
  222.        //Since OnPalNotify is not a VISUAL C++ Generated message map function
  223.        //it passes the old familiar WPARAM and LPARAM parameters
  224.           
  225.          if ((HWND)wParam != m_hWnd) {
  226.               //When not in OnDraw, you can obtain the device context
  227.               //with an embedded object of class CClientDC  which is
  228.               //derived from class CDC.
  229.               CClientDC dc(this);
  230.               
  231.               dc.SelectPalette(m_pPal, 0);
  232.               if (dc.RealizePalette()){
  233.                 /* some colors changed */
  234.                 Invalidate();
  235.                 MessageBeep(0);
  236.                }
  237.          }
  238.   return 0;
  239. }
  240.  
  241.  
  242. void CPalView::OnLButtonDown(UINT nFlags, CPoint point)
  243. {
  244.         //the location of the mouse in the client area
  245.         //of the window has been passed to us in an object (point) of
  246.         //class CPoint. point.x contains the x coor and point.y contains
  247.         //the y coor.   
  248.         
  249.         SetCapture();       /* grab the mouse input */
  250.         bCaptured = TRUE;
  251.         // call show_pixel passing the CPoint object
  252.         show_pixel(point);     /* show pixel value here */
  253.  
  254.        
  255.  
  256.     
  257.     CView::OnLButtonDown(nFlags, point);
  258. }
  259.  
  260. void CPalView::OnMouseMove(UINT nFlags, CPoint point)
  261. {      
  262.       
  263.            if (bCaptured) {
  264.             // call show_pixel passing the CPoint object
  265.             show_pixel(point); /* show pixel value here */
  266.         }
  267.  
  268.     
  269.     CView::OnMouseMove(nFlags, point);
  270. }
  271.  
  272. void CPalView::OnLButtonUp(UINT nFlags, CPoint point)
  273. {
  274.             if (bCaptured) {
  275.             ReleaseCapture();  //We don't want the window to grab the mouse
  276.             bCaptured = FALSE; //input anymore
  277.            
  278.         }
  279.  
  280.     
  281.     CView::OnLButtonUp(nFlags, point);
  282. }
  283.  
  284. void CPalView::OnDestroy()
  285. {
  286.     CView::OnDestroy();
  287.     
  288.             if (m_pPal) {
  289.             DeleteObject(m_pPal); //must delete pointer
  290.         }
  291.  
  292. }
  293.  
  294.  
  295.  
  296. int CPalView::get_xcells(int xsize, int ysize)
  297. {
  298.     int i, j;
  299.  
  300.     for (i=1; i<PALSIZE; i*=2) {
  301.         j = PALSIZE / i;
  302.         if (i * ysize / j >= xsize) break;
  303.     }
  304.     return i;
  305. }
  306.  
  307.  
  308.  
  309.  
  310. void CPalView::show_pixel(CPoint point)
  311. {
  312.    
  313.     
  314.     DWORD rgb;
  315.    
  316.  
  317.     /* get a hdc to the whole screen */
  318.  
  319.   //  CDC* pDC= GetWindowDC();
  320.  
  321.     /* convert mouse coords to screen coords */
  322.  
  323.     CClientDC dc(this); 
  324.  //   ClientToScreen(&point);
  325.  
  326.     /* get rgb at mouse position */
  327.  
  328.     rgb = dc.GetPixel(point);
  329.  
  330.   
  331.    
  332.       char buf[80];
  333.    
  334.  
  335.  
  336.       wsprintf(buf,
  337.             "RGB(%d,%d,%d)",
  338.              (WORD)(BYTE)GetRValue(rgb),
  339.              (WORD)(BYTE)GetGValue(rgb),
  340.              (WORD)(BYTE)GetBValue(rgb));
  341.  
  342.  
  343.     
  344.     
  345.  CPalDoc* pDoc = GetDocument();
  346.   pDoc->SetTitle(buf);
  347.  
  348.  
  349. }
  350.  
  351.   void CPalView::OnInitialUpdate()
  352.  { 
  353.        m_pPal = new CPalette;
  354.        
  355.        
  356.        
  357.        
  358.  
  359.         hLocPal =LocalAlloc (LMEM_MOVEABLE, 
  360.                    (sizeof (LOGPALETTE) + 
  361.                      (sizeof (PALETTEENTRY) * (PALSIZE))));
  362.         pLogPal=(PLOGPALETTE) LocalLock (hLocPal);
  363.         
  364.         pLogPal->palVersion = 0x300; /* bloody mysterious */
  365.         pLogPal->palNumEntries = PALSIZE;
  366.         for (int i=0; i<PALSIZE; i++) {
  367.             pLogPal->palPalEntry[i].peRed = (BYTE)i;
  368.             pLogPal->palPalEntry[i].peGreen = 0;
  369.             pLogPal->palPalEntry[i].peBlue =0;
  370.             pLogPal->palPalEntry[i].peFlags = PC_EXPLICIT;
  371.         }
  372.         m_pPal->CreatePalette(pLogPal);
  373.         LocalUnlock (hLocPal);
  374.  
  375.  }