home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xuserwnd.cpp < prev    next >
C/C++ Source or Header  |  1998-03-27  |  15KB  |  702 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. #include "xtraffic.h"
  18. #include "xled.h"
  19. #include "xpeak.h"
  20. #include "xdate.h"
  21. #include "xcal.h"
  22. #include "xmoushdl.h"
  23. #include "xmousevn.h"
  24. #include "oolres.rh"
  25.  
  26. MRESULT EXPENTRY userProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  27. {
  28.    XWindow *win = (XUserWindow *) WinQueryWindowPtr(hwnd, 0);
  29.  
  30.    if (msg == WM_CREATE)
  31.    {
  32.       WinSetWindowPtr(hwnd, 0, (void *) mp1);
  33.       mp1 = 0;
  34.       return WinDefWindowProc(hwnd, msg, mp1, mp2);
  35.    }
  36.  
  37.    if (win)
  38.    {
  39.       BOOL handled = FALSE;
  40.       MRESULT mr = HandleDefault(win, msg, mp1, mp2, handled);
  41.  
  42.       if (handled)
  43.          return mr;
  44.    }
  45.  
  46.    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  47. }
  48.  
  49.  
  50. /*@
  51. @class XUserWindow
  52. @parent XWindow
  53. @type overview
  54. @symbol _
  55. */
  56.  
  57. XUserWindow :: XUserWindow(const XWindow * w)
  58. {
  59.    if (w)
  60.       XUserWindow(w->GetClientHandle());
  61.    else
  62.       XUserWindow(HWND_DESKTOP);
  63. }
  64.  
  65.  
  66. XUserWindow :: XUserWindow(const LONG handle)
  67. {
  68.    if (WinRegisterClass(WinQueryAnchorBlock(handle), (PSZ) "OOL_USERCLASS", (PFNWP) userProc, 0, 4) == FALSE)
  69.       OOLThrow("error creating userwindow - couldn∩t register class", -10);
  70.  
  71.    winhandle = WinCreateWindow(handle, (PSZ) "OOL_USERCLASS", (PSZ) "", WS_VISIBLE | FS_SCREENALIGN,
  72.                         0, 0, 0, 0, handle, HWND_TOP, 0, this, 0);
  73. }
  74.  
  75.  
  76. XUserWindow :: XUserWindow( void )
  77. {
  78.    if (WinRegisterClass(XApplication::GetApplication()->hab, (PSZ) "OOL_USERCLASS", (PFNWP) userProc, 0, 4) == FALSE)
  79.       OOLThrow("error creating userwindow - couldn∩t register class", -10);
  80.  
  81.    winhandle = WinCreateWindow(HWND_DESKTOP, (PSZ) "OOL_USERCLASS", (PSZ) "", WS_VISIBLE | FS_SCREENALIGN,
  82.                        0, 0, 0, 0, HWND_DESKTOP, HWND_TOP, 0, this, 0);
  83.    if (winhandle == 0)
  84.       OOLThrow("error creating userwindow", -10);
  85. }
  86.  
  87.  
  88. XUserWindow :: ~XUserWindow()
  89. {
  90.    HWND hwnd;
  91.  
  92.    HENUM enumWindow = WinBeginEnumWindows(winhandle);
  93.  
  94.    while ((hwnd = WinGetNextWindow(enumWindow)) != NULLHANDLE)
  95.    {
  96.       XWindow *w = (XWindow *) WinQueryWindowPtr(hwnd, 0);
  97.  
  98.       if (w && w != this)
  99.          delete w;
  100.    };
  101.    WinEndEnumWindows(enumWindow);
  102.    WinDestroyWindow(winhandle);
  103. }
  104.  
  105.  
  106. class bubbletimer:public XTimer
  107. {
  108.       XBubbleHelp *bubble;
  109.    public:
  110.       ULONG id;
  111.       bubbletimer(XBubbleHelp * w):XTimer(w) { bubble = w; }
  112.       void TimerEvent(void);
  113. };
  114.  
  115.  
  116. void bubbletimer::TimerEvent(void)
  117. {
  118.    POINTL p;
  119.    POINTL buffer;
  120.  
  121.    if (!bubble->isActive)
  122.       return;
  123.    WinQueryPointerPos(HWND_DESKTOP, &p);
  124.    buffer = p;
  125.    SWP swp;
  126.  
  127.    WinMapWindowPoints(HWND_DESKTOP, bubble->aktWin->GetHandle(), &buffer, 1);
  128.    WinQueryWindowPos(bubble->aktWin->GetHandle(), &swp);
  129.  
  130.    if (!(buffer.x > 0 && buffer.x < swp.cx && buffer.y > 0 && buffer.y < swp.cy))
  131.       return;
  132.  
  133.    XRect rect;
  134.  
  135.    if (bubble->SetMsgText(id) == FALSE)
  136.       return;
  137.    rect.SetX(p.x);
  138.    rect.SetY(p.y + 2);
  139.    rect.SetWidth(174);
  140.    rect.SetHeight(71);
  141.    bubble->SetSize(&rect);
  142.    bubble->Show();
  143.    Stop();
  144. }
  145.  
  146.  
  147. class bhandler:public XMouseHandler
  148. {
  149.       XBubbleHelp *bubble;
  150.    public:
  151.       bhandler(XBubbleHelp * b, XWindow * w):XMouseHandler(w) { bubble = b; }
  152.       BOOL HandleEvent(XMouseEvent *);
  153. };
  154.  
  155.  
  156. BOOL bhandler::HandleEvent(XMouseEvent *)
  157. {
  158.    if (bubble->IsVisible())
  159.       bubble->Show(FALSE);
  160.    bubble->timer->Stop();
  161.    return FALSE;
  162. }
  163.  
  164.  
  165. class bubblehandler:public XMouseHandler
  166. {
  167.       XBubbleHelp *bubble;
  168.    public:
  169.       bubblehandler(XBubbleHelp * b, XWindow * w):XMouseHandler(w) {   bubble = b;   }
  170.       BOOL HandleEvent(XMouseEvent *);
  171. };
  172.  
  173.  
  174. BOOL bubblehandler::HandleEvent(XMouseEvent * e)
  175. {
  176.    switch (e->GetEventID())
  177.    {
  178.       case MOU_MOVE:
  179.       {
  180.          POINTL buffer;
  181.  
  182.          HWND i = 0;
  183.          SWP swp;
  184.  
  185.          WinQueryPointerPos(HWND_DESKTOP, &buffer);
  186.          WinMapWindowPoints(HWND_DESKTOP, GetWindow()->GetHandle(), &buffer, 1);
  187.          WinQueryWindowPos(GetWindow()->GetHandle(), &swp);
  188.  
  189.          if (buffer.x > 0 && buffer.x < swp.cx && buffer.y > 0 && buffer.y < swp.cy)
  190.             i = GetWindow()->GetHandle();
  191.          if (i)
  192.          {
  193.             if (!bubble->IsVisible() && bubble->isActive)
  194.             {
  195.                bubble->timer->id = WinQueryWindowUShort(GetWindow()->GetHandle(), QWS_ID);
  196.                bubble->aktWin = GetWindow();
  197.                bubble->timer->Start(1000);
  198.             }
  199.          }
  200.          else
  201.          {
  202.             bubble->Show(FALSE);
  203.             bubble->timer->Stop();
  204.          }
  205.          break;
  206.       }
  207.       default:
  208.          if (bubble->IsVisible())
  209.             bubble->Show(FALSE);
  210.          bubble->timer->Stop();
  211.          break;
  212.    }
  213.    return FALSE;
  214. }
  215.  
  216.  
  217. /*@ XBubbleHelp::SetMsgText()
  218. @group text functions
  219. @remarks Override this function to select the text to display with SetText()
  220. @parameters ULONG id    the window-id from the window to display a helptext for
  221. @returns    BOOL        TRUE=show the bubble, FALSE=dont display the bubble
  222. */
  223.  
  224.  
  225. /*@ XBubbleHelp::SetText()
  226. @group text functions
  227. @remarks Set the text to display
  228. @parameters char * theText
  229. */
  230.  
  231.  
  232. /*@ XBubbleHelp::Enable()
  233. @group misc
  234. @remarks Enable/disable XBubbleHelp
  235. @parameters BOOL enable    TRUE=enable, FALSE=disable (default is TRUE)
  236. */
  237.  
  238.  
  239. /*@
  240. @class XBubbleHelp
  241. @parent XUserWindow
  242. @type overview
  243. @symbol _
  244. @remarks XBubbleHelp is a window which can display short help-messages, it looks like
  245. the bubbles on a mac.<P>
  246. To use XBubbleHelp derive a class of it and override XBubbleHelp::SetMsgText().
  247. See XBubbleHelp::AddWindow() and XBubbleHelp::XBubbleHelp() for details how to
  248. select windows to show help-messages for.<P>
  249. For applications which use XBubbleHelp it is nessecary that the OOL-resourcelibrary
  250. OOLRES.DLL is in the libpath.<P>
  251. The destructor is not called automaticaly when the window is hidden.<P>
  252. XBubbleHelp does not work with menus!
  253. */
  254.  
  255.  
  256. /*@ XBubbleHelp: : XBubbleHelp(XWindow * w)
  257. @group constructors/destructors
  258. @remarks Construct a bubblehelp-window
  259. @parameters XWindow *   The owner-window.
  260. */
  261. XBubbleHelp :: XBubbleHelp( XWindow * w):XUserWindow()
  262. {
  263.    dev = NULL;
  264.    map1 = map2 = map3 = NULL;
  265.    dev = new XGraphicDevice(this, FALSE, FALSE);
  266.  
  267.    dev->SetWidth(174);
  268.    dev->SetHeight(71);
  269.  
  270.    XPoint p;
  271.  
  272.    map3 = new XBitmap();
  273.    map3->Copy(dev, &p);
  274.  
  275.    map1 = new XBitmap(&p, ROP_SRCAND);
  276.    map2 = new XBitmap(&p, ROP_SRCINVERT);
  277.  
  278.    XResourceLibrary lib("oolres");
  279.    XResource res( IDR_BUBBLE1, &lib);
  280.  
  281.    map1->Load( &res );
  282.  
  283.    res.SetID(IDR_BUBBLE2);
  284.    map2->Load( &res );
  285.  
  286.    XRect rec(12, 3, 152, 61);
  287.    XFont *font = new XFont(dev, "Helv", 8);
  288.  
  289.    text = new XText(font, &rec, "", DT_LEFT | DT_WORDBREAK | DT_TOP);
  290.  
  291.    timer = new bubbletimer(this);
  292.    text->Show();
  293.  
  294.    HWND hwnd;
  295.    XWindow *win;
  296.    HENUM henum = WinBeginEnumWindows(w->GetHandle());
  297.  
  298.    while ((hwnd = WinGetNextWindow(henum)) != 0)
  299.    {
  300.       win = (XWindow *) WinQueryWindowPtr(hwnd, 0);
  301.       if (win)
  302.          bubblehandler *h = new bubblehandler(this, win);
  303.    }
  304.    WinEndEnumWindows(henum);
  305.  
  306.    bhandler *b1 = new bhandler(this, w);
  307.    bhandler *b2 = new bhandler(this, this);
  308.  
  309.    isActive = TRUE;
  310. }
  311.  
  312.  
  313. XBubbleHelp :: ~XBubbleHelp()
  314. {
  315.    delete map1;
  316.    delete map2;
  317.    delete map3;
  318.    delete text;
  319.    delete dev;
  320. }
  321.  
  322.  
  323. void XBubbleHelp::Draw(void)
  324. {
  325.    if (IsVisible())
  326.    {
  327.       map1->Draw(dev);
  328.       map2->Draw(dev);
  329.       map3->Draw(dev);
  330.       text->Draw(dev);
  331.    } /* end if */
  332. }
  333.  
  334.  
  335. void XBubbleHelp::Show(const BOOL show)
  336. {
  337.    if (!dev)
  338.       return;
  339.    if (show)
  340.    {
  341.       XPoint p;
  342.       XWindow :: Show(TRUE);
  343.       if (map3)
  344.          map3->Copy(dev, &p);
  345.    }
  346.    else
  347.       XWindow :: Show(FALSE);
  348. }
  349.  
  350.  
  351. /*@
  352. @class XTrafficLight
  353. @parent XUserWindow
  354. @type overview
  355. @symbol _
  356. @remarks XTrafficLight is a class which creates a window that looks like a traffic light<P>
  357. For applications which use XTrafficLight it is nessecary that the OOL-resourcelibrary
  358. OOLRES.DLL is in the libpath.
  359. */
  360. XTrafficLight :: XTrafficLight( const XWindow * w, const XPoint& p, const USHORT id):XUserWindow(w->GetHandle())
  361. {
  362.    dev = new XGraphicDevice(this);
  363.    WinSetWindowUShort(winhandle, QWS_ID, id);
  364.    state = 0;
  365.  
  366.    XRect rect( p.GetX(), p.GetY(), 32, 32);
  367.    SetSize(&rect);
  368.  
  369.    dev->SetWidth(32);
  370.    dev->SetHeight(32);
  371.  
  372.    XPoint p0;
  373.  
  374.    map = new XBitmap( &p0);
  375.  
  376.    SetState(0);
  377.    Draw();
  378. }
  379.  
  380.  
  381. /*@ XTrafficLight::SetState( const SHORT st )
  382. @remarks Set the current state
  383. @parameters SHORT state, possible values are:
  384. <OL>
  385. <LI>TRAFFIC_OFF
  386. <LI>TRAFFIC_GREEN
  387. <LI>TRAFFIC_YELLOW
  388. <LI>TRAFFIC_RED
  389. <LI>TRAFFIC_YELLOWRED
  390. </OL>
  391. */
  392. void XTrafficLight::SetState( const SHORT st )
  393. {
  394.    XResourceLibrary lib("oolres");
  395.    XResource res( IDR_TRAFFIC + st, &lib);
  396.  
  397.    map->Load( &res );
  398.  
  399.    state = st;
  400.    Draw();
  401. }
  402.  
  403.  
  404. void XTrafficLight::Draw()
  405. {
  406.    map->Draw(dev);
  407. }
  408.  
  409.  
  410. XTrafficLight :: ~XTrafficLight()
  411. {
  412.    delete map;
  413.    delete dev;
  414. }
  415.  
  416. /*@
  417. @class XLED
  418. @parent XUserWindow
  419. @type overview
  420. @symbol _
  421. @remarks XLED is a class which creates a window that looks like a LED<P>
  422. For applications which use XLED it is nessecary that the OOL-resourcelibrary
  423. OOLRES.DLL is in the libpath.
  424. */
  425. XLED :: XLED( const XWindow * w, const XPoint& p, const USHORT id):XUserWindow(w->GetHandle())
  426. {
  427.    WinSetWindowUShort(winhandle, QWS_ID, id);
  428.    dev = new XGraphicDevice(this);
  429.    state = FALSE;
  430.  
  431.    XRect rect( p.GetX(), p.GetY(), 16, 16);
  432.    SetSize(&rect);
  433.  
  434.    dev->SetWidth(16);
  435.    dev->SetHeight(16);
  436.  
  437.    XPoint p0;
  438.  
  439.    map = new XBitmap( &p0);
  440.    Enable(FALSE);
  441.    Draw();
  442. }
  443.  
  444.  
  445. void XLED :: Draw( void )
  446. {
  447.    map->Draw(dev);
  448. }
  449.  
  450. XLED :: ~XLED()
  451. {
  452.    delete map;
  453.    delete dev;
  454. }
  455.  
  456.  
  457. /*@ XLED::Enable( const BOOL st )
  458. @remarks Enable/Disable the LED
  459. @parameters BOOL TRUE=enable, FALSE=disable, default is TRUE
  460. */
  461. void XLED::Enable( const BOOL st )
  462. {
  463.    XResourceLibrary lib("oolres");
  464.    XResource res(0, &lib);
  465.  
  466.    if(st == FALSE)
  467.    {
  468.       res.SetID(IDR_LED_OFF);
  469.       map->Load(&res);
  470.    }
  471.    else
  472.    {
  473.       res.SetID(IDR_LED_ON);
  474.       map->Load(&res);
  475.    }
  476.    state = st;
  477.    Draw();
  478. }
  479.  
  480.  
  481. XPeakMeter :: XPeakMeter( const XWindow * w, const XPoint& p, const USHORT id, const BOOL v):XUserWindow(w->GetHandle())
  482. {
  483.    WinSetWindowUShort(winhandle, QWS_ID, id);
  484.    dev = new XGraphicDevice(this);
  485.    vert = v;
  486.  
  487.    if(vert)
  488.    {
  489.       XRect rect( p.GetX(), p.GetY(), 215, 15);
  490.       SetSize(&rect);
  491.  
  492.       dev->SetWidth(215);
  493.       dev->SetHeight(15);
  494.    }
  495.    else
  496.    {
  497.       XRect rect( p.GetX(), p.GetY(), 20, 145);
  498.       SetSize(&rect);
  499.  
  500.       dev->SetWidth(20);
  501.       dev->SetHeight(145);
  502.    }
  503.  
  504.    XColor col(COL_BLACK);
  505.    dev->SetBackgroundColor( &col );
  506.  
  507.    XPoint point(10, 5);
  508.  
  509.    XResourceLibrary lib("oolres");
  510.    XResource res(IDR_PEAK_GREEN, &lib);
  511.  
  512.    mapGreen = new XBitmap( &point);
  513.    mapGreen->Load(&res);
  514.    mapGreen->Show();
  515.  
  516.    point.SetX( 200 );
  517.    res.SetID(IDR_PEAK_RED);
  518.    mapRed = new XBitmap( &point);
  519.    mapRed->Load(&res);
  520.    mapRed->Show();
  521.    SetLevel(0);
  522. }
  523.  
  524.  
  525. void XPeakMeter :: Draw( void )
  526. {
  527.    dev->FillBackground();
  528.    XPoint point(10, 5);
  529.    if(vert)
  530.    {
  531.       for(int i = 0; i < 14; i++)
  532.       {
  533.          if(i < level)
  534.          {
  535.             point.SetX( 5 + i * 15);
  536.             if(i < 10 )
  537.             {
  538.                mapGreen->Move( &point );
  539.                mapGreen->Draw(dev);
  540.             }
  541.             else
  542.             {
  543.                mapRed->Move( &point );
  544.                mapRed->Draw(dev);
  545.             }
  546.          } /* end if */
  547.       }
  548.    }
  549.    else
  550.    {
  551.       point.SetX(5);
  552.  
  553.       for(int i = 0; i < 14; i++)
  554.       {
  555.          if(i < level)
  556.          {
  557.             point.SetY( 5 + i * 10);
  558.             if(i < 10 )
  559.             {
  560.                mapGreen->Move( &point );
  561.                mapGreen->Draw(dev);
  562.             }
  563.             else
  564.             {
  565.                mapRed->Move( &point );
  566.                mapRed->Draw(dev);
  567.             }
  568.          } /* end if */
  569.       }
  570.    }
  571. }
  572.  
  573.  
  574. void XPeakMeter :: SetLevel( USHORT l)
  575. {
  576.    level = l;
  577.    Draw();
  578. }
  579.  
  580.  
  581. XPeakMeter::~XPeakMeter()
  582. {
  583.    delete mapGreen;
  584.    delete mapRed;
  585.    delete dev;
  586. }
  587.  
  588.  
  589. class CalendarHandler: public XMouseHandler
  590. {
  591.    public:
  592.       CalendarHandler(XWindow* w): XMouseHandler(w) {;}
  593.       BOOL HandleEvent( XMouseEvent *);
  594. };
  595.  
  596.  
  597. BOOL CalendarHandler :: HandleEvent( XMouseEvent * e)
  598. {
  599.    if( e->GetEventID() == MOU_BTN1CLICK )
  600.    {
  601.       XCalendarControl * win = (XCalendarControl*) GetWindow();
  602.       if( e->p.GetX() > 10 )
  603.       {
  604.          if(e->p.GetX() < 23 )
  605.          {
  606.               SHORT sel = (85 - e->p.GetY()) / 12 + 1 - win->date.GetWeekDay();
  607.             if(sel > 0)
  608.                win->DaySelected( sel );
  609.          }
  610.          else
  611.          {
  612.             SHORT x = (((e->p.GetX() - 23 ) / 18) + 2) * 7;
  613.             SHORT y = (85 - e->p.GetY()) / 12 + 1;
  614.             SHORT days = x + y - 7 - win->date.GetWeekDay();
  615.             if(days <= win->date.GetDaysOfMonth())
  616.                win->DaySelected( days );
  617.          }
  618.       }
  619.       return TRUE;
  620.    }
  621.    return FALSE;
  622. }
  623.  
  624.  
  625. XCalendarControl :: ~XCalendarControl()
  626. {
  627.    for(int i = 0; i < count; i++)
  628.       delete dayList[i];
  629.    free(dayList);
  630.    delete dev;
  631. }
  632.  
  633.  
  634. XCalendarControl :: XCalendarControl( const XWindow * w, const XPoint& p, const USHORT id, const XDate& startDate):XUserWindow(w->GetHandle())
  635. {
  636.    WinSetWindowUShort(winhandle, QWS_ID, id);
  637.    dev = new XGraphicDevice(this);
  638.  
  639.    XRect rect( p.GetX(), p.GetY(), 120, 85);
  640.    SetSize(&rect);
  641.  
  642.    dev->SetWidth(120);
  643.    dev->SetHeight(85);
  644.  
  645.    XColor col(COL_PALEGRAY);
  646.    dev->SetBackgroundColor( &col );
  647.  
  648.    XRect p0( 5, 0, 18, 12);
  649.  
  650.    p0.SetY( 73 - startDate.GetWeekDay() * 12);
  651.    date = startDate;
  652.    date.SetDays( 1 );
  653.  
  654.    XFont * font = new XFont( dev, "Helv", 7);
  655.    XColor colRed( COL_RED );
  656.  
  657.    dayList = (XText**) malloc( 31 * sizeof(void*));
  658.    count = startDate.GetDaysOfMonth() +1;
  659.    for(int i = 0; i < count; i++)
  660.    {
  661.       XString string = (LONG) i+1;
  662.       if(i > 0)
  663.       {
  664.          if( date.GetWeekDay() == 0 )
  665.          {
  666.             p0.SetY( 73 );
  667.             p0.SetX( p0.GetX() + 18);
  668.          }
  669.          else
  670.             p0.SetY( p0.GetY() - 12);
  671.       }
  672.  
  673.       dayList[i] = new XText( font, &p0, (char*) string, DT_RIGHT);
  674.       if(date.GetWeekDay() == 6 || date.IsCeremon())
  675.          dayList[i]->SetColor( &colRed );
  676.  
  677.       date.AddDays(1);
  678.    }
  679.  
  680.    date = startDate;
  681.    CalendarHandler * h = new CalendarHandler(this);
  682.  
  683.    Draw();
  684. }
  685.  
  686.  
  687. void XCalendarControl :: Draw( void )
  688. {
  689.    dev->FillBackground();
  690.    for(int i = 0; i < count; i++)
  691.       dayList[i]->Draw(dev);
  692. }
  693.  
  694.  
  695. XText * XCalendarControl :: GetDayObject(const UCHAR day) const
  696. {
  697.    if( day > 0 && day <= date.GetDaysOfMonth())
  698.       return dayList[day-1];
  699.    else
  700.       return NULL;
  701. }
  702.