|
|
1.1 root 1: /*==============================================================*\
2: * File.c - routines for handling the standard file menu
3: * commands
4: * Created 1989, 1990 Microsoft Corp.
5: *--------------------------------------------------------------
6: *
7: * This module contains the code for the WM_COMMAND messages
8: * posted by the standard File menu.
9: *
10: *--------------------------------------------------------------
11: *
12: * This source file contains the following functions:
13: *
14: * FileNew(mp2);
15: * FileOpen(mp2);
16: * FileSave(mp2);
17: * FileSaveAs(mp2);
18: * FilePrint(mp2);
19: * FilePageSetup(mp2);
20: * FilePrintSetup(mp2);
21: * FileExit(mp2);
22: * WriteFileToDisk(hf);
23: * GetFileName();
24: * UpdateTitleText(hwnd);
25: *
26: \*==============================================================*/
27:
28: /*--------------------------------------------------------------*\
29: * Include files, macros, defined constants, and externs
30: \*--------------------------------------------------------------*/
31:
32: #define INCL_WINFRAMEMGR
33: #define INCL_WINSWITCHLIST
34: #define INCL_WINSTDFILE
35:
36: #include <os2.h>
37: #include <string.h>
38: #include "main.h"
39: #include "xtrn.h"
40:
41: /*--------------------------------------------------------------*\
42: * Global variables
43: \*--------------------------------------------------------------*/
44:
45: CHAR szFullPath[CCHMAXPATH] = "";
46:
47: /*--------------------------------------------------------------*\
48: * Entry point declarations
49: \*--------------------------------------------------------------*/
50:
51:
52: /****************************************************************\
53: * New file routine
54: *--------------------------------------------------------------
55: *
56: * Name: FileNew(mp2)
57: *
58: * Purpose: Processes the File menu's New item
59: *
60: * Usage: called whenever New from the File menu is selected
61: *
62: * Method:
63: *
64: *
65: * Returns:
66: *
67: \****************************************************************/
68: VOID FileNew(mp2)
69: MPARAM mp2; /* second parameter of WM_COMMAND message */
70: {
71: /*--------------------------------------------------------------*\
72: * Enter routines for creating a new file and window
73: \*--------------------------------------------------------------*/
74:
75:
76: /* clear file name and reset the titlebar text */
77: szFullPath[0] = '\0';
78: UpdateTitleText(hwndMainFrame);
79:
80:
81: /* This routine currently doesn't use the mp2 parameter but *\
82: * it is referenced here to prevent an 'Unreferenced Parameter'
83: \* warning at compile time. */
84: mp2;
85:
86: } /* FileNew() */
87:
88:
89: /****************************************************************\
90: * Open file routine
91: *--------------------------------------------------------------
92: *
93: * Name: FileOpen(mp2)
94: *
95: * Purpose: Processes the File menu's Open item.
96: *
97: * Usage: called whenever New from the File menu is selected
98: *
99: * Method: calls the standard file open dialog to get the
100: * file name. The file name is passed onto DosOpen
101: * which returns the handle to the file. The file
102: * input procedure is called and then the file handle
103: * is closed.
104: *
105: * Returns:
106: *
107: \****************************************************************/
108: VOID FileOpen(mp2)
109: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
110: {
111: FILEDLG fdg;
112: HFILE hfIn;
113: ULONG ulAction;
114: CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
115:
116: fdg.cbsize = sizeof(FILEDLG);
117:
118: if(!WinLoadString(hab, NULL, IDS_OPENDLGTITLE, MESSAGELEN, szTitle)) {
119: MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
120: return;
121: }
122:
123: if(!WinLoadString(hab, NULL, IDS_OPENDLGBUTTON, MESSAGELEN, szButton)) {
124: MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
125: return;
126: }
127:
128: fdg.usDialogType = OPEN_DIALOG;
129: fdg.pszTitle = szTitle;
130: fdg.pszOKButton = szButton;
131: fdg.lUser = 0L;
132: fdg.fl = FDS_HELPBUTTON | FDS_CENTER;
133: fdg.pfnDlgProc = NULL;
134: fdg.lReturn = 0L;
135: fdg.lSRC = 0L;
136: fdg.hmod = NULL;
137: fdg.idDlg = FILEOPEN;
138: fdg.x = 0;
139: fdg.y = 0;
140:
141: if(!WinLoadString(hab,
142: NULL,
143: IDS_FILEOPENEXT,
144: CCHMAXPATH,
145: fdg.szFullFile)) {
146:
147: MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
148: return;
149: }
150:
151: fdg.pszIType = 0L;
152: fdg.ppszITypeList = 0L;
153: fdg.pszIDrive = 0L;
154: fdg.ppszIDriveList = 0L;
155: fdg.sEAType = 0;
156:
157: /* get the file */
158: if(!KitFileDlg(hwndMain, (PFILEDLG)&fdg))
159: return;
160:
161:
162: /*--------------------------------------------------------------*\
163: * Upon sucessful return of a file, open it for reading
164: \*--------------------------------------------------------------*/
165:
166: if(fdg.lReturn == ID_OK) {
167: if( DosOpen(fdg.szFullFile, /* file name from Open dialog */
168: &hfIn, /* file handle returned */
169: &ulAction,
170: 0L,
171: FILE_NORMAL,
172: FILE_OPEN,
173: OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
174: 0L)) {
175: MessageBox(hwndMain,
176: IDMSG_CANNOTOPENINPUTFILE,
177: MB_OK | MB_ERROR,
178: FALSE);
179:
180: /*-------------------------------------------------------*\
181: * NOTE: You now have several options on how to proceed
182: * from this point:
183: * - You can abort the File Open by returning from
184: * this procedure.
185: * - You can bring up the File Open dialog again
186: * and have the user pick another file.
187: * - You can check the error code from the DosOpen,
188: * determine why the open failed, and take an
189: * action appropriate to the specific failure.
190: \*-------------------------------------------------------*/
191:
192:
193: return;
194: }
195:
196: /* copy file name into file name buffer */
197: strcpy(szFullPath, fdg.szFullFile);
198:
199: /*--------------------------------------------------------------*\
200: * Place routine for reading the file here
201: \*--------------------------------------------------------------*/
202:
203: DosClose(hfIn);
204:
205: UpdateTitleText(hwndMainFrame);
206: }
207:
208:
209: /* This routine currently doesn't use the mp2 parameter but *\
210: * it is referenced here to prevent an 'Unreferenced Parameter'
211: \* warning at compile time. */
212: mp2;
213:
214: } /* FileOpen() */
215:
216:
217: /****************************************************************\
218: * Save file routine
219: *--------------------------------------------------------------
220: *
221: * Name: FileSave(mp2)
222: *
223: * Purpose: Processes the File menu's Save item.
224: *
225: * Usage: called whenever Save from the File menu is
226: * selected
227: *
228: * Method: Routine opens the file for output, calls the
229: * application's save routine, and closes the file
230: *
231: * Returns:
232: *
233: \****************************************************************/
234: VOID FileSave(mp2)
235: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
236: {
237: HFILE hf;
238: ULONG ulAction;
239:
240: /*
241: * If the file currently is untitled, we will need to get a file
242: * name from the user before we can open the file. Getting a
243: * file name is normally done during the FileSaveAs operation
244: * so we will treat this save as a SaveAs and call FileSaveAs().
245: * If the file is titled, then we save the file.
246: *
247: * NOTE: This routine will be called by FileSaveAs(), but only
248: * after a valid file name has been obtained. So, FileSaveAs()
249: * will not be called again from this routine.
250: */
251: if(szFullPath[0] == '\0') {
252: FileSaveAs(mp2);
253: return;
254: }
255:
256: /* open the file */
257: if( DosOpen(szFullPath, /* file name of current document */
258: &hf, /* file handle of output file */
259: &ulAction,
260: 0L,
261: FILE_NORMAL,
262: FILE_OPEN | FILE_CREATE,
263: OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
264: 0L)) {
265:
266: MessageBox(hwndMain,
267: IDMSG_CANNOTOPENOUTPUTFILE,
268: MB_OK | MB_ERROR,
269: FALSE);
270: return;
271: }
272:
273: WriteFileToDisk(hf);
274:
275: DosClose(hf);
276:
277:
278: /* This routine currently doesn't use the mp2 parameter but *\
279: * it is referenced here to prevent an 'Unreferenced Parameter'
280: \* warning at compile time. */
281: mp2;
282:
283: } /* FileSave() */
284:
285: /****************************************************************\
286: * Save As file routine
287: *--------------------------------------------------------------
288: *
289: * Name: FileSaveAs(mp2)
290: *
291: * Purpose: Processes the File menu's Save As item.
292: *
293: * Usage: called whenever Save As from the File menu is
294: * selected
295: *
296: * Method: Routine prompts the user for a name for the
297: * file and then saves the file.
298: *
299: * Returns:
300: *
301: \****************************************************************/
302: VOID FileSaveAs(mp2)
303: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
304: {
305: HFILE hf;
306: ULONG ulAction;
307: SHORT sT;
308:
309: while(TRUE) { /* infinite loop until we break out of it */
310:
311: /* Get a name for the file */
312: if(!GetFileName()) {
313: return;
314: }
315:
316: /* See if the file exists. If it does, then confirm that the
317: * user wants to overwrite it. If he doesn't, then get a new
318: * file name
319: */
320: if( DosOpen(szFullPath, /* file name from, GetFileName() */
321: &hf, /* handle of opened file */
322: &ulAction,
323: 0L,
324: FILE_NORMAL,
325: FILE_CREATE,
326: OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
327: 0L)) {
328:
329: MessageBox(hwndMain,
330: IDMSG_CANNOTOPENOUTPUTFILE,
331: MB_OK | MB_ERROR,
332: FALSE);
333: return;
334: } else
335: DosClose(hf);
336:
337: /* if file exists, ask if we want to overwrite it */
338: if(ulAction == FILE_EXISTED) {
339: sT = MessageBox(hwndMain,
340: IDMSG_OVERWRITEFILE,
341: MB_QUERY | MB_YESNOCANCEL,
342: FALSE);
343:
344:
345: if(sT == MBID_CANCEL)
346: return;
347:
348: if(sT == MBID_YES)
349: break;
350:
351: /* if user selected no, repeat the sequence */
352: }
353:
354: } /* while(TRUE) */
355:
356: UpdateTitleText(hwndMainFrame);
357:
358: /*
359: * Now that we have a valid file name, save the file. This is
360: * normally done under the File Save function so we can just
361: * call the FileSave() function here. Note that FileSave() will
362: * not call FileSaveAs() back since there is a valid file name
363: */
364: FileSave(mp2);
365:
366:
367:
368: /* This routine currently doesn't use the mp2 parameter but *\
369: * it is referenced here to prevent an 'Unreferenced Parameter'
370: \* warning at compile time. */
371: mp2;
372:
373: } /* FileSaveAs() */
374:
375: #ifdef PRINT_DLGS_ENABLED
376:
377: /****************************************************************\
378: * Print file routine
379: *--------------------------------------------------------------
380: *
381: * Name: FilePrint(mp2)
382: *
383: * Purpose: Processes the File menu's Print item.
384: *
385: * Usage: called whenever Print from the File menu is
386: * selected
387: *
388: * Method: Routine calls the application's print routine
389: *
390: * Returns:
391: *
392: \****************************************************************/
393: VOID FilePrint(mp2)
394: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
395: {
396:
397: Print(hwndMain);
398:
399: /* This routine currently doesn't use the mp2 parameter but *\
400: * it is referenced here to prevent an 'Unreferenced Parameter'
401: \* warning at compile time. */
402: mp2;
403:
404: } /* FilePrint() */
405:
406: /****************************************************************\
407: * Page setup routine
408: *--------------------------------------------------------------
409: *
410: * Name: FilePageSetup(mp2)
411: *
412: * Purpose: Processes the File menu's Page Setup item.
413: *
414: * Usage: called whenever Page Setup from the File menu is
415: * selected
416: *
417: * Method: Routine calls the application's page setup routine
418: *
419: * Returns:
420: *
421: \****************************************************************/
422: VOID FilePageSetup(mp2)
423: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
424: {
425:
426: PageSetup(hwndMain);
427:
428: /* This routine currently doesn't use the mp2 parameter but *\
429: * it is referenced here to prevent an 'Unreferenced Parameter'
430: \* warning at compile time. */
431: mp2;
432:
433: } /* FilePageSetup() */
434:
435: /****************************************************************\
436: * Print Setup routine
437: *--------------------------------------------------------------
438: *
439: * Name: FilePrintSetup(mp2)
440: *
441: * Purpose: Processes the File menu's Print Setup item.
442: *
443: * Usage: called whenever Print Setup from the File menu is
444: * selected
445: *
446: * Method: Routine calls the application's Print Setup routine
447: *
448: * Returns:
449: *
450: \****************************************************************/
451: VOID FilePrintSetup(mp2)
452: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
453: {
454:
455: PrintSetup(hwndMain);
456:
457: /* This routine currently doesn't use the mp2 parameter but *\
458: * it is referenced here to prevent an 'Unreferenced Parameter'
459: \* warning at compile time. */
460: mp2;
461:
462: } /* FilePrintSetup() */
463:
464: #endif /* PRINT_DLGS_ENABLED */
465:
466: /****************************************************************\
467: * Exit routine
468: *--------------------------------------------------------------
469: *
470: * Name: FileExit(mp2)
471: *
472: * Purpose: Processes the File menu's Exit item.
473: *
474: * Usage: called whenever Exit from the file menu is
475: * selected
476: *
477: * Method: Routine posts a WM_CLOSE message to the main
478: * application window.
479: *
480: * Returns:
481: *
482: \****************************************************************/
483: VOID FileExit(mp2)
484: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
485: {
486:
487: WinPostMsg(hwndMain, WM_CLOSE, (MPARAM)NULL, (MPARAM)NULL);
488:
489: /* This routine currently doesn't use the mp2 parameter but *\
490: * it is referenced here to prevent an 'Unreferenced Parameter'
491: \* warning at compile time. */
492: mp2;
493:
494: } /* FileExit() */
495:
496: /****************************************************************\
497: * Write file routine
498: *--------------------------------------------------------------
499: *
500: * Name: WriteFileToDisk(hf)
501: *
502: * Purpose: Writes the current file to the file in hf
503: *
504: * Usage: called from FileSave and FileSaveAs when a file
505: * is to be saved to disk
506: *
507: * Method:
508: *
509: *
510: * NOTE: This routine must not close the file.
511: *
512: * Returns:
513: *
514: \****************************************************************/
515: VOID WriteFileToDisk(hf) \
516: HFILE hf; /* file handle to file opened for input */
517: {
518:
519: /*--------------------------------------------------------------*\
520: * Place routine to write a disk file here
521: \*--------------------------------------------------------------*/
522:
523: hf;
524:
525:
526: } /* WriteFileToDisk() */
527:
528: /****************************************************************\
529: * Get file name routine
530: *--------------------------------------------------------------
531: *
532: * Name: GetFileName()
533: *
534: * Purpose: Gets the name of the save file.
535: *
536: * Usage: called when the user needs to supply a name for
537: * the file to be saved
538: *
539: * Method: calls the standard file open dialog to get the
540: * file name.
541: *
542: * Returns: TRUE if successful in getting a file name, FALSE
543: * if not
544: *
545: \****************************************************************/
546: BOOL GetFileName(VOID)
547: {
548: FILEDLG fdg;
549: CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
550:
551: fdg.cbsize = sizeof(FILEDLG);
552: fdg.usDialogType = SAVEAS_DIALOG;
553:
554: if(!WinLoadString(hab, NULL, IDS_SAVEDLGTITLE, MESSAGELEN, szTitle)) {
555: MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
556: return FALSE;
557: }
558:
559: if(!WinLoadString(hab, NULL, IDS_SAVEDLGBUTTON, MESSAGELEN, szButton)) {
560: MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
561: return FALSE;
562: }
563:
564: fdg.pszTitle = szTitle;
565: fdg.pszOKButton = szButton;
566:
567: fdg.lUser = 0L;
568: fdg.fl = FDS_HELPBUTTON | FDS_CENTER;
569: fdg.pfnDlgProc = NULL;
570: fdg.lReturn = 0L;
571: fdg.lSRC = 0L;
572: fdg.hmod = NULL;
573: fdg.idDlg = FILESAVE;
574: fdg.x = 0;
575: fdg.y = 0;
576: fdg.pszIType = 0L;
577: fdg.ppszITypeList = 0L;
578: fdg.pszIDrive = 0L;
579: fdg.ppszIDriveList = 0L;
580: fdg.sEAType = 0;
581: strcpy(fdg.szFullFile, szFullPath);
582:
583: /* get the file */
584: if(!KitFileDlg(hwndMain, (PFILEDLG)&fdg))
585: return FALSE;
586:
587: if(fdg.lReturn != ID_OK)
588: return FALSE;
589:
590: /* copy file name and path returned into buffers */
591: strcpy(szFullPath, fdg.szFullFile);
592:
593: return TRUE;
594:
595: } /* GetFileName() */
596:
597: /****************************************************************\
598: * Appends the app name to the title bar text
599: *--------------------------------------------------------------
600: *
601: * Name: UpdateTitleText(hwnd)
602: *
603: * Purpose: Updates the text in the main window's title bar to
604: * display the app name, followed by the separator,
605: * followed by the file name
606: *
607: * Usage: called at init time and when the text file is changed
608: *
609: * Method: gets the program name, appends the separator, and
610: * appends the file name.
611: *
612: * Returns:
613: *
614: \****************************************************************/
615: VOID UpdateTitleText(hwnd)
616: HWND hwnd; /* handle to frame window */
617: {
618: CHAR szBuf[MAXNAMEL];
619: CHAR szSeparator[TITLESEPARATORLEN+1];
620: PSZ pszT;
621:
622: WinQueryTaskTitle(NULL, szBuf, MAXNAMEL);
623:
624: WinLoadString(hab,
625: NULL,
626: IDS_TITLEBARSEPARATOR,
627: TITLESEPARATORLEN,
628: szSeparator);
629:
630: strcat(szBuf, szSeparator);
631:
632: if(szFullPath[0] == '\0')
633: pszT = szUntitled;
634: else
635: pszT = szFullPath;
636:
637: strcat(szBuf, pszT);
638:
639: WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);
640:
641: } /* UpdateTitleText() */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.