root / trunk / win / Clrpick.cpp

Revision 705, 5.6 kB (checked in by leo, 17 months ago)

Fixed color picker to display new colors.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// ColorPicker.cpp : implementation file
2//
3
4#include "lc_global.h"
5#include "leocad.h"
6#include "ClrPopup.h"
7#include "ClrPick.h"
8#include "lc_colors.h"
9
10#ifdef _DEBUG
11#define new DEBUG_NEW
12#undef THIS_FILE
13static char THIS_FILE[] = __FILE__;
14#endif
15
16/*
17void AFXAPI DDX_ColorPicker(CDataExchange *pDX, int nIDC, COLORREF& crColor)
18{
19    HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
20    ASSERT (hWndCtrl != NULL);               
21   
22    CColorPicker* pColorPicker = (CColorPicker*) CWnd::FromHandle(hWndCtrl);
23    if (pDX->m_bSaveAndValidate)
24    {
25        crColor = pColorPicker->GetColor();
26    }
27    else // initializing
28    {
29        pColorPicker->SetColor(crColor);
30    }
31}
32*/
33/////////////////////////////////////////////////////////////////////////////
34// CColorPicker
35
36CColorPicker::CColorPicker()
37{
38    m_bActive = FALSE;
39    m_nColor = -1;
40    SetColorIndex(-1);
41}
42
43CColorPicker::~CColorPicker()
44{
45}
46
47IMPLEMENT_DYNCREATE(CColorPicker, CButton)
48
49BEGIN_MESSAGE_MAP(CColorPicker, CButton)
50    //{{AFX_MSG_MAP(CColorPicker)
51    ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
52    ON_WM_CREATE()
53    //}}AFX_MSG_MAP
54    ON_MESSAGE(CPN_SELENDOK, OnSelEndOK)
55    ON_MESSAGE(CPN_SELENDCANCEL, OnSelEndCancel)
56END_MESSAGE_MAP()
57
58/////////////////////////////////////////////////////////////////////////////
59// CColorPicker message handlers
60
61LONG CColorPicker::OnSelEndOK(UINT /*lParam*/, LONG wParam)
62{
63    m_bActive = FALSE;
64    SetColorIndex(wParam);
65
66    CWnd *pParent = GetParent();
67    if (pParent)
68        pParent->SendMessage(CPN_SELENDOK, wParam, (LPARAM)GetDlgCtrlID());
69
70    return TRUE;
71}
72
73LONG CColorPicker::OnSelEndCancel(UINT /*lParam*/, LONG wParam)
74{
75    m_bActive = FALSE;
76
77    CWnd *pParent = GetParent();
78    if (pParent)
79        pParent->SendMessage(CPN_SELENDCANCEL, (WPARAM)wParam, (LPARAM)GetDlgCtrlID());
80
81    return TRUE;
82}
83
84int CColorPicker::OnCreate(LPCREATESTRUCT lpCreateStruct)
85{
86    if (CButton::OnCreate(lpCreateStruct) == -1)
87        return -1;
88
89    SetWindowSize();    // resize appropriately
90    return 0;
91}
92
93// On mouse click, create and show a CColorPopup window for colour selection
94BOOL CColorPicker::OnClicked()
95{
96    m_bActive = TRUE;
97    CRect rect;
98    GetWindowRect(rect);
99    new CColorPopup(CPoint(rect.left, rect.bottom), m_nColor, this);
100
101    return TRUE;
102}
103
104void CColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
105{
106    ASSERT(lpDrawItemStruct);
107
108    CDC*    pDC     = CDC::FromHandle(lpDrawItemStruct->hDC);
109    CRect   rect    = lpDrawItemStruct->rcItem;
110    UINT    state   = lpDrawItemStruct->itemState;
111    DWORD   dwStyle = GetStyle();
112    CString m_strText;
113
114    CSize Margins(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
115
116    // Draw arrow
117    if (m_bActive) state |= ODS_SELECTED;
118    pDC->DrawFrameControl(&m_ArrowRect, DFC_SCROLL, DFCS_SCROLLDOWN  |
119                          ((state & ODS_SELECTED) ? DFCS_PUSHED : 0) |
120                          ((state & ODS_DISABLED) ? DFCS_INACTIVE : 0));
121
122    pDC->DrawEdge(rect, EDGE_SUNKEN, BF_RECT);
123
124    // Must reduce the size of the "client" area of the button due to edge thickness.
125    rect.DeflateRect(Margins.cx, Margins.cy);
126    rect.bottom +=1;
127
128    // Fill remaining area with colour
129    rect.right -= m_ArrowRect.Width()-1;
130
131    CBrush brush(((state & ODS_DISABLED) || m_nColor == -1)? ::GetSysColor(COLOR_3DFACE) : LC_COLOR_RGB(m_nColor));
132    CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
133    pDC->SelectStockObject(NULL_PEN);
134    pDC->Rectangle(rect);
135    pDC->SelectObject(pOldBrush);
136
137    if (LC_COLOR_TRANSLUCENT(m_nColor))
138    {
139        for (int x = rect.left; x < rect.right; x++)
140        {
141            int y;
142
143            for (y = rect.top; y < rect.bottom; y+=4)
144            {
145                if (y == rect.top) y += x%4;
146                pDC->SetPixel (x,y,RGB(255,255,255));
147            }
148            for (y = rect.bottom; y > rect.top; y-=4)
149            {
150                if (y == rect.bottom) y-= x%4;
151                pDC->SetPixel (x,y,RGB(255,255,255));
152            }
153        }
154    }
155
156    // Draw focus rect
157    if (state & ODS_FOCUS)
158    {
159        rect.DeflateRect(1,1);
160        pDC->DrawFocusRect(rect);
161    }
162}
163
164/////////////////////////////////////////////////////////////////////////////
165// CColorPicker overrides
166
167void CColorPicker::PreSubclassWindow()
168{
169    ModifyStyle(0, BS_OWNERDRAW);        // Make it owner drawn
170    CButton::PreSubclassWindow();
171    SetWindowSize();                     // resize appropriately
172}
173
174/////////////////////////////////////////////////////////////////////////////
175// CColorPicker attributes
176
177int CColorPicker::GetColorIndex()
178{
179    return m_nColor;
180}
181
182void CColorPicker::SetColorIndex(int nColor)
183{
184    if (m_nColor != nColor)
185    {
186        m_nColor = nColor;
187        if (IsWindow(m_hWnd))
188            RedrawWindow();
189    }
190}
191
192/////////////////////////////////////////////////////////////////////////////
193// CColorPicker implementation
194
195void CColorPicker::SetWindowSize()
196{
197    // Get size dimensions of edges
198    CSize MarginSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
199
200    // Get size of dropdown arrow
201    int nArrowWidth = max(::GetSystemMetrics(SM_CXHTHUMB), 5*MarginSize.cx);
202    int nArrowHeight = max(::GetSystemMetrics(SM_CYVTHUMB), 5*MarginSize.cy);
203    CSize ArrowSize(max(nArrowWidth, nArrowHeight), max(nArrowWidth, nArrowHeight));
204
205    // Get window size
206    CRect rect;
207    GetWindowRect(rect);
208
209    CWnd* pParent = GetParent();
210    if (pParent)
211        pParent->ScreenToClient(rect);
212
213    // Set window size at least as wide as 2 arrows, and as high as arrow + margins
214    int nWidth = max(rect.Width(), 2*ArrowSize.cx + 2*MarginSize.cx);
215    int nHeight = max(rect.Height(), ArrowSize.cy + 2*MarginSize.cy);
216    MoveWindow(rect.left, rect.top, nWidth, nHeight, TRUE);
217
218    // Get the new coords of this window
219    GetWindowRect(rect);
220    ScreenToClient(rect);
221
222    // Get the rect where the arrow goes, and convert to client coords.
223    m_ArrowRect.SetRect(rect.right - ArrowSize.cx - MarginSize.cx,
224                        rect.top + MarginSize.cy, rect.right - MarginSize.cx,
225                        rect.bottom - MarginSize.cy);
226}
Note: See TracBrowser for help on using the browser.