HistoryListBox.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. // HistoryListBox.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "myicq.h"
  15. #include "HistoryListBox.h"
  16. #include "GfxPopupMenu.h"
  17. #include "icqlink.h"
  18. #include "icqdb.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CHistoryListBox
  26. CHistoryListBox::CHistoryListBox()
  27. {
  28. uin = 0;
  29. }
  30. CHistoryListBox::~CHistoryListBox()
  31. {
  32. }
  33. BOOL CHistoryListBox::loadHistory(uint32 uin)
  34. {
  35. this->uin = uin;
  36. IcqContact *c = icqLink->findContact(uin);
  37. if (!c)
  38. return FALSE;
  39. PtrList msgList;
  40. IcqDB::loadMsg(uin, msgList);
  41. while (!msgList.empty()) {
  42. IcqMsg *msg = (IcqMsg *) msgList.back();
  43. msgList.pop_back();
  44. CTime t(msg->when);
  45. CString strTime = t.Format("(%Y-%m-%d %H:%M:%S)");
  46. CString str;
  47. str.Format("%s   %srn%s", strTime,
  48. (msg->flags & MF_RECEIVED) ? c->nick.c_str() : icqLink->myInfo.nick.c_str(),
  49. msg->text.c_str());
  50. if (msg->flags & MF_RELAY) {
  51. CString tmp;
  52. tmp.LoadString(IDS_SERVER_RELAY);
  53. str += "rn";
  54. str += tmp;
  55. }
  56. AddString(str);
  57. delete msg;
  58. }
  59. return TRUE;
  60. }
  61. BEGIN_MESSAGE_MAP(CHistoryListBox, CListBox)
  62. //{{AFX_MSG_MAP(CHistoryListBox)
  63. ON_WM_RBUTTONDOWN()
  64. ON_COMMAND(ID_RECORD_COPY, OnRecordCopy)
  65. ON_COMMAND(ID_RECORD_DEL, OnRecordDel)
  66. ON_COMMAND(ID_RECORD_CUT, OnRecordCut)
  67. //}}AFX_MSG_MAP
  68. END_MESSAGE_MAP()
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CHistoryListBox message handlers
  71. void CHistoryListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  72. {
  73. ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
  74. int index = lpDrawItemStruct->itemID;
  75. if (index < 0)
  76. return;
  77. CRect rcItem(lpDrawItemStruct->rcItem);
  78. CString strText;
  79. GetText(index, strText);
  80. CDC dc;
  81. dc.Attach(lpDrawItemStruct->hDC);
  82. COLORREF bgColor;
  83. if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
  84. (lpDrawItemStruct->itemState & ODS_SELECTED) &&
  85. (GetFocus() == this))
  86. bgColor = GetSysColor(COLOR_HIGHLIGHT);
  87. else
  88. bgColor = GetSysColor(COLOR_WINDOW);
  89. dc.FillSolidRect(rcItem, bgColor);
  90. COLORREF oldTextColor = dc.SetTextColor(RGB(255, 0, 0));
  91. int oldBkMode = dc.SetBkMode(TRANSPARENT);
  92. int i = strText.Find("rn");
  93. rcItem.top += dc.DrawText(strText, i, rcItem, DT_SINGLELINE);
  94. dc.SetTextColor(RGB(0, 0, 255));
  95. int j;
  96. i += 2;
  97. while ((j = strText.Find("rn", i)) >= 0) {
  98. rcItem.top += dc.DrawText(strText.Mid(i, j - i), rcItem, DT_SINGLELINE);
  99. i = j + 2;
  100. }
  101. dc.DrawText(strText.Right(strText.GetLength() - i), rcItem, DT_SINGLELINE);
  102. dc.SetBkMode(oldBkMode);
  103. dc.SetTextColor(oldTextColor);
  104. dc.Detach();
  105. }
  106. void CHistoryListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
  107. {
  108. ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);
  109. CDC *pDC = GetDC();
  110. TEXTMETRIC tm;
  111. pDC->GetTextMetrics(&tm);
  112. ReleaseDC(pDC);
  113. CString strText;
  114. GetText(lpMeasureItemStruct->itemID, strText);
  115. int i = -2;
  116. lpMeasureItemStruct->itemHeight = tm.tmAscent;
  117. while ((i = strText.Find("rn", i + 2)) >= 0)
  118. lpMeasureItemStruct->itemHeight += tm.tmAscent;
  119. }
  120. void CHistoryListBox::OnRButtonDown(UINT nFlags, CPoint point) 
  121. {
  122. BOOL out;
  123. int i = ItemFromPoint(point, out);
  124. if (out)
  125. return;
  126. SetCurSel(i);
  127. CMenu tmp;
  128. tmp.LoadMenu(IDR_RECORD);
  129. CGfxPopupMenu menu;
  130. menu.Attach(*tmp.GetSubMenu(0));
  131. menu.modifyMenu(ID_RECORD_COPY, IDB_COPY);
  132. menu.modifyMenu(ID_RECORD_CUT, IDB_CUT);
  133. menu.modifyMenu(ID_RECORD_DEL, IDB_DEL);
  134. CPoint pt;
  135. GetCursorPos(&pt);
  136. menu.TrackPopupMenu(0, pt.x, pt.y, this);
  137. CListBox::OnRButtonDown(nFlags, point);
  138. }
  139. void CHistoryListBox::OnRecordCopy() 
  140. {
  141. int i = GetCurSel();
  142. if (i < 0)
  143. return;
  144. CString str;
  145. GetText(i, str);
  146. if (OpenClipboard()) {
  147. HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE, str.GetLength() + 1);
  148. char *p = (char *) GlobalLock(hMem);
  149. if (p) {
  150. lstrcpy(p, str);
  151. GlobalUnlock(hMem);
  152. SetClipboardData(CF_TEXT, hMem);
  153. }
  154. CloseClipboard();
  155. }
  156. }
  157. void CHistoryListBox::OnRecordDel() 
  158. {
  159. int i = GetCurSel();
  160. if (i < 0)
  161. return;
  162. DeleteString(i);
  163. IcqDB::delMsg(uin, GetCount() - i);
  164. }
  165. void CHistoryListBox::OnRecordCut() 
  166. {
  167. OnRecordCopy();
  168. OnRecordDel();
  169. }