OSDN Git Service

refactor
[winmerge-jp/winmerge-jp.git] / Externals / crystaledit / editlib / dialogs / memcombo.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 //  File:    memcombo.cpp
3 //  Version: 1.1.0.4
4 //  Updated: 19-Jul-1998
5 //
6 //  Copyright:  Ferdinand Prantl
7 //  E-mail:     prantl@ff.cuni.cz
8 //
9 //  Combo-box saving last typed expressions
10 //
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 ////////////////////////////////////////////////////////////////////////////
16
17 #include "StdAfx.h"
18 #include "editreg.h"
19 #include "memcombo.h"
20
21 #ifdef _DEBUG
22 #define new DEBUG_NEW
23 #endif
24
25 constexpr int REMEMBER_COUNT = 64;
26
27 /////////////////////////////////////////////////////////////////////////////
28 // CMemComboBox
29
30 BEGIN_MESSAGE_MAP (CMemComboBox, CComboBox)
31 //{{AFX_MSG_MAP(CMemComboBox)
32 ON_CONTROL_REFLECT (CBN_SETFOCUS, OnSetfocus)
33 //}}AFX_MSG_MAP
34 END_MESSAGE_MAP ()
35
36 ////////////////////////////////////////////////////////////////////////////////
37 // Methods
38
39 CMemComboBox::CMemComboBox () : m_bFirstFocus (true)
40 {
41 }
42
43 CMemComboBox::~CMemComboBox ()
44 {
45 }
46
47 /////////////////////////////////////////////////////////////////////////////
48 // CMemComboBox message handlers
49
50 CMap < CString, const tchar_t*, CString, const tchar_t* > CMemComboBox::groups;
51
52 void SetComboBoxHeight(CComboBox &Control)
53 {
54   int      nHeight = Control.GetCount(), nMax = ::GetSystemMetrics(SM_CYSCREEN) - 48;
55   CRect    rc;
56
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);
63 }
64
65 void SetComboBoxWidth(CComboBox &Control, const tchar_t* lpszText = nullptr)
66 {
67   int          cnt = Control.GetCount();
68
69   if(!cnt)
70     return;
71   CClientDC      dc(&Control);
72   CFont        *oldFont;
73   int          width = 0, nMax = ::GetSystemMetrics(SM_CXSCREEN) - 48;
74   CRect        rc;
75   CSize        size;
76
77   CFont *pFont = Control.GetFont();
78   oldFont = dc.SelectObject(pFont);
79   if(lpszText != nullptr && *lpszText != 0) {
80     size = dc.GetTextExtent(lpszText);
81     width = size.cx;
82   } else {
83     CString        item;
84
85     for(int i = 0; i < cnt; i++) {
86       Control.GetLBText(i, item);
87       size = dc.GetTextExtent(item);
88       if(size.cx > width)
89         width = size.cx;
90     }
91   }
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);
99 }
100
101 void CMemComboBox::FillCurrent ()
102 {
103   CString strText;
104   GetWindowText (strText);
105   Fill (strText);
106 }
107
108 void CMemComboBox::
109 Fill (const tchar_t* text)
110 {
111   if (text && *text)
112     {
113       int ol = GetCount ();
114       int nPos = FindStringExact (-1, text);
115       if (nPos  != CB_ERR)
116         DeleteString (nPos);
117       InsertString (0, text);
118       int l = GetCount ();
119       if (l > REMEMBER_COUNT)
120         DeleteString (--l);
121       if (ol != l)
122         SetComboBoxHeight (*this);
123       SetComboBoxWidth (*this);
124       SetCurSel (0);
125       if (!m_sGroup.IsEmpty ())
126         {
127           CString item, items;
128           for (int i = 0; i < l; i++)
129             {
130               GetLBText (i, item);
131               items += item + _T ('\n');
132             }
133           groups.SetAt (m_sGroup, items);
134         }
135     } 
136 }
137
138 void CMemComboBox::
139 LoadSettings ()
140 {
141   static const tchar_t* name[] = { _T("FindText"), _T("ReplaceText") };
142
143   for (int i = 0; i < sizeof (name) / sizeof (name[0]); i++)
144     {
145       auto value = AfxGetApp ()->GetProfileString (EDITPAD_SECTION, name[i], _T(""));
146       if (!value.IsEmpty ())
147         groups.SetAt (name[i], value);
148     }
149 }
150
151 void CMemComboBox::
152 SaveSettings ()
153 {
154   POSITION pos = groups.GetStartPosition ();
155   CString name, value;
156
157   while (pos)
158     {
159       groups.GetNextAssoc (pos, name, value);
160       VERIFY (AfxGetApp ()->WriteProfileString (EDITPAD_SECTION, name, value));
161     }
162   
163 }
164
165 void CMemComboBox::
166 OnSetfocus ()
167 {
168   if (m_bFirstFocus && !m_sGroup.IsEmpty ())
169   {
170     m_bFirstFocus = false;
171     // create the dropdown list
172     CString items;
173     if (groups.Lookup (m_sGroup, items))
174     {
175       int p;
176       while ((p = items.Find (_T ('\n'))) != -1)
177       {
178         AddString (items.Left (p));
179         items = items.Mid (p + 1);
180       }
181       SetComboBoxHeight (*this);
182       SetComboBoxWidth (*this);
183     }
184     // we don't modify the windowText value as it may be initialized 
185     // before the dialog is shown
186   }
187 }
188
189