DittoRulerRichEditCtrl.cpp 6.4 KB

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