annotate src/win32/ColorButton.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // ColorButton.cpp : implementation file
rlm@1 2 //
rlm@1 3
rlm@1 4 #include "stdafx.h"
rlm@1 5 #include "ColorButton.h"
rlm@1 6
rlm@1 7 bool ColorButton::isRegistered = false;
rlm@1 8
rlm@1 9 /////////////////////////////////////////////////////////////////////////////
rlm@1 10 // ColorButton
rlm@1 11
rlm@1 12 ColorButton::ColorButton()
rlm@1 13 {
rlm@1 14 color = 0;
rlm@1 15 registerClass();
rlm@1 16 }
rlm@1 17
rlm@1 18 ColorButton::~ColorButton()
rlm@1 19 {
rlm@1 20 }
rlm@1 21
rlm@1 22
rlm@1 23 BEGIN_MESSAGE_MAP(ColorButton, CButton)
rlm@1 24 //{{AFX_MSG_MAP(ColorButton)
rlm@1 25 // NOTE - the ClassWizard will add and remove mapping macros here.
rlm@1 26 //}}AFX_MSG_MAP
rlm@1 27 END_MESSAGE_MAP()
rlm@1 28
rlm@1 29 /////////////////////////////////////////////////////////////////////////////
rlm@1 30 // ColorButton message handlers
rlm@1 31
rlm@1 32 void ColorButton::PreSubclassWindow()
rlm@1 33 {
rlm@1 34 SetWindowLong(m_hWnd, GWL_STYLE, GetStyle() | BS_OWNERDRAW);
rlm@1 35 CWnd::PreSubclassWindow();
rlm@1 36 }
rlm@1 37
rlm@1 38 void ColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
rlm@1 39 {
rlm@1 40 ASSERT(lpDrawItemStruct);
rlm@1 41
rlm@1 42 int r = (color & 0x1f) << 3;
rlm@1 43 int g = (color & 0x3e0) >> 2;
rlm@1 44 int b = (color & 0x7c00) >> 7;
rlm@1 45
rlm@1 46 HDC dc = lpDrawItemStruct->hDC;
rlm@1 47 UINT state = lpDrawItemStruct->itemState;
rlm@1 48 RECT rect = lpDrawItemStruct->rcItem;
rlm@1 49
rlm@1 50 SIZE margins;
rlm@1 51 margins.cx = ::GetSystemMetrics(SM_CXEDGE);
rlm@1 52 margins.cy = ::GetSystemMetrics(SM_CYEDGE);
rlm@1 53
rlm@1 54 if(GetState() & BST_PUSHED)
rlm@1 55 DrawEdge(dc, &rect, EDGE_SUNKEN, BF_RECT);
rlm@1 56 else
rlm@1 57 DrawEdge(dc, &rect, EDGE_RAISED, BF_RECT);
rlm@1 58
rlm@1 59 InflateRect(&rect, -margins.cx, -margins.cy);
rlm@1 60
rlm@1 61 HBRUSH br = CreateSolidBrush((state & ODS_DISABLED) ?
rlm@1 62 ::GetSysColor(COLOR_3DFACE) : RGB(r,g,b));
rlm@1 63
rlm@1 64 FillRect(dc, &rect, br);
rlm@1 65
rlm@1 66 if(state & ODS_FOCUS) {
rlm@1 67 InflateRect(&rect, -1, -1);
rlm@1 68 DrawFocusRect(dc, &rect);
rlm@1 69 }
rlm@1 70
rlm@1 71 DeleteObject(br);
rlm@1 72 }
rlm@1 73
rlm@1 74 void ColorButton::setColor(u16 c)
rlm@1 75 {
rlm@1 76 color = c;
rlm@1 77 Invalidate();
rlm@1 78 }
rlm@1 79
rlm@1 80 void ColorButton::registerClass()
rlm@1 81 {
rlm@1 82 if(!isRegistered) {
rlm@1 83 WNDCLASS wc;
rlm@1 84 ZeroMemory(&wc, sizeof(wc));
rlm@1 85 wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
rlm@1 86 wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
rlm@1 87 wc.hInstance = AfxGetInstanceHandle();
rlm@1 88 wc.hIcon = LoadCursor(NULL, IDC_ARROW);
rlm@1 89 wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
rlm@1 90 wc.lpszMenuName = NULL;
rlm@1 91 wc.lpszClassName = "VbaColorButton";
rlm@1 92 AfxRegisterClass(&wc);
rlm@1 93 isRegistered = true;
rlm@1 94 }
rlm@1 95 }