home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / LCustomizeMenu.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  7.4 KB  |  340 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #include "prtypes.h"
  21.  
  22. #include "LCustomizeMenu.h"
  23.  
  24. //
  25. //    Constructor
  26. //
  27. LCustomizeMenu::LCustomizeMenu()
  28.     : LPatchedMenu()
  29. {
  30.     fItemHeight = 16;    // fix constant, changable
  31.     fItemCount = 0;
  32.     fTextMargin = 14;    // fix constant, changable
  33.     fMarkMargin = 2;    // fix constant, changable
  34. }
  35. //
  36. //    Size
  37. //
  38. void LCustomizeMenu::Size(MenuHandle menu, MenuDefUPP* root, Rect *rect, Point hitPt, short *item)
  39. {
  40.     fItemCount = ::CountMItems(menu);
  41.     int maxWidth = fTextMargin + 4;
  42.     fItemCount = ::CountMItems(menu);
  43.     for(int i = 1; i <= fItemCount; i++)
  44.     {
  45.         short itemWidth = MeasureItem(menu,  i);
  46.         if(itemWidth > maxWidth)
  47.             maxWidth = itemWidth;
  48.     }    
  49.     (**menu).menuHeight = fItemHeight * fItemCount;    
  50.      (**menu).menuWidth = maxWidth;
  51. }
  52.  
  53. //
  54. //    MeasureItem
  55. //
  56. short LCustomizeMenu::MeasureItem(MenuHandle menu, int item)
  57. {
  58.     Boolean AddSlop = FALSE;
  59.     short itemWidth = fTextMargin + 4;
  60.     Str255 itemtext;
  61.     ::GetMenuItemText(menu, item, itemtext);
  62.     if(itemtext[1] != '-')
  63.     {
  64.         short  iconindex;
  65.         short  cmd;
  66.         short  mark;
  67.         
  68.         // Get the needed Item data
  69.         ::GetItemCmd(menu, item, &cmd);
  70.         ::GetItemIcon(menu, item, &iconindex);
  71.         ::GetItemMark(menu, item, &mark);
  72.         
  73.         itemWidth += MeasureItemText(itemtext);
  74.         if(HaveCommand(cmd))    // add command width
  75.         {
  76.             itemWidth += MeasureItemCommand(cmd);
  77.             AddSlop = TRUE;
  78.         }
  79.         if(HaveSubmenu(cmd))    // add SubmenuIndicator width
  80.         {
  81.             itemWidth += MeasureItemSubmenuIndicator();
  82.             AddSlop = TRUE;
  83.         }
  84.         if(AddSlop)
  85.             itemWidth += MeasureItemSlop();
  86.     }
  87.     return itemWidth;
  88. }
  89. //
  90. //    MeasureItemText
  91. //
  92. short LCustomizeMenu::MeasureItemText(Str255 text)
  93. {
  94.     return ::StringWidth(text);
  95. }
  96. //
  97. //    MeasureItemCommand
  98. //
  99. short LCustomizeMenu::MeasureItemCommand(short command)
  100. {
  101.     ::TextFont(systemFont);
  102.     return ::CharWidth(17) + ::CharWidth(command);
  103. }
  104.  
  105. //
  106. //    Draw
  107. //
  108. void LCustomizeMenu::Draw(MenuHandle menu, MenuDefUPP* root, Rect *rect, Point hitPt, short *item)
  109. {
  110.     Assert_(fItemCount != 0);
  111.     Assert_(fItemHeight != 0);    
  112.     for(int i = 1; i <= fItemCount; i++)
  113.     {
  114.         Rect itemrect;
  115.         GetItemRect(i, rect, itemrect);
  116.         DrawItem(menu, i, itemrect);
  117.     }
  118. }
  119. //
  120. //    DrawItemSeparator
  121. //
  122. void LCustomizeMenu::DrawItemSeperator(Rect& itemrect )
  123. {
  124.     ::PenPat(&qd.gray);
  125.     ::MoveTo(itemrect.left, itemrect.top + 8);
  126.     ::LineTo(itemrect.right, itemrect.top + 8);
  127.     ::PenNormal();
  128. }
  129. //
  130. //    DrawItemIcon
  131. //    ÑÑ╩Unsupport Yet
  132. //
  133. void LCustomizeMenu::DrawItemIcon(Rect& itemrect, short iconindex)
  134. {
  135.     Assert_(FALSE);
  136.     // We current do not support Icon
  137. }
  138. //
  139. //    DrawItemCommand
  140. //
  141. void LCustomizeMenu::DrawItemCommand(Rect& itemrect, short cmd)
  142. {
  143.     ::DrawChar(17);
  144.     ::DrawChar(cmd);
  145. }
  146. //
  147. //    DrawItemSubmenuIndicator
  148. //    ÑÑ╩Unsupport Yet
  149. //
  150. void LCustomizeMenu::DrawItemSubmenuIndicator(Rect& itemrect)
  151. {
  152.     Assert_(FALSE);
  153.     // We current do not support SubmenuIndicator
  154. }
  155. //
  156. //    InvertItem
  157. //
  158. void LCustomizeMenu::InvertItem(MenuHandle menu, int item, Rect* menurect, Boolean leaveblack)
  159. {
  160.     RgnHandle rgn;
  161.     rgn = ::NewRgn();
  162.     ::GetClip(rgn);
  163.     
  164.     Rect itemrect;
  165.     GetItemRect(item, menurect, itemrect);
  166.     
  167.     ::EraseRect(&itemrect);
  168.     ::ClipRect(&itemrect);
  169.     
  170.     DrawItem( menu, item, itemrect);
  171.     
  172.     ::SetClip(rgn);
  173.     ::DisposeRgn(rgn);
  174.     if(leaveblack)
  175.         ::InvertRect(&itemrect);
  176. }
  177. //
  178. //    DrawItemDisable
  179. //
  180. void LCustomizeMenu::DrawItemDisable(Rect& itemrect)
  181. {
  182.     ::PenPat(&qd.gray);
  183.     ::PenMode(patBic);
  184.     itemrect.right++;
  185.     itemrect.left++;
  186.     ::PaintRect(&itemrect);
  187.     ::PenNormal();
  188. }
  189. //
  190. //    GetItemRect
  191. //
  192.  
  193. void LCustomizeMenu::GetItemRect(int i, Rect* menurect, Rect& itemrect)
  194. {
  195.     if(i > 0)
  196.     {
  197.         SetRect(&itemrect, menurect->left, menurect->top + ((i-1) * fItemHeight) , menurect->right, 0 );
  198.         itemrect.bottom = itemrect.top + fItemHeight;
  199.     }
  200.     else
  201.         SetRect(&itemrect, 0, 0 , 0, 0);
  202. }
  203. //
  204. //    MoveToItemTextPosition
  205. //
  206. void LCustomizeMenu::MoveToItemTextPosition(Rect& itemrect)
  207. {
  208.     FontInfo finfo;
  209.     ::GetFontInfo(&finfo);
  210.     ::MoveTo(itemrect.left + fTextMargin, itemrect.bottom - finfo.descent);    
  211. }
  212. //
  213. //    MoveToItemCommandPosition
  214. //
  215. void LCustomizeMenu::MoveToItemCommandPosition(Rect& itemrect)
  216. {
  217.     FontInfo finfo;
  218.     ::GetFontInfo(&finfo);
  219.     ::MoveTo(itemrect.left -24, itemrect.bottom - finfo.descent);    
  220. }
  221. //
  222. //    MoveToItemMarkPosition
  223. //
  224. void LCustomizeMenu::MoveToItemMarkPosition(Rect& itemrect)
  225. {
  226.     FontInfo finfo;
  227.     ::GetFontInfo(&finfo);
  228.     ::MoveTo(itemrect.left + fMarkMargin, itemrect.bottom - finfo.descent);    
  229. }
  230. //
  231. //    DrawItemText
  232. //
  233. void LCustomizeMenu::DrawItemText(Rect& itemrect, Str255 itemtext )
  234. {
  235.     ::DrawString(itemtext);    
  236. }
  237. //
  238. //    DrawItemMark
  239. //
  240. void LCustomizeMenu::DrawItemMark(Rect& itemrect, short mark )
  241. {
  242.     ::DrawChar(mark);
  243. }
  244. //
  245. //    DrawItem
  246. //
  247. void LCustomizeMenu::DrawItem(MenuHandle menu, int item, Rect& itemrect )
  248. {
  249.     if((item <=0) || (item > fItemCount))
  250.         return;
  251.     Str255 itemtext;
  252.     ::GetMenuItemText(menu, item, itemtext);
  253.     if(itemtext[1] == '-')
  254.     {
  255.         // Draw Seperator
  256.         DrawItemSeperator(itemrect );
  257.     }
  258.     else
  259.     {
  260.         Style style;
  261.         short  iconindex;
  262.         short  cmd;
  263.         short  mark;
  264.         
  265.         // Get the needed Item data
  266.         ::GetItemStyle(menu, item, &style);
  267.         ::GetItemCmd(menu, item, &cmd);
  268.         ::GetItemIcon(menu, item, &iconindex);
  269.         ::GetItemMark(menu, item, &mark);
  270.  
  271.         // Deal with Item Text First
  272.         ::TextFace(style);
  273.         MoveToItemTextPosition(itemrect);
  274.         DrawItemText(itemrect, itemtext );        
  275.         ::TextFace(normal);
  276.  
  277.         // Draw Mark if necessary
  278.         if(HaveMark(cmd, mark))
  279.         {
  280.             ::TextFont(systemFont);
  281.             MoveToItemMarkPosition(itemrect);
  282.             DrawItemMark(itemrect, mark);
  283.         }
  284.         // Draw Icon if necessary
  285.         if(HaveIcon(cmd, iconindex))
  286.         {
  287.             DrawItemIcon(itemrect, iconindex);
  288.         }
  289.         // Draw Command if necessary
  290.         if(HaveCommand(cmd))
  291.         {
  292.             ::TextFont(systemFont);
  293.             MoveToItemCommandPosition(itemrect);
  294.             DrawItemCommand(itemrect, cmd);
  295.         }
  296.         // Draw SubmenuIndicator if necessary
  297.         if(HaveSubmenu(cmd))
  298.         {
  299.             DrawItemSubmenuIndicator(itemrect);
  300.         }
  301.         // Grayout if necessary
  302.         if( ! ItemEnable(menu, item) )
  303.             DrawItemDisable(itemrect);
  304.     }
  305. }
  306. //
  307. //    Choose
  308. //
  309. void LCustomizeMenu::Choose(MenuHandle menu, MenuDefUPP* root, Rect *rect, Point hitPt, short *item)
  310. {
  311.     int olditem = *item;
  312.     *item = 0;
  313.     if( ::PtInRect(hitPt, rect))
  314.     {
  315.         int y = ((hitPt.v - rect->top) / fItemHeight) + 1;
  316.         Rect tmprect;
  317.         GetItemRect(y, rect, tmprect);
  318.         
  319.         *item = y;
  320.         if(*item != olditem)
  321.         {
  322.             if(ItemEnable(menu, *item))
  323.             {
  324.                 InvertItem(menu, *item, rect, TRUE);
  325.                 InvertItem(menu, olditem, rect, FALSE);        
  326.             }
  327.             else
  328.             {
  329.                 InvertItem(menu, olditem, rect, FALSE);
  330.                 *item = 0;
  331.             }
  332.         }
  333.     }
  334.     else
  335.     {
  336.         InvertItem(menu, olditem, rect, FALSE);
  337.     }
  338. }
  339.  
  340.