home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CLIP.ZIP / CLIP.C < prev    next >
C/C++ Source or Header  |  1989-12-11  |  8KB  |  240 lines

  1. #define INCL_WIN
  2. #define INCL_GPI
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "clip.h"
  7.  
  8. /*
  9.     This is a pretty primitive Clipboard Viewer utility for OS/2.
  10.     Currently it supports only Text and Bitmaps. Could use some
  11.     optimization and features. But, what the hell, it works! (I think!)
  12.  
  13.       Program Name.....: Clipview
  14.       Version..........: 1.0
  15.       Written By.......: Jay F. McLain (CompuServe 76701,157)
  16.       Date.............: May 30, 1989 (Clean-up December 10, 1989)
  17.       Operating System.: OS/2 PM Version 1.10
  18. */
  19.  
  20. /*
  21.     UPDATES\ENHANCEMENTS:
  22. */
  23.  
  24.  
  25. HAB      hAB;            /* Handle to Application Anchor Block         */
  26. HDC      hDC,            /* Handle to Application Device Context       */
  27.          hMemDC;         /* Handle to Bitmap Memory Device Context     */
  28. HPS      hMemPS;         /* Handle to Memory Presentation Space        */
  29. HWND     hwndFrame,      /* Handle to Application Frame                */
  30.          hwndClient,     /* Handle to Application Client Area          */
  31.          hwndSysMenu,    /* Handle to Application System Menu          */
  32.          hwndSysSubMenu; /* Handle to Application Sub Menu             */
  33. MENUITEM miSysMenu;      /* Menu Item Styles                           */
  34. SHORT    sItem,          /* Loop Variable for Initializing System Menu */
  35.          idSysMenu;      /* ID for System Menu                         */
  36.  
  37. PSZ      pszData[4]      = { "Display", NULL, NULL, NULL };
  38. SIZEL    sizlPage;
  39. POINTL   aptl[6];
  40.  
  41. CHAR     szClientClass[] = "Clipview";
  42.  
  43. CHAR     *szMenuText[2]  = {NULL,
  44.                             "A~bout Clipview..." };
  45.  
  46. MENUITEM mi[2] = { MIT_END, MIS_SEPARATOR, 0,         0, NULL, NULL,
  47.                    MIT_END, MIS_TEXT,      0, IDM_ABOUT, NULL, NULL };
  48.  
  49.  
  50. /****************************** Clipboard MAIN() ******************************/
  51.  
  52. INT main(VOID) {
  53.   static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU |
  54.                               FCF_SIZEBORDER    | FCF_MINMAX  |
  55.                               FCF_SHELLPOSITION | FCF_ICON;
  56.  
  57.   HMQ  hMQ;  /* Handle to Application Message Queue */
  58.   QMSG qmsg; /* A Queue Message                     */
  59.  
  60.   hAB = WinInitialize( NULL );
  61.   hMQ = WinCreateMsgQueue( hAB, 0 );
  62.  
  63.   WinRegisterClass( hAB, szClientClass, ClipboardWndProc, CS_SIZEREDRAW, 0 );
  64.  
  65.   hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
  66.                                   WS_VISIBLE,
  67.                                   &flFrameFlags,
  68.                                   szClientClass,
  69.                                   APP_TITLE,
  70.                                   0L,
  71.                                   (HMODULE) NULL,
  72.                                   ID_CLIPBOARD,
  73.                                   &hwndClient );
  74.  
  75.   while ( WinGetMsg( hAB, &qmsg, NULL, 0, 0 ) )
  76.     WinDispatchMsg( hAB, &qmsg );
  77.  
  78.   WinDestroyWindow( hwndFrame );
  79.   WinDestroyMsgQueue( hMQ );
  80.   WinTerminate( hAB );
  81.   return 0;
  82. }
  83.  
  84.  
  85. /************************* Clipboard Window Procedure *************************/
  86.  
  87. MRESULT EXPENTRY ClipboardWndProc(HWND hWnd, USHORT msg, MPARAM mp1, MPARAM mp2) {
  88.   switch ( msg ) {
  89.     case WM_CREATE:
  90.       hDC = WinOpenWindowDC(hWnd);
  91.  
  92.       hMemDC = DevOpenDC( hAB,
  93.                           OD_MEMORY,
  94.                           "*",
  95.                           4L,
  96.                           (PDEVOPENDATA) pszData,
  97.                           hDC );
  98.  
  99.       hMemPS = GpiCreatePS( hAB,
  100.                             hMemDC,
  101.                             &sizlPage,
  102.                             PU_PELS | GPIA_ASSOC | GPIT_MICRO );
  103.  
  104.       hwndSysMenu = WinWindowFromID( WinQueryWindow( hWnd, QW_PARENT, FALSE ),
  105.                                      FID_SYSMENU );
  106.  
  107.       idSysMenu   = SHORT1FROMMR( WinSendMsg( hwndSysMenu,
  108.                                               MM_ITEMIDFROMPOSITION,
  109.                                               NULL,
  110.                                               NULL ) );
  111.  
  112.       WinSendMsg( hwndSysMenu,
  113.                   MM_QUERYITEM,
  114.                   MPFROM2SHORT( idSysMenu,FALSE ),
  115.                   MPFROMP( &miSysMenu ) );
  116.  
  117.       hwndSysSubMenu = miSysMenu.hwndSubMenu;
  118.  
  119.       for (sItem = 0; sItem < 2; sItem++)
  120.         WinSendMsg( hwndSysSubMenu,
  121.                     MM_INSERTITEM,
  122.                     MPFROMP( mi + sItem ),
  123.                     MPFROMP( szMenuText[sItem] ) );
  124.  
  125.       WinSetClipbrdViewer( hAB, hWnd );
  126.       return 0;
  127.  
  128.     case WM_COMMAND:
  129.       switch ( COMMANDMSG( &msg )->cmd ) {
  130.         case IDM_ABOUT:
  131.           WinDlgBox( HWND_DESKTOP,
  132.                      hWnd,
  133.                      AboutDlgProc,
  134.                      NULL,
  135.                      IDD_ABOUT,
  136.                      NULL );
  137.           return 0;
  138.       }
  139.       break;
  140.  
  141.     case WM_PAINT:
  142.     {
  143.       HPS     hPS;         /* Handle to WM_PAINT Presentation Space */
  144.       USHORT  usLen,       /* String Length                         */
  145.               usfInfo;     /* Clipboard Format Information          */
  146.       HBITMAP hClipBitmap; /* Handle to Clipboard Bitmap            */
  147.       SEL     selClipText; /* Memory Selector for Clipboard Text    */
  148.       PCHAR   pchClipText; /* Pointer to Clipboard Text             */
  149.       POINTL  ptlStart;    /* String Display Location               */
  150.       SWP     swp;         /* Application Client Area Location\Size */
  151.       RECTL   rectl;       /* WM_PAINT Invalid Rectangle            */
  152.  
  153.  
  154.       hPS = WinBeginPaint( hWnd, NULL, &rectl );
  155.  
  156.       WinFillRect( hPS, &rectl, CLR_BLACK );
  157.  
  158.       /* Is Information in Clipboard Text? */
  159.       if ( WinQueryClipbrdFmtInfo( hAB, CF_TEXT, &usfInfo ) ) {
  160.         WinSetWindowText( hwndFrame, "Clipboard -- Text" );
  161.         WinOpenClipbrd( hAB );
  162.  
  163.         if ( (selClipText =
  164.                 (SEL) WinQueryClipbrdData( hAB, CF_TEXT ) ) != NULL ) {
  165.           pchClipText = MAKEP( selClipText, 0 );
  166.  
  167.           ptlStart.x = 10;
  168.           ptlStart.y = 10;
  169.  
  170.           /* Find Length of String in Clipboard */
  171.           for (usLen = 0; pchClipText[usLen]; usLen++);
  172.  
  173.           GpiCharStringAt( hPS, &ptlStart, (LONG) usLen, pchClipText );
  174.         }
  175.  
  176.         WinCloseClipbrd( hAB );
  177.       }
  178.       else {
  179.  
  180.       /* Is Information in Clipboard Bitmap? */
  181.         if ( WinQueryClipbrdFmtInfo( hAB, CF_BITMAP, &usfInfo ) ) {
  182.           WinSetWindowText( hwndFrame, "Clipboard -- Bitmap");
  183.           WinOpenClipbrd( hAB );
  184.  
  185.           if ( ( hClipBitmap =
  186.                    (HBITMAP) WinQueryClipbrdData( hAB, CF_BITMAP ) ) != NULL ) {
  187.             GpiSetBitmap( hMemPS, hClipBitmap );
  188.             WinQueryWindowPos( hWnd, &swp );
  189.  
  190.             aptl[0].x = (LONG) swp.cx;
  191.             aptl[0].y = (LONG) swp.cy;
  192.  
  193.             aptl[1].x = (LONG) 0;
  194.             aptl[1].y = (LONG) 0;
  195.  
  196.             aptl[2].x = (LONG) 0;
  197.             aptl[2].y = (LONG) 0;
  198.  
  199.             GpiBitBlt( hPS, hMemPS, 3L, aptl, ROP_SRCCOPY, BBO_IGNORE );
  200.           }
  201.  
  202.           WinCloseClipbrd( hAB );
  203.         }
  204.       }
  205.  
  206.       WinEndPaint( hPS );
  207.     }
  208.     return 0;
  209.  
  210.     case WM_DRAWCLIPBOARD:                   /* If New Data in Clipboard      */
  211.       WinInvalidateRect( hWnd, NULL, NULL ); /* Invalidate Client Area.       */
  212.       return 0;                              /* (Send WM_PAINT Message)       */
  213.  
  214.     case WM_DESTROY:                      /* Application is Terminating.      */
  215.       GpiDestroyPS( hMemPS );             /* So, Destroy Presentation Space   */
  216.       DevCloseDC( hMemDC );               /* and, Close Memory Device Context */
  217.       return 0;
  218.   }
  219.  
  220.   return WinDefWindowProc( hWnd, msg, mp1, mp2 );
  221. }
  222.  
  223.  
  224. /************************* AboutBox Dialog Procedure *************************/
  225.  
  226. MRESULT EXPENTRY AboutDlgProc(HWND hWnd, USHORT msg, MPARAM mp1, MPARAM mp2) {
  227.   switch ( msg ) {
  228.     case WM_COMMAND:
  229.       switch ( COMMANDMSG( &msg )->cmd ) {
  230.         case DID_OK:
  231.           WinDismissDlg( hWnd, TRUE );
  232.           return 0;
  233.       }
  234.  
  235.       break;
  236.   }
  237.  
  238.   return WinDefDlgProc( hWnd, msg, mp1, mp2 );
  239. }
  240.