?? jeditinit.cpp
字號:
/* * jeditinit.cpp - part of jEditLauncher package * Copyright (C) 2001, 2002 John Gellene * jgellene@nyc.rr.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * Notwithstanding the terms of the General Public License, the author grants * permission to compile and link object code generated by the compilation of * this program with object code and libraries that are not subject to the * GNU General Public License, provided that the executable output of such * compilation shall be distributed with source code on substantially the * same basis as the jEditLauncher package of which this program is a part. * By way of example, a distribution would satisfy this condition if it * included a working makefile for any freely available make utility that * runs on the Windows family of operating systems. This condition does not * require a licensee of this software to distribute any proprietary software * (including header files and libraries) that is licensed under terms * prohibiting redistribution to third parties. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: jeditinit.cpp,v 1.14 2002/03/24 21:09:00 jgellene Exp $ */// jeditinit.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "jeditinitDlg.h"#include "..\jeditlauncher\jeditlauncher_i.c"#include "..\jeditlauncher\jeditlauncher.h"#include "..\ltslog\ltslog.h" // logging module// NOTE: status codes for installation status#define NOT_RUN 0#define INSTALL_ERROR -1#define INSTALL_OK 1#define INSTALL_REBOOT 2// NOTE: function declarationsvoid ErrorMessage(LPCTSTR szMessage);bool IsInstalled();bool TryInstall();int Install(const char* szJavaHome);bool GetJavaDir(char *lpszDir);int Unregister();int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);void CloseLog();TCHAR *szApp = _T("jEdit 4.0");TCHAR *szVersion = _T("Software\\www.jedit.org\\jEditLauncher\\4.0");TCHAR *szCOMClassName = _T("JEdit.JEditLauncher.4.0");// NOTE: end version-specific// NOTE: logging module parametersHMODULE hLogModule;LogFunc logProc;CLtslog* pLog;// NOTE: dummy mapBEGIN_OBJECT_MAP(ObjectMap)END_OBJECT_MAP()CComModule _Module;int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ /* * NOTE: __argc and __argv contain parsed command line */ HRESULT hrCOM = CoInitialize(0); if(FAILED(hrCOM)) { ErrorMessage(_T("Application failure: could not initialize COM facilities.")); return 1; } // NOTE: initialize ATL _Module.Init(NULL, GetModuleHandle(NULL)); // NOTE: this code uses common controls, so we need to initialize them InitCommonControls(); // NOTE: initialize logging module pLog = 0; logProc = 0; hLogModule = LoadLibrary("ltslog.dll"); if(hLogModule != 0) { LogOpenFunc openProc = (LogOpenFunc)GetProcAddress(hLogModule, "OpenLog"); logProc = (LogFunc)GetProcAddress(hLogModule, "Log"); if(openProc != 0 && logProc != 0) { pLog = (openProc)("jelaunch.log", Debug); } } if(logProc) { (logProc)(pLog, true, Message, "[jedit.exe] Calling jedit.exe module.\n"); char **ppToken = __argv; while(*ppToken != 0) { (logProc)(pLog, true, Debug, "[jedit.exe] full command line token: %s\n", *ppToken); ++ppToken; } } // NOTE: check for existing installation bool bInstalled = IsInstalled(); int nReturnCode = -1; bool bShowUsage = true; bool bSingleFile = false; // NOTE: command line parsing ++ __argv; -- __argc; // NOTE: install option takes precedence if(__argc != 0 && __argc < 3 && lstrcmp(*__argv, _T("/i")) == 0) { char *szParam = (__argc == 2 ? *(__argv + 1) : 0); bInstalled = Install(szParam) ? true : false; bShowUsage = false; nReturnCode = !bInstalled; } else if(__argc == 1 && **__argv == _T('/')) { if(lstrlen(*__argv) > 2) nReturnCode = 1; else if(*(*__argv + 1) == _T('h')) nReturnCode = 0; else if(*(*__argv + 1) == _T('p')) { bShowUsage = false; if(bInstalled || TryInstall()) { CJeditinitDlg dlg; dlg.DoModal(); nReturnCode = 0; } else { nReturnCode = 1; } } else if(*(*__argv + 1) == _T('u')) { bShowUsage = false; Unregister(); nReturnCode = 0; } else if(*(*__argv + 1) == _T('1')) { nReturnCode = 1; } } else if(__argc > 1 && **__argv == _T('/') && *(*__argv + 1) == _T('1')) { bSingleFile = true; if(logProc) { (logProc)(pLog, true, Debug, "[jexit.exe] Calling launcher in single file mode.\n"); } } else if(!bInstalled && !TryInstall()) { bShowUsage = false; nReturnCode = 1; } if(nReturnCode != -1) { if(bShowUsage) { CSimpleDialog<IDD_ABOUTBOX, TRUE> dlg; dlg.DoModal(); } if(S_OK == hrCOM) CoUninitialize(); _Module.Term(); CloseLog(); return nReturnCode; } // NOTE: implicit else: argc either 0 or number of files // NOTE: in filelist; invoke jEditLauncher IJEditLauncher *piJEdit = 0; HRESULT hr = CoCreateInstance(CLSID_JEditLauncher40, 0 , CLSCTX_LOCAL_SERVER, IID_IJEditLauncher, (void**)&piJEdit); if(hr != S_OK) { ErrorMessage(_T("Could not create JEditLauncher object")); } else { BOOL bSuccess = FALSE; CHAR fullPath[MAX_PATH]; CHAR *pFileName; BSTR bstrFile = 0; WCHAR wszFile[MAX_PATH]; wszFile[0] = 0; UINT nCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; if(__argc == 0) { hr = piJEdit->Launch(); } // NOTE: get single path name from command line else if(bSingleFile) { LPTSTR pCmdLine = GetCommandLine(); if(logProc) { (logProc)(pLog, true, Debug, "[jexit.exe] Command line is %s\n", pCmdLine); } bool bSlash = false; bool bFound = false; for(TCHAR *p = pCmdLine; *p != 0; p = CharNext(p)) { if(bFound) { if(!_istspace(*p)) break; } else { if(*p == _T('1')) { if(bSlash) { bFound = true; continue; } } bSlash = (*p == _T('/')); } } if(*p != 0) { GetFullPathName(p, MAX_PATH, fullPath, &pFileName); bSuccess = MultiByteToWideChar(nCodePage, 0, fullPath, -1, wszFile, MAX_PATH); if(logProc) { (logProc)(pLog, true, Debug, "[jexit.exe] Calling launcher module on %S.\n", wszFile); } bstrFile = SysAllocString(wszFile); hr = piJEdit->OpenFile(bstrFile); SysFreeString(bstrFile); } else if(logProc) { (logProc)(pLog, true, Debug, "[jedit.exe] Could not find single file mode parameter.\n"); } } // NOTE: make a variant array to allow for marshalling of // NOTE: file name parameters across process boundaries else { SAFEARRAY *psa = 0; SAFEARRAYBOUND rgsabound[1]; rgsabound[0].lLbound = 0; rgsabound[0].cElements = __argc; psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound); if(logProc) (logProc)(pLog, true, Debug, "[jedit.exe] Code page for file I/O is %s default.\n", nCodePage == CP_ACP ? "ANSI" : "OEM"); BOOL bSuccess; // NOTE: if we can't create the array, grab the first file only if( psa == 0) { GetFullPathName(*__argv, MAX_PATH, fullPath, &pFileName); bSuccess = MultiByteToWideChar(nCodePage, 0, fullPath, -1, wszFile, MAX_PATH); if(logProc) { (logProc)(pLog, true, Debug, "[jexit.exe] Calling launcher module on %S.\n", wszFile); } bstrFile = SysAllocString(wszFile); hr = piJEdit->OpenFile(bstrFile); SysFreeString(bstrFile); } else { long ix[1]; long& i = *ix; VARIANT var; VariantInit(&var); for(i = 0L; i < (long)__argc; ++i) { GetFullPathName(*__argv++, MAX_PATH, fullPath, &pFileName); bSuccess = MultiByteToWideChar(nCodePage, 0, fullPath, -1, wszFile, MAX_PATH); bstrFile = ::SysAllocString(wszFile); var.vt = VT_BSTR; var.bstrVal = bstrFile; SafeArrayPutElement(psa, ix, (void*)&var); VariantClear(&var); SysFreeString(bstrFile); } if(logProc) { (logProc)(pLog, true, Debug, "[jedit.exe] Calling launcher module.\n"); } var.vt = (VT_ARRAY | VT_VARIANT); var.parray = psa; hr = piJEdit->OpenFiles(var); VariantClear(&var); } } if(hr != S_OK) { CString strError; IErrorInfo *pErr = 0; hr = GetErrorInfo(0L, &pErr); if(hr != S_OK) { strError.Format("Unknown error %s", __argc != 0 ? "opening files" : "launching jEdit"); } else { BSTR bstrSource, bstrDescription; pErr->GetSource(&bstrSource); pErr->GetDescription(&bstrDescription); pErr->Release(); CHAR pszSource[32]; CHAR pszDescription[255]; WideCharToMultiByte(CP_ACP, 0, bstrSource, -1, pszSource, 32, 0, 0); WideCharToMultiByte(CP_ACP, 0, bstrDescription, -1, pszDescription, 255, 0, 0); ::SysFreeString(bstrSource); ::SysFreeString(bstrDescription); strError.Format("Error %s\n%s - %s\n", __argc != 0 ? "opening files:" : "launching jEdit:", pszSource, pszDescription); } ErrorMessage(strError); } piJEdit->Release(); } if(S_OK == hrCOM) CoUninitialize(); _Module.Term(); CloseLog(); return hr;}void ErrorMessage(LPCTSTR szMessage){ ::MessageBox(0, szMessage, szApp, MB_ICONERROR); if(logProc) (logProc)(pLog, true, Error, "[jedit.exe] %s\n", szMessage);}typedef int(*InstallFunc)(LPCSTR);typedef int(*UninstallFunc)();int Install(const char* szJavaHome){ HMODULE hModule = LoadLibrary("jedinstl.dll"); if(hModule == 0) return INSTALL_ERROR; InstallFunc func = (InstallFunc)GetProcAddress(hModule, "Install"); if(func == 0) return INSTALL_ERROR; if(szJavaHome == 0 || *szJavaHome == 0) { char szJavaDir[MAX_PATH]; if(!GetJavaDir(szJavaDir)) return INSTALL_ERROR; szJavaHome = szJavaDir; } if(logProc) (logProc)(pLog, true, Message, "[jedit.exe] Calling installation module.\n"); int nReturnValue = (func)(szJavaHome); if(logProc) (logProc)(pLog, true, Message, "[jedit.exe] Installation module returns %d.\n", nReturnValue); FreeLibrary(hModule); return nReturnValue;}int Unregister(){ HMODULE hModule = LoadLibrary("jedinstl.dll"); if(hModule == 0) return INSTALL_ERROR; UninstallFunc func = (UninstallFunc)GetProcAddress(hModule, "Unregister"); if(func == 0) return INSTALL_ERROR; if(logProc) (logProc)(pLog, true, Message, "[jedit.exe] Calling uninstall module to delete registration entries only.\n"); int nReturnValue = (func)(); FreeLibrary(hModule); return nReturnValue;}int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData){ lParam, lpData; if(uMsg == BFFM_INITIALIZED) { SetWindowText(hwnd, "jEditLauncher Installation"); RECT rcDesktop, rcDlg; ::GetWindowRect(hwnd, &rcDlg); ::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcDesktop, NULL); int dlgWidth = rcDlg.right - rcDlg.left; int dlgHeight = rcDlg.bottom - rcDlg.top; // Note: find dialog's upper left based on rcDesktop int xLeft = (rcDesktop.left + rcDesktop.right) / 2 - dlgWidth / 2; int yTop = (rcDesktop.top + rcDesktop.bottom) / 2 - dlgHeight / 2; // NOTE: map screen coordinates to child coordinates ::SetWindowPos(hwnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); } return 0;}bool TryInstall(){ if(IDYES != MessageBox(0, CString((LPCSTR)IDS_MSG_INSTALLQUERY), szApp, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON1)) { ::MessageBox(0, CString((LPCSTR)IDS_ERR_NEEDINSTALL), szApp, MB_ICONEXCLAMATION); return false; } char szJavaDir[MAX_PATH]; return GetJavaDir(szJavaDir) && (INSTALL_ERROR != Install(szJavaDir));}bool GetJavaDir(char *lpszDir){ bool bFound = false; BROWSEINFO brinfo; ZeroMemory(&brinfo, sizeof(BROWSEINFO)); brinfo.hwndOwner = GetDesktopWindow(); brinfo.lpfn = BrowseCallbackProc; brinfo.lpszTitle = _T("Select folder with location of Java executable"); brinfo.ulFlags = BIF_RETURNONLYFSDIRS; brinfo.pszDisplayName = lpszDir; while(!bFound) { LPITEMIDLIST pIDList = SHBrowseForFolder(&brinfo); if(pIDList == 0) return false; SHGetPathFromIDList(pIDList, lpszDir); IMalloc *piMalloc = 0; if(SUCCEEDED(SHGetMalloc(&piMalloc))) { piMalloc->Free(pIDList); piMalloc->Release(); } CString strJavaExec(lpszDir); strJavaExec += _T("\\javaw.exe"); if(GetFileAttributes(strJavaExec) != -1) { bFound = true; } else { CString strMsg; strMsg.Format(IDS_ERR_JAVA_NOT_FOUND, lpszDir); if(IDYES != MessageBox(0, strMsg, szApp, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1)) { ::MessageBox(0, CString((LPCSTR)IDS_ERR_NEEDINSTALL), szApp, MB_ICONEXCLAMATION); return false; } } } return bFound;}// NOTE: check if this version of jEditLauncher is installed// NOTE: checks both class registration and command line parameteresbool IsInstalled(){ HKEY hKey; int nResult = RegOpenKeyEx(HKEY_CLASSES_ROOT, szCOMClassName, 0, KEY_READ, &hKey); RegCloseKey(hKey); if(nResult != ERROR_SUCCESS) return false; nResult = RegOpenKeyEx(HKEY_CURRENT_USER, szVersion, 0, KEY_READ, &hKey); RegCloseKey(hKey); return (nResult == ERROR_SUCCESS);}void CloseLog(){ if(pLog != 0) { LogCloseFunc closeProc = (LogCloseFunc)GetProcAddress(hLogModule, "CloseLog"); if(closeProc != 0) (closeProc)(pLog); pLog = 0; } if(hLogModule) FreeLibrary(hLogModule); hLogModule = 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -