Annotation of OpenXM_contrib/gnuplot/win/winmain.c, Revision 1.1.1.3
1.1 maekawa 1: #ifndef lint
1.1.1.3 ! ohara 2: static char *RCSid = "$Id: winmain.c,v 1.3.2.3 2002/11/20 17:11:53 broeker Exp $";
1.1 maekawa 3: #endif
4:
5: /* GNUPLOT - win/winmain.c */
6: /*[
7: * Copyright 1992, 1993, 1998 Maurice Castro, Russell Lang
8: *
9: * Permission to use, copy, and distribute this software and its
10: * documentation for any purpose with or without fee is hereby granted,
11: * provided that the above copyright notice appear in all copies and
12: * that both that copyright notice and this permission notice appear
13: * in supporting documentation.
14: *
15: * Permission to modify the software is granted, but not the right to
16: * distribute the complete modified source code. Modifications are to
17: * be distributed as patches to the released version. Permission to
18: * distribute binaries produced by compiling modified sources is granted,
19: * provided you
20: * 1. distribute the corresponding source modifications from the
21: * released version in the form of a patch file along with the binaries,
22: * 2. add special version identification to distinguish your version
23: * in addition to the base release version number,
24: * 3. provide your name and address as the primary contact for the
25: * support of your modified version, and
26: * 4. retain our contact information in regard to use of the base
27: * software.
28: * Permission to distribute the released version of the source code along
29: * with corresponding source modifications in the form of a patch file is
30: * granted with same provisions 2 through 4 for binary distributions.
31: *
32: * This software is provided "as is" without express or implied warranty
33: * to the extent permitted by applicable law.
34: ]*/
35:
36: /*
37: * AUTHORS
38: *
39: * Maurice Castro
40: * Russell Lang
41: *
42: * Send your comments or suggestions to
43: * info-gnuplot@dartmouth.edu.
44: * This is a mailing list; to join it send a note to
45: * majordomo@dartmouth.edu.
46: * Send bug reports to
47: * bug-gnuplot@dartmouth.edu.
48: */
49:
50: /* This file implements the initialization code for running gnuplot */
51: /* under Microsoft Windows. The code currently compiles only with the */
52: /* Borland C++ 3.1 compiler.
53: /* */
54: /* The modifications to allow Gnuplot to run under Windows were made */
55: /* by Maurice Castro. (maurice@bruce.cs.monash.edu.au) 3 Jul 1992 */
56: /* and Russell Lang (rjl@monu1.cc.monash.edu.au) 30 Nov 1992 */
57: /* */
58:
59: #define STRICT
60: #include <windows.h>
61: #include <windowsx.h>
62: #include <dos.h>
63: #include <stdio.h>
64: #include <stdlib.h>
65: #include <string.h>
66: #include <stdarg.h>
67: #include <ctype.h>
68: #ifdef __MSC__
69: #include <malloc.h>
70: #else
71: # ifdef __TURBOC__ /* HBB 981201: MinGW32 doesn't have this */
72: #include <alloc.h>
73: #endif
74: #endif
75: #include <io.h>
76: #include "plot.h"
77: #include "setshow.h"
78: #include "wgnuplib.h"
79: #include "wtext.h"
80:
81: /* limits */
82: #define MAXSTR 255
83: #define MAXPRINTF 1024
84:
85: /* globals */
86: TW textwin;
87: GW graphwin;
88: PW pausewin;
89: MW menuwin;
90: LPSTR szModuleName;
91: LPSTR winhelpname;
92: LPSTR szMenuName;
93: #define MENUNAME "wgnuplot.mnu"
94: #ifndef HELPFILE /* HBB 981203: makefile.win predefines this... */
95: #define HELPFILE "wgnuplot.hlp"
96: #endif
97:
1.1.1.3 ! ohara 98: extern char gnuplot_version[];
! 99: extern char gnuplot_patchlevel[];
! 100: extern char gnuplot_date[];
1.1 maekawa 101: /*extern char *authors[];*/
102: char *authors[]={
103: "Colin Kelly",
104: "Thomas Williams"
105: };
106:
107: extern char gnuplot_copyright[];
108: void WinExit(void);
109: int gnu_main(int argc, char *argv[], char *env[]);
110:
111: void
112: CheckMemory(LPSTR str)
113: {
114: if (str == (LPSTR)NULL) {
115: MessageBox(NULL, "out of memory", "gnuplot", MB_ICONSTOP | MB_OK);
116: exit(1);
117: }
118: }
119:
120: int
121: Pause(LPSTR str)
122: {
123: pausewin.Message = str;
124: return (PauseBox(&pausewin) == IDOK);
125: }
126:
127: /* atexit procedure */
128: void
129: WinExit(void)
130: {
131: term_reset();
132:
133: #ifndef __MINGW32__ /* HBB 980809: FIXME: doesn't exist for MinGW32. So...? */
134: fcloseall();
135: #endif
136: if (graphwin.hWndGraph && IsWindow(graphwin.hWndGraph))
137: GraphClose(&graphwin);
138: TextMessage(); /* process messages */
139: WinHelp(textwin.hWndText,(LPSTR)winhelpname,HELP_QUIT,(DWORD)NULL);
140: TextClose(&textwin);
141: TextMessage(); /* process messages */
142: return;
143: }
144:
145: /* call back function from Text Window WM_CLOSE */
146: int CALLBACK WINEXPORT
147: ShutDown(void)
148: {
1.1.1.2 maekawa 149: #if 0 /* HBB 19990505: try to avoid crash on clicking 'close' */
150: /* Problem was that WinExit was called *twice*, once directly,
151: * and again via 'atexit'. This caused problems by double-freeing
152: * of GlobalAlloc-ed memory inside TextClose() */
153: /* Caveat: relies on atexit() working properly */
1.1 maekawa 154: WinExit();
1.1.1.2 maekawa 155: #endif
1.1 maekawa 156: exit(0);
157: return 0;
158: }
159:
160: int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
161: LPSTR lpszCmdLine, int nCmdShow)
162: {
163: /*WNDCLASS wndclass;*/
164: LPSTR tail;
165:
166: #ifdef __MSC__ /* MSC doesn't give us _argc and _argv[] so ... */
1.1.1.3 ! ohara 167: # ifdef WIN32 /* WIN32 has __argc and __argv */
! 168: # define _argv __argv
! 169: # define _argc __argc
! 170: # else
! 171: # define MAXCMDTOKENS 128
1.1 maekawa 172: int _argc=0;
173: LPSTR _argv[MAXCMDTOKENS];
174: _argv[_argc] = "wgnuplot.exe";
175: _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
176: while (_argv[_argc] != NULL)
177: _argv[++_argc] = _fstrtok( NULL, " ");
1.1.1.3 ! ohara 178: # endif /* WIN32 */
1.1 maekawa 179: #endif /* __MSC__ */
180:
181: szModuleName = (LPSTR)farmalloc(MAXSTR+1);
182: CheckMemory(szModuleName);
183:
184: /* get path to EXE */
185: GetModuleFileName(hInstance, (LPSTR) szModuleName, MAXSTR);
186: #ifndef WIN32
187: if (CheckWGNUPLOTVersion(WGNUPLOTVERSION)) {
188: MessageBox(NULL, "Wrong version of WGNUPLOT.DLL", szModuleName, MB_ICONSTOP | MB_OK);
189: exit(1);
190: }
191: #endif
192: if ((tail = (LPSTR)_fstrrchr(szModuleName,'\\')) != (LPSTR)NULL)
193: {
194: tail++;
195: *tail = 0;
196: }
197: szModuleName = (LPSTR)farrealloc(szModuleName, _fstrlen(szModuleName)+1);
198: CheckMemory(szModuleName);
199:
200: winhelpname = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(HELPFILE)+1);
201: CheckMemory(winhelpname);
202: _fstrcpy(winhelpname,szModuleName);
203: _fstrcat(winhelpname,HELPFILE);
204:
205: szMenuName = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(MENUNAME)+1);
206: CheckMemory(szMenuName);
207: _fstrcpy(szMenuName,szModuleName);
208: _fstrcat(szMenuName,MENUNAME);
209:
210: textwin.hInstance = hInstance;
211: textwin.hPrevInstance = hPrevInstance;
212: textwin.nCmdShow = nCmdShow;
213: textwin.Title = "gnuplot";
214: textwin.IniFile = "wgnuplot.ini";
215: textwin.IniSection = "WGNUPLOT";
216: textwin.DragPre = "load '";
217: textwin.DragPost = "'\n";
218: textwin.lpmw = &menuwin;
219: textwin.ScreenSize.x = 80;
220: textwin.ScreenSize.y = 80;
221: textwin.KeyBufSize = 2048;
222: textwin.CursorFlag = 1; /* scroll to cursor after \n & \r */
223: textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance);
224: textwin.AboutText = (LPSTR)farmalloc(1024);
225: CheckMemory(textwin.AboutText);
226: sprintf(textwin.AboutText,"Version %s\nPatchlevel %s\nLast Modified %s\n%s\n%s, %s and many others",
1.1.1.3 ! ohara 227: gnuplot_version, gnuplot_patchlevel, gnuplot_date,
! 228: gnuplot_copyright, authors[1], authors[0]);
1.1 maekawa 229: textwin.AboutText = (LPSTR)farrealloc(textwin.AboutText, _fstrlen(textwin.AboutText)+1);
230: CheckMemory(textwin.AboutText);
231:
232: menuwin.szMenuName = szMenuName;
233:
234: pausewin.hInstance = hInstance;
235: pausewin.hPrevInstance = hPrevInstance;
236: pausewin.Title = "gnuplot pause";
237:
238: graphwin.hInstance = hInstance;
239: graphwin.hPrevInstance = hPrevInstance;
240: graphwin.Title = "gnuplot graph";
241: graphwin.lptw = &textwin;
242: graphwin.IniFile = textwin.IniFile;
243: graphwin.IniSection = textwin.IniSection;
244: graphwin.color=TRUE;
245: graphwin.fontsize = WINFONTSIZE;
246:
247: if (TextInit(&textwin))
248: exit(1);
249: textwin.hIcon = LoadIcon(hInstance, "TEXTICON");
250: #ifdef WIN32
251: SetClassLong(textwin.hWndParent, GCL_HICON, (DWORD)textwin.hIcon);
252: #else
253: SetClassWord(textwin.hWndParent, GCW_HICON, (WORD)textwin.hIcon);
254: #endif
255: if (_argc>1) {
256: int i,noend=FALSE;
257: for (i=0; i<_argc; ++i)
258: if (!stricmp(_argv[i],"-noend") || !stricmp(_argv[i],"/noend"))
259: noend = TRUE;
260: if (noend)
261: ShowWindow(textwin.hWndParent, textwin.nCmdShow);
262: }
263: else
264: ShowWindow(textwin.hWndParent, textwin.nCmdShow);
265: if (IsIconic(textwin.hWndParent)) { /* update icon */
266: RECT rect;
267: GetClientRect(textwin.hWndParent, (LPRECT) &rect);
268: InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1);
269: UpdateWindow(textwin.hWndParent);
270: }
271:
272:
273: atexit(WinExit);
274:
275: gnu_main(_argc, _argv, environ);
276:
277: return 0;
278: }
279:
280:
281: /* replacement stdio routines that use Text Window for stdin/stdout */
282: /* WARNING: Do not write to stdout/stderr with functions not listed
283: in win/wtext.h */
284:
285: #undef kbhit
286: #undef getche
287: #undef getch
288: #undef putch
289:
290: #undef fgetc
291: #undef getchar
292: #undef getc
293: #undef fgets
294: #undef gets
295:
296: #undef fputc
297: #undef putchar
298: #undef putc
299: #undef fputs
300: #undef puts
301:
302: #undef fprintf
303: #undef printf
304: #undef vprintf
305: #undef vfprintf
306:
307: #undef fwrite
308: #undef fread
309:
310: #if defined(__MSC__)|| defined(WIN32)
311: #define isterm(f) (f==stdin || f==stdout || f==stderr)
312: #else
313: #define isterm(f) isatty(fileno(f))
314: #endif
315:
316: int
317: MyPutCh(int ch)
318: {
319: return TextPutCh(&textwin, (BYTE)ch);
320: }
321:
322: int
323: MyKBHit(void)
324: {
325: return TextKBHit(&textwin);
326: }
327:
328: int
329: MyGetCh(void)
330: {
331: return TextGetCh(&textwin);
332: }
333:
334: int
335: MyGetChE(void)
336: {
337: return TextGetChE(&textwin);
338: }
339:
340: int
341: MyFGetC(FILE *file)
342: {
343: if (isterm(file)) {
344: return MyGetChE();
345: }
346: return fgetc(file);
347: }
348:
349: char *
350: MyGetS(char *str)
351: {
352: TextPutS(&textwin,"\nDANGER: gets() used\n");
353: MyFGetS(str,80,stdin);
354: if (strlen(str) > 0
355: && str[strlen(str)-1]=='\n')
356: str[strlen(str)-1] = '\0';
357: return str;
358: }
359:
360: char *
361: MyFGetS(char *str, unsigned int size, FILE *file)
362: {
363: char FAR *p;
364: if (isterm(file)) {
365: p = TextGetS(&textwin, str, size);
366: if (p != (char FAR *)NULL)
367: return str;
368: return (char *)NULL;
369: }
370: return fgets(str,size,file);
371: }
372:
373: int
374: MyFPutC(int ch, FILE *file)
375: {
376: if (isterm(file)) {
377: MyPutCh((BYTE)ch);
378: TextMessage();
379: return ch;
380: }
381: return fputc(ch,file);
382: }
383:
384: int
385: MyFPutS(char *str, FILE *file)
386: {
387: if (isterm(file)) {
388: TextPutS(&textwin, str);
389: TextMessage();
390: return (*str); /* different from Borland library */
391: }
392: return fputs(str,file);
393: }
394:
395: int
396: MyPutS(char *str)
397: {
398: TextPutS(&textwin, str);
399: MyPutCh('\n');
400: TextMessage();
401: return 0; /* different from Borland library */
402: }
403:
404: int MyFPrintF(FILE *file, char *fmt, ...)
405: {
406: int count;
407: va_list args;
408: va_start(args,fmt);
409: if (isterm(file)) {
410: char buf[MAXPRINTF];
411: count = vsprintf(buf,fmt,args);
412: TextPutS(&textwin,buf);
413: }
414: else
415: count = vfprintf(file, fmt, args);
416: va_end(args);
417: return count;
418: }
419:
420: int MyPrintF(char *fmt, ...)
421: {
422: int count;
423: char buf[MAXPRINTF];
424: va_list args;
425: va_start(args,fmt);
426: count = vsprintf(buf,fmt,args);
427: TextPutS(&textwin,buf);
428: va_end(args);
429: return count;
430: }
431:
432: size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *file)
433: {
434: if (isterm(file)) {
435: size_t i;
436: for (i=0; i<n; i++)
437: TextPutCh(&textwin, ((BYTE *)ptr)[i]);
438: TextMessage();
439: return n;
440: }
441: return fwrite(ptr, size, n, file);
442: }
443:
444: size_t MyFRead(void *ptr, size_t size, size_t n, FILE *file)
445: {
446: if (isterm(file)) {
447: size_t i;
448: for (i=0; i<n; i++)
449: ((BYTE *)ptr)[i] = TextGetChE(&textwin);
450: TextMessage();
451: return n;
452: }
453: return fread(ptr, size, n, file);
454: }
455:
456: /* public interface to printer routines : Windows PRN emulation
457: * (formerly in win.trm)
458: */
459:
460: #define MAX_PRT_LEN 256
461: static char win_prntmp[MAX_PRT_LEN+1];
462:
463: FILE *
464: open_printer()
465: {
466: char *temp;
467: if ((temp = getenv("TEMP")) == (char *)NULL)
468: *win_prntmp='\0';
469: else {
470: strncpy(win_prntmp,temp,MAX_PRT_LEN);
471: /* stop X's in path being converted by mktemp */
472: for (temp=win_prntmp; *temp; temp++)
473: *temp = tolower(*temp);
474: if ( strlen(win_prntmp) && (win_prntmp[strlen(win_prntmp)-1]!='\\') )
475: strcat(win_prntmp,"\\");
476: }
477: strncat(win_prntmp, "_gptmp",MAX_PRT_LEN-strlen(win_prntmp));
478: strncat(win_prntmp, "XXXXXX",MAX_PRT_LEN-strlen(win_prntmp));
479: mktemp(win_prntmp);
480: return fopen(win_prntmp, "w");
481: }
482:
483: void
484: close_printer(FILE *outfile)
485: {
486: fclose(outfile);
487: DumpPrinter(graphwin.hWndGraph, graphwin.Title, win_prntmp);
488: }
489:
490: void
491: screen_dump(void)
492: {
493: GraphPrint(&graphwin);
494: }
495:
496:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>