DittoRulerRichEditCtrl.cpp 6.3 KB

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