home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xuserwnd.cpp < prev    next >
C/C++ Source or Header  |  1997-04-05  |  8KB  |  336 lines

  1. #include "xuserwnd.h"
  2. #include "xrect.h"
  3. #include "xexcept.h"
  4. #include "xcntrevn.h"
  5. #include "stdlib.h"
  6. #include "xreslib.h"
  7. #include "xbubble.h"
  8. #include "xtimer.h"
  9. #include "xbitmap.h"
  10. #include "xgraphdv.h"
  11. #include "xres.h"
  12. #include "xprocess.h"
  13. #include "xmoushdl.h"
  14. #include "xfont.h"
  15. #include "xmousevn.h"
  16. #include "XText.h"
  17.  
  18.  
  19. MRESULT EXPENTRY userProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  20. {
  21.     XWindow *win = (XUserWindow *) WinQueryWindowPtr(hwnd, 0);
  22.  
  23.     if (msg == WM_CREATE)
  24.     {
  25.         WinSetWindowPtr(hwnd, 0, (void *) mp1);
  26.         mp1 = 0;
  27.         return WinDefWindowProc(hwnd, msg, mp1, mp2);
  28.     }
  29.  
  30.     if (win)
  31.     {
  32.         BOOL handled = FALSE;
  33.         MRESULT mr = HandleDefault(win, msg, mp1, mp2, handled);
  34.  
  35.         if (handled)
  36.             return mr;
  37.     }
  38.  
  39.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  40. }
  41.  
  42.  
  43. XUserWindow :: XUserWindow(const XWindow * w)
  44. {
  45.     if (w)
  46.         XUserWindow(w->GetClientHandle());
  47.     else
  48.         XUserWindow(HWND_DESKTOP);
  49. }
  50.  
  51.  
  52. XUserWindow :: XUserWindow(const LONG handle)
  53. {
  54.     if (WinRegisterClass(WinQueryAnchorBlock(handle), (PSZ) "OOL_USERCLASS", (PFNWP) userProc, 0, 4) == FALSE)
  55.         OOLThrow("error creating userwindow - couldn∩t register class", -10);
  56.  
  57.     winhandle = WinCreateWindow(handle, (PSZ) "OOL_USERCLASS", (PSZ) "", WS_VISIBLE | FS_SCREENALIGN,
  58.                                 0, 0, 0, 0, handle, HWND_TOP, 0, this, 0);
  59. }
  60.  
  61.  
  62. XUserWindow :: XUserWindow(const XResource * r)
  63. {
  64.     if (WinRegisterClass(r->GetResourceLibrary()->GetProcess()->hab, (PSZ) "OOL_USERCLASS", (PFNWP) userProc, 0, 4) == FALSE)
  65.         OOLThrow("error creating userwindow - couldn∩t register class", -10);
  66.  
  67.     winhandle = WinCreateWindow(HWND_DESKTOP, (PSZ) "OOL_USERCLASS", (PSZ) "", WS_VISIBLE | FS_SCREENALIGN,
  68.                               0, 0, 0, 0, HWND_DESKTOP, HWND_TOP, 0, this, 0);
  69.     if (winhandle == 0)
  70.         OOLThrow("error creating userwindow", -10);
  71. }
  72.  
  73.  
  74. XUserWindow :: ~XUserWindow()
  75. {
  76.     HWND hwnd;
  77.  
  78.     HENUM enumWindow = WinBeginEnumWindows(winhandle);
  79.  
  80.     while ((hwnd = WinGetNextWindow(enumWindow)) != NULLHANDLE)
  81.     {
  82.         XWindow *w = (XWindow *) WinQueryWindowPtr(hwnd, 0);
  83.  
  84.         if (w && w != this)
  85.             delete w;
  86.     };
  87.     WinEndEnumWindows(enumWindow);
  88.     WinDestroyWindow(winhandle);
  89. }
  90.  
  91.  
  92. class bubbletimer:public XTimer
  93. {
  94.         XBubbleHelp *bubble;
  95.     public:
  96.         ULONG id;
  97.         bubbletimer(XBubbleHelp * w):XTimer(w) {    bubble = w;    }
  98.         void TimerEvent(void);
  99. };
  100.  
  101.  
  102. void bubbletimer::TimerEvent(void)
  103. {
  104.     POINTL p;
  105.     POINTL buffer;
  106.  
  107.     if (!bubble->isActive)
  108.         return;
  109.     WinQueryPointerPos(HWND_DESKTOP, &p);
  110.     buffer = p;
  111.     SWP swp;
  112.  
  113.     WinMapWindowPoints(HWND_DESKTOP, bubble->aktWin->GetHandle(), &buffer, 1);
  114.     WinQueryWindowPos(bubble->aktWin->GetHandle(), &swp);
  115.  
  116.     if (!(buffer.x > 0 && buffer.x < swp.cx && buffer.y > 0 && buffer.y < swp.cy))
  117.         return;
  118.  
  119.     XRect rect;
  120.  
  121.     if (bubble->SetMsgText(id) == FALSE)
  122.         return;
  123.     rect.SetX(p.x);
  124.     rect.SetY(p.y + 2);
  125.     rect.SetWidth(174);
  126.     rect.SetHeight(71);
  127.     bubble->SetSize(&rect);
  128.     bubble->Show();
  129.     Stop();
  130. }
  131.  
  132.  
  133. class bhandler:public XMouseHandler
  134. {
  135.     XBubbleHelp *bubble;
  136.         public:
  137.         bhandler(XBubbleHelp * b, XWindow * w):XMouseHandler(w)
  138.     {
  139.         bubble = b;
  140.     }
  141.     BOOL HandleEvent(XMouseEvent *);
  142. };
  143.  
  144.  
  145. BOOL bhandler::HandleEvent(XMouseEvent *)
  146. {
  147.     if (bubble->IsVisible())
  148.         bubble->Show(FALSE);
  149.     bubble->timer->Stop();
  150.     return TRUE;
  151. }
  152.  
  153.  
  154. class bubblehandler:public XMouseHandler
  155. {
  156.         XBubbleHelp *bubble;
  157.     public:
  158.         bubblehandler(XBubbleHelp * b, XWindow * w):XMouseHandler(w) {    bubble = b;    }
  159.         BOOL HandleEvent(XMouseEvent *);
  160. };
  161.  
  162.  
  163. BOOL bubblehandler::HandleEvent(XMouseEvent * e)
  164. {
  165.     switch (e->GetEventID())
  166.     {
  167.     default:
  168.         if (bubble->IsVisible())
  169.             bubble->Show(FALSE);
  170.         bubble->timer->Stop();
  171.         break;
  172.     case MOU_MOVE:
  173.         {
  174.             POINTL buffer;
  175.  
  176.             HWND i = 0;
  177.             SWP swp;
  178.  
  179.             WinQueryPointerPos(HWND_DESKTOP, &buffer);
  180.             WinMapWindowPoints(HWND_DESKTOP, GetWindow()->GetHandle(), &buffer, 1);
  181.             WinQueryWindowPos(GetWindow()->GetHandle(), &swp);
  182.  
  183.             if (buffer.x > 0 && buffer.x < swp.cx && buffer.y > 0 && buffer.y < swp.cy)
  184.                 i = GetWindow()->GetHandle();
  185.             if (i)
  186.             {
  187.                 if (!bubble->IsVisible() && bubble->isActive)
  188.                 {
  189.                     bubble->timer->id = WinQueryWindowUShort(GetWindow()->GetHandle(), QWS_ID);
  190.                     bubble->aktWin = GetWindow();
  191.                     bubble->timer->Start(1000);
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 bubble->Show(FALSE);
  197.                 bubble->timer->Stop();
  198.             }
  199.         }
  200.     }
  201.     return FALSE;
  202. }
  203.  
  204.  
  205. /*@ XBubbleHelp::SetMsgText()
  206. @group text functions
  207. @remarks Override this function to select the text to display with SetText()
  208. @parameters ULONG id    the window-id from the window to display a helptext for
  209. @returns    BOOL        TRUE=show the bubble, FALSE=dont display the bubble
  210. */
  211.  
  212.  
  213. /*@ XBubbleHelp::SetText()
  214. @group text functions
  215. @remarks Set the text to display
  216. @parameters char * theText
  217. */
  218.  
  219.  
  220. /*@ XBubbleHelp::Enable()
  221. @group misc
  222. @remarks Enable/disable XBubbleHelp
  223. @parameters BOOL enable    TRUE=enable, FALSE=disable (default is TRUE)
  224. */
  225.  
  226.  
  227. /*@ 
  228. @class XBubbleHelp
  229. @parent XUserWindow
  230. @type overview
  231. @symbol _
  232. @remarks XBubbleHelp is a window which can display short help-messages, it looks like
  233. the bubbles on a mac.<P>
  234. To use XBubbleHelp derive a class of it and override XBubbleHelp::SetMsgText().
  235. See XBubbleHelp::AddWindow() and XBubbleHelp::XBubbleHelp() for details how to
  236. select windows to show help-messages for.<P>
  237. For applications which use XBubblehelp it is nessacary
  238. that two bitmaps are linked to the resources of the application (in the *.RES file):
  239. BUBBLE1.BMP which must have the resource-id 10000 and BUBBLE2.BMP with the resource-id 10001.
  240. You can find these bitmaps in the directory ..OOL\RESOURCE<P>
  241. The destructor is not called automaticaly when the window is hidden.<P>
  242. XBubbleHelp does not work with menus!
  243. */
  244.  
  245.  
  246. /*@ XBubbleHelp: : XBubbleHelp(XResource * r, XWindow * w)
  247. @group constructors/destructors
  248. @remarks Construct a bubblehelp-window
  249. @parameters <t '°' c=2>
  250.                 °XResource *      °A resource which points to a resource-library which contains the needed bitmaps
  251.             °XWindow *        °The owner-window.
  252.             °BOOL handleAll   °TRUE=all clients are handled automaticaly, you dont need to call AddWindow<BR>
  253.                              FALSE=no clients are handled, you must add clients to handle with AddWindow()
  254.                 </t>
  255. */
  256. XBubbleHelp :: XBubbleHelp(XResource * r, XWindow * w):XUserWindow(r)
  257. {
  258.     dev = NULL;
  259.     map1 = map2 = map3 = NULL;
  260.     dev = new XGraphicDevice(this, FALSE, FALSE);
  261.  
  262.     dev->SetWidth(174);
  263.     dev->SetHeight(71);
  264.  
  265.     XPoint p;
  266.  
  267.     map3 = new XBitmap(dev);
  268.     map3->Copy(dev, &p);
  269.  
  270. //   XPoint p(0,0);
  271.     map1 = new XBitmap(dev, &p, ROP_SRCAND);
  272.     XResource res(10000, r->GetResourceLibrary());
  273.  
  274.     map1->Load(&res);
  275.  
  276.     XResource res2(10001, r->GetResourceLibrary());
  277.  
  278.     map2 = new XBitmap(dev, &p, ROP_SRCINVERT);
  279.     map2->Load(&res2);
  280.  
  281.     XRect rec(12, 3, 152, 61);
  282.     XFont *font = new XFont(dev, "Helv", 8);
  283.  
  284.     text = new XText(dev, font, &rec, "", DT_LEFT | DT_WORDBREAK | DT_TOP);
  285.  
  286.     timer = new bubbletimer(this);
  287.     text->Show();
  288.  
  289.     HWND hwnd;
  290.     XWindow *win;
  291.     HENUM henum = WinBeginEnumWindows(w->GetHandle());
  292.  
  293.     while ((hwnd = WinGetNextWindow(henum)) != 0)
  294.     {
  295.         win = (XWindow *) WinQueryWindowPtr(hwnd, 0);
  296.         if (win)
  297.             bubblehandler *h = new bubblehandler(this, win);
  298.     }
  299.     WinEndEnumWindows(henum);
  300.  
  301.     bhandler *b1 = new bhandler(this, w);
  302.     bhandler *b2 = new bhandler(this, this);
  303.  
  304.     isActive = TRUE;
  305. }
  306.  
  307.  
  308. XBubbleHelp :: ~XBubbleHelp()
  309. {
  310.     delete dev;
  311. }
  312.  
  313.  
  314. void XBubbleHelp::Draw(void)
  315. {
  316.     if (IsVisible())
  317.         dev->Draw();
  318. }
  319.  
  320.  
  321. void XBubbleHelp::Show(const BOOL show)
  322. {
  323.     if (!dev)
  324.         return;
  325.     if (show)
  326.     {
  327.         XPoint p;
  328.  
  329.         XWindow :: Show(TRUE);
  330.         if (map3)
  331.             map3->Copy(dev, &p);
  332.     }
  333.     else
  334.         XWindow :: Show(FALSE);
  335. }
  336.