CopyProperties.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 lDataID = %d", m_MainTable.m_lDataID);
  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(bUpdate)
  160. m_MainTable.Update();
  161. if(m_bDeletedData)
  162. {
  163. DeleteFormats( m_MainTable.m_lDataID, m_DeletedData );
  164. }
  165. m_bHandleKillFocus = true;
  166. CDialog::OnOK();
  167. }
  168. void CCopyProperties::OnDeleteCopyData()
  169. {
  170. int nCount = m_lCopyData.GetSelCount();
  171. if(nCount)
  172. {
  173. m_bDeletedData = true;
  174. //Get the selected indexes
  175. ARRAY items;
  176. items.SetSize(nCount);
  177. m_lCopyData.GetSelItems(nCount, items.GetData());
  178. items.SortDescending();
  179. //Get the selected itemdata
  180. for(int i = 0; i < nCount; i++)
  181. {
  182. m_DeletedData.Add(m_lCopyData.GetItemData(items[i]));
  183. m_lCopyData.DeleteString(items[i]);
  184. }
  185. }
  186. }
  187. void CCopyProperties::OnCancel()
  188. {
  189. m_bHandleKillFocus = true;
  190. CDialog::OnCancel();
  191. }
  192. void CCopyProperties::OnBnClickedParseButton()
  193. {
  194. CPopup status(0,0,m_hWnd);
  195. status.Show("Parsing...");
  196. CString delims;
  197. m_ParseEdit.GetWindowText(delims);
  198. delims = RemoveEscapes( delims );
  199. // validate delimiters
  200. int nDelims = delims.GetLength();
  201. if( nDelims <= 0 )
  202. {
  203. ::MessageBox( m_hWnd, "Token Delimiters are not valid.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  204. return;
  205. }
  206. // load the text
  207. HGLOBAL hGlobal = CClip::LoadFormat( m_lCopyID, CF_TEXT );
  208. if( !hGlobal )
  209. {
  210. ::MessageBox( m_hWnd, "No CF_TEXT Format Data could be found.", "Parse Failure", MB_OK|MB_ICONINFORMATION );
  211. return;
  212. }
  213. // copy the text from the global into a string
  214. int nDataSize = ::GlobalSize( hGlobal );
  215. char* pData = (char*) ::GlobalLock( hGlobal );
  216. char* pDataEnd = pData + nDataSize;
  217. char* p = pData;
  218. ASSERT( pData != NULL && nDataSize > 0 );
  219. // Find the terminating NULL
  220. while( *p != '\0' && p < pDataEnd ) { p++; }
  221. // there was no NULL in the string!
  222. if( p >= pDataEnd )
  223. {
  224. ASSERT(0);
  225. return;
  226. }
  227. // copy the data into a string
  228. CString text = pData;
  229. // free the global
  230. ::GlobalUnlock( hGlobal );
  231. ::GlobalFree(hGlobal);
  232. // tokenize the text
  233. CString token;
  234. CStringArray tokens;
  235. CTokenizer tokenizer(text,delims);
  236. while( tokenizer.Next( token ) )
  237. {
  238. tokens.Add( token );
  239. }
  240. // save the tokens to the db
  241. CClip clip;
  242. int len;
  243. long lDate = (long) CTime::GetCurrentTime().GetTime();
  244. int count = tokens.GetSize();
  245. for( int i = 0; i < count; i++ )
  246. {
  247. status.Show( StrF("Saving Token %d out of %d", i+1, count) );
  248. len = tokens[i].GetLength();
  249. // ignore 0 length tokens
  250. if( len <= 0 )
  251. continue;
  252. clip.AddFormat( CF_TEXT, (void*) (LPCTSTR) tokens[i], len+1 );
  253. clip.m_Time = lDate;
  254. clip.AddToDB( false ); // false = don't check for duplicates
  255. clip.Clear();
  256. lDate++; // make sure they are sequential
  257. }
  258. if( count <= 0 )
  259. ::MessageBox( m_hWnd, "No new tokens found by parsing", "Parse Failed", MB_OK|MB_ICONINFORMATION );
  260. else
  261. {
  262. ::MessageBox( m_hWnd, StrF("Successfully parsed %d tokens.", tokens.GetSize()), "Parse Completed", MB_OK|MB_ICONINFORMATION );
  263. theApp.RefreshView();
  264. }
  265. }