home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / UMenuUtils.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  6.2 KB  |  219 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. //    UMenuUtils.cp
  21. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  22.  
  23. #include <UTextTraits.h>
  24. #include <LString.h>
  25.  
  26. #include <string.h>
  27.  
  28. #include "UMenuUtils.h"
  29. #include "cstring.h"
  30.  
  31. #include "net.h" // for NET_UnEscape
  32.  
  33. #ifndef __WINDOWS__
  34. #include <WIndows.h>
  35. #endif
  36.  
  37. #ifndef __LOWMEM__
  38. #include <LowMem.h>
  39. #endif
  40.  
  41. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  42. //    Ñ
  43. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  44.  
  45. Uint8 UMenuUtils::sEnabledDummy[] = "\px";
  46. Uint8 UMenuUtils::sDisabledDummy[] = "\p(x";
  47. Uint8 UMenuUtils::sDisabledSep[] = "\p(-";
  48.  
  49. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  50. //    Ñ
  51. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  52.  
  53. void UMenuUtils::PurgeMenuItems(MenuHandle inMenu, Int16 inAfter)
  54. {
  55.     Int16 theItemCount = ::CountMItems(inMenu) - inAfter;
  56.     while (theItemCount > 0)
  57.     {
  58.         ::DeleteMenuItem(inMenu, inAfter + 1);
  59.         theItemCount--;
  60.     }
  61. }
  62.  
  63. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  64. //    Ñ
  65. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  66.  
  67. Int16 UMenuUtils::InsertMenuItem(
  68.     MenuHandle         inMenu,
  69.     ConstStringPtr    inText,
  70.     Int16             inAfter,
  71.     Boolean            inEnabled)
  72. {
  73.     Int16 theItemIndex;
  74.     Int16 theItemCount;
  75.     
  76.     if (inAfter == 0)
  77.         theItemIndex = 1;
  78.     else
  79.         {
  80.         theItemCount = ::CountMItems(inMenu);
  81.         if (inAfter >= theItemCount)
  82.             theItemIndex = theItemCount + 1;
  83.         else
  84.             theItemIndex = inAfter + 1;
  85.         }
  86.  
  87.     if (inEnabled)
  88.         ::InsertMenuItem(inMenu, sEnabledDummy, inAfter);
  89.     else
  90.         ::InsertMenuItem(inMenu, sDisabledDummy, inAfter);
  91.     
  92.     ::SetMenuItemText(inMenu, theItemIndex, inText);
  93.     
  94.     return theItemIndex;
  95. }
  96.  
  97. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  98. //    Ñ
  99. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  100.  
  101. Int16 UMenuUtils::AppendMenuItem(
  102.     MenuHandle        inMenu,
  103.     ConstStringPtr    inText,
  104.     Boolean            inEnabled)
  105. {
  106.     Int16 theItemIndex = ::CountMItems(inMenu) + 1;
  107.     if (inEnabled)
  108.         ::AppendMenu(inMenu, sEnabledDummy);
  109.     else
  110.         ::AppendMenu(inMenu, sDisabledDummy);
  111.         
  112.     ::SetMenuItemText(inMenu, theItemIndex, inText);
  113.     
  114.     return theItemIndex;
  115. }
  116.  
  117. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  118. //    Ñ
  119. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  120.  
  121. void UMenuUtils::AppendSeparator(MenuHandle inMenu)
  122. {
  123.     ::AppendMenu(inMenu, sDisabledSep);
  124. }
  125.  
  126. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  127. //    Ñ
  128. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  129.  
  130. Int32 UMenuUtils::PopupWithTraits(
  131.     MenuHandle     inMenu,
  132.     Point        inGlobalWhere,
  133.     Int16         inTopItem,
  134.     ResIDT        inTraitsID)
  135. {
  136.     TextTraitsH theTraits = UTextTraits::LoadTextTraits(inTraitsID);
  137.     
  138.     CGrafPtr theWindowMgrPort;
  139.     ::GetCWMgrPort(&theWindowMgrPort);
  140.     
  141.     Int16 saveFontFam = LMGetSysFontFam();
  142.     Int16 saveFont = theWindowMgrPort->txFont;
  143.     Int16 saveSize = theWindowMgrPort->txSize;
  144.     Int16 saveFace = theWindowMgrPort->txFace;
  145.     
  146.     theWindowMgrPort->txFont = (*theTraits)->fontNumber;
  147.     theWindowMgrPort->txSize = (*theTraits)->size;
  148.     theWindowMgrPort->txFace = (*theTraits)->style;
  149.     LMSetSysFontFam((*theTraits)->fontNumber);
  150.  
  151.     Int32 theResult = ::PopUpMenuSelect(inMenu, inGlobalWhere.v, inGlobalWhere.h, inTopItem);
  152.     
  153.     theWindowMgrPort->txFont = saveFont;
  154.     theWindowMgrPort->txSize = saveSize;
  155.     theWindowMgrPort->txFace = saveFace;
  156.     LMSetSysFontFam(saveFontFam);
  157.  
  158.     return theResult;
  159. }
  160.  
  161. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  162. //    Ñ
  163. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  164. //    This is a copy of CreateMenuString that takes a char*
  165.  
  166. void UMenuUtils::AdjustStringForMenuTitle(cstring& inString)
  167. {
  168.     // NET_UnEscape returns a char* which, for now, is
  169.     // just the string passed in
  170.     NET_UnEscape(inString);
  171.     if ( LString::CStringLength(inString) > 50 )    // Cut it down to reasonable size
  172.     {
  173.         inString[50] = '\0';    // truncate inString
  174.         inString += "...";
  175.     }
  176. }
  177.  
  178.  
  179. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  180. //    Ñ
  181. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤=
  182. UInt16 UMenuUtils::GetMenuBarWidth()
  183. {
  184.     GDHandle    menuBarDevice;
  185.     Rect        screenRect;
  186.  
  187.     menuBarDevice = GetMainDevice();
  188.     screenRect = (**menuBarDevice).gdRect;
  189.     
  190.     return screenRect.right - screenRect.left;
  191. }
  192.  
  193.  
  194. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  195. //    Ñ
  196. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  197. void UMenuUtils::ConvertToIconMenu(
  198.     MenuHandle    inMenu,
  199.     ResIDT         inIconResID)
  200. {
  201.     Handle iconSuite;
  202.     
  203.     if (GetMenuBarWidth() >= 640) return;
  204.     
  205.     if (GetIconSuite(&iconSuite, inIconResID, kSelectorAllSmallData) == noErr)
  206.     {
  207.         //    Set the menu title to exactly 5 characters in length.
  208.         SetMenuItemText(inMenu, 0, "\p12345");
  209.         
  210.         char *menuData = *(char **)inMenu + sizeof(MenuInfo) - sizeof(Str255);
  211.         
  212.         //    Set the first byte of the title equal to 1 to indicate it's "iconized."
  213.         *(menuData + 1) = 1;
  214.         
  215.         //    Store the icon suite handle in the remaining 4 bytes of the title.
  216.         *(Handle *)(menuData + 2) = iconSuite;
  217.     }
  218. }
  219.