Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 // WebThemeControlDRTWin implements the generic rendering of controls
     32 // needed by WebThemeEngineDRTWin. See the comments in that class
     33 // header file for why this class is needed and used.
     34 //
     35 // This class implements a generic set of widgets using Skia. The widgets
     36 // are optimized for testability, not a pleasing appearance.
     37 //
     38 
     39 #ifndef WebThemeControlDRTWin_h
     40 #define WebThemeControlDRTWin_h
     41 
     42 #include "skia/ext/platform_canvas.h"
     43 #include "third_party/skia/include/core/SkColor.h"
     44 #include <wtf/Noncopyable.h>
     45 
     46 // Skia forward declarations
     47 struct SkIRect;
     48 
     49 class WebThemeControlDRTWin {
     50     WTF_MAKE_NONCOPYABLE(WebThemeControlDRTWin);
     51 public:
     52     // This list of states mostly mirrors the list in WebCore/platform/ThemeTypes.h
     53     // but is maintained separately since that isn't public and also to minimize
     54     // dependencies.
     55     // Note that the WebKit ThemeTypes seem to imply that a control can be
     56     // in multiple states simultaneously but WebThemeEngine only allows for
     57     // a single state at a time.
     58     //
     59     // Some definitions for the various states:
     60     //   Disabled - indicates that a control can't be modified or selected
     61     //              (corresponds to HTML 'disabled' attribute)
     62     //   ReadOnly - indicates that a control can't be modified but can be
     63     //              selected
     64     //   Normal   - the normal state of control on the page when it isn't
     65     //              focused or otherwise active
     66     //   Hot      - when the mouse is hovering over a part of the control,
     67     //              all the other parts are considered "hot"
     68     //   Hover    - when the mouse is directly over a control (the CSS
     69     //               :hover pseudo-class)
     70     //   Focused  - when the control has the keyboard focus
     71     //   Pressed  - when the control is being triggered (by a mousedown or
     72     //              a key event).
     73     //   Indeterminate - when set to indeterminate (only for progress bar)
     74     enum State {
     75         UnknownState = 0,
     76         DisabledState,
     77         ReadOnlyState,
     78         NormalState,
     79         HotState,
     80         HoverState,
     81         FocusedState,
     82         PressedState,
     83         IndeterminateState
     84     };
     85 
     86     // This list of types mostly mirrors the list in
     87     // WebCore/platform/ThemeTypes.h but is maintained
     88     // separately since that isn't public and also to minimize dependencies.
     89     //
     90     // Note that what the user might think of as a single control can be
     91     // made up of multiple parts. For example, a single scroll bar contains
     92     // six clickable parts - two arrows, the "thumb" indicating the current
     93     // position on the bar, the other two parts of the bar (before and after
     94     // the thumb) and the "gripper" on the thumb itself.
     95     //
     96     enum Type {
     97         UnknownType = 0,
     98         TextFieldType,
     99         PushButtonType,
    100         UncheckedBoxType,
    101         CheckedBoxType,
    102         IndeterminateCheckboxType,
    103         UncheckedRadioType,
    104         CheckedRadioType,
    105         HorizontalScrollTrackBackType,
    106         HorizontalScrollTrackForwardType,
    107         HorizontalScrollThumbType,
    108         HorizontalScrollGripType,
    109         VerticalScrollTrackBackType,
    110         VerticalScrollTrackForwardType,
    111         VerticalScrollThumbType,
    112         VerticalScrollGripType,
    113         LeftArrowType,
    114         RightArrowType,
    115         UpArrowType,
    116         DownArrowType,
    117         HorizontalSliderTrackType,
    118         HorizontalSliderThumbType,
    119         DropDownButtonType,
    120         ProgressBarType
    121     };
    122 
    123     // Constructs a control of the given size, type and state to draw
    124     // on to the given canvas.
    125     WebThemeControlDRTWin(SkCanvas*, const SkIRect&, Type, State);
    126     ~WebThemeControlDRTWin();
    127 
    128     // Draws the control.
    129     void draw();
    130 
    131     // Use this for TextField controls instead, because the logic
    132     // for drawing them is dependent on what WebKit tells us to do.
    133     // If drawEdges is true, draw an edge around the control. If
    134     // fillContentArea is true, fill the content area with the given color.
    135     void drawTextField(bool drawEdges, bool fillContentArea, SkColor color);
    136 
    137     // Use this for drawing ProgressBar controls instead, since we
    138     // need to know the rect to fill inside the bar.
    139     void drawProgressBar(const SkIRect& fillRect);
    140 
    141 private:
    142     // Draws a box of size specified by irect, filled with the given color.
    143     // The box will have a border drawn in the default edge color.
    144     void box(const SkIRect& irect, SkColor color);
    145 
    146 
    147     // Draws a triangle of size specified by the three pairs of coordinates,
    148     // filled with the given color. The box will have an edge drawn in the
    149     // default edge color.
    150     void triangle(int x0, int y0, int x1, int y1, int x2, int y2, SkColor color);
    151 
    152     // Draws a rectangle the size of the control with rounded corners, filled
    153     // with the specified color (and with a border in the default edge color).
    154     void roundRect(SkColor color);
    155 
    156     // Draws an oval the size of the control, filled with the specified color
    157     // and with a border in the default edge color.
    158     void oval(SkColor color);
    159 
    160     // Draws a circle centered in the control with the specified radius,
    161     // filled with the specified color, and with a border draw in the
    162     // default edge color.
    163     void circle(SkScalar radius, SkColor color);
    164 
    165     // Draws a box the size of the control, filled with the outerColor and
    166     // with a border in the default edge color, and then draws another box
    167     // indented on all four sides by the specified amounts, filled with the
    168     // inner color and with a border in the default edge color.
    169     void nestedBoxes(int indentLeft,
    170                      int indentTop,
    171                      int indentRight,
    172                      int indentBottom,
    173                      SkColor outerColor,
    174                      SkColor innerColor);
    175 
    176     // Draws a line between the two points in the given color.
    177     void line(int x0, int y0, int x1, int y1, SkColor color);
    178 
    179     // Draws a distinctive mark on the control for each state, so that the
    180     // state of the control can be determined without needing to know which
    181     // color is which.
    182     void markState();
    183 
    184     SkCanvas* m_canvas;
    185     const SkIRect m_irect;
    186     const Type m_type;
    187     const State m_state;
    188     const SkColor m_edgeColor;
    189     const SkColor m_bgColor;
    190     const SkColor m_fgColor;
    191 
    192     // The following are convenience accessors for m_irect.
    193     const int m_left;
    194     const int m_right;
    195     const int m_top;
    196     const int m_bottom;
    197     const int m_width;
    198     const int m_height;
    199 };
    200 
    201 #endif // WebThemeControlDRTWin_h
    202