home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / classlib / ctoolbar.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  13KB  |  624 lines

  1. /*
  2.  * CTOOLBAR.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Implementation of the CToolBar class
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include <windows.h>
  16. #include "classlib.h"
  17.  
  18.  
  19. /*
  20.  * CToolBar::CToolBar
  21.  * CToolBar::~CToolBar
  22.  *
  23.  * Constructor Parameters:
  24.  *  hInst           HINSTANCE of the module we're loaded in.
  25.  */
  26.  
  27. CToolBar::CToolBar(HINSTANCE hInst)
  28.     : CWindow(hInst)
  29.     {
  30.     return;
  31.     }
  32.  
  33.  
  34. CToolBar::~CToolBar(void)
  35.     {
  36.     return;
  37.     }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. /*
  44.  * CToolBar::Init
  45.  *
  46.  * Purpose:
  47.  *  Initializes a toolbar object by creating the control that it
  48.  *  owns.
  49.  *
  50.  * Parameters:
  51.  *  hWndParent      HWND of the parent window.  The toolbar is
  52.  *                  created up from the bottom of this window,
  53.  *                  spanning the entire width of the window.
  54.  *  uID             UINT id of the control.
  55.  *  cy              UINT height to create the control
  56.  *
  57.  * Return Value:
  58.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  59.  */
  60.  
  61. BOOL CToolBar::Init(HWND hWndParent, UINT uID, UINT cy)
  62.     {
  63.     RECT            rc;
  64.  
  65.     /*
  66.      * Note that the class is already registered since we live in a
  67.      * DLL and that DLL will be loaded if anyone is using this class
  68.      * library.
  69.      */
  70.  
  71.     GetClientRect(hWndParent, &rc);
  72.     m_cyBar=cy;
  73.  
  74.     m_hWnd=CreateWindow(CLASS_GIZMOBAR, TEXT("Wooley")
  75.         , WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, rc.left, rc.top
  76.         , rc.right-rc.left, m_cyBar, hWndParent, (HMENU)uID, m_hInst
  77.         , 0L);
  78.  
  79.     return (NULL!=m_hWnd);
  80.     }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. /*
  89.  * CToolBar::OnSize
  90.  *
  91.  * Purpose:
  92.  *  Handles parent resizing.  The owner of this window is responsible
  93.  *  to call this function when it wants the toolbar to resize.  The
  94.  *  toolbar will automatically occupy a strip of the appropriate
  95.  *  height along the top of the window.
  96.  *
  97.  * Parameters:
  98.  *  hWndParent      HWND of the parent window to which we're resizing
  99.  *
  100.  * Return Value:
  101.  *  None
  102.  */
  103.  
  104. void CToolBar::OnSize(HWND hWndParent)
  105.     {
  106.     RECT        rc;
  107.  
  108.     GetClientRect(hWndParent, &rc);
  109.  
  110.     SetWindowPos(m_hWnd, NULL, rc.left, rc.top, rc.right-rc.left
  111.         , m_cyBar, SWP_NOZORDER);
  112.  
  113.     return;
  114.     }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /*
  122.  * CToolBar::FontSet
  123.  *
  124.  * Purpose:
  125.  *  Changes the font in the StatStrip.
  126.  *
  127.  * Parameters:
  128.  *  hFont           HFONT of the font to use in the control.
  129.  *  fRedraw         BOOL indicating if the control is to repaint
  130.  *
  131.  * Return Value:
  132.  *  None
  133.  */
  134.  
  135. void CToolBar::FontSet(HFONT hFont, BOOL fRedraw)
  136.     {
  137.     SendMessage((UINT)m_hWnd, WM_SETFONT, (WPARAM)hFont, fRedraw);
  138.     return;
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*
  146.  * CToolBar::FontGet
  147.  *
  148.  * Purpose:
  149.  *  Retrieves the handle of the current font used in the control.
  150.  *
  151.  * Parameters:
  152.  *  None
  153.  *
  154.  * Return Value:
  155.  *  HFONT           Handle to the current font.
  156.  */
  157.  
  158. HFONT CToolBar::FontGet(void)
  159.     {
  160.     return (HFONT)(UINT)SendMessage((UINT)m_hWnd, WM_GETFONT, 0, 0L);
  161.     }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169.  * CToolBar::Enable
  170.  *
  171.  * Purpose:
  172.  *  Enables or disables the StatStrip window, graying the text if
  173.  *  the control is disabled.
  174.  *
  175.  * Parameters:
  176.  *  fEnable         BOOL specifying to enable (TRUE) or disable
  177.  *
  178.  * Return Value:
  179.  *  None
  180.  */
  181.  
  182. void CToolBar::Enable(BOOL fEnable)
  183.     {
  184.     EnableWindow(m_hWnd, fEnable);
  185.     return;
  186.     }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. /*
  194.  * CToolBar::HwndAssociateSet
  195.  *
  196.  * Purpose:
  197.  *  Changes the associate window of a toolbar.
  198.  *
  199.  * Parameters:
  200.  *  hWndNew         HWND of new associate.
  201.  *
  202.  * Return Value:
  203.  *  HWND            Handle of previous associate.
  204.  */
  205.  
  206. HWND CToolBar::HwndAssociateSet(HWND hWndNew)
  207.     {
  208.     return GBHwndAssociateSet(m_hWnd, hWndNew);
  209.     }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. /*
  216.  * CToolBar::HwndAssociateGet
  217.  *
  218.  * Purpose:
  219.  *  Retrieves the associate window of a toolbar
  220.  *
  221.  * Parameters:
  222.  *  None
  223.  *
  224.  * Return Value:
  225.  *  HWND            Handle of current associate.
  226.  */
  227.  
  228. HWND CToolBar::HwndAssociateGet(void)
  229.     {
  230.     return GBHwndAssociateGet(m_hWnd);
  231.     }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /*
  240.  * CToolBar::Add
  241.  *
  242.  * Purpose:
  243.  *  Creates a new tool on the toolbar.  Subsequent operations
  244.  *  should be done using the identifier, uID, for this tool.
  245.  *
  246.  * Parameters:
  247.  *  iType           UINT type of the tool to create.
  248.  *  iTool           UINT position (zero-based) for the tool.
  249.  *  uID             UINT identifier for WM_COMMAND from this tool.
  250.  *  dx, dy          UINT dimensions of the tool.
  251.  *  pszText         LPTSTR initial text for edit, list, combo, text
  252.  *  hBitmap         HBITMAP for tools of the button types (COMMAND
  253.  *                  or ATTRIBUTE) specifies a source bitmap from
  254.  *                  which the button image is taken.
  255.  *  iImage          UINT index into hBitmap for the button image
  256.  *  uState          UINT initial state of the tool.
  257.  *
  258.  * Return Value:
  259.  *  BOOL            TRUE if creation succeeded, FALSE otherwise.
  260.  */
  261.  
  262. BOOL CToolBar::Add(UINT iType, UINT iTool, UINT uID, UINT dx
  263.     , UINT dy, LPTSTR pszText, HBITMAP hBmp, UINT iImage, UINT uState)
  264.     {
  265.     return GBGizmoAdd(m_hWnd, iType, iTool, uID, dx, dy
  266.         , pszText, hBmp, iImage, uState);
  267.     }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. /*
  276.  * CToolBar::Remove
  277.  *
  278.  * Purpose:
  279.  *  Removes an existing tool from the toolbar.
  280.  *
  281.  * Parameters:
  282.  *  uID             UINT identifier for this tool.
  283.  *
  284.  * Return Value:
  285.  *  BOOL            TRUE if deletion succeeded, FALSE otherwise.
  286.  */
  287.  
  288. BOOL CToolBar::Remove(UINT uID)
  289.     {
  290.     return GBGizmoRemove(m_hWnd, uID);
  291.     }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. /*
  299.  * CToolBar::SendMessage
  300.  *
  301.  * Purpose:
  302.  *  Implements the equivalent of SendMessage to a tool in the
  303.  *  toolbar.  Separators, command buttons, and attribute buttons
  304.  *  do not accept messages.
  305.  *
  306.  * Parameters:
  307.  *  uID             UINT identifier of the tool to affect.
  308.  *  iMsg            UINT message to send.
  309.  *  wParam          WPARAM of the message.
  310.  *  lParam          LPARAM of the message.
  311.  *
  312.  * Return Value:
  313.  *  LONG            Return value from the message.  0L if the
  314.  *                  tool does not accept messages.
  315.  */
  316.  
  317. LONG CToolBar::SendMessage(UINT uID, UINT iMsg, WPARAM wParam
  318.     , LPARAM lParam)
  319.     {
  320.     return GBGizmoSendMessage(m_hWnd, uID, iMsg, wParam, lParam);
  321.     }
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /*
  330.  * CToolBar::Show
  331.  *
  332.  * Purpose:
  333.  *  Shows or hides a control, adjusting the positions of all others
  334.  *  to make room for or reuse the space for this control.
  335.  *
  336.  * Parameters:
  337.  *  uID             UINT identifier of the tool to affect.
  338.  *  fShow           BOOL TRUE to show the tool, FALSE to hide it.
  339.  *
  340.  * Return Value:
  341.  *  BOOL            TRUE if the function was successful, FALSE
  342.  *                  otherwise.
  343.  */
  344.  
  345. BOOL CToolBar::Show(UINT uID, BOOL fShow)
  346.     {
  347.     return GBGizmoShow(m_hWnd, uID, fShow);
  348.     }
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355. /*
  356.  * CToolBar::Enable
  357.  *
  358.  * Purpose:
  359.  *  Enables or disables a control on the toolbar.
  360.  *
  361.  * Parameters:
  362.  *  uID             UINT identifier of the tool to affect.
  363.  *  fEnable         BOOL TRUE to enable the tool, FALSE otherwise.
  364.  *
  365.  * Return Value:
  366.  *  BOOL            TRUE if the tool was previously disabled, FALSE
  367.  *                  otherwise.
  368.  */
  369.  
  370. BOOL CToolBar::Enable(UINT uID, BOOL fEnable)
  371.     {
  372.     return GBGizmoEnable(m_hWnd, uID, fEnable);
  373.     }
  374.  
  375.  
  376.  
  377.  
  378.  
  379. /*
  380.  * CToolBar::Check
  381.  *
  382.  * Purpose:
  383.  *  Checks or unchecks an attribute button in the toolbar.  If the
  384.  *  tool is part of a group of mutually exclusive attributes, then
  385.  *  other tools are unchecked when this one is checked.  If this is
  386.  *  the only one checked in these circumstances, this function is
  387.  *  a NOP.
  388.  *
  389.  * Parameters:
  390.  *  uID             UINT identifier of the tool to affect.
  391.  *  fCheck          BOOL TRUE to check this tool, FALSE to uncheck.
  392.  *
  393.  * Return Value:
  394.  *  BOOL            TRUE if the change took place.  FALSE otherwise.
  395.  */
  396.  
  397. BOOL CToolBar::Check(UINT uID, BOOL fCheck)
  398.     {
  399.     return GBGizmoCheck(m_hWnd, uID, fCheck);
  400.     }
  401.  
  402.  
  403.  
  404.  
  405.  
  406. /*
  407.  * CToolBar::FocusSet
  408.  *
  409.  * Purpose:
  410.  *  Sets the focus to a partuclar tool in the toolbar if that tool
  411.  *  can accept the focus.  Separators, attribute buttons, text,
  412.  *  and command buttons cannot have the focus.
  413.  *
  414.  * Parameters:
  415.  *  uID             UINT identifier of the tool to affect.
  416.  *
  417.  * Return Value:
  418.  *  BOOL            TRUE if the focus was set.  FALSE otherwise,
  419.  *                  such as when uID identifies a control that
  420.  *                  cannot have focus.
  421.  *
  422.  */
  423.  
  424. UINT CToolBar::FocusSet(UINT uID)
  425.     {
  426.     return GBGizmoFocusSet(m_hWnd, uID);
  427.     }
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436. /*
  437.  * CToolBar::Exist
  438.  *
  439.  * Purpose:
  440.  *  Determines if a tool of a given identifier exists.
  441.  *
  442.  * Parameters:
  443.  *  uID             UINT identifier to verify.
  444.  *
  445.  * Return Value:
  446.  *  BOOL            TRUE if the tool exists, FALSE otherwise.
  447.  */
  448.  
  449. BOOL CToolBar::Exist(UINT uID)
  450.     {
  451.     return GBGizmoExist(m_hWnd, uID);
  452.     }
  453.  
  454.  
  455.  
  456.  
  457.  
  458. /*
  459.  * CToolBar::TypeGet
  460.  *
  461.  * Purpose:
  462.  *  Returns the type of the tool specified by the given identifer.
  463.  *
  464.  * Parameters:
  465.  *  uID             UINT identifier to find.
  466.  *
  467.  * Return Value:
  468.  *  int             A GIZMOTYPE_* value if the function is
  469.  *                  successful, otherwise -1.
  470.  */
  471.  
  472. int CToolBar::TypeGet(UINT uID)
  473.     {
  474.     return GBGizmoTypeGet(m_hWnd, uID);
  475.     }
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484. /*
  485.  * CToolBar::DataSet
  486.  * CToolBar::DataGet
  487.  *
  488.  * Purpose:
  489.  *  Sets or retrieves an extra DWORD value associated with the given
  490.  *  tool.  Applications can store any information here they please.
  491.  *
  492.  * Parameters:
  493.  *  uID             UINT identifier of the tool.
  494.  *  dwData          (Set only) DWORD data to store with the tool.
  495.  *
  496.  * Return Value:
  497.  *  DWORD           Set:  Previous value
  498.  *                  Get:  Current value
  499.  */
  500.  
  501. DWORD CToolBar::DataSet(UINT uID, DWORD dwData)
  502.     {
  503.     return GBGizmoDataSet(m_hWnd, uID, dwData);
  504.     }
  505.  
  506.  
  507. DWORD CToolBar::DataGet(UINT uID)
  508.     {
  509.     return GBGizmoDataGet(m_hWnd, uID);
  510.     }
  511.  
  512.  
  513.  
  514.  
  515.  
  516. /*
  517.  * CToolBar::NotifySet
  518.  * CToolBar::NotifyGet
  519.  *
  520.  * Purpose:
  521.  *  Sets or retrieves the notify status of a tool.  If notify is
  522.  *  FALSE, the no WM_COMMAND messages are sent from the toolbar
  523.  *  to the parent window when this tool is used.
  524.  *
  525.  * Parameters:
  526.  *  uID             UINT identifier of the tool.
  527.  *  fNotify         (Set only) BOOL new notify status to set.
  528.  *
  529.  * Return Value:
  530.  *  BOOL            Set:  Previous value of the notify flag.
  531.  *                  Get:  Current value of the notify flag.
  532.  */
  533.  
  534. BOOL CToolBar::NotifySet(UINT uID, BOOL fNotify)
  535.     {
  536.     return GBGizmoNotifySet(m_hWnd, uID, fNotify);
  537.     }
  538.  
  539.  
  540. BOOL CToolBar::NotifyGet(UINT uID)
  541.     {
  542.     return GBGizmoNotifyGet(m_hWnd, uID);
  543.     }
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551. /*
  552.  * CToolBar::TextSet
  553.  * CToolBar::TextGet
  554.  *
  555.  * Purpose:
  556.  *  Retrieves or sets text in a toolbar control.  Separators,
  557.  *  command buttons, and attribute buttons are not affected by
  558.  *  this call.
  559.  *
  560.  * Parameters:
  561.  *  uID             UINT identifying the tool.
  562.  *  psz             LPTSTR pointing to a buffer to receive the text.
  563.  *  cch             (Get only) UINT maximum number of chars to copy
  564.  *                  to psz.
  565.  *
  566.  * Return Value:
  567.  *  int             Number of characters copied to psz.
  568.  */
  569.  
  570. void CToolBar::TextSet(UINT uID, LPTSTR psz)
  571.     {
  572.     GBGizmoTextSet(m_hWnd, uID, psz);
  573.     return;
  574.     }
  575.  
  576.  
  577. int CToolBar::TextGet(UINT uID, LPTSTR psz, UINT cch)
  578.     {
  579.     return GBGizmoTextGet(m_hWnd, uID, psz, cch);
  580.     }
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588. /*
  589.  * CToolBar::IntSet
  590.  * CToolBar::IntGet
  591.  *
  592.  * Purpose:
  593.  *  Retrieves or sets an integer in a toolbar control.  Separators,
  594.  *  command buttons, and attribute buttons are not affected by this
  595.  *  call.
  596.  *
  597.  * Parameters:
  598.  *  uID             UINT identifying the tool.
  599.  *
  600.  *  Get Parameters:
  601.  *  pfTrans         BOOL * in which the success of the function is
  602.  *                  returned.
  603.  *  fSigned         BOOL TRUE to indicate if the value is signed.
  604.  *
  605.  *  Set Parameters:
  606.  *  u               UINT value to set in the tool.
  607.  *  fSigned         BOOL TRUE to indicate if the value is signed.
  608.  *
  609.  * Return Value:
  610.  *  UINT            Integer translation of the tool's text.
  611.  */
  612.  
  613. void CToolBar::IntSet(UINT uID, int i, BOOL fSigned)
  614.     {
  615.     GBGizmoIntSet(m_hWnd, uID, i, fSigned);
  616.     return;
  617.     }
  618.  
  619.  
  620. UINT CToolBar::IntGet(UINT uID, BOOL *pfTrans, BOOL fSigned)
  621.     {
  622.     return GBGizmoIntGet(m_hWnd, uID, pfTrans, fSigned);
  623.     }
  624.