view src/win32/BitmapControl.cpp @ 3:b05d00f19d80

fix some formatting
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:33:11 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // BitmapControl.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "BitmapControl.h"
7 bool BitmapControl::isRegistered = false;
9 /////////////////////////////////////////////////////////////////////////////
10 // BitmapControl
12 IMPLEMENT_DYNCREATE(BitmapControl, CScrollView)
14 BitmapControl::BitmapControl()
15 {
16 w = 0;
17 h = 0;
18 data = NULL;
19 bmpInfo = NULL;
20 stretch = false;
21 registerClass();
22 CSize sizeTotal;
23 sizeTotal.cx = sizeTotal.cy = 0;
24 SetScrollSizes(MM_TEXT, sizeTotal);
25 }
27 BitmapControl::~BitmapControl()
28 {
29 }
32 BEGIN_MESSAGE_MAP(BitmapControl, CScrollView)
33 //{{AFX_MSG_MAP(BitmapControl)
34 ON_WM_ERASEBKGND()
35 ON_WM_SIZE()
36 ON_WM_LBUTTONDOWN()
37 //}}AFX_MSG_MAP
38 END_MESSAGE_MAP()
40 /////////////////////////////////////////////////////////////////////////////
41 // BitmapControl drawing
43 void BitmapControl::OnInitialUpdate()
44 {
45 CScrollView::OnInitialUpdate();
47 CSize sizeTotal;
48 // TODO: calculate the total size of this view
49 sizeTotal.cx = sizeTotal.cy = 100;
50 SetScrollSizes(MM_TEXT, sizeTotal);
51 }
53 void BitmapControl::OnDraw(CDC* dc)
54 {
55 RECT r;
56 GetClientRect(&r);
57 int w1 = r.right - r.left;
58 int h1 = r.bottom - r.top;
59 CDC memDC;
60 memDC.CreateCompatibleDC(dc);
61 if(!stretch) {
62 if(w > w1)
63 w1 = w;
64 if(h > h1)
65 h1 = h;
66 }
67 CBitmap bitmap, *pOldBitmap;
68 bitmap.CreateCompatibleBitmap(dc, w1, h1);
69 pOldBitmap = memDC.SelectObject(&bitmap);
70 if(stretch) {
71 bmpInfo->bmiHeader.biWidth = w;
72 bmpInfo->bmiHeader.biHeight = -h;
74 StretchDIBits(memDC.GetSafeHdc(),
75 0,
76 0,
77 w1,
78 h1,
79 0,
80 0,
81 w,
82 h,
83 data,
84 bmpInfo,
85 DIB_RGB_COLORS,
86 SRCCOPY);
87 } else {
88 FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE)));
90 bmpInfo->bmiHeader.biWidth = w;
91 bmpInfo->bmiHeader.biHeight = -h;
92 SetDIBitsToDevice(memDC.GetSafeHdc(),
93 0,
94 0,
95 w,
96 h,
97 0,
98 0,
99 0,
100 h,
101 data,
102 bmpInfo,
103 DIB_RGB_COLORS);
104 }
106 dc->BitBlt(0,0,w1,h1,
107 &memDC,0,0,SRCCOPY);
108 memDC.SelectObject(pOldBitmap);
110 bitmap.DeleteObject();
111 memDC.DeleteDC();
112 }
114 /////////////////////////////////////////////////////////////////////////////
115 // BitmapControl diagnostics
117 #ifdef _DEBUG
118 void BitmapControl::AssertValid() const
119 {
120 CScrollView::AssertValid();
121 }
123 void BitmapControl::Dump(CDumpContext& dc) const
124 {
125 CScrollView::Dump(dc);
126 }
127 #endif //_DEBUG
129 /////////////////////////////////////////////////////////////////////////////
130 // BitmapControl message handlers
132 BOOL BitmapControl::OnEraseBkgnd(CDC* pDC)
133 {
134 return TRUE;
135 }
137 void BitmapControl::OnSize(UINT nType, int cx, int cy)
138 {
139 if(!stretch)
140 CScrollView::OnSize(nType, cx, cy);
141 }
143 void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt)
144 {
145 if(!data)
146 return;
147 int x = pt.x;
148 int y = pt.y;
150 WPARAM point;
152 if(stretch) {
153 RECT rect;
154 GetClientRect(&rect);
156 int height = rect.bottom - rect.top;
157 int width = rect.right - rect.left;
159 int xx = (x * w) / width;
160 int yy = (y * h) / height;
162 point = xx | (yy<<16);
164 int xxx = xx / 8;
165 int yyy = yy / 8;
167 for(int i = 0; i < 8; i++) {
168 memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
169 w * yyy * 8 * 3 +
170 i * w * 3], 8 * 3);
171 }
172 } else {
173 POINT p;
174 p.x = GetScrollPos(SB_HORZ);
175 p.y = GetScrollPos(SB_VERT);
177 p.x += x;
178 p.y += y;
180 if(p.x >= w ||
181 p.y >= h)
182 return;
184 point = p.x | (p.y<<16);
186 int xxx = p.x / 8;
187 int yyy = p.y / 8;
189 for(int i = 0; i < 8; i++) {
190 memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
191 w * yyy * 8 * 3 +
192 i * w * 3], 8 * 3);
193 }
194 }
196 GetParent()->SendMessage(WM_MAPINFO,
197 point,
198 (LPARAM)colors);
199 }
201 void BitmapControl::setBmpInfo(BITMAPINFO *info)
202 {
203 bmpInfo = info;
204 }
206 void BitmapControl::setData(u8 *d)
207 {
208 data = d;
209 }
211 void BitmapControl::setSize(int w1, int h1)
212 {
213 if(w != w1 || h != h1) {
214 w = w1;
215 h = h1;
216 SIZE s;
217 s.cx = w;
218 s.cy = h;
219 SetScrollSizes(MM_TEXT, s);
220 }
221 }
223 void BitmapControl::refresh()
224 {
225 Invalidate();
226 }
229 void BitmapControl::registerClass()
230 {
231 if(!isRegistered) {
232 WNDCLASS wc;
233 ZeroMemory(&wc, sizeof(wc));
234 wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
235 wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
236 wc.hInstance = AfxGetInstanceHandle();
237 wc.hIcon = NULL;
238 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
239 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
240 wc.lpszMenuName = NULL;
241 wc.lpszClassName = "VbaBitmapControl";
242 AfxRegisterClass(&wc);
243 isRegistered = true;
244 }
245 }
247 void BitmapControl::setStretch(bool b)
248 {
249 if(b != stretch) {
250 stretch = b;
251 Invalidate();
252 }
253 }
255 bool BitmapControl::getStretch()
256 {
257 return stretch;
258 }
260 void BitmapControl::PostNcDestroy()
261 {
262 }