diff src/win32/ZoomControl.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/ZoomControl.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,166 @@
     1.4 +// ZoomControl.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "ZoomControl.h"
     1.9 +
    1.10 +bool ZoomControl::isRegistered = false;
    1.11 +
    1.12 +/////////////////////////////////////////////////////////////////////////////
    1.13 +// ZoomControl
    1.14 +
    1.15 +ZoomControl::ZoomControl()
    1.16 +{
    1.17 +	ZeroMemory(colors, 3*64);
    1.18 +	selected = -1;
    1.19 +	registerClass();
    1.20 +}
    1.21 +
    1.22 +ZoomControl::~ZoomControl()
    1.23 +{}
    1.24 +
    1.25 +BEGIN_MESSAGE_MAP(ZoomControl, CWnd)
    1.26 +//{{AFX_MSG_MAP(ZoomControl)
    1.27 +ON_WM_PAINT()
    1.28 +ON_WM_LBUTTONDOWN()
    1.29 +ON_WM_ERASEBKGND()
    1.30 +//}}AFX_MSG_MAP
    1.31 +END_MESSAGE_MAP()
    1.32 +
    1.33 +/////////////////////////////////////////////////////////////////////////////
    1.34 +// ZoomControl message handlers
    1.35 +
    1.36 +void ZoomControl::registerClass()
    1.37 +{
    1.38 +	if (!isRegistered)
    1.39 +	{
    1.40 +		WNDCLASS wc;
    1.41 +		ZeroMemory(&wc, sizeof(wc));
    1.42 +		wc.style         = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
    1.43 +		wc.lpfnWndProc   = (WNDPROC) ::DefWindowProc;
    1.44 +		wc.hInstance     = AfxGetInstanceHandle();
    1.45 +		wc.hIcon         = NULL;
    1.46 +		wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    1.47 +		wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    1.48 +		wc.lpszMenuName  = NULL;
    1.49 +		wc.lpszClassName = "VbaZoomControl";
    1.50 +		AfxRegisterClass(&wc);
    1.51 +		isRegistered = true;
    1.52 +	}
    1.53 +}
    1.54 +
    1.55 +void ZoomControl::OnPaint()
    1.56 +{
    1.57 +	CPaintDC dc(this); // device context for painting
    1.58 +
    1.59 +	RECT rect;
    1.60 +	GetClientRect(&rect);
    1.61 +
    1.62 +	int w = rect.right - rect.left;
    1.63 +	int h = rect.bottom - rect.top;
    1.64 +
    1.65 +	CDC memDC ;
    1.66 +	memDC.CreateCompatibleDC(&dc);
    1.67 +	CBitmap bitmap, *pOldBitmap;
    1.68 +	bitmap.CreateCompatibleBitmap(&dc, w, h);
    1.69 +
    1.70 +	pOldBitmap = memDC.SelectObject(&bitmap);
    1.71 +
    1.72 +	int multX = w / 8;
    1.73 +	int multY = h / 8;
    1.74 +
    1.75 +	int i;
    1.76 +	for (i = 0; i < 64; i++)
    1.77 +	{
    1.78 +		CBrush b;
    1.79 +		b.CreateSolidBrush(RGB(colors[i*3+2], colors[i*3+1], colors[i*3]));
    1.80 +
    1.81 +		RECT r;
    1.82 +		int  x = i & 7;
    1.83 +		int  y = i / 8;
    1.84 +		r.top    = y*multY;
    1.85 +		r.left   = x*multX;
    1.86 +		r.bottom = r.top + multY;
    1.87 +		r.right  = r.left + multX;
    1.88 +		memDC.FillRect(&r, &b);
    1.89 +		b.DeleteObject();
    1.90 +	}
    1.91 +
    1.92 +	CPen pen;
    1.93 +	pen.CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
    1.94 +	CPen *old = (CPen *)memDC.SelectObject(&pen);
    1.95 +
    1.96 +	for (i = 0; i < 8; i++)
    1.97 +	{
    1.98 +		memDC.MoveTo(0, i * multY);
    1.99 +		memDC.LineTo(w, i * multY);
   1.100 +		memDC.MoveTo(i * multX, 0);
   1.101 +		memDC.LineTo(i * multX, h);
   1.102 +	}
   1.103 +
   1.104 +	if (selected != -1)
   1.105 +	{
   1.106 +		CPen pen2;
   1.107 +		pen2.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
   1.108 +		CPen *old2 = (CPen *)memDC.SelectObject(&pen2);
   1.109 +
   1.110 +		int startX = (selected & 7)*multX+1;
   1.111 +		int startY = (selected / 8)*multY+1;
   1.112 +		int endX   = startX + multX-2;
   1.113 +		int endY   = startY + multY-2;
   1.114 +
   1.115 +		memDC.MoveTo(startX, startY);
   1.116 +		memDC.LineTo(endX, startY);
   1.117 +		memDC.LineTo(endX, endY);
   1.118 +		memDC.LineTo(startX, endY);
   1.119 +		memDC.LineTo(startX, startY-1);
   1.120 +		memDC.SelectObject(old2);
   1.121 +		pen2.DeleteObject();
   1.122 +	}
   1.123 +	memDC.SelectObject(old);
   1.124 +	pen.DeleteObject();
   1.125 +
   1.126 +	dc.BitBlt(0, 0, w, h,
   1.127 +	          &memDC, 0, 0, SRCCOPY);
   1.128 +
   1.129 +	memDC.SelectObject(pOldBitmap);
   1.130 +	bitmap.DeleteObject();
   1.131 +	memDC.DeleteDC();
   1.132 +}
   1.133 +
   1.134 +void ZoomControl::OnLButtonDown(UINT nFlags, CPoint point)
   1.135 +{
   1.136 +	RECT rect;
   1.137 +	GetClientRect(&rect);
   1.138 +
   1.139 +	int height = rect.bottom - rect.top;
   1.140 +	int width  = rect.right - rect.left;
   1.141 +
   1.142 +	int multX = width / 8;
   1.143 +	int multY = height / 8;
   1.144 +
   1.145 +	selected = point.x / multX + 8 * (point.y / multY);
   1.146 +
   1.147 +	int c     = point.x / multX + 8 * (point.y/multY);
   1.148 +	u16 color = colors[c*3] << 7 |
   1.149 +	            colors[c*3+1] << 2 |
   1.150 +	            (colors[c*3+2] >> 3);
   1.151 +
   1.152 +	GetParent()->PostMessage(WM_COLINFO,
   1.153 +	                         color,
   1.154 +	                         0);
   1.155 +
   1.156 +	Invalidate();
   1.157 +}
   1.158 +
   1.159 +BOOL ZoomControl::OnEraseBkgnd(CDC*pDC)
   1.160 +{
   1.161 +	return TRUE;
   1.162 +}
   1.163 +
   1.164 +void ZoomControl::setColors(const u8 *c)
   1.165 +{
   1.166 +	memcpy(colors, c, 3*64);
   1.167 +	selected = -1;
   1.168 +	Invalidate();
   1.169 +}