home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / tedevkit / ctrl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-05  |  10.0 KB  |  299 lines

  1. /*==============================================================================
  2.    CTRL.C :- Demo of TER as a custom control
  3.    Sub Systems, Inc.
  4.    Software License Agreement  (1989)              
  5.    ----------------------------------
  6.    This is a demo program.  The program shows how you can utilize TERW.OBJ 
  7.    routine to perform text editing functions. The program will prompt you 
  8.    for the control variables to be passed to TERW.OBJ routine.
  9. ===============================================================================*/
  10.  
  11. #define  STRICT
  12.  
  13. #include "windows.h"
  14. #include "stdio.h"
  15. #include "stdlib.h"
  16. #include "fcntl.h"
  17. #include "sys\types.h"
  18. #include "sys\stat.h"
  19. #include "io.h"
  20. #include "string.h"
  21.  
  22. /******************************************************************************
  23.     WIN32 specific defines
  24. *******************************************************************************/
  25. #if defined (_WIN32)
  26.    #if !defined(WIN32)
  27.      #define WIN32
  28.    #endif
  29. #endif
  30. #if defined (WIN32)
  31.    #undef  huge
  32.    #define huge
  33.    #undef  FreeProcInstance
  34.    #define FreeProcInstance(x) 
  35.    #define GET_WM_COMMAND_ID(wp,lp) LOWORD(wp)
  36.    #define CONTROL_ID(wp,lp)        LOWORD(wp)
  37.    #define NOTIFY_MSG(wp,lp)        HIWORD(wp)
  38.    #define farmalloc  malloc
  39.    #define farrealloc realloc
  40.    #define farfree    free
  41. #else
  42.    #define GET_WM_COMMAND_ID(wp,lp) (wp)
  43.    #define CONTROL_ID(wp,lp)        (wp)
  44.    #define NOTIFY_MSG(wp,lp)        HIWORD(lp)
  45. #endif
  46.  
  47. #include "ter.h"
  48.  
  49. #include "ctrl.h"
  50.  
  51. #include "ter_cmd.h"     // include TER accelerator keys
  52.  
  53. /****************************************************************************
  54.    Main Windows Routine
  55. ****************************************************************************/
  56. int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
  57. {
  58.     MSG msg;
  59.     int i;
  60.  
  61.     hPrevInst=hPrevInstance;
  62.     if (!hPrevInstance)
  63.         if (!InitApplication(hInstance))
  64.             return (FALSE);
  65.  
  66.     if (!InitInstance(hInstance, nCmdShow))
  67.         return (FALSE);
  68.  
  69.     while (GetMessage(&msg, NULL, 0, 0)) {
  70.         TranslateMessage(&msg);
  71.         DispatchMessage(&msg);
  72.     }
  73.     return (msg.wParam);
  74. }
  75.  
  76. /****************************************************************************
  77.    Initialize window data and register window class
  78. ****************************************************************************/
  79.  
  80. BOOL InitApplication(HINSTANCE hInstance)
  81. {
  82.     WNDCLASS  wc;
  83.  
  84.     wc.style = 0;
  85.     wc.lpfnWndProc = (void far *)DemoWndProc;
  86.     wc.cbClsExtra = 0;
  87.     wc.cbWndExtra = 0;
  88.     wc.hInstance = hInstance;
  89.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  90.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  91.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  92.     wc.lpszMenuName =  "DemoMenu";
  93.     wc.lpszClassName = "DemoCClass";
  94.  
  95.     return (RegisterClass(&wc));
  96. }
  97.  
  98.  
  99. /****************************************************************************
  100.     Save instance handle and create main window
  101. ****************************************************************************/
  102.  
  103. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  104. {
  105.     HWND  hwnd;
  106.     int   i;
  107.     char  DocFile[13];
  108.     HFILE hFile;
  109.     FILE *InStream;
  110.     char  huge *pBuf;
  111.  
  112.     hInst = hInstance;
  113.  
  114.     LoadTerControl();     // ensures the TER dll is actually loaded
  115.  
  116.     hwnd = CreateWindow(
  117.         "DemoCClass",
  118.         "TER Demonstration",
  119.         WS_OVERLAPPEDWINDOW,
  120.         CW_USEDEFAULT,
  121.         CW_USEDEFAULT,
  122.         CW_USEDEFAULT,
  123.         CW_USEDEFAULT,
  124.         NULL,
  125.         NULL,
  126.         hInstance,
  127.         NULL
  128.     );
  129.  
  130.     if (!hwnd)
  131.         return (FALSE);
  132.  
  133.     ShowWindow(hwnd, nCmdShow);
  134.     UpdateWindow(hwnd);
  135.  
  136.     // Read the DEMO.TER file in a buffer
  137.     #if defined(WIN32) 
  138.        strcpy(DocFile,"DEMO.SSE");
  139.     #else
  140.        strcpy(DocFile,"DEMO.TER");
  141.     #endif
  142.     if (access(DocFile,6)==-1) return TRUE;      // file not found
  143.  
  144.     // get the file length
  145.     if (NULL==(InStream=fopen(DocFile,"rb"))) return TRUE; // open the file
  146.     BufferLen=filelength(fileno(InStream));
  147.     fclose(InStream);
  148.  
  149.     // read the file
  150.     if (HFILE_ERROR==(hFile=_lopen(DocFile,OF_READ))) return TRUE;
  151.  
  152.     if ( NULL==(hBuf=GlobalAlloc(GMEM_MOVEABLE,BufferLen+1))
  153.       || NULL==(pBuf=(char huge *)GlobalLock(hBuf)) ) return TRUE;
  154.  
  155.     if ((BufferLen=_hread(hFile,pBuf,BufferLen))==-1) return TRUE;
  156.  
  157.     GlobalUnlock(hBuf);                          // unlock the buffer
  158.  
  159.     _lclose(hFile);                              // close the demo document file
  160.  
  161.     return (TRUE);
  162. }
  163.  
  164. /****************************************************************************
  165.     Process Main Window messages
  166. ****************************************************************************/
  167. long CALLBACK _export DemoWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  168. {
  169.     DLGPROC lpProcParam;
  170.     BOOL result;
  171.     int i;
  172.     int  CommandId;
  173.  
  174.     switch (message) {
  175.         case WM_COMMAND:
  176.            // extract the command id from wParam
  177.            CommandId=GET_WM_COMMAND_ID(wParam,lParam);
  178.  
  179.            switch (CommandId) {
  180.               case IDM_EDIT:                 // show the edit dialog box
  181.                  lpProcParam = (DLGPROC) MakeProcInstance((FARPROC) DemoEditDlg, hInst);
  182.                  DialogBox(hInst,"DemoEditDlg",hWnd,lpProcParam);
  183.                  FreeProcInstance((FARPROC) lpProcParam);
  184.                  
  185.                  break;
  186.  
  187.               default:
  188.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  189.            }
  190.            break;
  191.  
  192.         case WM_DESTROY:
  193.             PostQuitMessage(0);
  194.             break;
  195.  
  196.         default:
  197.             return (DefWindowProc(hWnd, message, wParam, lParam));
  198.     }
  199.     return (LRESULT)NULL;
  200. }
  201.  
  202. /****************************************************************************
  203.                       DEMO DIALOG PROCEDURES
  204. ****************************************************************************/
  205.  
  206. /****************************************************************************
  207.     Process messages for "DemoEditDlg" dialog box
  208.     This dialog box includes the TER edit control.
  209. ****************************************************************************/
  210. int CALLBACK _export DemoEditDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  211. {
  212.     HWND hWnd;
  213.  
  214.     switch (message) {
  215.         case WM_INITDIALOG:
  216.             
  217.             // Set initial values of the parameters 
  218.             if (hBuf) SetTerBuffer(GetDlgItem(hDlg,IDC_EDIT),hBuf,BufferLen,"",FALSE);
  219.  
  220.             SetFocus(GetDlgItem(hDlg,IDC_EDIT));
  221.  
  222.             return (TRUE);
  223.  
  224.         case WM_COMMAND:
  225.             hWnd=GetDlgItem(hDlg,IDC_EDIT);  // TER window handle
  226.             switch (CONTROL_ID(wParam,lParam)) {
  227.                 case IDOK:
  228.                    // Free the old buffer and retrieve the updated buffer
  229.                    if (hBuf) GlobalFree(hBuf);
  230.                    hBuf=GetTerBuffer(hWnd,&BufferLen);
  231.  
  232.                    EndDialog(hDlg, TRUE);
  233.                    return (TRUE);
  234.         
  235.                 case IDCANCEL:
  236.                    EndDialog(hDlg, FALSE);
  237.                    return (TRUE);
  238.  
  239.                 // Send command message to the TER window
  240.                 case IDC_CUT:
  241.                    SendMessage(hWnd,WM_COMMAND,ID_CUT,0L);break;
  242.                 case IDC_COPY:
  243.                    SendMessage(hWnd,WM_COMMAND,ID_COPY,0L);break;
  244.                 case IDC_PASTE:
  245.                    SendMessage(hWnd,WM_COMMAND,ID_PASTE,0L);break;
  246.                 case IDC_SEARCH:
  247.                    SendMessage(hWnd,WM_COMMAND,ID_SEARCH,0L);break;
  248.                 case IDC_REPLACE:
  249.                    SendMessage(hWnd,WM_COMMAND,ID_REPLACE,0L);break;
  250.                 case IDC_UNDO:
  251.                    SendMessage(hWnd,WM_COMMAND,ID_UNDO,0L);break;
  252.                 case IDC_PRINT:
  253.                    SendMessage(hWnd,WM_COMMAND,ID_PRINT,0L);break;
  254.                 case IDC_PAGE_OPTIONS:
  255.                    SendMessage(hWnd,WM_COMMAND,ID_PAGE_OPTIONS,0L);break;
  256.                 case IDC_PRINT_OPTIONS:
  257.                    SendMessage(hWnd,WM_COMMAND,ID_PRINT_OPTIONS,0L);break;
  258.                 case IDC_CHAR_NORMAL:
  259.                    SendMessage(hWnd,WM_COMMAND,ID_CHAR_NORMAL,0L);break;
  260.                 case IDC_BOLD_ON:
  261.                    SendMessage(hWnd,WM_COMMAND,ID_BOLD_ON,0L);break;
  262.                 case IDC_ULINE_ON:
  263.                    SendMessage(hWnd,WM_COMMAND,ID_ULINE_ON,0L);break;
  264.                 case IDC_ITALIC_ON:
  265.                    SendMessage(hWnd,WM_COMMAND,ID_ITALIC_ON,0L);break;
  266.                 case IDC_STRIKE_ON:
  267.                    SendMessage(hWnd,WM_COMMAND,ID_STRIKE_ON,0L);break;
  268.                 case IDC_SUPSCR_ON:
  269.                    SendMessage(hWnd,WM_COMMAND,ID_SUPSCR_ON,0L);break;
  270.                 case IDC_SUBSCR_ON:
  271.                    SendMessage(hWnd,WM_COMMAND,ID_SUBSCR_ON,0L);break;
  272.                 case IDC_COLOR:
  273.                    SendMessage(hWnd,WM_COMMAND,ID_COLOR,0L);break;
  274.                 case IDC_FONTS:
  275.                    SendMessage(hWnd,WM_COMMAND,ID_FONTS,0L);break;
  276.                 case IDC_PARA_NORMAL:
  277.                    SendMessage(hWnd,WM_COMMAND,ID_PARA_NORMAL,0L);break;
  278.                 case IDC_CENTER:
  279.                    SendMessage(hWnd,WM_COMMAND,ID_CENTER,0L);break;
  280.                 case IDC_RIGHT_JUSTIFY:
  281.                    SendMessage(hWnd,WM_COMMAND,ID_RIGHT_JUSTIFY,0L);break;
  282.                 case IDC_LEFT_INDENT:
  283.                    SendMessage(hWnd,WM_COMMAND,ID_LEFT_INDENT,0L);break;
  284.                 case IDC_RIGHT_INDENT:
  285.                    SendMessage(hWnd,WM_COMMAND,ID_RIGHT_INDENT,0L);break;
  286.                 case IDC_HANGING_INDENT:
  287.                    SendMessage(hWnd,WM_COMMAND,ID_HANGING_INDENT,0L);break;
  288.                 case IDC_DOUBLE_SPACE:
  289.                    SendMessage(hWnd,WM_COMMAND,ID_DOUBLE_SPACE,0L);break;
  290.  
  291.                 default:
  292.                    ;
  293.             }
  294.             break;
  295.     }
  296.     return (FALSE);
  297. }
  298.  
  299.