home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / G8 < prev    next >
Encoding:
Text File  |  1991-09-11  |  14.7 KB  |  511 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: FileOpen.c
  4.  
  5.     PURPOSE: Loads, saves, and edits text files
  6.  
  7.     FUNCTIONS:
  8.  
  9.         WinMain() - calls initialization function, processes message loop
  10.         InitApplication() - initializes window data and registers window
  11.         InitInstance() - saves instance handle and creates main window
  12.         MainWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.         OpenDlg() - let user select a file, and open it.
  15.         UpdateListBox() - Update the list box of OpenDlg
  16.         ChangeDefExt() - Change the default extension
  17.         SeparateFile() - Separate filename and pathname
  18.         AddExt() - Add default extension
  19.  
  20. ****************************************************************************/
  21.  
  22. #include "windows.h"
  23. #include "fileopen.h"
  24. #include <string.h>
  25.  
  26. HANDLE hInst;
  27.  
  28. HANDLE hAccTable;                                /* handle to accelerator table */
  29. HWND hEditWnd;                                      /* handle to edit window */
  30. HWND hwnd;                                      /* handle to main window */
  31.  
  32. char FileName[128];
  33. char PathName[128];
  34. char OpenName[128];
  35. char DefPath[128];
  36. char DefSpec[13] = "*.*";
  37. char DefExt[] = ".txt";
  38. char str[255];
  39.  
  40. /****************************************************************************
  41.  
  42.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  43.  
  44.     PURPOSE: calls initialization function, processes message loop
  45.  
  46. ****************************************************************************/
  47.  
  48. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  49. HANDLE hInstance;
  50. HANDLE hPrevInstance;
  51. LPSTR lpCmdLine;
  52. int nCmdShow;
  53. {
  54.     MSG msg;
  55.  
  56.     if (!hPrevInstance)
  57.         if (!InitApplication(hInstance))
  58.             return (FALSE);
  59.  
  60.     if (!InitInstance(hInstance, nCmdShow))
  61.         return (FALSE);
  62.  
  63.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  64.  
  65.     /* Only translate message if it is not an accelerator message */
  66.  
  67.         if (!TranslateAccelerator(hwnd, hAccTable, &msg)) {
  68.             TranslateMessage(&msg);
  69.             DispatchMessage(&msg); 
  70.         }
  71.     }
  72.     return (msg.wParam);
  73. }
  74.  
  75.  
  76. /****************************************************************************
  77.  
  78.     FUNCTION: InitApplication(HANDLE)
  79.  
  80.     PURPOSE: Initializes window data and registers window class
  81.  
  82. ****************************************************************************/
  83.  
  84. BOOL InitApplication(hInstance)
  85. HANDLE hInstance;
  86. {
  87.     WNDCLASS  wc;
  88.  
  89.     wc.style = NULL;
  90.     wc.lpfnWndProc = MainWndProc;
  91.     wc.cbClsExtra = 0;
  92.     wc.cbWndExtra = 0;
  93.     wc.hInstance = hInstance;
  94.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  95.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  96.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  97.     wc.lpszMenuName =  "FileOpenMenu";
  98.     wc.lpszClassName = "FileOpenWClass";
  99.  
  100.     return (RegisterClass(&wc));
  101. }
  102.  
  103.  
  104. /****************************************************************************
  105.  
  106.     FUNCTION:  InitInstance(HANDLE, int)
  107.  
  108.     PURPOSE:  Saves instance handle and creates main window
  109.  
  110. ****************************************************************************/
  111.  
  112. BOOL InitInstance(hInstance, nCmdShow)
  113.     HANDLE          hInstance;
  114.     int             nCmdShow;
  115. {
  116.     RECT            Rect;
  117.  
  118.     hInst = hInstance;
  119.  
  120.     hAccTable = LoadAccelerators(hInst, "FileOpenAcc");
  121.  
  122.     hwnd = CreateWindow(
  123.         "FileOpenWClass",
  124.         "FileOpen Sample Application",
  125.         WS_OVERLAPPEDWINDOW,
  126.         CW_USEDEFAULT,
  127.         CW_USEDEFAULT,
  128.         CW_USEDEFAULT,
  129.         CW_USEDEFAULT,
  130.         NULL,
  131.         NULL,
  132.         hInstance,
  133.         NULL
  134.     );
  135.  
  136.     if (!hwnd)
  137.         return (FALSE);
  138.  
  139.     GetClientRect(hwnd, (LPRECT) &Rect);
  140.  
  141.     /* Create a child window */
  142.  
  143.     hEditWnd = CreateWindow("Edit",
  144.         NULL,
  145.         WS_CHILD | WS_VISIBLE |
  146.         ES_MULTILINE |
  147.         WS_VSCROLL | WS_HSCROLL |
  148.         ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  149.         0,
  150.         0,
  151.         (Rect.right-Rect.left),
  152.         (Rect.bottom-Rect.top),
  153.         hwnd,
  154.         IDC_EDIT,                          /* Child control i.d. */
  155.         hInst,
  156.         NULL);
  157.  
  158.     if (!hEditWnd) {
  159.         DestroyWindow(hwnd);
  160.         return (NULL);
  161.     }
  162.  
  163.     ShowWindow(hwnd, nCmdShow);
  164.     UpdateWindow(hwnd);
  165.     return (TRUE);
  166.  
  167. }
  168.  
  169. /****************************************************************************
  170.  
  171.     FUNCTION: MainWndProc(HWND, WORD, WORD, LONG)
  172.  
  173.     PURPOSE:  Processes messages
  174.  
  175.     MESSAGES:
  176.  
  177.         WM_COMMAND    - application menu (About dialog box)
  178.         WM_DESTROY    - destroy window
  179.  
  180.     COMMENTS:
  181.  
  182.         WM_COMMAND processing:
  183.  
  184.             IDM_OPEN - query to save current file if there is one and it
  185.                        has been changed, open a new file.
  186.  
  187.             IDM_ABOUT - display "About" box.
  188.  
  189. ****************************************************************************/
  190.  
  191. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  192. HWND hWnd;
  193. WORD message;
  194. WORD wParam;
  195. LONG lParam;
  196. {
  197.     FARPROC lpProcAbout, lpOpenDlg;
  198.  
  199.     int Return;
  200.  
  201.     switch (message) {
  202.         case WM_COMMAND:
  203.             switch (wParam) {
  204.                 case IDM_ABOUT:
  205.                     lpProcAbout = MakeProcInstance(About, hInst);
  206.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  207.                     FreeProcInstance(lpProcAbout);
  208.                     break;
  209.  
  210.                 case IDM_OPEN:
  211.                     /* Call OpenDlg() to get the filename */
  212.  
  213.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  214.                     Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  215.                     FreeProcInstance(lpOpenDlg);
  216.  
  217.                     /* Let user know that "open" functionality is not yet
  218.                        implemented */
  219.                     if (Return)
  220.                        MessageBox (
  221.                              GetFocus(),
  222.                              "Command not implemented",
  223.                              "FileOpen Sample Application",
  224.                              MB_ICONASTERISK | MB_OK);
  225.                     break;
  226.  
  227.                 case IDM_NEW:
  228.                 case IDM_SAVE:
  229.                 case IDM_SAVEAS:
  230.                 case IDM_PRINT:
  231.                     MessageBox (
  232.                           GetFocus(),
  233.                           "Command not implemented",
  234.                           "FileOpen Sample Application",
  235.                           MB_ICONASTERISK | MB_OK);
  236.                     break;  
  237.  
  238.                 case IDM_EXIT:
  239.                     DestroyWindow(hWnd);
  240.                     break;
  241.     
  242.                 /* edit menu commands */
  243.  
  244.                 case IDM_UNDO:
  245.                 case IDM_CUT:
  246.                 case IDM_COPY:
  247.                 case IDM_PASTE:
  248.                 case IDM_CLEAR:
  249.                     MessageBox (
  250.                           GetFocus(),
  251.                           "Command not implemented",
  252.                           "FileOpen Sample Application",
  253.                           MB_ICONASTERISK | MB_OK);
  254.                     break;  
  255.  
  256.                 case IDC_EDIT:
  257.                     if (HIWORD (lParam) == EN_ERRSPACE) {
  258.                         MessageBox (
  259.                               GetFocus ()
  260.                             , "Out of memory."
  261.                             , "FileOpen Sample Application"
  262.                             , MB_ICONHAND | MB_OK
  263.                         );
  264.                     }
  265.                     break;
  266.  
  267.             } 
  268.             break;
  269.  
  270.         case WM_SETFOCUS:
  271.             SetFocus (hEditWnd);
  272.             break;
  273.  
  274.         case WM_SIZE:
  275.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  276.             break;
  277.  
  278.         case WM_DESTROY:
  279.             PostQuitMessage(0);
  280.             break;
  281.  
  282.         default:
  283.             return (DefWindowProc(hWnd, message, wParam, lParam));
  284.     }
  285.     return (NULL);
  286. }
  287.  
  288. /****************************************************************************
  289.  
  290.     FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)
  291.  
  292.     PURPOSE: Let user select a file, and return.  Open code not provided.
  293.  
  294. ****************************************************************************/
  295.  
  296. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  297. HWND hDlg;
  298. unsigned message;
  299. WORD wParam;
  300. LONG lParam;
  301. {
  302.     HANDLE hFile=1;     /* Temp value for return */
  303.  
  304.     switch (message) {
  305.         case WM_COMMAND:
  306.             switch (wParam) {
  307.  
  308.                 case IDC_LISTBOX:
  309.                     switch (HIWORD(lParam)) {
  310.  
  311.                         case LBN_SELCHANGE:
  312.                             /* If item is a directory name, append "*.*" */
  313.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX)) 
  314.                                 strcat(str, DefSpec);
  315.  
  316.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  317.                             SendDlgItemMessage(hDlg,
  318.                                 IDC_EDIT,
  319.                                 EM_SETSEL,
  320.                                 NULL,
  321.                                 MAKELONG(0, 0x7fff));
  322.                             break;
  323.  
  324.                         case LBN_DBLCLK:
  325.                             goto openfile;
  326.                     }
  327.                     return (TRUE);
  328.  
  329.                 case IDOK:
  330. openfile:
  331.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  332.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  333.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  334.                             (LPSTR) OpenName);
  335.                         if (str[0])
  336.                             strcpy(DefPath, str);
  337.                         ChangeDefExt(DefExt, DefSpec);
  338.                         UpdateListBox(hDlg);
  339.                         return (TRUE);
  340.                     }
  341.  
  342.                     if (!OpenName[0]) {
  343.                         MessageBox(hDlg, "No filename specified.",
  344.                             NULL, MB_OK | MB_ICONHAND);
  345.                         return (TRUE);
  346.                     }
  347.  
  348.                     AddExt(OpenName, DefExt);
  349.  
  350.                     /* The routine to open the file would go here, and the */
  351.                     /* file handle would be returned instead of NULL.      */
  352.                     EndDialog(hDlg, hFile);
  353.                     return (TRUE);
  354.  
  355.                 case IDCANCEL:
  356.                     EndDialog(hDlg, NULL);
  357.                     return (FALSE);
  358.             }
  359.             break;
  360.  
  361.         case WM_INITDIALOG:                        /* message: initialize    */
  362.             UpdateListBox(hDlg);
  363.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  364.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  365.                 IDC_EDIT,                          /* where to send message  */
  366.                 EM_SETSEL,                         /* select characters      */
  367.                 NULL,                              /* additional information */
  368.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  369.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  370.             return (FALSE); /* Indicates the focus is set to a control */
  371.     }
  372.     return FALSE;
  373. }
  374.  
  375. /****************************************************************************
  376.  
  377.     FUNCTION: UpdateListBox(HWND);
  378.  
  379.     PURPOSE: Update the list box of OpenDlg
  380.  
  381. ****************************************************************************/
  382.  
  383. void UpdateListBox(hDlg)
  384. HWND hDlg;
  385. {
  386.     strcpy(str, DefPath);
  387.     strcat(str, DefSpec);
  388.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  389.  
  390.     /* To ensure that the listing is made for a subdir. of
  391.      * current drive dir...
  392.      */
  393.     if (!strchr (DefPath, ':'))
  394.     DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
  395.  
  396.     /* Remove the '..' character from path if it exists, since this
  397.      * will make DlgDirList move us up an additional level in the tree
  398.      * when UpdateListBox() is called again.
  399.      */
  400.     if (strstr (DefPath, ".."))
  401.     DefPath[0] = '\0';
  402.  
  403.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  404. }
  405.  
  406. /****************************************************************************
  407.  
  408.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  409.  
  410.     PURPOSE: Change the default extension
  411.  
  412. ****************************************************************************/
  413.  
  414. void ChangeDefExt(Ext, Name)
  415. PSTR Ext, Name;
  416. {
  417.     PSTR pTptr;
  418.  
  419.     pTptr = Name;
  420.     while (*pTptr && *pTptr != '.')
  421.         pTptr++;
  422.     if (*pTptr)
  423.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  424.             strcpy(Ext, pTptr);
  425. }
  426.  
  427. /****************************************************************************
  428.  
  429.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  430.  
  431.     PURPOSE: Separate filename and pathname
  432.  
  433. ****************************************************************************/
  434.  
  435. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  436. HWND hDlg;
  437. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  438. {
  439.     LPSTR lpTmp;
  440.     char  cTmp;
  441.  
  442.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  443.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  444.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  445.     if (*lpTmp != ':' && *lpTmp != '\\') {
  446.         lstrcpy(lpDestFileName, lpSrcFileName);
  447.         lpDestPath[0] = 0;
  448.         return;
  449.     }
  450.     lstrcpy(lpDestFileName, lpTmp + 1);
  451.     cTmp = *(lpTmp + 1);
  452.     lstrcpy(lpDestPath, lpSrcFileName);
  453.      *(lpTmp + 1) = cTmp;
  454.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  455. }
  456.  
  457. /****************************************************************************
  458.  
  459.     FUNCTION: AddExt(PSTR, PSTR);
  460.  
  461.     PURPOSE: Add default extension
  462.  
  463. ***************************************************************************/
  464.  
  465. void AddExt(Name, Ext)
  466. PSTR Name, Ext;
  467. {
  468.     PSTR pTptr;
  469.  
  470.     pTptr = Name;
  471.     while (*pTptr && *pTptr != '.')
  472.         pTptr++;
  473.     if (*pTptr != '.')
  474.         strcat(Name, Ext);
  475. }
  476.  
  477. /****************************************************************************
  478.  
  479.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  480.  
  481.     PURPOSE:  Processes messages for "About" dialog box
  482.  
  483.     MESSAGES:
  484.  
  485.         WM_INITDIALOG - initialize dialog box
  486.         WM_COMMAND    - Input received
  487.  
  488. ****************************************************************************/
  489.  
  490. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  491. HWND hDlg;
  492. unsigned message;
  493. WORD wParam;
  494. LONG lParam;
  495. {
  496.     switch (message) {
  497.         case WM_INITDIALOG:
  498.             return (TRUE);
  499.  
  500.         case WM_COMMAND:
  501.         if (wParam == IDOK
  502.                 || wParam == IDCANCEL) {
  503.                 EndDialog(hDlg, TRUE);
  504.                 return (TRUE);
  505.             }
  506.             break;
  507.     }
  508.     return (FALSE);
  509. }
  510. 
  511.