CopyProperties.cpp 8.2 KB

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