DittoRulerRichEditCtrl.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "stdafx.h"
  2. #include "clip.h"
  3. #include "CP_Main.h"
  4. #include "shared/TextConvert.h"
  5. #include ".\dittorulerricheditctrl.h"
  6. #include "CopyProperties.h"
  7. CDittoRulerRichEditCtrl::CDittoRulerRichEditCtrl(void)
  8. {
  9. m_SaveTypes = stCF_UNICODETEXT|stCF_TEXT|stRTF;
  10. m_lID = -1;
  11. }
  12. CDittoRulerRichEditCtrl::~CDittoRulerRichEditCtrl(void)
  13. {
  14. }
  15. bool CDittoRulerRichEditCtrl::LoadItem(long lID, CString csDesc)
  16. {
  17. bool bSetText = false;
  18. CClipFormat Clip;
  19. m_lID = lID;
  20. m_csDescription = csDesc;
  21. //If creating a new clip
  22. if(m_lID < 0)
  23. {
  24. m_rtf.SetModify(FALSE);
  25. return false;
  26. }
  27. Clip.m_cfType = RegisterClipboardFormat(CF_RTF);
  28. if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
  29. {
  30. LPVOID pvData = GlobalLock(Clip.m_hgData);
  31. if(pvData)
  32. {
  33. SetRTF((char*)pvData);
  34. bSetText = true;
  35. }
  36. GlobalUnlock(Clip.m_hgData);
  37. Clip.Free();
  38. Clip.Clear();
  39. }
  40. if(bSetText == false)
  41. {
  42. Clip.m_cfType = CF_UNICODETEXT;
  43. if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
  44. {
  45. LPVOID pvData = GlobalLock(Clip.m_hgData);
  46. if(pvData)
  47. {
  48. CString csText = (WCHAR*)pvData;
  49. SetText(csText);
  50. bSetText = true;
  51. }
  52. GlobalUnlock(Clip.m_hgData);
  53. Clip.Free();
  54. Clip.Clear();
  55. }
  56. }
  57. if(bSetText == false)
  58. {
  59. Clip.m_cfType = CF_TEXT;
  60. if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
  61. {
  62. LPVOID pvData = GlobalLock(Clip.m_hgData);
  63. if(pvData)
  64. {
  65. CString csText = (char*)pvData;
  66. SetText(csText);
  67. bSetText = true;
  68. }
  69. GlobalUnlock(Clip.m_hgData);
  70. Clip.Free();
  71. Clip.Clear();
  72. }
  73. }
  74. m_rtf.SetModify(FALSE);
  75. return bSetText;
  76. }
  77. long CDittoRulerRichEditCtrl::GetTypeFlags(long lID)
  78. {
  79. long lRet = stNONE;
  80. try
  81. {
  82. CLIPFORMAT cfType = CF_TEXT;
  83. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
  84. if(q.eof() == false)
  85. {
  86. lRet |= stCF_TEXT;
  87. }
  88. cfType = CF_UNICODETEXT;
  89. q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
  90. if(q.eof() == false)
  91. {
  92. lRet |= stCF_UNICODETEXT;
  93. }
  94. cfType = RegisterClipboardFormat(_T("Rich Text Format"));
  95. q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
  96. if(q.eof() == false)
  97. {
  98. lRet |= stRTF;
  99. }
  100. }
  101. CATCH_SQLITE_EXCEPTION
  102. return lRet;
  103. }
  104. void CDittoRulerRichEditCtrl::d()
  105. {
  106. CString cs = m_rtf.GetText();
  107. CString s;
  108. s.Format(_T("error = %d, %s"), GetLastError(), cs);
  109. MessageBox(s);
  110. }
  111. int CDittoRulerRichEditCtrl::SaveToDB(BOOL bUpdateDesc)
  112. {
  113. int nRet = FALSE;
  114. if(m_SaveTypes == stNONE && m_lID >= 0)
  115. {
  116. return DIDNT_NEED_TO_SAVE;
  117. }
  118. if(m_rtf.GetModify() == FALSE)
  119. {
  120. Log(_T("Clip has not been modified"));
  121. return DIDNT_NEED_TO_SAVE;
  122. }
  123. bool bSetModifyToFalse = true;
  124. try
  125. {
  126. CClip Clip;
  127. Clip.m_id = m_lID;
  128. if(m_SaveTypes & stRTF)
  129. {
  130. LoadRTFData(Clip);
  131. }
  132. if(m_SaveTypes & stCF_TEXT || m_SaveTypes & stCF_UNICODETEXT)
  133. {
  134. LoadTextData(Clip);
  135. }
  136. if(Clip.m_Formats.GetSize() <= 0)
  137. {
  138. return FALSE;
  139. }
  140. theApp.m_db.execDML(_T("begin transaction;"));
  141. if(m_lID >= 0)
  142. {
  143. Clip.SaveFromEditWnd(bUpdateDesc);
  144. }
  145. else
  146. {
  147. bSetModifyToFalse = false;
  148. Clip.MakeLatestOrder();
  149. CCopyProperties Prop(-1, this, &Clip);
  150. Prop.SetHandleKillFocus(true);
  151. Prop.SetToTopMost(false);
  152. if(Prop.DoModal() == IDOK)
  153. {
  154. Clip.AddToDB();
  155. m_csDescription = Clip.m_Desc;
  156. m_lID = Clip.m_id;
  157. bUpdateDesc = TRUE;
  158. bSetModifyToFalse = true;
  159. }
  160. }
  161. nRet = SAVED_CLIP_TO_DB;
  162. theApp.m_db.execDML(_T("commit transaction;"));
  163. if(bUpdateDesc)
  164. theApp.RefreshView();
  165. }
  166. CATCH_SQLITE_EXCEPTION
  167. if(bSetModifyToFalse)
  168. m_rtf.SetModify(FALSE);
  169. return nRet;
  170. }
  171. bool CDittoRulerRichEditCtrl::LoadRTFData(CClip &Clip)
  172. {
  173. CString csRTFOriginal = GetRTF();
  174. if(csRTFOriginal.IsEmpty())
  175. {
  176. Log(_T("Rtf is empty, returning"));
  177. return false;
  178. }
  179. //remove the line feed at the end, not sure why the righ text always adds this
  180. CString right = csRTFOriginal.Right(9);
  181. if (right == _T("\\par\r\n}\r\n"))
  182. {
  183. CString r = csRTFOriginal.Left(csRTFOriginal.GetLength() - 9);
  184. r += _T("}");
  185. csRTFOriginal = r;
  186. }
  187. CStringA csRTF = CTextConvert::ConvertToChar(csRTFOriginal);
  188. CClipFormat format;
  189. format.m_cfType = RegisterClipboardFormat(_T("Rich Text Format"));
  190. int nLength = csRTF.GetLength() + 1;
  191. format.m_hgData = NewGlobalP(csRTF.GetBuffer(nLength), nLength);
  192. Clip.m_Formats.Add(format);
  193. format.m_hgData = NULL; //Clip.m_formats owns data now
  194. return true;
  195. }
  196. bool CDittoRulerRichEditCtrl::LoadTextData(CClip &Clip)
  197. {
  198. CString csText = GetText();
  199. if(csText.IsEmpty())
  200. {
  201. for(int i = 0; i < 20; i++)
  202. {
  203. Sleep(100);
  204. csText = GetText();
  205. if(csText.IsEmpty() == FALSE)
  206. break;
  207. Log(StrF(_T("Get Text still empty pass = %d"), i));
  208. }
  209. if(csText.IsEmpty())
  210. {
  211. Log(_T("Get Text still empty pass returning"));
  212. return false;
  213. }
  214. }
  215. CClipFormat format;
  216. #ifdef _UNICODE
  217. format.m_cfType = CF_UNICODETEXT;
  218. #else
  219. format.m_cfType = CF_TEXT;
  220. #endif
  221. int nLength = csText.GetLength() * sizeof(TCHAR) + sizeof(TCHAR);
  222. format.m_hgData = NewGlobalP(csText.GetBuffer(nLength), nLength);
  223. Clip.SetDescFromText(format.m_hgData, true);
  224. m_csDescription = Clip.m_Desc;
  225. m_csDescription = m_csDescription.Left(15);
  226. Clip.m_Formats.Add(format);
  227. format.m_hgData = NULL; //Clip.m_formats owns data now
  228. return true;
  229. }
  230. bool CDittoRulerRichEditCtrl::CloseEdit(bool bPrompt, BOOL bUpdateDesc)
  231. {
  232. if(m_rtf.GetModify())
  233. {
  234. int nRet = IDYES;
  235. if(bPrompt)
  236. {
  237. CString cs;
  238. cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("SaveChanges", "Do you want to save changes to"), m_csDescription);
  239. ::SetForegroundWindow(m_hWnd);
  240. nRet = MessageBox(cs, _T("Ditto"), MB_YESNOCANCEL);
  241. }
  242. if(nRet == IDYES)
  243. {
  244. if(SaveToDB(bUpdateDesc) == false)
  245. {
  246. CString cs;
  247. cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("ErrorSaving", "Error saving clip"), m_csDescription);
  248. MessageBox(cs, _T("Ditto"), MB_OK);
  249. }
  250. }
  251. else if(nRet == IDCANCEL)
  252. {
  253. return false;
  254. }
  255. }
  256. return true;
  257. }