home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / prefs / CSizePopup.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  12.5 KB  |  452 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. /* Portions copyright Metrowerks Corporation. */
  20.  
  21. #include "CSizePopup.h"
  22.  
  23. #include "PascalString.h"
  24. #include "uerrmgr.h"
  25. #include "resgui.h"
  26. #include "macutil.h"
  27. #include "UModalDialogs.h"
  28. #include "UGraphicGizmos.h"
  29.  
  30. #include <UNewTextDrawing.h>
  31. #include <UGAColorRamp.h>
  32. #include <UGraphicsUtilities.h>
  33.  
  34. static const Char16    gsPopup_SmallMark            =    'Ñ';    // Mark used for small font popups
  35. static const Int16 gsPopup_ArrowButtonWidth     =     22;    //    Width used in drawing the arrow only
  36. static const Int16 gsPopup_ArrowButtonHeight    =     16;    //    Height used for drawing arrow only
  37. static const Int16 gsPopup_ArrowHeight            =     5;        //    Actual height of the arrow
  38. static const Int16 gsPopup_ArrowWidth            =     9;        //    Actual width of the arrow at widest
  39.  
  40. //-----------------------------------
  41. CSizePopup::CSizePopup(LStream* inStream)
  42. //-----------------------------------
  43. :    mFontNumber(0)
  44. ,    LGAPopup(inStream)
  45. {
  46. }
  47.  
  48. //-----------------------------------
  49. Int32 CSizePopup::GetMenuSize() const
  50. //-----------------------------------
  51. {
  52.     Int32        size = 0;
  53.     MenuHandle    menuH = const_cast<CSizePopup*>(this)->GetMacMenuH();
  54.     
  55.     if (menuH)
  56.     {
  57.         size = ::CountMItems(menuH);
  58.     }
  59.     
  60.     return size;
  61. }
  62.  
  63. //-----------------------------------
  64. void CSizePopup::SetUpCurrentMenuItem(MenuHandle    inMenuH, Int16 inCurrentItem )
  65. //-----------------------------------
  66. {
  67.     
  68.     // Ñ If the current item has changed then make it so, this
  69.     // also involves removing the mark from any old item
  70.     if (inMenuH)
  71.     {
  72.         CStr255        sizeString;
  73.         CStr255        itemString;
  74.         Handle        ellipsisHandle = nil;
  75.         char        ellipsisChar;    
  76.         short        menuSize = ::CountMItems(inMenuH);
  77.         
  78.         ThrowIfNil_(ellipsisHandle = ::GetResource('elps', 1000));
  79.  
  80.         ellipsisChar = **ellipsisHandle;
  81.         ::ReleaseResource(ellipsisHandle);
  82.  
  83.         if (inCurrentItem == ::CountMItems(inMenuH))
  84.         {
  85.             itemString = ::GetCString(OTHER_FONT_SIZE);
  86.             ::StringParamText(itemString, (SInt32) GetFontSize(), 0, 0, 0);
  87.         }
  88.         else
  89.         {
  90.             itemString = ::GetCString(OTHER_RESID);
  91.             itemString += ellipsisChar;
  92.         }
  93.         
  94.         ::SetMenuItemText(inMenuH, menuSize, itemString);
  95.  
  96.         // Ñ Get the current value
  97.         Int16 oldItem = GetValue();
  98.         
  99.         // Ñ Remove the current mark
  100.         ::SetItemMark(inMenuH, oldItem, 0);
  101.         
  102.         // Ñ Always make sure item is marked
  103.         Char16    mark = GetMenuFontSize() < 12 ? gsPopup_SmallMark : checkMark;
  104.         ::SetItemMark(inMenuH, inCurrentItem,  mark);
  105.     }
  106.     
  107. }
  108.  
  109. //-----------------------------------
  110. Int32 CSizePopup::GetFontSizeFromMenuItem(Int32 inMenuItem) const
  111. //-----------------------------------
  112. {
  113.     Str255        sizeString;
  114.     Int32        fontSize = 0;
  115.  
  116.     ::GetMenuItemText(const_cast<CSizePopup*>(this)->GetMacMenuH(), inMenuItem, sizeString);
  117.     
  118.     myStringToNum(sizeString, &fontSize);
  119.     return fontSize;
  120. }
  121.  
  122. //-----------------------------------
  123. void CSizePopup::SetFontSize(Int32 inFontSize)
  124. //-----------------------------------
  125. {
  126.     mFontSize = inFontSize;
  127.     if (inFontSize)
  128.     {
  129.         MenuHandle sizeMenu = GetMacMenuH();
  130.         short menuSize = ::CountMItems(sizeMenu);
  131.         Boolean isOther = true;
  132.         for (int i = 1; i <= menuSize; ++i)
  133.         {
  134.             CStr255 sizeString;
  135.             ::GetMenuItemText(sizeMenu, i, sizeString);
  136.             Int32 fontSize = 0;
  137.             myStringToNum(sizeString, &fontSize);
  138.             if (fontSize == inFontSize)
  139.             {
  140.                 SetValue(i);
  141.                 if (i != menuSize)
  142.                     isOther = false;
  143.                 break;
  144.             }
  145.         }        
  146.         if (isOther)
  147.             SetValue(menuSize);
  148.     }
  149. }
  150.  
  151. //-----------------------------------
  152. void CSizePopup::SetValue(Int32 inValue)
  153. //-----------------------------------
  154. {
  155.     // Ñ We intentionally do not guard against setting the value to the 
  156.     //        same value the popup current has. This is so that the other
  157.     //        size stuff works correctly.
  158.  
  159.     // Ñ Get the current item setup in the menu
  160.     MenuHandle menuH = GetMacMenuH();
  161.     if ( menuH )
  162.     {
  163.         SetUpCurrentMenuItem( menuH, inValue );
  164.     }
  165.  
  166.     if (inValue < mMinValue) {        // Enforce min/max range
  167.         inValue = mMinValue;
  168.     } else if (inValue > mMaxValue) {
  169.         inValue = mMaxValue;
  170.     }
  171.  
  172.     mValue = inValue;            //   Store new value
  173.     BroadcastValueMessage();    //   Inform Listeners of value change
  174.  
  175.     // Ñ Now we need to get the popup redrawn so that the change
  176.     // will be seen
  177.     Draw( nil );
  178.     
  179. }    //    LGAPopup::SetValue
  180.  
  181. //-----------------------------------
  182. Boolean CSizePopup::TrackHotSpot(Int16 inHotSpot, Point inPoint, Int16 inModifiers)
  183. //-----------------------------------
  184. {
  185.     // Portions of this function are from LGAPopup.cp in PowerPlant
  186.  
  187.     // Ñ We only want the popup menu to appear if the mouse went down
  188.     // in the our hot spot which is the popup portion of the control
  189.     // not the label area
  190.     if ( PointInHotSpot( inPoint, inHotSpot ))
  191.     {
  192.         // Ñ Get things started off on the right foot
  193.         Boolean        currInside = true;
  194.         Boolean        prevInside = false;
  195.         HotSpotAction( inHotSpot, currInside, prevInside );
  196.  
  197.         // Ñ We skip the normal tracking that is done in the control as
  198.         // the call to PopupMenuSelect will take control of the tracking
  199.         // once the menu is up
  200.         // Ñ Now we need to handle the display of the actual popup menu
  201.         // we start by setting up some values that we will need
  202.         Int16    menuID = 0;
  203.         Int16 menuItem = GetValue();
  204.         Int16    currItem = IsPulldownMenu() ? 1 : GetValue();
  205.         Point popLoc;
  206.         GetPopupMenuPosition( popLoc );
  207.         
  208.         // Ñ Call our utility function which handles the display of the menu
  209.         // menu is disposed of inside this function
  210.         HandlePopupMenuSelect( popLoc, currItem, menuID, menuItem );
  211.         
  212.         if ( menuItem > 0)
  213.         {
  214.             if ( menuItem == ::CountMItems( GetMacMenuH() ))
  215.             {
  216.                 StDialogHandler handler(Wind_OtherSizeDialog, nil);
  217.                 LWindow* dialog = handler.GetDialog();
  218.                 LEditField    *sizeField = (LEditField *)dialog->FindPaneByID(1504);
  219.                 Assert_(sizeField);
  220.                 sizeField->SetValue(GetFontSize());
  221.                 sizeField->SelectAll();
  222.  
  223.                 // Run the dialog
  224.                 MessageT message = msg_Nothing;
  225.                 do
  226.                 {
  227.                     message = handler.DoDialog();
  228.                 } while (message == msg_Nothing);
  229.  
  230.                 if (msg_ChangeFontSize == message)
  231.                 {
  232.                     SetFontSize(sizeField->GetValue());
  233.                 }
  234.             }
  235.             else
  236.             {
  237.                 SetFontSize(GetFontSizeFromMenuItem(menuItem));
  238.             }
  239.         }
  240.             
  241.         // Ñ Make sure that we get the HotSpotAction called one last time
  242.         HotSpotAction( inHotSpot, false, true );
  243.         
  244.         return menuItem > 0;
  245.     }
  246.     else
  247.         return false;
  248.         
  249. }
  250.  
  251. //-----------------------------------
  252. void CSizePopup::MarkRealFontSizes(LGAPopup *fontPopup)
  253. //-----------------------------------
  254. {
  255.     CStr255    fontName;
  256.     ::GetMenuItemText(    fontPopup->GetMacMenuH(),
  257.                         fontPopup->GetValue(),
  258.                         fontName);
  259.     GetFNum(fontName, &mFontNumber);
  260.     MarkRealFontSizes(mFontNumber);
  261. }
  262.  
  263. //-----------------------------------
  264. void CSizePopup::MarkRealFontSizes(short fontNum)
  265. //-----------------------------------
  266. {
  267.     Str255            itemString;
  268.     MenuHandle        sizeMenu;
  269.     short            menuSize;
  270.     
  271.     sizeMenu = GetMacMenuH();
  272.     menuSize = CountMItems(sizeMenu);
  273.     
  274.     for (short menuItem = 1; menuItem <= menuSize; ++menuItem)
  275.     {
  276.         Int32    fontSize;
  277.         ::GetMenuItemText(sizeMenu, menuItem, itemString);
  278.         fontSize = 0;
  279.         myStringToNum(itemString, &fontSize);
  280.  
  281.         Style    theSyle;
  282.  
  283.         if (fontSize && RealFont(fontNum, fontSize))
  284.         {
  285.             theSyle = outline;
  286.         }
  287.         else
  288.         {
  289.             theSyle = normal;
  290.         }
  291.         ::SetItemStyle(    sizeMenu,
  292.                         menuItem,
  293.                         theSyle);
  294.     }
  295. }
  296.  
  297. //-----------------------------------
  298. void CSizePopup::DrawPopupTitle()
  299. // DrawPopupTitle is overridden to draw in the outline style
  300. // as needed
  301. //-----------------------------------
  302. {
  303.     StColorPenState    theColorPenState;
  304.     StTextState         theTextState;
  305.     
  306.     // Ñ Get some loal variables setup including the rect for the title
  307.     ResIDT    textTID = GetTextTraitsID();
  308.     Rect    titleRect;
  309.     Str255 title;
  310.     GetCurrentItemTitle( title );
  311.     
  312.     // Ñ Figure out what the justification is from the text trait and 
  313.     // get the port setup with the text traits
  314.     UTextTraits::SetPortTextTraits( textTID );
  315.         
  316.     // Ñ Set outline style if it's an outline size
  317.  
  318.     if (GetMacMenuH() && GetValue() != ::CountMItems(GetMacMenuH()))
  319.     {
  320.         Int32    fontSize;
  321.         CStr255 itemString;
  322.  
  323.         ::GetMenuItemText(GetMacMenuH(), GetValue(), itemString);
  324.         fontSize = 0;
  325.         myStringToNum(itemString, &fontSize);
  326.  
  327.         Style    theSyle;
  328.  
  329.         if (fontSize && ::RealFont(mFontNumber, fontSize))
  330.         {
  331.             theSyle = outline;
  332.         }
  333.         else
  334.         {
  335.             theSyle = normal;
  336.         }
  337.         
  338.         ::TextFace(theSyle);
  339.     }
  340.     
  341.     // Ñ Set up the title justification which is always left justified
  342.     Int16    titleJust = teFlushLeft;
  343.     
  344.     // Ñ Get the current item's title
  345.     Str255 currentItemTitle;
  346.     GetCurrentItemTitle( currentItemTitle );
  347.     
  348.     // Ñ Calculate the title rect
  349.     CalcTitleRect( titleRect );
  350.     
  351.     // Ñ Kludge for drawing (correctly) in outline style left-justified
  352.     Rect actualRect;
  353.     UNewTextDrawing::MeasureWithJustification(
  354.                                     (char*) ¤tItemTitle[1],
  355.                                     currentItemTitle[0],
  356.                                     titleRect,
  357.                                     titleJust,
  358.                                     actualRect);
  359.     actualRect.right += 2;    
  360.     titleRect = actualRect;
  361.     titleJust = teJustRight;                                
  362.     
  363.     // Ñ Set up the text color which by default is black
  364.     RGBColor    textColor;
  365.     ::GetForeColor( &textColor );
  366.  
  367.     // Ñ Loop over any devices we might be spanning and handle the drawing
  368.     // appropriately for each devices screen depth
  369.     StDeviceLoop    theLoop( titleRect );
  370.     Int16                depth;
  371.     while ( theLoop.NextDepth( depth )) 
  372.     {
  373.         if ( depth < 4 )        //    Ñ BLACK & WHITE
  374.         {
  375.             // Ñ If the control is dimmed then we use the grayishTextOr 
  376.             // transfer mode to draw the text
  377.             if ( !IsEnabled())
  378.             {
  379.                 ::RGBForeColor( &UGAColorRamp::GetBlackColor() );
  380.                 ::TextMode( grayishTextOr );
  381.             }
  382.             else if ( IsEnabled() && IsHilited() )
  383.             {
  384.                 // Ñ When we are hilited we simply draw the title in white
  385.                 ::RGBForeColor( &UGAColorRamp::GetWhiteColor() );
  386.             }
  387.                 
  388.             // Ñ Now get the actual title drawn with all the appropriate settings
  389.             UTextDrawing::DrawWithJustification(
  390.                                                 (char*) ¤tItemTitle[1],
  391.                                                 currentItemTitle[0],
  392.                                                 titleRect,
  393.                                                 titleJust);
  394.         }
  395.         else    // Ñ COLOR
  396.         {
  397.             // Ñ If control is selected we always draw the text in the title
  398.             // hilite color, if requested
  399.             if ( IsHilited())
  400.                 ::RGBForeColor( &UGAColorRamp::GetWhiteColor() );
  401.         
  402.             // Ñ If the box is dimmed then we have to do our own version of the
  403.             // grayishTextOr as it does not appear to work correctly across
  404.             // multiple devices
  405.             if ( !IsEnabled() || !IsActive())
  406.             {
  407.                 textColor = UGraphicsUtilities::Lighten( &textColor );
  408.                 ::TextMode( srcOr );
  409.                 ::RGBForeColor( &textColor );
  410.             }
  411.                 
  412.             // Ñ Now get the actual title drawn with all the appropriate settings
  413.             UTextDrawing::DrawWithJustification(
  414.                                                 (char*) ¤tItemTitle[1],
  415.                                                 currentItemTitle[0],
  416.                                                 titleRect,
  417.                                                 titleJust);
  418.         }    
  419.     }
  420. } // CSizePopup::DrawPopupTitle
  421.  
  422. //-----------------------------------
  423. void CSizePopup::DrawPopupArrow()
  424. //-----------------------------------
  425. {
  426.     StColorPenState    theColorPenState;
  427.     
  428.     // Ñ Get the local popup frame rect
  429.     Rect    popupFrame;
  430.     CalcLocalPopupFrameRect( popupFrame );
  431.     
  432.     // Ñ Set up some variables used in the drawing loop
  433.     Int16        start = (( UGraphicsUtilities::RectHeight( popupFrame ) - gsPopup_ArrowHeight) / 2) + 1;
  434.  
  435.     // Ñ Figure out the left and right edges based on whether we are drawing
  436.     // only the arrow portion or the entire popup
  437.     Int16        leftEdge = gsPopup_ArrowButtonWidth - 6; 
  438.     Int16        rightEdge = leftEdge - (gsPopup_ArrowWidth - 1);
  439.  
  440.     popupFrame.top        = popupFrame.top    + start; 
  441.     popupFrame.bottom    = popupFrame.top    + gsPopup_ArrowHeight - 1; 
  442.     popupFrame.left        = popupFrame.right    - leftEdge; 
  443.     popupFrame.right    = popupFrame.right    - rightEdge; 
  444.     
  445.     UGraphicGizmos::DrawPopupArrow(
  446.                                     popupFrame,
  447.                                     IsEnabled(),
  448.                                     IsActive(),
  449.                                     IsHilited());
  450. } // CSizePopup::DrawPopupArrow
  451.  
  452.