comparison src/win32/BitmapControl.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
comparison
equal deleted inserted replaced
0:8ced16adf2e1 1:f9f4f1b99eed
1 // BitmapControl.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "BitmapControl.h"
6
7 bool BitmapControl::isRegistered = false;
8
9 /////////////////////////////////////////////////////////////////////////////
10 // BitmapControl
11
12 IMPLEMENT_DYNCREATE(BitmapControl, CScrollView)
13
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 }
26
27 BitmapControl::~BitmapControl()
28 {
29 }
30
31
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()
39
40 /////////////////////////////////////////////////////////////////////////////
41 // BitmapControl drawing
42
43 void BitmapControl::OnInitialUpdate()
44 {
45 CScrollView::OnInitialUpdate();
46
47 CSize sizeTotal;
48 // TODO: calculate the total size of this view
49 sizeTotal.cx = sizeTotal.cy = 100;
50 SetScrollSizes(MM_TEXT, sizeTotal);
51 }
52
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;
73
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)));
89
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 }
105
106 dc->BitBlt(0,0,w1,h1,
107 &memDC,0,0,SRCCOPY);
108 memDC.SelectObject(pOldBitmap);
109
110 bitmap.DeleteObject();
111 memDC.DeleteDC();
112 }
113
114 /////////////////////////////////////////////////////////////////////////////
115 // BitmapControl diagnostics
116
117 #ifdef _DEBUG
118 void BitmapControl::AssertValid() const
119 {
120 CScrollView::AssertValid();
121 }
122
123 void BitmapControl::Dump(CDumpContext& dc) const
124 {
125 CScrollView::Dump(dc);
126 }
127 #endif //_DEBUG
128
129 /////////////////////////////////////////////////////////////////////////////
130 // BitmapControl message handlers
131
132 BOOL BitmapControl::OnEraseBkgnd(CDC* pDC)
133 {
134 return TRUE;
135 }
136
137 void BitmapControl::OnSize(UINT nType, int cx, int cy)
138 {
139 if(!stretch)
140 CScrollView::OnSize(nType, cx, cy);
141 }
142
143 void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt)
144 {
145 if(!data)
146 return;
147 int x = pt.x;
148 int y = pt.y;
149
150 WPARAM point;
151
152 if(stretch) {
153 RECT rect;
154 GetClientRect(&rect);
155
156 int height = rect.bottom - rect.top;
157 int width = rect.right - rect.left;
158
159 int xx = (x * w) / width;
160 int yy = (y * h) / height;
161
162 point = xx | (yy<<16);
163
164 int xxx = xx / 8;
165 int yyy = yy / 8;
166
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);
176
177 p.x += x;
178 p.y += y;
179
180 if(p.x >= w ||
181 p.y >= h)
182 return;
183
184 point = p.x | (p.y<<16);
185
186 int xxx = p.x / 8;
187 int yyy = p.y / 8;
188
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 }
195
196 GetParent()->SendMessage(WM_MAPINFO,
197 point,
198 (LPARAM)colors);
199 }
200
201 void BitmapControl::setBmpInfo(BITMAPINFO *info)
202 {
203 bmpInfo = info;
204 }
205
206 void BitmapControl::setData(u8 *d)
207 {
208 data = d;
209 }
210
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 }
222
223 void BitmapControl::refresh()
224 {
225 Invalidate();
226 }
227
228
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 }
246
247 void BitmapControl::setStretch(bool b)
248 {
249 if(b != stretch) {
250 stretch = b;
251 Invalidate();
252 }
253 }
254
255 bool BitmapControl::getStretch()
256 {
257 return stretch;
258 }
259
260 void BitmapControl::PostNcDestroy()
261 {
262 }