WndList2.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:14k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 界面窗口体系结构--列表窗口
  3. // Copyright : Kingsoft 2002
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2002-9-11
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../Elem/WndMessage.h"
  10. #include "WndList2.h"
  11. #include "UiImage.h"
  12. #include "../../../Engine/Src/Text.h"
  13. #include "../../../Represent/iRepresent/iRepresentShell.h"
  14. extern iRepresentShell* g_pRepresentShell;
  15. struct KWndList2Item
  16. {
  17. int Data;
  18. DWORD StringSize;
  19. char String[1];
  20. };
  21. //--------------------------------------------------------------------------
  22. // 功能:构造函数
  23. //--------------------------------------------------------------------------
  24. KWndList2::KWndList2()
  25. {
  26. m_pContent = NULL;
  27. m_nItemSpace = 0;
  28. m_pScrollbar    = NULL;
  29. m_nNumItem = 0;
  30. m_nFontSize = 12;
  31. m_nTopItemIndex = 0;
  32. m_nSelItemIndex   = -1;
  33. m_nHighLightItemIndex = -1;
  34. m_ItemBorderColor = m_SelItemBorderColor = m_HighLightBorderColor = 0;
  35. }
  36. //--------------------------------------------------------------------------
  37. // 功能:析构函数
  38. //--------------------------------------------------------------------------
  39. KWndList2::~KWndList2()
  40. {
  41. ResetContent();
  42. }
  43. void KWndList2::Clone(KWndList2* pCopy)
  44. {
  45. if (pCopy)
  46. {
  47. pCopy->m_nFontSize = m_nFontSize;
  48. pCopy->m_ItemColor = m_ItemColor;
  49. pCopy->m_SelItemColor = m_SelItemColor;
  50. pCopy->m_HighLightColor = m_HighLightColor;
  51. pCopy->m_ItemBorderColor = m_ItemBorderColor;
  52. pCopy->m_SelItemBorderColor = m_SelItemBorderColor;
  53. pCopy->m_HighLightBorderColor = m_HighLightBorderColor;
  54. }
  55. }
  56. //--------------------------------------------------------------------------
  57. // 功能:初始化
  58. //--------------------------------------------------------------------------
  59. int KWndList2::Init(KIniFile* pIniFile, const char* pSection)
  60. {
  61. if (KWndWindow::Init(pIniFile, pSection))
  62. {
  63. pIniFile->GetInteger(pSection, "Font", 16, &m_nFontSize);
  64. if (m_nFontSize < 8)
  65. m_nFontSize = 8;
  66. char Buff[16];
  67. pIniFile->GetString(pSection, "Color", "", Buff, 16);
  68. m_ItemColor = GetColor(Buff);
  69. pIniFile->GetString(pSection, "BorderColor", "", Buff, 16);
  70. m_ItemBorderColor = GetColor(Buff);
  71. pIniFile->GetString(pSection, "SelColor", "", Buff, 16);
  72. m_SelItemColor = GetColor(Buff);
  73. pIniFile->GetString(pSection, "SelBorderColor", "", Buff, 16);
  74. m_SelItemBorderColor = GetColor(Buff);
  75. int nValue;
  76. pIniFile->GetInteger(pSection, "HighLight", 0, &nValue);
  77. if (nValue)
  78. {
  79. m_Style |= WNDLIST_ES_HIGHLIGHT_ENABLE;
  80. pIniFile->GetString(pSection, "HighLightColor", "", Buff, 16);
  81. m_HighLightColor = GetColor(Buff);
  82. pIniFile->GetString(pSection, "HighLightBorderColor", "", Buff, 16);
  83. m_HighLightBorderColor = GetColor(Buff);
  84. }
  85. else
  86. m_Style &= ~WNDLIST_ES_HIGHLIGHT_ENABLE;
  87. return true;
  88. }
  89. return false;
  90. }
  91. void KWndList2::SetScrollbar(KWndScrollBar* pScroll)
  92. {
  93. if (m_pScrollbar = pScroll)
  94. {
  95. m_pScrollbar->SetStyle(m_pScrollbar->GetStyle() | WND_S_MOVEALBE);
  96. if (m_nNumItem > GetVisibleLineCount())
  97. {
  98. m_pScrollbar->Enable(true);
  99. m_pScrollbar->SetValueRange(0, m_nNumItem - GetCount());
  100. }
  101. else
  102. m_pScrollbar->Enable(false);
  103. }
  104. }
  105. //--------------------------------------------------------------------------
  106. // 功能:窗口函数
  107. //--------------------------------------------------------------------------
  108. int KWndList2::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  109. {
  110. switch(uMsg)
  111. {
  112. case WM_MOUSEMOVE://to be check
  113. OnMouseMove(LOWORD(nParam), HIWORD(nParam));
  114. break;
  115. case WM_LBUTTONDOWN:
  116. OnLButtonDown(LOWORD(nParam), HIWORD(nParam));
  117. break;
  118. case WM_MOUSEWHEEL:
  119. if (m_pScrollbar && !m_pScrollbar->IsDisable())
  120. {
  121. int zDelta = short(HIWORD(uParam));
  122. int nPos = m_pScrollbar->GetScrollPos();
  123. nPos += (-zDelta / WHEEL_DELTA);
  124. m_pScrollbar->SetScrollPos(nPos);
  125. }
  126. break;
  127. default:
  128. return KWndWindow::WndProc(uMsg, nParam, nParam);
  129. }
  130. return 0;
  131. }
  132. int KWndList2::SetCurSel(int nSel)
  133. {
  134. if (nSel >= 0 && nSel < m_nNumItem && nSel != m_nSelItemIndex)
  135. {
  136. m_nSelItemIndex = nSel;
  137. if (m_nSelItemIndex < m_nTopItemIndex)
  138. m_nTopItemIndex = m_nSelItemIndex;
  139. else if(m_nSelItemIndex >= m_nTopItemIndex + GetVisibleLineCount())
  140. {
  141. m_nTopItemIndex = m_nSelItemIndex - GetVisibleLineCount() + 1;
  142. if (m_nTopItemIndex < 0)
  143. m_nTopItemIndex = 0;
  144. }
  145. if (m_pParentWnd)
  146. m_pParentWnd->WndProc(WND_N_LIST_ITEM_SEL, (unsigned int)(KWndWindow*)this, m_nSelItemIndex);
  147. }
  148. return m_nSelItemIndex;
  149. }
  150. //--------------------------------------------------------------------------
  151. // 功能:响应鼠标左键在此按下
  152. //--------------------------------------------------------------------------
  153. void KWndList2::OnLButtonDown(int x, int y)
  154. {
  155. int nSel = m_nTopItemIndex + (y - m_nAbsoluteTop) / (m_nFontSize + 1);
  156. if (nSel >= m_nNumItem)
  157. nSel = -1;
  158. if (nSel != m_nSelItemIndex)
  159. {
  160. m_nSelItemIndex = nSel;
  161. if (m_pParentWnd)
  162. m_pParentWnd->WndProc(WND_N_LIST_ITEM_SEL, (unsigned int)(KWndWindow*)this, m_nSelItemIndex);
  163. }
  164. }
  165. //--------------------------------------------------------------------------
  166. // 功能:响应鼠标移动
  167. //--------------------------------------------------------------------------
  168. void KWndList2::OnMouseMove(int x, int y)
  169. {
  170. if (m_Style & WNDLIST_ES_HIGHLIGHT_ENABLE)
  171. {
  172. int nIndex = m_nTopItemIndex + (y - m_nAbsoluteTop) / (m_nFontSize + 1);
  173. if (nIndex >= m_nNumItem)
  174. nIndex = -1;
  175. if (nIndex != m_nHighLightItemIndex)
  176. {
  177. m_nHighLightItemIndex = nIndex;
  178. if (m_pParentWnd)
  179. m_pParentWnd->WndProc(WND_N_LIST_ITEM_HIGHLIGHT, (unsigned int)(KWndWindow*)this, m_nHighLightItemIndex);
  180. }
  181. }
  182. }
  183. //--------------------------------------------------------------------------
  184. // 功能:添加项
  185. // 参数:int nIndex --> 新项添加的位置(基于0的索引数)
  186. //  const char* pString --> 新项的字符串
  187. // 返回:成功添加的话,返回新项在列表中的索引未知;否则返回WNDLIST_ERROR,
  188. // 失败的原因可能是pString指针为NULL,或者无法为新项分配存储空间。
  189. //--------------------------------------------------------------------------
  190. int KWndList2::AddString(int nIndex, const char* pString)
  191. {
  192. if (pString == NULL)
  193. return WNDLIST_ERROR;
  194. if (m_nItemSpace >= m_nNumItem)
  195. {
  196. void** pNew = (void**)realloc(m_pContent, sizeof(void*) * (m_nItemSpace + 10));
  197. if (pNew)
  198. {
  199. m_pContent = pNew;
  200. m_nItemSpace += 10;
  201. }
  202. else
  203. return WNDLIST_ERROR;
  204. }
  205. int nStrLen = strlen(pString);
  206. KWndList2Item* pItem = (KWndList2Item*)malloc(sizeof(KWndList2Item) + nStrLen);
  207. if (pItem)
  208. {
  209. pItem->Data = 0;
  210. pItem->StringSize = nStrLen;
  211. memcpy(pItem->String, pString, nStrLen);
  212. pItem->String[nStrLen] = 0;
  213. if (nIndex < 0)
  214. nIndex = 0;
  215. else if (nIndex > m_nNumItem)
  216. nIndex = m_nNumItem;
  217. for (int i = m_nNumItem; i > nIndex; i--)
  218. m_pContent[i] = m_pContent[i - 1];
  219. m_pContent[nIndex] = pItem;
  220. m_nNumItem ++;
  221. if (m_Style & WNDLIST_ES_HIGHLIGHT_ENABLE)
  222. {
  223. m_nHighLightItemIndex = -1;
  224. if (m_pParentWnd)
  225. m_pParentWnd->WndProc(WND_N_LIST_ITEM_HIGHLIGHT, (unsigned int)(KWndWindow*)this, m_nHighLightItemIndex);
  226. }
  227. if (m_nSelItemIndex >= nIndex)
  228. {
  229. m_nSelItemIndex ++;
  230. if (m_pParentWnd)
  231. m_pParentWnd->WndProc(WND_N_LIST_ITEM_SEL, (unsigned int)(KWndWindow*)this, m_nSelItemIndex);
  232. }
  233. if (m_pScrollbar)
  234.     {
  235.      if(m_nNumItem > GetVisibleLineCount())
  236.     {
  237.      m_pScrollbar->Enable(TRUE);
  238.     }
  239.     else
  240.     {
  241.      m_pScrollbar->Enable(FALSE);
  242.     }
  243. m_pScrollbar->SetValueRange(0, m_nNumItem);
  244.     }
  245. }
  246. else
  247. nIndex = WNDLIST_ERROR;
  248. return nIndex;
  249. }
  250. //--------------------------------------------------------------------------
  251. // 功能:删除项
  252. // 参数:int nIndex --> 要删除项的索引
  253. // 返回:如果成功删除指定的项,则返回列表里剩余项的数目,否则返回WNDLIST_ERROR
  254. //--------------------------------------------------------------------------
  255. int KWndList2::DeleteString(int nIndex)
  256. {
  257. if (nIndex >= 0 && nIndex < m_nNumItem)
  258. {
  259. free(m_pContent[nIndex]);
  260. m_nNumItem --;
  261. for (int i = nIndex; i < m_nNumItem; i++)
  262. m_pContent[i] = m_pContent[i + 1];
  263. if (m_nTopItemIndex >= m_nNumItem && m_nTopItemIndex)
  264. m_nTopItemIndex --;
  265. if (m_nSelItemIndex > nIndex)
  266. m_nSelItemIndex --;
  267. else if (m_nSelItemIndex == nIndex)
  268. m_nSelItemIndex = -1;
  269. if (m_Style & WNDLIST_ES_HIGHLIGHT_ENABLE)
  270. {
  271. m_nHighLightItemIndex = -1;
  272. if (m_pParentWnd)
  273. m_pParentWnd->WndProc(WND_N_LIST_ITEM_HIGHLIGHT, (unsigned int)(KWndWindow*)this, m_nHighLightItemIndex);
  274. }
  275. if (m_pParentWnd)
  276. m_pParentWnd->WndProc(WND_N_LIST_ITEM_SEL, (unsigned int)(KWndWindow*)this, m_nSelItemIndex);
  277. if (m_pScrollbar)
  278.     {
  279.      if(m_nNumItem > GetVisibleLineCount())
  280.     {
  281.      m_pScrollbar->Enable(TRUE);
  282.     }
  283.     else
  284.     {
  285.      m_pScrollbar->Enable(FALSE);
  286.     }
  287. m_pScrollbar->SetValueRange(0, m_nNumItem);
  288.     }
  289. return m_nNumItem;
  290. }
  291. else
  292. return WNDLIST_ERROR;
  293. }
  294. //--------------------------------------------------------------------------
  295. // 功能:删除列表中全部的内容
  296. //--------------------------------------------------------------------------
  297. void KWndList2::ResetContent()
  298. {
  299. if (m_pContent)
  300. {
  301. for (int i = 0; i < m_nNumItem; i++)
  302. free(m_pContent[i]);
  303. m_nNumItem = 0;
  304. m_nItemSpace = 0;
  305. free(m_pContent);
  306. m_pContent = NULL;
  307. }
  308. m_nTopItemIndex = 0;
  309. m_nSelItemIndex = -1;
  310. m_nHighLightItemIndex = -1;
  311. if (m_pParentWnd)
  312. {
  313. if (m_Style & WNDLIST_ES_HIGHLIGHT_ENABLE)
  314. m_pParentWnd->WndProc(WND_N_LIST_ITEM_HIGHLIGHT, (unsigned int)(KWndWindow*)this, m_nHighLightItemIndex);
  315. m_pParentWnd->WndProc(WND_N_LIST_ITEM_SEL, (unsigned int)(KWndWindow*)this, m_nSelItemIndex);
  316. }
  317. if(m_pScrollbar)
  318. {
  319. m_pScrollbar->Enable(FALSE);
  320. m_pScrollbar->SetScrollPos(0);
  321. m_pScrollbar->SetValueRange(0, 0);
  322. }
  323. }
  324. //--------------------------------------------------------------------------
  325. // 功能:绘制窗口
  326. //--------------------------------------------------------------------------
  327. void KWndList2::PaintWindow()
  328. {
  329. KWndWindow::PaintWindow();
  330. if (g_pRepresentShell)
  331. {
  332. int y = m_nAbsoluteTop;
  333. int EndIndex = m_nTopItemIndex + GetVisibleLineCount();
  334. if (EndIndex > m_nNumItem)
  335. EndIndex = m_nNumItem;
  336. int nMaxLen = (m_Width * 2) / m_nFontSize + 1;
  337. char szBuffer[128];
  338. if (nMaxLen > sizeof(szBuffer))
  339. nMaxLen = sizeof(szBuffer);
  340. unsigned int Color;
  341. unsigned int BorderColor;
  342. for (int i = m_nTopItemIndex; i < EndIndex; i++)
  343. {
  344. if (i == m_nSelItemIndex)
  345. {
  346. Color = m_SelItemColor;
  347. BorderColor = m_SelItemBorderColor;
  348. }
  349. else if (i == m_nHighLightItemIndex)
  350. {
  351. Color = m_HighLightColor;
  352. BorderColor = m_HighLightBorderColor;
  353. }
  354. else
  355. {
  356. Color = m_ItemColor;
  357. BorderColor = m_ItemBorderColor;
  358. }
  359. KWndList2Item* pItem = (KWndList2Item*)m_pContent[i];
  360. const char* pShowString = TGetLimitLenString((const char*)(pItem->String), -1, szBuffer, nMaxLen);
  361. int nLen = strlen(pShowString);
  362. g_pRepresentShell->OutputText(m_nFontSize, pShowString, nLen, m_nAbsoluteLeft, y, Color, 0, TEXT_IN_SINGLE_PLANE_COORD, BorderColor);
  363. y += m_nFontSize + 1;
  364. }
  365. }
  366. }
  367. //--------------------------------------------------------------------------
  368. // 功能:查找列表(显示)字串
  369. //--------------------------------------------------------------------------
  370. int KWndList2::FindString(int nPrecedingStart, const char* pString)
  371. {
  372. if (pString)
  373. {
  374. unsigned int nLen = strlen(pString);
  375. for (int i = nPrecedingStart + 1; i < m_nNumItem; i++)
  376. {
  377. KWndList2Item* pItem = (KWndList2Item*)m_pContent[i];
  378. if (nLen == pItem->StringSize &&
  379. memcmp(pItem->String, pString, nLen) == 0)
  380. return i;
  381. }
  382. }
  383. return WNDLIST_ERROR;
  384. }
  385. //--------------------------------------------------------------------------
  386. // 功能:设置列表项关联数据的值
  387. //--------------------------------------------------------------------------
  388. int KWndList2::SetItemData(int nIndex, int nData)
  389. {
  390. if (nIndex >= 0 && nIndex < m_nNumItem)
  391. {
  392. ((KWndList2Item*)m_pContent[nIndex])->Data = nData;
  393. return 0;
  394. }
  395. return WNDLIST_ERROR;
  396. }
  397. //--------------------------------------------------------------------------
  398. // 功能:获得列表项数据中的一个整数
  399. //--------------------------------------------------------------------------
  400. int KWndList2::GetItemData(int nIndex)
  401. {
  402. if (nIndex >= 0 && nIndex < m_nNumItem)
  403. {
  404. return (((KWndList2Item*)m_pContent[nIndex])->Data);
  405. }
  406. return WNDLIST_ERROR;
  407. }
  408. //--------------------------------------------------------------------------
  409. // 功能:获得列表项的(显示)字串
  410. //--------------------------------------------------------------------------
  411. int KWndList2::GetString(int nIndex, char* pBuffer, unsigned int nSize)
  412. {
  413. if (nIndex >= 0 && nIndex < m_nNumItem && pBuffer)
  414. {
  415. KWndList2Item* pItem = (KWndList2Item*)m_pContent[nIndex];
  416. if (pItem->StringSize < nSize)
  417. {
  418. memcpy(pBuffer, pItem->String, pItem->StringSize);
  419. pBuffer[pItem->StringSize] = 0;
  420. return pItem->StringSize;
  421. }
  422. }
  423. return WNDLIST_ERROR;
  424. }
  425. //--------------------------------------------------------------------------
  426. // 功能:设置被显示的最顶列表项的索引值
  427. //--------------------------------------------------------------------------
  428. void KWndList2::SetTopItemIndex(int nTopItemIndex)
  429. {
  430. if (nTopItemIndex >= 0 && nTopItemIndex < m_nNumItem)
  431. m_nTopItemIndex = nTopItemIndex;
  432. }
  433. //--------------------------------------------------------------------------
  434. // 功能:获得列表框可以同时显示的项的数目
  435. //--------------------------------------------------------------------------
  436. int KWndList2::GetVisibleLineCount() const
  437. {
  438. return m_Height / (m_nFontSize + 1);
  439. }