home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / ZoomControl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  4.5 KB  |  191 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // ZoomControl.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "ZoomControl.h"
  25.  
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. bool ZoomControl::isRegistered = false;
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // ZoomControl
  36.  
  37. ZoomControl::ZoomControl()
  38. {
  39.   ZeroMemory(colors, 3*64);
  40.   selected = -1;
  41.   registerClass();
  42. }
  43.  
  44. ZoomControl::~ZoomControl()
  45. {
  46. }
  47.  
  48.  
  49. BEGIN_MESSAGE_MAP(ZoomControl, CWnd)
  50.   //{{AFX_MSG_MAP(ZoomControl)
  51.   ON_WM_PAINT()
  52.   ON_WM_LBUTTONDOWN()
  53.   ON_WM_ERASEBKGND()
  54.   //}}AFX_MSG_MAP
  55.   END_MESSAGE_MAP()
  56.  
  57.  
  58.   /////////////////////////////////////////////////////////////////////////////
  59. // ZoomControl message handlers
  60.  
  61. void ZoomControl::registerClass()
  62. {
  63.   if(!isRegistered) {
  64.     WNDCLASS wc;
  65.     ZeroMemory(&wc, sizeof(wc));
  66.     wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
  67.     wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
  68.     wc.hInstance = AfxGetInstanceHandle();
  69.     wc.hIcon = NULL;
  70.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  71.     wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
  72.     wc.lpszMenuName = NULL;
  73.     wc.lpszClassName = "VbaZoomControl";
  74.     AfxRegisterClass(&wc);
  75.     isRegistered = true;    
  76.   }
  77. }
  78.  
  79. void ZoomControl::OnPaint() 
  80. {
  81.   CPaintDC dc(this); // device context for painting
  82.   
  83.   RECT rect;
  84.   GetClientRect(&rect);
  85.  
  86.   int w = rect.right - rect.left;
  87.   int h = rect.bottom - rect.top;
  88.   
  89.   CDC memDC ;
  90.   memDC.CreateCompatibleDC(&dc);
  91.   CBitmap bitmap, *pOldBitmap;
  92.   bitmap.CreateCompatibleBitmap(&dc, w, h);
  93.  
  94.   pOldBitmap = memDC.SelectObject(&bitmap);
  95.   
  96.   int multX = w / 8;
  97.   int multY = h / 8;
  98.  
  99.   int i;
  100.   for(i = 0; i < 64; i++) {
  101.     CBrush b;
  102.     b.CreateSolidBrush(RGB(colors[i*3+2], colors[i*3+1], colors[i*3]));
  103.  
  104.     RECT r;
  105.     int x = i & 7;
  106.     int y = i / 8;
  107.     r.top = y*multY;
  108.     r.left = x*multX;
  109.     r.bottom = r.top + multY;
  110.     r.right = r.left + multX;
  111.     memDC.FillRect(&r, &b);
  112.     b.DeleteObject();
  113.   }
  114.  
  115.   CPen pen;
  116.   pen.CreatePen(PS_SOLID, 1, RGB(192,192,192));
  117.   CPen *old = (CPen *)memDC.SelectObject(&pen);
  118.  
  119.   for(i = 0; i < 8; i++) {
  120.     memDC.MoveTo(0, i * multY);
  121.     memDC.LineTo(w, i * multY);
  122.     memDC.MoveTo(i * multX, 0);
  123.     memDC.LineTo(i * multX, h);
  124.   }
  125.  
  126.   if(selected != -1) {
  127.     CPen pen2;
  128.     pen2.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  129.     CPen *old2 = (CPen*)memDC.SelectObject(&pen2);
  130.  
  131.     int startX = (selected & 7)*multX+1;
  132.     int startY = (selected / 8)*multY+1;
  133.     int endX = startX + multX-2;
  134.     int endY = startY + multY-2;
  135.     
  136.     memDC.MoveTo(startX, startY);
  137.     memDC.LineTo(endX, startY);
  138.     memDC.LineTo(endX, endY);
  139.     memDC.LineTo(startX, endY);
  140.     memDC.LineTo(startX, startY-1);
  141.     memDC.SelectObject(old2);
  142.     pen2.DeleteObject();
  143.   }
  144.   memDC.SelectObject(old);
  145.   pen.DeleteObject();
  146.  
  147.   dc.BitBlt(0,0,w,h,
  148.             &memDC,0,0, SRCCOPY);
  149.  
  150.   memDC.SelectObject(pOldBitmap);
  151.   bitmap.DeleteObject();
  152.   memDC.DeleteDC();
  153. }
  154.  
  155. void ZoomControl::OnLButtonDown(UINT nFlags, CPoint point) 
  156. {
  157.   RECT rect;
  158.   GetClientRect(&rect);
  159.   
  160.   int height = rect.bottom - rect.top;
  161.   int width = rect.right - rect.left;
  162.  
  163.   int multX = width / 8;
  164.   int multY = height / 8;
  165.  
  166.   selected = point.x / multX + 8 * (point.y / multY);
  167.   
  168.   int c = point.x / multX + 8 * (point.y/multY);
  169.   u16 color = colors[c*3] << 7 |
  170.     colors[c*3+1] << 2 |
  171.     (colors[c*3+2] >> 3);
  172.   
  173.   GetParent()->PostMessage(WM_COLINFO,
  174.                            color,
  175.                            0);
  176.  
  177.   Invalidate();
  178. }
  179.  
  180. BOOL ZoomControl::OnEraseBkgnd(CDC* pDC) 
  181. {
  182.   return TRUE;
  183. }
  184.  
  185. void ZoomControl::setColors(const u8 *c)
  186. {
  187.   memcpy(colors, c, 3*64);
  188.   selected = -1;
  189.   Invalidate();
  190. }
  191.