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

RichEdit

开发平台:

Visual C++

  1. /******************************************************************************
  2. *
  3. *
  4. * Notepad2
  5. *
  6. * Notepad2.c
  7. *   Main application window functionality
  8. *
  9. * See Readme.txt for more information about this source code.
  10. * Please send me your comments to this work.
  11. *
  12. * See License.txt for details about distribution and modification.
  13. *
  14. *                                              (c) Florian Balmer 1996-2010
  15. *                                                  florian.balmer@gmail.com
  16. *                                               http://www.flos-freeware.ch
  17. *
  18. *
  19. ******************************************************************************/
  20. #define _WIN32_WINNT 0x501
  21. #include <windows.h>
  22. #include <commctrl.h>
  23. #include <shlobj.h>
  24. #include <shlwapi.h>
  25. #include <shellapi.h>
  26. #include <commdlg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include "scintilla.h"
  31. #include "scilexer.h"
  32. #include "edit.h"
  33. #include "styles.h"
  34. #include "helpers.h"
  35. #include "dialogs.h"
  36. #include "notepad2.h"
  37. #include "resource.h"
  38. /******************************************************************************
  39. *
  40. * Local and global Variables for Notepad2.c
  41. *
  42. */
  43. HWND      hwndStatus;
  44. HWND      hwndToolbar;
  45. HWND      hwndReBar;
  46. HWND      hwndEdit;
  47. HWND      hwndEditFrame;
  48. HWND      hwndMain;
  49. HWND      hwndNextCBChain = NULL;
  50. HWND      hDlgFindReplace = NULL;
  51. #define NUMTOOLBITMAPS  23
  52. #define NUMINITIALTOOLS 24
  53. TBBUTTON  tbbMainWnd[] = { {0,IDT_FILE_NEW,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  54.                            {1,IDT_FILE_OPEN,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  55.                            {2,IDT_FILE_BROWSE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  56.                            {3,IDT_FILE_SAVE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  57.                            {0,0,0,TBSTYLE_SEP,0,0},
  58.                            {4,IDT_EDIT_UNDO,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  59.                            {5,IDT_EDIT_REDO,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  60.                            {0,0,0,TBSTYLE_SEP,0,0},
  61.                            {6,IDT_EDIT_CUT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  62.                            {7,IDT_EDIT_COPY,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  63.                            {8,IDT_EDIT_PASTE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  64.                            {0,0,0,TBSTYLE_SEP,0,0},
  65.                            {9,IDT_EDIT_FIND,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  66.                            {10,IDT_EDIT_REPLACE,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  67.                            {0,0,0,TBSTYLE_SEP,0,0},
  68.                            {11,IDT_VIEW_WORDWRAP,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  69.                            {0,0,0,TBSTYLE_SEP,0,0},
  70.                            {12,IDT_VIEW_ZOOMIN,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  71.                            {13,IDT_VIEW_ZOOMOUT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  72.                            {0,0,0,TBSTYLE_SEP,0,0},
  73.                            {14,IDT_VIEW_SCHEME,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  74.                            {15,IDT_VIEW_SCHEMECONFIG,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  75.                            {0,0,0,TBSTYLE_SEP,0,0},
  76.                            {16,IDT_FILE_EXIT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  77.                            {17,IDT_FILE_SAVEAS,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  78.                            {18,IDT_FILE_SAVECOPY,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  79.                            {19,IDT_EDIT_CLEAR,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  80.                            {20,IDT_FILE_PRINT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  81.                            {21,IDT_FILE_OPENFAV,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},
  82.                            {22,IDT_FILE_ADDTOFAV,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0} };
  83. WCHAR      szIniFile[MAX_PATH] = L"";
  84. WCHAR      szIniFile2[MAX_PATH] = L"";
  85. BOOL      bSaveSettings;
  86. BOOL      bSaveRecentFiles;
  87. BOOL      bSaveFindReplace;
  88. WCHAR      tchLastSaveCopyDir[MAX_PATH] = L"";
  89. WCHAR      tchOpenWithDir[MAX_PATH];
  90. WCHAR      tchFavoritesDir[MAX_PATH];
  91. WCHAR      tchDefaultDir[MAX_PATH];
  92. WCHAR      tchDefaultExtension[64];
  93. WCHAR      tchFileDlgFilters[5*1024];
  94. WCHAR      tchToolbarButtons[512];
  95. WCHAR      tchToolbarBitmap[MAX_PATH];
  96. WCHAR      tchToolbarBitmapHot[MAX_PATH];
  97. WCHAR      tchToolbarBitmapDisabled[MAX_PATH];
  98. int       iPathNameFormat;
  99. BOOL      fWordWrap;
  100. int       iWordWrapMode;
  101. int       iWordWrapIndent;
  102. int       iWordWrapSymbols;
  103. BOOL      bShowWordWrapSymbols;
  104. BOOL      bMatchBraces;
  105. BOOL      bAutoIndent;
  106. BOOL      bAutoCloseTags;
  107. BOOL      bShowIndentGuides;
  108. BOOL      bHiliteCurrentLine;
  109. BOOL      bTabsAsSpaces;
  110. BOOL      bTabsAsSpacesG;
  111. BOOL      bTabIndents;
  112. int       iTabWidth;
  113. int       iTabWidthG;
  114. int       iIndentWidth;
  115. int       iIndentWidthG;
  116. BOOL      bMarkLongLines;
  117. int       iLongLinesLimit;
  118. int       iLongLinesLimitG;
  119. int       iLongLineMode;
  120. int       iWrapCol = 0;
  121. BOOL      bShowSelectionMargin;
  122. BOOL      bShowLineNumbers;
  123. BOOL      bViewWhiteSpace;
  124. BOOL      bViewEOLs;
  125. int       iDefaultEncoding;
  126. BOOL      bSkipUnicodeDetection;
  127. BOOL      bLoadASCIIasUTF8;
  128. BOOL      bNoEncodingTags;
  129. int       iSrcEncoding = -1;
  130. int       iWeakSrcEncoding = -1;
  131. int       iDefaultEOLMode;
  132. BOOL      bFixLineEndings;
  133. BOOL      bAutoStripBlanks;
  134. int       iPrintHeader;
  135. int       iPrintFooter;
  136. int       iPrintColor;
  137. int       iPrintZoom;
  138. RECT      pagesetupMargin;
  139. BOOL      bSaveBeforeRunningTools;
  140. int       iFileWatchingMode;
  141. BOOL      bResetFileWatching;
  142. int       iFileCheckInverval;
  143. int       iEscFunction;
  144. BOOL      bAlwaysOnTop;
  145. BOOL      bMinimizeToTray;
  146. BOOL      bTransparentMode;
  147. BOOL      bTransparentModeAvailable;
  148. BOOL      bShowToolbar;
  149. BOOL      bShowStatusbar;
  150. typedef struct _wi
  151. {
  152.   int x;
  153.   int y;
  154.   int cx;
  155.   int cy;
  156.   int max;
  157. } WININFO;
  158. WININFO wi;
  159. BOOL    bStickyWinPos;
  160. BOOL    bIsAppThemed;
  161. int     cyReBar;
  162. int     cyReBarFrame;
  163. int     cxEditFrame;
  164. int     cyEditFrame;
  165. int     cxEncodingDlg;
  166. int     cyEncodingDlg;
  167. int     cxRecodeDlg;
  168. int     cyRecodeDlg;
  169. int     cxFileMRUDlg;
  170. int     cyFileMRUDlg;
  171. int     cxOpenWithDlg;
  172. int     cyOpenWithDlg;
  173. int     cxFavoritesDlg;
  174. int     cyFavoritesDlg;
  175. int     xFindReplaceDlg;
  176. int     yFindReplaceDlg;
  177. LPWSTR      lpFileList[32];
  178. int         cFileList = 0;
  179. int         cchiFileList = 0;
  180. LPWSTR      lpFileArg = NULL;
  181. LPWSTR      lpSchemeArg = NULL;
  182. LPWSTR      lpMatchArg = NULL;
  183. LPWSTR      lpEncodingArg = NULL;
  184. LPMRULIST  pFileMRU;
  185. LPMRULIST  mruFind;
  186. LPMRULIST  mruReplace;
  187. DWORD     dwLastIOError;
  188. WCHAR      szCurFile[MAX_PATH+40];
  189. FILEVARS   fvCurFile;
  190. BOOL      bModified;
  191. BOOL      bReadOnly = FALSE;
  192. int       iEncoding;
  193. int       iOriginalEncoding;
  194. int       iEOLMode;
  195. int       iDefaultCodePage;
  196. int       iDefaultCharSet;
  197. int       iInitialLine;
  198. int       iInitialColumn;
  199. int       iInitialLexer;
  200. BOOL      bLastCopyFromMe = FALSE;
  201. DWORD     dwLastCopyTime;
  202. UINT      uidsAppTitle = IDS_APPTITLE;
  203. WCHAR     szTitleExcerpt[128] = L"";
  204. int       fKeepTitleExcerpt = 0;
  205. HANDLE    hChangeHandle = NULL;
  206. BOOL      bRunningWatch = FALSE;
  207. WIN32_FIND_DATA fdCurFile;
  208. UINT      msgTaskbarCreated = 0;
  209. HMODULE   hModUxTheme = NULL;
  210. EDITFINDREPLACE efrData = { "", "", "", "", 0, 0, 0, 0, 0, 0, NULL };
  211. UINT cpLastFind = 0;
  212. BOOL bReplaceInitialized = FALSE;
  213. extern NP2ENCODING mEncoding[];
  214. int iLineEndings[3] = {
  215.   SC_EOL_CRLF,
  216.   SC_EOL_LF,
  217.   SC_EOL_CR
  218. };
  219. WCHAR wchPrefixSelection[256] = L"";
  220. WCHAR wchAppendSelection[256] = L"";
  221. WCHAR wchPrefixLines[256] = L"";
  222. WCHAR wchAppendLines[256] = L"";
  223. int   iSortOptions = 0;
  224. BOOL      fIsElevated = FALSE;
  225. WCHAR     wchWndClass[16] = WC_NOTEPAD2;
  226. LPMALLOC  g_lpMalloc;
  227. HINSTANCE g_hInstance;
  228. UINT16    g_uWinVer;
  229. WCHAR     g_wchAppUserModelID[64];
  230. //=============================================================================
  231. //
  232. // Flags
  233. //
  234. int flagNoReuseWindow      = 0;
  235. int flagReuseWindow        = 0;
  236. int flagMultiFileArg       = 0;
  237. int flagSingleFileInstance = 0;
  238. int flagStartAsTrayIcon    = 0;
  239. int flagRelativeFileMRU    = 0;
  240. int flagPortableMyDocs     = 0;
  241. int flagNoFadeHidden       = 0;
  242. int flagSimpleIndentGuides = 0;
  243. int fNoHTMLGuess           = 0;
  244. int fNoCGIGuess            = 0;
  245. int fNoFileVariables       = 0;
  246. int flagPosParam           = 0;
  247. int flagDefaultPos         = 0;
  248. int flagNewFromClipboard   = 0;
  249. int flagPasteBoard         = 0;
  250. int flagSetEncoding        = 0;
  251. int flagSetEOLMode         = 0;
  252. int flagJumpTo             = 0;
  253. int flagMatchText          = 0;
  254. int flagLexerSpecified     = 0;
  255. int flagQuietCreate        = 0;
  256. int flagUseSystemMRU       = 0;
  257. int flagRelaunchElevated   = 0;
  258. int flagDisplayHelp        = 0;
  259. //=============================================================================
  260. //
  261. //  WinMain()
  262. //
  263. //
  264. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,
  265.                    LPSTR lpCmdLine,int nCmdShow)
  266. {
  267.   MSG msg;
  268.   HWND hwnd;
  269.   HACCEL hAccMain;
  270.   HACCEL hAccFindReplace;
  271.   INITCOMMONCONTROLSEX icex;
  272.   //HMODULE hSciLexer;
  273.   // Set global variable g_hInstance
  274.   g_hInstance = hInstance;
  275.   // Set the Windows version global variable
  276.   g_uWinVer = LOWORD(GetVersion());
  277.   g_uWinVer = MAKEWORD(HIBYTE(g_uWinVer),LOBYTE(g_uWinVer));
  278.   SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
  279.   // check if running at least on Windows 2000
  280.   if (!Is2k()) {
  281.     LPVOID lpMsgBuf;
  282.     FormatMessage(
  283.         FORMAT_MESSAGE_ALLOCATE_BUFFER|
  284.         FORMAT_MESSAGE_FROM_SYSTEM|
  285.         FORMAT_MESSAGE_IGNORE_INSERTS,
  286.         NULL,
  287.         ERROR_OLD_WIN_VERSION,
  288.         MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Default language
  289.         (LPWSTR)&lpMsgBuf,
  290.         0,
  291.         NULL);
  292.     MessageBox(NULL,(LPCWSTR)lpMsgBuf,L"Notepad2",MB_OK|MB_ICONEXCLAMATION);
  293.     LocalFree(lpMsgBuf);
  294.     return(0);
  295.   }
  296.   // Check if running with elevated privileges
  297.   fIsElevated = IsElevated();
  298.   // Default Encodings (may already be used for command line parsing)
  299.   Encoding_InitDefaults();
  300.   // Command Line, Ini File and Flags
  301.   ParseCommandLine();
  302.   FindIniFile();
  303.   TestIniFile();
  304.   CreateIniFile();
  305.   LoadFlags();
  306.   // set AppUserModelID
  307.   PrivateSetCurrentProcessExplicitAppUserModelID(g_wchAppUserModelID);
  308.   // Command Line Help Dialog
  309.   if (flagDisplayHelp) {
  310.     DisplayCmdLineHelp();
  311.     return(0);
  312.   }
  313.   // Adapt window class name
  314.   if (fIsElevated)
  315.     StrCat(wchWndClass,L"U");
  316.   if (flagPasteBoard)
  317.     StrCat(wchWndClass,L"B");
  318.   // Relaunch with elevated privileges
  319.   if (RelaunchElevated())
  320.     return(0);
  321.   // Try to run multiple instances
  322.   if (RelaunchMultiInst())
  323.     return(0);
  324.   // Try to activate another window
  325.   if (ActivatePrevInst())
  326.     return(0);
  327.   // Init OLE and Common Controls
  328.   OleInitialize(NULL);
  329.   SHGetMalloc(&g_lpMalloc);
  330.   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  331.   icex.dwICC  = ICC_WIN95_CLASSES|ICC_COOL_CLASSES|ICC_BAR_CLASSES|ICC_USEREX_CLASSES;
  332.   InitCommonControlsEx(&icex);
  333.   msgTaskbarCreated = RegisterWindowMessage(L"TaskbarCreated");
  334.   hModUxTheme = LoadLibrary(L"uxtheme.dll");
  335.   Scintilla_RegisterClasses(hInstance);
  336.   // Load Settings
  337.   LoadSettings();
  338.   if (!InitApplication(hInstance))
  339.     return FALSE;
  340.   if (!(hwnd = InitInstance(hInstance,lpCmdLine,nCmdShow)))
  341.     return FALSE;
  342.   hAccMain = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_MAINWND));
  343.   hAccFindReplace = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCFINDREPLACE));
  344.   while (GetMessage(&msg,NULL,0,0))
  345.   {
  346.     if (IsWindow(hDlgFindReplace) && (msg.hwnd == hDlgFindReplace || IsChild(hDlgFindReplace,msg.hwnd)))
  347.       if (TranslateAccelerator(hDlgFindReplace,hAccFindReplace,&msg) || IsDialogMessage(hDlgFindReplace,&msg))
  348.         continue;
  349.     if (!TranslateAccelerator(hwnd,hAccMain,&msg)) {
  350.       TranslateMessage(&msg);
  351.       DispatchMessage(&msg);
  352.     }
  353.   }
  354.   // Save Settings is done elsewhere
  355.   Scintilla_ReleaseResources();
  356.   UnregisterClass(wchWndClass,hInstance);
  357.   if (hModUxTheme)
  358.     FreeLibrary(hModUxTheme);
  359.   g_lpMalloc->lpVtbl->Release(g_lpMalloc);
  360.   OleUninitialize();
  361.   return(msg.wParam);
  362.   hPrevInst;
  363. }
  364. //=============================================================================
  365. //
  366. //  InitApplication()
  367. //
  368. //
  369. BOOL InitApplication(HINSTANCE hInstance)
  370. {
  371.   WNDCLASS   wc;
  372.   wc.style         = CS_BYTEALIGNWINDOW | CS_DBLCLKS;
  373.   wc.lpfnWndProc   = (WNDPROC)MainWndProc;
  374.   wc.cbClsExtra    = 0;
  375.   wc.cbWndExtra    = 0;
  376.   wc.hInstance     = hInstance;
  377.   wc.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(IDR_MAINWND));
  378.   wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
  379.   wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  380.   wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MAINWND);
  381.   wc.lpszClassName = wchWndClass;
  382.   return RegisterClass(&wc);
  383. }
  384. //=============================================================================
  385. //
  386. //  InitInstance()
  387. //
  388. //
  389. HWND InitInstance(HINSTANCE hInstance,LPSTR pszCmdLine,int nCmdShow)
  390. {
  391.   RECT rc = { wi.x, wi.y, wi.x+wi.cx, wi.y+wi.cy };
  392.   RECT rc2;
  393.   MONITORINFO mi;
  394.   HMONITOR hMonitor = MonitorFromRect(&rc,MONITOR_DEFAULTTONEAREST);
  395.   mi.cbSize = sizeof(mi);
  396.   GetMonitorInfo(hMonitor,&mi);
  397.   if (flagDefaultPos == 1) {
  398.     wi.x = wi.y = wi.cx = wi.cy = CW_USEDEFAULT;
  399.     wi.max = 0;
  400.   }
  401.   else if (flagDefaultPos >= 4) {
  402.     SystemParametersInfo(SPI_GETWORKAREA,0,&rc,0);
  403.     if (flagDefaultPos & 8)
  404.       wi.x = (rc.right - rc.left) / 2;
  405.     else
  406.       wi.x = rc.left;
  407.     wi.cx = rc.right - rc.left;
  408.     if (flagDefaultPos & (4|8))
  409.       wi.cx /= 2;
  410.     if (flagDefaultPos & 32)
  411.       wi.y = (rc.bottom - rc.top) / 2;
  412.     else
  413.       wi.y = rc.top;
  414.     wi.cy = rc.bottom - rc.top;
  415.     if (flagDefaultPos & (16|32))
  416.       wi.cy /= 2;
  417.     if (flagDefaultPos & 64) {
  418.       wi.x = rc.left;
  419.       wi.y = rc.top;
  420.       wi.cx = rc.right - rc.left;
  421.       wi.cy = rc.bottom - rc.top;
  422.     }
  423.     if (flagDefaultPos & 128) {
  424.       wi.x += (flagDefaultPos & 8) ? 4 : 8;
  425.       wi.cx -= (flagDefaultPos & (4|8)) ? 12 : 16;
  426.       wi.y += (flagDefaultPos & 32) ? 4 : 8;
  427.       wi.cy -= (flagDefaultPos & (16|32)) ? 12 : 16;
  428.     }
  429.   }
  430.   else if (flagDefaultPos == 2 || flagDefaultPos == 3 ||
  431.       wi.x == CW_USEDEFAULT || wi.y == CW_USEDEFAULT ||
  432.       wi.cx == CW_USEDEFAULT || wi.cy == CW_USEDEFAULT) {
  433.     // default window position
  434.     SystemParametersInfo(SPI_GETWORKAREA,0,&rc,0);
  435.     wi.y = rc.top + 16;
  436.     wi.cy = rc.bottom - rc.top - 32;
  437.     wi.cx = min(rc.right - rc.left - 32,wi.cy);
  438.     wi.x = (flagDefaultPos == 3) ? 16 : rc.right - wi.cx - 16;
  439.   }
  440.   else {
  441.     // fit window into working area of current monitor
  442.     wi.x += (mi.rcWork.left - mi.rcMonitor.left);
  443.     wi.y += (mi.rcWork.top - mi.rcMonitor.top);
  444.     if (wi.x < mi.rcWork.left)
  445.       wi.x = mi.rcWork.left;
  446.     if (wi.y < mi.rcWork.top)
  447.       wi.y = mi.rcWork.top;
  448.     if (wi.x + wi.cx > mi.rcWork.right) {
  449.       wi.x -= (wi.x + wi.cx - mi.rcWork.right);
  450.       if (wi.x < mi.rcWork.left)
  451.         wi.x = mi.rcWork.left;
  452.       if (wi.x + wi.cx > mi.rcWork.right)
  453.         wi.cx = mi.rcWork.right - wi.x;
  454.     }
  455.     if (wi.y + wi.cy > mi.rcWork.bottom) {
  456.       wi.y -= (wi.y + wi.cy - mi.rcWork.bottom);
  457.       if (wi.y < mi.rcWork.top)
  458.         wi.y = mi.rcWork.top;
  459.       if (wi.y + wi.cy > mi.rcWork.bottom)
  460.         wi.cy = mi.rcWork.bottom - wi.y;
  461.     }
  462.     SetRect(&rc,wi.x,wi.y,wi.x+wi.cx,wi.y+wi.cy);
  463.     if (!IntersectRect(&rc2,&rc,&mi.rcWork)) {
  464.       wi.y = mi.rcWork.top + 16;
  465.       wi.cy = mi.rcWork.bottom - mi.rcWork.top - 32;
  466.       wi.cx = min(mi.rcWork.right - mi.rcWork.left - 32,wi.cy);
  467.       wi.x = mi.rcWork.right - wi.cx - 16;
  468.     }
  469.   }
  470.   hwndMain = CreateWindowEx(
  471.                0,
  472.                wchWndClass,
  473.                L"Notepad2",
  474.                WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  475.                wi.x,
  476.                wi.y,
  477.                wi.cx,
  478.                wi.cy,
  479.                NULL,
  480.                NULL,
  481.                hInstance,
  482.                NULL);
  483.   if (wi.max)
  484.     nCmdShow = SW_SHOWMAXIMIZED;
  485.   if (bAlwaysOnTop)
  486.     SetWindowPos(hwndMain,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  487.   if (bTransparentMode)
  488.     SetWindowTransparentMode(hwndMain,TRUE);
  489.   // Current file information -- moved in front of ShowWindow()
  490.   FileLoad(TRUE,TRUE,FALSE,FALSE,L"");
  491.   if (!flagStartAsTrayIcon) {
  492.     ShowWindow(hwndMain,nCmdShow);
  493.     UpdateWindow(hwndMain);
  494.   }
  495.   else {
  496.     ShowWindow(hwndMain,SW_HIDE);    // trick ShowWindow()
  497.     ShowNotifyIcon(hwndMain,TRUE);
  498.   }
  499.   // Source Encoding
  500.   if (lpEncodingArg)
  501.     iSrcEncoding = Encoding_MatchW(lpEncodingArg);
  502.   // Pathname parameter
  503.   if (lpFileArg /*&& !flagNewFromClipboard*/)
  504.   {
  505.     // Open from Directory
  506.     if (PathIsDirectory(lpFileArg)) {
  507.       WCHAR tchFile[MAX_PATH];
  508.       if (OpenFileDlg(hwndMain,tchFile,COUNTOF(tchFile),lpFileArg))
  509.         FileLoad(FALSE,FALSE,FALSE,FALSE,tchFile);
  510.     }
  511.     else {
  512.       if (FileLoad(FALSE,FALSE,FALSE,FALSE,lpFileArg)) {
  513.         if (flagJumpTo) // Jump to position
  514.           EditJumpTo(hwndEdit,iInitialLine,iInitialColumn);
  515.       }
  516.     }
  517.     GlobalFree(lpFileArg);
  518.   }
  519.   else {
  520.     if (iSrcEncoding != -1) {
  521.       iEncoding = iSrcEncoding;
  522.       iOriginalEncoding = iSrcEncoding;
  523.       SendMessage(hwndEdit,SCI_SETCODEPAGE,(iEncoding == CPI_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
  524.     }
  525.   }
  526.   // reset
  527.   iSrcEncoding = -1;
  528.   flagQuietCreate = 0;
  529.   fKeepTitleExcerpt = 0;
  530.   // Check for /c [if no file is specified] -- even if a file is specified
  531.   /*else */if (flagNewFromClipboard) {
  532.     if (SendMessage(hwndEdit,SCI_CANPASTE,0,0)) {
  533.       BOOL bAutoIndent2 = bAutoIndent;
  534.       bAutoIndent = 0;
  535.       EditJumpTo(hwndEdit,-1,0);
  536.       SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  537.       if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) > 0)
  538.         SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  539.       SendMessage(hwndEdit,SCI_PASTE,0,0);
  540.       SendMessage(hwndEdit,SCI_NEWLINE,0,0);
  541.       SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  542.       bAutoIndent = bAutoIndent2;
  543.     }
  544.   }
  545.   // Encoding
  546.   if (0 != flagSetEncoding) {
  547.     SendMessage(
  548.       hwndMain,
  549.       WM_COMMAND,
  550.       MAKELONG(IDM_ENCODING_ANSI + flagSetEncoding -1,1),
  551.       0);
  552.     flagSetEncoding = 0;
  553.   }
  554.   // EOL mode
  555.   if (0 != flagSetEOLMode) {
  556.     SendMessage(
  557.       hwndMain,
  558.       WM_COMMAND,
  559.       MAKELONG(IDM_LINEENDINGS_CRLF + flagSetEOLMode -1,1),
  560.       0);
  561.     flagSetEOLMode = 0;
  562.   }
  563.   // Match Text
  564.   if (flagMatchText && lpMatchArg) {
  565.     if (lstrlen(lpMatchArg) && SendMessage(hwndEdit,SCI_GETLENGTH,0,0)) {
  566.       UINT cp = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
  567.       WideCharToMultiByte(cp,0,lpMatchArg,-1,efrData.szFind,COUNTOF(efrData.szFind),NULL,NULL);
  568.       WideCharToMultiByte(CP_UTF8,0,lpMatchArg,-1,efrData.szFindUTF8,COUNTOF(efrData.szFindUTF8),NULL,NULL);
  569.       cpLastFind = cp;
  570.       if (flagMatchText & 4)
  571.         efrData.fuFlags |= SCFIND_REGEXP;
  572.       else if (flagMatchText & 8)
  573.         efrData.bTransformBS = TRUE;
  574.       if (flagMatchText & 2) {
  575.         EditJumpTo(hwndEdit,-1,0);
  576.         EditFindPrev(hwndEdit,&efrData,FALSE);
  577.       }
  578.       else {
  579.         SendMessage(hwndEdit,SCI_DOCUMENTSTART,0,0);
  580.         EditFindNext(hwndEdit,&efrData,FALSE);
  581.       }
  582.     }
  583.     GlobalFree(lpMatchArg);
  584.   }
  585.   // Check for Paste Board option -- after loading files
  586.   if (flagPasteBoard) {
  587.     bLastCopyFromMe = TRUE;
  588.     hwndNextCBChain = SetClipboardViewer(hwndMain);
  589.     uidsAppTitle = IDS_APPTITLE_PASTEBOARD;
  590.     SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  591.       iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  592.       IDS_READONLY,bReadOnly,szTitleExcerpt);
  593.     bLastCopyFromMe = FALSE;
  594.     dwLastCopyTime = 0;
  595.     SetTimer(hwndMain,ID_PASTEBOARDTIMER,100,PasteBoardTimer);
  596.   }
  597.   // check if a lexer was specified from the command line
  598.   if (flagLexerSpecified) {
  599.     if (lpSchemeArg) {
  600.       Style_SetLexerFromName(hwndEdit,szCurFile,lpSchemeArg);
  601.       LocalFree(lpSchemeArg);
  602.     }
  603.     else if (iInitialLexer >=0 && iInitialLexer < NUMLEXERS)
  604.       Style_SetLexerFromID(hwndEdit,iInitialLexer);
  605.     flagLexerSpecified = 0;
  606.   }
  607.   // If start as tray icon, set current filename as tooltip
  608.   if (flagStartAsTrayIcon)
  609.     SetNotifyIconTitle(hwndMain);
  610.   UpdateToolbar();
  611.   UpdateStatusbar();
  612.   return(hwndMain);
  613. }
  614. //=============================================================================
  615. //
  616. //  MainWndProc()
  617. //
  618. //  Messages are distributed to the MsgXXX-handlers
  619. //
  620. //
  621. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam)
  622. {
  623.   static BOOL bShutdownOK;
  624.   switch(umsg)
  625.   {
  626.     // Quickly handle painting and sizing messages, found in ScintillaWin.cxx
  627.     // Cool idea, don't know if this has any effect... ;-)
  628.     case WM_MOVE:
  629.     case WM_MOUSEACTIVATE:
  630.     case WM_NCHITTEST:
  631.     case WM_NCCALCSIZE:
  632.     case WM_NCPAINT:
  633.     case WM_PAINT:
  634.     case WM_ERASEBKGND:
  635.     case WM_NCMOUSEMOVE:
  636.     case WM_NCLBUTTONDOWN:
  637.     case WM_WINDOWPOSCHANGING:
  638.     case WM_WINDOWPOSCHANGED:
  639.       return DefWindowProc(hwnd,umsg,wParam,lParam);
  640.     case WM_CREATE:
  641.       return MsgCreate(hwnd,wParam,lParam);
  642.     case WM_DESTROY:
  643.     case WM_ENDSESSION:
  644.       if (!bShutdownOK) {
  645.         WINDOWPLACEMENT wndpl;
  646.         // Terminate file watching
  647.         InstallFileWatching(NULL);
  648.         // GetWindowPlacement
  649.         wndpl.length = sizeof(WINDOWPLACEMENT);
  650.         GetWindowPlacement(hwnd,&wndpl);
  651.         wi.x = wndpl.rcNormalPosition.left;
  652.         wi.y = wndpl.rcNormalPosition.top;
  653.         wi.cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
  654.         wi.cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
  655.         wi.max = (IsZoomed(hwnd) || (wndpl.flags & WPF_RESTORETOMAXIMIZED));
  656.         DragAcceptFiles(hwnd,FALSE);
  657.         // Terminate clipboard watching
  658.         if (flagPasteBoard) {
  659.           KillTimer(hwnd,ID_PASTEBOARDTIMER);
  660.           ChangeClipboardChain(hwnd,hwndNextCBChain);
  661.         }
  662.         // Destroy find / replace dialog
  663.         if (IsWindow(hDlgFindReplace))
  664.           DestroyWindow(hDlgFindReplace);
  665.         // call SaveSettings() when hwndToolbar is still valid
  666.         SaveSettings(FALSE);
  667.         if (lstrlen(szIniFile) != 0) {
  668.           // Cleanup unwanted MRU's
  669.           if (!bSaveRecentFiles) {
  670.             MRU_Empty(pFileMRU);
  671.             MRU_Save(pFileMRU);
  672.           }
  673.           else
  674.             MRU_MergeSave(pFileMRU,TRUE,flagRelativeFileMRU,flagPortableMyDocs);
  675.           MRU_Destroy(pFileMRU);
  676.           if (!bSaveFindReplace) {
  677.             MRU_Empty(mruFind);
  678.             MRU_Empty(mruReplace);
  679.             MRU_Save(mruFind);
  680.             MRU_Save(mruReplace);
  681.           }
  682.           else {
  683.             MRU_MergeSave(mruFind,FALSE,FALSE,FALSE);
  684.             MRU_MergeSave(mruReplace,FALSE,FALSE,FALSE);
  685.           }
  686.           MRU_Destroy(mruFind);
  687.           MRU_Destroy(mruReplace);
  688.         }
  689.         // Remove tray icon if necessary
  690.         ShowNotifyIcon(hwnd,FALSE);
  691.         bShutdownOK = TRUE;
  692.       }
  693.       if (umsg == WM_DESTROY)
  694.         PostQuitMessage(0);
  695.       break;
  696.     case WM_CLOSE:
  697.       if (FileSave(FALSE,TRUE,FALSE,FALSE))
  698.         DestroyWindow(hwnd);
  699.       break;
  700.     case WM_QUERYENDSESSION:
  701.       if (FileSave(FALSE,TRUE,FALSE,FALSE))
  702.         return TRUE;
  703.       else
  704.         return FALSE;
  705.     // Reinitialize theme-dependent values and resize windows
  706.     case WM_THEMECHANGED:
  707.       MsgThemeChanged(hwnd,wParam,lParam);
  708.       break;
  709.     // update Scintilla colors
  710.     case WM_SYSCOLORCHANGE:
  711.       {
  712.         extern PEDITLEXER pLexCurrent;
  713.         Style_SetLexer(hwndEdit,pLexCurrent);
  714.         return DefWindowProc(hwnd,umsg,wParam,lParam);
  715.       }
  716.     //case WM_TIMER:
  717.     //  break;
  718.     case WM_SIZE:
  719.       MsgSize(hwnd,wParam,lParam);
  720.       break;
  721.     case WM_SETFOCUS:
  722.       SetFocus(hwndEdit);
  723.       UpdateToolbar();
  724.       UpdateStatusbar();
  725.       //if (bPendingChangeNotify)
  726.       //  PostMessage(hwnd,WM_CHANGENOTIFY,0,0);
  727.       break;
  728.     case WM_DROPFILES:
  729.       {
  730.         WCHAR szBuf[MAX_PATH+40];
  731.         HDROP hDrop = (HDROP)wParam;
  732.         // Reset Change Notify
  733.         //bPendingChangeNotify = FALSE;
  734.         if (IsIconic(hwnd))
  735.           ShowWindow(hwnd,SW_RESTORE);
  736.         //SetForegroundWindow(hwnd);
  737.         DragQueryFile(hDrop,0,szBuf,COUNTOF(szBuf));
  738.         if (PathIsDirectory(szBuf)) {
  739.           WCHAR tchFile[MAX_PATH];
  740.           if (OpenFileDlg(hwndMain,tchFile,COUNTOF(tchFile),szBuf))
  741.             FileLoad(FALSE,FALSE,FALSE,FALSE,tchFile);
  742.         }
  743.         else
  744.           FileLoad(FALSE,FALSE,FALSE,FALSE,szBuf);
  745.         if (DragQueryFile(hDrop,(UINT)(-1),NULL,0) > 1)
  746.           MsgBox(MBINFO,IDS_ERR_DROP);
  747.         DragFinish(hDrop);
  748.       }
  749.       break;
  750.     case WM_COPYDATA:
  751.       {
  752.         PCOPYDATASTRUCT pcds = (PCOPYDATASTRUCT)lParam;
  753.         // Reset Change Notify
  754.         //bPendingChangeNotify = FALSE;
  755.         SetDlgItemInt(hwnd,IDC_REUSELOCK,GetTickCount(),FALSE);
  756.         if (pcds->dwData == DATA_NOTEPAD2_PARAMS) {
  757.           LPNP2PARAMS params = LocalAlloc(LPTR,pcds->cbData);
  758.           CopyMemory(params,pcds->lpData,pcds->cbData);
  759.           if (params->flagLexerSpecified)
  760.             flagLexerSpecified = 1;
  761.           if (params->flagQuietCreate)
  762.             flagQuietCreate = 1;
  763.           if (params->flagFileSpecified) {
  764.             BOOL  bOpened = FALSE;
  765.             iSrcEncoding = params->iSrcEncoding;
  766.             if (PathIsDirectory(&params->wchData)) {
  767.               WCHAR tchFile[MAX_PATH];
  768.               if (OpenFileDlg(hwndMain,tchFile,COUNTOF(tchFile),&params->wchData))
  769.                 bOpened = FileLoad(FALSE,FALSE,FALSE,FALSE,tchFile);
  770.             }
  771.             else
  772.               bOpened = FileLoad(FALSE,FALSE,FALSE,FALSE,&params->wchData);
  773.             if (bOpened) {
  774.               if (0 != params->flagSetEncoding) {
  775.                 flagSetEncoding = params->flagSetEncoding;
  776.                 SendMessage(
  777.                   hwnd,
  778.                   WM_COMMAND,
  779.                   MAKELONG(IDM_ENCODING_ANSI + flagSetEncoding -1,1),
  780.                   0);
  781.                 flagSetEncoding = 0;
  782.               }
  783.               if (0 != params->flagSetEOLMode) {
  784.                 flagSetEOLMode = params->flagSetEOLMode;
  785.                 SendMessage(
  786.                   hwndMain,
  787.                   WM_COMMAND,
  788.                   MAKELONG(IDM_LINEENDINGS_CRLF + flagSetEOLMode -1,1),
  789.                   0);
  790.                 flagSetEOLMode = 0;
  791.               }
  792.               if (params->flagLexerSpecified) {
  793.                 if (params->iInitialLexer < 0) {
  794.                   WCHAR wchExt[32] = L".";
  795.                   lstrcpyn(CharNext(wchExt),StrEnd(&params->wchData)+1,30);
  796.                   Style_SetLexerFromName(hwndEdit,&params->wchData,wchExt);
  797.                 }
  798.                 else if (params->iInitialLexer >=0 && params->iInitialLexer < NUMLEXERS)
  799.                   Style_SetLexerFromID(hwndEdit,params->iInitialLexer);
  800.               }
  801.               if (params->flagTitleExcerpt) {
  802.                 lstrcpyn(szTitleExcerpt,StrEnd(&params->wchData)+1,COUNTOF(szTitleExcerpt));
  803.                 SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  804.                   iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  805.                   IDS_READONLY,bReadOnly,szTitleExcerpt);
  806.               }
  807.             }
  808.             // reset
  809.             iSrcEncoding = -1;
  810.           }
  811.           if (params->flagJumpTo) {
  812.             if (params->iInitialLine == 0)
  813.               params->iInitialLine = 1;
  814.             EditJumpTo(hwndEdit,params->iInitialLine,params->iInitialColumn);
  815.           }
  816.           flagLexerSpecified = 0;
  817.           flagQuietCreate = 0;
  818.           LocalFree(params);
  819.           UpdateStatusbar();
  820.         }
  821.       }
  822.       return TRUE;
  823.     case WM_CONTEXTMENU:
  824.     {
  825.       HMENU hmenu;
  826.       int imenu = 0;
  827.       POINT pt;
  828.       int nID = GetDlgCtrlID((HWND)wParam);
  829.       if ((nID != IDC_EDIT) && (nID != IDC_STATUSBAR) &&
  830.           (nID != IDC_REBAR) && (nID != IDC_TOOLBAR))
  831.         return DefWindowProc(hwnd,umsg,wParam,lParam);
  832.       hmenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(IDR_POPUPMENU));
  833.       //SetMenuDefaultItem(GetSubMenu(hmenu,1),0,FALSE);
  834.       pt.x = (int)(short)LOWORD(lParam);
  835.       pt.y = (int)(short)HIWORD(lParam);
  836.       switch(nID)
  837.       {
  838.         case IDC_EDIT:
  839.           {
  840.             int iSelStart = SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  841.             int iSelEnd   = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0);
  842.             if (iSelStart == iSelEnd && pt.x != -1 && pt.y != -1)
  843.             {
  844.               int iNewPos;
  845.               POINT ptc = { pt.x, pt.y };
  846.               ScreenToClient(hwndEdit,&ptc);
  847.               iNewPos = SendMessage(hwndEdit,SCI_POSITIONFROMPOINT,(WPARAM)ptc.x,(LPARAM)ptc.y);
  848.               SendMessage(hwndEdit,SCI_GOTOPOS,(WPARAM)iNewPos,0);
  849.             }
  850.             if (pt.x == -1 && pt.y == -1)
  851.             {
  852.               int iCurrentPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  853.               pt.x = SendMessage(hwndEdit,SCI_POINTXFROMPOSITION,0,(LPARAM)iCurrentPos);
  854.               pt.y = SendMessage(hwndEdit,SCI_POINTYFROMPOSITION,0,(LPARAM)iCurrentPos);
  855.               ClientToScreen(hwndEdit,&pt);
  856.             }
  857.             imenu = 0;
  858.           }
  859.           break;
  860.         case IDC_TOOLBAR:
  861.         case IDC_STATUSBAR:
  862.         case IDC_REBAR:
  863.           if (pt.x == -1 && pt.y == -1)
  864.             GetCursorPos(&pt);
  865.           imenu = 1;
  866.           break;
  867.       }
  868.       TrackPopupMenuEx(GetSubMenu(hmenu,imenu),
  869.         TPM_LEFTBUTTON | TPM_RIGHTBUTTON,pt.x+1,pt.y+1,hwnd,NULL);
  870.       DestroyMenu(hmenu);
  871.     }
  872.     break;
  873.     case WM_INITMENU:
  874.       MsgInitMenu(hwnd,wParam,lParam);
  875.       break;
  876.     case WM_NOTIFY:
  877.       return MsgNotify(hwnd,wParam,lParam);
  878.     case WM_COMMAND:
  879.       return MsgCommand(hwnd,wParam,lParam);
  880.     case WM_SYSCOMMAND:
  881.       switch (wParam)
  882.       {
  883.         case SC_MINIMIZE:
  884.           ShowOwnedPopups(hwnd,FALSE);
  885.           if (bMinimizeToTray) {
  886.             MinimizeWndToTray(hwnd);
  887.             ShowNotifyIcon(hwnd,TRUE);
  888.             SetNotifyIconTitle(hwnd);
  889.             return(0);
  890.           }
  891.           else
  892.             return DefWindowProc(hwnd,umsg,wParam,lParam);
  893.         case SC_RESTORE: {
  894.           LRESULT lrv = DefWindowProc(hwnd,umsg,wParam,lParam);
  895.           ShowOwnedPopups(hwnd,TRUE);
  896.           return(lrv);
  897.         }
  898.       }
  899.       return DefWindowProc(hwnd,umsg,wParam,lParam);
  900.       case WM_CHANGENOTIFY:
  901.           if (iFileWatchingMode == 1 || bModified || iEncoding != iOriginalEncoding)
  902.             SetForegroundWindow(hwnd);
  903.           if (PathFileExists(szCurFile)) {
  904.             if ((iFileWatchingMode == 2 && !bModified && iEncoding == iOriginalEncoding) ||
  905.                  MsgBox(MBYESNO,IDS_FILECHANGENOTIFY) == IDYES) {
  906.               int iCurPos     = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  907.               int iAnchorPos  = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  908.               int iCurTopLine = SendMessage(hwndEdit,SCI_GETFIRSTVISIBLELINE,0,0);
  909.               int iXOffset    = SendMessage(hwndEdit,SCI_GETXOFFSET,0,0);
  910.               BOOL bIsTail    = (iCurPos == iAnchorPos) && (iCurPos == SendMessage(hwndEdit,SCI_GETLENGTH,0,0));
  911.               iWeakSrcEncoding = iEncoding;
  912.               if (FileLoad(TRUE,FALSE,TRUE,FALSE,szCurFile)) {
  913.                 if (bIsTail && iFileWatchingMode == 2)
  914.                   EditJumpTo(hwndEdit,-1,0);
  915.                 else if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) {
  916.                   char tch[5] = "";
  917.                   SendMessage(hwndEdit,SCI_GETTEXT,5,(LPARAM)tch);
  918.                   if (lstrcmpiA(tch,".LOG") != 0) {
  919.                     int iTopLineAfterLoad;
  920.                     SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
  921.                     iTopLineAfterLoad = SendMessage(hwndEdit,SCI_GETFIRSTVISIBLELINE,0,0);
  922.                     SendMessage(hwndEdit,SCI_LINESCROLL,0,(LPARAM)(iCurTopLine - iTopLineAfterLoad));
  923.                     SendMessage(hwndEdit,SCI_SETXOFFSET,(WPARAM)iXOffset,0);
  924.                   }
  925.                 }
  926.               }
  927.             }
  928.           }
  929.           else {
  930.             if (MsgBox(MBYESNO,IDS_FILECHANGENOTIFY2) == IDYES)
  931.               FileSave(TRUE,FALSE,FALSE,FALSE);
  932.           }
  933.           if (!bRunningWatch)
  934.             InstallFileWatching(szCurFile);
  935.         break;
  936.     //// This message is posted before Notepad2 reactivates itself
  937.     //case WM_CHANGENOTIFYCLEAR:
  938.     //  bPendingChangeNotify = FALSE;
  939.     //  break;
  940.     case WM_DRAWCLIPBOARD:
  941.       if (!bLastCopyFromMe)
  942.         dwLastCopyTime = GetTickCount();
  943.       else
  944.         bLastCopyFromMe = FALSE;
  945.       if (hwndNextCBChain)
  946.         SendMessage(hwndNextCBChain,WM_DRAWCLIPBOARD,wParam,lParam);
  947.       break;
  948.     case WM_CHANGECBCHAIN:
  949.       if ((HWND)wParam == hwndNextCBChain)
  950.         hwndNextCBChain = (HWND)lParam;
  951.       if (hwndNextCBChain)
  952.         SendMessage(hwndNextCBChain,WM_CHANGECBCHAIN,lParam,wParam);
  953.       break;
  954.     case WM_TRAYMESSAGE:
  955.       switch(lParam)
  956.       {
  957.         case WM_RBUTTONUP: {
  958.           HMENU hMenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(IDR_POPUPMENU));
  959.           HMENU hMenuPopup = GetSubMenu(hMenu,2);
  960.           POINT pt;
  961.           int iCmd;
  962.           SetForegroundWindow(hwnd);
  963.           GetCursorPos(&pt);
  964.           SetMenuDefaultItem(hMenuPopup,IDM_TRAY_RESTORE,FALSE);
  965.           iCmd = TrackPopupMenu(hMenuPopup,
  966.                    TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
  967.                    pt.x,pt.y,0,hwnd,NULL);
  968.           PostMessage(hwnd,WM_NULL,0,0);
  969.           DestroyMenu(hMenu);
  970.           if (iCmd == IDM_TRAY_RESTORE) {
  971.             ShowNotifyIcon(hwnd,FALSE);
  972.             RestoreWndFromTray(hwnd);
  973.             ShowOwnedPopups(hwnd,TRUE);
  974.           }
  975.           else if (iCmd == IDM_TRAY_EXIT) {
  976.               //ShowNotifyIcon(hwnd,FALSE);
  977.               SendMessage(hwnd,WM_CLOSE,0,0);
  978.             }
  979.           }
  980.           return TRUE;
  981.         case WM_LBUTTONUP:
  982.           ShowNotifyIcon(hwnd,FALSE);
  983.           RestoreWndFromTray(hwnd);
  984.           ShowOwnedPopups(hwnd,TRUE);
  985.           return TRUE;
  986.       }
  987.       break;
  988.     default:
  989.       if (umsg == msgTaskbarCreated) {
  990.         if (!IsWindowVisible(hwnd))
  991.           ShowNotifyIcon(hwnd,TRUE);
  992.           SetNotifyIconTitle(hwnd);
  993.         return(0);
  994.       }
  995.       return DefWindowProc(hwnd,umsg,wParam,lParam);
  996.   }
  997.   return(0);
  998. }
  999. //=============================================================================
  1000. //
  1001. //  MsgCreate() - Handles WM_CREATE
  1002. //
  1003. //
  1004. LRESULT MsgCreate(HWND hwnd,WPARAM wParam,LPARAM lParam)
  1005. {
  1006.   HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
  1007.   // Setup edit control
  1008.   hwndEdit = EditCreate(hwnd);
  1009.   // Tabs
  1010.   SendMessage(hwndEdit,SCI_SETUSETABS,!bTabsAsSpaces,0);
  1011.   SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
  1012.   SendMessage(hwndEdit,SCI_SETTABWIDTH,iTabWidth,0);
  1013.   SendMessage(hwndEdit,SCI_SETINDENT,iIndentWidth,0);
  1014.   // Indent Guides
  1015.   Style_SetIndentGuides(hwndEdit,bShowIndentGuides);
  1016.   // Word wrap
  1017.   if (!fWordWrap)
  1018.     SendMessage(hwndEdit,SCI_SETWRAPMODE,SC_WRAP_NONE,0);
  1019.   else
  1020.     SendMessage(hwndEdit,SCI_SETWRAPMODE,(iWordWrapMode == 0) ? SC_WRAP_WORD : SC_WRAP_CHAR,0);
  1021.   if (iWordWrapIndent == 5)
  1022.     SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_SAME,0);
  1023.   else if (iWordWrapIndent == 6)
  1024.     SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_INDENT,0);
  1025.   else {
  1026.     int i = 0;
  1027.     switch (iWordWrapIndent) {
  1028.       case 1: i = 1; break;
  1029.       case 2: i = 2; break;
  1030.       case 3: i = (iIndentWidth) ? 1 * iIndentWidth : 1 * iTabWidth; break;
  1031.       case 4: i = (iIndentWidth) ? 2 * iIndentWidth : 2 * iTabWidth; break;
  1032.     }
  1033.     SendMessage(hwndEdit,SCI_SETWRAPSTARTINDENT,i,0);
  1034.     SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_FIXED,0);
  1035.   }
  1036.   if (bShowWordWrapSymbols) {
  1037.     int wrapVisualFlags = 0;
  1038.     int wrapVisualFlagsLocation = 0;
  1039.     if (iWordWrapSymbols == 0)
  1040.       iWordWrapSymbols = 22;
  1041.     switch (iWordWrapSymbols%10) {
  1042.       case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_END_BY_TEXT; break;
  1043.       case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; break;
  1044.     }
  1045.     switch (((iWordWrapSymbols%100)-(iWordWrapSymbols%10))/10) {
  1046.       case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_START_BY_TEXT; break;
  1047.       case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; break;
  1048.     }
  1049.     SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGSLOCATION,wrapVisualFlagsLocation,0);
  1050.     SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,wrapVisualFlags,0);
  1051.   }
  1052.   else {
  1053.     SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,0,0);
  1054.   }
  1055.   // Long Lines
  1056.   if (bMarkLongLines)
  1057.     SendMessage(hwndEdit,SCI_SETEDGEMODE,(iLongLineMode == EDGE_LINE)?EDGE_LINE:EDGE_BACKGROUND,0);
  1058.   else
  1059.     SendMessage(hwndEdit,SCI_SETEDGEMODE,EDGE_NONE,0);
  1060.   SendMessage(hwndEdit,SCI_SETEDGECOLUMN,iLongLinesLimit,0);
  1061.   // Margins
  1062.   SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,2,0);
  1063.   SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,1,(bShowSelectionMargin)?16:0);
  1064.   UpdateLineNumberWidth();
  1065.   //SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,
  1066.   //  (bShowLineNumbers)?SendMessage(hwndEdit,SCI_TEXTWIDTH,STYLE_LINENUMBER,(LPARAM)L"_999999_"):0);
  1067.   // Nonprinting characters
  1068.   SendMessage(hwndEdit,SCI_SETVIEWWS,(bViewWhiteSpace)?SCWS_VISIBLEALWAYS:SCWS_INVISIBLE,0);
  1069.   SendMessage(hwndEdit,SCI_SETVIEWEOL,bViewEOLs,0);
  1070.   hwndEditFrame = CreateWindowEx(
  1071.                     WS_EX_CLIENTEDGE,
  1072.                     WC_LISTVIEW,
  1073.                     NULL,
  1074.                     WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
  1075.                     0,0,100,100,
  1076.                     hwnd,
  1077.                     (HMENU)IDC_EDITFRAME,
  1078.                     hInstance,
  1079.                     NULL);
  1080.   if (PrivateIsAppThemed()) {
  1081.     RECT rc, rc2;
  1082.     bIsAppThemed = TRUE;
  1083.     SetWindowLongPtr(hwndEdit,GWL_EXSTYLE,GetWindowLongPtr(hwndEdit,GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
  1084.     SetWindowPos(hwndEdit,NULL,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED);
  1085.     if (IsVista()) {
  1086.       cxEditFrame = 0;
  1087.       cyEditFrame = 0;
  1088.     }
  1089.     else {
  1090.       GetClientRect(hwndEditFrame,&rc);
  1091.       GetWindowRect(hwndEditFrame,&rc2);
  1092.       cxEditFrame = ((rc2.right-rc2.left) - (rc.right-rc.left)) / 2;
  1093.       cyEditFrame = ((rc2.bottom-rc2.top) - (rc.bottom-rc.top)) / 2;
  1094.     }
  1095.   }
  1096.   else {
  1097.     bIsAppThemed = FALSE;
  1098.     cxEditFrame = 0;
  1099.     cyEditFrame = 0;
  1100.   }
  1101.   // Create Toolbar and Statusbar
  1102.   CreateBars(hwnd,hInstance);
  1103.   // Window Initialization
  1104.   CreateWindow(
  1105.     WC_STATIC,
  1106.     NULL,
  1107.     WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
  1108.     0,0,10,10,
  1109.     hwnd,
  1110.     (HMENU)IDC_FILENAME,
  1111.     hInstance,
  1112.     NULL);
  1113.   SetDlgItemText(hwnd,IDC_FILENAME,szCurFile);
  1114.   CreateWindow(
  1115.     WC_STATIC,
  1116.     NULL,
  1117.     WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
  1118.     10,10,10,10,
  1119.     hwnd,
  1120.     (HMENU)IDC_REUSELOCK,
  1121.     hInstance,
  1122.     NULL);
  1123.   SetDlgItemInt(hwnd,IDC_REUSELOCK,GetTickCount(),FALSE);
  1124.   // Menu
  1125.   //SetMenuDefaultItem(GetSubMenu(GetMenu(hwnd),0),0);
  1126.   // Drag & Drop
  1127.   DragAcceptFiles(hwnd,TRUE);
  1128.   // File MRU
  1129.   pFileMRU = MRU_Create(L"Recent Files",MRU_NOCASE,32);
  1130.   MRU_Load(pFileMRU);
  1131.   mruFind = MRU_Create(L"Recent Find",(/*IsWindowsNT()*/1) ? MRU_UTF8 : 0,16);
  1132.   MRU_Load(mruFind);
  1133.   mruReplace = MRU_Create(L"Recent Replace",(/*IsWindowsNT()*/1) ? MRU_UTF8 : 0,16);
  1134.   MRU_Load(mruReplace);
  1135.   if (hwndEdit == NULL || hwndEditFrame == NULL ||
  1136.       hwndStatus == NULL || hwndToolbar == NULL || hwndReBar == NULL)
  1137.     return(-1);
  1138.   return(0);
  1139. }
  1140. //=============================================================================
  1141. //
  1142. //  CreateBars() - Create Toolbar and Statusbar
  1143. //
  1144. //
  1145. void CreateBars(HWND hwnd,HINSTANCE hInstance)
  1146. {
  1147.   RECT rc;
  1148.   REBARINFO rbi;
  1149.   REBARBANDINFO rbBand;
  1150.   BITMAP bmp;
  1151.   HBITMAP hbmp;
  1152.   HIMAGELIST himl;
  1153.   WCHAR szTmp[MAX_PATH];
  1154.   BOOL bExternalBitmap = TRUE;
  1155.   DWORD dwToolbarStyle = WS_TOOLBAR;
  1156.   DWORD dwStatusbarStyle = WS_CHILD | WS_CLIPSIBLINGS;
  1157.   DWORD dwReBarStyle = WS_REBAR;
  1158.   BOOL bIsAppThemed = PrivateIsAppThemed();
  1159.   int i,n;
  1160.   WCHAR tchDesc[256];
  1161.   WCHAR tchIndex[256];
  1162.   WCHAR *pIniSection = NULL;
  1163.   int   cchIniSection = 0;
  1164.   if (bShowToolbar)
  1165.     dwReBarStyle |= WS_VISIBLE;
  1166.   hwndToolbar = CreateWindowEx(0,TOOLBARCLASSNAME,NULL,dwToolbarStyle,
  1167.                                0,0,0,0,hwnd,(HMENU)IDC_TOOLBAR,hInstance,NULL);
  1168.   SendMessage(hwndToolbar,TB_BUTTONSTRUCTSIZE,(WPARAM)sizeof(TBBUTTON),0);
  1169.   // Add normal Toolbar Bitmap
  1170.   hbmp = NULL;
  1171.   if (lstrlen(tchToolbarBitmap))
  1172.   {
  1173.     if (!SearchPath(NULL,tchToolbarBitmap,NULL,COUNTOF(szTmp),szTmp,NULL))
  1174.       lstrcpy(szTmp,tchToolbarBitmap);
  1175.     hbmp = LoadImage(NULL,szTmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
  1176.   }
  1177.   if (!hbmp) {
  1178.     hbmp = LoadImage(hInstance,MAKEINTRESOURCE(IDR_MAINWND),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
  1179.     bExternalBitmap = FALSE;
  1180.   }
  1181.   GetObject(hbmp,sizeof(BITMAP),&bmp);
  1182.   himl = ImageList_Create(bmp.bmWidth/NUMTOOLBITMAPS,bmp.bmHeight,ILC_COLOR32|ILC_MASK,0,0);
  1183.   ImageList_AddMasked(himl,hbmp,CLR_DEFAULT);
  1184.   DeleteObject(hbmp);
  1185.   SendMessage(hwndToolbar,TB_SETIMAGELIST,0,(LPARAM)himl);
  1186.   // Optionally add hot Toolbar Bitmap
  1187.   hbmp = NULL;
  1188.   if (lstrlen(tchToolbarBitmapHot))
  1189.   {
  1190.     if (!SearchPath(NULL,tchToolbarBitmapHot,NULL,COUNTOF(szTmp),szTmp,NULL))
  1191.       lstrcpy(szTmp,tchToolbarBitmapHot);
  1192.     if (hbmp = LoadImage(NULL,szTmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE))
  1193.     {
  1194.       GetObject(hbmp,sizeof(BITMAP),&bmp);
  1195.       himl = ImageList_Create(bmp.bmWidth/NUMTOOLBITMAPS,bmp.bmHeight,ILC_COLOR32|ILC_MASK,0,0);
  1196.       ImageList_AddMasked(himl,hbmp,CLR_DEFAULT);
  1197.       DeleteObject(hbmp);
  1198.       SendMessage(hwndToolbar,TB_SETHOTIMAGELIST,0,(LPARAM)himl);
  1199.     }
  1200.   }
  1201.   // Optionally add disabled Toolbar Bitmap
  1202.   hbmp = NULL;
  1203.   if (lstrlen(tchToolbarBitmapDisabled))
  1204.   {
  1205.     if (!SearchPath(NULL,tchToolbarBitmapDisabled,NULL,COUNTOF(szTmp),szTmp,NULL))
  1206.       lstrcpy(szTmp,tchToolbarBitmapDisabled);
  1207.     if (hbmp = LoadImage(NULL,szTmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE))
  1208.     {
  1209.       GetObject(hbmp,sizeof(BITMAP),&bmp);
  1210.       himl = ImageList_Create(bmp.bmWidth/NUMTOOLBITMAPS,bmp.bmHeight,ILC_COLOR32|ILC_MASK,0,0);
  1211.       ImageList_AddMasked(himl,hbmp,CLR_DEFAULT);
  1212.       DeleteObject(hbmp);
  1213.       SendMessage(hwndToolbar,TB_SETDISABLEDIMAGELIST,0,(LPARAM)himl);
  1214.     }
  1215.   }
  1216.   // Load toolbar labels
  1217.   pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024);
  1218.   cchIniSection = LocalSize(pIniSection)/sizeof(WCHAR);
  1219.   LoadIniSection(L"Toolbar Labels",pIniSection,cchIniSection);
  1220.   n = 1;
  1221.   for (i = 0; i < COUNTOF(tbbMainWnd); i++) {
  1222.     if (tbbMainWnd[i].fsStyle == TBSTYLE_SEP)
  1223.       continue;
  1224.     wsprintf(tchIndex,L"%02i",n++);
  1225.     if (IniSectionGetString(pIniSection,tchIndex,L"",tchDesc,COUNTOF(tchDesc))) {
  1226.       tbbMainWnd[i].iString = SendMessage(hwndToolbar,TB_ADDSTRING,0,(LPARAM)tchDesc);
  1227.       tbbMainWnd[i].fsStyle |= BTNS_AUTOSIZE | BTNS_SHOWTEXT;
  1228.     }
  1229.     else
  1230.       tbbMainWnd[i].fsStyle &= ~(BTNS_AUTOSIZE | BTNS_SHOWTEXT);
  1231.   }
  1232.   LocalFree(pIniSection);
  1233.   SendMessage(hwndToolbar,TB_SETEXTENDEDSTYLE,0,
  1234.     SendMessage(hwndToolbar,TB_GETEXTENDEDSTYLE,0,0) | TBSTYLE_EX_MIXEDBUTTONS);
  1235.   SendMessage(hwndToolbar,TB_ADDBUTTONS,NUMINITIALTOOLS,(LPARAM)tbbMainWnd);
  1236.   if (Toolbar_SetButtons(hwndToolbar,IDT_FILE_NEW,tchToolbarButtons,tbbMainWnd,COUNTOF(tbbMainWnd)) == 0)
  1237.     SendMessage(hwndToolbar,TB_ADDBUTTONS,NUMINITIALTOOLS,(LPARAM)tbbMainWnd);
  1238.   SendMessage(hwndToolbar,TB_GETITEMRECT,0,(LPARAM)&rc);
  1239.   //SendMessage(hwndToolbar,TB_SETINDENT,2,0);
  1240.   if (bShowStatusbar)
  1241.     dwStatusbarStyle |= WS_VISIBLE;
  1242.   hwndStatus = CreateStatusWindow(dwStatusbarStyle,NULL,hwnd,IDC_STATUSBAR);
  1243.   // Create ReBar and add Toolbar
  1244.   hwndReBar = CreateWindowEx(WS_EX_TOOLWINDOW,REBARCLASSNAME,NULL,dwReBarStyle,
  1245.                              0,0,0,0,hwnd,(HMENU)IDC_REBAR,hInstance,NULL);
  1246.   rbi.cbSize = sizeof(REBARINFO);
  1247.   rbi.fMask  = 0;
  1248.   rbi.himl   = (HIMAGELIST)NULL;
  1249.   SendMessage(hwndReBar,RB_SETBARINFO,0,(LPARAM)&rbi);
  1250.   rbBand.cbSize  = sizeof(REBARBANDINFO);
  1251.   rbBand.fMask   = /*RBBIM_COLORS | RBBIM_TEXT | RBBIM_BACKGROUND | */
  1252.                    RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE /*| RBBIM_SIZE*/;
  1253.   rbBand.fStyle  = /*RBBS_CHILDEDGE |*//* RBBS_BREAK |*/ RBBS_FIXEDSIZE /*| RBBS_GRIPPERALWAYS*/;
  1254.   if (bIsAppThemed)
  1255.     rbBand.fStyle |= RBBS_CHILDEDGE;
  1256.   rbBand.hbmBack = NULL;
  1257.   rbBand.lpText     = L"Toolbar";
  1258.   rbBand.hwndChild  = hwndToolbar;
  1259.   rbBand.cxMinChild = (rc.right - rc.left) * COUNTOF(tbbMainWnd);
  1260.   rbBand.cyMinChild = (rc.bottom - rc.top) + 2 * rc.top;
  1261.   rbBand.cx         = 0;
  1262.   SendMessage(hwndReBar,RB_INSERTBAND,(WPARAM)-1,(LPARAM)&rbBand);
  1263.   SetWindowPos(hwndReBar,NULL,0,0,0,0,SWP_NOZORDER);
  1264.   GetWindowRect(hwndReBar,&rc);
  1265.   cyReBar = rc.bottom - rc.top;
  1266.   cyReBarFrame = bIsAppThemed ? 0 : 2;
  1267. }
  1268. //=============================================================================
  1269. //
  1270. //  MsgThemeChanged() - Handle WM_THEMECHANGED
  1271. //
  1272. //
  1273. void MsgThemeChanged(HWND hwnd,WPARAM wParam,LPARAM lParam)
  1274. {
  1275.   RECT rc, rc2;
  1276.   HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE);
  1277.   // reinitialize edit frame
  1278.   if (PrivateIsAppThemed()) {
  1279.     bIsAppThemed = TRUE;
  1280.     SetWindowLongPtr(hwndEdit,GWL_EXSTYLE,GetWindowLongPtr(hwndEdit,GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
  1281.     SetWindowPos(hwndEdit,NULL,0,0,0,0,SWP_NOZORDER|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE);
  1282.     if (IsVista()) {
  1283.       cxEditFrame = 0;
  1284.       cyEditFrame = 0;
  1285.     }
  1286.     else {
  1287.       SetWindowPos(hwndEditFrame,NULL,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED);
  1288.       GetClientRect(hwndEditFrame,&rc);
  1289.       GetWindowRect(hwndEditFrame,&rc2);
  1290.       cxEditFrame = ((rc2.right-rc2.left) - (rc.right-rc.left)) / 2;
  1291.       cyEditFrame = ((rc2.bottom-rc2.top) - (rc.bottom-rc.top)) / 2;
  1292.     }
  1293.   }
  1294.   else {
  1295.     bIsAppThemed = FALSE;
  1296.     SetWindowLongPtr(hwndEdit,GWL_EXSTYLE,WS_EX_CLIENTEDGE|GetWindowLongPtr(hwndEdit,GWL_EXSTYLE));
  1297.     SetWindowPos(hwndEdit,NULL,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED);
  1298.     cxEditFrame = 0;
  1299.     cyEditFrame = 0;
  1300.   }
  1301.   // recreate toolbar and statusbar
  1302.   Toolbar_GetButtons(hwndToolbar,IDT_FILE_NEW,tchToolbarButtons,COUNTOF(tchToolbarButtons));
  1303.   DestroyWindow(hwndToolbar);
  1304.   DestroyWindow(hwndReBar);
  1305.   DestroyWindow(hwndStatus);
  1306.   CreateBars(hwnd,hInstance);
  1307.   UpdateToolbar();
  1308.   GetClientRect(hwnd,&rc);
  1309.   SendMessage(hwnd,WM_SIZE,SIZE_RESTORED,MAKELONG(rc.right,rc.bottom));
  1310.   UpdateStatusbar();
  1311. }
  1312. //=============================================================================
  1313. //
  1314. //  MsgSize() - Handles WM_SIZE
  1315. //
  1316. //
  1317. void MsgSize(HWND hwnd,WPARAM wParam,LPARAM lParam)
  1318. {
  1319.   RECT rc;
  1320.   int x,y,cx,cy;
  1321.   HDWP hdwp;
  1322.   // Statusbar
  1323.   int aWidth[6];
  1324.   if (wParam == SIZE_MINIMIZED)
  1325.     return;
  1326.   x = 0;
  1327.   y = 0;
  1328.   cx = LOWORD(lParam);
  1329.   cy = HIWORD(lParam);
  1330.   if (bShowToolbar)
  1331.   {
  1332. /*  SendMessage(hwndToolbar,WM_SIZE,0,0);
  1333.     GetWindowRect(hwndToolbar,&rc);
  1334.     y = (rc.bottom - rc.top);
  1335.     cy -= (rc.bottom - rc.top);*/
  1336.     //SendMessage(hwndToolbar,TB_GETITEMRECT,0,(LPARAM)&rc);
  1337.     SetWindowPos(hwndReBar,NULL,0,0,LOWORD(lParam),cyReBar,SWP_NOZORDER);
  1338.     // the ReBar automatically sets the correct height
  1339.     // calling SetWindowPos() with the height of one toolbar button
  1340.     // causes the control not to temporarily use the whole client area
  1341.     // and prevents flickering
  1342.     //GetWindowRect(hwndReBar,&rc);
  1343.     y = cyReBar + cyReBarFrame;    // define
  1344.     cy -= cyReBar + cyReBarFrame;  // border
  1345.   }
  1346.   if (bShowStatusbar)
  1347.   {
  1348.     SendMessage(hwndStatus,WM_SIZE,0,0);
  1349.     GetWindowRect(hwndStatus,&rc);
  1350.     cy -= (rc.bottom - rc.top);
  1351.   }
  1352.   hdwp = BeginDeferWindowPos(2);
  1353.   DeferWindowPos(hdwp,hwndEditFrame,NULL,x,y,cx,cy,
  1354.                  SWP_NOZORDER | SWP_NOACTIVATE);
  1355.   DeferWindowPos(hdwp,hwndEdit,NULL,x+cxEditFrame,y+cyEditFrame,
  1356.                  cx-2*cxEditFrame,cy-2*cyEditFrame,
  1357.                  SWP_NOZORDER | SWP_NOACTIVATE);
  1358.   EndDeferWindowPos(hdwp);
  1359.   // Statusbar width
  1360.   aWidth[0] = max(120,min(cx/3,StatusCalcPaneWidth(hwndStatus,L"Ln 9'999'999 : 9'999'999   Col 9'999'999 : 999   Sel 9'999'999")));
  1361.   aWidth[1] = aWidth[0] + StatusCalcPaneWidth(hwndStatus,L"9'999'999 Bytes");
  1362.   aWidth[2] = aWidth[1] + StatusCalcPaneWidth(hwndStatus,L"Unicode BE BOM");
  1363.   aWidth[3] = aWidth[2] + StatusCalcPaneWidth(hwndStatus,L"CR+LF");
  1364.   aWidth[4] = aWidth[3] + StatusCalcPaneWidth(hwndStatus,L"OVR");
  1365.   aWidth[5] = -1;
  1366.   SendMessage(hwndStatus,SB_SETPARTS,COUNTOF(aWidth),(LPARAM)aWidth);
  1367.   //UpdateStatusbar();
  1368. }
  1369. //=============================================================================
  1370. //
  1371. //  MsgInitMenu() - Handles WM_INITMENU
  1372. //
  1373. //
  1374. void MsgInitMenu(HWND hwnd,WPARAM wParam,LPARAM lParam)
  1375. {
  1376.   int i,i2;
  1377.   HMENU hmenu = (HMENU)wParam;
  1378.   i = lstrlen(szCurFile);
  1379.   EnableCmd(hmenu,IDM_FILE_REVERT,i);
  1380.   EnableCmd(hmenu,IDM_FILE_LAUNCH,i);
  1381.   EnableCmd(hmenu,IDM_FILE_PROPERTIES,i);
  1382.   EnableCmd(hmenu,IDM_FILE_CREATELINK,i);
  1383.   EnableCmd(hmenu,IDM_FILE_ADDTOFAV,i);
  1384.   EnableCmd(hmenu,IDM_FILE_READONLY,i);
  1385.   CheckCmd(hmenu,IDM_FILE_READONLY,bReadOnly);
  1386.   //EnableCmd(hmenu,IDM_ENCODING_UNICODEREV,!bReadOnly);
  1387.   //EnableCmd(hmenu,IDM_ENCODING_UNICODE,!bReadOnly);
  1388.   //EnableCmd(hmenu,IDM_ENCODING_UTF8SIGN,!bReadOnly);
  1389.   //EnableCmd(hmenu,IDM_ENCODING_UTF8,!bReadOnly);
  1390.   //EnableCmd(hmenu,IDM_ENCODING_ANSI,!bReadOnly);
  1391.   //EnableCmd(hmenu,IDM_LINEENDINGS_CRLF,!bReadOnly);
  1392.   //EnableCmd(hmenu,IDM_LINEENDINGS_LF,!bReadOnly);
  1393.   //EnableCmd(hmenu,IDM_LINEENDINGS_CR,!bReadOnly);
  1394.   EnableCmd(hmenu,IDM_ENCODING_RECODE,i);
  1395.   if (mEncoding[iEncoding].uFlags & NCP_UNICODE_REVERSE)
  1396.     i = IDM_ENCODING_UNICODEREV;
  1397.   else if (mEncoding[iEncoding].uFlags & NCP_UNICODE)
  1398.     i = IDM_ENCODING_UNICODE;
  1399.   else if (mEncoding[iEncoding].uFlags & NCP_UTF8_SIGN)
  1400.     i = IDM_ENCODING_UTF8SIGN;
  1401.   else if (mEncoding[iEncoding].uFlags & NCP_UTF8)
  1402.     i = IDM_ENCODING_UTF8;
  1403.   else if (mEncoding[iEncoding].uFlags & NCP_DEFAULT)
  1404.     i = IDM_ENCODING_ANSI;
  1405.   else
  1406.     i = -1;
  1407.   CheckMenuRadioItem(hmenu,IDM_ENCODING_ANSI,IDM_ENCODING_UTF8SIGN,i,MF_BYCOMMAND);
  1408.   if (iEOLMode == SC_EOL_CRLF)
  1409.     i = IDM_LINEENDINGS_CRLF;
  1410.   else if (iEOLMode == SC_EOL_LF)
  1411.     i = IDM_LINEENDINGS_LF;
  1412.   else
  1413.     i = IDM_LINEENDINGS_CR;
  1414.   CheckMenuRadioItem(hmenu,IDM_LINEENDINGS_CRLF,IDM_LINEENDINGS_CR,i,MF_BYCOMMAND);
  1415.   EnableCmd(hmenu,IDM_FILE_RECENT,(MRU_Enum(pFileMRU,0,NULL,0) > 0));
  1416.   EnableCmd(hmenu,IDM_EDIT_UNDO,SendMessage(hwndEdit,SCI_CANUNDO,0,0) /*&& !bReadOnly*/);
  1417.   EnableCmd(hmenu,IDM_EDIT_REDO,SendMessage(hwndEdit,SCI_CANREDO,0,0) /*&& !bReadOnly*/);
  1418.   i  = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) - SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  1419.   i2 = SendMessage(hwndEdit,SCI_CANPASTE,0,0);
  1420.   EnableCmd(hmenu,IDM_EDIT_CUT,i /*&& !bReadOnly*/);
  1421.   EnableCmd(hmenu,IDM_EDIT_COPY,i /*&& !bReadOnly*/);
  1422.   EnableCmd(hmenu,IDM_EDIT_COPYALL,SendMessage(hwndEdit,SCI_GETLENGTH,0,0) /*&& !bReadOnly*/);
  1423.   EnableCmd(hmenu,IDM_EDIT_COPYADD,i /*&& !bReadOnly*/);
  1424.   EnableCmd(hmenu,IDM_EDIT_PASTE,i2 /*&& !bReadOnly*/);
  1425.   EnableCmd(hmenu,IDM_EDIT_SWAP,i || i2 /*&& !bReadOnly*/);
  1426.   EnableCmd(hmenu,IDM_EDIT_CLEAR,i /*&& !bReadOnly*/);
  1427.   OpenClipboard(hwnd);
  1428.   EnableCmd(hmenu,IDM_EDIT_CLEARCLIPBOARD,CountClipboardFormats());
  1429.   CloseClipboard();
  1430.   //EnableCmd(hmenu,IDM_EDIT_MOVELINEUP,!bReadOnly);
  1431.   //EnableCmd(hmenu,IDM_EDIT_MOVELINEDOWN,!bReadOnly);
  1432.   //EnableCmd(hmenu,IDM_EDIT_DUPLICATELINE,!bReadOnly);
  1433.   //EnableCmd(hmenu,IDM_EDIT_CUTLINE,!bReadOnly);
  1434.   //EnableCmd(hmenu,IDM_EDIT_COPYLINE,!bReadOnly);
  1435.   //EnableCmd(hmenu,IDM_EDIT_DELETELINE,!bReadOnly);
  1436.   //EnableCmd(hmenu,IDM_EDIT_INDENT,!bReadOnly);
  1437.   //EnableCmd(hmenu,IDM_EDIT_UNINDENT,!bReadOnly);
  1438.   //EnableCmd(hmenu,IDM_EDIT_PADWITHSPACES,!bReadOnly);
  1439.   //EnableCmd(hmenu,IDM_EDIT_STRIP1STCHAR,!bReadOnly);
  1440.   //EnableCmd(hmenu,IDM_EDIT_STRIPLASTCHAR,!bReadOnly);
  1441.   //EnableCmd(hmenu,IDM_EDIT_TRIMLINES,!bReadOnly);
  1442.   //EnableCmd(hmenu,IDM_EDIT_REMOVEBLANKLINES,!bReadOnly);
  1443.   EnableCmd(hmenu,IDM_EDIT_SORTLINES,
  1444.     SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0),0) -
  1445.     SendMessage(hwndEdit,SCI_LINEFROMPOSITION,(WPARAM)SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0),0) >= 1);
  1446.   EnableCmd(hmenu,IDM_EDIT_COLUMNWRAP,i /*&& IsWindowsNT()*/);
  1447.   EnableCmd(hmenu,IDM_EDIT_SPLITLINES,i /*&& !bReadOnly*/);
  1448.   EnableCmd(hmenu,IDM_EDIT_JOINLINES,i /*&& !bReadOnly*/);
  1449.   EnableCmd(hmenu,IDM_EDIT_JOINLINESEX,i /*&& !bReadOnly*/);
  1450.   EnableCmd(hmenu,IDM_EDIT_CONVERTUPPERCASE,i /*&& !bReadOnly*/);
  1451.   EnableCmd(hmenu,IDM_EDIT_CONVERTLOWERCASE,i /*&& !bReadOnly*/);
  1452.   EnableCmd(hmenu,IDM_EDIT_INVERTCASE,i /*&& !bReadOnly*/ /*&& IsWindowsNT()*/);
  1453.   EnableCmd(hmenu,IDM_EDIT_TITLECASE,i /*&& !bReadOnly*/ /*&& IsWindowsNT()*/);
  1454.   EnableCmd(hmenu,IDM_EDIT_SENTENCECASE,i /*&& !bReadOnly*/ /*&& IsWindowsNT()*/);
  1455.   EnableCmd(hmenu,IDM_EDIT_CONVERTTABS,i /*&& !bReadOnly*/);
  1456.   EnableCmd(hmenu,IDM_EDIT_CONVERTSPACES,i /*&& !bReadOnly*/);
  1457.   EnableCmd(hmenu,IDM_EDIT_CONVERTTABS2,i /*&& !bReadOnly*/);
  1458.   EnableCmd(hmenu,IDM_EDIT_CONVERTSPACES2,i /*&& !bReadOnly*/);
  1459.   EnableCmd(hmenu,IDM_EDIT_URLENCODE,i /*&& !bReadOnly*/);
  1460.   EnableCmd(hmenu,IDM_EDIT_URLDECODE,i /*&& !bReadOnly*/);
  1461.   EnableCmd(hmenu,IDM_EDIT_ESCAPECCHARS,i /*&& !bReadOnly*/);
  1462.   EnableCmd(hmenu,IDM_EDIT_UNESCAPECCHARS,i /*&& !bReadOnly*/);
  1463.   //EnableCmd(hmenu,IDM_EDIT_INCREASENUM,i /*&& !bReadOnly*/);
  1464.   //EnableCmd(hmenu,IDM_EDIT_DECREASENUM,i /*&& !bReadOnly*/);
  1465.   EnableCmd(hmenu,IDM_VIEW_SHOWEXCERPT,i);
  1466.   i = SendMessage(hwndEdit,SCI_GETLEXER,0,0);
  1467.   EnableCmd(hmenu,IDM_EDIT_LINECOMMENT,
  1468.     !(i == SCLEX_NULL || i == SCLEX_CSS || i == SCLEX_DIFF));
  1469.   EnableCmd(hmenu,IDM_EDIT_STREAMCOMMENT,
  1470.     !(i == SCLEX_NULL || i == SCLEX_VBSCRIPT || i == SCLEX_MAKEFILE || i == SCLEX_VB || i == SCLEX_ASM ||
  1471.       i == SCLEX_SQL || i == SCLEX_PERL || i == SCLEX_PYTHON || i == SCLEX_PROPERTIES ||i == SCLEX_CONF ||
  1472.       i == SCLEX_POWERSHELL || i == SCLEX_BATCH || i == SCLEX_DIFF));
  1473.   EnableCmd(hmenu,IDM_EDIT_INSERT_ENCODING,*mEncoding[iEncoding].pszParseNames);
  1474.   //EnableCmd(hmenu,IDM_EDIT_INSERT_SHORTDATE,!bReadOnly);
  1475.   //EnableCmd(hmenu,IDM_EDIT_INSERT_LONGDATE,!bReadOnly);
  1476.   //EnableCmd(hmenu,IDM_EDIT_INSERT_FILENAME,!bReadOnly);
  1477.   //EnableCmd(hmenu,IDM_EDIT_INSERT_PATHNAME,!bReadOnly);
  1478.   i = SendMessage(hwndEdit,SCI_GETLENGTH,0,0);
  1479.   EnableCmd(hmenu,IDM_EDIT_FIND,i);
  1480.   EnableCmd(hmenu,IDM_EDIT_SAVEFIND,i);
  1481.   EnableCmd(hmenu,IDM_EDIT_FINDNEXT,i);
  1482.   EnableCmd(hmenu,IDM_EDIT_FINDPREV,i && lstrlenA(efrData.szFind));
  1483.   EnableCmd(hmenu,IDM_EDIT_SELTONEXT,i);
  1484.   EnableCmd(hmenu,IDM_EDIT_SELTOPREV,i && lstrlenA(efrData.szFind));
  1485.   EnableCmd(hmenu,IDM_EDIT_REPLACE,i /*&& !bReadOnly*/);
  1486.   CheckCmd(hmenu,IDM_VIEW_USE2NDDEFAULT,Style_GetUse2ndDefault(hwndEdit));
  1487.   CheckCmd(hmenu,IDM_VIEW_WORDWRAP,fWordWrap);
  1488.   CheckCmd(hmenu,IDM_VIEW_LONGLINEMARKER,bMarkLongLines);
  1489.   CheckCmd(hmenu,IDM_VIEW_TABSASSPACES,bTabsAsSpaces);
  1490.   CheckCmd(hmenu,IDM_VIEW_SHOWINDENTGUIDES,bShowIndentGuides);
  1491.   CheckCmd(hmenu,IDM_VIEW_AUTOINDENTTEXT,bAutoIndent);
  1492.   CheckCmd(hmenu,IDM_VIEW_LINENUMBERS,bShowLineNumbers);
  1493.   CheckCmd(hmenu,IDM_VIEW_MARGIN,bShowSelectionMargin);
  1494.   CheckCmd(hmenu,IDM_VIEW_SHOWWHITESPACE,bViewWhiteSpace);
  1495.   CheckCmd(hmenu,IDM_VIEW_SHOWEOLS,bViewEOLs);
  1496.   CheckCmd(hmenu,IDM_VIEW_WORDWRAPSYMBOLS,bShowWordWrapSymbols);
  1497.   CheckCmd(hmenu,IDM_VIEW_MATCHBRACES,bMatchBraces);
  1498.   CheckCmd(hmenu,IDM_VIEW_TOOLBAR,bShowToolbar);
  1499.   EnableCmd(hmenu,IDM_VIEW_CUSTOMIZETB,bShowToolbar);
  1500.   CheckCmd(hmenu,IDM_VIEW_STATUSBAR,bShowStatusbar);
  1501.   i = SendMessage(hwndEdit,SCI_GETLEXER,0,0);
  1502.   //EnableCmd(hmenu,IDM_VIEW_AUTOCLOSETAGS,(i == SCLEX_HTML || i == SCLEX_XML));
  1503.   CheckCmd(hmenu,IDM_VIEW_AUTOCLOSETAGS,bAutoCloseTags /*&& (i == SCLEX_HTML || i == SCLEX_XML)*/);
  1504.   CheckCmd(hmenu,IDM_VIEW_HILITECURRENTLINE,bHiliteCurrentLine);
  1505.   i = IniGetInt(L"Settings2",L"ReuseWindow",0);
  1506.   CheckCmd(hmenu,IDM_VIEW_REUSEWINDOW,i);
  1507.   i = IniGetInt(L"Settings2",L"SingleFileInstance",0);
  1508.   CheckCmd(hmenu,IDM_VIEW_SINGLEFILEINSTANCE,i);
  1509.   bStickyWinPos = IniGetInt(L"Settings2",L"StickyWindowPosition",0);
  1510.   CheckCmd(hmenu,IDM_VIEW_STICKYWINPOS,bStickyWinPos);
  1511.   CheckCmd(hmenu,IDM_VIEW_ALWAYSONTOP,bAlwaysOnTop);
  1512.   CheckCmd(hmenu,IDM_VIEW_MINTOTRAY,bMinimizeToTray);
  1513.   CheckCmd(hmenu,IDM_VIEW_TRANSPARENT,bTransparentMode && bTransparentModeAvailable);
  1514.   EnableCmd(hmenu,IDM_VIEW_TRANSPARENT,bTransparentModeAvailable);
  1515.   CheckCmd(hmenu,IDM_VIEW_NOSAVERECENT,bSaveRecentFiles);
  1516.   CheckCmd(hmenu,IDM_VIEW_NOSAVEFINDREPL,bSaveFindReplace);
  1517.   CheckCmd(hmenu,IDM_VIEW_SAVEBEFORERUNNINGTOOLS,bSaveBeforeRunningTools);
  1518.   CheckCmd(hmenu,IDM_VIEW_CHANGENOTIFY,iFileWatchingMode);
  1519.   if (lstrlen(szTitleExcerpt))
  1520.     i = IDM_VIEW_SHOWEXCERPT;
  1521.   else if (iPathNameFormat == 0)
  1522.     i = IDM_VIEW_SHOWFILENAMEONLY;
  1523.   else if (iPathNameFormat == 1)
  1524.     i = IDM_VIEW_SHOWFILENAMEFIRST;
  1525.   else
  1526.     i = IDM_VIEW_SHOWFULLPATH;
  1527.   CheckMenuRadioItem(hmenu,IDM_VIEW_SHOWFILENAMEONLY,IDM_VIEW_SHOWEXCERPT,i,MF_BYCOMMAND);
  1528.   if (iEscFunction == 1)
  1529.     i = IDM_VIEW_ESCMINIMIZE;
  1530.   else if (iEscFunction == 2)
  1531.     i = IDM_VIEW_ESCEXIT;
  1532.   else
  1533.     i = IDM_VIEW_NOESCFUNC;
  1534.   CheckMenuRadioItem(hmenu,IDM_VIEW_NOESCFUNC,IDM_VIEW_ESCEXIT,i,MF_BYCOMMAND);
  1535.   i = lstrlen(szIniFile);
  1536.   CheckCmd(hmenu,IDM_VIEW_SAVESETTINGS,bSaveSettings && i);
  1537.   EnableCmd(hmenu,IDM_VIEW_REUSEWINDOW,i);
  1538.   EnableCmd(hmenu,IDM_VIEW_STICKYWINPOS,i);
  1539.   EnableCmd(hmenu,IDM_VIEW_SINGLEFILEINSTANCE,i);
  1540.   EnableCmd(hmenu,IDM_VIEW_NOSAVERECENT,i);
  1541.   EnableCmd(hmenu,IDM_VIEW_NOSAVEFINDREPL,i);
  1542.   EnableCmd(hmenu,IDM_VIEW_SAVESETTINGS,i);
  1543.   i = (lstrlen(szIniFile) > 0 || lstrlen(szIniFile2) > 0);
  1544.   EnableCmd(hmenu,IDM_VIEW_SAVESETTINGSNOW,i);
  1545. }
  1546. //=============================================================================
  1547. //
  1548. //  MsgCommand() - Handles WM_COMMAND
  1549. //
  1550. //
  1551. LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam)
  1552. {
  1553.   switch(LOWORD(wParam))
  1554.   {
  1555.     case IDM_FILE_NEW:
  1556.       FileLoad(FALSE,TRUE,FALSE,FALSE,L"");
  1557.       break;
  1558.     case IDM_FILE_OPEN:
  1559.       FileLoad(FALSE,FALSE,FALSE,FALSE,L"");
  1560.       break;
  1561.     case IDM_FILE_REVERT:
  1562.       {
  1563.         if (lstrlen(szCurFile)) {
  1564.           WCHAR tchCurFile2[MAX_PATH];
  1565.           int iCurPos     = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  1566.           int iAnchorPos  = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  1567.           int iCurTopLine = SendMessage(hwndEdit,SCI_GETFIRSTVISIBLELINE,0,0);
  1568.           int iXOffset    = SendMessage(hwndEdit,SCI_GETXOFFSET,0,0);
  1569.           if ((bModified || iEncoding != iOriginalEncoding) && MsgBox(MBOKCANCEL,IDS_ASK_REVERT) != IDOK)
  1570.             return(0);
  1571.           lstrcpy(tchCurFile2,szCurFile);
  1572.           iWeakSrcEncoding = iEncoding;
  1573.           if (FileLoad(TRUE,FALSE,TRUE,FALSE,tchCurFile2))
  1574.           {
  1575.             if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) {
  1576.               char tch[5] = "";
  1577.               SendMessage(hwndEdit,SCI_GETTEXT,5,(LPARAM)tch);
  1578.               if (lstrcmpiA(tch,".LOG") != 0) {
  1579.                 int iTopLineAfterLoad;
  1580.                 SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
  1581.                 iTopLineAfterLoad = SendMessage(hwndEdit,SCI_GETFIRSTVISIBLELINE,0,0);
  1582.                 SendMessage(hwndEdit,SCI_LINESCROLL,0,(LPARAM)(iCurTopLine - iTopLineAfterLoad));
  1583.                 SendMessage(hwndEdit,SCI_SETXOFFSET,(WPARAM)iXOffset,0);
  1584.               }
  1585.             }
  1586.           }
  1587.         }
  1588.       }
  1589.       break;
  1590.     case IDM_FILE_SAVE:
  1591.       FileSave(TRUE,FALSE,FALSE,FALSE);
  1592.       break;
  1593.     case IDM_FILE_SAVEAS:
  1594.       FileSave(TRUE,FALSE,TRUE,FALSE);
  1595.       break;
  1596.     case IDM_FILE_SAVECOPY:
  1597.       FileSave(TRUE,FALSE,TRUE,TRUE);
  1598.       break;
  1599.     case IDM_FILE_READONLY:
  1600.       //bReadOnly = (bReadOnly) ? FALSE : TRUE;
  1601.       //SendMessage(hwndEdit,SCI_SETREADONLY,bReadOnly,0);
  1602.       //UpdateToolbar();
  1603.       //UpdateStatusbar();
  1604.       if (lstrlen(szCurFile))
  1605.       {
  1606.         BOOL bSuccess = FALSE;
  1607.         DWORD dwFileAttributes = GetFileAttributes(szCurFile);
  1608.         if (dwFileAttributes != INVALID_FILE_ATTRIBUTES) {
  1609.           if (bReadOnly)
  1610.             dwFileAttributes = (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
  1611.           else
  1612.             dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
  1613.           if (!SetFileAttributes(szCurFile,dwFileAttributes))
  1614.             MsgBox(MBWARN,IDS_READONLY_MODIFY,szCurFile);
  1615.         }
  1616.         else
  1617.           MsgBox(MBWARN,IDS_READONLY_MODIFY,szCurFile);
  1618.         dwFileAttributes = GetFileAttributes(szCurFile);
  1619.         if (dwFileAttributes != INVALID_FILE_ATTRIBUTES)
  1620.           bReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY);
  1621.         SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  1622.           iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  1623.           IDS_READONLY,bReadOnly,szTitleExcerpt);
  1624.       }
  1625.       break;
  1626.     case IDM_FILE_BROWSE:
  1627.       {
  1628.         SHELLEXECUTEINFO sei;
  1629.         WCHAR tchParam[MAX_PATH+4];
  1630.         WCHAR tchExeFile[MAX_PATH+4];
  1631.         lstrcpy(tchParam,szCurFile);
  1632.         PathQuoteSpaces(tchParam);
  1633.         if (!IniGetString(L"Settings2",L"filebrowser.exe",L"",tchExeFile,COUNTOF(tchExeFile))) {
  1634.           if (!SearchPath(NULL,L"metapath.exe",NULL,COUNTOF(tchExeFile),tchExeFile,NULL)) {
  1635.             GetModuleFileName(NULL,tchExeFile,COUNTOF(tchExeFile));
  1636.             PathRemoveFileSpec(tchExeFile);
  1637.             PathAppend(tchExeFile,L"metapath.exe");
  1638.           }
  1639.         }
  1640.         else if (PathIsRelative(tchExeFile)) {
  1641.           WCHAR tch[MAX_PATH];
  1642.           if (!SearchPath(NULL,tchExeFile,NULL,COUNTOF(tch),tch,NULL)) {
  1643.             GetModuleFileName(NULL,tch,COUNTOF(tch));
  1644.             PathRemoveFileSpec(tch);
  1645.             PathAppend(tch,tchExeFile);
  1646.             lstrcpy(tchExeFile,tch);
  1647.           }
  1648.           else
  1649.             lstrcpy(tchExeFile,tch);
  1650.         }
  1651.         ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  1652.         sei.cbSize = sizeof(SHELLEXECUTEINFO);
  1653.         sei.fMask = SEE_MASK_FLAG_NO_UI | /*SEE_MASK_NOZONECHECKS*/0x00800000;
  1654.         sei.hwnd = hwnd;
  1655.         sei.lpVerb = NULL;
  1656.         sei.lpFile = tchExeFile;
  1657.         sei.lpParameters = tchParam;
  1658.         sei.lpDirectory = NULL;
  1659.         sei.nShow = SW_SHOWNORMAL;
  1660.         ShellExecuteEx(&sei);
  1661.         if ((int)sei.hInstApp < 32)
  1662.           MsgBox(MBWARN,IDS_ERR_BROWSE);
  1663.       }
  1664.       break;
  1665.     case IDM_FILE_NEWWINDOW:
  1666.     case IDM_FILE_NEWWINDOW2:
  1667.       {
  1668.         SHELLEXECUTEINFO sei;
  1669.         WCHAR szModuleName[MAX_PATH];
  1670.         WCHAR szFileName[MAX_PATH];
  1671.         WCHAR szParameters[2*MAX_PATH+64];
  1672.         MONITORINFO mi;
  1673.         HMONITOR hMonitor;
  1674.         WINDOWPLACEMENT wndpl;
  1675.         int x,y,cx,cy,imax;
  1676.         WCHAR tch[64];
  1677.         if (bSaveBeforeRunningTools && !FileSave(FALSE,TRUE,FALSE,FALSE))
  1678.           break;
  1679.         GetModuleFileName(NULL,szModuleName,COUNTOF(szModuleName));
  1680.         lstrcpy(szParameters,L"-f");
  1681.         if (lstrlen(szIniFile)) {
  1682.           lstrcat(szParameters,L" "");
  1683.           lstrcat(szParameters,szIniFile);
  1684.           lstrcat(szParameters,L""");
  1685.         }
  1686.         else
  1687.           lstrcat(szParameters,L"0");
  1688.         lstrcat(szParameters,L" -n");
  1689.         wndpl.length = sizeof(WINDOWPLACEMENT);
  1690.         GetWindowPlacement(hwnd,&wndpl);
  1691.         hMonitor = MonitorFromRect(&wndpl.rcNormalPosition,MONITOR_DEFAULTTONEAREST);
  1692.         mi.cbSize = sizeof(mi);
  1693.         GetMonitorInfo(hMonitor,&mi);
  1694.         // offset new window position +10/+10
  1695.         x = wndpl.rcNormalPosition.left + 10;
  1696.         y = wndpl.rcNormalPosition.top  + 10;
  1697.         cx = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
  1698.         cy = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
  1699.         // check if window fits monitor
  1700.         if ((x + cx) > mi.rcWork.right || (y + cy) > mi.rcWork.bottom) {
  1701.           x = mi.rcMonitor.left;
  1702.           y = mi.rcMonitor.top;
  1703.         }
  1704.         imax = IsZoomed(hwnd);
  1705.         wsprintf(tch,L" -p %i,%i,%i,%i,%i",x,y,cx,cy,imax);
  1706.         lstrcat(szParameters,tch);
  1707.         if (LOWORD(wParam) != IDM_FILE_NEWWINDOW2 && lstrlen(szCurFile)) {
  1708.           lstrcpy(szFileName,szCurFile);
  1709.           PathQuoteSpaces(szFileName);
  1710.           lstrcat(szParameters,L" ");
  1711.           lstrcat(szParameters,szFileName);
  1712.         }
  1713.         ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  1714.         sei.cbSize = sizeof(SHELLEXECUTEINFO);
  1715.         sei.fMask = /*SEE_MASK_NOZONECHECKS*/0x00800000;
  1716.         sei.hwnd = hwnd;
  1717.         sei.lpVerb = NULL;
  1718.         sei.lpFile = szModuleName;
  1719.         sei.lpParameters = szParameters;
  1720.         sei.lpDirectory = NULL;
  1721.         sei.nShow = SW_SHOWNORMAL;
  1722.         ShellExecuteEx(&sei);
  1723.       }
  1724.       break;
  1725.     case IDM_FILE_LAUNCH:
  1726.       {
  1727.         SHELLEXECUTEINFO sei;
  1728.         WCHAR wchDirectory[MAX_PATH] = L"";
  1729.         if (!lstrlen(szCurFile))
  1730.           break;
  1731.         if (bSaveBeforeRunningTools && !FileSave(FALSE,TRUE,FALSE,FALSE))
  1732.           break;
  1733.         if (lstrlen(szCurFile)) {
  1734.           lstrcpy(wchDirectory,szCurFile);
  1735.           PathRemoveFileSpec(wchDirectory);
  1736.         }
  1737.         ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  1738.         sei.cbSize = sizeof(SHELLEXECUTEINFO);
  1739.         sei.fMask = 0;
  1740.         sei.hwnd = hwnd;
  1741.         sei.lpVerb = NULL;
  1742.         sei.lpFile = szCurFile;
  1743.         sei.lpParameters = NULL;
  1744.         sei.lpDirectory = NULL;
  1745.         sei.nShow = SW_SHOWNORMAL;
  1746.         ShellExecuteEx(&sei);
  1747.       }
  1748.       break;
  1749.     case IDM_FILE_RUN:
  1750.       {
  1751.         WCHAR tchCmdLine[MAX_PATH+4];
  1752.         if (bSaveBeforeRunningTools && !FileSave(FALSE,TRUE,FALSE,FALSE))
  1753.           break;
  1754.         lstrcpy(tchCmdLine,szCurFile);
  1755.         PathQuoteSpaces(tchCmdLine);
  1756.         RunDlg(hwnd,tchCmdLine);
  1757.       }
  1758.       break;
  1759.     case IDM_FILE_OPENWITH:
  1760.       if (bSaveBeforeRunningTools && !FileSave(FALSE,TRUE,FALSE,FALSE))
  1761.         break;
  1762.       OpenWithDlg(hwnd,szCurFile);
  1763.       break;
  1764.     case IDM_FILE_PAGESETUP:
  1765.       EditPrintSetup(hwndEdit);
  1766.       break;
  1767.     case IDM_FILE_PRINT:
  1768.       {
  1769.         SHFILEINFO shfi;
  1770.         WCHAR *pszTitle;
  1771.         WCHAR tchUntitled[32];
  1772.         WCHAR tchPageFmt[32];
  1773.         if (lstrlen(szCurFile)) {
  1774.           SHGetFileInfo2(szCurFile,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
  1775.           pszTitle = shfi.szDisplayName;
  1776.         }
  1777.         else {
  1778.           GetString(IDS_UNTITLED,tchUntitled,COUNTOF(tchUntitled));
  1779.           pszTitle = tchUntitled;
  1780.         }
  1781.         GetString(IDS_PRINT_PAGENUM,tchPageFmt,COUNTOF(tchPageFmt));
  1782.         if (!EditPrint(hwndEdit,pszTitle,tchPageFmt))
  1783.           MsgBox(MBWARN,IDS_PRINT_ERROR,pszTitle);
  1784.       }
  1785.       break;
  1786.     case IDM_FILE_PROPERTIES:
  1787.       {
  1788.         SHELLEXECUTEINFO sei;
  1789.         if (lstrlen(szCurFile) == 0)
  1790.           break;
  1791.         ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  1792.         sei.cbSize = sizeof(SHELLEXECUTEINFO);
  1793.         sei.fMask = SEE_MASK_INVOKEIDLIST;
  1794.         sei.hwnd = hwnd;
  1795.         sei.lpVerb = L"properties";
  1796.         sei.lpFile = szCurFile;
  1797.         sei.nShow = SW_SHOWNORMAL;
  1798.         ShellExecuteEx(&sei);
  1799.       }
  1800.       break;
  1801.     case IDM_FILE_CREATELINK:
  1802.       {
  1803.         if (!lstrlen(szCurFile))
  1804.           break;
  1805.         if (!PathCreateDeskLnk(szCurFile))
  1806.           MsgBox(MBWARN,IDS_ERR_CREATELINK);
  1807.       }
  1808.       break;
  1809.     case IDM_FILE_OPENFAV:
  1810.       if (FileSave(FALSE,TRUE,FALSE,FALSE)) {
  1811.         WCHAR tchSelItem[MAX_PATH];
  1812.         if (FavoritesDlg(hwnd,tchSelItem))
  1813.         {
  1814.           if (PathIsLnkToDirectory(tchSelItem,NULL,0))
  1815.             PathGetLnkPath(tchSelItem,tchSelItem,COUNTOF(tchSelItem));
  1816.           if (PathIsDirectory(tchSelItem))
  1817.           {
  1818.             WCHAR tchFile[MAX_PATH];
  1819.             if (OpenFileDlg(hwndMain,tchFile,COUNTOF(tchFile),tchSelItem))
  1820.               FileLoad(TRUE,FALSE,FALSE,FALSE,tchFile);
  1821.           }
  1822.           else
  1823.             FileLoad(TRUE,FALSE,FALSE,FALSE,tchSelItem);
  1824.           }
  1825.         }
  1826.       break;
  1827.     case IDM_FILE_ADDTOFAV:
  1828.       if (lstrlen(szCurFile)) {
  1829.         SHFILEINFO shfi;
  1830.         SHGetFileInfo2(szCurFile,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
  1831.         AddToFavDlg(hwnd,shfi.szDisplayName,szCurFile);
  1832.       }
  1833.       break;
  1834.     case IDM_FILE_MANAGEFAV:
  1835.       {
  1836.         SHELLEXECUTEINFO sei;
  1837.         ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
  1838.         sei.cbSize = sizeof(SHELLEXECUTEINFO);
  1839.         sei.fMask = 0;
  1840.         sei.hwnd = hwnd;
  1841.         sei.lpVerb = NULL;
  1842.         sei.lpFile = tchFavoritesDir;
  1843.         sei.lpParameters = NULL;
  1844.         sei.lpDirectory = NULL;
  1845.         sei.nShow = SW_SHOWNORMAL;
  1846.         // Run favorites directory
  1847.         ShellExecuteEx(&sei);
  1848.       }
  1849.       break;
  1850.     case IDM_FILE_RECENT:
  1851.       if (MRU_Enum(pFileMRU,0,NULL,0) > 0) {
  1852.         if (FileSave(FALSE,TRUE,FALSE,FALSE)) {
  1853.           WCHAR tchFile[MAX_PATH];
  1854.           if (FileMRUDlg(hwnd,tchFile))
  1855.             FileLoad(TRUE,FALSE,FALSE,FALSE,tchFile);
  1856.           }
  1857.         }
  1858.       break;
  1859.     case IDM_FILE_EXIT:
  1860.       SendMessage(hwnd,WM_CLOSE,0,0);
  1861.       break;
  1862.     case IDM_ENCODING_ANSI:
  1863.     case IDM_ENCODING_UNICODE:
  1864.     case IDM_ENCODING_UNICODEREV:
  1865.     case IDM_ENCODING_UTF8:
  1866.     case IDM_ENCODING_UTF8SIGN:
  1867.     case IDM_ENCODING_SELECT:
  1868.       {
  1869.         int iNewEncoding = iEncoding;
  1870.         if (LOWORD(wParam) == IDM_ENCODING_SELECT && !SelectEncodingDlg(hwnd,&iNewEncoding))
  1871.           break;
  1872.         else {
  1873.           switch (LOWORD(wParam)) {
  1874.             case IDM_ENCODING_UNICODE:    iNewEncoding = CPI_UNICODEBOM; break;
  1875.             case IDM_ENCODING_UNICODEREV: iNewEncoding = CPI_UNICODEBEBOM; break;
  1876.             case IDM_ENCODING_UTF8:       iNewEncoding = CPI_UTF8; break;
  1877.             case IDM_ENCODING_UTF8SIGN:   iNewEncoding = CPI_UTF8SIGN; break;
  1878.             case IDM_ENCODING_ANSI:       iNewEncoding = CPI_DEFAULT; break;
  1879.           }
  1880.         }
  1881.         if (EditSetNewEncoding(hwndEdit,
  1882.           iEncoding,iNewEncoding,
  1883.           (flagSetEncoding),lstrlen(szCurFile) == 0)) {
  1884.           if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) == 0) {
  1885.             iEncoding = iNewEncoding;
  1886.             iOriginalEncoding = iNewEncoding;
  1887.           }
  1888.           else {
  1889.             if (iEncoding == CPI_DEFAULT || iNewEncoding == CPI_DEFAULT)
  1890.               iOriginalEncoding = -1;
  1891.             iEncoding = iNewEncoding;
  1892.           }
  1893.           UpdateToolbar();
  1894.           UpdateStatusbar();
  1895.           SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  1896.             iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  1897.             IDS_READONLY,bReadOnly,szTitleExcerpt);
  1898.         }
  1899.       }
  1900.       break;
  1901.     case IDM_ENCODING_RECODE:
  1902.       {
  1903.         if (lstrlen(szCurFile)) {
  1904.           WCHAR tchCurFile2[MAX_PATH];
  1905.           int iNewEncoding = -1;
  1906.           if (iEncoding != CPI_DEFAULT)
  1907.             iNewEncoding = iEncoding;
  1908.           if (iEncoding == CPI_UTF8SIGN)
  1909.             iNewEncoding = CPI_UTF8;
  1910.           if ((bModified || iEncoding != iOriginalEncoding) && MsgBox(MBOKCANCEL,IDS_ASK_RECODE) != IDOK)
  1911.             return(0);
  1912.           if (RecodeDlg(hwnd,&iNewEncoding)) {
  1913.             lstrcpy(tchCurFile2,szCurFile);
  1914.             iSrcEncoding = iNewEncoding;
  1915.             FileLoad(TRUE,FALSE,TRUE,FALSE,tchCurFile2);
  1916.           }
  1917.         }
  1918.       }
  1919.       break;
  1920.     case IDM_ENCODING_SETDEFAULT:
  1921.       SelectDefEncodingDlg(hwnd,&iDefaultEncoding);
  1922.       break;
  1923.     case IDM_LINEENDINGS_CRLF:
  1924.     case IDM_LINEENDINGS_LF:
  1925.     case IDM_LINEENDINGS_CR:
  1926.       {
  1927.         int iNewEOLMode = iLineEndings[LOWORD(wParam)-IDM_LINEENDINGS_CRLF];
  1928.         iEOLMode = iNewEOLMode;
  1929.         SendMessage(hwndEdit,SCI_SETEOLMODE,iEOLMode,0);
  1930.         SendMessage(hwndEdit,SCI_CONVERTEOLS,iEOLMode,0);
  1931.         UpdateToolbar();
  1932.         UpdateStatusbar();
  1933.         SetWindowTitle(hwnd,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
  1934.           iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
  1935.           IDS_READONLY,bReadOnly,szTitleExcerpt);
  1936.       }
  1937.       break;
  1938.     case IDM_LINEENDINGS_SETDEFAULT:
  1939.       SelectDefLineEndingDlg(hwnd,&iDefaultEOLMode);
  1940.       break;
  1941.     case IDM_EDIT_UNDO:
  1942.       SendMessage(hwndEdit,SCI_UNDO,0,0);
  1943.       break;
  1944.     case IDM_EDIT_REDO:
  1945.       SendMessage(hwndEdit,SCI_REDO,0,0);
  1946.       break;
  1947.     case IDM_EDIT_CUT:
  1948.       if (flagPasteBoard)
  1949.         bLastCopyFromMe = TRUE;
  1950.       SendMessage(hwndEdit,SCI_CUT,0,0);
  1951.       break;
  1952.     case IDM_EDIT_COPY:
  1953.       if (flagPasteBoard)
  1954.         bLastCopyFromMe = TRUE;
  1955.       SendMessage(hwndEdit,SCI_COPY,0,0);
  1956.       UpdateToolbar();
  1957.       break;
  1958.     case IDM_EDIT_COPYALL:
  1959.       if (flagPasteBoard)
  1960.         bLastCopyFromMe = TRUE;
  1961.       SendMessage(hwndEdit,SCI_COPYRANGE,0,SendMessage(hwndEdit,SCI_GETLENGTH,0,0));
  1962.       UpdateToolbar();
  1963.       break;
  1964.     case IDM_EDIT_COPYADD:
  1965.       if (flagPasteBoard)
  1966.         bLastCopyFromMe = TRUE;
  1967.       EditCopyAppend(hwndEdit);
  1968.       UpdateToolbar();
  1969.       break;
  1970.     case IDM_EDIT_PASTE:
  1971.       SendMessage(hwndEdit,SCI_PASTE,0,0);
  1972.       break;
  1973.     case IDM_EDIT_SWAP:
  1974.       if (SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  1975.           SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0) == 0) {
  1976.         int iNewPos = -1;
  1977.         int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  1978.         SendMessage(hwndEdit,SCI_PASTE,0,0);
  1979.         iNewPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  1980.         SendMessage(hwndEdit,SCI_SETSEL,iPos,iNewPos);
  1981.         SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_CLEARCLIPBOARD,1),0);
  1982.       }
  1983.       else {
  1984.         int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  1985.         int iAnchor = SendMessage(hwndEdit,SCI_GETANCHOR,0,0);
  1986.         char *pClip = EditGetClipboardText(hwndEdit);
  1987.         if (flagPasteBoard)
  1988.           bLastCopyFromMe = TRUE;
  1989.         SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  1990.         SendMessage(hwndEdit,SCI_CUT,0,0);
  1991.         SendMessage(hwndEdit,SCI_REPLACESEL,(WPARAM)0,(LPARAM)pClip);
  1992.         if (iPos > iAnchor)
  1993.           SendMessage(hwndEdit,SCI_SETSEL,iAnchor,iAnchor + lstrlenA(pClip));
  1994.         else
  1995.           SendMessage(hwndEdit,SCI_SETSEL,iPos + lstrlenA(pClip),iPos);
  1996.         SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  1997.         LocalFree(pClip);
  1998.       }
  1999.       break;
  2000.     case IDM_EDIT_CLEAR:
  2001.       SendMessage(hwndEdit,SCI_CLEAR,0,0);
  2002.       break;
  2003.     case IDM_EDIT_CLEARCLIPBOARD:
  2004.       if (OpenClipboard(hwnd)) {
  2005.         if (CountClipboardFormats() > 0) {
  2006.           EmptyClipboard();
  2007.           UpdateToolbar();
  2008.           UpdateStatusbar();
  2009.         }
  2010.         CloseClipboard();
  2011.       }
  2012.       break;
  2013.     case IDM_EDIT_SELECTALL:
  2014.       SendMessage(hwndEdit,SCI_SELECTALL,0,0);
  2015.       //SendMessage(hwndEdit,SCI_SETSEL,0,(LPARAM)-1);
  2016.       break;
  2017.     case IDM_EDIT_SELECTWORD:
  2018.       {
  2019.         int iSel =
  2020.           SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  2021.           SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2022.         int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  2023.         if (iSel == 0) {
  2024.           int iWordStart = SendMessage(hwndEdit,SCI_WORDSTARTPOSITION,iPos,TRUE);
  2025.           int iWordEnd   = SendMessage(hwndEdit,SCI_WORDENDPOSITION,iPos,TRUE);
  2026.           if (iWordStart == iWordEnd) // we are in whitespace salad...
  2027.           {
  2028.             iWordStart = SendMessage(hwndEdit,SCI_WORDENDPOSITION,iPos,FALSE);
  2029.             iWordEnd   = SendMessage(hwndEdit,SCI_WORDENDPOSITION,iWordStart,TRUE);
  2030.             if (iWordStart != iWordEnd) {
  2031.               //if (SCLEX_HTML == SendMessage(hwndEdit,SCI_GETLEXER,0,0) &&
  2032.               //    SCE_HPHP_VARIABLE == SendMessage(hwndEdit,SCI_GETSTYLEAT,(WPARAM)iWordStart,0) &&
  2033.               //    '$' == (char)SendMessage(hwndEdit,SCI_GETCHARAT,(WPARAM)iWordStart-1,0))
  2034.               //  iWordStart--;
  2035.               SendMessage(hwndEdit,SCI_SETSEL,iWordStart,iWordEnd);
  2036.             }
  2037.           }
  2038.           else {
  2039.             //if (SCLEX_HTML == SendMessage(hwndEdit,SCI_GETLEXER,0,0) &&
  2040.             //    SCE_HPHP_VARIABLE == SendMessage(hwndEdit,SCI_GETSTYLEAT,(WPARAM)iWordStart,0) &&
  2041.             //    '$' == (char)SendMessage(hwndEdit,SCI_GETCHARAT,(WPARAM)iWordStart-1,0))
  2042.             //  iWordStart--;
  2043.             SendMessage(hwndEdit,SCI_SETSEL,iWordStart,iWordEnd);
  2044.           }
  2045.           iSel =
  2046.             SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0) -
  2047.             SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2048.           if (iSel == 0) {
  2049.             int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iPos,0);
  2050.             int iLineStart = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,iLine,0);
  2051.             int iLineEnd   = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,iLine,0);
  2052.             SendMessage(hwndEdit,SCI_SETSEL,iLineStart,iLineEnd);
  2053.           }
  2054.         }
  2055.         else {
  2056.           int iLine = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iPos,0);
  2057.           int iLineStart = SendMessage(hwndEdit,SCI_GETLINEINDENTPOSITION,iLine,0);
  2058.           int iLineEnd   = SendMessage(hwndEdit,SCI_GETLINEENDPOSITION,iLine,0);
  2059.           SendMessage(hwndEdit,SCI_SETSEL,iLineStart,iLineEnd);
  2060.         }
  2061.       }
  2062.       break;
  2063.     case IDM_EDIT_SELECTLINE:
  2064.       {
  2065.         int iSelStart  = SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2066.         int iSelEnd    = SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0);
  2067.         int iLineStart = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iSelStart,0);
  2068.         int iLineEnd   = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,iSelEnd,0);
  2069.         iSelStart = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,iLineStart,0);
  2070.         iSelEnd   = SendMessage(hwndEdit,SCI_POSITIONFROMLINE,iLineEnd+1,0);
  2071.         SendMessage(hwndEdit,SCI_SETSEL,iSelStart,iSelEnd);
  2072.         SendMessage(hwndEdit,SCI_CHOOSECARETX,0,0);
  2073.       }
  2074.       break;
  2075.     case IDM_EDIT_MOVELINEUP:
  2076.       EditMoveUp(hwndEdit);
  2077.       break;
  2078.     case IDM_EDIT_MOVELINEDOWN:
  2079.       EditMoveDown(hwndEdit);
  2080.       break;
  2081.     case IDM_EDIT_DUPLICATELINE:
  2082.       SendMessage(hwndEdit,SCI_LINEDUPLICATE,0,0);
  2083.       break;
  2084.     case IDM_EDIT_CUTLINE:
  2085.       if (flagPasteBoard)
  2086.         bLastCopyFromMe = TRUE;
  2087.       SendMessage(hwndEdit,SCI_LINECUT,0,0);
  2088.       break;
  2089.     case IDM_EDIT_COPYLINE:
  2090.       if (flagPasteBoard)
  2091.         bLastCopyFromMe = TRUE;
  2092.       SendMessage(hwndEdit,SCI_LINECOPY,0,0);
  2093.       UpdateToolbar();
  2094.       break;
  2095.     case IDM_EDIT_DELETELINE:
  2096.       SendMessage(hwndEdit,SCI_LINEDELETE,0,0);
  2097.       break;
  2098.     case IDM_EDIT_DELETELINELEFT:
  2099.       SendMessage(hwndEdit,SCI_DELLINELEFT,0,0);
  2100.       break;
  2101.     case IDM_EDIT_DELETELINERIGHT:
  2102.       SendMessage(hwndEdit,SCI_DELLINERIGHT,0,0);
  2103.       break;
  2104.     case IDM_EDIT_INDENT:
  2105.       {
  2106.         int iLineSelStart = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,
  2107.           SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0),0);
  2108.         int iLineSelEnd   = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,
  2109.           SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0),0);
  2110.         SendMessage(hwndEdit,SCI_SETTABINDENTS,TRUE,0);
  2111.         if (iLineSelStart == iLineSelEnd) {
  2112.           SendMessage(hwndEdit,SCI_VCHOME,0,0);
  2113.           SendMessage(hwndEdit,SCI_TAB,0,0);
  2114.         }
  2115.         else
  2116.           SendMessage(hwndEdit,SCI_TAB,0,0);
  2117.         SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
  2118.       }
  2119.       break;
  2120.     case IDM_EDIT_UNINDENT:
  2121.       {
  2122.         int iLineSelStart = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,
  2123.           SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0),0);
  2124.         int iLineSelEnd   = SendMessage(hwndEdit,SCI_LINEFROMPOSITION,
  2125.           SendMessage(hwndEdit,SCI_GETSELECTIONEND,0,0),0);
  2126.         SendMessage(hwndEdit,SCI_SETTABINDENTS,TRUE,0);
  2127.         if (iLineSelStart == iLineSelEnd) {
  2128.           SendMessage(hwndEdit,SCI_VCHOME,0,0);
  2129.           SendMessage(hwndEdit,SCI_BACKTAB,0,0);
  2130.         }
  2131.         else
  2132.           SendMessage(hwndEdit,SCI_BACKTAB,0,0);
  2133.         SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
  2134.       }
  2135.       break;
  2136.     case IDM_EDIT_ENCLOSESELECTION:
  2137.       if (EditEncloseSelectionDlg(hwnd,wchPrefixSelection,wchAppendSelection)) {
  2138.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2139.         EditEncloseSelection(hwndEdit,wchPrefixSelection,wchAppendSelection);
  2140.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2141.       }
  2142.       break;
  2143.     case IDM_EDIT_SELECTIONDUPLICATE:
  2144.       SendMessage(hwndEdit,SCI_BEGINUNDOACTION,0,0);
  2145.       SendMessage(hwndEdit,SCI_SELECTIONDUPLICATE,0,0);
  2146.       SendMessage(hwndEdit,SCI_ENDUNDOACTION,0,0);
  2147.       break;
  2148.     case IDM_EDIT_PADWITHSPACES:
  2149.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2150.       EditPadWithSpaces(hwndEdit);
  2151.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2152.       break;
  2153.     case IDM_EDIT_STRIP1STCHAR:
  2154.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2155.       EditStripFirstCharacter(hwndEdit);
  2156.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2157.       break;
  2158.     case IDM_EDIT_STRIPLASTCHAR:
  2159.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2160.       EditStripLastCharacter(hwndEdit);
  2161.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2162.       break;
  2163.     case IDM_EDIT_TRIMLINES:
  2164.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2165.       EditStripTrailingBlanks(hwndEdit,FALSE);
  2166.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2167.       break;
  2168.     case IDM_EDIT_COMPRESSWS:
  2169.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2170.       EditCompressSpaces(hwndEdit);
  2171.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2172.       break;
  2173.     case IDM_EDIT_REMOVEBLANKLINES:
  2174.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2175.       EditRemoveBlankLines(hwndEdit);
  2176.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2177.       break;
  2178.     case IDM_EDIT_MODIFYLINES:
  2179.       if (EditModifyLinesDlg(hwnd,wchPrefixLines,wchAppendLines)) {
  2180.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2181.         EditModifyLines(hwndEdit,wchPrefixLines,wchAppendLines);
  2182.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2183.       }
  2184.       break;
  2185.     case IDM_EDIT_SORTLINES:
  2186.       if (EditSortDlg(hwnd,&iSortOptions)) {
  2187.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2188.         StatusSetText(hwndStatus,255,L"...");
  2189.         StatusSetSimple(hwndStatus,TRUE);
  2190.         InvalidateRect(hwndStatus,NULL,TRUE);
  2191.         UpdateWindow(hwndStatus);
  2192.         EditSortLines(hwndEdit,iSortOptions);
  2193.         StatusSetSimple(hwndStatus,FALSE);
  2194.         SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2195.       }
  2196.       break;
  2197.     case IDM_EDIT_COLUMNWRAP:
  2198.       {
  2199.         if (iWrapCol == 0)
  2200.           iWrapCol = iLongLinesLimit;
  2201.         if (ColumnWrapDlg(hwnd,IDD_COLUMNWRAP,&iWrapCol))
  2202.         {
  2203.           iWrapCol = max(min(iWrapCol,512),1);
  2204.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2205.           EditWrapToColumn(hwndEdit,iWrapCol);
  2206.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2207.         }
  2208.       }
  2209.       break;
  2210.     case IDM_EDIT_SPLITLINES:
  2211.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2212.       SendMessage(hwndEdit,SCI_TARGETFROMSELECTION,0,0);
  2213.       SendMessage(hwndEdit,SCI_LINESSPLIT,0,0);
  2214.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2215.       break;
  2216.     case IDM_EDIT_JOINLINES:
  2217.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2218.       SendMessage(hwndEdit,SCI_TARGETFROMSELECTION,0,0);
  2219.       SendMessage(hwndEdit,SCI_LINESJOIN,0,0);
  2220.       EditJoinLinesEx(hwndEdit);
  2221.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2222.       break;
  2223.     case IDM_EDIT_JOINLINESEX:
  2224.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2225.       EditJoinLinesEx(hwndEdit);
  2226.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2227.       break;
  2228.     case IDM_EDIT_CONVERTUPPERCASE:
  2229.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2230.       EditMakeUppercase(hwndEdit);
  2231.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2232.       break;
  2233.     case IDM_EDIT_CONVERTLOWERCASE:
  2234.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2235.       EditMakeLowercase(hwndEdit);
  2236.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2237.       break;
  2238.     case IDM_EDIT_INVERTCASE:
  2239.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2240.       EditInvertCase(hwndEdit);
  2241.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2242.       break;
  2243.     case IDM_EDIT_TITLECASE:
  2244.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2245.       EditTitleCase(hwndEdit);
  2246.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2247.       break;
  2248.     case IDM_EDIT_SENTENCECASE:
  2249.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2250.       EditSentenceCase(hwndEdit);
  2251.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2252.       break;
  2253.     case IDM_EDIT_CONVERTTABS:
  2254.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2255.       EditTabsToSpaces(hwndEdit,iTabWidth,FALSE);
  2256.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2257.       break;
  2258.     case IDM_EDIT_CONVERTSPACES:
  2259.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2260.       EditSpacesToTabs(hwndEdit,iTabWidth,FALSE);
  2261.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2262.       break;
  2263.     case IDM_EDIT_CONVERTTABS2:
  2264.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2265.       EditTabsToSpaces(hwndEdit,iTabWidth,TRUE);
  2266.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2267.       break;
  2268.     case IDM_EDIT_CONVERTSPACES2:
  2269.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2270.       EditSpacesToTabs(hwndEdit,iTabWidth,TRUE);
  2271.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2272.       break;
  2273.     case IDM_EDIT_INSERT_TAG:
  2274.       {
  2275.         WCHAR wszOpen[256] = L"";
  2276.         WCHAR wszClose[256] = L"";
  2277.         if (EditInsertTagDlg(hwnd,wszOpen,wszClose))
  2278.           EditEncloseSelection(hwndEdit,wszOpen,wszClose);
  2279.       }
  2280.       break;
  2281.     case IDM_EDIT_INSERT_ENCODING:
  2282.       {
  2283.         if (*mEncoding[iEncoding].pszParseNames) {
  2284.           char msz[32];
  2285.           char *p;
  2286.           int iSelStart;
  2287.           lstrcpynA(msz,mEncoding[iEncoding].pszParseNames,COUNTOF(msz));
  2288.           if (p = StrChrA(msz,','))
  2289.             *p = 0;
  2290.           iSelStart = SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2291.           SendMessage(hwndEdit,SCI_REPLACESEL,0,(LPARAM)msz);
  2292.           SendMessage(hwndEdit,SCI_SETANCHOR,(WPARAM)iSelStart,0);
  2293.         }
  2294.       }
  2295.       break;
  2296.     case IDM_EDIT_INSERT_SHORTDATE:
  2297.     case IDM_EDIT_INSERT_LONGDATE:
  2298.       {
  2299.         WCHAR tchDate[128];
  2300.         WCHAR tchTime[128];
  2301.         WCHAR tchDateTime[256];
  2302.         WCHAR tchTemplate[256];
  2303.         SYSTEMTIME st;
  2304.         char  mszBuf[MAX_PATH*3];
  2305.         UINT  uCP;
  2306.         int   iSelStart;
  2307.         GetLocalTime(&st);
  2308.         if (IniGetString(L"Settings2",
  2309.               (LOWORD(wParam) == IDM_EDIT_INSERT_SHORTDATE) ? L"DateTimeShort" : L"DateTimeLong",
  2310.               L"",tchTemplate,COUNTOF(tchTemplate))) {
  2311.           struct tm sst;
  2312.           sst.tm_isdst       = -1;
  2313.           sst.tm_sec         = (int)st.wSecond;
  2314.           sst.tm_min         = (int)st.wMinute;
  2315.           sst.tm_hour        = (int)st.wHour;
  2316.           sst.tm_mday        = (int)st.wDay;
  2317.           sst.tm_mon         = (int)st.wMonth - 1;
  2318.           sst.tm_year        = (int)st.wYear - 1900;
  2319.           sst.tm_wday        = (int)st.wDayOfWeek;
  2320.           mktime(&sst);
  2321.           wcsftime(tchDateTime,COUNTOF(tchDateTime),tchTemplate,&sst);
  2322.         }
  2323.         else {
  2324.           GetDateFormat(LOCALE_USER_DEFAULT,(
  2325.             LOWORD(wParam) == IDM_EDIT_INSERT_SHORTDATE) ? DATE_SHORTDATE : DATE_LONGDATE,
  2326.             &st,NULL,tchDate,COUNTOF(tchDate));
  2327.           GetTimeFormat(LOCALE_USER_DEFAULT,TIME_NOSECONDS,&st,NULL,tchTime,COUNTOF(tchTime));
  2328.           wsprintf(tchDateTime,L"%s %s",tchTime,tchDate);
  2329.         }
  2330.         uCP = (SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
  2331.         WideCharToMultiByte(uCP,0,tchDateTime,-1,mszBuf,COUNTOF(mszBuf),NULL,NULL);
  2332.         iSelStart = SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2333.         SendMessage(hwndEdit,SCI_REPLACESEL,0,(LPARAM)mszBuf);
  2334.         SendMessage(hwndEdit,SCI_SETANCHOR,(WPARAM)iSelStart,0);
  2335.       }
  2336.       break;
  2337.     case IDM_EDIT_INSERT_FILENAME:
  2338.     case IDM_EDIT_INSERT_PATHNAME:
  2339.       {
  2340.         SHFILEINFO shfi;
  2341.         WCHAR *pszInsert;
  2342.         WCHAR tchUntitled[32];
  2343.         char  mszBuf[MAX_PATH*3];
  2344.         UINT  uCP;
  2345.         int   iSelStart;
  2346.         if (lstrlen(szCurFile)) {
  2347.           if (LOWORD(wParam) == IDM_EDIT_INSERT_FILENAME) {
  2348.             SHGetFileInfo2(szCurFile,0,&shfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
  2349.             pszInsert = shfi.szDisplayName;
  2350.           }
  2351.           else
  2352.             pszInsert = szCurFile;
  2353.         }
  2354.         else {
  2355.           GetString(IDS_UNTITLED,tchUntitled,COUNTOF(tchUntitled));
  2356.           pszInsert = tchUntitled;
  2357.         }
  2358.         uCP = (SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
  2359.         WideCharToMultiByte(uCP,0,pszInsert,-1,mszBuf,COUNTOF(mszBuf),NULL,NULL);
  2360.         iSelStart = SendMessage(hwndEdit,SCI_GETSELECTIONSTART,0,0);
  2361.         SendMessage(hwndEdit,SCI_REPLACESEL,0,(LPARAM)mszBuf);
  2362.         SendMessage(hwndEdit,SCI_SETANCHOR,(WPARAM)iSelStart,0);
  2363.       }
  2364.       break;
  2365.     case IDM_EDIT_LINECOMMENT:
  2366.       switch (SendMessage(hwndEdit,SCI_GETLEXER,0,0)) {
  2367.         case SCLEX_NULL:
  2368.         case SCLEX_CSS:
  2369.         case SCLEX_DIFF:
  2370.           break;
  2371.         case SCLEX_HTML:
  2372.         case SCLEX_XML:
  2373.         case SCLEX_CPP:
  2374.         case SCLEX_PASCAL:
  2375.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2376.           EditToggleLineComments(hwndEdit,L"//",FALSE);
  2377.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2378.           break;
  2379.         case SCLEX_VBSCRIPT:
  2380.         case SCLEX_VB:
  2381.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2382.           EditToggleLineComments(hwndEdit,L"'",FALSE);
  2383.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2384.           break;
  2385.         case SCLEX_MAKEFILE:
  2386.         case SCLEX_PERL:
  2387.         case SCLEX_PYTHON:
  2388.         case SCLEX_CONF:
  2389.         case SCLEX_POWERSHELL:
  2390.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2391.           EditToggleLineComments(hwndEdit,L"#",TRUE);
  2392.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2393.           break;
  2394.         case SCLEX_ASM:
  2395.         case SCLEX_PROPERTIES:
  2396.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2397.           EditToggleLineComments(hwndEdit,L";",TRUE);
  2398.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2399.           break;
  2400.         case SCLEX_SQL:
  2401.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2402.           EditToggleLineComments(hwndEdit,L"--",TRUE);
  2403.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2404.           break;
  2405.         case SCLEX_BATCH:
  2406.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2407.           EditToggleLineComments(hwndEdit,L"rem ",TRUE);
  2408.           SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2409.           break;
  2410.       }
  2411.       break;
  2412.     case IDM_EDIT_STREAMCOMMENT:
  2413.       switch (SendMessage(hwndEdit,SCI_GETLEXER,0,0)) {
  2414.         case SCLEX_NULL:
  2415.         case SCLEX_VBSCRIPT:
  2416.         case SCLEX_MAKEFILE:
  2417.         case SCLEX_VB:
  2418.         case SCLEX_ASM:
  2419.         case SCLEX_SQL:
  2420.         case SCLEX_PERL:
  2421.         case SCLEX_PYTHON:
  2422.         case SCLEX_PROPERTIES:
  2423.         case SCLEX_CONF:
  2424.         case SCLEX_POWERSHELL:
  2425.         case SCLEX_BATCH:
  2426.         case SCLEX_DIFF:
  2427.           break;
  2428.         case SCLEX_HTML:
  2429.         case SCLEX_XML:
  2430.         case SCLEX_CSS:
  2431.         case SCLEX_CPP:
  2432.           EditEncloseSelection(hwndEdit,L"/*",L"*/");
  2433.           break;
  2434.         case SCLEX_PASCAL:
  2435.           EditEncloseSelection(hwndEdit,L"{",L"}");
  2436.       }
  2437.       break;
  2438.     case IDM_EDIT_URLENCODE:
  2439.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2440.       EditURLEncode(hwndEdit);
  2441.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2442.       break;
  2443.     case IDM_EDIT_URLDECODE:
  2444.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2445.       EditURLDecode(hwndEdit);
  2446.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2447.       break;
  2448.     case IDM_EDIT_ESCAPECCHARS:
  2449.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2450.       EditEscapeCChars(hwndEdit);
  2451.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2452.       break;
  2453.     case IDM_EDIT_UNESCAPECCHARS:
  2454.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORWAIT,0);
  2455.       EditUnescapeCChars(hwndEdit);
  2456.       SendMessage(hwndEdit,SCI_SETCURSOR,SC_CURSORNORMAL,0);
  2457.       break;
  2458.     case IDM_EDIT_FINDMATCHINGBRACE:
  2459.       {
  2460.         int iBrace2 = -1;
  2461.         int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  2462.         char c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  2463.         if (StrChrA("()[]{}",c))
  2464.           iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  2465.         // Try one before
  2466.         else {
  2467.           iPos = SendMessage(hwndEdit,SCI_POSITIONBEFORE,iPos,0);
  2468.           c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  2469.           if (StrChrA("()[]{}",c))
  2470.             iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  2471.         }
  2472.         if (iBrace2 != -1)
  2473.           SendMessage(hwndEdit,SCI_GOTOPOS,(WPARAM)iBrace2,0);
  2474.       }
  2475.       break;
  2476.     case IDM_EDIT_SELTOMATCHINGBRACE:
  2477.       {
  2478.         int iBrace2 = -1;
  2479.         int iPos = SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0);
  2480.         char c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  2481.         if (StrChrA("()[]{}",c))
  2482.           iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  2483.         // Try one before
  2484.         else {
  2485.           iPos = SendMessage(hwndEdit,SCI_POSITIONBEFORE,iPos,0);
  2486.           c = (char)SendMessage(hwndEdit,SCI_GETCHARAT,iPos,0);
  2487.           if (StrChrA("()[]{}",c))
  2488.             iBrace2 = SendMessage(hwndEdit,SCI_BRACEMATCH,iPos,0);
  2489.         }
  2490.         if (iBrace2 != -1) {
  2491.           if (iBrace2 > iPos)
  2492.             SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos,(LPARAM)iBrace2+1);
  2493.           else
  2494.             SendMessage(hwndEdit,SCI_SETSEL,(WPARAM)iPos+1,(LPARAM)iBrace2);
  2495.         }
  2496.       }
  2497.       break;
  2498.     case IDM_EDIT_FIND:
  2499.       if (!IsWindow(hDlgFindReplace))
  2500.         hDlgFindReplace = EditFindReplaceDlg(hwndEdit,&efrData,FALSE);
  2501.       else {
  2502.         if (GetDlgItem(hDlgFindReplace,IDC_REPLACE)) {
  2503.           SendMessage(hDlgFindReplace,WM_COMMAND,MAKELONG(IDMSG_SWITCHTOFIND,1),0);
  2504.           DestroyWindow(hDlgFindReplace);
  2505.           hDlgFindReplace = EditFindReplaceDlg(hwndEdit,&efrData,FALSE);
  2506.         }
  2507.         else {
  2508.           SetForegroundWindow(hDlgFindReplace);
  2509.           PostMessage(hDlgFindReplace,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(hDlgFindReplace,IDC_FINDTEXT)),1);
  2510.         }
  2511.       }
  2512.       break;
  2513.     case IDM_EDIT_FINDNEXT:
  2514.     case IDM_EDIT_FINDPREV:
  2515.     case IDM_EDIT_SELTONEXT:
  2516.     case IDM_EDIT_SELTOPREV:
  2517.     case IDM_EDIT_REPLACENEXT:
  2518.       if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) == 0)
  2519.         break;
  2520.       if (!lstrlenA(efrData.szFind)) {
  2521.         if (LOWORD(wParam) != IDM_EDIT_REPLACENEXT)
  2522.           SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_FIND,1),0);
  2523.         else
  2524.           SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REPLACE,1),0);
  2525.       }
  2526.       else {
  2527.         UINT cp = SendMessage(hwndEdit,SCI_GETCODEPAGE,0,0);
  2528.         if (cpLastFind != cp) {
  2529.           if (cp != SC_CP_UTF8) {
  2530.             WCHAR wch[512];
  2531.             MultiByteToWideChar(CP_UTF8,0,efrData.szFindUTF8,-1,wch,COUNTOF(wch));
  2532.             WideCharToMultiByte(cp,0,wch,-1,efrData.szFind,COUNTOF(efrData.szFind),NULL,NULL);
  2533.             MultiByteToWideChar(CP_UTF8,0,efrData.szReplaceUTF8,-1,wch,COUNTOF(wch));
  2534.             WideCharToMultiByte(cp,0,wch,-1,efrData.szReplace,COUNTOF(efrData.szReplace),NULL,NULL);
  2535.           }
  2536.           else {
  2537.             lstrcpyA(efrData.szFind,efrData.szFindUTF8);
  2538.             lstrcpyA(efrData.szReplace,efrData.szReplaceUTF8);
  2539.           }
  2540.         }
  2541.         cpLastFind = cp;
  2542.         switch (LOWORD(wParam)) {
  2543.           case IDM_EDIT_FINDNEXT:
  2544.             EditFindNext(hwndEdit,&efrData,FALSE);
  2545.             break;
  2546.           case IDM_EDIT_FINDPREV:
  2547.             EditFindPrev(hwndEdit,&efrData,FALSE);
  2548.             break;
  2549.           case IDM_EDIT_SELTONEXT:
  2550.             EditFindNext(hwndEdit,&efrData,TRUE);
  2551.             break;
  2552.           case IDM_EDIT_SELTOPREV:
  2553.             EditFindPrev(hwndEdit,&efrData,TRUE);
  2554.             break;
  2555.           case IDM_EDIT_REPLACENEXT:
  2556.             if (bReplaceInitialized)
  2557.               EditReplace(hwndEdit,&efrData);
  2558.             else
  2559.               SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_REPLACE,1),0);
  2560.             break;
  2561.         }
  2562.       }
  2563.       break;
  2564.     case IDM_EDIT_REPLACE:
  2565.       if (!IsWindow(hDlgFindReplace))
  2566.         hDlgFindReplace = EditFindReplaceDlg(hwndEdit,&efrData,TRUE);
  2567.       else {
  2568.         if (!GetDlgItem(hDlgFindReplace,IDC_REPLACE)) {
  2569.           SendMessage(hDlgFindReplace,WM_COMMAND,MAKELONG(IDMSG_SWITCHTOREPLACE,1),0);
  2570.           DestroyWindow(hDlgFindReplace);
  2571.           hDlgFindReplace = EditFindReplaceDlg(hwndEdit,&efrData,TRUE);
  2572.         }
  2573.         else {
  2574.           SetForegroundWindow(hDlgFindReplace);
  2575.           PostMessage(hDlgFindReplace,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(hDlgFindReplace,IDC_FINDTEXT)),1);
  2576.         }
  2577.       }
  2578.       break;
  2579.     case IDM_EDIT_GOTOLINE:
  2580.       EditLinenumDlg(hwndEdit);
  2581.       break;
  2582.     case IDM_VIEW_SCHEME:
  2583.       Style_SelectLexerDlg(hwndEdit);
  2584.       UpdateStatusbar();
  2585.       UpdateLineNumberWidth();
  2586.       break;
  2587.     case IDM_VIEW_USE2NDDEFAULT:
  2588.       Style_ToggleUse2ndDefault(hwndEdit);
  2589.       UpdateStatusbar();
  2590.       UpdateLineNumberWidth();
  2591.       break;
  2592.     case IDM_VIEW_SCHEMECONFIG:
  2593.       Style_ConfigDlg(hwndEdit);
  2594.       UpdateStatusbar();
  2595.       UpdateLineNumberWidth();
  2596.       break;
  2597.     case IDM_VIEW_FONT:
  2598.       Style_SetDefaultFont(hwndEdit);
  2599.       UpdateStatusbar();
  2600.       UpdateLineNumberWidth();
  2601.       break;
  2602.     case IDM_VIEW_WORDWRAP:
  2603.       fWordWrap = (fWordWrap) ? FALSE : TRUE;
  2604.       if (!fWordWrap)
  2605.         SendMessage(hwndEdit,SCI_SETWRAPMODE,SC_WRAP_NONE,0);
  2606.       else
  2607.         SendMessage(hwndEdit,SCI_SETWRAPMODE,(iWordWrapMode == 0) ? SC_WRAP_WORD : SC_WRAP_CHAR,0);
  2608.       UpdateToolbar();
  2609.       break;
  2610.     case IDM_VIEW_WORDWRAPSETTINGS:
  2611.       if (WordWrapSettingsDlg(hwnd,IDD_WORDWRAP,&iWordWrapIndent))
  2612.       {
  2613.         if (fWordWrap)
  2614.           SendMessage(hwndEdit,SCI_SETWRAPMODE,(iWordWrapMode == 0) ? SC_WRAP_WORD : SC_WRAP_CHAR,0);
  2615.         if (iWordWrapIndent == 5)
  2616.           SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_SAME,0);
  2617.         else if (iWordWrapIndent == 6)
  2618.           SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_INDENT,0);
  2619.         else {
  2620.           int i = 0;
  2621.           switch (iWordWrapIndent) {
  2622.             case 1: i = 1; break;
  2623.             case 2: i = 2; break;
  2624.             case 3: i = (iIndentWidth) ? 1 * iIndentWidth : 1 * iTabWidth; break;
  2625.             case 4: i = (iIndentWidth) ? 2 * iIndentWidth : 2 * iTabWidth; break;
  2626.           }
  2627.           SendMessage(hwndEdit,SCI_SETWRAPSTARTINDENT,i,0);
  2628.           SendMessage(hwndEdit,SCI_SETWRAPINDENTMODE,SC_WRAPINDENT_FIXED,0);
  2629.         }
  2630.         if (bShowWordWrapSymbols) {
  2631.           int wrapVisualFlags = 0;
  2632.           int wrapVisualFlagsLocation = 0;
  2633.           if (iWordWrapSymbols == 0)
  2634.             iWordWrapSymbols = 22;
  2635.           switch (iWordWrapSymbols%10) {
  2636.             case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_END_BY_TEXT; break;
  2637.             case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; break;
  2638.           }
  2639.           switch (((iWordWrapSymbols%100)-(iWordWrapSymbols%10))/10) {
  2640.             case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_START_BY_TEXT; break;
  2641.             case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; break;
  2642.           }
  2643.           SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGSLOCATION,wrapVisualFlagsLocation,0);
  2644.           SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,wrapVisualFlags,0);
  2645.         }
  2646.         else {
  2647.           SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,0,0);
  2648.         }
  2649.       }
  2650.       break;
  2651.     case IDM_VIEW_LONGLINEMARKER:
  2652.       bMarkLongLines = (bMarkLongLines) ? FALSE: TRUE;
  2653.       if (bMarkLongLines) {
  2654.         SendMessage(hwndEdit,SCI_SETEDGEMODE,(iLongLineMode == EDGE_LINE)?EDGE_LINE:EDGE_BACKGROUND,0);
  2655.         Style_SetLongLineColors(hwndEdit);
  2656.       }
  2657.       else
  2658.         SendMessage(hwndEdit,SCI_SETEDGEMODE,EDGE_NONE,0);
  2659.       UpdateStatusbar();
  2660.       break;
  2661.     case IDM_VIEW_LONGLINESETTINGS:
  2662.       if (LongLineSettingsDlg(hwnd,IDD_LONGLINES,&iLongLinesLimit)) {
  2663.         bMarkLongLines = TRUE;
  2664.         SendMessage(hwndEdit,SCI_SETEDGEMODE,(iLongLineMode == EDGE_LINE)?EDGE_LINE:EDGE_BACKGROUND,0);
  2665.         Style_SetLongLineColors(hwndEdit);
  2666.         iLongLinesLimit = max(min(iLongLinesLimit,4096),0);
  2667.         SendMessage(hwndEdit,SCI_SETEDGECOLUMN,iLongLinesLimit,0);
  2668.         UpdateStatusbar();
  2669.         iLongLinesLimitG = iLongLinesLimit;
  2670.       }
  2671.       break;
  2672.     case IDM_VIEW_TABSASSPACES:
  2673.       bTabsAsSpaces = (bTabsAsSpaces) ? FALSE : TRUE;
  2674.       SendMessage(hwndEdit,SCI_SETUSETABS,!bTabsAsSpaces,0);
  2675.       bTabsAsSpacesG = bTabsAsSpaces;
  2676.       break;
  2677.     case IDM_VIEW_TABSETTINGS:
  2678.       if (TabSettingsDlg(hwnd,IDD_TABSETTINGS,NULL))
  2679.       {
  2680.         SendMessage(hwndEdit,SCI_SETUSETABS,!bTabsAsSpaces,0);
  2681.         SendMessage(hwndEdit,SCI_SETTABINDENTS,bTabIndents,0);
  2682.         iTabWidth = max(min(iTabWidth,256),1);
  2683.         iIndentWidth = max(min(iIndentWidth,256),0);
  2684.         SendMessage(hwndEdit,SCI_SETTABWIDTH,iTabWidth,0);
  2685.         SendMessage(hwndEdit,SCI_SETINDENT,iIndentWidth,0);
  2686.         bTabsAsSpacesG = bTabsAsSpaces;
  2687.         iTabWidthG     = iTabWidth;
  2688.         iIndentWidthG  = iIndentWidth;
  2689.         if (SendMessage(hwndEdit,SCI_GETWRAPINDENTMODE,0,0) == SC_WRAPINDENT_FIXED) {
  2690.           int i = 0;
  2691.           switch (iWordWrapIndent) {
  2692.             case 1: i = 1; break;
  2693.             case 2: i = 2; break;
  2694.             case 3: i = (iIndentWidth) ? 1 * iIndentWidth : 1 * iTabWidth; break;
  2695.             case 4: i = (iIndentWidth) ? 2 * iIndentWidth : 2 * iTabWidth; break;
  2696.           }
  2697.           SendMessage(hwndEdit,SCI_SETWRAPSTARTINDENT,i,0);
  2698.         }
  2699.       }
  2700.       break;
  2701.     case IDM_VIEW_SHOWINDENTGUIDES:
  2702.       bShowIndentGuides = (bShowIndentGuides) ? FALSE : TRUE;
  2703.       Style_SetIndentGuides(hwndEdit,bShowIndentGuides);
  2704.       break;
  2705.     case IDM_VIEW_AUTOINDENTTEXT:
  2706.       bAutoIndent = (bAutoIndent) ? FALSE : TRUE;
  2707.       break;
  2708.     case IDM_VIEW_LINENUMBERS:
  2709.       bShowLineNumbers = (bShowLineNumbers) ? FALSE : TRUE;
  2710.       UpdateLineNumberWidth();
  2711.       //SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,0,
  2712.       //  (bShowLineNumbers)?SendMessage(hwndEdit,SCI_TEXTWIDTH,STYLE_LINENUMBER,(LPARAM)"_999999_"):0);
  2713.       break;
  2714.     case IDM_VIEW_MARGIN:
  2715.       bShowSelectionMargin = (bShowSelectionMargin) ? FALSE : TRUE;
  2716.       SendMessage(hwndEdit,SCI_SETMARGINWIDTHN,1,(bShowSelectionMargin)?16:0);
  2717.       break;
  2718.     case IDM_VIEW_SHOWWHITESPACE:
  2719.       bViewWhiteSpace = (bViewWhiteSpace) ? FALSE : TRUE;
  2720.       SendMessage(hwndEdit,SCI_SETVIEWWS,(bViewWhiteSpace)?SCWS_VISIBLEALWAYS:SCWS_INVISIBLE,0);
  2721.       break;
  2722.     case IDM_VIEW_SHOWEOLS:
  2723.       bViewEOLs = (bViewEOLs) ? FALSE : TRUE;
  2724.       SendMessage(hwndEdit,SCI_SETVIEWEOL,bViewEOLs,0);
  2725.       break;
  2726.     case IDM_VIEW_WORDWRAPSYMBOLS:
  2727.       bShowWordWrapSymbols = (bShowWordWrapSymbols) ? 0 : 1;
  2728.       if (bShowWordWrapSymbols) {
  2729.         int wrapVisualFlags = 0;
  2730.         int wrapVisualFlagsLocation = 0;
  2731.         if (iWordWrapSymbols == 0)
  2732.           iWordWrapSymbols = 22;
  2733.         switch (iWordWrapSymbols%10) {
  2734.           case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_END_BY_TEXT; break;
  2735.           case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_END; break;
  2736.         }
  2737.         switch (((iWordWrapSymbols%100)-(iWordWrapSymbols%10))/10) {
  2738.           case 1: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; wrapVisualFlagsLocation |= SC_WRAPVISUALFLAGLOC_START_BY_TEXT; break;
  2739.           case 2: wrapVisualFlags |= SC_WRAPVISUALFLAG_START; break;
  2740.         }
  2741.         SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGSLOCATION,wrapVisualFlagsLocation,0);
  2742.         SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,wrapVisualFlags,0);
  2743.       }
  2744.       else {
  2745.         SendMessage(hwndEdit,SCI_SETWRAPVISUALFLAGS,0,0);
  2746.       }
  2747.       break;
  2748.     case IDM_VIEW_MATCHBRACES:
  2749.       bMatchBraces = (bMatchBraces) ? FALSE : TRUE;
  2750.       if (bMatchBraces) {
  2751.         NMHDR nmhdr;
  2752.         nmhdr.hwndFrom = hwndEdit;
  2753.         nmhdr.idFrom = IDC_EDIT;
  2754.         nmhdr.code = SCN_UPDATEUI;
  2755.         SendMessage(hwnd,WM_NOTIFY,IDC_EDIT,(LPARAM)&nmhdr);
  2756.       }
  2757.       else
  2758.         SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,(WPARAM)-1,(LPARAM)-1);
  2759.       break;
  2760.     case IDM_VIEW_AUTOCLOSETAGS:
  2761.       bAutoCloseTags = (bAutoCloseTags) ? FALSE : TRUE;
  2762.       break;
  2763.     case IDM_VIEW_HILITECURRENTLINE:
  2764.       bHiliteCurrentLine = (bHiliteCurrentLine) ? FALSE : TRUE;
  2765.       Style_SetCurrentLineBackground(hwndEdit);
  2766.       break;
  2767.     case IDM_VIEW_ZOOMIN:
  2768.       SendMessage(hwndEdit,SCI_ZOOMIN,0,0);
  2769.       //UpdateLineNumberWidth();
  2770.       break;
  2771.     case IDM_VIEW_ZOOMOUT:
  2772.       SendMessage(hwndEdit,SCI_ZOOMOUT,0,0);
  2773.       //UpdateLineNumberWidth();
  2774.       break;
  2775.     case IDM_VIEW_RESETZOOM:
  2776.       SendMessage(hwndEdit,SCI_SETZOOM,0,0);
  2777.       //UpdateLineNumberWidth();
  2778.       break;
  2779.     case IDM_VIEW_TOOLBAR:
  2780.       if (bShowToolbar) {
  2781.         bShowToolbar = 0;
  2782.         ShowWindow(hwndReBar,SW_HIDE);