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