summaryrefslogtreecommitdiff
path: root/win/Clrpick.cpp
blob: e9103c4181859a8ec905531968fd1bd9f16ce028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// ColorPicker.cpp : implementation file
//

#include "stdafx.h"
#include "leocad.h"
#include "ClrPopup.h"
#include "ClrPick.h"
#include "globals.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/*
void AFXAPI DDX_ColorPicker(CDataExchange *pDX, int nIDC, COLORREF& crColor)
{
    HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
    ASSERT (hWndCtrl != NULL);                
    
    CColorPicker* pColorPicker = (CColorPicker*) CWnd::FromHandle(hWndCtrl);
    if (pDX->m_bSaveAndValidate)
    {
        crColor = pColorPicker->GetColor();
    }
    else // initializing
    {
        pColorPicker->SetColor(crColor);
    }
}
*/
/////////////////////////////////////////////////////////////////////////////
// CColorPicker

CColorPicker::CColorPicker()
{
    m_bActive = FALSE;
	m_bDefaultText = FALSE;
	m_bCustomText = FALSE;
    m_crColor = GetSysColor(COLOR_3DFACE);
	SetColorIndex (-1);
}

CColorPicker::~CColorPicker()
{
}

IMPLEMENT_DYNCREATE(CColorPicker, CButton)

BEGIN_MESSAGE_MAP(CColorPicker, CButton)
    //{{AFX_MSG_MAP(CColorPicker)
    ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    ON_MESSAGE(CPN_SELENDOK, OnSelEndOK)
    ON_MESSAGE(CPN_SELENDCANCEL, OnSelEndCancel)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorPicker message handlers

LONG CColorPicker::OnSelEndOK(UINT /*lParam*/, LONG wParam)
{
	m_bActive = FALSE;
	SetColorIndex(wParam);

	CWnd *pParent = GetParent();
	if (pParent)
		pParent->SendMessage(CPN_SELENDOK, wParam, (LPARAM)GetDlgCtrlID());

	return TRUE;
}

LONG CColorPicker::OnSelEndCancel(UINT /*lParam*/, LONG wParam)
{
    m_bActive = FALSE;

    CWnd *pParent = GetParent();
    if (pParent)
		pParent->SendMessage(CPN_SELENDCANCEL, (WPARAM)wParam, (LPARAM)GetDlgCtrlID());

    return TRUE;
}

int CColorPicker::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CButton::OnCreate(lpCreateStruct) == -1)
        return -1;

    SetWindowSize();    // resize appropriately
	return 0;
}

// On mouse click, create and show a CColorPopup window for colour selection
BOOL CColorPicker::OnClicked()
{
    m_bActive = TRUE;
    CRect rect;
    GetWindowRect(rect);
    new CColorPopup(CPoint(rect.left, rect.bottom), m_crColor, 
					this, m_bDefaultText, m_bCustomText);

	return TRUE;
}

void CColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    ASSERT(lpDrawItemStruct);
    
    CDC*    pDC     = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect   rect    = lpDrawItemStruct->rcItem;
    UINT    state   = lpDrawItemStruct->itemState;
    DWORD   dwStyle = GetStyle();
    CString m_strText;

    CSize Margins(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));

    // Draw arrow
    if (m_bActive) state |= ODS_SELECTED;
    pDC->DrawFrameControl(&m_ArrowRect, DFC_SCROLL, DFCS_SCROLLDOWN  | 
                          ((state & ODS_SELECTED) ? DFCS_PUSHED : 0) |
                          ((state & ODS_DISABLED) ? DFCS_INACTIVE : 0));

    pDC->DrawEdge(rect, EDGE_SUNKEN, BF_RECT);

    // Must reduce the size of the "client" area of the button due to edge thickness.
    rect.DeflateRect(Margins.cx, Margins.cy);
	rect.bottom +=1;

    // Fill remaining area with colour
    rect.right -= m_ArrowRect.Width()-1;

    CBrush brush( ((state & ODS_DISABLED) || m_crColor == CLR_DEFAULT)? 
                  ::GetSysColor(COLOR_3DFACE) : m_crColor);
    CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
	pDC->SelectStockObject(NULL_PEN);
    pDC->Rectangle(rect);
    pDC->SelectObject(pOldBrush);

	if (GetColorIndex() > 13 && GetColorIndex() < 22)
	{
		for (int x = rect.left; x < rect.right; x++)
		{
			for (int y = rect.top; y < rect.bottom; y+=4)
			{
				if (y == rect.top) y += x%4;
				pDC->SetPixel (x,y,RGB(255,255,255));
			}
			for (int y = rect.bottom; y > rect.top; y-=4)
			{
				if (y == rect.bottom) y-= x%4;
				pDC->SetPixel (x,y,RGB(255,255,255));
			}
		}
	}

    // Draw focus rect
    if (state & ODS_FOCUS) 
    {
        rect.DeflateRect(1,1);
        pDC->DrawFocusRect(rect);
    }
}

/////////////////////////////////////////////////////////////////////////////
// CColorPicker overrides

void CColorPicker::PreSubclassWindow() 
{
    ModifyStyle(0, BS_OWNERDRAW);        // Make it owner drawn
    CButton::PreSubclassWindow();
    SetWindowSize();                     // resize appropriately
}

/////////////////////////////////////////////////////////////////////////////
// CColorPicker attributes

int CColorPicker::GetColorIndex()
{
	return m_nColor;
}

void CColorPicker::SetColorIndex(int nColor)
{
	if (nColor != -1)
		m_crColor = RGB(FlatColorArray[nColor][0], FlatColorArray[nColor][1], FlatColorArray[nColor][2]);

	if (m_nColor != nColor)
	{
		m_nColor = nColor;
		if (IsWindow(m_hWnd))
			RedrawWindow();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CColorPicker implementation

void CColorPicker::SetWindowSize()
{
    // Get size dimensions of edges
    CSize MarginSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));

    // Get size of dropdown arrow
    int nArrowWidth = max(::GetSystemMetrics(SM_CXHTHUMB), 5*MarginSize.cx);
    int nArrowHeight = max(::GetSystemMetrics(SM_CYVTHUMB), 5*MarginSize.cy);
    CSize ArrowSize(max(nArrowWidth, nArrowHeight), max(nArrowWidth, nArrowHeight));

    // Get window size
    CRect rect;
    GetWindowRect(rect);

    CWnd* pParent = GetParent();
    if (pParent)
        pParent->ScreenToClient(rect);

    // Set window size at least as wide as 2 arrows, and as high as arrow + margins
    int nWidth = max(rect.Width(), 2*ArrowSize.cx + 2*MarginSize.cx);
    int nHeight = max(rect.Height(), ArrowSize.cy + 2*MarginSize.cy);
    MoveWindow(rect.left, rect.top, nWidth, nHeight, TRUE);

    // Get the new coords of this window
    GetWindowRect(rect);
    ScreenToClient(rect);

    // Get the rect where the arrow goes, and convert to client coords.
    m_ArrowRect.SetRect(rect.right - ArrowSize.cx - MarginSize.cx, 
                        rect.top + MarginSize.cy, rect.right - MarginSize.cx,
                        rect.bottom - MarginSize.cy);
}