DittoRulerRichEditCtrl.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. CStringA csRTF = CTextConvert::ConvertToChar(csRTFOriginal);
  180. CClipFormat format;
  181. format.m_cfType = RegisterClipboardFormat(_T("Rich Text Format"));
  182. int nLength = csRTF.GetLength() + 1;
  183. format.m_hgData = NewGlobalP(csRTF.GetBuffer(nLength), nLength);
  184. Clip.m_Formats.Add(format);
  185. format.m_hgData = NULL; //Clip.m_formats owns data now
  186. return true;
  187. }
  188. bool CDittoRulerRichEditCtrl::LoadTextData(CClip &Clip)
  189. {
  190. CString csText = GetText();
  191. if(csText.IsEmpty())
  192. {
  193. for(int i = 0; i < 20; i++)
  194. {
  195. Sleep(100);
  196. csText = GetText();
  197. if(csText.IsEmpty() == FALSE)
  198. break;
  199. Log(StrF(_T("Get Text still empty pass = %d"), i));
  200. }
  201. if(csText.IsEmpty())
  202. {
  203. Log(_T("Get Text still empty pass returning"));
  204. return false;
  205. }
  206. }
  207. CClipFormat format;
  208. #ifdef _UNICODE
  209. format.m_cfType = CF_UNICODETEXT;
  210. #else
  211. format.m_cfType = CF_TEXT;
  212. #endif
  213. int nLength = csText.GetLength() * sizeof(TCHAR) + sizeof(TCHAR);
  214. format.m_hgData = NewGlobalP(csText.GetBuffer(nLength), nLength);
  215. Clip.SetDescFromText(format.m_hgData, true);
  216. m_csDescription = Clip.m_Desc;
  217. m_csDescription = m_csDescription.Left(15);
  218. Clip.m_Formats.Add(format);
  219. format.m_hgData = NULL; //Clip.m_formats owns data now
  220. return true;
  221. }
  222. bool CDittoRulerRichEditCtrl::CloseEdit(bool bPrompt, BOOL bUpdateDesc)
  223. {
  224. if(m_rtf.GetModify())
  225. {
  226. int nRet = IDYES;
  227. if(bPrompt)
  228. {
  229. CString cs;
  230. cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("SaveChanges", "Do you want to save changes to"), m_csDescription);
  231. ::SetForegroundWindow(m_hWnd);
  232. nRet = MessageBox(cs, _T("Ditto"), MB_YESNOCANCEL);
  233. }
  234. if(nRet == IDYES)
  235. {
  236. if(SaveToDB(bUpdateDesc) == false)
  237. {
  238. CString cs;
  239. cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("ErrorSaving", "Error saving clip"), m_csDescription);
  240. MessageBox(cs, _T("Ditto"), MB_OK);
  241. }
  242. }
  243. else if(nRet == IDCANCEL)
  244. {
  245. return false;
  246. }
  247. }
  248. return true;
  249. }