AdvComboBox.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. /////////////////////////////////////////////////////////////////////////////
  2. // AdvComboBox.cpp : implementation file
  3. //
  4. // CAdvComboBox Control
  5. // Version: 2.1
  6. // Date: September 2002
  7. // Author: Mathias Tunared
  8. // Email: Mathias@inorbit.com
  9. // Copyright (c) 2002. All Rights Reserved.
  10. //
  11. // This code, in compiled form or as source code, may be redistributed
  12. // unmodified PROVIDING it is not sold for profit without the authors
  13. // written consent, and providing that this notice and the authors name
  14. // and all copyright notices remains intact.
  15. //
  16. // This file is provided "as is" with no expressed or implied warranty.
  17. // The author accepts no liability for any damage/loss of business that
  18. // this product may cause.
  19. //
  20. /////////////////////////////////////////////////////////////////////////////
  21. #include "stdafx.h"
  22. #include "AdvComboBox.h"
  23. #include "VisualStylesXP.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. #define IDC_COMBOEDIT 108
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CAdvComboBox
  32. IMPLEMENT_DYNAMIC(CAdvComboBox, CWnd)
  33. CAdvComboBox::CAdvComboBox( BOOL bInst, CWnd * pParent ) :
  34. m_pDropWnd(0),
  35. m_bDropListVisible(0),
  36. m_bInst( bInst ),
  37. m_pParent(pParent)
  38. {
  39. XLISTCTRL_TRACE(_T("in CAdvComboBox::CAdvComboBox\n"));
  40. RegisterWindowClass();
  41. m_pEdit = NULL;
  42. m_zDelta = 0;
  43. m_nCurSel = -1;
  44. m_bDropRectStored= false;
  45. m_bHasFocus = false;
  46. m_bHasSentFocus = false;
  47. m_bSelItem = false;
  48. m_bFirstPaint = true;
  49. m_nMinVisItems = 5;
  50. m_bCodeCreate = false;
  51. m_bAutoAppend = TRUE;
  52. m_bDropButtonHot = false;
  53. m_bTrackMouseLeave = false;
  54. m_nDefaultDropItems = -1;
  55. m_dwACBStyle = 0;
  56. m_dwACBStyle |= ACBS_STANDARD;
  57. m_pFont = NULL;
  58. }
  59. CAdvComboBox::~CAdvComboBox()
  60. {
  61. XLISTCTRL_TRACE(_T("in CAdvComboBox::~CAdvComboBox\n"));
  62. delete m_pFont;
  63. if( m_pDropWnd )
  64. {
  65. m_pDropWnd->ShowWindow( SW_HIDE );
  66. m_bDropListVisible = FALSE;
  67. m_pDropWnd->DestroyWindow();
  68. delete m_pDropWnd;
  69. m_pDropWnd = NULL;
  70. }
  71. if( m_pEdit )
  72. {
  73. m_pEdit->DestroyWindow();
  74. delete m_pEdit;
  75. }
  76. }
  77. BEGIN_MESSAGE_MAP(CAdvComboBox, CWnd)
  78. //{{AFX_MSG_MAP(CAdvComboBox)
  79. ON_WM_CREATE()
  80. ON_WM_PAINT()
  81. ON_WM_ERASEBKGND()
  82. ON_WM_LBUTTONDOWN()
  83. ON_WM_MOUSEWHEEL()
  84. ON_WM_SIZE()
  85. ON_WM_SETFOCUS()
  86. ON_WM_KILLFOCUS()
  87. ON_WM_SHOWWINDOW()
  88. ON_WM_ENABLE()
  89. ON_WM_CHILDACTIVATE()
  90. ON_WM_MOUSEMOVE()
  91. ON_WM_TIMER()
  92. ON_WM_CHAR()
  93. ON_WM_KEYDOWN()
  94. //ON_WM_ACTIVATEAPP()
  95. //}}AFX_MSG_MAP
  96. ON_MESSAGE( WM_SELECTED_ITEM, OnSelectedItem )
  97. ON_MESSAGE( WM_ON_DROPDOWN_BUTTON, OnDropdownButton )
  98. ON_MESSAGE( WM_DESTROY_DROPLIST, OnDestroyDropdownList )
  99. ON_EN_KILLFOCUS(IDC_COMBOEDIT, OnKillfocusEdit)
  100. ON_EN_SETFOCUS(IDC_COMBOEDIT, OnSetfocusEdit)
  101. ON_EN_CHANGE(IDC_COMBOEDIT, OnChangeEdit)
  102. ON_EN_UPDATE(IDC_COMBOEDIT, OnUpdateEdit)
  103. ON_MESSAGE( CB_ADDSTRING, OnAddString )
  104. ON_MESSAGE( CB_SETCURSEL, OnSetCurSel )
  105. ON_MESSAGE( CB_GETCURSEL, OnGetCurSel )
  106. ON_MESSAGE( CB_SELECTSTRING, OnSelectString )
  107. ON_MESSAGE( CB_GETCOUNT, OnGetCount )
  108. ON_MESSAGE( CB_RESETCONTENT, OnResetContent )
  109. ON_MESSAGE( CB_GETLBTEXT, OnGetLBText )
  110. ON_MESSAGE( CB_GETLBTEXTLEN, OnGetLBTextLen )
  111. ON_MESSAGE( CB_GETTOPINDEX, OnGetTopIndex )
  112. ON_MESSAGE( CB_SETTOPINDEX, OnSetTopIndex )
  113. END_MESSAGE_MAP()
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CAdvComboBox message handlers
  116. LONG CAdvComboBox::OnAddString( WPARAM /*wParam*/, LPARAM lString )
  117. {
  118. TCHAR* pStr = (TCHAR*)lString;
  119. return AddString( pStr );
  120. }
  121. LONG CAdvComboBox::OnSetCurSel( WPARAM wIndex, LPARAM /*lParam*/ )
  122. {
  123. int nIndex = (int)wIndex;
  124. return SetCurSel( nIndex );
  125. }
  126. LONG CAdvComboBox::OnGetCurSel( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  127. {
  128. return GetCurSel();
  129. }
  130. LONG CAdvComboBox::OnSelectString( WPARAM wItemStart, LPARAM lString )
  131. {
  132. int nItem = (int)wItemStart;
  133. TCHAR* pStr = (TCHAR*)lString;
  134. int nIndex = FindStringExact( nItem, pStr );
  135. return SetCurSel( nIndex );
  136. }
  137. LONG CAdvComboBox::OnGetCount( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  138. {
  139. return GetCount();
  140. }
  141. LONG CAdvComboBox::OnResetContent( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  142. {
  143. m_list.clear();
  144. m_strEdit = _T("");
  145. m_nCurSel = -1;
  146. SetWindowText( _T("") );
  147. if( m_pEdit )
  148. {
  149. m_pEdit->SetWindowText( _T("") );
  150. }
  151. Invalidate();
  152. return CB_OKAY;
  153. }
  154. LONG CAdvComboBox::OnGetLBText( WPARAM wIndex, LPARAM lString )
  155. {
  156. int nIndex = (int)wIndex;
  157. TCHAR* pOutStr = (TCHAR*)lString;
  158. return GetLBText( nIndex, pOutStr );
  159. }
  160. LONG CAdvComboBox::OnGetLBTextLen( WPARAM wIndex, LPARAM /*lParam*/ )
  161. {
  162. int nIndex = (int)wIndex;
  163. return GetLBTextLen( nIndex );
  164. }
  165. LONG CAdvComboBox::OnGetTopIndex( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  166. {
  167. return GetTopIndex();
  168. }
  169. LONG CAdvComboBox::OnSetTopIndex( WPARAM wIndex, LPARAM /*lParam*/ )
  170. {
  171. return SetTopIndex(wIndex);
  172. }
  173. BOOL CAdvComboBox::RegisterWindowClass()
  174. {
  175. WNDCLASS wndcls;
  176. HINSTANCE hInst;
  177. hInst = AfxGetInstanceHandle();
  178. ASSERT( hInst != 0 );
  179. if( !(::GetClassInfo(hInst, ADVCOMBOBOXCTRL_CLASSNAME, &wndcls)) )
  180. {
  181. wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  182. wndcls.lpfnWndProc = ::DefWindowProc;
  183. wndcls.cbClsExtra = 0;
  184. wndcls.cbWndExtra = 0;
  185. wndcls.hInstance = hInst;
  186. wndcls.hIcon = NULL;
  187. wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  188. wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW);
  189. wndcls.lpszMenuName = NULL;
  190. wndcls.lpszClassName = ADVCOMBOBOXCTRL_CLASSNAME;
  191. if( !AfxRegisterClass(&wndcls) )
  192. {
  193. AfxThrowResourceException();
  194. return FALSE;
  195. }
  196. }
  197. return TRUE;
  198. }
  199. BOOL CAdvComboBox::Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
  200. {
  201. XLISTCTRL_TRACE(_T("in CAdvComboBox::Create\n"));
  202. m_rcCombo = rect;
  203. m_bCodeCreate = true;
  204. m_dwACBStyle |= ACBS_STANDARD;
  205. m_dwStyle = dwStyle;
  206. LoadString( nID );
  207. BOOL bRet = CWnd::Create(NULL, _T(""), dwStyle, m_rcCombo, pParentWnd, nID );
  208. return bRet;
  209. }
  210. void CAdvComboBox::LoadString( UINT nStringID )
  211. {
  212. UINT nIDS;
  213. if( nStringID )
  214. nIDS = nStringID;
  215. else
  216. {
  217. nIDS = GetWindowLong( GetSafeHwnd(), GWL_ID );
  218. }
  219. //
  220. // Load string from resources
  221. CString strItems;
  222. if( !strItems.LoadString( nIDS ) )
  223. {
  224. return;
  225. }
  226. //
  227. // Clear the list
  228. m_list.clear();
  229. //
  230. // Go through the string and look after '\n'.
  231. TCHAR seps[] = _T("\n");
  232. TCHAR *token;
  233. int nLen = strItems.GetLength();
  234. TCHAR* szTok = new TCHAR[nLen+5];
  235. memset( szTok, 0, (nLen+5)*sizeof(TCHAR) );
  236. _tcscpy_s( szTok, nLen + 5, (LPCTSTR)strItems );
  237. TCHAR *next_token = NULL;
  238. token = _tcstok_s( szTok, seps, &next_token );
  239. while( token != NULL )
  240. {
  241. AddString( token );
  242. token = _tcstok_s( NULL, seps, &next_token );
  243. }
  244. //
  245. // Add item to list
  246. //+++
  247. delete [] szTok;
  248. }
  249. int CAdvComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  250. {
  251. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnCreate\n"));
  252. if (CWnd::OnCreate(lpCreateStruct) == -1)
  253. return -1;
  254. if( !m_pFont )
  255. {
  256. LOGFONT logFont;
  257. memset( &logFont, 0, sizeof(LOGFONT) );
  258. CFont* pF = m_pParent->GetFont();
  259. ASSERT(pF);
  260. pF->GetLogFont( &logFont );
  261. m_pFont = new CFont;
  262. m_pFont->CreateFontIndirect(&logFont);
  263. SetFont( m_pFont );
  264. }
  265. return 0;
  266. }
  267. void CAdvComboBox::PreSubclassWindow()
  268. {
  269. XLISTCTRL_TRACE(_T("in CAdvComboBox::PreSubclassWindow\n"));
  270. if (m_pParent == NULL)
  271. m_pParent = GetParent();
  272. ASSERT(m_pParent);
  273. // TODO: Add your specialized code here and/or call the base class
  274. LoadString();
  275. if( !m_pFont )
  276. {
  277. LOGFONT logFont;
  278. memset( &logFont, 0, sizeof(LOGFONT) );
  279. CFont* pF = m_pParent->GetFont();
  280. ASSERT(pF);
  281. pF->GetLogFont( &logFont );
  282. m_pFont = new CFont;
  283. m_pFont->CreateFontIndirect(&logFont);
  284. SetFont( m_pFont );
  285. }
  286. if( !m_bCodeCreate )
  287. {
  288. //
  289. // Do we need to show an edit control. (CBS_DROPDOWN)
  290. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  291. {
  292. if( !m_pEdit )
  293. {
  294. CRect rect;
  295. GetClientRect(rect);
  296. rect.right = rect.right - ::GetSystemMetrics(SM_CXHSCROLL);
  297. if( m_dwACBStyle & ACBS_STANDARD )
  298. {
  299. rect.DeflateRect( 2, 2 );
  300. rect.left += 1;
  301. rect.top += 2;
  302. //rect.bottom += 2;
  303. }
  304. XLISTCTRL_TRACE(_T("creating edit\n"));
  305. m_pEdit = new CAdvComboEdit; //+++
  306. ASSERT(m_pEdit);
  307. DWORD dwStyle = 0;
  308. dwStyle = WS_VISIBLE | WS_CHILD;
  309. if( GetStyle() & CBS_AUTOHSCROLL )
  310. {
  311. dwStyle |= ES_AUTOHSCROLL;
  312. }
  313. VERIFY(m_pEdit->Create( dwStyle, rect, this, IDC_COMBOEDIT ));
  314. m_pEdit->SetFont( m_pFont );
  315. m_pEdit->SetWindowText( m_strEdit.c_str() );
  316. }
  317. }
  318. }
  319. CWnd::PreSubclassWindow();
  320. }
  321. void CAdvComboBox::OnChildActivate()
  322. {
  323. CWnd::OnChildActivate();
  324. // TODO: Add your message handler code here
  325. if( !m_pFont )
  326. {
  327. LOGFONT logFont;
  328. memset( &logFont, 0, sizeof(LOGFONT) );
  329. CFont* pF = m_pParent->GetFont();
  330. pF->GetLogFont( &logFont );
  331. m_pFont = new CFont;
  332. m_pFont->CreateFontIndirect(&logFont);
  333. SetFont( m_pFont );
  334. }
  335. if( m_bCodeCreate )
  336. {
  337. //
  338. // Do we need to show an edit control. (CBS_DROPDOWN)
  339. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  340. {
  341. if( !m_pEdit )
  342. {
  343. CRect rect;
  344. GetClientRect(rect);
  345. rect.right = rect.right - ::GetSystemMetrics(SM_CXHSCROLL);
  346. if( m_dwACBStyle & ACBS_STANDARD )
  347. {
  348. rect.DeflateRect( 2, 2 );
  349. rect.left += 1;
  350. rect.top += 2;
  351. //rect.bottom += 2;
  352. }
  353. m_pEdit = new CAdvComboEdit; //+++
  354. DWORD dwStyle = 0;
  355. dwStyle = WS_VISIBLE | WS_CHILD;
  356. if( GetStyle() & CBS_AUTOHSCROLL )
  357. {
  358. dwStyle |= ES_AUTOHSCROLL;
  359. }
  360. m_pEdit->Create( dwStyle, rect, this, IDC_COMBOEDIT );
  361. m_pEdit->SetFont( m_pFont );
  362. m_pEdit->SetWindowText( m_strEdit.c_str() );
  363. }
  364. }
  365. }
  366. }
  367. void CAdvComboBox::OnPaint()
  368. {
  369. CPaintDC dc(this); // device context for painting
  370. if( m_nCurSel != -1 )
  371. {
  372. m_iter = m_list.begin();
  373. advance( m_iter, m_nCurSel );
  374. if( m_iter != m_list.end() )
  375. {
  376. m_strEdit = m_iter->strText;
  377. }
  378. if( m_bFirstPaint )
  379. {
  380. if( m_pEdit )
  381. {
  382. m_pEdit->SetWindowText( m_strEdit.c_str() );
  383. m_bFirstPaint = false;
  384. m_pEdit->EnableWindow( IsWindowEnabled() );
  385. }
  386. }
  387. }
  388. CRect rect;
  389. CRect rcText;
  390. GetClientRect(rect);
  391. rcText = rect;
  392. rect.left = rect.right - ::GetSystemMetrics(SM_CXHSCROLL);
  393. rcText.right = rect.left-1;
  394. m_rcDropButton = rect;
  395. GetClientRect(rect);
  396. BOOL bWndEnabled = IsWindowEnabled();
  397. COLORREF clrDisabledBkg = ::GetSysColor(COLOR_BTNFACE);
  398. COLORREF clrDisabledBorder = ::GetSysColor(COLOR_3DDKSHADOW);
  399. COLORREF clrDisabledText = ::GetSysColor(COLOR_GRAYTEXT);
  400. if( !bWndEnabled )
  401. {
  402. if( 1 ) // Draw disabled flat control with border? Change to '0'.
  403. {
  404. dc.FillSolidRect( rect, clrDisabledBkg );
  405. }
  406. else
  407. {
  408. CBrush brDisabled(clrDisabledBkg);
  409. CBrush* pOldBrush = dc.SelectObject(&brDisabled);
  410. CPen penDisabled( PS_SOLID, 0, clrDisabledBorder);
  411. CPen* pOldPen = dc.SelectObject(&penDisabled);
  412. dc.Rectangle(rect);
  413. dc.SelectObject(pOldBrush);
  414. dc.SelectObject(pOldPen);
  415. }
  416. }
  417. else
  418. {
  419. COLORREF clrEnabledBkg = ::GetSysColor(COLOR_WINDOW);
  420. dc.FillSolidRect( rect, clrEnabledBkg );
  421. }
  422. DWORD dwBtnStyle = 0;
  423. if( !bWndEnabled )
  424. {
  425. dwBtnStyle |= DFCS_INACTIVE;
  426. }
  427. dwBtnStyle |= m_bDropListVisible ? (DFCS_SCROLLDOWN|DFCS_PUSHED|DFCS_FLAT) : DFCS_SCROLLDOWN;
  428. BOOL bThemeActive = FALSE;
  429. HRESULT hr;
  430. bThemeActive = g_xpStyle.UseVisualStyles();
  431. HTHEME hTheme = NULL;
  432. if( bThemeActive )
  433. hTheme = g_xpStyle.OpenThemeData( m_hWnd, L"COMBOBOX" );
  434. // Theme drop btn style
  435. int nDropBtnThemeStyle = 0;
  436. if( m_bDropListVisible )
  437. {
  438. nDropBtnThemeStyle = CBXS_PRESSED;
  439. }
  440. else
  441. {
  442. nDropBtnThemeStyle = CBXS_NORMAL;
  443. if( m_bDropButtonHot )
  444. nDropBtnThemeStyle = CBXS_HOT;
  445. if( !bWndEnabled )
  446. nDropBtnThemeStyle = CBXS_DISABLED;
  447. }
  448. #if 0 // -----------------------------------------------------------
  449. if( m_dwACBStyle & ACBS_FLAT )
  450. {
  451. if( bThemeActive )
  452. {
  453. hr = g_xpStyle.DrawThemeBackground( hTheme, dc.m_hDC, CP_DROPDOWNBUTTON, nDropBtnThemeStyle, &m_rcDropButton, NULL);
  454. }
  455. else
  456. {
  457. dc.DrawFrameControl(m_rcDropButton, DFC_SCROLL, dwBtnStyle );
  458. }
  459. }
  460. else
  461. if( m_dwACBStyle & ACBS_STANDARD )
  462. #endif // -----------------------------------------------------------
  463. {
  464. if( bThemeActive )
  465. {
  466. COLORREF clrBorder;
  467. hr = g_xpStyle.GetThemeColor( hTheme, BP_PUSHBUTTON, bWndEnabled ? PBS_NORMAL : PBS_DISABLED, TMT_BORDERCOLOR, &clrBorder );
  468. if( FAILED( hr ) )
  469. {
  470. clrBorder = RGB(0,0,0);
  471. }
  472. CPen penBorder( PS_SOLID, 0, clrBorder );
  473. CPen* oldBorderPen = dc.SelectObject( &penBorder );
  474. dc.Rectangle( &rect );
  475. m_rcDropButton.DeflateRect(0,1,0,1);
  476. m_rcDropButton.left -= 1;
  477. m_rcDropButton.right -= 1;
  478. if( !bWndEnabled )
  479. {
  480. COLORREF clrDisabledLightBorder;
  481. COLORREF clrDisabledFill;
  482. hr = g_xpStyle.GetThemeColor( hTheme, BP_PUSHBUTTON, bWndEnabled ? PBS_NORMAL : PBS_DISABLED, TMT_FILLCOLOR, &clrDisabledLightBorder );
  483. if( FAILED( hr ) )
  484. {
  485. clrDisabledLightBorder = RGB(255,255,255);
  486. }
  487. hr = g_xpStyle.GetThemeColor( hTheme, WP_DIALOG, 0, TMT_FILLCOLOR, &clrDisabledFill );
  488. if( FAILED( hr ) )
  489. {
  490. clrDisabledFill = RGB(255,0,0);
  491. }
  492. CPen penDisBorder( PS_SOLID, 0, clrDisabledLightBorder );
  493. CBrush brFill( clrDisabledBkg );//clrDisabledFill );
  494. CRect rcl = rect;
  495. rcl.DeflateRect(1,1);
  496. rcl.right = m_rcDropButton.left;
  497. CBrush *oldBr = dc.SelectObject( &brFill );
  498. dc.SelectObject( &penDisBorder );
  499. dc.Rectangle( &rcl );
  500. dc.SelectObject( oldBr );
  501. }
  502. dc.SelectObject( &oldBorderPen );
  503. // Button
  504. hr = g_xpStyle.DrawThemeBackground( hTheme, dc.m_hDC, CP_DROPDOWNBUTTON, nDropBtnThemeStyle, &m_rcDropButton, NULL);
  505. }
  506. else
  507. {
  508. COLORREF clrTopLeft = ::GetSysColor(COLOR_3DSHADOW);
  509. COLORREF clrBottomRight = ::GetSysColor(COLOR_3DHILIGHT);
  510. dc.Draw3dRect( &rect, clrTopLeft, clrBottomRight );
  511. clrTopLeft = ::GetSysColor(COLOR_3DDKSHADOW);
  512. clrBottomRight = ::GetSysColor(COLOR_3DLIGHT);
  513. rect.DeflateRect(1,1);
  514. dc.Draw3dRect( &rect, clrTopLeft, clrBottomRight );
  515. m_rcDropButton.DeflateRect(0,2,0,2);
  516. m_rcDropButton.left -= 2;
  517. m_rcDropButton.right -= 2;
  518. // Button
  519. dc.DrawFrameControl(m_rcDropButton, DFC_SCROLL, dwBtnStyle );
  520. }
  521. //
  522. // Adjust rects
  523. rcText.DeflateRect(4,3,2,3);
  524. }
  525. if( bThemeActive )
  526. hr = g_xpStyle.CloseThemeData( hTheme );
  527. if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
  528. {
  529. //
  530. // Draw Text as selected
  531. COLORREF clrBackground;
  532. COLORREF clrOldBkColor;
  533. COLORREF clrOldTextColor;
  534. clrBackground = ::GetSysColor(COLOR_HIGHLIGHT);
  535. clrOldBkColor = dc.SetBkColor( clrBackground );
  536. // clrOldTextColor = dc.SetTextColor( ::GetSysColor(COLOR_HIGHLIGHTTEXT) );
  537. int nOldBkMode = dc.SetBkMode( TRANSPARENT );
  538. CFont* pOldFont = dc.SelectObject( m_pFont );
  539. rcText.top -= 2;
  540. rcText.bottom += 2;
  541. rcText.left -= 2;
  542. rcText.right += 1;
  543. if( m_bHasFocus && !m_bDropListVisible )
  544. {
  545. dc.FillSolidRect( rcText, bWndEnabled ? clrBackground : clrDisabledBkg );
  546. clrOldTextColor = dc.SetTextColor(
  547. bWndEnabled ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : clrDisabledText );
  548. dc.DrawText( m_strEdit.c_str(), &rcText, DT_SINGLELINE|DT_VCENTER);
  549. }
  550. else
  551. {
  552. //+++dc.FillSolidRect( rcText,
  553. //+++ bWndEnabled ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : clrDisabledBkg );
  554. dc.FillSolidRect( rcText,
  555. bWndEnabled ? ::GetSysColor(COLOR_WINDOW) : clrDisabledBkg );
  556. clrOldTextColor = dc.SetTextColor(
  557. bWndEnabled ? ::GetSysColor(COLOR_BTNTEXT) : clrDisabledText );
  558. dc.DrawText( m_strEdit.c_str(), &rcText, DT_SINGLELINE|DT_VCENTER);
  559. }
  560. dc.SelectObject( pOldFont );
  561. dc.SetBkMode( nOldBkMode );
  562. }
  563. else
  564. {
  565. if( m_pEdit )
  566. {
  567. m_pEdit->SetFont( m_pFont );
  568. }
  569. }
  570. // Do not call CWnd::OnPaint() for painting messages
  571. }
  572. BOOL CAdvComboBox::OnEraseBkgnd(CDC* pDC)
  573. {
  574. return CWnd::OnEraseBkgnd(pDC);
  575. // return TRUE;
  576. }
  577. int CAdvComboBox::SetItemHeight(int nIndex, int nHeight)
  578. {
  579. if( nIndex == -1 )
  580. {
  581. if( nHeight < 10 || nHeight > 50 )
  582. {
  583. return CB_ERR;
  584. }
  585. else
  586. {
  587. //
  588. // Button rect
  589. GetClientRect(m_rcDropButton);
  590. m_rcDropButton.left = m_rcDropButton.right - ::GetSystemMetrics(SM_CXHSCROLL);
  591. return 0;
  592. }
  593. }
  594. return CB_ERR;
  595. }
  596. void CAdvComboBox::OnLButtonDown(UINT nFlags, CPoint point)
  597. {
  598. if( GetFocus() != this )
  599. {
  600. SetFocus();
  601. }
  602. //
  603. // Is mouse over drop button?
  604. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  605. {
  606. if( m_rcDropButton.PtInRect( point ) )
  607. {
  608. SendMessage( WM_ON_DROPDOWN_BUTTON );
  609. InvalidateRect( m_rcDropButton );
  610. Invalidate();
  611. }
  612. }
  613. else
  614. if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
  615. {
  616. CRect rc = m_rcCombo;
  617. GetClientRect( &rc );
  618. if( rc.PtInRect( point ) )
  619. {
  620. SendMessage( WM_ON_DROPDOWN_BUTTON );
  621. Invalidate();
  622. }
  623. }
  624. CWnd::OnLButtonDown(nFlags, point);
  625. }
  626. LONG CAdvComboBox::OnSelectedItem( WPARAM wParam, LPARAM /*lParam*/ )
  627. {
  628. list<LIST_ITEM> itemlist;
  629. list<LIST_ITEM>::iterator itemiter;
  630. int nPos = (int)wParam;
  631. itemlist = m_pDropWnd->GetList();
  632. itemiter = itemlist.begin();
  633. advance( itemiter, nPos );
  634. m_strEdit = itemiter->strText;
  635. m_nCurSel = FindStringExact( 0, m_strEdit.c_str() );
  636. SetWindowText( m_strEdit.c_str() );
  637. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  638. {
  639. if( m_pEdit )
  640. {
  641. m_pEdit->SetWindowText( m_strEdit.c_str() );
  642. m_pEdit->SetFocus();
  643. m_pEdit->SetSel( 0, -1, TRUE );
  644. }
  645. }
  646. // Send message to parent(dialog)
  647. m_bSelItem = true;
  648. int nId = GetDlgCtrlID();
  649. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
  650. Invalidate();
  651. OnDestroyDropdownList(0,0);
  652. //
  653. // See to it that the drop button is redrawn
  654. InvalidateRect( m_rcDropButton );
  655. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
  656. return TRUE;
  657. }
  658. LONG CAdvComboBox::OnDropdownButton( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  659. {
  660. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnDropdownButton\n"));
  661. //
  662. //
  663. if( !m_bDropListVisible )
  664. {
  665. //
  666. // Button is pressed
  667. //
  668. // Create list
  669. if( !m_pDropWnd )
  670. {
  671. CreateDropList( m_list );
  672. }
  673. m_pDropWnd->ShowWindow( SW_SHOW );
  674. m_bDropListVisible = TRUE;
  675. }
  676. else
  677. {
  678. OnDestroyDropdownList(0,0);
  679. }
  680. // Return TRUE if OK to go back, else return FALSE.
  681. return TRUE;
  682. }
  683. LONG CAdvComboBox::OnDestroyDropdownList( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  684. {
  685. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnDestroyDropdownList\n"));
  686. //
  687. //
  688. if( m_pDropWnd )
  689. {
  690. m_pDropWnd->GetWindowRect( &m_rcDropWnd );
  691. m_bDropRectStored = true;
  692. m_pDropWnd->ShowWindow( SW_HIDE );
  693. m_bDropListVisible = FALSE;
  694. m_pDropWnd->DestroyWindow();
  695. delete m_pDropWnd;
  696. m_pDropWnd = NULL;
  697. InvalidateRect( &m_rcDropButton );
  698. int nId = GetDlgCtrlID();
  699. if( !m_bSelItem )
  700. {
  701. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL),
  702. (LPARAM)m_hWnd );
  703. }
  704. else
  705. {
  706. m_bSelItem = false;
  707. }
  708. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_CLOSEUP), (LPARAM)m_hWnd );
  709. }
  710. else
  711. {
  712. OnComboComplete();
  713. }
  714. return TRUE;
  715. }
  716. void CAdvComboBox::CreateDropList( list<LIST_ITEM> &droplist)
  717. {
  718. XLISTCTRL_TRACE(_T("in CAdvComboBox::CreateDropList\n"));
  719. CRect rc;
  720. if( m_pDropWnd )
  721. ASSERT(0);
  722. m_pDropWnd = new CDropWnd( this, droplist, m_dwACBStyle );
  723. GetWindowRect( &rc );
  724. rc.top = rc.bottom ;
  725. //
  726. // Get screen size
  727. CRect rcWorkArea;
  728. SystemParametersInfo( SPI_GETWORKAREA, 0, (LPRECT)rcWorkArea, 0) ;
  729. if( rc.bottom >= rcWorkArea.bottom )
  730. {
  731. rc.bottom = rcWorkArea.bottom;
  732. }
  733. else
  734. {
  735. }
  736. int nStyle = WS_CHILD|/*WS_BORDER|*/LBS_DISABLENOSCROLL|LBS_NOTIFY;
  737. m_pDropWnd->Create( 0, 0, nStyle , rc, 1 ? GetDesktopWindow() : this, 6 );
  738. //+++
  739. if (m_nCurSel > ((int)droplist.size() - 1))
  740. m_nCurSel = 0;
  741. m_pDropWnd->GetListBoxPtr()->SetCurSel( m_nCurSel );
  742. m_pDropWnd->SetFont( m_pFont );
  743. // Send message to parent(dialog)
  744. int nId = GetDlgCtrlID();
  745. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_DROPDOWN), (LPARAM)m_hWnd );
  746. }
  747. int CAdvComboBox::GetLBText(int nIndex, LPTSTR lpszText)
  748. {
  749. m_iter = m_list.begin();
  750. advance( m_iter, nIndex );
  751. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  752. {
  753. return CB_ERR;
  754. }
  755. _tcscpy( lpszText, m_iter->strText.c_str() );
  756. return m_iter->strText.length()+1;
  757. }
  758. void CAdvComboBox::GetLBText(int nIndex, CString &rString)
  759. {
  760. m_iter = m_list.begin();
  761. advance( m_iter, nIndex );
  762. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  763. {
  764. rString = "";
  765. return;
  766. }
  767. rString = m_iter->strText.c_str();
  768. }
  769. int CAdvComboBox::GetLBTextLen(int nIndex )
  770. {
  771. m_iter = m_list.begin();
  772. advance( m_iter, nIndex );
  773. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  774. {
  775. return CB_ERR;
  776. }
  777. return m_iter->strText.length()+1;
  778. }
  779. int CAdvComboBox::AddString(LPCTSTR lpszString) //+++
  780. {
  781. LIST_ITEM item;
  782. item.strText = lpszString;
  783. item.bChecked = false;
  784. item.bDisabled = false;
  785. item.vpItemData = NULL;
  786. m_list.push_back( item );
  787. // this takes too long -
  788. // sorting is handled in CXListCtrl::SetComboBox()
  789. //if( GetStyle() & CBS_SORT )
  790. //{
  791. // m_list.sort();
  792. // Find new item
  793. // return FindString( -1, item.strText.c_str() );
  794. //}
  795. //else
  796. return m_list.size()-1;
  797. }
  798. int CAdvComboBox::GetText(LPTSTR lpszText)
  799. {
  800. if( m_pEdit )
  801. {
  802. CString str;
  803. m_pEdit->GetWindowText( str );
  804. _tcscpy( lpszText, (LPCTSTR)str );
  805. return str.GetLength();
  806. }
  807. else
  808. {
  809. _tcscpy( lpszText, m_strEdit.c_str() );
  810. return m_strEdit.length();
  811. }
  812. }
  813. void CAdvComboBox::GetText(CString &rString)
  814. {
  815. if( m_pEdit )
  816. {
  817. m_pEdit->GetWindowText( rString );
  818. }
  819. else
  820. {
  821. rString = m_strEdit.c_str();
  822. }
  823. }
  824. void CAdvComboBox::SetText(LPCTSTR lpszText)
  825. {
  826. if( m_pEdit )
  827. {
  828. m_pEdit->SetWindowText( lpszText );
  829. }
  830. m_strEdit = lpszText;
  831. }
  832. BOOL CAdvComboBox::PointInWindow(CPoint ptScreenPoint)
  833. {
  834. CRect rc;
  835. GetWindowRect( &rc );
  836. return rc.PtInRect( ptScreenPoint );
  837. }
  838. BOOL CAdvComboBox::OnMouseWheel(UINT /*nFlags*/, short zDelta, CPoint /*pt*/)
  839. {
  840. // TODO: Add your message handler code here and/or call default
  841. if( !m_bDropListVisible )
  842. {
  843. string str;
  844. //
  845. // Select another string from the map
  846. m_zDelta += zDelta;
  847. if( m_zDelta >= WHEEL_DELTA )
  848. {
  849. //
  850. // Select item upwards
  851. m_zDelta = 0;
  852. SelPrevItem();
  853. }
  854. else
  855. if( m_zDelta <= -WHEEL_DELTA )
  856. {
  857. //
  858. // Select item downwards
  859. m_zDelta = 0;
  860. SelNextItem();
  861. }
  862. }
  863. else
  864. {
  865. //
  866. // Handle mousewheel for the droplist here
  867. //
  868. // Select another string from the map
  869. m_zDelta += zDelta;
  870. if( m_zDelta >= WHEEL_DELTA )
  871. {
  872. //
  873. // Scroll list upwards
  874. m_zDelta = 0;
  875. int nTop = m_pDropWnd->GetListBoxPtr()->GetTopIndex();
  876. nTop -= 3;
  877. nTop = nTop < 0 ? 0 : nTop;
  878. m_pDropWnd->GetListBoxPtr()->SetTopIdx( nTop, TRUE );
  879. }
  880. else
  881. if( m_zDelta <= -WHEEL_DELTA )
  882. {
  883. //
  884. // Scroll list downwards
  885. m_zDelta = 0;
  886. int nTop = m_pDropWnd->GetListBoxPtr()->GetTopIndex();
  887. nTop += 3;
  888. nTop = nTop > m_pDropWnd->GetListBoxPtr()->GetCount() ?
  889. m_pDropWnd->GetListBoxPtr()->GetCount() : nTop;
  890. m_pDropWnd->GetListBoxPtr()->SetTopIdx( nTop, TRUE );
  891. }
  892. }
  893. return TRUE;
  894. // return CWnd::OnMouseWheel(nFlags, zDelta, pt);
  895. }
  896. void CAdvComboBox::OnSize(UINT nType, int cx, int cy)
  897. {
  898. CWnd::OnSize(nType, cx, cy);
  899. //
  900. // Move Dropdown?
  901. }
  902. void CAdvComboBox::OnSetFocus(CWnd* pOldWnd)
  903. {
  904. CWnd::OnSetFocus(pOldWnd);
  905. // TODO: Add your message handler code here
  906. m_bHasFocus = true;
  907. Invalidate();
  908. //
  909. // Set focus to the edit control? (CBS_DROPDOWN)
  910. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  911. {
  912. if( m_pEdit )
  913. {
  914. m_pEdit->SetFocus();
  915. }
  916. }
  917. BOOL bDropdownList = (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE);
  918. if( bDropdownList )
  919. {
  920. // Send message to parent(dialog)
  921. int nId = GetDlgCtrlID();
  922. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
  923. }
  924. }
  925. void CAdvComboBox::OnSetfocusEdit()
  926. {
  927. m_bHasFocus = false;
  928. //CWnd* pWnd = GetFocus();
  929. //+++
  930. if (m_pEdit)
  931. {
  932. m_pEdit->PostMessage(EM_SETSEL, 0, -1);
  933. }
  934. if( !m_bHasSentFocus )
  935. {
  936. // Send message to parent(dialog)
  937. int nId = GetDlgCtrlID();
  938. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
  939. m_bHasSentFocus = true;
  940. }
  941. }
  942. void CAdvComboBox::OnKillFocus(CWnd* pNewWnd)
  943. {
  944. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnKillFocus\n"));
  945. CWnd::OnKillFocus(pNewWnd);
  946. OnEscapeKey();
  947. //+++
  948. #if 0 // -----------------------------------------------------------
  949. // Needed for keydown's like 'Alt-C'("&Cancel" button)
  950. if( m_pDropWnd )
  951. {
  952. OnDestroyDropdownList(0,0);
  953. //}
  954. m_bHasFocus = false;
  955. Invalidate();
  956. BOOL bDropdownList = (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE);
  957. if( bDropdownList && !m_pDropWnd )
  958. {
  959. // Send message to parent(dialog)
  960. int nId = GetDlgCtrlID();
  961. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
  962. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_KILLFOCUS), (LPARAM)m_hWnd );
  963. }
  964. }
  965. else
  966. {
  967. OnEscapeKey();
  968. }
  969. #endif // -----------------------------------------------------------
  970. }
  971. void CAdvComboBox::OnKillfocusEdit()
  972. {
  973. m_bHasFocus = false;
  974. Invalidate();
  975. CWnd* pWnd = GetFocus();
  976. if( !m_pDropWnd && pWnd != this )
  977. {
  978. // Send message to parent(dialog)
  979. int nId = GetDlgCtrlID();
  980. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
  981. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_KILLFOCUS), (LPARAM)m_hWnd );
  982. m_bHasSentFocus = false;
  983. m_pEdit->SetSel(0,0);
  984. }
  985. //+++
  986. if (m_pDropWnd)
  987. {
  988. OnDestroyDropdownList(0,0);
  989. }
  990. }
  991. void CAdvComboBox::OnShowWindow(BOOL bShow, UINT nStatus)
  992. {
  993. CWnd::OnShowWindow(bShow, nStatus);
  994. }
  995. void CAdvComboBox::ModifyACBStyle(UINT nRemoveStyle, UINT nAddStyle)
  996. {
  997. if( nAddStyle & ACBS_FLAT )
  998. {
  999. nRemoveStyle |= ACBS_STANDARD;
  1000. }
  1001. else
  1002. if( nAddStyle & ACBS_STANDARD )
  1003. {
  1004. nRemoveStyle |= ACBS_FLAT;
  1005. }
  1006. m_dwACBStyle &= ~nRemoveStyle;
  1007. m_dwACBStyle |= nAddStyle;
  1008. Invalidate();
  1009. }
  1010. int CAdvComboBox::GetCount()
  1011. {
  1012. return m_list.size();
  1013. }
  1014. int CAdvComboBox::GetCurSel()
  1015. {
  1016. CString str;
  1017. GetText( str );
  1018. return FindStringExact( -1, str );
  1019. }
  1020. int CAdvComboBox::SetCurSel(int nSelect)
  1021. {
  1022. if( nSelect == -1 )
  1023. {
  1024. m_nCurSel = nSelect;
  1025. m_strEdit = _T("");
  1026. Invalidate();
  1027. return CB_ERR;
  1028. }
  1029. else
  1030. if( m_list.size() == 0 )
  1031. {
  1032. m_nCurSel = nSelect;
  1033. return CB_ERR;
  1034. }
  1035. else
  1036. if( nSelect < -1 || nSelect > (int)m_list.size()-1 )
  1037. {
  1038. return CB_ERR;
  1039. }
  1040. else
  1041. {
  1042. m_nCurSel = nSelect;
  1043. m_iter = m_list.begin();
  1044. advance( m_iter, nSelect );
  1045. m_strEdit = m_iter->strText;
  1046. Invalidate();
  1047. return m_nCurSel;
  1048. }
  1049. }
  1050. int CAdvComboBox::FindString(int nStartAfter, LPCTSTR lpszString)
  1051. {
  1052. int nPos = 0;
  1053. m_iter = m_list.begin();
  1054. if( nStartAfter != -1 )
  1055. {
  1056. advance( m_iter, nStartAfter );
  1057. nPos = nStartAfter;
  1058. }
  1059. for( m_iter; m_iter != m_list.end(); ++m_iter )
  1060. {
  1061. if( _tcsncmp( m_iter->strText.c_str(), lpszString, _tcslen(lpszString) ) == 0 )
  1062. {
  1063. return nPos;
  1064. }
  1065. nPos++;
  1066. }
  1067. return CB_ERR;
  1068. }
  1069. int CAdvComboBox::FindStringExact(int nIndexStart, LPCTSTR lpszFind)
  1070. {
  1071. TRACE(_T("in CAdvComboBox::FindStringExact: nIndexStart=%d\n"), nIndexStart);
  1072. if( nIndexStart > (int)m_list.size() && nIndexStart != -1 )
  1073. return CB_ERR;
  1074. int nPos = 0;
  1075. m_iter = m_list.begin();
  1076. if( nIndexStart != -1 )
  1077. {
  1078. advance( m_iter, nIndexStart );
  1079. nPos = nIndexStart;
  1080. }
  1081. for( m_iter; m_iter != m_list.end(); ++m_iter )
  1082. {
  1083. if( _tcscmp( m_iter->strText.c_str(), lpszFind ) == 0 )
  1084. {
  1085. XLISTCTRL_TRACE(_T("CAdvComboBox::FindStringExact returning %d\n"), nPos);
  1086. return nPos;
  1087. }
  1088. nPos++;
  1089. }
  1090. return CB_ERR;
  1091. }
  1092. int CAdvComboBox::SelectString(int nStartAfter, LPCTSTR lpszString)
  1093. {
  1094. if( nStartAfter > (int)m_list.size() )
  1095. return CB_ERR;
  1096. int nPos = 0;
  1097. m_iter = m_list.begin();
  1098. if( nStartAfter != -1 )
  1099. {
  1100. advance( m_iter, nStartAfter );
  1101. nPos = nStartAfter;
  1102. }
  1103. for( m_iter; m_iter != m_list.end(); ++m_iter )
  1104. {
  1105. if( _tcscmp( m_iter->strText.c_str(), lpszString ) == 0 )
  1106. {
  1107. m_nCurSel = nPos;
  1108. m_strEdit = m_iter->strText;
  1109. Invalidate();
  1110. return nPos;
  1111. }
  1112. nPos++;
  1113. }
  1114. return CB_ERR;
  1115. }
  1116. int CAdvComboBox::SetItemData(int nIndex, DWORD dwItemData)
  1117. {
  1118. m_iter = m_list.begin();
  1119. advance( m_iter, nIndex );
  1120. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  1121. {
  1122. return CB_ERR;
  1123. }
  1124. m_iter->vpItemData = (void*)dwItemData;
  1125. return CB_OKAY;
  1126. }
  1127. DWORD CAdvComboBox::GetItemData(int nIndex)
  1128. {
  1129. m_iter = m_list.begin();
  1130. advance( m_iter, nIndex );
  1131. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  1132. {
  1133. return (DWORD)CB_ERR;
  1134. }
  1135. return (DWORD)m_iter->vpItemData;
  1136. }
  1137. int CAdvComboBox::SetItemDataPtr(int nIndex, void *pData)
  1138. {
  1139. m_iter = m_list.begin();
  1140. advance( m_iter, nIndex );
  1141. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  1142. {
  1143. return (DWORD)CB_ERR;
  1144. }
  1145. m_iter->vpItemData = pData;
  1146. return CB_OKAY;
  1147. }
  1148. void* CAdvComboBox::GetItemDataPtr(int nIndex)
  1149. {
  1150. m_iter = m_list.begin();
  1151. advance( m_iter, nIndex );
  1152. if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
  1153. {
  1154. return (void*)CB_ERR;
  1155. }
  1156. return m_iter->vpItemData;
  1157. }
  1158. void CAdvComboBox::ResetContent()
  1159. {
  1160. m_list.clear();
  1161. m_strEdit = _T("");
  1162. Invalidate();
  1163. }
  1164. void AFXAPI DDX_ACBIndex( CDataExchange* pDX, int nIDC, int& index )
  1165. {
  1166. /*HWND hWnd =*/ pDX->PrepareCtrl( nIDC );
  1167. if( pDX->m_bSaveAndValidate )
  1168. {
  1169. CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
  1170. index = pACB->GetCurSel();
  1171. }
  1172. else
  1173. {
  1174. CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
  1175. pACB->SetCurSel( index );
  1176. }
  1177. }
  1178. void AFXAPI DDX_ACBString( CDataExchange* pDX, int nIDC, CString& value )
  1179. {
  1180. /*HWND hWnd =*/ pDX->PrepareCtrl( nIDC );
  1181. if( pDX->m_bSaveAndValidate )
  1182. {
  1183. CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
  1184. pACB->GetText( value );
  1185. }
  1186. else
  1187. {
  1188. CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
  1189. pACB->SetText( value );
  1190. }
  1191. }
  1192. BOOL CAdvComboBox::GetItemDisabled( int nIndex )
  1193. {
  1194. if( nIndex > (int)m_list.size() )
  1195. return CB_ERR;
  1196. m_iter = m_list.begin();
  1197. advance( m_iter, nIndex );
  1198. return m_iter->bDisabled;
  1199. }
  1200. void CAdvComboBox::SetItemDisabled(int nIndex, BOOL bDisabled)
  1201. {
  1202. if( nIndex > (int)m_list.size() )
  1203. return;
  1204. m_iter = m_list.begin();
  1205. advance( m_iter, nIndex );
  1206. m_iter->bDisabled = bDisabled;
  1207. }
  1208. BOOL CAdvComboBox::GetItemChecked( int nIndex )
  1209. {
  1210. if( nIndex > (int)m_list.size() )
  1211. return CB_ERR;
  1212. m_iter = m_list.begin();
  1213. advance( m_iter, nIndex );
  1214. return m_iter->bChecked;
  1215. }
  1216. void CAdvComboBox::SetItemChecked(int nIndex, BOOL bChecked)
  1217. {
  1218. if( nIndex > (int)m_list.size() )
  1219. return;
  1220. m_iter = m_list.begin();
  1221. advance( m_iter, nIndex );
  1222. m_iter->bChecked = bChecked;
  1223. }
  1224. BOOL CAdvComboBox::PreTranslateMessage(MSG* pMsg)
  1225. {
  1226. if( pMsg->message == WM_KEYDOWN )
  1227. {
  1228. m_bAutoAppend = TRUE;
  1229. XLISTCTRL_TRACE(_T("CAdvComboBox: Key was pressed(AdvComboBox)\n"));
  1230. if( pMsg->wParam == VK_RETURN )
  1231. {
  1232. XLISTCTRL_TRACE(_T("CAdvComboBox: VK_RETURN\n"));
  1233. if( m_pDropWnd )
  1234. {
  1235. int nPos = m_pDropWnd->GetListBoxPtr()->GetCurSel();
  1236. if (nPos != LB_ERR) //+++
  1237. SendMessage( WM_SELECTED_ITEM, (WPARAM)nPos );
  1238. //else //+++
  1239. OnDestroyDropdownList(0,0); //+++
  1240. }
  1241. else
  1242. {
  1243. return CWnd::PreTranslateMessage(pMsg);
  1244. }
  1245. }
  1246. else
  1247. if( pMsg->wParam == VK_ESCAPE )
  1248. {
  1249. XLISTCTRL_TRACE(_T("CAdvComboBox: VK_ESCAPE\n"));
  1250. if( m_pDropWnd )
  1251. {
  1252. OnDestroyDropdownList(0,0);
  1253. Invalidate();
  1254. }
  1255. else //+++
  1256. {
  1257. OnEscapeKey();
  1258. return CWnd::PreTranslateMessage(pMsg);
  1259. }
  1260. }
  1261. else
  1262. if( pMsg->wParam == VK_F4 )
  1263. {
  1264. SendMessage( WM_ON_DROPDOWN_BUTTON );
  1265. Invalidate();
  1266. }
  1267. else
  1268. if( pMsg->wParam == VK_UP )
  1269. {
  1270. SelPrevItem();
  1271. }
  1272. else
  1273. if( pMsg->wParam == VK_DOWN )
  1274. {
  1275. SelNextItem();
  1276. }
  1277. else
  1278. if( pMsg->wParam == VK_PRIOR ) //+++
  1279. {
  1280. SelPrevPage();
  1281. }
  1282. else
  1283. if( pMsg->wParam == VK_NEXT ) //+++
  1284. {
  1285. SelNextPage();
  1286. }
  1287. else
  1288. if( pMsg->wParam == VK_DELETE || pMsg->wParam == VK_BACK )
  1289. {
  1290. m_bAutoAppend = FALSE;
  1291. return CWnd::PreTranslateMessage(pMsg);
  1292. }
  1293. else
  1294. if( pMsg->wParam == VK_RIGHT )
  1295. {
  1296. if( m_dwACBStyle & ACBS_AUTOAPPEND )
  1297. {
  1298. // If the cursor is at the end of the text, show autosuggest text
  1299. if( m_pEdit )
  1300. {
  1301. int nS, nE;
  1302. m_pEdit->GetSel( nS, nE );
  1303. if( nS == nE && nS == m_pEdit->LineLength() )
  1304. {
  1305. OnUpdateEdit();
  1306. }
  1307. else
  1308. {
  1309. return CWnd::PreTranslateMessage(pMsg);
  1310. }
  1311. }
  1312. }
  1313. else
  1314. {
  1315. return CWnd::PreTranslateMessage(pMsg);
  1316. }
  1317. }
  1318. else
  1319. {
  1320. return CWnd::PreTranslateMessage(pMsg);
  1321. }
  1322. return TRUE;
  1323. }
  1324. else
  1325. if( pMsg->message == WM_SYSKEYDOWN )
  1326. {
  1327. if( pMsg->wParam == VK_DOWN ||
  1328. pMsg->wParam == VK_UP )
  1329. {
  1330. SendMessage( WM_ON_DROPDOWN_BUTTON );
  1331. Invalidate();
  1332. }
  1333. }
  1334. return CWnd::PreTranslateMessage(pMsg);
  1335. }
  1336. void CAdvComboBox::SelPrevItem()
  1337. {
  1338. if( m_pDropWnd )
  1339. {
  1340. int nPos = m_pDropWnd->GetListBoxPtr()->GetCurSel();
  1341. if( nPos > 0 )
  1342. {
  1343. m_pDropWnd->GetListBoxPtr()->SetCurSel( --nPos );
  1344. }
  1345. }
  1346. else
  1347. {
  1348. m_iter = m_list.begin();
  1349. advance( m_iter, m_nCurSel );
  1350. --m_iter;
  1351. int nOldSel = m_nCurSel;
  1352. int nPos = m_nCurSel;
  1353. while( m_iter != m_list.end() )
  1354. {
  1355. nPos--;
  1356. if( !m_iter->bDisabled )
  1357. {
  1358. m_strEdit = m_iter->strText;
  1359. if( m_pEdit )
  1360. m_pEdit->SetWindowText( m_strEdit.c_str() );
  1361. m_nCurSel = nPos;
  1362. Invalidate();
  1363. break;
  1364. }
  1365. --m_iter;
  1366. }
  1367. if( nOldSel != m_nCurSel )
  1368. {
  1369. // Send message to parent(dialog)
  1370. int nId = GetDlgCtrlID();
  1371. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
  1372. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
  1373. }
  1374. }
  1375. }
  1376. void CAdvComboBox::SelNextItem()
  1377. {
  1378. if( m_pDropWnd )
  1379. {
  1380. int nPos = m_pDropWnd->GetListBoxPtr()->GetCurSel();
  1381. if( nPos < m_pDropWnd->GetListBoxPtr()->GetCount() )
  1382. {
  1383. m_pDropWnd->GetListBoxPtr()->SetCurSel( ++nPos );
  1384. }
  1385. }
  1386. else
  1387. {
  1388. m_iter = m_list.begin();
  1389. advance( m_iter, m_nCurSel );
  1390. ++m_iter;
  1391. int nOldSel = m_nCurSel;
  1392. int nPos = m_nCurSel;
  1393. while( m_iter != m_list.end() )
  1394. {
  1395. nPos++;
  1396. if( !m_iter->bDisabled )
  1397. {
  1398. m_strEdit = m_iter->strText;
  1399. if( m_pEdit )
  1400. m_pEdit->SetWindowText( m_strEdit.c_str() );
  1401. Invalidate();
  1402. m_nCurSel = nPos;
  1403. break;
  1404. }
  1405. ++m_iter;
  1406. }
  1407. if( nOldSel != m_nCurSel )
  1408. {
  1409. // Send message to parent(dialog)
  1410. int nId = GetDlgCtrlID();
  1411. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
  1412. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
  1413. }
  1414. }
  1415. }
  1416. //+++
  1417. void CAdvComboBox::SelNextPage()
  1418. {
  1419. XLISTCTRL_TRACE(_T("in CAdvComboBox::SelNextPage\n"));
  1420. if (m_pDropWnd)
  1421. {
  1422. CDropListBox * pListBox = m_pDropWnd->GetListBoxPtr();
  1423. ASSERT(pListBox);
  1424. int nPos = pListBox->GetCurSel();
  1425. int nCount = pListBox->GetCount();
  1426. int nMaxVisible = pListBox->GetMaxVisibleItems();
  1427. int nTop = pListBox->GetTopIndex();
  1428. if (nPos == LB_ERR) // no selection
  1429. nPos = 0;
  1430. else if (nPos == (nCount - 1)) // end of list
  1431. /*nPos = -1*/;
  1432. else if (nPos < (nTop + nMaxVisible - 1)) // middle of listbox window
  1433. nPos = nTop + nMaxVisible - 1;
  1434. else // at end of listbox window
  1435. nPos = nTop + nMaxVisible + nMaxVisible - 2;
  1436. if (nPos >= nCount) // moved past end, reset
  1437. nPos = nCount - 1;
  1438. pListBox->SetCurSel(nPos);
  1439. int nSel = pListBox->GetCurSel();
  1440. if (nSel == LB_ERR)
  1441. {
  1442. }
  1443. else
  1444. {
  1445. CString str = _T("");
  1446. pListBox->GetText(nSel, str);
  1447. SetWindowText(str);
  1448. }
  1449. }
  1450. else
  1451. {
  1452. // combo list not dropped
  1453. int nSize = m_list.size();
  1454. m_iter = m_list.begin();
  1455. if (m_nCurSel < 0 || m_nCurSel >= nSize)
  1456. m_nCurSel = 0;
  1457. advance(m_iter, m_nCurSel);
  1458. int nOldSel = m_nCurSel;
  1459. int nPos = m_nCurSel;
  1460. // Page Down goes forward by 10
  1461. for (int i = 0; i < 10; i++)
  1462. {
  1463. if (nPos >= (nSize-1))
  1464. {
  1465. nPos = nSize - 1;
  1466. break;
  1467. }
  1468. nPos++;
  1469. m_iter++;
  1470. }
  1471. if (nOldSel != nPos)
  1472. {
  1473. m_strEdit = m_iter->strText;
  1474. if (m_pEdit)
  1475. m_pEdit->SetWindowText(m_strEdit.c_str());
  1476. Invalidate();
  1477. m_nCurSel = nPos;
  1478. // Send message to parent(dialog)
  1479. int nId = GetDlgCtrlID();
  1480. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
  1481. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
  1482. }
  1483. }
  1484. }
  1485. //+++
  1486. void CAdvComboBox::SelPrevPage()
  1487. {
  1488. if (m_pDropWnd)
  1489. {
  1490. CDropListBox * pListBox = m_pDropWnd->GetListBoxPtr();
  1491. ASSERT(pListBox);
  1492. int nPos = pListBox->GetCurSel();
  1493. int nCount = pListBox->GetCount();
  1494. int nMaxVisible = pListBox->GetMaxVisibleItems();
  1495. int nTop = pListBox->GetTopIndex();
  1496. if (nPos <= 0)
  1497. return;
  1498. if (nPos == LB_ERR) // no selection
  1499. nPos = nCount - 1;
  1500. else if (nPos == 0) // top of list
  1501. /*nPos = -1*/;
  1502. else if (nPos == nTop) // at top of listbox window
  1503. {
  1504. nPos = nTop - nMaxVisible + 1; // this might be = -1
  1505. if (nPos < 0)
  1506. nPos = 0;
  1507. }
  1508. else // middle of listbox window
  1509. nPos = nTop;
  1510. pListBox->SetCurSel(nPos);
  1511. int nSel = pListBox->GetCurSel();
  1512. if (nSel == LB_ERR)
  1513. {
  1514. }
  1515. else
  1516. {
  1517. CString str = _T("");
  1518. pListBox->GetText(nSel, str);
  1519. SetWindowText(str);
  1520. }
  1521. }
  1522. else
  1523. {
  1524. // combo list not dropped
  1525. int nSize = m_list.size();
  1526. m_iter = m_list.begin();
  1527. if (m_nCurSel < 0 || m_nCurSel >= nSize)
  1528. m_nCurSel = 0;
  1529. advance(m_iter, m_nCurSel);
  1530. int nOldSel = m_nCurSel;
  1531. int nPos = m_nCurSel;
  1532. // Page Up goes back by 10
  1533. for (int i = 0; i < 10; i++)
  1534. {
  1535. if (nPos <= 0)
  1536. {
  1537. nPos = 0;
  1538. break;
  1539. }
  1540. nPos--;
  1541. m_iter--;
  1542. }
  1543. if (nOldSel != nPos)
  1544. {
  1545. m_strEdit = m_iter->strText;
  1546. if (m_pEdit)
  1547. m_pEdit->SetWindowText(m_strEdit.c_str());
  1548. Invalidate();
  1549. m_nCurSel = nPos;
  1550. // Send message to parent(dialog)
  1551. int nId = GetDlgCtrlID();
  1552. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
  1553. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
  1554. }
  1555. }
  1556. }
  1557. int CAdvComboBox::GetTopIndex()
  1558. {
  1559. if( m_bDropListVisible )
  1560. {
  1561. if( m_pDropWnd )
  1562. {
  1563. return m_pDropWnd->GetListBoxPtr()->GetTopIndex();
  1564. }
  1565. }
  1566. return CB_ERR;
  1567. }
  1568. int CAdvComboBox::SetTopIndex(int nIndex)
  1569. {
  1570. if( m_bDropListVisible )
  1571. {
  1572. if( m_pDropWnd )
  1573. {
  1574. return m_pDropWnd->GetListBoxPtr()->SetTopIndex(nIndex);
  1575. }
  1576. }
  1577. return CB_ERR;
  1578. }
  1579. //
  1580. // Will not allocate anything. I can't see the need in doing that.
  1581. // Everything is stored in a STL list.
  1582. int CAdvComboBox::InitStorage(int nItems, UINT /*nBytes*/)
  1583. {
  1584. return nItems;
  1585. }
  1586. void CAdvComboBox::ShowDropDown(BOOL bShowIt)
  1587. {
  1588. if( bShowIt )
  1589. {
  1590. if( !m_bDropListVisible )
  1591. {
  1592. SendMessage( WM_ON_DROPDOWN_BUTTON );
  1593. Invalidate();
  1594. }
  1595. }
  1596. else
  1597. {
  1598. if( m_bDropListVisible )
  1599. {
  1600. SendMessage( WM_DESTROY_DROPLIST );
  1601. Invalidate();
  1602. }
  1603. }
  1604. }
  1605. void CAdvComboBox::GetDroppedControlRect(LPRECT lprect)
  1606. {
  1607. if( m_bDropListVisible )
  1608. {
  1609. m_pDropWnd->GetWindowRect( lprect );
  1610. }
  1611. }
  1612. BOOL CAdvComboBox::GetDroppedState()
  1613. {
  1614. return m_bDropListVisible;
  1615. }
  1616. int CAdvComboBox::SetExtendedUI(BOOL /*bExtended*/)
  1617. {
  1618. return CB_OKAY;
  1619. }
  1620. BOOL CAdvComboBox::GetExtendedUI()
  1621. {
  1622. return FALSE;
  1623. }
  1624. int CAdvComboBox::DeleteString(UINT nIndex)
  1625. {
  1626. m_iter = m_list.begin();
  1627. advance( m_iter, nIndex );
  1628. if( m_iter != m_list.end() || nIndex > m_list.size() )
  1629. {
  1630. m_list.erase( m_iter );
  1631. return m_list.size();
  1632. }
  1633. else
  1634. {
  1635. return CB_ERR;
  1636. }
  1637. }
  1638. int CAdvComboBox::InsertString(int nIndex, LPCTSTR lpszString)
  1639. {
  1640. LIST_ITEM item;
  1641. item.strText = lpszString;
  1642. if( nIndex == -1 || (nIndex > (int)m_list.size()) )
  1643. {
  1644. m_list.push_back( item );
  1645. return m_list.size()-1;
  1646. }
  1647. if( nIndex == 0 && (m_list.size()==0) )
  1648. {
  1649. m_list.push_back( item );
  1650. return 0;
  1651. }
  1652. m_iter = m_list.begin();
  1653. advance( m_iter, nIndex );
  1654. if( m_iter != m_list.end() )
  1655. {
  1656. m_iter = m_list.insert( m_iter, item );
  1657. int nPos = 0;
  1658. while( m_iter != m_list.begin() )
  1659. {
  1660. nPos++;
  1661. --m_iter;
  1662. }
  1663. if( nIndex <= m_nCurSel )
  1664. {
  1665. m_nCurSel++;
  1666. }
  1667. return nPos;
  1668. }
  1669. return CB_ERR;
  1670. }
  1671. DWORD CAdvComboBox::GetEditSel()
  1672. {
  1673. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  1674. {
  1675. if( m_pEdit )
  1676. {
  1677. return m_pEdit->GetSel();
  1678. }
  1679. }
  1680. return (DWORD)CB_ERR;
  1681. }
  1682. BOOL CAdvComboBox::SetEditSel(int nStartChar, int nEndChar)
  1683. {
  1684. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  1685. {
  1686. if( m_pEdit )
  1687. {
  1688. m_pEdit->SetSel( nStartChar, nEndChar, TRUE );
  1689. return TRUE;
  1690. }
  1691. }
  1692. return CB_ERR;
  1693. }
  1694. void CAdvComboBox::OnChangeEdit()
  1695. {
  1696. if( !m_pDropWnd )
  1697. {
  1698. // Send message to parent(dialog)
  1699. int nId = GetDlgCtrlID();
  1700. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_EDITCHANGE), (LPARAM)m_hWnd );
  1701. }
  1702. }
  1703. void CAdvComboBox::OnUpdateEdit()
  1704. {
  1705. static bool bAutoAppendInProgress;
  1706. CString strEdit;
  1707. m_pEdit->GetWindowText( strEdit );
  1708. if( GetFocus() == m_pEdit )
  1709. {
  1710. if( !bAutoAppendInProgress )
  1711. {
  1712. // m_nCurSel = -1;
  1713. if( m_dwACBStyle & ACBS_AUTOAPPEND && m_bAutoAppend )
  1714. {
  1715. tstring str = (LPCTSTR)strEdit;
  1716. int nEditLen = str.length();
  1717. if( !nEditLen )
  1718. return;
  1719. int nStartSel;
  1720. int nEndSel;
  1721. m_pEdit->GetSel( nStartSel, nEndSel );
  1722. LIST_ITEM item;
  1723. m_iter = m_list.begin();
  1724. while( m_iter != m_list.end() )
  1725. {
  1726. item = *m_iter;
  1727. int nPos = m_iter->strText.find( str, 0 );
  1728. if( nPos == 0 )
  1729. {
  1730. bAutoAppendInProgress = true;
  1731. m_pEdit->SetWindowText( m_iter->strText.c_str() );
  1732. m_pEdit->SetSel( nEditLen, m_iter->strText.length(), TRUE );
  1733. bAutoAppendInProgress = false;
  1734. break;
  1735. }
  1736. ++m_iter;
  1737. }
  1738. }
  1739. }
  1740. if( !bAutoAppendInProgress )
  1741. {
  1742. if( m_dwACBStyle & ACBS_AUTOSUGGEST )
  1743. {
  1744. list<LIST_ITEM> suggestlist;
  1745. list<LIST_ITEM>::iterator suggestiter;
  1746. tstring str = (LPCTSTR)strEdit;
  1747. int nEditLen = str.length();
  1748. if( !nEditLen )
  1749. {
  1750. if( m_pDropWnd )
  1751. SendMessage( WM_DESTROY_DROPLIST );
  1752. return;
  1753. }
  1754. int nStartSel;
  1755. int nEndSel;
  1756. m_pEdit->GetSel( nStartSel, nEndSel );
  1757. LIST_ITEM item;
  1758. m_iter = m_list.begin();
  1759. while( m_iter != m_list.end() )
  1760. {
  1761. item = *m_iter;
  1762. int nPos = m_iter->strText.find( str, 0 );
  1763. if( nPos == 0 )
  1764. {
  1765. if (m_iter->strText != str) //+++
  1766. suggestlist.push_back( item );
  1767. }
  1768. ++m_iter;
  1769. }
  1770. if( m_pDropWnd )
  1771. SendMessage( WM_DESTROY_DROPLIST );
  1772. //+++if( suggestlist.size() != 0 )
  1773. if( suggestlist.size() > 1 ) //+++
  1774. {
  1775. m_nCurSel = 0; //+++
  1776. CreateDropList( suggestlist );
  1777. }
  1778. }
  1779. }
  1780. }
  1781. if( !m_pDropWnd )
  1782. {
  1783. // Send message to parent(dialog)
  1784. int nId = GetDlgCtrlID();
  1785. m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_EDITUPDATE), (LPARAM)m_hWnd );
  1786. }
  1787. }
  1788. void CAdvComboBox::OnEnable(BOOL bEnable)
  1789. {
  1790. CWnd::OnEnable(bEnable);
  1791. Invalidate();
  1792. if( m_pEdit )
  1793. {
  1794. m_pEdit->EnableWindow( bEnable );
  1795. }
  1796. }
  1797. BOOL CAdvComboBox::LimitText(int nMaxChars)
  1798. {
  1799. if( m_pEdit )
  1800. {
  1801. int nCh = !nMaxChars ? 65535 : nMaxChars;
  1802. m_pEdit->SetLimitText( nCh );
  1803. return TRUE;
  1804. }
  1805. else
  1806. {
  1807. return CB_ERR;
  1808. }
  1809. }
  1810. void CAdvComboBox::SetMinVisibleItems(int nMinItems)
  1811. {
  1812. m_nMinVisItems = nMinItems;
  1813. }
  1814. int CAdvComboBox::GetMinVisibleItems()
  1815. {
  1816. return m_nMinVisItems;
  1817. }
  1818. void CAdvComboBox::OnMouseMove(UINT nFlags, CPoint point)
  1819. {
  1820. bool bChange = m_bDropButtonHot;
  1821. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  1822. {
  1823. if( m_rcDropButton.PtInRect( point ) )
  1824. {
  1825. m_bDropButtonHot = true;
  1826. }
  1827. else
  1828. {
  1829. m_bDropButtonHot = false;
  1830. }
  1831. }
  1832. else
  1833. if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
  1834. {
  1835. GetClientRect( &m_rcCombo );
  1836. if( m_rcCombo.PtInRect( point ) )
  1837. {
  1838. m_bDropButtonHot = true;
  1839. }
  1840. else
  1841. {
  1842. m_bDropButtonHot = false;
  1843. }
  1844. }
  1845. if( bChange != m_bDropButtonHot )
  1846. {
  1847. if( !m_bTrackMouseLeave )
  1848. {
  1849. //
  1850. // Could not use TrackMouseEvent() under Release. A bug appeared
  1851. // the second time the dropwindow was shown, and I did not
  1852. // have to strenght to go look for it. :)
  1853. /* TRACKMOUSEEVENT tme;
  1854. tme.cbSize = sizeof(tme);
  1855. tme.dwFlags = TME_LEAVE;
  1856. tme.hwndTrack = m_hWnd;
  1857. TrackMouseEvent(&tme);*/
  1858. m_bTrackMouseLeave = true;
  1859. SetTimer(1, 50, NULL);
  1860. }
  1861. InvalidateRect( &m_rcDropButton );
  1862. }
  1863. CWnd::OnMouseMove(nFlags, point);
  1864. }
  1865. void CAdvComboBox::OnMouseLeave()
  1866. {
  1867. // TODO: Add your message handler code here and/or call default
  1868. bool bChange = m_bDropButtonHot;
  1869. m_bDropButtonHot = false;
  1870. m_bTrackMouseLeave = false;
  1871. if( bChange != m_bDropButtonHot )
  1872. InvalidateRect( &m_rcDropButton );
  1873. }
  1874. void CAdvComboBox::OnTimer(UINT nIDEvent)
  1875. {
  1876. if( nIDEvent == 1 )
  1877. {
  1878. CPoint point;
  1879. GetCursorPos( &point );
  1880. ScreenToClient( &point );
  1881. if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
  1882. {
  1883. if( !m_rcDropButton.PtInRect( point ) )
  1884. {
  1885. KillTimer( 1 );
  1886. OnMouseLeave();
  1887. }
  1888. }
  1889. else
  1890. if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
  1891. {
  1892. GetClientRect( &m_rcCombo );
  1893. if( !m_rcCombo.PtInRect( point ) )
  1894. {
  1895. KillTimer( 1 );
  1896. OnMouseLeave();
  1897. }
  1898. }
  1899. }
  1900. CWnd::OnTimer(nIDEvent);
  1901. }
  1902. int CAdvComboBox::GetDefaultVisibleItems()
  1903. {
  1904. return m_nDefaultDropItems;
  1905. }
  1906. void CAdvComboBox::SetDefaultVisibleItems(int nItems)
  1907. {
  1908. m_nDefaultDropItems = nItems;
  1909. }
  1910. //+++
  1911. // function to get combo width based on strings
  1912. UINT CAdvComboBox::GetComboWidth()
  1913. {
  1914. CDC * pDC = GetDC();
  1915. CFont * pFont = GetFont();
  1916. CFont* pOldFont = pDC->SelectObject(pFont);
  1917. TEXTMETRIC tm;
  1918. pDC->GetTextMetrics(&tm);
  1919. CString str = _T("");
  1920. CSize sz;
  1921. UINT dx = 0;
  1922. for (int i = 0; i < GetCount(); i++)
  1923. {
  1924. GetLBText(i, str);
  1925. sz = pDC->GetTextExtent(str);
  1926. sz.cx += tm.tmAveCharWidth;
  1927. if (sz.cx > (int)dx)
  1928. dx = sz.cx;
  1929. }
  1930. pDC->SelectObject(pOldFont);
  1931. ReleaseDC(pDC);
  1932. dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE) + 5;
  1933. return dx;
  1934. }
  1935. //+++
  1936. void CAdvComboBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  1937. {
  1938. // handle escape and return, in case combo's edit does NOT have focus
  1939. if (m_pParent)
  1940. {
  1941. if (nChar == VK_ESCAPE)
  1942. {
  1943. OnEscapeKey();
  1944. return;
  1945. }
  1946. else if (nChar == VK_RETURN)
  1947. {
  1948. OnComboComplete();
  1949. return;
  1950. }
  1951. else if (nChar == VK_DOWN)
  1952. {
  1953. SelNextItem();
  1954. }
  1955. }
  1956. CWnd::OnChar(nChar, nRepCnt, nFlags);
  1957. }
  1958. void CAdvComboBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  1959. {
  1960. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnKeyDown\n"));
  1961. if (nChar == VK_DOWN)
  1962. SelNextItem();
  1963. else if (nChar == VK_UP)
  1964. SelPrevItem();
  1965. else if (nChar == VK_NEXT)
  1966. SelNextPage();
  1967. else if (nChar == VK_PRIOR)
  1968. SelPrevPage();
  1969. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  1970. }
  1971. void CAdvComboBox::OnActivateApp(BOOL bActive, DWORD hTask)
  1972. {
  1973. XLISTCTRL_TRACE(_T("in CAdvComboBox::OnActivateApp\n"));
  1974. CWnd::OnActivateApp(bActive, hTask);
  1975. }