MainFrm.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 "focusdll\focusdll.h"
  12. #include "HyperLink.h"
  13. #include "tinyxml.h"
  14. #include "Path.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #define WM_ICON_NOTIFY WM_APP+10
  21. bool CShowMainFrame::m_bShowingMainFrame = false;
  22. CShowMainFrame::CShowMainFrame() :
  23. m_bHideMainFrameOnExit(false),
  24. m_hWnd(NULL)
  25. {
  26. if(m_bShowingMainFrame == false)
  27. {
  28. theApp.m_pMainFrame->m_TrayIcon.MaximiseFromTray(theApp.m_pMainFrame);
  29. m_bHideMainFrameOnExit = true;
  30. m_bShowingMainFrame = true;
  31. }
  32. m_hWnd = theApp.m_pMainFrame->GetSafeHwnd();
  33. }
  34. CShowMainFrame::~CShowMainFrame()
  35. {
  36. if(m_bHideMainFrameOnExit && m_hWnd && ::IsWindow(m_hWnd))
  37. {
  38. theApp.m_pMainFrame->m_TrayIcon.MinimiseToTray(theApp.m_pMainFrame);
  39. m_bShowingMainFrame = false;
  40. }
  41. }
  42. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  43. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  44. //{{AFX_MSG_MAP(CMainFrame)
  45. ON_WM_CREATE()
  46. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  47. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  48. ON_WM_TIMER()
  49. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  50. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  51. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  52. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  53. //}}AFX_MSG_MAP
  54. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  55. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  56. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  57. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  58. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  59. ON_WM_CLOSE()
  60. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  61. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  62. ON_MESSAGE(WM_FOCUS_CHANGED, OnFocusChanged)
  63. ON_MESSAGE(WM_FOCUS_CHANGED+1, OnKeyBoardChanged)
  64. ON_MESSAGE(WM_CUSTOMIZE_TRAY_MENU, OnCustomizeTrayMenu)
  65. ON_COMMAND(ID_FIRST_IMPORT, OnFirstImport)
  66. ON_MESSAGE(WM_EDIT_WND_CLOSING, OnEditWndClose)
  67. ON_WM_DESTROY()
  68. ON_COMMAND(ID_FIRST_NEWCLIP, OnFirstNewclip)
  69. ON_MESSAGE(WM_SET_CONNECTED, OnSetConnected)
  70. END_MESSAGE_MAP()
  71. static UINT indicators[] =
  72. {
  73. ID_SEPARATOR, // status line indicator
  74. ID_INDICATOR_CAPS,
  75. ID_INDICATOR_NUM,
  76. ID_INDICATOR_SCRL,
  77. };
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CMainFrame construction/destruction
  80. CMainFrame::CMainFrame()
  81. {
  82. m_pEditFrameWnd = NULL;
  83. }
  84. CMainFrame::~CMainFrame()
  85. {
  86. if(g_Opt.m_bUseHookDllForFocus)
  87. StopMonitoringFocusChanges();
  88. CGetSetOptions::SetMainHWND(0);
  89. }
  90. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  91. {
  92. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  93. return -1;
  94. //Center the main window so message boxes are in the center
  95. CRect rcScreen;
  96. GetMonitorRect(0, &rcScreen);
  97. CPoint cpCenter = rcScreen.CenterPoint();
  98. MoveWindow(cpCenter.x,cpCenter.x, -2, -2);
  99. //Then set the main window to transparent so it's never shown
  100. //if it is shown then only the task tray icon
  101. m_Transparency.SetTransparent(m_hWnd, 0, true);
  102. SetWindowText(_T(""));
  103. if(g_Opt.m_bUseHookDllForFocus)
  104. MonitorFocusChanges(m_hWnd, WM_FOCUS_CHANGED);
  105. SetWindowText(_T("Ditto"));
  106. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  107. m_TrayIcon.Create(
  108. NULL, // Let icon deal with its own messages
  109. WM_ICON_NOTIFY, // Icon notify message to use
  110. _T("Ditto"), // tooltip
  111. hIcon,
  112. IDR_MENU, // ID of tray icon
  113. FALSE,
  114. _T(""), // balloon tip
  115. _T(""), // balloon title
  116. NULL, // balloon icon
  117. 20 );
  118. m_TrayIcon.SetSingleClickSelect(TRUE);
  119. m_TrayIcon.MinimiseToTray(this);
  120. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  121. //Only if in release
  122. #ifndef _DEBUG
  123. {
  124. //If not showing the icon show it for 40 seconds so they can get to the option
  125. //in case they can't remember the hot keys or something like that
  126. if(!(CGetSetOptions::GetShowIconInSysTray()))
  127. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  128. }
  129. #endif
  130. //don't check for updates if running from a U3 device
  131. if(!g_Opt.m_bU3)
  132. {
  133. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  134. }
  135. SetTimer(CLOSE_WINDOW_TIMER, ONE_MINUTE*60, 0);
  136. SetTimer(REMOVE_OLD_REMOTE_COPIES, ONE_DAY, 0);
  137. theApp.Delayed_RemoveOldEntries(ONE_MINUTE*2);
  138. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  139. theApp.AfterMainCreate();
  140. return 0;
  141. }
  142. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  143. {
  144. if(cs.hMenu!=NULL)
  145. {
  146. ::DestroyMenu(cs.hMenu); // delete menu if loaded
  147. cs.hMenu = NULL; // no menu for this window
  148. }
  149. if( !CFrameWnd::PreCreateWindow(cs) )
  150. return FALSE;
  151. WNDCLASS wc;
  152. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  153. wc.lpfnWndProc = AfxWndProc;
  154. wc.cbClsExtra = 0;
  155. wc.cbWndExtra = 0;
  156. wc.hInstance = AfxGetInstanceHandle();
  157. wc.hIcon = NULL;
  158. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  159. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  160. wc.lpszMenuName = NULL;
  161. wc.lpszClassName = _T("Ditto");
  162. // Create the QPaste window class
  163. if (!AfxRegisterClass(&wc))
  164. return FALSE;
  165. cs.lpszClass = wc.lpszClassName;
  166. return TRUE;
  167. }
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CMainFrame diagnostics
  170. #ifdef _DEBUG
  171. void CMainFrame::AssertValid() const
  172. {
  173. CFrameWnd::AssertValid();
  174. }
  175. void CMainFrame::Dump(CDumpContext& dc) const
  176. {
  177. CFrameWnd::Dump(dc);
  178. }
  179. #endif //_DEBUG
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CMainFrame message handlers
  182. void CMainFrame::OnFirstOption()
  183. {
  184. theApp.ShowOptionsDlg();
  185. }
  186. void CMainFrame::OnFirstExit()
  187. {
  188. // CloseAllOpenDialogs();
  189. this->SendMessage(WM_CLOSE, 0, 0);
  190. }
  191. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  192. {
  193. if(wParam == theApp.m_pDittoHotKey->m_Atom)
  194. {
  195. if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && QuickPaste.IsWindowVisibleEx())
  196. {
  197. QuickPaste.HideQPasteWnd();
  198. }
  199. else
  200. {
  201. theApp.TargetActiveWindow();
  202. QuickPaste.ShowQPasteWnd(this, false, true, FALSE);
  203. }
  204. }
  205. else if(!g_Opt.m_bU3 && wParam == theApp.m_pNamedCopy->m_Atom)
  206. {
  207. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  208. {
  209. theApp.m_QuickPasteMode = CCP_MainApp::ADDING_QUICK_PASTE;
  210. theApp.SendCopy();
  211. m_pTypingToolTip = new CToolTipEx;
  212. m_pTypingToolTip->Create(this);
  213. csTypeToolTipTitle = theApp.m_Language.GetString("Named_Copy_Title", "Ditto - Named Copy");
  214. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle);
  215. CRect rcScreen;
  216. GetMonitorRect(0, &rcScreen);
  217. m_ToolTipPoint = GetFocusedCaretPos();
  218. if(m_ToolTipPoint.x < 0 || m_ToolTipPoint.y < 0)
  219. {
  220. CRect cr;
  221. ::GetWindowRect(theApp.m_hTargetWnd, cr);
  222. m_ToolTipPoint = cr.CenterPoint();
  223. }
  224. m_ToolTipPoint.Offset(-15, 15);
  225. m_pTypingToolTip->Show(m_ToolTipPoint);
  226. //If they don't type anything for 2 seconds stop looking
  227. SetTimer(STOP_LOOKING_FOR_KEYBOARD, 20000, NULL);
  228. MonitorKeyboardChanges(m_hWnd, WM_FOCUS_CHANGED+1);
  229. SetCaptureKeys(true);
  230. }
  231. }
  232. else if(!g_Opt.m_bU3 && wParam == theApp.m_pNamedPaste->m_Atom)
  233. {
  234. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  235. {
  236. theApp.m_QuickPasteMode = CCP_MainApp::PASTING_QUICK_PASTE;
  237. m_pTypingToolTip = new CToolTipEx;
  238. m_pTypingToolTip->Create(this);
  239. csTypeToolTipTitle = theApp.m_Language.GetString("Named_Paste_Title", "Ditto - Named Paste");
  240. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle);
  241. CRect rcScreen;
  242. m_ToolTipPoint = GetFocusedCaretPos();
  243. if(m_ToolTipPoint.x < 0 || m_ToolTipPoint.y < 0)
  244. {
  245. CRect cr;
  246. ::GetWindowRect(theApp.m_hTargetWnd, cr);
  247. m_ToolTipPoint = cr.CenterPoint();
  248. }
  249. m_ToolTipPoint.Offset(-15, 15);
  250. m_pTypingToolTip->Show(m_ToolTipPoint);
  251. //If they don't type anything for 2 seconds stop looking
  252. SetTimer(STOP_LOOKING_FOR_KEYBOARD, 20000, NULL);
  253. MonitorKeyboardChanges(m_hWnd, WM_FOCUS_CHANGED+1);
  254. SetCaptureKeys(true);
  255. }
  256. }
  257. else if(wParam == theApp.m_pPosOne->m_Atom)
  258. {
  259. DoFirstTenPositionsPaste(0);
  260. }
  261. else if(wParam == theApp.m_pPosTwo->m_Atom)
  262. {
  263. DoFirstTenPositionsPaste(1);
  264. }
  265. else if(wParam == theApp.m_pPosThree->m_Atom)
  266. {
  267. DoFirstTenPositionsPaste(2);
  268. }
  269. else if(wParam == theApp.m_pPosFour->m_Atom)
  270. {
  271. DoFirstTenPositionsPaste(3);
  272. }
  273. else if(wParam == theApp.m_pPosFive->m_Atom)
  274. {
  275. DoFirstTenPositionsPaste(4);
  276. }
  277. else if(wParam == theApp.m_pPosSix->m_Atom)
  278. {
  279. DoFirstTenPositionsPaste(5);
  280. }
  281. else if(wParam == theApp.m_pPosSeven->m_Atom)
  282. {
  283. DoFirstTenPositionsPaste(6);
  284. }
  285. else if(wParam == theApp.m_pPosEight->m_Atom)
  286. {
  287. DoFirstTenPositionsPaste(7);
  288. }
  289. else if(wParam == theApp.m_pPosNine->m_Atom)
  290. {
  291. DoFirstTenPositionsPaste(8);
  292. }
  293. else if(wParam == theApp.m_pPosTen->m_Atom)
  294. {
  295. DoFirstTenPositionsPaste(9);
  296. }
  297. return TRUE;
  298. }
  299. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  300. {
  301. try
  302. {
  303. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, bIsGroup, lDate FROM Main ")
  304. _T("WHERE ((bIsGroup = 1 AND lParentID = -1) OR bIsGroup = 0) ")
  305. _T("ORDER BY bIsGroup ASC, lDate DESC ")
  306. _T("LIMIT 1 OFFSET %d"), nPos);
  307. if(q.eof() == false)
  308. {
  309. if(q.getIntField(_T("bIsGroup")) == FALSE)
  310. {
  311. //Don't move these to the top
  312. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  313. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  314. CProcessPaste paste;
  315. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  316. paste.m_bActivateTarget = false;
  317. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  318. paste.DoPaste();
  319. theApp.OnPasteCompleted();
  320. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  321. }
  322. }
  323. }
  324. CATCH_SQLITE_EXCEPTION
  325. }
  326. void CMainFrame::OnTimer(UINT nIDEvent)
  327. {
  328. switch(nIDEvent)
  329. {
  330. case HIDE_ICON_TIMER:
  331. {
  332. m_TrayIcon.HideIcon();
  333. KillTimer(nIDEvent);
  334. break;
  335. }
  336. case CLOSE_WINDOW_TIMER:
  337. {
  338. QuickPaste.CloseQPasteWnd();
  339. break;
  340. }
  341. case REMOVE_OLD_ENTRIES_TIMER:
  342. {
  343. theApp.m_bRemoveOldEntriesPending = false;
  344. RemoveOldEntries();
  345. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  346. break;
  347. }
  348. case CHECK_FOR_UPDATE:
  349. {
  350. KillTimer(CHECK_FOR_UPDATE);
  351. CInternetUpdate Update;
  352. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  353. {
  354. PostMessage(WM_CLOSE, 0, 0);
  355. }
  356. else
  357. {
  358. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*60*24, NULL);
  359. }
  360. break;
  361. }
  362. case CLOSE_APP:
  363. {
  364. PostMessage(WM_CLOSE, 0, 0);
  365. KillTimer(CLOSE_APP);
  366. break;
  367. }
  368. case STOP_MONITORING_KEYBOARD_TIMER:
  369. {
  370. StopLookingForKeystrokes(false);
  371. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  372. {
  373. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  374. {
  375. PasteQuickPasteEntry(m_csKeyboardPaste);
  376. }
  377. else
  378. {
  379. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  380. }
  381. StopLookingForKeystrokes(true);
  382. }
  383. break;
  384. }
  385. case STOP_LOOKING_FOR_KEYBOARD:
  386. {
  387. if(theApp.m_QuickPasteMode == CCP_MainApp::ADDING_QUICK_PASTE)
  388. {
  389. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  390. }
  391. //They didn't type anything within 2 seconds stop looking
  392. StopLookingForKeystrokes(true);
  393. break;
  394. }
  395. case REMOVE_OLD_REMOTE_COPIES:
  396. AfxBeginThread(CMainFrame::RemoteOldRemoteFilesThread, NULL);
  397. break;
  398. }
  399. CFrameWnd::OnTimer(nIDEvent);
  400. }
  401. void CMainFrame::StopLookingForKeystrokes(bool bInitAppVaribles)
  402. {
  403. StopMonitoringKeyboardChanges();
  404. SetCaptureKeys(false);
  405. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  406. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  407. if(bInitAppVaribles)
  408. {
  409. theApp.m_QuickPasteMode = CCP_MainApp::NONE_QUICK_PASTE;
  410. m_csKeyboardPaste.Empty();
  411. delete theApp.m_pQuickPasteClip;
  412. theApp.m_pQuickPasteClip = NULL;
  413. m_pTypingToolTip->Hide();
  414. m_pTypingToolTip->DestroyWindow();
  415. }
  416. }
  417. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  418. {
  419. if(lParam)
  420. {
  421. if(!m_TrayIcon.Visible())
  422. {
  423. KillTimer(HIDE_ICON_TIMER);
  424. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  425. }
  426. }
  427. if(wParam)
  428. m_TrayIcon.ShowIcon();
  429. else
  430. m_TrayIcon.HideIcon();
  431. return TRUE;
  432. }
  433. void CMainFrame::OnFirstShowquickpaste()
  434. {
  435. QuickPaste.ShowQPasteWnd(this, true, false, FALSE);
  436. }
  437. void CMainFrame::OnFirstToggleConnectCV()
  438. {
  439. theApp.ToggleConnectCV();
  440. }
  441. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI* pCmdUI)
  442. {
  443. theApp.UpdateMenuConnectCV( pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV );
  444. }
  445. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  446. {
  447. long lID = (long)wParam;
  448. if(lID > 0)
  449. {
  450. bool bOldState = theApp.EnableCbCopy(false);
  451. CCopyProperties props(lID, this);
  452. props.SetHideOnKillFocus(true);
  453. props.DoModal();
  454. theApp.EnableCbCopy( bOldState );
  455. }
  456. return TRUE;
  457. }
  458. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  459. {
  460. SetTimer(CLOSE_APP, 100, NULL);
  461. return TRUE;
  462. }
  463. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  464. {
  465. // if the delay is undesirable, this could be altered to save one at a time,
  466. // allowing the processing of other messages between saving clips.
  467. theApp.SaveCopyClips();
  468. return TRUE;
  469. }
  470. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  471. {
  472. // target before mouse messages change the focus
  473. if( theApp.m_bShowingQuickPaste &&
  474. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  475. { theApp.TargetActiveWindow(); }
  476. return CFrameWnd::PreTranslateMessage(pMsg);
  477. }
  478. void CMainFrame::OnClose()
  479. {
  480. CloseAllOpenDialogs();
  481. if(m_pEditFrameWnd)
  482. {
  483. if(m_pEditFrameWnd->CloseAll() == false)
  484. return;
  485. }
  486. theApp.BeforeMainClose();
  487. CFrameWnd::OnClose();
  488. }
  489. bool CMainFrame::CloseAllOpenDialogs()
  490. {
  491. bool bRet = false;
  492. DWORD dwordProcessId;
  493. DWORD dwordChildWindowProcessId;
  494. GetWindowThreadProcessId(this->m_hWnd, &dwordProcessId);
  495. ASSERT(dwordProcessId);
  496. CWnd *pTempWnd = GetDesktopWindow()->GetWindow(GW_CHILD);
  497. while((pTempWnd = pTempWnd->GetWindow(GW_HWNDNEXT)) != NULL)
  498. {
  499. if(pTempWnd->GetSafeHwnd() == NULL)
  500. break;
  501. GetWindowThreadProcessId(pTempWnd->GetSafeHwnd(), &dwordChildWindowProcessId);
  502. if(dwordChildWindowProcessId == dwordProcessId)
  503. {
  504. TCHAR szTemp[100];
  505. GetClassName(pTempWnd->GetSafeHwnd(), szTemp, 100);
  506. // #32770 is class name for dialogs so don't process the message if it is a dialog
  507. if(STRCMP(szTemp, _T("#32770")) == 0)
  508. {
  509. pTempWnd->SendMessage(WM_CLOSE, 0, 0);
  510. bRet = true;
  511. }
  512. }
  513. }
  514. MSG msg;
  515. while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  516. {
  517. TranslateMessage(&msg);
  518. DispatchMessage(&msg);
  519. }
  520. return bRet;
  521. }
  522. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  523. {
  524. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  525. CClipList *pClipList = (CClipList*)wParam;
  526. if(pClipList == NULL)
  527. {
  528. LogSendRecieveInfo("---------ERROR pClipList == NULL");
  529. return FALSE;
  530. }
  531. BOOL bSetToClipBoard = (BOOL)lParam;
  532. if(bSetToClipBoard)
  533. {
  534. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  535. CClip *pClip = pClipList->GetTail();
  536. if(pClip)
  537. {
  538. CClip NewClip;
  539. NewClip = *pClip;
  540. LogSendRecieveInfo("---------After =");
  541. CProcessPaste paste;
  542. //Don't send the paste just load it into memory
  543. paste.m_bSendPaste = false;
  544. paste.m_pOle->PutFormatOnClipboard(&NewClip.m_Formats, false);
  545. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  546. LogSendRecieveInfo("---------After LoadFormats");
  547. paste.DoPaste();
  548. }
  549. else
  550. {
  551. LogSendRecieveInfo("---------GetTail returned NULL");
  552. }
  553. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  554. }
  555. pClipList->AddToDB(true);
  556. LogSendRecieveInfo("---------After AddToDB");
  557. CClip *pClip = pClipList->GetTail();
  558. if(pClip)
  559. {
  560. theApp.m_FocusID = pClip->m_ID;
  561. }
  562. theApp.RefreshView();
  563. delete pClipList;
  564. pClipList = NULL;
  565. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  566. theApp.Delayed_RemoveOldEntries(60000);
  567. return TRUE;
  568. }
  569. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  570. {
  571. CString csNewText = (TCHAR*)wParam;
  572. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  573. return TRUE;
  574. }
  575. LRESULT CMainFrame::OnKeyBoardChanged(WPARAM wParam, LPARAM lParam)
  576. {
  577. if(wParam == VK_ESCAPE)
  578. {
  579. StopLookingForKeystrokes(true);
  580. }
  581. else if(wParam == VK_RETURN)
  582. {
  583. StopLookingForKeystrokes(false);
  584. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  585. {
  586. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  587. {
  588. PasteQuickPasteEntry(m_csKeyboardPaste);
  589. }
  590. else
  591. {
  592. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  593. }
  594. }
  595. StopLookingForKeystrokes(true);
  596. }
  597. else if((wParam >= 32 && wParam <= 96) || wParam == VK_BACK)
  598. {
  599. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  600. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  601. bool bContinue = true;
  602. if(wParam == VK_BACK)
  603. {
  604. m_csKeyboardPaste = m_csKeyboardPaste.Left(m_csKeyboardPaste.GetLength()-1);
  605. }
  606. else
  607. {
  608. CString cs((char)wParam);
  609. m_csKeyboardPaste += cs;
  610. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  611. {
  612. try
  613. {
  614. int nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText LIKE \'%%%s%%\'"), m_csKeyboardPaste);
  615. if(nCount == 1)
  616. {
  617. nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText = \'%s\'"), m_csKeyboardPaste);
  618. if(nCount == 1)
  619. {
  620. StopLookingForKeystrokes(false);
  621. PasteQuickPasteEntry(m_csKeyboardPaste);
  622. StopLookingForKeystrokes(true);
  623. bContinue = false;
  624. }
  625. }
  626. }
  627. CATCH_SQLITE_EXCEPTION
  628. }
  629. }
  630. if(bContinue)
  631. {
  632. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle + "\n\n" + m_csKeyboardPaste);
  633. m_pTypingToolTip->Show(m_ToolTipPoint);
  634. SetTimer(STOP_MONITORING_KEYBOARD_TIMER, 10000, NULL);
  635. Log(StrF(_T("OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  636. }
  637. }
  638. else
  639. {
  640. CString cs((char)wParam);
  641. Log(StrF(_T("INVALID Key OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  642. }
  643. return TRUE;
  644. }
  645. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  646. {
  647. if(g_Opt.m_bUseHookDllForFocus == FALSE)
  648. return TRUE;
  649. HWND hFocus = (HWND)wParam;
  650. HWND hParent = hFocus;
  651. HWND hLastGoodParent = hParent;
  652. static DWORD dLastDittoHasFocusTick = 0;
  653. //Sometimes when we bring ditto up there will come a null focus
  654. //rite after that
  655. if(hFocus == NULL && (GetTickCount() - dLastDittoHasFocusTick < 500))
  656. {
  657. Log(_T("NULL focus within 500 ticks of bringing up ditto"));
  658. return TRUE;
  659. }
  660. else if(hFocus == NULL)
  661. {
  662. Log(_T("NULL focus received"));
  663. }
  664. //only proceed if something changed
  665. if(theApp.m_hTargetWnd == hFocus)
  666. return TRUE;
  667. TCHAR cWindowText[100];
  668. ::GetWindowText(hFocus, cWindowText, 100);
  669. HWND hTray = ::FindWindow(_T("Shell_TrayWnd"), _T(""));
  670. // Log(StrF("Focus = %d", hFocus));
  671. int nCount = 0;
  672. while(true)
  673. {
  674. hParent = ::GetParent(hParent);
  675. if(hParent == NULL)
  676. break;
  677. // Log(StrF("Focus2 = %d", hParent));
  678. //allow focus on edit window
  679. if(m_pEditFrameWnd && hParent == m_pEditFrameWnd->GetSafeHwnd())
  680. {
  681. break;
  682. }
  683. hLastGoodParent = hParent;
  684. nCount++;
  685. if(nCount > 100)
  686. {
  687. Log(_T("OnFocusChanged reached maximum search depth of 100"));
  688. break;
  689. }
  690. }
  691. //If the parent is ditto or the tray icon then don't set focus to that window
  692. if(hLastGoodParent == m_hWnd || hLastGoodParent == hTray)
  693. {
  694. Log(_T("Ditto Has Focus"));
  695. theApp.m_bDittoHasFocus = true;
  696. dLastDittoHasFocusTick = GetTickCount();
  697. }
  698. else
  699. {
  700. theApp.m_bDittoHasFocus = false;
  701. theApp.m_hTargetWnd = hFocus;
  702. if(theApp.QPasteWnd() )
  703. theApp.QPasteWnd()->UpdateStatus(true);
  704. }
  705. return TRUE;
  706. }
  707. void CMainFrame::OnFirstHelp()
  708. {
  709. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  710. csFile += "DittoGettingStarted.htm";
  711. CHyperLink::GotoURL(csFile, SW_SHOW);
  712. }
  713. LRESULT CMainFrame::OnCustomizeTrayMenu(WPARAM wParam, LPARAM lParam)
  714. {
  715. CMenu *pMenu = (CMenu*)wParam;
  716. if(pMenu)
  717. {
  718. theApp.m_Language.UpdateTrayIconRightClickMenu(pMenu);
  719. }
  720. return true;
  721. }
  722. bool CMainFrame::PasteQuickPasteEntry(CString csQuickPaste)
  723. {
  724. Log(StrF(_T("PasteQuickPasteEntry -- %s"), csQuickPaste));
  725. CString csTitle = theApp.m_Language.GetString("Named_Paste_Title", "Ditto - Named Paste");
  726. bool bRet = false;
  727. try
  728. {
  729. csQuickPaste.Replace(_T("'"), _T("''"));
  730. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE QuickPasteText = '%s'"), csQuickPaste);
  731. if(q.eof() == false)
  732. {
  733. CClip Clip;
  734. if(Clip.LoadFormats(q.getIntField(_T("lID"))))
  735. {
  736. CProcessPaste paste;
  737. paste.m_pOle->PutFormatOnClipboard(&Clip.m_Formats, false);
  738. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  739. if(paste.DoPaste())
  740. {
  741. bRet = TRUE;
  742. }
  743. }
  744. else
  745. {
  746. CString csError = theApp.m_Language.GetString("Named_Paste_Error1", "Error loading data");
  747. ShowErrorMessage(csTitle, StrF(_T("%s - id:%d"), csError, q.getIntField(_T("lID"))));
  748. }
  749. }
  750. else
  751. {
  752. CString csError = theApp.m_Language.GetString("Named_Paste_Error2", "Error finding clip with QuickPaste Text of");
  753. ShowErrorMessage(csTitle, StrF(_T("%s - '%s'"), csError, csQuickPaste));
  754. }
  755. }
  756. catch (CppSQLite3Exception& e)
  757. {
  758. CString csError = theApp.m_Language.GetString("Named_Paste_Error3", "Exception finding the clip");
  759. ShowErrorMessage(csTitle, StrF(_T("%s - '%s' - %s"), csError, csQuickPaste, e.errorMessage()));
  760. }
  761. return bRet;
  762. }
  763. bool CMainFrame::SaveQuickPasteEntry(CString csQuickPaste, CClipList *pClipList)
  764. {
  765. Log(StrF(_T("SaveQuickPasteEntry text = %s - recived copy = %d"), csQuickPaste, pClipList != NULL));
  766. CString csTitle = theApp.m_Language.GetString("Named_Copy_Title", "Ditto - Named Copy");
  767. bool bRet = false;
  768. if(pClipList)
  769. {
  770. try
  771. {
  772. CClip *pClip = pClipList->GetHead();
  773. pClip->m_csQuickPaste = csQuickPaste;
  774. pClip->m_lDontAutoDelete = (long)CTime::GetCurrentTime().GetTime();
  775. if(csQuickPaste.IsEmpty() == FALSE)
  776. {
  777. //Remove any other instances of this quick paste
  778. csQuickPaste.Replace(_T("'"), _T("''"));
  779. int nCount = theApp.m_db.execDMLEx(_T("UPDATE Main SET QuickPasteText = '' WHERE QuickPasteText = '%s';"), csQuickPaste);
  780. if(nCount > 0)
  781. {
  782. Log(StrF(_T("Removed quick paste '%s', count = %d"), csQuickPaste, nCount));
  783. }
  784. }
  785. int count = pClipList->AddToDB(true);
  786. if(count > 0)
  787. {
  788. long lID = pClipList->GetTail()->m_ID;
  789. theApp.OnCopyCompleted(lID, count);
  790. bRet = true;
  791. }
  792. else
  793. {
  794. CString csError = theApp.m_Language.GetString("Named_Copy_Error1", "Error saving Named Coy to Database");
  795. ShowErrorMessage(csTitle, csError);
  796. }
  797. }
  798. catch (CppSQLite3Exception& e)
  799. {
  800. Log(StrF(_T("SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage()));
  801. ASSERT(FALSE);
  802. CString csError = theApp.m_Language.GetString("Named_Copy_Error2", "Exception saving Named Copy");
  803. ShowErrorMessage(csTitle, StrF(_T("%s - %s"), csError, e.errorMessage()));
  804. }
  805. }
  806. else
  807. {
  808. CString csError = theApp.m_Language.GetString("Named_Copy_Error3", "Ditto did not receive a copy");
  809. ShowErrorMessage(csTitle, csError);
  810. }
  811. return bRet;
  812. }
  813. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  814. {
  815. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  816. CToolTipEx *pErrorWnd = new CToolTipEx;
  817. pErrorWnd->Create(this);
  818. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  819. CPoint pt;
  820. CRect rcScreen;
  821. GetMonitorRect(0, &rcScreen);
  822. pt = rcScreen.BottomRight();
  823. CRect cr = pErrorWnd->GetBoundsRect();
  824. pt.x -= max(cr.Width()+50, 150);
  825. pt.y -= max(cr.Height()+50, 150);
  826. pErrorWnd->Show(pt);
  827. pErrorWnd->HideWindowInXMilliSeconds(4000);
  828. }
  829. void CMainFrame::DeleteOldRemoteCopies(CString csDir)
  830. {
  831. //must be deleting a sub folder in the musicgen directory
  832. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  833. return;
  834. FIX_CSTRING_PATH(csDir);
  835. CTime ctOld = CTime::GetCurrentTime();
  836. CTime ctFile;
  837. ctOld -= CTimeSpan(0, 0, 0, 1);
  838. CFileFind Find;
  839. CString csFindString;
  840. csFindString.Format(_T("%s*.*"), csDir);
  841. BOOL bFound = Find.FindFile(csFindString);
  842. while(bFound)
  843. {
  844. bFound = Find.FindNextFile();
  845. if(Find.IsDots())
  846. continue;
  847. if(Find.IsDirectory())
  848. {
  849. CString csDir(Find.GetFilePath());
  850. DeleteOldRemoteCopies(csDir);
  851. RemoveDirectory(csDir);
  852. }
  853. if(Find.GetLastAccessTime(ctFile))
  854. {
  855. //Delete the remote copied file if it has'nt been used for the last day
  856. if(ctFile < ctOld)
  857. {
  858. DeleteFile(Find.GetFilePath());
  859. }
  860. }
  861. else
  862. {
  863. DeleteFile(Find.GetFilePath());
  864. }
  865. }
  866. }
  867. UINT CMainFrame::RemoteOldRemoteFilesThread(LPVOID pParam)
  868. {
  869. CString csDir = CGetSetOptions::GetPath(PATH_REMOTE_FILES);
  870. if(FileExists(csDir))
  871. {
  872. DeleteOldRemoteCopies(csDir);
  873. }
  874. return TRUE;
  875. }
  876. void CMainFrame::OnFirstImport()
  877. {
  878. theApp.ImportClips(theApp.m_MainhWnd);
  879. }
  880. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  881. {
  882. CWaitCursor wait;
  883. bool bCreatedWindow = false;
  884. if(m_pEditFrameWnd == NULL)
  885. {
  886. m_pEditFrameWnd = new CEditFrameWnd;
  887. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  888. bCreatedWindow = true;
  889. }
  890. if(m_pEditFrameWnd)
  891. {
  892. m_pEditFrameWnd->EditIds(Ids);
  893. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  894. if(bCreatedWindow)
  895. {
  896. CSize sz;
  897. CPoint pt;
  898. CGetSetOptions::GetEditWndSize(sz);
  899. CGetSetOptions::GetEditWndPoint(pt);
  900. CRect cr(pt, sz);
  901. EnsureWindowVisible(&cr);
  902. m_pEditFrameWnd->MoveWindow(cr);
  903. }
  904. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  905. m_pEditFrameWnd->SetForegroundWindow();
  906. m_pEditFrameWnd->SetFocus();
  907. }
  908. }
  909. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  910. {
  911. m_pEditFrameWnd = NULL;
  912. return TRUE;
  913. }
  914. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  915. {
  916. if(wParam)
  917. theApp.SetConnectCV(true);
  918. else if(lParam)
  919. theApp.SetConnectCV(false);
  920. return TRUE;
  921. }
  922. void CMainFrame::OnDestroy()
  923. {
  924. CFrameWnd::OnDestroy();
  925. if(m_pEditFrameWnd)
  926. {
  927. m_pEditFrameWnd->DestroyWindow();
  928. }
  929. }
  930. void CMainFrame::OnFirstNewclip()
  931. {
  932. CClipIDs IDs;
  933. IDs.Add(-1);
  934. theApp.EditItems(IDs, true);
  935. }