ProcessPaste.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "ProcessPaste.h"
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char THIS_FILE[]=__FILE__;
  7. #define new DEBUG_NEW
  8. #endif
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CProcessPaste::CProcessPaste()
  13. {
  14. }
  15. CProcessPaste::~CProcessPaste()
  16. {
  17. }
  18. BOOL CProcessPaste::LoadDataAndPaste(long lID, HWND hWnd)
  19. {
  20. COleDataSource *pData = new COleDataSource;
  21. if(LoadData(lID, pData))
  22. {
  23. MarkDataAsPasted(lID);
  24. //Set this becase we just change the clipboard data so we will get a
  25. //clipboard change message
  26. //If this is set we don't handle that message
  27. theApp.m_bHandleClipboardDataChange = false;
  28. // pData->FlushClipboard();
  29. pData->SetClipboard();
  30. SendPaste(hWnd);
  31. //Set this back to handling messages
  32. theApp.m_bHandleClipboardDataChange = true;
  33. return TRUE;
  34. }
  35. return FALSE;
  36. }
  37. BOOL CProcessPaste::LoadDataAndDrag(long lID)
  38. {
  39. COleDataSource *pData = new COleDataSource;
  40. if(LoadData(lID, pData))
  41. {
  42. if(pData->DoDragDrop(DROPEFFECT_COPY) != DROPEFFECT_NONE)
  43. {
  44. MarkDataAsPasted(lID);
  45. return TRUE;
  46. }
  47. }
  48. return FALSE;
  49. }
  50. BOOL CProcessPaste::MarkDataAsPasted(long lID)
  51. {
  52. try
  53. {
  54. //Update the time it was copied so that it appears at the top of the
  55. //past list items are sorted by this time
  56. CMainTable ctMain;
  57. ctMain.Open("SELECT * FROM Main WHERE lID = %d", lID);
  58. ctMain.Edit();
  59. CTime now = CTime::GetCurrentTime();
  60. ctMain.m_lDate = (long)now.GetTime();
  61. ctMain.Update();
  62. ctMain.Close();
  63. CGetSetOptions::SetTripPasteCount(-1);
  64. CGetSetOptions::SetTotalPasteCount(-1);
  65. return TRUE;
  66. }
  67. catch(CDaoException *e)
  68. {
  69. ASSERT(FALSE);
  70. e->Delete();
  71. }
  72. return FALSE;
  73. }
  74. BOOL CProcessPaste::LoadData(long lID, COleDataSource *pData)
  75. {
  76. if(pData)
  77. {
  78. int nCounter = 0;
  79. try
  80. {
  81. CDataTable recset;
  82. //Open the data table for all that have the parent id
  83. CString csSQL, csFormatText;
  84. csSQL.Format("SELECT * FROM Data WHERE lParentID = %d", lID);
  85. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, csSQL);
  86. //Go throught each item in the db putting it on the clipboard
  87. while(!recset.IsEOF())
  88. {
  89. csFormatText = recset.m_strClipBoardFormat;
  90. if(recset.LoadData(pData, GetFormatID(csFormatText)))
  91. nCounter++;
  92. recset.MoveNext();
  93. }
  94. recset.Close();
  95. if(nCounter)
  96. return TRUE;
  97. }
  98. catch(CDaoException* e)
  99. {
  100. ASSERT(FALSE);
  101. e->Delete();
  102. }
  103. }
  104. return FALSE;
  105. }
  106. bool CopyToGlobal( HGLOBAL hGlobal, LPVOID pBuf, ULONG ulBufLen )
  107. {
  108. if( hGlobal == 0 || pBuf == 0 || ulBufLen == 0 )
  109. {
  110. ASSERT(FALSE);
  111. return 0;
  112. }
  113. LPVOID pvData = GlobalLock(hGlobal);
  114. if(!pvData)
  115. {
  116. ASSERT(FALSE);
  117. return false;
  118. }
  119. ULONG size = GlobalSize(hGlobal);
  120. if( size < ulBufLen )
  121. {
  122. ASSERT(FALSE);
  123. GlobalUnlock(hGlobal);
  124. return false;
  125. }
  126. memcpy(pvData, pBuf, ulBufLen);
  127. GlobalUnlock(hGlobal);
  128. return true;
  129. }
  130. HGLOBAL NewGlobal( LPVOID pBuf, ULONG ulBufLen )
  131. {
  132. if( pBuf == 0 || ulBufLen == 0 )
  133. {
  134. ASSERT(FALSE);
  135. return 0;
  136. }
  137. HGLOBAL hGlobal = GlobalAlloc( GMEM_MOVEABLE, ulBufLen );
  138. if( hGlobal == 0 )
  139. {
  140. ASSERT(FALSE);
  141. return 0;
  142. }
  143. CopyToGlobal( hGlobal, pBuf, ulBufLen );
  144. return hGlobal;
  145. }
  146. BOOL CProcessPaste::MultiPaste( int numIDs, int* pIDs, HWND hWnd )
  147. {
  148. if( numIDs <= 0 || pIDs == NULL || hWnd == NULL )
  149. return FALSE;
  150. if( numIDs == 1 )
  151. return LoadDataAndPaste( pIDs[0], hWnd );
  152. CString text = AggregateText(numIDs, pIDs, CF_TEXT, "\r\n");
  153. HGLOBAL hGlobal = NewGlobal( (void*)(LPCSTR) text, text.GetLength()+1 );
  154. if( !hGlobal )
  155. return FALSE;
  156. COleDataSource *pOle = new COleDataSource;
  157. pOle->CacheGlobalData(CF_TEXT, hGlobal);
  158. // Should we allow Ditto to add the aggregate to its history list?
  159. bool bOldHandleChange = theApp.m_bHandleClipboardDataChange;
  160. theApp.m_bHandleClipboardDataChange = false;
  161. // pOle->FlushClipboard();
  162. pOle->SetClipboard();
  163. SendPaste(hWnd);
  164. //Set this back to original value
  165. theApp.m_bHandleClipboardDataChange = bOldHandleChange;
  166. return TRUE;
  167. }
  168. BOOL CProcessPaste::MultiDrag( int numIDs, int* pIDs, HWND hWnd )
  169. {
  170. if( numIDs <= 0 || pIDs == NULL || hWnd == NULL )
  171. return FALSE;
  172. if( numIDs == 1 )
  173. return LoadDataAndDrag( pIDs[0] );
  174. CString text = AggregateText(numIDs, pIDs, CF_TEXT, "\r\n");
  175. HGLOBAL hGlobal = NewGlobal( (void*)(LPCSTR) text, text.GetLength()+1 );
  176. if( !hGlobal )
  177. return FALSE;
  178. COleDataSource *pOle = new COleDataSource;
  179. pOle->CacheGlobalData(CF_TEXT, hGlobal);
  180. if( pOle->DoDragDrop(DROPEFFECT_COPY) != DROPEFFECT_NONE )
  181. return TRUE;
  182. return FALSE;
  183. }
  184. // This assumes that the given uiPastType is a null terminated text type
  185. CString CProcessPaste::AggregateText(int numIDs, int* pIDs, UINT uiPastType, char* pSeparator)
  186. {
  187. CString csSQL;
  188. CDataTable recset;
  189. CString text;
  190. char* pData = NULL;
  191. DWORD len;
  192. DWORD maxLen;
  193. // maybe we should sum up the "recset.m_ooData.m_dwDataLength" of all IDs first
  194. // in order to determine the max space required??? Or would that be wastefull?
  195. // allocate a large initial buffer to minimize realloc for concatenations
  196. text.GetBuffer(4096);
  197. text = "";
  198. csSQL.Format("SELECT * FROM Data WHERE strClipBoardFormat = \'%s\' AND lParentID = %%d", GetFormatName(uiPastType));
  199. try
  200. {
  201. for( int i=0; i < numIDs; i++ )
  202. {
  203. recset.Open( csSQL, pIDs[i] );
  204. if( !recset.IsEOF() )
  205. {
  206. maxLen = recset.m_ooData.m_dwDataLength;
  207. if( maxLen == 0 )
  208. continue;
  209. pData = (char*) GlobalLock(recset.m_ooData.m_hData);
  210. if(!pData)
  211. {
  212. ASSERT(FALSE);
  213. break;
  214. }
  215. // verify that pData is null terminated
  216. // do a quick check to see if the last character is null
  217. if( pData[maxLen-1] != '\0' )
  218. {
  219. for( len=0; len < maxLen && pData[len] != '\0'; len++ ) {}
  220. // if it is not null terminated, skip this item
  221. if( len >= maxLen )
  222. continue;
  223. }
  224. text += pData;
  225. GlobalUnlock(recset.m_ooData.m_hData);
  226. if( pSeparator )
  227. text += pSeparator;
  228. }
  229. recset.Close();
  230. }
  231. }
  232. catch(CDaoException* e)
  233. {
  234. ASSERT(FALSE);
  235. e->Delete();
  236. }
  237. return text;
  238. }
  239. void CProcessPaste::SendPaste(HWND hWnd)
  240. {
  241. if(hWnd)
  242. {
  243. //Set a Control key down then a 'V' down then the ups
  244. //To simulate a paste
  245. ::SetForegroundWindow(hWnd);
  246. ::SetFocus(hWnd);
  247. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  248. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  249. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  250. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  251. }
  252. else
  253. ASSERT(FALSE);
  254. }