home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Code Resources / Windows 95 MDEF 2.0.1 / Sourcery / DrawMenuItem.cpp next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  8.0 KB  |  299 lines  |  [TEXT/CWIE]

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <ToolUtils.h>
  6. #include <Icons.h>
  7. #include <Controls.h>
  8. #include <Gestalt.h>
  9. #include <Resources.h>
  10.  
  11. #include "DrawMenuItem.h"
  12. #include "Win95Look.h"
  13.  
  14. void DrawItemSeparator(Rect *itemRect, MDEFstuff *mdefData);
  15.  
  16. void DrawItemMark(
  17.     char        theMark,
  18.     short        hiliteState,
  19.     Rect        *itemRect,
  20.     MDEFstuff    *mdefData);
  21.  
  22. void DrawMenuItemText(
  23.     Str255        itemText,
  24.     short        hiliteState,
  25.     short        itemMarkOffset,
  26.     Rect        *itemRect,
  27.     MenuHandle    whichMenu,
  28.     short        menuItem,
  29.     MDEFstuff    *mdefData);
  30.  
  31. void DrawSubMenuTriangle(
  32.     Rect *itemRect,
  33.     short hiliteState,
  34.     MDEFstuff *mdefData);
  35.  
  36. void DrawCmdKey(
  37.     char        theChar,
  38.     short        hiliteState,
  39.     Rect        *itemRect,
  40.     MDEFstuff    *mdefData);
  41.  
  42. // ---------------------------------------------------------------------------
  43.  
  44. // DrawMenuItem.
  45. // The "meat" of this MDEF. Does the actual drawing.
  46. // Draws the menu item's frame, item text, cmd-keys, and submenu symbols.
  47. // It does this and also accounts for menu item's enabled/disabled status,
  48. // among other things. Pretty tedious.
  49.  
  50. void DrawMenuItem(MenuHandle whichMenu, Rect *menuRect, short whichItem,
  51.                     short hiliteState, MDEFstuff *mdefData) {
  52.     Str255    itemText;
  53.     Rect    itemRect;
  54.     Boolean    itemDisabled;
  55.     short    itemMarkOffset = 0;
  56.  
  57.     // Get text of menu item.
  58.     GetItem(whichMenu, whichItem, itemText);
  59.  
  60.     // Determine menu item's rect.
  61.     GetMenuItemRect(menuRect, &itemRect, whichItem);
  62.     // Do some rect clipping & fudging
  63.     InsetRect(&itemRect, kRectPadding, kRectPadding);
  64.  
  65.     // Catch special menu item cases, such as the dividing line "(-"
  66.     if (itemText[1] == kDividerChar) {
  67.         DrawItemSeparator(&itemRect, mdefData);
  68.         return; // No more to draw, so exit
  69.     }
  70.  
  71.     itemDisabled = IsItemDisabled(whichMenu, whichItem);
  72.  
  73.     if (hiliteState == kMenuUnhilited) {
  74.         RGBForeColor(&(mdefData->params).menuBkgndColor);
  75.         PaintRect(&itemRect);
  76.     }
  77.     else {
  78.         RGBForeColor(&(mdefData->params).menuSelectionColor);
  79.         PaintRect(&itemRect);
  80.     }
  81.  
  82.     if (itemDisabled)
  83.         hiliteState = kMenuDisabled;
  84.  
  85.     // Draw item mark, if any. Remember to note if menu item is disabled or not
  86.     itemMarkOffset = CharWidth(kCheckMarkChar) + kItemMarkPadding;
  87.     short theMark, theChar;
  88.     GetItemMark(whichMenu, whichItem, &theMark);
  89.     GetItemCmd(whichMenu, whichItem, &theChar);
  90.     if (theMark != noMark && theChar != hMenuCmd) {
  91.         // Not a submenu, but an actual item mark, so we have to draw it
  92.         DrawItemMark(theMark, hiliteState, &itemRect, mdefData);
  93.     }
  94.  
  95.     // Now time to draw the menu item text itself...
  96.     DrawMenuItemText(itemText, hiliteState, itemMarkOffset,
  97.         &itemRect, whichMenu, whichItem, mdefData);
  98.     
  99.     // Alright, time to draw Cmd-keys and/or submenu triangles
  100.     switch(theChar) {
  101.         case kSubmenuCode:
  102.             DrawSubMenuTriangle(&itemRect, hiliteState, mdefData);
  103.         break;
  104.         
  105.         case kScriptCode:
  106.         case kUseICONCode:
  107.         case kUseSICNCode:
  108.             // Not supported in this version. Do nothing.
  109.         break;
  110.         
  111.         default:
  112.             if (theChar != 0)
  113.                 DrawCmdKey(theChar, hiliteState, &itemRect, mdefData);
  114.         break;
  115.     }
  116. } // END DrawMenuItem
  117.  
  118. // ---------------------------------------------------------------------------
  119.  
  120. void DrawItemSeparator(Rect *itemRect, MDEFstuff *mdefData) {
  121.     // Find height of menu item, divided by two
  122.     short halfWay = (itemRect->bottom - itemRect->top) / 2;
  123.  
  124.     MoveTo(itemRect->left + 1, itemRect->top + halfWay);
  125.     W95LineTo(itemRect->right - 1, itemRect->top + halfWay,
  126.         &(mdefData->params).menuShadowColor,
  127.         &(mdefData->params).menuHiliteColor);
  128. } // END DrawItemSeparator
  129.  
  130. // ---------------------------------------------------------------------------
  131.  
  132. /*
  133.     Always use system font for drawing item marks, so checkmarks, bullets,
  134.     etc. will always be available no matter what font the user is using
  135.     for the menu text...
  136. */
  137. void DrawItemMark(
  138.     char        theMark,
  139.     short        hiliteState,
  140.     Rect        *itemRect,
  141.     MDEFstuff    *mdefData) {
  142.  
  143.     short textLocH, textLocV;
  144.     RGBColor *textColor;
  145.  
  146.     textLocH = itemRect->left + kWidthPadding;
  147.     textLocV = itemRect->bottom - kHeightPadding + kTextHtPadding;
  148.     TextFont(systemFont);
  149.  
  150.     MoveTo(textLocH, textLocV);
  151.     if (hiliteState != kMenuDisabled)
  152.         textColor = &(mdefData->black);
  153.     else
  154.         textColor = &(mdefData->params).menuShadowColor;
  155.     W95DrawChar(theMark, textColor, &(mdefData->params).menuHiliteColor);
  156.  
  157.     TextFont((mdefData->params).menuFont);
  158. } // END DrawItemMark
  159.  
  160. // ---------------------------------------------------------------------------
  161.  
  162. /*
  163.     The menuFace parameter in the 'MnuT' resource defines the textFace for
  164.     the entire menu. If however, an individual menu item has a text style
  165.     other than plain, it will override the settings in the 'MnuT' resource
  166.     for that menu.
  167. */
  168.  
  169. void DrawMenuItemText(
  170.     Str255        itemText,
  171.     short        hiliteState,
  172.     short        itemMarkOffset,
  173.     Rect        *itemRect,
  174.     MenuHandle    whichMenu,
  175.     short        menuItem,
  176.     MDEFstuff    *mdefData) {
  177.  
  178.     short textLocH, textLocV;
  179.     Style textStyle;
  180.     RGBColor *textColor;
  181.  
  182.     GetItemStyle(whichMenu, menuItem, &textStyle);
  183.     if (textStyle) {
  184.         TextFace(textStyle);
  185.     }
  186.  
  187.     /*
  188.         Determine which colors to draw the text in, based on whether
  189.         the text is disabled, and whether we adhere strictly to
  190.         the Windows95 look.
  191.     */
  192.     if (hiliteState == kMenuDisabled) {
  193.         textColor = &(mdefData->params).menuShadowColor;
  194.     }
  195.     else {
  196.         if ((mdefData->params).exactWin95Look) {
  197.             if (hiliteState == kMenuHilited)
  198.                 textColor = &mdefData->white;
  199.             else
  200.                 textColor = &mdefData->black;
  201.         }
  202.         else
  203.             textColor = &mdefData->black;
  204.     }
  205.  
  206.     textLocH = itemRect->left + kWidthPadding - 1 + itemMarkOffset;
  207.     textLocV = itemRect->bottom - kHeightPadding + kTextHtPadding;
  208.  
  209.     /*
  210.         If the item is disabled, or if we're not using the "exact"
  211.         Windows95 look, we draw the text embossed. Else we draw
  212.         the text plainly.
  213.     */
  214.     if (hiliteState == kMenuDisabled || !(mdefData->params).exactWin95Look) {
  215.         W95DrawTextAt((Ptr)&itemText[1], itemText[0],
  216.             textLocH, textLocV, textColor, &(mdefData->params).menuHiliteColor);
  217.     }
  218.     else {
  219.         RGBForeColor(textColor);
  220.         MoveTo(textLocH, textLocV);
  221.         DrawString(itemText);
  222.     }
  223.     
  224.     TextFace((mdefData->params).menuFace);
  225. } // END DrawMenuItemText
  226.  
  227. // ---------------------------------------------------------------------------
  228.  
  229. void DrawSubMenuTriangle(
  230.     Rect *itemRect,
  231.     short hiliteState,
  232.     MDEFstuff *mdefData) {
  233.  
  234.     // Draw submenu symbol
  235.     Rect subMenuRect;
  236.     RGBColor *lineColor;
  237.     RGBColor *hiliteColor;
  238.     RGBColor fillColor;
  239.  
  240.     SetRect(&subMenuRect, 0, 0, kSubMenuWd, kSubMenuHt);
  241.     subMenuRect.top = (itemRect->top +
  242.         ((itemRect->bottom - itemRect->top) / 2)) - (kSubMenuHt / 2);
  243.     subMenuRect.right = itemRect->right - kRectPadding - 4;
  244.     subMenuRect.left = subMenuRect.right - kSubMenuWd;
  245.     subMenuRect.bottom = subMenuRect.top + kSubMenuHt;
  246.  
  247.     if ((mdefData->params).exactWin95Look) {
  248.         if (hiliteState == kMenuHilited) {
  249.             fillColor.red = fillColor.green = fillColor.blue = 0xFFFF;
  250.         }
  251.         else if (hiliteState == kMenuUnhilited) {
  252.             fillColor.red = fillColor.green = fillColor.blue = 0x0000;
  253.         }
  254.         else {
  255.             // Disabled.
  256.             fillColor = (mdefData->params).menuShadowColor;
  257.         }
  258.         lineColor = hiliteColor = &fillColor;
  259.     }
  260.     else {
  261.         lineColor = &(mdefData->black);
  262.         hiliteColor = &(mdefData->params).menuHiliteColor;
  263.     }
  264.  
  265.  
  266.     W95DrawTriangleRight(&subMenuRect, (mdefData->params).exactWin95Look,
  267.         lineColor, hiliteColor, &fillColor);
  268. } // END DrawSubMenuTriangle
  269.  
  270. // ---------------------------------------------------------------------------
  271.  
  272. void DrawCmdKey(
  273.     char        theChar,
  274.     short        hiliteState,
  275.     Rect        *itemRect,
  276.     MDEFstuff    *mdefData) {
  277.  
  278.     short textLocH, textLocV;
  279.     short cmdWidth;
  280.     RGBColor *textColor;
  281.     
  282.     cmdWidth = CharWidth(kWidestChar);        // "W" is widest character (I think)
  283.     cmdWidth += CharWidth(kCmdKeyChar);        // "Cmd" key character
  284.     textLocH = itemRect->right - (kCmdKeyPadding + cmdWidth);
  285.     textLocV = itemRect->bottom - kHeightPadding + kTextHtPadding;
  286.  
  287.     if (hiliteState != kMenuDisabled)
  288.         textColor = &mdefData->black;
  289.     else
  290.         textColor = &(mdefData->params).menuShadowColor;
  291.     TextFont(systemFont);
  292.     TextFace(0);
  293.     MoveTo(textLocH, textLocV);
  294.     W95DrawChar(kCmdKeyChar, textColor, &(mdefData->params).menuHiliteColor);
  295.  
  296.     TextFont((mdefData->params).menuFont);
  297.     TextFace((mdefData->params).menuFace);
  298.     W95DrawChar(theChar, textColor, &(mdefData->params).menuHiliteColor);
  299. } // END  DrawCmdKey