CopyProperties.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // CopyProperties.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "CopyProperties.h"
  6. #include ".\copyproperties.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CCopyProperties dialog
  14. CCopyProperties::CCopyProperties(long lCopyID, CWnd* pParent /*=NULL*/)
  15. : CDialog(CCopyProperties::IDD, pParent)
  16. {
  17. m_lCopyID = lCopyID;
  18. m_bDeletedData = false;
  19. m_bChangedText = false;
  20. m_bHandleKillFocus = false;
  21. m_bHideOnKillFocus = false;
  22. //{{AFX_DATA_INIT(CCopyProperties)
  23. m_eDisplayText = _T("");
  24. m_eDate = _T("");
  25. m_bNeverAutoDelete = FALSE;
  26. //}}AFX_DATA_INIT
  27. }
  28. void CCopyProperties::DoDataExchange(CDataExchange* pDX)
  29. {
  30. CDialog::DoDataExchange(pDX);
  31. //{{AFX_DATA_MAP(CCopyProperties)
  32. DDX_Control(pDX, IDC_HOTKEY, m_HotKey);
  33. DDX_Control(pDX, IDC_COPY_DATA, m_lCopyData);
  34. DDX_Text(pDX, IDC_EDIT_DISPLAY_TEXT, m_eDisplayText);
  35. DDV_MaxChars(pDX, m_eDisplayText, 250);
  36. DDX_Text(pDX, IDC_DATE, m_eDate);
  37. DDX_Check(pDX, IDC_NEVER_AUTO_DELETE, m_bNeverAutoDelete);
  38. //}}AFX_DATA_MAP
  39. DDX_Control(pDX, IDC_PARSE_EDIT, m_ParseEdit);
  40. DDX_Control(pDX, IDC_PARSE_BUTTON, m_ParseButton);
  41. }
  42. BEGIN_MESSAGE_MAP(CCopyProperties, CDialog)
  43. //{{AFX_MSG_MAP(CCopyProperties)
  44. ON_BN_CLICKED(IDC_DELETE_COPY_DATA, OnDeleteCopyData)
  45. ON_WM_ACTIVATE()
  46. //}}AFX_MSG_MAP
  47. ON_BN_CLICKED(IDC_PARSE_BUTTON, OnBnClickedParseButton)
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CCopyProperties message handlers
  51. BOOL CCopyProperties::OnInitDialog()
  52. {
  53. CDialog::OnInitDialog();
  54. m_ParseEdit.SetWindowText("\\r\\n");
  55. m_MainTable.Open("SELECT * FROM Main WHERE lID = %d", m_lCopyID);
  56. if(!m_MainTable.IsEOF())
  57. {
  58. CTime time(m_MainTable.m_lDate);
  59. m_eDate = time.Format("%m/%d/%Y %I:%M %p");
  60. m_eDisplayText = m_MainTable.m_strText;
  61. if(m_MainTable.m_lDontAutoDelete)
  62. {
  63. m_bNeverDelete = TRUE;
  64. m_bNeverAutoDelete = TRUE;
  65. }
  66. else
  67. {
  68. m_bNeverAutoDelete = FALSE;
  69. m_bNeverDelete = FALSE;
  70. }
  71. m_HotKey.SetHotKey(LOBYTE(m_MainTable.m_lShortCut), HIBYTE(m_MainTable.m_lShortCut));
  72. m_HotKey.SetRules(HKCOMB_A, 0);
  73. CString cs;
  74. cs.Format("SELECT * FROM Data WHERE lParentID = %d", m_MainTable.m_lID);
  75. m_DataTable.Open(AFX_DAO_USE_DEFAULT_TYPE, cs ,NULL);
  76. while(!m_DataTable.IsEOF())
  77. {
  78. cs.Format("%s, %d", m_DataTable.m_strClipBoardFormat, m_DataTable.m_ooData.m_dwDataLength);
  79. int nIndex = m_lCopyData.AddString(cs);
  80. m_lCopyData.SetItemData(nIndex, m_DataTable.m_lID);
  81. m_DataTable.MoveNext();
  82. }
  83. }
  84. UpdateData(FALSE);
  85. SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
  86. m_HotKey.SetFocus();
  87. return FALSE;
  88. }
  89. void CCopyProperties::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  90. {
  91. CDialog::OnActivate(nState, pWndOther, bMinimized);
  92. if (nState == WA_INACTIVE)
  93. {
  94. if(m_bHideOnKillFocus)
  95. {
  96. if(!m_bHandleKillFocus)
  97. {
  98. EndDialog(-1);
  99. m_bHandleKillFocus = false;
  100. }
  101. }
  102. }
  103. else if (nState == WA_ACTIVE)
  104. {
  105. SetFocus();
  106. ::SetForegroundWindow(m_hWnd);
  107. }
  108. }
  109. void CCopyProperties::OnOK()
  110. {
  111. UpdateData();
  112. bool bUpdate = false;
  113. long lHotKey = m_HotKey.GetHotKey();
  114. if(lHotKey != m_MainTable.m_lShortCut)
  115. {
  116. if(lHotKey > 0)
  117. {
  118. CMainTable recset;
  119. recset.Open("SELECT * FROM Main WHERE lShortCut = %d", lHotKey);
  120. if(!recset.IsEOF())
  121. {
  122. CString cs;
  123. cs.Format("The shortcut is currently assigned to \n\n%s\n\nAssign to new copy item?", recset.m_strText);
  124. if(MessageBox(cs, "Ditto", MB_YESNO) == IDNO)
  125. return;
  126. recset.Edit();
  127. recset.m_lShortCut = 0;
  128. recset.Update();
  129. }
  130. }
  131. if(!bUpdate)
  132. m_MainTable.Edit();
  133. m_MainTable.m_lShortCut = lHotKey;
  134. bUpdate = true;
  135. }
  136. if(m_eDisplayText != m_MainTable.m_strText)
  137. {
  138. if(!bUpdate)
  139. m_MainTable.Edit();
  140. m_bChangedText = true;
  141. m_MainTable.m_strText = m_eDisplayText;
  142. bUpdate = true;
  143. }
  144. if(m_bNeverAutoDelete != m_bNeverDelete)
  145. {
  146. if(!bUpdate)
  147. m_MainTable.Edit();
  148. if(m_bNeverAutoDelete)
  149. m_MainTable.m_lDontAutoDelete = (long)CTime::GetCurrentTime().GetTime();
  150. else
  151. m_MainTable.m_lDontAutoDelete = 0;
  152. bUpdate = true;
  153. }
  154. if(m_bDeletedData)
  155. {
  156. m_DeletedData.SortAscending();
  157. long lNewTotalSize = 0;
  158. //Go through the data table and find the deleted items
  159. m_DataTable.MoveFirst();
  160. while(!m_DataTable.IsEOF())
  161. {
  162. if(m_DeletedData.Find(m_DataTable.m_lID))
  163. m_DataTable.Delete();
  164. else
  165. lNewTotalSize += m_DataTable.m_ooData.m_dwDataLength;
  166. m_DataTable.MoveNext();
  167. }
  168. if(lNewTotalSize > 0)
  169. {
  170. if(!bUpdate)
  171. m_MainTable.Edit();
  172. m_MainTable.m_lTotalCopySize = lNewTotalSize;
  173. bUpdate = true;
  174. }
  175. }
  176. if(bUpdate)
  177. m_MainTable.Update();
  178. m_bHandleKillFocus = true;
  179. CDialog::OnOK();
  180. }
  181. void CCopyProperties::OnDeleteCopyData()
  182. {
  183. int nCount = m_lCopyData.GetSelCount();
  184. if(nCount)
  185. {
  186. m_bDeletedData = true;
  187. //Get the selected indexes
  188. ARRAY items;
  189. items.SetSize(nCount);
  190. m_lCopyData.GetSelItems(nCount, items.GetData());
  191. items.SortDescending();
  192. //Get the selected itemdata
  193. for(int i = 0; i < nCount; i++)
  194. {
  195. m_DeletedData.Add(m_lCopyData.GetItemData(items[i]));
  196. m_lCopyData.DeleteString(items[i]);
  197. }
  198. }
  199. }
  200. void CCopyProperties::OnCancel()
  201. {
  202. m_bHandleKillFocus = true;
  203. CDialog::OnCancel();
  204. }
  205. void CCopyProperties::OnBnClickedParseButton()
  206. {
  207. CString sep;
  208. m_ParseEdit.GetWindowText(sep);
  209. sep = RemoveEscapes( sep );
  210. int nSep = sep.GetLength();
  211. if( nSep <= 0 )
  212. {
  213. ::MessageBox( m_hWnd, "Token Separator is not valid.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  214. return;
  215. }
  216. HGLOBAL hGlobal = CClip::LoadFormat( m_lCopyID, CF_TEXT );
  217. if( !hGlobal )
  218. {
  219. ::MessageBox( m_hWnd, "No CF_TEXT Format Data could be found.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  220. return;
  221. }
  222. int nDataSize = ::GlobalSize( hGlobal );
  223. char* pData = (char*) ::GlobalLock( hGlobal );
  224. char* pDataEnd = pData + nDataSize;
  225. int nDataLen = 0;
  226. char* p = pData;
  227. ASSERT( pData != NULL && nDataSize > 0 );
  228. // Find the terminating NULL
  229. while( *p != '\0' && p < pDataEnd ) { p++; }
  230. // there was no NULL in the string!
  231. if( p >= pDataEnd )
  232. {
  233. ASSERT(0);
  234. return;
  235. }
  236. // set pDataEnd to the terminating NULL
  237. pDataEnd = p;
  238. CStringArray tokens;
  239. char* pToken = pData;
  240. p = pData;
  241. while( p = strstr( pToken, sep ) )
  242. {
  243. // null out the separator string
  244. for( int i=0; i < nSep; i++, p++ ) { *p = '\0'; }
  245. // add the substring token
  246. tokens.Add( pToken );
  247. pToken = p; // start the next token after the separator
  248. }
  249. // if we found at least one occurrence of sep, then get the last token
  250. if( tokens.GetCount() > 0 )
  251. tokens.Add( pToken );
  252. ::GlobalUnlock( hGlobal );
  253. ::GlobalFree(hGlobal);
  254. CClip clip;
  255. int len;
  256. long lDate = (long) CTime::GetCurrentTime().GetTime();
  257. int count = tokens.GetCount();
  258. // add in reverse order so that right to left corresponds to top to bottom
  259. // in the Ditto window (ditto sorts by time).
  260. for( int i = count - 1; i >= 0; i-- )
  261. {
  262. theApp.SetStatus( StrF("%d",i+1) );
  263. len = tokens[i].GetLength();
  264. if( len <= 0 )
  265. continue;
  266. clip.AddFormat( CF_TEXT, (void*) (LPCTSTR) tokens[i], len+1 );
  267. clip.m_Time = lDate;
  268. clip.AddToDB();
  269. clip.Clear();
  270. lDate++; // make sure they are sequential
  271. }
  272. theApp.SetStatus();
  273. if( count <= 0 )
  274. ::MessageBox( m_hWnd, "No new tokens found by parsing", "Parse Failed", MB_OK|MB_ICONINFORMATION );
  275. else
  276. {
  277. ::MessageBox( m_hWnd, StrF("Successfully parsed %d tokens.", tokens.GetCount()), "Parse Completed", MB_OK|MB_ICONINFORMATION );
  278. theApp.RefreshView();
  279. }
  280. }