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