home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c045 / 5.ddi / FILEOPEN / FILEOPEN.C$ / FILEOPEN.bin
Encoding:
Text File  |  1992-01-01  |  12.4 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. #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, unsigned, 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. unsigned message;
  194. WORD wParam;
  195. LONG lParam;
  196. {
  197.     FARPROC lpProcAbout, lpOpenDlg, lpSaveAsDlg;
  198.  
  199.     int Success;                            /* return value from SaveAsDlg() */
  200.     int IOStatus;                           /* result of file i/o      */
  201.     int Return;
  202.  
  203.     switch (message) {
  204.         case WM_COMMAND:
  205.             switch (wParam) {
  206.                 case IDM_ABOUT:
  207.                     lpProcAbout = MakeProcInstance(About, hInst);
  208.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  209.                     FreeProcInstance(lpProcAbout);
  210.                     break;
  211.  
  212.                 case IDM_OPEN:
  213.                     /* Call OpenDlg() to get the filename */
  214.  
  215.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  216.                     Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  217.                     FreeProcInstance(lpOpenDlg);
  218.  
  219.                     /* Let user know that "open" functionality is not yet
  220.                        implemented */
  221.                     if (Return)
  222.                        MessageBox (
  223.                              GetFocus(),
  224.                              "Command not implemented",
  225.                              "FileOpen Sample Application",
  226.                              MB_ICONASTERISK | MB_OK);
  227.                     break;
  228.  
  229.                 case IDM_NEW:
  230.                 case IDM_SAVE:
  231.                 case IDM_SAVEAS:
  232.                 case IDM_PRINT:
  233.                     MessageBox (
  234.                           GetFocus(),
  235.                           "Command not implemented",
  236.                           "FileOpen Sample Application",
  237.                           MB_ICONASTERISK | MB_OK);
  238.                     break;
  239.  
  240.                 case IDM_EXIT:
  241.                     DestroyWindow(hWnd);
  242.                     break;
  243.  
  244.                 /* edit menu commands */
  245.  
  246.                 case IDM_UNDO:
  247.                 case IDM_CUT:
  248.                 case IDM_COPY:
  249.                 case IDM_PASTE:
  250.                 case IDM_CLEAR:
  251.                     MessageBox (
  252.                           GetFocus(),
  253.                           "Command not implemented",
  254.                           "FileOpen Sample Application",
  255.                           MB_ICONASTERISK | MB_OK);
  256.                     break;
  257.  
  258.                 case IDC_EDIT:
  259.                     if (HIWORD (lParam) == EN_ERRSPACE) {
  260.                         MessageBox (
  261.                               GetFocus ()
  262.                             , "Out of memory."
  263.                             , "FileOpen Sample Application"
  264.                             , MB_ICONHAND | MB_OK
  265.                         );
  266.                     }
  267.                     break;
  268.  
  269.             }
  270.             break;
  271.  
  272.         case WM_SETFOCUS:
  273.             SetFocus (hEditWnd);
  274.             break;
  275.  
  276.         case WM_SIZE:
  277.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  278.             break;
  279.  
  280.         case WM_DESTROY:
  281.             PostQuitMessage(0);
  282.             break;
  283.  
  284.         default:
  285.             return (DefWindowProc(hWnd, message, wParam, lParam));
  286.     }
  287.     return (NULL);
  288. }
  289.  
  290. /****************************************************************************
  291.  
  292.     FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)
  293.  
  294.     PURPOSE: Let user select a file, and return.  Open code not provided.
  295.  
  296. ****************************************************************************/
  297.  
  298. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  299. HWND hDlg;
  300. unsigned message;
  301. WORD wParam;
  302. LONG lParam;
  303. {
  304.     WORD index;
  305.     PSTR pTptr;
  306.     HANDLE hFile=1;     /* Temp value for return */
  307.  
  308.     switch (message) {
  309.         case WM_COMMAND:
  310.             switch (wParam) {
  311.  
  312.                 case IDC_LISTBOX:
  313.                     switch (HIWORD(lParam)) {
  314.  
  315.                         case LBN_SELCHANGE:
  316.                             /* If item is a directory name, append "*.*" */
  317.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
  318.                                 strcat(str, DefSpec);
  319.  
  320.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  321.                             SendDlgItemMessage(hDlg,
  322.                                 IDC_EDIT,
  323.                                 EM_SETSEL,
  324.                                 NULL,
  325.                                 MAKELONG(0, 0x7fff));
  326.                             break;
  327.  
  328.                         case LBN_DBLCLK:
  329.                             goto openfile;
  330.                     }
  331.                     return (TRUE);
  332.  
  333.                 case IDOK:
  334. openfile:
  335.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  336.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  337.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  338.                             (LPSTR) OpenName);
  339.                         if (str[0])
  340.                             strcpy(DefPath, str);
  341.                         ChangeDefExt(DefExt, DefSpec);
  342.                         UpdateListBox(hDlg);
  343.                         return (TRUE);
  344.                     }
  345.  
  346.                     if (!OpenName[0]) {
  347.                         MessageBox(hDlg, "No filename specified.",
  348.                             NULL, MB_OK | MB_ICONHAND);
  349.                         return (TRUE);
  350.                     }
  351.  
  352.                     AddExt(OpenName, DefExt);
  353.  
  354.                     /* The routine to open the file would go here, and the */
  355.                     /* file handle would be returned instead of NULL.           */
  356.                     EndDialog(hDlg, hFile);
  357.                     return (TRUE);
  358.  
  359.                 case IDCANCEL:
  360.                     EndDialog(hDlg, NULL);
  361.                     return (FALSE);
  362.             }
  363.             break;
  364.  
  365.         case WM_INITDIALOG:                        /* message: initialize    */
  366.             UpdateListBox(hDlg);
  367.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  368.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  369.                 IDC_EDIT,                          /* where to send message  */
  370.                 EM_SETSEL,                         /* select characters      */
  371.                 NULL,                              /* additional information */
  372.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  373.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  374.             return (FALSE); /* Indicates the focus is set to a control */
  375.     }
  376.     return FALSE;
  377. }
  378.  
  379. /****************************************************************************
  380.  
  381.     FUNCTION: UpdateListBox(HWND);
  382.  
  383.     PURPOSE: Update the list box of OpenDlg
  384.  
  385. ****************************************************************************/
  386.  
  387. void UpdateListBox(hDlg)
  388. HWND hDlg;
  389. {
  390.     strcpy(str, DefPath);
  391.     strcat(str, DefSpec);
  392.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  393.  
  394.     /* To ensure that the listing is made for a subdir. of
  395.      * current drive dir...
  396.      */
  397.     if (!strchr (DefPath, ':'))
  398.     DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
  399.  
  400.     /* Remove the '..' character from path if it exists, since this
  401.      * will make DlgDirList move us up an additional level in the tree
  402.      * when UpdateListBox() is called again.
  403.      */
  404.     if (strstr (DefPath, ".."))
  405.     DefPath[0] = '\0';
  406.  
  407.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  408. }
  409.  
  410. /****************************************************************************
  411.  
  412.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  413.  
  414.     PURPOSE: Change the default extension
  415.  
  416. ****************************************************************************/
  417.  
  418. void ChangeDefExt(Ext, Name)
  419. PSTR Ext, Name;
  420. {
  421.     PSTR pTptr;
  422.  
  423.     pTptr = Name;
  424.     while (*pTptr && *pTptr != '.')
  425.         pTptr++;
  426.     if (*pTptr)
  427.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  428.             strcpy(Ext, pTptr);
  429. }
  430.  
  431. /****************************************************************************
  432.  
  433.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  434.  
  435.     PURPOSE: Separate filename and pathname
  436.  
  437. ****************************************************************************/
  438.  
  439. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  440. HWND hDlg;
  441. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  442. {
  443.     LPSTR lpTmp;
  444.     char  cTmp;
  445.  
  446.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  447.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  448.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  449.     if (*lpTmp != ':' && *lpTmp != '\\') {
  450.         lstrcpy(lpDestFileName, lpSrcFileName);
  451.         lpDestPath[0] = 0;
  452.         return;
  453.     }
  454.     lstrcpy(lpDestFileName, lpTmp + 1);
  455.     cTmp = *(lpTmp + 1);
  456.     lstrcpy(lpDestPath, lpSrcFileName);
  457.      *(lpTmp + 1) = cTmp;
  458.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  459. }
  460.  
  461. /****************************************************************************
  462.  
  463.     FUNCTION: AddExt(PSTR, PSTR);
  464.  
  465.     PURPOSE: Add default extension
  466.  
  467. /***************************************************************************/
  468.  
  469. void AddExt(Name, Ext)
  470. PSTR Name, Ext;
  471. {
  472.     PSTR pTptr;
  473.  
  474.     pTptr = Name;
  475.     while (*pTptr && *pTptr != '.')
  476.         pTptr++;
  477.     if (*pTptr != '.')
  478.         strcat(Name, Ext);
  479. }
  480.  
  481. /****************************************************************************
  482.  
  483.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  484.  
  485.     PURPOSE:  Processes messages for "About" dialog box
  486.  
  487.     MESSAGES:
  488.  
  489.         WM_INITDIALOG - initialize dialog box
  490.         WM_COMMAND    - Input received
  491.  
  492. ****************************************************************************/
  493.  
  494. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  495. HWND hDlg;
  496. unsigned message;
  497. WORD wParam;
  498. LONG lParam;
  499. {
  500.     switch (message) {
  501.         case WM_INITDIALOG:
  502.             return (TRUE);
  503.  
  504.         case WM_COMMAND:
  505.         if (wParam == IDOK
  506.                 || wParam == IDCANCEL) {
  507.                 EndDialog(hDlg, TRUE);
  508.                 return (TRUE);
  509.             }
  510.             break;
  511.     }
  512.     return (FALSE);
  513. }
  514.