CopyProperties.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_lGroupChangedTo = nParentID;
  178. bUpdate = true;
  179. }
  180. }
  181. if(bUpdate)
  182. m_MainTable.Update();
  183. if(m_bDeletedData)
  184. {
  185. DeleteFormats( m_MainTable.m_lDataID, m_DeletedData );
  186. }
  187. m_bHandleKillFocus = true;
  188. CDialog::OnOK();
  189. }
  190. void CCopyProperties::OnDeleteCopyData()
  191. {
  192. int nCount = m_lCopyData.GetSelCount();
  193. if(nCount)
  194. {
  195. m_bDeletedData = true;
  196. //Get the selected indexes
  197. ARRAY items;
  198. items.SetSize(nCount);
  199. m_lCopyData.GetSelItems(nCount, items.GetData());
  200. items.SortDescending();
  201. //Get the selected itemdata
  202. for(int i = 0; i < nCount; i++)
  203. {
  204. m_DeletedData.Add(m_lCopyData.GetItemData(items[i]));
  205. m_lCopyData.DeleteString(items[i]);
  206. }
  207. }
  208. }
  209. void CCopyProperties::OnCancel()
  210. {
  211. m_bHandleKillFocus = true;
  212. CDialog::OnCancel();
  213. }
  214. void CCopyProperties::OnBnClickedParseButton()
  215. {
  216. CPopup status(0,0,m_hWnd);
  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( false ); // false = don't check for duplicates
  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. }