1 /****************************************************************************
2
3 PROGRAM: gnupmdrv
4
5 Outboard PM driver for GNUPLOT 3.x
6
7 MODULE: dialogs.c Dialog procedures for gnupmdrv
8
9 ****************************************************************************/
10
11 /* PM driver for GNUPLOT */
12
13 /*[
14 * Copyright 1992, 1993, 1998, 2004 Roger Fearick
15 *
16 * Permission to use, copy, and distribute this software and its
17 * documentation for any purpose with or without fee is hereby granted,
18 * provided that the above copyright notice appear in all copies and
19 * that both that copyright notice and this permission notice appear
20 * in supporting documentation.
21 *
22 * Permission to modify the software is granted, but not the right to
23 * distribute the complete modified source code. Modifications are to
24 * be distributed as patches to the released version. Permission to
25 * distribute binaries produced by compiling modified sources is granted,
26 * provided you
27 * 1. distribute the corresponding source modifications from the
28 * released version in the form of a patch file along with the binaries,
29 * 2. add special version identification to distinguish your version
30 * in addition to the base release version number,
31 * 3. provide your name and address as the primary contact for the
32 * support of your modified version, and
33 * 4. retain our contact information in regard to use of the base
34 * software.
35 * Permission to distribute the released version of the source code along
36 * with corresponding source modifications in the form of a patch file is
37 * granted with same provisions 2 through 4 for binary distributions.
38 *
39 * This software is provided "as is" without express or implied warranty
40 * to the extent permitted by applicable law.
41 ]*/
42
43 /*
44 * AUTHOR
45 *
46 * Gnuplot driver for OS/2: Roger Fearick
47 */
48
49 #define INCL_PM
50 #define INCL_WIN
51 #define INCL_DEV
52 #define INCL_SPL
53 #define INCL_SPLDOSPRINT
54 #define INCL_WINDIALOGS
55 #define INCL_WINBUTTONS
56 #define INCL_WINSYS
57 #define INCL_WINFRAMEMGR
58 #define INCL_WINPOINTERS
59 #define INCL_WINTRACKRECT
60 #define INCL_WINENTRYFIELDS
61 #define INCL_WINWINDOWMGR
62 #include <os2.h>
63 #include <math.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include "gnupmdrv.h"
68
69 /* struct for printer capabilities */
70
71 static struct {
72 long lTech ; // printer technology
73 long lVer ; // driver version
74 long lWidth ; // page width in pels
75 long lHeight ; // page height in pels
76 long lWChars ; // page width in chars
77 long lHChars ; // page height in chars
78 long lHorRes ; // horizontal resolution pels / metre
79 long lVertRes ; // vertical resolution pels / metre
80 } prCaps ;
81
82 ULONG GetPrinters( PPRQINFO3* pprq, ULONG *pcTot ) ;
83
84
QPrintDlgProc(HWND hwnd,ULONG usMsg,MPARAM mp1,MPARAM mp2)85 MRESULT EXPENTRY QPrintDlgProc ( HWND hwnd, ULONG usMsg, MPARAM mp1, MPARAM mp2 )
86 /*
87 ** Query print area and printer setup
88 */
89 {
90 static PQPRINT pqp = NULL ;
91 static SWP swp ;
92 static RECTL rectlDef ;
93 TRACKINFO ti ;
94 RECTL rectlBox ;
95 HDC hdc ;
96 ULONG ulStyle ;
97 char *psz ;
98
99 switch ( usMsg ) {
100
101 case WM_INITDLG :
102
103 pqp = (PQPRINT) PVOIDFROMMP( mp2 ) ;
104 if( pqp->caps & QP_CAPS_FILE ) {
105 ulStyle = WinQueryWindowULong( WinWindowFromID( hwnd, IDD_PRINTQNAME ),
106 QWL_STYLE ) ;
107 WinSetWindowULong( WinWindowFromID( hwnd, IDD_PRINTQNAME ),
108 QWL_STYLE, ulStyle ^ DT_HALFTONE ) ;
109 }
110 else {
111 WinSendMsg( WinWindowFromID( hwnd, IDD_QPRNAME ),
112 EM_SETREADONLY, (MPARAM)TRUE, 0L ) ;
113 }
114 WinQueryWindowRect( WinWindowFromID( hwnd, IDD_QPRBOX ),
115 &rectlDef ) ;
116
117 case WM_USER_SET_DATA :
118
119 psz = *pqp->piPrinter->pszComment ? pqp->piPrinter->pszComment :
120 pqp->piPrinter->pszName ;
121 WinSetDlgItemText( hwnd, IDD_PRINTNAME, psz ) ;
122 WinSetDlgItemFloatF( hwnd, IDD_QPRXSIZE, 1, pqp->xsize ) ;
123 WinSetDlgItemFloatF( hwnd, IDD_QPRYSIZE, 1, pqp->ysize ) ;
124 WinSetDlgItemFloat( hwnd, IDD_QPRXFRAC, pqp->xfrac ) ;
125 WinSetDlgItemFloat( hwnd, IDD_QPRYFRAC, pqp->yfrac ) ;
126 WinQueryWindowPos( WinWindowFromID( hwnd, IDD_QPRBOX ),
127 &swp ) ;
128 {
129 int y = rectlDef.yTop ;
130 rectlBox = rectlDef ;
131 if( pqp->xsize < pqp->ysize )
132 rectlBox.xRight = rectlDef.yTop * pqp->xsize/pqp->ysize ;
133 else {
134 int x = rectlDef.yTop * pqp->ysize/pqp->xsize ;
135 rectlBox.yTop = x ;
136 rectlBox.xRight = y ;
137 }
138 }
139 WinSetWindowPos( WinWindowFromID( hwnd, IDD_QPRBOX ),
140 NULLHANDLE, 0, 0, (short) rectlBox.xRight, (short)rectlBox.yTop, SWP_SIZE ) ;
141 {
142 double ratio = 1.560 ;
143 double xs = rectlBox.xRight - rectlBox.xLeft ;
144 double ys = rectlBox.yTop - rectlBox.yBottom ;
145 if( ys > xs/ratio ) { /* reduce ys to fit */
146 rectlBox.yTop = rectlBox.yBottom + (int)(xs/ratio) ;
147 }
148 else if( ys < xs/ratio ) { /* reduce xs to fit */
149 rectlBox.xRight = rectlBox.xLeft + (int)(ys*ratio) ;
150 }
151 }
152 rectlBox.xRight *= pqp->xfrac ;
153 rectlBox.yTop *= pqp->yfrac ;
154 WinSetWindowPos( WinWindowFromID( hwnd, IDD_QPRFRAME ),
155 NULLHANDLE,
156 swp.x, swp.y, //+(short)(swp.cy*(1.0-pqp->yfrac)),
157 (short)rectlBox.xRight, (short)rectlBox.yTop, SWP_SIZE|SWP_MOVE ) ;
158 break ;
159
160 case WM_COMMAND :
161
162 switch ( SHORT1FROMMP(mp1) ) {
163
164 case DID_OK:
165 WinQueryDlgItemFloat( hwnd, IDD_QPRXFRAC, &pqp->xfrac ) ;
166 WinQueryDlgItemFloat( hwnd, IDD_QPRYFRAC, &pqp->yfrac ) ;
167
168 if( pqp->caps & QP_CAPS_FILE ) {
169 WinQueryDlgItemText( hwnd, IDD_QPRNAME, 32, pqp->szFilename ) ;
170 }
171 break ;
172
173 case IDD_QPRSETPR: /* printer setup */
174 if( SetPrinterMode( hwnd, pqp ) == 1 ) {
175 if( (hdc = OpenPrinterDC( WinQueryAnchorBlock( hwnd ),
176 pqp,
177 OD_INFO,
178 NULL )) != DEV_ERROR ) {
179 DevQueryCaps( hdc, CAPS_TECHNOLOGY, (long)sizeof(prCaps)/sizeof(long), (PLONG)&prCaps ) ;
180 DevCloseDC( hdc ) ;
181 pqp->xsize = (float)100.0* (float) prCaps.lWidth / (float) prCaps.lHorRes ; // in cm
182 pqp->ysize = (float)100.0* (float) prCaps.lHeight / (float) prCaps.lVertRes ; // in cm
183 WinSendMsg( hwnd, WM_USER_SET_DATA, 0L, 0L ) ;
184 }
185 }
186 return 0L ;
187
188 case IDD_QPRTRACK : /* track plot area */
189 WinQueryWindowRect( WinWindowFromID( hwnd, IDD_QPRBOX ),
190 &rectlBox ) ;
191 {
192 double ratio = 1.560 ;
193 double xs = rectlBox.xRight - rectlBox.xLeft ;
194 double ys = rectlBox.yTop - rectlBox.yBottom ;
195 if( ys > xs/ratio ) { /* reduce ys to fit */
196 rectlBox.yTop = rectlBox.yBottom + (int)(xs/ratio) ;
197 }
198 else if( ys < xs/ratio ) { /* reduce xs to fit */
199 rectlBox.xRight = rectlBox.xLeft + (int)(ys*ratio) ;
200 }
201 }
202 ti.cxBorder = ti.cyBorder = 2 ;
203 ti.cxGrid = ti.cyGrid = 0 ;
204 ti.cxKeyboard = ti.cyKeyboard = 2 ;
205 ti.ptlMinTrackSize.x = ti.ptlMinTrackSize.y = 2 ;
206 ti.rclBoundary = rectlBox ;
207 ti.ptlMaxTrackSize.x = rectlBox.xRight ;
208 ti.ptlMaxTrackSize.y = rectlBox.yTop ;
209 ti.rclTrack.xRight = pqp->xfrac * rectlBox.xRight ;
210 ti.rclTrack.yTop = pqp->yfrac*rectlBox.yTop ;
211 ti.rclTrack.xLeft = 0 ;
212 ti.rclTrack.yBottom = 0 ;//(1.0-pqp->yfrac) * rectlBox.yTop ;
213 ti.fs = TF_RIGHT|TF_TOP|TF_STANDARD|TF_SETPOINTERPOS|TF_ALLINBOUNDARY ;
214 WinSetPointer( HWND_DESKTOP,
215 WinQuerySysPointer( HWND_DESKTOP, SPTR_SIZENWSE, FALSE ) ) ;
216 WinTrackRect( WinWindowFromID( hwnd, IDD_QPRBOX ),
217 NULLHANDLE,
218 &ti ) ;
219 pqp->xfrac = (float)ti.rclTrack.xRight / (float)rectlBox.xRight ;
220 pqp->yfrac = ((float)(ti.rclTrack.yTop-ti.rclTrack.yBottom) / (float)rectlBox.yTop) ;
221 rectlBox.yTop = ti.rclTrack.yTop - ti.rclTrack.yBottom ;
222 rectlBox.xRight = ti.rclTrack.xRight ;
223 {
224 double ratio = 1.560 ;
225 double xs = rectlBox.xRight - rectlBox.xLeft ;
226 double ys = rectlBox.yTop - rectlBox.yBottom ;
227 if( ys > xs/ratio ) { /* reduce ys to fit */
228 rectlBox.yTop = rectlBox.yBottom + (int)(xs/ratio) ;
229 }
230 else if( ys < xs/ratio ) { /* reduce xs to fit */
231 rectlBox.xRight = rectlBox.xLeft + (int)(ys*ratio) ;
232 }
233 }
234 WinSetWindowPos( WinWindowFromID( hwnd, IDD_QPRFRAME ),
235 NULLHANDLE,
236 swp.x, swp.y,//+(short)(swp.cy*(1.0-pqp->yfrac)),
237 (short)rectlBox.xRight, (short)rectlBox.yTop, SWP_SIZE|SWP_MOVE ) ;
238 WinSetDlgItemFloat( hwnd, IDD_QPRXFRAC, pqp->xfrac ) ;
239 WinSetDlgItemFloat( hwnd, IDD_QPRYFRAC, pqp->yfrac ) ;
240 return 0L ;
241
242 default:
243 break ;
244 }
245
246 default:
247 break ;
248 }
249 /* fall through to the default control processing */
250
251 return WinDefDlgProc ( hwnd , usMsg , mp1 , mp2 ) ;
252 }
253
QPrintersDlgProc(HWND hwnd,ULONG usMsg,MPARAM mp1,MPARAM mp2)254 MRESULT EXPENTRY QPrintersDlgProc ( HWND hwnd, ULONG usMsg, MPARAM mp1, MPARAM mp2 )
255 /*
256 ** Query printers and allow selection
257 */
258 {
259 static HWND hwndLB ;
260 static PPRQINFO3 pprq=NULL ;
261 static ULONG cPrinters ;
262 static USHORT usItem ;
263 static char *szPrinterName ;
264 int i, iSelect ;
265 char *psz ;
266
267 switch ( usMsg ) {
268
269 case WM_INITDLG :
270
271 szPrinterName = (char*) PVOIDFROMMP( mp2 ) ;
272 iSelect = 0 ;
273 GetPrinters( &pprq, &cPrinters ) ;
274 hwndLB = WinWindowFromID( hwnd, IDD_QPRSLIST ) ;
275 for( i=0; i<cPrinters;i++ ) {
276 psz = *pprq[i].pszComment ? pprq[i].pszComment :
277 pprq[i].pszName ;
278 WinSendMsg( hwndLB,
279 LM_INSERTITEM,
280 (MPARAM)LIT_END,
281 MPFROMP(psz) ) ;
282 if( strcmp( pprq[i].pszName, szPrinterName ) == 0 )
283 iSelect = i ;
284 }
285
286 WinSendMsg( hwndLB,
287 LM_SELECTITEM,
288 MPFROMSHORT( iSelect ),
289 (MPARAM)TRUE ) ;
290
291 break ;
292
293 case WM_COMMAND :
294
295 switch ( SHORT1FROMMP(mp1) ) {
296
297 case DID_OK:
298 strcpy( szPrinterName, pprq[usItem].pszName ) ;
299 case DID_CANCEL:
300 free( pprq ) ;
301 pprq = NULL;
302 break ;
303 }
304 break ;
305
306 case WM_CONTROL:
307
308 if( SHORT1FROMMP( mp1 ) == IDD_QPRSLIST ) {
309 if( SHORT2FROMMP( mp1 ) == LN_SELECT ) {
310 usItem = (ULONG)WinSendMsg( hwndLB,
311 LM_QUERYSELECTION,
312 0L,
313 0L ) ;
314 }
315 }
316
317 default:
318 break ;
319 }
320 /* fall through to the default control processing */
321 return WinDefDlgProc ( hwnd , usMsg , mp1 , mp2 ) ;
322 }
323
GetPrinters(PPRQINFO3 * pprq,ULONG * pcTot)324 ULONG GetPrinters( PPRQINFO3 *pprq, ULONG *pcTot )
325 /*
326 ** get a list of printers
327 */
328 {
329 ULONG rc ;
330 ULONG cQueues, cbData ;
331
332 rc = SplEnumQueue( NULL, 3, NULL, 0, &cQueues, pcTot, &cbData, NULL ) ;
333
334 if( *pprq != NULL ) {
335 free( *pprq ) ;
336 *pprq = NULL ;
337 }
338 if( *pcTot == 0 ) { /* no printers */
339 return 0 ;
340 }
341 *pprq = malloc( cbData ) ;
342 rc = SplEnumQueue( NULL,
343 3,
344 *pprq,
345 cbData,
346 &cQueues,
347 pcTot,
348 &cbData,
349 NULL ) ;
350 return *pcTot ;
351 }
352
PauseMsgDlgProc(HWND hwnd,ULONG usMsg,MPARAM mp1,MPARAM mp2)353 MRESULT EXPENTRY PauseMsgDlgProc ( HWND hwnd, ULONG usMsg, MPARAM mp1, MPARAM mp2 )
354 /*
355 ** Pause message dialog box proc
356 */
357 {
358 static PPAUSEDATA ppdata = NULL ;
359 char *pszText ;
360 switch ( usMsg ) {
361
362 case WM_INITDLG :
363 /* set the position so user can move out the way, and
364 have it come back there next time */
365 ppdata = (PPAUSEDATA) PVOIDFROMMP( mp2 ) ;
366 if( ppdata->pswp != NULL )
367 WinSetWindowPos( hwnd, HWND_TOP, ppdata->pswp->x, ppdata->pswp->y,
368 0, 0, SWP_MOVE ) ;
369 pszText = ppdata->pszMessage ;
370 while(*pszText==' ') ++pszText ;
371 WinSetDlgItemText( hwnd, IDD_PAUSETEXT, pszText ) ;
372 break ;
373
374 case WM_COMMAND :
375 switch ( SHORT1FROMMP(mp1) ) {
376 case DID_OK:
377 case DID_CANCEL:
378 WinPostMsg( WinQueryWindow( hwnd, QW_OWNER ),
379 WM_PAUSEEND,
380 SHORT1FROMMP(mp1)==DID_OK?(MPARAM)1L:0L,
381 0L ) ;
382 if( ppdata->pswp == NULL ) ppdata->pswp = (PSWP)malloc( sizeof(SWP) ) ;
383 WinQueryWindowPos( hwnd, ppdata->pswp ) ;
384 WinDismissDlg( hwnd, 0 ) ;
385 break ;
386 case IDM_PRINT:
387 WinPostMsg( WinQueryWindow( hwnd, QW_OWNER ),
388 WM_COMMAND,
389 MPFROMSHORT(IDM_PRINT),
390 0L ) ;
391 return 0 ;
392 default:
393 break ;
394 }
395 default:
396 break ;
397 }
398 /* fall through to the default control processing */
399 return WinDefDlgProc ( hwnd , usMsg , mp1 , mp2 ) ;
400 }
401
WinSetDlgItemFloatF(HWND hwnd,USHORT usID,int nDec,float flValue)402 void WinSetDlgItemFloatF( HWND hwnd, USHORT usID, int nDec, float flValue )
403 /*
404 */
405 {
406 char achBuffer [ 34 ]; // default string field size ...
407 char fmt[32] ;
408
409 sprintf( fmt, "%%12.%df", nDec ) ;
410 sprintf( achBuffer, fmt, flValue ) ;
411 WinSetDlgItemText( hwnd, usID, achBuffer ) ;
412 }
413
WinSetDlgItemFloat(HWND hwnd,USHORT usID,float flValue)414 void WinSetDlgItemFloat( HWND hwnd, USHORT usID, float flValue )
415 /*
416 */
417 {
418 char achBuffer [ 34 ] ; // default string field size ...
419 char fmt[10] ;
420
421 sprintf( fmt, "%%12.%df", 4 ) ;
422 sprintf( achBuffer, fmt, flValue ) ;
423 WinSetDlgItemText( hwnd, usID, achBuffer ) ;
424 WinSetDlgItemText( hwnd, usID, achBuffer ) ;
425 }
426
WinQueryDlgItemFloat(HWND hwnd,USHORT usID,float * pflValue)427 void WinQueryDlgItemFloat( HWND hwnd, USHORT usID, float *pflValue )
428 /*
429 */
430 {
431 char achBuffer [ 34 ] ; // default string field size ...
432
433 WinQueryDlgItemText( hwnd, usID, 34, achBuffer ) ;
434 *pflValue = (float) atof( achBuffer ) ;
435 }
436
SendCommandDlgProc(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)437 MRESULT EXPENTRY SendCommandDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
438 /*
439 ** Dialog for sending a command to gnuplot
440 ** //PM Petr Mikulik, 17. 8. 1999
441 */
442 {
443 #define TEXT_LENGTH 80
444 static char buf[TEXT_LENGTH+1];
445 extern void gp_execute (char *s);
446
447 switch (msg)
448 {
449 case WM_INITDLG:
450 WinSendDlgItemMsg( hwnd, IDM_DO_SENDCOMMAND, EM_SETTEXTLIMIT,
451 MPFROMSHORT (TEXT_LENGTH), 0L );
452 WinSetDlgItemText( hwnd, IDM_DO_SENDCOMMAND, buf );
453 WinSendDlgItemMsg( hwnd, IDM_DO_SENDCOMMAND, EM_SETSEL,
454 MPFROM2SHORT (0, strlen(buf)), 0L );
455 return (MRESULT)FALSE;
456
457 case WM_COMMAND:
458 switch (COMMANDMSG (&msg)->cmd)
459 {
460 case DID_OK:
461 WinQueryDlgItemText( hwnd, IDM_DO_SENDCOMMAND, sizeof(buf), buf );
462 WinDismissDlg( hwnd, TRUE );
463 gp_execute(buf);
464 break;
465 default:
466 WinDismissDlg( hwnd, FALSE );
467 break;
468 }
469 return 0;
470 }
471 return ( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
472 }
473