home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CGAStatusBar.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  10.6 KB  |  345 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 "CGAStatusBar.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.  
  40. /*====================================================================================*/
  41.     #pragma mark INTERNAL CLASS DECLARATIONS
  42. /*====================================================================================*/
  43.  
  44.  
  45. /*====================================================================================*/
  46.     #pragma mark INTERNAL FUNCTION PROTOTYPES
  47. /*====================================================================================*/
  48.  
  49.  
  50. /*====================================================================================*/
  51.     #pragma mark CLASS IMPLEMENTATIONS
  52. /*====================================================================================*/
  53.  
  54. #pragma mark -
  55.  
  56. /*======================================================================================
  57.     Constructor.
  58. ======================================================================================*/
  59.  
  60. CGAStatusBar::CGAStatusBar(LStream *inStream) :
  61.                            LGABox(inStream),
  62.                            mIndefiniteProgessPatH(nil) {
  63.     
  64.     mValue = eHiddenValue;
  65.     mHasBorder = true;
  66.     mBorderStyle = borderStyleGA_RecessedOneBorder;
  67.     mTitlePosition = titlePositionGA_None;
  68.     mCurIndefiniteIndex = 0;
  69.     mNextIndefiniteDraw = 0;
  70.     Assert_((mUserCon < 0) || ((mUserCon <= 100) && (mUserCon >= 0)));
  71. }
  72.  
  73.  
  74. /*======================================================================================
  75.     Destructor.
  76. ======================================================================================*/
  77.  
  78. CGAStatusBar::~CGAStatusBar(void) {
  79.     
  80.     if ( mIndefiniteProgessPatH != nil ) {
  81.         ::DisposePixPat(mIndefiniteProgessPatH);
  82.         mIndefiniteProgessPatH = nil;
  83.     }
  84. }
  85.  
  86.  
  87. /*======================================================================================
  88.     Get the status title.
  89. ======================================================================================*/
  90.  
  91. StringPtr CGAStatusBar::GetDescriptor(Str255 outDescriptor) const {
  92.  
  93.     return inherited::GetDescriptor(outDescriptor);
  94. }
  95.  
  96.  
  97. /*======================================================================================
  98.     Set the status title.
  99. ======================================================================================*/
  100.  
  101. void CGAStatusBar::SetDescriptor(ConstStr255Param inDescriptor) {
  102.  
  103.     if ( mTitle != inDescriptor ) {
  104.         if ( FocusExposed() ) {
  105.             Rect titleRect1, titleRect2;
  106.             CalcTitleRect(titleRect1);
  107.             mTitle = inDescriptor;
  108.             CalcTitleRect(titleRect2);
  109.             ::PenNormal();
  110.             ApplyForeAndBackColors();
  111.             if ( titleRect1.right > (titleRect2.right - 1) ) {
  112.                 titleRect1.left = titleRect2.right - 1;
  113.                 ::EraseRect(&titleRect1);
  114.             }
  115.             DrawBoxTitle();
  116.         } else {
  117.             mTitle = inDescriptor;
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. /*======================================================================================
  124.     Get the current progress value (0..100) or eIndefiniteValue.
  125. ======================================================================================*/
  126.  
  127. Int32 CGAStatusBar::GetValue(void) const {
  128.  
  129.     return mValue;
  130. }
  131.  
  132.  
  133. /*======================================================================================
  134.     Set the current progress value (0..100) or eIndefiniteValue.
  135. ======================================================================================*/
  136.  
  137. void CGAStatusBar::SetValue(Int32 inValue) {
  138.  
  139.     if ( inValue == eIndefiniteValue ) {
  140.         if ( mNextIndefiniteDraw < LMGetTicks() ) {
  141.             if ( ++mCurIndefiniteIndex > eMaxIndefiniteIndex ) {
  142.                 mCurIndefiniteIndex = 0;
  143.             }
  144.         } else {
  145.             mValue = inValue;
  146.             return;
  147.         }
  148.     } else if ( inValue != eHiddenValue ) {
  149.         if ( inValue < eMinValue ) {
  150.             inValue = eMinValue;
  151.         } else if ( inValue > eMaxValue ) {
  152.             inValue = eMaxValue;
  153.         }
  154.         if ( mValue == inValue ) return;
  155.     } else {
  156.         if ( mValue != inValue ) {
  157.             mValue = inValue;
  158.             Refresh();
  159.         }
  160.         return;
  161.     }
  162.     
  163.     mValue = inValue;
  164.     if ( FocusExposed() ) DrawBoxBorder();
  165.     if ( mValue == eIndefiniteValue ) mNextIndefiniteDraw = LMGetTicks() + 2;
  166. }
  167.  
  168.  
  169. /*======================================================================================
  170.     Draw the b&w progress bar.
  171. ======================================================================================*/
  172.  
  173. void CGAStatusBar::DrawBoxTitle(void) {
  174.  
  175.     StTextState theTextState;
  176.     StColorPenState theColorPenState;
  177.     Rect titleRect;
  178.     CalcTitleRect(titleRect);
  179.     
  180.     UTextTraits::SetPortTextTraits(GetTextTraitsID());
  181.     
  182.     Str255 boxTitle;
  183.     GetDescriptor(boxTitle);
  184.     
  185.     {
  186.         RGBColor textColor;
  187.         ::GetForeColor(&textColor);
  188.         ApplyForeAndBackColors();
  189.         ::RGBForeColor(&textColor);
  190.     }
  191.     
  192.     StClipRgnState clipState(titleRect);
  193.     
  194.     UStringDrawing::DrawJustifiedString(boxTitle, titleRect, teJustCenter );
  195. }
  196.  
  197.  
  198. /*======================================================================================
  199.     Draw the b&w progress bar.
  200. ======================================================================================*/
  201.  
  202. void CGAStatusBar::DrawBlackAndWhiteBorder(const Rect &inBorderRect,
  203.                                             EGABorderStyle inBorderStyle) {
  204.  
  205.     #pragma unused(inBorderStyle)
  206.     
  207.     if ( mValue == eHiddenValue ) return;
  208.     
  209.     DrawProgressBar(inBorderRect, false);
  210. }
  211.  
  212.  
  213. /*======================================================================================
  214.     Draw the color progress bar.
  215. ======================================================================================*/
  216.  
  217. void CGAStatusBar::DrawColorBorder(const Rect &inBorderRect,
  218.                                    EGABorderStyle inBorderStyle) {
  219.  
  220.     #pragma unused(inBorderStyle)
  221.     
  222.     if ( mValue == eHiddenValue ) return;
  223.     
  224.     inherited::DrawColorBorder(inBorderRect, inBorderStyle);
  225.     
  226.     DrawProgressBar(inBorderRect, true);
  227. }
  228.  
  229.  
  230. /*======================================================================================
  231.     Draw the progress bar.
  232. ======================================================================================*/
  233.  
  234. void CGAStatusBar::DrawProgressBar(const Rect &inBorderRect, Boolean inUseColor) {
  235.  
  236.     StColorPenState::Normalize();
  237.     Rect drawRect = inBorderRect;
  238.  
  239.     ::InsetRect(&drawRect, 1, 1);
  240.     ::FrameRect(&drawRect);
  241.     ::InsetRect((Rect *) &drawRect, 1, 1);
  242.  
  243.     if ( mValue != eIndefiniteValue ) {
  244.         Int16 mid = drawRect.left + (((drawRect.right - drawRect.left) * mValue) / eMaxValue);
  245.         Int16 right = drawRect.right;
  246.  
  247.         drawRect.right = mid;
  248.         if ( inUseColor ) {
  249.             ::RGBForeColor(&UGAColorRamp::GetColor(11));
  250.         }
  251.         ::PaintRect(&drawRect);
  252.         drawRect.right = right;
  253.         drawRect.left = mid;
  254.         if ( inUseColor ) {
  255.             ::RGBForeColor(&UGAColorRamp::GetColor(colorRamp_Purple1));
  256.             ::PaintRect(&drawRect);
  257.             ::ForeColor(blackColor);
  258.         } else {
  259.             ::EraseRect(&drawRect);
  260.         }
  261.     } else {
  262.         if ( mIndefiniteProgessPatH == nil ) {
  263.             mIndefiniteProgessPatH = ::GetPixPat(mTextTraitsID);
  264.         }
  265.         if ( mIndefiniteProgessPatH != nil ) {
  266.             Point saveOrigin = topLeft(GetMacPort()->portRect);
  267.             ::SetOrigin(saveOrigin.h - mCurIndefiniteIndex, saveOrigin.v);
  268.             ::OffsetRgn(GetMacPort()->clipRgn, -mCurIndefiniteIndex, 0);
  269.             ::OffsetRect(&drawRect, -mCurIndefiniteIndex, 0);
  270.             ::PenPixPat(mIndefiniteProgessPatH);
  271.             ::PaintRect(&drawRect);
  272.             StColorPenState::Normalize();
  273.             ::OffsetRgn(GetMacPort()->clipRgn, mCurIndefiniteIndex, 0);
  274.             ::SetOrigin(saveOrigin.h, saveOrigin.v);
  275.         }
  276.     }
  277. }
  278.  
  279.  
  280. /*======================================================================================
  281.     Calculate the rectagle used for drawing the status bar.
  282. ======================================================================================*/
  283.  
  284. static const Int16 cStatusBarHeight = 12;
  285. static const Int16 cStatusBarTitleMargin = 5;
  286.  
  287. void CGAStatusBar::CalcBorderRect(Rect &outRect) {
  288.  
  289.     CalcLocalFrameRect(outRect);
  290.     
  291.     if ( mValue == eHiddenValue ) {
  292.         outRect.right = outRect.left;
  293.         outRect.bottom = outRect.top;
  294.         return;
  295.     } else if ( mUserCon < 0 ) {
  296.         outRect.left = outRect.right + mUserCon;
  297.     } else {
  298.         const Int32 width = outRect.right - outRect.left;
  299.         outRect.left = outRect.right - ((width * mUserCon) / 100L);
  300.     }
  301.     
  302.     const Int32 height = outRect.bottom - outRect.top;
  303.     outRect.top += (height - cStatusBarHeight) / 2;
  304.     outRect.bottom = outRect.top + cStatusBarHeight;
  305. }
  306.  
  307.  
  308. /*======================================================================================
  309.     Nothing.
  310. ======================================================================================*/
  311.  
  312. void CGAStatusBar::CalcContentRect(Rect &outRect) {
  313.  
  314.     CalcLocalFrameRect(outRect);
  315.     ::SetRect(&outRect, outRect.left, outRect.top, outRect.left, outRect.top);
  316. }
  317.  
  318.  
  319. /*======================================================================================
  320.     Calculate the rectangle for drawing the status text.
  321. ======================================================================================*/
  322.  
  323. void CGAStatusBar::CalcTitleRect(Rect &outRect) {
  324.  
  325.     CalcLocalFrameRect(outRect);
  326.     
  327.     Int16 txHeight, txWidth;
  328.     CalculateTitleHeightAndWidth(&txHeight, &txWidth);
  329.     
  330.     if ( mUserCon < 0 ) {
  331.         outRect.right += mUserCon;
  332.     } else {
  333.         const Int32 width = outRect.right - outRect.left;
  334.         outRect.right -= ((width * mUserCon) / 100L);
  335.     }
  336.     outRect.right -= cStatusBarTitleMargin;
  337.     
  338.     if ( outRect.right < outRect.left ) outRect.right = outRect.left;
  339.     if ( (outRect.right - outRect.left) > txWidth ) {
  340.         outRect.right = outRect.left + txWidth;
  341.     }
  342. }
  343.  
  344.  
  345.