Initial release of Maemo 5 port of gnuplot
[gnuplot] / src / os2 / gnupmdrv.c
1 #ifdef INCRCSDATA
2 static char RCSid[]="$Id: gnupmdrv.c,v 1.6 2005/07/28 07:46:06 mikulik Exp $" ;
3 #endif
4
5 /****************************************************************************
6
7     PROGRAM: gnupmdrv
8
9     Outboard PM driver for GNUPLOT 3.x
10
11     MODULE:  gnupmdrv.c
12
13     This file contains the startup procedures for gnupmdrv
14
15 ****************************************************************************/
16
17 /* PM driver for GNUPLOT */
18
19 /*[
20  * Copyright 1992, 1993, 1998, 2004   Roger Fearick
21  *
22  * Permission to use, copy, and distribute this software and its
23  * documentation for any purpose with or without fee is hereby granted,
24  * provided that the above copyright notice appear in all copies and
25  * that both that copyright notice and this permission notice appear
26  * in supporting documentation.
27  *
28  * Permission to modify the software is granted, but not the right to
29  * distribute the complete modified source code.  Modifications are to
30  * be distributed as patches to the released version.  Permission to
31  * distribute binaries produced by compiling modified sources is granted,
32  * provided you
33  *   1. distribute the corresponding source modifications from the
34  *    released version in the form of a patch file along with the binaries,
35  *   2. add special version identification to distinguish your version
36  *    in addition to the base release version number,
37  *   3. provide your name and address as the primary contact for the
38  *    support of your modified version, and
39  *   4. retain our contact information in regard to use of the base
40  *    software.
41  * Permission to distribute the released version of the source code along
42  * with corresponding source modifications in the form of a patch file is
43  * granted with same provisions 2 through 4 for binary distributions.
44  *
45  * This software is provided "as is" without express or implied warranty
46  * to the extent permitted by applicable law.
47 ]*/
48
49
50 /*
51  * AUTHOR
52  *
53  *   Gnuplot driver for OS/2:  Roger Fearick
54  */
55
56 #define INCL_PM
57 #define INCL_WIN
58 #define INCL_SPL
59 #define INCL_SPLDOSPRINT
60 #define INCL_DOSMEMMGR
61 #define INCL_DOSPROCESS
62 #define INCL_DOSFILEMGR
63 #include <os2.h>
64 #include <string.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include "config.h"
68 #include "gnupmdrv.h"
69
70 /*==== g l o b a l    d a t a ================================================*/
71
72 char szIPCName[256] ;
73 char szIniFile[256] ;
74 #define IPCDEFAULT "gnuplot"
75 int  bServer=0 ;
76 int  bPersist=0 ;
77 int  bWideLines=0 ;
78 #ifdef PM_KEEP_OLD_ENHANCED_TEXT
79 int  bEnhanced=0 ;
80 #endif
81
82 /*==== l o c a l    d a t a ==================================================*/
83
84             /* class names for window registration */
85
86 static char szTitle[256] = "Gnuplot" ;
87
88 /*==== f u n c t i o n s =====================================================*/
89
90 BOOL             QueryIni( HAB ) ;
91 int              main( int, char** ) ;
92 static HWND      InitHelp( HAB, HWND ) ;
93
94 /*==== c o d e ===============================================================*/
95
96 int main ( int argc, char **argv )
97 /*
98 ** args:  argv[1] : name to be used for IPC (pipes/semaphores) with gnuplot
99 **
100 ** Standard PM initialisation:
101 ** -- set up message processing loop
102 ** -- register all window classes
103 ** -- start up main window
104 ** -- subclass main window for help and dde message trapping to frame window
105 ** -- init help system
106 ** -- check command line and open any filename found there
107 **
108 */
109     {
110     static ULONG flFrameFlags = (FCF_ACCELTABLE|FCF_STANDARD);//&(~FCF_TASKLIST) ;
111     static ULONG flClientFlags = WS_VISIBLE ;
112     HMQ          hmq ;
113     QMSG         qmsg ;
114     PFNWP        pfnOldFrameWndProc ;
115     HWND         hwndHelp ;
116     BOOL         bPos ;
117
118     /* (am, 19981001)
119      * A subtile problem is fixed here:
120      * upon the first initialization of this driver (i.e. we're here in main())
121      * it may inherit handles of files opened (temporarily) by gnuplot itself!
122      * We close them here.
123      */
124     _fcloseall();
125
126     if( argc <= 1 ) strcpy( szIPCName, IPCDEFAULT ) ;
127     else {
128         int i ;
129         strcpy( szIPCName, argv[1] ) ;
130         for ( i=2; i<argc; i++ ) {
131                     while( *argv[i] != '\0' ) {
132                 if( *argv[i] == '-' ) {
133                     ++argv[i] ;
134                     switch( *argv[i] ) {
135                         case 's' :
136                             bServer = 1 ;
137                             break ;
138 #ifdef PM_KEEP_OLD_ENHANCED_TEXT
139                         case 'e' :
140                             bEnhanced = 1 ;
141                             break ;
142 #endif
143                         case 'p' :
144                             bPersist = 1 ;
145                             break ;
146                         case 'w' :
147                             bWideLines = 1 ;
148                             break ;
149                         }
150                     }
151                 else if ( *argv[i] == '"' ) {
152                     char *p = szTitle ;
153                     argv[i]++ ;
154                     while( *argv[i] != '"' ) {
155                         *p++ = *argv[i]++ ;
156                         }
157                     *p = '\0' ;
158                     }
159                 argv[i]++ ;
160                 }
161             }
162         }
163     {
164     char *p ;
165         /* get path from argv[0] to track down program files */
166     strcpy( szIniFile, argv[0] ) ;
167     while( (p=strchr(szIniFile,'/'))!=NULL ) *p = '\\' ;
168     p = strrchr(szIniFile,'\\') ;
169     if(p==NULL) p = strrchr(szIniFile,':') ;
170     if(p==NULL) p = szIniFile ;
171     else ++p ;
172     strcpy(p,GNUINI);
173     }
174
175     hab = WinInitialize( 0 ) ;
176     hmq = WinCreateMsgQueue( hab, 50 ) ;
177
178                 // get defaults from gnupmdrv.ini
179
180     bPos = QueryIni( hab ) ;
181
182                 // register window and child window classes
183
184     if( ! WinRegisterClass( hab,        /* Exit if can't register */
185                             APP_NAME,
186                             (PFNWP)DisplayClientWndProc,
187                             CS_SIZEREDRAW,
188                             0 )
189                             ) return 0L ;
190
191                 // create main window
192
193     hwndFrame = WinCreateStdWindow (
194                     HWND_DESKTOP,
195                     0,//WS_VISIBLE,
196                     &flFrameFlags,
197                     APP_NAME,
198                     NULL,
199                     flClientFlags,
200                     0L,
201                     1,
202                     &hApp) ;
203
204     if ( ! hwndFrame ) return 0 ;
205
206                 // subclass window for help & DDE trapping
207
208     pfnOldFrameWndProc = WinSubclassWindow( hwndFrame, (PFNWP)NewFrameWndProc ) ;
209     WinSetWindowULong( hwndFrame, QWL_USER, (ULONG) pfnOldFrameWndProc ) ;
210
211                 // init the help manager
212
213     hwndHelp = InitHelp( hab, hwndFrame ) ;
214
215                 // set window title and make it active
216
217     {
218     char text[256] = APP_NAME;
219     strcat( text, " [" ) ;
220     strcat( text, szTitle ) ;
221     strcat( text, "]" ) ;
222     WinSetWindowText( hwndFrame, text ) ;
223     }
224                 // process window messages
225
226     while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0))
227          WinDispatchMsg (hab, &qmsg) ;
228
229                 // shut down
230
231     WinDestroyHelpInstance( hwndHelp ) ;
232     WinDestroyWindow (hwndFrame) ;
233     WinDestroyMsgQueue (hmq) ;
234     WinTerminate (hab) ;
235
236     return 0 ;
237     }
238
239 static HWND InitHelp( HAB hab, HWND hwnd )
240 /*
241 **  initialise the help system
242 */
243     {
244     static HELPINIT helpinit = { sizeof(HELPINIT),
245                                  0L,
246                                  NULL,
247                                  (PHELPTABLE)MAKELONG(1, 0xFFFF),
248                                  0L,
249                                  0L,
250                                  0,
251                                  0,
252                                  "GnuplotPM Help",
253                                  CMIC_HIDE_PANEL_ID,
254                                  "gnupmdrv.hlp" } ;
255     HWND hwndHelp ;
256     /* should be bigger or dynamic */
257     static char helppath[256] ;
258     char *p;
259     _execname(helppath, sizeof(helppath));
260     _fnslashify(helppath);
261     p=strrchr(helppath, '/');
262     if (p)
263        *p='\0';
264     strcat( helppath, "/" ) ;
265     strcat( helppath, helpinit.pszHelpLibraryName ) ;
266     helpinit.pszHelpLibraryName = helppath ;
267
268     hwndHelp = WinCreateHelpInstance( hab, &helpinit ) ;
269     WinAssociateHelpInstance( hwndHelp, hwnd ) ;
270     return hwndHelp ;
271     }
272
273 MRESULT EXPENTRY NewFrameWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
274 /*
275 **  Subclasses top-level frame window to trap help & dde messages
276 */
277     {
278     PFNWP       pfnOldFrameWndProc ;
279
280     pfnOldFrameWndProc = (PFNWP) WinQueryWindowULong( hwnd, QWL_USER ) ;
281     switch( msg ) {
282         default:
283             break ;
284
285         case HM_QUERY_KEYS_HELP:
286             return (MRESULT) IDH_KEYS ;
287         }
288     return (*pfnOldFrameWndProc)(hwnd, msg, mp1, mp2) ;
289     }
290
291
292 MRESULT EXPENTRY About( HWND hDlg, ULONG message, MPARAM mp1, MPARAM mp2)
293 /*
294 ** 'About' box dialog function
295 */
296     {
297     return WinDefDlgProc( hDlg, message, mp1, mp2 ) ;
298     }
299