Notepad2.c
资源名称:np2src.zip [点击查看]
上传用户:tj_dwf
上传日期:2020-11-17
资源大小:215k
文件大小:211k
源码类别:
RichEdit
开发平台:
Visual C++
- }
- else {
- bShowToolbar = 1;
- UpdateToolbar();
- ShowWindow(hwndReBar,SW_SHOW);
- }
- SendWMSize(hwnd);
- break;
- case IDM_VIEW_CUSTOMIZETB:
- SendMessage(hwndToolbar,TB_CUSTOMIZE,0,0);
- break;
- case IDM_VIEW_STATUSBAR:
- if (bShowStatusbar) {
- bShowStatusbar = 0;
- ShowWindow(hwndStatus,SW_HIDE);
- }
- else {
- bShowStatusbar = 1;
- UpdateStatusbar();
- ShowWindow(hwndStatus,SW_SHOW);
- }
- SendWMSize(hwnd);
- break;
- case IDM_VIEW_STICKYWINPOS:
- bStickyWinPos = IniGetInt(L"Settings2",L"StickyWindowPosition",bStickyWinPos);
- if (!bStickyWinPos) {
- WINDOWPLACEMENT wndpl;
- WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
- int ResX = GetSystemMetrics(SM_CXSCREEN);
- int ResY = GetSystemMetrics(SM_CYSCREEN);
- // GetWindowPlacement
- wndpl.length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement(hwndMain,&wndpl);
- wi.x = wndpl.rcNormalPosition.left;
- wi.y = wndpl.rcNormalPosition.top;
- wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
- wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
- wi.max = (IsZoomed(hwndMain) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
- wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
- wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
- wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
- wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
- wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
- bStickyWinPos = 1;
- IniSetInt(L"Settings2",L"StickyWindowPosition",1);
- IniSetInt(L"Window",tchPosX,wi.x);
- IniSetInt(L"Window",tchPosY,wi.y);
- IniSetInt(L"Window",tchSizeX,wi.cx);
- IniSetInt(L"Window",tchSizeY,wi.cy);
- IniSetInt(L"Window",tchMaximized,wi.max);
- InfoBox(0,L"MsgStickyWinPos",IDS_STICKYWINPOS);
- }
- else {
- bStickyWinPos = 0;
- IniSetInt(L"Settings2",L"StickyWindowPosition",0);
- }
- break;
- case IDM_VIEW_REUSEWINDOW:
- if (IniGetInt(L"Settings2",L"ReuseWindow",0))
- IniSetInt(L"Settings2",L"ReuseWindow",0);
- else
- IniSetInt(L"Settings2",L"ReuseWindow",1);
- break;
- case IDM_VIEW_SINGLEFILEINSTANCE:
- if (IniGetInt(L"Settings2",L"SingleFileInstance",0))
- IniSetInt(L"Settings2",L"SingleFileInstance",0);
- else
- IniSetInt(L"Settings2",L"SingleFileInstance",1);
- break;
- case IDM_VIEW_ALWAYSONTOP:
- if (bAlwaysOnTop) {
- bAlwaysOnTop = 0;
- SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
- }
- else {
- bAlwaysOnTop = 1;
- SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
- }
- break;
- case IDM_VIEW_MINTOTRAY:
- bMinimizeToTray =(bMinimizeToTray) ? FALSE : TRUE;
- break;
- case IDM_VIEW_TRANSPARENT:
- bTransparentMode =(bTransparentMode) ? FALSE : TRUE;
- SetWindowTransparentMode(hwnd,bTransparentMode);
- break;
- case IDM_VIEW_SHOWFILENAMEONLY:
- iPathNameFormat = 0;
- lstrcpy(szTitleExcerpt,L"");
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case IDM_VIEW_SHOWFILENAMEFIRST:
- iPathNameFormat = 1;
- lstrcpy(szTitleExcerpt,L"");
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case IDM_VIEW_SHOWFULLPATH:
- iPathNameFormat = 2;
- lstrcpy(szTitleExcerpt,L"");
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case IDM_VIEW_SHOWEXCERPT:
- EditGetExcerpt(hwndEdit,szTitleExcerpt,COUNTOF(szTitleExcerpt));
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case IDM_VIEW_NOSAVERECENT:
- bSaveRecentFiles = (bSaveRecentFiles) ? FALSE : TRUE;
- break;
- case IDM_VIEW_NOSAVEFINDREPL:
- bSaveFindReplace = (bSaveFindReplace) ? FALSE : TRUE;
- break;
- case IDM_VIEW_SAVEBEFORERUNNINGTOOLS:
- bSaveBeforeRunningTools = (bSaveBeforeRunningTools) ? FALSE : TRUE;
- break;
- case IDM_VIEW_CHANGENOTIFY:
- if (ChangeNotifyDlg(hwnd))
- InstallFileWatching(szCurFile);
- break;
- case IDM_VIEW_NOESCFUNC:
- iEscFunction = 0;
- break;
- case IDM_VIEW_ESCMINIMIZE:
- iEscFunction = 1;
- break;
- case IDM_VIEW_ESCEXIT:
- iEscFunction = 2;
- break;
- case IDM_VIEW_SAVESETTINGS:
- bSaveSettings = (bSaveSettings) ? FALSE : TRUE;
- break;
- case IDM_VIEW_SAVESETTINGSNOW:
- {
- BOOL bCreateFailure = FALSE;
- if (lstrlen(szIniFile) == 0) {
- if (lstrlen(szIniFile2) > 0) {
- if (CreateIniFileEx(szIniFile2)) {
- lstrcpy(szIniFile,szIniFile2);
- lstrcpy(szIniFile2,L"");
- }
- else
- bCreateFailure = TRUE;
- }
- else
- break;
- }
- if (!bCreateFailure) {
- if (WritePrivateProfileString(L"Settings",L"WriteTest",L"ok",szIniFile)) {
- BeginWaitCursor();
- StatusSetTextID(hwndStatus,STATUS_HELP,IDS_SAVINGSETTINGS);
- StatusSetSimple(hwndStatus,TRUE);
- InvalidateRect(hwndStatus,NULL,TRUE);
- UpdateWindow(hwndStatus);
- SaveSettings(TRUE);
- StatusSetSimple(hwndStatus,FALSE);
- EndWaitCursor();
- MsgBox(MBINFO,IDS_SAVEDSETTINGS);
- }
- else {
- dwLastIOError = GetLastError();
- MsgBox(MBINFO,IDS_WRITEINI_FAIL);
- }
- }
- else
- MsgBox(MBINFO,IDS_CREATEINI_FAIL);
- }
- break;
- case IDM_HELP_ABOUT:
- ThemedDialogBox(g_hInstance,MAKEINTRESOURCE(IDD_ABOUT),
- hwnd,AboutDlgProc);
- break;
- case CMD_ESCAPE:
- if (iEscFunction == 1)
- SendMessage(hwnd,WM_SYSCOMMAND,SC_MINIMIZE,0);
- else if (iEscFunction == 2)
- SendMessage(hwnd,WM_CLOSE,0,0);
- break;
- case CMD_SHIFTESC:
- if (FileSave(TRUE,FALSE,FALSE,FALSE))
- SendMessage(hwnd,WM_CLOSE,0,0);
- break;
- // Newline with toggled auto indent setting
- case CMD_CTRLENTER:
- bAutoIndent = (bAutoIndent) ? 0 : 1;
- SendMessage(hwndEdit,SCI_NEWLINE,0,0);
- bAutoIndent = (bAutoIndent) ? 0 : 1;
- break;
- case CMD_CTRLBACK:
- {
- int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- int iAnchor = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
- int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iPos,0);
- int iStartPos = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
- int iEndPos = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
- int iIndentPos = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,(WPARAM)iLine,0);
- if (iPos != iAnchor)
- SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos,(LPARAM)iPos);
- else {
- if (iPos == iStartPos)
- SendMessage(hwndEdit,SCI_DELETEBACK,0,0);
- else if (iPos <= iIndentPos)
- SendMessage(hwndEdit,SCI_DELLINELEFT,0,0);
- else
- SendMessage(hwndEdit,SCI_DELWORDLEFT,0,0);
- }
- }
- break;
- case CMD_CTRLDEL:
- {
- int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- int iAnchor = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
- int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iPos,0);
- int iStartPos = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
- int iEndPos = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
- if (iPos != iAnchor)
- SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos,(LPARAM)iPos);
- else {
- if (iStartPos != iEndPos)
- SendMessage(hwndEdit,SCI_DELWORDRIGHT,0,0);
- else // iStartPos == iEndPos
- SendMessage(hwndEdit,SCI_LINEDELETE,0,0);
- }
- }
- break;
- case CMD_CTRLTAB:
- SendMessage(hwndEdit,SCI_SETTABINDENTS,FALSE,0);
- SendMessage(hwndEdit,SCI_SETUSETABS,TRUE,0);
- SendMessage(hwndEdit,SCI_TAB,0,0);
- SendMessage(hwndEdit,SCI_SETUSETABS,!bTabsAsSpaces,0);
- SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
- break;
- case CMD_RELOADANSI:
- {
- WCHAR tchCurFile2[MAX_PATH];
- if (lstrlen(szCurFile)) {
- iSrcEncoding = CPI_DEFAULT;
- lstrcpy(tchCurFile2,szCurFile);
- FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
- }
- }
- break;
- case CMD_RELOADOEM:
- {
- WCHAR tchCurFile2[MAX_PATH];
- if (lstrlen(szCurFile)) {
- iSrcEncoding = CPI_OEM;
- lstrcpy(tchCurFile2,szCurFile);
- FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
- }
- }
- break;
- case CMD_RELOADASCIIASUTF8:
- {
- WCHAR tchCurFile2[MAX_PATH];
- BOOL _bLoadASCIIasUTF8 = bLoadASCIIasUTF8;
- if (lstrlen(szCurFile)) {
- bLoadASCIIasUTF8 = 1;
- lstrcpy(tchCurFile2,szCurFile);
- FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
- bLoadASCIIasUTF8 = _bLoadASCIIasUTF8;
- }
- }
- break;
- case CMD_RELOADNOFILEVARS:
- {
- WCHAR tchCurFile2[MAX_PATH];
- if (lstrlen(szCurFile)) {
- int _fNoFileVariables = fNoFileVariables;
- BOOL _bNoEncodingTags = bNoEncodingTags;
- fNoFileVariables = 1;
- bNoEncodingTags = 1;
- lstrcpy(tchCurFile2,szCurFile);
- FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
- fNoFileVariables = _fNoFileVariables;
- bNoEncodingTags = _bNoEncodingTags;
- }
- }
- break;
- case CMD_LEXDEFAULT:
- Style_SetDefaultLexer(hwndEdit);
- UpdateStatusbar();
- UpdateLineNumberWidth();
- break;
- case CMD_LEXHTML:
- Style_SetHTMLLexer(hwndEdit);
- UpdateStatusbar();
- UpdateLineNumberWidth();
- break;
- case CMD_LEXXML:
- Style_SetXMLLexer(hwndEdit);
- UpdateStatusbar();
- UpdateLineNumberWidth();
- break;
- case CMD_TIMESTAMPS:
- {
- WCHAR wchFind[256] = {0};
- WCHAR wchTemplate[256] = {0};
- WCHAR *pwchSep;
- WCHAR wchReplace[256];
- SYSTEMTIME st;
- struct tm sst;
- UINT cp;
- EDITFINDREPLACE efrTS = { "", "", "", "", SCFIND_REGEXP, 0, 0, 0, 0, 0, hwndEdit };
- IniGetString(L"Settings2",L"TimeStamp",L"\$Date:[^\$]+\$ | $Date: %Y/%m/%d %H:%M:%S $",wchFind,COUNTOF(wchFind));
- if (pwchSep = StrChr(wchFind,L'|')) {
- lstrcpy(wchTemplate,pwchSep+1);
- *pwchSep = 0;
- }
- StrTrim(wchFind,L" ");
- StrTrim(wchTemplate,L" ");
- if (lstrlen(wchFind) == 0 || lstrlen(wchTemplate) == 0)
- break;
- GetLocalTime(&st);
- sst.tm_isdst = -1;
- sst.tm_sec = (int)st.wSecond;
- sst.tm_min = (int)st.wMinute;
- sst.tm_hour = (int)st.wHour;
- sst.tm_mday = (int)st.wDay;
- sst.tm_mon = (int)st.wMonth - 1;
- sst.tm_year = (int)st.wYear - 1900;
- sst.tm_wday = (int)st.wDayOfWeek;
- mktime(&sst);
- wcsftime(wchReplace,COUNTOF(wchReplace),wchTemplate,&sst);
- cp = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
- WideCharToMultiByte(cp,0,wchFind,-1,efrTS.szFind,COUNTOF(efrTS.szFind),NULL,NULL);
- WideCharToMultiByte(cp,0,wchReplace,-1,efrTS.szReplace,COUNTOF(efrTS.szReplace),NULL,NULL);
- if (SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0) !=
- SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0))
- EditReplaceAllInSelection(hwndEdit,&efrTS,TRUE);
- else
- EditReplaceAll(hwndEdit,&efrTS,TRUE);
- }
- break;
- case CMD_WEBACTION1:
- case CMD_WEBACTION2:
- {
- BOOL bCmdEnabled = FALSE;
- LPWSTR lpszTemplateName;
- WCHAR szCmdTemplate[256];
- char mszSelection[512] = { 0 };
- DWORD cchSelection;
- char *lpsz;
- LPWSTR lpszCommand;
- LPWSTR lpszArgs;
- SHELLEXECUTEINFO sei;
- WCHAR wchDirectory[MAX_PATH] = L"";
- lpszTemplateName = (LOWORD(wParam) == CMD_WEBACTION1) ? L"WebTemplate1" : L"WebTemplate2";
- bCmdEnabled = IniGetString(L"Settings2",lpszTemplateName,L"",szCmdTemplate,COUNTOF(szCmdTemplate));
- if (bCmdEnabled) {
- cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
- SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
- if (cchSelection > 0 && cchSelection <= 500 && SendMessage(hwndEdit,SCI_GETSELTEXT,0,0) < COUNTOF(mszSelection))
- {
- SendMessage(hwndEdit,SCI_GETSELTEXT,0,(LPARAM)mszSelection);
- mszSelection[cchSelection] = 0; // zero terminate
- // Check lpszSelection and truncate bad WCHARs
- lpsz = StrChrA(mszSelection,13);
- if (lpsz) *lpsz = ' ';
- lpsz = StrChrA(mszSelection,10);
- if (lpsz) *lpsz = ' ';
- lpsz = StrChrA(mszSelection,9);
- if (lpsz) *lpsz = ' ';
- if (lstrlenA(mszSelection)) {
- WCHAR wszSelection[512];
- UINT uCP = (SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
- MultiByteToWideChar(uCP,0,mszSelection,-1,wszSelection,COUNTOF(wszSelection));
- lpszCommand = GlobalAlloc(GPTR,sizeof(WCHAR)*(512+COUNTOF(szCmdTemplate)+MAX_PATH+32));
- wsprintf(lpszCommand,szCmdTemplate,wszSelection);
- ExpandEnvironmentStringsEx(lpszCommand,GlobalSize(lpszCommand)/sizeof(WCHAR));
- lpszArgs = GlobalAlloc(GPTR,GlobalSize(lpszCommand));
- ExtractFirstArgument(lpszCommand,lpszCommand,lpszArgs);
- if (lstrlen(szCurFile)) {
- lstrcpy(wchDirectory,szCurFile);
- PathRemoveFileSpec(wchDirectory);
- }
- ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
- sei.cbSize = sizeof(SHELLEXECUTEINFO);
- sei.fMask = /*SEE_MASK_NOZONECHECKS*/0x00800000;
- sei.hwnd = NULL;
- sei.lpVerb = NULL;
- sei.lpFile = lpszCommand;
- sei.lpParameters = lpszArgs;
- sei.lpDirectory = wchDirectory;
- sei.nShow = SW_SHOWNORMAL;
- ShellExecuteEx(&sei);
- GlobalFree(lpszCommand);
- GlobalFree(lpszArgs);
- }
- }
- }
- }
- break;
- case CMD_FINDNEXTSEL:
- case CMD_FINDPREVSEL:
- case IDM_EDIT_SAVEFIND:
- {
- int cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
- SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
- if (cchSelection == 0)
- {
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_SELECTWORD,1),0);
- cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
- SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
- }
- if (cchSelection > 0 && cchSelection <= 500 && SendMessage(hwndEdit,SCI_GETSELTEXT,0,0) < 512)
- {
- char mszSelection[512];
- char *lpsz;
- SendMessage(hwndEdit,SCI_GETSELTEXT,0,(LPARAM)mszSelection);
- mszSelection[cchSelection] = 0; // zero terminate
- // Check lpszSelection and truncate newlines
- lpsz = StrChrA(mszSelection,'n');
- if (lpsz) *lpsz = ' ';
- lpsz = StrChrA(mszSelection,'r');
- if (lpsz) *lpsz = ' ';
- cpLastFind = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
- lstrcpyA(efrData.szFind,mszSelection);
- if (cpLastFind != SC_CP_UTF8)
- {
- WCHAR wszBuf[512];
- MultiByteToWideChar(cpLastFind,0,mszSelection,-1,wszBuf,COUNTOF(wszBuf));
- WideCharToMultiByte(CP_UTF8,0,wszBuf,-1,efrData.szFindUTF8,COUNTOF(efrData.szFindUTF8),NULL,NULL);
- }
- else
- lstrcpyA(efrData.szFindUTF8,mszSelection);
- efrData.fuFlags &= (~SCFIND_REGEXP);
- efrData.bTransformBS = FALSE;
- switch (LOWORD(wParam)) {
- case IDM_EDIT_SAVEFIND:
- break;
- case CMD_FINDNEXTSEL:
- EditFindNext(hwndEdit,&efrData,FALSE);
- break;
- case CMD_FINDPREVSEL:
- EditFindPrev(hwndEdit,&efrData,FALSE);
- break;
- }
- }
- }
- break;
- case CMD_INCLINELIMIT:
- case CMD_DECLINELIMIT:
- if (!bMarkLongLines)
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_LONGLINEMARKER,1),0);
- else {
- if (LOWORD(wParam) == CMD_INCLINELIMIT)
- iLongLinesLimit++;
- else
- iLongLinesLimit--;
- iLongLinesLimit = max(min(iLongLinesLimit,4096),0);
- SendMessage(hwndEdit,SCI_SETEDGECOLUMN,iLongLinesLimit,0);
- UpdateStatusbar();
- iLongLinesLimitG = iLongLinesLimit;
- }
- break;
- case CMD_STRINGIFY:
- EditEncloseSelection(hwndEdit,L"'",L"'");
- break;
- case CMD_STRINGIFY2:
- EditEncloseSelection(hwndEdit,L""",L""");
- break;
- case CMD_EMBRACE:
- EditEncloseSelection(hwndEdit,L"(",L")");
- break;
- case CMD_EMBRACE2:
- EditEncloseSelection(hwndEdit,L"[",L"]");
- break;
- case CMD_EMBRACE3:
- EditEncloseSelection(hwndEdit,L"{",L"}");
- break;
- case CMD_EMBRACE4:
- EditEncloseSelection(hwndEdit,L"`",L"`");
- break;
- case CMD_INCREASENUM:
- EditModifyNumber(hwndEdit,TRUE);
- break;
- case CMD_DECREASENUM:
- EditModifyNumber(hwndEdit,FALSE);
- break;
- case CMD_TOGGLETITLE:
- EditGetExcerpt(hwndEdit,szTitleExcerpt,COUNTOF(szTitleExcerpt));
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case CMD_JUMP2SELSTART:
- if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
- int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
- int iCursorPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- if (iCursorPos > iAnchorPos) {
- SendMessage(hwndEdit,SCI_SETSEL,iCursorPos,iAnchorPos);
- SendMessage(hwndEdit,SCI_CHOOSECARETX,0,0);
- }
- }
- break;
- case CMD_JUMP2SELEND:
- if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
- int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
- int iCursorPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- if (iCursorPos < iAnchorPos) {
- SendMessage(hwndEdit,SCI_SETSEL,iCursorPos,iAnchorPos);
- SendMessage(hwndEdit,SCI_CHOOSECARETX,0,0);
- }
- }
- break;
- case CMD_COPYPATHNAME: {
- WCHAR *pszCopy;
- WCHAR tchUntitled[32];
- if (lstrlen(szCurFile))
- pszCopy = szCurFile;
- else {
- GetString(IDS_UNTITLED,tchUntitled,COUNTOF(tchUntitled));
- pszCopy = tchUntitled;
- }
- if (OpenClipboard(hwnd)) {
- HANDLE hData;
- WCHAR *pData;
- EmptyClipboard();
- hData = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(WCHAR) * (lstrlen(pszCopy) + 1));
- pData = GlobalLock(hData);
- StrCpyN(pData,pszCopy,GlobalSize(hData) / sizeof(WCHAR));
- GlobalUnlock(hData);
- SetClipboardData(CF_UNICODETEXT,hData);
- CloseClipboard();
- }
- }
- break;
- case CMD_OPENINIFILE:
- if (lstrlen(szIniFile)) {
- CreateIniFile();
- FileLoad(FALSE,FALSE,FALSE,FALSE,szIniFile);
- }
- break;
- case IDT_FILE_NEW:
- if (IsCmdEnabled(hwnd,IDM_FILE_NEW))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_NEW,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_OPEN:
- if (IsCmdEnabled(hwnd,IDM_FILE_OPEN))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_OPEN,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_BROWSE:
- if (IsCmdEnabled(hwnd,IDM_FILE_BROWSE))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_BROWSE,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_SAVE:
- if (IsCmdEnabled(hwnd,IDM_FILE_SAVE))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVE,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_UNDO:
- if (IsCmdEnabled(hwnd,IDM_EDIT_UNDO))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_UNDO,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_REDO:
- if (IsCmdEnabled(hwnd,IDM_EDIT_REDO))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REDO,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_CUT:
- if (IsCmdEnabled(hwnd,IDM_EDIT_CUT))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_CUT,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_COPY:
- if (IsCmdEnabled(hwnd,IDM_EDIT_COPY))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_COPY,1),0);
- else
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_COPYALL,1),0);
- break;
- case IDT_EDIT_PASTE:
- if (IsCmdEnabled(hwnd,IDM_EDIT_PASTE))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_PASTE,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_FIND:
- if (IsCmdEnabled(hwnd,IDM_EDIT_FIND))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_FIND,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_REPLACE:
- if (IsCmdEnabled(hwnd,IDM_EDIT_REPLACE))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REPLACE,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_VIEW_WORDWRAP:
- if (IsCmdEnabled(hwnd,IDM_VIEW_WORDWRAP))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_WORDWRAP,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_VIEW_ZOOMIN:
- if (IsCmdEnabled(hwnd,IDM_VIEW_ZOOMIN))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_ZOOMIN,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_VIEW_ZOOMOUT:
- if (IsCmdEnabled(hwnd,IDM_VIEW_ZOOMOUT))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_ZOOMOUT,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_VIEW_SCHEME:
- if (IsCmdEnabled(hwnd,IDM_VIEW_SCHEME))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEME,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_VIEW_SCHEMECONFIG:
- if (IsCmdEnabled(hwnd,IDM_VIEW_SCHEMECONFIG))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEMECONFIG,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_EXIT:
- SendMessage(hwnd,WM_CLOSE,0,0);
- break;
- case IDT_FILE_SAVEAS:
- if (IsCmdEnabled(hwnd,IDM_FILE_SAVEAS))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVEAS,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_SAVECOPY:
- if (IsCmdEnabled(hwnd,IDM_FILE_SAVECOPY))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVECOPY,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_EDIT_CLEAR:
- if (IsCmdEnabled(hwnd,IDM_EDIT_CLEAR))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_CLEAR,1),0);
- else
- SendMessage(hwndEdit,SCI_CLEARALL,0,0);
- break;
- case IDT_FILE_PRINT:
- if (IsCmdEnabled(hwnd,IDM_FILE_PRINT))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_PRINT,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_OPENFAV:
- if (IsCmdEnabled(hwnd,IDM_FILE_OPENFAV))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_OPENFAV,1),0);
- else
- MessageBeep(0);
- break;
- case IDT_FILE_ADDTOFAV:
- if (IsCmdEnabled(hwnd,IDM_FILE_ADDTOFAV))
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_ADDTOFAV,1),0);
- else
- MessageBeep(0);
- break;
- }
- return(0);
- }
- //=============================================================================
- //
- // MsgNotify() - Handles WM_NOTIFY
- //
- //
- LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam)
- {
- LPNMHDR pnmh = (LPNMHDR)lParam;
- struct SCNotification* scn = (struct SCNotification*)lParam;
- switch(pnmh->idFrom)
- {
- case IDC_EDIT:
- switch(pnmh->code)
- {
- case SCN_UPDATEUI:
- UpdateToolbar();
- UpdateStatusbar();
- // Invalidate invalid selections
- #pragma message("TODO: Remove check for invalid selections once fixed in Scintilla")
- if (SendMessage(hwndEdit,SCI_GETSELECTIONS,0,0) > 1 &&
- SendMessage(hwndEdit,SCI_GETSELECTIONMODE,0,0) != SC_SEL_RECTANGLE) {
- int iCurPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- SendMessage(hwndEdit,WM_CANCELMODE,0,0);
- SendMessage(hwndEdit,SCI_CLEARSELECTIONS,0,0);
- SendMessage(hwndEdit,SCI_SETSELECTION,(WPARAM)iCurPos,(LPARAM)iCurPos);
- }
- // Brace Match
- if (bMatchBraces)
- {
- int iPos;
- char c;
- int iEndStyled = SendMessage(hwndEdit,SCI_GETENDSTYLED,0,0);
- if (iEndStyled < SendMessage(hwndEdit,SCI_GETLENGTH,0,0)) {
- int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iEndStyled,0);
- int iEndStyled = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,iLine,0);
- SendMessage(hwndEdit,SCI_COLOURISE,iEndStyled,-1);
- }
- iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
- if (StrChrA("()[]{}",c)) {
- int iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
- if (iBrace2 != -1) {
- int col1 = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0);
- int col2 = SendMessage(hwndEdit,SCI_GETCOLUMN,iBrace2,0);
- SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,iPos,iBrace2);
- SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,min(col1,col2),0);
- }
- else {
- SendMessage(hwndEdit,SCI_BRACEBADLIGHT,iPos,0);
- SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
- }
- }
- // Try one before
- else
- {
- iPos = SendMessage(hwndEdit,SCI_POSITIONBEFORE,iPos,0);
- c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
- if (StrChrA("()[]{}",c)) {
- int iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
- if (iBrace2 != -1) {
- int col1 = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0);
- int col2 = SendMessage(hwndEdit,SCI_GETCOLUMN,iBrace2,0);
- SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,iPos,iBrace2);
- SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,min(col1,col2),0);
- }
- else {
- SendMessage(hwndEdit,SCI_BRACEBADLIGHT,iPos,0);
- SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
- }
- }
- else {
- SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,(WPARAM)-1,(LPARAM)-1);
- SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
- }
- }
- }
- break;
- case SCN_CHARADDED:
- // Auto indent
- if (bAutoIndent && (scn->ch == 'x0D' || scn->ch == 'x0A'))
- {
- // in CRLF mode handle LF only...
- if ((SC_EOL_CRLF == iEOLMode && scn->ch != 'x0A') || SC_EOL_CRLF != iEOLMode)
- {
- char *pLineBuf;
- char *pPos;
- //int iIndentLen;
- int iCurPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- //int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
- int iCurLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
- int iLineLength = SendMessage(hwndEdit,SCI_LINELENGTH,iCurLine,0);
- //int iIndentBefore = SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine-1,0);
- if (iCurLine > 0/* && iLineLength <= 2*/)
- {
- int iPrevLineLength = SendMessage(hwndEdit,SCI_LINELENGTH,iCurLine-1,0);
- if (pLineBuf = GlobalAlloc(GPTR,iPrevLineLength+1))
- {
- SendMessage(hwndEdit,SCI_GETLINE,iCurLine-1,(LPARAM)pLineBuf);
- *(pLineBuf+iPrevLineLength) = ' ';
- for (pPos = pLineBuf; *pPos; pPos++) {
- if (*pPos != ' ' && *pPos != 't')
- *pPos = ' ';
- }
- if (*pLineBuf) {
- //int iPrevLineStartPos;
- //int iPrevLineEndPos;
- //int iPrevLineIndentPos;
- SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
- SendMessage(hwndEdit,SCI_ADDTEXT,lstrlenA(pLineBuf),(LPARAM)pLineBuf);
- SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
- //iPrevLineStartPos = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iCurLine-1,0);
- //iPrevLineEndPos = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iCurLine-1,0);
- //iPrevLineIndentPos = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,(WPARAM)iCurLine-1,0);
- //if (iPrevLineEndPos == iPrevLineIndentPos) {
- // SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
- // SendMessage(hwndEdit,SCI_SETTARGETSTART,(WPARAM)iPrevLineStartPos,0);
- // SendMessage(hwndEdit,SCI_SETTARGETEND,(WPARAM)iPrevLineEndPos,0);
- // SendMessage(hwndEdit,SCI_REPLACETARGET,0,(LPARAM)"");
- // SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
- //}
- }
- GlobalFree(pLineBuf);
- //int iIndent = SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine,0);
- //SendMessage(hwndEdit,SCI_SETLINEINDENTATION,(WPARAM)iCurLine,(LPARAM)iIndentBefore);
- //iIndentLen = /*- iIndent +*/ SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine,0);
- //if (iIndentLen > 0)
- // SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iAnchorPos+iIndentLen,(LPARAM)iCurPos+iIndentLen);
- }
- }
- }
- }
- // Auto close tags
- else if (bAutoCloseTags && scn->ch == '>')
- {
- int iLexer = SendMessage(hwndEdit,SCI_GETLEXER,0,0);
- if (/*iLexer == SCLEX_HTML || iLexer == SCLEX_XML*/ 1)
- {
- char tchBuf[512];
- char tchIns[516] = "</";
- int cchIns = 2;
- int iCurPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- int iHelper = iCurPos - (COUNTOF(tchBuf) - 1);
- int iStartPos = max(0,iHelper);
- int iSize = iCurPos - iStartPos;
- if (iSize >= 3) {
- struct TextRange tr = { { iStartPos, iCurPos }, tchBuf };
- SendMessage(hwndEdit,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
- if (tchBuf[iSize - 2] != '/') {
- const char* pBegin = &tchBuf[0];
- const char* pCur = &tchBuf[iSize - 2];
- while (pCur > pBegin && *pCur != '<' && *pCur != '>')
- --pCur;
- if (*pCur == '<') {
- pCur++;
- while (StrChrA(":_-.", *pCur) || IsCharAlphaNumericA(*pCur)) {
- tchIns[cchIns++] = *pCur;
- pCur++;
- }
- }
- tchIns[cchIns++] = '>';
- tchIns[cchIns] = 0;
- if (cchIns > 3 &&
- lstrcmpiA(tchIns,"</base>") &&
- lstrcmpiA(tchIns,"</bgsound>") &&
- lstrcmpiA(tchIns,"</br>") &&
- lstrcmpiA(tchIns,"</embed>") &&
- lstrcmpiA(tchIns,"</hr>") &&
- lstrcmpiA(tchIns,"</img>") &&
- lstrcmpiA(tchIns,"</input>") &&
- lstrcmpiA(tchIns,"</link>") &&
- lstrcmpiA(tchIns,"</meta>"))
- {
- SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
- SendMessage(hwndEdit,SCI_REPLACESEL,0,(LPARAM)tchIns);
- SendMessage(hwndEdit,SCI_SETSEL,iCurPos,iCurPos);
- SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
- }
- }
- }
- }
- }
- break;
- case SCN_MODIFIED:
- case SCN_ZOOM:
- UpdateLineNumberWidth();
- break;
- case SCN_SAVEPOINTREACHED:
- bModified = FALSE;
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- case SCN_SAVEPOINTLEFT:
- bModified = TRUE;
- SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- break;
- }
- break;
- case IDC_TOOLBAR:
- switch(pnmh->code)
- {
- case TBN_ENDADJUST:
- UpdateToolbar();
- break;
- case TBN_QUERYDELETE:
- case TBN_QUERYINSERT:
- return TRUE;
- case TBN_GETBUTTONINFO:
- {
- if (((LPTBNOTIFY)lParam)->iItem < COUNTOF(tbbMainWnd))
- {
- WCHAR tch[256];
- GetString(tbbMainWnd[((LPTBNOTIFY)lParam)->iItem].idCommand,tch,COUNTOF(tch));
- lstrcpyn(((LPTBNOTIFY)lParam)->pszText,/*StrChr(tch,L'n')+1*/tch,((LPTBNOTIFY)lParam)->cchText);
- CopyMemory(&((LPTBNOTIFY)lParam)->tbButton,&tbbMainWnd[((LPTBNOTIFY)lParam)->iItem],sizeof(TBBUTTON));
- return TRUE;
- }
- }
- return FALSE;
- case TBN_RESET:
- {
- int i; int c = SendMessage(hwndToolbar,TB_BUTTONCOUNT,0,0);
- for (i = 0; i < c; i++)
- SendMessage(hwndToolbar,TB_DELETEBUTTON,0,0);
- SendMessage(hwndToolbar,TB_ADDBUTTONS,NUMINITIALTOOLS,(LPARAM)tbbMainWnd);
- return(0);
- }
- }
- break;
- case IDC_STATUSBAR:
- switch(pnmh->code)
- {
- case NM_CLICK:
- {
- LPNMMOUSE pnmm = (LPNMMOUSE)lParam;
- switch (pnmm->dwItemSpec)
- {
- case STATUS_EOLMODE:
- SendMessage(hwndEdit,SCI_CONVERTEOLS,SendMessage(hwndEdit,SCI_GETEOLMODE,0,0),0);
- return TRUE;
- default:
- return FALSE;
- }
- }
- case NM_DBLCLK:
- {
- int i;
- LPNMMOUSE pnmm = (LPNMMOUSE)lParam;
- switch (pnmm->dwItemSpec)
- {
- case STATUS_CODEPAGE:
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_ENCODING_SELECT,1),0);
- return TRUE;
- case STATUS_EOLMODE:
- if (iEOLMode == SC_EOL_CRLF)
- i = IDM_LINEENDINGS_CRLF;
- else if (iEOLMode == SC_EOL_LF)
- i = IDM_LINEENDINGS_LF;
- else
- i = IDM_LINEENDINGS_CR;
- i++;
- if (i > IDM_LINEENDINGS_CR)
- i = IDM_LINEENDINGS_CRLF;
- SendMessage(hwnd,WM_COMMAND,MAKELONG(i,1),0);
- return TRUE;
- case STATUS_LEXER:
- SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEME,1),0);
- return TRUE;
- case STATUS_OVRMODE:
- SendMessage(hwndEdit,SCI_EDITTOGGLEOVERTYPE,0,0);
- return TRUE;
- default:
- return FALSE;
- }
- }
- break;
- }
- break;
- default:
- switch(pnmh->code)
- {
- case TTN_NEEDTEXT:
- {
- WCHAR tch[256];
- if (((LPTOOLTIPTEXT)lParam)->uFlags & TTF_IDISHWND)
- {
- ;
- }
- else
- {
- GetString(pnmh->idFrom,tch,COUNTOF(tch));
- lstrcpyn(((LPTOOLTIPTEXT)lParam)->szText,/*StrChr(tch,L'n')+1*/tch,80);
- }
- }
- break;
- }
- break;
- }
- return(0);
- }
- //=============================================================================
- //
- // LoadSettings()
- //
- //
- void LoadSettings()
- {
- WCHAR *pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
- int cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
- LoadIniSection(L"Settings",pIniSection,cchIniSection);
- bSaveSettings =
- IniSectionGetInt(pIniSection,L"SaveSettings",1);
- if (bSaveSettings) bSaveSettings = 1;
- bSaveRecentFiles =
- IniSectionGetInt(pIniSection,L"SaveRecentFiles",0);
- if (bSaveRecentFiles) bSaveRecentFiles = 1;
- bSaveFindReplace =
- IniSectionGetInt(pIniSection,L"SaveFindReplace",0);
- if (bSaveFindReplace) bSaveFindReplace = 1;
- efrData.bFindClose = IniSectionGetInt(pIniSection,L"CloseFind",0);
- if (efrData.bFindClose) efrData.bReplaceClose = TRUE;
- efrData.bReplaceClose = IniSectionGetInt(pIniSection,L"CloseReplace",0);
- if (efrData.bReplaceClose) efrData.bReplaceClose = TRUE;
- efrData.bNoFindWrap = IniSectionGetInt(pIniSection,L"NoFindWrap",0);
- if (efrData.bNoFindWrap) efrData.bNoFindWrap = TRUE;
- if (!IniSectionGetString(pIniSection,L"OpenWithDir",L"",
- tchOpenWithDir,COUNTOF(tchOpenWithDir)))
- SHGetSpecialFolderPath(NULL,tchOpenWithDir,CSIDL_DESKTOP,TRUE);
- else
- PathAbsoluteFromApp(tchOpenWithDir,NULL,COUNTOF(tchOpenWithDir),TRUE);
- if (!IniSectionGetString(pIniSection,L"Favorites",L"",
- tchFavoritesDir,COUNTOF(tchFavoritesDir)))
- SHGetFolderPath(NULL,CSIDL_PERSONAL,NULL,SHGFP_TYPE_CURRENT,tchFavoritesDir);
- else
- PathAbsoluteFromApp(tchFavoritesDir,NULL,COUNTOF(tchFavoritesDir),TRUE);
- iPathNameFormat = IniSectionGetInt(pIniSection,L"PathNameFormat",0);
- iPathNameFormat = max(min(iPathNameFormat,2),0);
- fWordWrap = IniSectionGetInt(pIniSection,L"WordWrap",0);
- if (fWordWrap) fWordWrap = 1;
- iWordWrapMode = IniSectionGetInt(pIniSection,L"WordWrapMode",0);
- iWordWrapMode = max(min(iWordWrapMode,1),0);
- iWordWrapIndent = IniSectionGetInt(pIniSection,L"WordWrapIndent",0);
- iWordWrapIndent = max(min(iWordWrapIndent,6),0);
- iWordWrapSymbols = IniSectionGetInt(pIniSection,L"WordWrapSymbols",22);
- iWordWrapSymbols = max(min(iWordWrapSymbols%10,2),0)+max(min((iWordWrapSymbols%100-iWordWrapSymbols%10)/10,2),0)*10;
- bShowWordWrapSymbols = IniSectionGetInt(pIniSection,L"ShowWordWrapSymbols",0);
- if (bShowWordWrapSymbols) bShowWordWrapSymbols = 1;
- bMatchBraces = IniSectionGetInt(pIniSection,L"MatchBraces",1);
- if (bMatchBraces) bMatchBraces = 1;
- bAutoCloseTags = IniSectionGetInt(pIniSection,L"AutoCloseTags",0);
- if (bAutoCloseTags) bAutoCloseTags = 1;
- bHiliteCurrentLine = IniSectionGetInt(pIniSection,L"HighlightCurrentLine",0);
- if (bHiliteCurrentLine) bHiliteCurrentLine = 1;
- bAutoIndent = IniSectionGetInt(pIniSection,L"AutoIndent",1);
- if (bAutoIndent) bAutoIndent = 1;
- bShowIndentGuides = IniSectionGetInt(pIniSection,L"ShowIndentGuides",0);
- if (bShowIndentGuides) bShowIndentGuides = 1;
- bTabsAsSpaces = IniSectionGetInt(pIniSection,L"TabsAsSpaces",1);
- if (bTabsAsSpaces) bTabsAsSpaces = 1;
- bTabsAsSpacesG = bTabsAsSpaces;
- bTabIndents = IniSectionGetInt(pIniSection,L"TabIndents",1);
- if (bTabIndents) bTabIndents = 1;
- iTabWidth = IniSectionGetInt(pIniSection,L"TabWidth",2);
- iTabWidth = max(min(iTabWidth,256),1);
- iTabWidthG = iTabWidth;
- iIndentWidth = IniSectionGetInt(pIniSection,L"IndentWidth",0);
- iIndentWidth = max(min(iIndentWidth,256),0);
- iIndentWidthG = iIndentWidth;
- bMarkLongLines = IniSectionGetInt(pIniSection,L"MarkLongLines",0);
- if (bMarkLongLines) bMarkLongLines = 1;
- iLongLinesLimit = IniSectionGetInt(pIniSection,L"LongLinesLimit",72);
- iLongLinesLimit = max(min(iLongLinesLimit,4096),0);
- iLongLinesLimitG = iLongLinesLimit;
- iLongLineMode = IniSectionGetInt(pIniSection,L"LongLineMode",EDGE_LINE);
- iLongLineMode = max(min(iLongLineMode,EDGE_BACKGROUND),EDGE_LINE);
- bShowSelectionMargin = IniSectionGetInt(pIniSection,L"ShowSelectionMargin",0);
- if (bShowSelectionMargin) bShowSelectionMargin = 1;
- bShowLineNumbers = IniSectionGetInt(pIniSection,L"ShowLineNumbers",1);
- if (bShowLineNumbers) bShowLineNumbers = 1;
- bViewWhiteSpace = IniSectionGetInt(pIniSection,L"ViewWhiteSpace",0);
- if (bViewWhiteSpace) bViewWhiteSpace = 1;
- bViewEOLs = IniSectionGetInt(pIniSection,L"ViewEOLs",0);
- if (bViewEOLs) bViewEOLs = 1;
- iDefaultEncoding = IniSectionGetInt(pIniSection,L"DefaultEncoding",0);
- iDefaultEncoding = Encoding_MapIniSetting(TRUE,iDefaultEncoding);
- if (!Encoding_IsValid(iDefaultEncoding)) iDefaultEncoding = CPI_DEFAULT;
- bSkipUnicodeDetection = IniSectionGetInt(pIniSection,L"SkipUnicodeDetection",0);
- if (bSkipUnicodeDetection) bSkipUnicodeDetection = 1;
- bLoadASCIIasUTF8 = IniSectionGetInt(pIniSection,L"LoadASCIIasUTF8",0);
- if (bLoadASCIIasUTF8) bLoadASCIIasUTF8 = 1;
- bNoEncodingTags = IniSectionGetInt(pIniSection,L"NoEncodingTags",0);
- if (bNoEncodingTags) bNoEncodingTags = 1;
- iDefaultEOLMode = IniSectionGetInt(pIniSection,L"DefaultEOLMode",0);
- iDefaultEOLMode = max(min(iDefaultEOLMode,2),0);
- bFixLineEndings = IniSectionGetInt(pIniSection,L"FixLineEndings",1);
- if (bFixLineEndings) bFixLineEndings = 1;
- bAutoStripBlanks = IniSectionGetInt(pIniSection,L"FixTrailingBlanks",0);
- if (bAutoStripBlanks) bAutoStripBlanks = 1;
- iPrintHeader = IniSectionGetInt(pIniSection,L"PrintHeader",1);
- iPrintHeader = max(min(iPrintHeader,3),0);
- iPrintFooter = IniSectionGetInt(pIniSection,L"PrintFooter",0);
- iPrintFooter = max(min(iPrintFooter,1),0);
- iPrintColor = IniSectionGetInt(pIniSection,L"PrintColorMode",3);
- iPrintColor = max(min(iPrintColor,4),0);
- iPrintZoom = IniSectionGetInt(pIniSection,L"PrintZoom",10)-10;
- iPrintZoom = max(min(iPrintZoom,20),-10);
- pagesetupMargin.left = IniSectionGetInt(pIniSection,L"PrintMarginLeft",-1);
- pagesetupMargin.left = max(pagesetupMargin.left,-1);
- pagesetupMargin.top = IniSectionGetInt(pIniSection,L"PrintMarginTop",-1);
- pagesetupMargin.top = max(pagesetupMargin.top,-1);
- pagesetupMargin.right = IniSectionGetInt(pIniSection,L"PrintMarginRight",-1);
- pagesetupMargin.right = max(pagesetupMargin.right,-1);
- pagesetupMargin.bottom = IniSectionGetInt(pIniSection,L"PrintMarginBottom",-1);
- pagesetupMargin.bottom = max(pagesetupMargin.bottom,-1);
- bSaveBeforeRunningTools = IniSectionGetInt(pIniSection,L"SaveBeforeRunningTools",0);
- if (bSaveBeforeRunningTools) bSaveBeforeRunningTools = 1;
- iFileWatchingMode = IniSectionGetInt(pIniSection,L"FileWatchingMode",0);
- iFileWatchingMode = max(min(iFileWatchingMode,2),0);
- bResetFileWatching = IniSectionGetInt(pIniSection,L"ResetFileWatching",1);
- if (bResetFileWatching) bResetFileWatching = 1;
- iEscFunction = IniSectionGetInt(pIniSection,L"EscFunction",0);
- iEscFunction = max(min(iEscFunction,2),0);
- bAlwaysOnTop = IniSectionGetInt(pIniSection,L"AlwaysOnTop",0);
- if (bAlwaysOnTop) bAlwaysOnTop = 1;
- bMinimizeToTray = IniSectionGetInt(pIniSection,L"MinimizeToTray",0);
- if (bMinimizeToTray) bMinimizeToTray = 1;
- bTransparentMode = IniSectionGetInt(pIniSection,L"TransparentMode",0);
- if (bTransparentMode) bTransparentMode = 1;
- // Check if SetLayeredWindowAttributes() is available
- bTransparentModeAvailable =
- (GetProcAddress(GetModuleHandle(L"User32"),"SetLayeredWindowAttributes") != NULL);
- IniSectionGetString(pIniSection,L"ToolbarButtons",L"",
- tchToolbarButtons,COUNTOF(tchToolbarButtons));
- bShowToolbar = IniSectionGetInt(pIniSection,L"ShowToolbar",1);
- if (bShowToolbar) bShowToolbar = 1;
- bShowStatusbar = IniSectionGetInt(pIniSection,L"ShowStatusbar",1);
- if (bShowStatusbar) bShowStatusbar = 1;
- cxEncodingDlg = IniSectionGetInt(pIniSection,L"EncodingDlgSizeX",256);
- cxEncodingDlg = max(cxEncodingDlg,0);
- cyEncodingDlg = IniSectionGetInt(pIniSection,L"EncodingDlgSizeY",262);
- cyEncodingDlg = max(cyEncodingDlg,0);
- cxRecodeDlg = IniSectionGetInt(pIniSection,L"RecodeDlgSizeX",256);
- cxRecodeDlg = max(cxRecodeDlg,0);
- cyRecodeDlg = IniSectionGetInt(pIniSection,L"RecodeDlgSizeY",262);
- cyRecodeDlg = max(cyRecodeDlg,0);
- cxFileMRUDlg = IniSectionGetInt(pIniSection,L"FileMRUDlgSizeX",412);
- cxFileMRUDlg = max(cxFileMRUDlg,0);
- cyFileMRUDlg = IniSectionGetInt(pIniSection,L"FileMRUDlgSizeY",376);
- cyFileMRUDlg = max(cyFileMRUDlg,0);
- cxOpenWithDlg = IniSectionGetInt(pIniSection,L"OpenWithDlgSizeX",384);
- cxOpenWithDlg = max(cxOpenWithDlg,0);
- cyOpenWithDlg = IniSectionGetInt(pIniSection,L"OpenWithDlgSizeY",386);
- cyOpenWithDlg = max(cyOpenWithDlg,0);
- cxFavoritesDlg = IniSectionGetInt(pIniSection,L"FavoritesDlgSizeX",334);
- cxFavoritesDlg = max(cxFavoritesDlg,0);
- cyFavoritesDlg = IniSectionGetInt(pIniSection,L"FavoritesDlgSizeY",316);
- cyFavoritesDlg = max(cyFavoritesDlg,0);
- xFindReplaceDlg = IniSectionGetInt(pIniSection,L"FindReplaceDlgPosX",0);
- yFindReplaceDlg = IniSectionGetInt(pIniSection,L"FindReplaceDlgPosY",0);
- LoadIniSection(L"Settings2",pIniSection,cchIniSection);
- bStickyWinPos = IniSectionGetInt(pIniSection,L"StickyWindowPosition",0);
- if (bStickyWinPos) bStickyWinPos = 1;
- IniSectionGetString(pIniSection,L"DefaultExtension",L"txt",
- tchDefaultExtension,COUNTOF(tchDefaultExtension));
- StrTrim(tchDefaultExtension,L" t."");
- IniSectionGetString(pIniSection,L"DefaultDirectory",L"",
- tchDefaultDir,COUNTOF(tchDefaultDir));
- ZeroMemory(tchFileDlgFilters,sizeof(WCHAR)*COUNTOF(tchFileDlgFilters));
- IniSectionGetString(pIniSection,L"FileDlgFilters",L"",
- tchFileDlgFilters,COUNTOF(tchFileDlgFilters)-2);
- iFileCheckInverval = IniSectionGetInt(pIniSection,L"FileCheckInverval",2000);
- LoadIniSection(L"Toolbar Images",pIniSection,cchIniSection);
- IniSectionGetString(pIniSection,L"BitmapDefault",L"",
- tchToolbarBitmap,COUNTOF(tchToolbarBitmap));
- IniSectionGetString(pIniSection,L"BitmapHot",L"",
- tchToolbarBitmapHot,COUNTOF(tchToolbarBitmap));
- IniSectionGetString(pIniSection,L"BitmapDisabled",L"",
- tchToolbarBitmapDisabled,COUNTOF(tchToolbarBitmap));
- if (!flagPosParam /*|| bStickyWinPos*/) { // ignore window position if /p was specified
- WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
- int ResX = GetSystemMetrics(SM_CXSCREEN);
- int ResY = GetSystemMetrics(SM_CYSCREEN);
- wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
- wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
- wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
- wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
- wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
- LoadIniSection(L"Window",pIniSection,cchIniSection);
- wi.x = IniSectionGetInt(pIniSection,tchPosX,CW_USEDEFAULT);
- wi.y = IniSectionGetInt(pIniSection,tchPosY,CW_USEDEFAULT);
- wi.cx = IniSectionGetInt(pIniSection,tchSizeX,CW_USEDEFAULT);
- wi.cy = IniSectionGetInt(pIniSection,tchSizeY,CW_USEDEFAULT);
- wi.max = IniSectionGetInt(pIniSection,tchMaximized,0);
- if (wi.max) wi.max = 1;
- }
- LocalFree(pIniSection);
- iDefaultCodePage = 0; {
- int acp = GetACP();
- if (acp == 932 || acp == 936 || acp == 949 || acp == 950 || acp == 1361)
- iDefaultCodePage = acp;
- }
- {
- CHARSETINFO ci;
- if (TranslateCharsetInfo((DWORD*)iDefaultCodePage,&ci,TCI_SRCCODEPAGE))
- iDefaultCharSet = ci.ciCharset;
- else
- iDefaultCharSet = ANSI_CHARSET;
- }
- // Scintilla Styles
- Style_Load();
- }
- //=============================================================================
- //
- // SaveSettings()
- //
- //
- void SaveSettings(BOOL bSaveSettingsNow)
- {
- WCHAR *pIniSection = NULL;
- int cchIniSection = 0;
- WCHAR wchTmp[MAX_PATH];
- if (lstrlen(szIniFile) == 0)
- return;
- CreateIniFile();
- if (!bSaveSettings && !bSaveSettingsNow) {
- IniSetInt(L"Settings",L"SaveSettings",bSaveSettings);
- return;
- }
- pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
- cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
- IniSectionSetInt(pIniSection,L"SaveSettings",bSaveSettings);
- IniSectionSetInt(pIniSection,L"SaveRecentFiles",bSaveRecentFiles);
- IniSectionSetInt(pIniSection,L"SaveFindReplace",bSaveFindReplace);
- IniSectionSetInt(pIniSection,L"CloseFind",efrData.bFindClose);
- IniSectionSetInt(pIniSection,L"CloseReplace",efrData.bReplaceClose);
- IniSectionSetInt(pIniSection,L"NoFindWrap",efrData.bNoFindWrap);
- PathRelativeToApp(tchOpenWithDir,wchTmp,COUNTOF(wchTmp),FALSE,TRUE,flagPortableMyDocs);
- IniSectionSetString(pIniSection,L"OpenWithDir",wchTmp);
- PathRelativeToApp(tchFavoritesDir,wchTmp,COUNTOF(wchTmp),FALSE,TRUE,flagPortableMyDocs);
- IniSectionSetString(pIniSection,L"Favorites",wchTmp);
- IniSectionSetInt(pIniSection,L"PathNameFormat",iPathNameFormat);
- IniSectionSetInt(pIniSection,L"WordWrap",fWordWrap);
- IniSectionSetInt(pIniSection,L"WordWrapMode",iWordWrapMode);
- IniSectionSetInt(pIniSection,L"WordWrapIndent",iWordWrapIndent);
- IniSectionSetInt(pIniSection,L"WordWrapSymbols",iWordWrapSymbols);
- IniSectionSetInt(pIniSection,L"ShowWordWrapSymbols",bShowWordWrapSymbols);
- IniSectionSetInt(pIniSection,L"MatchBraces",bMatchBraces);
- IniSectionSetInt(pIniSection,L"AutoCloseTags",bAutoCloseTags);
- IniSectionSetInt(pIniSection,L"HighlightCurrentLine",bHiliteCurrentLine);
- IniSectionSetInt(pIniSection,L"AutoIndent",bAutoIndent);
- IniSectionSetInt(pIniSection,L"ShowIndentGuides",bShowIndentGuides);
- IniSectionSetInt(pIniSection,L"TabsAsSpaces",bTabsAsSpacesG);
- IniSectionSetInt(pIniSection,L"TabIndents",bTabIndents);
- IniSectionSetInt(pIniSection,L"TabWidth",iTabWidthG);
- IniSectionSetInt(pIniSection,L"IndentWidth",iIndentWidthG);
- IniSectionSetInt(pIniSection,L"MarkLongLines",bMarkLongLines);
- IniSectionSetInt(pIniSection,L"LongLinesLimit",iLongLinesLimitG);
- IniSectionSetInt(pIniSection,L"LongLineMode",iLongLineMode);
- IniSectionSetInt(pIniSection,L"ShowSelectionMargin",bShowSelectionMargin);
- IniSectionSetInt(pIniSection,L"ShowLineNumbers",bShowLineNumbers);
- IniSectionSetInt(pIniSection,L"ViewWhiteSpace",bViewWhiteSpace);
- IniSectionSetInt(pIniSection,L"ViewEOLs",bViewEOLs);
- IniSectionSetInt(pIniSection,L"DefaultEncoding",Encoding_MapIniSetting(FALSE,iDefaultEncoding));
- IniSectionSetInt(pIniSection,L"SkipUnicodeDetection",bSkipUnicodeDetection);
- IniSectionSetInt(pIniSection,L"LoadASCIIasUTF8",bLoadASCIIasUTF8);
- IniSectionSetInt(pIniSection,L"NoEncodingTags",bNoEncodingTags);
- IniSectionSetInt(pIniSection,L"DefaultEOLMode",iDefaultEOLMode);
- IniSectionSetInt(pIniSection,L"FixLineEndings",bFixLineEndings);
- IniSectionSetInt(pIniSection,L"FixTrailingBlanks",bAutoStripBlanks);
- IniSectionSetInt(pIniSection,L"PrintHeader",iPrintHeader);
- IniSectionSetInt(pIniSection,L"PrintFooter",iPrintFooter);
- IniSectionSetInt(pIniSection,L"PrintColorMode",iPrintColor);
- IniSectionSetInt(pIniSection,L"PrintZoom",iPrintZoom+10);
- IniSectionSetInt(pIniSection,L"PrintMarginLeft",pagesetupMargin.left);
- IniSectionSetInt(pIniSection,L"PrintMarginTop",pagesetupMargin.top);
- IniSectionSetInt(pIniSection,L"PrintMarginRight",pagesetupMargin.right);
- IniSectionSetInt(pIniSection,L"PrintMarginBottom",pagesetupMargin.bottom);
- IniSectionSetInt(pIniSection,L"SaveBeforeRunningTools",bSaveBeforeRunningTools);
- IniSectionSetInt(pIniSection,L"FileWatchingMode",iFileWatchingMode);
- IniSectionSetInt(pIniSection,L"ResetFileWatching",bResetFileWatching);
- IniSectionSetInt(pIniSection,L"EscFunction",iEscFunction);
- IniSectionSetInt(pIniSection,L"AlwaysOnTop",bAlwaysOnTop);
- IniSectionSetInt(pIniSection,L"MinimizeToTray",bMinimizeToTray);
- IniSectionSetInt(pIniSection,L"TransparentMode",bTransparentMode);
- Toolbar_GetButtons(hwndToolbar,IDT_FILE_NEW,tchToolbarButtons,COUNTOF(tchToolbarButtons));
- IniSectionSetString(pIniSection,L"ToolbarButtons",tchToolbarButtons);
- IniSectionSetInt(pIniSection,L"ShowToolbar",bShowToolbar);
- IniSectionSetInt(pIniSection,L"ShowStatusbar",bShowStatusbar);
- IniSectionSetInt(pIniSection,L"EncodingDlgSizeX",cxEncodingDlg);
- IniSectionSetInt(pIniSection,L"EncodingDlgSizeY",cyEncodingDlg);
- IniSectionSetInt(pIniSection,L"RecodeDlgSizeX",cxRecodeDlg);
- IniSectionSetInt(pIniSection,L"RecodeDlgSizeY",cyRecodeDlg);
- IniSectionSetInt(pIniSection,L"FileMRUDlgSizeX",cxFileMRUDlg);
- IniSectionSetInt(pIniSection,L"FileMRUDlgSizeY",cyFileMRUDlg);
- IniSectionSetInt(pIniSection,L"OpenWithDlgSizeX",cxOpenWithDlg);
- IniSectionSetInt(pIniSection,L"OpenWithDlgSizeY",cyOpenWithDlg);
- IniSectionSetInt(pIniSection,L"FavoritesDlgSizeX",cxFavoritesDlg);
- IniSectionSetInt(pIniSection,L"FavoritesDlgSizeY",cyFavoritesDlg);
- IniSectionSetInt(pIniSection,L"FindReplaceDlgPosX",xFindReplaceDlg);
- IniSectionSetInt(pIniSection,L"FindReplaceDlgPosY",yFindReplaceDlg);
- SaveIniSection(L"Settings",pIniSection);
- LocalFree(pIniSection);
- /*
- SaveSettingsNow(): query Window Dimensions
- */
- if (bSaveSettingsNow)
- {
- WINDOWPLACEMENT wndpl;
- // GetWindowPlacement
- wndpl.length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement(hwndMain,&wndpl);
- wi.x = wndpl.rcNormalPosition.left;
- wi.y = wndpl.rcNormalPosition.top;
- wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
- wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
- wi.max = (IsZoomed(hwndMain) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
- }
- if (!IniGetInt(L"Settings2",L"StickyWindowPosition",0)) {
- WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
- int ResX = GetSystemMetrics(SM_CXSCREEN);
- int ResY = GetSystemMetrics(SM_CYSCREEN);
- wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
- wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
- wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
- wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
- wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
- IniSetInt(L"Window",tchPosX,wi.x);
- IniSetInt(L"Window",tchPosY,wi.y);
- IniSetInt(L"Window",tchSizeX,wi.cx);
- IniSetInt(L"Window",tchSizeY,wi.cy);
- IniSetInt(L"Window",tchMaximized,wi.max);
- }
- // Scintilla Styles
- Style_Save();
- }
- //=============================================================================
- //
- // ParseCommandLine()
- //
- //
- void ParseCommandLine()
- {
- LPWSTR lp1,lp2,lp3;
- BOOL bContinue = TRUE;
- BOOL bIsNotepadReplacement = FALSE;
- LPWSTR lpCmdLine = GetCommandLine();
- if (lstrlen(lpCmdLine) == 0)
- return;
- // Good old console can also send args separated by Tabs
- StrTab2Space(lpCmdLine);
- lp1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- lp2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- lp3 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- // Start with 2nd argument
- ExtractFirstArgument(lpCmdLine,lp1,lp3);
- while (bContinue && ExtractFirstArgument(lp3,lp1,lp2))
- {
- // options
- if (lstrcmp(lp1,L"+") == 0)
- flagMultiFileArg = 2;
- else if (lstrcmp(lp1,L"-") == 0)
- flagMultiFileArg = 1;
- else if ((*lp1 == L'/') || (*lp1 == L'-'))
- {
- StrTrim(lp1,L"-/");
- // Encoding
- if (lstrcmpi(lp1,L"ANSI") == 0 || lstrcmpi(lp1,L"A") == 0 || lstrcmpi(lp1,L"MBCS") == 0)
- flagSetEncoding = IDM_ENCODING_ANSI-IDM_ENCODING_ANSI + 1;
- else if (lstrcmpi(lp1,L"UNICODE") == 0 || lstrcmpi(lp1,L"W") == 0)
- flagSetEncoding = IDM_ENCODING_UNICODE-IDM_ENCODING_ANSI + 1;
- else if (lstrcmpi(lp1,L"UNICODEBE") == 0 || lstrcmpi(lp1,L"UNICODE-BE") == 0)
- flagSetEncoding = IDM_ENCODING_UNICODEREV-IDM_ENCODING_ANSI + 1;
- else if (lstrcmpi(lp1,L"UTF8") == 0 || lstrcmpi(lp1,L"UTF-8") == 0)
- flagSetEncoding = IDM_ENCODING_UTF8-IDM_ENCODING_ANSI + 1;
- else if (lstrcmpi(lp1,L"UTF8SIG") == 0 || lstrcmpi(lp1,L"UTF-8SIG") == 0 ||
- lstrcmpi(lp1,L"UTF8SIGNATURE") == 0 || lstrcmpi(lp1,L"UTF-8SIGNATURE") == 0 ||
- lstrcmpi(lp1,L"UTF8-SIGNATURE") == 0 || lstrcmpi(lp1,L"UTF-8-SIGNATURE") == 0)
- flagSetEncoding = IDM_ENCODING_UTF8SIGN-IDM_ENCODING_ANSI + 1;
- // EOL Mode
- else if (lstrcmpi(lp1,L"CRLF") == 0 || lstrcmpi(lp1,L"CR+LF") == 0)
- flagSetEOLMode = IDM_LINEENDINGS_CRLF-IDM_LINEENDINGS_CRLF + 1;
- else if (lstrcmpi(lp1,L"LF") == 0)
- flagSetEOLMode = IDM_LINEENDINGS_LF-IDM_LINEENDINGS_CRLF + 1;
- else if (lstrcmpi(lp1,L"CR") == 0)
- flagSetEOLMode = IDM_LINEENDINGS_CR-IDM_LINEENDINGS_CRLF + 1;
- else switch (*CharUpper(lp1))
- {
- case L'N':
- flagReuseWindow = 0;
- flagNoReuseWindow = 1;
- if (*CharUpper(lp1+1) == L'S')
- flagSingleFileInstance = 1;
- else
- flagSingleFileInstance = 0;
- break;
- case L'R':
- flagReuseWindow = 1;
- flagNoReuseWindow = 0;
- if (*CharUpper(lp1+1) == L'S')
- flagSingleFileInstance = 1;
- else
- flagSingleFileInstance = 0;
- break;
- case L'F':
- if (*(lp1+1) == L'0' || *CharUpper(lp1+1) == L'O')
- lstrcpy(szIniFile,L"*?");
- else if (ExtractFirstArgument(lp2,lp1,lp2)) {
- StrCpyN(szIniFile,lp1,COUNTOF(szIniFile));
- TrimString(szIniFile);
- PathUnquoteSpaces(szIniFile);
- }
- break;
- case L'I':
- flagStartAsTrayIcon = 1;
- break;
- case L'P':
- if (StrCmpNI(lp1,L"POS:",4) == 0)
- lp1 += 3;
- else if (StrCmpNI(lp1,L"POS",3) == 0)
- lp1 += 2;
- else if (*(lp1+1) == L':')
- lp1 += 1;
- else if (bIsNotepadReplacement) {
- if (*(lp1+1) == L'T')
- ExtractFirstArgument(lp2,lp1,lp2);
- break;
- }
- if (*(lp1+1) == L'0' || *CharUpper(lp1+1) == L'O') {
- flagPosParam = 1;
- flagDefaultPos = 1;
- }
- else if (*CharUpper(lp1+1) == L'D' || *CharUpper(lp1+1) == L'S') {
- flagPosParam = 1;
- flagDefaultPos = (StrChrI((lp1+1),L'L')) ? 3 : 2;
- }
- else if (StrChrI(L"FLTRBM",*(lp1+1))) {
- WCHAR *p = (lp1+1);
- flagPosParam = 1;
- flagDefaultPos = 0;
- while (*p) {
- switch (*CharUpper(p)) {
- case L'F':
- flagDefaultPos &= ~(4|8|16|32);
- flagDefaultPos |= 64;
- break;
- case L'L':
- flagDefaultPos &= ~(8|64);
- flagDefaultPos |= 4;
- break;
- case L'R':
- flagDefaultPos &= ~(4|64);
- flagDefaultPos |= 8;
- break;
- case L'T':
- flagDefaultPos &= ~(32|64);
- flagDefaultPos |= 16;
- break;
- case L'B':
- flagDefaultPos &= ~(16|64);
- flagDefaultPos |= 32;
- break;
- case L'M':
- if (flagDefaultPos == 0)
- flagDefaultPos |= 64;
- flagDefaultPos |= 128;
- break;
- }
- p = CharNext(p);
- }
- }
- else if (ExtractFirstArgument(lp2,lp1,lp2)) {
- int itok =
- swscanf(lp1,L"%i,%i,%i,%i,%i",&wi.x,&wi.y,&wi.cx,&wi.cy,&wi.max);
- if (itok == 4 || itok == 5) { // scan successful
- flagPosParam = 1;
- if (wi.cx < 1) wi.cx = CW_USEDEFAULT;
- if (wi.cy < 1) wi.cy = CW_USEDEFAULT;
- if (wi.max) wi.max = 1;
- if (itok == 4) wi.max = 0;
- }
- }
- break;
- case L'T':
- if (ExtractFirstArgument(lp2,lp1,lp2)) {
- StrCpyN(szTitleExcerpt,lp1,COUNTOF(szTitleExcerpt));
- fKeepTitleExcerpt = 1;
- }
- break;
- case L'C':
- flagNewFromClipboard = 1;
- break;
- case L'B':
- flagPasteBoard = 1;
- break;
- case L'E':
- if (ExtractFirstArgument(lp2,lp1,lp2)) {
- if (lpEncodingArg)
- LocalFree(lpEncodingArg);
- lpEncodingArg = StrDup(lp1);
- }
- break;
- case L'G':
- if (ExtractFirstArgument(lp2,lp1,lp2)) {
- int itok =
- swscanf(lp1,L"%i,%i",&iInitialLine,&iInitialColumn);
- if (itok == 1 || itok == 2) { // scan successful
- flagJumpTo = 1;
- }
- }
- break;
- case L'M':
- {
- BOOL bFindUp = FALSE;
- BOOL bRegex = FALSE;
- BOOL bTransBS = FALSE;
- if (StrChr(lp1,L'-'))
- bFindUp = TRUE;
- if (StrChr(lp1,L'R'))
- bRegex = TRUE;
- if (StrChr(lp1,L'B'))
- bTransBS = TRUE;
- if (ExtractFirstArgument(lp2,lp1,lp2)) {
- if (lpMatchArg)
- GlobalFree(lpMatchArg);
- lpMatchArg = StrDup(lp1);
- flagMatchText = 1;
- if (bFindUp)
- flagMatchText |= 2;
- if (bRegex) {
- flagMatchText &= ~8;
- flagMatchText |= 4;
- }
- if (bTransBS) {
- flagMatchText &= ~4;
- flagMatchText |= 8;
- }
- }
- }
- break;
- case L'Q':
- flagQuietCreate = 1;
- break;
- case L'S':
- if (ExtractFirstArgument(lp2,lp1,lp2)) {
- if (lpSchemeArg)
- LocalFree(lpSchemeArg);
- lpSchemeArg = StrDup(lp1);
- flagLexerSpecified = 1;
- }
- break;
- case L'D':
- if (lpSchemeArg) {
- LocalFree(lpSchemeArg);
- lpSchemeArg = NULL;
- }
- iInitialLexer = 0;
- flagLexerSpecified = 1;
- break;
- case L'H':
- if (lpSchemeArg) {
- LocalFree(lpSchemeArg);
- lpSchemeArg = NULL;
- }
- iInitialLexer = 1;
- flagLexerSpecified = 1;
- break;
- case L'X':
- if (lpSchemeArg) {
- LocalFree(lpSchemeArg);
- lpSchemeArg = NULL;
- }
- iInitialLexer = 2;
- flagLexerSpecified = 1;
- break;
- case L'U':
- flagRelaunchElevated = 1;
- break;
- case L'Z':
- ExtractFirstArgument(lp2,lp1,lp2);
- flagMultiFileArg = 1;
- bIsNotepadReplacement = TRUE;
- break;
- case L'?':
- flagDisplayHelp = 1;
- break;
- default:
- break;
- }
- }
- // pathname
- else
- {
- LPWSTR lpFileBuf = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- cchiFileList = lstrlen(lpCmdLine) - lstrlen(lp3);
- if (lpFileArg)
- GlobalFree(lpFileArg);
- lpFileArg = GlobalAlloc(GPTR,sizeof(WCHAR)*(MAX_PATH+2)); // changed for ActivatePrevInst() needs
- StrCpyN(lpFileArg,lp3,MAX_PATH);
- StrTrim(lpFileArg,L" "");
- while (cFileList < 32 && ExtractFirstArgument(lp3,lpFileBuf,lp3)) {
- PathQuoteSpaces(lpFileBuf);
- lpFileList[cFileList++] = StrDup(lpFileBuf);
- }
- bContinue = FALSE;
- LocalFree(lpFileBuf);
- }
- // Continue with next argument
- if (bContinue)
- lstrcpy(lp3,lp2);
- }
- LocalFree(lp1);
- LocalFree(lp2);
- LocalFree(lp3);
- }
- //=============================================================================
- //
- // LoadFlags()
- //
- //
- void LoadFlags()
- {
- WCHAR *pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
- int cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
- LoadIniSection(L"Settings2",pIniSection,cchIniSection);
- if (!flagReuseWindow && !flagNoReuseWindow) {
- if (!IniSectionGetInt(pIniSection,L"ReuseWindow",0))
- flagNoReuseWindow = 1;
- if (IniSectionGetInt(pIniSection,L"SingleFileInstance",0))
- flagSingleFileInstance = 1;
- }
- if (flagMultiFileArg == 0) {
- if (IniSectionGetInt(pIniSection,L"MultiFileArg",0))
- flagMultiFileArg = 2;
- }
- if (IniSectionGetInt(pIniSection,L"RelativeFileMRU",1))
- flagRelativeFileMRU = 1;
- if (IniSectionGetInt(pIniSection,L"PortableMyDocs",flagRelativeFileMRU))
- flagPortableMyDocs = 1;
- if (IniSectionGetInt(pIniSection,L"NoFadeHidden",0))
- flagNoFadeHidden = 1;
- if (IniSectionGetInt(pIniSection,L"SimpleIndentGuides",0))
- flagSimpleIndentGuides = 1;
- if (IniSectionGetInt(pIniSection,L"NoHTMLGuess",0))
- fNoHTMLGuess = 1;
- if (IniSectionGetInt(pIniSection,L"NoCGIGuess",0))
- fNoCGIGuess = 1;
- if (IniSectionGetInt(pIniSection,L"NoFileVariables",0))
- fNoFileVariables = 1;
- IniSectionGetString(pIniSection,L"ShellAppUserModelID",L"(default)",
- g_wchAppUserModelID,COUNTOF(g_wchAppUserModelID));
- if (IniSectionGetInt(pIniSection,L"ShellUseSystemMRU",0))
- flagUseSystemMRU = 1;
- LocalFree(pIniSection);
- }
- //=============================================================================
- //
- // FindIniFile()
- //
- //
- int CheckIniFile(LPWSTR lpszFile,LPCWSTR lpszModule)
- {
- WCHAR tchFileExpanded[MAX_PATH];
- WCHAR tchBuild[MAX_PATH];
- ExpandEnvironmentStrings(lpszFile,tchFileExpanded,COUNTOF(tchFileExpanded));
- if (PathIsRelative(tchFileExpanded)) {
- // program directory
- lstrcpy(tchBuild,lpszModule);
- lstrcpy(PathFindFileName(tchBuild),tchFileExpanded);
- if (PathFileExists(tchBuild)) {
- lstrcpy(lpszFile,tchBuild);
- return(1);
- }
- // %appdata%
- if (S_OK == SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,SHGFP_TYPE_CURRENT,tchBuild)) {
- PathAppend(tchBuild,tchFileExpanded);
- if (PathFileExists(tchBuild)) {
- lstrcpy(lpszFile,tchBuild);
- return(1);
- }
- }
- // general
- if (SearchPath(NULL,tchFileExpanded,NULL,COUNTOF(tchBuild),tchBuild,NULL)) {
- lstrcpy(lpszFile,tchBuild);
- return(1);
- }
- }
- else if (PathFileExists(tchFileExpanded)) {
- lstrcpy(lpszFile,tchFileExpanded);
- return(1);
- }
- return(0);
- }
- int CheckIniFileRedirect(LPWSTR lpszFile,LPCWSTR lpszModule)
- {
- WCHAR tch[MAX_PATH];
- if (GetPrivateProfileString(L"Notepad2",L"Notepad2.ini",L"",tch,COUNTOF(tch),lpszFile)) {
- if (CheckIniFile(tch,lpszModule)) {
- lstrcpy(lpszFile,tch);
- return(1);
- }
- else {
- WCHAR tchFileExpanded[MAX_PATH];
- ExpandEnvironmentStrings(tch,tchFileExpanded,COUNTOF(tchFileExpanded));
- if (PathIsRelative(tchFileExpanded)) {
- lstrcpy(lpszFile,lpszModule);
- lstrcpy(PathFindFileName(lpszFile),tchFileExpanded);
- return(1);
- }
- else {
- lstrcpy(lpszFile,tchFileExpanded);
- return(1);
- }
- }
- }
- return(0);
- }
- int FindIniFile() {
- int bFound = 0;
- WCHAR tchTest[MAX_PATH];
- WCHAR tchModule[MAX_PATH];
- GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
- if (lstrlen(szIniFile)) {
- if (lstrcmpi(szIniFile,L"*?") == 0)
- return(0);
- else {
- if (!CheckIniFile(szIniFile,tchModule)) {
- ExpandEnvironmentStringsEx(szIniFile,COUNTOF(szIniFile));
- if (PathIsRelative(szIniFile)) {
- lstrcpy(tchTest,tchModule);
- PathRemoveFileSpec(tchTest);
- PathAppend(tchTest,szIniFile);
- lstrcpy(szIniFile,tchTest);
- }
- }
- }
- return(1);
- }
- lstrcpy(tchTest,PathFindFileName(tchModule));
- PathRenameExtension(tchTest,L".ini");
- bFound = CheckIniFile(tchTest,tchModule);
- if (!bFound) {
- lstrcpy(tchTest,L"Notepad2.ini");
- bFound = CheckIniFile(tchTest,tchModule);
- }
- if (bFound) {
- // allow two redirections: administrator -> user -> custom
- if (CheckIniFileRedirect(tchTest,tchModule))
- CheckIniFileRedirect(tchTest,tchModule);
- lstrcpy(szIniFile,tchTest);
- }
- else {
- lstrcpy(szIniFile,tchModule);
- PathRenameExtension(szIniFile,L".ini");
- }
- return(1);
- }
- int TestIniFile() {
- if (lstrcmpi(szIniFile,L"*?") == 0) {
- lstrcpy(szIniFile2,L"");
- lstrcpy(szIniFile,L"");
- return(0);
- }
- if (PathIsDirectory(szIniFile) || *CharPrev(szIniFile,StrEnd(szIniFile)) == L'\') {
- WCHAR wchModule[MAX_PATH];
- GetModuleFileName(NULL,wchModule,COUNTOF(wchModule));
- PathAppend(szIniFile,PathFindFileName(wchModule));
- PathRenameExtension(szIniFile,L".ini");
- if (!PathFileExists(szIniFile)) {
- lstrcpy(PathFindFileName(szIniFile),L"Notepad2.ini");
- if (!PathFileExists(szIniFile)) {
- lstrcpy(PathFindFileName(szIniFile),PathFindFileName(wchModule));
- PathRenameExtension(szIniFile,L".ini");
- }
- }
- }
- if (!PathFileExists(szIniFile) || PathIsDirectory(szIniFile)) {
- lstrcpy(szIniFile2,szIniFile);
- lstrcpy(szIniFile,L"");
- return(0);
- }
- else
- return(1);
- }
- int CreateIniFile() {
- return(CreateIniFileEx(szIniFile));
- }
- int CreateIniFileEx(LPCWSTR lpszIniFile) {
- if (*lpszIniFile) {
- HANDLE hFile;
- WCHAR *pwchTail;
- if (pwchTail = StrRChrW(lpszIniFile,NULL,L'\')) {
- *pwchTail = 0;
- SHCreateDirectoryEx(NULL,lpszIniFile,NULL);
- *pwchTail = L'\';
- }
- hFile = CreateFile(lpszIniFile,
- GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
- NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- dwLastIOError = GetLastError();
- if (hFile != INVALID_HANDLE_VALUE) {
- if (GetFileSize(hFile,NULL) == 0) {
- DWORD dw;
- WriteFile(hFile,(LPCVOID)L"xFEFF[Notepad2]rn",26,&dw,NULL);
- }
- CloseHandle(hFile);
- return(1);
- }
- else
- return(0);
- }
- else
- return(0);
- }
- //=============================================================================
- //
- // UpdateToolbar()
- //
- //
- #define EnableTool(id,b) SendMessage(hwndToolbar,TB_ENABLEBUTTON,id,
- MAKELONG(((b) ? 1 : 0), 0))
- #define CheckTool(id,b) SendMessage(hwndToolbar,TB_CHECKBUTTON,id,
- MAKELONG(b,0))
- void UpdateToolbar()
- {
- int i;
- if (!bShowToolbar)
- return;
- EnableTool(IDT_FILE_ADDTOFAV,lstrlen(szCurFile));
- EnableTool(IDT_EDIT_UNDO,SendMessage(hwndEdit,SCI_CANUNDO,0,0) /*&& !bReadOnly*/);
- EnableTool(IDT_EDIT_REDO,SendMessage(hwndEdit,SCI_CANREDO,0,0) /*&& !bReadOnly*/);
- i = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) - SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
- EnableTool(IDT_EDIT_CUT,i /*&& !bReadOnly*/);
- EnableTool(IDT_EDIT_COPY,SendMessage(hwndEdit,SCI_GETLENGTH,0,0));
- EnableTool(IDT_EDIT_PASTE,SendMessage(hwndEdit,SCI_CANPASTE,0,0) /*&& !bReadOnly*/);
- i = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
- EnableTool(IDT_EDIT_FIND,i);
- //EnableTool(IDT_EDIT_FINDNEXT,i);
- //EnableTool(IDT_EDIT_FINDPREV,i && lstrlen(efrData.szFind));
- EnableTool(IDT_EDIT_REPLACE,i /*&& !bReadOnly*/);
- EnableTool(IDT_EDIT_CLEAR,i /*&& !bReadOnly*/);
- CheckTool(IDT_VIEW_WORDWRAP,fWordWrap);
- }
- //=============================================================================
- //
- // UpdateStatusbar()
- //
- //
- void UpdateStatusbar()
- {
- int iPos;
- int iLn;
- int iLines;
- int iCol;
- int iSel;
- WCHAR tchLn[32];
- WCHAR tchLines[32];
- WCHAR tchCol[32];
- WCHAR tchCols[32];
- WCHAR tchSel[32];
- WCHAR tchDocPos[256];
- int iBytes;
- WCHAR tchBytes[64];
- WCHAR tchDocSize[256];
- WCHAR tchEOLMode[32];
- WCHAR tchOvrMode[32];
- WCHAR tchLexerName[128];
- if (!bShowStatusbar)
- return;
- iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
- iLn = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iPos,0) + 1;
- wsprintf(tchLn,L"%i",iLn);
- FormatNumberStr(tchLn);
- iLines = SendMessage(hwndEdit,SCI_GETLINECOUNT,0,0);
- wsprintf(tchLines,L"%i",iLines);
- FormatNumberStr(tchLines);
- iCol = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0) + 1;
- wsprintf(tchCol,L"%i",iCol);
- FormatNumberStr(tchCol);
- if (bMarkLongLines) {
- wsprintf(tchCols,L"%i",iLongLinesLimit);
- FormatNumberStr(tchCols);
- }
- if (SC_SEL_RECTANGLE != SendMessage(hwndEdit,SCI_GETSELECTIONMODE,0,0))
- {
- iSel = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) - SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
- wsprintf(tchSel,L"%i",iSel);
- FormatNumberStr(tchSel);
- }
- else
- lstrcpy(tchSel,L"--");
- if (!bMarkLongLines)
- FormatString(tchDocPos,COUNTOF(tchDocPos),IDS_DOCPOS,tchLn,tchLines,tchCol,tchSel);
- else
- FormatString(tchDocPos,COUNTOF(tchDocPos),IDS_DOCPOS2,tchLn,tchLines,tchCol,tchCols,tchSel);
- iBytes = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
- StrFormatByteSize(iBytes,tchBytes,COUNTOF(tchBytes));
- FormatString(tchDocSize,COUNTOF(tchDocSize),IDS_DOCSIZE,tchBytes);
- Encoding_GetLabel(iEncoding);
- if (iEOLMode == SC_EOL_CR)
- lstrcpy(tchEOLMode,L"CR");
- else if (iEOLMode == SC_EOL_LF)
- lstrcpy(tchEOLMode,L"LF");
- else
- lstrcpy(tchEOLMode,L"CR+LF");
- if (SendMessage(hwndEdit,SCI_GETOVERTYPE,0,0))
- lstrcpy(tchOvrMode,L"OVR");
- else
- lstrcpy(tchOvrMode,L"INS");
- Style_GetCurrentLexerName(tchLexerName,COUNTOF(tchLexerName));
- StatusSetText(hwndStatus,STATUS_DOCPOS,tchDocPos);
- StatusSetText(hwndStatus,STATUS_DOCSIZE,tchDocSize);
- StatusSetText(hwndStatus,STATUS_CODEPAGE,mEncoding[iEncoding].wchLabel);
- StatusSetText(hwndStatus,STATUS_EOLMODE,tchEOLMode);
- StatusSetText(hwndStatus,STATUS_OVRMODE,tchOvrMode);
- StatusSetText(hwndStatus,STATUS_LEXER,tchLexerName);
- //InvalidateRect(hwndStatus,NULL,TRUE);
- }
- //=============================================================================
- //
- // UpdateLineNumberWidth()
- //
- //
- void UpdateLineNumberWidth()
- {
- char tchLines[32];
- int iLineMarginWidthNow;
- int iLineMarginWidthFit;
- if (bShowLineNumbers) {
- wsprintfA(tchLines,"_%i_",SendMessage(hwndEdit,SCI_GETLINECOUNT,0,0));
- iLineMarginWidthNow = SendMessage(hwndEdit,SCI_GETMARGINWIDTHN,0,0);
- iLineMarginWidthFit = SendMessage(hwndEdit,SCI_TEXTWIDTH,STYLE_LINENUMBER,(LPARAM)tchLines);
- if (iLineMarginWidthNow != iLineMarginWidthFit) {
- //SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,0);
- SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,iLineMarginWidthFit);
- }
- }
- else
- SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,0);
- }
- //=============================================================================
- //
- // FileIO()
- //
- //
- BOOL FileIO(BOOL fLoad,LPCWSTR psz,BOOL bNoEncDetect,int *ienc,int *ieol,
- BOOL *pbUnicodeErr,BOOL *pbFileTooBig,
- BOOL *pbCancelDataLoss,BOOL bSaveCopy)
- {
- SHFILEINFO shfi;
- WCHAR tch[MAX_PATH+40];
- BOOL fSuccess;
- DWORD dwFileAttributes;
- BeginWaitCursor();
- SHGetFileInfo2(psz,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
- FormatString(tch,COUNTOF(tch),(fLoad) ? IDS_LOADFILE : IDS_SAVEFILE,shfi.szDisplayName);
- StatusSetText(hwndStatus,STATUS_HELP,tch);
- StatusSetSimple(hwndStatus,TRUE);
- InvalidateRect(hwndStatus,NULL,TRUE);
- UpdateWindow(hwndStatus);
- if (fLoad)
- fSuccess = EditLoadFile(hwndEdit,psz,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig);
- else
- fSuccess = EditSaveFile(hwndEdit,psz,*ienc,pbCancelDataLoss,bSaveCopy);
- dwFileAttributes = GetFileAttributes(psz);
- bReadOnly = (dwFileAttributes != INVALID_FILE_ATTRIBUTES && dwFileAttributes & FILE_ATTRIBUTE_READONLY);
- StatusSetSimple(hwndStatus,FALSE);
- EndWaitCursor();
- return(fSuccess);
- }
- //=============================================================================
- //
- // FileLoad()
- //
- //
- BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lpszFile)
- {
- WCHAR tch[MAX_PATH] = L"";
- WCHAR szFileName[MAX_PATH] = L"";
- BOOL fSuccess;
- BOOL bUnicodeErr = FALSE;
- BOOL bFileTooBig = FALSE;
- if (!bDontSave)
- {
- if (!FileSave(FALSE,TRUE,FALSE,FALSE))
- return FALSE;
- }
- if (bNew) {
- lstrcpy(szCurFile,L"");
- SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
- SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
- if (!fKeepTitleExcerpt)
- lstrcpy(szTitleExcerpt,L"");
- FileVars_Init(NULL,0,&fvCurFile);
- EditSetNewText(hwndEdit,"",0);
- Style_SetLexer(hwndEdit,NULL);
- UpdateLineNumberWidth();
- bModified = FALSE;
- bReadOnly = FALSE;
- iEOLMode = iLineEndings[iDefaultEOLMode];
- SendMessage(hwndEdit,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0);
- iEncoding = iDefaultEncoding;
- iOriginalEncoding = iDefaultEncoding;
- SendMessage(hwndEdit,SCI_SETCODEPAGE,(iDefaultEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
- EditSetNewText(hwndEdit,"",0);
- SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- // Terminate file watching
- if (bResetFileWatching)
- iFileWatchingMode = 0;
- InstallFileWatching(NULL);
- return TRUE;
- }
- if (!lpszFile || lstrlen(lpszFile) == 0) {
- if (!OpenFileDlg(hwndMain,tch,COUNTOF(tch),NULL))
- return FALSE;
- }
- else
- lstrcpy(tch,lpszFile);
- ExpandEnvironmentStringsEx(tch,COUNTOF(tch));
- if (PathIsRelative(tch)) {
- GetCurrentDirectory(COUNTOF(szFileName),szFileName);
- PathAppend(szFileName,tch);
- if (!PathFileExists(szFileName)) {
- WCHAR wchFullPath[MAX_PATH];
- if (SearchPath(NULL,tch,NULL,COUNTOF(wchFullPath),wchFullPath,NULL)) {
- lstrcpy(szFileName,wchFullPath);
- }
- }
- }
- else
- lstrcpy(szFileName,tch);
- PathCanonicalizeEx(szFileName);
- GetLongPathNameEx(szFileName,szFileName,COUNTOF(szFileName));
- if (PathIsLnkFile(szFileName))
- PathGetLnkPath(szFileName,szFileName,COUNTOF(szFileName));
- // Ask to create a new file...
- if (!bReload && !PathFileExists(szFileName))
- {
- if (flagQuietCreate || MsgBox(MBYESNO,IDS_ASK_CREATE,szFileName) == IDYES) {
- HANDLE hFile = CreateFile(szFileName,
- GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
- NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
- dwLastIOError = GetLastError();
- if (fSuccess = (hFile != INVALID_HANDLE_VALUE)) {
- CloseHandle(hFile);
- FileVars_Init(NULL,0,&fvCurFile);
- EditSetNewText(hwndEdit,"",0);
- Style_SetLexer(hwndEdit,NULL);
- iEOLMode = iLineEndings[iDefaultEOLMode];
- SendMessage(hwndEdit,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0);
- if (iSrcEncoding != -1) {
- iEncoding = iSrcEncoding;
- iOriginalEncoding = iSrcEncoding;
- }
- else {
- iEncoding = iDefaultEncoding;
- iOriginalEncoding = iDefaultEncoding;
- }
- SendMessage(hwndEdit,SCI_SETCODEPAGE,(iEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
- bReadOnly = FALSE;
- EditSetNewText(hwndEdit,"",0);
- }
- }
- else
- return FALSE;
- }
- else
- fSuccess = FileIO(TRUE,szFileName,bNoEncDetect,&iEncoding,&iEOLMode,&bUnicodeErr,&bFileTooBig,NULL,FALSE);
- if (fSuccess) {
- lstrcpy(szCurFile,szFileName);
- SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
- SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
- if (!fKeepTitleExcerpt)
- lstrcpy(szTitleExcerpt,L"");
- if (!flagLexerSpecified) // flag will be cleared
- Style_SetLexerFromFile(hwndEdit,szCurFile);
- UpdateLineNumberWidth();
- iOriginalEncoding = iEncoding;
- bModified = FALSE;
- //bReadOnly = FALSE;
- SendMessage(hwndEdit,SCI_SETEOLMODE,iEOLMode,0);
- MRU_AddFile(pFileMRU,szFileName,flagRelativeFileMRU,flagPortableMyDocs);
- if (flagUseSystemMRU)
- SHAddToRecentDocs(SHARD_PATHW,szFileName);
- SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szFileName,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- // Install watching of the current file
- if (!bReload && bResetFileWatching)
- iFileWatchingMode = 0;
- InstallFileWatching(szCurFile);
- // the .LOG feature ...
- if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) {
- char tchLog[5] = "";
- SendMessage(hwndEdit,SCI_GETTEXT,5,(LPARAM)tchLog);
- if (lstrcmpiA(tchLog,".LOG") == 0) {
- EditJumpTo(hwndEdit,-1,0);
- SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
- SendMessage(hwndEdit,SCI_NEWLINE,0,0);
- SendMessage(hwndMain,WM_COMMAND,MAKELONG(IDM_EDIT_INSERT_SHORTDATE,1),0);
- SendMessage(hwndEdit,SCI_NEWLINE,0,0);
- SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
- EditJumpTo(hwndEdit,-1,0);
- }
- }
- // Show warning: Unicode file loaded as ANSI
- if (bUnicodeErr)
- MsgBox(MBWARN,IDS_ERR_UNICODE);
- }
- else if (!bFileTooBig)
- MsgBox(MBWARN,IDS_ERR_LOADFILE,szFileName);
- return(fSuccess);
- }
- //=============================================================================
- //
- // FileSave()
- //
- //
- BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
- {
- WCHAR tchFile[MAX_PATH];
- BOOL fSuccess = FALSE;
- BOOL bCancelDataLoss = FALSE;
- BOOL bIsEmptyNewFile = FALSE;
- if (lstrlen(szCurFile) == 0) {
- int cchText = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
- if (cchText == 0)
- bIsEmptyNewFile = TRUE;
- else if (cchText < 1023) {
- char tchText[2048];
- SendMessage(hwndEdit,SCI_GETTEXT,(WPARAM)2047,(LPARAM)tchText);
- StrTrimA(tchText," tnr");
- if (lstrlenA(tchText) == 0)
- bIsEmptyNewFile = TRUE;
- }
- }
- if (!bSaveAlways && (!bModified && iEncoding == iOriginalEncoding || bIsEmptyNewFile) && !bSaveAs)
- return TRUE;
- if (bAsk)
- {
- // File or "Untitled" ...
- WCHAR tch[MAX_PATH];
- if (lstrlen(szCurFile))
- lstrcpy(tch,szCurFile);
- else
- GetString(IDS_UNTITLED,tch,COUNTOF(tch));
- switch (MsgBox(MBYESNOCANCEL,IDS_ASK_SAVE,tch)) {
- case IDCANCEL:
- return FALSE;
- case IDNO:
- return TRUE;
- }
- }
- // Read only...
- if (!bSaveAs && !bSaveCopy && lstrlen(szCurFile))
- {
- DWORD dwFileAttributes = GetFileAttributes(szCurFile);
- if (dwFileAttributes != INVALID_FILE_ATTRIBUTES)
- bReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY);
- if (bReadOnly) {
- SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- if (MsgBox(MBYESNOWARN,IDS_READONLY_SAVE,szCurFile) == IDYES)
- bSaveAs = TRUE;
- else
- return FALSE;
- }
- }
- // Save As...
- if (bSaveAs || bSaveCopy || lstrlen(szCurFile) == 0)
- {
- WCHAR tchInitialDir[MAX_PATH] = L"";
- if (bSaveCopy && lstrlen(tchLastSaveCopyDir)) {
- lstrcpy(tchInitialDir,tchLastSaveCopyDir);
- lstrcpy(tchFile,tchLastSaveCopyDir);
- PathAppend(tchFile,PathFindFileName(szCurFile));
- }
- else
- lstrcpy(tchFile,szCurFile);
- if (SaveFileDlg(hwndMain,tchFile,COUNTOF(tchFile),tchInitialDir))
- {
- if (fSuccess = FileIO(FALSE,tchFile,FALSE,&iEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,bSaveCopy))
- {
- if (!bSaveCopy)
- {
- lstrcpy(szCurFile,tchFile);
- SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
- SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
- if (!fKeepTitleExcerpt)
- lstrcpy(szTitleExcerpt,L"");
- Style_SetLexerFromFile(hwndEdit,szCurFile);
- UpdateStatusbar();
- UpdateLineNumberWidth();
- }
- else {
- lstrcpy(tchLastSaveCopyDir,tchFile);
- PathRemoveFileSpec(tchLastSaveCopyDir);
- }
- }
- }
- else
- return FALSE;
- }
- else
- fSuccess = FileIO(FALSE,szCurFile,FALSE,&iEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,FALSE);
- if (fSuccess)
- {
- if (!bSaveCopy)
- {
- bModified = FALSE;
- iOriginalEncoding = iEncoding;
- MRU_AddFile(pFileMRU,szCurFile,flagRelativeFileMRU,flagPortableMyDocs);
- if (flagUseSystemMRU)
- SHAddToRecentDocs(SHARD_PATHW,szCurFile);
- SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- // Install watching of the current file
- if (bSaveAs && bResetFileWatching)
- iFileWatchingMode = 0;
- InstallFileWatching(szCurFile);
- }
- }
- else if (!bCancelDataLoss)
- {
- if (lstrlen(szCurFile) != 0)
- lstrcpy(tchFile,szCurFile);
- SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
- iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
- IDS_READONLY,bReadOnly,szTitleExcerpt);
- MsgBox(MBWARN,IDS_ERR_SAVEFILE,tchFile);
- }
- return(fSuccess);
- }
- //=============================================================================
- //
- // OpenFileDlg()
- //
- //
- BOOL OpenFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
- {
- OPENFILENAME ofn;
- WCHAR szFile[MAX_PATH];
- WCHAR szFilter[NUMLEXERS*1024];
- WCHAR tchInitialDir[MAX_PATH];
- lstrcpy(szFile,L"");
- Style_GetOpenDlgFilterStr(szFilter,COUNTOF(szFilter));
- if (!lpstrInitialDir) {
- if (lstrlen(szCurFile)) {
- lstrcpy(tchInitialDir,szCurFile);
- PathRemoveFileSpec(tchInitialDir);
- }
- else if (lstrlen(tchDefaultDir)) {
- ExpandEnvironmentStrings(tchDefaultDir,tchInitialDir,COUNTOF(tchInitialDir));
- if (PathIsRelative(tchInitialDir)) {
- WCHAR tchModule[MAX_PATH];
- GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
- PathRemoveFileSpec(tchModule);
- PathAppend(tchModule,tchInitialDir);
- PathCanonicalize(tchInitialDir,tchModule);
- }
- }
- }
- ZeroMemory(&ofn,sizeof(OPENFILENAME));
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = hwnd;
- ofn.lpstrFilter = szFilter;
- ofn.lpstrFile = szFile;
- ofn.lpstrInitialDir = (lpstrInitialDir) ? lpstrInitialDir : tchInitialDir;
- ofn.nMaxFile = COUNTOF(szFile);
- ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | /* OFN_NOCHANGEDIR |*/
- OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST |
- OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/;
- ofn.lpstrDefExt = (lstrlen(tchDefaultExtension)) ? tchDefaultExtension : NULL;
- if (GetOpenFileName(&ofn)) {
- lstrcpyn(lpstrFile,szFile,cchFile);
- return TRUE;
- }
- else
- return FALSE;
- }
- //=============================================================================
- //
- // SaveFileDlg()
- //
- //
- BOOL SaveFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
- {
- OPENFILENAME ofn;
- WCHAR szNewFile[MAX_PATH];
- WCHAR szFilter[NUMLEXERS*1024];
- WCHAR tchInitialDir[MAX_PATH];
- lstrcpy(szNewFile,lpstrFile);
- Style_GetOpenDlgFilterStr(szFilter,COUNTOF(szFilter));
- if (lstrlen(lpstrInitialDir))
- lstrcpy(tchInitialDir,lpstrInitialDir);
- else if (lstrlen(szCurFile)) {
- lstrcpy(tchInitialDir,szCurFile);
- PathRemoveFileSpec(tchInitialDir);
- }
- else if (lstrlen(tchDefaultDir)) {
- ExpandEnvironmentStrings(tchDefaultDir,tchInitialDir,COUNTOF(tchInitialDir));
- if (PathIsRelative(tchInitialDir)) {
- WCHAR tchModule[MAX_PATH];
- GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
- PathRemoveFileSpec(tchModule);
- PathAppend(tchModule,tchInitialDir);
- PathCanonicalize(tchInitialDir,tchModule);
- }
- }
- ZeroMemory(&ofn,sizeof(OPENFILENAME));
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = hwnd;
- ofn.lpstrFilter = szFilter;
- ofn.lpstrFile = szNewFile;
- ofn.lpstrInitialDir = tchInitialDir;
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_HIDEREADONLY /*| OFN_NOCHANGEDIR*/ |
- /*OFN_NODEREFERENCELINKS |*/ OFN_OVERWRITEPROMPT |
- OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST;
- ofn.lpstrDefExt = (lstrlen(tchDefaultExtension)) ? tchDefaultExtension : NULL;
- if (GetSaveFileName(&ofn)) {
- lstrcpyn(lpstrFile,szNewFile,cchFile);
- return TRUE;
- }
- else
- return FALSE;
- }
- /******************************************************************************
- *
- * ActivatePrevInst()
- *
- * Tries to find and activate an already open Notepad2 Window
- *
- *
- ******************************************************************************/
- BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
- {
- BOOL bContinue = TRUE;
- WCHAR szClassName[64];
- if (GetClassName(hwnd,szClassName,COUNTOF(szClassName)))
- if (lstrcmpi(szClassName,wchWndClass) == 0) {
- DWORD dwReuseLock = GetDlgItemInt(hwnd,IDC_REUSELOCK,NULL,FALSE);
- if (GetTickCount() - dwReuseLock >= REUSEWINDOWLOCKTIMEOUT) {
- *(HWND*)lParam = hwnd;
- if (IsWindowEnabled(hwnd))
- bContinue = FALSE;
- }
- }
- return(bContinue);
- }
- BOOL CALLBACK EnumWndProc2(HWND hwnd,LPARAM lParam)
- {
- BOOL bContinue = TRUE;
- WCHAR szClassName[64];
- if (GetClassName(hwnd,szClassName,COUNTOF(szClassName)))
- if (lstrcmpi(szClassName,wchWndClass) == 0) {
- DWORD dwReuseLock = GetDlgItemInt(hwnd,IDC_REUSELOCK,NULL,FALSE);
- if (GetTickCount() - dwReuseLock >= REUSEWINDOWLOCKTIMEOUT) {
- WCHAR tchFileName[MAX_PATH] = L"";
- if (IsWindowEnabled(hwnd))
- bContinue = FALSE;
- GetDlgItemText(hwnd,IDC_FILENAME,tchFileName,COUNTOF(tchFileName));
- if (lstrcmpi(tchFileName,lpFileArg) == 0)
- *(HWND*)lParam = hwnd;
- else
- bContinue = TRUE;
- }
- }
- return(bContinue);
- }
- BOOL ActivatePrevInst()
- {
- HWND hwnd = NULL;
- COPYDATASTRUCT cds;
- if ((flagNoReuseWindow && !flagSingleFileInstance) || flagStartAsTrayIcon || flagNewFromClipboard || flagPasteBoard)
- return(FALSE);
- if (flagSingleFileInstance && lpFileArg) {
- // Search working directory from second instance, first!
- // lpFileArg is at least MAX_PATH+2 bytes
- WCHAR tchTmp[MAX_PATH];
- ExpandEnvironmentStringsEx(lpFileArg,GlobalSize(lpFileArg)/sizeof(WCHAR));
- if (PathIsRelative(lpFileArg)) {
- GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
- PathAppend(tchTmp,lpFileArg);
- if (PathFileExists(tchTmp))
- lstrcpy(lpFileArg,tchTmp);
- else {
- if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
- lstrcpy(lpFileArg,tchTmp);
- else {
- GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
- PathAppend(tchTmp,lpFileArg);
- lstrcpy(lpFileArg,tchTmp);
- }
- }
- }
- else if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
- lstrcpy(lpFileArg,tchTmp);
- GetLongPathName(lpFileArg,lpFileArg,MAX_PATH);
- EnumWindows(EnumWndProc2,(LPARAM)&hwnd);
- if (hwnd != NULL)
- {
- // Enabled
- if (IsWindowEnabled(hwnd))
- {
- LPNP2PARAMS params;
- DWORD cb = sizeof(NP2PARAMS);
- // Make sure the previous window won't pop up a change notification message
- //SendMessage(hwnd,WM_CHANGENOTIFYCLEAR,0,0);
- if (IsIconic(hwnd))
- ShowWindowAsync(hwnd,SW_RESTORE);
- if (!IsWindowVisible(hwnd)) {
- SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK);
- SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP);
- }
- SetForegroundWindow(hwnd);
- if (lpSchemeArg)
- cb += (lstrlen(lpSchemeArg) + 1) * sizeof(WCHAR);
- params = GlobalAlloc(GPTR,cb);
- params->flagFileSpecified = FALSE;
- params->flagQuietCreate = FALSE;
- params->flagLexerSpecified = flagLexerSpecified;
- if (flagLexerSpecified && lpSchemeArg) {
- lstrcpy(StrEnd(¶ms->wchData)+1,lpSchemeArg);
- params->iInitialLexer = -1;
- }
- else
- params->iInitialLexer = iInitialLexer;
- params->flagJumpTo = flagJumpTo;
- params->iInitialLine = iInitialLine;
- params->iInitialColumn = iInitialColumn;
- params->iSrcEncoding = (lpEncodingArg) ? Encoding_MatchW(lpEncodingArg) : -1;
- params->flagSetEncoding = flagSetEncoding;
- params->flagSetEOLMode = flagSetEOLMode;
- params->flagTitleExcerpt = 0;
- cds.dwData = DATA_NOTEPAD2_PARAMS;
- cds.cbData = GlobalSize(params);
- cds.lpData = params;
- SendMessage(hwnd,WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
- GlobalFree(params);
- return(TRUE);
- }
- else // IsWindowEnabled()
- {
- // Ask...
- if (IDYES == MsgBox(MBYESNO,IDS_ERR_PREVWINDISABLED))
- return(FALSE);
- else
- return(TRUE);
- }
- }
- }
- if (flagNoReuseWindow)
- return(FALSE);
- hwnd = NULL;
- EnumWindows(EnumWndProc,(LPARAM)&hwnd);
- // Found a window
- if (hwnd != NULL)
- {
- // Enabled
- if (IsWindowEnabled(hwnd))
- {
- // Make sure the previous window won't pop up a change notification message
- //SendMessage(hwnd,WM_CHANGENOTIFYCLEAR,0,0);
- if (IsIconic(hwnd))
- ShowWindowAsync(hwnd,SW_RESTORE);
- if (!IsWindowVisible(hwnd)) {
- SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK);
- SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP);
- }
- SetForegroundWindow(hwnd);
- if (lpFileArg)
- {
- // Search working directory from second instance, first!
- // lpFileArg is at least MAX_PATH+2 bytes
- WCHAR tchTmp[MAX_PATH];
- LPNP2PARAMS params;
- DWORD cb = sizeof(NP2PARAMS);
- int cchTitleExcerpt;
- ExpandEnvironmentStringsEx(lpFileArg,GlobalSize(lpFileArg)/sizeof(WCHAR));
- if (PathIsRelative(lpFileArg)) {
- GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
- PathAppend(tchTmp,lpFileArg);
- if (PathFileExists(tchTmp))
- lstrcpy(lpFileArg,tchTmp);
- else {
- if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
- lstrcpy(lpFileArg,tchTmp);
- }
- }
- else if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
- lstrcpy(lpFileArg,tchTmp);
- cb += (lstrlen(lpFileArg) + 1) * sizeof(WCHAR);
- if (lpSchemeArg)
- cb += (lstrlen(lpSchemeArg) + 1) * sizeof(WCHAR);
- cchTitleExcerpt = lstrlen(szTitleExcerpt);
- if (cchTitleExcerpt)
- cb += (cchTitleExcerpt + 1) * sizeof(WCHAR);
- params = GlobalAlloc(GPTR,cb);
- params->flagFileSpecified = TRUE;
- lstrcpy(¶ms->wchData,lpFileArg);
- params->flagQuietCreate = flagQuietCreate;
- params->flagLexerSpecified = flagLexerSpecified;
- if (flagLexerSpecified && lpSchemeArg) {
- lstrcpy(StrEnd(¶ms->wchData)+1,lpSchemeArg);
- params->iInitialLexer = -1;
- }
- else
- params->iInitialLexer = iInitialLexer;
- params->flagJumpTo = flagJumpTo;
- params->iInitialLine = iInitialLine;
- params->iInitialColumn = iInitialColumn;
- params->iSrcEncoding = (lpEncodingArg) ? Encoding_MatchW(lpEncodingArg) : -1;
- params->flagSetEncoding = flagSetEncoding;
- params->flagSetEOLMode = flagSetEOLMode;
- if (cchTitleExcerpt) {
- lstrcpy(StrEnd(¶ms->wchData)+1,szTitleExcerpt);
- params->flagTitleExcerpt = 1;
- }
- else
- params->flagTitleExcerpt = 0;
- cds.dwData = DATA_NOTEPAD2_PARAMS;
- cds.cbData = GlobalSize(params);
- cds.lpData = params;
- SendMessage(hwnd,WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
- GlobalFree(params);
- GlobalFree(lpFileArg);
- }
- return(TRUE);
- }
- else // IsWindowEnabled()
- {
- // Ask...
- if (IDYES == MsgBox(MBYESNO,IDS_ERR_PREVWINDISABLED))
- return(FALSE);
- else
- return(TRUE);
- }
- }
- else
- return(FALSE);
- }
- //=============================================================================
- //
- // RelaunchMultiInst()
- //
- //
- BOOL RelaunchMultiInst() {
- if (flagMultiFileArg == 2 && cFileList > 1) {
- int i;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- LPWSTR lpCmdLineNew = StrDup(GetCommandLine());
- LPWSTR lp1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLineNew) + 1));
- LPWSTR lp2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLineNew) + 1));
- StrTab2Space(lpCmdLineNew);
- for (i = 0; i < cFileList; i++) {
- lstrcpy(lpCmdLineNew + cchiFileList,L" /n ");
- lstrcat(lpCmdLineNew,lpFileList[i]);
- LocalFree(lpFileList[i]);
- ZeroMemory(&si,sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
- CreateProcess(NULL,lpCmdLineNew,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
- }
- LocalFree(lpCmdLineNew);
- LocalFree(lp1);
- LocalFree(lp2);
- GlobalFree(lpFileArg);
- return TRUE;
- }
- else {
- int i;
- for (i = 0; i < cFileList; i++)
- LocalFree(lpFileList[i]);
- return FALSE;
- }
- }
- //=============================================================================
- //
- // RelaunchElevated()
- //
- //
- BOOL RelaunchElevated() {
- if (!IsVista() || fIsElevated || !flagRelaunchElevated || flagDisplayHelp)
- return(FALSE);
- else {
- LPWSTR lpCmdLine;
- LPWSTR lpArg1, lpArg2;
- STARTUPINFO si;
- SHELLEXECUTEINFO sei;
- si.cb = sizeof(STARTUPINFO);
- GetStartupInfo(&si);
- lpCmdLine = GetCommandLine();
- lpArg1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- lpArg2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
- ExtractFirstArgument(lpCmdLine,lpArg1,lpArg2);
- if (lstrlen(lpArg1)) {
- ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
- sei.cbSize = sizeof(SHELLEXECUTEINFO);
- sei.fMask = SEE_MASK_FLAG_NO_UI | /*SEE_MASK_NOZONECHECKS*/0x00800000;
- sei.hwnd = NULL;
- sei.lpVerb = L"runas";
- sei.lpFile = lpArg1;
- sei.lpParameters = lpArg2;
- sei.lpDirectory = NULL;
- sei.nShow = si.wShowWindow;
- ShellExecuteEx(&sei);
- }
- LocalFree(lpArg1);
- LocalFree(lpArg2);
- return(TRUE);
- }
- }
- //=============================================================================
- //
- // ShowNotifyIcon()
- //
- //
- void ShowNotifyIcon(HWND hwnd,BOOL bAdd)
- {
- static HICON hIcon;
- NOTIFYICONDATA nid;
- if (!hIcon)
- hIcon = LoadImage(g_hInstance,MAKEINTRESOURCE(IDR_MAINWND),
- IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
- ZeroMemory(&nid,sizeof(NOTIFYICONDATA));
- nid.cbSize = sizeof(NOTIFYICONDATA);
- nid.hWnd = hwnd;
- nid.uID = 0;
- nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
- nid.uCallbackMessage = WM_TRAYMESSAGE;
- nid.hIcon = hIcon;
- lstrcpy(nid.szTip,L"Notepad2");
- if(bAdd)
- Shell_NotifyIcon(NIM_ADD,&nid);
- else
- Shell_NotifyIcon(NIM_DELETE,&nid);
- }
- //=============================================================================
- //
- // SetNotifyIconTitle()
- //
- //
- void SetNotifyIconTitle(HWND hwnd)
- {
- NOTIFYICONDATA nid;
- SHFILEINFO shfi;
- WCHAR tchTitle[128];
- WCHAR tchFormat[32];
- ZeroMemory(&nid,sizeof(NOTIFYICONDATA));
- nid.cbSize = sizeof(NOTIFYICONDATA);
- nid.hWnd = hwnd;
- nid.uID = 0;
- nid.uFlags = NIF_TIP;
- if (lstrlen(szTitleExcerpt)) {
- GetString(IDS_TITLEEXCERPT,tchFormat,COUNTOF(tchFormat));
- wsprintf(tchTitle,tchFormat,szTitleExcerpt);
- }
- else if (lstrlen(szCurFile)) {
- SHGetFileInfo2(szCurFile,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
- PathCompactPathEx(tchTitle,shfi.szDisplayName,COUNTOF(tchTitle)-4,0);
- }
- else
- GetString(IDS_UNTITLED,tchTitle,COUNTOF(tchTitle)-4);
- if (bModified || iEncoding != iOriginalEncoding)
- lstrcpy(nid.szTip,L"* ");
- else
- lstrcpy(nid.szTip,L"");
- lstrcat(nid.szTip,tchTitle);
- Shell_NotifyIcon(NIM_MODIFY,&nid);
- }
- //=============================================================================
- //
- // InstallFileWatching()
- //
- //
- void InstallFileWatching(LPCWSTR lpszFile)
- {
- WCHAR tchDirectory[MAX_PATH];
- HANDLE hFind;
- // Terminate
- if (!iFileWatchingMode || !lpszFile || lstrlen(lpszFile) == 0)
- {
- if (bRunningWatch)
- {
- FindCloseChangeNotification(hChangeHandle);
- KillTimer(NULL,ID_WATCHTIMER);
- bRunningWatch = FALSE;
- //bPendingChangeNotify = FALSE;
- }
- return;
- }
- // Install
- else
- {
- // Terminate previous watching
- if (bRunningWatch) {
- FindCloseChangeNotification(hChangeHandle);
- //bPendingChangeNotify = FALSE;
- }
- // No previous watching installed, so launch the timer first
- else
- SetTimer(NULL,ID_WATCHTIMER,iFileCheckInverval,WatchTimerProc);
- lstrcpy(tchDirectory,lpszFile);
- PathRemoveFileSpec(tchDirectory);
- // Save data of current file
- hFind = FindFirstFile(szCurFile,&fdCurFile);
- if (hFind != INVALID_HANDLE_VALUE)
- FindClose(hFind);
- else
- ZeroMemory(&fdCurFile,sizeof(WIN32_FIND_DATA));
- hChangeHandle = FindFirstChangeNotification(tchDirectory,FALSE,
- FILE_NOTIFY_CHANGE_FILE_NAME |
- FILE_NOTIFY_CHANGE_DIR_NAME |
- FILE_NOTIFY_CHANGE_ATTRIBUTES |
- FILE_NOTIFY_CHANGE_SIZE |
- FILE_NOTIFY_CHANGE_LAST_WRITE);
- bRunningWatch = TRUE;
- //bPendingChangeNotify = FALSE;
- }
- }
- //=============================================================================
- //
- // WatchTimerProc()
- //
- //
- void CALLBACK WatchTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
- {
- // Check Change Notification Handle
- if (bRunningWatch && WAIT_OBJECT_0 == WaitForSingleObject(hChangeHandle,0))
- {
- // Check if the changes affect the current file
- WIN32_FIND_DATA fdUpdated;
- HANDLE hFind = FindFirstFile(szCurFile,&fdUpdated);
- if (INVALID_HANDLE_VALUE != hFind)
- FindClose(hFind);
- else
- // The current file has been removed
- ZeroMemory(&fdUpdated,sizeof(WIN32_FIND_DATA));
- // Check if the file has been changed
- if (CompareFileTime(&fdCurFile.ftLastWriteTime,&fdUpdated.ftLastWriteTime) != 0 ||
- fdCurFile.nFileSizeLow != fdUpdated.nFileSizeLow ||
- fdCurFile.nFileSizeHigh != fdUpdated.nFileSizeHigh)
- {
- // Shutdown current watching and give control to main window
- bRunningWatch = FALSE;
- //bPendingChangeNotify = TRUE;
- FindCloseChangeNotification(hChangeHandle);
- KillTimer(NULL,ID_WATCHTIMER);
- SendMessage(hwndMain,WM_CHANGENOTIFY,0,0);
- }
- else
- FindNextChangeNotification(hChangeHandle);
- }
- }
- //=============================================================================
- //
- // PasteBoardTimer()
- //
- //
- void CALLBACK PasteBoardTimer(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
- {
- if (dwLastCopyTime > 0 && GetTickCount() - dwLastCopyTime > 200) {
- if (SendMessage(hwndEdit,SCI_CANPASTE,0,0)) {
- BOOL bAutoIndent2 = bAutoIndent;
- bAutoIndent = 0;
- EditJumpTo(hwndEdit,-1,0);
- SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
- if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) > 0)
- SendMessage(hwndEdit,SCI_NEWLINE,0,0);
- SendMessage(hwndEdit,SCI_PASTE,0,0);
- SendMessage(hwndEdit,SCI_NEWLINE,0,0);
- SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
- bAutoIndent = bAutoIndent2;
- }
- dwLastCopyTime = 0;
- }
- }
- /// End of Notepad2.c \