📄 jeditinit.cpp
字号:
/* * jeditinit.cpp - part of jEditLauncher package * Copyright (C) 2001 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.10 2001/08/28 19:37:01 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 <stdio.h> // special build#include <io.h>#include <assert.h>// status codes for installation status#define NOT_RUN 0#define INSTALL_ERROR -1#define INSTALL_OK 1#define INSTALL_REBOOT 2// function declarationsvoid ErrorMessage(LPCTSTR szMessage);bool IsInstalled();bool TryInstall();int Install(const char* szJavaHome);int Unregister();int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);TCHAR *szApp = _T("jEdit 3.2");TCHAR *szVersion = _T("Software\\www.jedit.org\\jEditLauncher\\3.2");TCHAR *szCOMClassName = _T("JEdit.JEditLauncher.3.2");// end version-specific// dummy mapBEGIN_OBJECT_MAP(ObjectMap)END_OBJECT_MAP()CComModule _Module;//#define SPECIAL_BUILDint 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; } // initialize ATL _Module.Init(NULL, GetModuleHandle(NULL)); // this code uses common controls, so we need to initialize them InitCommonControls();#if defined SPECIAL_BUILD // special build debug code char szFile[MAX_PATH]; GetModuleFileName(NULL, szFile, MAX_PATH); char *pSlash = strrchr(szFile, '\\'); strcpy(pSlash + 1, "jedebug.log"); FILE *fp = fopen(szFile, "at"); fseek(fp, 0, SEEK_END); fprintf(fp, "[jedit.exe] Calling jedit.exe module.\n"); char **ppToken = __argv; while(*ppToken != 0) { fprintf(fp, "[jedit.exe] full command line token: %s\n", *ppToken); ++ppToken; }#endif // check for existing installation bool bInstalled = IsInstalled(); int nReturnCode = -1; bool bShowUsage = true; // command line parsing ++ __argv; -- __argc; // 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(!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(); return nReturnCode; } // implicit else: argc either 0 or number of files // in filelist; invoke jEditLauncher IJEditLauncher *piJEdit = 0; HRESULT hr = CoCreateInstance(CLSID_JEditLauncher32, 0 , CLSCTX_LOCAL_SERVER, IID_IJEditLauncher, (void**)&piJEdit); if(hr != S_OK) { ErrorMessage(_T("Could not create JEditLauncher object")); } else { if(__argc == 0) {#if defined SPECIAL_BUILDfclose(fp);#endif hr = piJEdit->Launch(); } // make a variant array to allow for marshalling of // 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); CHAR fullPath[MAX_PATH]; CHAR *pFileName; BSTR bstrFile = 0; WCHAR wszFile[MAX_PATH]; wszFile[0] = 0; UINT nCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;#if defined SPECIAL_BUILDfprintf(fp, "[jedit.exe] Code page for file I/O is %s default.\n", nCodePage == CP_ACP ? "ANSI" : "OEM");#endif BOOL bSuccess; // 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 defined SPECIAL_BUILDfprintf(fp, "[jedit.exe] WCHAR conversion %s.\n", bSuccess ? "succeeded" : "did not succeed");fprintf(fp, "[jedit.exe] Converted file name is %S.\n", wszFile);fprintf(fp, "[jexit.exe] Calling launcher module.\n");fclose(fp);#endif 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);#if defined SPECIAL_BUILDfprintf(fp, "[jedit.exe] WCHAR conversion %s.\n", bSuccess ? "succeeded" : "did not succeed");fprintf(fp, "[jedit.exe] Converted file name is %S.\n", wszFile);#endif bstrFile = ::SysAllocString(wszFile); var.vt = VT_BSTR; var.bstrVal = bstrFile; SafeArrayPutElement(psa, ix, (void*)&var); VariantClear(&var); SysFreeString(bstrFile); }#if defined SPECIAL_BUILDfprintf(fp, "[jedit.exe] Calling launcher module.\n");fclose(fp);#endif 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();#if defined SPECIAL_BUILD fclose(fp);#endif return hr;}void ErrorMessage(LPCTSTR szMessage){ ::MessageBox(0, szMessage, szApp, MB_ICONERROR);}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; int nReturnValue = (func)(szJavaHome); 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; 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; // 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; // 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; } bool bFound = false; char szJavaDir[MAX_PATH]; 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 = szJavaDir; while(!bFound) { LPITEMIDLIST pIDList = SHBrowseForFolder(&brinfo); if(pIDList == 0) return false; SHGetPathFromIDList(pIDList, szJavaDir); IMalloc *piMalloc = 0; if(SUCCEEDED(SHGetMalloc(&piMalloc))) { piMalloc->Free(pIDList); piMalloc->Release(); } CString strJavaExec(szJavaDir); strJavaExec += _T("\\javaw.exe"); if(GetFileAttributes(strJavaExec) != -1) { bFound = true; } else { CString strMsg; strMsg.Format(IDS_ERR_JAVA_NOT_FOUND, szJavaDir); 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 (INSTALL_ERROR != Install(szJavaDir));}// check if this version of jEditLauncher is installed// 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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -