Objective Grid : PART II Programmer’s Guide : Chapter 27 GridEx Feature Pack Extension : Color Button
Color Button
Objective GridEx combines a new Feature Pack control CMFCColorButton with CGXPushButton to create a CGXColorButton that provides an enhanced color picking.
By default, the CGXColorButton class behaves as a push button that opens a color picker dialog box. The color picker dialog box contains an array of small color buttons and an “other” button that displays a custom color picker. (The standard system “other” button is labeled “More Colors…”) When you select a new color, the CGXColorButton object reflects the change and displays the selected color.
Implementation
A new class CGXColorButton derives from CMFCColorButton and CGXPushButton. The CGXColorButton control is designed to be embedded in one or more grid cells. Implementation details are in the files GXColorButton.h and GXColorButton.cpp.
To implement a GridEx color button control, add a CGXColorButton variable to your application, and then call the constructor and Create() methods of the CGXColorButton object.
The color picker dialog box is displayed by the CGXColorButton::OnShowColorPopup() method when the framework calls the OnLButtonDown event handler. To support custom color selection, you can override the CGXColorButton::OnShowColorPopup() method.
The CGXColorButton object notifies its parent that a color is changing by sending it a WM_COMMAND | BN_CLICKED notification. The parent uses the CGXColorButton::GetColor() method to retrieve the current color.
Please refer to the ColorDlg.cpp file in the GridEx sample ToolTipColorPicker for details on constructing, registering, embedding, and using this new control.
Setting CGXColorButton in a Grid
To set or embed a color button in a grid:
1. Add #include "CGXColorButton.h" to the .cpp file for your CGXGridView-derived class, or to your CDialog-derived class if Grid is used on a dialog.
2. Register the control, for example:
 
CGXColorButton* pColorButton = new CGXColorButton(&m_wndGrid);
pColorButton->EnableOtherButton(_T("Other"));
pColorButton->EnableAutomaticButton(_T("Automatic"),
RGB(255, 0, 255));
pColorButton->SetColumnsNumber(5);
pColorButton->SizeToContent(TRUE);
m_wndGrid.RegisterControl(IDS_CTRL_COLORBUTTON_OGEX,
(CGXControl*)pColorButton, FALSE, FALSE);
3. Set the control in the CGXStyle object in a cell, like so:
 
.SetControl(IDS_CTRL_COLORBUTTON_OGEX)
.SetValue(RGB(255,0,0))
Intercepting Calls in the CGXColorButton Control
You can intercept calls to functions in the CGXColorButton control in two ways:
Via message handling
By overriding the function OnModifyCell() in your CGXGridCore-derived class
Message Handling
In the header file for the class in which the message should be trapped, add:
 
afx_msg void OnColorPicker();
Add to the message map:
 
ON_BN_CLICKED(IDS_CTRL_COLORBUTTON_OGEX, OnColorPicker)
For example, your handle code might look like this:
 
void CColorPickerDlg::OnColorPicker()
{
CGXControl* pControl = m_wndGrid.GetCurrentCellControl();
if(CString(pControl->GetControlClass()->m_lpszClassName) ==
_T("CGXColorButton"))
{
COLORREF color = ((CGXColorButton*)pControl)->GetColor();
CString s;
s.Format(_T("%d,%d,%d"),
GetRValue(color),
GetGValue(color),
GetBValue(color));
ROWCOL nRow, nCol;
m_wndGrid.GetCurrentCell(nRow, nCol);
m_wndGrid.SetStyleRange(CGXRange(nRow+2,nCol),
CGXStyle()
.SetValue(s).SetHorizontalAlignment(DT_CENTER));
}
m_wndGrid.Redraw();
}
Overriding OnModifyCell()
Example code:
 
void CToolTipsGridWnd::OnModifyCell(ROWCOL nRow, ROWCOL nCol)
{
if(CString(
GetCurrentCellControl()->GetControlClass()->m_lpszClassName) ==
_T("CGXColorButton"))
{
CGXStyle style;
GetStyleRowCol(nRow, nCol, style);
COLORREF color = (COLORREF)(style.GetDWordValue());
SetStyleRange(CGXRange( nRow+2, nCol),
CGXStyle().SetInterior(color));
}
CGXGridCore::OnModifyCell(nRow, nCol);
}
User Interaction
Cells that change values as a result of direct or indirect interaction are redrawn. Users cause direct interactions, while indirect interactions are the result of cell reference changes.
Users can interact with the CGXColorButton control in a Grid application using a mouse or keyboard.
Keyboard actions:
Arrow keys: Moves current cell to make control active
Arrow Left, Right, Up, Ctrl+Down, and Tab keys: Moves current cell from cell with control
Down Arrow or Space key when control is active: Shows pop-up
Space key when pop-up is displayed: Hides pop-up
Arrow Up and Down: Navigates in pop-up
Enter: Chooses selection on pop-up and hides pop-up
Arrow left and right when pop-up is displayed: Hides pop-up and moves current cell
NOTE >> Note: The key navigation on the Colors dialog was not updated, so it is similar to CMFCColorButton.
Related Samples
CGXColorButton is demonstrated in following samples:
ToolTipColorPicker
VisualDataSet