home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / shapw102.zip / romy102.zip / romy.c < prev    next >
C/C++ Source or Header  |  1998-11-13  |  14KB  |  587 lines

  1. /*
  2.  * romy.c - Mr Romy watchs you
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10. #define INCL_PM
  11. #include <os2.h>
  12.  
  13. #define DEBUG
  14.  
  15. #include "shapewin.h"
  16. #include "romy.h"
  17. #include "romyres.h"
  18. #include "romybmp.h"
  19.  
  20. /*
  21.  * myname - adjust and save program names
  22.  */
  23.  
  24. UCHAR   ProgramPath[256] ;
  25. UCHAR   ProgramName[256] ;
  26.  
  27. static  void    myname(PSZ me)
  28. {
  29.     PUCHAR  p, last ;
  30.  
  31.     /*
  32.      * full pathname of program
  33.      */
  34.  
  35.     for (p = me, last = NULL ; *p ; p++) {
  36.         if (*p == '/' || *p == '\\') {
  37.             last = p ;
  38.         }
  39.     }
  40.     if (last != NULL) {
  41.         strcpy(ProgramPath, me) ;
  42.     } else if (DosSearchPath(7, "PATH", me, ProgramPath, 256) != 0) {
  43.         strcpy(ProgramPath, me) ;
  44.     }
  45.  
  46.     /*
  47.      * basename of program
  48.      */
  49.  
  50.     for (p = ProgramPath, last = NULL ; *p ; p++) {
  51.         if (*p == '/' || *p == '\\') {
  52.             last = p ;
  53.         }
  54.     }
  55.     if (last == NULL) {
  56.         strcpy(ProgramName, ProgramPath) ;
  57.     } else {
  58.         strcpy(ProgramName, &last[1]) ;
  59.     }
  60.     if ((p = strrchr(ProgramName, '.')) != NULL) {
  61.         *p = '\0' ;
  62.     }
  63. }
  64.  
  65. /*
  66.  * Error Notify
  67.  */
  68.  
  69. void    trMessage(PSZ msg)
  70. {
  71.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, msg, ProgramName, 0, MB_OK) ;
  72. }
  73.  
  74. /*
  75.  * initPos - initial Positions from argument (or default)
  76.  */
  77.  
  78. static  void    initPos(int ac, char *av[], PSWP swp)
  79. {
  80.     POINTL  ptCur  ;
  81.     int     i      ;
  82.  
  83.     TRACE("InitPos\n") ;
  84.     
  85.     /*
  86.      * default position from Pointer
  87.      */
  88.  
  89.     WinQueryPointerPos(HWND_DESKTOP, &ptCur) ;
  90.     
  91.     swp->x = ptCur.x ;
  92.     swp->y = ptCur.y ;
  93.  
  94.     /*
  95.      * change them if option specified
  96.      */
  97.  
  98.     for (i = 1 ; i < ac ; i++) {
  99.         if (av[i][0] != '-') {
  100.         continue ;
  101.     }
  102.     switch (av[i][1]) {
  103.     case 'x' :
  104.     case 'X' :
  105.         if (av[i][2] != '\0') {
  106.             swp->x = atoi(&av[i][2]) ;
  107.         } else if ((i + 1) < ac) {
  108.             swp->x = atoi(av[i+=1]) ;
  109.         }
  110.         break ;
  111.     case 'y' :
  112.     case 'Y' :
  113.         if (av[i][2] != '\0') {
  114.             swp->y = atoi(&av[i][2]) ;
  115.         } else if ((i + 1) < ac) {
  116.             swp->y = atoi(av[i+=1]) ;
  117.         }
  118.         break ;
  119.     }
  120.     }
  121. }
  122.  
  123. /*
  124.  * load/free Bitmap
  125.  */
  126.  
  127. HDC     hdcBitmap = NULLHANDLE ;
  128. HPS     hpsBitmap = NULLHANDLE ;
  129. HBITMAP hbmBitmap = NULLHANDLE ;
  130.  
  131. static  void    loadBitmap(HAB hab)
  132. {
  133.     SIZEL   siz ;
  134.     
  135.     hdcBitmap =DevOpenDC(hab, OD_MEMORY, "*", 0, NULL, NULLHANDLE) ;
  136.     siz.cx = siz.cy = 0 ;
  137.     hpsBitmap = GpiCreatePS(hab, hdcBitmap, &siz,
  138.             PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC) ;
  139.     if (hdcBitmap == NULLHANDLE || hpsBitmap == NULLHANDLE) {
  140.         return ;
  141.     }
  142.  
  143.     hbmBitmap = GpiLoadBitmap(hpsBitmap, NULLHANDLE, ID_BITMAP, 0, 0) ;
  144.     GpiSetBitmap(hpsBitmap, hbmBitmap) ;
  145. }
  146.  
  147. static  void    freeBitmap(void)
  148. {
  149.     if (hbmBitmap != NULLHANDLE) {
  150.         GpiDeleteBitmap(hbmBitmap) ;
  151.     hbmBitmap = NULLHANDLE ;
  152.     }
  153.     if (hpsBitmap != NULLHANDLE) {
  154.         GpiDestroyPS(hpsBitmap) ;
  155.     hpsBitmap = NULLHANDLE ;
  156.     }
  157.     if (hdcBitmap != NULLHANDLE) {
  158.         DevCloseDC(hdcBitmap) ;
  159.     hdcBitmap = NULLHANDLE ;
  160.     }
  161. }
  162.  
  163. /*
  164.  * Uses two window for control transparent bitmap
  165.  */
  166.  
  167. HWND    hwndFrame = NULLHANDLE ;    /* Invisible Frame Window   */
  168. HWND    hwndShape = NULLHANDLE ;    /* Shape Window for Bitmap  */
  169.  
  170. /*
  171.  * drawEyes - draw eye balls on Bitmap (Memory PS)
  172.  */
  173.  
  174. static  void    calcPos(PPOINTL base, PPOINTL ptr, PPOINTL eye)
  175. {
  176.     int     ix, iy, rad ;
  177.     double  r ;
  178.     
  179.     ix = ptr->x - base->x ;
  180.     iy = ptr->y - base->y ;
  181.     rad = EYE_RO ;
  182.     
  183.     if (((ix *  ix) + (iy * iy)) <= (rad * rad)) {
  184.         eye->x = ptr->x ;
  185.     eye->y = ptr->y ;
  186.     } else {
  187.         r = atan2((double) ix, (double) iy) ;
  188.     eye->x = base->x + (LONG) (sin(r) * (double) rad) ;
  189.     eye->y = base->y + (LONG) (cos(r) * (double) rad) ;
  190.     }
  191. }
  192.  
  193. static  POINTL  ptLast  = { 0, 0 } ;
  194. static  POINTL  ptCentL = { EYE_XL, EYE_Y } ;
  195. static  POINTL  ptCentR = { EYE_XR, EYE_Y } ;
  196. static  POINTL  ptLastL = { EYE_XL, EYE_Y } ;
  197. static  POINTL  ptLastR = { EYE_XR, EYE_Y } ;
  198. static  RECTL   rctUpdt = { RCT_XL, RCT_YB, RCT_XR, RCT_YT } ;
  199.  
  200. static  void    drawEyes(void)
  201. {
  202.     ARCPARAMS   arcParam ;
  203.     POINTL      pt ;
  204.  
  205.     if (hwndFrame == NULLHANDLE || hwndShape == NULLHANDLE) {
  206.         return ;
  207.     }
  208.     if (hpsBitmap == NULLHANDLE) {
  209.         return ;
  210.     }
  211.     
  212.     /*
  213.      * get mouse position, do nothing if not changed
  214.      */
  215.      
  216.     WinQueryPointerPos(HWND_DESKTOP, &pt) ;
  217.     WinMapWindowPoints(HWND_DESKTOP, hwndFrame, &pt, 1) ;
  218.  
  219.     if (pt.x == ptLast.x && pt.y == ptLast.y) {
  220.         return ;
  221.     }
  222.     ptLast.x = pt.x ;
  223.     ptLast.y = pt.y ;
  224.     
  225.     /*
  226.      * prepare arc params for eye balls
  227.      */
  228.      
  229.     arcParam.lP = EYE_RI ;
  230.     arcParam.lQ = EYE_RI ;
  231.     arcParam.lR = 0 ;
  232.     arcParam.lS = 0 ;
  233.     GpiSetArcParams(hpsBitmap, &arcParam) ;
  234.     
  235.     /*
  236.      * clear last position
  237.      */
  238.  
  239.     GpiSetColor(hpsBitmap, CLR_BACK) ;
  240.     GpiMove(hpsBitmap, &ptLastL) ;
  241.     GpiFullArc(hpsBitmap, DRO_FILL, (FIXED) 0x10000) ;
  242.     GpiMove(hpsBitmap, &ptLastR) ;
  243.     GpiFullArc(hpsBitmap, DRO_FILL, (FIXED) 0x10000) ;
  244.  
  245.     /*
  246.      * calc. new positions
  247.      */
  248.     
  249.     calcPos(&ptCentL, &pt, &ptLastL) ;
  250.     calcPos(&ptCentR, &pt, &ptLastR) ;
  251.  
  252.     /*
  253.      * draw on new positions
  254.      */
  255.      
  256.     GpiSetColor(hpsBitmap, CLR_BALL) ;
  257.     GpiMove(hpsBitmap, &ptLastL) ;
  258.     GpiFullArc(hpsBitmap, DRO_FILL, (FIXED) 0x10000) ;
  259.     GpiMove(hpsBitmap, &ptLastR) ;
  260.     GpiFullArc(hpsBitmap, DRO_FILL, (FIXED) 0x10000) ;
  261.  
  262.     /*
  263.      * update shape window
  264.      */
  265.     
  266.     WinSendMsg(hwndShape, SHAPEWIN_MSG_UPDATE, MPFROMP(&rctUpdt), NULL) ;
  267. }
  268.  
  269. /*
  270.  * createFrame - create frame window
  271.  */
  272.  
  273. static  PFNWP   pfnFrame ;
  274. static MRESULT EXPENTRY procFrame(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) ;
  275.  
  276. static  void    createFrame(HAB hab)
  277. {
  278.     FRAMECDATA  fcd    ;
  279.  
  280.     memset(&fcd, 0, sizeof(fcd)) ;
  281.     fcd.cb = sizeof(fcd) ;
  282.     fcd.flCreateFlags = (FCF_TASKLIST | FCF_ICON) ;
  283.     fcd.hmodResources = NULLHANDLE ;
  284.     fcd.idResources   = ID_ROMY    ;   
  285.  
  286.     hwndFrame = WinCreateWindow(
  287.             HWND_DESKTOP,           /* Parent window handle     */
  288.             WC_FRAME,               /* Frame Window Class       */
  289.             ProgramName,            /* as Title                 */
  290.             0,                      /* Window Style             */
  291.             0, 0, 0, 0,             /* Position & size          */
  292.             NULLHANDLE,             /* Owner Window             */
  293.             HWND_TOP,               /* Z-Order                  */
  294.             0,                      /* Window ID                */
  295.             &fcd,                   /* Control Data             */
  296.             NULL) ;                 /* Presentation Parameter   */
  297.  
  298.     if (hwndFrame == NULLHANDLE) {
  299.         return ;
  300.     }
  301.  
  302.     pfnFrame = WinSubclassWindow(hwndFrame, procFrame) ;
  303.  
  304.     WinSendMsg(hwndFrame, WM_SETICON, 
  305.         MPFROMP(WinLoadPointer(HWND_DESKTOP, NULLHANDLE, ID_ROMY)), NULL) ;
  306. }
  307.  
  308. /*
  309.  * createShape - create shape window
  310.  */
  311.  
  312. static  void    createShape(HAB hab)
  313. {
  314.     BITMAPINFOHEADER2   bmi ;
  315.     SHAPEWIN    shpctrl ;
  316.     
  317.     if (hwndFrame == NULLHANDLE) {
  318.         return ;
  319.     }
  320.     if (hpsBitmap == NULLHANDLE || hbmBitmap == NULLHANDLE) {
  321.         return ;
  322.     }
  323.     bmi.cbFix = sizeof(bmi) ;
  324.     GpiQueryBitmapInfoHeader(hbmBitmap, &bmi) ;
  325.     
  326.     /*
  327.      * Register Window Class
  328.      */
  329.      
  330.     WinRegisterClass(hab, ShapeWinName, ShapeWinProc, 0L, sizeof(PVOID)) ;
  331.  
  332.     /*
  333.      * Create Image Window
  334.      */
  335.  
  336.     shpctrl.cx = bmi.cx ;
  337.     shpctrl.cy = bmi.cy ;
  338.     shpctrl.hpsDraw = hpsBitmap ;
  339.     shpctrl.hpsMask = hpsBitmap ;
  340.     
  341.     hwndShape = WinCreateWindow(
  342.             HWND_DESKTOP,           /* Parent Window    */
  343.             ShapeWinName,           /* Window Class     */
  344.         NULL,                   /* Window Text      */
  345.         0,                      /* Window Style     */
  346.         0, 0, 0, 0,             /* Pos & Size       */
  347.         hwndFrame,              /* Owner Window     */
  348.         HWND_TOP,               /* Z-Order          */
  349.         0,                      /* Window ID        */
  350.         &shpctrl,               /* Control Data     */
  351.         NULL) ;                 /* Pres. Param.     */
  352.     
  353.     if (hwndShape == NULLHANDLE) {
  354.         return ;
  355.     }
  356. }
  357.  
  358. /*
  359.  * placeStartup - set startup position
  360.  */
  361.  
  362. static  void    placeStartup(PSWP swp)
  363. {
  364.     SWP     swpScreen ;
  365.     BITMAPINFOHEADER2   bmi ;
  366.     SHORT   x, y ;
  367.     
  368.     if (hbmBitmap == NULLHANDLE || hwndFrame == NULLHANDLE || hwndShape == NULLHANDLE) {
  369.         return ;
  370.     }
  371.     
  372.     x = swp->x ;
  373.     y = swp->y ;
  374.     
  375.     bmi.cbFix = sizeof(bmi) ;
  376.     GpiQueryBitmapInfoHeader(hbmBitmap, &bmi) ;
  377.  
  378.     WinQueryWindowPos(HWND_DESKTOP, &swpScreen) ;
  379.  
  380.     if ((x + bmi.cx) > swpScreen.cx) {
  381.         x = swpScreen.cx - bmi.cx ;
  382.     }
  383.     if ((y + bmi.cy) > swpScreen.cy) {
  384.         y = swpScreen.cy - bmi.cy ;
  385.     }
  386.     
  387.     WinSetWindowPos(hwndFrame, NULLHANDLE, 
  388.                     x, y, bmi.cx, bmi.cy, (SWP_MOVE | SWP_SIZE | SWP_HIDE)) ;
  389.     WinSetWindowPos(hwndShape, NULLHANDLE, 
  390.                     x, y, bmi.cx, bmi.cy, (SWP_MOVE | SWP_SIZE | SWP_SHOW)) ;
  391. }
  392.  
  393. /*
  394.  * context menu
  395.  */
  396.  
  397. static  HWND    hwndPopup = NULLHANDLE ;    /* context menu */
  398.  
  399. static  void    contextMenu(void)
  400. {
  401.     POINTL  pt   ;
  402.     ULONG   opts ;
  403.     
  404.     if (hwndPopup == NULLHANDLE) {
  405.         hwndPopup = WinLoadMenu(hwndFrame, NULLHANDLE, IDM_POPUP) ;
  406.     }
  407.     if (hwndPopup == NULLHANDLE) {
  408.         printf("failed to load opup menu\n") ;
  409.         return ;
  410.     }
  411.     
  412.     WinQueryPointerPos(HWND_DESKTOP, &pt) ;
  413.  
  414.     opts = PU_HCONSTRAIN | PU_VCONSTRAIN |
  415.          PU_KEYBOARD | PU_MOUSEBUTTON1 | PU_MOUSEBUTTON2 ;
  416.  
  417.     WinPopupMenu(HWND_DESKTOP, hwndFrame, hwndPopup, 
  418.                                 pt.x, pt.y, IDM_HIDE, opts) ;
  419. }
  420.  
  421. /*
  422.  * ABOUT dialog box
  423.  */
  424.  
  425. static  MRESULT EXPENTRY procAbout(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  426. {
  427.     HAB     hab ;
  428.     UCHAR   buff[256] ;
  429.     
  430.     switch (msg) {
  431.     
  432.     case WM_INITDLG :
  433.         hab = WinQueryAnchorBlock(hwnd) ;
  434.     WinSendMsg(WinWindowFromID(hwnd, IDD_ROMYTTL), 
  435.                     EM_SETTEXTLIMIT, MPFROMSHORT(128), NULL) ;
  436.     WinSendMsg(WinWindowFromID(hwnd, IDD_ROMYURL), 
  437.                     EM_SETTEXTLIMIT, MPFROMSHORT(128), NULL) ;
  438.         WinLoadString(hab, NULLHANDLE, IDS_ROMYTTL, 256, buff) ;
  439.     WinSetWindowText(WinWindowFromID(hwnd, IDD_ROMYTTL), buff) ;
  440.         WinLoadString(hab, NULLHANDLE, IDS_ROMYURL, 256, buff) ;
  441.     WinSetWindowText(WinWindowFromID(hwnd, IDD_ROMYURL), buff) ;
  442.         return (MRESULT) 0 ;
  443.  
  444.     case WM_COMMAND :
  445.         switch (SHORT1FROMMP(mp1)) {
  446.     case DID_OK :
  447.     case DID_CANCEL :
  448.         WinDismissDlg(hwnd, DID_OK) ;
  449.         return (MRESULT) 0 ;
  450.         }
  451.     }
  452.     return WinDefDlgProc(hwnd, msg, mp1, mp2) ;
  453. }
  454.  
  455. /*
  456.  * procFrame - sub-classed frame window procedure
  457.  */
  458.  
  459. static  BOOL    inDialog = FALSE ;
  460.  
  461. static MRESULT EXPENTRY procFrame(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  462. {
  463.     PSWP    pswp ;
  464.     SHORT   x, y ;
  465.     
  466.     TRACE("frame %08x %08x %08x\n", msg, mp1, mp2) ;
  467.     
  468.     switch (msg) {
  469.         
  470.     case WM_ADJUSTWINDOWPOS :
  471.         pswp = (PSWP) PVOIDFROMMP(mp1) ;
  472.         if (! inDialog) {
  473.             WinSetWindowPos(hwndShape, pswp->hwndInsertBehind,
  474.                 pswp->x, pswp->y, pswp->cx, pswp->cy, pswp->fl) ;
  475.         }
  476.     pswp->fl &= ~SWP_SHOW ;
  477.     pswp->fl |=  SWP_HIDE ;
  478.     return (*pfnFrame) (hwnd, msg, mp1, mp2) ;
  479.  
  480.     case WM_SINGLESELECT :
  481.         WinSetWindowPos(hwndShape, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER) ;
  482.     return (MRESULT) 0 ;
  483.                 
  484.     case WM_BEGINDRAG :
  485.         WinSendMsg(hwndFrame, WM_TRACKFRAME, 
  486.             MPFROMSHORT(TF_MOVE | TF_SETPOINTERPOS), NULL) ;
  487.         return (MRESULT) 0 ;
  488.  
  489.     case WM_CONTEXTMENU :
  490.         contextMenu() ;
  491.         return (MRESULT) 0 ;
  492.     
  493.     case WM_COMMAND :
  494.         switch (SHORT1FROMMP(mp1)) {
  495.     case IDM_MOVE :
  496.             WinSendMsg(hwndFrame, WM_TRACKFRAME, 
  497.                     MPFROMSHORT(TF_MOVE | TF_SETPOINTERPOS), NULL) ;
  498.             return (MRESULT) 0 ;
  499.     case IDM_HIDE :
  500.         WinShowWindow(hwnd, FALSE) ;
  501.         return (MRESULT) 0 ;
  502.     case IDM_ABOUT :
  503.             inDialog = TRUE ;
  504.         WinDlgBox(HWND_DESKTOP, hwnd, procAbout, NULLHANDLE, IDD_ABOUT, NULL) ;
  505.         inDialog = FALSE ;
  506.         return (MRESULT) 0 ;
  507.     case IDM_EXIT  :
  508.         WinPostMsg(hwnd, WM_CLOSE, NULL, NULL) ;
  509.         return (MRESULT) 0 ;
  510.         }
  511.         return (MRESULT) 0 ;
  512.  
  513.     case WM_TIMER :
  514.         drawEyes() ;
  515.     return (MRESULT) 0 ;
  516.     }
  517.     return (*pfnFrame) (hwnd, msg, mp1, mp2) ;
  518. }
  519.  
  520. /*
  521.  * main - program start here
  522.  */
  523.  
  524. int     main(int ac, char *av[])
  525. {
  526.     HAB     hab  ;
  527.     HMQ     hmq  ;
  528.     QMSG    qmsg ;
  529.     SWP     swp  ;
  530.     
  531.     myname(av[0]) ;
  532.     _wildcard(&ac, &av) ;
  533.  
  534.     hab = WinInitialize(0) ;
  535.     hmq = WinCreateMsgQueue(hab, 0) ;
  536.     
  537.     initPos(ac, av, &swp) ;
  538.  
  539.     loadBitmap(hab) ;
  540.     
  541.     if (hbmBitmap == NULLHANDLE) {
  542.         trMessage("failed to load bitmap") ;
  543.         freeBitmap() ;
  544.         WinDestroyMsgQueue(hmq) ;
  545.         WinTerminate(hab) ;
  546.     return 1 ;
  547.     }
  548.     
  549.     /*
  550.      * Start Window Processing
  551.      */
  552.  
  553.     createFrame(hab) ;
  554.     createShape(hab) ;
  555.  
  556.     if (hwndFrame == NULLHANDLE || hwndShape == NULLHANDLE) {    
  557.         trMessage("failed to create windows") ;
  558.     if (hwndFrame != NULLHANDLE) WinDestroyWindow(hwndFrame) ;
  559.     if (hwndShape != NULLHANDLE) WinDestroyWindow(hwndShape) ;
  560.         freeBitmap() ;
  561.         WinDestroyMsgQueue(hmq) ;
  562.         WinTerminate(hab) ;
  563.     return 1 ;
  564.     }
  565.     
  566.     placeStartup(&swp) ;
  567.     WinStartTimer(hab, hwndFrame, 1, 200) ;
  568.     
  569.     while (WinGetMsg(hab, &qmsg, 0, 0, 0)) {
  570.         WinDispatchMsg(hab, &qmsg) ;
  571.     }
  572.  
  573.     /*
  574.      * dispose resources
  575.      */
  576.     
  577.     WinDestroyWindow(hwndFrame)  ;
  578.     WinDestroyWindow(hwndShape) ;
  579.  
  580.     freeBitmap() ;
  581.     
  582.     WinDestroyMsgQueue(hmq) ;
  583.     WinTerminate(hab) ;
  584.     
  585.     return 0 ; 
  586. }
  587.