CopyProperties.cpp 7.7 KB

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