MainFrm.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "afxole.h"
  7. #include "Misc.h"
  8. #include "CopyProperties.h"
  9. #include "InternetUpdate.h"
  10. #include ".\mainfrm.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. #define WM_ICON_NOTIFY WM_APP+10
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMainFrame
  19. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  20. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  21. //{{AFX_MSG_MAP(CMainFrame)
  22. ON_WM_CREATE()
  23. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  24. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  25. // ON_WM_CHANGECBCHAIN()
  26. // ON_WM_DRAWCLIPBOARD()
  27. ON_WM_TIMER()
  28. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  29. ON_COMMAND(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnFirstReconnecttoclipboardchain)
  30. ON_UPDATE_COMMAND_UI(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnUpdateFirstReconnecttoclipboardchain)
  31. //}}AFX_MSG_MAP
  32. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  33. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  34. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  35. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  36. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  37. ON_WM_CLOSE()
  38. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  39. ON_MESSAGE(WM_LOAD_FORMATS, OnLoadFormats)
  40. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  41. END_MESSAGE_MAP()
  42. static UINT indicators[] =
  43. {
  44. ID_SEPARATOR, // status line indicator
  45. ID_INDICATOR_CAPS,
  46. ID_INDICATOR_NUM,
  47. ID_INDICATOR_SCRL,
  48. };
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CMainFrame construction/destruction
  51. CMainFrame::CMainFrame()
  52. {
  53. }
  54. CMainFrame::~CMainFrame()
  55. {
  56. CGetSetOptions::SetMainHWND(0);
  57. }
  58. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  59. {
  60. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  61. return -1;
  62. SetWindowText(MAIN_WND_TITLE);
  63. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  64. m_TrayIcon.Create(
  65. NULL, // Let icon deal with its own messages
  66. WM_ICON_NOTIFY, // Icon notify message to use
  67. _T("Ditto"), // tooltip
  68. hIcon,
  69. IDR_MENU, // ID of tray icon
  70. FALSE,
  71. _T(""), // balloon tip
  72. _T(""), // balloon title
  73. NULL, // balloon icon
  74. 20 );
  75. m_TrayIcon.MinimiseToTray(this);
  76. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  77. //Only if in release
  78. #ifndef _DEBUG
  79. {
  80. //If not showing the icon show it for 40 seconds so they can get to the option
  81. //in case they can't remember the hot keys or something like that
  82. if(!(CGetSetOptions::GetShowIconInSysTray()))
  83. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  84. }
  85. #endif
  86. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  87. theApp.Delayed_RemoveOldEntries(ONE_MINUTE*2);
  88. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  89. // QuickPaste.Create( this );
  90. theApp.AfterMainCreate();
  91. return 0;
  92. }
  93. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  94. {
  95. if( !CFrameWnd::PreCreateWindow(cs) )
  96. return FALSE;
  97. WNDCLASS wc;
  98. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  99. wc.lpfnWndProc = AfxWndProc;
  100. wc.cbClsExtra = 0;
  101. wc.cbWndExtra = 0;
  102. wc.hInstance = AfxGetInstanceHandle();
  103. wc.hIcon = NULL;
  104. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  105. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  106. wc.lpszMenuName = NULL;
  107. wc.lpszClassName = "Ditto";
  108. // Create the QPaste window class
  109. if (!AfxRegisterClass(&wc))
  110. return FALSE;
  111. cs.lpszClass = wc.lpszClassName;
  112. return TRUE;
  113. }
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CMainFrame diagnostics
  116. #ifdef _DEBUG
  117. void CMainFrame::AssertValid() const
  118. {
  119. CFrameWnd::AssertValid();
  120. }
  121. void CMainFrame::Dump(CDumpContext& dc) const
  122. {
  123. CFrameWnd::Dump(dc);
  124. }
  125. #endif //_DEBUG
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CMainFrame message handlers
  128. void CMainFrame::OnFirstOption()
  129. {
  130. DoOptions(this);
  131. }
  132. void CMainFrame::OnFirstExit()
  133. {
  134. this->SendMessage(WM_CLOSE, 0, 0);
  135. }
  136. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  137. {
  138. if(wParam == theApp.m_pDittoHotKey->m_Atom)
  139. {
  140. theApp.TargetActiveWindow();
  141. QuickPaste.ShowQPasteWnd(this);
  142. }
  143. else if(wParam == theApp.m_pCopyHotKey->m_Atom)
  144. {
  145. theApp.TargetActiveWindow();
  146. theApp.m_bShowCopyProperties = true;
  147. //Simulate the Copy
  148. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  149. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  150. Sleep(100);
  151. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  152. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  153. }
  154. else if(wParam == theApp.m_pPosOne->m_Atom)
  155. {
  156. DoFirstTenPositionsPaste(1);
  157. }
  158. else if(wParam == theApp.m_pPosTwo->m_Atom)
  159. {
  160. DoFirstTenPositionsPaste(2);
  161. }
  162. else if(wParam == theApp.m_pPosThree->m_Atom)
  163. {
  164. DoFirstTenPositionsPaste(3);
  165. }
  166. else if(wParam == theApp.m_pPosFour->m_Atom)
  167. {
  168. DoFirstTenPositionsPaste(4);
  169. }
  170. else if(wParam == theApp.m_pPosFive->m_Atom)
  171. {
  172. DoFirstTenPositionsPaste(5);
  173. }
  174. else if(wParam == theApp.m_pPosSix->m_Atom)
  175. {
  176. DoFirstTenPositionsPaste(6);
  177. }
  178. else if(wParam == theApp.m_pPosSeven->m_Atom)
  179. {
  180. DoFirstTenPositionsPaste(7);
  181. }
  182. else if(wParam == theApp.m_pPosEight->m_Atom)
  183. {
  184. DoFirstTenPositionsPaste(8);
  185. }
  186. else if(wParam == theApp.m_pPosNine->m_Atom)
  187. {
  188. DoFirstTenPositionsPaste(9);
  189. }
  190. else if(wParam == theApp.m_pPosTen->m_Atom)
  191. {
  192. DoFirstTenPositionsPaste(10);
  193. }
  194. return TRUE;
  195. }
  196. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  197. {
  198. try
  199. {
  200. CMainTable Recset;
  201. Recset.m_strSort = "bIsGroup DESC, lDate DESC";
  202. Recset.m_strFilter = "((bIsGroup = TRUE AND lParentID = 0) OR bIsGroup = FALSE)";
  203. Recset.Open("");
  204. if(!Recset.IsEOF())
  205. {
  206. Recset.MoveLast();
  207. if(Recset.GetRecordCount() > nPos)
  208. {
  209. Recset.SetAbsolutePosition(nPos-1);
  210. if(Recset.m_bIsGroup == FALSE)
  211. {
  212. //Don't move these to the top
  213. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  214. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  215. CProcessPaste paste;
  216. paste.GetClipIDs().Add(Recset.m_lID);
  217. paste.m_bActivateTarget = false;
  218. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  219. paste.DoPaste();
  220. theApp.OnPasteCompleted();
  221. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  222. }
  223. }
  224. }
  225. }
  226. catch(CDaoException* e)
  227. {
  228. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  229. ASSERT(0);
  230. e->Delete();
  231. }
  232. }
  233. void CMainFrame::OnTimer(UINT nIDEvent)
  234. {
  235. switch(nIDEvent)
  236. {
  237. case HIDE_ICON_TIMER:
  238. {
  239. m_TrayIcon.HideIcon();
  240. KillTimer(nIDEvent);
  241. break;
  242. }
  243. case KILL_DB_TIMER:
  244. {
  245. if(QuickPaste.CloseQPasteWnd())
  246. {
  247. theApp.CloseDB();
  248. AfxDaoTerm();
  249. KillTimer(KILL_DB_TIMER);
  250. }
  251. break;
  252. }
  253. case REMOVE_OLD_ENTRIES_TIMER:
  254. {
  255. theApp.m_bRemoveOldEntriesPending = false;
  256. RemoveOldEntries();
  257. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  258. break;
  259. }
  260. case CHECK_FOR_UPDATE:
  261. {
  262. CInternetUpdate Update;
  263. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  264. {
  265. SendMessage(WM_CLOSE, 0, 0);
  266. }
  267. KillTimer(CHECK_FOR_UPDATE);
  268. break;
  269. }
  270. case CLOSE_APP:
  271. {
  272. if(theApp.m_bShowingOptions == false)
  273. {
  274. PostMessage(WM_CLOSE, 0, 0);
  275. KillTimer(CLOSE_APP);
  276. }
  277. break;
  278. }
  279. case HIDE_ERROR_POPUP:
  280. {
  281. KillTimer(HIDE_ERROR_POPUP);
  282. if(theApp.m_pcpSendRecieveError)
  283. {
  284. delete theApp.m_pcpSendRecieveError;
  285. theApp.m_pcpSendRecieveError = NULL;
  286. }
  287. break;
  288. }
  289. }
  290. CFrameWnd::OnTimer(nIDEvent);
  291. }
  292. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  293. {
  294. if(lParam)
  295. {
  296. if(!m_TrayIcon.Visible())
  297. {
  298. KillTimer(HIDE_ICON_TIMER);
  299. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  300. }
  301. }
  302. if(wParam)
  303. m_TrayIcon.ShowIcon();
  304. else
  305. m_TrayIcon.HideIcon();
  306. return TRUE;
  307. }
  308. void CMainFrame::OnFirstShowquickpaste()
  309. {
  310. QuickPaste.ShowQPasteWnd(this, TRUE);
  311. }
  312. void CMainFrame::OnFirstReconnecttoclipboardchain()
  313. {
  314. ::PostMessage( theApp.GetClipboardViewer(), WM_CV_RECONNECT, 0, 0 );
  315. }
  316. void CMainFrame::OnUpdateFirstReconnecttoclipboardchain(CCmdUI* pCmdUI)
  317. {
  318. if( theApp.IsClipboardViewerConnected() )
  319. pCmdUI->m_pMenu->DeleteMenu(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, MF_BYCOMMAND);
  320. }
  321. BOOL CMainFrame::ResetKillDBTimer()
  322. {
  323. KillTimer(KILL_DB_TIMER);
  324. SetTimer(KILL_DB_TIMER, ONE_MINUTE*2, NULL);
  325. return TRUE;
  326. }
  327. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  328. {
  329. long lID = (long)wParam;
  330. if(lID > 0)
  331. {
  332. bool bOldState = theApp.EnableCbCopy(false);
  333. theApp.m_bShowingOptions = true;
  334. CCopyProperties props(lID, this);
  335. props.SetHideOnKillFocus(true);
  336. props.DoModal();
  337. theApp.m_bShowingOptions = false;
  338. theApp.EnableCbCopy( bOldState );
  339. }
  340. return TRUE;
  341. }
  342. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  343. {
  344. SetTimer(CLOSE_APP, 100, NULL);
  345. return TRUE;
  346. }
  347. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  348. {
  349. // if the delay is undesirable, this could be altered to save one at a time,
  350. // allowing the processing of other messages between saving clips.
  351. theApp.SaveCopyClips();
  352. return TRUE;
  353. }
  354. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  355. {
  356. // target before mouse messages change the focus
  357. if( theApp.m_bShowingQuickPaste &&
  358. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  359. { theApp.TargetActiveWindow(); }
  360. return CFrameWnd::PreTranslateMessage(pMsg);
  361. }
  362. void CMainFrame::OnClose()
  363. {
  364. theApp.BeforeMainClose();
  365. CFrameWnd::OnClose();
  366. }
  367. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  368. {
  369. CClip *pClip = (CClip*)wParam;
  370. BOOL bSetToClipBoard = (BOOL)lParam;
  371. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  372. if(bSetToClipBoard)
  373. {
  374. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  375. CClip NewClip;
  376. NewClip = *pClip;
  377. LogSendRecieveInfo("---------After =");
  378. CProcessPaste paste;
  379. //Don't send the paste just load it into memory
  380. paste.m_bSendPaste = false;
  381. paste.m_pOle->LoadFormats(&NewClip.m_Formats);
  382. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  383. LogSendRecieveInfo("---------After LoadFormats");
  384. paste.DoPaste();
  385. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  386. }
  387. pClip->MakeLatestTime();
  388. pClip->AddToDB(true);
  389. LogSendRecieveInfo("---------After AddToDB");
  390. theApp.RefreshView();
  391. theApp.m_FocusID = pClip->m_ID;
  392. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  393. return TRUE;
  394. }
  395. LRESULT CMainFrame::OnLoadFormats(WPARAM wParam, LPARAM lParam)
  396. {
  397. int nID = (int)wParam;
  398. CClipFormats *pFormats = (CClipFormats *)lParam;
  399. CClip::LoadFormats(nID, *pFormats);
  400. return TRUE;
  401. }
  402. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  403. {
  404. KillTimer(HIDE_ERROR_POPUP);
  405. if(theApp.m_pcpSendRecieveError)
  406. {
  407. CString csOldText = theApp.m_pcpSendRecieveError->m_csToolTipText;
  408. CString csNewText = (char*)wParam;
  409. CString csCombinedText = csOldText + "\n" + csNewText;
  410. theApp.m_pcpSendRecieveError->Show(csCombinedText, CPoint(20, 20), true);
  411. }
  412. else
  413. {
  414. theApp.m_pcpSendRecieveError = new CPopup(20, 20, ::GetForegroundWindow());
  415. CString csNewText = (char*)wParam;
  416. theApp.m_pcpSendRecieveError->Show(csNewText);
  417. }
  418. SetTimer(HIDE_ERROR_POPUP, 6000, NULL);
  419. return TRUE;
  420. }