summaryrefslogtreecommitdiffstats
path: root/src/ColorSelectComboBox.cpp
blob: f014973f1454f86d581a6d0912b0dafeba07c5f5 (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
//******************************************************************************
// MIDIシーケンサーソフト『世界樹』
// 色選択コンボボックスクラス
// (C)2002-2012 おーぷんMIDIぷろじぇくと/くず
//******************************************************************************

/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */

/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "winver.h"
#include <afxwin.h>
#include <afxdlgs.h>
#include "ColorSelectComboBox.h"

// メッセージマップ
IMPLEMENT_DYNCREATE (CColorSelectComboBox, CComboBox)

// メッセージマップ
BEGIN_MESSAGE_MAP (CColorSelectComboBox, CComboBox)
	ON_CONTROL_REFLECT (CBN_SELCHANGE, OnSelChange)
END_MESSAGE_MAP ()

//------------------------------------------------------------------------------
// 構築と破壊
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// オーバーライド
//------------------------------------------------------------------------------

// ウィンドウスタイルの変更
BOOL CColorSelectComboBox::PreCreateWindow (CREATESTRUCT& cs) {
	if (!CComboBox::PreCreateWindow (cs)) {
		return FALSE;
	}
	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~(CBS_DROPDOWN | CBS_SIMPLE | CBS_OWNERDRAWVARIABLE | CBS_SORT);
	cs.style |= (CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS);
	return TRUE;
}

// 項目の高さ
void CColorSelectComboBox::MeasureItem (LPMEASUREITEMSTRUCT lpmis) {
	lpmis->itemHeight = 14;
}

// 項目の描画
void CColorSelectComboBox::DrawItem (LPDRAWITEMSTRUCT lpdis) {
	CDC dc;
	dc.Attach (lpdis->hDC);
	CRect rect = lpdis->rcItem;
	int nIndex = lpdis->itemID;

	CBrush* pBrush = new CBrush;
	pBrush->CreateSolidBrush (::GetSysColor ((lpdis->itemState &
		ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
	dc.FillRect (rect, pBrush);
	delete pBrush;

	if (lpdis->itemState & ODS_FOCUS) {
		dc.DrawFocusRect (rect);
	}

	if (nIndex != (UINT)-1) {
		COLORREF clrSample = (COLORREF)GetItemData (nIndex);
		if (clrSample == COLORSELECTCOMBOBOX_OTHER) {
			CFont* pFont = CFont::FromHandle 
				((HFONT)(::GetStockObject (DEFAULT_GUI_FONT)));
			CFont* pOldFont = dc.SelectObject (pFont);
			dc.SetBkMode (TRANSPARENT);
			dc.DrawText (_T("Others..."), rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
			dc.SelectObject (pOldFont);
		}
		else {
			CRect rcSample (rect);
			rcSample.DeflateRect (3, 3);
			dc.FillSolidRect (rcSample, clrSample);
		}
	}
	else {
		CRect rcSample (rect);
		rcSample.DeflateRect (3, 3);
		dc.FillSolidRect (rcSample, m_clrCurColor);		
	}
	dc.Detach ();
}

//------------------------------------------------------------------------------
// オペレーション
//------------------------------------------------------------------------------

// 現在の色を設定する(この関数はいつでも使える。)
BOOL CColorSelectComboBox::SetCurColor (COLORREF clr) {
	if (clr < 0x00000000 || clr > 0x00FFFFFF) {
		return FALSE;
	}
	m_clrCurColor = clr;
	if (m_hWnd != NULL) {
		int nCount = GetCount ();
		int i;
		// 一致するカラーサンプルを選択状態にする
		for (i = 0; i <= nCount; i++) {
			COLORREF clrSampleColor = GetItemData (i);
			if (clrSampleColor == m_clrCurColor) {
				SetCurSel (i);
				break;
			}
		}
		// 一致するカラーサンプルがなかったので選択解除
		if (i >= nCount) {
			SetCurSel (-1);
		}
		Invalidate ();
	}
	return TRUE;
}

// すべてのカラーサンプルをクリアする。
void CColorSelectComboBox::ClearSampleColor () {
	ResetContent ();
}

// カラーサンプルを追加する。
int CColorSelectComboBox::AddSampleColor (COLORREF clr) {
	int nIndex = AddString (_T(""));
	if (nIndex == CB_ERR || nIndex == CB_ERRSPACE) {
		return nIndex;
	}
	return SetSampleColor (nIndex , (DWORD)clr);
}

// カラーサンプルを取得する。
COLORREF CColorSelectComboBox::GetSampleColor (int nIndex) const {
	return (COLORREF)GetItemData (nIndex);
}

// カラーサンプルを設定する。
int CColorSelectComboBox::SetSampleColor (int nIndex, COLORREF clr) {
	int nRet = SetItemData (nIndex, (DWORD)clr);
	int nCurSel = GetCurSel ();
	// 新しいカラーサンプルとカレントカラーの一致チェック
	if (nCurSel == CB_ERR || nCurSel == nIndex) { 
		COLORREF clrSampleColor = GetItemData (nIndex);
		// 新しいカラーサンプルとカレントカラーは一致したので選択
		if (clrSampleColor == m_clrCurColor) {
			SetCurSel (nIndex);
		}
		// 新しいカラーサンプルとカレントカラーは一致しなかったので選択解除
		else {
			SetCurSel (-1);
		}
	}
	return nRet;
}

//------------------------------------------------------------------------------
// メッセージマップ
//------------------------------------------------------------------------------

// カレントセルが変更されたとき(メッセージリフレクション)
void CColorSelectComboBox::OnSelChange () {
	int nCurSel = GetCurSel ();
	if (0 <= nCurSel && nCurSel < GetCount ()) {
		COLORREF clrCurColor = (COLORREF)GetItemData (nCurSel);
		if (clrCurColor == COLORSELECTCOMBOBOX_OTHER) {
			CColorDialog theDlg;
			theDlg.m_cc.Flags |= (CC_FULLOPEN | CC_RGBINIT);
			theDlg.m_cc.rgbResult = m_clrCurColor;
			if (theDlg.DoModal () == IDOK) {
				m_clrCurColor = theDlg.GetColor ();
			}
			SetCurSel (-1);
		}
		else {
			m_clrCurColor = clrCurColor;
		}
		Invalidate ();
	}
}