[BACK]Return to winmain.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gnuplot / win

Annotation of OpenXM_contrib/gnuplot/win/winmain.c, Revision 1.1.1.2

1.1       maekawa     1: #ifndef lint
1.1.1.2 ! maekawa     2: static char *RCSid = "$Id: winmain.c,v 1.3.2.1 1999/09/21 18:48:28 lhecking 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:
                     98: extern char version[];
                     99: extern char patchlevel[];
                    100: extern char date[];
                    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 ...   */
                    167: #define MAXCMDTOKENS 128
                    168:        int     _argc=0;
                    169:        LPSTR   _argv[MAXCMDTOKENS];
                    170:        _argv[_argc] = "wgnuplot.exe";
                    171:        _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
                    172:        while (_argv[_argc] != NULL)
                    173:                _argv[++_argc] = _fstrtok( NULL, " ");
                    174: #endif /* __MSC__ */
                    175:
                    176:        szModuleName = (LPSTR)farmalloc(MAXSTR+1);
                    177:        CheckMemory(szModuleName);
                    178:
                    179:        /* get path to EXE */
                    180:        GetModuleFileName(hInstance, (LPSTR) szModuleName, MAXSTR);
                    181: #ifndef WIN32
                    182:        if (CheckWGNUPLOTVersion(WGNUPLOTVERSION)) {
                    183:                MessageBox(NULL, "Wrong version of WGNUPLOT.DLL", szModuleName, MB_ICONSTOP | MB_OK);
                    184:                exit(1);
                    185:        }
                    186: #endif
                    187:        if ((tail = (LPSTR)_fstrrchr(szModuleName,'\\')) != (LPSTR)NULL)
                    188:        {
                    189:                tail++;
                    190:                *tail = 0;
                    191:        }
                    192:        szModuleName = (LPSTR)farrealloc(szModuleName, _fstrlen(szModuleName)+1);
                    193:        CheckMemory(szModuleName);
                    194:
                    195:        winhelpname = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(HELPFILE)+1);
                    196:        CheckMemory(winhelpname);
                    197:        _fstrcpy(winhelpname,szModuleName);
                    198:        _fstrcat(winhelpname,HELPFILE);
                    199:
                    200:        szMenuName = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(MENUNAME)+1);
                    201:        CheckMemory(szMenuName);
                    202:        _fstrcpy(szMenuName,szModuleName);
                    203:        _fstrcat(szMenuName,MENUNAME);
                    204:
                    205:        textwin.hInstance = hInstance;
                    206:        textwin.hPrevInstance = hPrevInstance;
                    207:        textwin.nCmdShow = nCmdShow;
                    208:        textwin.Title = "gnuplot";
                    209:        textwin.IniFile = "wgnuplot.ini";
                    210:        textwin.IniSection = "WGNUPLOT";
                    211:        textwin.DragPre = "load '";
                    212:        textwin.DragPost = "'\n";
                    213:        textwin.lpmw = &menuwin;
                    214:        textwin.ScreenSize.x = 80;
                    215:        textwin.ScreenSize.y = 80;
                    216:        textwin.KeyBufSize = 2048;
                    217:        textwin.CursorFlag = 1; /* scroll to cursor after \n & \r */
                    218:        textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance);
                    219:        textwin.AboutText = (LPSTR)farmalloc(1024);
                    220:        CheckMemory(textwin.AboutText);
                    221:        sprintf(textwin.AboutText,"Version %s\nPatchlevel %s\nLast Modified %s\n%s\n%s, %s and many others",
                    222:                version, patchlevel, date, gnuplot_copyright, authors[1], authors[0]);
                    223:        textwin.AboutText = (LPSTR)farrealloc(textwin.AboutText, _fstrlen(textwin.AboutText)+1);
                    224:        CheckMemory(textwin.AboutText);
                    225:
                    226:        menuwin.szMenuName = szMenuName;
                    227:
                    228:        pausewin.hInstance = hInstance;
                    229:        pausewin.hPrevInstance = hPrevInstance;
                    230:        pausewin.Title = "gnuplot pause";
                    231:
                    232:        graphwin.hInstance = hInstance;
                    233:        graphwin.hPrevInstance = hPrevInstance;
                    234:        graphwin.Title = "gnuplot graph";
                    235:        graphwin.lptw = &textwin;
                    236:        graphwin.IniFile = textwin.IniFile;
                    237:        graphwin.IniSection = textwin.IniSection;
                    238:        graphwin.color=TRUE;
                    239:        graphwin.fontsize = WINFONTSIZE;
                    240:
                    241:        if (TextInit(&textwin))
                    242:                exit(1);
                    243:        textwin.hIcon = LoadIcon(hInstance, "TEXTICON");
                    244: #ifdef WIN32
                    245:        SetClassLong(textwin.hWndParent, GCL_HICON, (DWORD)textwin.hIcon);
                    246: #else
                    247:        SetClassWord(textwin.hWndParent, GCW_HICON, (WORD)textwin.hIcon);
                    248: #endif
                    249:        if (_argc>1) {
                    250:                int i,noend=FALSE;
                    251:                for (i=0; i<_argc; ++i)
                    252:                        if (!stricmp(_argv[i],"-noend") || !stricmp(_argv[i],"/noend"))
                    253:                                noend = TRUE;
                    254:                if (noend)
                    255:                        ShowWindow(textwin.hWndParent, textwin.nCmdShow);
                    256:        }
                    257:        else
                    258:                ShowWindow(textwin.hWndParent, textwin.nCmdShow);
                    259:        if (IsIconic(textwin.hWndParent)) { /* update icon */
                    260:                RECT rect;
                    261:                GetClientRect(textwin.hWndParent, (LPRECT) &rect);
                    262:                InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1);
                    263:                UpdateWindow(textwin.hWndParent);
                    264:        }
                    265:
                    266:
                    267:        atexit(WinExit);
                    268:
                    269:        gnu_main(_argc, _argv, environ);
                    270:
                    271:        return 0;
                    272: }
                    273:
                    274:
                    275: /* replacement stdio routines that use Text Window for stdin/stdout */
                    276: /* WARNING: Do not write to stdout/stderr with functions not listed
                    277:    in win/wtext.h */
                    278:
                    279: #undef kbhit
                    280: #undef getche
                    281: #undef getch
                    282: #undef putch
                    283:
                    284: #undef fgetc
                    285: #undef getchar
                    286: #undef getc
                    287: #undef fgets
                    288: #undef gets
                    289:
                    290: #undef fputc
                    291: #undef putchar
                    292: #undef putc
                    293: #undef fputs
                    294: #undef puts
                    295:
                    296: #undef fprintf
                    297: #undef printf
                    298: #undef vprintf
                    299: #undef vfprintf
                    300:
                    301: #undef fwrite
                    302: #undef fread
                    303:
                    304: #if defined(__MSC__)|| defined(WIN32)
                    305: #define isterm(f) (f==stdin || f==stdout || f==stderr)
                    306: #else
                    307: #define isterm(f) isatty(fileno(f))
                    308: #endif
                    309:
                    310: int
                    311: MyPutCh(int ch)
                    312: {
                    313:        return TextPutCh(&textwin, (BYTE)ch);
                    314: }
                    315:
                    316: int
                    317: MyKBHit(void)
                    318: {
                    319:        return TextKBHit(&textwin);
                    320: }
                    321:
                    322: int
                    323: MyGetCh(void)
                    324: {
                    325:        return TextGetCh(&textwin);
                    326: }
                    327:
                    328: int
                    329: MyGetChE(void)
                    330: {
                    331:        return TextGetChE(&textwin);
                    332: }
                    333:
                    334: int
                    335: MyFGetC(FILE *file)
                    336: {
                    337:        if (isterm(file)) {
                    338:                return MyGetChE();
                    339:        }
                    340:        return fgetc(file);
                    341: }
                    342:
                    343: char *
                    344: MyGetS(char *str)
                    345: {
                    346:        TextPutS(&textwin,"\nDANGER: gets() used\n");
                    347:        MyFGetS(str,80,stdin);
                    348:        if (strlen(str) > 0
                    349:         && str[strlen(str)-1]=='\n')
                    350:                str[strlen(str)-1] = '\0';
                    351:        return str;
                    352: }
                    353:
                    354: char *
                    355: MyFGetS(char *str, unsigned int size, FILE *file)
                    356: {
                    357: char FAR *p;
                    358:        if (isterm(file)) {
                    359:                p = TextGetS(&textwin, str, size);
                    360:                if (p != (char FAR *)NULL)
                    361:                        return str;
                    362:                return (char *)NULL;
                    363:        }
                    364:        return fgets(str,size,file);
                    365: }
                    366:
                    367: int
                    368: MyFPutC(int ch, FILE *file)
                    369: {
                    370:        if (isterm(file)) {
                    371:                MyPutCh((BYTE)ch);
                    372:                TextMessage();
                    373:                return ch;
                    374:        }
                    375:        return fputc(ch,file);
                    376: }
                    377:
                    378: int
                    379: MyFPutS(char *str, FILE *file)
                    380: {
                    381:        if (isterm(file)) {
                    382:                TextPutS(&textwin, str);
                    383:                TextMessage();
                    384:                return (*str);  /* different from Borland library */
                    385:        }
                    386:        return fputs(str,file);
                    387: }
                    388:
                    389: int
                    390: MyPutS(char *str)
                    391: {
                    392:        TextPutS(&textwin, str);
                    393:        MyPutCh('\n');
                    394:        TextMessage();
                    395:        return 0;       /* different from Borland library */
                    396: }
                    397:
                    398: int MyFPrintF(FILE *file, char *fmt, ...)
                    399: {
                    400: int count;
                    401: va_list args;
                    402:        va_start(args,fmt);
                    403:        if (isterm(file)) {
                    404:                char buf[MAXPRINTF];
                    405:                count = vsprintf(buf,fmt,args);
                    406:                TextPutS(&textwin,buf);
                    407:        }
                    408:        else
                    409:                count = vfprintf(file, fmt, args);
                    410:        va_end(args);
                    411:        return count;
                    412: }
                    413:
                    414: int MyPrintF(char *fmt, ...)
                    415: {
                    416: int count;
                    417: char buf[MAXPRINTF];
                    418: va_list args;
                    419:        va_start(args,fmt);
                    420:        count = vsprintf(buf,fmt,args);
                    421:        TextPutS(&textwin,buf);
                    422:        va_end(args);
                    423:        return count;
                    424: }
                    425:
                    426: size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *file)
                    427: {
                    428:        if (isterm(file)) {
                    429:                size_t i;
                    430:                for (i=0; i<n; i++)
                    431:                        TextPutCh(&textwin, ((BYTE *)ptr)[i]);
                    432:                TextMessage();
                    433:                return n;
                    434:        }
                    435:        return fwrite(ptr, size, n, file);
                    436: }
                    437:
                    438: size_t MyFRead(void *ptr, size_t size, size_t n, FILE *file)
                    439: {
                    440:        if (isterm(file)) {
                    441:                size_t i;
                    442:                for (i=0; i<n; i++)
                    443:                        ((BYTE *)ptr)[i] = TextGetChE(&textwin);
                    444:                TextMessage();
                    445:                return n;
                    446:        }
                    447:        return fread(ptr, size, n, file);
                    448: }
                    449:
                    450: /* public interface to printer routines : Windows PRN emulation
                    451:  * (formerly in win.trm)
                    452:  */
                    453:
                    454: #define MAX_PRT_LEN 256
                    455: static char win_prntmp[MAX_PRT_LEN+1];
                    456:
                    457: FILE *
                    458: open_printer()
                    459: {
                    460: char *temp;
                    461:        if ((temp = getenv("TEMP")) == (char *)NULL)
                    462:                *win_prntmp='\0';
                    463:        else  {
                    464:                strncpy(win_prntmp,temp,MAX_PRT_LEN);
                    465:                /* stop X's in path being converted by mktemp */
                    466:                for (temp=win_prntmp; *temp; temp++)
                    467:                        *temp = tolower(*temp);
                    468:                if ( strlen(win_prntmp) && (win_prntmp[strlen(win_prntmp)-1]!='\\') )
                    469:                        strcat(win_prntmp,"\\");
                    470:        }
                    471:        strncat(win_prntmp, "_gptmp",MAX_PRT_LEN-strlen(win_prntmp));
                    472:        strncat(win_prntmp, "XXXXXX",MAX_PRT_LEN-strlen(win_prntmp));
                    473:        mktemp(win_prntmp);
                    474:        return fopen(win_prntmp, "w");
                    475: }
                    476:
                    477: void
                    478: close_printer(FILE *outfile)
                    479: {
                    480:        fclose(outfile);
                    481:        DumpPrinter(graphwin.hWndGraph, graphwin.Title, win_prntmp);
                    482: }
                    483:
                    484: void
                    485: screen_dump(void)
                    486: {
                    487:        GraphPrint(&graphwin);
                    488: }
                    489:
                    490:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>