CP_Main.cpp 12 KB

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