Leaked source code of windows server 2003
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.

1639 lines
52 KiB

  1. /*
  2. * SxSpad application
  3. * Copyright (C) 1984-1995 Microsoft Inc.
  4. */
  5. #include "precomp.h"
  6. #include <htmlhelp.h>
  7. #define DeepTrouble() MessageBox(hwndSP, szErrSpace, szNN, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
  8. UINT lGotoLine; /* line number to goto to */
  9. TCHAR chMerge;
  10. HWND hwndSP = 0; /* handle to sxspad parent window */
  11. HWND hwndEdit = 0; /* handle to main text control item */
  12. HANDLE hEdit; /* Handle to storage for edit item */
  13. HWND hDlgFind = NULL; /* handle to modeless FindText window */
  14. HANDLE hStdCursor; /* handle to arrow or beam cursor */
  15. HANDLE hWaitCursor; /* handle to hour glass cursor */
  16. HANDLE hInstanceSP; /* Module instance handle */
  17. HANDLE hFont; /* handle to Unicode font */
  18. LOGFONT FontStruct; /* font dialog structure */
  19. INT iPointSize=120; /* current point size unit=1/10 pts */
  20. TCHAR szFileName[MAX_PATH+1]; /* Current sxspad filename */
  21. TCHAR szSearch[CCHKEYMAX]; /* Search string */
  22. TCHAR szReplace[CCHKEYMAX]; /* replace string */
  23. BOOL fUntitled = TRUE; /* TRUE if sxspad has no title */
  24. HMENU hSysMenuSetup; /* Save Away for disabled Minimize */
  25. DWORD dwEmSetHandle = 0; /* Is EM_SETHANDLE in process? */
  26. HANDLE hAccel; /* Handle to accelerator table */
  27. BOOL fRunBySetup = FALSE; /* Did SlipUp WinExec us?? */
  28. BOOL fWrap = 0; /* Flag for word wrap */
  29. TCHAR szSxspad[] = TEXT("Sxspad");/* Name of sxspad window class */
  30. BOOL fInSaveAsDlg = FALSE;
  31. /* variables for the new File/Open, File/Saveas,Find Text and Print dialogs */
  32. OPENFILENAME OFN; /* passed to the File Open/save APIs */
  33. TCHAR szOpenFilterSpec[CCHFILTERMAX]; /* default open filter spec */
  34. TCHAR szSaveFilterSpec[CCHFILTERMAX]; /* default save filter spec */
  35. SP_FILETYPE g_ftOpenedAs=FT_UNKNOWN; /* current file was opened */
  36. SP_FILETYPE g_ftSaveAs; /* current file was opened */
  37. FINDREPLACE FR; /* Passed to FindText() */
  38. PAGESETUPDLG g_PageSetupDlg;
  39. UINT wFRMsg; /* message used in communicating */
  40. /* with Find/Replace dialog */
  41. DWORD dwCurrentSelectionStart = 0L; /* WM_ACTIVATEAPP selection pos */
  42. DWORD dwCurrentSelectionEnd = 0L; /* WM_ACTIVATEAPP selection pos */
  43. UINT wHlpMsg; /* message used in invoking help */
  44. /* Strings loaded from resource file passed to LoadString at initialization time */
  45. /* To add resource string:
  46. * 1) create IDS_ macro definition in sxspad.h
  47. * 2) create string in resource file
  48. * 3) create 'TCHAR*' variable directly below and in sxspad.h file
  49. * 4) add &variable to rgsz
  50. * 5) increment CSTRINGS
  51. */
  52. TCHAR *szDiskError =(TCHAR *)IDS_DISKERROR; /* Can't open File, check disk */
  53. TCHAR *szFNF =(TCHAR *)IDS_FNF; /* File not found */
  54. TCHAR *szSCBC =(TCHAR *)IDS_SCBC; /* Save changes before closing? */
  55. TCHAR *szUntitled =(TCHAR *)IDS_UNTITLED; /* Untitled */
  56. TCHAR *szNpTitle =(TCHAR *)IDS_SXSPAD; /* Sxspad - */
  57. TCHAR *szCFS =(TCHAR *)IDS_CFS; /* Can't find string */
  58. TCHAR *szErrSpace =(TCHAR *)IDS_ERRSPACE; /* Memory space exhausted */
  59. TCHAR *szFTL =(TCHAR *)IDS_FTL; /* File too large for sxspad */
  60. TCHAR *szNN =(TCHAR *)IDS_NN; /* sxspad name */
  61. TCHAR *szCommDlgInitErr = (TCHAR*)IDS_COMMDLGINIT; /* common dialog error %x */
  62. TCHAR *szPDIE =(TCHAR*) IDS_PRINTDLGINIT; /* Print dialog init error */
  63. TCHAR *szCP =(TCHAR*) IDS_CANTPRINT; /* Can't print */
  64. TCHAR *szNVF =(TCHAR*) IDS_NVF; /* Not a valid filename. */
  65. TCHAR *szCREATEERR =(TCHAR*) IDS_CREATEERR; /* cannot create file */
  66. TCHAR *szNoWW =(TCHAR*) IDS_NOWW; /* Too much text to word wrap */
  67. TCHAR *szMerge =(TCHAR*) IDS_MERGE1; /* search string for merge */
  68. TCHAR *szHelpFile =(TCHAR*) IDS_HELPFILE; /* Name of helpfile. */
  69. TCHAR *szHeader =(TCHAR*) IDS_HEADER;
  70. TCHAR *szFooter =(TCHAR*) IDS_FOOTER;
  71. TCHAR *szAnsiText = (TCHAR*)IDS_ANSITEXT; /* File/Open ANSI filter spec. string */
  72. TCHAR *szAllFiles = (TCHAR*)IDS_ALLFILES; /* File/Open Filter spec. string */
  73. TCHAR *szOpenCaption = (TCHAR*)IDS_OPENCAPTION; /* caption for File/Open dlg */
  74. TCHAR *szSaveCaption = (TCHAR*)IDS_SAVECAPTION; /* caption for File/Save dlg */
  75. TCHAR *szCannotQuit = (TCHAR*)IDS_CANNOTQUIT; /* cannot quit during a WM_QUERYENDSESSION */
  76. TCHAR *szLoadDrvFail = (TCHAR*)IDS_LOADDRVFAIL; /* LOADDRVFAIL from PrintDlg */
  77. TCHAR *szACCESSDENY = (TCHAR*)IDS_ACCESSDENY; /* Access denied on Open */
  78. TCHAR *szErrUnicode = (TCHAR*)IDS_ERRUNICODE; /* Unicode character existence error */
  79. TCHAR *szFontTooBig = (TCHAR*)IDS_FONTTOOBIG; /* font too big or page too small */
  80. TCHAR *szCommDlgErr = (TCHAR*) IDS_COMMDLGERR; /* common dialog error %x */
  81. TCHAR *szLineError = (TCHAR*) IDS_LINEERROR; /* line number error */
  82. TCHAR *szLineTooLarge = (TCHAR*) IDS_LINETOOLARGE;/* line number out of range */
  83. TCHAR *szFtAnsi = (TCHAR*) IDS_FT_ANSI;
  84. TCHAR *szFtUnicode = (TCHAR*) IDS_FT_UNICODE;
  85. TCHAR *szFtUnicodeBe = (TCHAR*) IDS_FT_UNICODEBE;
  86. TCHAR *szFtUtf8 = (TCHAR*) IDS_FT_UTF8;
  87. TCHAR *szCurrentPage = (TCHAR*) IDS_CURRENT_PAGE;
  88. TCHAR **rgsz[CSTRINGS] = {
  89. &szDiskError,
  90. &szFNF,
  91. &szSCBC,
  92. &szUntitled,
  93. &szErrSpace,
  94. &szCFS,
  95. &szNpTitle,
  96. &szFTL,
  97. &szNN,
  98. &szCommDlgInitErr,
  99. &szPDIE,
  100. &szCP,
  101. &szNVF,
  102. &szCREATEERR,
  103. &szNoWW,
  104. &szMerge,
  105. &szHelpFile,
  106. &szAnsiText,
  107. &szAllFiles,
  108. &szOpenCaption,
  109. &szSaveCaption,
  110. &szCannotQuit,
  111. &szLoadDrvFail,
  112. &szACCESSDENY,
  113. &szErrUnicode,
  114. &szCommDlgErr,
  115. &szFontTooBig,
  116. &szLineError,
  117. &szLineTooLarge,
  118. &szFtAnsi,
  119. &szFtUnicode,
  120. &szFtUnicodeBe,
  121. &szFtUtf8,
  122. &szCurrentPage,
  123. &szHeader,
  124. &szFooter,
  125. };
  126. HANDLE fp; /* file pointer */
  127. static TCHAR szPath[MAX_PATH];
  128. void FileDragOpen(void);
  129. VOID NpResetMenu(HWND hWnd);
  130. BOOL SignalCommDlgError(VOID);
  131. VOID ReplaceSel( BOOL bView );
  132. /* FreeGlobal, frees all global memory allocated. */
  133. void NEAR PASCAL FreeGlobal()
  134. {
  135. if(g_PageSetupDlg.hDevMode)
  136. {
  137. GlobalFree(g_PageSetupDlg.hDevMode);
  138. }
  139. if(g_PageSetupDlg.hDevNames)
  140. {
  141. GlobalFree(g_PageSetupDlg.hDevNames);
  142. }
  143. g_PageSetupDlg.hDevMode= NULL; // make sure they are zero for PrintDlg
  144. g_PageSetupDlg.hDevNames= NULL;
  145. }
  146. VOID PASCAL SetPageSetupDefaults( VOID )
  147. {
  148. TCHAR szIMeasure[ 2 ];
  149. g_PageSetupDlg.lpfnPageSetupHook= PageSetupHookProc;
  150. g_PageSetupDlg.lpPageSetupTemplateName= MAKEINTRESOURCE(IDD_PAGESETUP);
  151. GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IMEASURE, szIMeasure, 2 );
  152. g_PageSetupDlg.Flags= PSD_MARGINS |
  153. PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGESETUPTEMPLATE;
  154. if (szIMeasure[ 0 ] == TEXT( '1' ))
  155. {
  156. // English measure (in thousandths of inches).
  157. g_PageSetupDlg.Flags |= PSD_INTHOUSANDTHSOFINCHES;
  158. g_PageSetupDlg.rtMargin.top = 1000;
  159. g_PageSetupDlg.rtMargin.bottom = 1000;
  160. g_PageSetupDlg.rtMargin.left = 750;
  161. g_PageSetupDlg.rtMargin.right = 750;
  162. }
  163. else
  164. {
  165. // Metric measure (in hundreths of millimeters).
  166. g_PageSetupDlg.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
  167. g_PageSetupDlg.rtMargin.top = 2500;
  168. g_PageSetupDlg.rtMargin.bottom = 2500;
  169. g_PageSetupDlg.rtMargin.left = 2000;
  170. g_PageSetupDlg.rtMargin.right = 2000;
  171. }
  172. }
  173. /* Standard window size proc */
  174. void SPSize (int cxNew, int cyNew)
  175. {
  176. /* Invalidate the edit control window so that it is redrawn with the new
  177. * margins. Needed when comming up from iconic and when doing word wrap so
  178. * the new margins are accounted for.
  179. */
  180. InvalidateRect(hwndEdit, (LPRECT)NULL, TRUE);
  181. MoveWindow( hwndEdit, 0, 0, cxNew, cyNew, TRUE );
  182. }
  183. // NpSaveDialogHookProc
  184. //
  185. // Common dialog hook procedure for handling
  186. // the file type while saving.
  187. //
  188. const DWORD s_SaveAsHelpIDs[]=
  189. {
  190. IDC_FILETYPE, IDH_FILETYPE,
  191. IDC_ENCODING, IDH_FILETYPE,
  192. 0, 0
  193. };
  194. UINT_PTR APIENTRY NpSaveDialogHookProc(
  195. HWND hWnd,
  196. UINT msg,
  197. WPARAM wParam,
  198. LPARAM lParam)
  199. {
  200. INT id;
  201. POINT pt;
  202. TCHAR* szSelect; // selected type
  203. switch( msg )
  204. {
  205. case WM_INITDIALOG:
  206. // Warning: the order here must be the same as SP_FILETYPE
  207. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtAnsi );
  208. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicode );
  209. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUnicodeBe );
  210. SendDlgItemMessage(hWnd, IDC_FILETYPE,CB_ADDSTRING, 0, (LPARAM) szFtUtf8 );
  211. szSelect= szFtAnsi; // default
  212. g_ftSaveAs= g_ftOpenedAs; // default: save as same type as opened
  213. switch( g_ftSaveAs )
  214. {
  215. case FT_UNICODE: szSelect= szFtUnicode; break;
  216. case FT_UNICODEBE: szSelect= szFtUnicodeBe; break;
  217. case FT_UTF8: szSelect= szFtUtf8; break;
  218. default: break;
  219. }
  220. SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)szSelect );
  221. break;
  222. case WM_COMMAND:
  223. g_ftSaveAs= (SP_FILETYPE) SendDlgItemMessage( hWnd, IDC_FILETYPE, CB_GETCURSEL, 0, 0 );
  224. break;
  225. case WM_HELP:
  226. //
  227. // We only want to intercept help messages for controls that we are
  228. // responsible for.
  229. //
  230. id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
  231. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  232. break;
  233. WinHelp(((LPHELPINFO) lParam)-> hItemHandle,
  234. szHelpFile,
  235. HELP_WM_HELP,
  236. (ULONG_PTR) s_SaveAsHelpIDs);
  237. return TRUE;
  238. case WM_CONTEXTMENU:
  239. //
  240. // If the user clicks on any of our labels, then the wParam will
  241. // be the hwnd of the dialog, not the static control. WinHelp()
  242. // handles this, but because we hook the dialog, we must catch it
  243. // first.
  244. //
  245. if( hWnd == (HWND) wParam )
  246. {
  247. GetCursorPos(&pt);
  248. ScreenToClient(hWnd, &pt);
  249. wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
  250. }
  251. //
  252. // We only want to intercept help messages for controls that we are
  253. // responsible for.
  254. //
  255. id = GetDlgCtrlID((HWND) wParam);
  256. if ( id != IDC_FILETYPE && id != IDC_ENCODING)
  257. break;
  258. WinHelp( (HWND) wParam,
  259. szHelpFile,
  260. HELP_CONTEXTMENU,
  261. (ULONG_PTR) s_SaveAsHelpIDs);
  262. return TRUE;
  263. }
  264. return( FALSE );
  265. }
  266. /* ** Sxspad command proc - called whenever sxspad gets WM_COMMAND
  267. message. wParam passed as cmd */
  268. INT SPCommand(
  269. HWND hwnd,
  270. WPARAM wParam,
  271. LPARAM lParam )
  272. {
  273. HWND hwndFocus;
  274. LONG lSel;
  275. TCHAR szNewName[MAX_PATH] = TEXT(""); /* New file name */
  276. FARPROC lpfn;
  277. LONG style;
  278. DWORD rc;
  279. switch (LOWORD(wParam))
  280. {
  281. case M_EXIT:
  282. PostMessage(hwnd, WM_CLOSE, 0, 0L);
  283. break;
  284. case M_NEW:
  285. New(TRUE);
  286. break;
  287. case M_OPEN:
  288. if (CheckSave(FALSE))
  289. {
  290. /* set up the variable fields of the OPENFILENAME struct.
  291. * (the constant fields have been set in SPInit()
  292. */
  293. OFN.lpstrFile = szNewName;
  294. lstrcpy(szNewName, TEXT("*.txt") ); /* set default selection */
  295. OFN.lpstrTitle = szOpenCaption;
  296. /* ALL non-zero long pointers must be defined immediately
  297. * before the call, as the DS might move otherwise.
  298. * 12 February 1991 clarkc
  299. */
  300. OFN.lpstrFilter = szOpenFilterSpec;
  301. OFN.lpstrDefExt = TEXT("txt");
  302. /* Added OFN_FILEMUSTEXIST to eliminate problems in LoadFile.
  303. * 12 February 1991 clarkc
  304. */
  305. OFN.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
  306. OFN.nFilterIndex = FILE_TEXT;
  307. OFN.lpTemplateName = NULL;
  308. OFN.lpfnHook = NULL;
  309. if( GetOpenFileName( (LPOPENFILENAME)&OFN ) )
  310. {
  311. HANDLE oldfp= fp;
  312. fp= CreateFile( szNewName, // filename
  313. GENERIC_READ, // access mode
  314. FILE_SHARE_READ|FILE_SHARE_WRITE,
  315. NULL, // security descriptor
  316. OPEN_EXISTING, // how to create
  317. FILE_ATTRIBUTE_NORMAL,// file attributes
  318. NULL); // hnd to file attrs
  319. /* Try to load the file and reset fp if failed */
  320. if( !LoadFile( szNewName, FT_UNKNOWN ) )
  321. {
  322. fp= oldfp;
  323. }
  324. }
  325. else
  326. {
  327. SignalCommDlgError();
  328. }
  329. }
  330. break;
  331. case M_SAVE:
  332. /* set up the variable fields of the OPENFILENAME struct.
  333. * (the constant fields have been sel in SPInit()
  334. */
  335. g_ftSaveAs = g_ftOpenedAs;
  336. if( !fUntitled && SaveFile( hwndSP, szFileName, FALSE ) )
  337. break;
  338. /* fall through */
  339. case M_SAVEAS:
  340. OFN.lpstrFile = szNewName;
  341. OFN.lpstrTitle = szSaveCaption;
  342. /* Added OFN_PATHMUSTEXIST to eliminate problems in SaveFile.
  343. * 12 February 1991 clarkc
  344. */
  345. OFN.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
  346. OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST |
  347. OFN_EXPLORER |
  348. OFN_ENABLESIZING |
  349. OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
  350. OFN.lpTemplateName= TEXT("NpSaveDialog");
  351. OFN.lpfnHook= NpSaveDialogHookProc;
  352. /* ALL non-zero long pointers must be defined immediately
  353. * before the call, as the DS might move otherwise.
  354. * 12 February 1991 clarkc
  355. */
  356. OFN.lpstrFilter = szSaveFilterSpec;
  357. OFN.lpstrDefExt = TEXT("txt");
  358. if (!fUntitled)
  359. {
  360. lstrcpyn(szNewName, szFileName, MAX_PATH); /* set default selection */
  361. }
  362. else
  363. {
  364. lstrcpy (szNewName, TEXT("*.txt") );
  365. }
  366. fInSaveAsDlg = TRUE;
  367. OFN.nFilterIndex= FILE_TEXT;
  368. //
  369. // Do common dialog to save file
  370. //
  371. if (GetSaveFileName(&OFN))
  372. {
  373. if( SaveFile(hwnd, szNewName, TRUE) )
  374. {
  375. lstrcpyn( szFileName, szNewName, MAX_PATH);
  376. g_ftOpenedAs= g_ftSaveAs;
  377. }
  378. }
  379. else
  380. {
  381. SignalCommDlgError();
  382. }
  383. fInSaveAsDlg = FALSE;
  384. break;
  385. case M_SELECTALL:
  386. {
  387. HMENU hMenu;
  388. hMenu = GetMenu(hwndSP);
  389. lSel = (LONG) SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, 0L);
  390. SendMessage (hwndEdit, EM_SETSEL, 0, lSel );
  391. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  392. EnableMenuItem(GetSubMenu(hMenu, 1), M_SELECTALL, MF_GRAYED);
  393. break;
  394. }
  395. case M_REPLACE:
  396. if( hDlgFind )
  397. {
  398. SetFocus( hDlgFind );
  399. }
  400. else
  401. {
  402. FR.Flags= FR_HIDEWHOLEWORD | FR_REPLACE;
  403. FR.lpstrReplaceWith= szReplace;
  404. FR.wReplaceWithLen= CCHKEYMAX;
  405. FR.lpstrFindWhat = szSearch;
  406. FR.wFindWhatLen = CCHKEYMAX;
  407. hDlgFind = ReplaceText( &FR );
  408. }
  409. break;
  410. case M_FINDNEXT:
  411. if (szSearch[0])
  412. {
  413. Search(szSearch);
  414. break;
  415. }
  416. /* else fall thro' a,d bring up "find" dialog */
  417. case M_FIND:
  418. if (hDlgFind)
  419. {
  420. SetFocus(hDlgFind);
  421. }
  422. else
  423. {
  424. FR.Flags= FR_DOWN | FR_HIDEWHOLEWORD;
  425. FR.lpstrReplaceWith= NULL;
  426. FR.wReplaceWithLen= 0;
  427. FR.lpstrFindWhat = szSearch;
  428. FR.wFindWhatLen = CCHKEYMAX;
  429. hDlgFind = FindText((LPFINDREPLACE)&FR);
  430. }
  431. break;
  432. case M_GOTO:
  433. {
  434. INT Result;
  435. Result= (INT)DialogBox( hInstanceSP,
  436. MAKEINTRESOURCE(IDD_GOTODIALOG),
  437. hwndSP,
  438. GotoDlgProc );
  439. //
  440. // move cursor only if ok pressed and line number ok
  441. //
  442. if( Result == 0 )
  443. {
  444. UINT CharIndex;
  445. CharIndex= (UINT) SendMessage( hwndEdit,
  446. EM_LINEINDEX,
  447. lGotoLine-1,
  448. 0 );
  449. if( CharIndex != (UINT) -1 )
  450. {
  451. SendMessage( hwndEdit, EM_SETSEL, CharIndex, CharIndex);
  452. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
  453. }
  454. }
  455. }
  456. break;
  457. case M_ABOUT:
  458. ShellAbout(hwndSP,
  459. szNN,
  460. TEXT(""),
  461. LoadIcon(hInstanceSP,
  462. (LPTSTR)MAKEINTRESOURCE(ID_ICON)));
  463. break;
  464. case M_HELP:
  465. HtmlHelpA(GetDesktopWindow(), "sxspad.chm", HH_DISPLAY_TOPIC, 0L);
  466. break;
  467. case M_CUT:
  468. case M_COPY:
  469. case M_CLEAR:
  470. lSel = (LONG)SendMessage (hwndEdit, EM_GETSEL, 0, 0L);
  471. if (LOWORD(lSel) == HIWORD(lSel))
  472. break;
  473. case M_PASTE:
  474. /* If sxspad parent or edit window has the focus,
  475. pass command to edit window.
  476. make sure line resulting from paste will not be too long. */
  477. hwndFocus = GetFocus();
  478. if (hwndFocus == hwndEdit || hwndFocus == hwndSP)
  479. {
  480. PostMessage(hwndEdit, LOWORD(wParam), 0, 0);
  481. }
  482. break;
  483. case M_DATETIME:
  484. InsertDateTime(FALSE);
  485. break;
  486. case M_UNDO:
  487. SendMessage (hwndEdit, EM_UNDO, 0, 0L);
  488. break;
  489. case M_WW:
  490. style= (!fWrap) ? ES_STD : (ES_STD | WS_HSCROLL);
  491. if( NpReCreate( style ) )
  492. {
  493. fWrap= !fWrap;
  494. }
  495. else
  496. {
  497. MessageBox(hwndSP, szNoWW, szNN,
  498. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  499. }
  500. break;
  501. case ID_EDIT:
  502. break;
  503. case M_PRINT:
  504. PrintIt( UseDialog );
  505. break;
  506. case M_PAGESETUP:
  507. TryPrintDlgAgain:
  508. if( PageSetupDlg(&g_PageSetupDlg) )
  509. {
  510. // We know it's okay to copy these strings over...
  511. lstrcpy(chPageText[HEADER], chPageTextTemp[HEADER]);
  512. lstrcpy(chPageText[FOOTER], chPageTextTemp[FOOTER]);
  513. }
  514. else
  515. {
  516. rc= CommDlgExtendedError();
  517. if( rc == PDERR_PRINTERNOTFOUND ||
  518. rc == PDERR_DNDMMISMATCH ||
  519. rc == PDERR_DEFAULTDIFFERENT )
  520. {
  521. FreeGlobal();
  522. g_PageSetupDlg.hDevMode= g_PageSetupDlg.hDevNames= 0;
  523. goto TryPrintDlgAgain;
  524. }
  525. // Check for Dialog Failure
  526. SignalCommDlgError( );
  527. }
  528. break;
  529. case M_SETFONT:
  530. {
  531. CHOOSEFONT cf;
  532. HFONT hFontNew;
  533. HDC hDisplayDC; // display DC
  534. hDisplayDC= GetDC(NULL); // try to get display DC
  535. if( !hDisplayDC )
  536. break;
  537. /* calls the font chooser (in commdlg)
  538. * We set lfHeight; choosefont returns ipointsize
  539. */
  540. cf.lStructSize = sizeof(CHOOSEFONT);
  541. cf.hwndOwner = hwnd;
  542. cf.lpLogFont = &FontStruct; // filled in by init
  543. FontStruct.lfHeight= -MulDiv(iPointSize,GetDeviceCaps(hDisplayDC,LOGPIXELSY),720);
  544. cf.Flags = CF_INITTOLOGFONTSTRUCT |
  545. CF_SCREENFONTS |
  546. CF_NOVERTFONTS |
  547. 0;
  548. cf.rgbColors = 0; // only if cf_effects
  549. cf.lCustData = 0; // for hook function
  550. cf.lpfnHook = (LPCFHOOKPROC) NULL;
  551. cf.lpTemplateName = (LPTSTR) NULL;
  552. cf.hInstance = NULL;
  553. cf.lpszStyle = NULL; // iff cf_usestyle
  554. cf.nFontType = SCREEN_FONTTYPE;
  555. cf.nSizeMin = 0; // iff cf_limitsize
  556. cf.nSizeMax = 0; // iff cf_limitsize
  557. ReleaseDC( NULL, hDisplayDC );
  558. if( ChooseFont(&cf) )
  559. {
  560. SetCursor( hWaitCursor ); // may take some time
  561. hFontNew= CreateFontIndirect(&FontStruct);
  562. if( hFontNew )
  563. {
  564. DeleteObject( hFont );
  565. hFont= hFontNew;
  566. SendMessage( hwndEdit, WM_SETFONT,
  567. (WPARAM)hFont, MAKELPARAM(TRUE, 0));
  568. iPointSize= cf.iPointSize; // remember for printer
  569. }
  570. SetCursor( hStdCursor );
  571. }
  572. break;
  573. }
  574. default:
  575. return FALSE;
  576. }
  577. return TRUE;
  578. }
  579. // for some reason, this procedure tries to maintain
  580. // a valid 'fp' even though I believe it does not need
  581. // to be.
  582. void FileDragOpen(void)
  583. {
  584. HANDLE oldfp;
  585. oldfp= fp; // remember in case of error
  586. if( CheckSave(FALSE) )
  587. {
  588. fp= CreateFile( szPath, // filename
  589. GENERIC_READ, // access mode
  590. FILE_SHARE_READ|FILE_SHARE_WRITE,
  591. NULL, // security descriptor
  592. OPEN_EXISTING, // how to create
  593. FILE_ATTRIBUTE_NORMAL,// file attributes
  594. NULL); // hnd to file attrs
  595. if( fp == INVALID_HANDLE_VALUE )
  596. {
  597. AlertUser_FileFail( szPath );
  598. // Restore fp to original file.
  599. fp= oldfp;
  600. }
  601. /* Try to load the file and reset fp if failed */
  602. else if( !LoadFile( szPath, FT_UNKNOWN ) )
  603. {
  604. fp= oldfp;
  605. }
  606. }
  607. }
  608. /* Proccess file drop/drag options. */
  609. void doDrop (WPARAM wParam, HWND hwnd)
  610. {
  611. /* If user dragged/dropped a file regardless of keys pressed
  612. * at the time, open the first selected file from file manager. */
  613. if (DragQueryFile ((HANDLE)wParam, 0xFFFFFFFF, NULL, 0)) /* # of files dropped */
  614. {
  615. DragQueryFile ((HANDLE)wParam, 0, szPath, CharSizeOf(szPath));
  616. SetActiveWindow (hwnd);
  617. FileDragOpen();
  618. }
  619. DragFinish ((HANDLE)wParam); /* Delete structure alocated for WM_DROPFILES*/
  620. }
  621. /* ** if sxspad is dirty, check to see if user wants to save contents */
  622. BOOL FAR CheckSave (BOOL fSysModal)
  623. {
  624. INT mdResult = IDOK;
  625. TCHAR szNewName[MAX_PATH] = TEXT(""); /* New file name */
  626. TCHAR *pszFileName;
  627. /* If it's untitled and there's no text, don't worry about it */
  628. if (fUntitled && !SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, (LPARAM)0))
  629. return (TRUE);
  630. if (SendMessage (hwndEdit, EM_GETMODIFY, 0, 0L))
  631. {
  632. if( fUntitled )
  633. pszFileName= szUntitled;
  634. else
  635. pszFileName= szFileName;
  636. mdResult= AlertBox( hwndSP, szNN, szSCBC, pszFileName,
  637. (WORD)((fSysModal ? MB_SYSTEMMODAL :
  638. MB_APPLMODAL)|MB_YESNOCANCEL|MB_ICONEXCLAMATION));
  639. if( mdResult == IDYES )
  640. {
  641. if( fUntitled )
  642. {
  643. lstrcpy( szNewName, TEXT("*.txt") );
  644. SaveFilePrompt:
  645. OFN.lpstrFile = szNewName;
  646. OFN.lpstrTitle = szSaveCaption;
  647. /* Added OFN_PATHMUSTEXIST to eliminate problems in SaveFile.
  648. * 12 February 1991 clarkc
  649. */
  650. OFN.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
  651. OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST |
  652. OFN_EXPLORER |
  653. OFN_ENABLESIZING |
  654. OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
  655. OFN.lpTemplateName= TEXT("NpSaveDialog");
  656. OFN.lpfnHook= NpSaveDialogHookProc;
  657. /* ALL non-zero long pointers must be defined immediately
  658. * before the call, as the DS might move otherwise.
  659. * 12 February 1991 clarkc
  660. */
  661. OFN.lpstrFilter = szSaveFilterSpec;
  662. OFN.lpstrDefExt = TEXT("txt");
  663. OFN.nFilterIndex= FILE_TEXT;
  664. //
  665. // Set dialog checkmark by current file type
  666. //
  667. fInSaveAsDlg = TRUE;
  668. if (GetSaveFileName(&OFN))
  669. {
  670. // since SaveFile() uses near ptr to name (obs.)
  671. lstrcpy(szNewName, OFN.lpstrFile);
  672. if( SaveFile(hwndSP, szNewName, TRUE) )
  673. {
  674. lstrcpy(szFileName, szNewName);
  675. g_ftOpenedAs= g_ftSaveAs;
  676. }
  677. else
  678. { // Fixing close without saving file when disk-full
  679. lstrcpy(szNewName, szFileName);
  680. goto SaveFilePrompt;
  681. }
  682. }
  683. else
  684. {
  685. mdResult= IDCANCEL; /* Don't exit Program */
  686. if( CommDlgExtendedError() )/* Dialog box failed, Lo-mem*/
  687. DeepTrouble();
  688. }
  689. fInSaveAsDlg = FALSE;
  690. }
  691. else
  692. {
  693. // initialize the save type.
  694. g_ftSaveAs = g_ftOpenedAs;
  695. if( SaveFile(hwndSP, szFileName, FALSE) )
  696. return(TRUE);
  697. lstrcpy(szNewName, szFileName);
  698. goto SaveFilePrompt;
  699. }
  700. }
  701. }
  702. return (mdResult != IDCANCEL);
  703. }
  704. /* sxspad window class procedure */
  705. LRESULT FAR SPWndProc(
  706. HWND hwnd,
  707. UINT message,
  708. WPARAM wParam,
  709. LPARAM lParam)
  710. {
  711. RECT rc;
  712. LPFINDREPLACE lpfr;
  713. DWORD dwFlags;
  714. HANDLE hMenu;
  715. switch (message)
  716. {
  717. /* If we're being run by Setup and it's the system menu, be certain that
  718. * the minimize menu item is disabled. Note that hSysMenuSetup is only
  719. * initialized if sxspad is being run by Setup. Don't use it outside
  720. * the fRunBySetup conditional! 28 June 1991 Clark Cyr
  721. */
  722. case WM_INITMENUPOPUP:
  723. if (fRunBySetup && HIWORD(lParam))
  724. EnableMenuItem(hSysMenuSetup,SC_MINIMIZE,MF_GRAYED|MF_DISABLED);
  725. break;
  726. case WM_SYSCOMMAND:
  727. if (fRunBySetup)
  728. {
  729. /* If we have been spawned by SlipUp we need to make sure the
  730. * user doesn't minimize us or alt tab/esc away.
  731. */
  732. if (wParam == SC_MINIMIZE ||
  733. wParam == SC_NEXTWINDOW ||
  734. wParam == SC_PREVWINDOW)
  735. break;
  736. }
  737. DefWindowProc(hwnd, message, wParam, lParam);
  738. break;
  739. case WM_SETFOCUS:
  740. if (!IsIconic(hwndSP))
  741. {
  742. SetFocus(hwndEdit);
  743. }
  744. break;
  745. case WM_KILLFOCUS:
  746. SendMessage (hwndEdit, message, wParam, lParam);
  747. break;
  748. case WM_DESTROY:
  749. PostQuitMessage(0);
  750. break;
  751. case WM_CLOSE:
  752. // Save any globals in the registry if need be
  753. SaveGlobals();
  754. if (CheckSave(FALSE))
  755. {
  756. /* Exit help */
  757. if(!WinHelp(hwndSP, (LPTSTR)szHelpFile, HELP_QUIT, 0))
  758. DeepTrouble();
  759. DestroyWindow(hwndSP);
  760. DeleteObject(hFont);
  761. }
  762. break;
  763. case WM_QUERYENDSESSION:
  764. if (fInSaveAsDlg)
  765. {
  766. MessageBeep (0);
  767. MessageBeep (0);
  768. MessageBox (hwndSP, szCannotQuit, szNN, MB_OK|MB_SYSTEMMODAL);
  769. return FALSE;
  770. }
  771. else
  772. return (CheckSave(TRUE));
  773. break;
  774. case WM_ACTIVATEAPP:
  775. if (wParam)
  776. {
  777. /* This causes the caret position to be at the end of the selection
  778. * but there's no way to ask where it was or set it if known. This
  779. * will cause a caret change when the selection is made from bottom
  780. * to top.
  781. */
  782. if( dwCurrentSelectionStart != 0 || dwCurrentSelectionEnd != 0 )
  783. {
  784. SendMessage( hwndEdit, EM_SETSEL,
  785. dwCurrentSelectionStart,
  786. dwCurrentSelectionEnd );
  787. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
  788. }
  789. }
  790. else
  791. {
  792. SendMessage( hwndEdit, EM_GETSEL,
  793. (WPARAM) &dwCurrentSelectionStart,
  794. (LPARAM) &dwCurrentSelectionEnd);
  795. if (dwCurrentSelectionStart == dwCurrentSelectionEnd)
  796. {
  797. dwCurrentSelectionStart = 0L;
  798. dwCurrentSelectionEnd = 0L;
  799. }
  800. else
  801. {
  802. SendMessage (hwndEdit, EM_SETSEL, dwCurrentSelectionStart,
  803. dwCurrentSelectionEnd);
  804. SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0);
  805. }
  806. }
  807. break;
  808. case WM_ACTIVATE:
  809. if ((LOWORD(wParam) == WA_ACTIVE ||
  810. LOWORD(wParam) == WA_CLICKACTIVE) &&
  811. !IsIconic(hwndSP))
  812. {
  813. // active doesn't always mean foreground (ntbug# 53048)
  814. if( GetForegroundWindow() == hwndSP )
  815. {
  816. SetFocus(GetForegroundWindow());
  817. }
  818. }
  819. break;
  820. case WM_SIZE:
  821. switch (wParam)
  822. {
  823. case SIZENORMAL:
  824. case SIZEFULLSCREEN:
  825. SPSize(MAKEPOINTS(lParam).x, MAKEPOINTS(lParam).y);
  826. break;
  827. case SIZEICONIC:
  828. return (DefWindowProc(hwnd, message, wParam, lParam));
  829. break;
  830. }
  831. break;
  832. case WM_INITMENU:
  833. NpResetMenu( hwnd );
  834. break;
  835. //
  836. // Some keyboards come with a "Search" button which the shell team
  837. // wanted us to handle. See ntbug# 380067
  838. //
  839. case WM_APPCOMMAND:
  840. if( ( GET_APPCOMMAND_LPARAM(lParam) == APPCOMMAND_BROWSER_SEARCH ) )
  841. {
  842. SPCommand(hwnd, M_FIND, 0);
  843. break;
  844. }
  845. // otherwise fall through
  846. case WM_COMMAND:
  847. if ((HWND)(lParam) == hwndEdit &&
  848. (HIWORD(wParam) == EN_ERRSPACE ||
  849. HIWORD(wParam) == EN_MAXTEXT))
  850. {
  851. if (dwEmSetHandle == SETHANDLEINPROGRESS)
  852. dwEmSetHandle = SETHANDLEFAILED;
  853. else
  854. DeepTrouble();
  855. return 0L;
  856. }
  857. if (!SPCommand(hwnd, wParam, lParam))
  858. return (DefWindowProc(hwnd, message, wParam, lParam));
  859. break;
  860. case WM_WININICHANGE:
  861. // Ignore for now.
  862. // If you put this back in, be sure it handles both
  863. // the metric change and the decimal change.
  864. //NpWinIniChange ();
  865. break;
  866. case WM_DROPFILES: /*case added 03/26/91 for file drag/drop support*/
  867. doDrop (wParam,hwnd);
  868. break;
  869. case PWM_CHECK_HKL: /* private message: corresponding to HKL change message */
  870. {
  871. LPARAM lParam = 0;
  872. if (PRIMARYLANGID(LOWORD((DWORD) (INT_PTR) GetKeyboardLayout(0))) == LANG_JAPANESE) {
  873. /*
  874. * If new current HKL is Japanese, handle the result string at once.
  875. */
  876. lParam = EIMES_GETCOMPSTRATONCE;
  877. }
  878. SendMessage(hwndEdit, EM_SETIMESTATUS, EMSIS_COMPOSITIONSTRING, lParam);
  879. }
  880. break;
  881. default:
  882. /* this can be a message from the modeless Find Text window */
  883. if (message == wFRMsg)
  884. {
  885. BOOL bStatus; // true if found text
  886. lpfr = (LPFINDREPLACE)lParam;
  887. dwFlags = lpfr->Flags;
  888. fReverse = (dwFlags & FR_DOWN ? FALSE : TRUE);
  889. fCase = (dwFlags & FR_MATCHCASE ? TRUE : FALSE);
  890. if( dwFlags & FR_FINDNEXT )
  891. {
  892. SetCursor( hWaitCursor );
  893. Search( szSearch );
  894. SetCursor( hStdCursor );
  895. }
  896. else if( dwFlags & FR_REPLACE )
  897. {
  898. //
  899. // Replace current selection if it matches
  900. // then highlight the next occurence of the string.
  901. //
  902. SetCursor( hWaitCursor );
  903. ReplaceSel( TRUE );
  904. Search( szSearch );
  905. SetCursor( hStdCursor );
  906. }
  907. else if( dwFlags & FR_REPLACEALL )
  908. {
  909. //
  910. // The replace dialog doesn't allow reverse searches
  911. // but just it cases it changes, for it to false.
  912. //
  913. if( fReverse )
  914. {
  915. fReverse= FALSE;
  916. }
  917. //
  918. // Replace all occurances of text in the file
  919. // starting from the top. Reset the selection
  920. // to the top of the file.
  921. //
  922. SetCursor( hWaitCursor );
  923. SendMessage( hwndEdit, EM_SETSEL, 0, 0 );
  924. do
  925. {
  926. ReplaceSel( FALSE );
  927. bStatus= Search( szSearch );
  928. }
  929. while( bStatus );
  930. SetCursor( hStdCursor );
  931. //
  932. // back to the top of the file.
  933. //
  934. SendMessage( hwndEdit, EM_SETSEL, 0, 0 );
  935. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0);
  936. }
  937. else if (dwFlags & FR_DIALOGTERM)
  938. hDlgFind = NULL; /* invalidate modeless window handle */
  939. break;
  940. }
  941. return (DefWindowProc(hwnd, message, wParam, lParam));
  942. }
  943. return (0L);
  944. }
  945. LPTSTR SkipProgramName (LPTSTR lpCmdLine)
  946. {
  947. LPTSTR p = lpCmdLine;
  948. BOOL bInQuotes = FALSE;
  949. //
  950. // Skip executable name
  951. //
  952. for (p; *p; p = CharNext(p))
  953. {
  954. if ((*p == TEXT(' ') || *p == TEXT('\t')) && !bInQuotes)
  955. break;
  956. if (*p == TEXT('\"'))
  957. bInQuotes = !bInQuotes;
  958. }
  959. while (*p == TEXT(' ') || *p == TEXT('\t'))
  960. p++;
  961. return (p);
  962. }
  963. /* ** Main loop */
  964. INT WINAPI WinMain(
  965. HINSTANCE hInstance,
  966. HINSTANCE hPrevInstance,
  967. LPSTR lpAnsiCmdLine,
  968. INT cmdShow)
  969. {
  970. MSG msg;
  971. VOID (FAR PASCAL *lpfnRegisterPenApp)(WORD, BOOL) = NULL;
  972. LPTSTR lpCmdLine = GetCommandLine ();
  973. /* PenWindow registration must be before creating an edit class window.
  974. * Moved here, along with goto statement below for appropriate cleanup.
  975. * 10 July 1991 ClarkC
  976. */
  977. if ((FARPROC) lpfnRegisterPenApp = GetProcAddress((HINSTANCE)(INT_PTR)(GetSystemMetrics(SM_PENWINDOWS)),
  978. "RegisterPenApp"))
  979. (*lpfnRegisterPenApp)(1, TRUE);
  980. if (!SPInit(hInstance, hPrevInstance, SkipProgramName (lpCmdLine), cmdShow))
  981. {
  982. msg.wParam = FALSE;
  983. goto UnRegisterPenWindows;
  984. }
  985. while (GetMessage((LPMSG)&msg, (HWND)NULL, 0, 0))
  986. {
  987. //
  988. // To handle IME status when active KL is changed.
  989. //
  990. if (msg.message == WM_INPUTLANGCHANGEREQUEST) {
  991. //
  992. // WM_INPUTLANGCHANGE will be *sent* to WndProc,
  993. // so there's no chance to catch WM_INPUTLANGCHANGE from the frame window.
  994. // Instead, we post the private message to check the active HKL later.
  995. //
  996. PostMessage(hwndSP, PWM_CHECK_HKL, 0, 0);
  997. }
  998. if (!hDlgFind || !IsDialogMessage(hDlgFind, &msg))
  999. {
  1000. if (TranslateAccelerator(hwndSP, hAccel, (LPMSG)&msg) == 0)
  1001. {
  1002. TranslateMessage ((LPMSG)&msg);
  1003. DispatchMessage ((LPMSG)&msg);
  1004. }
  1005. }
  1006. }
  1007. /* Clean up any global allocations */
  1008. FreeGlobal();
  1009. LocalFree( hEdit );
  1010. UnRegisterPenWindows:
  1011. if (lpfnRegisterPenApp)
  1012. (*lpfnRegisterPenApp)(1, FALSE);
  1013. return (int)(msg.wParam);
  1014. }
  1015. /* ** Set Window caption text */
  1016. void FAR SetTitle( TCHAR *sz )
  1017. {
  1018. TCHAR szWindowText[MAX_PATH+50];
  1019. TCHAR szFileName[MAX_PATH];
  1020. HANDLE hFindFile;
  1021. WIN32_FIND_DATA info;
  1022. // if "untitled" then don't do all this work...
  1023. if( lstrcmp( sz, szUntitled ) == 0 )
  1024. lstrcpy( szWindowText, sz );
  1025. else
  1026. {
  1027. // Get real(file system) name for the file.
  1028. hFindFile= FindFirstFile( sz, &info );
  1029. if( hFindFile != INVALID_HANDLE_VALUE )
  1030. {
  1031. lstrcpy( szFileName, info.cFileName );
  1032. FindClose( hFindFile );
  1033. }
  1034. else
  1035. lstrcpy( szFileName, sz );
  1036. GetFileTitle(szFileName, szWindowText, MAX_PATH);
  1037. }
  1038. lstrcat(szWindowText, szNpTitle);
  1039. SetWindowText(hwndSP, (LPTSTR)szWindowText);
  1040. }
  1041. /* ** Given filename which may or maynot include path, return pointer to
  1042. filename (not including path part.) */
  1043. LPTSTR PASCAL far PFileInPath(
  1044. LPTSTR sz)
  1045. {
  1046. LPTSTR pch = sz;
  1047. LPTSTR psz;
  1048. /* Strip path/drive specification from name if there is one */
  1049. /* Ripped out AnsiPrev calls. 21 March 1991 clarkc */
  1050. for (psz = sz; *psz; psz = CharNext(psz))
  1051. {
  1052. if ((*psz == TEXT(':')) || (*psz == TEXT('\\')))
  1053. pch = psz;
  1054. }
  1055. if (pch != sz) /* If found slash or colon, return the next character */
  1056. pch++; /* increment OK, pch not pointing to DB character */
  1057. return(pch);
  1058. }
  1059. /* ** Enable or disable menu items according to selection state
  1060. This routine is called when user tries to pull down a menu. */
  1061. VOID NpResetMenu( HWND hwnd )
  1062. {
  1063. LONG lsel;
  1064. INT mfcc; /* menuflag for cut, copy */
  1065. BOOL fCanUndo;
  1066. HANDLE hMenu;
  1067. TCHAR msgbuf[20];
  1068. BOOL fPaste= FALSE;
  1069. UINT uSelState;
  1070. hMenu = GetMenu(hwndSP);
  1071. // cut, copy and delete only get enabled if there is text selected.
  1072. lsel = (LONG)SendMessage(hwndEdit, EM_GETSEL, 0, 0L);
  1073. mfcc = LOWORD(lsel) == HIWORD(lsel) ? MF_GRAYED : MF_ENABLED;
  1074. EnableMenuItem(GetSubMenu(hMenu, 1), M_CUT, mfcc);
  1075. EnableMenuItem(GetSubMenu(hMenu, 1), M_COPY, mfcc);
  1076. EnableMenuItem(GetSubMenu(hMenu, 1), M_CLEAR, mfcc);
  1077. // check if the selectall is gray (that means the user has already
  1078. // done select-all) and it the user has deselected - if so, time
  1079. // to re-enable selectall menu.
  1080. uSelState = GetMenuState(GetSubMenu(hMenu, 1), M_SELECTALL, MF_BYCOMMAND);
  1081. if ((uSelState == MF_GRAYED) && (mfcc == MF_GRAYED))
  1082. {
  1083. EnableMenuItem(GetSubMenu(hMenu, 1), M_SELECTALL, MF_ENABLED);
  1084. }
  1085. // paste is enabled if there is text in the clipboard
  1086. if( OpenClipboard(hwnd) )
  1087. {
  1088. fPaste= IsClipboardFormatAvailable(CF_TEXT);
  1089. CloseClipboard();
  1090. }
  1091. EnableMenuItem(GetSubMenu(hMenu, 1), M_PASTE, fPaste ? MF_ENABLED : MF_GRAYED);
  1092. // enable Undo only if editcontrol says we can do it.
  1093. fCanUndo = (BOOL) SendMessage(hwndEdit, EM_CANUNDO, 0, 0L);
  1094. EnableMenuItem(GetSubMenu(hMenu, 1), M_UNDO, fCanUndo ? MF_ENABLED : MF_GRAYED);
  1095. // check the word wrap item correctly
  1096. CheckMenuItem(GetSubMenu(hMenu, 2), M_WW, fWrap ? MF_CHECKED : MF_UNCHECKED);
  1097. }
  1098. void FAR NpWinIniChange(VOID)
  1099. {
  1100. InitLocale ();
  1101. }
  1102. /* ** Scan sz1 for merge spec. If found, insert string sz2 at that point.
  1103. Then append rest of sz1 NOTE! Merge spec guaranteed to be two chars.
  1104. returns TRUE if it does a merge, false otherwise. */
  1105. BOOL MergeStrings(
  1106. TCHAR *szSrc,
  1107. TCHAR *szMerge,
  1108. TCHAR *szDst)
  1109. {
  1110. register TCHAR *pchSrc;
  1111. register TCHAR *pchDst;
  1112. pchSrc = szSrc;
  1113. pchDst = szDst;
  1114. /* Find merge spec if there is one. */
  1115. while ( *pchSrc != chMerge)
  1116. {
  1117. *pchDst++ = *pchSrc;
  1118. /* If we reach end of string before merge spec, just return. */
  1119. if (!*pchSrc++)
  1120. return FALSE;
  1121. }
  1122. /* If merge spec found, insert sz2 there. (check for null merge string */
  1123. if (szMerge)
  1124. {
  1125. while (*szMerge)
  1126. *pchDst++ = *szMerge++;
  1127. }
  1128. /* Jump over merge spec */
  1129. pchSrc++,pchSrc++;
  1130. /* Now append rest of Src String */
  1131. while (*pchDst++ = *pchSrc++);
  1132. return TRUE;
  1133. }
  1134. /* ** Post a message box */
  1135. INT FAR AlertBox(
  1136. HWND hwndParent,
  1137. TCHAR *szCaption,
  1138. TCHAR *szText1,
  1139. TCHAR *szText2,
  1140. UINT style)
  1141. {
  1142. INT iResult; // result of function
  1143. INT iAllocSize; // size needed for message
  1144. TCHAR* pszMessage; // combined message
  1145. // Allocate a message buffer assuming there will be a merge.
  1146. // If we cannot do the allocation, tell the user something
  1147. // related to the original problem. (not the allocation failure)
  1148. // Then pray that MessageBox can get enough memory to actually work.
  1149. iAllocSize= (lstrlen(szText1) + (szText2 ? lstrlen(szText2) : 0) + 1 ) * sizeof(TCHAR);
  1150. pszMessage= (TCHAR*) LocalAlloc( LPTR, iAllocSize );
  1151. if( pszMessage )
  1152. {
  1153. MergeStrings( szText1, szText2, pszMessage );
  1154. iResult= MessageBox( hwndParent, pszMessage, szCaption, style );
  1155. LocalFree( (HLOCAL) pszMessage );
  1156. }
  1157. else
  1158. {
  1159. iResult= MessageBox( hwndParent, szText1, szCaption, style );
  1160. }
  1161. return( iResult );
  1162. }
  1163. // SignalCommDlgError
  1164. //
  1165. // If a common dialog error occurred, put up reasonable message box.
  1166. //
  1167. // returns: TRUE if error occurred, FALSE if no error.
  1168. //
  1169. typedef struct tagMAPERROR
  1170. {
  1171. DWORD rc; // return code from CommDlgExtendedError()
  1172. PTCHAR* ppszMsg; // text of message pointer
  1173. } MAPERROR;
  1174. // errors not in this list get generic "common dialog error %x" message.
  1175. static TCHAR* szNull= TEXT("");
  1176. MAPERROR maperror[]=
  1177. {
  1178. CDERR_DIALOGFAILURE, &szErrSpace,
  1179. CDERR_INITIALIZATION, &szCommDlgInitErr,
  1180. CDERR_MEMLOCKFAILURE, &szPDIE,
  1181. CDERR_LOADSTRFAILURE, &szErrSpace,
  1182. CDERR_FINDRESFAILURE, &szErrSpace,
  1183. PDERR_LOADDRVFAILURE, &szLoadDrvFail,
  1184. PDERR_GETDEVMODEFAIL, &szErrSpace,
  1185. PDERR_NODEFAULTPRN, &szNull, // don't report; common dialog does already
  1186. };
  1187. BOOL SignalCommDlgError(VOID)
  1188. {
  1189. DWORD rc; // return code
  1190. TCHAR* pszMsg; // message
  1191. INT i;
  1192. TCHAR szBuf[200]; // just for common dialog failure
  1193. rc= CommDlgExtendedError();
  1194. // no failure - just return
  1195. if( rc == 0 )
  1196. {
  1197. return FALSE;
  1198. }
  1199. // some sort of error - pick up message
  1200. pszMsg= NULL;
  1201. for( i=0; i< sizeof(maperror)/sizeof(maperror[0]); i++ )
  1202. {
  1203. if( rc == maperror[i].rc )
  1204. {
  1205. pszMsg= *maperror[i].ppszMsg;
  1206. }
  1207. }
  1208. // if no known mapping - tell user the actual return code
  1209. // this may be a bit confusing, but rare hopefully.
  1210. if( !pszMsg )
  1211. {
  1212. wsprintf( szBuf, szCommDlgErr, rc ); // fill in error code
  1213. pszMsg= szBuf;
  1214. }
  1215. // popup if there is any message to give user
  1216. if( *pszMsg )
  1217. {
  1218. MessageBox(hwndSP, pszMsg, szNN, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
  1219. }
  1220. return TRUE;
  1221. }
  1222. // ReplaceSel
  1223. //
  1224. // Replace the current selection with string from FR struct
  1225. // if the current selection matches our search string.
  1226. //
  1227. // MLE will show selection if bView is true.
  1228. //
  1229. VOID ReplaceSel( BOOL bView )
  1230. {
  1231. DWORD StartSel; // start of selected text
  1232. DWORD EndSel; // end of selected text
  1233. HANDLE hEText;
  1234. TCHAR* pStart;
  1235. DWORD ReplaceWithLength; // length of replacement string
  1236. DWORD FindWhatLength;
  1237. ReplaceWithLength= lstrlen(FR.lpstrReplaceWith);
  1238. FindWhatLength= lstrlen(FR.lpstrFindWhat);
  1239. SendMessage( hwndEdit, EM_GETSEL, (WPARAM) &StartSel, (LPARAM) &EndSel );
  1240. hEText= (HANDLE) SendMessage( hwndEdit, EM_GETHANDLE, 0, 0 );
  1241. if( !hEText ) // silently return if we can't get it
  1242. {
  1243. return;
  1244. }
  1245. pStart= LocalLock( hEText );
  1246. if( !pStart )
  1247. {
  1248. return;
  1249. }
  1250. if( (EndSel-StartSel) == FindWhatLength )
  1251. {
  1252. if( (fCase &&
  1253. !_tcsncmp( FR.lpstrFindWhat, pStart+StartSel, FindWhatLength) ) ||
  1254. (!fCase &&
  1255. ( 2 == CompareString(LOCALE_USER_DEFAULT,
  1256. NORM_IGNORECASE | SORT_STRINGSORT | NORM_STOP_ON_NULL,
  1257. FR.lpstrFindWhat, FindWhatLength,
  1258. pStart+StartSel, FindWhatLength ) ) ) )
  1259. {
  1260. SendMessage( hwndEdit, EM_REPLACESEL,
  1261. TRUE, (LPARAM) FR.lpstrReplaceWith);
  1262. SendMessage( hwndEdit, EM_SETSEL,
  1263. StartSel, StartSel+ReplaceWithLength );
  1264. if( bView )
  1265. {
  1266. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  1267. }
  1268. }
  1269. }
  1270. LocalUnlock( hEText );
  1271. }
  1272. // GotoDlgProc
  1273. //
  1274. // Handle the Goto Dialog window processing
  1275. //
  1276. // Returns:
  1277. //
  1278. // 1 if successfull
  1279. // 0 if not (cancelled)
  1280. //
  1281. // Modifies global lGotoLine
  1282. //
  1283. const DWORD s_GotoHelpIDs[] = {
  1284. IDC_GOTO, IDH_GOTO,
  1285. 0, 0
  1286. };
  1287. #define GOTOBUFSIZE 100
  1288. INT_PTR CALLBACK GotoDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
  1289. {
  1290. TCHAR szBuf[GOTOBUFSIZE];
  1291. UINT LineNum;
  1292. DWORD SelStart, SelEnd;
  1293. POINT pt;
  1294. INT id;
  1295. switch (message)
  1296. {
  1297. //
  1298. // initialize input field to size of file
  1299. //
  1300. case WM_INITDIALOG:
  1301. SendMessage(hwndEdit,EM_GETSEL,(WPARAM) &SelStart,(WPARAM)&SelEnd);
  1302. // the line numbers are 1 based instead 0 based. hence add 1.
  1303. LineNum= (UINT)SendMessage( hwndEdit, EM_LINEFROMCHAR, SelStart, 0 ) + 1;
  1304. wsprintf(szBuf, TEXT("%d"), LineNum);
  1305. SetDlgItemText( hDlg, IDC_GOTO, szBuf );
  1306. SetFocus( hDlg );
  1307. return TRUE;
  1308. break;
  1309. // context sensitive help.
  1310. case WM_HELP:
  1311. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, szHelpFile,
  1312. HELP_WM_HELP, (ULONG_PTR) (LPVOID) s_GotoHelpIDs);
  1313. break;
  1314. case WM_CONTEXTMENU:
  1315. WinHelp((HWND) wParam, szHelpFile, HELP_CONTEXTMENU,
  1316. (ULONG_PTR) (LPVOID) s_GotoHelpIDs);
  1317. break;
  1318. case WM_COMMAND:
  1319. switch (LOWORD(wParam))
  1320. {
  1321. UINT CharIndex;
  1322. case IDC_GOTO:
  1323. return TRUE;
  1324. break;
  1325. case IDOK:
  1326. GetDlgItemText( hDlg, IDC_GOTO, szBuf, GOTOBUFSIZE );
  1327. // convert all unicode numbers to range L'0' to L'9'
  1328. FoldString( MAP_FOLDDIGITS, szBuf, -1, szBuf, GOTOBUFSIZE);
  1329. lGotoLine= _ttol( szBuf );
  1330. //
  1331. // see if valid line number
  1332. //
  1333. CharIndex= (UINT)SendMessage( hwndEdit,
  1334. EM_LINEINDEX,
  1335. lGotoLine-1,
  1336. 0);
  1337. if( lGotoLine > 0 && CharIndex != -1 )
  1338. {
  1339. EndDialog(hDlg, 0); // successfull
  1340. return TRUE;
  1341. }
  1342. //
  1343. // Invalid line number
  1344. // warning user and set to reasonable value
  1345. //
  1346. MessageBox( hDlg, szLineTooLarge, szLineError, MB_OK );
  1347. LineNum= (UINT)SendMessage( hwndEdit, EM_GETLINECOUNT, 0, 0 );
  1348. wsprintf(szBuf, TEXT("%d"), LineNum);
  1349. SetDlgItemText( hDlg, IDC_GOTO, szBuf );
  1350. SetFocus( hDlg );
  1351. break;
  1352. case IDCANCEL :
  1353. EndDialog(hDlg, 1 ); // cancelled
  1354. return TRUE;
  1355. break;
  1356. default:
  1357. break;
  1358. } // switch (wParam)
  1359. break;
  1360. default:
  1361. break;
  1362. } // switch (message)
  1363. return FALSE; // Didn't process a message
  1364. } // GotoDlgProc()