home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 4418 / DEMO / SOURCE.ZIP / GV_TBAR.CPP < prev    next >
C/C++ Source or Header  |  1993-04-22  |  31KB  |  789 lines

  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include "winx31ad.h"
  4.  
  5. #include <string.h>
  6. #include "GV_TBAR.h"
  7.  
  8. #define GETLPTBINFOHEADER(hwnd) ((LPTBINFOHEADER) GetWindowLong(hwnd,0))
  9.  
  10. // sequence /***+++***/ mark new WM_MENUITEM PostMessaging code
  11. // plus message WM_CTLCOLORSTATIC renvoye
  12. typedef struct
  13. {
  14.   BOOL     fButton;     // Button or Window
  15.   UINT     cxBefore;
  16.   UINT     cx;
  17.   UINT     cxAfter;
  18.   UINT     cy;
  19. // for Button :
  20.   WORD     wId  ;       // ID of Button ou WIndow for Internal TB
  21.   WORD     wIdFirst;
  22.   WORD     wIdLast;
  23.   BYTE     bStyle;      //  ACTION / RADIO / CHECK
  24.   WORD     nFirst;      // index to first toggle btn
  25.   WORD     nLast;       // index to last  toggle btn
  26.   BYTE     bState;      // button state BTNS_RAISED, BTNS_PUSHED, BTNS_INVALID
  27.   WORD     wPosBitmap;  //
  28. // for Window :
  29.   HWND     hWnd;        // Handle of the Windows (child of toolbar)
  30. // private :
  31.   RECT          Emplact;
  32. } TBELEM;
  33. typedef TBELEM FAR* LPTBELEM;
  34.  
  35.  
  36. typedef struct
  37. {
  38. HWND     hwndParent;
  39. HWND     hwndTB;
  40. HBITMAP  hPictBtn;
  41. WORD     wNbElem ; //  Number of Btn or Window in TB
  42. UINT     wLastx;
  43. UINT     wFirstx,wFirsty;
  44. LPTBELEM lpTbFirst;
  45. LPTBELEM lpTbInPush;
  46. BYTE     bOldStateInPush;
  47. BYTE     bStateInPush;
  48. BOOL     fMouseOnButton;
  49. } TBINFOHEADER;
  50. typedef TBINFOHEADER FAR* LPTBINFOHEADER;
  51.  
  52.  
  53. char szClassName[] = "classToolBar";
  54.  
  55. // Function prototyping
  56. static BOOL AddBtnIndirect (HWND hWndTB,LPTBELEM lpBtnCreateStruct);
  57. //BOOL SetBtnState (HWND hWndTB,WORD wId,BYTE bState);
  58. //BOOL DelTbElem (HWND hWndTB,WORD wId);
  59. //BOOL DeleteTB (HWND hWndTB,WORD wId);
  60. LRESULT CALLBACK ToolbarWndProc (HWND hwnd, UINT uiMsg,
  61.                                    WPARAM wParam, LPARAM lParam);
  62.  
  63. // --------------------------------------------------------------------------
  64. //  FUNCTION    RegisterTBClass
  65. //  PURPOSE     Register the class of toolbar window
  66. // --------------------------------------------------------------------------
  67. //  INPUT       hInst           Handle of instace
  68. //  OUTPUT      TRUE if registering is OK
  69. // --------------------------------------------------------------------------
  70. //  COMMENTS    ...
  71. // --------------------------------------------------------------------------
  72. BOOL RegisterTBClass (HINSTANCE hInst)
  73. {
  74.     WNDCLASS  wc;
  75.  
  76.     wc.style         = 0;
  77.     wc.lpfnWndProc   = ToolbarWndProc ;
  78.     wc.cbClsExtra    = 0 ;
  79.     wc.cbWndExtra    = sizeof(LPTBINFOHEADER) ;
  80.     wc.hInstance     = hInst ;
  81.     wc.hIcon         = NULL;
  82.     wc.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  83.     wc.hCursor       = NULL ;
  84.     wc.hbrBackground = GetStockObject ( LTGRAY_BRUSH );
  85.     wc.lpszMenuName  = (LPSTR) NULL ;
  86.     wc.lpszClassName = szClassName ;
  87.  
  88.     if (!RegisterClass (&wc))
  89.         return (FALSE);
  90.  
  91.     return TRUE;
  92. }
  93.  
  94. // --------------------------------------------------------------------------
  95. //  FUNCTION    UnregTBClass
  96. //  PURPOSE     Unregister the class of toolbar window
  97. // --------------------------------------------------------------------------
  98. //  INPUT       hInst           Handle of instace
  99. //  OUTPUT      TRUE if unregistering is OK
  100. // --------------------------------------------------------------------------
  101. //  COMMENTS    ...
  102. // --------------------------------------------------------------------------
  103. BOOL UnregTBClass (HINSTANCE hInst)
  104. {
  105.   return UnregisterClass(szClassName,hInst);
  106. }
  107.  
  108. // --------------------------------------------------------------------------
  109. //  FUNCTION    CreateTB
  110. //  PURPOSE     Create the ToolBar window
  111. // --------------------------------------------------------------------------
  112. //  INPUT       hWndParent      the handle of parent Toolbar
  113. //              hInst           Handle of instace
  114. //              hPictBtn        the handle of bitmap which contain Button
  115. //              rect            the position of the ntoolbar in parent window
  116. //  OUTPUT      the Handle of ToolBar window
  117. // --------------------------------------------------------------------------
  118. //  COMMENTS    The Toolbar class must be registered with RegisterTBClass
  119. //                 before calling CreateTB
  120. // --------------------------------------------------------------------------
  121. HWND CreateTB (HWND hWndParent,HINSTANCE hInst,HBITMAP hPictBtn,RECT rect)
  122. {
  123. HWND hWndTB =
  124.         CreateWindow (szClassName, "ControlBar",
  125.                                WS_CHILD,rect.left,rect.top,
  126.                                rect.right-rect.left,rect.bottom-rect.top,
  127.                                hWndParent,NULL, hInst, NULL) ;
  128.      SetBitmap (hWndTB,hPictBtn);
  129.      ShowWindow(hWndTB,SW_SHOW);
  130.  
  131.      return hWndTB;
  132. }
  133.  
  134.  
  135. static BOOL AddBtnIndirect (HWND hWndTB,LPTBELEM lpBtnCreateStruct) ;
  136.  
  137. // --------------------------------------------------------------------------
  138. //  FUNCTION    AddBtn
  139. //  PURPOSE     Add a button on a toolbar
  140. // --------------------------------------------------------------------------
  141. //  INPUT       hWndTB          the handle of ToolBar window
  142. //              wId             IDentifier of the button to create
  143. //              wPosBitmap      Index of the bitmap of the button in hPictBtn
  144. //              bStyle          BTSY_xxx (see in the end of TOOLBAR.H)
  145. //              bState          BTNS_xxx : initial state of the button
  146. //              cxBefore        Space before button
  147. //              cxAfter         Space after button
  148. //              wIdFirst,wIdLast id of the first and last button of a group
  149. //                                      (if bStyle = BSTY_GROUPTOOGLE[OPT])
  150. //  OUTPUT      TRUE if the button is added
  151. // --------------------------------------------------------------------------
  152. //  COMMENTS    ...
  153. // --------------------------------------------------------------------------
  154. BOOL AddBtn (HWND hWndTB,WORD wId,WORD wPosBitmap,BYTE bStyle,BYTE bState,
  155.                    WORD cxBefore,WORD cxAfter,WORD wIdFirst,WORD wIdLast)
  156. {
  157. TBELEM BtnCreateStruct;
  158.     BtnCreateStruct.fButton=TRUE;
  159.     BtnCreateStruct.cxBefore=cxBefore;
  160.     BtnCreateStruct.cxAfter=cxAfter;
  161.     BtnCreateStruct.cx=CXTBBUTTON-1;
  162.     BtnCreateStruct.cy=CYTBBUTTON;
  163.     BtnCreateStruct.wId=wId;
  164.     BtnCreateStruct.wIdFirst=wIdFirst;
  165.     BtnCreateStruct.wIdLast=wIdLast;
  166.     BtnCreateStruct.wPosBitmap=wPosBitmap;
  167.     BtnCreateStruct.bStyle=bStyle;
  168.     BtnCreateStruct.bState=bState;
  169.     return AddBtnIndirect(hWndTB,&BtnCreateStruct);
  170. }
  171.  
  172.  
  173.  
  174. /***************************************************************************/
  175. /***************************************************************************/
  176. /*                                                                         */
  177. /*                        Function for WndProc                             */
  178. /*                                                                         */
  179. /***************************************************************************/
  180. /***************************************************************************/
  181.  
  182.  
  183. // --------------------------------------------------------------------------
  184. //  FUNCTION    CalcPosit
  185. //  PURPOSE     calculate position of an element
  186. // --------------------------------------------------------------------------
  187. //  INPUT       lpTbInfoHeader  toolbar's header
  188. //              lpNewElem       elem to add
  189. //              lpElemPrec      previous elem (NULL if first)
  190. //  OUTPUT      lpElem is updated
  191. // --------------------------------------------------------------------------
  192. //  COMMENTS    ...
  193. // --------------------------------------------------------------------------
  194. static void CalcPosit(LPTBINFOHEADER lpTbInfoHeader,LPTBELEM lpNewElem,
  195.                                                LPTBELEM lpElemPrec)
  196. {
  197.   lpNewElem->Emplact.bottom = lpNewElem -> cy +
  198.                            (lpNewElem->Emplact.top = lpTbInfoHeader->wFirsty);
  199.   if (lpElemPrec == NULL) lpNewElem->Emplact.left= lpTbInfoHeader->wFirstx ;
  200.                      else lpNewElem->Emplact.left=(lpElemPrec)->Emplact.right +
  201.                                                          lpElemPrec->cxAfter ;
  202.   lpNewElem->Emplact.left += lpNewElem->cxBefore ;
  203.   lpNewElem->Emplact.right=lpNewElem->Emplact.left + lpNewElem->cx;
  204. }
  205.  
  206. // --------------------------------------------------------------------------
  207. //  FUNCTION    Toolbar_AddElem
  208. //  PURPOSE     Add an element in toolber (btn or window)
  209. // --------------------------------------------------------------------------
  210. //  INPUT       lpTbInfoHeader  toolbar's header
  211. //              lpElem          elem to add
  212. //  OUTPUT      lpTbInfoHeader  new toolbar structure (old is destroy)
  213. // --------------------------------------------------------------------------
  214. //  COMMENTS    ...
  215. // --------------------------------------------------------------------------
  216. static LPTBELEM Toolbar_AddElem(LPTBINFOHEADER lpTbInfoHeader,
  217.                                                     LPTBELEM lpElem)
  218. {
  219. WORD wNbElem = lpTbInfoHeader->wNbElem;
  220. LPTBELEM lpTbFirst;
  221. LPTBELEM lpNewElem;
  222.   lpTbFirst = lpTbInfoHeader->lpTbFirst;
  223.  
  224.   if (lpTbFirst==NULL) lpTbFirst = (LPTBELEM) GlobalAllocPtr(GMEM_MOVEABLE,
  225.                               (wNbElem+1)*sizeof(TBELEM));
  226.     else lpTbFirst = (LPTBELEM) GlobalReAllocPtr(lpTbFirst,
  227.                               (wNbElem+1)*sizeof(TBELEM),GMEM_MOVEABLE);
  228.  
  229.   lpTbInfoHeader->lpTbFirst = lpTbFirst;
  230.   lpNewElem = lpTbFirst + wNbElem;
  231.  
  232.   _fmemcpy(lpNewElem,lpElem,sizeof(TBELEM));
  233.   CalcPosit(lpTbInfoHeader,lpNewElem,(wNbElem == 0) ? NULL : (lpNewElem-1));
  234.  
  235.   lpTbInfoHeader->wNbElem ++;
  236.   return lpNewElem;
  237. }
  238.  
  239. static BOOL AddBtnIndirect (HWND hwnd,LPTBELEM lpTbElem)
  240. {
  241. LPTBINFOHEADER lpTbInfoHeader ;
  242.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  243.   Toolbar_AddElem(lpTbInfoHeader,lpTbElem);
  244.   return TRUE;
  245. }
  246.  
  247.  
  248. // --------------------------------------------------------------------------
  249. //  FUNCTION    AddWndIndirect
  250. //  PURPOSE     Add a window (control,... ) on a toolbar
  251. // --------------------------------------------------------------------------
  252. //  INPUT       hWndTB          the handle of ToolBar window
  253. //              lpWindowInTbCreateStruct  point to a structure which contain
  254. //                      the information needed by CreateWindow
  255. //  OUTPUT      TRUE if the button is added
  256. // --------------------------------------------------------------------------
  257. //  COMMENTS    ...
  258. // --------------------------------------------------------------------------
  259. HWND AddWndIndirect (HWND hwnd,LPWINDOWINTBCREATESTRUCT lpWindowInTbCreateStruct)
  260. {
  261. TBELEM tbElem;
  262. LPTBELEM lpTbElem;
  263. LPTBINFOHEADER lpTbInfoHeader ;
  264.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  265.   tbElem.fButton = FALSE;
  266.   tbElem.cx = lpWindowInTbCreateStruct->cx;
  267.   tbElem.cxAfter = lpWindowInTbCreateStruct->cxAfter;
  268.   tbElem.cxBefore = lpWindowInTbCreateStruct->cxBefore;
  269.   tbElem.cy = lpWindowInTbCreateStruct->cy;
  270.   tbElem.wId = lpWindowInTbCreateStruct->wId;
  271.  
  272.   lpTbElem = Toolbar_AddElem (lpTbInfoHeader,&tbElem);
  273.   SetWindowLong(hwnd,0,(LONG)lpTbInfoHeader);
  274.   lpTbElem->hWnd = CreateWindow(
  275.                      lpWindowInTbCreateStruct->lpszClassName,
  276.                      lpWindowInTbCreateStruct->lpszWindowName,
  277.                      lpWindowInTbCreateStruct->dwstyle,
  278.                      lpTbElem->Emplact.left,
  279.                      lpTbElem->Emplact.top,
  280.                      lpWindowInTbCreateStruct->cx,
  281.                      lpWindowInTbCreateStruct->cy,
  282.                      hwnd,
  283.                      (HMENU)lpWindowInTbCreateStruct->wId,
  284.                      lpWindowInTbCreateStruct->hOwner,
  285.                      (LPVOID) NULL);
  286.   return lpTbElem->hWnd;
  287. }
  288.  
  289. // --------------------------------------------------------------------------
  290. //  FUNCTION    AddWnd
  291. //  PURPOSE     Add a window (control,... ) on a toolbar
  292. // --------------------------------------------------------------------------
  293. //  INPUT       hWndTB          the handle of ToolBar window
  294. //        cx, cxAfter, cxBefore,cy define space of window (see AddBtn)
  295. //        the other parameters contain the information needed by CreateWindow
  296. //                    (see CreateWindow API for explanation)
  297. //  OUTPUT      TRUE if the button is added
  298. // --------------------------------------------------------------------------
  299. //  COMMENTS    ...
  300. // --------------------------------------------------------------------------
  301. HWND AddWnd (HWND hwnd,WORD wId,LPCSTR lpszClassName,
  302.         LPCSTR lpszWindowName,DWORD dwstyle,
  303.         UINT cx,UINT cxAfter,UINT cxBefore,UINT cy,HINSTANCE hOwner)
  304. {
  305. WINDOWINTBCREATESTRUCT WinTBCS;
  306.   WinTBCS.wId = wId ;
  307.   WinTBCS.lpszClassName = lpszClassName ;
  308.   WinTBCS.lpszWindowName = lpszWindowName;
  309.   WinTBCS.dwstyle = dwstyle;
  310.   WinTBCS.cx = cx;
  311.   WinTBCS.cxAfter = cxAfter;
  312.   WinTBCS.cxBefore = cxBefore;
  313.   WinTBCS.cy = cy;
  314.   WinTBCS.hOwner = hOwner;
  315.   return AddWndIndirect(hwnd,&WinTBCS);
  316. }
  317.  
  318.  
  319. // --------------------------------------------------------------------------
  320. //  FUNCTION    BtnInPoint
  321. //  PURPOSE     Give the button in a point
  322. // --------------------------------------------------------------------------
  323. //  INPUT       lpTbInfoHeader  toolbar's header
  324. //              x,y             point where search btn
  325. //  OUTPUT      NULL if no button on point, or if button is INVALID
  326. //              else button found
  327. // --------------------------------------------------------------------------
  328. //  COMMENTS    ...
  329. // --------------------------------------------------------------------------
  330. static LPTBELEM BtnInPoint(LPTBINFOHEADER lpTbInfoHeader,UINT x,UINT y)
  331. {
  332. WORD i;
  333. LPTBELEM lpTbElem;
  334. POINT pt;
  335.   pt.x = x;
  336.   pt.y = y;
  337.   lpTbElem = lpTbInfoHeader->lpTbFirst;
  338.  
  339.   for(i = 0; i < lpTbInfoHeader->wNbElem; i++,lpTbElem++)
  340.    {
  341.      if (PtInRect(&(lpTbElem->Emplact),pt))
  342.        {
  343.          if ((lpTbElem->fButton) && (lpTbElem->bState != BTNS_INVALID))
  344.                 return lpTbElem ;
  345.            else return (LPTBELEM) NULL;
  346.        }
  347.    }
  348.   return (LPTBELEM) NULL;
  349. }
  350.  
  351. // --------------------------------------------------------------------------
  352. //  FUNCTION    DoPaintBtn
  353. //  PURPOSE     Show a button in a specified state, with hdc
  354. // --------------------------------------------------------------------------
  355. //  INPUT       hdc             handle to valid dc to draw button
  356. //              hdcMemory       handle to mem dc with bitmap selected
  357. //              lpTbInfoHeader
  358. //              lpTbElem        the button to show
  359. //              bState          the state for show
  360. //  OUTPUT      void
  361. // --------------------------------------------------------------------------
  362. //  COMMENTS    ...
  363. // --------------------------------------------------------------------------
  364.  
  365. static void DoPaintBtn(HDC hdc,HDC hdcMemory,LPTBELEM lpTbElem,BYTE bState)
  366. {
  367.    BitBlt(hdc, lpTbElem->Emplact.left,lpTbElem->Emplact.top,
  368.           CXTBBUTTON, CYTBBUTTON,
  369.           hdcMemory, lpTbElem->wPosBitmap * (CXTBBUTTON - 1),
  370.           bState * (CYTBBUTTON - 1), SRCCOPY);
  371. }
  372.  
  373.  
  374. // --------------------------------------------------------------------------
  375. //  FUNCTION    Toolbar_ShowBtn
  376. //  PURPOSE     Show a button in a specified state
  377. // --------------------------------------------------------------------------
  378. //  INPUT       hwnd            handle to window
  379. //              lpTbInfoHeader
  380. //              lpTbElem        the button to show
  381. //              bState          the state for show
  382. //  OUTPUT      void
  383. // --------------------------------------------------------------------------
  384. //  COMMENTS    ...
  385. // --------------------------------------------------------------------------
  386. static void Toolbar_ShowBtn(HWND hwnd,LPTBINFOHEADER lpTbInfoHeader,
  387.                              LPTBELEM lpTbElem,BYTE bState)
  388. {
  389. HDC  hdcMemory;
  390. HDC  hdc;
  391. HBITMAP   hBmpOld;
  392.   hdc = GetDC(hwnd);
  393.   hdcMemory = CreateCompatibleDC(hdc);
  394.  
  395.   hBmpOld=SelectObject(hdcMemory, lpTbInfoHeader->hPictBtn);
  396.   DoPaintBtn(hdc,hdcMemory,lpTbElem,bState);
  397.   SelectObject(hdcMemory,hBmpOld);
  398.   DeleteDC(hdcMemory);
  399.   ReleaseDC(hwnd, hdc);
  400. }
  401.  
  402.  
  403. // --------------------------------------------------------------------------
  404. //  FUNCTION    Toolbar_OnPaint
  405. //  PURPOSE     Toolbar Window WM_PAINT handler
  406. // --------------------------------------------------------------------------
  407. //  INPUT       hwnd            handle to window
  408. //
  409. //  OUTPUT      void
  410. // --------------------------------------------------------------------------
  411. //  COMMENTS    ...
  412. // --------------------------------------------------------------------------
  413.  
  414. static void Toolbar_OnPaint(HWND hwnd)
  415. {
  416. PAINTSTRUCT ps;
  417. HBITMAP   hBmpOld;
  418. HBRUSH    hBrush;
  419. HPEN      hOldPen;
  420. HDC       hdc, hdcMemory;
  421. RECT      rect;
  422. WORD      i;
  423. LPTBINFOHEADER lpTbInfoHeader ;
  424. LPTBELEM lpTbElem;
  425.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  426.  
  427.   hdc        = BeginPaint(hwnd, &ps);
  428.   hdcMemory  = CreateCompatibleDC(hdc);
  429.  
  430.   GetClientRect(hwnd, &rect);
  431.   // Paint the background    (COLOR_BTNFACE : gris)
  432.   hBrush=CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  433.   FillRect(hdc,&rect,hBrush);
  434.   DeleteObject(hBrush);
  435.  
  436.   // COLOR_BTNHIGHLIGHT : noir (RGB(0,0,0)
  437.   hOldPen=SelectObject(hdc,
  438.                 CreatePen(PS_SOLID,1,RGB(0,0,0)));
  439.   MoveToEx(hdc,0,rect.bottom-1,NULL);
  440.   LineTo(hdc,rect.right,rect.bottom-1);
  441.   DeleteObject(SelectObject(hdc,hOldPen));
  442.  
  443.   // Paint Buttons
  444.   hBmpOld = SelectBitmap(hdcMemory, lpTbInfoHeader->hPictBtn);
  445.   lpTbElem = lpTbInfoHeader->lpTbFirst;
  446.   for(i = 0; i < lpTbInfoHeader->wNbElem; i++,lpTbElem++)
  447.        if (lpTbElem->fButton)
  448.                 DoPaintBtn(hdc,hdcMemory,lpTbElem,lpTbElem->bState);
  449.  
  450.   // clean-up
  451.   SelectBitmap(hdcMemory, hBmpOld);
  452.   DeleteDC(hdcMemory);
  453.   EndPaint(hwnd, &ps);
  454. }
  455.  
  456. // --------------------------------------------------------------------------
  457. //  FUNCTION    PushBtn
  458. //  PURPOSE     Push a button in a group a raise other
  459. // --------------------------------------------------------------------------
  460. //  INPUT       hwnd             HWND of toolbar
  461. //              wIdFirst,wIdLast Bound of Group
  462. //              wIdPush          Button to push
  463. //              wIdExcl          Button on group to exclude
  464. //                                 (for a btn whe want change ourself)
  465. //  OUTPUT      NULL
  466. // --------------------------------------------------------------------------
  467. //  COMMENTS    same use as CheckRadioButton
  468. // --------------------------------------------------------------------------
  469. void PushBtn(HWND hwnd,WORD wIdFirst,WORD wIdLast,WORD wIdPush,WORD wIdExcl)
  470. {
  471. WORD i;
  472. LPTBELEM lpTbElem;
  473. LPTBINFOHEADER lpTbInfoHeader ;
  474.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  475.   lpTbElem = lpTbInfoHeader->lpTbFirst;
  476.  
  477.   for(i = 0; i < lpTbInfoHeader->wNbElem; i++,lpTbElem++)
  478.    {
  479.      if ((lpTbElem->wId >= wIdFirst) && (lpTbElem->wId <= wIdLast) &&
  480.           (lpTbElem->wId != wIdPush) && (lpTbElem->wId != wIdExcl))
  481.           {
  482.             Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbElem,BTNS_RAISED);
  483.             lpTbElem -> bState = BTNS_RAISED ;
  484.           }
  485.      if (lpTbElem->wId == wIdPush)
  486.          {
  487.             Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbElem,BTNS_PUSHED);
  488.             lpTbElem -> bState = BTNS_PUSHED ;
  489.          }
  490.    }
  491. }
  492.  
  493. // --------------------------------------------------------------------------
  494. //  FUNCTION    FoundBtn
  495. //  PURPOSE     Search a button in a toolbar
  496. // --------------------------------------------------------------------------
  497. //  INPUT       hwnd             HWND of toolbar
  498. //              wIdSearch        Button to search
  499. //  OUTPUT      the button is found, otherwise NULL
  500. // --------------------------------------------------------------------------
  501. //  COMMENTS
  502. // --------------------------------------------------------------------------
  503. static LPTBELEM FoundBtn(HWND hwnd,WORD wIdSearch)
  504. {
  505. WORD i;
  506. LPTBELEM lpTbElem;
  507. LPTBINFOHEADER lpTbInfoHeader ;
  508.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  509.   lpTbElem = lpTbInfoHeader->lpTbFirst;
  510.  
  511.   for(i = 0; i < lpTbInfoHeader->wNbElem; i++,lpTbElem++)
  512.      if (lpTbElem->wId == wIdSearch)
  513.         return lpTbElem;
  514.   return NULL ;
  515. }
  516.  
  517. // --------------------------------------------------------------------------
  518. //  FUNCTION    GetBtnState
  519. //  PURPOSE     Get a button state
  520. // --------------------------------------------------------------------------
  521. //  INPUT       hwnd             HWND of toolbar
  522. //              wId              id of button to search
  523. //  OUTPUT      bState of button
  524. //              0xFF if the button isn't found
  525. // --------------------------------------------------------------------------
  526. //  COMMENTS
  527. // --------------------------------------------------------------------------
  528. BYTE GetBtnState (HWND hwnd,WORD wId)
  529. {
  530. LPTBELEM lpTbElem = FoundBtn(hwnd,wId);
  531.   if (lpTbElem == NULL) return 0xFF;
  532.                    else return lpTbElem->bState;
  533. }
  534.  
  535. // --------------------------------------------------------------------------
  536. //  FUNCTION    SetBtnState
  537. //  PURPOSE     Set a button state
  538. // --------------------------------------------------------------------------
  539. //  INPUT       hwnd             HWND of toolbar
  540. //              wId              id of button to search
  541. //              bState           new state of button
  542. //  OUTPUT      BOOL             say if button found and set
  543. // --------------------------------------------------------------------------
  544. //  COMMENTS    the button is draw
  545. // --------------------------------------------------------------------------
  546. BOOL SetBtnState (HWND hwnd,WORD wId,BYTE bState)
  547. {
  548. LPTBELEM lpTbElem = FoundBtn(hwnd,wId);
  549. LPTBINFOHEADER lpTbInfoHeader ;
  550.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  551.   if (lpTbElem == NULL) return FALSE;
  552.   lpTbElem->bState = bState;
  553.   Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbElem,bState);
  554.  
  555.   return TRUE;
  556. }
  557.  
  558. // --------------------------------------------------------------------------
  559. //  FUNCTION    Toolbar_OnLButtonDown
  560. //  PURPOSE     Toolbar WM_LBUTTONDOWN handler
  561. // --------------------------------------------------------------------------
  562. //  INPUT       hwnd            Handle to window receiving messages
  563. //              fDoubleClick    Double click flag
  564. //              x               x position
  565. //              y               y position
  566. //              keyFlags        shift, ctrl ...
  567. //
  568. //  OUTPUT      void
  569. // --------------------------------------------------------------------------
  570. //  COMMENTS
  571. // --------------------------------------------------------------------------
  572. static void Toolbar_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
  573. {
  574. LPTBINFOHEADER lpTbInfoHeader ;
  575. LPTBELEM lpTbInPush;
  576.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  577.   SetFocus(hwnd);       // kill focus in toolbar's chid window
  578.   lpTbInfoHeader->fMouseOnButton=TRUE;
  579.   lpTbInPush = BtnInPoint(lpTbInfoHeader,x,y);
  580.   // Test if the btn is the pushed in a GROUPTOGGLE
  581.   if (lpTbInPush==NULL) return;
  582.   if ((lpTbInPush->bStyle == BSTY_GROUPTOGGLE) &&
  583.                              (lpTbInPush->bState == BTNS_PUSHED))
  584.              return ;
  585.  
  586.   lpTbInfoHeader->lpTbInPush = lpTbInPush ;
  587.  
  588.  
  589.   if (lpTbInPush==NULL) return;
  590.   lpTbInfoHeader->bOldStateInPush = lpTbInPush -> bState ;
  591.   SetCapture(hwnd);
  592.   Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbInPush,
  593.        lpTbInfoHeader->bStateInPush=(lpTbInPush->bState==BTNS_PUSHED) ?
  594.                                          BTNS_RAISED : BTNS_PUSHED) ;
  595.                    /***+++***/ //begin
  596.   PostMessage(lpTbInfoHeader->hwndParent,WM_MENUSELECT,
  597.              GET_WM_MENUSELECT_MPS(lpTbInPush->wId,0,0));                   
  598.                    /***+++***/ //end
  599. }
  600.  
  601. // --------------------------------------------------------------------------
  602. //  FUNCTION    Toolbar_OnMouseMove
  603. //  PURPOSE     Toolbar WM_MOUSEMOVE handler
  604. // --------------------------------------------------------------------------
  605. //  INPUT       hwnd            Handle to window receiving messages
  606. //              x               x position
  607. //              y               y position
  608. //              keyFlags        shift, ctrl ...
  609. //
  610. //  OUTPUT      void
  611. // --------------------------------------------------------------------------
  612. //  COMMENTS
  613. // --------------------------------------------------------------------------
  614. static void Toolbar_OnMouseMove(HWND hwnd, int x, int y, UINT keyFlags)
  615. {
  616. LPTBINFOHEADER lpTbInfoHeader ;
  617. BOOL fOldMouseOnButton;
  618. BOOL fNewMouseOnButton;
  619. LPTBELEM lpTbInPush;
  620. POINT pt;
  621.  
  622.   pt.x = x;
  623.   pt.y = y;
  624.  
  625.   SetCursor(LoadCursor(NULL,IDC_ARROW));
  626.  
  627.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  628.   lpTbInPush=lpTbInfoHeader->lpTbInPush;
  629.   if (lpTbInPush==NULL) return ;
  630.  
  631.   fOldMouseOnButton = lpTbInfoHeader->fMouseOnButton ;
  632.   fNewMouseOnButton = PtInRect(&(lpTbInPush->Emplact),pt);
  633.   lpTbInPush->bState= (fNewMouseOnButton ? BTNS_PUSHED : BTNS_RAISED);
  634.   if (fNewMouseOnButton != fOldMouseOnButton)
  635.       Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbInPush,
  636.               (BYTE)(fNewMouseOnButton ? lpTbInfoHeader->bStateInPush :
  637.                                          lpTbInPush->bState));
  638.  
  639.   lpTbInfoHeader->fMouseOnButton = fNewMouseOnButton ;                   
  640. }
  641.  
  642.  
  643. // --------------------------------------------------------------------------
  644. //  FUNCTION    Toolbar_OnLButtonUp
  645. //  PURPOSE     Toolbar WM_LBUTTONUP handler
  646. // --------------------------------------------------------------------------
  647. //  INPUT       hwnd            Handle to window receiving messages
  648. //              fDoubleClick    Double click flag
  649. //              x               x position
  650. //              y               y position
  651. //              keyFlags        shift, ctrl ...
  652. //
  653. //  OUTPUT      void
  654. // --------------------------------------------------------------------------
  655. //  COMMENTS :
  656. //     GET_WM_COMMAND_MPS defined in WINDOWSX.H for WIN32
  657. //                            and in WINX31AD.H for WIN16
  658. // --------------------------------------------------------------------------
  659. void Toolbar_OnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
  660. {
  661. LPTBINFOHEADER lpTbInfoHeader ;
  662. LPTBELEM lpTbInPush;
  663.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  664.   ReleaseCapture();
  665.   lpTbInPush=lpTbInfoHeader->lpTbInPush;
  666.   if (lpTbInPush==NULL) return;
  667.   if (BtnInPoint(lpTbInfoHeader,x,y)==lpTbInPush)
  668.     {
  669.       if ((lpTbInPush->bStyle == BSTY_GROUPTOGGLE) ||
  670.           (lpTbInPush->bStyle == BSTY_GROUPTOGGLEOPT))
  671.                PushBtn(hwnd,lpTbInPush->wIdFirst,lpTbInPush->wIdLast,0,
  672.                             lpTbInPush->wId);
  673.  
  674.      if (lpTbInPush->bStyle == BSTY_ACTION)
  675.         lpTbInPush->bState = BTNS_RAISED;
  676.        else
  677.         lpTbInPush->bState = (BYTE) (lpTbInfoHeader->bOldStateInPush ==
  678.                                   BTNS_PUSHED) ? BTNS_RAISED : BTNS_PUSHED ;
  679.  
  680.       PostMessage(lpTbInfoHeader->hwndParent,WM_COMMAND,
  681.              GET_WM_COMMAND_MPS (lpTbInPush->wId,hwnd,lpTbInPush->bState));            
  682.     }
  683.    /***+++***/ //begin
  684.   PostMessage(lpTbInfoHeader->hwndParent,WM_MENUSELECT,
  685.              GET_WM_MENUSELECT_MPS(0,0,0));                   
  686.    /***+++***/ //end
  687.  
  688.   Toolbar_ShowBtn(hwnd,lpTbInfoHeader,lpTbInPush,lpTbInPush->bState);
  689.   lpTbInfoHeader->lpTbInPush=NULL;
  690. }
  691.  
  692. // --------------------------------------------------------------------------
  693. //  FUNCTION    SetBitmap
  694. //  PURPOSE     Change the ToolBar bitmap
  695. // --------------------------------------------------------------------------
  696. //  INPUT       hWndTB          the handle of ToolBar window
  697. //              hPictBtn        the Handle of bitmap which contain Button
  698. //  OUTPUT      the handle of the bitmap being replaced
  699. // --------------------------------------------------------------------------
  700. //  COMMENTS    ...
  701. // --------------------------------------------------------------------------
  702. HBITMAP SetBitmap(HWND hwnd,HBITMAP hPictBtn)
  703. {
  704. LPTBINFOHEADER lpTbInfoHeader ;
  705. HBITMAP hOldBmp;
  706.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  707.   hOldBmp = lpTbInfoHeader->hPictBtn;
  708.   lpTbInfoHeader->hPictBtn = hPictBtn;
  709.   return hOldBmp;
  710. }
  711.  
  712. // --------------------------------------------------------------------------
  713. //  FUNCTION    Toolbar_OnCreate
  714. //  PURPOSE     initialize the Toolbar
  715. // --------------------------------------------------------------------------
  716. //  INPUT       hWnd            handle to window receiving message
  717. //              lpCreateStruct  lp to CreateStruct associated to window
  718. //
  719. //  OUTPUT      BOOL            0 to continue to process, -1 otherwise
  720. // --------------------------------------------------------------------------
  721. //  COMMENTS
  722. // --------------------------------------------------------------------------
  723. static BOOL Toolbar_OnCreate(HWND hwnd, CREATESTRUCT FAR *lpCreateStruct)
  724. {
  725. LPTBINFOHEADER lpTbInfoHeader ;
  726.   lpTbInfoHeader = (LPTBINFOHEADER) GlobalAllocPtr(GHND,sizeof(TBINFOHEADER));
  727.   lpTbInfoHeader->wFirstx=MARGINX;
  728.   lpTbInfoHeader->wFirsty=MARGINY;
  729.   lpTbInfoHeader->hwndTB=hwnd;
  730.   lpTbInfoHeader->hwndParent=lpCreateStruct->hwndParent;
  731.   SetWindowLong(hwnd,0,(LONG)lpTbInfoHeader);
  732.   return TRUE;
  733. }
  734.  
  735.  
  736.  
  737. // --------------------------------------------------------------------------
  738. //  FUNCTION    Toolbar_OnDestroy
  739. //  PURPOSE     Toolbar WM_DESTROY handler
  740. // --------------------------------------------------------------------------
  741. //  INPUT       hwnd            Handle to window receiving messages
  742. //
  743. //  OUTPUT      void
  744. // --------------------------------------------------------------------------
  745. //  COMMENTS
  746. // --------------------------------------------------------------------------
  747.  
  748. static void Toolbar_OnDestroy(HWND hwnd)
  749. {
  750. LPTBINFOHEADER lpTbInfoHeader ;
  751.   lpTbInfoHeader = GETLPTBINFOHEADER(hwnd);
  752.   GlobalFreePtr(lpTbInfoHeader);
  753. //  PostQuitMessage(0);  /**** A VOIR ***/
  754. }
  755.  
  756. LRESULT CALLBACK ToolbarWndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
  757. {
  758.   switch (uiMsg)
  759.     {
  760.       HANDLE_MSG(hwnd, WM_CREATE,          Toolbar_OnCreate);
  761.       HANDLE_MSG(hwnd, WM_PAINT,           Toolbar_OnPaint);
  762.       HANDLE_MSG(hwnd, WM_LBUTTONDOWN,     Toolbar_OnLButtonDown);
  763.       HANDLE_MSG(hwnd, WM_LBUTTONUP,       Toolbar_OnLButtonUp);
  764.       HANDLE_MSG(hwnd, WM_MOUSEMOVE,       Toolbar_OnMouseMove);
  765.       HANDLE_MSG(hwnd, WM_DESTROY,         Toolbar_OnDestroy);
  766.  
  767.          // here are messages from Child Window which must be send to parent window
  768.          // you can add somes message here
  769.         case WM_DRAWITEM :
  770.         #ifdef WIN32
  771.         case WM_CTLCOLORSTATIC:
  772.         case WM_CTLCOLORBTN:           
  773.         case WM_CTLCOLORDLG:
  774.         case WM_CTLCOLOREDIT:
  775.         case WM_CTLCOLORLISTBOX:
  776.         case WM_CTLCOLORMSGBOX:
  777.         case WM_CTLCOLORSCROLLBAR:
  778.         #else
  779.         case WM_CTLCOLOR:
  780.         #endif
  781.         case WM_COMMAND :
  782.         case WM_MEASUREITEM :
  783.              return SendMessage(GETLPTBINFOHEADER(hwnd)
  784.                                    ->hwndParent,
  785.                                    uiMsg,wParam,lParam);
  786.     }
  787.  return DefWindowProc(hwnd, uiMsg, wParam, lParam);
  788. }
  789.