home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CGAIconPopup.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  6.4 KB  |  199 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. /*====================================================================================*/
  21.     #pragma mark INCLUDE FILES
  22. /*====================================================================================*/
  23.  
  24. #include "CGAIconPopup.h"
  25.  
  26. #include <UGAColorRamp.h>
  27. #include <UGraphicsUtilities.h>
  28.  
  29.  
  30. /*====================================================================================*/
  31.     #pragma mark TYPEDEFS
  32. /*====================================================================================*/
  33.  
  34.  
  35. /*====================================================================================*/
  36.     #pragma mark CONSTANTS
  37. /*====================================================================================*/
  38.  
  39. static const Int16 gsPopup_RightInset             =     24;        //    Used to position the title rect
  40. static const Int16 gsPopup_TitleInset            =     8;        //     Apple specification
  41. static const Int16 gsPopup_LabelOffset             =     2;        //    Offset of label from popup
  42.  
  43.  
  44. /*====================================================================================*/
  45.     #pragma mark INTERNAL CLASS DECLARATIONS
  46. /*====================================================================================*/
  47.  
  48.  
  49. /*====================================================================================*/
  50.     #pragma mark INTERNAL FUNCTION PROTOTYPES
  51. /*====================================================================================*/
  52.  
  53.  
  54. /*====================================================================================*/
  55.     #pragma mark CLASS IMPLEMENTATIONS
  56. /*====================================================================================*/
  57.  
  58. #pragma mark -
  59.  
  60. /*======================================================================================
  61.     Return the icon resource ID for the current title, 0 if none. Returns the actual
  62.     resource ID that can be used to access the icon using ::GetCIcon().
  63. ======================================================================================*/
  64.  
  65. Int16 CGAIconPopup::GetTitleIconID(void) {
  66.     
  67.     Int16 iconID;
  68.     ::GetItemIcon(GetMacMenuH(), mValue, &iconID);
  69.     
  70.     return (iconID ? iconID + 256 : 0);
  71. }
  72.  
  73.  
  74. /*======================================================================================
  75.     Refresh just the menu portion, not the title
  76. ======================================================================================*/
  77.  
  78. void CGAIconPopup::RefreshMenu(void) {
  79.  
  80.     if ( !IsVisible() || (GetSuperView() == nil) ) return;
  81.  
  82.     Rect refreshRect, superRevealed;
  83.     GetSuperView()->GetRevealedRect(superRevealed);
  84.     
  85.     CalcLocalPopupFrameRect(refreshRect);
  86.     ::InsetRect(&refreshRect, 0, 2);    // Dont refresh shadows
  87.     refreshRect.right -= gsPopup_RightInset;
  88.     refreshRect.left += gsPopup_TitleInset;
  89.     
  90.     LocalToPortPoint(topLeft(refreshRect));
  91.     LocalToPortPoint(botRight(refreshRect));
  92.     
  93.     if ( ::SectRect(&refreshRect, &superRevealed, &refreshRect) ) {
  94.         InvalPortRect(&refreshRect);
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. /*======================================================================================
  101.     Get the title rect. Fix a bug in class that doesn't center text vertically.
  102. ======================================================================================*/
  103.  
  104. void CGAIconPopup::CalcTitleRect(Rect &outRect) {
  105.     
  106.     StTextState theTextState;
  107.     const Int16 bevelWidth = 2;
  108.     
  109.     UTextTraits::SetPortTextTraits(GetTextTraitsID());
  110.     
  111.     FontInfo fInfo;
  112.     GetFontInfo(&fInfo);
  113.     Int16 textHeight = fInfo.ascent + fInfo.descent;
  114.     
  115.     CalcLocalPopupFrameRect(outRect);
  116.     ::InsetRect(&outRect, 0, bevelWidth);
  117.     outRect.right -= gsPopup_RightInset;
  118.     outRect.left += gsPopup_TitleInset;
  119.     
  120.     outRect.top += ((UGraphicsUtilities::RectHeight(outRect) - textHeight) / 2) - 1;
  121.     outRect.bottom = outRect.top + textHeight;
  122.  
  123.     if ( GetTitleIconID() ) {
  124.         outRect.left += cMenuIconWidth + cMenuIconTitleMargin;
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. /*======================================================================================
  131.     Get the label rect. Fix a bug in class that doesn't center text vertically.
  132. ======================================================================================*/
  133.  
  134. void CGAIconPopup::CalcLabelRect(Rect &outRect) {
  135.     
  136.     if ( HasLabel() ) {
  137.         StTextState theTextState;
  138.         const Int16 bevelWidth = 2;
  139.         
  140.         UTextTraits::SetPortTextTraits(GetTextTraitsID());
  141.         
  142.         FontInfo fInfo;
  143.         GetFontInfo(&fInfo);
  144.         Int16 textHeight = fInfo.ascent + fInfo.descent;
  145.             
  146.         CalcLocalFrameRect(outRect);
  147.         outRect.right = outRect.left + (mLabelWidth - gsPopup_LabelOffset);
  148.         ::InsetRect(&outRect, 0, bevelWidth);
  149.         
  150.         outRect.top += ((UGraphicsUtilities::RectHeight(outRect) - textHeight) / 2) - 1;
  151.         outRect.bottom = outRect.top + textHeight;
  152.     } else {
  153.         outRect = gEmptyRect;
  154.     }
  155. }
  156.  
  157.  
  158.  
  159. /*======================================================================================
  160.     Draw the popup title
  161. ======================================================================================*/
  162.  
  163. void CGAIconPopup::DrawPopupTitle(void) {
  164.     
  165.     LGAPopup::DrawPopupTitle();
  166.     
  167.     Int16 iconID = GetTitleIconID();
  168.     
  169.     if ( iconID != 0 ) {
  170.     
  171.         CIconHandle theIconH = ::GetCIcon(iconID);
  172.         
  173.         if ( theIconH != nil ) {
  174.             Rect iconRect;
  175.             LGAPopup::CalcTitleRect(iconRect);
  176.             
  177.             iconRect.right = iconRect.left + cMenuIconWidth;
  178.             iconRect.top = iconRect.top + ((iconRect.bottom - iconRect.top - cMenuIconHeight) / 2);
  179.             iconRect.bottom = iconRect.top + cMenuIconHeight;
  180.             
  181.             Int16 transform = ttNone;
  182.             
  183.             if ( IsEnabled() ) {
  184.                 if ( IsHilited() ) {
  185.                     transform = ttSelected;
  186.                 }
  187.             } else {
  188.                 transform = ttDisabled;
  189.             }
  190.         
  191.             ::PlotCIconHandle(&iconRect, ttNone, transform, theIconH);
  192.             
  193.             ::DisposeCIcon(theIconH);
  194.         }
  195.     }
  196. }
  197.  
  198.  
  199.