CP_Main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // CP_Main.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "Misc.h"
  7. #include "SelectDB.h"
  8. #include ".\cp_main.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // The one and only CCP_MainApp object
  16. CCP_MainApp theApp;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CCP_MainApp
  19. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  20. //{{AFX_MSG_MAP(CCP_MainApp)
  21. // NOTE - the ClassWizard will add and remove mapping macros here.
  22. // DO NOT EDIT what you see in these blocks of generated code!
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCP_MainApp construction
  27. CCP_MainApp::CCP_MainApp()
  28. {
  29. m_bAppRunning = false;
  30. m_bAppExiting = false;
  31. m_MainhWnd = NULL;
  32. m_pMainFrame = NULL;
  33. m_bShowingQuickPaste = false;
  34. m_bShowingOptions = false;
  35. m_bShowCopyProperties = false;
  36. m_bRemoveOldEntriesPending = false;
  37. m_IC_bCopy = false;
  38. m_GroupDefaultID = 0;
  39. m_GroupID = 0;
  40. m_GroupParentID = 0;
  41. m_GroupText = "History";
  42. m_FocusID = -1; // -1 == keep previous position, 0 == go to latest ID
  43. m_pDatabase = NULL;
  44. // Place all significant initialization in InitInstance
  45. }
  46. CCP_MainApp::~CCP_MainApp()
  47. {
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CCP_MainApp initialization
  51. BOOL CCP_MainApp::InitInstance()
  52. {
  53. LOG("InitInstance");
  54. AfxEnableControlContainer();
  55. m_hMutex = CreateMutex(NULL, FALSE, "Ditto Is Now Running");
  56. DWORD dwError = GetLastError();
  57. if(dwError == ERROR_ALREADY_EXISTS)
  58. {
  59. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  60. if(hWnd)
  61. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  62. return TRUE;
  63. }
  64. m_cfIgnoreClipboard = ::RegisterClipboardFormat("Clipboard Viewer Ignore");
  65. if(CheckDBExists(CGetSetOptions::GetDBPath()) == FALSE)
  66. {
  67. AfxMessageBox("Error Opening Database.");
  68. return TRUE;
  69. }
  70. AfxOleInit();
  71. // if(DoCleanups() == FALSE)
  72. // return TRUE;
  73. CMainFrame* pFrame = new CMainFrame;
  74. m_pMainWnd = m_pMainFrame = pFrame;
  75. // prevent no one having focus on startup
  76. TargetActiveWindow();
  77. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  78. pFrame->ShowWindow(SW_SHOW);
  79. pFrame->UpdateWindow();
  80. // prevent no one having focus on startup
  81. ReleaseFocus();
  82. return TRUE;
  83. }
  84. void CCP_MainApp::AfterMainCreate()
  85. {
  86. m_MainhWnd = m_pMainFrame->m_hWnd;
  87. ASSERT( ::IsWindow(m_MainhWnd) );
  88. g_Opt.SetMainHWND((long)m_MainhWnd);
  89. g_HotKeys.Init( m_MainhWnd );
  90. // create hotkeys here. They are automatically deleted on exit
  91. m_pDittoHotKey = new CHotKey("DittoHotKey",704); //704 is ctrl-tilda
  92. m_pCopyHotKey = new CHotKey("CopyHotKey");
  93. g_HotKeys.RegisterAll();
  94. // CopyThread initialization
  95. StartCopyThread();
  96. m_bAppRunning = true;
  97. }
  98. void CCP_MainApp::BeforeMainClose()
  99. {
  100. ASSERT( m_bAppRunning && !m_bAppExiting );
  101. m_bAppRunning = false;
  102. m_bAppExiting = true;
  103. g_HotKeys.UnregisterAll();
  104. StopCopyThread();
  105. }
  106. /*
  107. Re: Targeting the previous focus window
  108. We usually gain focus after the following messages:
  109. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  110. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  111. CMainFrame::PreTranslateMessage is used to intercept messages before
  112. they are processed (before we are actually given focus) in order to
  113. save the previous window that had focus.
  114. - It currently just handles "activating" mouse messages when showing.
  115. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  116. finding the Target on hotkey activation.
  117. This works well for most window switching (mouse or hotkey), but does
  118. not work well for <Alt>-<Tab> or other window switching applications
  119. (e.g. the taskbar tray), since the previous window was only a means to
  120. switching and not the target itself.
  121. - one solution might be to always monitor the current foreground
  122. window using system hooks or a short (e.g. 1 sec) timer... though this
  123. *might* be cpu intensive (slow). I'm currently looking into using
  124. WH_CBT system hooks in a separate dll (see: robpitt's
  125. http://website.lineone.net/~codebox/focuslog.zip).
  126. */
  127. bool CCP_MainApp::TargetActiveWindow()
  128. {
  129. HWND hOld = m_hTargetWnd;
  130. HWND hNew = ::GetForegroundWindow();
  131. if( hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew) )
  132. return false;
  133. m_hTargetWnd = hNew;
  134. if( QPasteWnd() )
  135. QPasteWnd()->UpdateStatus(true);
  136. // Tracking / Debugging
  137. /*
  138. LOG( StrF(
  139. "Target Changed" \
  140. "\n\tOld = 0x%08x: \"%s\"" \
  141. "\n\tNew = 0x%08x: \"%s\"\n",
  142. hOld, (LPCTSTR) GetWndText(hOld),
  143. hNew, (LPCTSTR) GetWndText(hNew) ) );
  144. */
  145. return true;
  146. }
  147. bool CCP_MainApp::ActivateTarget()
  148. {
  149. if( !::IsWindow(m_hTargetWnd) || m_hTargetWnd == ::GetForegroundWindow() )
  150. return false;
  151. ::SetForegroundWindow( m_hTargetWnd );
  152. // ::SetFocus( m_hTargetWnd );
  153. return true;
  154. }
  155. bool CCP_MainApp::ReleaseFocus()
  156. {
  157. if( IsAppWnd(::GetForegroundWindow()) )
  158. return ActivateTarget();
  159. return false;
  160. }
  161. // sends Ctrl-V to the TargetWnd
  162. void CCP_MainApp::SendPaste()
  163. {
  164. if( !ActivateTarget() )
  165. {
  166. SetStatus("SendPaste FAILED!");
  167. return;
  168. }
  169. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  170. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  171. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  172. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  173. }
  174. // CopyThread
  175. void CCP_MainApp::StartCopyThread()
  176. {
  177. ASSERT( m_MainhWnd );
  178. CClipTypes* pTypes = LoadTypesFromDB();
  179. // initialize to:
  180. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  181. // - true = use Asynchronous communication (PostMessage)
  182. // - true = enable copying on clipboard changes
  183. // - pTypes = the supported types to use
  184. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  185. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  186. m_CopyThread.ResumeThread();
  187. }
  188. void CCP_MainApp::StopCopyThread()
  189. {
  190. EnableCbCopy(false);
  191. m_CopyThread.Quit();
  192. SaveCopyClips();
  193. }
  194. // Allocates a new CClipTypes
  195. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  196. {
  197. CClipTypes* pTypes = new CClipTypes;
  198. try
  199. {
  200. CTypesTable recset;
  201. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Types" ,NULL);
  202. while(!recset.IsEOF())
  203. {
  204. pTypes->Add( GetFormatID(recset.m_TypeText) );
  205. recset.MoveNext();
  206. }
  207. recset.Close();
  208. }
  209. catch(CDaoException* e)
  210. {
  211. ASSERT(FALSE);
  212. e->Delete();
  213. }
  214. if( pTypes->GetSize() <= 0 )
  215. {
  216. pTypes->Add(CF_TEXT);
  217. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  218. }
  219. return pTypes;
  220. }
  221. void CCP_MainApp::ReloadTypes()
  222. {
  223. CClipTypes* pTypes = LoadTypesFromDB();
  224. if( pTypes )
  225. m_CopyThread.SetSupportedTypes( pTypes );
  226. }
  227. long CCP_MainApp::SaveCopyClips()
  228. {
  229. long lID = 0;
  230. int count;
  231. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  232. if( !pClips )
  233. return 0;
  234. count = pClips->AddToDB( true );
  235. if( count > 0 )
  236. {
  237. OnCopyCompleted( lID, count );
  238. lID = pClips->GetTail()->m_ID;
  239. }
  240. delete pClips;
  241. return lID;
  242. }
  243. void CCP_MainApp::RefreshView()
  244. {
  245. if( m_bShowingQuickPaste )
  246. {
  247. ASSERT( QPasteWnd() );
  248. QPasteWnd()->PostMessage(WM_REFRESH_VIEW);
  249. }
  250. }
  251. void CCP_MainApp::OnPasteCompleted()
  252. {
  253. // the list only changes if UpdateTimeOnPaste is true (updated time)
  254. if( g_Opt.m_bUpdateTimeOnPaste )
  255. RefreshView();
  256. }
  257. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  258. {
  259. if( count <= 0 )
  260. return;
  261. // queue a message to RemoveOldEntries
  262. Delayed_RemoveOldEntries( 60000 );
  263. // update copy statistics
  264. CGetSetOptions::SetTripCopyCount( -count );
  265. CGetSetOptions::SetTotalCopyCount( -count );
  266. // if we are in the History group, focus on the latest copy
  267. if( m_GroupID == 0 )
  268. m_FocusID = 0;
  269. RefreshView();
  270. ShowCopyProperties( lLastID );
  271. }
  272. // Internal Clipboard for cut/copy/paste items between Groups
  273. // if NULL, this uses the current QPaste selection
  274. void CCP_MainApp::IC_Cut( ARRAY* pIDs )
  275. {
  276. if( pIDs == NULL )
  277. {
  278. if( QPasteWnd() )
  279. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  280. else
  281. m_IC_IDs.SetSize(0);
  282. }
  283. else
  284. m_IC_IDs.Copy( *pIDs );
  285. m_IC_bCopy = false;
  286. if( QPasteWnd() )
  287. QPasteWnd()->UpdateStatus();
  288. }
  289. // if NULL, this uses the current QPaste selection
  290. void CCP_MainApp::IC_Copy( ARRAY* pIDs )
  291. {
  292. if( pIDs == NULL )
  293. {
  294. if( QPasteWnd() )
  295. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  296. else
  297. m_IC_IDs.SetSize(0);
  298. }
  299. else
  300. m_IC_IDs.Copy( *pIDs );
  301. m_IC_bCopy = true;
  302. if( QPasteWnd() )
  303. QPasteWnd()->UpdateStatus();
  304. }
  305. void CCP_MainApp::IC_Paste()
  306. {
  307. if( m_IC_IDs.GetSize() <= 0 )
  308. return;
  309. if( m_IC_bCopy )
  310. m_IC_IDs.CopyTo( GetValidGroupID() );
  311. else // Move
  312. m_IC_IDs.MoveTo( GetValidGroupID() );
  313. // don't process the same items twice.
  314. m_IC_IDs.SetSize(0);
  315. RefreshView();
  316. }
  317. // Groups
  318. BOOL CCP_MainApp::EnterGroupID( long lID )
  319. {
  320. BOOL bResult = FALSE;
  321. if( m_GroupID == lID )
  322. return TRUE;
  323. // if we are switching to the parent, focus on the previous group
  324. if( m_GroupParentID == lID && m_GroupID > 0 )
  325. m_FocusID = m_GroupID;
  326. switch( lID )
  327. {
  328. case 0: // History Group "ID"
  329. m_GroupID = 0;
  330. m_GroupParentID = 0;
  331. m_GroupText = "History";
  332. bResult = TRUE;
  333. break;
  334. case -1: // All Groups "ID"
  335. m_GroupID = -1;
  336. m_GroupParentID = 0;
  337. m_GroupText = "Groups";
  338. bResult = TRUE;
  339. break;
  340. default: // Normal Group
  341. try
  342. {
  343. CMainTable recs;
  344. COleVariant varKey( lID, VT_I4 );
  345. recs.Open( dbOpenTable, "Main" );
  346. recs.SetCurrentIndex("lID");
  347. // Find first record whose [lID] field == lID
  348. if( recs.Seek(_T("="), &varKey) && recs.m_bIsGroup )
  349. {
  350. m_GroupID = recs.m_lID;
  351. m_GroupParentID = recs.m_lParentID;
  352. if( m_GroupParentID == 0 )
  353. m_GroupParentID = -1; // back out into "all top-level groups" list.
  354. m_GroupText = recs.m_strText;
  355. bResult = TRUE;
  356. }
  357. recs.Close();
  358. }
  359. CATCHDAO
  360. break;
  361. }
  362. if( bResult )
  363. {
  364. theApp.RefreshView();
  365. if( QPasteWnd() )
  366. QPasteWnd()->UpdateStatus( true );
  367. }
  368. return bResult;
  369. }
  370. // returns a usable group id (not negative)
  371. long CCP_MainApp::GetValidGroupID()
  372. {
  373. if( m_GroupID <= 0 )
  374. return 0;
  375. return m_GroupID;
  376. }
  377. // sets a valid id
  378. void CCP_MainApp::SetGroupDefaultID( long lID )
  379. {
  380. if( m_GroupDefaultID == lID )
  381. return;
  382. if( lID <= 0 )
  383. m_GroupDefaultID = 0;
  384. else
  385. m_GroupDefaultID = lID;
  386. if( QPasteWnd() )
  387. QPasteWnd()->UpdateStatus();
  388. }
  389. // Window States
  390. void CCP_MainApp::SetStatus( const char* status, bool bRepaintImmediately )
  391. {
  392. m_Status = status;
  393. if( QPasteWnd() )
  394. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  395. }
  396. void CCP_MainApp::ShowPersistent( bool bVal )
  397. {
  398. g_Opt.SetShowPersistent( bVal );
  399. // give some visual indication
  400. if( m_bShowingQuickPaste )
  401. {
  402. ASSERT( QPasteWnd() );
  403. QPasteWnd()->SetCaptionColorActive( !g_Opt.m_bShowPersistent );
  404. QPasteWnd()->RefreshNc();
  405. }
  406. }
  407. void CCP_MainApp::ShowCopyProperties( long lID )
  408. {
  409. if( m_bShowCopyProperties && lID > 0 )
  410. {
  411. HWND hWndFocus = ::GetForegroundWindow();
  412. m_bShowCopyProperties = false;
  413. ::SendMessage(m_MainhWnd, WM_COPYPROPERTIES, lID, 0); // modal
  414. ::SetForegroundWindow(hWndFocus);
  415. }
  416. }
  417. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  418. {
  419. if( !m_bRemoveOldEntriesPending )
  420. {
  421. m_bRemoveOldEntriesPending = true;
  422. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  423. }
  424. }
  425. /////////////////////////////////////////////////////////////////////////////
  426. // CCP_MainApp message handlers
  427. int CCP_MainApp::ExitInstance()
  428. {
  429. LOG("ExitInstance");
  430. if(CGetSetOptions::GetCompactAndRepairOnExit())
  431. CompactDatabase();
  432. CloseDB();
  433. return CWinApp::ExitInstance();
  434. }
  435. CDaoDatabase* CCP_MainApp::EnsureOpenDB(CString csName)
  436. {
  437. try
  438. {
  439. if(!m_pDatabase)
  440. m_pDatabase = new CDaoDatabase;
  441. if(!m_pDatabase->IsOpen())
  442. {
  443. if(csName == "")
  444. m_pDatabase->Open(GetDBName());
  445. else
  446. m_pDatabase->Open(csName);
  447. }
  448. if(m_pMainWnd)
  449. ((CMainFrame *)m_pMainWnd)->ResetKillDBTimer();
  450. }
  451. CATCHDAO
  452. return m_pDatabase;
  453. }
  454. BOOL CCP_MainApp::CloseDB()
  455. {
  456. if(m_pDatabase)
  457. {
  458. if(m_pDatabase->IsOpen())
  459. m_pDatabase->Close();
  460. delete m_pDatabase;
  461. m_pDatabase = NULL;
  462. }
  463. return TRUE;
  464. }
  465. // return TRUE if there is more idle processing to do
  466. BOOL CCP_MainApp::OnIdle(LONG lCount)
  467. {
  468. // let winapp handle its idle processing
  469. if( CWinApp::OnIdle(lCount) )
  470. return TRUE;
  471. return FALSE;
  472. }