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