Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_COCOA_GRADIENT_BUTTON_CELL_H_
      6 #define CHROME_BROWSER_UI_COCOA_GRADIENT_BUTTON_CELL_H_
      7 
      8 #import <Cocoa/Cocoa.h>
      9 
     10 #include "base/mac/scoped_nsobject.h"
     11 
     12 namespace ui {
     13 class ThemeProvider;
     14 }
     15 
     16 // Base class for button cells for toolbar and bookmark bar.
     17 //
     18 // This is a button cell that handles drawing/highlighting of buttons.
     19 // The appearance is determined by setting the cell's tag (not the
     20 // view's) to one of the constants below (ButtonType).
     21 
     22 // Set this as the cell's tag.
     23 enum {
     24   kLeftButtonType = -1,
     25   kLeftButtonWithShadowType = -2,
     26   kStandardButtonType = 0,
     27   kRightButtonType = 1,
     28   kMiddleButtonType = 2,
     29   // Draws like a standard button, except when clicked where the interior
     30   // doesn't darken using the theme's "pressed" gradient. Instead uses the
     31   // normal un-pressed gradient.
     32   kStandardButtonTypeWithLimitedClickFeedback = 3,
     33 };
     34 typedef NSInteger ButtonType;
     35 
     36 namespace gradient_button_cell {
     37 
     38 // Pulsing state for this button.
     39 typedef enum {
     40   // Stable states.
     41   kPulsedOn,
     42   kPulsedOff,
     43   // In motion which will end in a stable state.
     44   kPulsingOn,
     45   kPulsingOff,
     46   // In continuous motion.
     47   kPulsingContinuous,
     48 } PulseState;
     49 
     50 };
     51 
     52 
     53 @interface GradientButtonCell : NSButtonCell {
     54  @private
     55   // Custom drawing means we need to perform our own mouse tracking if
     56   // the cell is setShowsBorderOnlyWhileMouseInside:YES.
     57   BOOL isMouseInside_;
     58   base::scoped_nsobject<NSTrackingArea> trackingArea_;
     59   BOOL shouldTheme_;
     60   CGFloat hoverAlpha_;  // 0-1. Controls the alpha during mouse hover
     61   NSTimeInterval lastHoverUpdate_;
     62   base::scoped_nsobject<NSGradient> gradient_;
     63   gradient_button_cell::PulseState pulseState_;
     64   CGFloat pulseMultiplier_;  // for selecting pulse direction when continuous.
     65   CGFloat outerStrokeAlphaMult_;  // For pulsing.
     66   base::scoped_nsobject<NSImage> overlayImage_;
     67 }
     68 
     69 // Turn off theming.  Temporary work-around.
     70 - (void)setShouldTheme:(BOOL)shouldTheme;
     71 
     72 - (void)drawBorderAndFillForTheme:(ui::ThemeProvider*)themeProvider
     73                       controlView:(NSView*)controlView
     74                         innerPath:(NSBezierPath*)innerPath
     75               showClickedGradient:(BOOL)showClickedGradient
     76             showHighlightGradient:(BOOL)showHighlightGradient
     77                        hoverAlpha:(CGFloat)hoverAlpha
     78                            active:(BOOL)active
     79                         cellFrame:(NSRect)cellFrame
     80                   defaultGradient:(NSGradient*)defaultGradient;
     81 
     82 // Let the view know when the mouse moves in and out. A timer will update
     83 // the current hoverAlpha_ based on these events.
     84 - (void)setMouseInside:(BOOL)flag animate:(BOOL)animate;
     85 
     86 // Gets the path which tightly bounds the outside of the button. This is needed
     87 // to produce images of clear buttons which only include the area inside, since
     88 // the background of the button is drawn by someone else.
     89 - (NSBezierPath*)clipPathForFrame:(NSRect)cellFrame
     90                            inView:(NSView*)controlView;
     91 
     92 // Turn on or off continuous pulsing.  When turning off continuous
     93 // pulsing, leave our pulse state in the correct ending position for
     94 // our isMouseInside_ property.  Public since it's called from the
     95 // bookmark bubble.
     96 - (void)setIsContinuousPulsing:(BOOL)continuous;
     97 
     98 // Returns continuous pulse state.
     99 - (BOOL)isContinuousPulsing;
    100 
    101 // Safely stop continuous pulsing by turning off all timers.
    102 // May leave the cell in an odd state.
    103 // Needed by an owning control's dealloc routine.
    104 - (void)safelyStopPulsing;
    105 
    106 // Actually fetches current mouse position and does a hit test.
    107 - (BOOL)isMouseReallyInside;
    108 
    109 // Defines the top offset of text within the cell. Used by drawTitle and can
    110 // be overriden by objects that inherit this class for placement of text.
    111 - (int)verticalTextOffset;
    112 
    113 @property(assign, nonatomic) CGFloat hoverAlpha;
    114 
    115 // An image that will be drawn after the normal content of the button cell,
    116 // overlaying it.  Never themed.
    117 @property(retain, nonatomic) NSImage* overlayImage;
    118 
    119 @end
    120 
    121 @interface GradientButtonCell(TestingAPI)
    122 - (BOOL)isMouseInside;
    123 - (BOOL)pulsing;
    124 - (gradient_button_cell::PulseState)pulseState;
    125 - (void)setPulseState:(gradient_button_cell::PulseState)pstate;
    126 @end
    127 
    128 #endif  // CHROME_BROWSER_UI_COCOA_GRADIENT_BUTTON_CELL_H_
    129