CopyProperties.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. DDX_Control(pDX, IDC_EDIT_DISPLAY_TEXT, m_DisplayTextEdit);
  42. }
  43. BEGIN_MESSAGE_MAP(CCopyProperties, CDialog)
  44. //{{AFX_MSG_MAP(CCopyProperties)
  45. ON_BN_CLICKED(IDC_DELETE_COPY_DATA, OnDeleteCopyData)
  46. ON_WM_ACTIVATE()
  47. //}}AFX_MSG_MAP
  48. ON_BN_CLICKED(IDC_PARSE_BUTTON, OnBnClickedParseButton)
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CCopyProperties message handlers
  52. BOOL CCopyProperties::OnInitDialog()
  53. {
  54. CDialog::OnInitDialog();
  55. m_ParseEdit.SetWindowText("\\r\\n");
  56. m_MainTable.Open("SELECT * FROM Main WHERE lID = %d", m_lCopyID);
  57. if(!m_MainTable.IsEOF())
  58. {
  59. CTime time(m_MainTable.m_lDate);
  60. m_eDate = time.Format("%m/%d/%Y %I:%M %p");
  61. m_eDisplayText = m_MainTable.m_strText;
  62. if(m_MainTable.m_lDontAutoDelete)
  63. {
  64. m_bNeverDelete = TRUE;
  65. m_bNeverAutoDelete = TRUE;
  66. }
  67. else
  68. {
  69. m_bNeverAutoDelete = FALSE;
  70. m_bNeverDelete = FALSE;
  71. }
  72. m_HotKey.SetHotKey(LOBYTE(m_MainTable.m_lShortCut), HIBYTE(m_MainTable.m_lShortCut));
  73. m_HotKey.SetRules(HKCOMB_A, 0);
  74. CString cs;
  75. cs.Format("SELECT * FROM Data WHERE lParentID = %d", m_MainTable.m_lID);
  76. m_DataTable.Open(AFX_DAO_USE_DEFAULT_TYPE, cs ,NULL);
  77. while(!m_DataTable.IsEOF())
  78. {
  79. cs.Format("%s, %d", m_DataTable.m_strClipBoardFormat, m_DataTable.m_ooData.m_dwDataLength);
  80. int nIndex = m_lCopyData.AddString(cs);
  81. m_lCopyData.SetItemData(nIndex, m_DataTable.m_lID);
  82. m_DataTable.MoveNext();
  83. }
  84. }
  85. UpdateData(FALSE);
  86. SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
  87. // if this is a result of a NamedCopy, focus on the hotkey
  88. if( theApp.m_bShowCopyProperties )
  89. m_HotKey.SetFocus();
  90. else
  91. m_DisplayTextEdit.SetFocus();
  92. return FALSE;
  93. }
  94. void CCopyProperties::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  95. {
  96. CDialog::OnActivate(nState, pWndOther, bMinimized);
  97. if (nState == WA_INACTIVE)
  98. {
  99. if(m_bHideOnKillFocus)
  100. {
  101. if(!m_bHandleKillFocus)
  102. {
  103. EndDialog(-1);
  104. m_bHandleKillFocus = false;
  105. }
  106. }
  107. }
  108. else if (nState == WA_ACTIVE)
  109. {
  110. SetFocus();
  111. ::SetForegroundWindow(m_hWnd);
  112. }
  113. }
  114. void CCopyProperties::OnOK()
  115. {
  116. UpdateData();
  117. bool bUpdate = false;
  118. long lHotKey = m_HotKey.GetHotKey();
  119. if(lHotKey != m_MainTable.m_lShortCut)
  120. {
  121. if(lHotKey > 0)
  122. {
  123. CMainTable recset;
  124. recset.Open("SELECT * FROM Main WHERE lShortCut = %d", lHotKey);
  125. if(!recset.IsEOF())
  126. {
  127. CString cs;
  128. cs.Format("The shortcut is currently assigned to \n\n%s\n\nAssign to new copy item?", recset.m_strText);
  129. if(MessageBox(cs, "Ditto", MB_YESNO) == IDNO)
  130. return;
  131. recset.Edit();
  132. recset.m_lShortCut = 0;
  133. recset.Update();
  134. }
  135. }
  136. if(!bUpdate)
  137. m_MainTable.Edit();
  138. m_MainTable.m_lShortCut = lHotKey;
  139. bUpdate = true;
  140. }
  141. if(m_eDisplayText != m_MainTable.m_strText)
  142. {
  143. if(!bUpdate)
  144. m_MainTable.Edit();
  145. m_bChangedText = true;
  146. m_MainTable.m_strText = m_eDisplayText;
  147. bUpdate = true;
  148. }
  149. if(m_bNeverAutoDelete != m_bNeverDelete)
  150. {
  151. if(!bUpdate)
  152. m_MainTable.Edit();
  153. if(m_bNeverAutoDelete)
  154. m_MainTable.m_lDontAutoDelete = (long)CTime::GetCurrentTime().GetTime();
  155. else
  156. m_MainTable.m_lDontAutoDelete = 0;
  157. bUpdate = true;
  158. }
  159. if(m_bDeletedData)
  160. {
  161. m_DeletedData.SortAscending();
  162. long lNewTotalSize = 0;
  163. //Go through the data table and find the deleted items
  164. m_DataTable.MoveFirst();
  165. while(!m_DataTable.IsEOF())
  166. {
  167. if(m_DeletedData.Find(m_DataTable.m_lID))
  168. m_DataTable.Delete();
  169. else
  170. lNewTotalSize += m_DataTable.m_ooData.m_dwDataLength;
  171. m_DataTable.MoveNext();
  172. }
  173. if(lNewTotalSize > 0)
  174. {
  175. if(!bUpdate)
  176. m_MainTable.Edit();
  177. m_MainTable.m_lTotalCopySize = lNewTotalSize;
  178. bUpdate = true;
  179. }
  180. }
  181. if(bUpdate)
  182. m_MainTable.Update();
  183. m_bHandleKillFocus = true;
  184. CDialog::OnOK();
  185. }
  186. void CCopyProperties::OnDeleteCopyData()
  187. {
  188. int nCount = m_lCopyData.GetSelCount();
  189. if(nCount)
  190. {
  191. m_bDeletedData = true;
  192. //Get the selected indexes
  193. ARRAY items;
  194. items.SetSize(nCount);
  195. m_lCopyData.GetSelItems(nCount, items.GetData());
  196. items.SortDescending();
  197. //Get the selected itemdata
  198. for(int i = 0; i < nCount; i++)
  199. {
  200. m_DeletedData.Add(m_lCopyData.GetItemData(items[i]));
  201. m_lCopyData.DeleteString(items[i]);
  202. }
  203. }
  204. }
  205. void CCopyProperties::OnCancel()
  206. {
  207. m_bHandleKillFocus = true;
  208. CDialog::OnCancel();
  209. }
  210. void CCopyProperties::OnBnClickedParseButton()
  211. {
  212. //RECT rcScreen;
  213. // SystemParametersInfo (SPI_GETWORKAREA, 0, &rcScreen, 0);
  214. CPoint pos(0,0);
  215. ClientToScreen(&pos);
  216. CPopup status( pos );
  217. status.Show("Parsing...");
  218. CString delims;
  219. m_ParseEdit.GetWindowText(delims);
  220. delims = RemoveEscapes( delims );
  221. // validate delimiters
  222. int nDelims = delims.GetLength();
  223. if( nDelims <= 0 )
  224. {
  225. ::MessageBox( m_hWnd, "Token Delimiters are not valid.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  226. return;
  227. }
  228. // load the text
  229. HGLOBAL hGlobal = CClip::LoadFormat( m_lCopyID, CF_TEXT );
  230. if( !hGlobal )
  231. {
  232. ::MessageBox( m_hWnd, "No CF_TEXT Format Data could be found.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  233. return;
  234. }
  235. // copy the text from the global into a string
  236. int nDataSize = ::GlobalSize( hGlobal );
  237. char* pData = (char*) ::GlobalLock( hGlobal );
  238. char* pDataEnd = pData + nDataSize;
  239. char* p = pData;
  240. ASSERT( pData != NULL && nDataSize > 0 );
  241. // Find the terminating NULL
  242. while( *p != '\0' && p < pDataEnd ) { p++; }
  243. // there was no NULL in the string!
  244. if( p >= pDataEnd )
  245. {
  246. ASSERT(0);
  247. return;
  248. }
  249. // copy the data into a string
  250. CString text = pData;
  251. // free the global
  252. ::GlobalUnlock( hGlobal );
  253. ::GlobalFree(hGlobal);
  254. // tokenize the text
  255. CString token;
  256. CStringArray tokens;
  257. CTokenizer tokenizer(text,delims);
  258. while( tokenizer.Next( token ) )
  259. {
  260. tokens.Add( token );
  261. }
  262. // save the tokens to the db
  263. CClip clip;
  264. int len;
  265. long lDate = (long) CTime::GetCurrentTime().GetTime();
  266. int count = tokens.GetSize();
  267. for( int i = 0; i < count; i++ )
  268. {
  269. status.Show( StrF("Saving Token %d out of %d", i+1, count) );
  270. len = tokens[i].GetLength();
  271. // ignore 0 length tokens
  272. if( len <= 0 )
  273. continue;
  274. clip.AddFormat( CF_TEXT, (void*) (LPCTSTR) tokens[i], len+1 );
  275. clip.m_Time = lDate;
  276. clip.AddToDB();
  277. clip.Clear();
  278. lDate++; // make sure they are sequential
  279. }
  280. if( count <= 0 )
  281. ::MessageBox( m_hWnd, "No new tokens found by parsing", "Parse Failed", MB_OK|MB_ICONINFORMATION );
  282. else
  283. {
  284. ::MessageBox( m_hWnd, StrF("Successfully parsed %d tokens.", tokens.GetSize()), "Parse Completed", MB_OK|MB_ICONINFORMATION );
  285. theApp.RefreshView();
  286. }
  287. }