MainFrm.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. #include "HyperLink.h"
  12. #include "tinyxml\tinyxml.h"
  13. #include "Path.h"
  14. #include "DittoCopyBuffer.h"
  15. #include "HotKeys.h"
  16. #include "GlobalClips.h"
  17. #include "OptionsSheet.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. #define WM_ICON_NOTIFY WM_APP+10
  24. #define MYWM_NOTIFYICON (WM_USER+1)
  25. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  26. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  27. //{{AFX_MSG_MAP(CMainFrame)
  28. ON_WM_CREATE()
  29. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  30. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  31. ON_WM_TIMER()
  32. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  33. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  34. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  35. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  36. //}}AFX_MSG_MAP
  37. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  38. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  39. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  40. ON_WM_CLOSE()
  41. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  42. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  43. ON_MESSAGE(WM_CUSTOMIZE_TRAY_MENU, OnCustomizeTrayMenu)
  44. ON_COMMAND(ID_FIRST_IMPORT, OnFirstImport)
  45. ON_MESSAGE(WM_EDIT_WND_CLOSING, OnEditWndClose)
  46. ON_WM_DESTROY()
  47. ON_COMMAND(ID_FIRST_NEWCLIP, OnFirstNewclip)
  48. ON_MESSAGE(WM_SET_CONNECTED, OnSetConnected)
  49. ON_MESSAGE(WM_LOAD_ClIP_ON_CLIPBOARD, OnLoadClipOnClipboard)
  50. ON_MESSAGE(WM_TRAY_MENU_MOUSE_MOVE, OnSystemTrayMouseMove)
  51. ON_COMMAND(ID_FIRST_GLOBALHOTKEYS, &CMainFrame::OnFirstGlobalhotkeys)
  52. ON_MESSAGE(WM_GLOBAL_CLIPS_CLOSED, OnGlobalClipsClosed)
  53. ON_MESSAGE(WM_OPTIONS_CLOSED, OnOptionsClosed)
  54. ON_MESSAGE(WM_SHOW_OPTIONS, OnShowOptions)
  55. END_MESSAGE_MAP()
  56. static UINT indicators[] =
  57. {
  58. ID_SEPARATOR, // status line indicator
  59. ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
  60. };
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CMainFrame construction/destruction
  63. CMainFrame::CMainFrame()
  64. {
  65. m_pEditFrameWnd = NULL;
  66. m_keyStateModifiers = 0;
  67. m_startKeyStateTime = 0;
  68. m_bMovedSelectionMoveKeyState = false;
  69. m_keyModifiersTimerCount = 0;
  70. m_pGlobalClips = NULL;
  71. m_pOptions = NULL;
  72. }
  73. CMainFrame::~CMainFrame()
  74. {
  75. CGetSetOptions::SetMainHWND(0);
  76. }
  77. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  78. {
  79. if(CFrameWnd::OnCreate(lpCreateStruct) == - 1)
  80. {
  81. return - 1;
  82. }
  83. //Center the main window so message boxes are in the center
  84. CRect rcScreen;
  85. GetMonitorRect(0, &rcScreen);
  86. CPoint cpCenter = rcScreen.CenterPoint();
  87. MoveWindow(cpCenter.x, cpCenter.x, - 2, - 2);
  88. //Then set the main window to transparent so it's never shown
  89. //if it is shown then only the task tray icon
  90. m_Transparency.SetTransparent(m_hWnd, 0, true);
  91. SetWindowText(_T(""));
  92. Log(_T("Setting polling timer to track focus"));
  93. SetTimer(ACTIVE_WINDOW_TIMER, g_Opt.FocusWndTimerTimeout(), 0);
  94. SetWindowText(_T("Ditto"));
  95. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  96. m_TrayIcon.Create(NULL, WM_ICON_NOTIFY, _T("Ditto"), hIcon, IDR_MENU, FALSE, _T(""), _T(""), NULL, 20);
  97. m_TrayIcon.SetSingleClickSelect(TRUE);
  98. m_TrayIcon.MinimiseToTray(this);
  99. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  100. //Only if in release
  101. #ifndef _DEBUG
  102. {
  103. //If not showing the icon show it for 40 seconds so they can get to the option
  104. //in case they can't remember the hot keys or something like that
  105. if(!(CGetSetOptions::GetShowIconInSysTray()))
  106. {
  107. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  108. }
  109. }
  110. #endif
  111. SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR*24, 0);
  112. SetTimer(REMOVE_OLD_REMOTE_COPIES, ONE_DAY, 0);
  113. SetTimer(REMOVE_OLD_ENTRIES_TIMER, ONE_MINUTE*15, 0);
  114. //found on some computers GetTickCount gettickcount returns a smaller value than other, can't explain
  115. //check here to see if we need to make an adjustment
  116. IdleSeconds();
  117. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  118. theApp.AfterMainCreate();
  119. m_thread.Start(this);
  120. return 0;
  121. }
  122. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
  123. {
  124. if(cs.hMenu != NULL)
  125. {
  126. ::DestroyMenu(cs.hMenu); // delete menu if loaded
  127. cs.hMenu = NULL; // no menu for this window
  128. }
  129. if(!CFrameWnd::PreCreateWindow(cs))
  130. {
  131. return FALSE;
  132. }
  133. WNDCLASS wc;
  134. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  135. wc.lpfnWndProc = AfxWndProc;
  136. wc.cbClsExtra = 0;
  137. wc.cbWndExtra = 0;
  138. wc.hInstance = AfxGetInstanceHandle();
  139. wc.hIcon = NULL;
  140. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  141. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  142. wc.lpszMenuName = NULL;
  143. wc.lpszClassName = _T("Ditto");
  144. // Create the QPaste window class
  145. if(!AfxRegisterClass(&wc))
  146. {
  147. return FALSE;
  148. }
  149. cs.lpszClass = wc.lpszClassName;
  150. return TRUE;
  151. }
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CMainFrame diagnostics
  154. #ifdef _DEBUG
  155. void CMainFrame::AssertValid()const
  156. {
  157. CFrameWnd::AssertValid();
  158. }
  159. void CMainFrame::Dump(CDumpContext &dc)const
  160. {
  161. CFrameWnd::Dump(dc);
  162. }
  163. #endif //_DEBUG
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CMainFrame message handlers
  166. void CMainFrame::OnFirstExit()
  167. {
  168. this->SendMessage(WM_CLOSE, 0, 0);
  169. }
  170. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  171. {
  172. if(theApp.m_pDittoHotKey && wParam == theApp.m_pDittoHotKey->m_Atom)
  173. {
  174. //If they still have the shift/ctrl keys down
  175. if(m_keyStateModifiers != 0 && m_quickPaste.IsWindowVisibleEx())
  176. {
  177. Log(_T("On Show Ditto HotKey, key state modifiers are still down, moving selection"));
  178. if(m_bMovedSelectionMoveKeyState == false)
  179. {
  180. Log(_T("Setting flag m_bMovedSelectionMoveKeyState to true, will paste when modifer keys are up"));
  181. }
  182. m_quickPaste.MoveSelection(true);
  183. m_bMovedSelectionMoveKeyState = true;
  184. }
  185. else if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && m_quickPaste.IsWindowVisibleEx() && g_Opt.GetShowPersistent() == FALSE)
  186. {
  187. Log(_T("On Show Ditto HotKey, window is alread visible, hiding window"));
  188. m_quickPaste.HideQPasteWnd();
  189. }
  190. else
  191. {
  192. Log(_T("On Show Ditto HotKey, showing window"));
  193. m_keyModifiersTimerCount = 0;
  194. m_bMovedSelectionMoveKeyState = false;
  195. m_startKeyStateTime = GetTickCount();
  196. m_keyStateModifiers = CAccels::GetKeyStateModifiers();
  197. SetTimer(KEY_STATE_MODIFIERS, 50, NULL);
  198. //Before we show our window find the current focused window for paste into
  199. theApp.m_activeWnd.TrackActiveWnd(true);
  200. m_quickPaste.ShowQPasteWnd(this, false, true, FALSE);
  201. }
  202. KillTimer(CLOSE_WINDOW_TIMER);
  203. SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR *24, 0);
  204. }
  205. else if(theApp.m_pPosOne && wParam == theApp.m_pPosOne->m_Atom)
  206. {
  207. Log(_T("Pos 1 hot key"));
  208. DoFirstTenPositionsPaste(0);
  209. }
  210. else if(theApp.m_pPosTwo && wParam == theApp.m_pPosTwo->m_Atom)
  211. {
  212. Log(_T("Pos 2 hot key"));
  213. DoFirstTenPositionsPaste(1);
  214. }
  215. else if(theApp.m_pPosThree && wParam == theApp.m_pPosThree->m_Atom)
  216. {
  217. Log(_T("Pos 3 hot key"));
  218. DoFirstTenPositionsPaste(2);
  219. }
  220. else if(theApp.m_pPosFour && wParam == theApp.m_pPosFour->m_Atom)
  221. {
  222. Log(_T("Pos 4 hot key"));
  223. DoFirstTenPositionsPaste(3);
  224. }
  225. else if(theApp.m_pPosFive && wParam == theApp.m_pPosFive->m_Atom)
  226. {
  227. Log(_T("Pos 5 hot key"));
  228. DoFirstTenPositionsPaste(4);
  229. }
  230. else if(theApp.m_pPosSix && wParam == theApp.m_pPosSix->m_Atom)
  231. {
  232. Log(_T("Pos 6 hot key"));
  233. DoFirstTenPositionsPaste(5);
  234. }
  235. else if(theApp.m_pPosSeven && wParam == theApp.m_pPosSeven->m_Atom)
  236. {
  237. Log(_T("Pos 7 hot key"));
  238. DoFirstTenPositionsPaste(6);
  239. }
  240. else if(theApp.m_pPosEight && wParam == theApp.m_pPosEight->m_Atom)
  241. {
  242. Log(_T("Pos 8 hot key"));
  243. DoFirstTenPositionsPaste(7);
  244. }
  245. else if(theApp.m_pPosNine && wParam == theApp.m_pPosNine->m_Atom)
  246. {
  247. Log(_T("Pos 9 hot key"));
  248. DoFirstTenPositionsPaste(8);
  249. }
  250. else if(theApp.m_pPosTen && wParam == theApp.m_pPosTen->m_Atom)
  251. {
  252. Log(_T("Pos 10 hot key"));
  253. DoFirstTenPositionsPaste(9);
  254. }
  255. else if(theApp.m_pCopyBuffer1 && wParam == theApp.m_pCopyBuffer1->m_Atom)
  256. {
  257. Log(_T("Copy buffer 1 hot key"));
  258. theApp.m_CopyBuffer.StartCopy(0);
  259. }
  260. else if(theApp.m_pPasteBuffer1 && wParam == theApp.m_pPasteBuffer1->m_Atom)
  261. {
  262. Log(_T("Paste buffer 1 hot key"));
  263. theApp.m_CopyBuffer.PastCopyBuffer(0);
  264. }
  265. else if(theApp.m_pCutBuffer1 && wParam == theApp.m_pCutBuffer1->m_Atom)
  266. {
  267. Log(_T("Cut buffer 1 hot key"));
  268. theApp.m_CopyBuffer.StartCopy(0, true);
  269. }
  270. else if(theApp.m_pCopyBuffer2 && wParam == theApp.m_pCopyBuffer2->m_Atom)
  271. {
  272. Log(_T("Copy buffer 2 hot key"));
  273. theApp.m_CopyBuffer.StartCopy(1);
  274. }
  275. else if(theApp.m_pPasteBuffer2 && wParam == theApp.m_pPasteBuffer2->m_Atom)
  276. {
  277. Log(_T("Paste buffer 2 hot key"));
  278. theApp.m_CopyBuffer.PastCopyBuffer(1);
  279. }
  280. else if(theApp.m_pCutBuffer2 && wParam == theApp.m_pCutBuffer2->m_Atom)
  281. {
  282. Log(_T("Cut buffer 2 hot key"));
  283. theApp.m_CopyBuffer.StartCopy(1, true);
  284. }
  285. else if(theApp.m_pCopyBuffer3 && wParam == theApp.m_pCopyBuffer3->m_Atom)
  286. {
  287. Log(_T("Copy buffer 3 hot key"));
  288. theApp.m_CopyBuffer.StartCopy(2);
  289. }
  290. else if(theApp.m_pPasteBuffer3 && wParam == theApp.m_pPasteBuffer3->m_Atom)
  291. {
  292. Log(_T("Paste buffer 3 hot key"));
  293. theApp.m_CopyBuffer.PastCopyBuffer(2);
  294. }
  295. else if(theApp.m_pCutBuffer3 && wParam == theApp.m_pCutBuffer3->m_Atom)
  296. {
  297. Log(_T("Cut buffer 3 hot key"));
  298. theApp.m_CopyBuffer.StartCopy(2, true);
  299. }
  300. else if(theApp.m_pTextOnlyPaste && wParam == theApp.m_pTextOnlyPaste->m_Atom)
  301. {
  302. DoTextOnlyPaste();
  303. }
  304. else
  305. {
  306. for(int i = 0; i < g_HotKeys.GetCount(); i++)
  307. {
  308. if(g_HotKeys[i] != NULL &&
  309. g_HotKeys[i]->m_Atom == wParam &&
  310. g_HotKeys[i]->m_clipId > 0)
  311. {
  312. Log(StrF(_T("Pasting clip from global shortcut, clipId: %d"), g_HotKeys[i]->m_clipId));
  313. CProcessPaste paste;
  314. paste.GetClipIDs().Add(g_HotKeys[i]->m_clipId);
  315. paste.m_bActivateTarget = false;
  316. paste.m_bSendPaste = true;
  317. paste.DoPaste();
  318. theApp.OnPasteCompleted();
  319. break;
  320. }
  321. }
  322. }
  323. return TRUE;
  324. }
  325. void CMainFrame::DoTextOnlyPaste()
  326. {
  327. Log(_T("Text Only paste, saving clipboard to be restored later"));
  328. m_textOnlyPaste.Save();
  329. Log(_T("Text Only paste, Add cf_text or cf_unicodetext to clipboard"));
  330. m_textOnlyPaste.RestoreTextOnly();
  331. DWORD pasteDelay = g_Opt.GetTextOnlyPasteDelay();
  332. DWORD restoreDelay = g_Opt.GetTextOnlyRestoreDelay();
  333. Log(StrF(_T("Text Only paste, delaying %d ms before sending paste"), pasteDelay));
  334. Sleep(pasteDelay);
  335. Log(_T("Text Only paste, Sending paste"));
  336. theApp.m_activeWnd.SendPaste(false);
  337. Log(StrF(_T("Text Only paste, delaying %d ms before restoring clipboard to original state"), restoreDelay));
  338. SetTimer(TEXT_ONLY_PASTE, restoreDelay, 0);
  339. }
  340. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  341. {
  342. try
  343. {
  344. CString filter = "((Main.bIsGroup = 1 AND Main.lParentID = -1) OR (Main.bIsGroup = 0 AND Main.lParentID = -1))";
  345. if(g_Opt.m_bShowAllClipsInMainList)
  346. {
  347. filter = "((Main.bIsGroup = 1 AND Main.lParentID = -1) OR Main.bIsGroup = 0)";
  348. }
  349. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, bIsGroup FROM Main WHERE %s ORDER BY bIsGroup ASC, clipOrder DESC LIMIT 1 OFFSET %d"), filter, nPos);
  350. if(q.eof() == false)
  351. {
  352. if(q.getIntField(_T("bIsGroup")) == FALSE)
  353. {
  354. //Don't move these to the top
  355. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  356. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  357. CProcessPaste paste;
  358. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  359. paste.m_bActivateTarget = false;
  360. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  361. paste.DoPaste();
  362. theApp.OnPasteCompleted();
  363. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  364. }
  365. }
  366. }
  367. CATCH_SQLITE_EXCEPTION
  368. }
  369. void CMainFrame::DoDittoCopyBufferPaste(int nCopyBuffer)
  370. {
  371. try
  372. {
  373. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CopyBuffer = %d"), nCopyBuffer);
  374. if(q.eof() == false)
  375. {
  376. //Don't move these to the top
  377. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  378. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  379. CProcessPaste paste;
  380. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  381. paste.m_bActivateTarget = false;
  382. paste.DoPaste();
  383. theApp.OnPasteCompleted();
  384. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  385. }
  386. }
  387. CATCH_SQLITE_EXCEPTION
  388. }
  389. void CMainFrame::OnTimer(UINT_PTR nIDEvent)
  390. {
  391. switch(nIDEvent)
  392. {
  393. case HIDE_ICON_TIMER:
  394. {
  395. m_TrayIcon.HideIcon();
  396. KillTimer(nIDEvent);
  397. }
  398. break;
  399. case CLOSE_WINDOW_TIMER:
  400. {
  401. m_quickPaste.CloseQPasteWnd();
  402. }
  403. break;
  404. case REMOVE_OLD_ENTRIES_TIMER:
  405. {
  406. m_thread.FireDeleteEntries();
  407. }
  408. break;
  409. case REMOVE_OLD_REMOTE_COPIES:
  410. {
  411. m_thread.FireRemoveRemoteFiles();
  412. }
  413. break;
  414. case KEY_STATE_MODIFIERS:
  415. m_keyModifiersTimerCount++;
  416. if(m_keyStateModifiers != 0)
  417. {
  418. BYTE keyState = CAccels::GetKeyStateModifiers();
  419. //Have they release the key state modifiers yet(ctrl, shift, alt)
  420. if((m_keyStateModifiers &keyState) == 0)
  421. {
  422. KillTimer(KEY_STATE_MODIFIERS);
  423. long waitTime = (long)(GetTickCount() - m_startKeyStateTime);
  424. if(m_bMovedSelectionMoveKeyState || m_keyModifiersTimerCount > g_Opt.GetKeyStateWaitTimerCount())
  425. {
  426. Log(StrF(_T("Timer KEY_STATE_MODIFIERS timeout count hit(%d), count (%d), time (%d), Move Selection from Modifer (%d) sending paste"), g_Opt.GetKeyStateWaitTimerCount(), m_keyModifiersTimerCount, waitTime, m_bMovedSelectionMoveKeyState));
  427. m_quickPaste.OnKeyStateUp();
  428. }
  429. else
  430. {
  431. Log(StrF(_T("Timer KEY_STATE_MODIFIERS count NOT hit(%d), count (%d) time (%d)"), g_Opt.GetKeyStateWaitTimerCount(), m_keyModifiersTimerCount, waitTime));
  432. m_quickPaste.SetKeyModiferState(false);
  433. }
  434. m_keyStateModifiers = 0;
  435. m_keyModifiersTimerCount = 0;
  436. m_bMovedSelectionMoveKeyState = 0;
  437. }
  438. }
  439. else
  440. {
  441. KillTimer(KEY_STATE_MODIFIERS);
  442. }
  443. break;
  444. case ACTIVE_WINDOW_TIMER:
  445. {
  446. if(theApp.m_bShowingQuickPaste)
  447. {
  448. theApp.m_activeWnd.TrackActiveWnd(false);
  449. }
  450. }
  451. break;
  452. case TEXT_ONLY_PASTE:
  453. {
  454. KillTimer(TEXT_ONLY_PASTE);
  455. Log(_T("Text Only Paste, restoring original clipboard data"));
  456. m_textOnlyPaste.Restore();
  457. }
  458. break;
  459. }
  460. CFrameWnd::OnTimer(nIDEvent);
  461. }
  462. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  463. {
  464. if(lParam)
  465. {
  466. if(!m_TrayIcon.Visible())
  467. {
  468. KillTimer(HIDE_ICON_TIMER);
  469. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  470. }
  471. }
  472. if(wParam)
  473. {
  474. m_TrayIcon.ShowIcon();
  475. }
  476. else
  477. {
  478. m_TrayIcon.HideIcon();
  479. }
  480. return TRUE;
  481. }
  482. void CMainFrame::OnFirstShowquickpaste()
  483. {
  484. m_quickPaste.ShowQPasteWnd(this, false, false, FALSE);
  485. }
  486. void CMainFrame::OnFirstToggleConnectCV()
  487. {
  488. theApp.ToggleConnectCV();
  489. }
  490. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI *pCmdUI)
  491. {
  492. theApp.UpdateMenuConnectCV(pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV);
  493. }
  494. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  495. {
  496. Log(_T("Start of function OnClipboardCopied, adding clip to thread for processing"));
  497. CClip *pClip = (CClip*)wParam;
  498. if(pClip != NULL)
  499. {
  500. m_thread.AddClipToSave(pClip);
  501. }
  502. Log(_T("End of function OnClipboardCopied"));
  503. return TRUE;
  504. }
  505. BOOL CMainFrame::PreTranslateMessage(MSG *pMsg)
  506. {
  507. // target before mouse messages change the focus
  508. /*if(theApp.m_bShowingQuickPaste && WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST)
  509. {
  510. theApp.m_activeWnd.TrackActiveWnd(true);
  511. }*/
  512. return CFrameWnd::PreTranslateMessage(pMsg);
  513. }
  514. void CMainFrame::OnClose()
  515. {
  516. CloseAllOpenDialogs();
  517. if(m_pEditFrameWnd)
  518. {
  519. if(m_pEditFrameWnd->CloseAll() == false)
  520. {
  521. return ;
  522. }
  523. }
  524. Log(_T("OnClose - before stop MainFrm thread"));
  525. m_thread.Stop();
  526. Log(_T("OnClose - after stop MainFrm thread"));
  527. theApp.BeforeMainClose();
  528. CFrameWnd::OnClose();
  529. }
  530. bool CMainFrame::CloseAllOpenDialogs()
  531. {
  532. bool bRet = false;
  533. DWORD dwordProcessId;
  534. DWORD dwordChildWindowProcessId;
  535. GetWindowThreadProcessId(this->m_hWnd, &dwordProcessId);
  536. ASSERT(dwordProcessId);
  537. CWnd *pTempWnd = GetDesktopWindow()->GetWindow(GW_CHILD);
  538. while((pTempWnd = pTempWnd->GetWindow(GW_HWNDNEXT)) != NULL)
  539. {
  540. if(pTempWnd->GetSafeHwnd() == NULL)
  541. {
  542. break;
  543. }
  544. GetWindowThreadProcessId(pTempWnd->GetSafeHwnd(), &dwordChildWindowProcessId);
  545. if(dwordChildWindowProcessId == dwordProcessId)
  546. {
  547. TCHAR szTemp[100];
  548. GetClassName(pTempWnd->GetSafeHwnd(), szTemp, 100);
  549. // #32770 is class name for dialogs so don't process the message if it is a dialog
  550. if(STRCMP(szTemp, _T("#32770")) == 0)
  551. {
  552. pTempWnd->SendMessage(WM_CLOSE, 0, 0);
  553. bRet = true;
  554. }
  555. }
  556. }
  557. MSG msg;
  558. while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  559. {
  560. TranslateMessage(&msg);
  561. DispatchMessage(&msg);
  562. }
  563. return bRet;
  564. }
  565. LRESULT CMainFrame::OnSystemTrayMouseMove(WPARAM wParam, LPARAM lParam)
  566. {
  567. theApp.m_activeWnd.TrackActiveWnd(true);
  568. return 0;
  569. }
  570. LRESULT CMainFrame::OnLoadClipOnClipboard(WPARAM wParam, LPARAM lParam)
  571. {
  572. CClip *pClip = (CClip*)wParam;
  573. if(pClip == NULL)
  574. {
  575. LogSendRecieveInfo("---------ERROR OnLoadClipOnClipboard pClip == NULL");
  576. return FALSE;
  577. }
  578. if(pClip)
  579. {
  580. CProcessPaste paste;
  581. paste.m_bSendPaste = false;
  582. paste.m_bActivateTarget = false;
  583. LogSendRecieveInfo("---------OnLoadClipOnClipboard - Before PutFormats on clipboard");
  584. paste.m_pOle->PutFormatOnClipboard(&pClip->m_Formats);
  585. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  586. LogSendRecieveInfo("---------OnLoadClipOnClipboard - After PutFormats on clipboard");
  587. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - Setting clip id: %d on ole clipboard"), pClip->m_id));
  588. paste.GetClipIDs().Add(pClip->m_id);
  589. paste.DoPaste();
  590. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - After paste clip id: %d on ole clipboard"), pClip->m_id));
  591. }
  592. delete pClip;
  593. return TRUE;
  594. }
  595. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  596. {
  597. CClipList *pClipList = (CClipList*)wParam;
  598. if(pClipList == NULL)
  599. {
  600. LogSendRecieveInfo("---------OnAddToDatabaseFromSocket - ERROR pClipList == NULL");
  601. return FALSE;
  602. }
  603. BOOL bSetToClipBoard = (BOOL)lParam;
  604. if(bSetToClipBoard)
  605. {
  606. CClip *pClip = pClipList->GetTail();
  607. if(pClip)
  608. {
  609. LogSendRecieveInfo("OnAddToDatabaseFromSocket - Adding clip from socket setting clip to be put on clipboard");
  610. pClip->m_param1 = TRUE;
  611. }
  612. }
  613. m_thread.AddRemoteClipToSave(pClipList);
  614. delete pClipList;
  615. return TRUE;
  616. }
  617. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  618. {
  619. CString csNewText = (TCHAR*)wParam;
  620. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  621. return TRUE;
  622. }
  623. CString WndName(HWND hParent)
  624. {
  625. TCHAR cWindowText[200];
  626. ::GetWindowText(hParent, cWindowText, 100);
  627. int nCount = 0;
  628. while(STRLEN(cWindowText) <= 0)
  629. {
  630. hParent = ::GetParent(hParent);
  631. if(hParent == NULL)
  632. {
  633. break;
  634. }
  635. ::GetWindowText(hParent, cWindowText, 100);
  636. nCount++;
  637. if(nCount > 100)
  638. {
  639. Log(_T("GetTargetName reached maximum search depth of 100"));
  640. break;
  641. }
  642. }
  643. return cWindowText;
  644. }
  645. void CMainFrame::OnFirstHelp()
  646. {
  647. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  648. csFile += "DittoGettingStarted.htm";
  649. CHyperLink::GotoURL(csFile, SW_SHOW);
  650. }
  651. LRESULT CMainFrame::OnCustomizeTrayMenu(WPARAM wParam, LPARAM lParam)
  652. {
  653. CMenu *pMenu = (CMenu*)wParam;
  654. if(pMenu)
  655. {
  656. theApp.m_Language.UpdateTrayIconRightClickMenu(pMenu);
  657. }
  658. return true;
  659. }
  660. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  661. {
  662. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  663. CToolTipEx *pErrorWnd = new CToolTipEx;
  664. pErrorWnd->Create(this);
  665. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  666. CPoint pt;
  667. CRect rcScreen;
  668. GetMonitorRect(0, &rcScreen);
  669. pt = rcScreen.BottomRight();
  670. CRect cr = pErrorWnd->GetBoundsRect();
  671. pt.x -= max(cr.Width() + 50, 150);
  672. pt.y -= max(cr.Height() + 50, 150);
  673. pErrorWnd->Show(pt);
  674. pErrorWnd->HideWindowInXMilliSeconds(4000);
  675. }
  676. void CMainFrame::OnFirstImport()
  677. {
  678. theApp.ImportClips(theApp.m_MainhWnd);
  679. }
  680. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  681. {
  682. CWaitCursor wait;
  683. bool bCreatedWindow = false;
  684. if(m_pEditFrameWnd == NULL)
  685. {
  686. m_pEditFrameWnd = new CEditFrameWnd;
  687. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  688. bCreatedWindow = true;
  689. }
  690. if(m_pEditFrameWnd)
  691. {
  692. m_pEditFrameWnd->EditIds(Ids);
  693. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  694. if(bCreatedWindow)
  695. {
  696. CSize sz;
  697. CPoint pt;
  698. CGetSetOptions::GetEditWndSize(sz);
  699. CGetSetOptions::GetEditWndPoint(pt);
  700. CRect cr(pt, sz);
  701. EnsureWindowVisible(&cr);
  702. m_pEditFrameWnd->MoveWindow(cr);
  703. }
  704. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  705. m_pEditFrameWnd->SetForegroundWindow();
  706. m_pEditFrameWnd->SetFocus();
  707. }
  708. }
  709. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  710. {
  711. m_pEditFrameWnd = NULL;
  712. return TRUE;
  713. }
  714. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  715. {
  716. if(wParam)
  717. {
  718. theApp.SetConnectCV(true);
  719. }
  720. else if(lParam)
  721. {
  722. theApp.SetConnectCV(false);
  723. }
  724. return TRUE;
  725. }
  726. void CMainFrame::OnDestroy()
  727. {
  728. CFrameWnd::OnDestroy();
  729. if(m_pEditFrameWnd)
  730. {
  731. m_pEditFrameWnd->DestroyWindow();
  732. }
  733. }
  734. void CMainFrame::OnFirstNewclip()
  735. {
  736. CClipIDs IDs;
  737. IDs.Add( - 1);
  738. theApp.EditItems(IDs, true);
  739. }
  740. void CMainFrame::OnFirstOption()
  741. {
  742. if(m_pOptions != NULL)
  743. {
  744. ::SetForegroundWindow(m_pOptions->m_hWnd);
  745. }
  746. else
  747. {
  748. m_pOptions = new COptionsSheet(_T(""));
  749. if(m_pOptions != NULL)
  750. {
  751. ((COptionsSheet*)m_pOptions)->SetNotifyWnd(m_hWnd);
  752. m_pOptions->Create();
  753. m_pOptions->ShowWindow(SW_SHOW);
  754. }
  755. }
  756. }
  757. void CMainFrame::OnFirstGlobalhotkeys()
  758. {
  759. if(m_pGlobalClips != NULL)
  760. {
  761. ::SetForegroundWindow(m_pGlobalClips->m_hWnd);
  762. }
  763. else
  764. {
  765. m_pGlobalClips = new GlobalClips();
  766. if(m_pGlobalClips != NULL)
  767. {
  768. ((GlobalClips*)m_pGlobalClips)->SetNotifyWnd(m_hWnd);
  769. m_pGlobalClips->Create(IDD_GLOBAL_CLIPS, NULL);
  770. m_pGlobalClips->ShowWindow(SW_SHOW);
  771. }
  772. }
  773. }
  774. LRESULT CMainFrame::OnShowOptions(WPARAM wParam, LPARAM lParam)
  775. {
  776. OnFirstOption();
  777. return 0;
  778. }
  779. LRESULT CMainFrame::OnOptionsClosed(WPARAM wParam, LPARAM lParam)
  780. {
  781. delete m_pOptions;
  782. m_pOptions = NULL;
  783. return 0;
  784. }
  785. LRESULT CMainFrame::OnGlobalClipsClosed(WPARAM wParam, LPARAM lParam)
  786. {
  787. delete m_pGlobalClips;
  788. m_pGlobalClips = NULL;
  789. return 0;
  790. }