Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2167 lines
69 KiB

  1. /*
  2. * Notepad application
  3. * Copyright (C) 1984-2001 Microsoft Inc.
  4. */
  5. #include "precomp.h"
  6. #include <htmlhelp.h>
  7. #define DeepTrouble() MessageBox(hwndNP, szErrSpace, szNN, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
  8. UINT lGotoLine; /* line number to goto to */
  9. TCHAR chMerge;
  10. HWND hwndNP = 0; /* handle to notepad parent window */
  11. HWND hwndStatus = 0; /* handle to notepad status window */
  12. HWND hwndEdit = 0; /* handle to main text control item */
  13. HANDLE hEdit; /* Handle to storage for edit item */
  14. HWND hDlgFind = NULL; /* handle to modeless FindText window */
  15. HANDLE hStdCursor; /* handle to arrow or beam cursor */
  16. HANDLE hWaitCursor; /* handle to hour glass cursor */
  17. HANDLE hInstanceNP; /* Module instance handle */
  18. HANDLE hFont; /* handle to Unicode font */
  19. LOGFONT FontStruct; /* font dialog structure */
  20. INT iPointSize=120; /* current point size unit=1/10 pts */
  21. TCHAR szFileName[MAX_PATH+1]; /* Current notepad filename */
  22. TCHAR szSearch[CCHKEYMAX]; /* Search string */
  23. TCHAR szReplace[CCHKEYMAX]; /* replace string */
  24. BOOL fUntitled = TRUE; /* TRUE iff notepad has no title */
  25. BOOL fStatus = FALSE; /* status bar shown? */
  26. BOOL fLastStatus = FALSE; /* status bar status when wordwrap was turned off */
  27. INT dyStatus; /* height of status bar */
  28. HMENU hSysMenuSetup; /* Save Away for disabled Minimize */
  29. DWORD dwEmSetHandle = 0; /* Is EM_SETHANDLE in process? */
  30. HANDLE hAccel; /* Handle to accelerator table */
  31. BOOL fRunBySetup = FALSE; /* Did SlipUp WinExec us?? */
  32. BOOL fWrap = 0; /* Flag for word wrap */
  33. TCHAR szNotepad[] = TEXT("Notepad");/* Name of notepad window class */
  34. BOOL fInSaveAsDlg = FALSE;
  35. // Edit control used to AV is EM_FMTLINES was turned off when cursor was near the end
  36. // To get around this, notepad moved to cursor to 0,0 when it turned off wordwrap.
  37. // Users were not happy, so we will put up with possible AVs. Note: as of June 27, 2001
  38. // we could not repro the AV behavior, so perhaps it is not there anymore.
  39. BOOL fMLE_is_broken= FALSE;
  40. /* variables for the new File/Open, File/Saveas,Find Text and Print dialogs */
  41. OPENFILENAME OFN; /* passed to the File Open/save APIs */
  42. TCHAR szOpenFilterSpec[CCHFILTERMAX]; /* default open filter spec */
  43. TCHAR szSaveFilterSpec[CCHFILTERMAX]; /* default save filter spec */
  44. NP_FILETYPE g_ftOpenedAs=FT_UNKNOWN; /* current file was opened */
  45. NP_FILETYPE g_ftSaveAs; /* current file was opened */
  46. FINDREPLACE FR; /* Passed to FindText() */
  47. PAGESETUPDLG g_PageSetupDlg;
  48. UINT wFRMsg; /* message used in communicating */
  49. /* with Find/Replace dialog */
  50. DWORD dwCurrentSelectionStart = 0L; /* WM_ACTIVATEAPP selection pos */
  51. DWORD dwCurrentSelectionEnd = 0L; /* WM_ACTIVATEAPP selection pos */
  52. UINT wHlpMsg; /* message used in invoking help */
  53. /* Strings loaded from resource file passed to LoadString at initialization time */
  54. /* To add resource string:
  55. * 1) create IDS_ macro definition in notepad.h
  56. * 2) create string in resource file
  57. * 3) create 'TCHAR*' variable directly below and in notepad.h file
  58. * 4) add &variable to rgsz
  59. * 5) increment CSTRINGS
  60. */
  61. TCHAR *szDiskError =(TCHAR *)IDS_DISKERROR; /* Can't open File, check disk */
  62. TCHAR *szFNF =(TCHAR *)IDS_FNF; /* File not found */
  63. TCHAR *szSCBC =(TCHAR *)IDS_SCBC; /* Save changes before closing? */
  64. TCHAR *szUntitled =(TCHAR *)IDS_UNTITLED; /* Untitled */
  65. TCHAR *szNpTitle =(TCHAR *)IDS_NOTEPAD; /* Notepad - */
  66. TCHAR *szCFS =(TCHAR *)IDS_CFS; /* Can't find string */
  67. TCHAR *szErrSpace =(TCHAR *)IDS_ERRSPACE; /* Memory space exhausted */
  68. TCHAR *szFTL =(TCHAR *)IDS_FTL; /* File too large for notepad */
  69. TCHAR *szNN =(TCHAR *)IDS_NN; /* Notepad name */
  70. TCHAR *szCommDlgInitErr = (TCHAR*)IDS_COMMDLGINIT; /* common dialog error %x */
  71. TCHAR *szPDIE =(TCHAR*) IDS_PRINTDLGINIT; /* Print dialog init error */
  72. TCHAR *szCP =(TCHAR*) IDS_CANTPRINT; /* Can't print */
  73. TCHAR *szNVF =(TCHAR*) IDS_NVF; /* Not a valid filename. */
  74. TCHAR *szCREATEERR =(TCHAR*) IDS_CREATEERR; /* cannot create file */
  75. TCHAR *szNoWW =(TCHAR*) IDS_NOWW; /* Too much text to word wrap */
  76. TCHAR *szMerge =(TCHAR*) IDS_MERGE1; /* search string for merge */
  77. TCHAR *szHelpFile =(TCHAR*) IDS_HELPFILE; /* Name of helpfile. */
  78. TCHAR *szHeader =(TCHAR*) IDS_HEADER;
  79. TCHAR *szFooter =(TCHAR*) IDS_FOOTER;
  80. TCHAR *szLetters =(TCHAR*) IDS_LETTERS; /* formatting letters in pagesetup */
  81. TCHAR *szAnsiText = (TCHAR*)IDS_ANSITEXT; /* File/Open ANSI filter spec. string */
  82. TCHAR *szAllFiles = (TCHAR*)IDS_ALLFILES; /* File/Open Filter spec. string */
  83. TCHAR *szOpenCaption = (TCHAR*)IDS_OPENCAPTION; /* caption for File/Open dlg */
  84. TCHAR *szSaveCaption = (TCHAR*)IDS_SAVECAPTION; /* caption for File/Save dlg */
  85. TCHAR *szCannotQuit = (TCHAR*)IDS_CANNOTQUIT; /* cannot quit during a WM_QUERYENDSESSION */
  86. TCHAR *szLoadDrvFail = (TCHAR*)IDS_LOADDRVFAIL; /* LOADDRVFAIL from PrintDlg */
  87. TCHAR *szACCESSDENY = (TCHAR*)IDS_ACCESSDENY; /* Access denied on Open */
  88. TCHAR *szErrUnicode = (TCHAR*)IDS_ERRUNICODE; /* Unicode character existence error */
  89. TCHAR *szFontTooBig = (TCHAR*)IDS_FONTTOOBIG; /* font too big or page too small */
  90. TCHAR *szCommDlgErr = (TCHAR*) IDS_COMMDLGERR; /* common dialog error %x */
  91. TCHAR *szLineError = (TCHAR*) IDS_LINEERROR; /* line number error */
  92. TCHAR *szLineTooLarge = (TCHAR*) IDS_LINETOOLARGE;/* line number out of range */
  93. TCHAR *szFtAnsi = (TCHAR*) IDS_FT_ANSI;
  94. TCHAR *szFtUnicode = (TCHAR*) IDS_FT_UNICODE;
  95. TCHAR *szFtUnicodeBe = (TCHAR*) IDS_FT_UNICODEBE;
  96. TCHAR *szFtUtf8 = (TCHAR*) IDS_FT_UTF8;
  97. TCHAR *szCurrentPage = (TCHAR*) IDS_CURRENT_PAGE;
  98. // strings for the status bar
  99. TCHAR *szLineCol = (TCHAR*) IDS_LINECOL;
  100. TCHAR *szCompressedFile = (TCHAR*) IDS_COMPRESSED_FILE;
  101. TCHAR *szEncryptedFile = (TCHAR*) IDS_ENCRYPTED_FILE;
  102. TCHAR *szHiddenFile = (TCHAR*) IDS_HIDDEN_FILE;
  103. TCHAR *szOfflineFile = (TCHAR*) IDS_OFFLINE_FILE;
  104. TCHAR *szReadOnlyFile = (TCHAR*) IDS_READONLY_FILE;
  105. TCHAR *szSystemFile = (TCHAR*) IDS_SYSTEM_FILE;
  106. TCHAR *szFile = (TCHAR*) IDS_FILE;
  107. TCHAR **rgsz[CSTRINGS] = {
  108. &szDiskError,
  109. &szFNF,
  110. &szSCBC,
  111. &szUntitled,
  112. &szErrSpace,
  113. &szCFS,
  114. &szNpTitle,
  115. &szFTL,
  116. &szNN,
  117. &szCommDlgInitErr,
  118. &szPDIE,
  119. &szCP,
  120. &szNVF,
  121. &szCREATEERR,
  122. &szNoWW,
  123. &szMerge,
  124. &szHelpFile,
  125. &szAnsiText,
  126. &szAllFiles,
  127. &szOpenCaption,
  128. &szSaveCaption,
  129. &szCannotQuit,
  130. &szLoadDrvFail,
  131. &szACCESSDENY,
  132. &szErrUnicode,
  133. &szCommDlgErr,
  134. &szFontTooBig,
  135. &szLineError,
  136. &szLineTooLarge,
  137. &szFtAnsi,
  138. &szFtUnicode,
  139. &szFtUnicodeBe,
  140. &szFtUtf8,
  141. &szCurrentPage,
  142. &szHeader,
  143. &szFooter,
  144. &szLineCol,
  145. &szCompressedFile,
  146. &szEncryptedFile,
  147. &szHiddenFile,
  148. &szOfflineFile,
  149. &szReadOnlyFile,
  150. &szSystemFile,
  151. &szFile,
  152. &szLetters,
  153. };
  154. HANDLE fp; /* file pointer */
  155. #if 0
  156. VOID DisplayFont( LOGFONT* pf )
  157. {
  158. TCHAR dbuf[100];
  159. ODS(TEXT("-----------------------\n"));
  160. wsprintf(dbuf,TEXT("lfHeight %d\n"),pf->lfHeight); ODS(dbuf);
  161. wsprintf(dbuf,TEXT("lfWidth %d\n"),pf->lfWidth ); ODS(dbuf);
  162. wsprintf(dbuf,TEXT("lfEscapement %d\n"),pf->lfEscapement); ODS(dbuf);
  163. wsprintf(dbuf,TEXT("lfOrientation %d\n"),pf->lfOrientation); ODS(dbuf);
  164. wsprintf(dbuf,TEXT("lfWeight %d\n"),pf->lfWeight); ODS(dbuf);
  165. wsprintf(dbuf,TEXT("lfItalic %d\n"),pf->lfItalic); ODS(dbuf);
  166. wsprintf(dbuf,TEXT("lfUnderLine %d\n"),pf->lfUnderline); ODS(dbuf);
  167. wsprintf(dbuf,TEXT("lfStrikeOut %d\n"),pf->lfStrikeOut); ODS(dbuf);
  168. wsprintf(dbuf,TEXT("lfCharSet %d\n"),pf->lfCharSet); ODS(dbuf);
  169. wsprintf(dbuf,TEXT("lfOutPrecision %d\n"),pf->lfOutPrecision); ODS(dbuf);
  170. wsprintf(dbuf,TEXT("lfClipPrecision %d\n"),pf->lfClipPrecision); ODS(dbuf);
  171. wsprintf(dbuf,TEXT("lfQuality %d\n"),pf->lfQuality); ODS(dbuf);
  172. wsprintf(dbuf,TEXT("lfPitchAndFamily %d\n"),pf->lfPitchAndFamily); ODS(dbuf);
  173. wsprintf(dbuf,TEXT("lfFaceName %s\n"),pf->lfFaceName); ODS(dbuf);
  174. }
  175. #endif
  176. static TCHAR szPath[MAX_PATH];
  177. void FileDragOpen(void);
  178. VOID NpResetMenu(HWND hWnd);
  179. BOOL SignalCommDlgError(VOID);
  180. VOID ReplaceSel( BOOL bView );
  181. /* FreeGlobal, frees all global memory allocated. */
  182. void NEAR PASCAL FreeGlobal()
  183. {
  184. if(g_PageSetupDlg.hDevMode)
  185. {
  186. GlobalFree(g_PageSetupDlg.hDevMode);
  187. }
  188. if(g_PageSetupDlg.hDevNames)
  189. {
  190. GlobalFree(g_PageSetupDlg.hDevNames);
  191. }
  192. g_PageSetupDlg.hDevMode= NULL; // make sure they are zero for PrintDlg
  193. g_PageSetupDlg.hDevNames= NULL;
  194. }
  195. VOID PASCAL SetPageSetupDefaults( VOID )
  196. {
  197. TCHAR szIMeasure[ 2 ];
  198. g_PageSetupDlg.lpfnPageSetupHook= PageSetupHookProc;
  199. g_PageSetupDlg.lpPageSetupTemplateName= MAKEINTRESOURCE(IDD_PAGESETUP);
  200. GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IMEASURE, szIMeasure, 2 );
  201. g_PageSetupDlg.Flags= PSD_MARGINS |
  202. PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGESETUPTEMPLATE;
  203. if (szIMeasure[ 0 ] == TEXT( '1' ))
  204. {
  205. // English measure (in thousandths of inches).
  206. g_PageSetupDlg.Flags |= PSD_INTHOUSANDTHSOFINCHES;
  207. g_PageSetupDlg.rtMargin.top = 1000;
  208. g_PageSetupDlg.rtMargin.bottom = 1000;
  209. g_PageSetupDlg.rtMargin.left = 750;
  210. g_PageSetupDlg.rtMargin.right = 750;
  211. }
  212. else
  213. {
  214. // Metric measure (in hundreths of millimeters).
  215. g_PageSetupDlg.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
  216. g_PageSetupDlg.rtMargin.top = 2500;
  217. g_PageSetupDlg.rtMargin.bottom = 2500;
  218. g_PageSetupDlg.rtMargin.left = 2000;
  219. g_PageSetupDlg.rtMargin.right = 2000;
  220. }
  221. }
  222. /* Standard window size proc */
  223. void NPSize (int cxNew, int cyNew)
  224. {
  225. /* Invalidate the edit control window so that it is redrawn with the new
  226. * margins. Needed when comming up from iconic and when doing word wrap so
  227. * the new margins are accounted for.
  228. */
  229. InvalidateRect(hwndEdit, (LPRECT)NULL, TRUE);
  230. // the height of the edit window depends on whether the status bar is
  231. // displayed.
  232. MoveWindow (hwndEdit, 0, 0, cxNew, cyNew - (fStatus?dyStatus:0), TRUE);
  233. }
  234. // NpSaveDialogHookProc
  235. //
  236. // Common dialog hook procedure for handling
  237. // the file type while saving.
  238. //
  239. const DWORD s_SaveAsHelpIDs[]=
  240. {
  241. IDC_FILETYPE, IDH_FILETYPE,
  242. IDC_ENCODING, IDH_FILETYPE,
  243. 0, 0
  244. };
  245. UINT_PTR APIENTRY NpSaveDialogHookProc(
  246. HWND hWnd,
  247. UINT msg,
  248. WPARAM wParam,
  249. LPARAM lParam)
  250. {
  251. INT id;
  252. POINT pt;
  253. TCHAR* szSelect; // selected type
  254. switch( msg )
  255. {
  256. case WM_INITDIALOG:
  257. // Warning: the order here must be the same as NP_FILETYPE
  258. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtAnsi );
  259. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicode );
  260. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicodeBe );
  261. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUtf8 );
  262. szSelect= szFtAnsi; // default
  263. g_ftSaveAs= g_ftOpenedAs; // default: save as same type as opened
  264. switch( g_ftSaveAs )
  265. {
  266. case FT_UNICODE: szSelect= szFtUnicode; break;
  267. case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
  268. case FT_UTF8: szSelect= szFtUtf8; break;
  269. default: break;
  270. }
  271. SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
  272. break;
  273. case WM_COMMAND:
  274. g_ftSaveAs= (NP_FILETYPE) SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_GETCURSEL, 0, 0 );
  275. break;
  276. case WM_HELP:
  277. //
  278. // We only want to intercept help messages for controls that we are
  279. // responsible for.
  280. //
  281. id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
  282. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  283. break;
  284. WinHelp(((LPHELPINFO) lParam)-> hItemHandle,
  285. szHelpFile,
  286. HELP_WM_HELP,
  287. (ULONG_PTR) s_SaveAsHelpIDs);
  288. return TRUE;
  289. case WM_CONTEXTMENU:
  290. //
  291. // If the user clicks on any of our labels, then the wParam will
  292. // be the hwnd of the dialog, not the static control. WinHelp()
  293. // handles this, but because we hook the dialog, we must catch it
  294. // first.
  295. //
  296. if( hWnd == (HWND) wParam )
  297. {
  298. GetCursorPos(&pt);
  299. ScreenToClient(hWnd, &pt);
  300. wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
  301. }
  302. //
  303. // We only want to intercept help messages for controls that we are
  304. // responsible for.
  305. //
  306. id = GetDlgCtrlID((HWND) wParam);
  307. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  308. break;
  309. WinHelp( (HWND) wParam,
  310. szHelpFile,
  311. HELP_CONTEXTMENU,
  312. (ULONG_PTR) s_SaveAsHelpIDs);
  313. return TRUE;
  314. }
  315. return( FALSE );
  316. }
  317. // NpOpenDialogHookProc
  318. //
  319. // Common dialog hook procedure for handling
  320. // the file type while opening.
  321. //
  322. UINT_PTR APIENTRY NpOpenDialogHookProc(
  323. HWND hWnd,
  324. UINT msg,
  325. WPARAM wParam,
  326. LPARAM lParam)
  327. {
  328. INT id;
  329. POINT pt;
  330. TCHAR* szSelect; // selected type
  331. static TCHAR szPrevFileName[MAX_PATH] = TEXT("");
  332. switch( msg )
  333. {
  334. case WM_INITDIALOG:
  335. // Warning: the order here must be the same as NP_FILETYPE
  336. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtAnsi );
  337. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicode );
  338. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicodeBe );
  339. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUtf8 );
  340. szSelect= szFtAnsi; // default
  341. switch( g_ftOpenedAs )
  342. {
  343. case FT_UNICODE: szSelect= szFtUnicode; break;
  344. case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
  345. case FT_UTF8: szSelect= szFtUtf8; break;
  346. default: break;
  347. }
  348. // set the current filetype.
  349. SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
  350. break;
  351. case WM_COMMAND:
  352. g_ftOpenedAs= (NP_FILETYPE) SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_GETCURSEL, 0, 0 );
  353. break;
  354. case WM_HELP:
  355. //
  356. // We only want to intercept help messages for controls that we are
  357. // responsible for.
  358. //
  359. id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
  360. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  361. break;
  362. WinHelp(((LPHELPINFO) lParam)-> hItemHandle,
  363. szHelpFile,
  364. HELP_WM_HELP,
  365. (ULONG_PTR) s_SaveAsHelpIDs);
  366. return TRUE;
  367. case WM_CONTEXTMENU:
  368. //
  369. // If the user clicks on any of our labels, then the wParam will
  370. // be the hwnd of the dialog, not the static control. WinHelp()
  371. // handles this, but because we hook the dialog, we must catch it
  372. // first.
  373. //
  374. if( hWnd == (HWND) wParam )
  375. {
  376. GetCursorPos(&pt);
  377. ScreenToClient(hWnd, &pt);
  378. wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
  379. }
  380. //
  381. // We only want to intercept help messages for controls that we are
  382. // responsible for.
  383. //
  384. id = GetDlgCtrlID((HWND) wParam);
  385. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  386. break;
  387. WinHelp( (HWND) wParam,
  388. szHelpFile,
  389. HELP_CONTEXTMENU,
  390. (ULONG_PTR) s_SaveAsHelpIDs);
  391. return TRUE;
  392. case WM_NOTIFY:
  393. {
  394. LPOFNOTIFY pofn;
  395. TCHAR szFileName[MAX_PATH];
  396. BYTE szFileBuffer[BUFFER_TEST_SIZE];
  397. HANDLE hFile;
  398. DWORD dwBytesRead;
  399. // process the message when the file selection changes.
  400. pofn = (LPOFNOTIFY)lParam;
  401. switch (pofn->hdr.code)
  402. {
  403. case CDN_SELCHANGE:
  404. {
  405. // get the filename.
  406. if (CommDlg_OpenSave_GetFilePath(GetParent(hWnd), szFileName, sizeof(szFileName)/sizeof(TCHAR)) > 0)
  407. {
  408. // if same file as the previous file, don't do anything.
  409. if (lstrcmpi(szFileName, szPrevFileName) == 0)
  410. break;
  411. // open the file.
  412. hFile = CreateFile(szFileName,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  413. if (hFile != INVALID_HANDLE_VALUE)
  414. {
  415. // if the file read fails, just quit.
  416. if ( (ReadFile(hFile, szFileBuffer, BUFFER_TEST_SIZE, &dwBytesRead, NULL) <= 0) || dwBytesRead == 0)
  417. {
  418. CloseHandle(hFile);
  419. break;
  420. }
  421. // determine the file type based on dwBytesRead bytes of the file.
  422. g_ftOpenedAs = fDetermineFileType(szFileBuffer, dwBytesRead);
  423. // set the dropdown filetype to the filetype we think based on the initial part of the file.
  424. szSelect = szFtAnsi; // default
  425. switch( g_ftOpenedAs )
  426. {
  427. case FT_UNICODE: szSelect= szFtUnicode; break;
  428. case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
  429. case FT_UTF8: szSelect= szFtUtf8; break;
  430. default: break;
  431. }
  432. SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
  433. // cleanup.
  434. lstrcpy(szPrevFileName, szFileName);
  435. CloseHandle(hFile);
  436. }
  437. }
  438. break;
  439. }
  440. }
  441. }
  442. }
  443. return( FALSE );
  444. }
  445. // GotoAndScrollInView
  446. //
  447. // Put the cursor at the begining of a line, and scroll the
  448. // editbox so the user can see it.
  449. //
  450. // If there is a failure, it just leaves the cursor where it is.
  451. //
  452. VOID GotoAndScrollInView( INT OneBasedLineNumber )
  453. {
  454. UINT CharIndex;
  455. CharIndex= (UINT) SendMessage( hwndEdit,
  456. EM_LINEINDEX,
  457. OneBasedLineNumber-1,
  458. 0 );
  459. if( CharIndex != (UINT) -1 )
  460. {
  461. SendMessage( hwndEdit, EM_SETSEL, CharIndex, CharIndex);
  462. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
  463. }
  464. }
  465. /* ** Notepad command proc - called whenever notepad gets WM_COMMAND
  466. message. wParam passed as cmd */
  467. INT NPCommand(
  468. HWND hwnd,
  469. WPARAM wParam,
  470. LPARAM lParam )
  471. {
  472. HWND hwndFocus;
  473. LONG lSel;
  474. TCHAR szNewName[MAX_PATH] = TEXT(""); /* New file name */
  475. FARPROC lpfn;
  476. LONG style;
  477. DWORD rc;
  478. RECT rcClient;
  479. switch (LOWORD(wParam))
  480. {
  481. case M_EXIT:
  482. PostMessage(hwnd, WM_CLOSE, 0, 0L);
  483. break;
  484. case M_NEW:
  485. New(TRUE);
  486. break;
  487. case M_OPEN:
  488. if (CheckSave(FALSE))
  489. {
  490. NP_FILETYPE g_ftOldOpenedAs = g_ftOpenedAs;
  491. /* set up the variable fields of the OPENFILENAME struct.
  492. * (the constant fields have been set in NPInit()
  493. */
  494. OFN.lpstrFile = szNewName;
  495. lstrcpy(szNewName, TEXT("*.txt") ); /* set default selection */
  496. OFN.lpstrTitle = szOpenCaption;
  497. /* ALL non-zero long pointers must be defined immediately
  498. * before the call, as the DS might move otherwise.
  499. * 12 February 1991 clarkc
  500. */
  501. OFN.lpstrFilter = szOpenFilterSpec;
  502. OFN.lpstrDefExt = TEXT("txt");
  503. /* Added OFN_FILEMUSTEXIST to eliminate problems in LoadFile.
  504. * 12 February 1991 clarkc
  505. */
  506. OFN.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
  507. OFN_EXPLORER |
  508. OFN_ENABLESIZING |
  509. OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
  510. OFN.nFilterIndex = FILE_TEXT;
  511. // show encoding listbox
  512. OFN.lpTemplateName= TEXT("NpEncodingDialog");
  513. OFN.lpfnHook= NpOpenDialogHookProc;
  514. if( GetOpenFileName( (LPOPENFILENAME)&OFN ) )
  515. {
  516. HANDLE oldfp= fp;
  517. fp= CreateFile( szNewName, // filename
  518. GENERIC_READ, // access mode
  519. FILE_SHARE_READ|FILE_SHARE_WRITE,
  520. NULL, // security descriptor
  521. OPEN_EXISTING, // how to create
  522. FILE_ATTRIBUTE_NORMAL,// file attributes
  523. NULL); // hnd to file attrs
  524. /* Try to load the file and reset fp if failed */
  525. if( !LoadFile( szNewName, g_ftOpenedAs ) )
  526. {
  527. fp= oldfp;
  528. g_ftOpenedAs = g_ftOldOpenedAs;
  529. }
  530. }
  531. else
  532. {
  533. g_ftOpenedAs = g_ftOldOpenedAs;
  534. SignalCommDlgError();
  535. }
  536. }
  537. break;
  538. case M_SAVE:
  539. /* set up the variable fields of the OPENFILENAME struct.
  540. * (the constant fields have been sel in NPInit()
  541. */
  542. g_ftSaveAs = g_ftOpenedAs;
  543. if( !fUntitled && SaveFile( hwndNP, szFileName, FALSE ) )
  544. break;
  545. /* fall through */
  546. case M_SAVEAS:
  547. OFN.lpstrFile = szNewName;
  548. OFN.lpstrTitle = szSaveCaption;
  549. /* Added OFN_PATHMUSTEXIST to eliminate problems in SaveFile.
  550. * 12 February 1991 clarkc
  551. */
  552. OFN.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
  553. OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST |
  554. OFN_EXPLORER |
  555. OFN_ENABLESIZING |
  556. OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
  557. OFN.lpTemplateName= TEXT("NpEncodingDialog");
  558. OFN.lpfnHook= NpSaveDialogHookProc;
  559. /* ALL non-zero long pointers must be defined immediately
  560. * before the call, as the DS might move otherwise.
  561. * 12 February 1991 clarkc
  562. */
  563. OFN.lpstrFilter = szSaveFilterSpec;
  564. OFN.lpstrDefExt = TEXT("txt");
  565. if (!fUntitled)
  566. {
  567. lstrcpyn(szNewName, szFileName, MAX_PATH); /* set default selection */
  568. }
  569. else
  570. {
  571. lstrcpy (szNewName, TEXT("*.txt") );
  572. }
  573. fInSaveAsDlg = TRUE;
  574. OFN.nFilterIndex= FILE_TEXT;
  575. //
  576. // Do common dialog to save file
  577. //
  578. if (GetSaveFileName(&OFN))
  579. {
  580. if( SaveFile(hwnd, szNewName, TRUE) )
  581. {
  582. lstrcpyn( szFileName, szNewName, MAX_PATH);
  583. g_ftOpenedAs= g_ftSaveAs;
  584. }
  585. }
  586. else
  587. {
  588. SignalCommDlgError();
  589. }
  590. fInSaveAsDlg = FALSE;
  591. break;
  592. case M_SELECTALL:
  593. {
  594. HMENU hMenu;
  595. hMenu = GetMenu(hwndNP);
  596. lSel = (LONG) SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, 0L);
  597. SendMessage (hwndEdit, EM_SETSEL, 0, lSel );
  598. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  599. EnableMenuItem(GetSubMenu(hMenu, 1), M_SELECTALL, MF_GRAYED);
  600. break;
  601. }
  602. case M_REPLACE:
  603. if( hDlgFind )
  604. {
  605. SetFocus( hDlgFind );
  606. }
  607. else
  608. {
  609. FR.Flags= FR_HIDEWHOLEWORD | FR_REPLACE;
  610. FR.lpstrReplaceWith= szReplace;
  611. FR.wReplaceWithLen= CCHKEYMAX;
  612. FR.lpstrFindWhat = szSearch;
  613. FR.wFindWhatLen = CCHKEYMAX;
  614. hDlgFind = ReplaceText( &FR );
  615. }
  616. break;
  617. case M_FINDNEXT:
  618. if (szSearch[0])
  619. {
  620. Search(szSearch);
  621. break;
  622. }
  623. /* else fall thro' a,d bring up "find" dialog */
  624. case M_FIND:
  625. if (hDlgFind)
  626. {
  627. SetFocus(hDlgFind);
  628. }
  629. else
  630. {
  631. FR.Flags= FR_DOWN | FR_HIDEWHOLEWORD;
  632. FR.lpstrReplaceWith= NULL;
  633. FR.wReplaceWithLen= 0;
  634. FR.lpstrFindWhat = szSearch;
  635. FR.wFindWhatLen = CCHKEYMAX;
  636. hDlgFind = FindText((LPFINDREPLACE)&FR);
  637. }
  638. break;
  639. case M_GOTO:
  640. {
  641. INT Result;
  642. Result= (INT)DialogBox( hInstanceNP,
  643. MAKEINTRESOURCE(IDD_GOTODIALOG),
  644. hwndNP,
  645. GotoDlgProc );
  646. //
  647. // move cursor only if ok pressed and line number ok
  648. //
  649. if( Result == 0 )
  650. {
  651. GotoAndScrollInView( lGotoLine );
  652. }
  653. }
  654. break;
  655. case M_ABOUT:
  656. ShellAbout(hwndNP,
  657. szNN,
  658. TEXT(""),
  659. LoadIcon(hInstanceNP,
  660. (LPTSTR)MAKEINTRESOURCE(ID_ICON)));
  661. break;
  662. case M_HELP:
  663. HtmlHelpA(GetDesktopWindow(), "notepad.chm", HH_DISPLAY_TOPIC, 0L);
  664. break;
  665. case M_CUT:
  666. case M_COPY:
  667. case M_CLEAR:
  668. lSel = (LONG)SendMessage (hwndEdit, EM_GETSEL, 0, 0L);
  669. if (LOWORD(lSel) == HIWORD(lSel))
  670. break;
  671. case M_PASTE:
  672. /* If notepad parent or edit window has the focus,
  673. pass command to edit window.
  674. make sure line resulting from paste will not be too long. */
  675. hwndFocus = GetFocus();
  676. if (hwndFocus == hwndEdit || hwndFocus == hwndNP)
  677. {
  678. PostMessage(hwndEdit, LOWORD(wParam), 0, 0);
  679. }
  680. break;
  681. case M_DATETIME:
  682. InsertDateTime(FALSE);
  683. break;
  684. case M_UNDO:
  685. SendMessage (hwndEdit, EM_UNDO, 0, 0L);
  686. break;
  687. case M_WW:
  688. style= (!fWrap) ? ES_STD : (ES_STD | WS_HSCROLL);
  689. if( NpReCreate( style ) )
  690. {
  691. fWrap= !fWrap;
  692. }
  693. else
  694. {
  695. MessageBox(hwndNP, szNoWW, szNN,
  696. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  697. }
  698. // disable the status bar
  699. // Line numbers when wordwrap is on are very confusing for now. Just turn them
  700. // off until we better understand what the user wants to see.
  701. if (fWrap)
  702. {
  703. HMENU hMenu;
  704. // Uncheck the StatusBar and remove it.
  705. fLastStatus= fStatus; // remember for when wordwrap gets turned off
  706. if( fStatus )
  707. {
  708. SendMessage(hwnd, WM_COMMAND, M_STATUSBAR, 0L);
  709. }
  710. hMenu = GetMenu(hwndNP);
  711. CheckMenuItem (GetSubMenu(hMenu, 3), M_STATUSBAR, MF_UNCHECKED);
  712. EnableMenuItem(GetSubMenu(hMenu, 3), M_STATUSBAR, MF_GRAYED);
  713. }
  714. // enable the status bar
  715. else
  716. {
  717. HMENU hMenu;
  718. hMenu = GetMenu(hwndNP);
  719. EnableMenuItem(GetSubMenu(hMenu, 3), M_STATUSBAR, MF_ENABLED);
  720. // change the statusbar status to what it was before wordwrap was turned on
  721. if( fLastStatus )
  722. {
  723. SendMessage( hwnd, WM_COMMAND, M_STATUSBAR, 0L);
  724. }
  725. }
  726. break;
  727. case M_STATUSBAR:
  728. // hide/show the statusbar and also redraw the edit window accordingly.
  729. GetClientRect(hwndNP, &rcClient);
  730. if ( fStatus )
  731. {
  732. fStatus = FALSE;
  733. ShowWindow ( hwndStatus, SW_HIDE );
  734. NPSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
  735. }
  736. else
  737. {
  738. fStatus = TRUE;
  739. NPSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
  740. UpdateStatusBar( TRUE );
  741. ShowWindow( hwndStatus, SW_SHOW );
  742. }
  743. break;
  744. case ID_EDIT:
  745. break;
  746. case M_PRINT:
  747. PrintIt( UseDialog );
  748. break;
  749. case M_PAGESETUP:
  750. TryPrintDlgAgain:
  751. if( PageSetupDlg(&g_PageSetupDlg) )
  752. {
  753. // We know it's okay to copy these strings over...
  754. lstrcpy(chPageText[HEADER], chPageTextTemp[HEADER]);
  755. lstrcpy(chPageText[FOOTER], chPageTextTemp[FOOTER]);
  756. }
  757. else
  758. {
  759. rc= CommDlgExtendedError();
  760. if( rc == PDERR_PRINTERNOTFOUND ||
  761. rc == PDERR_DNDMMISMATCH ||
  762. rc == PDERR_DEFAULTDIFFERENT )
  763. {
  764. FreeGlobal();
  765. g_PageSetupDlg.hDevMode= g_PageSetupDlg.hDevNames= 0;
  766. goto TryPrintDlgAgain;
  767. }
  768. // Check for Dialog Failure
  769. SignalCommDlgError( );
  770. }
  771. break;
  772. case M_SETFONT:
  773. {
  774. CHOOSEFONT cf;
  775. HFONT hFontNew;
  776. HDC hDisplayDC; // display DC
  777. hDisplayDC= GetDC(NULL); // try to get display DC
  778. if( !hDisplayDC )
  779. break;
  780. // calls the font chooser (in commdlg)
  781. // We set lfHeight; choosefont returns ipointsize
  782. //
  783. cf.lStructSize = sizeof(CHOOSEFONT);
  784. cf.hwndOwner = hwnd;
  785. cf.lpLogFont = &FontStruct; // filled in by init
  786. FontStruct.lfHeight= -MulDiv(iPointSize,GetDeviceCaps(hDisplayDC,LOGPIXELSY),720);
  787. // We filter out useless stuff here
  788. // We tried CF_NOSCRIPTSEL, but the FE had fits.
  789. // Even though it looks useless, it changes the font that gets mapped on FE builds.
  790. // Even though we ignore the lfCharSet that gets returned, we have the "right"
  791. // font according to the FE guys. It might make sense to use lfCharSet to
  792. // convert the ansi file when it is converted to Unicode, but this might be
  793. // confusing.
  794. cf.Flags = CF_INITTOLOGFONTSTRUCT |
  795. CF_SCREENFONTS |
  796. CF_NOVERTFONTS |
  797. // CF_NOSCRIPTSEL | // windows bug# 7770 (April 10,2001)
  798. 0;
  799. cf.rgbColors = 0; // only if cf_effects
  800. cf.lCustData = 0; // for hook function
  801. cf.lpfnHook = (LPCFHOOKPROC) NULL;
  802. cf.lpTemplateName = (LPTSTR) NULL;
  803. cf.hInstance = NULL;
  804. cf.lpszStyle = NULL; // iff cf_usestyle
  805. cf.nFontType = SCREEN_FONTTYPE;
  806. cf.nSizeMin = 0; // iff cf_limitsize
  807. cf.nSizeMax = 0; // iff cf_limitsize
  808. ReleaseDC( NULL, hDisplayDC );
  809. if( ChooseFont(&cf) )
  810. {
  811. SetCursor( hWaitCursor ); // may take some time
  812. hFontNew= CreateFontIndirect(&FontStruct);
  813. if( hFontNew )
  814. {
  815. DeleteObject( hFont );
  816. hFont= hFontNew;
  817. SendMessage( hwndEdit, WM_SETFONT,
  818. (WPARAM)hFont, MAKELPARAM(TRUE, 0));
  819. iPointSize= cf.iPointSize; // remember for printer
  820. }
  821. SetCursor( hStdCursor );
  822. }
  823. break;
  824. }
  825. default:
  826. return FALSE;
  827. }
  828. return TRUE;
  829. }
  830. // for some reason, this procedure tries to maintain
  831. // a valid 'fp' even though I believe it does not need
  832. // to be.
  833. void FileDragOpen(void)
  834. {
  835. HANDLE oldfp;
  836. oldfp= fp; // remember in case of error
  837. if( CheckSave(FALSE) )
  838. {
  839. fp= CreateFile( szPath, // filename
  840. GENERIC_READ, // access mode
  841. FILE_SHARE_READ|FILE_SHARE_WRITE,
  842. NULL, // security descriptor
  843. OPEN_EXISTING, // how to create
  844. FILE_ATTRIBUTE_NORMAL,// file attributes
  845. NULL); // hnd to file attrs
  846. if( fp == INVALID_HANDLE_VALUE )
  847. {
  848. AlertUser_FileFail( szPath );
  849. // Restore fp to original file.
  850. fp= oldfp;
  851. }
  852. /* Try to load the file and reset fp if failed */
  853. else if( !LoadFile( szPath, FT_UNKNOWN ) )
  854. {
  855. fp= oldfp;
  856. }
  857. }
  858. }
  859. /* Proccess file drop/drag options. */
  860. void doDrop (WPARAM wParam, HWND hwnd)
  861. {
  862. /* If user dragged/dropped a file regardless of keys pressed
  863. * at the time, open the first selected file from file manager. */
  864. if (DragQueryFile ((HANDLE)wParam, 0xFFFFFFFF, NULL, 0)) /* # of files dropped */
  865. {
  866. DragQueryFile ((HANDLE)wParam, 0, szPath, CharSizeOf(szPath));
  867. SetActiveWindow (hwnd);
  868. FileDragOpen();
  869. }
  870. DragFinish ((HANDLE)wParam); /* Delete structure alocated for WM_DROPFILES*/
  871. }
  872. /* ** if notepad is dirty, check to see if user wants to save contents */
  873. BOOL FAR CheckSave (BOOL fSysModal)
  874. {
  875. INT mdResult = IDOK;
  876. TCHAR szNewName[MAX_PATH] = TEXT(""); /* New file name */
  877. TCHAR *pszFileName;
  878. /* If it's untitled and there's no text, don't worry about it */
  879. if (fUntitled && !SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, (LPARAM)0))
  880. return (TRUE);
  881. if (SendMessage (hwndEdit, EM_GETMODIFY, 0, 0L))
  882. {
  883. if( fUntitled )
  884. pszFileName= szUntitled;
  885. else
  886. pszFileName= szFileName;
  887. // put up message box
  888. fInSaveAsDlg= TRUE; // inform wm_queryendsession that we are trying to save
  889. mdResult= AlertBox( hwndNP, szNN, szSCBC, pszFileName,
  890. (WORD)((fSysModal ? MB_SYSTEMMODAL :
  891. MB_APPLMODAL)|MB_YESNOCANCEL|MB_ICONEXCLAMATION));
  892. fInSaveAsDlg= FALSE;
  893. if( mdResult == IDYES )
  894. {
  895. if( fUntitled )
  896. {
  897. lstrcpy( szNewName, TEXT("*.txt") );
  898. SaveFilePrompt:
  899. OFN.lpstrFile = szNewName;
  900. OFN.lpstrTitle = szSaveCaption;
  901. /* Added OFN_PATHMUSTEXIST to eliminate problems in SaveFile.
  902. * 12 February 1991 clarkc
  903. */
  904. OFN.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
  905. OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST |
  906. OFN_EXPLORER |
  907. OFN_ENABLESIZING |
  908. OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
  909. OFN.lpTemplateName= TEXT("NpEncodingDialog");
  910. OFN.lpfnHook= NpSaveDialogHookProc;
  911. /* ALL non-zero long pointers must be defined immediately
  912. * before the call, as the DS might move otherwise.
  913. * 12 February 1991 clarkc
  914. */
  915. OFN.lpstrFilter = szSaveFilterSpec;
  916. OFN.lpstrDefExt = TEXT("txt");
  917. OFN.nFilterIndex= FILE_TEXT;
  918. //
  919. // Set dialog checkmark by current file type
  920. //
  921. fInSaveAsDlg = TRUE;
  922. if (GetSaveFileName(&OFN))
  923. {
  924. // since SaveFile() uses near ptr to name (obs.)
  925. lstrcpy(szNewName, OFN.lpstrFile);
  926. if( SaveFile(hwndNP, szNewName, TRUE) )
  927. {
  928. lstrcpy(szFileName, szNewName);
  929. g_ftOpenedAs= g_ftSaveAs;
  930. }
  931. else
  932. { // Fixing close without saving file when disk-full
  933. lstrcpy(szNewName, szFileName);
  934. goto SaveFilePrompt;
  935. }
  936. }
  937. else
  938. {
  939. mdResult= IDCANCEL; /* Don't exit Program */
  940. if( CommDlgExtendedError() )/* Dialog box failed, Lo-mem*/
  941. DeepTrouble();
  942. }
  943. fInSaveAsDlg = FALSE;
  944. }
  945. else
  946. {
  947. // initialize the save type.
  948. g_ftSaveAs = g_ftOpenedAs;
  949. if( SaveFile(hwndNP, szFileName, FALSE) )
  950. return(TRUE);
  951. lstrcpy(szNewName, szFileName);
  952. goto SaveFilePrompt;
  953. }
  954. }
  955. }
  956. return (mdResult != IDCANCEL);
  957. }
  958. /* Notepad window class procedure */
  959. LRESULT FAR NPWndProc(
  960. HWND hwnd,
  961. UINT message,
  962. WPARAM wParam,
  963. LPARAM lParam)
  964. {
  965. RECT rc;
  966. LPFINDREPLACE lpfr;
  967. DWORD dwFlags;
  968. HANDLE hMenu;
  969. INT iParts[2];
  970. switch (message)
  971. {
  972. /* If we're being run by Setup and it's the system menu, be certain that
  973. * the minimize menu item is disabled. Note that hSysMenuSetup is only
  974. * initialized if Notepad is being run by Setup. Don't use it outside
  975. * the fRunBySetup conditional! 28 June 1991 Clark Cyr
  976. */
  977. case WM_INITMENUPOPUP:
  978. if (fRunBySetup && HIWORD(lParam))
  979. EnableMenuItem(hSysMenuSetup,SC_MINIMIZE,MF_GRAYED|MF_DISABLED);
  980. break;
  981. case WM_SYSCOMMAND:
  982. if (fRunBySetup)
  983. {
  984. /* If we have been spawned by SlipUp we need to make sure the
  985. * user doesn't minimize us or alt tab/esc away.
  986. */
  987. if (wParam == SC_MINIMIZE ||
  988. wParam == SC_NEXTWINDOW ||
  989. wParam == SC_PREVWINDOW)
  990. break;
  991. }
  992. DefWindowProc(hwnd, message, wParam, lParam);
  993. break;
  994. case WM_SETFOCUS:
  995. if (!IsIconic(hwndNP))
  996. {
  997. SetFocus(hwndEdit);
  998. }
  999. break;
  1000. case WM_KILLFOCUS:
  1001. SendMessage (hwndEdit, message, wParam, lParam);
  1002. break;
  1003. case WM_DESTROY:
  1004. PostQuitMessage(0);
  1005. break;
  1006. case WM_CLOSE:
  1007. // Save any globals in the registry if need be
  1008. SaveGlobals();
  1009. if (CheckSave(FALSE))
  1010. {
  1011. /* Exit help */
  1012. if(!WinHelp(hwndNP, (LPTSTR)szHelpFile, HELP_QUIT, 0))
  1013. DeepTrouble();
  1014. DestroyWindow(hwndStatus);
  1015. DestroyWindow(hwndNP);
  1016. DeleteObject(hFont);
  1017. }
  1018. break;
  1019. case WM_QUERYENDSESSION:
  1020. if (fInSaveAsDlg)
  1021. {
  1022. MessageBeep (0);
  1023. MessageBeep (0);
  1024. MessageBox (hwndNP, szCannotQuit, szNN, MB_OK|MB_SYSTEMMODAL);
  1025. return FALSE;
  1026. }
  1027. else
  1028. return (CheckSave(TRUE));
  1029. break;
  1030. case WM_ACTIVATEAPP:
  1031. if (wParam)
  1032. {
  1033. /* This causes the caret position to be at the end of the selection
  1034. * but there's no way to ask where it was or set it if known. This
  1035. * will cause a caret change when the selection is made from bottom
  1036. * to top.
  1037. */
  1038. if( dwCurrentSelectionStart != 0 || dwCurrentSelectionEnd != 0 )
  1039. {
  1040. SendMessage( hwndEdit, EM_SETSEL,
  1041. dwCurrentSelectionStart,
  1042. dwCurrentSelectionEnd );
  1043. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
  1044. }
  1045. }
  1046. else
  1047. {
  1048. SendMessage( hwndEdit, EM_GETSEL,
  1049. (WPARAM) &dwCurrentSelectionStart,
  1050. (LPARAM) &dwCurrentSelectionEnd);
  1051. if (dwCurrentSelectionStart == dwCurrentSelectionEnd)
  1052. {
  1053. dwCurrentSelectionStart = 0L;
  1054. dwCurrentSelectionEnd = 0L;
  1055. }
  1056. else
  1057. {
  1058. SendMessage (hwndEdit, EM_SETSEL, dwCurrentSelectionStart,
  1059. dwCurrentSelectionEnd);
  1060. SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0);
  1061. }
  1062. }
  1063. break;
  1064. case WM_ACTIVATE:
  1065. if ((LOWORD(wParam) == WA_ACTIVE ||
  1066. LOWORD(wParam) == WA_CLICKACTIVE) &&
  1067. !IsIconic(hwndNP)
  1068. )
  1069. {
  1070. // active doesn't always mean foreground (ntbug# 53048)
  1071. if( GetForegroundWindow() == hwndNP )
  1072. {
  1073. SetFocus(GetForegroundWindow());
  1074. }
  1075. }
  1076. break;
  1077. case WM_SIZE:
  1078. switch (wParam)
  1079. {
  1080. case SIZENORMAL:
  1081. case SIZEFULLSCREEN:
  1082. // resize the status window.
  1083. SendMessage (hwndStatus, WM_SIZE, 0, 0L);
  1084. iParts[0] = 3 * (MAKEPOINTS(lParam).x)/4;
  1085. iParts[1] = -1;
  1086. // Divide the status window into two parts
  1087. SendMessage(hwndStatus, SB_SETPARTS, (WPARAM) sizeof(iParts)/sizeof(INT), (LPARAM) &iParts);
  1088. NPSize(MAKEPOINTS(lParam).x, MAKEPOINTS(lParam).y);
  1089. break;
  1090. case SIZEICONIC:
  1091. return (DefWindowProc(hwnd, message, wParam, lParam));
  1092. break;
  1093. }
  1094. break;
  1095. case WM_INITMENU:
  1096. NpResetMenu( hwnd );
  1097. break;
  1098. //
  1099. // Some keyboards come with a "Search" button which the shell team
  1100. // wanted us to handle. See ntbug# 380067
  1101. //
  1102. case WM_APPCOMMAND:
  1103. if( ( GET_APPCOMMAND_LPARAM(lParam) == APPCOMMAND_BROWSER_SEARCH ) )
  1104. {
  1105. NPCommand(hwnd, M_FIND, 0);
  1106. break;
  1107. }
  1108. // otherwise fall through
  1109. case WM_COMMAND:
  1110. if ((HWND)(lParam) == hwndEdit &&
  1111. (HIWORD(wParam) == EN_ERRSPACE ||
  1112. HIWORD(wParam) == EN_MAXTEXT))
  1113. {
  1114. if (dwEmSetHandle == SETHANDLEINPROGRESS)
  1115. dwEmSetHandle = SETHANDLEFAILED;
  1116. else
  1117. DeepTrouble();
  1118. return 0L;
  1119. }
  1120. if (!NPCommand(hwnd, wParam, lParam))
  1121. return (DefWindowProc(hwnd, message, wParam, lParam));
  1122. break;
  1123. case WM_WININICHANGE:
  1124. // Ignore for now.
  1125. // If you put this back in, be sure it handles both
  1126. // the metric change and the decimal change.
  1127. //NpWinIniChange ();
  1128. break;
  1129. case WM_DROPFILES: /*case added 03/26/91 for file drag/drop support*/
  1130. doDrop (wParam,hwnd);
  1131. break;
  1132. case PWM_CHECK_HKL: /* private message: corresponding to HKL change message */
  1133. {
  1134. LPARAM lParam = 0;
  1135. if (PRIMARYLANGID(LOWORD((DWORD) (INT_PTR) GetKeyboardLayout(0))) == LANG_JAPANESE) {
  1136. /*
  1137. * If new current HKL is Japanese, handle the result string at once.
  1138. */
  1139. lParam = EIMES_GETCOMPSTRATONCE;
  1140. }
  1141. SendMessage(hwndEdit, EM_SETIMESTATUS, EMSIS_COMPOSITIONSTRING, lParam);
  1142. }
  1143. break;
  1144. default:
  1145. /* this can be a message from the modeless Find Text window */
  1146. if (message == wFRMsg)
  1147. {
  1148. BOOL bStatus; // true if found text
  1149. lpfr = (LPFINDREPLACE)lParam;
  1150. dwFlags = lpfr->Flags;
  1151. fReverse = (dwFlags & FR_DOWN ? FALSE : TRUE);
  1152. fCase = (dwFlags & FR_MATCHCASE ? TRUE : FALSE);
  1153. if( dwFlags & FR_FINDNEXT )
  1154. {
  1155. SetCursor( hWaitCursor );
  1156. Search( szSearch );
  1157. SetCursor( hStdCursor );
  1158. }
  1159. else if( dwFlags & FR_REPLACE )
  1160. {
  1161. //
  1162. // Replace current selection if it matches
  1163. // then highlight the next occurence of the string.
  1164. //
  1165. SetCursor( hWaitCursor );
  1166. ReplaceSel( TRUE );
  1167. Search( szSearch );
  1168. SetCursor( hStdCursor );
  1169. }
  1170. else if( dwFlags & FR_REPLACEALL )
  1171. {
  1172. //
  1173. // The replace dialog doesn't allow reverse searches
  1174. // but just it cases it changes, for it to false.
  1175. //
  1176. if( fReverse )
  1177. {
  1178. fReverse= FALSE;
  1179. }
  1180. //
  1181. // Replace all occurances of text in the file
  1182. // starting from the top. Reset the selection
  1183. // to the top of the file.
  1184. //
  1185. SetCursor( hWaitCursor );
  1186. SendMessage( hwndEdit, EM_SETSEL, 0, 0 );
  1187. do
  1188. {
  1189. ReplaceSel( FALSE );
  1190. bStatus= Search( szSearch );
  1191. }
  1192. while( bStatus );
  1193. SetCursor( hStdCursor );
  1194. //
  1195. // back to the top of the file.
  1196. //
  1197. SendMessage( hwndEdit, EM_SETSEL, 0, 0 );
  1198. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0);
  1199. UpdateStatusBar( TRUE );
  1200. }
  1201. else if (dwFlags & FR_DIALOGTERM)
  1202. hDlgFind = NULL; /* invalidate modeless window handle */
  1203. break;
  1204. }
  1205. return (DefWindowProc(hwnd, message, wParam, lParam));
  1206. }
  1207. return (0L);
  1208. }
  1209. LPTSTR SkipProgramName (LPTSTR lpCmdLine)
  1210. {
  1211. LPTSTR p = lpCmdLine;
  1212. BOOL bInQuotes = FALSE;
  1213. //
  1214. // Skip executable name
  1215. //
  1216. for (p; *p; p = CharNext(p))
  1217. {
  1218. if ((*p == TEXT(' ') || *p == TEXT('\t')) && !bInQuotes)
  1219. break;
  1220. if (*p == TEXT('\"'))
  1221. bInQuotes = !bInQuotes;
  1222. }
  1223. while (*p == TEXT(' ') || *p == TEXT('\t'))
  1224. p++;
  1225. return (p);
  1226. }
  1227. /* ** Main loop */
  1228. INT WINAPI WinMain(
  1229. HINSTANCE hInstance,
  1230. HINSTANCE hPrevInstance,
  1231. LPSTR lpAnsiCmdLine,
  1232. INT cmdShow)
  1233. {
  1234. MSG msg;
  1235. VOID (FAR PASCAL *lpfnRegisterPenApp)(WORD, BOOL) = NULL;
  1236. LPTSTR lpCmdLine = GetCommandLine ();
  1237. HWINEVENTHOOK hEventHook = NULL;
  1238. /* PenWindow registration must be before creating an edit class window.
  1239. * Moved here, along with goto statement below for appropriate cleanup.
  1240. * 10 July 1991 ClarkC
  1241. */
  1242. if ((FARPROC) lpfnRegisterPenApp = GetProcAddress((HINSTANCE)(INT_PTR)(GetSystemMetrics(SM_PENWINDOWS)),
  1243. "RegisterPenApp"))
  1244. (*lpfnRegisterPenApp)(1, TRUE);
  1245. if (!NPInit(hInstance, hPrevInstance, SkipProgramName (lpCmdLine), cmdShow))
  1246. {
  1247. msg.wParam = FALSE;
  1248. goto UnRegisterPenWindows;
  1249. }
  1250. // set an event hook to get the cursor position! this event hook is used to update
  1251. // the line & column position of the caret shown in the statusbar.
  1252. hEventHook = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, NULL, WinEventFunc,
  1253. (DWORD) GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
  1254. while (GetMessage((LPMSG)&msg, (HWND)NULL, 0, 0))
  1255. {
  1256. //
  1257. // To handle IME status when active KL is changed.
  1258. //
  1259. if (msg.message == WM_INPUTLANGCHANGEREQUEST) {
  1260. //
  1261. // WM_INPUTLANGCHANGE will be *sent* to WndProc,
  1262. // so there's no chance to catch WM_INPUTLANGCHANGE from the frame window.
  1263. // Instead, we post the private message to check the active HKL later.
  1264. //
  1265. PostMessage(hwndNP, PWM_CHECK_HKL, 0, 0);
  1266. }
  1267. if (!hDlgFind || !IsDialogMessage(hDlgFind, &msg))
  1268. {
  1269. if (TranslateAccelerator(hwndNP, hAccel, (LPMSG)&msg) == 0)
  1270. {
  1271. TranslateMessage ((LPMSG)&msg);
  1272. DispatchMessage ((LPMSG)&msg);
  1273. }
  1274. }
  1275. }
  1276. /* Clean up any global allocations */
  1277. FreeGlobal();
  1278. LocalFree( hEdit );
  1279. if (hEventHook)
  1280. UnhookWinEvent(hEventHook);
  1281. UnRegisterPenWindows:
  1282. if (lpfnRegisterPenApp)
  1283. (*lpfnRegisterPenApp)(1, FALSE);
  1284. return (int)(msg.wParam);
  1285. }
  1286. // WinEventFunc is called whenever the location of the caret changes
  1287. // in the edit window. The function updates the statusbar with the current
  1288. // line number, column of the caret. This event is called when the mouse is moved.
  1289. // If the caret moves without mouse input, the UpdateStatusBar is called.
  1290. //
  1291. // UpdateStatusBar( TRUE ) is called to force the display being changed.
  1292. static DWORD iLastCol;
  1293. static DWORD iLastLine;
  1294. VOID UpdateStatusBar( BOOL fForceStatus )
  1295. {
  1296. DWORD SelStart, SelEnd;
  1297. UINT iLine, iCol;
  1298. TCHAR szStatusText[128];
  1299. // get the current caret position.
  1300. SendMessage(hwndEdit,EM_GETSEL,(WPARAM) &SelStart,(WPARAM)&SelEnd);
  1301. // the line numbers are 1 based instead 0 based. hence add 1.
  1302. iLine = (UINT)SendMessage( hwndEdit, EM_LINEFROMCHAR, SelStart, 0 ) + 1;
  1303. iCol = SelStart - (UINT)SendMessage( hwndEdit, EM_LINEINDEX, iLine-1, 0 ) + 1;
  1304. // don't bother to update status if it hasn't changed
  1305. if( fForceStatus || (iCol!=iLastCol) || (iLine!=iLastLine) )
  1306. {
  1307. // prepare and display the statusbar.
  1308. // make sure you don't overflow the buffer boundary.
  1309. _sntprintf(szStatusText, sizeof(szStatusText)/sizeof(TCHAR) -1, szLineCol, iLine, iCol);
  1310. szStatusText[ sizeof(szStatusText)/sizeof(TCHAR) -1 ] = TEXT('\0');
  1311. SetStatusBarText(szStatusText, 1);
  1312. }
  1313. iLastCol= iCol;
  1314. iLastLine= iLine;
  1315. };
  1316. VOID CALLBACK WinEventFunc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject,
  1317. LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
  1318. {
  1319. UpdateStatusBar( FALSE );
  1320. }
  1321. /* ** Set Window caption text */
  1322. void FAR SetTitle( TCHAR *sz )
  1323. {
  1324. TCHAR szWindowText[MAX_PATH+50];
  1325. TCHAR szFileName[MAX_PATH];
  1326. HANDLE hFindFile;
  1327. WIN32_FIND_DATA info;
  1328. TCHAR szStatusText[128] = TEXT("");
  1329. DWORD dwAttributes;
  1330. // if "untitled" then don't do all this work...
  1331. if( lstrcmp( sz, szUntitled ) == 0 )
  1332. {
  1333. lstrcpy( szWindowText, sz );
  1334. }
  1335. else
  1336. {
  1337. // get the attributes for file. these will be shown
  1338. // in the status bar.
  1339. dwAttributes = GetFileAttributes(sz);
  1340. // Get real(file system) name for the file.
  1341. hFindFile= FindFirstFile( sz, &info );
  1342. if( hFindFile != INVALID_HANDLE_VALUE )
  1343. {
  1344. lstrcpy( szFileName, info.cFileName );
  1345. FindClose( hFindFile );
  1346. }
  1347. else
  1348. lstrcpy( szFileName, sz );
  1349. GetFileTitle(szFileName, szWindowText, MAX_PATH);
  1350. // Removed the left part of the status bar to show file properties.
  1351. // The correct way to fix it is to make sure that the correct delimiters (like commas),
  1352. // and correct order is used for various localized builds.
  1353. #ifdef UNUSED_STATUSBARCODE
  1354. // prepare the status bar text and show
  1355. // if the file has any special properties (such as hidden, readonly etc.)
  1356. if (dwAttributes & FILE_ATTRIBUTE_COMPRESSED)
  1357. if ((lstrlen(szStatusText) + lstrlen(szCompressedFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1358. lstrcpy(szStatusText, szCompressedFile);
  1359. if (dwAttributes & FILE_ATTRIBUTE_ENCRYPTED)
  1360. if ((lstrlen(szStatusText) + lstrlen(szEncryptedFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1361. lstrcat(szStatusText, szEncryptedFile);
  1362. if (dwAttributes & FILE_ATTRIBUTE_HIDDEN)
  1363. if ((lstrlen(szStatusText) + lstrlen(szHiddenFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1364. lstrcat(szStatusText, szHiddenFile);
  1365. if (dwAttributes & FILE_ATTRIBUTE_OFFLINE)
  1366. if ((lstrlen(szStatusText) + lstrlen(szOfflineFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1367. lstrcat(szStatusText, szOfflineFile);
  1368. if (dwAttributes & FILE_ATTRIBUTE_READONLY)
  1369. {
  1370. if ((lstrlen(szStatusText) + lstrlen(szReadOnlyFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1371. lstrcat(szStatusText, szReadOnlyFile);
  1372. }
  1373. if (dwAttributes & FILE_ATTRIBUTE_SYSTEM)
  1374. if ((lstrlen(szStatusText) + lstrlen(szSystemFile) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1375. lstrcat(szStatusText, szSystemFile);
  1376. // if the status did get updated by file properties
  1377. if (*szStatusText != TEXT('\0'))
  1378. {
  1379. // get rid of the last comma
  1380. szStatusText[lstrlen(szStatusText)-1] = TEXT(' ');
  1381. if ((lstrlen(szStatusText) + lstrlen(szFile)) < sizeof(szStatusText)/sizeof(TCHAR) - 1)
  1382. lstrcat(szStatusText, szFile);
  1383. }
  1384. #endif
  1385. }
  1386. // set the status bar. the Line and Col count is 1 initially for
  1387. // the newly opened file as the caret position is at the first character.
  1388. // SetStatusBarText(szStatusText, 0);
  1389. _sntprintf(szStatusText, sizeof(szStatusText)/sizeof(TCHAR) -1, szLineCol, 1, 1);
  1390. SetStatusBarText(szStatusText, 1);
  1391. lstrcat(szWindowText, szNpTitle);
  1392. SetWindowText(hwndNP, (LPTSTR)szWindowText);
  1393. }
  1394. /* ** Given filename which may or maynot include path, return pointer to
  1395. filename (not including path part.) */
  1396. LPTSTR PASCAL far PFileInPath(
  1397. LPTSTR sz)
  1398. {
  1399. LPTSTR pch = sz;
  1400. LPTSTR psz;
  1401. /* Strip path/drive specification from name if there is one */
  1402. /* Ripped out AnsiPrev calls. 21 March 1991 clarkc */
  1403. for (psz = sz; *psz; psz = CharNext(psz))
  1404. {
  1405. if ((*psz == TEXT(':')) || (*psz == TEXT('\\')))
  1406. pch = psz;
  1407. }
  1408. if (pch != sz) /* If found slash or colon, return the next character */
  1409. pch++; /* increment OK, pch not pointing to DB character */
  1410. return(pch);
  1411. }
  1412. /* ** Enable or disable menu items according to selection state
  1413. This routine is called when user tries to pull down a menu. */
  1414. VOID NpResetMenu( HWND hwnd )
  1415. {
  1416. LONG lsel;
  1417. INT mfcc; /* menuflag for cut, copy */
  1418. BOOL fCanUndo;
  1419. HANDLE hMenu;
  1420. TCHAR msgbuf[20];
  1421. BOOL fPaste= FALSE;
  1422. UINT uSelState;
  1423. hMenu = GetMenu(hwndNP);
  1424. // cut, copy and delete only get enabled if there is text selected.
  1425. lsel = (LONG)SendMessage(hwndEdit, EM_GETSEL, 0, 0L);
  1426. mfcc = LOWORD(lsel) == HIWORD(lsel) ? MF_GRAYED : MF_ENABLED;
  1427. EnableMenuItem(GetSubMenu(hMenu, 1), M_CUT, mfcc);
  1428. EnableMenuItem(GetSubMenu(hMenu, 1), M_COPY, mfcc);
  1429. EnableMenuItem(GetSubMenu(hMenu, 1), M_CLEAR, mfcc);
  1430. // check if the selectall is gray (that means the user has already
  1431. // done select-all) and if the user has deselected - if so, time
  1432. // to re-enable selectall menu.
  1433. uSelState = GetMenuState(GetSubMenu(hMenu, 1), M_SELECTALL, MF_BYCOMMAND);
  1434. if ((uSelState == MF_GRAYED) && (mfcc == MF_GRAYED))
  1435. {
  1436. EnableMenuItem(GetSubMenu(hMenu, 1), M_SELECTALL, MF_ENABLED);
  1437. }
  1438. // paste is enabled if there is text in the clipboard
  1439. if( OpenClipboard(hwnd) )
  1440. {
  1441. fPaste= IsClipboardFormatAvailable(CF_TEXT);
  1442. CloseClipboard();
  1443. }
  1444. EnableMenuItem(GetSubMenu(hMenu, 1), M_PASTE, fPaste ? MF_ENABLED : MF_GRAYED);
  1445. // enable 'goto' iff wordwrap is off; MLE doesn't give good results if word wrap on
  1446. EnableMenuItem(GetSubMenu(hMenu, 1), M_GOTO, fWrap ? MF_GRAYED : MF_ENABLED );
  1447. // enable Undo only if editcontrol says we can do it.
  1448. fCanUndo = (BOOL) SendMessage(hwndEdit, EM_CANUNDO, 0, 0L);
  1449. EnableMenuItem(GetSubMenu(hMenu, 1), M_UNDO, fCanUndo ? MF_ENABLED : MF_GRAYED);
  1450. // check the word wrap item correctly
  1451. CheckMenuItem(GetSubMenu(hMenu, 2), M_WW, fWrap ? MF_CHECKED : MF_UNCHECKED);
  1452. // check the status bar
  1453. CheckMenuItem (GetSubMenu(hMenu, 3), M_STATUSBAR, fStatus ? MF_CHECKED: MF_UNCHECKED );
  1454. }
  1455. void FAR NpWinIniChange(VOID)
  1456. {
  1457. InitLocale ();
  1458. }
  1459. /* ** Scan sz1 for merge spec. If found, insert string sz2 at that point.
  1460. Then append rest of sz1 NOTE! Merge spec guaranteed to be two chars.
  1461. returns TRUE if it does a merge, false otherwise. */
  1462. BOOL MergeStrings(
  1463. TCHAR *szSrc,
  1464. TCHAR *szMerge,
  1465. TCHAR *szDst)
  1466. {
  1467. register TCHAR *pchSrc;
  1468. register TCHAR *pchDst;
  1469. pchSrc = szSrc;
  1470. pchDst = szDst;
  1471. /* Find merge spec if there is one. */
  1472. while ( *pchSrc != chMerge)
  1473. {
  1474. *pchDst++ = *pchSrc;
  1475. /* If we reach end of string before merge spec, just return. */
  1476. if (!*pchSrc++)
  1477. return FALSE;
  1478. }
  1479. /* If merge spec found, insert sz2 there. (check for null merge string */
  1480. if (szMerge)
  1481. {
  1482. while (*szMerge)
  1483. *pchDst++ = *szMerge++;
  1484. }
  1485. /* Jump over merge spec */
  1486. pchSrc++,pchSrc++;
  1487. /* Now append rest of Src String */
  1488. while (*pchDst++ = *pchSrc++);
  1489. return TRUE;
  1490. }
  1491. /* ** Post a message box */
  1492. INT FAR AlertBox(
  1493. HWND hwndParent,
  1494. TCHAR *szCaption,
  1495. TCHAR *szText1,
  1496. TCHAR *szText2,
  1497. UINT style)
  1498. {
  1499. INT iResult; // result of function
  1500. INT iAllocSize; // size needed for message
  1501. TCHAR* pszMessage; // combined message
  1502. // Allocate a message buffer assuming there will be a merge.
  1503. // If we cannot do the allocation, tell the user something
  1504. // related to the original problem. (not the allocation failure)
  1505. // Then pray that MessageBox can get enough memory to actually work.
  1506. iAllocSize= (lstrlen(szText1) + (szText2 ? lstrlen(szText2) : 0) + 1 ) * sizeof(TCHAR);
  1507. pszMessage= (TCHAR*) LocalAlloc( LPTR, iAllocSize );
  1508. if( pszMessage )
  1509. {
  1510. MergeStrings( szText1, szText2, pszMessage );
  1511. iResult= MessageBox( hwndParent, pszMessage, szCaption, style );
  1512. LocalFree( (HLOCAL) pszMessage );
  1513. }
  1514. else
  1515. {
  1516. iResult= MessageBox( hwndParent, szText1, szCaption, style );
  1517. }
  1518. return( iResult );
  1519. }
  1520. // SignalCommDlgError
  1521. //
  1522. // If a common dialog error occurred, put up reasonable message box.
  1523. //
  1524. // returns: TRUE if error occurred, FALSE if no error.
  1525. //
  1526. typedef struct tagMAPERROR
  1527. {
  1528. DWORD rc; // return code from CommDlgExtendedError()
  1529. PTCHAR* ppszMsg; // text of message pointer
  1530. } MAPERROR;
  1531. // errors not in this list get generic "common dialog error %x" message.
  1532. static TCHAR* szNull= TEXT("");
  1533. MAPERROR maperror[]=
  1534. {
  1535. CDERR_DIALOGFAILURE, &szErrSpace,
  1536. CDERR_INITIALIZATION, &szCommDlgInitErr,
  1537. CDERR_MEMLOCKFAILURE, &szPDIE,
  1538. CDERR_LOADSTRFAILURE, &szErrSpace,
  1539. CDERR_FINDRESFAILURE, &szErrSpace,
  1540. PDERR_LOADDRVFAILURE, &szLoadDrvFail,
  1541. PDERR_GETDEVMODEFAIL, &szErrSpace,
  1542. PDERR_NODEFAULTPRN, &szNull, // don't report; common dialog does already
  1543. };
  1544. BOOL SignalCommDlgError(VOID)
  1545. {
  1546. DWORD rc; // return code
  1547. TCHAR* pszMsg; // message
  1548. INT i;
  1549. TCHAR szBuf[200]; // just for common dialog failure
  1550. rc= CommDlgExtendedError();
  1551. // no failure - just return
  1552. if( rc == 0 )
  1553. {
  1554. return FALSE;
  1555. }
  1556. // some sort of error - pick up message
  1557. pszMsg= NULL;
  1558. for( i=0; i< sizeof(maperror)/sizeof(maperror[0]); i++ )
  1559. {
  1560. if( rc == maperror[i].rc )
  1561. {
  1562. pszMsg= *maperror[i].ppszMsg;
  1563. }
  1564. }
  1565. // if no known mapping - tell user the actual return code
  1566. // this may be a bit confusing, but rare hopefully.
  1567. if( !pszMsg )
  1568. {
  1569. _sntprintf(szBuf, sizeof(szBuf)/sizeof(TCHAR) -1, szCommDlgErr, rc);
  1570. pszMsg= szBuf;
  1571. }
  1572. // popup if there is any message to give user
  1573. if( *pszMsg )
  1574. {
  1575. MessageBox(hwndNP, pszMsg, szNN, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
  1576. }
  1577. return TRUE;
  1578. }
  1579. // ReplaceSel
  1580. //
  1581. // Replace the current selection with string from FR struct
  1582. // if the current selection matches our search string.
  1583. //
  1584. // MLE will show selection if bView is true.
  1585. //
  1586. VOID ReplaceSel( BOOL bView )
  1587. {
  1588. DWORD StartSel; // start of selected text
  1589. DWORD EndSel; // end of selected text
  1590. HANDLE hEText;
  1591. TCHAR* pStart;
  1592. DWORD ReplaceWithLength; // length of replacement string
  1593. DWORD FindWhatLength;
  1594. ReplaceWithLength= lstrlen(FR.lpstrReplaceWith);
  1595. FindWhatLength= lstrlen(FR.lpstrFindWhat);
  1596. SendMessage( hwndEdit, EM_GETSEL, (WPARAM) &StartSel, (LPARAM) &EndSel );
  1597. hEText= (HANDLE) SendMessage( hwndEdit, EM_GETHANDLE, 0, 0 );
  1598. if( !hEText ) // silently return if we can't get it
  1599. {
  1600. return;
  1601. }
  1602. pStart= LocalLock( hEText );
  1603. if( !pStart )
  1604. {
  1605. return;
  1606. }
  1607. if( (EndSel-StartSel) == FindWhatLength )
  1608. {
  1609. if( (fCase &&
  1610. !_tcsncmp( FR.lpstrFindWhat, pStart+StartSel, FindWhatLength) ) ||
  1611. (!fCase &&
  1612. ( 2 == CompareString(LOCALE_USER_DEFAULT,
  1613. NORM_IGNORECASE | SORT_STRINGSORT | NORM_STOP_ON_NULL,
  1614. FR.lpstrFindWhat, FindWhatLength,
  1615. pStart+StartSel, FindWhatLength ) ) ) )
  1616. {
  1617. SendMessage( hwndEdit, EM_REPLACESEL,
  1618. TRUE, (LPARAM) FR.lpstrReplaceWith);
  1619. SendMessage( hwndEdit, EM_SETSEL,
  1620. StartSel, StartSel+ReplaceWithLength );
  1621. if( bView )
  1622. {
  1623. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  1624. }
  1625. }
  1626. }
  1627. LocalUnlock( hEText );
  1628. }
  1629. // GotoDlgProc
  1630. //
  1631. // Handle the Goto Dialog window processing
  1632. //
  1633. // Returns:
  1634. //
  1635. // 1 if successfull
  1636. // 0 if not (cancelled)
  1637. //
  1638. // Modifies global lGotoLine
  1639. //
  1640. const DWORD s_GotoHelpIDs[] = {
  1641. IDC_GOTO, IDH_GOTO,
  1642. 0, 0
  1643. };
  1644. #define GOTOBUFSIZE 100
  1645. INT_PTR CALLBACK GotoDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
  1646. {
  1647. TCHAR szBuf[GOTOBUFSIZE];
  1648. UINT LineNum;
  1649. DWORD SelStart, SelEnd;
  1650. POINT pt;
  1651. INT id;
  1652. switch (message)
  1653. {
  1654. //
  1655. // initialize input field to size of file
  1656. //
  1657. case WM_INITDIALOG:
  1658. SendMessage(hwndEdit,EM_GETSEL,(WPARAM) &SelStart,(WPARAM)&SelEnd);
  1659. // the line numbers are 1 based instead 0 based. hence add 1.
  1660. LineNum= (UINT)SendMessage( hwndEdit, EM_LINEFROMCHAR, SelStart, 0 ) + 1;
  1661. wsprintf(szBuf, TEXT("%d"), LineNum);
  1662. SetDlgItemText( hDlg, IDC_GOTO, szBuf );
  1663. SetFocus( hDlg );
  1664. return TRUE;
  1665. break;
  1666. // context sensitive help.
  1667. case WM_HELP:
  1668. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, szHelpFile,
  1669. HELP_WM_HELP, (ULONG_PTR) (LPVOID) s_GotoHelpIDs);
  1670. break;
  1671. case WM_CONTEXTMENU:
  1672. WinHelp((HWND) wParam, szHelpFile, HELP_CONTEXTMENU,
  1673. (ULONG_PTR) (LPVOID) s_GotoHelpIDs);
  1674. break;
  1675. case WM_COMMAND:
  1676. switch (LOWORD(wParam))
  1677. {
  1678. UINT CharIndex;
  1679. case IDC_GOTO:
  1680. return TRUE;
  1681. break;
  1682. case IDOK:
  1683. GetDlgItemText( hDlg, IDC_GOTO, szBuf, GOTOBUFSIZE );
  1684. // convert all unicode numbers to range L'0' to L'9'
  1685. FoldString( MAP_FOLDDIGITS, szBuf, -1, szBuf, GOTOBUFSIZE);
  1686. lGotoLine= _ttol( szBuf );
  1687. //
  1688. // see if valid line number
  1689. //
  1690. CharIndex= (UINT)SendMessage( hwndEdit,
  1691. EM_LINEINDEX,
  1692. lGotoLine-1,
  1693. 0);
  1694. if( lGotoLine > 0 && CharIndex != -1 )
  1695. {
  1696. EndDialog(hDlg, 0); // successfull
  1697. return TRUE;
  1698. }
  1699. //
  1700. // Invalid line number
  1701. // warning user and set to reasonable value
  1702. //
  1703. MessageBox( hDlg, szLineTooLarge, szLineError, MB_OK );
  1704. LineNum= (UINT)SendMessage( hwndEdit, EM_GETLINECOUNT, 0, 0 );
  1705. wsprintf(szBuf, TEXT("%d"), LineNum);
  1706. SetDlgItemText( hDlg, IDC_GOTO, szBuf );
  1707. SetFocus( hDlg );
  1708. break;
  1709. case IDCANCEL :
  1710. EndDialog(hDlg, 1 ); // cancelled
  1711. return TRUE;
  1712. break;
  1713. default:
  1714. break;
  1715. } // switch (wParam)
  1716. break;
  1717. default:
  1718. break;
  1719. } // switch (message)
  1720. return FALSE; // Didn't process a message
  1721. } // GotoDlgProc()
  1722. // This procedure looks at the iSize bytes pointed by lpFileContents and
  1723. // determines based on that the encoding of the file (Ansi, Unicode, Reverse Unicode, UTF8)
  1724. NP_FILETYPE fDetermineFileType(LPBYTE lpFileContents, UINT iSize)
  1725. {
  1726. NP_FILETYPE ftFileType = FT_ANSI;
  1727. LPTSTR szBuf = (LPTSTR) lpFileContents;
  1728. if (iSize <= 1)
  1729. return ftFileType;
  1730. switch(*szBuf)
  1731. {
  1732. // look for the standard BOMs.
  1733. case BYTE_ORDER_MARK:
  1734. ftFileType = FT_UNICODE;
  1735. break;
  1736. case REVERSE_BYTE_ORDER_MARK:
  1737. ftFileType = FT_UNICODEBE;
  1738. break;
  1739. // UTF bom has 3 bytes.
  1740. case BOM_UTF8_HALF:
  1741. if (iSize > 2 && ((BYTE) *((lpFileContents)+2) == BOM_UTF8_2HALF) )
  1742. {
  1743. ftFileType = FT_UTF8;
  1744. }
  1745. break;
  1746. default:
  1747. // Is the file unicode without BOM ?
  1748. if (IsInputTextUnicode(lpFileContents, iSize))
  1749. {
  1750. ftFileType = FT_UNICODE;
  1751. }
  1752. else
  1753. {
  1754. // Is the file UTF-8 even though it doesn't have UTF-8 BOM ?
  1755. // BUGBUG: the iSize that we pass should be correct so that the string ends on the
  1756. // boundary of an UTF8 character.
  1757. if (IsTextUTF8(lpFileContents, iSize))
  1758. {
  1759. ftFileType = FT_UTF8;
  1760. }
  1761. // well, it is most likely an ansi file.
  1762. else
  1763. {
  1764. ftFileType = FT_ANSI;
  1765. }
  1766. }
  1767. break;
  1768. }
  1769. return ftFileType;
  1770. }