home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CToolbarButton.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.0 KB  |  161 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. #include "CToolbarButton.h"
  20. #include "UGraphicGizmos.h"
  21.  
  22. // "Magic" constants
  23. const Int16 cIconOnlyHeight = 32;
  24. const Int16 cIconOnlyWidth = 36;
  25. const Int16 cTextOnlyHeight = 21;
  26.  
  27. CToolbarButton::CToolbarButton(LStream* inStream)
  28.     :    CButton(inStream),
  29.         mCurrentMode(defaultMode),
  30.         mOriginalWidth(0),
  31.         mOriginalHeight(0)
  32. {
  33. }
  34.  
  35. CToolbarButton::~CToolbarButton()
  36. {
  37. }
  38.  
  39. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  40. //    Ñ    FinishCreateSelf
  41. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  42. void CToolbarButton::FinishCreateSelf()
  43. {
  44.     CButton::FinishCreateSelf();
  45.  
  46.     Rect theButtonRect;
  47.     CalcLocalFrameRect(theButtonRect);
  48.  
  49.     mOriginalWidth = theButtonRect.right - theButtonRect.left;
  50.     mOriginalHeight = theButtonRect.bottom - theButtonRect.top;
  51. }
  52.  
  53. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  54. //    Ñ    DrawSelf
  55. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  56.  
  57. void CToolbarButton::DrawSelf()
  58. {
  59.     PrepareDrawButton();
  60.  
  61.     DrawButtonContent();
  62.  
  63.     if (mCurrentMode != eMode_IconsOnly && mTitle.Length() > 0)
  64.         DrawButtonTitle();
  65.  
  66.     if (mCurrentMode != eMode_TextOnly && GetGraphicID() != 0)
  67.         DrawButtonGraphic();
  68.             
  69.     if (!IsEnabled() || !IsActive())
  70.         DrawSelfDisabled();
  71.             
  72.     FinalizeDrawButton();
  73. }
  74.  
  75. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  76. //    Ñ    ChangeMode
  77. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  78.  
  79. Boolean CToolbarButton::ChangeMode(Int8 inNewMode, SDimension16& outDimensionDeltas)
  80. {
  81.     SDimension16 oldDimensions;
  82.     GetFrameSize(oldDimensions);
  83.  
  84.     outDimensionDeltas.width = 0;
  85.     outDimensionDeltas.height = 0;
  86.  
  87.     mCurrentMode = inNewMode;
  88.  
  89.     switch (inNewMode)
  90.     {
  91.         case eMode_IconsOnly:
  92.             outDimensionDeltas.width = cIconOnlyWidth - oldDimensions.width;
  93.             outDimensionDeltas.height = cIconOnlyHeight - oldDimensions.height;
  94.             break;
  95.         
  96.         case eMode_TextOnly:
  97.             outDimensionDeltas.width = mOriginalWidth - oldDimensions.width;
  98.             outDimensionDeltas.height = cTextOnlyHeight - oldDimensions.height;
  99.             break;
  100.         
  101.         case eMode_IconsAndText:
  102.             outDimensionDeltas.width = mOriginalWidth - oldDimensions.width;
  103.             outDimensionDeltas.height = mOriginalHeight - oldDimensions.height;
  104.             break;
  105.     }
  106.  
  107.     ResizeFrameBy(outDimensionDeltas.width, outDimensionDeltas.height, true);
  108.     return true;
  109. }
  110.  
  111. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  112. //    Ñ    DrawButtonTitle
  113. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  114.  
  115. void CToolbarButton::DrawButtonTitle(void)
  116. {
  117.     if (mCurrentMode == eMode_TextOnly && (!IsActive() || !IsEnabled()))
  118.         ::TextMode(grayishTextOr);  // this is so light you cant see it.
  119.  
  120.     CButton::DrawButtonTitle();
  121. }
  122.  
  123. // Since we have constant heights for eMode_TextOnly and eMode_IconOnly,
  124. // we need to ignore mTitlePadPixels and mGraphicPadPixels
  125.  
  126. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  127. //    Ñ    CalcTitleFrame
  128. //
  129. //    This calculates the bounding box of the title (if any).  This is useful
  130. //    for both the string placement, as well as position the button graphic
  131. //    (again, if any).
  132. //
  133. //    Note that this routine sets the text traits for the ensuing draw.  If
  134. //    you override this method, make sure that you're doing the same.
  135. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  136.  
  137. void CToolbarButton::CalcTitleFrame(void)
  138. {
  139.     if (mTitle.Length() == 0)
  140.         return;
  141.  
  142.     UTextTraits::SetPortTextTraits(mTitleTraitsID);
  143.  
  144.     FontInfo theInfo;
  145.     ::GetFontInfo(&theInfo);
  146.     mCachedTitleFrame.top = mCachedButtonFrame.top;
  147.     mCachedTitleFrame.left = mCachedButtonFrame.left;        
  148.     mCachedTitleFrame.right = mCachedTitleFrame.left + ::StringWidth(mTitle);;
  149.     mCachedTitleFrame.bottom = mCachedTitleFrame.top + theInfo.ascent + theInfo.descent + theInfo.leading;;
  150.  
  151.     if (mCurrentMode != eMode_TextOnly)
  152.     {
  153.         UGraphicGizmos::AlignRectOnRect(mCachedTitleFrame, mCachedButtonFrame, mTitleAlignment);
  154.         UGraphicGizmos::PadAlignedRect(mCachedTitleFrame, mTitlePadPixels, mTitleAlignment);
  155.     }
  156.     else
  157.     {
  158.         UGraphicGizmos::CenterRectOnRect(mCachedTitleFrame, mCachedButtonFrame);
  159.     }
  160. }
  161.