home *** CD-ROM | disk | FTP | other *** search
- // palview.cpp : implementation of the CPalView class
- //
-
- #include "stdafx.h"
- #include "pal.h"
-
- #include "paldoc.h"
- #include "palview.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- //Allow for a system palette with 256 colors, just like Palette.c
- #define PALSIZE 256
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView
-
- IMPLEMENT_DYNCREATE(CPalView, CView)
-
- BEGIN_MESSAGE_MAP(CPalView, CView)
- //{{AFX_MSG_MAP(CPalView)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_MESSAGE(WM_PALETTECHANGED, OnPalNotify)
- ON_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONUP()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView construction/destruction
-
- CPalView::CPalView()
- {
- //intialize value to FALSE since we are not ready to capture mouse input
- bCaptured=FALSE;
-
- }
-
- CPalView::~CPalView()
- {
- //dynamically created object must be deleted
- delete m_pPal;
- }
-
-
-
-
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView drawing
-
- void CPalView::OnDraw(CDC* pDC)
- {
- CPalDoc* pDoc = GetDocument();
-
- //pointer to later save the original brush
- CBrush* pOldBrush;
- // embedded object for a new brush
- CBrush Brush;
- //embedded object for a new pen
- CPen Pen;
-
-
- int i, j, top, left, bottom, right;
- //every time you redraw you should select the palette anew
- //the palette won't be put into effect until you realize it,
- //so always pair the following two functions
- pDC->SelectPalette(m_pPal, 0);
- pDC->RealizePalette();
- //blank out the pen
- pDC->SelectStockObject(NULL_PEN);
-
-
- //the following code is taken directly out of Palette.c
- //with minor change to do thing the cpp way.
- /*
- note: the use of (long) casts here is to avoid overrange
- values for large windows
- */
-
- for (j=0, top=0; j<iYcells; j++, top=bottom) {
- bottom = (int)((long)(j+1)*(long)iHeight/(long)iYcells) + 1;
- for (i=0, left=0; i<iXcells; i++, left=right) {
- right = (int)((long)(i+1)*(long)iWidth/(long)iXcells) + 1;
- //embedded Brush object returns BOOL
- Brush.CreateSolidBrush(PALETTEINDEX(j * iXcells + i));
- //select the new brush into the DC
- //function returns the old brush
- pOldBrush = pDC->SelectObject(&Brush);
- //make a rectangle
- pDC->Rectangle(left,top, right,bottom);
- //we are done with this palette color brush
- //so put the old brush back into the DC
- pDC->SelectObject(pOldBrush);
- //you MUST call DeleteObject() before you can
- //use the Brush object again with another palette color
- Brush.DeleteObject();
- }
-
- }
- //restore the pen
- pDC->SelectStockObject(BLACK_PEN);
-
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView printing
-
- BOOL CPalView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
-
- void CPalView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
-
- void CPalView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
-
-
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView diagnostics
-
- #ifdef _DEBUG
- void CPalView::AssertValid() const
- {
- CView::AssertValid();
- }
-
- void CPalView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
-
- CPalDoc* CPalView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPalDoc)));
- return (CPalDoc*) m_pDocument;
- }
-
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CPalView message handlers
-
- BOOL CPalView::PreCreateWindow(CREATESTRUCT& cs)
- {
- ASSERT(cs.lpszClass == NULL);
- //PreCreateWindow is called after the constructor but before
- //the window is created based on the framework's default window
- //registration.
-
- // AfxRegisterWndClass returns a null terminated string containing
- // the class name (not a c++ class but a WINDOW 3.1 variety)
- // which is the WNDCLASS structure you know and love.
- // by calling AfxRegisterWndClass function before the window is
- // created, you can customize the window (most) any way you like!
- // This is where we can load our custom cursor.
- // AfxGetApp() returns a pointer that can access application specific
- // information as a pointer to class CWndApp LoadCursor() and LoadIcon()
- // are members of CWndApp.
- cs.lpszClass = AfxRegisterWndClass(CS_VREDRAW/CS_HREDRAW,
- AfxGetApp()->LoadCursor("Cursor"), (HBRUSH)(COLOR_WINDOW+1),
- AfxGetApp()->LoadIcon(IDR_MAINFRAME));
- return TRUE;
- }
-
-
-
- int CPalView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
-
-
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
-
-
-
- return 0;
- }
-
- void CPalView::OnSize(UINT nType, int cx, int cy)
- {
- //the window's coordinates have been passed to OnSize()
- //so we don't have to deal with LPARAM
- CView::OnSize(nType, cx, cy);
-
-
- /* compute the number of cells on each axis to keep aspect good */
- iXcells = get_xcells(cx, cy);
- iYcells = PALSIZE / iXcells;
- iWidth = cx;
- iHeight = cy;
-
-
- }
-
-
-
-
- LRESULT CPalView::OnPalNotify(WPARAM wParam, LPARAM)
- {
- //Since OnPalNotify is not a VISUAL C++ Generated message map function
- //it passes the old familiar WPARAM and LPARAM parameters
-
- if ((HWND)wParam != m_hWnd) {
- //When not in OnDraw, you can obtain the device context
- //with an embedded object of class CClientDC which is
- //derived from class CDC.
- CClientDC dc(this);
-
- dc.SelectPalette(m_pPal, 0);
- if (dc.RealizePalette()){
- /* some colors changed */
- Invalidate();
- MessageBeep(0);
- }
- }
- return 0;
- }
-
-
- void CPalView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- //the location of the mouse in the client area
- //of the window has been passed to us in an object (point) of
- //class CPoint. point.x contains the x coor and point.y contains
- //the y coor.
-
- SetCapture(); /* grab the mouse input */
- bCaptured = TRUE;
- // call show_pixel passing the CPoint object
- show_pixel(point); /* show pixel value here */
-
-
-
-
- CView::OnLButtonDown(nFlags, point);
- }
-
- void CPalView::OnMouseMove(UINT nFlags, CPoint point)
- {
-
- if (bCaptured) {
- // call show_pixel passing the CPoint object
- show_pixel(point); /* show pixel value here */
- }
-
-
- CView::OnMouseMove(nFlags, point);
- }
-
- void CPalView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if (bCaptured) {
- ReleaseCapture(); //We don't want the window to grab the mouse
- bCaptured = FALSE; //input anymore
-
- }
-
-
- CView::OnLButtonUp(nFlags, point);
- }
-
- void CPalView::OnDestroy()
- {
- CView::OnDestroy();
-
- if (m_pPal) {
- DeleteObject(m_pPal); //must delete pointer
- }
-
- }
-
-
-
- int CPalView::get_xcells(int xsize, int ysize)
- {
- int i, j;
-
- for (i=1; i<PALSIZE; i*=2) {
- j = PALSIZE / i;
- if (i * ysize / j >= xsize) break;
- }
- return i;
- }
-
-
-
-
- void CPalView::show_pixel(CPoint point)
- {
-
-
- DWORD rgb;
-
-
- /* get a hdc to the whole screen */
-
- // CDC* pDC= GetWindowDC();
-
- /* convert mouse coords to screen coords */
-
- CClientDC dc(this);
- // ClientToScreen(&point);
-
- /* get rgb at mouse position */
-
- rgb = dc.GetPixel(point);
-
-
-
- char buf[80];
-
-
-
- wsprintf(buf,
- "RGB(%d,%d,%d)",
- (WORD)(BYTE)GetRValue(rgb),
- (WORD)(BYTE)GetGValue(rgb),
- (WORD)(BYTE)GetBValue(rgb));
-
-
-
-
- CPalDoc* pDoc = GetDocument();
- pDoc->SetTitle(buf);
-
-
- }
-
- void CPalView::OnInitialUpdate()
- {
- m_pPal = new CPalette;
-
-
-
-
-
- hLocPal =LocalAlloc (LMEM_MOVEABLE,
- (sizeof (LOGPALETTE) +
- (sizeof (PALETTEENTRY) * (PALSIZE))));
- pLogPal=(PLOGPALETTE) LocalLock (hLocPal);
-
- pLogPal->palVersion = 0x300; /* bloody mysterious */
- pLogPal->palNumEntries = PALSIZE;
- for (int i=0; i<PALSIZE; i++) {
- pLogPal->palPalEntry[i].peRed = (BYTE)i;
- pLogPal->palPalEntry[i].peGreen = 0;
- pLogPal->palPalEntry[i].peBlue =0;
- pLogPal->palPalEntry[i].peFlags = PC_EXPLICIT;
- }
- m_pPal->CreatePalette(pLogPal);
- LocalUnlock (hLocPal);
-
- }