view src/win32/ZoomControl.cpp @ 7:c0a590a394c3

ignore generated files
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:50:33 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // ZoomControl.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "ZoomControl.h"
7 bool ZoomControl::isRegistered = false;
9 /////////////////////////////////////////////////////////////////////////////
10 // ZoomControl
12 ZoomControl::ZoomControl()
13 {
14 ZeroMemory(colors, 3*64);
15 selected = -1;
16 registerClass();
17 }
19 ZoomControl::~ZoomControl()
20 {}
22 BEGIN_MESSAGE_MAP(ZoomControl, CWnd)
23 //{{AFX_MSG_MAP(ZoomControl)
24 ON_WM_PAINT()
25 ON_WM_LBUTTONDOWN()
26 ON_WM_ERASEBKGND()
27 //}}AFX_MSG_MAP
28 END_MESSAGE_MAP()
30 /////////////////////////////////////////////////////////////////////////////
31 // ZoomControl message handlers
33 void ZoomControl::registerClass()
34 {
35 if (!isRegistered)
36 {
37 WNDCLASS wc;
38 ZeroMemory(&wc, sizeof(wc));
39 wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
40 wc.lpfnWndProc = (WNDPROC) ::DefWindowProc;
41 wc.hInstance = AfxGetInstanceHandle();
42 wc.hIcon = NULL;
43 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
44 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
45 wc.lpszMenuName = NULL;
46 wc.lpszClassName = "VbaZoomControl";
47 AfxRegisterClass(&wc);
48 isRegistered = true;
49 }
50 }
52 void ZoomControl::OnPaint()
53 {
54 CPaintDC dc(this); // device context for painting
56 RECT rect;
57 GetClientRect(&rect);
59 int w = rect.right - rect.left;
60 int h = rect.bottom - rect.top;
62 CDC memDC ;
63 memDC.CreateCompatibleDC(&dc);
64 CBitmap bitmap, *pOldBitmap;
65 bitmap.CreateCompatibleBitmap(&dc, w, h);
67 pOldBitmap = memDC.SelectObject(&bitmap);
69 int multX = w / 8;
70 int multY = h / 8;
72 int i;
73 for (i = 0; i < 64; i++)
74 {
75 CBrush b;
76 b.CreateSolidBrush(RGB(colors[i*3+2], colors[i*3+1], colors[i*3]));
78 RECT r;
79 int x = i & 7;
80 int y = i / 8;
81 r.top = y*multY;
82 r.left = x*multX;
83 r.bottom = r.top + multY;
84 r.right = r.left + multX;
85 memDC.FillRect(&r, &b);
86 b.DeleteObject();
87 }
89 CPen pen;
90 pen.CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
91 CPen *old = (CPen *)memDC.SelectObject(&pen);
93 for (i = 0; i < 8; i++)
94 {
95 memDC.MoveTo(0, i * multY);
96 memDC.LineTo(w, i * multY);
97 memDC.MoveTo(i * multX, 0);
98 memDC.LineTo(i * multX, h);
99 }
101 if (selected != -1)
102 {
103 CPen pen2;
104 pen2.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
105 CPen *old2 = (CPen *)memDC.SelectObject(&pen2);
107 int startX = (selected & 7)*multX+1;
108 int startY = (selected / 8)*multY+1;
109 int endX = startX + multX-2;
110 int endY = startY + multY-2;
112 memDC.MoveTo(startX, startY);
113 memDC.LineTo(endX, startY);
114 memDC.LineTo(endX, endY);
115 memDC.LineTo(startX, endY);
116 memDC.LineTo(startX, startY-1);
117 memDC.SelectObject(old2);
118 pen2.DeleteObject();
119 }
120 memDC.SelectObject(old);
121 pen.DeleteObject();
123 dc.BitBlt(0, 0, w, h,
124 &memDC, 0, 0, SRCCOPY);
126 memDC.SelectObject(pOldBitmap);
127 bitmap.DeleteObject();
128 memDC.DeleteDC();
129 }
131 void ZoomControl::OnLButtonDown(UINT nFlags, CPoint point)
132 {
133 RECT rect;
134 GetClientRect(&rect);
136 int height = rect.bottom - rect.top;
137 int width = rect.right - rect.left;
139 int multX = width / 8;
140 int multY = height / 8;
142 selected = point.x / multX + 8 * (point.y / multY);
144 int c = point.x / multX + 8 * (point.y/multY);
145 u16 color = colors[c*3] << 7 |
146 colors[c*3+1] << 2 |
147 (colors[c*3+2] >> 3);
149 GetParent()->PostMessage(WM_COLINFO,
150 color,
151 0);
153 Invalidate();
154 }
156 BOOL ZoomControl::OnEraseBkgnd(CDC*pDC)
157 {
158 return TRUE;
159 }
161 void ZoomControl::setColors(const u8 *c)
162 {
163 memcpy(colors, c, 3*64);
164 selected = -1;
165 Invalidate();
166 }