home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / skinButton.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  7.4 KB  |  329 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. // skinButton.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "skinButton.h"
  25.  
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. extern bool winAccelGetID(const char *command, WORD& id);
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // SkinButton
  36.  
  37. SkinButton::SkinButton()
  38. {
  39.   normalBmp = NULL;
  40.   downBmp = NULL;
  41.   overBmp = NULL;
  42.   mouseOver = false;
  43.   id = "";
  44.   idCommand = 0;
  45.   region = NULL;
  46.   buttonMask = 0;
  47.   menu = -1;
  48. }
  49.  
  50. SkinButton::~SkinButton()
  51. {
  52.   DestroyWindow();
  53.   if(normalBmp) {
  54.     DeleteObject(normalBmp);
  55.     normalBmp = NULL;
  56.   }
  57.   if(downBmp) {
  58.     DeleteObject(downBmp);
  59.     downBmp = NULL;
  60.   }
  61.   if(overBmp) {
  62.     DeleteObject(overBmp);
  63.     overBmp = NULL;
  64.   }
  65.   if(region) {
  66.     DeleteObject(region);
  67.     region = NULL;
  68.   }
  69. }
  70.  
  71.  
  72. BEGIN_MESSAGE_MAP(SkinButton, CWnd)
  73.   //{{AFX_MSG_MAP(SkinButton)
  74.   ON_WM_ERASEBKGND()
  75.   ON_WM_PAINT()
  76.   ON_WM_KILLFOCUS()
  77.   ON_WM_CAPTURECHANGED()
  78.   ON_WM_CONTEXTMENU()
  79.   //}}AFX_MSG_MAP
  80.   ON_MESSAGE(WM_LBUTTONUP, OnLButtonUpMsg)
  81.   ON_MESSAGE(WM_LBUTTONDOWN, OnLButtonDownMsg)
  82.   ON_MESSAGE(WM_MOUSEMOVE, OnMouseMoveMsg)
  83.   ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeaveMsg)
  84.   END_MESSAGE_MAP()
  85.  
  86.  
  87.   /////////////////////////////////////////////////////////////////////////////
  88. // SkinButton message handlers
  89.  
  90. BOOL SkinButton::OnEraseBkgnd(CDC* pDC) 
  91. {
  92.   return TRUE;
  93. }
  94.  
  95. void SkinButton::OnPaint() 
  96. {
  97.   PAINTSTRUCT ps;
  98.   HDC hDC = ::BeginPaint(m_hWnd, &ps);
  99.   HDC memDC = ::CreateCompatibleDC(hDC);
  100.   UINT state = ::SendMessage(m_hWnd, BM_GETSTATE, 0, 0);
  101.   HBITMAP oldBitmap;
  102.   if(state & BST_PUSHED)
  103.     oldBitmap = (HBITMAP)SelectObject(memDC, downBmp);
  104.   else if(mouseOver && overBmp != NULL)
  105.     oldBitmap = (HBITMAP)SelectObject(memDC, overBmp);
  106.   else
  107.     oldBitmap = (HBITMAP)SelectObject(memDC, normalBmp);
  108.   SelectClipRgn(hDC, region);
  109.   BitBlt(hDC, 0, 0, theApp.rect.right - theApp.rect.left,
  110.          theApp.rect.bottom - theApp.rect.top, memDC, 0, 0, SRCCOPY);
  111.   SelectClipRgn(hDC, NULL);
  112.   SelectObject(memDC, oldBitmap);
  113.   DeleteDC(memDC);
  114.  
  115.   ::EndPaint(m_hWnd, &ps);
  116. }
  117.  
  118. LRESULT SkinButton::OnLButtonUpMsg(WPARAM wParam, LPARAM lParam)
  119. {
  120.   POINT pt;
  121.   pt.x = LOWORD(lParam);
  122.   pt.y = HIWORD(lParam);
  123.   RECT r;
  124.   GetClientRect(&r);
  125.   BOOL inside = PtInRect(&r, pt);
  126.   if(region != NULL)
  127.     inside &= PtInRegion(region, pt.x, pt.y);
  128.   if(inside) {
  129.     HWND hWnd = m_hWnd;
  130.     if(idCommand != 0)
  131.       GetParent()->SendMessage(WM_COMMAND, idCommand, 0);
  132.     else if(buttonMask)
  133.       theApp.skinButtons = 0;
  134.     else if(menu != -1) {
  135.       HMENU m = GetSubMenu(theApp.menu, menu);
  136.       pt.x = r.left;
  137.       pt.y = r.bottom;
  138.       ClientToScreen(&pt);
  139.       theApp.m_pMainWnd->SendMessage(WM_INITMENUPOPUP, (WPARAM)m, menu);
  140.       TrackPopupMenu(m, 0, pt.x, pt.y, 0, *theApp.m_pMainWnd, NULL);
  141.     }
  142.  
  143.     return ::DefWindowProc(hWnd, WM_LBUTTONUP, wParam, lParam);
  144.   }
  145.   return GetParent()->SendMessage(WM_LBUTTONUP, wParam, lParam);
  146. }
  147.  
  148. LRESULT SkinButton::OnLButtonDownMsg(WPARAM wParam, LPARAM lParam)
  149. {
  150.   if(idCommand != 0)
  151.     return Default();
  152.  
  153.   POINT pt;
  154.   pt.x = LOWORD(lParam);
  155.   pt.y = HIWORD(lParam);
  156.   RECT r;
  157.   GetClientRect(&r);
  158.   BOOL inside = PtInRect(&r, pt);
  159.   if(region != NULL)
  160.     inside &= PtInRegion(region, pt.x, pt.y);
  161.   if(inside) {
  162.     if(buttonMask)
  163.       theApp.skinButtons = buttonMask;
  164.     return Default();
  165.   }
  166.   return GetParent()->SendMessage(WM_LBUTTONDOWN, wParam, lParam);
  167. }
  168.  
  169. LRESULT SkinButton::OnMouseMoveMsg(WPARAM wParam, LPARAM lParam)
  170. {
  171.   if(wParam & MK_LBUTTON && !mouseOver)
  172.     return Default();
  173.  
  174.   if(GetCapture() != this) {
  175.     SetCapture();
  176.   }
  177.   POINT pt;
  178.   pt.x = LOWORD(lParam);
  179.   pt.y = HIWORD(lParam);
  180.   //  ClientToScreen(getHandle(), &p);
  181.   RECT r;
  182.   GetClientRect(&r);
  183.   BOOL inside = PtInRect(&r, pt);
  184.   if(region != NULL)
  185.     inside &= PtInRegion(region, pt.x, pt.y);
  186.  
  187.   if(!inside) {
  188.     //  HWND h = WindowFromPoint(p);
  189.     //  if(h != getHandle()) {
  190.     if(mouseOver) {
  191.       mouseOver = false;
  192.       Invalidate();
  193.     }
  194.     if(!(wParam & MK_LBUTTON))
  195.       ReleaseCapture();
  196.   } else {
  197.     if(!mouseOver) {
  198.       mouseOver = true;
  199.       Invalidate();
  200.     }
  201.   }
  202.   return Default();
  203. }
  204.  
  205. void SkinButton::OnKillFocus(CWnd* pNewWnd) 
  206. {
  207.   mouseOver = false;
  208.   Invalidate();
  209.  
  210.   CWnd::OnKillFocus(pNewWnd);
  211. }
  212.  
  213. void SkinButton::OnCaptureChanged(CWnd *pWnd) 
  214. {
  215.   if(mouseOver) {
  216.     ReleaseCapture();
  217.     Invalidate();
  218.   }
  219.   
  220.   CWnd::OnCaptureChanged(pWnd);
  221. }
  222.  
  223. LRESULT SkinButton::OnMouseLeaveMsg(WPARAM wParam, LPARAM lParam)
  224. {
  225.   if(mouseOver) {
  226.     ReleaseCapture();
  227.     mouseOver = false;
  228.     Invalidate();
  229.   }
  230.  
  231.   return Default();
  232. }
  233.  
  234. void SkinButton::OnContextMenu(CWnd* pWnd, CPoint point) 
  235. {
  236. }
  237.  
  238. void SkinButton::SetNormalBitmap(HBITMAP bmp)
  239. {
  240.   normalBmp = bmp;
  241. }
  242.  
  243. void SkinButton::SetDownBitmap(HBITMAP bmp)
  244. {
  245.   downBmp = bmp;
  246. }
  247.  
  248. void SkinButton::SetOverBitmap(HBITMAP bmp)
  249. {
  250.   overBmp = bmp;
  251. }
  252.  
  253. void SkinButton::SetRect(const RECT& r)
  254. {
  255.   rect = r;
  256. }
  257.  
  258. void SkinButton::SetId(const char *id)
  259. {
  260.   this->id = id;
  261.   if(!winAccelGetID(id, idCommand)) {
  262.     if(!strcmp(id, "A"))
  263.       buttonMask = 1;
  264.     else if(!strcmp("B", id))
  265.       buttonMask = 2;
  266.     else if(!strcmp("SEL", id))
  267.       buttonMask = 4;
  268.     else if(!strcmp("START", id))
  269.       buttonMask = 8;
  270.     else if(!strcmp("R", id))
  271.       buttonMask = 16;
  272.     else if(!strcmp("L", id))
  273.       buttonMask = 32;
  274.     else if(!strcmp("U", id))
  275.       buttonMask = 64;
  276.     else if(!strcmp("D", id))
  277.       buttonMask = 128;
  278.     else if(!strcmp("BR", id))
  279.       buttonMask = 256;
  280.     else if(!strcmp("BL", id))
  281.       buttonMask = 512;
  282.     else if(!strcmp("SPEED", id))
  283.       buttonMask = 1024;
  284.     else if(!strcmp("CAPTURE", id))
  285.       buttonMask = 2048;
  286.     else if(!strcmp("GS", id))
  287.       buttonMask = 4096;
  288.     else if(!strcmp("UR", id))
  289.       buttonMask = 64+16;
  290.     else if(!strcmp("UL", id))
  291.       buttonMask = 64+32;
  292.     else if(!strcmp("DR", id))
  293.       buttonMask = 128+16;
  294.     else if(!strcmp("DL", id))
  295.       buttonMask = 128+32;
  296.     else if(!strcmp("MENUFILE", id))
  297.       menu = 0;
  298.     else if(!strcmp("MENUOPTIONS", id))
  299.       menu = 1;
  300.     else if(!strcmp("MENUCHEATS", id))
  301.       menu = 2;
  302.     else if(!strcmp("MENUTOOLS", id))
  303.       menu = 3;
  304.     else if(!strcmp("MENUHELP", id))
  305.       menu = 4;
  306.   }
  307. }
  308.  
  309. void SkinButton::SetRegion(HRGN rgn)
  310. {
  311.   region = rgn;
  312. }
  313.  
  314. void SkinButton::GetRect(RECT& r)
  315. {
  316.   r = rect;
  317. }
  318.  
  319. BOOL SkinButton::CreateButton(const char *name, DWORD style, const RECT& r, 
  320.                               CWnd *parent, UINT id)
  321. {
  322.   return CWnd::Create("BUTTON",
  323.                       name,
  324.                       style|WS_CHILDWINDOW,
  325.                       r,
  326.                       parent,
  327.                       id);
  328. }
  329.