www.pudn.com > colortable.zip > colorsDlg.cpp


/***************************************************************************** 
*           Change Log 
*  Date     | Change 
*-----------+----------------------------------------------------------------- 
* 13-Jan-02 | Created 
*****************************************************************************/ 
// colorsDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "colors.h" 
#include "ColorList.h" 
#include "Swatch.h" 
#include "ImageButton.h" 
#include "StockCombo.h" 
#include "colorsDlg.h" 
#include "ErrorString.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
static class { 
    public: 
       COLORREF color; 
       LPCTSTR name; 
} colortable[] = { 
   RGB(  0,  0,  0), _T("Black"), 
   RGB( 25, 25, 25), _T("90% Black"), 
   RGB( 51, 51, 51), _T("80% Black"), 
   RGB( 76, 76, 76), _T("70% Black"), 
   RGB(102,102,102), _T("60& Black"), 
   RGB(127,127,127), _T("50% Black"), 
   RGB(153,153,153), _T("40% Black"), 
   RGB(178,178,178), _T("30% Black"), 
   RGB(204,204,204), _T("20% Black"), 
   RGB(229,229,229), _T("10% Black"), 
   RGB(255,255,255), _T("White"), 
   RGB(  0,  0,255), _T("Blue"), 
   RGB(  0,255,255), _T("Cyan"), 
   RGB(  0,255,  0), _T("Green"), 
   RGB(255,255,  0), _T("Yellow"), 
   RGB(255,  0,  0), _T("Red"), 
   RGB(255,  0,255), _T("Magenta"), 
   RGB(153,  0,204), _T("Purple"), 
   RGB(255,102,  0), _T("Orange"), 
   RGB(255,153,204), _T("Pink"), 
   RGB(102, 51, 51), _T("Dark Brown"), 
   RGB(204,204,255), _T("Powder Blue"), 
   RGB(153,153,255), _T("Pastel Blue"), 
   RGB(102,153,255), _T("Baby Blue"), 
   RGB(102,102,255), _T("Electric Blue"), 
   RGB(102,102,204), _T("Twilight Blue"), 
   RGB(  0, 51,153), _T("Navy Blue"), 
   RGB(  0,  0,102), _T("Deep Navy Blue"), 
   RGB( 51,102,153), _T("Desert Blue"), 
   RGB(  0,204,255), _T("Sky Blue"), 
   RGB(153,255,255), _T("Ice Blue"), 
   RGB(153,204,204), _T("Light Blue-Green"), 
   RGB(102,153,153), _T("Ocean Green"), 
   RGB( 51,102,102), _T("Moss Green"), 
   RGB(  0, 51, 51), _T("Dark Green"), 
   RGB(  0,102, 51), _T("Forest Green"), 
   RGB(  0,153, 51), _T("Grass Green"), 
   RGB( 51,153,102), _T("Kentucky Green"), 
   RGB( 51,204,102), _T("Light Green"), 
   RGB( 51,204, 51), _T("Spring Green"), 
   RGB(102,255,204), _T("Turquoise"), 
   RGB( 51,204,153), _T("Sea Green"), 
   RGB(153,204,153), _T("Faded Green"), 
   RGB(204,255,204), _T("Ghost Green"), 
   RGB(153,155,153), _T("Mint Green"), 
   RGB(153,102,153), _T("Army Green"), 
   RGB(102,153, 51), _T("Avocado Green"), 
   RGB(153,204, 51), _T("Martian Green"),  
   RGB(153,204,102), _T("Dull Green"), 
   RGB(153,255,  0), _T("Chartreuse"), 
   RGB(254,255,102), _T("Moon Green"), 
   RGB( 51, 51,  0), _T("Murky Green"), 
   RGB(102,102, 51), _T("Olive Drab"), 
   RGB(153,153,102), _T("Khaki"), 
   RGB(153,153, 51), _T("Olive"), 
   RGB(204,204, 51), _T("Banana Yellow"), 
   RGB(255,255,102), _T("Light Yellow"), 
   RGB(255,255,153), _T("Chalk"), 
   RGB(255,255,204), _T("Pale Yellow"), 
   RGB(153,102, 51), _T("Brown"), 
   RGB(204,102, 51), _T("Red-Brown"), 
   RGB(204,153, 51), _T("Gold"), 
   RGB(255,102, 51), _T("Autumn Orange"), 
   RGB(255,153, 51), _T("Light Orange"), 
   RGB(255,153,102), _T("Peach"), 
   RGB(255,204,  0), _T("Deep Yellow"), 
   RGB(255,204,153), _T("Sand"), 
   RGB(102, 51,  0), _T("Walnut"), 
   RGB(153,  0,  0), _T("Ruby Red"), 
   RGB(204, 51,  0), _T("Brick Red"), 
   RGB(255,102,102), _T("Tropical Pink"), 
   RGB(255,153,153), _T("Soft Pink"), 
   RGB(255,204,204), _T("Faded Pink"), 
   RGB(153, 51,102), _T("Crimson"), 
   RGB(204, 51,102), _T("Regal Red"), 
   RGB(204, 51,153), _T("Deep Rose"), 
   RGB(255,  0,102), _T("Neon Red"), 
   RGB(255,102,153), _T("Deep Pink"), 
   RGB(255, 51,153), _T("Hot Pink"), 
   RGB(204,102,153), _T("Dusty Rose"), 
   RGB(102,  0,102), _T("Plum"), 
   RGB(153,  0,153), _T("Deep Violet"), 
   RGB(255,153,255), _T("Light Violet"), 
   RGB(102,  0,102), _T("Violet"), 
   RGB(153,102,153), _T("Dusty Plum"), 
   RGB(204,153,204), _T("Pale Purple"), 
   RGB(153, 51,204), _T("Majestic Purple"), 
   RGB(204, 51,255), _T("Neon Purple"), 
   RGB(204,102,255), _T("Light Purple"), 
   RGB(153,102,204), _T("Twilight Violet"), 
   RGB(204,153,255), _T("Easter Purple"), 
   RGB( 51,  0,102), _T("Deep Purple"), 
   RGB(102, 51,153), _T("Grape"), 
   RGB(153,102,255), _T("Blue Violet"), 
   RGB(153,  0,255), _T("Blue Purple"), 
   RGB(102,  0,204), _T("Deep River"), 
   RGB(102, 51,255), _T("Deep Azure"), 
   RGB( 51,  0,153), _T("Storm Blue"),  
   RGB( 51,  0,204), _T("Deep Blue"), 
   RGB(0,0,0), NULL   // END OF TABLE MARK 
}; 
    
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorsDlg dialog 
 
/**************************************************************************** 
*                            CColorsDlg::FindColor 
* Inputs: 
*       COLOREF color 
* Result: CString 
*       Color name, or empty string if not found 
****************************************************************************/ 
 
CString CColorsDlg::FindColor(COLORREF color) 
   { 
    for(UINT i = 0; colortable[i].name != NULL; i++) 
       { /* scan table */ 
	if(colortable[i].color == color) 
	   return CString(colortable[i].name); 
       } /* scan table */ 
    return CString(_T("")); 
   } 
 
/**************************************************************************** 
*                           CColorsDlg::CColorsDlg 
* Inputs: 
*       CWnd * pParent: Parent dialog 
* Effect:  
*       Constructor 
****************************************************************************/ 
 
CColorsDlg::CColorsDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CColorsDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CColorsDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
/**************************************************************************** 
*                         CColorsDlg::DoDataExchange 
****************************************************************************/ 
 
void CColorsDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CColorsDlg) 
	DDX_Control(pDX, IDC_DELETE, c_Delete); 
	DDX_Control(pDX, IDC_STOCK, c_Stock); 
	DDX_Control(pDX, IDC_COLOR, c_Swatch); 
	DDX_Control(pDX, IDC_COLORS, c_Colors); 
	DDX_Control(pDX, IDC_UP, c_Up); 
	DDX_Control(pDX, IDC_SPINR, c_SpinR); 
	DDX_Control(pDX, IDC_SPING, c_SpinG); 
	DDX_Control(pDX, IDC_SPINB, c_SpinB); 
	DDX_Control(pDX, IDC_DOWN, c_Down); 
	//}}AFX_DATA_MAP 
} 
 
/**************************************************************************** 
*                                 MESSAGE_MAP 
****************************************************************************/ 
 
BEGIN_MESSAGE_MAP(CColorsDlg, CDialog) 
	//{{AFX_MSG_MAP(CColorsDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_CLOSE() 
	ON_EN_CHANGE(IDC_B, OnChangeB) 
	ON_BN_CLICKED(IDC_DOWN, OnDown) 
	ON_EN_CHANGE(IDC_G, OnChangeG) 
	ON_EN_CHANGE(IDC_R, OnChangeR) 
	ON_BN_CLICKED(IDC_SAVE, OnSave) 
	ON_BN_CLICKED(IDC_UP, OnUp) 
	ON_BN_CLICKED(IDC_ADD, OnAdd) 
	ON_BN_CLICKED(IDC_DELETE, OnDelete) 
	ON_LBN_SELCHANGE(IDC_COLORS, OnSelchangeColors) 
	ON_BN_CLICKED(IDC_B0, OnB0) 
	ON_BN_CLICKED(IDC_B102, OnB102) 
	ON_BN_CLICKED(IDC_B127, OnB127) 
	ON_BN_CLICKED(IDC_B153, OnB153) 
	ON_BN_CLICKED(IDC_B192, OnB192) 
	ON_BN_CLICKED(IDC_B204, OnB204) 
	ON_BN_CLICKED(IDC_B255, OnB255) 
	ON_BN_CLICKED(IDC_B51, OnB51) 
	ON_BN_CLICKED(IDC_G0, OnG0) 
	ON_BN_CLICKED(IDC_G102, OnG102) 
	ON_BN_CLICKED(IDC_G127, OnG127) 
	ON_BN_CLICKED(IDC_G153, OnG153) 
	ON_BN_CLICKED(IDC_G192, OnG192) 
	ON_BN_CLICKED(IDC_G204, OnG204) 
	ON_BN_CLICKED(IDC_G255, OnG255) 
	ON_BN_CLICKED(IDC_G51, OnG51) 
	ON_BN_CLICKED(IDC_R0, OnR0) 
	ON_BN_CLICKED(IDC_R102, OnR102) 
	ON_BN_CLICKED(IDC_R127, OnR127) 
	ON_BN_CLICKED(IDC_R153, OnR153) 
	ON_BN_CLICKED(IDC_R192, OnR192) 
	ON_BN_CLICKED(IDC_R204, OnR204) 
	ON_BN_CLICKED(IDC_R255, OnR255) 
	ON_BN_CLICKED(IDC_R51, OnR51) 
	ON_CBN_SELENDOK(IDC_STOCK, OnSelendokStock) 
	ON_BN_CLICKED(IDC_LOAD, OnLoad) 
	ON_BN_CLICKED(IDC_CLEAR, OnClear) 
	ON_BN_CLICKED(IDC_APPEND, OnAppend) 
	ON_BN_CLICKED(IDC_BELOW, OnBelow) 
	ON_COMMAND(IDM_ABOUTBOX, OnAboutbox) 
	//}}AFX_MSG_MAP 
	ON_COMMAND(IDC_CLOSE, OnClose) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorsDlg message handlers 
 
/**************************************************************************** 
*                          CColorsDlg::OnInitDialog 
* Result: BOOL 
*       TRUE, always 
****************************************************************************/ 
 
BOOL CColorsDlg::OnInitDialog() 
   { 
    CDialog::OnInitDialog(); 
 
    // Add "About..." menu item to system menu. 
 
    // IDM_ABOUTBOX must be in the system command range. 
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
    ASSERT(IDM_ABOUTBOX < 0xF000); 
 
    CMenu* pSysMenu = GetSystemMenu(FALSE); 
    if (pSysMenu != NULL) 
       { 
	CString strAboutMenu; 
	strAboutMenu.LoadString(IDS_ABOUTBOX); 
	if (!strAboutMenu.IsEmpty()) 
	   { 
	    pSysMenu->AppendMenu(MF_SEPARATOR); 
	    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
	   } 
       } 
     
    // Set the icon for this dialog.  The framework does this automatically 
    //  when the application's main window is not a dialog 
    SetIcon(m_hIcon, TRUE);			// Set big icon 
    SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
 
    c_SpinR.SetRange(0, 255); 
    c_SpinG.SetRange(0, 255); 
    c_SpinB.SetRange(0, 255); 
    c_SpinR.SetPos(255); 
    c_SpinG.SetPos(255); 
    c_SpinB.SetPos(255); 
 
    c_Down.LoadBitmaps(IDB_DOWN); 
    c_Up.LoadBitmaps(IDB_UP); 
 
    for(UINT i = 0; colortable[i].name != NULL; i++) 
       { /* add items */ 
	int n = c_Stock.AddString(colortable[i].name); 
	c_Stock.SetItemData(n, colortable[i].color); 
       } /* add items */ 
 
    c_Stock.SetCurSel(0); 
    updateControls(); 
	 
    return TRUE;  // return TRUE  unless you set the focus to a control 
   } 
 
/**************************************************************************** 
*                          CColorsDlg::OnSysCommand 
* Inputs: 
*       UINT nID: System menu ID 
*	LPARAM lParam: 
* Result: void 
*        
* Effect:  
*       Handles "About" box triggering 
****************************************************************************/ 
 
void CColorsDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
/**************************************************************************** 
*                             CColorsDlg::OnPaint 
* Result: void 
*        
* Effect:  
*       If you add a minimize button to your dialog, you will need the code 
*       below to draw the icon.  For MFC applications using the document/view 
*       model, this is automatically done for you by the framework. 
****************************************************************************/ 
 
void CColorsDlg::OnPaint()  
   { 
    if (IsIconic()) 
       { 
	CPaintDC dc(this); // device context for painting 
 
	SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
	// Center icon in client rectangle 
	int cxIcon = GetSystemMetrics(SM_CXICON); 
	int cyIcon = GetSystemMetrics(SM_CYICON); 
	CRect rect; 
	GetClientRect(&rect); 
	int x = (rect.Width() - cxIcon + 1) / 2; 
	int y = (rect.Height() - cyIcon + 1) / 2; 
 
	// Draw the icon 
	dc.DrawIcon(x, y, m_hIcon); 
       } 
    else 
       { 
	CDialog::OnPaint(); 
       } 
   } 
 
/**************************************************************************** 
*                         CColorsDlg::OnQueryDragIcon 
* Result: HCURSOR 
*        
* Effect:  
*       The system calls this to obtain the cursor to display while the user 
*       drags the minimized window. 
****************************************************************************/ 
 
HCURSOR CColorsDlg::OnQueryDragIcon() 
   { 
    return (HCURSOR) m_hIcon; 
   } 
 
/**************************************************************************** 
*                              CColorsDlg::OnOK 
* Result: void 
*        
* Effect:  
*       Does nothing. This keeps an unsolicted  key from closing the app 
****************************************************************************/ 
 
void CColorsDlg::OnOK()  
   { 
   } 
 
/**************************************************************************** 
*                            CColorsDlg::OnCancel 
* Result: void 
*        
* Effect:  
*       Nothing. This keeps the ESC key from closing the app 
****************************************************************************/ 
 
void CColorsDlg::OnCancel()  
   { 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnClose 
* Result: void 
*        
* Effect:  
*       This allows the [x] button to close the dialog 
****************************************************************************/ 
 
void CColorsDlg::OnClose()  
   { 
    CDialog::OnOK(); 
   } 
 
/**************************************************************************** 
*                            CColorsDlg::OnChangeR 
* Result: void 
*        
* Effect:  
*       Updates the swatch 
****************************************************************************/ 
 
void CColorsDlg::OnChangeR()  
   { 
    if(c_Swatch.GetSafeHwnd() != NULL) 
       c_Swatch.SetColor(RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
   } 
 
/**************************************************************************** 
*                            CColorsDlg::OnChangeG 
* Result: void 
*        
* Effect:  
*       Changes the swatch 
****************************************************************************/ 
 
void CColorsDlg::OnChangeG()  
   { 
    if(c_Swatch.GetSafeHwnd() != NULL) 
       c_Swatch.SetColor(RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
   } 
 
/**************************************************************************** 
*                            CColorsDlg::OnChangeB 
* Result: void 
*        
* Effect:  
*       Updates the swatch 
****************************************************************************/ 
 
void CColorsDlg::OnChangeB()  
   { 
    if(c_Swatch.GetSafeHwnd() != NULL) 
       c_Swatch.SetColor(RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnDown 
* Result: void 
*        
* Effect:  
*       Moves the current selection down in the list 
****************************************************************************/ 
 
void CColorsDlg::OnDown()  
   { 
    int n = c_Colors.GetCurSel(); 
    if(n == c_Colors.GetCount() - 1) 
       return; 
    COLORREF next = (COLORREF)c_Colors.GetItemData(n + 1); 
    COLORREF current = (COLORREF)c_Colors.GetItemData(n); 
 
    c_Colors.SetItemData(n + 1, current); 
    c_Colors.SetItemData(n, next); 
 
    CRect r; 
    c_Colors.GetItemRect(n + 1, &r); 
    c_Colors.InvalidateRect(&r); 
    c_Colors.GetItemRect(n, &r); 
    c_Colors.InvalidateRect(&r); 
    c_Colors.SetCurSel(n + 1); 
    updateControls(); 
   } 
 
/**************************************************************************** 
*                              CColorsDlg::OnUp 
* Result: void 
*        
* Effect:  
*       Moves the current selection up in the list 
****************************************************************************/ 
 
void CColorsDlg::OnUp()  
   { 
    int n = c_Colors.GetCurSel(); 
    if(n < 1) 
       return; 
    COLORREF prev = (COLORREF)c_Colors.GetItemData(n - 1); 
    COLORREF current = (COLORREF)c_Colors.GetItemData(n); 
     
    c_Colors.SetItemData(n - 1, current); 
    c_Colors.SetItemData(n, prev); 
     
    CRect r; 
    c_Colors.GetItemRect(n - 1, &r); 
    c_Colors.InvalidateRect(&r); 
    c_Colors.GetItemRect(n, &r); 
    c_Colors.InvalidateRect(&r); 
    c_Colors.SetCurSel(n - 1); 
     
    updateControls(); 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnSave 
* Result: void 
*        
* Effect:  
*       Saves the current list as a text file of RGB triples 
****************************************************************************/ 
 
void CColorsDlg::OnSave()  
   { 
    CFileDialog dlg(FALSE, _T("col"), NULL, 0); 
    if(dlg.DoModal() != IDOK) 
       return; 
 
    CStdioFile f; 
    if(!f.Open(dlg.GetPathName(), CFile::modeWrite | CFile::modeCreate)) 
       { /* failed */ 
	DWORD err = ::GetLastError(); 
	CString errmsg = ErrorString(err); 
	CString s; 
	s.Format(_T("File open failed\n%s\n%s"), dlg.GetPathName(), errmsg); 
	AfxMessageBox(s); 
	return; 
       } /* failed */ 
     
    for(int i = 0; i < c_Colors.GetCount(); i++) 
       { /* scan list */ 
	COLORREF c = (COLORREF)c_Colors.GetItemData(i); 
	CString s; 
	s.Format(_T("   RGB(%3d, %3d, %3d),"), GetRValue(c), GetGValue(c), GetBValue(c)); 
	f.WriteString(s); 
	s = FindColor(c); 
	if(!s.IsEmpty()) 
	   { /* has name */ 
	    f.WriteString(_T("  // ")); 
	    f.WriteString(s); 
	   } /* has name */ 
	f.WriteString(_T("\n")); 
       } /* scan list */ 
    f.Close(); 
   } 
 
/**************************************************************************** 
*                         CColorsDlg::updateControls 
* Result: void 
*        
* Effect:  
*       Enables/disables controls 
****************************************************************************/ 
 
void CColorsDlg::updateControls() 
    { 
     c_Up.EnableWindow(c_Colors.GetCurSel() >= 1); 
     c_Down.EnableWindow(c_Colors.GetCurSel() < c_Colors.GetCount() - 1); 
     c_Delete.EnableWindow(c_Colors.GetCurSel() >= 0); 
    } // CColorsDlg::updateControls 
 
/**************************************************************************** 
*                              CColorsDlg::OnAdd 
* Result: void 
*        
* Effect:  
*       Inserts the item above the current selection 
****************************************************************************/ 
 
void CColorsDlg::OnAdd()  
   { 
    int n = c_Colors.GetCurSel(); 
    c_Colors.InsertString(n, (LPCTSTR)RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
    updateControls(); 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnBelow 
* Result: void 
*        
* Effect:  
*       Inserts the entry below the current selection 
****************************************************************************/ 
 
void CColorsDlg::OnBelow()  
   { 
    int n = c_Colors.GetCurSel(); 
    if(n < 0 || n == c_Colors.GetCount() - 1) 
       { /* just append */ 
	OnAppend(); 
	return; 
       } /* just append */ 
 
    c_Colors.InsertString(n + 1, (LPCTSTR)RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
    updateControls(); 
   } 
 
 
/**************************************************************************** 
*                            CColorsDlg::OnAppend 
* Result: void 
*        
* Effect:  
*       Appends the color to the list 
****************************************************************************/ 
 
void CColorsDlg::OnAppend()  
   { 
    c_Colors.AddString((LPCTSTR)RGB((BYTE)c_SpinR.GetPos(), (BYTE)c_SpinG.GetPos(), (BYTE)c_SpinB.GetPos())); 
    updateControls(); 
   } 
 
/**************************************************************************** 
*                            CColorsDlg::OnDelete 
* Result: void 
*        
* Effect:  
*       Deletes the current selection 
****************************************************************************/ 
 
void CColorsDlg::OnDelete()  
   { 
    int n = c_Colors.GetCurSel(); 
    if(n < 0) 
       return; 
    c_Colors.DeleteString(n); 
    if(n < c_Colors.GetCount() - 1) 
       c_Colors.SetCurSel(n); 
    else 
    if(c_Colors.GetCount() > 0) 
       c_Colors.SetCurSel(c_Colors.GetCount() - 1); 
        
    updateControls(); 
   } 
 
/**************************************************************************** 
*                        CColorsDlg::OnSelchangeColors 
* Result: void 
*        
* Effect:  
*       Loads the RGB spinners based on the selection just made 
****************************************************************************/ 
 
void CColorsDlg::OnSelchangeColors()  
   { 
    int n = c_Colors.GetCurSel(); 
    if(n < 0) 
       return; 
    COLORREF c = c_Colors.GetItemData(n); 
    c_SpinR.SetPos(GetRValue(c)); 
    c_SpinG.SetPos(GetGValue(c)); 
    c_SpinB.SetPos(GetBValue(c)); 
    updateControls(); 
   } 
 
/**************************************************************************** 
*                        CColorsDlg::OnR?, OnG?, OnB? 
* Result: void 
*        
* Effect:  
*       Simplest way to set values from the matrix 
****************************************************************************/ 
 
//---------------- 
// Red 
//---------------- 
 
void CColorsDlg::OnR0()  
   { 
    c_SpinR.SetPos(0); 
   } 
 
void CColorsDlg::OnR51()  
   { 
    c_SpinR.SetPos(51); 
   } 
 
void CColorsDlg::OnR102()  
   { 
    c_SpinR.SetPos(102); 
   } 
 
void CColorsDlg::OnR127()  
   { 
    c_SpinR.SetPos(127); 
   } 
 
void CColorsDlg::OnR153()  
   { 
    c_SpinR.SetPos(153); 
   } 
 
void CColorsDlg::OnR192()  
   { 
    c_SpinR.SetPos(192); 
   } 
 
void CColorsDlg::OnR204()  
   { 
    c_SpinR.SetPos(204); 
   } 
 
void CColorsDlg::OnR255()  
   { 
    c_SpinR.SetPos(255); 
   } 
 
//---------------- 
// Green 
//---------------- 
void CColorsDlg::OnG0()  
   { 
    c_SpinG.SetPos(0); 
   } 
 
void CColorsDlg::OnG51()  
   { 
    c_SpinG.SetPos(51); 
   } 
 
void CColorsDlg::OnG102()  
   { 
    c_SpinG.SetPos(102); 
   } 
 
void CColorsDlg::OnG127()  
   { 
    c_SpinG.SetPos(127); 
   } 
 
void CColorsDlg::OnG153()  
   { 
    c_SpinG.SetPos(153); 
   } 
 
void CColorsDlg::OnG192()  
   { 
    c_SpinG.SetPos(192); 
   } 
 
void CColorsDlg::OnG204()  
   { 
    c_SpinG.SetPos(204); 
   } 
 
void CColorsDlg::OnG255()  
   { 
    c_SpinG.SetPos(255); 
   } 
 
//---------------- 
// Blue 
//---------------- 
 
void CColorsDlg::OnB0()  
   { 
    c_SpinB.SetPos(0); 
   } 
 
void CColorsDlg::OnB51()  
   { 
    c_SpinB.SetPos(51); 
   } 
 
void CColorsDlg::OnB102()  
   { 
    c_SpinB.SetPos(102); 
   } 
 
void CColorsDlg::OnB127()  
   { 
    c_SpinB.SetPos(127); 
   } 
 
void CColorsDlg::OnB153()  
   { 
    c_SpinB.SetPos(153); 
   } 
 
void CColorsDlg::OnB192()  
   { 
    c_SpinB.SetPos(192); 
   } 
 
void CColorsDlg::OnB204()  
   { 
    c_SpinB.SetPos(204); 
   } 
 
void CColorsDlg::OnB255()  
   { 
    c_SpinB.SetPos(255); 
   } 
 
/**************************************************************************** 
*                         CColorsDlg::OnSelendokStock 
* Result: void 
*        
* Effect:  
*       Selects the color values to be the values of the selected stock color 
****************************************************************************/ 
 
void CColorsDlg::OnSelendokStock()  
   { 
    int n = c_Stock.GetCurSel(); 
    if(n < 0) 
       return; 
    COLORREF c = (COLORREF)c_Stock.GetItemData(n); 
    c_SpinR.SetPos(GetRValue(c)); 
    c_SpinG.SetPos(GetGValue(c)); 
    c_SpinB.SetPos(GetBValue(c)); 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnLoad 
* Result: void 
*        
* Effect:  
*       Loads a file that contains RGB triples. Any line that contains 
*	RGB and parses the interval values as three integers will be 
*	added to the list in the order it is found in the input file 
****************************************************************************/ 
 
void CColorsDlg::OnLoad()  
   { 
    CFileDialog dlg(TRUE, _T("col")); 
    if(dlg.DoModal() != IDOK) 
       return; 
 
    CStdioFile f; 
    if(!f.Open(dlg.GetPathName(), CFile::modeRead)) 
       { /* failed */ 
	DWORD err = ::GetLastError(); 
	CString errmsg = ErrorString(err); 
	CString s; 
	s.Format(_T("File open failed\n%s\n%s"), dlg.GetPathName(), errmsg); 
	AfxMessageBox(s); 
	return; 
       } /* failed */ 
 
    c_Colors.ResetContent(); 
 
    enum { STATE_S0, STATE_R, STATE_G, STATE_B }; 
 
 
    CString s; 
    while(f.ReadString(s)) 
       { /* scan file */ 
	LPCTSTR pos = _tcsstr(s, _T("RGB")); 
	if(pos == NULL) 
	   continue; 
	// Simple scan. Look for three numbers starting at pos 
	int state = STATE_S0; 
	CString val; 
	BOOL valid = FALSE; 
	BYTE R; 
	BYTE G; 
	BYTE B; 
	while(*pos != _T('\0')) 
	   { /* scan RGB */ 
	    // RGB ( nnn, nnn, nnn) 
	    switch(MAKELONG(*pos, state)) 
	       { /* FSM */ 
		case MAKELONG(_T('R'), STATE_S0): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('G'), STATE_S0): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('B'), STATE_S0): 
		   pos++; 
		   continue; 
		case MAKELONG(_T(' '), STATE_S0): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('('), STATE_S0): 
		   state = STATE_R; 
		   pos++; 
		   continue; 
		case MAKELONG(_T(' '), STATE_R): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('0'), STATE_R): 
		case MAKELONG(_T('1'), STATE_R): 
		case MAKELONG(_T('2'), STATE_R): 
		case MAKELONG(_T('3'), STATE_R): 
		case MAKELONG(_T('4'), STATE_R): 
		case MAKELONG(_T('5'), STATE_R): 
		case MAKELONG(_T('6'), STATE_R): 
		case MAKELONG(_T('7'), STATE_R): 
		case MAKELONG(_T('8'), STATE_R): 
		case MAKELONG(_T('9'), STATE_R): 
		   val += *pos; 
                   pos++; 
		   continue; 
		case MAKELONG(_T(','), STATE_R): 
		   R = atoi(val); 
		   val = _T(""); 
		   pos++; 
		   state = STATE_G; 
		   continue; 
		case MAKELONG(_T(' '), STATE_G): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('0'), STATE_G): 
		case MAKELONG(_T('1'), STATE_G): 
		case MAKELONG(_T('2'), STATE_G): 
		case MAKELONG(_T('3'), STATE_G): 
		case MAKELONG(_T('4'), STATE_G): 
		case MAKELONG(_T('5'), STATE_G): 
		case MAKELONG(_T('6'), STATE_G): 
		case MAKELONG(_T('7'), STATE_G): 
		case MAKELONG(_T('8'), STATE_G): 
		case MAKELONG(_T('9'), STATE_G): 
		   val += *pos; 
		   pos++; 
		   continue; 
		case MAKELONG(_T(','), STATE_G): 
		   G = atoi(val); 
		   val = _T(""); 
		   pos++; 
		   state = STATE_B; 
		   continue; 
 		case MAKELONG(_T(' '), STATE_B): 
		   pos++; 
		   continue; 
		case MAKELONG(_T('0'), STATE_B): 
		case MAKELONG(_T('1'), STATE_B): 
		case MAKELONG(_T('2'), STATE_B): 
		case MAKELONG(_T('3'), STATE_B): 
		case MAKELONG(_T('4'), STATE_B): 
		case MAKELONG(_T('5'), STATE_B): 
		case MAKELONG(_T('6'), STATE_B): 
		case MAKELONG(_T('7'), STATE_B): 
		case MAKELONG(_T('8'), STATE_B): 
		case MAKELONG(_T('9'), STATE_B): 
		   val += *pos; 
		   pos++; 
		   continue; 
		case MAKELONG(_T(')'), STATE_B): 
		   B = atoi(val); 
		   val = _T(""); 
		   pos++; 
		   valid = TRUE; 
		   break; 
	       } /* FSM */ 
	    break; // we only get here if the switch does a 'break', and this 
		   // 'break' exits the loop (overloaded keyword) 
	   } /* scan RGB */ 
	if(valid) 
	   c_Colors.AddString((LPCTSTR)RGB(R, G, B)); 
	valid = FALSE; 
       } /* scan file */ 
    f.Close(); 
    updateControls(); 
   } 
 
/**************************************************************************** 
*                             CColorsDlg::OnClear 
* Result: void 
*        
* Effect:  
*       Clears the color samples 
****************************************************************************/ 
 
void CColorsDlg::OnClear()  
   { 
    c_Colors.ResetContent(); 
   } 
 
 
/**************************************************************************** 
*                           CColorsDlg::OnAboutBox 
* Result: void 
*        
* Effect:  
*       Shows the About... box 
****************************************************************************/ 
 
void CColorsDlg::OnAboutbox()  
   { 
    CAboutDlg dlgAbout; 
    dlgAbout.DoModal(); 
   }