|
|
1.1 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: ! 12: #include "porttool.h" ! 13: #include "port.h" ! 14: ! 15: /* forward declarations of helper functions in this module */ ! 16: HWND WINAPI StartInteractive (HANDLE, char *, int); ! 17: HANDLE WINAPI StartBackground (HANDLE, HWND, char *); ! 18: VOID WINAPI InitializeMenu (HWND, HANDLE); ! 19: LONG WINAPI CommandHandler (HWND, UINT, LONG); ! 20: int WINAPI SetWrap (HWND); ! 21: BOOL WINAPI GetCmdLine (char *, char *, BOOL *); ! 22: int WINAPI Save_YNC (HWND); ! 23: VOID WINAPI SetWindowTitle (HWND, char*); ! 24: VOID WINAPI GetEditSubString (HWND, PUINT, PUINT, char *); ! 25: ! 26: UINT uSearchMsg; ! 27: HWND hDlgPortStatus; ! 28: HWND hDlgPort; ! 29: ! 30: ! 31: /* test case static variables for background porting */ ! 32: HANDLE hEvents[nBKPORTEVENTS]; ! 33: BKPORTFILESTRUCT BkPort; ! 34: ! 35: ! 36: ! 37: /* entry point ot this executable */ ! 38: int WINAPI WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow) ! 39: HANDLE hInstance; ! 40: HANDLE hPrevInstance; ! 41: LPSTR lpCmdLine; ! 42: int nCmdShow; ! 43: { ! 44: MSG msg; ! 45: HWND hWnd; ! 46: HANDLE hAccel; ! 47: BOOL bBkgnd = 0; ! 48: char *lpszBuff = NULL; ! 49: char *lpszCmdLine = NULL; ! 50: char *lpCL; ! 51: ! 52: ! 53: /* previous instances do not exist in Win32 */ ! 54: if (hPrevInstance) ! 55: return 0; ! 56: ! 57: /* parse and copy command line parameters to local memory */ ! 58: lpCL = GetCommandLine (); ! 59: if (lpszCmdLine = (char *)LocalAlloc (LPTR, strlen (lpCL) + 1)) ! 60: GetCmdLine (lpCL, lpszCmdLine, &bBkgnd); ! 61: ! 62: /* if /b switch, start background porting session */ ! 63: if (bBkgnd) ! 64: { ! 65: /* invoke background port status dialog */ ! 66: if (!(hDlgPortStatus = StartBackground (hInstance, NULL, lpszCmdLine))) ! 67: return FALSE; ! 68: } ! 69: ! 70: /* start interactive porting session */ ! 71: else ! 72: { ! 73: if (!(hWnd = StartInteractive (hInstance, lpszCmdLine, nCmdShow))) ! 74: return FALSE; ! 75: } ! 76: ! 77: /* free memory allocated for pCmdLine */ ! 78: if (lpszCmdLine) ! 79: LocalFree ((HLOCAL)lpszCmdLine); ! 80: ! 81: /* load main accelerator table */ ! 82: hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE (IDA_PORTTOOL)); ! 83: ! 84: /* main window message loop */ ! 85: while (GetMessage (&msg, NULL, 0, 0)) ! 86: { ! 87: if ((!hDlgSearch || !IsDialogMessage (hDlgSearch, &msg)) && ! 88: (!hDlgPort || !IsDialogMessage (hDlgPort, &msg)) && ! 89: (!hDlgPortStatus || !IsDialogMessage (hDlgPortStatus, &msg)) && ! 90: (!TranslateAccelerator (hWnd, hAccel, &msg))) ! 91: { ! 92: TranslateMessage (&msg); ! 93: DispatchMessage (&msg); ! 94: } ! 95: } ! 96: ! 97: /* return success of application */ ! 98: return TRUE; ! 99: } ! 100: ! 101: ! 102: ! 103: ! 104: /* start background port status dialog */ ! 105: HANDLE WINAPI StartBackground ( ! 106: HANDLE hModule, ! 107: HWND hWndParent, ! 108: char *lpszCmdLine) ! 109: { ! 110: return (CreateDialogParam (hModule, ! 111: IDD_BKPORTDIALOG, ! 112: hWndParent, ! 113: BkPortDlgProc, ! 114: (LPARAM)lpszCmdLine)); ! 115: } ! 116: ! 117: ! 118: ! 119: ! 120: /* start interactive version of app */ ! 121: HWND WINAPI StartInteractive ( ! 122: HANDLE hInstance, ! 123: char *lpszCmdLine, ! 124: int nCmdShow) ! 125: { ! 126: WNDCLASS wc; ! 127: char lpszClass[MAX_PATH]; ! 128: HWND hWnd; ! 129: ! 130: /* load resources strings */ ! 131: LoadString (hInstance, IDS_APPNAME, lpszClass, sizeof (lpszClass)); ! 132: ! 133: /* Register the frame class */ ! 134: wc.style = 0; ! 135: wc.lpfnWndProc = (WNDPROC)MainWndProc; ! 136: wc.cbClsExtra = 0; ! 137: wc.cbWndExtra = CBWNDEXTRA; ! 138: wc.hInstance = hInstance; ! 139: wc.hIcon = LoadIcon (hInstance, IDPortTool); ! 140: wc.hCursor = LoadCursor (NULL,IDC_ARROW); ! 141: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); ! 142: wc.lpszMenuName = IDPortTool; ! 143: wc.lpszClassName = lpszClass; ! 144: ! 145: if (!RegisterClass (&wc) ) ! 146: return FALSE; ! 147: ! 148: /* Create the frame */ ! 149: hWnd = CreateWindow (lpszClass, ! 150: "Win32 Port", ! 151: WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | DS_LOCALEDIT, ! 152: CW_USEDEFAULT, ! 153: 0, ! 154: CW_USEDEFAULT, ! 155: 0, ! 156: NULL, ! 157: NULL, ! 158: hInstance, ! 159: (*lpszCmdLine ? lpszCmdLine : 0)); ! 160: ! 161: /* make sure window was created */ ! 162: if (!hWnd) ! 163: return FALSE; ! 164: ! 165: /* register search/replace message for common dialog use */ ! 166: uSearchMsg = RegisterWindowMessage ((char *)FINDMSGSTRING); ! 167: ! 168: /* show and update main window */ ! 169: ShowWindow (hWnd, nCmdShow); ! 170: UpdateWindow (hWnd); ! 171: ! 172: return hWnd; ! 173: } ! 174: ! 175: ! 176: ! 177: /* main window procedure */ ! 178: LONG WINAPI MainWndProc ( ! 179: HWND hWnd, ! 180: UINT uMsg, ! 181: UINT uParam, ! 182: LONG lParam) ! 183: { ! 184: LONG lRet = 1; ! 185: int nResult; ! 186: RECT rc; ! 187: ! 188: switch (uMsg) ! 189: { ! 190: case WM_CREATE: ! 191: { ! 192: HWND hWndEdit; ! 193: LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam; ! 194: char lpszBuff[MAX_PATH]; ! 195: LOGFONT lfEditFont; ! 196: HFONT hFont; ! 197: HCURSOR hOldCursor; ! 198: ! 199: /* put hourglass cursor up */ ! 200: hOldCursor = (HCURSOR)SetClassLong (hWnd, GCL_HCURSOR, 0); ! 201: SetCursor (LoadCursor (NULL, IDC_WAIT)); ! 202: ! 203: /* Create an edit control */ ! 204: GetClientRect (hWnd, &rc); ! 205: hWndEdit = CreateWindow ("edit", ! 206: " ", ! 207: WS_CHILD | WS_VISIBLE | DS_LOCALEDIT | ! 208: WS_HSCROLL | WS_VSCROLL | ! 209: ES_AUTOHSCROLL | ES_AUTOVSCROLL | ! 210: ES_MULTILINE | ES_NOHIDESEL, ! 211: rc.left, ! 212: rc.top, ! 213: rc.right-rc.left, ! 214: rc.bottom-rc.top, ! 215: hWnd, ! 216: (HMENU)IDC_EDIT, ! 217: (HANDLE)GetModuleHandle (NULL), ! 218: 0); ! 219: ! 220: /* if edit control failed, abort aplication */ ! 221: if (!IsWindow (hWndEdit)) ! 222: { ! 223: ErrorNotify (hWnd, IDS_EDITWNDFAILED); ! 224: return FALSE; ! 225: } ! 226: ! 227: /* increase edit control max character limit beyond 30,000 default */ ! 228: SendMessage (hWndEdit, EM_LIMITTEXT, 0x0FFFFFFF, 0); ! 229: ! 230: /* save edit window handle and init state variables */ ! 231: SetWindowLong (hWnd, WL_HWNDEDIT, (LONG) hWndEdit); ! 232: SetWindowWord (hWnd, WW_SCROLL, TRUE); ! 233: SetWindowWord (hWnd, WW_UNTITLED, TRUE); ! 234: SetWindowWord (hWnd, WW_SEARCHCASE, TRUE); ! 235: SetWindowWord (hWnd, WW_SEARCHDN, TRUE); ! 236: SetWindowLong (hWnd, WL_HPTRDEVNAMES, 0); ! 237: ! 238: /* get printer configuration */ ! 239: if (!GetPrinterConfig (hWnd)) ! 240: ErrorNotify (hWnd, IDS_PTRCONFIGFAILED); ! 241: ! 242: /* if initialization file passed, load file now */ ! 243: if (lpcs->lpCreateParams) ! 244: { ! 245: /* load filename passed at initialization */ ! 246: if ((nResult = LoadFile (hWnd, lpcs->lpCreateParams)) == TRUE ) ! 247: { ! 248: /* save filename and path in global string */ ! 249: strcpy (lpszFilePath, lpcs->lpCreateParams); ! 250: ! 251: /* The file has a title, so reset the UNTITLED flag. */ ! 252: SetWindowWord(hWnd, WW_UNTITLED, FALSE); ! 253: ! 254: /* extract filename from path */ ! 255: GetFileFromPath (lpszFilePath, lpszBuff); ! 256: ! 257: /* set window text title to be "AppName - filename" */ ! 258: SetWindowTitle (hWnd, lpszBuff); ! 259: } ! 260: else ! 261: { ! 262: /* notify user of error */ ! 263: ErrorNotify (hWnd, nResult); ! 264: ! 265: /* set window title to "AppName - Untitled" */ ! 266: LoadString ((HANDLE)GetModuleHandle (NULL), ! 267: IDS_UNTITLED, ! 268: lpszBuff, ! 269: sizeof (lpszBuff)); ! 270: SetWindowTitle (hWnd, lpszBuff); ! 271: } ! 272: } ! 273: else ! 274: { ! 275: /* set window title to "AppName - Untitled" */ ! 276: LoadString ((HANDLE)GetModuleHandle (NULL), ! 277: IDS_UNTITLED, ! 278: lpszBuff, ! 279: sizeof (lpszBuff)); ! 280: SetWindowTitle (hWnd, lpszBuff); ! 281: ! 282: /* set global file and path variables to null */ ! 283: *lpszFilePath = 0; ! 284: } ! 285: ! 286: /* create fixed pitch font as default font */ ! 287: lfEditFont.lfHeight = 16; ! 288: lfEditFont.lfWidth = 0; ! 289: lfEditFont.lfEscapement = 0; ! 290: lfEditFont.lfOrientation = 0; ! 291: lfEditFont.lfWeight = 400; ! 292: lfEditFont.lfItalic = FALSE; ! 293: lfEditFont.lfUnderline = FALSE; ! 294: lfEditFont.lfStrikeOut = FALSE; ! 295: lfEditFont.lfCharSet = ANSI_CHARSET; ! 296: lfEditFont.lfOutPrecision = OUT_DEFAULT_PRECIS; ! 297: lfEditFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; ! 298: lfEditFont.lfQuality = DEFAULT_QUALITY; ! 299: lfEditFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN; ! 300: *lfEditFont.lfFaceName = 0; ! 301: ! 302: /* make scroll bars initially visible */ ! 303: SetScrollRange (hWndEdit, SB_VERT, 0, 100, TRUE); ! 304: SetScrollRange (hWndEdit, SB_HORZ, 0, 100, TRUE); ! 305: ! 306: /* create the logical font */ ! 307: if (hFont = CreateFontIndirect (&lfEditFont)) ! 308: SendMessage (hWndEdit, WM_SETFONT, (UINT)hFont, 0); ! 309: else ! 310: ErrorNotify (hWnd, IDS_FONTFAILEDTOCREATE); ! 311: ! 312: /* remove hourglass cursor */ ! 313: SetClassLong (hWnd, GCL_HCURSOR, (LONG)hOldCursor); ! 314: SetCursor (hOldCursor); ! 315: ! 316: /* set focus to edit window */ ! 317: SetFocus (hWndEdit); ! 318: } ! 319: break; ! 320: ! 321: case WM_SIZE: ! 322: { ! 323: RECT rc; ! 324: HWND hWndEdit; ! 325: ! 326: /* resize the edit control to match the client area */ ! 327: hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT); ! 328: GetClientRect (hWnd, &rc); ! 329: MoveWindow (hWndEdit, ! 330: rc.left, ! 331: rc.top, ! 332: rc.right-rc.left, ! 333: rc.bottom-rc.top, ! 334: TRUE); ! 335: } ! 336: break; ! 337: ! 338: case WM_SETFOCUS: ! 339: SetFocus ((HWND)GetWindowLong (hWnd, WL_HWNDEDIT)); ! 340: break; ! 341: ! 342: case WM_INITMENU: ! 343: /* initialize menuitems */ ! 344: InitializeMenu (hWnd, (HMENU)uParam); ! 345: break; ! 346: ! 347: case WM_WININICHANGE: ! 348: case WM_DEVMODECHANGE: ! 349: /* get printer configuration */ ! 350: GetPrinterConfig (hWnd); ! 351: break; ! 352: ! 353: case WM_COMMAND: ! 354: /* handle all command messages in a localized function */ ! 355: lRet = CommandHandler (hWnd, uParam, lParam); ! 356: break; ! 357: ! 358: case WM_CLOSE: ! 359: case WM_QUERYENDSESSION: ! 360: { ! 361: HWND hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT); ! 362: ! 363: /* check if there are changes to the edit contents, and offer to save first */ ! 364: if (SendMessage (hWndEdit, EM_GETMODIFY, 0, 0)) ! 365: { ! 366: switch (Save_YNC (hWnd)) ! 367: { ! 368: case IDCANCEL: ! 369: /* abort exit */ ! 370: return FALSE; ! 371: case IDYES: ! 372: /* save data then continue */ ! 373: if (!SendMessage (hWnd, WM_COMMAND, (UINT)UM_SAVEFILE, 0)) ! 374: return FALSE; ! 375: case IDNO: ! 376: /* just fall through */ ! 377: break; ! 378: } ! 379: } ! 380: ! 381: /* call destroy window to cleanup and go away */ ! 382: DestroyWindow (hWnd); ! 383: } ! 384: break; ! 385: ! 386: case WM_DESTROY: ! 387: { ! 388: HFONT hFont = (HFONT)SendMessage ((HWND)GetWindowLong (hWnd, WL_HWNDEDIT), ! 389: WM_GETFONT, ! 390: 0, ! 391: 0); ! 392: ! 393: /* destroy font handle from edit control if exists */ ! 394: if (hFont) ! 395: DeleteObject (hFont); ! 396: ! 397: PostQuitMessage (0); ! 398: } ! 399: break; ! 400: ! 401: default: ! 402: /* process common dialog search message here, then break */ ! 403: if (uMsg == uSearchMsg) ! 404: { ! 405: LPFINDREPLACE lpfr = (LPFINDREPLACE)lParam; ! 406: ! 407: if (lpfr->Flags & FR_DIALOGTERM) ! 408: { ! 409: /* save case and direction */ ! 410: SetWindowWord (hWnd, WW_SEARCHCASE, (WORD)(lpfr->Flags & FR_MATCHCASE)); ! 411: SetWindowWord (hWnd, WW_SEARCHDN, (WORD)(lpfr->Flags & FR_DOWN)); ! 412: hDlgSearch = NULL; ! 413: } ! 414: ! 415: else if (lpfr->Flags & FR_FINDNEXT) ! 416: if (!LocateText (hWnd, ! 417: (WORD)(lpfr->Flags & FR_MATCHCASE), ! 418: (WORD)(lpfr->Flags & FR_DOWN), ! 419: lpszSearch)) ! 420: ErrorNotify (hWnd, IDS_STRINGNOTFOUND); ! 421: break; ! 422: } ! 423: ! 424: /* pass all unhandled messages to DefWindowProc */ ! 425: lRet = DefWindowProc (hWnd, uMsg, uParam, lParam); ! 426: break; ! 427: } ! 428: ! 429: /* return 1 if handled message, 0 if not */ ! 430: return lRet; ! 431: } ! 432: ! 433: ! 434: ! 435: /* about box dialog procedure */ ! 436: BOOL WINAPI AboutDlgProc ( ! 437: HWND hwnd, ! 438: UINT uMsg, ! 439: UINT uParam, ! 440: LONG lParam) ! 441: { ! 442: BOOL bRet = TRUE; ! 443: ! 444: switch (uMsg) ! 445: { ! 446: case WM_COMMAND: ! 447: switch (LOWORD (uParam)) ! 448: { ! 449: /* end dialog with TRUE or FALSE for OK or CANCEL */ ! 450: case IDOK: ! 451: case IDCANCEL: ! 452: EndDialog(hwnd, (LOWORD (uParam) == IDOK)); ! 453: break; ! 454: ! 455: default: ! 456: bRet = FALSE; ! 457: } ! 458: break; ! 459: ! 460: default: ! 461: bRet = FALSE; ! 462: break; ! 463: } ! 464: return bRet; ! 465: } ! 466: ! 467: ! 468: ! 469: /* initialize menu function */ ! 470: VOID WINAPI InitializeMenu ( ! 471: HWND hWnd, ! 472: HANDLE hMenu) ! 473: { ! 474: WORD mfStatus; ! 475: DWORD dwSel; ! 476: HWND hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT); ! 477: WORD wScroll; ! 478: ! 479: /* see if edit control can undo last command */ ! 480: if (SendMessage (hWndEdit, EM_CANUNDO, 0, 0L)) ! 481: mfStatus = MF_ENABLED; ! 482: else ! 483: mfStatus = MF_GRAYED; ! 484: EnableMenuItem (hMenu, IDM_EDITUNDO, mfStatus); ! 485: ! 486: /* enable menuitems CUT, COPY and CLEAR if selected text */ ! 487: dwSel = SendMessage (hWndEdit, EM_GETSEL, 0, 0); ! 488: mfStatus = (WORD)((LOWORD(dwSel) == HIWORD(dwSel))?MF_GRAYED:MF_ENABLED); ! 489: EnableMenuItem (hMenu, IDM_EDITCUT, mfStatus); ! 490: EnableMenuItem (hMenu, IDM_EDITCOPY, mfStatus); ! 491: EnableMenuItem (hMenu, IDM_EDITCLEAR, mfStatus); ! 492: ! 493: /* if text allow for new file, enable menuitem */ ! 494: if (SendMessage (hWndEdit, WM_GETTEXTLENGTH, 0, 0)) ! 495: EnableMenuItem (hMenu, IDM_FILENEW, MF_ENABLED); ! 496: else ! 497: EnableMenuItem (hMenu, IDM_FILENEW, MF_GRAYED); ! 498: ! 499: /* if CF_TEXT format data is available in clipboard */ ! 500: if (OpenClipboard (hWnd) & IsClipboardFormatAvailable (CF_TEXT)) ! 501: EnableMenuItem (hMenu, IDM_EDITPASTE, MF_ENABLED); ! 502: else ! 503: EnableMenuItem (hMenu, IDM_EDITPASTE, MF_GRAYED); ! 504: CloseClipboard (); ! 505: ! 506: /* set the scroll bars menuitem */ ! 507: wScroll = GetWindowWord (hWnd, WW_SCROLL); ! 508: CheckMenuItem (hMenu, IDM_EDITSCROLL, (UINT)(wScroll ? MF_CHECKED : MF_UNCHECKED)); ! 509: ! 510: /* set the word wrap state for the window */ ! 511: dwSel = GetWindowLong (hWndEdit, GWL_STYLE); ! 512: CheckMenuItem (hMenu, ! 513: IDM_EDITWRAP, ! 514: (UINT)((dwSel & ES_AUTOHSCROLL) ? MF_UNCHECKED : MF_CHECKED)); ! 515: ! 516: /* enable search menuitems if a search string exists */ ! 517: if (*lpszSearch) ! 518: { ! 519: EnableMenuItem (hMenu, IDM_SEARCHNEXT, MF_ENABLED); ! 520: EnableMenuItem (hMenu, IDM_SEARCHPREV, MF_ENABLED); ! 521: } ! 522: else ! 523: { ! 524: EnableMenuItem (hMenu, IDM_SEARCHNEXT, MF_GRAYED); ! 525: EnableMenuItem (hMenu, IDM_SEARCHPREV, MF_GRAYED); ! 526: } ! 527: ! 528: /* enable Print menuitems if a printer is available */ ! 529: if (GetWindowLong (hWnd, WL_HPTRDEVNAMES)) ! 530: { ! 531: EnableMenuItem (hMenu, IDM_FILEPRINT, MF_ENABLED); ! 532: EnableMenuItem ( hMenu, IDM_FILESETUP, MF_ENABLED); ! 533: } ! 534: else ! 535: { ! 536: EnableMenuItem (hMenu, IDM_FILEPRINT, MF_GRAYED); ! 537: EnableMenuItem ( hMenu, IDM_FILESETUP, MF_GRAYED); ! 538: } ! 539: ! 540: /* menuitems that are always enabled */ ! 541: EnableMenuItem(hMenu, IDM_EDITSCROLL, MF_ENABLED); ! 542: EnableMenuItem(hMenu, IDM_EDITSELECT, MF_ENABLED); ! 543: EnableMenuItem(hMenu, IDM_EDITWRAP, MF_ENABLED); ! 544: EnableMenuItem(hMenu, IDM_SEARCHFIND, MF_ENABLED); ! 545: } ! 546: ! 547: ! 548: ! 549: /* handle all WM_COMMAND messages here */ ! 550: LONG WINAPI CommandHandler ( ! 551: HWND hWnd, ! 552: UINT uParam, ! 553: LONG lParam) ! 554: { ! 555: int nResult; ! 556: HWND hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT); ! 557: ! 558: switch (LOWORD(uParam)) ! 559: { ! 560: case IDM_FILENEW: ! 561: { ! 562: char lpszMsg[MAX_PATH]; ! 563: ! 564: /* check if there are changes, and offer to save first */ ! 565: if (SendMessage (hWndEdit, EM_GETMODIFY, 0, 0)) ! 566: { ! 567: switch (Save_YNC (hWnd)) ! 568: { ! 569: case IDCANCEL: ! 570: /* abort operation */ ! 571: return FALSE; ! 572: case IDYES: ! 573: /* save data then continue if successful */ ! 574: if (!SendMessage (hWnd, WM_COMMAND, (UINT)UM_SAVEFILE, 0)) ! 575: return FALSE; ! 576: case IDNO: ! 577: /* just fall through */ ! 578: break; ! 579: } ! 580: ! 581: /* reset edit changes flag */ ! 582: SendMessage (hWndEdit, EM_SETMODIFY, FALSE, 0); ! 583: } ! 584: ! 585: /* clear edit control */ ! 586: SetWindowText (hWndEdit, ""); ! 587: ! 588: /* set window title to "Untitled" */ ! 589: LoadString ((HANDLE)GetModuleHandle (NULL), ! 590: IDS_UNTITLED, ! 591: lpszMsg, ! 592: sizeof (lpszMsg)); ! 593: SetWindowTitle (hWnd, lpszMsg); ! 594: SetWindowWord (hWnd, WW_UNTITLED, TRUE); ! 595: ! 596: /* reset caret position by sending WM_KILLFOCUS/WM_SETFOCUS messages */ ! 597: SetFocus (NULL); ! 598: SetFocus (hWndEdit); ! 599: } ! 600: break; ! 601: ! 602: case IDM_FILEOPEN: ! 603: { ! 604: char lpszFileName[MAX_PATH]; ! 605: int nResult; ! 606: ! 607: /* if changes to current file, ask to save first */ ! 608: if (SendMessage (hWndEdit, EM_GETMODIFY, 0, 0)) ! 609: { ! 610: switch (Save_YNC (hWnd)) ! 611: { ! 612: case IDCANCEL: ! 613: /* abort operation */ ! 614: return FALSE; ! 615: case IDYES: ! 616: /* save data then continue */ ! 617: if (!SendMessage (hWnd, WM_COMMAND, (UINT)UM_SAVEFILE, 0)) ! 618: return FALSE; ! 619: case IDNO: ! 620: /* just fall through */ ! 621: break; ! 622: } ! 623: ! 624: /* reset edit changes flag */ ! 625: SendMessage (hWndEdit, EM_SETMODIFY, FALSE, 0); ! 626: } ! 627: ! 628: /* get new filename and load into edit control */ ! 629: if (GetFileName (hWnd, lpszFileName, lpszFilePath)) ! 630: { ! 631: if ((nResult = LoadFile (hWnd, lpszFilePath)) != TRUE ) ! 632: /* notify user of error */ ! 633: ErrorNotify (hWnd, nResult); ! 634: else ! 635: { ! 636: /* update window text */ ! 637: SetWindowTitle (hWnd, lpszFileName); ! 638: SetWindowWord (hWnd, WW_UNTITLED, FALSE); ! 639: } ! 640: } ! 641: else ! 642: return FALSE; ! 643: } ! 644: break; ! 645: ! 646: case IDM_FILESAVEAS: ! 647: { ! 648: char lpszFileTitle[MAX_PATH]; ! 649: int nResult; ! 650: ! 651: /* get a valid filename and path */ ! 652: if (SaveAsFileName (hWnd, lpszFileTitle, lpszFilePath)) ! 653: { ! 654: /* if file saves okay */ ! 655: if ((nResult = SaveFile (hWnd, lpszFilePath)) > 0) ! 656: { ! 657: /* reset changed flag, and window title */ ! 658: SendMessage (hWndEdit, EM_SETMODIFY, FALSE, 0); ! 659: SetWindowTitle (hWnd, lpszFileTitle); ! 660: SetWindowWord (hWnd, WW_UNTITLED, FALSE); ! 661: } ! 662: else ! 663: { ! 664: /* unable to save file, return error */ ! 665: ErrorNotify (hWnd, nResult); ! 666: return FALSE; ! 667: } ! 668: } ! 669: else ! 670: return FALSE; ! 671: } ! 672: break; ! 673: ! 674: case IDM_FILESAVE: ! 675: { ! 676: char lpszFileTitle[MAX_PATH]; ! 677: int nResult; ! 678: ! 679: /* if called from menu, check to see if file modified */ ! 680: if (!lParam && ! 681: SendMessage (hWndEdit, EM_GETMODIFY, 0, 0)) ! 682: return FALSE; ! 683: ! 684: /* user defined msg is sent from Exit, Open and New menuitems */ ! 685: case UM_SAVEFILE: ! 686: ! 687: /* if untitled, get new name */ ! 688: if (GetWindowWord (hWnd, WW_UNTITLED) && ! 689: !(SaveAsFileName(hWnd, lpszFileTitle, lpszFilePath))) ! 690: { ! 691: /* make sure no name was saved */ ! 692: *lpszFilePath = 0; ! 693: return FALSE; ! 694: } ! 695: ! 696: /* save buffer and reset flag */ ! 697: if ((nResult = SaveFile (hWnd, lpszFilePath)) > 0) ! 698: { ! 699: /* if new file name, set window title */ ! 700: if (*lpszFileTitle) ! 701: { ! 702: SetWindowTitle (hWnd, lpszFileTitle); ! 703: SetWindowWord (hWnd, WW_UNTITLED, FALSE); ! 704: } ! 705: SendMessage (hWndEdit, EM_SETMODIFY, FALSE, 0); ! 706: } ! 707: else ! 708: { ! 709: /* unable to save file, return error */ ! 710: ErrorNotify (hWnd, nResult); ! 711: return FALSE; ! 712: } ! 713: } ! 714: break; ! 715: ! 716: case IDM_FILEPRINT: ! 717: { ! 718: int nRes; ! 719: ! 720: /* print the file */ ! 721: if ((nRes = PrintFile (hWnd)) < 0) ! 722: ErrorNotify (hWnd, nRes); ! 723: } ! 724: break; ! 725: ! 726: case IDM_FILESETUP: ! 727: { ! 728: int nRes; ! 729: ! 730: /* set up the printer environment */ ! 731: if ((nRes = PrinterSetup (hWnd)) < 0) ! 732: ErrorNotify (hWnd, nRes); ! 733: } ! 734: break; ! 735: ! 736: case IDM_FILEEXIT: ! 737: /* exit application */ ! 738: PostMessage (hWnd, WM_CLOSE, 0, 0L); ! 739: break; ! 740: ! 741: case IDM_FILEABOUT: ! 742: /* call about dialog box */ ! 743: DialogBox ((HANDLE)GetModuleHandle (NULL), ! 744: (LPSTR)IDD_ABOUT, ! 745: hWnd, ! 746: AboutDlgProc); ! 747: break; ! 748: ! 749: case IDM_EDITUNDO: ! 750: SendMessage (hWndEdit, EM_UNDO, 0, 0L); ! 751: break; ! 752: ! 753: case IDM_EDITCUT: ! 754: SendMessage (hWndEdit, WM_CUT, 0, 0L); ! 755: break; ! 756: ! 757: case IDM_EDITCOPY: ! 758: SendMessage (hWndEdit, WM_COPY, 0, 0L); ! 759: break; ! 760: ! 761: case IDM_EDITPASTE: ! 762: SendMessage (hWndEdit, WM_PASTE, 0, 0L); ! 763: break; ! 764: ! 765: case IDM_EDITCLEAR: ! 766: SendMessage (hWndEdit, WM_CUT, 0, 0L); ! 767: break; ! 768: ! 769: case IDM_EDITSELECT: ! 770: /* 0, -1 selects the entire string */ ! 771: SendMessage (hWndEdit, EM_SETSEL, 0, (LONG)(int)-1); ! 772: break; ! 773: ! 774: case IDM_EDITSCROLL: ! 775: { ! 776: WORD wScroll = !GetWindowWord (hWnd, WW_SCROLL); ! 777: ! 778: /* toggle vertical scroll bar */ ! 779: SetScrollRange (hWndEdit, SB_VERT, 0, (int)(wScroll ? 100 : 0), TRUE); ! 780: ! 781: /* if not word wrap, toggle horizontal scroll */ ! 782: if (ES_AUTOHSCROLL & GetWindowLong (hWndEdit, GWL_STYLE)) ! 783: SetScrollRange (hWndEdit, SB_HORZ, 0, (int)(wScroll ? 100 : 0), TRUE); ! 784: ! 785: /* set scroll flag */ ! 786: SetWindowWord (hWnd, WW_SCROLL, wScroll); ! 787: } ! 788: break; ! 789: ! 790: case IDM_EDITWRAP: ! 791: /* change edit control */ ! 792: if ((nResult = SetWrap (hWnd)) < 0) ! 793: /* notify user of error */ ! 794: ErrorNotify (hWnd, nResult); ! 795: break; ! 796: ! 797: case IDM_EDITFONT: ! 798: { ! 799: CHOOSEFONT cf; ! 800: LOGFONT lfEditFont; ! 801: HFONT hEditFont; ! 802: int iSize=0; ! 803: ! 804: /* get logical font structure from current font */ ! 805: if (hEditFont = (HFONT)SendMessage (hWndEdit, WM_GETFONT, 0, 0)) ! 806: { ! 807: GetObject (hEditFont, sizeof (lfEditFont), &lfEditFont); ! 808: cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS; ! 809: } ! 810: else ! 811: cf.Flags = CF_SCREENFONTS; ! 812: ! 813: /* call common dialog to select a font */ ! 814: cf.lStructSize = sizeof (CHOOSEFONT); ! 815: cf.hwndOwner = hWnd; ! 816: cf.hDC = NULL; ! 817: cf.lpLogFont = &lfEditFont; ! 818: cf.iPointSize = iSize; ! 819: cf.rgbColors = 0; ! 820: cf.lCustData = 0; ! 821: cf.lpfnHook = NULL; ! 822: cf.lpTemplateName = NULL; ! 823: cf.hInstance = NULL; ! 824: cf.lpszStyle = NULL; ! 825: cf.nFontType = SCREEN_FONTTYPE; ! 826: cf.nSizeMin = 0; ! 827: cf.nSizeMax = 0; ! 828: ! 829: if (ChooseFont (&cf)) ! 830: { ! 831: /* create new font and put in edit control */ ! 832: SendMessage (hWndEdit, ! 833: WM_SETFONT, ! 834: (UINT)CreateFontIndirect ! 835: (&lfEditFont), ! 836: TRUE); ! 837: ! 838: if (hEditFont) ! 839: DeleteObject (hEditFont); ! 840: } ! 841: } ! 842: break; ! 843: ! 844: case IDM_SEARCHFIND: ! 845: { ! 846: UINT uBegSel, uEndSel; ! 847: WORD wCase = GetWindowWord (hWnd, WW_SEARCHCASE); ! 848: WORD wDir = GetWindowWord (hWnd, WW_SEARCHDN); ! 849: ! 850: /* if selected text, replace global search string */ ! 851: SendMessage (hWndEdit, EM_GETSEL, (UINT)&uBegSel, (UINT)&uEndSel); ! 852: if (uBegSel != uEndSel) ! 853: GetEditSubString (hWndEdit, &uBegSel, &uEndSel, lpszSearch); ! 854: ! 855: /* Put up the find dialog box */ ! 856: if (!FindDialog (hWnd, wCase, wDir, lpszSearch)) ! 857: ErrorNotify (hWnd, IDS_SEARCHDLGFAILED); ! 858: } ! 859: break; ! 860: ! 861: case IDM_SEARCHNEXT: ! 862: /* locate next search string in edit control */ ! 863: if (!LocateText (hWnd, ! 864: GetWindowWord (hWnd, WW_SEARCHCASE), ! 865: TRUE, ! 866: lpszSearch)) ! 867: ErrorNotify (hWnd, IDS_STRINGNOTFOUND); ! 868: break; ! 869: ! 870: case IDM_SEARCHPREV: ! 871: /* locate previous search string in edit control */ ! 872: if (!LocateText (hWnd, ! 873: GetWindowWord (hWnd, WW_SEARCHCASE), ! 874: FALSE, ! 875: lpszSearch)) ! 876: ErrorNotify (hWnd, IDS_STRINGNOTFOUND); ! 877: break; ! 878: ! 879: case IDM_PORTCURFILE: ! 880: /* if untitled, save first */ ! 881: if (GetWindowWord (hWnd, WW_UNTITLED) && ! 882: !SendMessage (hWnd, WM_COMMAND, IDM_FILEOPEN, 0)) ! 883: { ! 884: ErrorNotify (hWnd, IDS_NOFILETOPORT); ! 885: break; ! 886: } ! 887: ! 888: /* start port dialog with flags */ ! 889: hDlgPort = CreateDialog (GetModuleHandle (NULL), ! 890: IDD_PORTDIALOG, ! 891: hWnd, ! 892: PortDlgProc); ! 893: break; ! 894: ! 895: case IDM_PORTBKGND: ! 896: if (IsWindow (hDlgPortStatus)) ! 897: ShowWindow (hDlgPortStatus, SW_SHOWNORMAL); ! 898: else if (!(hDlgPortStatus = StartBackground (GetModuleHandle (NULL), ! 899: hWnd, ! 900: NULL))) ! 901: ErrorNotify (hWnd, IDS_BKPORTSTARTFAILED); ! 902: break; ! 903: ! 904: default: ! 905: return FALSE; ! 906: } ! 907: return TRUE; ! 908: } ! 909: ! 910: ! 911: ! 912: /* change the word wrapping in an edit control */ ! 913: int WINAPI SetWrap ( ! 914: HWND hWnd) ! 915: ! 916: { ! 917: DWORD dwStyle; ! 918: HANDLE hOldData, hNewData; ! 919: char *lpOldData, *lpNewData; ! 920: HWND hWndOld = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT); ! 921: HWND hWndNew; ! 922: RECT rc; ! 923: UINT uLen; ! 924: UINT uBegSel, uEndSel; ! 925: WORD wScroll = GetWindowWord (hWnd, WW_SCROLL); ! 926: HFONT hEditFont = (HFONT)SendMessage (hWndOld, WM_GETFONT, 0, 0); ! 927: ! 928: ! 929: /* turn off scroll bars if currently present */ ! 930: if (wScroll) ! 931: { ! 932: SetScrollRange (hWndOld, SB_VERT, 0, 0, FALSE); ! 933: SetScrollRange (hWndOld, SB_HORZ, 0, 0, FALSE); ! 934: } ! 935: ! 936: /* new edit style = old style XOR ES_AUTOHSCROLL */ ! 937: dwStyle = ES_AUTOHSCROLL ^ (DWORD)GetWindowLong (hWndOld, GWL_STYLE); ! 938: ! 939: /* save text selection */ ! 940: SendMessage (hWndOld, EM_GETSEL, (UINT)&uBegSel, (UINT)&uEndSel); ! 941: ! 942: /* create a new edit control with the new style */ ! 943: GetClientRect (hWnd, &rc); ! 944: hWndNew = CreateWindow ("edit", ! 945: "", ! 946: dwStyle, ! 947: rc.left, ! 948: rc.top, ! 949: rc.right-rc.left, ! 950: rc.bottom-rc.top, ! 951: hWnd, ! 952: (HMENU)IDC_EDIT, ! 953: (HANDLE)GetModuleHandle (NULL), ! 954: 0); ! 955: ! 956: /* check for window created */ ! 957: if (!IsWindow (hWndNew)) ! 958: return IDS_EDITWNDFAILED; ! 959: ! 960: /* reset to no format lines */ ! 961: SendMessage (hWndOld, EM_FMTLINES, !(dwStyle & ES_AUTOHSCROLL), 0); ! 962: ! 963: /* get the data handle of the old control */ ! 964: if (!(hOldData = (HANDLE)SendMessage (hWndOld, EM_GETHANDLE, 0, 0))) ! 965: { ! 966: /* delete new edit window and return error */ ! 967: DestroyWindow (hWndNew); ! 968: return IDS_GETHANDLEFAILED; ! 969: } ! 970: ! 971: /* get data handle to new edit control and reallocate to size of old edit text */ ! 972: hNewData = (HANDLE)SendMessage (hWndNew, EM_GETHANDLE, 0, 0); ! 973: uLen = GetWindowTextLength (hWndOld); ! 974: if (LocalReAlloc(hNewData, uLen+1, LHND) == NULL) ! 975: { ! 976: /* abort, clean up and return error */ ! 977: DestroyWindow (hWndNew); ! 978: return IDS_REALLOCFAILED; ! 979: } ! 980: ! 981: lpOldData = LocalLock (hOldData); ! 982: lpNewData = LocalLock (hNewData); ! 983: lpNewData[uLen+1] = 0; ! 984: ! 985: /* copy from one buffer to the other */ ! 986: while (uLen-- > 0) ! 987: lpNewData[uLen] = lpOldData[uLen]; ! 988: ! 989: /* unlock and release data buffers */ ! 990: LocalUnlock (hOldData); ! 991: LocalUnlock (hNewData); ! 992: ! 993: /* swap windows */ ! 994: SetWindowLong (hWnd, WL_HWNDEDIT, (LONG)hWndNew); ! 995: DestroyWindow (hWndOld); ! 996: SendMessage (hWndNew, EM_SETHANDLE, (UINT)hNewData, 0); ! 997: ! 998: /* set line formatting */ ! 999: SendMessage (hWndNew, EM_FMTLINES, !(dwStyle & ES_AUTOHSCROLL), 0); ! 1000: ! 1001: /* replace font in new edit control */ ! 1002: if (hEditFont) ! 1003: SendMessage (hWndNew, WM_SETFONT, (UINT)hEditFont, 0); ! 1004: ! 1005: /* set scroll bars if appropriate */ ! 1006: if (wScroll) ! 1007: { ! 1008: SetScrollRange (hWndNew, SB_VERT, 0, 100, TRUE); ! 1009: SetScrollRange (hWndNew, SB_HORZ, 0, (ES_AUTOHSCROLL & dwStyle ? 100 : 0), TRUE); ! 1010: } ! 1011: ! 1012: /* restore text selection, repaint and set focus */ ! 1013: InvalidateRect (hWndNew, NULL, TRUE); ! 1014: UpdateWindow (hWndNew); ! 1015: SendMessage (hWndNew, EM_SETSEL, uBegSel, uEndSel); ! 1016: SetFocus (hWndNew); ! 1017: ! 1018: /* return success */ ! 1019: return TRUE; ! 1020: } ! 1021: ! 1022: ! 1023: ! 1024: /* get win32 command line parameters */ ! 1025: BOOL WINAPI GetCmdLine( ! 1026: char *lpStr, ! 1027: char *lpszCmdLine, ! 1028: BOOL *bBkgnd) ! 1029: { ! 1030: if (*lpStr) ! 1031: { ! 1032: /* skip application name which precedes parameters */ ! 1033: while (*lpStr != ' ' && *lpStr != 0) ! 1034: lpStr++; ! 1035: ! 1036: /* skip spaces */ ! 1037: while (*lpStr == ' ' && *lpStr != 0) ! 1038: lpStr++; ! 1039: ! 1040: /* indeed command line parameter(s) present */ ! 1041: if (*lpStr != 0) ! 1042: { ! 1043: /* if background switch, set flag and remove switch from command line */ ! 1044: if ((*lpStr == '/' || *lpStr == '-') && ! 1045: (*(lpStr+1) == 'b' || *(lpStr+1) == 'B')) ! 1046: { ! 1047: *bBkgnd = TRUE; ! 1048: lpStr += 2; ! 1049: ! 1050: if (*lpStr == 0) ! 1051: *lpszCmdLine = 0; ! 1052: else ! 1053: strcpy (lpszCmdLine, lpStr); ! 1054: } ! 1055: /* maybe switch is embedded in parameter(s) somewhere */ ! 1056: else ! 1057: { ! 1058: char *pStr = lpStr; ! 1059: char *pCmdLine = lpszCmdLine; ! 1060: int i, nCnt; ! 1061: ! 1062: while (*pStr != 0) ! 1063: { ! 1064: /* background switch is set, so prepare parameters and set flag */ ! 1065: if ((*pStr == '/' || *pStr == '-') && ! 1066: (*(pStr+1) == 'b' || *(pStr+1) == 'B')) ! 1067: { ! 1068: *bBkgnd = TRUE; ! 1069: ! 1070: /* copy from beg. of lpStr to *pStr to lpszCmdLine */ ! 1071: nCnt = pStr - lpStr; ! 1072: for (i=0; i<nCnt; i++) ! 1073: lpszCmdLine[i] = lpStr[i]; ! 1074: lpszCmdLine[i] = 0; ! 1075: strcat (lpszCmdLine, (pStr+2)); ! 1076: ! 1077: /* break from loop */ ! 1078: break; ! 1079: } ! 1080: ! 1081: pStr++; ! 1082: } ! 1083: ! 1084: /* no switch found, can only edit one file, remove extra parameters */ ! 1085: if (*pStr == 0) ! 1086: { ! 1087: pStr = lpStr; ! 1088: ! 1089: while (*pStr != ' ' && *pStr != 0) ! 1090: pStr++; ! 1091: ! 1092: if (*pStr == ' ') ! 1093: *pStr = 0; ! 1094: ! 1095: strcpy (lpszCmdLine, lpStr); ! 1096: } ! 1097: } ! 1098: } ! 1099: else ! 1100: return FALSE; ! 1101: } ! 1102: else ! 1103: return FALSE; ! 1104: ! 1105: return TRUE; ! 1106: } ! 1107: ! 1108: ! 1109: ! 1110: /* display a Save, Yes|No|Cancel message box */ ! 1111: int WINAPI Save_YNC ( ! 1112: HWND hWnd) ! 1113: { ! 1114: char lpszMsg[MAX_PATH]; ! 1115: char lpszAppName[MAX_PATH]; ! 1116: ! 1117: /* load string to prompt user */ ! 1118: LoadString ((HANDLE)GetModuleHandle (NULL), ! 1119: IDS_DATACHANGED, ! 1120: lpszMsg, ! 1121: sizeof (lpszMsg)); ! 1122: LoadString ((HANDLE)GetModuleHandle (NULL), ! 1123: IDS_APPNAME, ! 1124: lpszAppName, ! 1125: sizeof (lpszAppName)); ! 1126: ! 1127: /* return user response */ ! 1128: return (MessageBox (hWnd, ! 1129: lpszMsg, ! 1130: lpszAppName, ! 1131: MB_YESNOCANCEL | MB_ICONQUESTION)); ! 1132: } ! 1133: ! 1134: ! 1135: ! 1136: /* error notification routine */ ! 1137: VOID WINAPI ErrorNotify ( ! 1138: HWND hWnd, ! 1139: int nErrorString) ! 1140: { ! 1141: char lpszAppName[MAX_PATH]; ! 1142: char lpszErrStr[MAX_PATH]; ! 1143: char szNum[10]; ! 1144: ! 1145: LoadString ((HANDLE)GetModuleHandle (NULL), ! 1146: IDS_APPNAME, ! 1147: lpszAppName, ! 1148: sizeof (lpszAppName)); ! 1149: if (!LoadString ((HANDLE)GetModuleHandle (NULL), ! 1150: nErrorString, ! 1151: lpszErrStr, ! 1152: sizeof (lpszErrStr))) ! 1153: { ! 1154: strcpy (lpszErrStr, "Unknown error => "); ! 1155: strcat (lpszErrStr, itoa (nErrorString, szNum, 10)); ! 1156: } ! 1157: ! 1158: MessageBox (hWnd, lpszErrStr, lpszAppName, MB_OK | MB_ICONSTOP); ! 1159: } ! 1160: ! 1161: ! 1162: ! 1163: VOID WINAPI SetWindowTitle ( ! 1164: HWND hWnd, ! 1165: char *lpszFile) ! 1166: { ! 1167: char lpszAppName[MAX_PATH]; ! 1168: ! 1169: /* load AppName and Window title string into window text */ ! 1170: LoadString ((HANDLE)GetModuleHandle (NULL), ! 1171: IDS_PRINTJOB, ! 1172: lpszAppName, ! 1173: sizeof (lpszAppName)); ! 1174: strcat (lpszAppName, lpszFile); ! 1175: SetWindowText (hWnd, lpszAppName); ! 1176: } ! 1177: ! 1178: ! 1179: ! 1180: /* extract a substring from the edit controls data buffer */ ! 1181: VOID WINAPI GetEditSubString ( ! 1182: HWND hWndEdit, ! 1183: PUINT puStart, ! 1184: PUINT puEnd, ! 1185: char *lpszString) ! 1186: { ! 1187: HANDLE hEditData; ! 1188: char *lpEditData; ! 1189: UINT i; ! 1190: ! 1191: /* maximum search string length is MAXSEARCHSTRING characters */ ! 1192: if ((*puEnd - *puStart) > MAXSEARCHSTRING) ! 1193: *puEnd = *puStart + MAXSEARCHSTRING; ! 1194: ! 1195: /* get edit data handle and lock */ ! 1196: hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0); ! 1197: lpEditData = (char *)LocalLock (hEditData); ! 1198: ! 1199: /* copy characters to string and terminate */ ! 1200: for (i=*puStart; i<*puEnd; i++) ! 1201: lpszString[i-*puStart] = lpEditData[i]; ! 1202: lpszString[i-*puStart] = 0; ! 1203: ! 1204: /* unlock and return edit handle */ ! 1205: LocalUnlock (hEditData); ! 1206: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.