1 ///////////////////////////////////////////////////////////////////////////
4 // Updated: 19-Jul-1998
6 // Copyright: Ferdinand Prantl
7 // E-mail: prantl@ff.cuni.cz
9 // Combo-box saving last typed expressions
11 // You are free to use or modify this code to the following restrictions:
12 // - Acknowledge me somewhere in your about box, simple "Parts of code by.."
13 // will be enough. If you can't (or don't want to), contact me personally.
14 // - LEAVE THIS HEADER INTACT
15 ////////////////////////////////////////////////////////////////////////////
25 constexpr int REMEMBER_COUNT = 64;
27 /////////////////////////////////////////////////////////////////////////////
30 BEGIN_MESSAGE_MAP (CMemComboBox, CComboBox)
31 //{{AFX_MSG_MAP(CMemComboBox)
32 ON_CONTROL_REFLECT (CBN_SETFOCUS, OnSetfocus)
36 ////////////////////////////////////////////////////////////////////////////////
39 CMemComboBox::CMemComboBox () : m_bFirstFocus (true)
43 CMemComboBox::~CMemComboBox ()
47 /////////////////////////////////////////////////////////////////////////////
48 // CMemComboBox message handlers
50 CMap < CString, const tchar_t*, CString, const tchar_t* > CMemComboBox::groups;
52 void SetComboBoxHeight(CComboBox &Control)
54 int nHeight = Control.GetCount(), nMax = ::GetSystemMetrics(SM_CYSCREEN) - 48;
57 Control.GetClientRect(rc);
58 Control.ClientToScreen(rc);
59 nHeight = rc.Height() * nHeight + 16;
60 if(rc.top + nHeight > nMax)
61 nHeight = nMax - rc.top;
62 Control.SetWindowPos(nullptr, 0, 0, rc.Width(), nHeight, SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
65 void SetComboBoxWidth(CComboBox &Control, const tchar_t* lpszText = nullptr)
67 int cnt = Control.GetCount();
71 CClientDC dc(&Control);
73 int width = 0, nMax = ::GetSystemMetrics(SM_CXSCREEN) - 48;
77 CFont *pFont = Control.GetFont();
78 oldFont = dc.SelectObject(pFont);
79 if(lpszText != nullptr && *lpszText != 0) {
80 size = dc.GetTextExtent(lpszText);
85 for(int i = 0; i < cnt; i++) {
86 Control.GetLBText(i, item);
87 size = dc.GetTextExtent(item);
92 width += GetSystemMetrics(SM_CXVSCROLL) + 2 * GetSystemMetrics(SM_CXEDGE);
93 Control.GetClientRect(rc);
94 Control.ClientToScreen(rc);
95 if(rc.left + width > nMax)
96 width = nMax - rc.left;
97 Control.SetDroppedWidth(width);
98 dc.SelectObject(oldFont);
101 void CMemComboBox::FillCurrent ()
104 GetWindowText (strText);
109 Fill (const tchar_t* text)
113 int ol = GetCount ();
114 int nPos = FindStringExact (-1, text);
117 InsertString (0, text);
119 if (l > REMEMBER_COUNT)
122 SetComboBoxHeight (*this);
123 SetComboBoxWidth (*this);
125 if (!m_sGroup.IsEmpty ())
128 for (int i = 0; i < l; i++)
131 items += item + _T ('\n');
133 groups.SetAt (m_sGroup, items);
141 static const tchar_t* name[] = { _T("FindText"), _T("ReplaceText") };
143 for (int i = 0; i < sizeof (name) / sizeof (name[0]); i++)
145 auto value = AfxGetApp ()->GetProfileString (EDITPAD_SECTION, name[i], _T(""));
146 if (!value.IsEmpty ())
147 groups.SetAt (name[i], value);
154 POSITION pos = groups.GetStartPosition ();
159 groups.GetNextAssoc (pos, name, value);
160 VERIFY (AfxGetApp ()->WriteProfileString (EDITPAD_SECTION, name, value));
168 if (m_bFirstFocus && !m_sGroup.IsEmpty ())
170 m_bFirstFocus = false;
171 // create the dropdown list
173 if (groups.Lookup (m_sGroup, items))
176 while ((p = items.Find (_T ('\n'))) != -1)
178 AddString (items.Left (p));
179 items = items.Mid (p + 1);
181 SetComboBoxHeight (*this);
182 SetComboBoxWidth (*this);
184 // we don't modify the windowText value as it may be initialized
185 // before the dialog is shown