OSDN Git Service

crystaledit: Make almost the same code into a common function
[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 #include "../utils/registry.h"
21
22 #ifdef _DEBUG
23 #define new DEBUG_NEW
24 #endif
25
26 #define REMEMBER_COUNT  64
27
28 /////////////////////////////////////////////////////////////////////////////
29 // CMemComboBox
30
31 BEGIN_MESSAGE_MAP (CMemComboBox, CComboBox)
32 //{{AFX_MSG_MAP(CMemComboBox)
33 ON_CONTROL_REFLECT (CBN_SETFOCUS, OnSetfocus)
34 //}}AFX_MSG_MAP
35 END_MESSAGE_MAP ()
36
37 ////////////////////////////////////////////////////////////////////////////////
38 // Methods
39
40 CMemComboBox::CMemComboBox () : m_bFirstFocus (true)
41 {
42 }
43
44 CMemComboBox::~CMemComboBox ()
45 {
46 }
47
48 /////////////////////////////////////////////////////////////////////////////
49 // CMemComboBox message handlers
50
51 CMap < CString, LPCTSTR, CString, LPCTSTR > CMemComboBox::groups;
52
53 void SetComboBoxHeight(CComboBox &Control)
54 {
55   int      nHeight = Control.GetCount(), nMax = ::GetSystemMetrics(SM_CYSCREEN) - 48;
56   CRect    rc;
57
58   Control.GetClientRect(rc);
59   Control.ClientToScreen(rc);
60   nHeight = rc.Height() * nHeight + 16;
61   if(rc.top + nHeight > nMax)
62     nHeight = nMax - rc.top;
63   Control.SetWindowPos(nullptr, 0, 0, rc.Width(), nHeight, SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
64 }
65
66 void SetComboBoxWidth(CComboBox &Control, LPCTSTR lpszText = nullptr)
67 {
68   int          cnt = Control.GetCount();
69
70   if(!cnt)
71     return;
72   CClientDC      dc(&Control);
73   NONCLIENTMETRICS  info = { 0 };
74   CFont        oFont, *oldFont;
75   int          width = 0, nMax = ::GetSystemMetrics(SM_CXSCREEN) - 48;
76   CRect        rc;
77   CSize        size;
78
79   SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0);
80   info.lfMenuFont.lfHeight = -MulDiv(9, dc.GetDeviceCaps(LOGPIXELSY), 72);
81   info.lfMenuFont.lfWidth = 0;
82   info.lfMenuFont.lfWeight = FW_THIN;
83   info.lfMenuFont.lfItalic = false;
84   info.lfMenuFont.lfUnderline = false;
85   info.lfMenuFont.lfCharSet = DEFAULT_CHARSET;
86   info.lfMenuFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
87   info.lfMenuFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
88   info.lfMenuFont.lfQuality = DEFAULT_QUALITY;
89   info.lfMenuFont.lfPitchAndFamily = FF_SWISS;
90   _tcscpy_s(info.lfMenuFont.lfFaceName, _T("MS Sans Serif"));
91   oFont.CreateFontIndirect(&info.lfMenuFont);
92   oldFont = dc.SelectObject(&oFont);
93   if(lpszText != nullptr && *lpszText != 0) {
94     size = dc.GetTextExtent(lpszText);
95     width = size.cx;
96   } else {
97     CString        item;
98
99     for(int i = 0; i < cnt; i++) {
100       Control.GetLBText(i, item);
101       size = dc.GetTextExtent(item);
102       if(size.cx > width)
103         width = size.cx;
104     }
105   }
106   Control.GetClientRect(rc);
107   Control.ClientToScreen(rc);
108   if(rc.left + width > nMax)
109     width = nMax - rc.left;
110   Control.SetDroppedWidth(width);
111   dc.SelectObject(oldFont);
112 }
113
114 void CMemComboBox::FillCurrent ()
115 {
116   CString strText;
117   GetWindowText (strText);
118   Fill (strText);
119 }
120
121 void CMemComboBox::
122 Fill (LPCTSTR text)
123 {
124   if (text && *text)
125     {
126       int ol = GetCount ();
127       int nPos = FindStringExact (-1, text);
128       if (nPos  != CB_ERR)
129         DeleteString (nPos);
130       InsertString (0, text);
131       int l = GetCount ();
132       if (l > REMEMBER_COUNT)
133         DeleteString (--l);
134       if (ol != l)
135         SetComboBoxHeight (*this);
136       SetComboBoxWidth (*this);
137       SetCurSel (0);
138       if (!m_sGroup.IsEmpty ())
139         {
140           CString item, items;
141           for (int i = 0; i < l; i++)
142             {
143               GetLBText (i, item);
144               items += item + _T ('\n');
145             }
146           groups.SetAt (m_sGroup, items);
147         }
148     } 
149 }
150
151 void CMemComboBox::
152 LoadSettings ()
153 {
154   CReg reg;
155   if (reg.Open (HKEY_CURRENT_USER, REG_EDITPAD, KEY_READ))
156     {
157       static LPCTSTR name[] = { _T("FindText"), _T("ReplaceText") };
158       CString value;
159
160       for (int i = 0; i < sizeof (name) / sizeof (name[0]); i++)
161         {
162           if (reg.LoadString (name[i], value))
163             {
164               groups.SetAt (name[i], value);
165             }
166         }
167     }
168 }
169
170 void CMemComboBox::
171 SaveSettings ()
172 {
173   CReg reg;
174   if (reg.Create (HKEY_CURRENT_USER, REG_EDITPAD, KEY_WRITE))
175     {
176       POSITION pos = groups.GetStartPosition ();
177       CString name, value;
178
179       while (pos)
180         {
181           groups.GetNextAssoc (pos, name, value);
182           VERIFY (reg.SaveString (name, value));
183         }
184     }
185 }
186
187 void CMemComboBox::
188 OnSetfocus ()
189 {
190   if (m_bFirstFocus && !m_sGroup.IsEmpty ())
191   {
192     m_bFirstFocus = false;
193     // create the dropdown list
194     CString items;
195     if (groups.Lookup (m_sGroup, items))
196     {
197       int p;
198       while ((p = items.Find (_T ('\n'))) != -1)
199       {
200         AddString (items.Left (p));
201         items = items.Mid (p + 1);
202       }
203       SetComboBoxHeight (*this);
204       SetComboBoxWidth (*this);
205     }
206     // we don't modify the windowText value as it may be initialized 
207     // before the dialog is shown
208   }
209 }
210
211