OleClipSource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "OleClipSource.h"
  4. #include "TextConvert.h"
  5. /*------------------------------------------------------------------*\
  6. COleClipSource
  7. \*------------------------------------------------------------------*/
  8. //IMPLEMENT_DYNAMIC(COleClipSource, COleDataSource)
  9. COleClipSource::COleClipSource()
  10. {
  11. m_bLoadedFormats = false;
  12. m_bOnlyPaste_CF_TEXT = false;
  13. m_bPasteHTMLFormatAs_CF_TEXT = false;
  14. }
  15. COleClipSource::~COleClipSource()
  16. {
  17. }
  18. BOOL COleClipSource::DoDelayRender()
  19. {
  20. CClipTypes types;
  21. m_ClipIDs.GetTypes(types);
  22. int count = types.GetSize();
  23. for(int i=0; i < count; i++)
  24. DelayRenderData(types[i]);
  25. return count;
  26. }
  27. #include "Client.h"
  28. BOOL COleClipSource::DoImmediateRender()
  29. {
  30. if(m_bLoadedFormats)
  31. return TRUE;
  32. m_bLoadedFormats = true;
  33. int count = m_ClipIDs.GetSize();
  34. if(count <= 0)
  35. return 0;
  36. if(count == 1)
  37. {
  38. CClip clip;
  39. CClipFormats formats;
  40. clip.LoadFormats(m_ClipIDs[0], m_bOnlyPaste_CF_TEXT);
  41. return PutFormatOnClipboard(&clip.m_Formats, m_bPasteHTMLFormatAs_CF_TEXT);
  42. }
  43. HGLOBAL hGlobal;
  44. CStringA csAText;
  45. if(m_ClipIDs.AggregateText(CF_TEXT, "\r\n", g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop, csAText))
  46. {
  47. long lLen = csAText.GetLength();
  48. hGlobal = NewGlobalP(csAText.GetBuffer(lLen), lLen+sizeof(char));
  49. csAText.ReleaseBuffer();
  50. CacheGlobalData(CF_TEXT, hGlobal);
  51. }
  52. CStringW Sep = _T("\r\n");
  53. CStringW csWText;
  54. if(m_ClipIDs.AggregateUnicodeText(CF_UNICODETEXT, Sep, g_Opt.m_bMultiPasteReverse && g_Opt.m_bHistoryStartTop, csWText))
  55. {
  56. long lLen = csWText.GetLength() * sizeof(wchar_t);
  57. hGlobal = NewGlobalP(csWText.GetBuffer(lLen), lLen+sizeof(wchar_t));
  58. csWText.ReleaseBuffer();
  59. CacheGlobalData(CF_UNICODETEXT, hGlobal);
  60. }
  61. // text = "{\rtf1";
  62. // text += m_ClipIDs.AggregateText(GetFormatID(CF_RTF), "\r\n", true);
  63. // text += "}";
  64. //
  65. // hGlobal = NewGlobalP((void*)(LPCSTR) text, text.GetLength()+1);
  66. // CacheGlobalData(GetFormatID(CF_RTF), hGlobal);
  67. return hGlobal != 0;
  68. }
  69. long COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats, bool bPasteHTMLFormatAs_CF_TEXT)
  70. {
  71. CClipFormat* pCF;
  72. bool bDelayedRenderCF_HDROP = false;
  73. int count = pFormats->GetSize();
  74. int i = 0;
  75. //see if the html format is in the list
  76. //if it is the list we will not paste CF_TEXT
  77. for(i = 0; i < count; i++)
  78. {
  79. pCF = &pFormats->ElementAt(i);
  80. if(bPasteHTMLFormatAs_CF_TEXT)
  81. {
  82. if(pCF->m_cfType == theApp.m_HTML_Format)
  83. break;
  84. }
  85. if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
  86. {
  87. bDelayedRenderCF_HDROP = true;
  88. }
  89. }
  90. //Didn't find html format
  91. if(i == count)
  92. bPasteHTMLFormatAs_CF_TEXT = false;
  93. for(i = 0; i < count; i++)
  94. {
  95. pCF = &pFormats->ElementAt(i);
  96. if(bDelayedRenderCF_HDROP)
  97. {
  98. if(pCF->m_cfType == CF_HDROP)
  99. {
  100. DelayRenderData(pCF->m_cfType);
  101. }
  102. continue;
  103. }
  104. if(bPasteHTMLFormatAs_CF_TEXT)
  105. {
  106. if(pCF->m_cfType == CF_TEXT)
  107. continue;
  108. if(pCF->m_cfType == theApp.m_HTML_Format)
  109. pCF->m_cfType = CF_TEXT;
  110. }
  111. CacheGlobalData( pCF->m_cfType, pCF->m_hgData );
  112. pCF->m_hgData = 0; // OLE owns it now
  113. }
  114. pFormats->RemoveAll();
  115. m_bLoadedFormats = true;
  116. return count;
  117. }
  118. BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  119. {
  120. static bool bInHere = false;
  121. if(bInHere)
  122. {
  123. return FALSE;
  124. }
  125. bInHere = true;
  126. HGLOBAL hData = NULL;
  127. CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
  128. if(pFind)
  129. {
  130. if(pFind->m_hgData)
  131. {
  132. hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
  133. }
  134. }
  135. else
  136. {
  137. CClip clip;
  138. clip.LoadFormats(m_ClipIDs[0]);
  139. CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
  140. CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
  141. if(pDittoDelayCF_HDROP && pCF_HDROP)
  142. {
  143. CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
  144. if(pData)
  145. {
  146. CString csComputerName;
  147. CString csIP;
  148. CTextConvert::ConvertFromUTF8(pData->m_cIP, csIP);
  149. CTextConvert::ConvertFromUTF8(pData->m_cComputerName, csComputerName);
  150. GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
  151. CClient cl;
  152. hData = cl.RequestCopiedFiles(*pCF_HDROP, csIP, csComputerName);
  153. }
  154. }
  155. else
  156. {
  157. hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
  158. }
  159. //Add to a cache of already rendered data
  160. //Windows seems to call this function multiple times
  161. //so only the first time do we need to go get the data
  162. HGLOBAL hCopy = NULL;
  163. if(hData)
  164. {
  165. hCopy = NewGlobalH(hData, GlobalSize(hData));
  166. }
  167. CClipFormat format(lpFormatEtc->cfFormat, hCopy);
  168. format.bDeleteData = false; //owned by m_DelayRenderedFormats
  169. m_DelayRenderedFormats.Add(format);
  170. }
  171. BOOL bRet = FALSE;
  172. if(hData)
  173. {
  174. // if phGlobal is null, we can just give the allocated mem
  175. // else, our data must fit within the GlobalSize(*phGlobal)
  176. if(*phGlobal == 0)
  177. *phGlobal = hData;
  178. else
  179. {
  180. UINT len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
  181. if(len)
  182. CopyToGlobalHH(*phGlobal, hData, len);
  183. ::GlobalFree(hData);
  184. }
  185. bRet = TRUE;
  186. }
  187. bInHere = false;
  188. return bRet;
  189. }