home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TOOLOW.ZIP / JTOOLS.CPP < prev    next >
C/C++ Source or Header  |  1992-03-01  |  7KB  |  275 lines

  1. #include <owl.h>
  2. #include <string.h>
  3. #include "jtools.hpp"
  4.  
  5. TToolItem::TToolItem(PTWindowsObject AParent, Pchar Type, int id, int X,int Y,int W,int H, Pchar pBitmap1,
  6. Pchar pBitmap2, Pchar Shadow, Pchar Border )
  7. {
  8.     nID = id;
  9.     hBitmap1 = LoadBitmap(AParent->GetApplication()->hInstance, pBitmap1);
  10.     hBitmap2 = LoadBitmap(AParent->GetApplication()->hInstance, pBitmap2);
  11.     
  12.     rect.left = X;
  13.     rect.top = Y;
  14.     rect.right = X + W;
  15.     rect.bottom = Y + H;
  16.     bState = FALSE;
  17.     bEnabled = TRUE;
  18.  
  19.     if ( *Shadow == 'Y' )
  20.         bShadow = TRUE;
  21.     else
  22.         bShadow = FALSE;
  23.  
  24.     if ( *Border == 'Y' )
  25.         bBorder = TRUE;
  26.     else
  27.         bBorder = FALSE;
  28.  
  29.     if ( !stricmp(Type, "Button") )
  30.         bButton = TRUE;
  31.     else
  32.         bButton = FALSE;
  33. }
  34. TToolItem::~TToolItem()
  35. {
  36.     if ( hBitmap1 ) 
  37.         DeleteObject(hBitmap1);
  38.  
  39.     if ( hBitmap2 ) 
  40.         DeleteObject(hBitmap2);
  41. }
  42. void TToolItem::Show(HDC PaintDC, HBRUSH hButtonBrush, HPEN hShadowPen)
  43. {
  44.     HDC MemoryDC;
  45.     HANDLE OldBitmapHandle;
  46.     DWORD dwMode; 
  47.  
  48.     HPEN hOldPen = SelectObject(PaintDC, GetStockObject(BLACK_PEN));
  49.     HBRUSH hOldBrush = SelectObject(PaintDC, hButtonBrush);
  50.  
  51.     int    nOffset = 0, nShift = 0;
  52.  
  53.     if ( bBorder )
  54.         nOffset++;
  55.     if ( bShadow )
  56.         nOffset++;
  57.     if ( bState && bShadow)
  58.         nShift=1;
  59.  
  60.     if (bEnabled)
  61.         dwMode = SRCCOPY;
  62.     else
  63.         dwMode = MERGECOPY;
  64.  
  65.     if (bBorder)
  66.         Rectangle(PaintDC, rect.left, rect.top, rect.right, rect.bottom);
  67.     else
  68.       FillRect(PaintDC, &rect, hButtonBrush);
  69.  
  70.     if ( !hBitmap1 )
  71.         return;
  72.  
  73.     MemoryDC = CreateCompatibleDC(PaintDC);
  74.     if (bState && hBitmap2)
  75.       OldBitmapHandle = SelectObject(MemoryDC, hBitmap2);
  76.     else
  77.       OldBitmapHandle = SelectObject(MemoryDC, hBitmap1);
  78.     BitBlt(PaintDC, rect.left+nOffset+nShift, rect.top+nOffset+nShift, rect.right-rect.left, rect.bottom-rect.top,
  79.             MemoryDC, 0, 0, dwMode);
  80.  
  81.     SelectObject(MemoryDC, OldBitmapHandle);
  82.     DeleteDC(MemoryDC);
  83.  
  84.     if (bShadow)
  85.     {
  86.         if (bState)
  87.              SelectObject(PaintDC, hShadowPen);
  88.         else
  89.              SelectObject(PaintDC, GetStockObject(WHITE_PEN));
  90.          MoveTo(PaintDC, rect.left+nOffset-1, rect.bottom-nOffset);
  91.          LineTo(PaintDC, rect.left+nOffset-1, rect.top+nOffset-1);
  92.          LineTo(PaintDC, rect.right-nOffset+1, rect.top+nOffset-1);
  93.     
  94.         if (!bState)
  95.         {
  96.              SelectObject(PaintDC, hShadowPen);
  97.              MoveTo(PaintDC, rect.right-nOffset, rect.top+nOffset-1);
  98.              LineTo(PaintDC, rect.right-nOffset, rect.bottom-nOffset);
  99.              LineTo(PaintDC, rect.left+nOffset-2, rect.bottom-nOffset);
  100.              MoveTo(PaintDC, rect.right-nOffset-1, rect.top+nOffset);
  101.              LineTo(PaintDC, rect.right-nOffset-1, rect.bottom-nOffset-1);
  102.              LineTo(PaintDC, rect.left+nOffset-1, rect.bottom-nOffset-1);
  103.         }
  104.      
  105.     }
  106.  
  107.     SelectObject(PaintDC, hOldPen);
  108.     SelectObject(PaintDC, hOldBrush);
  109. }
  110.  
  111. int TToolItem::HitTest(int nX,int nY)
  112. {
  113.     POINT pt;
  114.     pt.x = nX, pt.y = nY;
  115.  
  116.     if (!bEnabled)            // Allow hits for Enabled Tools
  117.         return FALSE;
  118.  
  119. #pragma warn -stv
  120.     return PtInRect(&rect, pt);
  121. #pragma warn .stv
  122. }
  123.  
  124. TToolBar::TToolBar(PTWindowsObject AParent, int nHeight) :
  125.    TWindow(AParent, "")
  126. {
  127.  
  128.     bButtonDown = FALSE;
  129.     hShadowPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
  130.     hButtonBrush = GetStockObject(LTGRAY_BRUSH);
  131.     SelToolItem = NULL;
  132.  
  133.     Attr.X = 0;
  134.     Attr.Y = 0;
  135.     Attr.W = GetSystemMetrics(SM_CXSCREEN);     // Default to largest possible
  136.     Attr.H = nHeight;
  137.     Attr.Style = WS_CHILD|WS_VISIBLE;
  138.  
  139.  
  140.     ToolItems = new Array(5, 0, 5);
  141. }
  142. TToolBar::~TToolBar()
  143. {
  144.     delete ToolItems;
  145.     DeleteObject(hShadowPen);
  146. }
  147. void TToolBar::GetWindowClass(WNDCLASS& AWndClass)
  148. {
  149.       TWindow::GetWindowClass(AWndClass);
  150.     AWndClass.hbrBackground = hButtonBrush;
  151. }
  152. void TToolBar::Paint(HDC DC, PAINTSTRUCT& ) 
  153. {
  154.     RECT     rcWin;
  155.   
  156.       GetClientRect( HWindow, (LPRECT)&rcWin );
  157.  
  158.     HPEN hOldPen = SelectObject(DC, GetStockObject(BLACK_PEN));
  159.     MoveTo(DC, 0, rcWin.bottom-1); LineTo(DC, rcWin.right, rcWin.bottom-1);
  160.  
  161.     SelectObject(DC, GetStockObject(WHITE_PEN));
  162.     MoveTo(DC, 0, 0); LineTo(DC, rcWin.right, 0);
  163.  
  164.     SelectObject(DC, hShadowPen);
  165.     MoveTo(DC, 0, rcWin.bottom-2); LineTo(DC, rcWin.right, rcWin.bottom-2);
  166.  
  167.     SelectObject(DC, hOldPen);
  168.  
  169.     RArrayIterator ToolIterator = (RArrayIterator)(ToolItems->initIterator());
  170.     while ( int(ToolIterator) != 0 )
  171.     {
  172.           RObject AnObject = ToolIterator++;
  173.         if ( AnObject != NOOBJECT )
  174.             ((PTToolItem)(&AnObject))->Show(DC, hButtonBrush, hShadowPen);
  175.       }
  176.     delete &ToolIterator;
  177. }
  178.  
  179. void TToolBar::WMLButtonDown(RTMessage Msg)
  180. {
  181.     SelToolItem = NULL;
  182.  
  183.     RArrayIterator ToolIterator = (RArrayIterator)(ToolItems->initIterator());
  184.     while ( int(ToolIterator) != 0 )
  185.       {
  186.           RObject AnObject = ToolIterator++;
  187.           if ( AnObject != NOOBJECT )
  188.         {
  189.             PTToolItem pToolItem = (PTToolItem)(&AnObject);
  190.             if (pToolItem->HitTest(Msg.LP.Lo, Msg.LP.Hi))
  191.             {
  192.                 SelToolItem = pToolItem;
  193.                 SelToolItem->SetState( !SelToolItem->GetState() );
  194.                 HDC DC = GetDC(HWindow);
  195.                 SelToolItem->Show(DC, hButtonBrush, hShadowPen);
  196.                 ReleaseDC(HWindow, DC);
  197.                 
  198.                 if ( !SelToolItem->bButton )    // Tell Toolbar the CheckBox has been set
  199.                     PostMessage(HWindow, WM_COMMAND, SelToolItem->GetID(), 0);    
  200.                 break;
  201.             }
  202.         }
  203.       }
  204.     delete &ToolIterator;
  205.     bButtonDown = TRUE;
  206.     SetCapture(HWindow);
  207. }
  208. void TToolBar::WMMouseMove(RTMessage Msg)
  209. {
  210.     if ( bButtonDown && SelToolItem && SelToolItem->bButton )
  211.     {
  212.         if ( SelToolItem->HitTest(Msg.LP.Lo, Msg.LP.Hi) != SelToolItem->GetState())
  213.         {
  214.             SelToolItem->SetState( !SelToolItem->GetState() );
  215.             HDC DC = GetDC(HWindow);
  216.             SelToolItem->Show(DC, hButtonBrush, hShadowPen);
  217.             ReleaseDC(HWindow, DC);
  218.         }
  219.     }
  220. }
  221. void TToolBar::WMLButtonUp(RTMessage Msg)
  222. {
  223.     RArrayIterator ToolIterator = (RArrayIterator)(ToolItems->initIterator());
  224.     while ( int(ToolIterator) != 0 )
  225.     {
  226.           RObject AnObject = ToolIterator++;
  227.           if ( AnObject != NOOBJECT )
  228.         {
  229.             PTToolItem pToolItem = (PTToolItem)(&AnObject);
  230.             if (pToolItem->HitTest(Msg.LP.Lo, Msg.LP.Hi) && pToolItem->GetState())
  231.             {
  232.                 if ( pToolItem->bButton )
  233.                 {
  234.                     pToolItem->SetState( !pToolItem->GetState() );
  235.                     HDC DC = GetDC(HWindow);
  236.                     pToolItem->Show(DC, hButtonBrush, hShadowPen);
  237.                     ReleaseDC(HWindow, DC);
  238.  
  239.                     PostMessage(HWindow, WM_COMMAND,pToolItem->GetID(), 0);    // Tell Toolbar the button has been set
  240.                 }
  241.             }
  242.             else
  243.             {
  244.                 if ( pToolItem->bButton && pToolItem->GetState())
  245.                 {
  246.                     pToolItem->SetState( !pToolItem->GetState() );
  247.                     HDC DC = GetDC(HWindow);
  248.                     pToolItem->Show(DC, hButtonBrush, hShadowPen);
  249.                     ReleaseDC(HWindow, DC);
  250.                 }
  251.             }
  252.         }
  253.   }
  254.   delete &ToolIterator;
  255.  
  256.   bButtonDown = FALSE;
  257.     ReleaseCapture();
  258. }
  259. void TToolBar::SetItemState(int ID, int bState)
  260. {
  261.     RArrayIterator ToolIterator = (RArrayIterator)(ToolItems->initIterator());
  262.     while ( int(ToolIterator) != 0 )
  263.     {
  264.         RObject AnObject = ToolIterator++;
  265.         if ( AnObject != NOOBJECT )
  266.             if(((PTToolItem)(&AnObject))->GetID() == ID)
  267.             {
  268.                 ((PTToolItem)(&AnObject))->SetState(bState);
  269.                 break;
  270.             }
  271.     }
  272.     delete &ToolIterator;
  273. }
  274.  
  275.