home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WRITEPAD.PAK / STATBAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  10.2 KB  |  348 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: statbar.c
  9. //
  10. //  PURPOSE: Handles general routines for the Barsdi sample.
  11. //
  12. //  FUNCTIONS:
  13. //    MsgTimer      - Handles the WM_TIMER messages to set the time on
  14. //                    the status bar.
  15. //    MsgMenuSelect - Handle the WM_MENUSELECT message. This message will
  16. //                    enable the status bar control to update when the user
  17. //                    moves across menu items on the main window.
  18. //    InitializeStatusBar - Initialize statusbar control with time and
  19. //                    mouse positions.
  20. //    CreateSBar    - Calls CreateStatusWindow() to create the status bar
  21. //    UpdateStatusBar - Updates the statusbar control with appropriate text
  22. //
  23.  
  24. #include <windows.h>            // required for all Windows applications
  25. #include <windowsx.h>
  26. #include <commctrl.h>           // prototypes and defs for common controls
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29. #include "statbar.h"            // prototypes specific to statbar.c
  30.  
  31.  
  32. // Global Variable for the status bar control.
  33.  
  34. HWND  hWndStatusbar;
  35.  
  36. //  **TODO**  Add entries to the string table in barsdi.rc for each menu
  37. //            command.  MsgMenuSelect (below) loads these strings to display
  38. //            information in the status bar.  MsgMenuSelect assumes that the
  39. //            string ID is the same as the command ID and that a string
  40. //            exists for every command.
  41. //
  42. // The following array contains resource string ID's for popup menus
  43. // in the main application menu.  This array is used by MsgMenuSelect
  44. // to display information in the status bar.
  45. //
  46. //  **TODO**  Add entries to this array for each popup menu in the same
  47. //            positions as they appear in the main menu.  Remember to define
  48. //            the ID's in globals.h and add the strings to barsdi.rc.
  49.  
  50. UINT idPopup[] =
  51. {
  52.     IDS_MDISYSMENU,     // ***MULTIPAD*** String ID for the MDI child sysmenu
  53.     IDS_FILEMENU,
  54.     IDS_EDITMENU,
  55.     IDS_SEARCHMENU,
  56.     IDS_WINDOWMENU,
  57.     IDS_HELPMENU,
  58. };
  59.  
  60.  
  61. //
  62. //  FUNCTION: MsgTimer(HWND, UINT, WPARAM, LPARAM)
  63. //
  64. //  PURPOSE: Calls GetLocalTime() to set the time on the status bar
  65. //
  66. //
  67. //  PARAMETERS:
  68. //
  69. //    hwnd      - Window handle  (Unused)
  70. //    uMessage  - Message number (Unused)
  71. //    wparam    - Extra data     (Unused)
  72. //    lparam    - Extra data     (Unused)
  73. //
  74. //  RETURN VALUE:
  75. //
  76. //    Always returns 0 - Message handled
  77. //
  78. //  COMMENTS:
  79. //
  80. //    Every time the window procedure receives a Timer message, it calls
  81. //    GetLocalTime() to obtain the time and then formats the time into
  82. //    a string. The time sting is then displayed on the status bar.
  83. //
  84.  
  85. #pragma argsused
  86. LRESULT MsgTimer(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  87. {
  88.     char        szBuf[16];      // Temp buffer.
  89.     SYSTEMTIME  sysTime;
  90.  
  91.  
  92.     GetLocalTime(&sysTime);
  93.  
  94.     wsprintf(szBuf,
  95.              "%2d:%02d:%02d %s",
  96.              (sysTime.wHour == 0 ? 12 :
  97.              (sysTime.wHour <= 12 ? sysTime.wHour : sysTime.wHour -12)),
  98.              sysTime.wMinute,
  99.              sysTime.wSecond,
  100.              (sysTime.wHour < 12 ? "AM":"PM"));
  101.  
  102.     UpdateStatusBar(szBuf, 2, 0);
  103.     return 0;
  104. }
  105.  
  106.  
  107. //
  108. //  FUNCTION: MsgMenuSelect(HWND, UINT, WPARAM, LPARAM)
  109. //
  110. //  PURPOSE:  Upadates menu selections on the staus bar.
  111. //
  112. //
  113. //  PARAMETERS:
  114. //
  115. //    hwnd      - Window handle  (Used)
  116. //    uMessage  - Message number (Used)
  117. //    wparam    - Extra data     (Used)
  118. //    lparam    - Extra data     (Used)
  119. //
  120. //  RETURN VALUE:
  121. //
  122. //    Always returns 0 - Message handled
  123. //
  124. //  COMMENTS:
  125. //    This message is sent when the user selects menu items by
  126. //    by pulling down  a popup menu move the mouse around to highlite
  127. //    different menu items.
  128. //
  129. //
  130.  
  131. #pragma argsused
  132. LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  133. {
  134.     static char szBuffer[128];
  135.      char   szTitle[32];
  136.      LPSTR  lpTitle;
  137.      UINT   nStringID;
  138.     UINT   fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
  139.     UINT   uCmd    = GET_WM_MENUSELECT_CMD(wparam, lparam);
  140.     HMENU  hMenu   = GET_WM_MENUSELECT_HMENU(wparam, lparam);
  141.  
  142.  
  143.     szBuffer[0] = 0;                            // First reset the buffer
  144.  
  145.  
  146.     if (fuFlags == 0xffff && hMenu == NULL)     // Menu has been closed
  147.         nStringID = IDS_DESCRIPTION;
  148.  
  149.     else if (fuFlags & MFT_SEPARATOR)           // Ignore separators
  150.         nStringID = 0;
  151.  
  152.     else if (fuFlags & MF_POPUP)                // Popup menu
  153.     {
  154.         if (fuFlags & MF_SYSMENU)               // System menu
  155.             nStringID = IDS_SYSMENU;
  156.  
  157.         else
  158.         {
  159.             // ***MULTIPAD*** If there is a maximized MDI child window,
  160.             //                its system menu will be added to the main
  161.             //                window's menu bar.  Since the string ID for
  162.             //                the MDI child's sysmenu is already in the
  163.             //                idPopup array, all we need to do is patch up
  164.             //                the popup menu index (uCmd) when the child's
  165.             //                system menu is NOT present.
  166.  
  167.             HWND hwndChild = GetActiveMDIChild();
  168.  
  169.             if (!hwndChild || !IsZoomed(hwndChild)) // No MDI child sysmenu
  170.                 uCmd++;
  171.  
  172.             // Get string ID for popup menu from idPopup array.
  173.             nStringID = ((uCmd < sizeof(idPopup)/sizeof(idPopup[0])) ?
  174.                             idPopup[uCmd] : 0);
  175.         }
  176.     }  // for MF_POPUP
  177.  
  178.     else                                        // Must be a command item
  179.     {
  180.         // ***MULTIPAD*** The Window menu has a dynamic part at the bottom
  181.         //                where the MDI Client window adds entries for each
  182.         //                child window that is open.  By getting the menu
  183.         //                item string we can customize the status bar string
  184.         //                with the name of the document.
  185.  
  186.           if (uCmd >= IDM_WINDOWCHILD && uCmd < IDS_HELPMENU)
  187.         {
  188.             LoadString(hInst, IDM_WINDOWCHILD, szBuffer, sizeof(szBuffer));
  189.  
  190.             GetMenuString(hMenu,
  191.                           uCmd,
  192.                           szTitle,
  193.                           sizeof(szTitle),
  194.                           MF_BYCOMMAND);
  195.  
  196.             lpTitle = szTitle;
  197.  
  198.             while (*lpTitle && *lpTitle != ' ')
  199.                 lpTitle++;
  200.  
  201.                 lstrcat(szBuffer, lpTitle);
  202.  
  203.             nStringID = 0;
  204.         }
  205.         else
  206.             nStringID = uCmd;               // String ID == Command ID
  207.     }
  208.  
  209.     // Load the string if we have an ID
  210.     if (0 != nStringID)
  211.         LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
  212.  
  213.     // Finally... send the string to the status bar
  214.     UpdateStatusBar(szBuffer, 0, 0);
  215.  
  216.      return 0;
  217. }
  218.  
  219.  
  220. //
  221. //  FUNCTION: InitializeStatusBar(HWND)
  222. //
  223. //  PURPOSE:  Initialize statusbar control with time and mouse positions.
  224. //
  225. //
  226. //  PARAMETERS:
  227. //
  228. //  hwndParent - Window handle of the status bar's parent
  229. //
  230. //
  231. //  RETURN VALUE:  NONE
  232. //
  233. //
  234. //  COMMENTS:
  235. //
  236. //   This function initializes the time  and mouse positions sections of
  237. //   the statubar window. The Date for the time section is obtained by
  238. //   calling SetTimer API. When the timer messages start comming in,
  239. //   GetSytemTime() to fill the time section.
  240. //   The WPARAM of SB_SETTEXT is divided into 2 parameters. The LOWORD
  241. //   determines which section/part the text goes into, and the HIWORD
  242. //   tells how the bar is drawn (popin or popout).
  243. //
  244.  
  245. void InitializeStatusBar(HWND hwndParent)
  246. {
  247.     const cSpaceInBetween = 8;
  248.     int   ptArray[3];   // Array defining the number of parts/sections
  249.     SIZE  size;         // the Status bar will display.
  250.     RECT  rect;
  251.     HDC   hDC;
  252.  
  253.    /*
  254.     * Fill in the ptArray...
  255.     */
  256.  
  257.     hDC = GetDC(hwndParent);
  258.     GetClientRect(hwndParent, &rect);
  259.  
  260.     ptArray[2] = rect.right;
  261.  
  262.     if (GetTextExtentPoint(hDC, "00:00:00 PM", 12, &size))
  263.         ptArray[1] = ptArray[2] - (size.cx) - cSpaceInBetween;
  264.     else
  265.         ptArray[1] = 0;
  266.  
  267.     if (GetTextExtentPoint(hDC, "Time:", 6, &size))
  268.         ptArray[0] = ptArray[1] - (size.cx) - cSpaceInBetween;
  269.     else
  270.         ptArray[0] = 0;
  271.  
  272.     ReleaseDC(hwndParent, hDC);
  273.  
  274.     SendMessage(hWndStatusbar,
  275.                 SB_SETPARTS,
  276.                 sizeof(ptArray)/sizeof(ptArray[0]),
  277.                 (LPARAM)(LPINT)ptArray);
  278.  
  279.     UpdateStatusBar(szTitle, 0, 0);
  280.     UpdateStatusBar("Time:", 1, SBT_POPOUT);
  281. }
  282.  
  283.  
  284. //
  285. //  FUNCTION: CreateSBar(HWND, UINT, WPARAM, LPARAM)
  286. //
  287. //  PURPOSE:  Calls CreateStatusWindow() to create the status bar
  288. //
  289. //
  290. //  PARAMETERS:
  291. //
  292. //  hwndParent - Window handle of the status bar's parent
  293. //
  294. //  RETURN VALUE:
  295. //
  296. //  If both controls were created successfully Return TRUE,
  297. //  else returns FALSE.
  298. //
  299. //  COMMENTS:
  300. //
  301. //
  302.  
  303. BOOL CreateSBar(HWND hwndParent)
  304. {
  305.     hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER,
  306.                                        szTitle,
  307.                                        hwndParent,
  308.                                        IDM_STATUSBAR);
  309.     if(hWndStatusbar)
  310.     {
  311.         InitializeStatusBar(hwndParent);
  312.         SetTimer(hwndParent, IDM_TIMER, TIMER_TIMEOUT, NULL);
  313.         return TRUE;
  314.     }
  315.  
  316.     return FALSE;
  317. }
  318.  
  319.  
  320. //
  321. //  FUNCTION: UpdateStatusBar(HWND)
  322. //
  323. //  PURPOSE:  Updates the statusbar control with appropriate text
  324. //
  325. //
  326. //  PARAMETERS:
  327. //
  328. //  lpszStatusString - text to be displayed
  329. //  partNumber       - which part of the status bar to display text in
  330. //  displayFlags     - display flags
  331. //
  332. //
  333. //  RETURN VALUE: NONE
  334. //
  335. //
  336. //  COMMENTS:
  337. //      None
  338. //
  339. //
  340.  
  341. void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
  342. {
  343.     SendMessage(hWndStatusbar,
  344.                 SB_SETTEXT,
  345.                 partNumber | displayFlags,
  346.                 (LPARAM)lpszStatusString);
  347. }
  348.