home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / drgmon.zip / debugwin.c next >
C/C++ Source or Header  |  1993-07-28  |  17KB  |  425 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  debugwin.c             AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  07-20-93                                           *
  5.  *                                                                   *
  6.  * MODULE DESCRIPTION:                                               *
  7.  *                                                                   *
  8.  *  Part of the 'DRGDROP' drag/drop sample program.                  *
  9.  *                                                                   *
  10.  *  This module creates listbox windows that are used to display     *
  11.  *  Drag/Drop messages and structure contents.                       *
  12.  *                                                                   *
  13.  * NOTES:                                                            *
  14.  *                                                                   *
  15.  * FUNCTIONS AVALABLE TO OTHER MODULES:                              *
  16.  *                                                                   *
  17.  *   dbgCreateWin                                                    *
  18.  *   dbgInsert                                                       *
  19.  *                                                                   *
  20.  *                                                                   *
  21.  * HISTORY:                                                          *
  22.  *                                                                   *
  23.  *  07-20-93 - Program coded.                                        *
  24.  *                                                                   *
  25.  *  Rick Fishman                                                     *
  26.  *  Code Blazers, Inc.                                               *
  27.  *  4113 Apricot                                                     *
  28.  *  Irvine, CA. 92720                                                *
  29.  *  CIS ID: 72251,750                                                *
  30.  *                                                                   *
  31.  *********************************************************************/
  32.  
  33. #pragma strings(readonly)   // used for debug version of memory mgmt routines
  34.  
  35. /*********************************************************************/
  36. /*------- Include relevant sections of the OS/2 header files --------*/
  37. /*********************************************************************/
  38.  
  39. #define  INCL_DEV
  40. #define  INCL_SPL
  41. #define  INCL_SPLDOSPRINT
  42. #define  INCL_WINDIALOGS
  43. #define  INCL_WINERRORS
  44. #define  INCL_WINFRAMEMGR
  45. #define  INCL_WININPUT
  46. #define  INCL_WINLISTBOXES
  47. #define  INCL_WINSHELLDATA
  48. #define  INCL_WINSTDCNR
  49. #define  INCL_WINSTDDRAG
  50. #define  INCL_WINSTDFILE
  51. #define  INCL_WINWINDOWMGR
  52. #define  INCL_WINSYS
  53.  
  54. /**********************************************************************/
  55. /*----------------------------- INCLUDES -----------------------------*/
  56. /**********************************************************************/
  57.  
  58. #include <os2.h>
  59. #include <stdarg.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include "drgdrop.h"
  64.  
  65. /*********************************************************************/
  66. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  67. /*********************************************************************/
  68.  
  69. #define MESSAGE_SIZE    1024
  70.  
  71. #define PRINT_DOCNAME   "Drag/Drop output"
  72.  
  73. /**********************************************************************/
  74. /*---------------------------- STRUCTURES ----------------------------*/
  75. /**********************************************************************/
  76.  
  77.  
  78. /**********************************************************************/
  79. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  80. /**********************************************************************/
  81.  
  82. void Save( HWND hwndFrame );
  83. void Print( HWND hwndFrame );
  84.  
  85. FNWP wpDbgFrame;
  86.  
  87. /**********************************************************************/
  88. /*------------------------ GLOBAL VARIABLES --------------------------*/
  89. /**********************************************************************/
  90.  
  91. PFNWP pfnwpFrame;
  92.  
  93. char szSaveFileName[ CCHMAXPATH ];
  94.  
  95. /**********************************************************************/
  96. /*--------------------------- dbgCreateWin ---------------------------*/
  97. /*                                                                    */
  98. /*  CREATE THE DEBUG LISTBOX WINDOW                                   */
  99. /*                                                                    */
  100. /*  PARMS: title for the debug window                                 */
  101. /*                                                                    */
  102. /*  NOTES:                                                            */
  103. /*                                                                    */
  104. /*  RETURNS: HWND of debug window or NULL if unsuccessful             */
  105. /*                                                                    */
  106. /*--------------------------------------------------------------------*/
  107. /**********************************************************************/
  108. HWND dbgCreateWin( PSZ szTitle )
  109. {
  110.     FRAMECDATA  fcdata;
  111.     HWND        hwndFrame, hwndLB;
  112.  
  113.     (void) memset( &fcdata, 0, sizeof( FRAMECDATA ) );
  114.  
  115.     fcdata.cb            = sizeof( FRAMECDATA );
  116.     fcdata.flCreateFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER |
  117.                            FCF_MINMAX | FCF_MENU;
  118.     fcdata.idResources   = ID_DEBUGWIN_RESOURCES;
  119.  
  120.     hwndFrame = WinCreateWindow( HWND_DESKTOP, WC_FRAME, NULL,
  121.                                  FS_NOBYTEALIGN | WS_CLIPCHILDREN,
  122.                                  0, 0, 0, 0, NULLHANDLE,
  123.                                  HWND_TOP, 1, &fcdata, NULL );
  124.     if( hwndFrame )
  125.     {
  126.         hwndLB = WinCreateWindow( hwndFrame, WC_LISTBOX, NULL,
  127.                                   WS_VISIBLE | LS_HORZSCROLL | LS_NOADJUSTPOS,
  128.                                   0, 0, 0, 0, hwndFrame, HWND_TOP, FID_CLIENT,
  129.                                   NULL, NULL );
  130.         if( hwndLB )
  131.         {
  132.             pfnwpFrame = WinSubclassWindow( hwndFrame, wpDbgFrame );
  133.  
  134.             if( pfnwpFrame )
  135.             {
  136.                 WinSetPresParam( hwndLB, PP_FONTNAMESIZE,
  137.                                  strlen( DEBUG_WINDOW_FONT ) + 1,
  138.                                  DEBUG_WINDOW_FONT );
  139.                 WinSendMsg( hwndFrame, WM_UPDATEFRAME, NULL, NULL );
  140.                 WinSetWindowText( hwndFrame, szTitle );
  141.             }
  142.             else
  143.             {
  144.                 Msg( "Subclass of debug window failed. RC: %X",HWNDERR(hwndLB));
  145.                 WinDestroyWindow( hwndFrame );
  146.                 hwndFrame = NULLHANDLE;
  147.             }
  148.         }
  149.         else
  150.         {
  151.             Msg( "Listbox debug window creation failed. RC: %X",
  152.                  HWNDERR( hwndFrame ) );
  153.             WinDestroyWindow( hwndFrame );
  154.             hwndFrame = NULLHANDLE;
  155.         }
  156.     }
  157.     else
  158.         Msg( "Listbox debug window frame creation failed. RC: %X", HABERR(0) );
  159.  
  160.     return hwndFrame;
  161. }
  162.  
  163. /**********************************************************************/
  164. /*---------------------------- dbgInsert -----------------------------*/
  165. /*                                                                    */
  166. /*  INSERT A DEBUG MESSAGE INTO THE DEBUG LISTBOX                     */
  167. /*                                                                    */
  168. /*  PARMS: debug window handle,                                       */
  169. /*         a message in printf format with its parms                  */
  170. /*                                                                    */
  171. /*  NOTES:                                                            */
  172. /*                                                                    */
  173. /*  RETURNS: index of inserted item                                   */
  174. /*                                                                    */
  175. /*--------------------------------------------------------------------*/
  176. /**********************************************************************/
  177. SHORT dbgInsert( HWND hwndDbg, PSZ szFormat,... )
  178. {
  179.     SHORT   sLastItem;
  180.     PSZ     szMsg;
  181.     va_list argptr;
  182.  
  183.     if( (szMsg = (PSZ) malloc( MESSAGE_SIZE )) == NULL )
  184.     {
  185.         DosBeep( 1000, 1000 );
  186.         return FALSE;
  187.     }
  188.  
  189.     va_start( argptr, szFormat );
  190.     vsprintf( szMsg, szFormat, argptr );
  191.     va_end( argptr );
  192.  
  193.     szMsg[ MESSAGE_SIZE - 1 ] = 0;
  194.  
  195.     sLastItem = (SHORT) WinSendMsg( WinWindowFromID( hwndDbg, FID_CLIENT ),
  196.                                     LM_INSERTITEM, MPFROMSHORT( LIT_END ),
  197.                                     MPFROMP( szMsg ) );
  198.  
  199.     if( szMsg )
  200.         free( szMsg );
  201.  
  202.     if( dlgInfo.fScrollToBottom )
  203.         WinSendDlgItemMsg( hwndDbg, FID_CLIENT, LM_SETTOPINDEX,
  204.                            MPFROMSHORT( sLastItem ), NULL );
  205.  
  206.     return sLastItem;
  207. }
  208.  
  209. /**********************************************************************/
  210. /*--------------------------- wpDbgFrame -----------------------------*/
  211. /*                                                                    */
  212. /*  SUBCLASSED FRAME WINDOW PROCEDURE.                                */
  213. /*                                                                    */
  214. /*  PARMS: standard window procedure parameters                       */
  215. /*                                                                    */
  216. /*  NOTES:                                                            */
  217. /*                                                                    */
  218. /*  RETURNS: nothing                                                  */
  219. /*                                                                    */
  220. /*--------------------------------------------------------------------*/
  221. /**********************************************************************/
  222. MRESULT EXPENTRY wpDbgFrame( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  223. {
  224.     switch( msg )
  225.     {
  226.         case WM_SYSCOMMAND:
  227.             if( SHORT1FROMMP( mp1 ) == SC_CLOSE )
  228.             {
  229.                 WinDestroyWindow( hwnd );
  230.                 return 0;
  231.             }
  232.  
  233.             break;
  234.  
  235.         case WM_COMMAND:
  236.             switch( SHORT1FROMMP( mp1 ) )
  237.             {
  238.                 case IDM_DEBUGWIN_SAVE:
  239.                     Save( hwnd );
  240.                     return 0;
  241.  
  242.                 case IDM_DEBUGWIN_PRINT:
  243.                     Print( hwnd );
  244.                     return 0;
  245.  
  246.                 case IDM_DEBUGWIN_CLEAR:
  247.                     WinSendDlgItemMsg( hwnd, FID_CLIENT, LM_DELETEALL, NULL,
  248.                                        NULL );
  249.                     return 0;
  250.             }
  251.  
  252.             break;
  253.     }
  254.  
  255.     return pfnwpFrame( hwnd, msg, mp1, mp2 );
  256. }
  257.  
  258. /**********************************************************************/
  259. /*------------------------------ Save --------------------------------*/
  260. /*                                                                    */
  261. /*  SAVE THE LISTBOX CONTENTS TO A FILE.                              */
  262. /*                                                                    */
  263. /*  PARMS: Debug window's frame window handle                         */
  264. /*                                                                    */
  265. /*  NOTES: This one function requires *lots* of stack space!          */
  266. /*                                                                    */
  267. /*  RETURNS: nothing                                                  */
  268. /*                                                                    */
  269. /*--------------------------------------------------------------------*/
  270. /**********************************************************************/
  271. void Save( HWND hwndFrame )
  272. {
  273.     FILEDLG fd;
  274.  
  275.     (void) memset( &fd, 0, sizeof( FILEDLG ) );
  276.     fd.cbSize   = sizeof( FILEDLG );
  277.     fd.fl       = FDS_CENTER | FDS_SAVEAS_DIALOG;
  278.     fd.pszTitle = "Please select a FileName";
  279.     strcpy( fd.szFullFile, szSaveFileName );
  280.  
  281.     WinFileDlg( HWND_DESKTOP, hwndFrame, &fd );
  282.  
  283.     if( *(fd.szFullFile) && fd.lReturn == DID_OK )
  284.     {
  285.         HWND  hwndLB = WinWindowFromID( hwndFrame, FID_CLIENT );
  286.         SHORT cItems;
  287.         int   i;
  288.         char  szItem[ MANUAL_RMF_LEN + 2 ];   // This would be the longest item
  289.         FILE  *fh = fopen( fd.szFullFile, "w" );
  290.  
  291.         if( fh )
  292.         {
  293.             // Store it for the next time the user wants to save
  294.  
  295.             strcpy( szSaveFileName, fd.szFullFile );
  296.  
  297.             cItems = (SHORT) WinSendMsg( hwndLB, LM_QUERYITEMCOUNT, NULL, NULL);
  298.             for( i = 0; i < cItems; i++ )
  299.             {
  300.                 if( WinSendMsg( hwndLB, LM_QUERYITEMTEXT,
  301.                                 MPFROM2SHORT( i, sizeof szItem ),
  302.                                 MPFROMP( szItem ) ) )
  303.                 {
  304.                     strcat( szItem, "\n" );
  305.                     if( fputs( szItem, fh ) == EOF )
  306.                     {
  307.                         Msg( "Out of disk space!" );
  308.                         break;
  309.                     }
  310.                 }
  311.             }
  312.  
  313.             fclose( fh );
  314.         }
  315.     }
  316. }
  317.  
  318. /**********************************************************************/
  319. /*------------------------------ Print -------------------------------*/
  320. /*                                                                    */
  321. /*  PRINT THE LISTBOX CONTENTS TO THE DEFAULT PRINTER.                */
  322. /*                                                                    */
  323. /*  PARMS: Debug window's frame window handle                         */
  324. /*                                                                    */
  325. /*  NOTES:                                                            */
  326. /*                                                                    */
  327. /*  RETURNS: nothing                                                  */
  328. /*                                                                    */
  329. /*--------------------------------------------------------------------*/
  330. /**********************************************************************/
  331. void Print( HWND hwndFrame )
  332. {
  333.     DEVOPENSTRUC dop = { "LPT1Q", "IBM4201", 0, "PM_Q_RAW", 0, 0, 0, 0, 0 };
  334.     PPRQINFO3    pprq3 = NULL;
  335.     char         szBuf[ 256 ];
  336.     char         szItem[ MANUAL_RMF_LEN + 2 ]; // This would be the longest item
  337.     USHORT       usSize = (SHORT) PrfQueryProfileString( HINI_PROFILE,
  338.                                         SPL_INI_SPOOLER, "QUEUE", NULL, szBuf,
  339.                                         (LONG) sizeof( szBuf ) );
  340.     if( usSize )
  341.     {
  342.         HWND   hwndLB = WinWindowFromID( hwndFrame, FID_CLIENT );
  343.         ULONG  cbNeeded;
  344.         APIRET rc;
  345.         PCH    pchDot;
  346.         HSPL   hspl;
  347.  
  348.         szBuf[ usSize - 2 ] = 0;    // get rid of terminating semicolon
  349.  
  350.         dop.pszLogAddress = szBuf;  // default queue
  351.  
  352.         SplQueryQueue( NULL, szBuf, 3, NULL, 0, &cbNeeded );
  353.  
  354.         pprq3 = (PPRQINFO3) malloc( cbNeeded );
  355.         if( !pprq3 )
  356.         {
  357.             Msg( "Out of memory" );
  358.             return;
  359.         }
  360.  
  361.         rc = SplQueryQueue( NULL, szBuf, 3, pprq3, cbNeeded, &cbNeeded );
  362.  
  363.         if( rc )
  364.         {
  365.             Msg( "SplQueryQueue RC: %u", rc );
  366.             return;
  367.         }
  368.  
  369.         pchDot = strchr( pprq3->pszDriverName, '.' );
  370.  
  371.         if( pchDot )
  372.             *pchDot = 0;
  373.  
  374.         dop.pszDriverName = pprq3->pszDriverName;
  375.         dop.pdriv = pprq3->pDriverData;
  376.  
  377.         // Open the Print Manager
  378.  
  379.         if( !(hspl = SplQmOpen( "*", 4L, (PQMOPENDATA) &dop )) )
  380.             Msg( "SplQmOpen failed\n" );
  381.  
  382.         // Start the spool file and name it
  383.  
  384.         if( hspl && !(SplQmStartDoc( hspl, PRINT_DOCNAME )) )
  385.             Msg( "SplQmStartDoc failed\n" );
  386.  
  387.         // Write to the spool file
  388.  
  389.         if( hspl )
  390.         {
  391.             int i;
  392.             int cItems = (int) WinSendMsg( hwndLB,LM_QUERYITEMCOUNT,NULL,NULL );
  393.             for( i = 0; i < cItems; i++ )
  394.             {
  395.                 if( WinSendMsg( hwndLB, LM_QUERYITEMTEXT,
  396.                                 MPFROM2SHORT( i, sizeof szItem ),
  397.                                 MPFROMP( szItem ) ) )
  398.                 {
  399.                     strcat( szItem, "\n\r" );
  400.                     if( !SplQmWrite( hspl, strlen( szItem ), szItem ) )
  401.                         Msg( "SplQmWrite failed\n" );
  402.                 }
  403.             }
  404.  
  405.         }
  406.  
  407.         // End the spool file. This starts it printing.
  408.  
  409.         if( hspl && !SplQmEndDoc( hspl ) )
  410.             Msg( "SplQmEndDoc failed\n" );
  411.  
  412.         // Close the Print Manager
  413.  
  414.         if( hspl && !SplQmClose( hspl ) )
  415.             Msg( "SplQmClose failed\n" );
  416.     }
  417.     else
  418.         Msg( "PrfQueryProfileString for default queue failed\n" );
  419.  
  420. }
  421.  
  422. /*************************************************************************
  423.  *                     E N D     O F     S O U R C E                     *
  424.  *************************************************************************/
  425.