view src/win32/ColorButton.cpp @ 3:b05d00f19d80

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