CP_Main.cpp 8.7 KB

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