Notepad2.c
上传用户:tj_dwf
上传日期:2020-11-17
资源大小:215k
文件大小:211k
源码类别:

RichEdit

开发平台:

Visual C++

  1.       }
  2.       else {
  3.         bShowToolbar = 1;
  4.         UpdateToolbar();
  5.         ShowWindow(hwndReBar,SW_SHOW);
  6.       }
  7.       SendWMSize(hwnd);
  8.       break;
  9.     case IDM_VIEW_CUSTOMIZETB:
  10.       SendMessage(hwndToolbar,TB_CUSTOMIZE,0,0);
  11.       break;
  12.     case IDM_VIEW_STATUSBAR:
  13.       if (bShowStatusbar) {
  14.         bShowStatusbar = 0;
  15.         ShowWindow(hwndStatus,SW_HIDE);
  16.       }
  17.       else {
  18.         bShowStatusbar = 1;
  19.         UpdateStatusbar();
  20.         ShowWindow(hwndStatus,SW_SHOW);
  21.       }
  22.       SendWMSize(hwnd);
  23.       break;
  24.     case IDM_VIEW_STICKYWINPOS:
  25.       bStickyWinPos = IniGetInt(L"Settings2",L"StickyWindowPosition",bStickyWinPos);
  26.       if (!bStickyWinPos) {
  27.         WINDOWPLACEMENT wndpl;
  28.         WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
  29.         int ResX = GetSystemMetrics(SM_CXSCREEN);
  30.         int ResY = GetSystemMetrics(SM_CYSCREEN);
  31.         // GetWindowPlacement
  32.         wndpl.length = sizeof(WINDOWPLACEMENT);
  33.         GetWindowPlacement(hwndMain,&wndpl);
  34.         wi.x = wndpl.rcNormalPosition.left;
  35.         wi.y = wndpl.rcNormalPosition.top;
  36.         wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
  37.         wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
  38.         wi.max = (IsZoomed(hwndMain) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
  39.         wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
  40.         wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
  41.         wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
  42.         wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
  43.         wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
  44.         bStickyWinPos = 1;
  45.         IniSetInt(L"Settings2",L"StickyWindowPosition",1);
  46.         IniSetInt(L"Window",tchPosX,wi.x);
  47.         IniSetInt(L"Window",tchPosY,wi.y);
  48.         IniSetInt(L"Window",tchSizeX,wi.cx);
  49.         IniSetInt(L"Window",tchSizeY,wi.cy);
  50.         IniSetInt(L"Window",tchMaximized,wi.max);
  51.         InfoBox(0,L"MsgStickyWinPos",IDS_STICKYWINPOS);
  52.       }
  53.       else {
  54.         bStickyWinPos = 0;
  55.         IniSetInt(L"Settings2",L"StickyWindowPosition",0);
  56.       }
  57.       break;
  58.     case IDM_VIEW_REUSEWINDOW:
  59.       if (IniGetInt(L"Settings2",L"ReuseWindow",0))
  60.         IniSetInt(L"Settings2",L"ReuseWindow",0);
  61.       else
  62.         IniSetInt(L"Settings2",L"ReuseWindow",1);
  63.       break;
  64.     case IDM_VIEW_SINGLEFILEINSTANCE:
  65.       if (IniGetInt(L"Settings2",L"SingleFileInstance",0))
  66.         IniSetInt(L"Settings2",L"SingleFileInstance",0);
  67.       else
  68.         IniSetInt(L"Settings2",L"SingleFileInstance",1);
  69.       break;
  70.     case IDM_VIEW_ALWAYSONTOP:
  71.       if (bAlwaysOnTop) {
  72.         bAlwaysOnTop = 0;
  73.         SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  74.       }
  75.       else {
  76.         bAlwaysOnTop = 1;
  77.         SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  78.       }
  79.       break;
  80.     case IDM_VIEW_MINTOTRAY:
  81.       bMinimizeToTray =(bMinimizeToTray) ? FALSE : TRUE;
  82.       break;
  83.     case IDM_VIEW_TRANSPARENT:
  84.       bTransparentMode =(bTransparentMode) ? FALSE : TRUE;
  85.       SetWindowTransparentMode(hwnd,bTransparentMode);
  86.       break;
  87.     case IDM_VIEW_SHOWFILENAMEONLY:
  88.       iPathNameFormat = 0;
  89.       lstrcpy(szTitleExcerpt,L"");
  90.       SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  91.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  92.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  93.       break;
  94.     case IDM_VIEW_SHOWFILENAMEFIRST:
  95.       iPathNameFormat = 1;
  96.       lstrcpy(szTitleExcerpt,L"");
  97.       SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  98.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  99.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  100.       break;
  101.     case IDM_VIEW_SHOWFULLPATH:
  102.       iPathNameFormat = 2;
  103.       lstrcpy(szTitleExcerpt,L"");
  104.       SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  105.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  106.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  107.       break;
  108.     case IDM_VIEW_SHOWEXCERPT:
  109.       EditGetExcerpt(hwndEdit,szTitleExcerpt,COUNTOF(szTitleExcerpt));
  110.       SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  111.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  112.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  113.       break;
  114.     case IDM_VIEW_NOSAVERECENT:
  115.       bSaveRecentFiles = (bSaveRecentFiles) ? FALSE : TRUE;
  116.       break;
  117.     case IDM_VIEW_NOSAVEFINDREPL:
  118.       bSaveFindReplace = (bSaveFindReplace) ? FALSE : TRUE;
  119.       break;
  120.     case IDM_VIEW_SAVEBEFORERUNNINGTOOLS:
  121.       bSaveBeforeRunningTools = (bSaveBeforeRunningTools) ? FALSE : TRUE;
  122.       break;
  123.     case IDM_VIEW_CHANGENOTIFY:
  124.       if (ChangeNotifyDlg(hwnd))
  125.         InstallFileWatching(szCurFile);
  126.       break;
  127.     case IDM_VIEW_NOESCFUNC:
  128.       iEscFunction = 0;
  129.       break;
  130.     case IDM_VIEW_ESCMINIMIZE:
  131.       iEscFunction = 1;
  132.       break;
  133.     case IDM_VIEW_ESCEXIT:
  134.       iEscFunction = 2;
  135.       break;
  136.     case IDM_VIEW_SAVESETTINGS:
  137.       bSaveSettings = (bSaveSettings) ? FALSE : TRUE;
  138.       break;
  139.     case IDM_VIEW_SAVESETTINGSNOW:
  140.       {
  141.         BOOL bCreateFailure = FALSE;
  142.         if (lstrlen(szIniFile) == 0) {
  143.           if (lstrlen(szIniFile2) > 0) {
  144.             if (CreateIniFileEx(szIniFile2)) {
  145.               lstrcpy(szIniFile,szIniFile2);
  146.               lstrcpy(szIniFile2,L"");
  147.             }
  148.             else
  149.               bCreateFailure = TRUE;
  150.           }
  151.           else
  152.             break;
  153.         }
  154.         if (!bCreateFailure) {
  155.           if (WritePrivateProfileString(L"Settings",L"WriteTest",L"ok",szIniFile)) {
  156.             BeginWaitCursor();
  157.             StatusSetTextID(hwndStatus,STATUS_HELP,IDS_SAVINGSETTINGS);
  158.             StatusSetSimple(hwndStatus,TRUE);
  159.             InvalidateRect(hwndStatus,NULL,TRUE);
  160.             UpdateWindow(hwndStatus);
  161.             SaveSettings(TRUE);
  162.             StatusSetSimple(hwndStatus,FALSE);
  163.             EndWaitCursor();
  164.             MsgBox(MBINFO,IDS_SAVEDSETTINGS);
  165.           }
  166.           else {
  167.             dwLastIOError = GetLastError();
  168.             MsgBox(MBINFO,IDS_WRITEINI_FAIL);
  169.           }
  170.         }
  171.         else
  172.           MsgBox(MBINFO,IDS_CREATEINI_FAIL);
  173.       }
  174.       break;
  175.     case IDM_HELP_ABOUT:
  176.       ThemedDialogBox(g_hInstance,MAKEINTRESOURCE(IDD_ABOUT),
  177.         hwnd,AboutDlgProc);
  178.       break;
  179.     case CMD_ESCAPE:
  180.       if (iEscFunction == 1)
  181.         SendMessage(hwnd,WM_SYSCOMMAND,SC_MINIMIZE,0);
  182.       else if (iEscFunction == 2)
  183.         SendMessage(hwnd,WM_CLOSE,0,0);
  184.       break;
  185.     case CMD_SHIFTESC:
  186.       if (FileSave(TRUE,FALSE,FALSE,FALSE))
  187.         SendMessage(hwnd,WM_CLOSE,0,0);
  188.       break;
  189.     // Newline with toggled auto indent setting
  190.     case CMD_CTRLENTER:
  191.       bAutoIndent = (bAutoIndent) ? 0 : 1;
  192.       SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  193.       bAutoIndent = (bAutoIndent) ? 0 : 1;
  194.       break;
  195.     case CMD_CTRLBACK:
  196.       {
  197.         int iPos        = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  198.         int iAnchor     = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  199.         int iLine       = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iPos,0);
  200.         int iStartPos   = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
  201.         int iEndPos     = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
  202.         int iIndentPos  = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,(WPARAM)iLine,0);
  203.         if (iPos != iAnchor)
  204.           SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos,(LPARAM)iPos);
  205.         else {
  206.           if (iPos == iStartPos)
  207.             SendMessage(hwndEdit,SCI_DELETEBACK,0,0);
  208.           else if (iPos <= iIndentPos)
  209.             SendMessage(hwndEdit,SCI_DELLINELEFT,0,0);
  210.           else
  211.             SendMessage(hwndEdit,SCI_DELWORDLEFT,0,0);
  212.         }
  213.       }
  214.       break;
  215.     case CMD_CTRLDEL:
  216.       {
  217.         int iPos        = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  218.         int iAnchor     = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  219.         int iLine       = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iPos,0);
  220.         int iStartPos   = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
  221.         int iEndPos     = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
  222.         if (iPos != iAnchor)
  223.           SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos,(LPARAM)iPos);
  224.         else {
  225.           if (iStartPos != iEndPos)
  226.             SendMessage(hwndEdit,SCI_DELWORDRIGHT,0,0);
  227.           else // iStartPos == iEndPos
  228.             SendMessage(hwndEdit,SCI_LINEDELETE,0,0);
  229.         }
  230.       }
  231.       break;
  232.     case CMD_CTRLTAB:
  233.       SendMessage(hwndEdit,SCI_SETTABINDENTS,FALSE,0);
  234.       SendMessage(hwndEdit,SCI_SETUSETABS,TRUE,0);
  235.       SendMessage(hwndEdit,SCI_TAB,0,0);
  236.       SendMessage(hwndEdit,SCI_SETUSETABS,!bTabsAsSpaces,0);
  237.       SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
  238.       break;
  239.     case CMD_RELOADANSI:
  240.       {
  241.         WCHAR tchCurFile2[MAX_PATH];
  242.         if (lstrlen(szCurFile)) {
  243.           iSrcEncoding = CPI_DEFAULT;
  244.           lstrcpy(tchCurFile2,szCurFile);
  245.           FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
  246.         }
  247.       }
  248.       break;
  249.     case CMD_RELOADOEM:
  250.       {
  251.         WCHAR tchCurFile2[MAX_PATH];
  252.         if (lstrlen(szCurFile)) {
  253.           iSrcEncoding = CPI_OEM;
  254.           lstrcpy(tchCurFile2,szCurFile);
  255.           FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
  256.         }
  257.       }
  258.       break;
  259.     case CMD_RELOADASCIIASUTF8:
  260.       {
  261.         WCHAR tchCurFile2[MAX_PATH];
  262.         BOOL _bLoadASCIIasUTF8 = bLoadASCIIasUTF8;
  263.         if (lstrlen(szCurFile)) {
  264.           bLoadASCIIasUTF8 = 1;
  265.           lstrcpy(tchCurFile2,szCurFile);
  266.           FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
  267.           bLoadASCIIasUTF8 = _bLoadASCIIasUTF8;
  268.         }
  269.       }
  270.       break;
  271.     case CMD_RELOADNOFILEVARS:
  272.       {
  273.         WCHAR tchCurFile2[MAX_PATH];
  274.         if (lstrlen(szCurFile)) {
  275.           int _fNoFileVariables = fNoFileVariables;
  276.           BOOL _bNoEncodingTags = bNoEncodingTags;
  277.           fNoFileVariables = 1;
  278.           bNoEncodingTags = 1;
  279.           lstrcpy(tchCurFile2,szCurFile);
  280.           FileLoad(FALSE,FALSE,TRUE,FALSE,tchCurFile2);
  281.           fNoFileVariables = _fNoFileVariables;
  282.           bNoEncodingTags = _bNoEncodingTags;
  283.         }
  284.       }
  285.       break;
  286.     case CMD_LEXDEFAULT:
  287.       Style_SetDefaultLexer(hwndEdit);
  288.       UpdateStatusbar();
  289.       UpdateLineNumberWidth();
  290.       break;
  291.     case CMD_LEXHTML:
  292.       Style_SetHTMLLexer(hwndEdit);
  293.       UpdateStatusbar();
  294.       UpdateLineNumberWidth();
  295.       break;
  296.     case CMD_LEXXML:
  297.       Style_SetXMLLexer(hwndEdit);
  298.       UpdateStatusbar();
  299.       UpdateLineNumberWidth();
  300.       break;
  301.     case CMD_TIMESTAMPS:
  302.       {
  303.         WCHAR wchFind[256] = {0};
  304.         WCHAR wchTemplate[256] = {0};
  305.         WCHAR *pwchSep;
  306.         WCHAR wchReplace[256];
  307.         SYSTEMTIME st;
  308.         struct tm sst;
  309.         UINT cp;
  310.         EDITFINDREPLACE efrTS = { "", "", "", "", SCFIND_REGEXP, 0, 0, 0, 0, 0, hwndEdit };
  311.         IniGetString(L"Settings2",L"TimeStamp",L"\$Date:[^\$]+\$ | $Date: %Y/%m/%d %H:%M:%S $",wchFind,COUNTOF(wchFind));
  312.         if (pwchSep = StrChr(wchFind,L'|')) {
  313.           lstrcpy(wchTemplate,pwchSep+1);
  314.           *pwchSep = 0;
  315.         }
  316.         StrTrim(wchFind,L" ");
  317.         StrTrim(wchTemplate,L" ");
  318.         if (lstrlen(wchFind) == 0 || lstrlen(wchTemplate) == 0)
  319.           break;
  320.         GetLocalTime(&st);
  321.         sst.tm_isdst = -1;
  322.         sst.tm_sec   = (int)st.wSecond;
  323.         sst.tm_min   = (int)st.wMinute;
  324.         sst.tm_hour  = (int)st.wHour;
  325.         sst.tm_mday  = (int)st.wDay;
  326.         sst.tm_mon   = (int)st.wMonth - 1;
  327.         sst.tm_year  = (int)st.wYear - 1900;
  328.         sst.tm_wday  = (int)st.wDayOfWeek;
  329.         mktime(&sst);
  330.         wcsftime(wchReplace,COUNTOF(wchReplace),wchTemplate,&sst);
  331.         cp = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
  332.         WideCharToMultiByte(cp,0,wchFind,-1,efrTS.szFind,COUNTOF(efrTS.szFind),NULL,NULL);
  333.         WideCharToMultiByte(cp,0,wchReplace,-1,efrTS.szReplace,COUNTOF(efrTS.szReplace),NULL,NULL);
  334.         if (SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0) !=
  335.             SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0))
  336.           EditReplaceAllInSelection(hwndEdit,&efrTS,TRUE);
  337.         else
  338.           EditReplaceAll(hwndEdit,&efrTS,TRUE);
  339.       }
  340.       break;
  341.     case CMD_WEBACTION1:
  342.     case CMD_WEBACTION2:
  343.       {
  344.         BOOL  bCmdEnabled = FALSE;
  345.         LPWSTR lpszTemplateName;
  346.         WCHAR  szCmdTemplate[256];
  347.         char  mszSelection[512] = { 0 };
  348.         DWORD cchSelection;
  349.         char  *lpsz;
  350.         LPWSTR lpszCommand;
  351.         LPWSTR lpszArgs;
  352.         SHELLEXECUTEINFO sei;
  353.         WCHAR wchDirectory[MAX_PATH] = L"";
  354.         lpszTemplateName = (LOWORD(wParam) == CMD_WEBACTION1) ? L"WebTemplate1" : L"WebTemplate2";
  355.         bCmdEnabled = IniGetString(L"Settings2",lpszTemplateName,L"",szCmdTemplate,COUNTOF(szCmdTemplate));
  356.         if (bCmdEnabled) {
  357.           cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  358.                           SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  359.           if (cchSelection > 0 && cchSelection <= 500 && SendMessage(hwndEdit,SCI_GETSELTEXT,0,0) < COUNTOF(mszSelection))
  360.           {
  361.             SendMessage(hwndEdit,SCI_GETSELTEXT,0,(LPARAM)mszSelection);
  362.             mszSelection[cchSelection] = 0; // zero terminate
  363.             // Check lpszSelection and truncate bad WCHARs
  364.             lpsz = StrChrA(mszSelection,13);
  365.             if (lpsz) *lpsz = '';
  366.             lpsz = StrChrA(mszSelection,10);
  367.             if (lpsz) *lpsz = '';
  368.             lpsz = StrChrA(mszSelection,9);
  369.             if (lpsz) *lpsz = '';
  370.             if (lstrlenA(mszSelection)) {
  371.               WCHAR wszSelection[512];
  372.               UINT uCP = (SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
  373.               MultiByteToWideChar(uCP,0,mszSelection,-1,wszSelection,COUNTOF(wszSelection));
  374.               lpszCommand = GlobalAlloc(GPTR,sizeof(WCHAR)*(512+COUNTOF(szCmdTemplate)+MAX_PATH+32));
  375.               wsprintf(lpszCommand,szCmdTemplate,wszSelection);
  376.               ExpandEnvironmentStringsEx(lpszCommand,GlobalSize(lpszCommand)/sizeof(WCHAR));
  377.               lpszArgs = GlobalAlloc(GPTR,GlobalSize(lpszCommand));
  378.               ExtractFirstArgument(lpszCommand,lpszCommand,lpszArgs);
  379.               if (lstrlen(szCurFile)) {
  380.                 lstrcpy(wchDirectory,szCurFile);
  381.                 PathRemoveFileSpec(wchDirectory);
  382.               }
  383.               ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  384.               sei.cbSize = sizeof(SHELLEXECUTEINFO);
  385.               sei.fMask = /*SEE_MASK_NOZONECHECKS*/0x00800000;
  386.               sei.hwnd = NULL;
  387.               sei.lpVerb = NULL;
  388.               sei.lpFile = lpszCommand;
  389.               sei.lpParameters = lpszArgs;
  390.               sei.lpDirectory = wchDirectory;
  391.               sei.nShow = SW_SHOWNORMAL;
  392.               ShellExecuteEx(&sei);
  393.               GlobalFree(lpszCommand);
  394.               GlobalFree(lpszArgs);
  395.             }
  396.           }
  397.         }
  398.       }
  399.       break;
  400.     case CMD_FINDNEXTSEL:
  401.     case CMD_FINDPREVSEL:
  402.     case IDM_EDIT_SAVEFIND:
  403.       {
  404.         int cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  405.                              SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  406.         if (cchSelection == 0)
  407.         {
  408.           SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_SELECTWORD,1),0);
  409.           cchSelection = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  410.                            SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  411.         }
  412.         if (cchSelection > 0 && cchSelection <= 500 && SendMessage(hwndEdit,SCI_GETSELTEXT,0,0) < 512)
  413.         {
  414.           char  mszSelection[512];
  415.           char  *lpsz;
  416.           SendMessage(hwndEdit,SCI_GETSELTEXT,0,(LPARAM)mszSelection);
  417.           mszSelection[cchSelection] = 0; // zero terminate
  418.           // Check lpszSelection and truncate newlines
  419.           lpsz = StrChrA(mszSelection,'n');
  420.           if (lpsz) *lpsz = '';
  421.           lpsz = StrChrA(mszSelection,'r');
  422.           if (lpsz) *lpsz = '';
  423.           cpLastFind = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
  424.           lstrcpyA(efrData.szFind,mszSelection);
  425.           if (cpLastFind != SC_CP_UTF8)
  426.           {
  427.             WCHAR wszBuf[512];
  428.             MultiByteToWideChar(cpLastFind,0,mszSelection,-1,wszBuf,COUNTOF(wszBuf));
  429.             WideCharToMultiByte(CP_UTF8,0,wszBuf,-1,efrData.szFindUTF8,COUNTOF(efrData.szFindUTF8),NULL,NULL);
  430.           }
  431.           else
  432.             lstrcpyA(efrData.szFindUTF8,mszSelection);
  433.           efrData.fuFlags &= (~SCFIND_REGEXP);
  434.           efrData.bTransformBS = FALSE;
  435.           switch (LOWORD(wParam)) {
  436.             case IDM_EDIT_SAVEFIND:
  437.               break;
  438.             case CMD_FINDNEXTSEL:
  439.               EditFindNext(hwndEdit,&efrData,FALSE);
  440.               break;
  441.             case CMD_FINDPREVSEL:
  442.               EditFindPrev(hwndEdit,&efrData,FALSE);
  443.               break;
  444.           }
  445.         }
  446.       }
  447.       break;
  448.     case CMD_INCLINELIMIT:
  449.     case CMD_DECLINELIMIT:
  450.       if (!bMarkLongLines)
  451.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_LONGLINEMARKER,1),0);
  452.       else {
  453.         if (LOWORD(wParam) == CMD_INCLINELIMIT)
  454.           iLongLinesLimit++;
  455.         else
  456.           iLongLinesLimit--;
  457.         iLongLinesLimit = max(min(iLongLinesLimit,4096),0);
  458.         SendMessage(hwndEdit,SCI_SETEDGECOLUMN,iLongLinesLimit,0);
  459.         UpdateStatusbar();
  460.         iLongLinesLimitG = iLongLinesLimit;
  461.       }
  462.       break;
  463.     case CMD_STRINGIFY:
  464.       EditEncloseSelection(hwndEdit,L"'",L"'");
  465.       break;
  466.     case CMD_STRINGIFY2:
  467.       EditEncloseSelection(hwndEdit,L""",L""");
  468.       break;
  469.     case CMD_EMBRACE:
  470.       EditEncloseSelection(hwndEdit,L"(",L")");
  471.       break;
  472.     case CMD_EMBRACE2:
  473.       EditEncloseSelection(hwndEdit,L"[",L"]");
  474.       break;
  475.     case CMD_EMBRACE3:
  476.       EditEncloseSelection(hwndEdit,L"{",L"}");
  477.       break;
  478.     case CMD_EMBRACE4:
  479.       EditEncloseSelection(hwndEdit,L"`",L"`");
  480.       break;
  481.     case CMD_INCREASENUM:
  482.       EditModifyNumber(hwndEdit,TRUE);
  483.       break;
  484.     case CMD_DECREASENUM:
  485.       EditModifyNumber(hwndEdit,FALSE);
  486.       break;
  487.     case CMD_TOGGLETITLE:
  488.       EditGetExcerpt(hwndEdit,szTitleExcerpt,COUNTOF(szTitleExcerpt));
  489.       SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  490.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  491.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  492.       break;
  493.     case CMD_JUMP2SELSTART:
  494.       if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
  495.         int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  496.         int iCursorPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  497.         if (iCursorPos > iAnchorPos) {
  498.           SendMessage(hwndEdit,SCI_SETSEL,iCursorPos,iAnchorPos);
  499.           SendMessage(hwndEdit,SCI_CHOOSECARETX,0,0);
  500.         }
  501.       }
  502.       break;
  503.     case CMD_JUMP2SELEND:
  504.       if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
  505.         int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  506.         int iCursorPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  507.         if (iCursorPos < iAnchorPos) {
  508.           SendMessage(hwndEdit,SCI_SETSEL,iCursorPos,iAnchorPos);
  509.           SendMessage(hwndEdit,SCI_CHOOSECARETX,0,0);
  510.         }
  511.       }
  512.       break;
  513.     case CMD_COPYPATHNAME: {
  514.         WCHAR *pszCopy;
  515.         WCHAR tchUntitled[32];
  516.         if (lstrlen(szCurFile))
  517.           pszCopy = szCurFile;
  518.         else {
  519.           GetString(IDS_UNTITLED,tchUntitled,COUNTOF(tchUntitled));
  520.           pszCopy = tchUntitled;
  521.         }
  522.         if (OpenClipboard(hwnd)) {
  523.           HANDLE hData;
  524.           WCHAR *pData;
  525.           EmptyClipboard();
  526.           hData = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(WCHAR) * (lstrlen(pszCopy) + 1));
  527.           pData = GlobalLock(hData);
  528.           StrCpyN(pData,pszCopy,GlobalSize(hData) / sizeof(WCHAR));
  529.           GlobalUnlock(hData);
  530.           SetClipboardData(CF_UNICODETEXT,hData);
  531.           CloseClipboard();
  532.         }
  533.       }
  534.       break;
  535.     case CMD_OPENINIFILE:
  536.       if (lstrlen(szIniFile)) {
  537.         CreateIniFile();
  538.         FileLoad(FALSE,FALSE,FALSE,FALSE,szIniFile);
  539.       }
  540.       break;
  541.     case IDT_FILE_NEW:
  542.       if (IsCmdEnabled(hwnd,IDM_FILE_NEW))
  543.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_NEW,1),0);
  544.       else
  545.         MessageBeep(0);
  546.       break;
  547.     case IDT_FILE_OPEN:
  548.       if (IsCmdEnabled(hwnd,IDM_FILE_OPEN))
  549.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_OPEN,1),0);
  550.       else
  551.         MessageBeep(0);
  552.       break;
  553.     case IDT_FILE_BROWSE:
  554.       if (IsCmdEnabled(hwnd,IDM_FILE_BROWSE))
  555.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_BROWSE,1),0);
  556.       else
  557.         MessageBeep(0);
  558.       break;
  559.     case IDT_FILE_SAVE:
  560.       if (IsCmdEnabled(hwnd,IDM_FILE_SAVE))
  561.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVE,1),0);
  562.       else
  563.         MessageBeep(0);
  564.       break;
  565.     case IDT_EDIT_UNDO:
  566.       if (IsCmdEnabled(hwnd,IDM_EDIT_UNDO))
  567.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_UNDO,1),0);
  568.       else
  569.         MessageBeep(0);
  570.       break;
  571.     case IDT_EDIT_REDO:
  572.       if (IsCmdEnabled(hwnd,IDM_EDIT_REDO))
  573.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REDO,1),0);
  574.       else
  575.         MessageBeep(0);
  576.       break;
  577.     case IDT_EDIT_CUT:
  578.       if (IsCmdEnabled(hwnd,IDM_EDIT_CUT))
  579.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_CUT,1),0);
  580.       else
  581.         MessageBeep(0);
  582.       break;
  583.     case IDT_EDIT_COPY:
  584.       if (IsCmdEnabled(hwnd,IDM_EDIT_COPY))
  585.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_COPY,1),0);
  586.       else
  587.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_COPYALL,1),0);
  588.       break;
  589.     case IDT_EDIT_PASTE:
  590.       if (IsCmdEnabled(hwnd,IDM_EDIT_PASTE))
  591.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_PASTE,1),0);
  592.       else
  593.         MessageBeep(0);
  594.       break;
  595.     case IDT_EDIT_FIND:
  596.       if (IsCmdEnabled(hwnd,IDM_EDIT_FIND))
  597.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_FIND,1),0);
  598.       else
  599.         MessageBeep(0);
  600.       break;
  601.     case IDT_EDIT_REPLACE:
  602.       if (IsCmdEnabled(hwnd,IDM_EDIT_REPLACE))
  603.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REPLACE,1),0);
  604.       else
  605.         MessageBeep(0);
  606.       break;
  607.     case IDT_VIEW_WORDWRAP:
  608.       if (IsCmdEnabled(hwnd,IDM_VIEW_WORDWRAP))
  609.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_WORDWRAP,1),0);
  610.       else
  611.         MessageBeep(0);
  612.       break;
  613.     case IDT_VIEW_ZOOMIN:
  614.       if (IsCmdEnabled(hwnd,IDM_VIEW_ZOOMIN))
  615.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_ZOOMIN,1),0);
  616.       else
  617.         MessageBeep(0);
  618.       break;
  619.     case IDT_VIEW_ZOOMOUT:
  620.       if (IsCmdEnabled(hwnd,IDM_VIEW_ZOOMOUT))
  621.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_ZOOMOUT,1),0);
  622.       else
  623.         MessageBeep(0);
  624.       break;
  625.     case IDT_VIEW_SCHEME:
  626.       if (IsCmdEnabled(hwnd,IDM_VIEW_SCHEME))
  627.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEME,1),0);
  628.       else
  629.         MessageBeep(0);
  630.       break;
  631.     case IDT_VIEW_SCHEMECONFIG:
  632.       if (IsCmdEnabled(hwnd,IDM_VIEW_SCHEMECONFIG))
  633.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEMECONFIG,1),0);
  634.       else
  635.         MessageBeep(0);
  636.       break;
  637.     case IDT_FILE_EXIT:
  638.       SendMessage(hwnd,WM_CLOSE,0,0);
  639.       break;
  640.     case IDT_FILE_SAVEAS:
  641.       if (IsCmdEnabled(hwnd,IDM_FILE_SAVEAS))
  642.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVEAS,1),0);
  643.       else
  644.         MessageBeep(0);
  645.       break;
  646.     case IDT_FILE_SAVECOPY:
  647.       if (IsCmdEnabled(hwnd,IDM_FILE_SAVECOPY))
  648.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_SAVECOPY,1),0);
  649.       else
  650.         MessageBeep(0);
  651.       break;
  652.     case IDT_EDIT_CLEAR:
  653.       if (IsCmdEnabled(hwnd,IDM_EDIT_CLEAR))
  654.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_CLEAR,1),0);
  655.       else
  656.         SendMessage(hwndEdit,SCI_CLEARALL,0,0);
  657.       break;
  658.     case IDT_FILE_PRINT:
  659.       if (IsCmdEnabled(hwnd,IDM_FILE_PRINT))
  660.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_PRINT,1),0);
  661.       else
  662.         MessageBeep(0);
  663.       break;
  664.     case IDT_FILE_OPENFAV:
  665.       if (IsCmdEnabled(hwnd,IDM_FILE_OPENFAV))
  666.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_OPENFAV,1),0);
  667.       else
  668.         MessageBeep(0);
  669.       break;
  670.     case IDT_FILE_ADDTOFAV:
  671.       if (IsCmdEnabled(hwnd,IDM_FILE_ADDTOFAV))
  672.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_FILE_ADDTOFAV,1),0);
  673.       else
  674.         MessageBeep(0);
  675.       break;
  676.   }
  677.   return(0);
  678. }
  679. //=============================================================================
  680. //
  681. //  MsgNotify() - Handles WM_NOTIFY
  682. //
  683. //
  684. LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam)
  685. {
  686.   LPNMHDR pnmh = (LPNMHDR)lParam;
  687.   struct SCNotification* scn = (struct SCNotification*)lParam;
  688.   switch(pnmh->idFrom)
  689.   {
  690.     case IDC_EDIT:
  691.       switch(pnmh->code)
  692.       {
  693.         case SCN_UPDATEUI:
  694.           UpdateToolbar();
  695.           UpdateStatusbar();
  696.           // Invalidate invalid selections
  697.           #pragma message("TODO: Remove check for invalid selections once fixed in Scintilla")
  698.           if (SendMessage(hwndEdit,SCI_GETSELECTIONS,0,0) > 1 &&
  699.               SendMessage(hwndEdit,SCI_GETSELECTIONMODE,0,0) != SC_SEL_RECTANGLE) {
  700.             int iCurPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  701.             SendMessage(hwndEdit,WM_CANCELMODE,0,0);
  702.             SendMessage(hwndEdit,SCI_CLEARSELECTIONS,0,0);
  703.             SendMessage(hwndEdit,SCI_SETSELECTION,(WPARAM)iCurPos,(LPARAM)iCurPos);
  704.           }
  705.           // Brace Match
  706.           if (bMatchBraces)
  707.           {
  708.             int iPos;
  709.             char c;
  710.             int iEndStyled = SendMessage(hwndEdit,SCI_GETENDSTYLED,0,0);
  711.             if (iEndStyled < SendMessage(hwndEdit,SCI_GETLENGTH,0,0)) {
  712.               int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iEndStyled,0);
  713.               int iEndStyled = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,iLine,0);
  714.               SendMessage(hwndEdit,SCI_COLOURISE,iEndStyled,-1);
  715.             }
  716.             iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  717.             c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  718.             if (StrChrA("()[]{}",c)) {
  719.               int iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  720.               if (iBrace2 != -1) {
  721.                 int col1 = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0);
  722.                 int col2 = SendMessage(hwndEdit,SCI_GETCOLUMN,iBrace2,0);
  723.                 SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,iPos,iBrace2);
  724.                 SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,min(col1,col2),0);
  725.               }
  726.               else {
  727.                 SendMessage(hwndEdit,SCI_BRACEBADLIGHT,iPos,0);
  728.                 SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
  729.               }
  730.             }
  731.             // Try one before
  732.             else
  733.             {
  734.               iPos = SendMessage(hwndEdit,SCI_POSITIONBEFORE,iPos,0);
  735.               c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  736.               if (StrChrA("()[]{}",c)) {
  737.                 int iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  738.                 if (iBrace2 != -1) {
  739.                   int col1 = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0);
  740.                   int col2 = SendMessage(hwndEdit,SCI_GETCOLUMN,iBrace2,0);
  741.                   SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,iPos,iBrace2);
  742.                   SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,min(col1,col2),0);
  743.                 }
  744.                 else {
  745.                   SendMessage(hwndEdit,SCI_BRACEBADLIGHT,iPos,0);
  746.                   SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
  747.                 }
  748.               }
  749.               else {
  750.                 SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,(WPARAM)-1,(LPARAM)-1);
  751.                 SendMessage(hwndEdit,SCI_SETHIGHLIGHTGUIDE,0,0);
  752.               }
  753.             }
  754.           }
  755.           break;
  756.         case SCN_CHARADDED:
  757.           // Auto indent
  758.           if (bAutoIndent && (scn->ch == 'x0D' || scn->ch == 'x0A'))
  759.           {
  760.             // in CRLF mode handle LF only...
  761.             if ((SC_EOL_CRLF == iEOLMode && scn->ch != 'x0A') || SC_EOL_CRLF != iEOLMode)
  762.             {
  763.               char *pLineBuf;
  764.               char *pPos;
  765.               //int  iIndentLen;
  766.               int iCurPos    = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  767.               //int iAnchorPos = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  768.               int iCurLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
  769.               int iLineLength = SendMessage(hwndEdit,SCI_LINELENGTH,iCurLine,0);
  770.               //int iIndentBefore = SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine-1,0);
  771.               if (iCurLine > 0/* && iLineLength <= 2*/)
  772.               {
  773.                 int iPrevLineLength = SendMessage(hwndEdit,SCI_LINELENGTH,iCurLine-1,0);
  774.                 if (pLineBuf = GlobalAlloc(GPTR,iPrevLineLength+1))
  775.                 {
  776.                   SendMessage(hwndEdit,SCI_GETLINE,iCurLine-1,(LPARAM)pLineBuf);
  777.                   *(pLineBuf+iPrevLineLength) = '';
  778.                   for (pPos = pLineBuf; *pPos; pPos++) {
  779.                     if (*pPos != ' ' && *pPos != 't')
  780.                       *pPos = '';
  781.                   }
  782.                   if (*pLineBuf) {
  783.                     //int iPrevLineStartPos;
  784.                     //int iPrevLineEndPos;
  785.                     //int iPrevLineIndentPos;
  786.                     SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  787.                     SendMessage(hwndEdit,SCI_ADDTEXT,lstrlenA(pLineBuf),(LPARAM)pLineBuf);
  788.                     SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  789.                     //iPrevLineStartPos  = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,(WPARAM)iCurLine-1,0);
  790.                     //iPrevLineEndPos    = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,(WPARAM)iCurLine-1,0);
  791.                     //iPrevLineIndentPos = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,(WPARAM)iCurLine-1,0);
  792.                     //if (iPrevLineEndPos == iPrevLineIndentPos) {
  793.                     //  SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  794.                     //  SendMessage(hwndEdit,SCI_SETTARGETSTART,(WPARAM)iPrevLineStartPos,0);
  795.                     //  SendMessage(hwndEdit,SCI_SETTARGETEND,(WPARAM)iPrevLineEndPos,0);
  796.                     //  SendMessage(hwndEdit,SCI_REPLACETARGET,0,(LPARAM)"");
  797.                     //  SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  798.                     //}
  799.                   }
  800.                   GlobalFree(pLineBuf);
  801.                   //int iIndent = SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine,0);
  802.                   //SendMessage(hwndEdit,SCI_SETLINEINDENTATION,(WPARAM)iCurLine,(LPARAM)iIndentBefore);
  803.                   //iIndentLen = /*- iIndent +*/ SendMessage(hwndEdit,SCI_GETLINEINDENTATION,(WPARAM)iCurLine,0);
  804.                   //if (iIndentLen > 0)
  805.                   //  SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iAnchorPos+iIndentLen,(LPARAM)iCurPos+iIndentLen);
  806.                 }
  807.               }
  808.             }
  809.           }
  810.           // Auto close tags
  811.           else if (bAutoCloseTags && scn->ch == '>')
  812.           {
  813.             int iLexer = SendMessage(hwndEdit,SCI_GETLEXER,0,0);
  814.             if (/*iLexer == SCLEX_HTML || iLexer == SCLEX_XML*/ 1)
  815.             {
  816.               char tchBuf[512];
  817.               char tchIns[516] = "</";
  818.               int  cchIns = 2;
  819.               int  iCurPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  820.               int  iHelper = iCurPos - (COUNTOF(tchBuf) - 1);
  821.               int  iStartPos = max(0,iHelper);
  822.               int  iSize = iCurPos - iStartPos;
  823.               if (iSize >= 3) {
  824.                 struct TextRange tr = { { iStartPos, iCurPos }, tchBuf };
  825.                 SendMessage(hwndEdit,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
  826.                 if (tchBuf[iSize - 2] != '/') {
  827.                   const char* pBegin = &tchBuf[0];
  828.                   const char* pCur = &tchBuf[iSize - 2];
  829.                   while (pCur > pBegin && *pCur != '<' && *pCur != '>')
  830.                     --pCur;
  831.                   if (*pCur == '<') {
  832.                     pCur++;
  833.                     while (StrChrA(":_-.", *pCur) || IsCharAlphaNumericA(*pCur)) {
  834.                       tchIns[cchIns++] = *pCur;
  835.                       pCur++;
  836.                     }
  837.                   }
  838.                   tchIns[cchIns++] = '>';
  839.                   tchIns[cchIns] = 0;
  840.                   if (cchIns > 3 &&
  841.                       lstrcmpiA(tchIns,"</base>") &&
  842.                       lstrcmpiA(tchIns,"</bgsound>") &&
  843.                       lstrcmpiA(tchIns,"</br>") &&
  844.                       lstrcmpiA(tchIns,"</embed>") &&
  845.                       lstrcmpiA(tchIns,"</hr>") &&
  846.                       lstrcmpiA(tchIns,"</img>") &&
  847.                       lstrcmpiA(tchIns,"</input>") &&
  848.                       lstrcmpiA(tchIns,"</link>") &&
  849.                       lstrcmpiA(tchIns,"</meta>"))
  850.                   {
  851.                     SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  852.                     SendMessage(hwndEdit,SCI_REPLACESEL,0,(LPARAM)tchIns);
  853.                     SendMessage(hwndEdit,SCI_SETSEL,iCurPos,iCurPos);
  854.                     SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  855.                   }
  856.                 }
  857.               }
  858.             }
  859.           }
  860.           break;
  861.         case SCN_MODIFIED:
  862.         case SCN_ZOOM:
  863.           UpdateLineNumberWidth();
  864.           break;
  865.         case SCN_SAVEPOINTREACHED:
  866.           bModified = FALSE;
  867.           SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  868.             iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  869.             IDS_READONLY,bReadOnly,szTitleExcerpt);
  870.           break;
  871.         case SCN_SAVEPOINTLEFT:
  872.           bModified = TRUE;
  873.           SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  874.             iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  875.             IDS_READONLY,bReadOnly,szTitleExcerpt);
  876.           break;
  877.       }
  878.       break;
  879.     case IDC_TOOLBAR:
  880.       switch(pnmh->code)
  881.       {
  882.         case TBN_ENDADJUST:
  883.           UpdateToolbar();
  884.           break;
  885.         case TBN_QUERYDELETE:
  886.         case TBN_QUERYINSERT:
  887.           return TRUE;
  888.         case TBN_GETBUTTONINFO:
  889.           {
  890.             if (((LPTBNOTIFY)lParam)->iItem < COUNTOF(tbbMainWnd))
  891.             {
  892.               WCHAR tch[256];
  893.               GetString(tbbMainWnd[((LPTBNOTIFY)lParam)->iItem].idCommand,tch,COUNTOF(tch));
  894.               lstrcpyn(((LPTBNOTIFY)lParam)->pszText,/*StrChr(tch,L'n')+1*/tch,((LPTBNOTIFY)lParam)->cchText);
  895.               CopyMemory(&((LPTBNOTIFY)lParam)->tbButton,&tbbMainWnd[((LPTBNOTIFY)lParam)->iItem],sizeof(TBBUTTON));
  896.               return TRUE;
  897.             }
  898.           }
  899.           return FALSE;
  900.         case TBN_RESET:
  901.           {
  902.             int i; int c = SendMessage(hwndToolbar,TB_BUTTONCOUNT,0,0);
  903.             for (i = 0; i < c; i++)
  904.               SendMessage(hwndToolbar,TB_DELETEBUTTON,0,0);
  905.             SendMessage(hwndToolbar,TB_ADDBUTTONS,NUMINITIALTOOLS,(LPARAM)tbbMainWnd);
  906.             return(0);
  907.           }
  908.       }
  909.       break;
  910.     case IDC_STATUSBAR:
  911.       switch(pnmh->code)
  912.       {
  913.         case NM_CLICK:
  914.           {
  915.             LPNMMOUSE pnmm = (LPNMMOUSE)lParam;
  916.             switch (pnmm->dwItemSpec)
  917.             {
  918.               case STATUS_EOLMODE:
  919.                 SendMessage(hwndEdit,SCI_CONVERTEOLS,SendMessage(hwndEdit,SCI_GETEOLMODE,0,0),0);
  920.                 return TRUE;
  921.               default:
  922.                 return FALSE;
  923.             }
  924.           }
  925.         case NM_DBLCLK:
  926.           {
  927.             int i;
  928.             LPNMMOUSE pnmm = (LPNMMOUSE)lParam;
  929.             switch (pnmm->dwItemSpec)
  930.             {
  931.               case STATUS_CODEPAGE:
  932.                 SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_ENCODING_SELECT,1),0);
  933.                 return TRUE;
  934.               case STATUS_EOLMODE:
  935.                 if (iEOLMode == SC_EOL_CRLF)
  936.                   i = IDM_LINEENDINGS_CRLF;
  937.                 else if (iEOLMode == SC_EOL_LF)
  938.                   i = IDM_LINEENDINGS_LF;
  939.                 else
  940.                   i = IDM_LINEENDINGS_CR;
  941.                 i++;
  942.                 if (i > IDM_LINEENDINGS_CR)
  943.                   i = IDM_LINEENDINGS_CRLF;
  944.                 SendMessage(hwnd,WM_COMMAND,MAKELONG(i,1),0);
  945.                 return TRUE;
  946.               case STATUS_LEXER:
  947.                 SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_VIEW_SCHEME,1),0);
  948.                 return TRUE;
  949.               case STATUS_OVRMODE:
  950.                 SendMessage(hwndEdit,SCI_EDITTOGGLEOVERTYPE,0,0);
  951.                 return TRUE;
  952.               default:
  953.                 return FALSE;
  954.             }
  955.           }
  956.           break;
  957.       }
  958.       break;
  959.     default:
  960.       switch(pnmh->code)
  961.       {
  962.         case TTN_NEEDTEXT:
  963.           {
  964.             WCHAR tch[256];
  965.             if (((LPTOOLTIPTEXT)lParam)->uFlags & TTF_IDISHWND)
  966.             {
  967.               ;
  968.             }
  969.             else
  970.             {
  971.               GetString(pnmh->idFrom,tch,COUNTOF(tch));
  972.               lstrcpyn(((LPTOOLTIPTEXT)lParam)->szText,/*StrChr(tch,L'n')+1*/tch,80);
  973.             }
  974.           }
  975.           break;
  976.       }
  977.       break;
  978.   }
  979.   return(0);
  980. }
  981. //=============================================================================
  982. //
  983. //  LoadSettings()
  984. //
  985. //
  986. void LoadSettings()
  987. {
  988.   WCHAR *pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
  989.   int   cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
  990.   LoadIniSection(L"Settings",pIniSection,cchIniSection);
  991.   bSaveSettings =
  992.     IniSectionGetInt(pIniSection,L"SaveSettings",1);
  993.   if (bSaveSettings) bSaveSettings = 1;
  994.   bSaveRecentFiles =
  995.     IniSectionGetInt(pIniSection,L"SaveRecentFiles",0);
  996.   if (bSaveRecentFiles) bSaveRecentFiles = 1;
  997.   bSaveFindReplace =
  998.     IniSectionGetInt(pIniSection,L"SaveFindReplace",0);
  999.   if (bSaveFindReplace) bSaveFindReplace = 1;
  1000.   efrData.bFindClose = IniSectionGetInt(pIniSection,L"CloseFind",0);
  1001.   if (efrData.bFindClose) efrData.bReplaceClose = TRUE;
  1002.   efrData.bReplaceClose = IniSectionGetInt(pIniSection,L"CloseReplace",0);
  1003.   if (efrData.bReplaceClose) efrData.bReplaceClose = TRUE;
  1004.   efrData.bNoFindWrap = IniSectionGetInt(pIniSection,L"NoFindWrap",0);
  1005.   if (efrData.bNoFindWrap) efrData.bNoFindWrap = TRUE;
  1006.   if (!IniSectionGetString(pIniSection,L"OpenWithDir",L"",
  1007.         tchOpenWithDir,COUNTOF(tchOpenWithDir)))
  1008.     SHGetSpecialFolderPath(NULL,tchOpenWithDir,CSIDL_DESKTOP,TRUE);
  1009.   else
  1010.     PathAbsoluteFromApp(tchOpenWithDir,NULL,COUNTOF(tchOpenWithDir),TRUE);
  1011.   if (!IniSectionGetString(pIniSection,L"Favorites",L"",
  1012.         tchFavoritesDir,COUNTOF(tchFavoritesDir)))
  1013.     SHGetFolderPath(NULL,CSIDL_PERSONAL,NULL,SHGFP_TYPE_CURRENT,tchFavoritesDir);
  1014.   else
  1015.     PathAbsoluteFromApp(tchFavoritesDir,NULL,COUNTOF(tchFavoritesDir),TRUE);
  1016.   iPathNameFormat = IniSectionGetInt(pIniSection,L"PathNameFormat",0);
  1017.   iPathNameFormat = max(min(iPathNameFormat,2),0);
  1018.   fWordWrap = IniSectionGetInt(pIniSection,L"WordWrap",0);
  1019.   if (fWordWrap) fWordWrap = 1;
  1020.   iWordWrapMode = IniSectionGetInt(pIniSection,L"WordWrapMode",0);
  1021.   iWordWrapMode = max(min(iWordWrapMode,1),0);
  1022.   iWordWrapIndent = IniSectionGetInt(pIniSection,L"WordWrapIndent",0);
  1023.   iWordWrapIndent = max(min(iWordWrapIndent,6),0);
  1024.   iWordWrapSymbols = IniSectionGetInt(pIniSection,L"WordWrapSymbols",22);
  1025.   iWordWrapSymbols = max(min(iWordWrapSymbols%10,2),0)+max(min((iWordWrapSymbols%100-iWordWrapSymbols%10)/10,2),0)*10;
  1026.   bShowWordWrapSymbols = IniSectionGetInt(pIniSection,L"ShowWordWrapSymbols",0);
  1027.   if (bShowWordWrapSymbols) bShowWordWrapSymbols = 1;
  1028.   bMatchBraces = IniSectionGetInt(pIniSection,L"MatchBraces",1);
  1029.   if (bMatchBraces) bMatchBraces = 1;
  1030.   bAutoCloseTags = IniSectionGetInt(pIniSection,L"AutoCloseTags",0);
  1031.   if (bAutoCloseTags) bAutoCloseTags = 1;
  1032.   bHiliteCurrentLine = IniSectionGetInt(pIniSection,L"HighlightCurrentLine",0);
  1033.   if (bHiliteCurrentLine) bHiliteCurrentLine = 1;
  1034.   bAutoIndent = IniSectionGetInt(pIniSection,L"AutoIndent",1);
  1035.   if (bAutoIndent) bAutoIndent = 1;
  1036.   bShowIndentGuides = IniSectionGetInt(pIniSection,L"ShowIndentGuides",0);
  1037.   if (bShowIndentGuides) bShowIndentGuides = 1;
  1038.   bTabsAsSpaces = IniSectionGetInt(pIniSection,L"TabsAsSpaces",1);
  1039.   if (bTabsAsSpaces) bTabsAsSpaces = 1;
  1040.   bTabsAsSpacesG = bTabsAsSpaces;
  1041.   bTabIndents = IniSectionGetInt(pIniSection,L"TabIndents",1);
  1042.   if (bTabIndents) bTabIndents = 1;
  1043.   iTabWidth = IniSectionGetInt(pIniSection,L"TabWidth",2);
  1044.   iTabWidth = max(min(iTabWidth,256),1);
  1045.   iTabWidthG = iTabWidth;
  1046.   iIndentWidth = IniSectionGetInt(pIniSection,L"IndentWidth",0);
  1047.   iIndentWidth = max(min(iIndentWidth,256),0);
  1048.   iIndentWidthG = iIndentWidth;
  1049.   bMarkLongLines = IniSectionGetInt(pIniSection,L"MarkLongLines",0);
  1050.   if (bMarkLongLines) bMarkLongLines = 1;
  1051.   iLongLinesLimit = IniSectionGetInt(pIniSection,L"LongLinesLimit",72);
  1052.   iLongLinesLimit = max(min(iLongLinesLimit,4096),0);
  1053.   iLongLinesLimitG = iLongLinesLimit;
  1054.   iLongLineMode = IniSectionGetInt(pIniSection,L"LongLineMode",EDGE_LINE);
  1055.   iLongLineMode = max(min(iLongLineMode,EDGE_BACKGROUND),EDGE_LINE);
  1056.   bShowSelectionMargin = IniSectionGetInt(pIniSection,L"ShowSelectionMargin",0);
  1057.   if (bShowSelectionMargin) bShowSelectionMargin = 1;
  1058.   bShowLineNumbers = IniSectionGetInt(pIniSection,L"ShowLineNumbers",1);
  1059.   if (bShowLineNumbers) bShowLineNumbers = 1;
  1060.   bViewWhiteSpace = IniSectionGetInt(pIniSection,L"ViewWhiteSpace",0);
  1061.   if (bViewWhiteSpace) bViewWhiteSpace = 1;
  1062.   bViewEOLs = IniSectionGetInt(pIniSection,L"ViewEOLs",0);
  1063.   if (bViewEOLs) bViewEOLs = 1;
  1064.   iDefaultEncoding = IniSectionGetInt(pIniSection,L"DefaultEncoding",0);
  1065.   iDefaultEncoding = Encoding_MapIniSetting(TRUE,iDefaultEncoding);
  1066.   if (!Encoding_IsValid(iDefaultEncoding)) iDefaultEncoding = CPI_DEFAULT;
  1067.   bSkipUnicodeDetection = IniSectionGetInt(pIniSection,L"SkipUnicodeDetection",0);
  1068.   if (bSkipUnicodeDetection) bSkipUnicodeDetection = 1;
  1069.   bLoadASCIIasUTF8 = IniSectionGetInt(pIniSection,L"LoadASCIIasUTF8",0);
  1070.   if (bLoadASCIIasUTF8) bLoadASCIIasUTF8 = 1;
  1071.   bNoEncodingTags = IniSectionGetInt(pIniSection,L"NoEncodingTags",0);
  1072.   if (bNoEncodingTags) bNoEncodingTags = 1;
  1073.   iDefaultEOLMode = IniSectionGetInt(pIniSection,L"DefaultEOLMode",0);
  1074.   iDefaultEOLMode = max(min(iDefaultEOLMode,2),0);
  1075.   bFixLineEndings = IniSectionGetInt(pIniSection,L"FixLineEndings",1);
  1076.   if (bFixLineEndings) bFixLineEndings = 1;
  1077.   bAutoStripBlanks = IniSectionGetInt(pIniSection,L"FixTrailingBlanks",0);
  1078.   if (bAutoStripBlanks) bAutoStripBlanks = 1;
  1079.   iPrintHeader = IniSectionGetInt(pIniSection,L"PrintHeader",1);
  1080.   iPrintHeader = max(min(iPrintHeader,3),0);
  1081.   iPrintFooter = IniSectionGetInt(pIniSection,L"PrintFooter",0);
  1082.   iPrintFooter = max(min(iPrintFooter,1),0);
  1083.   iPrintColor = IniSectionGetInt(pIniSection,L"PrintColorMode",3);
  1084.   iPrintColor = max(min(iPrintColor,4),0);
  1085.   iPrintZoom = IniSectionGetInt(pIniSection,L"PrintZoom",10)-10;
  1086.   iPrintZoom = max(min(iPrintZoom,20),-10);
  1087.   pagesetupMargin.left = IniSectionGetInt(pIniSection,L"PrintMarginLeft",-1);
  1088.   pagesetupMargin.left = max(pagesetupMargin.left,-1);
  1089.   pagesetupMargin.top = IniSectionGetInt(pIniSection,L"PrintMarginTop",-1);
  1090.   pagesetupMargin.top = max(pagesetupMargin.top,-1);
  1091.   pagesetupMargin.right = IniSectionGetInt(pIniSection,L"PrintMarginRight",-1);
  1092.   pagesetupMargin.right = max(pagesetupMargin.right,-1);
  1093.   pagesetupMargin.bottom = IniSectionGetInt(pIniSection,L"PrintMarginBottom",-1);
  1094.   pagesetupMargin.bottom = max(pagesetupMargin.bottom,-1);
  1095.   bSaveBeforeRunningTools = IniSectionGetInt(pIniSection,L"SaveBeforeRunningTools",0);
  1096.   if (bSaveBeforeRunningTools) bSaveBeforeRunningTools = 1;
  1097.   iFileWatchingMode = IniSectionGetInt(pIniSection,L"FileWatchingMode",0);
  1098.   iFileWatchingMode = max(min(iFileWatchingMode,2),0);
  1099.   bResetFileWatching = IniSectionGetInt(pIniSection,L"ResetFileWatching",1);
  1100.   if (bResetFileWatching) bResetFileWatching = 1;
  1101.   iEscFunction = IniSectionGetInt(pIniSection,L"EscFunction",0);
  1102.   iEscFunction = max(min(iEscFunction,2),0);
  1103.   bAlwaysOnTop = IniSectionGetInt(pIniSection,L"AlwaysOnTop",0);
  1104.   if (bAlwaysOnTop) bAlwaysOnTop = 1;
  1105.   bMinimizeToTray = IniSectionGetInt(pIniSection,L"MinimizeToTray",0);
  1106.   if (bMinimizeToTray) bMinimizeToTray = 1;
  1107.   bTransparentMode = IniSectionGetInt(pIniSection,L"TransparentMode",0);
  1108.   if (bTransparentMode) bTransparentMode = 1;
  1109.   // Check if SetLayeredWindowAttributes() is available
  1110.   bTransparentModeAvailable =
  1111.     (GetProcAddress(GetModuleHandle(L"User32"),"SetLayeredWindowAttributes") != NULL);
  1112.   IniSectionGetString(pIniSection,L"ToolbarButtons",L"",
  1113.     tchToolbarButtons,COUNTOF(tchToolbarButtons));
  1114.   bShowToolbar = IniSectionGetInt(pIniSection,L"ShowToolbar",1);
  1115.   if (bShowToolbar) bShowToolbar = 1;
  1116.   bShowStatusbar = IniSectionGetInt(pIniSection,L"ShowStatusbar",1);
  1117.   if (bShowStatusbar) bShowStatusbar = 1;
  1118.   cxEncodingDlg = IniSectionGetInt(pIniSection,L"EncodingDlgSizeX",256);
  1119.   cxEncodingDlg = max(cxEncodingDlg,0);
  1120.   cyEncodingDlg = IniSectionGetInt(pIniSection,L"EncodingDlgSizeY",262);
  1121.   cyEncodingDlg = max(cyEncodingDlg,0);
  1122.   cxRecodeDlg = IniSectionGetInt(pIniSection,L"RecodeDlgSizeX",256);
  1123.   cxRecodeDlg = max(cxRecodeDlg,0);
  1124.   cyRecodeDlg = IniSectionGetInt(pIniSection,L"RecodeDlgSizeY",262);
  1125.   cyRecodeDlg = max(cyRecodeDlg,0);
  1126.   cxFileMRUDlg = IniSectionGetInt(pIniSection,L"FileMRUDlgSizeX",412);
  1127.   cxFileMRUDlg = max(cxFileMRUDlg,0);
  1128.   cyFileMRUDlg = IniSectionGetInt(pIniSection,L"FileMRUDlgSizeY",376);
  1129.   cyFileMRUDlg = max(cyFileMRUDlg,0);
  1130.   cxOpenWithDlg = IniSectionGetInt(pIniSection,L"OpenWithDlgSizeX",384);
  1131.   cxOpenWithDlg = max(cxOpenWithDlg,0);
  1132.   cyOpenWithDlg = IniSectionGetInt(pIniSection,L"OpenWithDlgSizeY",386);
  1133.   cyOpenWithDlg = max(cyOpenWithDlg,0);
  1134.   cxFavoritesDlg = IniSectionGetInt(pIniSection,L"FavoritesDlgSizeX",334);
  1135.   cxFavoritesDlg = max(cxFavoritesDlg,0);
  1136.   cyFavoritesDlg = IniSectionGetInt(pIniSection,L"FavoritesDlgSizeY",316);
  1137.   cyFavoritesDlg = max(cyFavoritesDlg,0);
  1138.   xFindReplaceDlg = IniSectionGetInt(pIniSection,L"FindReplaceDlgPosX",0);
  1139.   yFindReplaceDlg = IniSectionGetInt(pIniSection,L"FindReplaceDlgPosY",0);
  1140.   LoadIniSection(L"Settings2",pIniSection,cchIniSection);
  1141.   bStickyWinPos = IniSectionGetInt(pIniSection,L"StickyWindowPosition",0);
  1142.   if (bStickyWinPos) bStickyWinPos = 1;
  1143.   IniSectionGetString(pIniSection,L"DefaultExtension",L"txt",
  1144.     tchDefaultExtension,COUNTOF(tchDefaultExtension));
  1145.   StrTrim(tchDefaultExtension,L" t."");
  1146.   IniSectionGetString(pIniSection,L"DefaultDirectory",L"",
  1147.     tchDefaultDir,COUNTOF(tchDefaultDir));
  1148.   ZeroMemory(tchFileDlgFilters,sizeof(WCHAR)*COUNTOF(tchFileDlgFilters));
  1149.   IniSectionGetString(pIniSection,L"FileDlgFilters",L"",
  1150.     tchFileDlgFilters,COUNTOF(tchFileDlgFilters)-2);
  1151.   iFileCheckInverval = IniSectionGetInt(pIniSection,L"FileCheckInverval",2000);
  1152.   LoadIniSection(L"Toolbar Images",pIniSection,cchIniSection);
  1153.   IniSectionGetString(pIniSection,L"BitmapDefault",L"",
  1154.     tchToolbarBitmap,COUNTOF(tchToolbarBitmap));
  1155.   IniSectionGetString(pIniSection,L"BitmapHot",L"",
  1156.     tchToolbarBitmapHot,COUNTOF(tchToolbarBitmap));
  1157.   IniSectionGetString(pIniSection,L"BitmapDisabled",L"",
  1158.     tchToolbarBitmapDisabled,COUNTOF(tchToolbarBitmap));
  1159.   if (!flagPosParam /*|| bStickyWinPos*/) { // ignore window position if /p was specified
  1160.     WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
  1161.     int ResX = GetSystemMetrics(SM_CXSCREEN);
  1162.     int ResY = GetSystemMetrics(SM_CYSCREEN);
  1163.     wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
  1164.     wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
  1165.     wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
  1166.     wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
  1167.     wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
  1168.     LoadIniSection(L"Window",pIniSection,cchIniSection);
  1169.     wi.x = IniSectionGetInt(pIniSection,tchPosX,CW_USEDEFAULT);
  1170.     wi.y = IniSectionGetInt(pIniSection,tchPosY,CW_USEDEFAULT);
  1171.     wi.cx = IniSectionGetInt(pIniSection,tchSizeX,CW_USEDEFAULT);
  1172.     wi.cy = IniSectionGetInt(pIniSection,tchSizeY,CW_USEDEFAULT);
  1173.     wi.max = IniSectionGetInt(pIniSection,tchMaximized,0);
  1174.     if (wi.max) wi.max = 1;
  1175.   }
  1176.   LocalFree(pIniSection);
  1177.   iDefaultCodePage = 0; {
  1178.     int acp = GetACP();
  1179.     if (acp == 932 || acp == 936 || acp == 949 || acp == 950 || acp == 1361)
  1180.       iDefaultCodePage = acp;
  1181.   }
  1182.   {
  1183.     CHARSETINFO ci;
  1184.     if (TranslateCharsetInfo((DWORD*)iDefaultCodePage,&ci,TCI_SRCCODEPAGE))
  1185.       iDefaultCharSet = ci.ciCharset;
  1186.     else
  1187.       iDefaultCharSet = ANSI_CHARSET;
  1188.   }
  1189.   // Scintilla Styles
  1190.   Style_Load();
  1191. }
  1192. //=============================================================================
  1193. //
  1194. //  SaveSettings()
  1195. //
  1196. //
  1197. void SaveSettings(BOOL bSaveSettingsNow)
  1198. {
  1199.   WCHAR *pIniSection = NULL;
  1200.   int   cchIniSection = 0;
  1201.   WCHAR wchTmp[MAX_PATH];
  1202.   if (lstrlen(szIniFile) == 0)
  1203.     return;
  1204.   CreateIniFile();
  1205.   if (!bSaveSettings && !bSaveSettingsNow) {
  1206.     IniSetInt(L"Settings",L"SaveSettings",bSaveSettings);
  1207.     return;
  1208.   }
  1209.   pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
  1210.   cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
  1211.   IniSectionSetInt(pIniSection,L"SaveSettings",bSaveSettings);
  1212.   IniSectionSetInt(pIniSection,L"SaveRecentFiles",bSaveRecentFiles);
  1213.   IniSectionSetInt(pIniSection,L"SaveFindReplace",bSaveFindReplace);
  1214.   IniSectionSetInt(pIniSection,L"CloseFind",efrData.bFindClose);
  1215.   IniSectionSetInt(pIniSection,L"CloseReplace",efrData.bReplaceClose);
  1216.   IniSectionSetInt(pIniSection,L"NoFindWrap",efrData.bNoFindWrap);
  1217.   PathRelativeToApp(tchOpenWithDir,wchTmp,COUNTOF(wchTmp),FALSE,TRUE,flagPortableMyDocs);
  1218.   IniSectionSetString(pIniSection,L"OpenWithDir",wchTmp);
  1219.   PathRelativeToApp(tchFavoritesDir,wchTmp,COUNTOF(wchTmp),FALSE,TRUE,flagPortableMyDocs);
  1220.   IniSectionSetString(pIniSection,L"Favorites",wchTmp);
  1221.   IniSectionSetInt(pIniSection,L"PathNameFormat",iPathNameFormat);
  1222.   IniSectionSetInt(pIniSection,L"WordWrap",fWordWrap);
  1223.   IniSectionSetInt(pIniSection,L"WordWrapMode",iWordWrapMode);
  1224.   IniSectionSetInt(pIniSection,L"WordWrapIndent",iWordWrapIndent);
  1225.   IniSectionSetInt(pIniSection,L"WordWrapSymbols",iWordWrapSymbols);
  1226.   IniSectionSetInt(pIniSection,L"ShowWordWrapSymbols",bShowWordWrapSymbols);
  1227.   IniSectionSetInt(pIniSection,L"MatchBraces",bMatchBraces);
  1228.   IniSectionSetInt(pIniSection,L"AutoCloseTags",bAutoCloseTags);
  1229.   IniSectionSetInt(pIniSection,L"HighlightCurrentLine",bHiliteCurrentLine);
  1230.   IniSectionSetInt(pIniSection,L"AutoIndent",bAutoIndent);
  1231.   IniSectionSetInt(pIniSection,L"ShowIndentGuides",bShowIndentGuides);
  1232.   IniSectionSetInt(pIniSection,L"TabsAsSpaces",bTabsAsSpacesG);
  1233.   IniSectionSetInt(pIniSection,L"TabIndents",bTabIndents);
  1234.   IniSectionSetInt(pIniSection,L"TabWidth",iTabWidthG);
  1235.   IniSectionSetInt(pIniSection,L"IndentWidth",iIndentWidthG);
  1236.   IniSectionSetInt(pIniSection,L"MarkLongLines",bMarkLongLines);
  1237.   IniSectionSetInt(pIniSection,L"LongLinesLimit",iLongLinesLimitG);
  1238.   IniSectionSetInt(pIniSection,L"LongLineMode",iLongLineMode);
  1239.   IniSectionSetInt(pIniSection,L"ShowSelectionMargin",bShowSelectionMargin);
  1240.   IniSectionSetInt(pIniSection,L"ShowLineNumbers",bShowLineNumbers);
  1241.   IniSectionSetInt(pIniSection,L"ViewWhiteSpace",bViewWhiteSpace);
  1242.   IniSectionSetInt(pIniSection,L"ViewEOLs",bViewEOLs);
  1243.   IniSectionSetInt(pIniSection,L"DefaultEncoding",Encoding_MapIniSetting(FALSE,iDefaultEncoding));
  1244.   IniSectionSetInt(pIniSection,L"SkipUnicodeDetection",bSkipUnicodeDetection);
  1245.   IniSectionSetInt(pIniSection,L"LoadASCIIasUTF8",bLoadASCIIasUTF8);
  1246.   IniSectionSetInt(pIniSection,L"NoEncodingTags",bNoEncodingTags);
  1247.   IniSectionSetInt(pIniSection,L"DefaultEOLMode",iDefaultEOLMode);
  1248.   IniSectionSetInt(pIniSection,L"FixLineEndings",bFixLineEndings);
  1249.   IniSectionSetInt(pIniSection,L"FixTrailingBlanks",bAutoStripBlanks);
  1250.   IniSectionSetInt(pIniSection,L"PrintHeader",iPrintHeader);
  1251.   IniSectionSetInt(pIniSection,L"PrintFooter",iPrintFooter);
  1252.   IniSectionSetInt(pIniSection,L"PrintColorMode",iPrintColor);
  1253.   IniSectionSetInt(pIniSection,L"PrintZoom",iPrintZoom+10);
  1254.   IniSectionSetInt(pIniSection,L"PrintMarginLeft",pagesetupMargin.left);
  1255.   IniSectionSetInt(pIniSection,L"PrintMarginTop",pagesetupMargin.top);
  1256.   IniSectionSetInt(pIniSection,L"PrintMarginRight",pagesetupMargin.right);
  1257.   IniSectionSetInt(pIniSection,L"PrintMarginBottom",pagesetupMargin.bottom);
  1258.   IniSectionSetInt(pIniSection,L"SaveBeforeRunningTools",bSaveBeforeRunningTools);
  1259.   IniSectionSetInt(pIniSection,L"FileWatchingMode",iFileWatchingMode);
  1260.   IniSectionSetInt(pIniSection,L"ResetFileWatching",bResetFileWatching);
  1261.   IniSectionSetInt(pIniSection,L"EscFunction",iEscFunction);
  1262.   IniSectionSetInt(pIniSection,L"AlwaysOnTop",bAlwaysOnTop);
  1263.   IniSectionSetInt(pIniSection,L"MinimizeToTray",bMinimizeToTray);
  1264.   IniSectionSetInt(pIniSection,L"TransparentMode",bTransparentMode);
  1265.   Toolbar_GetButtons(hwndToolbar,IDT_FILE_NEW,tchToolbarButtons,COUNTOF(tchToolbarButtons));
  1266.   IniSectionSetString(pIniSection,L"ToolbarButtons",tchToolbarButtons);
  1267.   IniSectionSetInt(pIniSection,L"ShowToolbar",bShowToolbar);
  1268.   IniSectionSetInt(pIniSection,L"ShowStatusbar",bShowStatusbar);
  1269.   IniSectionSetInt(pIniSection,L"EncodingDlgSizeX",cxEncodingDlg);
  1270.   IniSectionSetInt(pIniSection,L"EncodingDlgSizeY",cyEncodingDlg);
  1271.   IniSectionSetInt(pIniSection,L"RecodeDlgSizeX",cxRecodeDlg);
  1272.   IniSectionSetInt(pIniSection,L"RecodeDlgSizeY",cyRecodeDlg);
  1273.   IniSectionSetInt(pIniSection,L"FileMRUDlgSizeX",cxFileMRUDlg);
  1274.   IniSectionSetInt(pIniSection,L"FileMRUDlgSizeY",cyFileMRUDlg);
  1275.   IniSectionSetInt(pIniSection,L"OpenWithDlgSizeX",cxOpenWithDlg);
  1276.   IniSectionSetInt(pIniSection,L"OpenWithDlgSizeY",cyOpenWithDlg);
  1277.   IniSectionSetInt(pIniSection,L"FavoritesDlgSizeX",cxFavoritesDlg);
  1278.   IniSectionSetInt(pIniSection,L"FavoritesDlgSizeY",cyFavoritesDlg);
  1279.   IniSectionSetInt(pIniSection,L"FindReplaceDlgPosX",xFindReplaceDlg);
  1280.   IniSectionSetInt(pIniSection,L"FindReplaceDlgPosY",yFindReplaceDlg);
  1281.   SaveIniSection(L"Settings",pIniSection);
  1282.   LocalFree(pIniSection);
  1283.   /*
  1284.     SaveSettingsNow(): query Window Dimensions
  1285.   */
  1286.   if (bSaveSettingsNow)
  1287.   {
  1288.     WINDOWPLACEMENT wndpl;
  1289.     // GetWindowPlacement
  1290.     wndpl.length = sizeof(WINDOWPLACEMENT);
  1291.     GetWindowPlacement(hwndMain,&wndpl);
  1292.     wi.x = wndpl.rcNormalPosition.left;
  1293.     wi.y = wndpl.rcNormalPosition.top;
  1294.     wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
  1295.     wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
  1296.     wi.max = (IsZoomed(hwndMain) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
  1297.   }
  1298.   if (!IniGetInt(L"Settings2",L"StickyWindowPosition",0)) {
  1299.     WCHAR tchPosX[32], tchPosY[32], tchSizeX[32], tchSizeY[32], tchMaximized[32];
  1300.     int ResX = GetSystemMetrics(SM_CXSCREEN);
  1301.     int ResY = GetSystemMetrics(SM_CYSCREEN);
  1302.     wsprintf(tchPosX,L"%ix%i PosX",ResX,ResY);
  1303.     wsprintf(tchPosY,L"%ix%i PosY",ResX,ResY);
  1304.     wsprintf(tchSizeX,L"%ix%i SizeX",ResX,ResY);
  1305.     wsprintf(tchSizeY,L"%ix%i SizeY",ResX,ResY);
  1306.     wsprintf(tchMaximized,L"%ix%i Maximized",ResX,ResY);
  1307.     IniSetInt(L"Window",tchPosX,wi.x);
  1308.     IniSetInt(L"Window",tchPosY,wi.y);
  1309.     IniSetInt(L"Window",tchSizeX,wi.cx);
  1310.     IniSetInt(L"Window",tchSizeY,wi.cy);
  1311.     IniSetInt(L"Window",tchMaximized,wi.max);
  1312.   }
  1313.   // Scintilla Styles
  1314.   Style_Save();
  1315. }
  1316. //=============================================================================
  1317. //
  1318. //  ParseCommandLine()
  1319. //
  1320. //
  1321. void ParseCommandLine()
  1322. {
  1323.   LPWSTR lp1,lp2,lp3;
  1324.   BOOL bContinue = TRUE;
  1325.   BOOL bIsNotepadReplacement = FALSE;
  1326.   LPWSTR lpCmdLine = GetCommandLine();
  1327.   if (lstrlen(lpCmdLine) == 0)
  1328.     return;
  1329.   // Good old console can also send args separated by Tabs
  1330.   StrTab2Space(lpCmdLine);
  1331.   lp1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  1332.   lp2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  1333.   lp3 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  1334.   // Start with 2nd argument
  1335.   ExtractFirstArgument(lpCmdLine,lp1,lp3);
  1336.   while (bContinue && ExtractFirstArgument(lp3,lp1,lp2))
  1337.   {
  1338.     // options
  1339.     if (lstrcmp(lp1,L"+") == 0)
  1340.       flagMultiFileArg = 2;
  1341.     else if (lstrcmp(lp1,L"-") == 0)
  1342.       flagMultiFileArg = 1;
  1343.     else if ((*lp1 == L'/') || (*lp1 == L'-'))
  1344.     {
  1345.       StrTrim(lp1,L"-/");
  1346.       // Encoding
  1347.       if (lstrcmpi(lp1,L"ANSI") == 0 || lstrcmpi(lp1,L"A") == 0 || lstrcmpi(lp1,L"MBCS") == 0)
  1348.         flagSetEncoding = IDM_ENCODING_ANSI-IDM_ENCODING_ANSI + 1;
  1349.       else if (lstrcmpi(lp1,L"UNICODE") == 0 || lstrcmpi(lp1,L"W") == 0)
  1350.         flagSetEncoding = IDM_ENCODING_UNICODE-IDM_ENCODING_ANSI + 1;
  1351.       else if (lstrcmpi(lp1,L"UNICODEBE") == 0 || lstrcmpi(lp1,L"UNICODE-BE") == 0)
  1352.         flagSetEncoding = IDM_ENCODING_UNICODEREV-IDM_ENCODING_ANSI + 1;
  1353.       else if (lstrcmpi(lp1,L"UTF8") == 0 || lstrcmpi(lp1,L"UTF-8") == 0)
  1354.         flagSetEncoding = IDM_ENCODING_UTF8-IDM_ENCODING_ANSI + 1;
  1355.       else if (lstrcmpi(lp1,L"UTF8SIG") == 0 || lstrcmpi(lp1,L"UTF-8SIG") == 0 ||
  1356.               lstrcmpi(lp1,L"UTF8SIGNATURE") == 0 || lstrcmpi(lp1,L"UTF-8SIGNATURE") == 0 ||
  1357.               lstrcmpi(lp1,L"UTF8-SIGNATURE") == 0 || lstrcmpi(lp1,L"UTF-8-SIGNATURE") == 0)
  1358.         flagSetEncoding = IDM_ENCODING_UTF8SIGN-IDM_ENCODING_ANSI + 1;
  1359.       // EOL Mode
  1360.       else if (lstrcmpi(lp1,L"CRLF") == 0 || lstrcmpi(lp1,L"CR+LF") == 0)
  1361.         flagSetEOLMode = IDM_LINEENDINGS_CRLF-IDM_LINEENDINGS_CRLF + 1;
  1362.       else if (lstrcmpi(lp1,L"LF") == 0)
  1363.         flagSetEOLMode = IDM_LINEENDINGS_LF-IDM_LINEENDINGS_CRLF + 1;
  1364.       else if (lstrcmpi(lp1,L"CR") == 0)
  1365.         flagSetEOLMode = IDM_LINEENDINGS_CR-IDM_LINEENDINGS_CRLF + 1;
  1366.       else switch (*CharUpper(lp1))
  1367.       {
  1368.         case L'N':
  1369.           flagReuseWindow = 0;
  1370.           flagNoReuseWindow = 1;
  1371.           if (*CharUpper(lp1+1) == L'S')
  1372.             flagSingleFileInstance = 1;
  1373.           else
  1374.             flagSingleFileInstance = 0;
  1375.           break;
  1376.         case L'R':
  1377.           flagReuseWindow = 1;
  1378.           flagNoReuseWindow = 0;
  1379.           if (*CharUpper(lp1+1) == L'S')
  1380.             flagSingleFileInstance = 1;
  1381.           else
  1382.             flagSingleFileInstance = 0;
  1383.           break;
  1384.         case L'F':
  1385.           if (*(lp1+1) == L'0' || *CharUpper(lp1+1) == L'O')
  1386.             lstrcpy(szIniFile,L"*?");
  1387.           else if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1388.             StrCpyN(szIniFile,lp1,COUNTOF(szIniFile));
  1389.             TrimString(szIniFile);
  1390.             PathUnquoteSpaces(szIniFile);
  1391.           }
  1392.           break;
  1393.         case L'I':
  1394.           flagStartAsTrayIcon = 1;
  1395.           break;
  1396.         case L'P':
  1397.           if (StrCmpNI(lp1,L"POS:",4) == 0)
  1398.             lp1 += 3;
  1399.           else if (StrCmpNI(lp1,L"POS",3) == 0)
  1400.             lp1 += 2;
  1401.           else if (*(lp1+1) == L':')
  1402.             lp1 += 1;
  1403.           else if (bIsNotepadReplacement) {
  1404.             if (*(lp1+1) == L'T')
  1405.               ExtractFirstArgument(lp2,lp1,lp2);
  1406.             break;
  1407.           }
  1408.           if (*(lp1+1) == L'0' || *CharUpper(lp1+1) == L'O') {
  1409.             flagPosParam = 1;
  1410.             flagDefaultPos = 1;
  1411.           }
  1412.           else if (*CharUpper(lp1+1) == L'D' || *CharUpper(lp1+1) == L'S') {
  1413.             flagPosParam = 1;
  1414.             flagDefaultPos = (StrChrI((lp1+1),L'L')) ? 3 : 2;
  1415.           }
  1416.           else if (StrChrI(L"FLTRBM",*(lp1+1))) {
  1417.             WCHAR *p = (lp1+1);
  1418.             flagPosParam = 1;
  1419.             flagDefaultPos = 0;
  1420.             while (*p) {
  1421.               switch (*CharUpper(p)) {
  1422.                 case L'F':
  1423.                   flagDefaultPos &= ~(4|8|16|32);
  1424.                   flagDefaultPos |= 64;
  1425.                   break;
  1426.                 case L'L':
  1427.                   flagDefaultPos &= ~(8|64);
  1428.                   flagDefaultPos |= 4;
  1429.                   break;
  1430.                 case  L'R':
  1431.                   flagDefaultPos &= ~(4|64);
  1432.                   flagDefaultPos |= 8;
  1433.                   break;
  1434.                 case L'T':
  1435.                   flagDefaultPos &= ~(32|64);
  1436.                   flagDefaultPos |= 16;
  1437.                   break;
  1438.                 case L'B':
  1439.                   flagDefaultPos &= ~(16|64);
  1440.                   flagDefaultPos |= 32;
  1441.                   break;
  1442.                 case L'M':
  1443.                   if (flagDefaultPos == 0)
  1444.                     flagDefaultPos |= 64;
  1445.                   flagDefaultPos |= 128;
  1446.                   break;
  1447.               }
  1448.               p = CharNext(p);
  1449.             }
  1450.           }
  1451.           else if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1452.             int itok =
  1453.               swscanf(lp1,L"%i,%i,%i,%i,%i",&wi.x,&wi.y,&wi.cx,&wi.cy,&wi.max);
  1454.             if (itok == 4 || itok == 5) { // scan successful
  1455.               flagPosParam = 1;
  1456.               if (wi.cx < 1) wi.cx = CW_USEDEFAULT;
  1457.               if (wi.cy < 1) wi.cy = CW_USEDEFAULT;
  1458.               if (wi.max) wi.max = 1;
  1459.               if (itok == 4) wi.max = 0;
  1460.             }
  1461.           }
  1462.           break;
  1463.         case L'T':
  1464.           if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1465.             StrCpyN(szTitleExcerpt,lp1,COUNTOF(szTitleExcerpt));
  1466.             fKeepTitleExcerpt = 1;
  1467.           }
  1468.           break;
  1469.         case L'C':
  1470.           flagNewFromClipboard = 1;
  1471.           break;
  1472.         case L'B':
  1473.           flagPasteBoard = 1;
  1474.           break;
  1475.         case L'E':
  1476.           if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1477.             if (lpEncodingArg)
  1478.               LocalFree(lpEncodingArg);
  1479.             lpEncodingArg = StrDup(lp1);
  1480.           }
  1481.           break;
  1482.         case L'G':
  1483.           if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1484.             int itok =
  1485.               swscanf(lp1,L"%i,%i",&iInitialLine,&iInitialColumn);
  1486.             if (itok == 1 || itok == 2) { // scan successful
  1487.               flagJumpTo = 1;
  1488.             }
  1489.           }
  1490.           break;
  1491.         case L'M':
  1492.           {
  1493.             BOOL bFindUp  = FALSE;
  1494.             BOOL bRegex   = FALSE;
  1495.             BOOL bTransBS = FALSE;
  1496.             if (StrChr(lp1,L'-'))
  1497.               bFindUp = TRUE;
  1498.             if (StrChr(lp1,L'R'))
  1499.               bRegex = TRUE;
  1500.             if (StrChr(lp1,L'B'))
  1501.               bTransBS = TRUE;
  1502.             if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1503.               if (lpMatchArg)
  1504.                 GlobalFree(lpMatchArg);
  1505.               lpMatchArg = StrDup(lp1);
  1506.               flagMatchText = 1;
  1507.               if (bFindUp)
  1508.                 flagMatchText |= 2;
  1509.               if (bRegex) {
  1510.                 flagMatchText &= ~8;
  1511.                 flagMatchText |= 4;
  1512.               }
  1513.               if (bTransBS) {
  1514.                 flagMatchText &= ~4;
  1515.                 flagMatchText |= 8;
  1516.               }
  1517.             }
  1518.           }
  1519.           break;
  1520.         case L'Q':
  1521.           flagQuietCreate = 1;
  1522.           break;
  1523.         case L'S':
  1524.           if (ExtractFirstArgument(lp2,lp1,lp2)) {
  1525.             if (lpSchemeArg)
  1526.               LocalFree(lpSchemeArg);
  1527.             lpSchemeArg = StrDup(lp1);
  1528.             flagLexerSpecified = 1;
  1529.           }
  1530.           break;
  1531.         case L'D':
  1532.           if (lpSchemeArg) {
  1533.             LocalFree(lpSchemeArg);
  1534.             lpSchemeArg = NULL;
  1535.           }
  1536.           iInitialLexer = 0;
  1537.           flagLexerSpecified = 1;
  1538.           break;
  1539.         case L'H':
  1540.           if (lpSchemeArg) {
  1541.             LocalFree(lpSchemeArg);
  1542.             lpSchemeArg = NULL;
  1543.           }
  1544.           iInitialLexer = 1;
  1545.           flagLexerSpecified = 1;
  1546.           break;
  1547.         case L'X':
  1548.           if (lpSchemeArg) {
  1549.             LocalFree(lpSchemeArg);
  1550.             lpSchemeArg = NULL;
  1551.           }
  1552.           iInitialLexer = 2;
  1553.           flagLexerSpecified = 1;
  1554.           break;
  1555.         case L'U':
  1556.           flagRelaunchElevated = 1;
  1557.           break;
  1558.         case L'Z':
  1559.           ExtractFirstArgument(lp2,lp1,lp2);
  1560.           flagMultiFileArg = 1;
  1561.           bIsNotepadReplacement = TRUE;
  1562.           break;
  1563.         case L'?':
  1564.           flagDisplayHelp = 1;
  1565.           break;
  1566.         default:
  1567.           break;
  1568.       }
  1569.     }
  1570.     // pathname
  1571.     else
  1572.     {
  1573.       LPWSTR lpFileBuf = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  1574.       cchiFileList = lstrlen(lpCmdLine) - lstrlen(lp3);
  1575.       if (lpFileArg)
  1576.         GlobalFree(lpFileArg);
  1577.       lpFileArg = GlobalAlloc(GPTR,sizeof(WCHAR)*(MAX_PATH+2)); // changed for ActivatePrevInst() needs
  1578.       StrCpyN(lpFileArg,lp3,MAX_PATH);
  1579.       StrTrim(lpFileArg,L" "");
  1580.       while (cFileList < 32 && ExtractFirstArgument(lp3,lpFileBuf,lp3)) {
  1581.         PathQuoteSpaces(lpFileBuf);
  1582.         lpFileList[cFileList++] = StrDup(lpFileBuf);
  1583.       }
  1584.       bContinue = FALSE;
  1585.       LocalFree(lpFileBuf);
  1586.     }
  1587.     // Continue with next argument
  1588.     if (bContinue)
  1589.       lstrcpy(lp3,lp2);
  1590.   }
  1591.   LocalFree(lp1);
  1592.   LocalFree(lp2);
  1593.   LocalFree(lp3);
  1594. }
  1595. //=============================================================================
  1596. //
  1597. //  LoadFlags()
  1598. //
  1599. //
  1600. void LoadFlags()
  1601. {
  1602.   WCHAR *pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
  1603.   int   cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
  1604.   LoadIniSection(L"Settings2",pIniSection,cchIniSection);
  1605.   if (!flagReuseWindow && !flagNoReuseWindow) {
  1606.     if (!IniSectionGetInt(pIniSection,L"ReuseWindow",0))
  1607.       flagNoReuseWindow = 1;
  1608.     if (IniSectionGetInt(pIniSection,L"SingleFileInstance",0))
  1609.       flagSingleFileInstance = 1;
  1610.   }
  1611.   if (flagMultiFileArg == 0) {
  1612.     if (IniSectionGetInt(pIniSection,L"MultiFileArg",0))
  1613.       flagMultiFileArg = 2;
  1614.   }
  1615.   if (IniSectionGetInt(pIniSection,L"RelativeFileMRU",1))
  1616.     flagRelativeFileMRU = 1;
  1617.   if (IniSectionGetInt(pIniSection,L"PortableMyDocs",flagRelativeFileMRU))
  1618.     flagPortableMyDocs = 1;
  1619.   if (IniSectionGetInt(pIniSection,L"NoFadeHidden",0))
  1620.     flagNoFadeHidden = 1;
  1621.   if (IniSectionGetInt(pIniSection,L"SimpleIndentGuides",0))
  1622.     flagSimpleIndentGuides = 1;
  1623.   if (IniSectionGetInt(pIniSection,L"NoHTMLGuess",0))
  1624.     fNoHTMLGuess = 1;
  1625.   if (IniSectionGetInt(pIniSection,L"NoCGIGuess",0))
  1626.     fNoCGIGuess = 1;
  1627.   if (IniSectionGetInt(pIniSection,L"NoFileVariables",0))
  1628.     fNoFileVariables = 1;
  1629.   IniSectionGetString(pIniSection,L"ShellAppUserModelID",L"(default)",
  1630.     g_wchAppUserModelID,COUNTOF(g_wchAppUserModelID));
  1631.   if (IniSectionGetInt(pIniSection,L"ShellUseSystemMRU",0))
  1632.     flagUseSystemMRU = 1;
  1633.   LocalFree(pIniSection);
  1634. }
  1635. //=============================================================================
  1636. //
  1637. //  FindIniFile()
  1638. //
  1639. //
  1640. int CheckIniFile(LPWSTR lpszFile,LPCWSTR lpszModule)
  1641. {
  1642.   WCHAR tchFileExpanded[MAX_PATH];
  1643.   WCHAR tchBuild[MAX_PATH];
  1644.   ExpandEnvironmentStrings(lpszFile,tchFileExpanded,COUNTOF(tchFileExpanded));
  1645.   if (PathIsRelative(tchFileExpanded)) {
  1646.     // program directory
  1647.     lstrcpy(tchBuild,lpszModule);
  1648.     lstrcpy(PathFindFileName(tchBuild),tchFileExpanded);
  1649.     if (PathFileExists(tchBuild)) {
  1650.       lstrcpy(lpszFile,tchBuild);
  1651.       return(1);
  1652.     }
  1653.     // %appdata%
  1654.     if (S_OK == SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,SHGFP_TYPE_CURRENT,tchBuild)) {
  1655.       PathAppend(tchBuild,tchFileExpanded);
  1656.       if (PathFileExists(tchBuild)) {
  1657.         lstrcpy(lpszFile,tchBuild);
  1658.         return(1);
  1659.       }
  1660.     }
  1661.     // general
  1662.     if (SearchPath(NULL,tchFileExpanded,NULL,COUNTOF(tchBuild),tchBuild,NULL)) {
  1663.       lstrcpy(lpszFile,tchBuild);
  1664.       return(1);
  1665.     }
  1666.   }
  1667.   else if (PathFileExists(tchFileExpanded)) {
  1668.     lstrcpy(lpszFile,tchFileExpanded);
  1669.     return(1);
  1670.   }
  1671.   return(0);
  1672. }
  1673. int CheckIniFileRedirect(LPWSTR lpszFile,LPCWSTR lpszModule)
  1674. {
  1675.   WCHAR tch[MAX_PATH];
  1676.   if (GetPrivateProfileString(L"Notepad2",L"Notepad2.ini",L"",tch,COUNTOF(tch),lpszFile)) {
  1677.     if (CheckIniFile(tch,lpszModule)) {
  1678.       lstrcpy(lpszFile,tch);
  1679.       return(1);
  1680.     }
  1681.     else {
  1682.       WCHAR tchFileExpanded[MAX_PATH];
  1683.       ExpandEnvironmentStrings(tch,tchFileExpanded,COUNTOF(tchFileExpanded));
  1684.       if (PathIsRelative(tchFileExpanded)) {
  1685.         lstrcpy(lpszFile,lpszModule);
  1686.         lstrcpy(PathFindFileName(lpszFile),tchFileExpanded);
  1687.         return(1);
  1688.       }
  1689.       else {
  1690.         lstrcpy(lpszFile,tchFileExpanded);
  1691.         return(1);
  1692.       }
  1693.     }
  1694.   }
  1695.   return(0);
  1696. }
  1697. int FindIniFile() {
  1698.   int bFound = 0;
  1699.   WCHAR tchTest[MAX_PATH];
  1700.   WCHAR tchModule[MAX_PATH];
  1701.   GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
  1702.   if (lstrlen(szIniFile)) {
  1703.     if (lstrcmpi(szIniFile,L"*?") == 0)
  1704.       return(0);
  1705.     else {
  1706.       if (!CheckIniFile(szIniFile,tchModule)) {
  1707.         ExpandEnvironmentStringsEx(szIniFile,COUNTOF(szIniFile));
  1708.         if (PathIsRelative(szIniFile)) {
  1709.           lstrcpy(tchTest,tchModule);
  1710.           PathRemoveFileSpec(tchTest);
  1711.           PathAppend(tchTest,szIniFile);
  1712.           lstrcpy(szIniFile,tchTest);
  1713.         }
  1714.       }
  1715.     }
  1716.     return(1);
  1717.   }
  1718.   lstrcpy(tchTest,PathFindFileName(tchModule));
  1719.   PathRenameExtension(tchTest,L".ini");
  1720.   bFound = CheckIniFile(tchTest,tchModule);
  1721.   if (!bFound) {
  1722.     lstrcpy(tchTest,L"Notepad2.ini");
  1723.     bFound = CheckIniFile(tchTest,tchModule);
  1724.   }
  1725.   if (bFound) {
  1726.     // allow two redirections: administrator -> user -> custom
  1727.     if (CheckIniFileRedirect(tchTest,tchModule))
  1728.       CheckIniFileRedirect(tchTest,tchModule);
  1729.     lstrcpy(szIniFile,tchTest);
  1730.   }
  1731.   else {
  1732.     lstrcpy(szIniFile,tchModule);
  1733.     PathRenameExtension(szIniFile,L".ini");
  1734.   }
  1735.   return(1);
  1736. }
  1737. int TestIniFile() {
  1738.   if (lstrcmpi(szIniFile,L"*?") == 0) {
  1739.     lstrcpy(szIniFile2,L"");
  1740.     lstrcpy(szIniFile,L"");
  1741.     return(0);
  1742.   }
  1743.   if (PathIsDirectory(szIniFile) || *CharPrev(szIniFile,StrEnd(szIniFile)) == L'\') {
  1744.     WCHAR wchModule[MAX_PATH];
  1745.     GetModuleFileName(NULL,wchModule,COUNTOF(wchModule));
  1746.     PathAppend(szIniFile,PathFindFileName(wchModule));
  1747.     PathRenameExtension(szIniFile,L".ini");
  1748.     if (!PathFileExists(szIniFile)) {
  1749.       lstrcpy(PathFindFileName(szIniFile),L"Notepad2.ini");
  1750.       if (!PathFileExists(szIniFile)) {
  1751.         lstrcpy(PathFindFileName(szIniFile),PathFindFileName(wchModule));
  1752.         PathRenameExtension(szIniFile,L".ini");
  1753.       }
  1754.     }
  1755.   }
  1756.   if (!PathFileExists(szIniFile) || PathIsDirectory(szIniFile)) {
  1757.     lstrcpy(szIniFile2,szIniFile);
  1758.     lstrcpy(szIniFile,L"");
  1759.     return(0);
  1760.   }
  1761.   else
  1762.     return(1);
  1763. }
  1764. int CreateIniFile() {
  1765.   return(CreateIniFileEx(szIniFile));
  1766. }
  1767. int CreateIniFileEx(LPCWSTR lpszIniFile) {
  1768.   if (*lpszIniFile) {
  1769.     HANDLE hFile;
  1770.     WCHAR *pwchTail;
  1771.     if (pwchTail = StrRChrW(lpszIniFile,NULL,L'\')) {
  1772.       *pwchTail = 0;
  1773.       SHCreateDirectoryEx(NULL,lpszIniFile,NULL);
  1774.       *pwchTail = L'\';
  1775.     }
  1776.     hFile = CreateFile(lpszIniFile,
  1777.               GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
  1778.               NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  1779.     dwLastIOError = GetLastError();
  1780.     if (hFile != INVALID_HANDLE_VALUE) {
  1781.       if (GetFileSize(hFile,NULL) == 0) {
  1782.         DWORD dw;
  1783.         WriteFile(hFile,(LPCVOID)L"xFEFF[Notepad2]rn",26,&dw,NULL);
  1784.       }
  1785.       CloseHandle(hFile);
  1786.       return(1);
  1787.     }
  1788.     else
  1789.       return(0);
  1790.   }
  1791.   else
  1792.     return(0);
  1793. }
  1794. //=============================================================================
  1795. //
  1796. //  UpdateToolbar()
  1797. //
  1798. //
  1799. #define EnableTool(id,b) SendMessage(hwndToolbar,TB_ENABLEBUTTON,id, 
  1800.                            MAKELONG(((b) ? 1 : 0), 0))
  1801. #define CheckTool(id,b)  SendMessage(hwndToolbar,TB_CHECKBUTTON,id, 
  1802.                            MAKELONG(b,0))
  1803. void UpdateToolbar()
  1804. {
  1805.   int i;
  1806.   if (!bShowToolbar)
  1807.     return;
  1808.   EnableTool(IDT_FILE_ADDTOFAV,lstrlen(szCurFile));
  1809.   EnableTool(IDT_EDIT_UNDO,SendMessage(hwndEdit,SCI_CANUNDO,0,0) /*&& !bReadOnly*/);
  1810.   EnableTool(IDT_EDIT_REDO,SendMessage(hwndEdit,SCI_CANREDO,0,0) /*&& !bReadOnly*/);
  1811.   i = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) - SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  1812.   EnableTool(IDT_EDIT_CUT,i /*&& !bReadOnly*/);
  1813.   EnableTool(IDT_EDIT_COPY,SendMessage(hwndEdit,SCI_GETLENGTH,0,0));
  1814.   EnableTool(IDT_EDIT_PASTE,SendMessage(hwndEdit,SCI_CANPASTE,0,0) /*&& !bReadOnly*/);
  1815.   i = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
  1816.   EnableTool(IDT_EDIT_FIND,i);
  1817.   //EnableTool(IDT_EDIT_FINDNEXT,i);
  1818.   //EnableTool(IDT_EDIT_FINDPREV,i && lstrlen(efrData.szFind));
  1819.   EnableTool(IDT_EDIT_REPLACE,i /*&& !bReadOnly*/);
  1820.   EnableTool(IDT_EDIT_CLEAR,i /*&& !bReadOnly*/);
  1821.   CheckTool(IDT_VIEW_WORDWRAP,fWordWrap);
  1822. }
  1823. //=============================================================================
  1824. //
  1825. //  UpdateStatusbar()
  1826. //
  1827. //
  1828. void UpdateStatusbar()
  1829. {
  1830.   int iPos;
  1831.   int iLn;
  1832.   int iLines;
  1833.   int iCol;
  1834.   int iSel;
  1835.   WCHAR tchLn[32];
  1836.   WCHAR tchLines[32];
  1837.   WCHAR tchCol[32];
  1838.   WCHAR tchCols[32];
  1839.   WCHAR tchSel[32];
  1840.   WCHAR tchDocPos[256];
  1841.   int iBytes;
  1842.   WCHAR tchBytes[64];
  1843.   WCHAR tchDocSize[256];
  1844.   WCHAR tchEOLMode[32];
  1845.   WCHAR tchOvrMode[32];
  1846.   WCHAR tchLexerName[128];
  1847.   if (!bShowStatusbar)
  1848.     return;
  1849.   iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  1850.   iLn = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iPos,0) + 1;
  1851.   wsprintf(tchLn,L"%i",iLn);
  1852.   FormatNumberStr(tchLn);
  1853.   iLines = SendMessage(hwndEdit,SCI_GETLINECOUNT,0,0);
  1854.   wsprintf(tchLines,L"%i",iLines);
  1855.   FormatNumberStr(tchLines);
  1856.   iCol = SendMessage(hwndEdit,SCI_GETCOLUMN,iPos,0) + 1;
  1857.   wsprintf(tchCol,L"%i",iCol);
  1858.   FormatNumberStr(tchCol);
  1859.   if (bMarkLongLines) {
  1860.     wsprintf(tchCols,L"%i",iLongLinesLimit);
  1861.     FormatNumberStr(tchCols);
  1862.   }
  1863.   if (SC_SEL_RECTANGLE != SendMessage(hwndEdit,SCI_GETSELECTIONMODE,0,0))
  1864.   {
  1865.     iSel = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) - SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  1866.     wsprintf(tchSel,L"%i",iSel);
  1867.     FormatNumberStr(tchSel);
  1868.   }
  1869.   else
  1870.     lstrcpy(tchSel,L"--");
  1871.   if (!bMarkLongLines)
  1872.     FormatString(tchDocPos,COUNTOF(tchDocPos),IDS_DOCPOS,tchLn,tchLines,tchCol,tchSel);
  1873.   else
  1874.     FormatString(tchDocPos,COUNTOF(tchDocPos),IDS_DOCPOS2,tchLn,tchLines,tchCol,tchCols,tchSel);
  1875.   iBytes = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
  1876.   StrFormatByteSize(iBytes,tchBytes,COUNTOF(tchBytes));
  1877.   FormatString(tchDocSize,COUNTOF(tchDocSize),IDS_DOCSIZE,tchBytes);
  1878.   Encoding_GetLabel(iEncoding);
  1879.   if (iEOLMode == SC_EOL_CR)
  1880.     lstrcpy(tchEOLMode,L"CR");
  1881.   else if (iEOLMode == SC_EOL_LF)
  1882.     lstrcpy(tchEOLMode,L"LF");
  1883.   else
  1884.     lstrcpy(tchEOLMode,L"CR+LF");
  1885.   if (SendMessage(hwndEdit,SCI_GETOVERTYPE,0,0))
  1886.     lstrcpy(tchOvrMode,L"OVR");
  1887.   else
  1888.     lstrcpy(tchOvrMode,L"INS");
  1889.   Style_GetCurrentLexerName(tchLexerName,COUNTOF(tchLexerName));
  1890.   StatusSetText(hwndStatus,STATUS_DOCPOS,tchDocPos);
  1891.   StatusSetText(hwndStatus,STATUS_DOCSIZE,tchDocSize);
  1892.   StatusSetText(hwndStatus,STATUS_CODEPAGE,mEncoding[iEncoding].wchLabel);
  1893.   StatusSetText(hwndStatus,STATUS_EOLMODE,tchEOLMode);
  1894.   StatusSetText(hwndStatus,STATUS_OVRMODE,tchOvrMode);
  1895.   StatusSetText(hwndStatus,STATUS_LEXER,tchLexerName);
  1896.   //InvalidateRect(hwndStatus,NULL,TRUE);
  1897. }
  1898. //=============================================================================
  1899. //
  1900. //  UpdateLineNumberWidth()
  1901. //
  1902. //
  1903. void UpdateLineNumberWidth()
  1904. {
  1905.   char tchLines[32];
  1906.   int  iLineMarginWidthNow;
  1907.   int  iLineMarginWidthFit;
  1908.   if (bShowLineNumbers) {
  1909.     wsprintfA(tchLines,"_%i_",SendMessage(hwndEdit,SCI_GETLINECOUNT,0,0));
  1910.     iLineMarginWidthNow = SendMessage(hwndEdit,SCI_GETMARGINWIDTHN,0,0);
  1911.     iLineMarginWidthFit = SendMessage(hwndEdit,SCI_TEXTWIDTH,STYLE_LINENUMBER,(LPARAM)tchLines);
  1912.     if (iLineMarginWidthNow != iLineMarginWidthFit) {
  1913.       //SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,0);
  1914.       SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,iLineMarginWidthFit);
  1915.     }
  1916.   }
  1917.   else
  1918.     SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,0);
  1919. }
  1920. //=============================================================================
  1921. //
  1922. //  FileIO()
  1923. //
  1924. //
  1925. BOOL FileIO(BOOL fLoad,LPCWSTR psz,BOOL bNoEncDetect,int *ienc,int *ieol,
  1926.             BOOL *pbUnicodeErr,BOOL *pbFileTooBig,
  1927.             BOOL *pbCancelDataLoss,BOOL bSaveCopy)
  1928. {
  1929.   SHFILEINFO shfi;
  1930.   WCHAR tch[MAX_PATH+40];
  1931.   BOOL fSuccess;
  1932.   DWORD dwFileAttributes;
  1933.   BeginWaitCursor();
  1934.   SHGetFileInfo2(psz,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
  1935.   FormatString(tch,COUNTOF(tch),(fLoad) ? IDS_LOADFILE : IDS_SAVEFILE,shfi.szDisplayName);
  1936.   StatusSetText(hwndStatus,STATUS_HELP,tch);
  1937.   StatusSetSimple(hwndStatus,TRUE);
  1938.   InvalidateRect(hwndStatus,NULL,TRUE);
  1939.   UpdateWindow(hwndStatus);
  1940.   if (fLoad)
  1941.     fSuccess = EditLoadFile(hwndEdit,psz,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig);
  1942.   else
  1943.     fSuccess = EditSaveFile(hwndEdit,psz,*ienc,pbCancelDataLoss,bSaveCopy);
  1944.   dwFileAttributes = GetFileAttributes(psz);
  1945.   bReadOnly = (dwFileAttributes != INVALID_FILE_ATTRIBUTES && dwFileAttributes & FILE_ATTRIBUTE_READONLY);
  1946.   StatusSetSimple(hwndStatus,FALSE);
  1947.   EndWaitCursor();
  1948.   return(fSuccess);
  1949. }
  1950. //=============================================================================
  1951. //
  1952. //  FileLoad()
  1953. //
  1954. //
  1955. BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lpszFile)
  1956. {
  1957.   WCHAR tch[MAX_PATH] = L"";
  1958.   WCHAR szFileName[MAX_PATH] = L"";
  1959.   BOOL fSuccess;
  1960.   BOOL bUnicodeErr = FALSE;
  1961.   BOOL bFileTooBig = FALSE;
  1962.   if (!bDontSave)
  1963.   {
  1964.     if (!FileSave(FALSE,TRUE,FALSE,FALSE))
  1965.       return FALSE;
  1966.   }
  1967.   if (bNew) {
  1968.     lstrcpy(szCurFile,L"");
  1969.     SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
  1970.     SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
  1971.     if (!fKeepTitleExcerpt)
  1972.       lstrcpy(szTitleExcerpt,L"");
  1973.     FileVars_Init(NULL,0,&fvCurFile);
  1974.     EditSetNewText(hwndEdit,"",0);
  1975.     Style_SetLexer(hwndEdit,NULL);
  1976.     UpdateLineNumberWidth();
  1977.     bModified = FALSE;
  1978.     bReadOnly = FALSE;
  1979.     iEOLMode = iLineEndings[iDefaultEOLMode];
  1980.     SendMessage(hwndEdit,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0);
  1981.     iEncoding = iDefaultEncoding;
  1982.     iOriginalEncoding = iDefaultEncoding;
  1983.     SendMessage(hwndEdit,SCI_SETCODEPAGE,(iDefaultEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
  1984.     EditSetNewText(hwndEdit,"",0);
  1985.     SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  1986.       iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  1987.       IDS_READONLY,bReadOnly,szTitleExcerpt);
  1988.     // Terminate file watching
  1989.     if (bResetFileWatching)
  1990.       iFileWatchingMode = 0;
  1991.     InstallFileWatching(NULL);
  1992.     return TRUE;
  1993.   }
  1994.   if (!lpszFile || lstrlen(lpszFile) == 0) {
  1995.     if (!OpenFileDlg(hwndMain,tch,COUNTOF(tch),NULL))
  1996.       return FALSE;
  1997.   }
  1998.   else
  1999.     lstrcpy(tch,lpszFile);
  2000.   ExpandEnvironmentStringsEx(tch,COUNTOF(tch));
  2001.   if (PathIsRelative(tch)) {
  2002.     GetCurrentDirectory(COUNTOF(szFileName),szFileName);
  2003.     PathAppend(szFileName,tch);
  2004.     if (!PathFileExists(szFileName)) {
  2005.       WCHAR wchFullPath[MAX_PATH];
  2006.       if (SearchPath(NULL,tch,NULL,COUNTOF(wchFullPath),wchFullPath,NULL)) {
  2007.         lstrcpy(szFileName,wchFullPath);
  2008.       }
  2009.     }
  2010.   }
  2011.   else
  2012.     lstrcpy(szFileName,tch);
  2013.   PathCanonicalizeEx(szFileName);
  2014.   GetLongPathNameEx(szFileName,szFileName,COUNTOF(szFileName));
  2015.   if (PathIsLnkFile(szFileName))
  2016.     PathGetLnkPath(szFileName,szFileName,COUNTOF(szFileName));
  2017.   // Ask to create a new file...
  2018.   if (!bReload && !PathFileExists(szFileName))
  2019.   {
  2020.     if (flagQuietCreate || MsgBox(MBYESNO,IDS_ASK_CREATE,szFileName) == IDYES) {
  2021.       HANDLE hFile = CreateFile(szFileName,
  2022.                       GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
  2023.                       NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
  2024.       dwLastIOError = GetLastError();
  2025.       if (fSuccess = (hFile != INVALID_HANDLE_VALUE)) {
  2026.         CloseHandle(hFile);
  2027.         FileVars_Init(NULL,0,&fvCurFile);
  2028.         EditSetNewText(hwndEdit,"",0);
  2029.         Style_SetLexer(hwndEdit,NULL);
  2030.         iEOLMode = iLineEndings[iDefaultEOLMode];
  2031.         SendMessage(hwndEdit,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0);
  2032.         if (iSrcEncoding != -1) {
  2033.           iEncoding = iSrcEncoding;
  2034.           iOriginalEncoding = iSrcEncoding;
  2035.         }
  2036.         else {
  2037.           iEncoding = iDefaultEncoding;
  2038.           iOriginalEncoding = iDefaultEncoding;
  2039.         }
  2040.         SendMessage(hwndEdit,SCI_SETCODEPAGE,(iEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
  2041.         bReadOnly = FALSE;
  2042.         EditSetNewText(hwndEdit,"",0);
  2043.       }
  2044.     }
  2045.     else
  2046.       return FALSE;
  2047.   }
  2048.   else
  2049.     fSuccess = FileIO(TRUE,szFileName,bNoEncDetect,&iEncoding,&iEOLMode,&bUnicodeErr,&bFileTooBig,NULL,FALSE);
  2050.   if (fSuccess) {
  2051.     lstrcpy(szCurFile,szFileName);
  2052.     SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
  2053.     SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
  2054.     if (!fKeepTitleExcerpt)
  2055.       lstrcpy(szTitleExcerpt,L"");
  2056.     if (!flagLexerSpecified) // flag will be cleared
  2057.       Style_SetLexerFromFile(hwndEdit,szCurFile);
  2058.     UpdateLineNumberWidth();
  2059.     iOriginalEncoding = iEncoding;
  2060.     bModified = FALSE;
  2061.     //bReadOnly = FALSE;
  2062.     SendMessage(hwndEdit,SCI_SETEOLMODE,iEOLMode,0);
  2063.     MRU_AddFile(pFileMRU,szFileName,flagRelativeFileMRU,flagPortableMyDocs);
  2064.     if (flagUseSystemMRU)
  2065.       SHAddToRecentDocs(SHARD_PATHW,szFileName);
  2066.     SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szFileName,
  2067.       iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  2068.       IDS_READONLY,bReadOnly,szTitleExcerpt);
  2069.     // Install watching of the current file
  2070.     if (!bReload && bResetFileWatching)
  2071.       iFileWatchingMode = 0;
  2072.     InstallFileWatching(szCurFile);
  2073.     // the .LOG feature ...
  2074.     if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) {
  2075.       char tchLog[5] = "";
  2076.       SendMessage(hwndEdit,SCI_GETTEXT,5,(LPARAM)tchLog);
  2077.       if (lstrcmpiA(tchLog,".LOG") == 0) {
  2078.         EditJumpTo(hwndEdit,-1,0);
  2079.         SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  2080.         SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  2081.         SendMessage(hwndMain,WM_COMMAND,MAKELONG(IDM_EDIT_INSERT_SHORTDATE,1),0);
  2082.         SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  2083.         SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  2084.         EditJumpTo(hwndEdit,-1,0);
  2085.       }
  2086.     }
  2087.     // Show warning: Unicode file loaded as ANSI
  2088.     if (bUnicodeErr)
  2089.       MsgBox(MBWARN,IDS_ERR_UNICODE);
  2090.   }
  2091.   else if (!bFileTooBig)
  2092.     MsgBox(MBWARN,IDS_ERR_LOADFILE,szFileName);
  2093.   return(fSuccess);
  2094. }
  2095. //=============================================================================
  2096. //
  2097. //  FileSave()
  2098. //
  2099. //
  2100. BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
  2101. {
  2102.   WCHAR tchFile[MAX_PATH];
  2103.   BOOL fSuccess = FALSE;
  2104.   BOOL bCancelDataLoss = FALSE;
  2105.   BOOL bIsEmptyNewFile = FALSE;
  2106.   if (lstrlen(szCurFile) == 0) {
  2107.     int cchText = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
  2108.     if (cchText == 0)
  2109.       bIsEmptyNewFile = TRUE;
  2110.     else if (cchText < 1023) {
  2111.       char tchText[2048];
  2112.       SendMessage(hwndEdit,SCI_GETTEXT,(WPARAM)2047,(LPARAM)tchText);
  2113.       StrTrimA(tchText," tnr");
  2114.       if (lstrlenA(tchText) == 0)
  2115.         bIsEmptyNewFile = TRUE;
  2116.     }
  2117.   }
  2118.   if (!bSaveAlways && (!bModified && iEncoding == iOriginalEncoding || bIsEmptyNewFile) && !bSaveAs)
  2119.     return TRUE;
  2120.   if (bAsk)
  2121.   {
  2122.     // File or "Untitled" ...
  2123.     WCHAR tch[MAX_PATH];
  2124.     if (lstrlen(szCurFile))
  2125.       lstrcpy(tch,szCurFile);
  2126.     else
  2127.       GetString(IDS_UNTITLED,tch,COUNTOF(tch));
  2128.     switch (MsgBox(MBYESNOCANCEL,IDS_ASK_SAVE,tch)) {
  2129.       case IDCANCEL:
  2130.         return FALSE;
  2131.       case IDNO:
  2132.         return TRUE;
  2133.     }
  2134.   }
  2135.   // Read only...
  2136.   if (!bSaveAs && !bSaveCopy && lstrlen(szCurFile))
  2137.   {
  2138.     DWORD dwFileAttributes = GetFileAttributes(szCurFile);
  2139.     if (dwFileAttributes != INVALID_FILE_ATTRIBUTES)
  2140.       bReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY);
  2141.     if (bReadOnly) {
  2142.       SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  2143.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  2144.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  2145.       if (MsgBox(MBYESNOWARN,IDS_READONLY_SAVE,szCurFile) == IDYES)
  2146.         bSaveAs = TRUE;
  2147.       else
  2148.         return FALSE;
  2149.     }
  2150.   }
  2151.   // Save As...
  2152.   if (bSaveAs || bSaveCopy || lstrlen(szCurFile) == 0)
  2153.   {
  2154.     WCHAR tchInitialDir[MAX_PATH] = L"";
  2155.     if (bSaveCopy && lstrlen(tchLastSaveCopyDir)) {
  2156.       lstrcpy(tchInitialDir,tchLastSaveCopyDir);
  2157.       lstrcpy(tchFile,tchLastSaveCopyDir);
  2158.       PathAppend(tchFile,PathFindFileName(szCurFile));
  2159.     }
  2160.     else
  2161.       lstrcpy(tchFile,szCurFile);
  2162.     if (SaveFileDlg(hwndMain,tchFile,COUNTOF(tchFile),tchInitialDir))
  2163.     {
  2164.       if (fSuccess = FileIO(FALSE,tchFile,FALSE,&iEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,bSaveCopy))
  2165.       {
  2166.         if (!bSaveCopy)
  2167.         {
  2168.           lstrcpy(szCurFile,tchFile);
  2169.           SetDlgItemText(hwndMain,IDC_FILENAME,szCurFile);
  2170.           SetDlgItemInt(hwndMain,IDC_REUSELOCK,GetTickCount(),FALSE);
  2171.           if (!fKeepTitleExcerpt)
  2172.             lstrcpy(szTitleExcerpt,L"");
  2173.           Style_SetLexerFromFile(hwndEdit,szCurFile);
  2174.           UpdateStatusbar();
  2175.           UpdateLineNumberWidth();
  2176.         }
  2177.         else {
  2178.           lstrcpy(tchLastSaveCopyDir,tchFile);
  2179.           PathRemoveFileSpec(tchLastSaveCopyDir);
  2180.         }
  2181.       }
  2182.     }
  2183.     else
  2184.       return FALSE;
  2185.   }
  2186.   else
  2187.     fSuccess = FileIO(FALSE,szCurFile,FALSE,&iEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,FALSE);
  2188.   if (fSuccess)
  2189.   {
  2190.     if (!bSaveCopy)
  2191.     {
  2192.       bModified = FALSE;
  2193.       iOriginalEncoding = iEncoding;
  2194.       MRU_AddFile(pFileMRU,szCurFile,flagRelativeFileMRU,flagPortableMyDocs);
  2195.       if (flagUseSystemMRU)
  2196.         SHAddToRecentDocs(SHARD_PATHW,szCurFile);
  2197.       SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  2198.         iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  2199.         IDS_READONLY,bReadOnly,szTitleExcerpt);
  2200.       // Install watching of the current file
  2201.       if (bSaveAs && bResetFileWatching)
  2202.         iFileWatchingMode = 0;
  2203.       InstallFileWatching(szCurFile);
  2204.     }
  2205.   }
  2206.   else if (!bCancelDataLoss)
  2207.   {
  2208.     if (lstrlen(szCurFile) != 0)
  2209.       lstrcpy(tchFile,szCurFile);
  2210.     SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  2211.       iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  2212.       IDS_READONLY,bReadOnly,szTitleExcerpt);
  2213.     MsgBox(MBWARN,IDS_ERR_SAVEFILE,tchFile);
  2214.   }
  2215.   return(fSuccess);
  2216. }
  2217. //=============================================================================
  2218. //
  2219. //  OpenFileDlg()
  2220. //
  2221. //
  2222. BOOL OpenFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
  2223. {
  2224.   OPENFILENAME ofn;
  2225.   WCHAR szFile[MAX_PATH];
  2226.   WCHAR szFilter[NUMLEXERS*1024];
  2227.   WCHAR tchInitialDir[MAX_PATH];
  2228.   lstrcpy(szFile,L"");
  2229.   Style_GetOpenDlgFilterStr(szFilter,COUNTOF(szFilter));
  2230.   if (!lpstrInitialDir) {
  2231.     if (lstrlen(szCurFile)) {
  2232.       lstrcpy(tchInitialDir,szCurFile);
  2233.       PathRemoveFileSpec(tchInitialDir);
  2234.     }
  2235.     else if (lstrlen(tchDefaultDir)) {
  2236.       ExpandEnvironmentStrings(tchDefaultDir,tchInitialDir,COUNTOF(tchInitialDir));
  2237.       if (PathIsRelative(tchInitialDir)) {
  2238.         WCHAR tchModule[MAX_PATH];
  2239.         GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
  2240.         PathRemoveFileSpec(tchModule);
  2241.         PathAppend(tchModule,tchInitialDir);
  2242.         PathCanonicalize(tchInitialDir,tchModule);
  2243.       }
  2244.     }
  2245.   }
  2246.   ZeroMemory(&ofn,sizeof(OPENFILENAME));
  2247.   ofn.lStructSize = sizeof(OPENFILENAME);
  2248.   ofn.hwndOwner = hwnd;
  2249.   ofn.lpstrFilter = szFilter;
  2250.   ofn.lpstrFile = szFile;
  2251.   ofn.lpstrInitialDir = (lpstrInitialDir) ? lpstrInitialDir : tchInitialDir;
  2252.   ofn.nMaxFile = COUNTOF(szFile);
  2253.   ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | /* OFN_NOCHANGEDIR |*/
  2254.             OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST |
  2255.               OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/;
  2256.   ofn.lpstrDefExt = (lstrlen(tchDefaultExtension)) ? tchDefaultExtension : NULL;
  2257.   if (GetOpenFileName(&ofn)) {
  2258.     lstrcpyn(lpstrFile,szFile,cchFile);
  2259.     return TRUE;
  2260.   }
  2261.   else
  2262.     return FALSE;
  2263. }
  2264. //=============================================================================
  2265. //
  2266. //  SaveFileDlg()
  2267. //
  2268. //
  2269. BOOL SaveFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
  2270. {
  2271.   OPENFILENAME ofn;
  2272.   WCHAR szNewFile[MAX_PATH];
  2273.   WCHAR szFilter[NUMLEXERS*1024];
  2274.   WCHAR tchInitialDir[MAX_PATH];
  2275.   lstrcpy(szNewFile,lpstrFile);
  2276.   Style_GetOpenDlgFilterStr(szFilter,COUNTOF(szFilter));
  2277.   if (lstrlen(lpstrInitialDir))
  2278.     lstrcpy(tchInitialDir,lpstrInitialDir);
  2279.   else if (lstrlen(szCurFile)) {
  2280.     lstrcpy(tchInitialDir,szCurFile);
  2281.     PathRemoveFileSpec(tchInitialDir);
  2282.   }
  2283.   else if (lstrlen(tchDefaultDir)) {
  2284.     ExpandEnvironmentStrings(tchDefaultDir,tchInitialDir,COUNTOF(tchInitialDir));
  2285.     if (PathIsRelative(tchInitialDir)) {
  2286.       WCHAR tchModule[MAX_PATH];
  2287.       GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
  2288.       PathRemoveFileSpec(tchModule);
  2289.       PathAppend(tchModule,tchInitialDir);
  2290.       PathCanonicalize(tchInitialDir,tchModule);
  2291.     }
  2292.   }
  2293.   ZeroMemory(&ofn,sizeof(OPENFILENAME));
  2294.   ofn.lStructSize = sizeof(OPENFILENAME);
  2295.   ofn.hwndOwner = hwnd;
  2296.   ofn.lpstrFilter = szFilter;
  2297.   ofn.lpstrFile = szNewFile;
  2298.   ofn.lpstrInitialDir = tchInitialDir;
  2299.   ofn.nMaxFile = MAX_PATH;
  2300.   ofn.Flags = OFN_HIDEREADONLY /*| OFN_NOCHANGEDIR*/ |
  2301.             /*OFN_NODEREFERENCELINKS |*/ OFN_OVERWRITEPROMPT |
  2302.             OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST;
  2303.   ofn.lpstrDefExt = (lstrlen(tchDefaultExtension)) ? tchDefaultExtension : NULL;
  2304.   if (GetSaveFileName(&ofn)) {
  2305.     lstrcpyn(lpstrFile,szNewFile,cchFile);
  2306.     return TRUE;
  2307.   }
  2308.   else
  2309.     return FALSE;
  2310. }
  2311. /******************************************************************************
  2312. *
  2313. * ActivatePrevInst()
  2314. *
  2315. * Tries to find and activate an already open Notepad2 Window
  2316. *
  2317. *
  2318. ******************************************************************************/
  2319. BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
  2320. {
  2321.   BOOL bContinue = TRUE;
  2322.   WCHAR szClassName[64];
  2323.   if (GetClassName(hwnd,szClassName,COUNTOF(szClassName)))
  2324.     if (lstrcmpi(szClassName,wchWndClass) == 0) {
  2325.       DWORD dwReuseLock = GetDlgItemInt(hwnd,IDC_REUSELOCK,NULL,FALSE);
  2326.       if (GetTickCount() - dwReuseLock >= REUSEWINDOWLOCKTIMEOUT) {
  2327.         *(HWND*)lParam = hwnd;
  2328.         if (IsWindowEnabled(hwnd))
  2329.           bContinue = FALSE;
  2330.       }
  2331.     }
  2332.   return(bContinue);
  2333. }
  2334. BOOL CALLBACK EnumWndProc2(HWND hwnd,LPARAM lParam)
  2335. {
  2336.   BOOL bContinue = TRUE;
  2337.   WCHAR szClassName[64];
  2338.   if (GetClassName(hwnd,szClassName,COUNTOF(szClassName)))
  2339.     if (lstrcmpi(szClassName,wchWndClass) == 0) {
  2340.       DWORD dwReuseLock = GetDlgItemInt(hwnd,IDC_REUSELOCK,NULL,FALSE);
  2341.       if (GetTickCount() - dwReuseLock >= REUSEWINDOWLOCKTIMEOUT) {
  2342.         WCHAR tchFileName[MAX_PATH] = L"";
  2343.         if (IsWindowEnabled(hwnd))
  2344.           bContinue = FALSE;
  2345.         GetDlgItemText(hwnd,IDC_FILENAME,tchFileName,COUNTOF(tchFileName));
  2346.         if (lstrcmpi(tchFileName,lpFileArg) == 0)
  2347.           *(HWND*)lParam = hwnd;
  2348.         else
  2349.           bContinue = TRUE;
  2350.       }
  2351.     }
  2352.   return(bContinue);
  2353. }
  2354. BOOL ActivatePrevInst()
  2355. {
  2356.   HWND hwnd = NULL;
  2357.   COPYDATASTRUCT cds;
  2358.   if ((flagNoReuseWindow && !flagSingleFileInstance) || flagStartAsTrayIcon || flagNewFromClipboard || flagPasteBoard)
  2359.     return(FALSE);
  2360.   if (flagSingleFileInstance && lpFileArg) {
  2361.     // Search working directory from second instance, first!
  2362.     // lpFileArg is at least MAX_PATH+2 bytes
  2363.     WCHAR tchTmp[MAX_PATH];
  2364.     ExpandEnvironmentStringsEx(lpFileArg,GlobalSize(lpFileArg)/sizeof(WCHAR));
  2365.     if (PathIsRelative(lpFileArg)) {
  2366.       GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
  2367.       PathAppend(tchTmp,lpFileArg);
  2368.       if (PathFileExists(tchTmp))
  2369.         lstrcpy(lpFileArg,tchTmp);
  2370.       else {
  2371.         if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
  2372.           lstrcpy(lpFileArg,tchTmp);
  2373.         else {
  2374.           GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
  2375.           PathAppend(tchTmp,lpFileArg);
  2376.           lstrcpy(lpFileArg,tchTmp);
  2377.         }
  2378.       }
  2379.     }
  2380.     else if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
  2381.       lstrcpy(lpFileArg,tchTmp);
  2382.     GetLongPathName(lpFileArg,lpFileArg,MAX_PATH);
  2383.     EnumWindows(EnumWndProc2,(LPARAM)&hwnd);
  2384.     if (hwnd != NULL)
  2385.     {
  2386.       // Enabled
  2387.       if (IsWindowEnabled(hwnd))
  2388.       {
  2389.         LPNP2PARAMS params;
  2390.         DWORD cb = sizeof(NP2PARAMS);
  2391.         // Make sure the previous window won't pop up a change notification message
  2392.         //SendMessage(hwnd,WM_CHANGENOTIFYCLEAR,0,0);
  2393.         if (IsIconic(hwnd))
  2394.           ShowWindowAsync(hwnd,SW_RESTORE);
  2395.         if (!IsWindowVisible(hwnd)) {
  2396.           SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK);
  2397.           SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP);
  2398.         }
  2399.         SetForegroundWindow(hwnd);
  2400.         if (lpSchemeArg)
  2401.           cb += (lstrlen(lpSchemeArg) + 1) * sizeof(WCHAR);
  2402.         params = GlobalAlloc(GPTR,cb);
  2403.         params->flagFileSpecified = FALSE;
  2404.         params->flagQuietCreate = FALSE;
  2405.         params->flagLexerSpecified = flagLexerSpecified;
  2406.         if (flagLexerSpecified && lpSchemeArg) {
  2407.           lstrcpy(StrEnd(&params->wchData)+1,lpSchemeArg);
  2408.           params->iInitialLexer = -1;
  2409.         }
  2410.         else
  2411.           params->iInitialLexer = iInitialLexer;
  2412.         params->flagJumpTo = flagJumpTo;
  2413.         params->iInitialLine = iInitialLine;
  2414.         params->iInitialColumn = iInitialColumn;
  2415.         params->iSrcEncoding = (lpEncodingArg) ? Encoding_MatchW(lpEncodingArg) : -1;
  2416.         params->flagSetEncoding = flagSetEncoding;
  2417.         params->flagSetEOLMode = flagSetEOLMode;
  2418.         params->flagTitleExcerpt = 0;
  2419.         cds.dwData = DATA_NOTEPAD2_PARAMS;
  2420.         cds.cbData = GlobalSize(params);
  2421.         cds.lpData = params;
  2422.         SendMessage(hwnd,WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
  2423.         GlobalFree(params);
  2424.         return(TRUE);
  2425.       }
  2426.       else // IsWindowEnabled()
  2427.       {
  2428.         // Ask...
  2429.         if (IDYES == MsgBox(MBYESNO,IDS_ERR_PREVWINDISABLED))
  2430.           return(FALSE);
  2431.         else
  2432.           return(TRUE);
  2433.       }
  2434.     }
  2435.   }
  2436.   if (flagNoReuseWindow)
  2437.     return(FALSE);
  2438.   hwnd = NULL;
  2439.   EnumWindows(EnumWndProc,(LPARAM)&hwnd);
  2440.   // Found a window
  2441.   if (hwnd != NULL)
  2442.   {
  2443.     // Enabled
  2444.     if (IsWindowEnabled(hwnd))
  2445.     {
  2446.       // Make sure the previous window won't pop up a change notification message
  2447.       //SendMessage(hwnd,WM_CHANGENOTIFYCLEAR,0,0);
  2448.       if (IsIconic(hwnd))
  2449.         ShowWindowAsync(hwnd,SW_RESTORE);
  2450.       if (!IsWindowVisible(hwnd)) {
  2451.         SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK);
  2452.         SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP);
  2453.       }
  2454.       SetForegroundWindow(hwnd);
  2455.       if (lpFileArg)
  2456.       {
  2457.         // Search working directory from second instance, first!
  2458.         // lpFileArg is at least MAX_PATH+2 bytes
  2459.         WCHAR tchTmp[MAX_PATH];
  2460.         LPNP2PARAMS params;
  2461.         DWORD cb = sizeof(NP2PARAMS);
  2462.         int cchTitleExcerpt;
  2463.         ExpandEnvironmentStringsEx(lpFileArg,GlobalSize(lpFileArg)/sizeof(WCHAR));
  2464.         if (PathIsRelative(lpFileArg)) {
  2465.           GetCurrentDirectory(COUNTOF(tchTmp),tchTmp);
  2466.           PathAppend(tchTmp,lpFileArg);
  2467.           if (PathFileExists(tchTmp))
  2468.             lstrcpy(lpFileArg,tchTmp);
  2469.           else {
  2470.             if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
  2471.               lstrcpy(lpFileArg,tchTmp);
  2472.           }
  2473.         }
  2474.         else if (SearchPath(NULL,lpFileArg,NULL,COUNTOF(tchTmp),tchTmp,NULL))
  2475.           lstrcpy(lpFileArg,tchTmp);
  2476.         cb += (lstrlen(lpFileArg) + 1) * sizeof(WCHAR);
  2477.         if (lpSchemeArg)
  2478.           cb += (lstrlen(lpSchemeArg) + 1) * sizeof(WCHAR);
  2479.         cchTitleExcerpt = lstrlen(szTitleExcerpt);
  2480.         if (cchTitleExcerpt)
  2481.           cb += (cchTitleExcerpt + 1) * sizeof(WCHAR);
  2482.         params = GlobalAlloc(GPTR,cb);
  2483.         params->flagFileSpecified = TRUE;
  2484.         lstrcpy(&params->wchData,lpFileArg);
  2485.         params->flagQuietCreate = flagQuietCreate;
  2486.         params->flagLexerSpecified = flagLexerSpecified;
  2487.         if (flagLexerSpecified && lpSchemeArg) {
  2488.           lstrcpy(StrEnd(&params->wchData)+1,lpSchemeArg);
  2489.           params->iInitialLexer = -1;
  2490.         }
  2491.         else
  2492.           params->iInitialLexer = iInitialLexer;
  2493.         params->flagJumpTo = flagJumpTo;
  2494.         params->iInitialLine = iInitialLine;
  2495.         params->iInitialColumn = iInitialColumn;
  2496.         params->iSrcEncoding = (lpEncodingArg) ? Encoding_MatchW(lpEncodingArg) : -1;
  2497.         params->flagSetEncoding = flagSetEncoding;
  2498.         params->flagSetEOLMode = flagSetEOLMode;
  2499.         if (cchTitleExcerpt) {
  2500.           lstrcpy(StrEnd(&params->wchData)+1,szTitleExcerpt);
  2501.           params->flagTitleExcerpt = 1;
  2502.         }
  2503.         else
  2504.           params->flagTitleExcerpt = 0;
  2505.         cds.dwData = DATA_NOTEPAD2_PARAMS;
  2506.         cds.cbData = GlobalSize(params);
  2507.         cds.lpData = params;
  2508.         SendMessage(hwnd,WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
  2509.         GlobalFree(params);
  2510.         GlobalFree(lpFileArg);
  2511.       }
  2512.       return(TRUE);
  2513.     }
  2514.     else // IsWindowEnabled()
  2515.     {
  2516.       // Ask...
  2517.       if (IDYES == MsgBox(MBYESNO,IDS_ERR_PREVWINDISABLED))
  2518.         return(FALSE);
  2519.       else
  2520.         return(TRUE);
  2521.     }
  2522.   }
  2523.   else
  2524.     return(FALSE);
  2525. }
  2526. //=============================================================================
  2527. //
  2528. //  RelaunchMultiInst()
  2529. //
  2530. //
  2531. BOOL RelaunchMultiInst() {
  2532.   if (flagMultiFileArg == 2 && cFileList > 1) {
  2533.     int i;
  2534.     STARTUPINFO si;
  2535.     PROCESS_INFORMATION pi;
  2536.     LPWSTR lpCmdLineNew = StrDup(GetCommandLine());
  2537.     LPWSTR lp1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLineNew) + 1));
  2538.     LPWSTR lp2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLineNew) + 1));
  2539.     StrTab2Space(lpCmdLineNew);
  2540.     for (i = 0; i < cFileList; i++) {
  2541.       lstrcpy(lpCmdLineNew + cchiFileList,L" /n ");
  2542.       lstrcat(lpCmdLineNew,lpFileList[i]);
  2543.       LocalFree(lpFileList[i]);
  2544.       ZeroMemory(&si,sizeof(STARTUPINFO));
  2545.       si.cb = sizeof(STARTUPINFO);
  2546.       ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
  2547.       CreateProcess(NULL,lpCmdLineNew,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
  2548.     }
  2549.     LocalFree(lpCmdLineNew);
  2550.     LocalFree(lp1);
  2551.     LocalFree(lp2);
  2552.     GlobalFree(lpFileArg);
  2553.     return TRUE;
  2554.   }
  2555.   else {
  2556.     int i;
  2557.     for (i = 0; i < cFileList; i++)
  2558.       LocalFree(lpFileList[i]);
  2559.     return FALSE;
  2560.   }
  2561. }
  2562. //=============================================================================
  2563. //
  2564. //  RelaunchElevated()
  2565. //
  2566. //
  2567. BOOL RelaunchElevated() {
  2568.   if (!IsVista() || fIsElevated || !flagRelaunchElevated || flagDisplayHelp)
  2569.     return(FALSE);
  2570.   else {
  2571.     LPWSTR lpCmdLine;
  2572.     LPWSTR lpArg1, lpArg2;
  2573.     STARTUPINFO si;
  2574.     SHELLEXECUTEINFO sei;
  2575.     si.cb = sizeof(STARTUPINFO);
  2576.     GetStartupInfo(&si);
  2577.     lpCmdLine = GetCommandLine();
  2578.     lpArg1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  2579.     lpArg2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
  2580.     ExtractFirstArgument(lpCmdLine,lpArg1,lpArg2);
  2581.     if (lstrlen(lpArg1)) {
  2582.       ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  2583.       sei.cbSize = sizeof(SHELLEXECUTEINFO);
  2584.       sei.fMask = SEE_MASK_FLAG_NO_UI | /*SEE_MASK_NOZONECHECKS*/0x00800000;
  2585.       sei.hwnd = NULL;
  2586.       sei.lpVerb = L"runas";
  2587.       sei.lpFile = lpArg1;
  2588.       sei.lpParameters = lpArg2;
  2589.       sei.lpDirectory = NULL;
  2590.       sei.nShow = si.wShowWindow;
  2591.       ShellExecuteEx(&sei);
  2592.     }
  2593.     LocalFree(lpArg1);
  2594.     LocalFree(lpArg2);
  2595.     return(TRUE);
  2596.   }
  2597. }
  2598. //=============================================================================
  2599. //
  2600. //  ShowNotifyIcon()
  2601. //
  2602. //
  2603. void ShowNotifyIcon(HWND hwnd,BOOL bAdd)
  2604. {
  2605.   static HICON hIcon;
  2606.   NOTIFYICONDATA nid;
  2607.   if (!hIcon)
  2608.     hIcon = LoadImage(g_hInstance,MAKEINTRESOURCE(IDR_MAINWND),
  2609.                       IMAGE_ICON,16,16,LR_DEFAULTCOLOR);
  2610.   ZeroMemory(&nid,sizeof(NOTIFYICONDATA));
  2611.   nid.cbSize = sizeof(NOTIFYICONDATA);
  2612.   nid.hWnd = hwnd;
  2613.   nid.uID = 0;
  2614.   nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  2615.   nid.uCallbackMessage = WM_TRAYMESSAGE;
  2616.   nid.hIcon = hIcon;
  2617.   lstrcpy(nid.szTip,L"Notepad2");
  2618.   if(bAdd)
  2619.     Shell_NotifyIcon(NIM_ADD,&nid);
  2620.   else
  2621.     Shell_NotifyIcon(NIM_DELETE,&nid);
  2622. }
  2623. //=============================================================================
  2624. //
  2625. //  SetNotifyIconTitle()
  2626. //
  2627. //
  2628. void SetNotifyIconTitle(HWND hwnd)
  2629. {
  2630.   NOTIFYICONDATA nid;
  2631.   SHFILEINFO shfi;
  2632.   WCHAR tchTitle[128];
  2633.   WCHAR tchFormat[32];
  2634.   ZeroMemory(&nid,sizeof(NOTIFYICONDATA));
  2635.   nid.cbSize = sizeof(NOTIFYICONDATA);
  2636.   nid.hWnd = hwnd;
  2637.   nid.uID = 0;
  2638.   nid.uFlags = NIF_TIP;
  2639.   if (lstrlen(szTitleExcerpt)) {
  2640.     GetString(IDS_TITLEEXCERPT,tchFormat,COUNTOF(tchFormat));
  2641.     wsprintf(tchTitle,tchFormat,szTitleExcerpt);
  2642.   }
  2643.   else if (lstrlen(szCurFile)) {
  2644.     SHGetFileInfo2(szCurFile,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
  2645.     PathCompactPathEx(tchTitle,shfi.szDisplayName,COUNTOF(tchTitle)-4,0);
  2646.   }
  2647.   else
  2648.     GetString(IDS_UNTITLED,tchTitle,COUNTOF(tchTitle)-4);
  2649.   if (bModified || iEncoding != iOriginalEncoding)
  2650.     lstrcpy(nid.szTip,L"* ");
  2651.   else
  2652.     lstrcpy(nid.szTip,L"");
  2653.   lstrcat(nid.szTip,tchTitle);
  2654.   Shell_NotifyIcon(NIM_MODIFY,&nid);
  2655. }
  2656. //=============================================================================
  2657. //
  2658. //  InstallFileWatching()
  2659. //
  2660. //
  2661. void InstallFileWatching(LPCWSTR lpszFile)
  2662. {
  2663.   WCHAR tchDirectory[MAX_PATH];
  2664.   HANDLE hFind;
  2665.   // Terminate
  2666.   if (!iFileWatchingMode || !lpszFile || lstrlen(lpszFile) == 0)
  2667.   {
  2668.     if (bRunningWatch)
  2669.     {
  2670.       FindCloseChangeNotification(hChangeHandle);
  2671.       KillTimer(NULL,ID_WATCHTIMER);
  2672.       bRunningWatch = FALSE;
  2673.       //bPendingChangeNotify = FALSE;
  2674.     }
  2675.     return;
  2676.   }
  2677.   // Install
  2678.   else
  2679.   {
  2680.     // Terminate previous watching
  2681.     if (bRunningWatch) {
  2682.       FindCloseChangeNotification(hChangeHandle);
  2683.       //bPendingChangeNotify = FALSE;
  2684.     }
  2685.     // No previous watching installed, so launch the timer first
  2686.     else
  2687.       SetTimer(NULL,ID_WATCHTIMER,iFileCheckInverval,WatchTimerProc);
  2688.     lstrcpy(tchDirectory,lpszFile);
  2689.     PathRemoveFileSpec(tchDirectory);
  2690.     // Save data of current file
  2691.     hFind = FindFirstFile(szCurFile,&fdCurFile);
  2692.     if (hFind != INVALID_HANDLE_VALUE)
  2693.       FindClose(hFind);
  2694.     else
  2695.       ZeroMemory(&fdCurFile,sizeof(WIN32_FIND_DATA));
  2696.     hChangeHandle = FindFirstChangeNotification(tchDirectory,FALSE,
  2697.       FILE_NOTIFY_CHANGE_FILE_NAME  | 
  2698.       FILE_NOTIFY_CHANGE_DIR_NAME   | 
  2699.       FILE_NOTIFY_CHANGE_ATTRIBUTES | 
  2700.       FILE_NOTIFY_CHANGE_SIZE | 
  2701.       FILE_NOTIFY_CHANGE_LAST_WRITE);
  2702.     bRunningWatch = TRUE;
  2703.     //bPendingChangeNotify = FALSE;
  2704.   }
  2705. }
  2706. //=============================================================================
  2707. //
  2708. //  WatchTimerProc()
  2709. //
  2710. //
  2711. void CALLBACK WatchTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
  2712. {
  2713.   // Check Change Notification Handle
  2714.   if (bRunningWatch && WAIT_OBJECT_0 == WaitForSingleObject(hChangeHandle,0))
  2715.   {
  2716.     // Check if the changes affect the current file
  2717.     WIN32_FIND_DATA fdUpdated;
  2718.     HANDLE hFind = FindFirstFile(szCurFile,&fdUpdated);
  2719.     if (INVALID_HANDLE_VALUE != hFind)
  2720.       FindClose(hFind);
  2721.     else
  2722.       // The current file has been removed
  2723.       ZeroMemory(&fdUpdated,sizeof(WIN32_FIND_DATA));
  2724.     // Check if the file has been changed
  2725.     if (CompareFileTime(&fdCurFile.ftLastWriteTime,&fdUpdated.ftLastWriteTime) != 0 ||
  2726.           fdCurFile.nFileSizeLow != fdUpdated.nFileSizeLow ||
  2727.           fdCurFile.nFileSizeHigh != fdUpdated.nFileSizeHigh)
  2728.     {
  2729.       // Shutdown current watching and give control to main window
  2730.       bRunningWatch = FALSE;
  2731.       //bPendingChangeNotify = TRUE;
  2732.       FindCloseChangeNotification(hChangeHandle);
  2733.       KillTimer(NULL,ID_WATCHTIMER);
  2734.       SendMessage(hwndMain,WM_CHANGENOTIFY,0,0);
  2735.     }
  2736.     else
  2737.       FindNextChangeNotification(hChangeHandle);
  2738.   }
  2739. }
  2740. //=============================================================================
  2741. //
  2742. //  PasteBoardTimer()
  2743. //
  2744. //
  2745. void CALLBACK PasteBoardTimer(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
  2746. {
  2747.   if (dwLastCopyTime > 0 && GetTickCount() - dwLastCopyTime > 200) {
  2748.     if (SendMessage(hwndEdit,SCI_CANPASTE,0,0)) {
  2749.       BOOL bAutoIndent2 = bAutoIndent;
  2750.       bAutoIndent = 0;
  2751.       EditJumpTo(hwndEdit,-1,0);
  2752.       SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  2753.       if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) > 0)
  2754.         SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  2755.       SendMessage(hwndEdit,SCI_PASTE,0,0);
  2756.       SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  2757.       SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  2758.       bAutoIndent = bAutoIndent2;
  2759.     }
  2760.     dwLastCopyTime = 0;
  2761.   }
  2762. }
  2763. ///  End of Notepad2.c  \