home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xuserbtn.cpp < prev    next >
C/C++ Source or Header  |  1998-03-15  |  8KB  |  349 lines

  1. #include "xuserbtn.h"
  2. #include "xdefhdl.h"
  3. #include "xmoushdl.h"
  4. #include "xmousevn.h"
  5. #include "xbitmap.h"
  6. #include "XLine.h"
  7. #include "xbox.h"
  8. #include "xfont.h"
  9.  
  10.  
  11. class userbtnhandler:public XDefaultHandler
  12. {
  13.    public:
  14.       userbtnhandler(XWindow * w):XDefaultHandler(w) {;}
  15.       BOOL HandleEvent(ULONG, void *, void *, LONG*);
  16. };
  17.  
  18.  
  19. class userbtnmousehandler:public XMouseHandler
  20. {
  21.    public:
  22.       userbtnmousehandler(XWindow * w):XMouseHandler(w) {;}
  23.       BOOL HandleEvent(XMouseEvent *);
  24. };
  25.  
  26.  
  27. /*@
  28. @class XUserButton
  29. @parent XSettingButton
  30. @type overview
  31. @symbol _
  32. */
  33.  
  34.  
  35. /*@ XUserButton::XUserButton( const XWindow * w, const XRect& r, const LONG id, const LONG st, const char * t, const SHORT yOff)
  36. @group constructors/destructors
  37. @remarks Constructs a userbuttom
  38. @parameters <t '°' c=2>
  39.             °XWindow * owner      °the owner
  40.             °XRect& rect         °the rectangle
  41.             °USHORT id            °id of the window
  42.             °ULONG style          °style, valid values are:
  43. <BR>BU_HELP
  44. <BR>BU_DEFAULT
  45. <BR>BU_NOPOINTERFOCUS
  46. <BR>BU_NOCURSORSELECT
  47. <BR>BU_NOBORDER
  48. <BR>BU_TWOSTATE
  49. <BR>
  50. (can be or-ed).
  51.             °const char * string  °text to display (default is NULL)
  52.             °SHORT yOffset        °an y-offset for the bitmap (useful if you use bimaps and text, default is NULL)
  53.             </t>
  54. */
  55. XUserButton :: XUserButton(const XWindow * w, const XRect& r, const LONG id, const LONG st, const char *t, const SHORT yOff):XSettingButton(w, &r, id, st | BS_PUSHBUTTON | BS_USERBUTTON, (const char *) "", (unsigned char *) WC_BUTTON)
  56. {
  57.    graph = NULL;
  58.    bmp[0] = bmp[1] = NULL;
  59.    isPressed = 0;
  60.    style = st;
  61.    graph = new XGraphicDevice(this, FALSE, TRUE, PU_PELS, GPIF_LONG | GPIA_ASSOC);
  62.    XColor colPaleGray(COL_PALEGRAY);
  63.  
  64.    graph->SetBackgroundColor(&colPaleGray);
  65.    bmp[0] = new XBitmap();
  66.    bmp[1] = new XBitmap();
  67.  
  68.    XPoint p1(1, 2), p2(1, 0), p3, p4(1, 2), pa(0, 0), pb;
  69.  
  70.    p2.SetY(r.GetHeight() - 1);
  71.    p3.SetY(p2.GetY());
  72.    p3.SetX(r.GetWidth() - 1);
  73.    p4.SetX(p3.GetX());
  74.    pb.SetX(r.GetWidth());
  75.    pb.SetY(r.GetHeight());
  76.    XFont *font = new XFont(graph, "Helvetica", 9);
  77.  
  78.    XRect rect;
  79.  
  80.    GetSize(&rect);
  81.    rect.SetX(2);
  82.    rect.SetY(2);
  83.    rect.SetWidth(rect.GetWidth() - 2);
  84.    rect.SetHeight(rect.GetHeight() - 2);
  85.    text = new XText(font, &rect, t, DT_BOTTOM | DT_CENTER);
  86.  
  87.    if (style & BU_TWOSTATE)
  88.       userbtnmousehandler *m = new userbtnmousehandler(this);
  89.  
  90.    else
  91.       userbtnhandler * u = new userbtnhandler(this);
  92. }
  93.  
  94.  
  95. void XUserButton::GetText(XString * buffer) const
  96. {
  97.    text->GetText(buffer);
  98. }
  99.  
  100.  
  101. void XUserButton::SetText(const char *t)
  102. {
  103.    text->SetText(t);
  104. }
  105.  
  106. //#include "XProcess.h"
  107. XUserButton :: ~XUserButton()
  108. {
  109.    delete bmp[0];
  110.    delete bmp[1];
  111.    WinSetWindowULong(winhandle, QWL_USER, 0);
  112.    delete graph;
  113. }
  114.  
  115.  
  116. void XUserButton::Draw(void)
  117. {
  118.    if (graph)
  119.    {
  120.       graph->FillBackground();
  121.       bmp[0]->Draw(graph);
  122.       bmp[1]->Draw(graph);
  123.       text->Draw(graph);
  124.    } /* end if */
  125. //      graph->Draw();
  126.    RECTL rect;
  127.    WinQueryWindowRect(winhandle, &rect);
  128.  
  129.    if (!(style & BS_NOBORDER)) {
  130.       HPS hps = graph->GetHPS();
  131.       if(isPressed)
  132.          WinDrawBorder(hps, &rect, 2,2,0,0,DB_PATCOPY|DB_DEPRESSED);
  133.       else
  134.          WinDrawBorder(hps, &rect, 2,2,0,0,DB_PATCOPY|DB_RAISED);
  135.    }
  136. }
  137.  
  138.  
  139. /*@ XUserButton :: IsSelected( void )
  140. @group misc
  141. @remarks Query if the button is selected or not
  142. @returns BOOL selection
  143. */
  144. BOOL XUserButton::IsSelected(void) const
  145. {
  146.    if (!(style & BU_TWOSTATE))
  147.       return XSettingButton::IsSelected();
  148.    else
  149.       return isPressed;
  150. }
  151.  
  152.  
  153. /*@ XUserButton::Toggle(void)
  154. @group misc
  155. @remarks Toggles the button
  156. */
  157. void XUserButton::Toggle(void)
  158. {
  159.    if (!(style & BU_TWOSTATE))
  160.    {
  161.       BOOL res = (XSettingButton::IsSelected() ? FALSE : TRUE);
  162.       XSettingButton::Select(res);
  163.    }
  164.    else
  165.    {
  166.       isPressed = (isPressed ? FALSE : TRUE);
  167.       Invalidate();
  168.    }
  169. }
  170.  
  171.  
  172. /*@ XUserButton :: Select( const BOOL sel )
  173. @group misc
  174. @remarks Select te button
  175. @parameters BOOL selection      TRUE=select, FALSE=unselect
  176. */
  177. void XUserButton::Select( const BOOL sel)
  178. {
  179.    if (!(style & BU_TWOSTATE))
  180.       XSettingButton::Select(sel);
  181.    else
  182.    {
  183.       isPressed = sel;
  184.       Invalidate();
  185.    }
  186. }
  187.  
  188.  
  189. /*@ XUserButton::GetBitmapPointer()
  190. @group style
  191. @remarks Returns a pointer to one of two used bitmaps.
  192. @parameters   <t '°' c=2>
  193.             °UCHAR      index      °index of the bitmap:
  194. <BR>
  195.                               0 = the bitmap to display if the button is not pressed
  196. <BR>
  197.                               1 = the bitmap to display if the button is pressed
  198.             </t>
  199. @returns   XBitmap * bmp         the requested bitmap
  200. */
  201.  
  202.  
  203. /*@ XUserButton::GetGraphPointer()
  204. @group style
  205. @remarks Returns a pointer to the graphic-device of the button.
  206. @returns   XGraphicDevice * dev         the graphic device
  207. */
  208.  
  209.  
  210. /*@ XUserButton::GetTextPointer()
  211. @group style
  212. @remarks Returns a pointer to used text-object of the button.
  213. @returns   XText * text         the text object
  214. */
  215.  
  216.  
  217. /*@ XUserButton::SetBitmap()
  218. @group style
  219. @remarks Set a bitmap to the button. The bitmap is copied so the destructor of the bitmap given in
  220. the argument can be called when this functions returns.
  221. @parameters   <t '°' c=2>
  222.             °XBitmap * bmp      °the bitmap
  223.             °UCHAR      index      °index of the bitmap:
  224. <BR>
  225.                               0 = the bitmap to display if the button is not pressed
  226. <BR>
  227.                               1 = the bitmap to display if the button is pressed
  228.             /t>
  229. */
  230. void XUserButton::SetBitmap(const XBitmap * b, const UCHAR index)
  231. {
  232.    if (index > 1)
  233.       return;
  234.    *bmp[index] = *b;
  235.    XPoint p(1, 1);
  236.  
  237.    bmp[index]->Move(&p);
  238.  
  239.    if (index == 1)
  240.    {
  241.       bmp[1]->Show(FALSE);
  242.       bmp[0]->Show(TRUE);
  243.    }
  244.    Invalidate();
  245. }
  246.  
  247.  
  248. void XUserButton::Setup(void)
  249. {
  250. /*
  251.    XColor colWhite(COL_WHITE);
  252.    XColor colGray(COL_DARKGRAY);
  253. */
  254.    if (isPressed)
  255.    {
  256.       XPoint p;
  257.  
  258.       text->GetPos(&p);
  259.       p.SetX(p.GetX() + 1);
  260.       p.SetY(p.GetY() - 1);
  261.       text->Move(&p);
  262.  
  263.       if (bmp[1]->cx == 0)
  264.       {
  265.          bmp[0]->GetPos(&p);
  266.          p.SetX(p.GetX() + 1);
  267.          p.SetY(p.GetY() - 1);
  268.          bmp[0]->Move(&p);
  269.       }
  270.       else
  271.       {
  272.          bmp[0]->Show(FALSE);
  273.          bmp[1]->Show();
  274.       }
  275. /*
  276.       line1->SetColor(&colGray);
  277.       line2->SetColor(&colGray);
  278.       line3->SetColor(&colWhite);
  279.       line4->SetColor(&colWhite);
  280. */
  281.    }
  282.    else
  283.    {
  284.       XPoint p;
  285.  
  286.       text->GetPos(&p);
  287.       p.SetX(p.GetX() - 1);
  288.       p.SetY(p.GetY() + 1);
  289.       text->Move(&p);
  290.  
  291.       if (bmp[1]->cx == 0)
  292.       {
  293.          bmp[0]->GetPos(&p);
  294.          p.SetX(p.GetX() - 1);
  295.          p.SetY(p.GetY() + 1);
  296.          bmp[0]->Move(&p);
  297.       }
  298.       else
  299.       {
  300.          bmp[0]->Show();
  301.          bmp[1]->Show(FALSE);
  302.       }
  303. /*
  304.       line1->SetColor(&colWhite);
  305.       line2->SetColor(&colWhite);
  306.       line3->SetColor(&colGray);
  307.       line4->SetColor(&colGray);
  308. */
  309.    }
  310.    Invalidate();
  311. }
  312.  
  313.  
  314. BOOL userbtnhandler::HandleEvent(ULONG msg, void *mp1, void *, LONG * retVal)
  315. {
  316.    if (msg == BM_SETHILITE)
  317.    {
  318.       XUserButton *b = (XUserButton *) GetWindow();
  319.  
  320.       b->isPressed = SHORT1FROMMP(mp1);
  321.       b->Setup();
  322.       b->Invalidate();
  323.    }
  324.    return FALSE;
  325. }
  326.  
  327.  
  328. BOOL userbtnmousehandler::HandleEvent(XMouseEvent * event)
  329. {
  330.    if (event->GetEventID() == MOU_BTN1UP)
  331.    {
  332.       XUserButton *b = (XUserButton *) GetWindow();
  333.  
  334.       b->isPressed = (b->buffer ? FALSE : TRUE);
  335.       if (b->isPressed != b->buffer)
  336.          b->Setup();
  337.    }
  338.    else if (event->GetEventID() == MOU_BTN1DOWN)
  339.    {
  340.       XUserButton *b = (XUserButton *) GetWindow();
  341.  
  342.       b->buffer = b->isPressed;
  343.       b->isPressed = (b->buffer ? TRUE : FALSE);
  344.       if (b->isPressed != b->buffer)
  345.          b->Setup();
  346.    }
  347.    return FALSE;
  348. }
  349.