CP_Main.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_pDatabase = NULL;
  38. // Place all significant initialization in InitInstance
  39. }
  40. CCP_MainApp::~CCP_MainApp()
  41. {
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CCP_MainApp initialization
  45. BOOL CCP_MainApp::InitInstance()
  46. {
  47. LOG("InitInstance");
  48. AfxEnableControlContainer();
  49. m_hMutex = CreateMutex(NULL, FALSE, "Ditto Is Now Running");
  50. DWORD dwError = GetLastError();
  51. if(dwError == ERROR_ALREADY_EXISTS)
  52. {
  53. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  54. if(hWnd)
  55. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  56. return TRUE;
  57. }
  58. m_cfIgnoreClipboard = ::RegisterClipboardFormat("Clipboard Viewer Ignore");
  59. if(CheckDBExists(CGetSetOptions::GetDBPath()) == FALSE)
  60. {
  61. AfxMessageBox("Error Opening Database.");
  62. return TRUE;
  63. }
  64. AfxOleInit();
  65. if(DoCleanups() == FALSE)
  66. return TRUE;
  67. CMainFrame* pFrame = new CMainFrame;
  68. m_pMainWnd = m_pMainFrame = pFrame;
  69. // prevent no one having focus on startup
  70. TargetActiveWindow();
  71. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  72. pFrame->ShowWindow(SW_SHOW);
  73. pFrame->UpdateWindow();
  74. // prevent no one having focus on startup
  75. ReleaseFocus();
  76. return TRUE;
  77. }
  78. void CCP_MainApp::AfterMainCreate()
  79. {
  80. m_MainhWnd = m_pMainFrame->m_hWnd;
  81. ASSERT( ::IsWindow(m_MainhWnd) );
  82. g_Opt.SetMainHWND((long)m_MainhWnd);
  83. g_HotKeys.Init( m_MainhWnd );
  84. // create hotkeys here. They are automatically deleted on exit
  85. m_pDittoHotKey = new CHotKey("DittoHotKey",704); //704 is ctrl-tilda
  86. m_pCopyHotKey = new CHotKey("CopyHotKey");
  87. g_HotKeys.RegisterAll();
  88. // CopyThread initialization
  89. StartCopyThread();
  90. m_bAppRunning = true;
  91. }
  92. void CCP_MainApp::BeforeMainClose()
  93. {
  94. ASSERT( m_bAppRunning && !m_bAppExiting );
  95. m_bAppRunning = false;
  96. m_bAppExiting = true;
  97. g_HotKeys.UnregisterAll();
  98. StopCopyThread();
  99. }
  100. /*
  101. Re: Targeting the previous focus window
  102. We usually gain focus after the following messages:
  103. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  104. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  105. CMainFrame::PreTranslateMessage is used to intercept messages before
  106. they are processed (before we are actually given focus) in order to
  107. save the previous window that had focus.
  108. - It currently just handles "activating" mouse messages when showing.
  109. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  110. finding the Target on hotkey activation.
  111. This works well for most window switching (mouse or hotkey), but does
  112. not work well for <Alt>-<Tab> or other window switching applications
  113. (e.g. the taskbar tray), since the previous window was only a means to
  114. switching and not the target itself.
  115. - one solution might be to always monitor the current foreground
  116. window using system hooks or a short (e.g. 1 sec) timer... though this
  117. *might* be cpu intensive (slow). I'm currently looking into using
  118. WH_CBT system hooks in a separate dll (see: robpitt's
  119. http://website.lineone.net/~codebox/focuslog.zip).
  120. */
  121. bool CCP_MainApp::TargetActiveWindow()
  122. {
  123. HWND hOld = m_hTargetWnd;
  124. HWND hNew = ::GetForegroundWindow();
  125. if( hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew) )
  126. return false;
  127. m_hTargetWnd = hNew;
  128. if( QPasteWnd() )
  129. QPasteWnd()->UpdateStatus(true);
  130. // Tracking / Debugging
  131. /*
  132. LOG( StrF(
  133. "Target Changed" \
  134. "\n\tOld = 0x%08x: \"%s\"" \
  135. "\n\tNew = 0x%08x: \"%s\"\n",
  136. hOld, (LPCTSTR) GetWndText(hOld),
  137. hNew, (LPCTSTR) GetWndText(hNew) ) );
  138. */
  139. return true;
  140. }
  141. bool CCP_MainApp::ActivateTarget()
  142. {
  143. if( !::IsWindow(m_hTargetWnd) || m_hTargetWnd == ::GetForegroundWindow() )
  144. return false;
  145. ::SetForegroundWindow( m_hTargetWnd );
  146. // ::SetFocus( m_hTargetWnd );
  147. return true;
  148. }
  149. bool CCP_MainApp::ReleaseFocus()
  150. {
  151. if( IsAppWnd(::GetForegroundWindow()) )
  152. return ActivateTarget();
  153. return false;
  154. }
  155. // sends Ctrl-V to the TargetWnd
  156. void CCP_MainApp::SendPaste()
  157. {
  158. if( !ActivateTarget() )
  159. {
  160. SetStatus("SendPaste FAILED!");
  161. return;
  162. }
  163. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  164. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  165. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  166. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  167. }
  168. // CopyThread
  169. void CCP_MainApp::StartCopyThread()
  170. {
  171. ASSERT( m_MainhWnd );
  172. CClipTypes* pTypes = LoadTypesFromDB();
  173. // initialize to:
  174. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  175. // - true = use Asynchronous communication (PostMessage)
  176. // - true = enable copying on clipboard changes
  177. // - pTypes = the supported types to use
  178. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  179. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  180. m_CopyThread.ResumeThread();
  181. }
  182. void CCP_MainApp::StopCopyThread()
  183. {
  184. EnableCbCopy(false);
  185. m_CopyThread.Quit();
  186. SaveCopyClips();
  187. }
  188. // Allocates a new CClipTypes
  189. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  190. {
  191. CClipTypes* pTypes = new CClipTypes;
  192. try
  193. {
  194. CTypesTable recset;
  195. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Types" ,NULL);
  196. while(!recset.IsEOF())
  197. {
  198. pTypes->Add( GetFormatID(recset.m_TypeText) );
  199. recset.MoveNext();
  200. }
  201. recset.Close();
  202. }
  203. catch(CDaoException* e)
  204. {
  205. ASSERT(FALSE);
  206. e->Delete();
  207. }
  208. if( pTypes->GetSize() <= 0 )
  209. {
  210. pTypes->Add(CF_TEXT);
  211. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  212. }
  213. return pTypes;
  214. }
  215. void CCP_MainApp::ReloadTypes()
  216. {
  217. CClipTypes* pTypes = LoadTypesFromDB();
  218. if( pTypes )
  219. m_CopyThread.SetSupportedTypes( pTypes );
  220. }
  221. long CCP_MainApp::SaveCopyClips()
  222. {
  223. long lID = 0;
  224. int count;
  225. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  226. if( !pClips )
  227. return 0;
  228. count = pClips->AddToDB( true );
  229. if( count > 0 )
  230. {
  231. OnCopyCompleted( lID, count );
  232. lID = pClips->GetTail()->m_ID;
  233. }
  234. delete pClips;
  235. return lID;
  236. }
  237. void CCP_MainApp::RefreshView()
  238. {
  239. if( m_bShowingQuickPaste )
  240. {
  241. ASSERT( QPasteWnd() );
  242. QPasteWnd()->PostMessage(WM_REFRESH_VIEW);
  243. }
  244. }
  245. void CCP_MainApp::OnPasteCompleted()
  246. {
  247. // the list only changes if UpdateTimeOnPaste is true (updated time)
  248. if( g_Opt.m_bUpdateTimeOnPaste )
  249. RefreshView();
  250. }
  251. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  252. {
  253. if( count <= 0 )
  254. return;
  255. // queue a message to RemoveOldEntries
  256. Delayed_RemoveOldEntries( 60000 );
  257. // update copy statistics
  258. CGetSetOptions::SetTripCopyCount( -count );
  259. CGetSetOptions::SetTotalCopyCount( -count );
  260. RefreshView();
  261. ShowCopyProperties( lLastID );
  262. }
  263. void CCP_MainApp::SetStatus( const char* status, bool bRepaintImmediately )
  264. {
  265. m_Status = status;
  266. if( QPasteWnd() )
  267. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  268. }
  269. void CCP_MainApp::ShowPersistent( bool bVal )
  270. {
  271. g_Opt.SetShowPersistent( bVal );
  272. // give some visual indication
  273. if( m_bShowingQuickPaste )
  274. {
  275. ASSERT( QPasteWnd() );
  276. QPasteWnd()->SetCaptionColorActive( !g_Opt.m_bShowPersistent );
  277. QPasteWnd()->RefreshNc();
  278. }
  279. }
  280. void CCP_MainApp::ShowCopyProperties( long lID )
  281. {
  282. if( m_bShowCopyProperties && lID > 0 )
  283. {
  284. HWND hWndFocus = ::GetForegroundWindow();
  285. m_bShowCopyProperties = false;
  286. ::SendMessage(m_MainhWnd, WM_COPYPROPERTIES, lID, 0); // modal
  287. ::SetForegroundWindow(hWndFocus);
  288. }
  289. }
  290. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  291. {
  292. if( !m_bRemoveOldEntriesPending )
  293. {
  294. m_bRemoveOldEntriesPending = true;
  295. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  296. }
  297. }
  298. /////////////////////////////////////////////////////////////////////////////
  299. // CCP_MainApp message handlers
  300. int CCP_MainApp::ExitInstance()
  301. {
  302. LOG("ExitInstance");
  303. if(CGetSetOptions::GetCompactAndRepairOnExit())
  304. CompactDatabase();
  305. CloseDB();
  306. return CWinApp::ExitInstance();
  307. }
  308. CDaoDatabase* CCP_MainApp::EnsureOpenDB(CString csName)
  309. {
  310. if(!m_pDatabase)
  311. m_pDatabase = new CDaoDatabase;
  312. if(!m_pDatabase->IsOpen())
  313. {
  314. if(csName == "")
  315. m_pDatabase->Open(GetDBName());
  316. else
  317. m_pDatabase->Open(csName);
  318. }
  319. if(m_pMainWnd)
  320. ((CMainFrame *)m_pMainWnd)->ResetKillDBTimer();
  321. return m_pDatabase;
  322. }
  323. BOOL CCP_MainApp::CloseDB()
  324. {
  325. if(m_pDatabase)
  326. {
  327. if(m_pDatabase->IsOpen())
  328. m_pDatabase->Close();
  329. delete m_pDatabase;
  330. m_pDatabase = NULL;
  331. }
  332. return TRUE;
  333. }
  334. // return TRUE if there is more idle processing to do
  335. BOOL CCP_MainApp::OnIdle(LONG lCount)
  336. {
  337. // let winapp handle its idle processing
  338. if( CWinApp::OnIdle(lCount) )
  339. return TRUE;
  340. return FALSE;
  341. }