home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / 3DTOSHI2.ZIP / mpgui / source / guitbar.cpp < prev    next >
C/C++ Source or Header  |  1996-04-17  |  14KB  |  457 lines

  1.  
  2. // guitbar.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "guitbar.h"
  8.  
  9. APIRESULT APIPROC ToolBarProc ( HWINDOW hWindow, MESSAGE iMessage,
  10.                                 PARAM1 Param1, PARAM2 Param2 );
  11.  
  12. //**************************************************
  13. //
  14. // GUI Tool Bar Class
  15. //
  16. //**************************************************
  17.  
  18. GUITOOLBAR::GUITOOLBAR () : GUICHILD ()
  19.   {
  20.     // Set default size
  21.     ImgWd = 16;
  22.     ImgHt = 15;
  23.     BarHt = ImgHt+5;
  24.  
  25.     hMemDisplay = NULL;
  26.     Selected = -1;
  27.     Buttons = NULL;
  28.     hBitmap = NULL;
  29.     NumButtons = 0;
  30.     strcpy ( ClassName, "GUITOOLBAR" );
  31.   } // End of Constructor for GUITOOLBAR
  32.  
  33. GUITOOLBAR::~GUITOOLBAR ()
  34.   {
  35.     DestroyBar ();
  36.     if (Buttons!=NULL)
  37.       delete Buttons;
  38.     Buttons = NULL;
  39.   } // End of Destructor for GUITOOLBAR
  40.  
  41. VOID GUITOOLBAR::SetButtonSize ( LONG Wd, LONG Ht )
  42.   {
  43.     ImgWd = Wd;
  44.     ImgHt = Ht;
  45.   } // End of SetButtonSize for GUITOOLBAR
  46.  
  47. VOID GUITOOLBAR::SetBarHeight ( LONG BarHeight )
  48.   {
  49.     BarHt = BarHeight;  
  50.   } // End of SetBarHeight for GUITOOLBAR
  51.  
  52. BOOLEAN GUITOOLBAR::Load ( STRING Name, LONG Num, BUTTONDATA *Data )
  53.   {
  54.     if (Name==NULL)
  55.       return FAILURE;  
  56.     #if defined (__FORWINDOWS__)
  57.       hBitmap = LoadBitmap ( hInstance, Name );
  58.     #else
  59.     #endif  
  60.     NumButtons = Num;
  61.     if (Buttons!=NULL)
  62.       delete Buttons;
  63.     Buttons = new BUTTONDATA [NumButtons];
  64.       
  65.     INT i;
  66.  
  67.     for (i=0;i<NumButtons;i++)
  68.       {
  69.         Buttons[i].x = Data[i].x;
  70.         Buttons[i].y = Data[i].y;
  71.         Buttons[i].Command = Data[i].Command;
  72.         Buttons[i].State = Data[i].State;
  73.       } // End for
  74.     return SUCCESS;
  75.   } // End of Load for GUITOOLBAR
  76.  
  77. VOID GUITOOLBAR::SetButtonAttr ( LONG Index, LONG Command )
  78.   {
  79.     if ((Index<0)||(Index>=NumButtons))
  80.       return;
  81.     Buttons[Index].Command = Command;  
  82.   } // End of SetButtonAttr for GUITOOLBAR
  83.  
  84. VOID GUITOOLBAR::RegisterWindow ()
  85.   {
  86.     #if defined (__FORWINDOWS__)
  87.       WNDCLASS WndClass;
  88.       WndClass.cbClsExtra = 0;
  89.       WndClass.cbWndExtra = 4;
  90.       WndClass.hbrBackground = GetStockObject ( LTGRAY_BRUSH );
  91.       WndClass.hCursor = LoadCursor ( NULL, IDC_ARROW );
  92.       WndClass.hIcon = LoadIcon ( NULL, "END" );
  93.       WndClass.hInstance = hInstance;
  94.       WndClass.lpfnWndProc = (WNDPROC)ToolBarProc;
  95.       WndClass.lpszClassName = "GUITOOLBAR";
  96.       WndClass.lpszMenuName = NULL;      
  97.       WndClass.style = CS_HREDRAW | CS_VREDRAW;
  98.  
  99.       RegisterClass ( &WndClass );
  100.     #else
  101.     #endif
  102.   } // End of RegisterWindow for GUITOOLBAR
  103.  
  104. BOOLEAN GUITOOLBAR::Create ( STRING Title, LONG x, LONG y, LONG Wd, LONG Ht,
  105.                              GUIWINDOW *Parent )
  106.   {
  107.     if (Title)
  108.       {}
  109.     if (x&y&Wd&Ht)
  110.       {}
  111.     if (Parent)
  112.       {}
  113.     return FAILURE;
  114.   } // End of Create for GUITOOLBAR
  115.  
  116. VOID GUITOOLBAR::CreateBar ()
  117.   {
  118.     DestroyBar ();
  119.     #if defined (__FORWINDOWS__)
  120.       HDISPLAY hDisplay = GetDisplay ( hClient );
  121.       hMemDisplay = CreateCompatibleDC ( hDisplay );
  122.       OldBitmap = SelectObject ( hMemDisplay, hBitmap );
  123.       ReleaseDisplay ( hClient, hDisplay );
  124.     #else
  125.     #endif
  126.   } // End of CreateBar for GUITOOLBAR
  127.  
  128. VOID GUITOOLBAR::DestroyBar ()
  129.   {
  130.     #if defined (__FORWINDOWS__)
  131.       if (hMemDisplay!=NULL)
  132.         {
  133.           SelectObject ( hMemDisplay, OldBitmap );
  134.           DeleteObject ( hBitmap );
  135.           DeleteDC ( hMemDisplay );
  136.           hMemDisplay = NULL;
  137.           hBitmap = NULL;
  138.         } // End if
  139.     #else
  140.     #endif
  141.   } // End of DestroyBar
  142.  
  143. BOOLEAN GUITOOLBAR::Create ( GUIWINDOW *Parent )
  144.   {
  145.     if (hBitmap==NULL)
  146.       return FAILURE;
  147.     if (Parent==NULL)
  148.      return FAILURE;
  149.  
  150.     HWINDOW hParent;
  151.     HWINDOW hNew;
  152.  
  153.     ParentWindow = Parent;
  154.     hParent = Parent->GetHandle ();
  155.     if (hParent==NULL)
  156.       return FAILURE;
  157.  
  158.     RegisterWindow ();
  159.  
  160.     if (BarHt<ImgHt+5)
  161.       BarHt = ImgHt+5;
  162.       
  163.     #if defined (__FORWINDOWS__)    
  164.       RECT r;
  165.       GetClientRect ( ParentWindow->GetHandle (), &r );
  166.       #if defined (__FORWIN386__)
  167.         hWindow = CreateWindow ( "GUITOOLBAR", "", WS_CHILD | WS_VISIBLE,
  168.                                  0, 0, r.right, (short)BarHt,
  169.                                  hParent, NULL, hInstance, NULL );
  170.       #else
  171.         hWindow = CreateWindow ( "GUITOOLBAR", "", WS_CHILD | WS_VISIBLE,
  172.                                  0, 0, r.right, BarHt,
  173.                                  hParent, NULL, hInstance, NULL );
  174.       #endif
  175.       Parent->SetToolBar ( hWindow, BarHt );                         
  176.       hClient = hWindow;
  177.       hNew = hWindow;
  178.       CreateBar ();
  179.     #else
  180.       if (hNew)
  181.         {}
  182.     #endif
  183.     AddWindow ( this, hNew );
  184.     return SUCCESS;
  185.   } // End of Create for GUITOOLBAR
  186.  
  187. LONG GUITOOLBAR::FindButton ( LONG Mx, LONG My )
  188.   {
  189.     INT i;
  190.     if (Buttons==NULL)
  191.       return -1;
  192.     if ((My<0)||(My>BarHt))
  193.       return -1;
  194.         
  195.     for (i=0;i<NumButtons;i++)
  196.       {
  197.         if ((Mx>=Buttons[i].x)&&(My>=Buttons[i].y)&&
  198.              (Mx<=Buttons[i].x+ImgWd+5)&&(My<=Buttons[i].y+ImgHt+5))
  199.           return i; 
  200.       } // End for
  201.     return -1;  
  202.   } // End of FindButton for GUITOOLBAR
  203.  
  204. LONG GUITOOLBAR::OnMouse ( LONG Event, LONG Flags, LONG Mx, LONG My )
  205.   {
  206.     LONG Index;
  207.     if (Flags)
  208.       {}  
  209.     switch (Event)
  210.       {
  211.         case GUI_WM_LBUTTONDOWN :
  212.         case GUI_WM_RBUTTONDOWN :
  213.           Index = FindButton ( Mx, My );
  214.           if (Index!=-1)
  215.             {
  216.               Buttons[Index].State = BUTTON_SELECT;
  217.               HDISPLAY hDisplay;
  218.               hDisplay = GetDisplay ( hClient );
  219.               DrawButton ( hDisplay, Index );
  220.               ReleaseDisplay ( hClient, hDisplay );
  221.               Selected = Index;
  222.             } // End if
  223.           break;  
  224.         case GUI_WM_LBUTTONUP :
  225.         case GUI_WM_RBUTTONUP :
  226.           if (Selected!=-1)
  227.             {
  228.               Buttons[Selected].State = BUTTON_UNSELECT;
  229.               HDISPLAY hDisplay;
  230.               hDisplay = GetDisplay ( hClient );
  231.               DrawButton ( hDisplay, Selected );
  232.               ReleaseDisplay ( hClient, hDisplay );
  233.               #if defined (__FORWINDOWS__)
  234.                 #if defined (__FORWIN386__)
  235.                   SendMessage ( ParentWindow->GetHandle (), GUI_WM_COMMAND,
  236.                                 (short)Buttons[Selected].Command, 0 );
  237.                 #else
  238.                   SendMessage ( ParentWindow->GetHandle (), GUI_WM_COMMAND,
  239.                                 Buttons[Selected].Command, 0 );
  240.                 #endif
  241.               #endif
  242.               Selected = -1;
  243.             } // End if
  244.           break;
  245.         case GUI_WM_MOUSEMOVE :
  246.           if (Selected!=-1)
  247.             {
  248.               Index = FindButton ( Mx, My );
  249.               if (Index!=Selected)
  250.                 { 
  251.                   Buttons[Selected].State = BUTTON_UNSELECT;
  252.                   HDISPLAY hDisplay;
  253.                   hDisplay = GetDisplay ( hClient );
  254.                   DrawButton ( hDisplay, Selected );
  255.                   ReleaseDisplay ( hClient, hDisplay );
  256.                   #if defined (__FORWINDOWS__)
  257.                     #if defined (__FORWIN386__)
  258.                       SendMessage ( ParentWindow->GetHandle (), GUI_WM_COMMAND,
  259.                                     (short)Buttons[Selected].Command, 0 );
  260.                     #else
  261.                       SendMessage ( ParentWindow->GetHandle (), GUI_WM_COMMAND,
  262.                                     Buttons[Selected].Command, 0 );
  263.                     #endif             
  264.                   #endif
  265.                   Selected = -1;
  266.                 } // End if  
  267.             } // End if
  268.           break;  
  269.       } // End switch
  270.     return 0;  
  271.   } // End of OnMouse for GUITOOLBAR
  272.  
  273. VOID GUITOOLBAR::DrawButton ( HDISPLAY hDisplay, LONG Index )
  274.   {
  275.     LONG x,y;
  276.     INT AddX,AddY;
  277.  
  278.     x = Buttons[Index].x;  
  279.     y = Buttons[Index].y;
  280.  
  281.     #if defined (__FORWINDOWS__)
  282.       Grafix.FGColor = RGB ( 0, 0, 0 );  
  283.     #endif
  284.     
  285.     Grafix.DrawLine ( hDisplay, x+1, y, x+ImgWd+4, y );
  286.     Grafix.DrawLine ( hDisplay, x+1, y+ImgHt+4, x+ImgWd+4, y+ImgHt+4 );
  287.     Grafix.DrawLine ( hDisplay, x, y+1, x, y+ImgHt+4 );
  288.     Grafix.DrawLine ( hDisplay, x+ImgWd+4, y+1, x+ImgWd+4, y+ImgHt+4 );
  289.     
  290.     if (Buttons[Index].State==BUTTON_SELECT)
  291.       {
  292.         AddX = 4;
  293.         AddY = 4;
  294.         #if defined (__FORWINDOWS__)
  295.           Grafix.FGColor = RGB ( 128, 128, 128 );
  296.         #endif
  297.         Grafix.DrawLine ( hDisplay, x+1, y+1, x+ImgWd+4, y+1 );
  298.         Grafix.DrawLine ( hDisplay, x+1, y+2, x+1, y+ImgHt+4 );
  299.         
  300.         Grafix.FGColor = RGB ( 192, 192, 192 );           
  301.         Grafix.DrawLine ( hDisplay, x+2, y+2, x+ImgWd+4, y+2 );
  302.         Grafix.DrawLine ( hDisplay, x+2, y+3, x+ImgWd+4, y+3 );
  303.         Grafix.DrawLine ( hDisplay, x+2, y+4, x+2, y+ImgHt+4 );
  304.         Grafix.DrawLine ( hDisplay, x+3, y+4, x+3, y+ImgHt+4 );
  305.       } // End if
  306.     else
  307.       {
  308.         AddX = 2;
  309.         AddY = 2;  
  310.         Grafix.FGColor = RGB ( 255, 255, 255 );           
  311.         Grafix.DrawLine ( hDisplay, x+1, y+1, x+ImgWd+3, y+1 );
  312.         Grafix.DrawLine ( hDisplay, x+1, y+1, x+1, y+ImgHt+3 );
  313.  
  314.         Grafix.FGColor = RGB ( 128, 128, 128 );
  315.         Grafix.DrawLine ( hDisplay, x+ImgWd+3, y+1, x+ImgWd+3, y+ImgHt+3 );
  316.         Grafix.DrawLine ( hDisplay, x+ImgWd+2, y+2, x+ImgWd+2, y+ImgHt+3 );
  317.         Grafix.DrawLine ( hDisplay, x+2, y+ImgHt+2, x+ImgWd+3, y+ImgHt+2 );
  318.         Grafix.DrawLine ( hDisplay, x+1, y+ImgHt+3, x+ImgWd+4, y+ImgHt+3 );          
  319.       } // End else    
  320.     #if defined (__FORWINDOWS__)
  321.       #if defined (__FORWIN386__)
  322.         BitBlt ( hDisplay, (short)(x+AddX), (short)(y+AddY), (short)ImgWd, (short)ImgHt,
  323.                  hMemDisplay, (short)(Index*ImgWd), 0, SRCCOPY );
  324.       #else
  325.         BitBlt ( hDisplay, x+AddX, y+AddY, ImgWd, ImgHt, hMemDisplay, Index*ImgWd, 0, SRCCOPY );
  326.       #endif
  327.     #else
  328.          #endif
  329.          if (AddX+AddY)
  330.            {}  
  331.   } // End of DrawButton for GUITOOLBAR
  332.   
  333. LONG GUITOOLBAR::OnPaint ( HDISPLAY hDisplay )
  334.   {
  335.     if (hDisplay)
  336.       {}
  337.     #if defined (__FORWINDOWS__)
  338.       INT i;
  339.       for (i=0;i<NumButtons;i++)
  340.         DrawButton ( hDisplay, i );
  341.     #else
  342.     #endif
  343.     return 0;
  344.   } // End of OnPaint for GUITOOLBAR
  345.  
  346. LONG GUITOOLBAR::WndProc ( HWINDOW hWnd, MESSAGE iMessage, PARAM1 Param1, PARAM2 Param2, BOOLEAN FromClient )
  347.   {
  348.     LONG Result;
  349.     hWindow = hWnd;
  350.     if (Param1)
  351.       {}
  352.     if (Param2)
  353.       {}
  354.     if (FromClient)
  355.       {}  
  356.     switch (iMessage)
  357.       {
  358.         case GUI_WM_CREATE :
  359.           Result = OnCreate ();
  360.           break;
  361.  
  362.         case GUI_WM_CLOSE :
  363.           Result = OnClose ();
  364.           break;
  365.           
  366.         case GUI_WM_DESTROY :
  367.           Result = OnDestroy ();
  368.           DiscardWindow ( hWindow );
  369.           break;
  370.  
  371.         case GUI_WM_PAINT :
  372.           #if defined (__FORWINDOWS__)
  373.             PAINTSTRUCT Ps;
  374.             HDISPLAY hDisplay;
  375.             hDisplay = BeginPaint ( hWindow, &Ps );                  
  376.             Result = OnPaint ( hDisplay );
  377.             EndPaint ( hWindow, &Ps );            
  378.           #elif defined (__FOROS2__)
  379.             HDISPLAY hDisplay;
  380.             hDisplay = WinBeginPaint ( hWindow, NULLHANDLE, NULL );
  381.             Result = OnPaint ( hDisplay );
  382.             WinEndPaint ( hDisplay );
  383.           #elif defined (__FORDOS__)
  384.             Result = OnPaint ( 0 );
  385.           #endif
  386.           break;
  387.  
  388.         case GUI_WM_MOUSEMOVE :
  389.         case GUI_WM_LBUTTONDOWN :
  390.         case GUI_WM_LBUTTONUP :
  391.         case GUI_WM_LBUTTONDBLCLK :
  392.         case GUI_WM_MBUTTONDOWN :
  393.         case GUI_WM_MBUTTONUP :
  394.         case GUI_WM_MBUTTONDBLCLK :
  395.         case GUI_WM_RBUTTONDOWN :
  396.         case GUI_WM_RBUTTONUP :
  397.         case GUI_WM_RBUTTONDBLCLK :
  398.           LONG Mx, My;
  399.           LONG Flags;
  400.           #if defined (__FORWINDOWS__)
  401.             Flags = Param1;
  402.             Mx = LOWORD ( Param2 );
  403.             My = HIWORD ( Param2 );
  404.             Result = OnMouse ( iMessage, Flags, Mx, My );
  405.           #elif defined (__FOROS2__)
  406.             if (Mx&My&Flags)
  407.               {}
  408.           #elif defined (__FORDOS__)
  409.             if (Mx&My&Flags)
  410.               {}
  411.           #endif
  412.           break;
  413.  
  414.         default :
  415.           Result = UserProc ( iMessage, Param1, Param2 );
  416.           break;
  417.       } // End switch
  418.     return Result;
  419.   } // End of WndProc for GUITOOLBAR
  420.  
  421. //**************************************************
  422. //
  423. // Tool Bar Procedure
  424. //
  425. //**************************************************
  426.  
  427. APIRESULT APIPROC ToolBarProc ( HWINDOW hWindow, MESSAGE iMessage,
  428.                                 PARAM1 Param1, PARAM2 Param2 )
  429.   {
  430.     GUIWINDOW *WinPtr;
  431.     LONG Result;
  432.  
  433.     WinPtr = FindWindow ( hWindow );
  434.  
  435.     Result = 0;
  436.     if (WinPtr!=NULL)
  437.       {
  438.         Result = WinPtr->WndProc ( hWindow, iMessage,
  439.                                    Param1, Param2, TRUE );
  440.       } // End if
  441.  
  442.     if (Result!=0)
  443.       return 0;
  444.  
  445.     #if defined (__FORWINDOWS__)
  446.       return DefWindowProc ( hWindow, iMessage, Param1, Param2 );
  447.  
  448.     #elif defined (__FOROS2__)
  449.       return WinDefWindowProc ( hWindow, iMessage, Param1, Param2 );
  450.  
  451.     #elif defined (__FORDOS__)
  452.       if (hWindow&iMessage&Param1&Param2)
  453.         {}
  454.       return 0;
  455.     #endif
  456.   } // End of ToolBarProc
  457.