diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/BitmapControl.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,262 @@
     1.4 +// BitmapControl.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "BitmapControl.h"
     1.9 +
    1.10 +bool BitmapControl::isRegistered = false;
    1.11 +
    1.12 +/////////////////////////////////////////////////////////////////////////////
    1.13 +// BitmapControl
    1.14 +
    1.15 +IMPLEMENT_DYNCREATE(BitmapControl, CScrollView)
    1.16 +
    1.17 +  BitmapControl::BitmapControl()
    1.18 +{
    1.19 +  w = 0;
    1.20 +  h = 0;
    1.21 +  data = NULL;
    1.22 +  bmpInfo = NULL;
    1.23 +  stretch = false;
    1.24 +  registerClass();
    1.25 +  CSize sizeTotal;
    1.26 +  sizeTotal.cx = sizeTotal.cy = 0;
    1.27 +  SetScrollSizes(MM_TEXT, sizeTotal);
    1.28 +}
    1.29 +
    1.30 +BitmapControl::~BitmapControl()
    1.31 +{
    1.32 +}
    1.33 +
    1.34 +
    1.35 +BEGIN_MESSAGE_MAP(BitmapControl, CScrollView)
    1.36 +  //{{AFX_MSG_MAP(BitmapControl)
    1.37 +  ON_WM_ERASEBKGND()
    1.38 +  ON_WM_SIZE()
    1.39 +  ON_WM_LBUTTONDOWN()
    1.40 +  //}}AFX_MSG_MAP
    1.41 +  END_MESSAGE_MAP()
    1.42 +
    1.43 +  /////////////////////////////////////////////////////////////////////////////
    1.44 +// BitmapControl drawing
    1.45 +
    1.46 +void BitmapControl::OnInitialUpdate()
    1.47 +{
    1.48 +  CScrollView::OnInitialUpdate();
    1.49 +
    1.50 +  CSize sizeTotal;
    1.51 +  // TODO: calculate the total size of this view
    1.52 +  sizeTotal.cx = sizeTotal.cy = 100;
    1.53 +  SetScrollSizes(MM_TEXT, sizeTotal);
    1.54 +}
    1.55 +
    1.56 +void BitmapControl::OnDraw(CDC* dc)
    1.57 +{
    1.58 +  RECT r;
    1.59 +  GetClientRect(&r);
    1.60 +  int w1 = r.right - r.left;
    1.61 +  int h1 = r.bottom - r.top;
    1.62 +  CDC memDC;
    1.63 +  memDC.CreateCompatibleDC(dc);
    1.64 +  if(!stretch) {
    1.65 +    if(w > w1)
    1.66 +      w1 = w;
    1.67 +    if(h > h1)
    1.68 +      h1 = h;
    1.69 +  }
    1.70 +  CBitmap bitmap, *pOldBitmap;
    1.71 +  bitmap.CreateCompatibleBitmap(dc, w1, h1);
    1.72 +  pOldBitmap = memDC.SelectObject(&bitmap);
    1.73 +  if(stretch) {
    1.74 +    bmpInfo->bmiHeader.biWidth = w;
    1.75 +    bmpInfo->bmiHeader.biHeight = -h;
    1.76 +    
    1.77 +    StretchDIBits(memDC.GetSafeHdc(),
    1.78 +                  0,
    1.79 +                  0,
    1.80 +                  w1,
    1.81 +                  h1, 
    1.82 +                  0,
    1.83 +                  0,
    1.84 +                  w,
    1.85 +                  h,
    1.86 +                  data,
    1.87 +                  bmpInfo,
    1.88 +                  DIB_RGB_COLORS,
    1.89 +                  SRCCOPY);
    1.90 +  } else {
    1.91 +    FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE)));
    1.92 +    
    1.93 +    bmpInfo->bmiHeader.biWidth = w;
    1.94 +    bmpInfo->bmiHeader.biHeight = -h;
    1.95 +    SetDIBitsToDevice(memDC.GetSafeHdc(),
    1.96 +                      0,
    1.97 +                      0,
    1.98 +                      w,
    1.99 +                      h,
   1.100 +                      0,
   1.101 +                      0,
   1.102 +                      0,
   1.103 +                      h,
   1.104 +                      data,
   1.105 +                      bmpInfo,
   1.106 +                      DIB_RGB_COLORS);
   1.107 +  }
   1.108 +
   1.109 +  dc->BitBlt(0,0,w1,h1,
   1.110 +             &memDC,0,0,SRCCOPY);
   1.111 +  memDC.SelectObject(pOldBitmap);
   1.112 +
   1.113 +  bitmap.DeleteObject();
   1.114 +  memDC.DeleteDC();  
   1.115 +}
   1.116 +
   1.117 +/////////////////////////////////////////////////////////////////////////////
   1.118 +// BitmapControl diagnostics
   1.119 +
   1.120 +#ifdef _DEBUG
   1.121 +void BitmapControl::AssertValid() const
   1.122 +{
   1.123 +  CScrollView::AssertValid();
   1.124 +}
   1.125 +
   1.126 +void BitmapControl::Dump(CDumpContext& dc) const
   1.127 +{
   1.128 +  CScrollView::Dump(dc);
   1.129 +}
   1.130 +#endif //_DEBUG
   1.131 +
   1.132 +/////////////////////////////////////////////////////////////////////////////
   1.133 +// BitmapControl message handlers
   1.134 +
   1.135 +BOOL BitmapControl::OnEraseBkgnd(CDC* pDC) 
   1.136 +{
   1.137 +  return TRUE;
   1.138 +}
   1.139 +
   1.140 +void BitmapControl::OnSize(UINT nType, int cx, int cy) 
   1.141 +{
   1.142 +  if(!stretch)
   1.143 +    CScrollView::OnSize(nType, cx, cy);
   1.144 +}
   1.145 +
   1.146 +void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt) 
   1.147 +{
   1.148 +  if(!data)
   1.149 +    return;
   1.150 +  int x = pt.x;
   1.151 +  int y = pt.y;
   1.152 +
   1.153 +  WPARAM point;
   1.154 +  
   1.155 +  if(stretch) {
   1.156 +    RECT rect;
   1.157 +    GetClientRect(&rect);
   1.158 +  
   1.159 +    int height = rect.bottom - rect.top;
   1.160 +    int width = rect.right - rect.left;
   1.161 +  
   1.162 +    int xx = (x * w) / width;
   1.163 +    int yy = (y * h) / height;
   1.164 +
   1.165 +    point = xx | (yy<<16);
   1.166 +
   1.167 +    int xxx = xx / 8;
   1.168 +    int yyy = yy / 8;
   1.169 +
   1.170 +    for(int i = 0; i < 8; i++) {
   1.171 +      memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
   1.172 +                                   w * yyy * 8 * 3 +
   1.173 +                                   i * w * 3], 8 * 3);
   1.174 +    }
   1.175 +  } else {
   1.176 +    POINT p;
   1.177 +    p.x = GetScrollPos(SB_HORZ);
   1.178 +    p.y = GetScrollPos(SB_VERT);
   1.179 +
   1.180 +    p.x += x;
   1.181 +    p.y += y;
   1.182 +
   1.183 +    if(p.x >= w ||
   1.184 +       p.y >= h)
   1.185 +      return;
   1.186 +
   1.187 +    point = p.x | (p.y<<16);
   1.188 +    
   1.189 +    int xxx = p.x / 8;
   1.190 +    int yyy = p.y / 8;
   1.191 +
   1.192 +    for(int i = 0; i < 8; i++) {
   1.193 +      memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
   1.194 +                                   w * yyy * 8 * 3 +
   1.195 +                                   i * w * 3], 8 * 3);
   1.196 +    }
   1.197 +  }
   1.198 +  
   1.199 +  GetParent()->SendMessage(WM_MAPINFO,
   1.200 +                           point,
   1.201 +                           (LPARAM)colors);
   1.202 +}
   1.203 +
   1.204 +void BitmapControl::setBmpInfo(BITMAPINFO *info)
   1.205 +{
   1.206 +  bmpInfo = info;
   1.207 +}
   1.208 +
   1.209 +void BitmapControl::setData(u8 *d)
   1.210 +{
   1.211 +  data = d;
   1.212 +}
   1.213 +
   1.214 +void BitmapControl::setSize(int w1, int h1)
   1.215 +{
   1.216 +  if(w != w1 || h != h1) {
   1.217 +    w = w1;
   1.218 +    h = h1;
   1.219 +    SIZE s;
   1.220 +    s.cx = w;
   1.221 +    s.cy = h;
   1.222 +    SetScrollSizes(MM_TEXT, s);
   1.223 +  }
   1.224 +}
   1.225 +
   1.226 +void BitmapControl::refresh()
   1.227 +{
   1.228 +  Invalidate();
   1.229 +}
   1.230 +
   1.231 +
   1.232 +void BitmapControl::registerClass()
   1.233 +{
   1.234 +  if(!isRegistered) {
   1.235 +    WNDCLASS wc;
   1.236 +    ZeroMemory(&wc, sizeof(wc));
   1.237 +    wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
   1.238 +    wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
   1.239 +    wc.hInstance = AfxGetInstanceHandle();
   1.240 +    wc.hIcon = NULL;
   1.241 +    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   1.242 +    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   1.243 +    wc.lpszMenuName = NULL;
   1.244 +    wc.lpszClassName = "VbaBitmapControl";
   1.245 +    AfxRegisterClass(&wc);
   1.246 +    isRegistered = true;
   1.247 +  }
   1.248 +}
   1.249 +
   1.250 +void BitmapControl::setStretch(bool b)
   1.251 +{
   1.252 +  if(b != stretch) {
   1.253 +    stretch = b;
   1.254 +    Invalidate();
   1.255 +  }
   1.256 +}
   1.257 +
   1.258 +bool BitmapControl::getStretch()
   1.259 +{
   1.260 +  return stretch;
   1.261 +}
   1.262 +
   1.263 +void BitmapControl::PostNcDestroy() 
   1.264 +{
   1.265 +}