home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / EDIT.WEX / EDIT.C next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  9.4 KB  |  385 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%                                       %
  4.  *%    Copyright (C) 1991, by WATCOM Systems Inc. All rights reserved.    %
  5.  *%                                       %
  6.  *%     Permission is granted to anyone to use this example program for       %
  7.  *%     any purpose on any computer system, subject to the following       %
  8.  *%    restrictions:                               %
  9.  *%                                       %
  10.  *%     1. This example is provided on an "as is" basis, without warranty. %
  11.  *%       You indemnify, hold harmless and defend WATCOM from and against %
  12.  *%       any claims or lawsuits, including attorney's, that arise or       %
  13.  *%       result from the use or distribution of this example, or any     %
  14.  *%       modification thereof.                       %
  15.  *%                                       %
  16.  *%     2. You may not remove, alter or suppress this notice from this       %
  17.  *%        example program or any modification thereof.               %
  18.  *%                                       %
  19.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20.  *
  21.  * EDIT.C
  22.  *
  23.  * Windows edit program
  24.  *
  25.  */
  26. #include <windows.h>
  27. #include <stdio.h>
  28. #include "edit.h"
  29.  
  30. char         EditTitle[] = "WATCOM Edit Example";
  31. psupp        PrinterSupport;
  32. static char     EditClass[32]="EditClass";
  33.  
  34. static BOOL FirstInstance( HANDLE );
  35. static BOOL AnyInstance( HANDLE, int );
  36. long _EXPORT FAR PASCAL WindowProc( HWND, unsigned, WORD, LONG );
  37.  
  38. /*
  39.  * WinMain - initialization, message loop
  40.  */
  41. int PASCAL WinMain( HANDLE inst, HANDLE previnst, LPSTR cmdline,
  42.             int cmdshow )
  43. {
  44.     MSG        msg;
  45.  
  46.     previnst = previnst;    /* shut up warning */
  47.     cmdline = cmdline;
  48. #ifdef __WINDOWS_386__
  49.     sprintf( EditClass,"EditClass%d", inst );
  50. #else
  51.     if( !previnst )
  52. #endif
  53.     if( !FirstInstance( inst ) ) return( FALSE );
  54.  
  55.     if( !AnyInstance( inst, cmdshow ) ) return( FALSE );
  56.  
  57.     while( GetMessage( &msg, NULL, NULL, NULL ) ) {
  58.  
  59.     TranslateMessage( &msg );
  60.     DispatchMessage( &msg );
  61.  
  62.     }
  63.  
  64.     return( msg.wParam );
  65.  
  66. } /* WinMain */
  67.  
  68. /*
  69.  * FirstInstance - register window class for the application,
  70.  *           and do any other application initialization
  71.  */
  72. static BOOL FirstInstance( HANDLE inst )
  73. {
  74.     WNDCLASS    wc;
  75.     BOOL    rc;
  76.  
  77.     /*
  78.      * set up and register window class
  79.      */
  80.     wc.style = CS_HREDRAW | CS_VREDRAW;
  81.     wc.lpfnWndProc = (LPVOID) WindowProc;
  82.     wc.cbClsExtra = 0;
  83.     wc.cbWndExtra = sizeof( LPEDATA );
  84.     wc.hInstance = inst;
  85.     wc.hIcon = LoadIcon( inst, IDI_APPLICATION );
  86.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  87.     wc.hbrBackground = GetStockObject( WHITE_BRUSH );
  88.     wc.lpszMenuName = "EditMenu";
  89.     wc.lpszClassName = EditClass;
  90.     rc = RegisterClass( &wc );
  91.     return( rc );
  92.  
  93. } /* FirstInstance */
  94.  
  95. /*
  96.  * AnyInstance - do work required for every instance of the application:
  97.  *          create the window, initialize data
  98.  */
  99. static BOOL AnyInstance( HANDLE inst, int cmdshow )
  100. {
  101.     RECT    rect;
  102.     HWND     hwnd;
  103.     HWND    editwnd;
  104.     LPEDATA    edata_ptr;
  105.     
  106.     /*
  107.      * create main window
  108.      */
  109.     hwnd = CreateWindow(
  110.     EditClass,        /* class */
  111.     EditTitle,        /* caption */
  112.     WS_OVERLAPPEDWINDOW,    /* style */
  113.     CW_USEDEFAULT,        /* init. x pos */
  114.     CW_USEDEFAULT,        /* init. y pos */
  115.     CW_USEDEFAULT,        /* init. x size */
  116.     CW_USEDEFAULT,        /* init. y size */
  117.     NULL,            /* parent window */
  118.     NULL,            /* menu handle */
  119.     inst,            /* program handle */
  120.     NULL            /* create parms */
  121.     );
  122.             
  123.     if( !hwnd ) return( FALSE );
  124.     GetClientRect( hwnd, &rect );
  125.  
  126.     editwnd = CreateWindow(
  127.         "EDIT",                /* class */
  128.     NULL,                /* no caption */
  129.     WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  130.     ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, /* style */
  131.     0,                /* init. x pos */
  132.     0,                /* init. y pos */
  133.     rect.right-rect.left,        /* init. x size (entire parent) */
  134.     rect.bottom-rect.top,        /* init. y size  (entire parent) */
  135.     hwnd,                /* parent window */
  136.     EDIT_ID,            /* i.d. */
  137.     inst,                /* program handle */
  138.     NULL                /* create parms */
  139.     );
  140.  
  141.     /*
  142.      * if this failed, then kill original window
  143.      */
  144.     if( !editwnd ) {
  145.     DestroyWindow( hwnd );
  146.     return( FALSE );
  147.     }
  148.  
  149.     /*
  150.      * set up data associated with window (IMAGINE! not using
  151.      * a pile of global variables!)
  152.      */
  153.     edata_ptr = MemAlloc( sizeof( extra_data ) );
  154.     if( edata_ptr == NULL ) return( FALSE );
  155.     edata_ptr->hwnd = hwnd;
  156.     edata_ptr->editwnd = editwnd;
  157.     edata_ptr->inst = inst;
  158.     edata_ptr->filename = NULL;
  159.     edata_ptr->needs_saving = FALSE;
  160.     edata_ptr->font = NULL;
  161.     SetWindowLong( hwnd, EXTRA_DATA_OFFSET, (DWORD) edata_ptr );
  162.  
  163.     /*
  164.      * display window
  165.      */
  166.     ShowWindow( hwnd, cmdshow );
  167.     UpdateWindow( hwnd );
  168.     GetAllFonts( edata_ptr );
  169.     
  170.     return( TRUE );
  171.             
  172. } /* AnyInstance */
  173.  
  174. /*
  175.  * AboutDlgProc - processes messages for the about dialog.
  176.  */
  177. BOOL _EXPORT FAR PASCAL AboutDlgProc( HWND hwnd, unsigned msg,
  178.                 WORD wparam, LONG lparam )
  179. {
  180.     lparam = lparam;            /* turn off warning */
  181.  
  182.     switch( msg ) {
  183.     case WM_INITDIALOG:
  184.     return( TRUE );
  185.  
  186.     case WM_COMMAND:
  187.         if( wparam == IDOK ) {
  188.         EndDialog( hwnd, TRUE );
  189.         return( TRUE );
  190.     }
  191.     break;
  192.     }
  193.     return( FALSE );
  194.  
  195. } /* AboutDlgProc */
  196.  
  197. /*
  198.  * WindowProc - handle messages for the main application window
  199.  */
  200. LONG _EXPORT FAR PASCAL WindowProc( HWND hwnd, unsigned msg,
  201.                      WORD wparam, LONG lparam )
  202. {
  203.     FARPROC     proc;
  204.     HANDLE    hinst;
  205.     LPEDATA    ed;
  206.     DWORD    sel;
  207.     int        state;
  208.     HDC        hdc;
  209.  
  210.     ed = (LPEDATA) GetWindowLong( hwnd, EXTRA_DATA_OFFSET );
  211.  
  212.     /*
  213.      * all messages are in alphabetical order, except WM_COMMAND, at the end
  214.      */
  215.     switch( msg ) {
  216.     case WM_CLOSE:
  217.         /*
  218.      * see if it is okay to close down
  219.      */
  220.     if( CheckFileSave( ed ) ) {
  221.         DestroyWindow( hwnd );
  222.     }
  223.     break;
  224.  
  225.     case WM_CREATE:
  226.         /*
  227.      * try to get printer support.  If it works, delete the DC,
  228.      * since these are a system resource
  229.      */
  230.         hdc = PrinterDC();
  231.     if( hdc ) {
  232.         DeleteDC( hdc );
  233.     }
  234.     break;
  235.  
  236.     case WM_DESTROY:
  237.         if( ed->font != NULL ) DeleteObject( ed->font );
  238.     PostQuitMessage( 0 );
  239.     break;
  240.  
  241.     case WM_DEVMODECHANGE:
  242.     case WM_WININICHANGE:
  243.         /*
  244.      * handle user changing printer info
  245.      */
  246.         hdc = PrinterDC();
  247.     if( hdc ) {
  248.         DeleteDC( hdc );
  249.     }
  250.     break;
  251.  
  252.     case WM_FONTCHANGE:
  253.     GetAllFonts( ed );
  254.     break;
  255.  
  256.     case WM_INITMENU:
  257.         /*
  258.      * initial menu state set here
  259.      */
  260.     if( wparam == GetMenu( hwnd ) ) {
  261.  
  262.         state = MF_GRAYED;
  263.         if( OpenClipboard( ed->hwnd ) ) {
  264.         if( IsClipboardFormatAvailable( CF_TEXT ) ||
  265.             IsClipboardFormatAvailable( CF_OEMTEXT )) {
  266.             state = MF_ENABLED;
  267.         }
  268.         CloseClipboard();
  269.         }
  270.         EnableMenuItem( wparam, MENU_PASTE, state );
  271.     
  272.         state = MF_GRAYED;
  273.         if( SendMessage( ed->editwnd, EM_CANUNDO, 0, 0L ) ) {
  274.         state = MF_ENABLED;
  275.         }
  276.         EnableMenuItem( wparam, MENU_UNDO, state );
  277.         sel = SendMessage( ed->editwnd, EM_GETSEL, 0, 0L );
  278.     
  279.         state = MF_GRAYED;
  280.         if( HIWORD( sel ) != LOWORD( sel ) ) state = MF_ENABLED;
  281.         EnableMenuItem( wparam, MENU_CLEAR, state );
  282.         EnableMenuItem( wparam, MENU_COPY, state );
  283.         EnableMenuItem( wparam, MENU_CUT, state );
  284.  
  285.         state = MF_GRAYED;
  286.         if( PrinterSupport != PSUPP_NONE ) state = MF_ENABLED;
  287.         EnableMenuItem( wparam, MENU_PRINT, state );
  288.         state = MF_GRAYED;
  289.         if( PrinterSupport == PSUPP_CANPRINTANDSET ) state = MF_ENABLED;
  290.         EnableMenuItem( wparam, MENU_PRINT_SETUP, state );
  291.  
  292.     }
  293.     return( NULL );
  294.  
  295.     case WM_QUERYENDSESSION:
  296.         /*
  297.      * check if it is okay to end the session
  298.      */
  299.     return( CheckFileSave( ed ) );
  300.  
  301.     case WM_SETFOCUS:
  302.         /*
  303.      * move the focus to our editor, rather than to the frame
  304.      */
  305.     SetFocus( ed->editwnd );
  306.     break;
  307.  
  308.     case WM_SIZE:
  309.         /*
  310.      * resize edit window to match size of our client area
  311.      */
  312.     MoveWindow( ed->editwnd, 0, 0, LOWORD( lparam ),
  313.             HIWORD( lparam ), TRUE );
  314.     break;
  315.  
  316.  
  317.     case WM_COMMAND:
  318.         switch( wparam ) {
  319.     case EDIT_ID:
  320.         switch( HIWORD( lparam ) ){
  321.         case EN_CHANGE:
  322.             ed->needs_saving = TRUE;
  323.         break;
  324.         case EN_ERRSPACE:
  325.             MessageBox( hwnd, "Out of Space", EditTitle, MB_OK );
  326.         break;
  327.         }
  328.         break;
  329.     case MENU_ABOUT:
  330.         hinst = GetWindowWord( hwnd, GWW_HINSTANCE );
  331.         proc = MakeProcInstance( AboutDlgProc, hinst );
  332.         DialogBox( hinst,"AboutBox", hwnd, proc );
  333.         FreeProcInstance( proc );
  334.         break;
  335.     case MENU_CLEAR:
  336.         SendMessage( ed->editwnd, EM_REPLACESEL, 0, (LONG) (LPSTR)"" );
  337.         break;
  338.     case MENU_COPY:
  339.             SendMessage( ed->editwnd, WM_COPY, 0, 0L );
  340.         break;
  341.     case MENU_CUT:
  342.             SendMessage( ed->editwnd, WM_CUT, 0, 0L );
  343.         break;
  344.     case MENU_EXIT:
  345.         if( CheckFileSave( ed ) ) {
  346.         DestroyWindow( hwnd );
  347.         }
  348.         break;
  349.     case MENU_FONT_SELECT:
  350.         FontSelect( ed );
  351.         break;
  352.     case MENU_NEW:
  353.         FileEdit( ed, FALSE );
  354.         break;
  355.     case MENU_OPEN:
  356.         FileEdit( ed, TRUE );
  357.         break;
  358.     case MENU_PASTE:
  359.             SendMessage( ed->editwnd, WM_PASTE, 0, 0L );
  360.         break;
  361.     case MENU_PRINT:
  362.         Print( ed );
  363.         break;
  364.     case MENU_PRINT_SETUP:
  365.         GetPrinterSetup( hwnd );
  366.         break;
  367.     case MENU_SAVE:
  368.         FileSave( ed, FALSE );
  369.         break;
  370.     case MENU_SAVE_AS:
  371.         FileSave( ed, TRUE );
  372.         break;
  373.     case MENU_UNDO:
  374.             SendMessage( ed->editwnd, EM_UNDO, 0, 0L );
  375.         break;
  376.     }
  377.     break;
  378.  
  379.     default:
  380.     return( DefWindowProc( hwnd, msg, wparam, lparam ) );
  381.     }
  382.     return( 0L );
  383.  
  384. } /* WindowProc */
  385.