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

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. // MsgListView.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "myicq.h"
  15. #include "MsgListView.h"
  16. #include "MsgView.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. void exportMsgTxt(CStdioFile &file, IcqMsg *msg, const char *nick)
  25. {
  26. CTime t((time_t) msg->when);
  27. CString str;
  28. CString text;
  29. getMsgText(msg, text);
  30. str.Format("%s %s  %sn%snn",
  31. t.Format("%Y-%m-%d"),
  32. t.Format("%H:%M:%S"),
  33. (msg->flags & MF_RECEIVED) ? nick : icqLink->myInfo.nick.c_str(),
  34. text
  35. );
  36. file.WriteString(str);
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CMsgListView
  40. IMPLEMENT_DYNCREATE(CMsgListView, CListView)
  41. CMsgListView::CMsgListView()
  42. {
  43. msgView = NULL;
  44. }
  45. CMsgListView::~CMsgListView()
  46. {
  47. }
  48. void CMsgListView::save(DBOutStream &out)
  49. {
  50. out << GetListCtrl().GetTextColor();
  51. out << GetListCtrl().GetBkColor();
  52. }
  53. void CMsgListView::load(DBInStream &in)
  54. {
  55. COLORREF color;
  56. in >> color;
  57. GetListCtrl().SetTextColor(color);
  58. in >> color;
  59. GetListCtrl().SetBkColor(color);
  60. }
  61. void CMsgListView::clear()
  62. {
  63. CListCtrl &ctrl = GetListCtrl();
  64. int n = ctrl.GetItemCount();
  65. for (int i = 0; i < n; ++i)
  66. delete (IcqMsg *) ctrl.GetItemData(i);
  67. ctrl.DeleteAllItems();
  68. }
  69. void CMsgListView::showMsg(DWORD uin)
  70. {
  71. clear();
  72. if (uin > 0xfffffff0) {
  73. msgView->showMsg(NULL);
  74. return;
  75. }
  76. PtrList msgList;
  77. IcqDB::loadMsg(uin, msgList);
  78. IcqUser &myInfo = icqLink->myInfo;
  79. CString nick;
  80. if (uin) {
  81. IcqContact *c = icqLink->findContact(uin);
  82. if (c)
  83. nick = c->nick.c_str();
  84. else
  85. nick.Format("%lu", uin);
  86. }
  87. CListCtrl &ctrl = GetListCtrl();
  88. IcqMsg *msg = NULL;
  89. for (int i = 0; !msgList.empty(); ++i) {
  90. msg = (IcqMsg *) msgList.front();
  91. msgList.pop_front();
  92. CString str;
  93. if (msg->flags & MF_RECEIVED) {
  94. if (uin)
  95. str = nick;
  96. else
  97. str.Format("%lu", msg->uin);
  98. } else
  99. str = icqLink->myInfo.nick.c_str();
  100. ctrl.InsertItem(i, str);
  101. ctrl.SetItemData(i, (DWORD) msg);
  102. CTime t((time_t) msg->when);
  103. ctrl.SetItemText(i, 1, t.Format("%Y-%m-%d"));
  104. ctrl.SetItemText(i, 2, t.Format("%H:%M:%S"));
  105. getMsgText(msg, str);
  106. ctrl.SetItemText(i, 3, str);
  107. }
  108. msgView->showMsg(msg);
  109. }
  110. void CMsgListView::delSelectedMsg()
  111. {
  112. CListCtrl &ctrl = GetListCtrl();
  113. POSITION pos;
  114. while ((pos = ctrl.GetFirstSelectedItemPosition()) != NULL) {
  115. int i = ctrl.GetNextSelectedItem(pos);
  116. IcqMsg *msg = (IcqMsg *) ctrl.GetItemData(i);
  117. uint32 uin = (msg->isSysMsg() ? 0 : msg->uin);
  118. IcqDB::delMsg(uin, i);
  119. ctrl.DeleteItem(i);
  120. delete msg;
  121. }
  122. }
  123. void CMsgListView::exportTxt(CStdioFile &file)
  124. {
  125. CString nick;
  126. CListCtrl &ctrl = GetListCtrl();
  127. POSITION pos = ctrl.GetFirstSelectedItemPosition();
  128. while (pos) {
  129. int i = ctrl.GetNextSelectedItem(pos);
  130. IcqMsg *msg = (IcqMsg *) ctrl.GetItemData(i);
  131. if (nick.IsEmpty()) {
  132. IcqContact *c = icqLink->findContact(msg->uin);
  133. if (c && !msg->isSysMsg())
  134. nick = c->nick.c_str();
  135. else
  136. nick.Format("%lu", c->uin);
  137. }
  138. exportMsgTxt(file, msg, nick);
  139. delete msg;
  140. }
  141. }
  142. BEGIN_MESSAGE_MAP(CMsgListView, CListView)
  143. //{{AFX_MSG_MAP(CMsgListView)
  144. ON_WM_CREATE()
  145. ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
  146. ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
  147. ON_UPDATE_COMMAND_UI(ID_EDIT_DEL, OnUpdateEditDel)
  148. ON_WM_DESTROY()
  149. //}}AFX_MSG_MAP
  150. END_MESSAGE_MAP()
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CMsgListView diagnostics
  153. #ifdef _DEBUG
  154. void CMsgListView::AssertValid() const
  155. {
  156. CListView::AssertValid();
  157. }
  158. void CMsgListView::Dump(CDumpContext& dc) const
  159. {
  160. CListView::Dump(dc);
  161. }
  162. #endif //_DEBUG
  163. /////////////////////////////////////////////////////////////////////////////
  164. // CMsgListView message handlers
  165. int CMsgListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  166. {
  167. if (CListView::OnCreate(lpCreateStruct) == -1)
  168. return -1;
  169. CListCtrl &ctrl = GetListCtrl();
  170. ctrl.SetExtendedStyle(ctrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  171. CString str;
  172. str.LoadString(IDS_MSG_SENDER);
  173. ctrl.InsertColumn(0, str, LVCFMT_LEFT, 100);
  174. str.LoadString(IDS_DATE);
  175. ctrl.InsertColumn(1, str, LVCFMT_LEFT, 100);
  176. str.LoadString(IDS_TIME);
  177. ctrl.InsertColumn(2, str, LVCFMT_LEFT, 100);
  178. str.LoadString(IDS_CONTENT);
  179. ctrl.InsertColumn(3, str, LVCFMT_LEFT, 500);
  180. return 0;
  181. }
  182. BOOL CMsgListView::PreCreateWindow(CREATESTRUCT& cs) 
  183. {
  184. cs.style |= LVS_REPORT | LVS_SHOWSELALWAYS;
  185. return CListView::PreCreateWindow(cs);
  186. }
  187. void CMsgListView::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
  188. {
  189. CListCtrl &ctrl = GetListCtrl();
  190. IcqMsg *msg = NULL;
  191. POSITION pos = ctrl.GetFirstSelectedItemPosition();
  192. if (pos) {
  193. int i = ctrl.GetNextSelectedItem(pos);
  194. msg = (IcqMsg *) ctrl.GetItemData(i);
  195. }
  196. msgView->showMsg(msg);
  197. *pResult = 0;
  198. }
  199. void CMsgListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
  200. {
  201. CMenu menu;
  202. menu.LoadMenu(IDR_MSGMGR_POPUP);
  203. CPoint pt;
  204. GetCursorPos(&pt);
  205. menu.GetSubMenu(0)->TrackPopupMenu(0, pt.x, pt.y, GetParentFrame());
  206. *pResult = 0;
  207. }
  208. void CMsgListView::OnUpdateEditDel(CCmdUI* pCmdUI) 
  209. {
  210. if (GetParentFrame()->GetActiveView() == this)
  211. pCmdUI->Enable(GetListCtrl().GetSelectedCount() > 0);
  212. }
  213. void CMsgListView::OnDestroy() 
  214. {
  215. clear();
  216. CListView::OnDestroy();
  217. }